List of usage examples for org.apache.commons.math3.linear RealMatrix setEntry
void setEntry(int row, int column, double value) throws OutOfRangeException;
From source file:com.itemanalysis.psychometrics.factoranalysis.MatrixUtils.java
/** * Creates a vector (1 X size matrix) with all elements set to value. * * @param size/* ww w . j a v a 2s. c om*/ * @param value * @return */ public static RealMatrix getVector(int size, double value) { RealMatrix vec = new Array2DRowRealMatrix(1, size); for (int i = 0; i < size; i++) { vec.setEntry(0, i, value); } return vec; }
From source file:com.cloudera.oryx.common.math.SolverLoadIT.java
private static RealMatrix randomSymmetricMatrix(int dimension) { RandomGenerator random = RandomManager.getRandom(); RealMatrix symmetric = new Array2DRowRealMatrix(dimension, dimension); for (int j = 0; j < dimension; j++) { // Diagonal symmetric.setEntry(j, j, random.nextDouble()); for (int k = j + 1; k < dimension; k++) { // Off-diagonal double d = random.nextDouble(); symmetric.setEntry(j, k, d); symmetric.setEntry(k, j, d); }//from ww w .ja va 2s. c o m } return symmetric; }
From source file:com.google.location.lbs.gnss.gps.pseudorange.Ecef2EnuConverter.java
/** * Computes a rotation matrix for converting a vector in Earth-Centered Earth-Fixed (ECEF) * Cartesian system into a vector in local east-north-up (ENU) Cartesian system with respect to * a reference location. The matrix has the following content: * * - sinLng cosLng 0 * - sinLat * cosLng - sinLat * sinLng cosLat * cosLat * cosLng cosLat * sinLng sinLat * * <p> Reference: Pratap Misra and Per Enge * "Global Positioning System: Signals, Measurements, and Performance" Page 137. * * @param refLat Latitude of reference location * @param refLng Longitude of reference location * @return the Ecef to Enu rotation matrix *///from w w w. ja v a 2 s .c o m public static RealMatrix getRotationMatrix(double refLat, double refLng) { RealMatrix rotationMatrix = new Array2DRowRealMatrix(3, 3); // Fill in the rotation Matrix rotationMatrix.setEntry(0, 0, -1 * Math.sin(refLng)); rotationMatrix.setEntry(1, 0, -1 * Math.cos(refLng) * Math.sin(refLat)); rotationMatrix.setEntry(2, 0, Math.cos(refLng) * Math.cos(refLat)); rotationMatrix.setEntry(0, 1, Math.cos(refLng)); rotationMatrix.setEntry(1, 1, -1 * Math.sin(refLat) * Math.sin(refLng)); rotationMatrix.setEntry(2, 1, Math.cos(refLat) * Math.sin(refLng)); rotationMatrix.setEntry(0, 2, 0); rotationMatrix.setEntry(1, 2, Math.cos(refLat)); rotationMatrix.setEntry(2, 2, Math.sin(refLat)); return rotationMatrix; }
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 ww w.j ava 2 s .c o 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: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))); }//from w ww. j a v a 2 s .c om } return result; }
From source file:edu.washington.gs.skyline.model.quantification.WeightedRegression.java
public static double[] weighted(double[][] x, double[] y, double[] weights, boolean intercept) { RealMatrix predictor; if (intercept) { int nRows = x.length; int nCols = x[0].length + 1; predictor = MatrixUtils.createRealMatrix(nRows, nCols); for (int iRow = 0; iRow < nRows; iRow++) { predictor.setEntry(iRow, 0, 1.0); for (int iCol = 1; iCol < nCols; iCol++) { predictor.setEntry(iRow, iCol, x[iRow][iCol - 1]); }/*from w w w. ja v a 2 s . c om*/ } } else { predictor = MatrixUtils.createRealMatrix(x); } RealVector responseVector = MatrixUtils.createRealVector(y); RealMatrix weightsMatrix = MatrixUtils.createRealDiagonalMatrix(weights); RealMatrix predictorTransposed = predictor.transpose(); RealMatrix predictorTransposedTimesWeights = predictorTransposed .multiply(weightsMatrix.multiply(predictor)); CholeskyDecomposition choleskyDecomposition = new CholeskyDecomposition(predictorTransposedTimesWeights); RealVector vectorToSolve = predictorTransposed.operate(weightsMatrix.operate(responseVector)); RealVector result = choleskyDecomposition.getSolver().solve(vectorToSolve); return result.toArray(); }
From source file:net.sf.dsp4j.octave_3_2_4.m.polynomial.Roots.java
public static Complex[] roots(RealVector v) { if (v.isInfinite() || v.isNaN()) { throw new RuntimeException("roots: inputs must not contain Inf or NaN"); }/*from ww w .ja v a 2 s . co m*/ int n = v.getDimension(); // ## If v = [ 0 ... 0 v(k+1) ... v(k+l) 0 ... 0 ], we can remove the // ## leading k zeros and n - k - l roots of the polynomial are zero. int[] f = new int[v.getDimension()]; if (v.getDimension() > 0) { int fI = 0; double max = v.getMaxValue(); double min = FastMath.abs(v.getMinValue()); if (min > max) { max = min; } RealVector v1 = v.mapDivide(max); f = OctaveBuildIn.find(v1); } Complex[] r = new Complex[0]; if (f.length > 0 && n > 1) { v = v.getSubVector(f[0], f[f.length - 1] - f[0] + 1); if (v.getDimension() > 1) { double[] ones = new double[v.getDimension() - 2]; Arrays.fill(ones, 1); RealMatrix A = OctaveBuildIn.diag(ones, -1); for (int i = 0; i < A.getRowDimension(); i++) { A.setEntry(i, 0, -v.getEntry(i + 1) / v.getEntry(0)); } try { r = Eig.eig(A); } catch (Exception e) { throw new RuntimeException(e); } if (f[f.length - 1] < n) { int diffLength = n - 1 - f[f.length - 1]; if (diffLength > 0) { int rl = r.length; r = Arrays.copyOf(r, r.length + diffLength); Arrays.fill(r, rl, r.length, Complex.ZERO); } } } else { r = new Complex[n - f[f.length - 1]]; Arrays.fill(r, Complex.ZERO); } } else { r = new Complex[0]; } return r; }
From source file:edu.washington.gs.skyline.model.quantification.DesignMatrix.java
private static RealMatrix matrixFromColumnVectors(double[][] columnVectors) { RealMatrix realMatrix = MatrixUtils.createRealMatrix(columnVectors[0].length, columnVectors.length); for (int iRow = 0; iRow < realMatrix.getRowDimension(); iRow++) { for (int iCol = 0; iCol < realMatrix.getColumnDimension(); iCol++) { realMatrix.setEntry(iRow, iCol, columnVectors[iCol][iRow]); }//w ww . ja va 2 s. com } return realMatrix; }
From source file:com.simiacryptus.mindseye.test.PCAUtil.java
/** * Forked from Apache Commons Math//w w w . j a va2s . c o m * * @param stream the stream * @return covariance covariance */ @Nonnull public static RealMatrix getCovariance(@Nonnull final Supplier<Stream<double[]>> stream) { final int dimension = stream.get().findAny().get().length; final List<DoubleStatistics> statList = IntStream.range(0, dimension * dimension) .mapToObj(i -> new DoubleStatistics()).collect(Collectors.toList()); stream.get().forEach(array -> { for (int i = 0; i < dimension; i++) { for (int j = 0; j <= i; j++) { statList.get(i * dimension + j).accept(array[i] * array[j]); } } RecycleBin.DOUBLES.recycle(array, array.length); }); @Nonnull final RealMatrix covariance = new BlockRealMatrix(dimension, dimension); for (int i = 0; i < dimension; i++) { for (int j = 0; j <= i; j++) { final double v = statList.get(i + dimension * j).getAverage(); covariance.setEntry(i, j, v); covariance.setEntry(j, i, v); } } return covariance; }
From source file:edu.mit.genecircuits.GcUtils.java
/** Convert dense Colt to Apache Commons matrix */ static public RealMatrix colt2apache(DoubleMatrix2D colt) { RealMatrix apache = MatrixUtils.createRealMatrix(colt.rows(), colt.columns()); for (int i = 0; i < colt.rows(); i++) for (int j = 0; j < colt.columns(); j++) apache.setEntry(i, j, colt.get(i, j)); return apache; }