Example usage for org.apache.commons.math3.linear RealVector getDimension

List of usage examples for org.apache.commons.math3.linear RealVector getDimension

Introduction

In this page you can find the example usage for org.apache.commons.math3.linear RealVector getDimension.

Prototype

public abstract int getDimension();

Source Link

Document

Returns the size of the vector.

Usage

From source file:org.lenskit.transform.quantize.ValueArrayQuantizer.java

public ValueArrayQuantizer(RealVector vs) {
    Preconditions.checkArgument(vs.getDimension() > 0, "must have at least one value");
    values = new ArrayRealVector(vs);
}

From source file:org.meteoinfo.math.linalg.LinalgUtil.java

/**
 * Calculates the eigen decomposition of a real matrix.
 * The eigen decomposition of matrix A is a set of two matrices: V and D such that 
 * A = V  D  VT. A, V and D are all m  m matrices.
 * @param a Given matrix.//from w w  w.ja v a2  s  .  c  o  m
 * @return Result W/V arrays.
 */
public static Array[] eigen(Array a) {
    int m = a.getShape()[0];
    Array Wa;
    Array Va = Array.factory(DataType.DOUBLE, new int[] { m, m });
    double[][] aa = (double[][]) ArrayUtil.copyToNDJavaArray(a);
    RealMatrix matrix = new Array2DRowRealMatrix(aa, false);
    EigenDecomposition decomposition = new EigenDecomposition(matrix);
    double[] rev = decomposition.getRealEigenvalues();
    double[] iev = decomposition.getImagEigenvalues();
    if (decomposition.hasComplexEigenvalues()) {
        Wa = Array.factory(DataType.OBJECT, new int[] { m });
        for (int i = 0; i < m; i++) {
            Wa.setObject(i, new Complex(rev[i], iev[i]));
            RealVector v = decomposition.getEigenvector(i);
            for (int j = 0; j < v.getDimension(); j++) {
                Va.setDouble(i * m + j, v.getEntry(j));
            }
        }
    } else {
        Wa = Array.factory(DataType.DOUBLE, new int[] { m });
        for (int i = 0; i < m; i++) {
            Wa.setDouble(i, rev[i]);
            RealVector v = decomposition.getEigenvector(i);
            for (int j = 0; j < v.getDimension(); j++) {
                Va.setDouble(i * m + j, v.getEntry(j));
            }
        }
    }

    return new Array[] { Wa, Va };
}

From source file:org.opentestsystem.airose.regression.orderedprobit.LinearEquationSolver.java

public static double[][] SolveLinear(double[][] XpX, double[][] XpY) {

    RealMatrix coefficients = new Array2DRowRealMatrix(XpX, false);
    LUDecomposition luDecomposition = new LUDecomposition(coefficients);

    DecompositionSolver solver = luDecomposition.getSolver();

    double[] yRowVector = new double[XpY.length];
    for (int counter1 = 0; counter1 < XpY.length; ++counter1) {
        yRowVector[counter1] = XpY[counter1][0];
    }/* www. ja  va  2 s .  co  m*/

    RealVector constants = new ArrayRealVector(yRowVector, false);
    RealVector solution = solver.solve(constants);

    double[][] result = new double[solution.getDimension()][1];
    for (int counter1 = 0; counter1 < solution.getDimension(); ++counter1) {
        result[counter1][0] = solution.getEntry(counter1);
    }
    return result;
}

From source file:org.rhwlab.BHC.LogNode.java

public String vectorAsString(RealVector v) {
    boolean first = true;
    StringBuilder builder = new StringBuilder();
    builder.append("(");
    for (int i = 0; i < v.getDimension(); ++i) {

        if (!first) {
            builder.append(",");
        }/*from   www  .  j a v  a2  s.  c  o  m*/
        builder.append(String.format("%.2f", v.getEntry(i)));
        first = false;
    }
    builder.append(")");
    return builder.toString();
}

From source file:org.rhwlab.BHCnotused.Algorithm.java

public void init(int seg) {
    clusters = new ArrayList<>();
    for (int n = 0; n < source.getClusterCount(); ++n) {
        RealVector v = source.getCenter(n);
        Dfp[] z = new Dfp[v.getDimension()];
        for (int i = 0; i < z.length; ++i) {
            z[i] = field.newDfp(v.getEntry(i));
        }//from www  .  ja v a  2s .c  om
        LabeledFieldVector fv = new LabeledFieldVector(z, n);
        Cluster cluster = new Cluster(new GaussianGIWPrior(fv));
        clusters.add(cluster);
    }
    pairs = new HashMap<>();
    for (int i = 0; i < clusters.size() - 1; ++i) {
        Cluster clusterI = clusters.get(i);
        HashMap<Cluster, Cluster> map = new HashMap<>();
        for (int j = i + 1; j < clusters.size(); ++j) {
            Cluster clusterJ = clusters.get(j);
            Cluster T = new Cluster(clusterI, clusterJ);
            map.put(clusterJ, T);
        }
        pairs.put(clusterI, map);
    }
}

From source file:org.rhwlab.BHCnotused.GaussianGIWPrior.java

static public void main(String[] args) throws Exception {
    System.out.println("GaussianGIWPrior");
    field = new DfpField(20); // 20 decimal digits
    Cluster.setDfpField(field);/*from   www . ja  v  a  2  s .co  m*/
    ThreadedAlgorithm.setDfpField(field);
    GaussianGIWPrior.setDfpField(field);

    //        SegmentedTiffDataSource source = new SegmentedTiffDataSource("/nfs/waterston/pete/Segmentation/Cherryimg75.tif",
    //                "/nfs/waterston/pete/Segmentation/Cherryimg75_SimpleSegmentation.tiff",1);  // background is seg=1
    //       source = new MicroClusterDataSource("/nfs/waterston/pete/Segmentation/Cherryimg_SimpleSegmentationMulti0350.xml");
    source = new MicroClusterDataSource(
            "/nfs/waterston/pete/Segmentation/Cherryimg_SimpleSegmentation0075.save");
    RealVector mean = source.getDataMean();
    Dfp[] mu = new Dfp[mean.getDimension()];
    for (int i = 0; i < mu.length; ++i) {
        mu[i] = field.newDfp(mean.getEntry(i));
    }
    GaussianGIWPrior.setParameters(4.0, 0.001, mu, 200.0);
    Cluster.setAlpha(30);
    ThreadedAlgorithm alg = new ThreadedAlgorithm();
    alg.setSource(source);
    alg.init(2);
    alg.run();
    alg.saveResultAsXML("/nfs/waterston/pete/Segmentation/Cherryimg_SimpleSegmentation0075_BHC.xml");
    int hfuis = 0;
}

From source file:org.rhwlab.BHCnotused.StdGaussianWishartGaussian.java

private void printVector(PrintStream stream, RealVector v) {
    boolean first = true;
    stream.print("(");
    for (int i = 0; i < v.getDimension(); ++i) {
        if (!first) {
            stream.printf(",%d", (int) v.getEntry(i));
        } else {//ww w  .  jav a 2 s .  c  o m
            stream.printf("%d", (int) v.getEntry(i));
        }
        first = false;
    }
    stream.print(")");
}

From source file:org.rhwlab.BHCnotused.ThreadedAlgorithm.java

public void init(int seg) throws Exception {
    clusters = new ArrayList<>();
    // build the initial clusters with one data point in each cluster
    for (int n = 0; n < source.getK(); ++n) {
        RealVector v = source.getCenter(n);
        Dfp[] z = new Dfp[v.getDimension()];
        for (int i = 0; i < z.length; ++i) {
            z[i] = field.newDfp(v.getEntry(i));
        }//from w w  w .  j  ava  2s  .c  o m
        LabeledFieldVector fv = new LabeledFieldVector(z, n);
        Cluster cluster = new Cluster(new GaussianGIWPrior(fv));
        clusters.add(cluster);
    }

    // make all possible pairings of initial clusters
    pairs = new HashMap<>();
    for (int i = 0; i < clusters.size() - 1; ++i) {
        HashMap<Cluster, Cluster> map = new HashMap<>();
        MergeAction merge = new MergeAction(clusters, i, i + 1, map);
        ForkJoinPool pool = new ForkJoinPool();
        pool.invoke(merge);
        Cluster clusterI = clusters.get(i);
        pairs.put(clusterI, map);
        System.out.printf("Cluster %d paired\n", i);
    }
}

From source file:org.rhwlab.dispim.datasource.GaussianComponent.java

public RealMatrix precision(RealVector mu) {
    RealMatrix ret = new Array2DRowRealMatrix(mu.getDimension(), mu.getDimension());
    ret.scalarMultiply(0.0); // make sure it is zero
    int n = 0;//  ww w  . j  av  a 2 s.  c  o  m
    for (Integer index : indexes) {
        RealVector x = source.get(index).coords;
        RealVector del = x.subtract(mu);
        ret = ret.add(del.outerProduct(del));
        ++n;
    }

    ret = ret.scalarMultiply(1.0 / n);
    LUDecomposition lud = new LUDecomposition(ret);
    RealMatrix prec = lud.getSolver().getInverse();
    return prec;
}

From source file:org.rhwlab.dispim.datasource.MicroCluster.java

License:asdf

public static RealVector mean(List<MicroCluster> data) {
    if (data.isEmpty()) {
        return null;
    }//from  www  . j  a  va  2  s  . c  o  m
    RealVector first = data.get(0).asRealVector();
    long n = 0;
    long[] mu = new long[first.getDimension()];
    for (MicroCluster micro : data) {
        for (int p = 0; p < micro.points.length; ++p) {
            for (int d = 0; d < mu.length; ++d) {
                mu[d] = mu[d] + micro.points[p][d];
            }
            ++n;
        }
    }
    RealVector ret = new ArrayRealVector(first.getDimension());
    for (int d = 0; d < mu.length; ++d) {
        ret.setEntry(d, (double) mu[d] / (double) n);
    }
    return ret;
}