/** * Constructs a new matrix from a two-dimensional array of doubles. * * @param matrix Array of rows. Each row must have the same length. * @throws IllegalArgumentException if the specified matrix is not regular (i.e. if all rows * doesn't have the same length). */ public GeneralMatrix(final double[][] matrix) throws IllegalArgumentException { mat = new DMatrixRMaj(matrix); final int numRow = getNumRow(); final int numCol = getNumCol(); for (int j = 0; j < numRow; j++) { if (matrix[j].length != numCol) { throw new IllegalArgumentException(Errors.format(ErrorKeys.MATRIX_NOT_REGULAR)); } for (int i = 0; i < numCol; i++) { mat.set(j, i, matrix[j][i]); } } }
/** * Extract col to provided array. * * @param col * @param array */ public void getColumn(int col, double[] array) { for (int j = 0; j < array.length; j++) { array[j] = mat.get(j, col); } }
/** * In-place update from matrix1 * matrix2. * * @param matrix1 * @param matrix2 */ public void mul(Matrix matrix1, Matrix matrix2) { DMatrixRMaj a = internal(matrix1); DMatrixRMaj b = internal(matrix2); if (a == mat || b == mat) { mat = new DMatrixRMaj(a.numRows, b.numCols); } else { mat.reshape(a.numRows, b.numCols, false); } CommonOps_DDRM.mult(a, b, mat); }
public void setRow(int row, double... values) { if (values.length != mat.getNumCols()) { throw new IllegalArgumentException( "Call setRow received an array of length " + values.length + ". " + "The dimensions of the matrix is " + mat.getNumRows() + " by " + mat.getNumCols() + "."); } for (int i = 0; i < values.length; i++) { mat.set(row, i, values[i]); } }
@Override public DMatrixRMaj getW(DMatrixRMaj W ) { int m = compact ? numSingular : numRows; int n = compact ? numSingular : numCols; if( W == null ) W = new DMatrixRMaj(m,n); else { W.reshape(m,n, false); W.zero(); } for( int i = 0; i < numSingular; i++ ) { W.unsafe_set(i,i, singularValues[i]); } return W; }
@Override public DMatrixRMaj getW(DMatrixRMaj W ) { int m = compact ? numSingular : numRows; int n = compact ? numSingular : numCols; if( W == null ) W = new DMatrixRMaj(m,n); else { W.reshape(m,n, false); W.zero(); } for( int i = 0; i < numSingular; i++ ) { W.data[i*W.numCols+i] = singularValues[i]; } return W; }
/** * Converts {@link DMatrix2} into {@link DMatrixRMaj}. * * @param input Input matrix. * @param output Output matrix. If null a new matrix will be declared. * @return Converted matrix. */ public static DMatrixRMaj convert(DMatrix2 input , DMatrixRMaj output ) { if( output == null) output = new DMatrixRMaj(2,1); if( output.getNumRows() != 1 && output.getNumCols() != 1 ) throw new IllegalArgumentException("One row or column must have a length of 1 for it to be a vector"); int length = Math.max(output.getNumRows(),output.getNumCols()); if( length != 2 ) throw new IllegalArgumentException("Length of input vector is not 2. It is "+length); output.data[0] = input.a1; output.data[1] = input.a2; return output; }
/** * Constructs a new matrix and copies the initial values from the parameter matrix. * * @param matrix The matrix to copy. */ public GeneralMatrix(final GeneralMatrix matrix) { mat = new DMatrixRMaj(matrix.mat); }
/** * This is a total hack. Set B to a diagonal matrix where each diagonal element * is the value of the largest absolute value in B. This will be SPD and hopefully * not screw up the search. */ private void resetMatrixB() { // find the magnitude of the largest diagonal element double maxDiag = 0; for( int i = 0; i < N; i++ ) { double d = Math.abs(B.get(i,i)); if( d > maxDiag ) maxDiag = d; } B.zero(); for( int i = 0; i < N; i++ ) { B.set(i,i,maxDiag); } }
@Override public DMatrixRMaj getColPivotMatrix(DMatrixRMaj P) { if( P == null ) P = new DMatrixRMaj(numCols,numCols); else if( P.numRows != numCols ) throw new IllegalArgumentException("Number of rows must be "+numCols); else if( P.numCols != numCols ) throw new IllegalArgumentException("Number of columns must be "+numCols); else { P.zero(); } for( int i = 0; i < numCols; i++ ) { P.set(pivots[i],i,1); } return P; } }
/** * Override parent to initialize matrices */ @Override public void initialize(double[] initial, int numberOfParameters, double minimumFunctionValue) { super.initialize(initial, numberOfParameters,minimumFunctionValue); y.reshape(numberOfParameters,1); xPrevious.reshape(numberOfParameters,1); x.reshape(numberOfParameters,1); // set the previous gradient to zero gradientPrevious.reshape(numberOfParameters,1); gradientPrevious.zero(); firstIteration = true; }
private boolean bidiagonalization(DMatrixRMaj orig, boolean transposed) { if( transposed ) { A_mod.reshape(orig.numCols,orig.numRows,false); CommonOps_DDRM.transpose(orig,A_mod); } else { A_mod.reshape(orig.numRows,orig.numCols,false); A_mod.set(orig); } if( !bidiag.decompose(A_mod) ) return true; return false; }
/** * Converts {@link DMatrixRMaj} into {@link DMatrix4} * * @param input Input matrix. * @param output Output matrix. If null a new matrix will be declared. * @return Converted matrix. */ public static DMatrix4 convert(DMatrixRMaj input , DMatrix4 output ) { if( output == null) output = new DMatrix4(); if( input.getNumRows() != 1 && input.getNumCols() != 1 ) throw new IllegalArgumentException("One row or column must have a length of 1 for it to be a vector"); int length = Math.max(input.getNumRows(),input.getNumCols()); if( length != 4 ) throw new IllegalArgumentException("Length of input vector is not 4. It is "+length); output.a1 = input.data[0]; output.a2 = input.data[1]; output.a3 = input.data[2]; output.a4 = input.data[3]; return output; }
/** * <p> * Extracts the diagonal elements 'src' write it to the 'dst' vector. 'dst' * can either be a row or column vector. * <p> * * @param src Matrix whose diagonal elements are being extracted. Not modified. * @param dst A vector the results will be written into. Modified. */ public static void extractDiag(DMatrixRMaj src, DMatrixRMaj dst ) { int N = Math.min(src.numRows, src.numCols); if( !MatrixFeatures_DDRM.isVector(dst) || dst.numCols*dst.numCols != N ) { dst.reshape(N,1); } for( int i = 0; i < N; i++ ) { dst.set( i , src.unsafe_get(i,i) ); } }
/** * Sets the value of the row, column position in the matrix. * * @param row * @param column * @param value */ @Override public void setElement(int row, int column, double value) { mat.set(row, column, value); }
public GaussianGmm_F64 copy() { GaussianGmm_F64 out = new GaussianGmm_F64(mean.getNumElements()); out.mean.set(mean); out.covariance.set(covariance); out.weight = weight; return out; }
@Override public void sub(double scalar, Matrix matrix) { DMatrixRMaj a = internal(matrix); mat.reshape(a.numRows, a.numCols, false); CommonOps_DDRM.subtract(scalar, a, mat); }
@Override public void getDiagonal(double[] diag, double[] off) { diag[0] = UBV.get(0); for( int i = 1; i < n; i++ ) { diag[i] = UBV.unsafe_get(i,i); off[i-1] = UBV.unsafe_get(i-1,i); } }
@Override public DMatrixRMaj getT(DMatrixRMaj T) { int N = Ablock.numRows; if( T == null ) { T = new DMatrixRMaj(N,N); } else { CommonOps_DDRM.fill(T, 0); } double[] diag = new double[ N ]; double[] off = new double[ N ]; ((TridiagonalDecompositionHouseholder_DDRB)alg).getDiagonal(diag,off); T.unsafe_set(0,0,diag[0]); for( int i = 1; i < N; i++ ) { T.unsafe_set(i,i,diag[i]); T.unsafe_set(i,i-1,off[i-1]); T.unsafe_set(i-1,i,off[i-1]); } return T; }
/** * Retrieve projective transform H */ public void computeH( DMatrixRMaj H ) { H.reshape(4,4); CommonOps_DDRM.insert(PA,H,0,0); for (int i = 0; i < 4; i++) { H.unsafe_set(i,3,ns.data[i]); } }