@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"); } }
@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 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("}};"); } }
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; }
@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); }
@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"); } }
@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"); } }
public static void print(PrintStream out , DMatrix mat , String format , int row0 , int row1, int col0 , int col1 ) { out.println("Type = submatrix , rows "+row0+" to "+row1+" columns "+col0+" to "+col1); format += " "; for( int y = row0; y < row1; y++ ) { for( int x = col0; x < col1; x++ ) { out.printf(format,mat.get(y,x)); } out.println(); } }
@Override public void set(Matrix original) { DMatrix m = (DMatrix)original; if( m.getNumCols() == 1 && m.getNumRows() == 5 ) { a1 = m.get(0,0); a2 = m.get(1,0); a3 = m.get(2,0); a4 = m.get(3,0); a5 = m.get(4,0); } else if( m.getNumRows() == 1 && m.getNumCols() == 5 ){ a1 = m.get(0,0); a2 = m.get(0,1); a3 = m.get(0,2); a4 = m.get(0,3); a5 = m.get(0,4); } else { throw new IllegalArgumentException("Incompatible shape"); } }
public static void extract(DMatrix src, int srcY0, int srcX0, DMatrix dst, int dstY0, int dstX0, int numRows, int numCols ) { for( int y = 0; y < numRows; y++ ) { for( int x = 0; x < numCols; x++ ) { double v = src.get(y+srcY0,x+srcX0); dst.set(dstY0+y , dstX0 +x, v); } } } }
/** * 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)); } } }
@Override public void set(Matrix original) { if( original instanceof DMatrixRBlock) { set((DMatrixRBlock)original); } else { DMatrix m = (DMatrix) original; for (int i = 0; i < numRows; i++) { for (int j = 0; j < numCols; j++) { set(i, j, m.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)); } } }
@Override public void set(Matrix original) { DMatrix m = (DMatrix)original; reshape(original.getNumRows(),original.getNumCols()); if( original instanceof DMatrixRMaj) { // do a faster copy if its of type DMatrixRMaj System.arraycopy(((DMatrixRMaj)m).data,0,data,0,numRows*numCols); } else { int index = 0; for (int i = 0; i < numRows; i++) { for (int j = 0; j < numCols; j++) { data[index++] = m.get(i, j); } } } }
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(" ]"); } }
@Override public void process(double[] input, double []output) { func.process(input,funcOutput); deriv.process(input, J); int N = deriv.getNumOfInputsN(); int M = deriv.getNumOfOutputsM(); for( int i = 0; i < N; i++ ) { output[i] = 0; } for( int i = 0; i < M; i++ ) { double f = funcOutput[i]; for( int j = 0; j < N; j++ ) { output[j] += 2*f*J.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 process(double[] input, double []output) { func.process(input,funcOutput); deriv.process(input, J); int N = deriv.getNumOfInputsN(); int M = deriv.getNumOfOutputsM(); for( int i = 0; i < N; i++ ) { output[i] = 0; } for( int i = 0; i < M; i++ ) { double f = funcOutput[i]; for( int j = 0; j < N; j++ ) { output[j] += 2*f*J.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(); } }