@Override public void set(Matrix original) { DMatrix m = (DMatrix)original; if( m.getNumCols() == 1 && m.getNumRows() == 2 ) { a1 = m.get(0,0); a2 = m.get(1,0); } else if( m.getNumRows() == 1 && m.getNumCols() == 2 ){ a1 = m.get(0,0); a2 = m.get(0,1); } else { throw new IllegalArgumentException("Incompatible shape"); } }
/** * <p> * Checks to see if each element in the two matrices are equal: * a<sub>ij</sub> == b<sub>ij</sub> * <p> * * <p> * NOTE: If any of the elements are NaN then false is returned. If two corresponding * elements are both positive or negative infinity then they are equal. * </p> * * @param a A matrix. Not modified. * @param b A matrix. Not modified. * @return true if identical and false otherwise. */ public static boolean isEquals(DMatrix a, DMatrix b ) { if( a.getNumRows() != b.getNumRows() || a.getNumCols() != b.getNumCols() ) return false; final int numRows = a.getNumRows(); final int numCols = a.getNumCols(); for (int row = 0; row < numRows; row++) { for (int col = 0; col < numCols; col++) { if( !(a.unsafe_get(row,col) == b.unsafe_get(row,col))) return false; } } return true; }
public static void copy(DMatrix from , DMatrix to ) { int numCols = from.getNumCols(); int numRows = from.getNumRows(); for( int i = 0; i < numRows; i++ ) { for( int j = 0; j < numCols; j++ ) { to.set(i,j,from.get(i,j)); } } }
/** * Inserts matrix 'src' into matrix 'dest' with the (0,0) of src at (row,col) in dest. * This is equivalent to calling extract(src,0,src.numRows,0,src.numCols,dest,destY0,destX0). * * @param src matrix that is being copied into dest. Not modified. * @param dest Where src is being copied into. Modified. * @param destY0 Start row for the copy into dest. * @param destX0 Start column for the copy into dest. */ public static void insert(DMatrix src, DMatrix dest, int destY0, int destX0) { extract(src, 0, src.getNumRows(), 0, src.getNumCols(), dest, destY0, destX0); }
/** * Assert equals with a relative error */ public static void assertRelativeEquals(DMatrix A , DMatrix B , double tol ) { assertShape(A,B); for( int i = 0; i < A.getNumRows(); i++ ){ for( int j = 0; j < A.getNumCols(); j++ ) { double valA = A.get(i,j); double valB = B.get(i,j); if( (Double.isNaN(valA) != Double.isNaN(valB)) || (Double.isInfinite(valA) != Double.isInfinite(valB))) { throw new AssertionError("At ("+i+","+j+") A = "+valA+" B = "+valB); } double max = Math.max(Math.abs(valA), Math.abs(valB)); double error = Math.abs(valA - valB) / max; if( error > tol ) { System.out.println("------------ A -----------"); A.print(); System.out.println("\n------------ B -----------"); B.print(); throw new AssertionError("At (" + i + "," + j + ") A = " + valA + " B = " + valB + " error = " + error); } } } }
public static void printJava(PrintStream out , DMatrix mat , String format ) { String type = mat.getType().getBits() == 64 ? "double" : "float"; out.println("new "+type+"[][]{"); format += " "; for( int y = 0; y < mat.getNumRows(); y++ ) { out.print("{"); for( int x = 0; x < mat.getNumCols(); x++ ) { out.printf(format,mat.get(y,x)); if( x+1<mat.getNumCols()) out.print(", "); } if( y+1 < mat.getNumRows()) out.println("},"); else out.println("}};"); } }
/** * Generic, but slow, conversion function. * * @param input Input matrix. * @param output Output matrix. */ public static void convert(DMatrix input , DMatrix output ) { if( output instanceof ReshapeMatrix ) { ((ReshapeMatrix)output).reshape(input.getNumRows(),input.getNumCols()); } else { if (input.getNumRows() != output.getNumRows()) throw new IllegalArgumentException("Number of rows do not match"); if (input.getNumCols() != output.getNumCols()) throw new IllegalArgumentException("Number of columns do not match"); } for( int i = 0; i < input.getNumRows(); i++ ) { for( int j = 0; j < input.getNumCols(); j++ ) { output.unsafe_set(i,j,input.unsafe_get(i,j)); } } }
public static void setRandom(DMatrix a , double min , double max , Random rand ) { for( int i = 0; i < a.getNumRows(); i++ ) { for( int j = 0; j < a.getNumCols(); j++ ) { double val = rand.nextDouble()*(max-min)+min; a.set(i,j,val); } } } }
@Override public void set(Matrix original) { if( original.getNumCols() != 4 || original.getNumRows() != 4 ) throw new IllegalArgumentException("Rows and/or columns do not match"); DMatrix m = (DMatrix)original; a11 = m.get(0,0); a12 = m.get(0,1); a13 = m.get(0,2); a14 = m.get(0,3); a21 = m.get(1,0); a22 = m.get(1,1); a23 = m.get(1,2); a24 = m.get(1,3); a31 = m.get(2,0); a32 = m.get(2,1); a33 = m.get(2,2); a34 = m.get(2,3); a41 = m.get(3,0); a42 = m.get(3,1); a43 = m.get(3,2); a44 = m.get(3,3); }
public static void convert(FMatrix input , DMatrix output ) { if( input.getNumRows() != output.getNumRows() ) throw new IllegalArgumentException("Number of rows do not match"); if( input.getNumCols() != output.getNumCols() ) throw new IllegalArgumentException("Number of columns do not match"); for( int i = 0; i < input.getNumRows(); i++ ) { for( int j = 0; j < input.getNumCols(); j++ ) { output.unsafe_set(i,j, input.unsafe_get(i,j)); } } }
found.print(); System.out.println("-----------------------------"); System.out.println("Numerical"); for( int y = 0; y < expected.numRows; y++ ) { for( int x = 0; x < expected.numCols; x++ ) { double diff = Math.abs(found.unsafe_get(y,x)-expected.unsafe_get(y,x)); if( diff > tol ) {
/** * A simple convinience function that decomposes the matrix but automatically checks the input ti make * sure is not being modified. * * @param decomp Decomposition which is being wrapped * @param M THe matrix being decomposed. * @param <T> Matrix type. * @return If the decomposition was successful or not. */ public static <T extends DMatrix> boolean decomposeSafe(DecompositionInterface<T> decomp, T M ) { if( decomp.inputModified() ) { return decomp.decompose(M.<T>copy()); } else { return decomp.decompose(M); } } }
/** * <p> * Extracts a submatrix from 'src' and inserts it in a submatrix in 'dst'. Uses the shape of dst * to determine the size of the matrix extracted. * </p> * * @param src The original matrix which is to be copied. Not modified. * @param srcY0 Start row in src. * @param srcX0 Start column in src. * @param dst Where the matrix is extracted into. */ public static void extract( DMatrix src, int srcY0, int srcX0, DMatrix dst ) { extract(src,srcY0,srcY0+dst.getNumRows(),srcX0,srcX0+dst.getNumCols(),dst,0,0); }
@Override public void set(Matrix original) { if( original.getNumCols() != 3 || original.getNumRows() != 3 ) throw new IllegalArgumentException("Rows and/or columns do not match"); DMatrix m = (DMatrix)original; a11 = m.get(0,0); a12 = m.get(0,1); a13 = m.get(0,2); a21 = m.get(1,0); a22 = m.get(1,1); a23 = m.get(1,2); a31 = m.get(2,0); a32 = m.get(2,1); a33 = m.get(2,2); }
found.print(); System.out.println("-----------------------------"); System.out.println("Numerical"); for( int y = 0; y < expected.numRows; y++ ) { for( int x = 0; x < expected.numCols; x++ ) { double diff = Math.abs(found.unsafe_get(y,x)-expected.unsafe_get(y,x)); if( diff > tol ) {
public static boolean isEquivalent(DMatrix a , DMatrix b , double tol ) { if( a.getNumRows() != b.getNumRows() || a.getNumCols() != b.getNumCols() ) return false; for( int i = 0; i < a.getNumRows(); i++ ) { for( int j = 0; j < a.getNumCols(); j++ ) { double diff = Math.abs(a.get(i,j) - b.get(i,j)); if( diff > tol ) return false; } } return true; }
public static DMatrixSparseTriplet convert(DMatrix src , DMatrixSparseTriplet dst , double tol ) { if( dst == null ) dst = new DMatrixSparseTriplet(src.getNumRows(), src.getNumCols(), 1); else dst.reshape(src.getNumRows(), src.getNumCols()); for (int row = 0; row < src.getNumRows(); row++) { for (int col = 0; col < src.getNumCols(); col++) { double value = src.unsafe_get(row,col); if( Math.abs(value) > tol ) dst.addItem(row,col,value); } } return dst; }