List of usage examples for org.apache.commons.math3.linear Array2DRowRealMatrix Array2DRowRealMatrix
public Array2DRowRealMatrix(final double[][] d, final boolean copyArray) throws DimensionMismatchException, NoDataException, NullArgumentException
From source file:lanchester.TestEigen.java
public static Array2DRowRealMatrix getMat(int num) { Array2DRowRealMatrix mat = new Array2DRowRealMatrix(num, num); for (int i1 = 0; i1 < num; i1++) { for (int i2 = 0; i2 < num; i2++) { mat.setEntry(i1, i2, Math.random()); }// w w w .j a v a2 s . com } return mat; }
From source file:com.clust4j.sample.BootstrapTest.java
static Array2DRowRealMatrix toMatrix(final double[][] data) { return new Array2DRowRealMatrix(data, false); }
From source file:com.cloudera.oryx.common.math.MatrixUtilsTest.java
public static RealMatrix multiplyXYT(LongObjectMap<float[]> X, LongObjectMap<float[]> Y) { int Ysize = Y.size(); int Xsize = X.size(); RealMatrix result = new Array2DRowRealMatrix(Xsize, Ysize); for (int row = 0; row < Xsize; row++) { for (int col = 0; col < Ysize; col++) { result.setEntry(row, col, SimpleVectorMath.dot(X.get(row), Y.get(col))); }//ww w .j av a2 s .c o m } return result; }
From source file:com.github.thorbenlindhauer.test.util.LinearAlgebraUtil.java
public static RealMatrix asSingleRowMatrix(double value, int repetitions) { double[] values = new double[repetitions]; for (int i = 0; i < repetitions; i++) { values[i] = value;//from w ww . java 2 s .c o m } RealMatrix matrix = new Array2DRowRealMatrix(1, repetitions); matrix.setRow(0, values); return matrix; }
From source file:com.anhth12.lambda.common.math.VectorMath.java
public static RealMatrix transposeTimesSelf(Collection<float[]> M) { if (M == null || M.isEmpty()) { return null; }//from w w w. ja v a 2s .c o m int features = 0; RealMatrix result = null; for (float[] vector : M) { if (result == null) { features = vector.length; result = new Array2DRowRealMatrix(features, features); } for (int row = 0; row < features; row++) { float rowValue = vector[row]; for (int col = 0; col < features; col++) { result.addToEntry(row, col, rowValue * vector[col]); } } } return result; }
From source file:com.github.thorbenlindhauer.test.util.LinearAlgebraUtil.java
public static RealMatrix asRowMatrix(double... values) { RealMatrix matrix = new Array2DRowRealMatrix(1, values.length); matrix.setRow(0, values);/* w ww . jav a 2 s. c om*/ return matrix; }
From source file:fp.overlapr.algorithmen.StressMajorization.java
/** * Fhrt die Stress-Majorization durch. siehe: Gansner, Koren, North: Graph * Drawing by Stress Majorization, 2004//from w ww. j av a 2 s. c om * * @param graph * Graph, dessen Knoten-Positionen neu berechnet werden sollen * @param d * Matrix, die die idealen Distanzen (d_ij) zwischen den Knoten * enthlt * @return Matrix, die die neuen x- und y-Werte der einzelnen Knoten enthlt */ public static double[][] doStressMajorization(Graph graph, double[][] d) { int iter = 0; // X holen Array2DRowRealMatrix X = new Array2DRowRealMatrix(graph.getKnotenAnzahl(), 2); for (int i = 0; i < graph.getKnotenAnzahl(); i++) { X.setEntry(i, 0, graph.getKnoten().get(i).getX()); X.setEntry(i, 1, graph.getKnoten().get(i).getY()); } // D holen Array2DRowRealMatrix D = new Array2DRowRealMatrix(d); // W berechnen Array2DRowRealMatrix W = new Array2DRowRealMatrix(D.getRowDimension(), D.getColumnDimension()); W.walkInRowOrder(new DefaultRealMatrixChangingVisitor() { @Override public double visit(int row, int column, double value) { if (D.getEntry(row, column) == 0) { return 0.0; } else { return 1.0 / (D.getEntry(row, column) * D.getEntry(row, column)); } } }); // LW berechnen Array2DRowRealMatrix LW = new Array2DRowRealMatrix(D.getRowDimension(), D.getColumnDimension()); LW.walkInRowOrder(new DefaultRealMatrixChangingVisitor() { @Override public double visit(int row, int column, double value) { if (row != column) { return (-1) * W.getEntry(row, column); } else { return value; } } }); LW.walkInRowOrder(new DefaultRealMatrixChangingVisitor() { @Override public double visit(int row, int column, double value) { if (row == column) { double sum = 0; for (int k = 0; k < LW.getColumnDimension(); k++) { if (k != row) { sum = sum + W.getEntry(row, k); } } return sum; } else { return value; } } }); double[][] x = null; while (iter < ITER) { iter++; // LX berechnen Array2DRowRealMatrix LX = new Array2DRowRealMatrix(D.getRowDimension(), D.getColumnDimension()); LX.walkInRowOrder(new DefaultRealMatrixChangingVisitor() { @Override public double visit(int row, int column, double value) { if (row != column) { // norm 2 double term1 = FastMath.pow((X.getEntry(row, 0) - X.getEntry(column, 0)), 2); double term2 = FastMath.pow((X.getEntry(row, 1) - X.getEntry(column, 1)), 2); double abst = Math.sqrt(term1 + term2); return (-1) * W.getEntry(row, column) * D.getEntry(row, column) * inv(abst); } else { return value; } } }); LX.walkInRowOrder(new DefaultRealMatrixChangingVisitor() { @Override public double visit(int row, int column, double value) { if (row == column) { double sum = 0; for (int k = 0; k < LX.getColumnDimension(); k++) { if (k != row) { sum = sum + LX.getEntry(row, k); } } return (-1) * sum; } else { return value; } } }); /* * Lineare Gleichungssysteme lsen */ // x-Werte holen ArrayRealVector xWerte = new ArrayRealVector(X.getColumn(0)); // y-Werte holen ArrayRealVector yWerte = new ArrayRealVector(X.getColumn(1)); // b_x berechnen ArrayRealVector b_x = (ArrayRealVector) LX.operate(xWerte); // b_y berechnen ArrayRealVector b_y = (ArrayRealVector) LX.operate(yWerte); /* * CG-Verfahren anwenden */ // neue x-Werte berechnen mittels PCG // xWerte = conjugateGradientsMethod(LW, b_x, xWerte); // neue y-Werte berechnen mittels PCG // yWerte = conjugateGradientsMethod(LW, b_y, yWerte); ConjugateGradient cg = new ConjugateGradient(Integer.MAX_VALUE, TOL, false); xWerte = (ArrayRealVector) cg.solve(LW, JacobiPreconditioner.create(LW), b_x); yWerte = (ArrayRealVector) cg.solve(LW, JacobiPreconditioner.create(LW), b_y); /* * neue Positiones-Werte zurckgeben */ x = new double[X.getRowDimension()][2]; for (int i = 0; i < x.length; i++) { x[i][0] = xWerte.getEntry(i); x[i][1] = yWerte.getEntry(i); X.setEntry(i, 0, xWerte.getEntry(i)); X.setEntry(i, 1, yWerte.getEntry(i)); } } return x; }
From source file:com.cloudera.oryx.kmeans.common.ClusterValidityStatistics.java
public static ClusterValidityStatistics create(Iterable<WeightedRealVector> points, Centers test, Centers train, int replicaId) { Preconditions.checkArgument(test.size() == train.size()); int k = test.size(); double n = 0.0; double testCost = 0.0; double trainCost = 0.0; RealMatrix contingencyMatrix = new Array2DRowRealMatrix(k, k); double[] rowSums = new double[k]; double[] colSums = new double[k]; for (WeightedRealVector wv : points) { Distance d1 = test.getDistance(wv.thing()); Distance d2 = train.getDistance(wv.thing()); contingencyMatrix.addToEntry(d1.getClosestCenterId(), d2.getClosestCenterId(), wv.weight()); rowSums[d1.getClosestCenterId()] += wv.weight(); colSums[d2.getClosestCenterId()] += wv.weight(); testCost += d1.getSquaredDistance() * wv.weight(); trainCost += d2.getSquaredDistance() * wv.weight(); n += wv.weight();/* w ww. j a v a 2 s.c o m*/ } return new ClusterValidityStatistics(k, replicaId, testCost, trainCost, normVarInformation(contingencyMatrix, rowSums, colSums, n), normVanDongen(contingencyMatrix, rowSums, colSums, n)); }
From source file:com.itemanalysis.psychometrics.factoranalysis.MatrixUtils.java
/** * Elementwise multiplication of elements in two arrays. This is equivalent to * using A*B in R when both A and B are matrices. * * @param A a matrix//from w w w . ja v a 2s . co m * @param B a matrix of the same dimension as A * @return a matrix with elements that are the produce of elements in A and B. * @throws org.apache.commons.math3.exception.DimensionMismatchException */ public static RealMatrix multiplyElements(RealMatrix A, RealMatrix B) throws DimensionMismatchException { int nrow = A.getRowDimension(); int ncol = A.getColumnDimension(); if (nrow != B.getRowDimension()) { throw new DimensionMismatchException(nrow, B.getRowDimension()); } if (ncol != B.getColumnDimension()) { throw new DimensionMismatchException(ncol, B.getColumnDimension()); } RealMatrix M = new Array2DRowRealMatrix(nrow, ncol); for (int i = 0; i < nrow; i++) { for (int j = 0; j < ncol; j++) { M.setEntry(i, j, A.getEntry(i, j) * B.getEntry(i, j)); } } return M; }
From source file:jurls.core.becca.Daisychain.java
public double[] in(double[] cable) { this.cable = cable; double min = Double.POSITIVE_INFINITY, max = Double.NEGATIVE_INFINITY; if ((tp == null) || (tp.getRowDimension() != cable.length)) { tp = new Array2DRowRealMatrix(cable.length, cable.length); // TODO transfer old probability to the new instance }/*from w ww .j av a2s . co m*/ if (cablePre != null) { for (int i = 0; i < cablePre.length; i++) { for (int j = 0; j < cable.length; j++) { double c = cablePre[i] * cable[j]; double prevProb = tp.getEntry(i, j); if (Double.isNaN(prevProb)) prevProb = 0; double newProb = c * (updateRate) + prevProb * (1.0 - updateRate); if (newProb < min) min = newProb; if (newProb > max) max = newProb; tp.setEntry(i, j, newProb); } } } if ((cablePre == null) || (cablePre.length != cable.length)) cablePre = new double[cable.length]; System.arraycopy(cable, 0, cablePre, 0, cable.length); return ravel(tp, chainVector, 0, max); }