com.ibm.bluej.commonutil.DenseVectors.java Source code

Java tutorial

Introduction

Here is the source code for com.ibm.bluej.commonutil.DenseVectors.java

Source

/*
Copyright (c) 2012 IBM Corp.
    
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    
http://www.apache.org/licenses/LICENSE-2.0
    
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.ibm.bluej.commonutil;

import java.util.*;

import org.apache.commons.math.stat.correlation.*;

public class DenseVectors {
    public static double mean(double[] v) {
        double sum = 0;
        for (double vi : v) {
            sum += vi;
        }
        return sum / v.length;
    }

    public static int maxIndex(double[] v) {
        int maxNdx = 0;
        double max = Double.NEGATIVE_INFINITY;
        for (int i = 0; i < v.length; ++i) {
            if (v[i] > max) {
                max = v[i];
                maxNdx = i;
            }
        }
        return maxNdx;
    }

    public static double stdDev(double[] v, double m) {
        double sum = 0;
        for (double vi : v) {
            sum += (vi - m) * (vi - m);
        }
        return Math.sqrt(sum / v.length);
    }

    public static HashMap<Integer, MutableDouble> toSparse(double[] v) {
        HashMap<Integer, MutableDouble> s = new HashMap<Integer, MutableDouble>();
        for (int vi = 0; vi < v.length; ++vi) {
            s.put(vi, new MutableDouble(v[vi]));
        }
        return s;
    }

    public static double[] extend(double[] v1, double... add) {
        double[] ve = new double[v1.length + add.length];
        System.arraycopy(v1, 0, ve, 0, v1.length);
        System.arraycopy(add, 0, ve, v1.length, add.length);
        return ve;
    }

    public static double pearsonsR(double[] x, double[] y) {
        PearsonsCorrelation pc = new PearsonsCorrelation();
        double corrValue = pc.correlation(x, y);

        if (new Double(corrValue).isNaN())
            corrValue = 0.0;

        return corrValue;
    }

    /*
    public static double pearsonsR(double[] v1, double[] v2) {
       double m1 = mean(v1);
       double m2 = mean(v2);
       double s1 = stdDev(v1,m1)+0.0000001;
       double s2 = stdDev(v2,m2)+0.0000001;
       double sum = 0;
       for (int i = 0; i < v1.length; ++i) {
     sum += ((v1[i]-m1)/s1) * ((v2[i]-m2)/s2);
       }
       return (1.0/(v1.length - 1)) * sum;
    }
    */
    public static void addTo(double[] v1, double[] v2) {
        for (int i = 0; i < v1.length; ++i) {
            v1[i] += v2[i];
        }
    }

    public static void scale(double[] v, double s) {
        for (int i = 0; i < v.length; ++i) {
            v[i] *= s;
        }
    }

    public static double cosine(double[] v1, double[] v2) {
        double numer = 0;
        double len1 = 0;
        double len2 = 0;
        for (int i = 0; i < v1.length; ++i) {
            double d1 = v1[i];
            double d2 = v2[i];
            numer += d1 * d2;
            len2 += d2 * d2;
            len1 += d1 * d1;
        }
        double length = Math.sqrt(len1 * len2);
        if (length == 0) {
            return 0;
        }

        double sim = numer / length;
        return sim;
    }

    public static double dotProduct(double[] v1, double[] v2) {
        double dp = 0;
        for (int i = 0; i < v1.length; ++i) {
            dp += v1[i] * v2[i];
        }
        return dp;
    }

    public static double[] toPrimativeArray(Collection<Double> l) {
        double[] d = new double[l.size()];
        int ndx = 0;
        for (Double di : l) {
            d[ndx++] = di;
        }
        return d;
    }

    public static String toString(double[] v) {
        StringBuffer buf = new StringBuffer();
        buf.append("[");
        for (int i = 0; i < v.length; ++i) {
            if (i != 0) {
                buf.append(", ");
            }
            buf.append(v[i]);
        }
        buf.append("]");
        return buf.toString();
    }

}