/** * 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); }
/** * Constructs a {@code numRow} × {@code numCol} matrix initialized to the values * in the {@code matrix} array. The array values are copied in one row at a time in row major * fashion. The array should be exactly <code>numRow*numCol</code> in length. Note that because * row and column numbering begins with zero, {@code numRow} and {@code numCol} will be one * larger than the maximum possible matrix index values. * * @param numRow Number of rows. * @param numCol Number of columns. * @param matrix Initial values in row order */ public GeneralMatrix(final int numRow, final int numCol, final double... matrix) { mat = new DMatrixRMaj(numRow, numCol, true, matrix); if (numRow * numCol != matrix.length) { throw new IllegalArgumentException(String.valueOf(matrix.length)); } }
/** * Constructs a square identity matrix of size {@code size} × {@code size}. * * @param size The number of rows and columns. */ public GeneralMatrix(final int size) { mat = new DMatrixRMaj(size, size); setIdentity(); }
/** * Creates a matrix of size {@code numRow} × {@code numCol}. Elements on the * diagonal <var>j==i</var> are set to 1. * * @param numRow Number of rows. * @param numCol Number of columns. */ public GeneralMatrix(final int numRow, final int numCol) { mat = new DMatrixRMaj(numRow, numCol); setIdentity(); }
/** * Resize the matrix to the specified number of rows and columns (preserving remaining * elements). * * @param numRows The new number of rows in the matrix. * @param numCols The new number of columns in the matrix. */ public void setSize(int numRows, int numCols) { if (numRows != mat.numCols || numCols != mat.numCols) { // grow or shrink DMatrixRMaj ret = new DMatrixRMaj(numRows, numCols); CommonOps_DDRM.extract(mat, 0, numRows, 0, numCols, ret, 0, 0); mat = ret; } }
/** * In-place multiply with provided matrix. * * @param matrix */ public final void mul(Matrix matrix) { DMatrixRMaj b = internal(matrix); DMatrixRMaj ret = new DMatrixRMaj(mat.numRows, b.numCols); CommonOps_DDRM.mult(mat, b, ret); mat = ret; }
@Override public void invert(Matrix matrix) throws SingularMatrixException { DMatrixRMaj a; if (matrix instanceof GeneralMatrix) { a = new DMatrixRMaj(((GeneralMatrix) matrix).mat); } else { a = new DMatrixRMaj(matrix.getNumRow(), matrix.getNumCol()); for (int j = 0; j < mat.numRows; j++) { for (int i = 0; i < mat.numCols; i++) { mat.set(j, i, matrix.getElement(j, i)); } } } boolean success = CommonOps_DDRM.invert(a); if (!success) { throw new SingularMatrixException("Could not invert, possible singular matrix?"); } this.mat = a; }
@Override public void mul(double scalar, Matrix matrix) { DMatrixRMaj a = new DMatrixRMaj(matrix.getNumRow(), matrix.getNumCol()); CommonOps_DDRM.scale(scalar, internal(matrix), a); mat = a; }
/** * Constructs a new matrix and copies the initial values from the parameter matrix. * * @param matrix The matrix to copy. */ public GeneralMatrix(final Matrix matrix) { if (matrix instanceof GeneralMatrix) { mat = new DMatrixRMaj(((GeneralMatrix) matrix).mat); } else { mat = new DMatrixRMaj(matrix.getNumRow(), matrix.getNumCol()); final int height = getNumRow(); final int width = getNumCol(); for (int j = 0; j < height; j++) { for (int i = 0; i < width; i++) { mat.set(j, i, matrix.getElement(j, i)); } } } }
/** * 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]); } } }
/** * 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); }
/** * Constructs a {@code numRow} × {@code numCol} matrix initialized to the values * in the {@code matrix} array. The array values are copied in one row at a time in row major * fashion. The array should be exactly <code>numRow*numCol</code> in length. Note that because * row and column numbering begins with zero, {@code numRow} and {@code numCol} will be one * larger than the maximum possible matrix index values. * * @param numRow Number of rows. * @param numCol Number of columns. * @param matrix Initial values in row order */ public GeneralMatrix(final int numRow, final int numCol, final Matrix matrix) { mat = new DMatrixRMaj(numRow, numCol); if (matrix.getNumRow() != numRow || matrix.getNumCol() != numCol) { throw new IllegalArgumentException(Errors.format(ErrorKeys.ILLEGAL_MATRIX_SIZE)); } for (int j = 0; j < numRow; j++) { for (int i = 0; i < numCol; i++) { setElement(j, i, matrix.getElement(j, i)); } } }
/** * Cast (or convert) Matrix to internal DMatrixRMaj representation required for CommonOps_DDRM. * * @param matrix * @return */ private DMatrixRMaj internal(Matrix matrix) { if (matrix instanceof GeneralMatrix) { return ((GeneralMatrix) matrix).mat; } else { DMatrixRMaj a = new DMatrixRMaj(matrix.getNumRow(), matrix.getNumCol()); for (int j = 0; j < a.numRows; j++) { for (int i = 0; i < a.numCols; i++) { a.set(j, i, matrix.getElement(j, i)); } } return a; } }
@Override public DMatrixRMaj createMatrix() { return new DMatrixRMaj(1,1); } }
@Override public DMatrixRMaj declareMatrixMxN() { return new DMatrixRMaj(M,N); } }
/** * Creates a decompose that defines the specified amount of memory. * * @param numElements number of elements in the matrix. */ public BidiagonalDecompositionRow_DDRM(int numElements) { UBV = new DMatrixRMaj(numElements); gammasU = new double[ numElements ]; gammasV = new double[ numElements ]; b = new double[ numElements ]; u = new double[ numElements ]; }
mat = new DMatrixRMaj(dstRegion.getDimension() + 1, srcRegion.getDimension() + 1);
public static DMatrixRMaj checkZeros(DMatrixRMaj A , int numRows , int numCols) { if( A == null ) { return new DMatrixRMaj(numRows,numCols); } else if( numRows != A.numRows || numCols != A.numCols ) throw new IllegalArgumentException("Input is not "+numRows+" x "+numCols+" matrix"); else A.zero(); return A; }
public static double quality(DMatrixRMaj orig , DMatrixRMaj U , DMatrixRMaj W , DMatrixRMaj Vt ) { // foundA = U*W*Vt DMatrixRMaj UW = new DMatrixRMaj(U.numRows,W.numCols); CommonOps_DDRM.mult(U,W,UW); DMatrixRMaj foundA = new DMatrixRMaj(UW.numRows,Vt.numCols); CommonOps_DDRM.mult(UW,Vt,foundA); double normA = NormOps_DDRM.normF(foundA); return SpecializedOps_DDRM.diffNormF(orig,foundA)/normA; }
public DMatrixRMaj matrix() { DMatrixRMaj M = new DMatrixRMaj(3,3); M.set(0,0,1.0/stdX); M.set(1,1,1.0/stdY); M.set(0,2,-meanX/stdX); M.set(1,2,-meanY/stdY); M.set(2,2,1); return M; }