/** * Constructor for DenseMatrix. Copies from the passed array * * @param values * Arrays to copy from. Every sub-array must have the same size */ public DenseMatrix(double[][] values) { super(values.length, values[0].length); // Copy the contents for (int i = 0; i < values.length; ++i) { if (values[i].length != numColumns) throw new IllegalArgumentException("Array cannot be jagged"); for (int j = 0; j < values[i].length; ++j) set(i, j, values[i][j]); } }
/** * Constructor for DenseMatrix. Copies from the passed array * * @param values * Arrays to copy from. Every sub-array must have the same size */ public DenseMatrix(double[][] values) { super(values.length, values[0].length); // Copy the contents for (int i = 0; i < values.length; ++i) { if (values[i].length != numColumns) throw new IllegalArgumentException("Array cannot be jagged"); for (int j = 0; j < values[i].length; ++j) set(i, j, values[i][j]); } }
/** * Returns the identity matrix of the given size * * @param size * Number of rows/columns of the matrix * @return Matrix of the given size, with ones on the main diagonal */ public static DenseMatrix identity(int size) { DenseMatrix A = new DenseMatrix(size, size); for (int i = 0; i < size; ++i) A.set(i, i, 1); return A; }
/** * Returns the identity matrix of the given size * * @param size * Number of rows/columns of the matrix * @return Matrix of the given size, with ones on the main diagonal */ public static DenseMatrix identity(int size) { DenseMatrix A = new DenseMatrix(size, size); for (int i = 0; i < size; ++i) A.set(i, i, 1); return A; }
@Override void copy(Matrix A) { for (MatrixEntry e : A) set(e.row(), e.column(), e.get()); }
@Override void copy(Matrix A) { for (MatrixEntry e : A) set(e.row(), e.column(), e.get()); }
/** * Constructor for DenseMatrix. Builds the matrix from vectors. Each vector * will correspond to a column of the matrix * * @param x * Vectors which forms the columns of this matrix. Every vector * must have the same size */ public DenseMatrix(Vector[] x) { super(x[0].size(), x.length); // Ensure correct sizes for (Vector v : x) if (v.size() != numRows) throw new IllegalArgumentException( "All vectors must be of the same size"); // Copy the contents for (int j = 0; j < x.length; ++j) for (VectorEntry e : x[j]) set(e.index(), j, e.get()); }
/** * Constructor for DenseMatrix. Builds the matrix from vectors. Each vector * will correspond to a column of the matrix * * @param x * Vectors which forms the columns of this matrix. Every vector * must have the same size */ public DenseMatrix(Vector[] x) { super(x[0].size(), x.length); // Ensure correct sizes for (Vector v : x) if (v.size() != numRows) throw new IllegalArgumentException( "All vectors must be of the same size"); // Copy the contents for (int j = 0; j < x.length; ++j) for (VectorEntry e : x[j]) set(e.index(), j, e.get()); }
/** * Constructor for DenseMatrix. Builds the matrix from a vector * * @param x * Vector to copy from. This will form this matrix' single column * @param deep * If true, x is copied, if false, the internal storage of this * matrix is the same as that of the vector. In that case, * <code>x</code> must be a <code>DenseVector</code> */ public DenseMatrix(Vector x, boolean deep) { super(x.size(), 1); if (deep) for (VectorEntry e : x) set(e.index(), 0, e.get()); else { if (!(x instanceof DenseVector)) throw new IllegalArgumentException("x must be a DenseVector"); data = ((DenseVector) x).getData(); } }
/** * Constructor for DenseMatrix. Builds the matrix from a vector * * @param x * Vector to copy from. This will form this matrix' single column * @param deep * If true, x is copied, if false, the internal storage of this * matrix is the same as that of the vector. In that case, * <code>x</code> must be a <code>DenseVector</code> */ public DenseMatrix(Vector x, boolean deep) { super(x.size(), 1); if (deep) for (VectorEntry e : x) set(e.index(), 0, e.get()); else { if (!(x instanceof DenseVector)) throw new IllegalArgumentException("x must be a DenseVector"); data = ((DenseVector) x).getData(); } }
@Override public long process(BenchmarkMatrix[] inputs, BenchmarkMatrix[] outputs, long numTrials) { DenseMatrix matA = inputs[0].getOriginal(); DenseMatrix mod = new DenseMatrix(matA.numRows(),matA.numColumns()); long prev = System.nanoTime(); for( long i = 0; i < numTrials; i++ ) { // in-place operator mod.set(matA); mod.scale(BenchmarkConstants.SCALE); } long elapsedTime = System.nanoTime()-prev; if( outputs != null ) { outputs[0] = new MtjBenchmarkMatrix(mod); } return elapsedTime; } }
@Override public long process(BenchmarkMatrix[] inputs, BenchmarkMatrix[] outputs, long numTrials) { DenseMatrix matA = inputs[0].getOriginal(); DenseMatrix matB = inputs[1].getOriginal(); DenseMatrix result = new DenseMatrix(matA.numRows(),matB.numColumns()); long prev = System.nanoTime(); for( long i = 0; i < numTrials; i++ ) { // in-place operator result.set(matA); result.add(matB); } long elapsedTime = System.nanoTime()-prev; if( outputs != null ) { outputs[0] = new MtjBenchmarkMatrix(result); } return elapsedTime; } }
/** * Computes the estimate of the covariance matrix. * * @param matrix A multi-dimensional array containing the matrix values (assumed to contain at least one row). * @param weights The observation weights, normalized to sum to 1. * @param mean The values' mean vector. * @return The covariance matrix, including the ridge. */ private UpperSPDDenseMatrix weightedCovariance(double[][] matrix, DenseVector weights, Vector mean) { int rows = matrix.length; int cols = matrix[0].length; if (mean.size() != cols) { throw new IllegalArgumentException("Length of the mean vector must match matrix."); } // Create matrix with centered transposed data, weighted appropriately DenseMatrix transposed = new DenseMatrix(cols, rows); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { transposed.set(j, i, Math.sqrt(weights.get(i)) * (matrix[i][j] - mean.get(j))); } } UpperSPDDenseMatrix covT = (UpperSPDDenseMatrix) new UpperSPDDenseMatrix(cols).rank1(transposed); for (int i = 0; i < cols; i++) { covT.add(i, i, m_Ridge); } return covT; }
/** * Creates an LU decomposition of the given matrix * * @param A * Matrix to decompose. Overwritten with the decomposition * @return The current decomposition */ public DenseLU factor(DenseMatrix A) { singular = false; intW info = new intW(0); LAPACK.getInstance().dgetrf(A.numRows(), A.numColumns(), A.getData(), Matrices.ld(A.numRows()), piv, info); if (info.val > 0) singular = true; else if (info.val < 0) throw new IllegalArgumentException(); LU.set(A); return this; }
/** * Creates an LU decomposition of the given matrix * * @param A * Matrix to decompose. Overwritten with the decomposition * @return The current decomposition */ public DenseLU factor(DenseMatrix A) { singular = false; intW info = new intW(0); LAPACK.getInstance().dgetrf(A.numRows(), A.numColumns(), A.getData(), Matrices.ld(A.numRows()), piv, info); if (info.val > 0) singular = true; else if (info.val < 0) throw new IllegalArgumentException(); LU.set(A); return this; }
@Override public long process(BenchmarkMatrix[] inputs, BenchmarkMatrix[] outputs, long numTrials) { DenseMatrix matA = inputs[0].getOriginal(); no.uib.cipr.matrix.QR qr = new no.uib.cipr.matrix.QR(matA.numRows(),matA.numColumns()); DenseMatrix tmp = new DenseMatrix(matA); DenseMatrix Q = null; UpperTriangDenseMatrix R = null; long prev = System.nanoTime(); for( long i = 0; i < numTrials; i++ ) { // the input matrix is over written tmp.set(matA); qr.factor(tmp); Q = qr.getQ(); R = qr.getR(); } long elapsedTime = System.nanoTime()-prev; if( outputs != null ) { outputs[0] = new MtjBenchmarkMatrix(Q); outputs[1] = new MtjBenchmarkMatrix(R); } return elapsedTime; } }
public Matrix transAmult(Matrix B, DenseMatrix C) { if (pivots == null) return super.transAmult(B, C); checkTransAmultAdd(B, C); C.set(B); LAPACK.getInstance().dlaswp(C.numColumns(), C.getData(), Matrices.ld(C.numRows()), 1, pivots.length, pivots, transposed ? 1 : -1); return C; }
public Matrix transAmult(Matrix B, DenseMatrix C) { if (pivots == null) return super.transAmult(B, C); checkTransAmultAdd(B, C); C.set(B); LAPACK.getInstance().dlaswp(C.numColumns(), C.getData(), Matrices.ld(C.numRows()), 1, pivots.length, pivots, transposed ? 1 : -1); return C; }
public Matrix mult(Matrix B, DenseMatrix C) { if (pivots == null) return super.mult(B, C); checkMultAdd(B, C); C.set(B); LAPACK.getInstance().dlaswp(C.numColumns(), C.getData(), Matrices.ld(C.numRows()), 1, pivots.length, pivots, transposed ? -1 : 1); return C; }
public Matrix mult(Matrix B, DenseMatrix C) { if (pivots == null) return super.mult(B, C); checkMultAdd(B, C); C.set(B); LAPACK.getInstance().dlaswp(C.numColumns(), C.getData(), Matrices.ld(C.numRows()), 1, pivots.length, pivots, transposed ? -1 : 1); return C; }