public static void printMatlab(PrintStream out , DMatrix mat ) { out.print("[ "); for( int row = 0; row < mat.getNumRows(); row++ ) { for( int col = 0; col < mat.getNumCols(); col++ ) { out.printf("%.12E",mat.get(row,col)); if( col+1 < mat.getNumCols() ) { out.print(" , "); } } if( row+1 < mat.getNumRows() ) out.println(" ;"); else out.println(" ]"); } }
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); } } } }
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; }
/** * 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); }
/** * 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 printFancy(PrintStream out , DMatrix mat , int length ) { printTypeSize(out, mat); DecimalFormat format = new DecimalFormat("#"); final int cols = mat.getNumCols(); for (int row = 0; row < mat.getNumRows(); row++) { for (int col = 0; col < cols; col++) { out.print(fancyStringF(mat.get(row,col),format,length, 4)); if( col != cols-1 ) out.print(" "); } out.println(); } }
private static <S extends DMatrix> void checkJacobianShape(S found, DMatrixRMaj expected) { if( expected.getNumRows() != found.getNumRows() || expected.getNumCols() != found.getNumCols() ) { String message = "Expected "+expected.getNumRows()+"x"+expected.getNumCols() + " Found "+found.getNumRows()+"x"+found.getNumCols(); throw new RuntimeException("Unexpected jacobian shape. "+message); } }
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; }
public static void convert(DMatrix input , ZMatrix 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.set(i,j, input.unsafe_get(i,j), 0); } } }
public static void convert(DMatrix input , CMatrix 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.set(i,j, (float)input.unsafe_get(i,j), 0); } } }
@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> * 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); }
private static <S extends DMatrix> void checkJacobianShape(S found, DMatrixRMaj expected) { if( expected.getNumRows() != found.getNumRows() || expected.getNumCols() != found.getNumCols() ) { String message = "Expected "+expected.getNumRows()+"x"+expected.getNumCols() + " Found "+found.getNumRows()+"x"+found.getNumCols(); throw new RuntimeException("Unexpected jacobian shape. "+message); } }
/** * Creates a new DMatrixRMaj which contains the same information as the provided Matrix64F. * * @param mat Matrix whose values will be copied. Not modified. */ public DMatrixRMaj(DMatrix mat) { this(mat.getNumRows(),mat.getNumCols()); for( int i = 0; i < numRows; i++ ) { for( int j = 0; j < numCols; j++ ) { set(i,j, mat.get(i,j)); } } }
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)); } } }
/** * Checks to see if every element in A is countable. A doesn't have any element with * a value of NaN or infinite. * * @param A Matrix */ public static void assertCountable( DMatrix A ) { for( int i = 0; i < A.getNumRows(); i++ ){ for( int j = 0; j < A.getNumCols(); j++ ) { assertTrue( !Double.isNaN(A.get(i,j)) , "NaN found at "+i+" "+j ); assertTrue( !Double.isInfinite(A.get(i,j)) , "Infinite found at "+i+" "+j ); } } }
@Override public void set(Matrix original) { DMatrix m = (DMatrix)original; if( m.getNumCols() == 1 && m.getNumRows() == 3 ) { a1 = m.get(0,0); a2 = m.get(1,0); a3 = m.get(2,0); } else if( m.getNumRows() == 1 && m.getNumCols() == 3 ){ a1 = m.get(0,0); a2 = m.get(0,1); a3 = m.get(0,2); } else { throw new IllegalArgumentException("Incompatible shape"); } }
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)); } } }
@Override public void set(Matrix original) { DMatrix m = (DMatrix)original; if( m.getNumCols() == 1 && m.getNumRows() == 4 ) { a1 = m.get(0,0); a2 = m.get(1,0); a3 = m.get(2,0); a4 = m.get(3,0); } else if( m.getNumRows() == 1 && m.getNumCols() == 4 ){ a1 = m.get(0,0); a2 = m.get(0,1); a3 = m.get(0,2); a4 = m.get(0,3); } else { throw new IllegalArgumentException("Incompatible shape"); } }
@Test public void createMatrix() { T a = alg.createMatrix(); assertEquals(1,a.getNumCols()); assertEquals(1,a.getNumRows()); } }