List of usage examples for org.apache.commons.math3.linear RealMatrix getEntry
double getEntry(int row, int column) throws OutOfRangeException;
From source file:edu.oregonstate.eecs.mcplan.ml.KulisLowRankKernelLearner.java
private void choleskyUpdate(final int r, final double alpha, final RealVector x, final RealMatrix B) { final double[] a = new double[r + 1]; final double[] h = new double[r]; a[0] = alpha;/*from www .j av a 2 s . c o m*/ for (int i = 0; i < r; ++i) { final double xi = x.getEntry(i); double t = 1 + a[i] * xi * xi; h[i] = Math.sqrt(t); a[i + 1] = a[i] / t; t = B.getEntry(i, i); double s = 0.0; B.setEntry(i, i, t * h[i]); // t == B_ii for (int j = i - 1; j >= 0; --j) { s += t * x.getEntry(j + 1); t = B.getEntry(i, j); B.setEntry(i, j, (t + a[j + 1] * x.getEntry(j) * s) * h[j]); // t == B_ij } } }
From source file:com.itemanalysis.psychometrics.factoranalysis.VarimaxCriteria.java
/** * Computes the function value for varimax rotation. * * @param L matrix of factor loadings.//from w w w . j a va 2 s .c o m */ public void computeValues(RealMatrix L) { //initialize dimensions and column mean array int nrow = L.getRowDimension(); int ncol = L.getColumnDimension(); Mean[] colMean = new Mean[ncol]; for (int i = 0; i < ncol; i++) { colMean[i] = new Mean(); } //square each element in matrix RealMatrix L2 = L.copy(); double value = 0.0; for (int i = 0; i < nrow; i++) { for (int j = 0; j < ncol; j++) { value = L.getEntry(i, j); value *= value; L2.setEntry(i, j, value); colMean[j].increment(value); } } double dif = 0.0; RealMatrix QL = new Array2DRowRealMatrix(nrow, ncol); for (int i = 0; i < nrow; i++) { for (int j = 0; j < ncol; j++) { dif = L2.getEntry(i, j) - colMean[j].getResult(); QL.setEntry(i, j, dif); } } //compute gradientAt gradient = new Array2DRowRealMatrix(nrow, ncol); for (int i = 0; i < nrow; i++) { for (int j = 0; j < ncol; j++) { value = -L.getEntry(i, j) * QL.getEntry(i, j); gradient.setEntry(i, j, value); } } //compute function value RealMatrix B = QL.transpose().multiply(QL); double sum = B.getTrace(); functionValue = -sum / 4.0; }
From source file:eagle.security.userprofile.impl.UserProfileAnomalyEigenEvaluator.java
private RealMatrix normalizeData(RealMatrix matrix, UserProfileEigenModel model) { RealMatrix normalizedData = new Array2DRowRealMatrix(matrix.getRowDimension(), matrix.getColumnDimension()); if (LOG.isDebugEnabled()) LOG.debug("model statistics size: " + model.statistics().length); for (int i = 0; i < matrix.getRowDimension(); i++) { for (int j = 0; j < matrix.getColumnDimension(); j++) { double value = (matrix.getEntry(i, j) - model.statistics()[j].getMean()) / model.statistics()[j].getStddev(); normalizedData.setEntry(i, j, value); }/*from ww w. j av a 2 s.c o m*/ } return normalizedData; }
From source file:edu.stanford.cfuller.imageanalysistools.fitting.BisquareLinearFit.java
/** * Calculates the leverages of data points for least squares fitting (assuming equal variances). * * @param indVarValues The values of the independent variable used for the fitting. * @return a RealVector containing a leverage value for each independent variable value. *///from ww w .java 2 s . c o m protected RealVector calculateLeverages(RealVector indVarValues) { RealMatrix indVarMatrix = null; if (this.noIntercept) { indVarMatrix = new Array2DRowRealMatrix(indVarValues.getDimension(), 1); } else { indVarMatrix = new Array2DRowRealMatrix(indVarValues.getDimension(), 2); } indVarMatrix.setColumnVector(0, indVarValues); if (!this.noIntercept) { indVarMatrix.setColumnVector(1, indVarValues.mapMultiply(0).mapAdd(1)); } RealVector leverages = new ArrayRealVector(indVarValues.getDimension()); QRDecomposition xQR = new QRDecomposition(indVarMatrix); RealMatrix xR = xQR.getR(); int smallerDim = xR.getRowDimension() < xR.getColumnDimension() ? xR.getRowDimension() : xR.getColumnDimension(); RealMatrix xRSq = xR.getSubMatrix(0, smallerDim - 1, 0, smallerDim - 1); QRDecomposition xRQR = new QRDecomposition(xRSq); RealMatrix xRInv = xRQR.getSolver().getInverse(); RealMatrix xxRInv = indVarMatrix.multiply(xRInv); for (int i = 0; i < indVarValues.getDimension(); i++) { double sum = 0; for (int j = 0; j < xxRInv.getColumnDimension(); j++) { sum += Math.pow(xxRInv.getEntry(i, j), 2); } leverages.setEntry(i, sum); } return leverages; }
From source file:edu.cudenver.bios.matrix.MatrixUtils.java
/** * Calculate the Kronecker product of a list of matrices. Applies the * Kronecker product following the list order (i.e. from left to right). * * @param matrixList list of matrices/* ww w. ja v a 2s . c o m*/ * @return Kronecker product of matrices */ public static RealMatrix getKroneckerProduct(List<RealMatrix> matrixList) { if (matrixList == null || matrixList.size() <= 0) throw new IllegalArgumentException("no input matrices"); if (matrixList.size() == 1) return matrixList.get(0); // nothing to do, only one matrix // calculate the dimensions of the Kronecker product matrix int totalRows = 1; int totalCols = 1; for (RealMatrix matrix : matrixList) { totalRows *= matrix.getRowDimension(); totalCols *= matrix.getColumnDimension(); } // create a matrix to hold the data double[][] productData = new double[totalRows][totalCols]; // initialize to 1 (allows us to multiple the contents of each matrix // onto the result sequentially for (int prodRow = 0; prodRow < totalRows; prodRow++) { for (int prodCol = 0; prodCol < totalCols; prodCol++) { productData[prodRow][prodCol] = 1; } } // multiply the contents of each matrix onto the result int maxRow = totalRows; int maxCol = totalCols; for (RealMatrix matrix : matrixList) { maxRow /= matrix.getRowDimension(); maxCol /= matrix.getColumnDimension(); int matrixRow = 0; int matrixCol = 0; // multiply onto the result for (int prodRow = 0, sectionRow = 0; prodRow < totalRows; prodRow++, sectionRow++) { matrixCol = 0; double value = matrix.getEntry(matrixRow, matrixCol); for (int prodCol = 0, sectionCol = 0; prodCol < totalCols; prodCol++, sectionCol++) { productData[prodRow][prodCol] *= value; if (sectionCol >= maxCol - 1) { matrixCol++; if (matrixCol >= matrix.getColumnDimension()) matrixCol = 0; sectionCol = -1; value = matrix.getEntry(matrixRow, matrixCol); } } if (sectionRow >= maxRow - 1) { matrixRow++; if (matrixRow >= matrix.getRowDimension()) matrixRow = 0; sectionRow = -1; } } } // return a new matrix containing the Kronecker product return new Array2DRowRealMatrix(productData); }
From source file:com.github.thorbenlindhauer.factor.GaussianFactorTest.java
@Test public void testValueObservation() { GaussianFactor reducedVector = abcFactor.observation(newScope(new ContinuousVariable("A")), new double[] { 2.5d }); // then//from w w w.j a va2 s .c om Collection<Variable> newVariables = reducedVector.getVariables().getVariables(); assertThat(newVariables).hasSize(2); assertThat(newVariables).contains(new ContinuousVariable("B"), new ContinuousVariable("C")); // B C A //6.0d, 7.0d, 3.0d //3.0d, 5.5d, 10.0d //4.0d, 6.0d, 3.0d // // X = {B, C}, Y = {A} // precision matrix: K_xx RealMatrix precisionMatrix = reducedVector.getPrecisionMatrix(); assertThat(precisionMatrix.isSquare()).isTrue(); assertThat(precisionMatrix.getColumnDimension()).isEqualTo(2); double precision = precisionMatrix.getEntry(0, 0); assertThat(precision).isEqualTo(6.0d, TestConstants.DOUBLE_VALUE_TOLERANCE); precision = precisionMatrix.getEntry(0, 1); assertThat(precision).isEqualTo(7.0d, TestConstants.DOUBLE_VALUE_TOLERANCE); precision = precisionMatrix.getEntry(1, 0); assertThat(precision).isEqualTo(3.0d, TestConstants.DOUBLE_VALUE_TOLERANCE); precision = precisionMatrix.getEntry(1, 1); assertThat(precision).isEqualTo(5.5d, TestConstants.DOUBLE_VALUE_TOLERANCE); // scaled mean vector: h_x - K_xy * y RealVector scaledMeanVector = reducedVector.getScaledMeanVector(); assertThat(scaledMeanVector.getDimension()).isEqualTo(2); double meanValue = scaledMeanVector.getEntry(0); assertThat(meanValue).isEqualTo(-5.5d, TestConstants.DOUBLE_VALUE_TOLERANCE); meanValue = scaledMeanVector.getEntry(1); assertThat(meanValue).isEqualTo(-23.5d, TestConstants.DOUBLE_VALUE_TOLERANCE); // normalization constant: g + h_y * y - 0.5 * (y * K_yy * y) // 8.5 + 7.5 - 9,375 assertThat(reducedVector.getNormalizationConstant()).isEqualTo(6.625d, TestConstants.DOUBLE_VALUE_TOLERANCE); }
From source file:edu.cudenver.bios.matrix.test.TestMatrixOrthonormalization.java
/** * Verify that the Q'Q = I for the Q matrix produced by the * orthonormalization//from w ww .j av a 2s . c o m */ public void testQQisIdentity() { RealMatrix Q = norm.getQ(); // verify that Q'Q = identity RealMatrix shouldBeIdentityMatrix = Q.transpose().multiply(Q); // make sure the matrix is sqaure if (!shouldBeIdentityMatrix.isSquare()) { fail(); } // make sure the diagonal elements are one (within tolerance), and off diagonals // are zero (within tolerance) for (int r = 0; r < shouldBeIdentityMatrix.getRowDimension(); r++) { for (int c = 0; c < shouldBeIdentityMatrix.getColumnDimension(); c++) { double shouldBeValue = (r == c) ? 1 : 0; if (Precision.compareTo(shouldBeIdentityMatrix.getEntry(r, c), shouldBeValue, TOLERANCE) != 0) fail(); } } assertTrue(true); }
From source file:mase.app.allocation.AllocationProblem.java
private DescriptiveStatistics pairDistances(RealMatrix distMatrix) { DescriptiveStatistics ds = new DescriptiveStatistics(); distMatrix = distMatrix.copy();/* w w w. j av a 2s . c o m*/ for (int k = 0; k < numAgents; k++) { // find closest pair double min = Double.POSITIVE_INFINITY; int minI = -1, minJ = -1; for (int i = 0; i < numAgents; i++) { for (int j = 0; j < numAgents; j++) { double d = distMatrix.getEntry(i, j); if (!Double.isNaN(d) && d < min) { min = d; minI = i; minJ = j; } } } ds.addValue(min); distMatrix.setRow(minI, nulify); distMatrix.setColumn(minJ, nulify); } return ds; }
From source file:ffx.autoparm.Superpose.java
/** * <p>//from ww w.j a v a2s . co m * quatfit</p> */ public void quatfit() { double xxyx = 0.0; double xxyy = 0.0; double xxyz = 0.0; double xyyx = 0.0; double xyyy = 0.0; double xyyz = 0.0; double xzyx = 0.0; double xzyy = 0.0; double xzyz = 0.0; double c[][] = new double[4][4]; for (int i = 0; i < nfit; i++) { xxyx = xxyx + xyz1[i][0] * xyz2[i][0]; xxyy = xxyy + xyz1[i][1] * xyz2[i][0]; xxyz = xxyz + xyz1[i][2] * xyz2[i][0]; xyyx = xyyx + xyz1[i][0] * xyz2[i][1]; xyyy = xyyy + xyz1[i][1] * xyz2[i][1]; xyyz = xyyz + xyz1[i][2] * xyz2[i][1]; xzyx = xzyx + xyz1[i][0] * xyz2[i][2]; xzyy = xzyy + xyz1[i][1] * xyz2[i][2]; xzyz = xzyz + xyz1[i][2] * xyz2[i][2]; } c[0][0] = xxyx + xyyy + xzyz; c[0][1] = xzyy - xyyz; c[1][1] = xxyx - xyyy - xzyz; c[0][2] = xxyz - xzyx; c[1][2] = xxyy + xyyx; c[2][2] = xyyy - xzyz - xxyx; c[0][3] = xyyx - xxyy; c[1][3] = xzyx + xxyz; c[2][3] = xyyz + xzyy; c[3][3] = xzyz - xxyx - xyyy; RealMatrix a = new Array2DRowRealMatrix( new double[][] { { c[0][0], c[0][1], c[0][2], c[0][3] }, { c[0][1], c[1][1], c[1][2], c[1][3] }, { c[0][2], c[1][2], c[2][2], c[2][3] }, { c[0][3], c[1][3], c[2][3], c[3][3] } }); EigenDecomposition e = new EigenDecomposition(a, 1); a = e.getV(); double[] q = { a.getEntry(0, 0), a.getEntry(1, 0), a.getEntry(2, 0), a.getEntry(3, 0) }; double rot[][] = new double[3][3]; rot[0][0] = q[0] * q[0] + q[1] * q[1] - q[2] * q[2] - q[3] * q[3]; rot[1][0] = 2.0 * (q[1] * q[2] - q[0] * q[3]); rot[2][0] = 2.0 * (q[1] * q[3] + q[0] * q[2]); rot[0][1] = 2.0 * (q[2] * q[1] + q[0] * q[3]); rot[1][1] = q[0] * q[0] - q[1] * q[1] + q[2] * q[2] - q[3] * q[3]; rot[2][1] = 2.0 * (q[2] * q[3] - q[0] * q[1]); rot[0][2] = 2.0 * (q[3] * q[1] - q[0] * q[2]); rot[1][2] = 2.0 * (q[3] * q[2] + q[0] * q[1]); rot[2][2] = q[0] * q[0] - q[1] * q[1] - q[2] * q[2] + q[3] * q[3]; double xrot, yrot, zrot; for (int i = 0; i < n2; i++) { xrot = xyz2[i][0] * rot[0][0] + xyz2[i][1] * rot[0][1] + xyz2[i][2] * rot[0][2]; yrot = xyz2[i][0] * rot[1][0] + xyz2[i][1] * rot[1][1] + xyz2[i][2] * rot[1][2]; zrot = xyz2[i][0] * rot[2][0] + xyz2[i][1] * rot[2][1] + xyz2[i][2] * rot[2][2]; xyz2[i][0] = xrot; xyz2[i][1] = yrot; xyz2[i][2] = zrot; } }
From source file:edu.cudenver.bios.matrix.test.TestMatrixOrthonormalization.java
/** * Verify that the A = QxR for the Q, R matrices produced by the * orthonormalization//from w w w.java 2s . c om */ public void testQRshouldBeA() { RealMatrix Q = norm.getQ(); RealMatrix R = norm.getR(); // verify that QR = A RealMatrix shouldBeOriginalMatrix = Q.multiply(R); // Make sure that QR has same dimension as original A matrix if (shouldBeOriginalMatrix.getRowDimension() != data.length || shouldBeOriginalMatrix.getColumnDimension() != data[0].length) { fail(); } // make sure elements of QR match elements of A (within tolerance) for (int r = 0; r < shouldBeOriginalMatrix.getRowDimension(); r++) { for (int c = 0; c < shouldBeOriginalMatrix.getColumnDimension(); c++) { if (Precision.compareTo(shouldBeOriginalMatrix.getEntry(r, c), data[r][c], TOLERANCE) != 0) fail(); } } assertTrue(true); }