@Override public void solve(D B, D X) { if( alg.modifiesB() ) { if( this.B == null ) { this.B = B.copy(); } else { if( this.B.getNumRows() != B.getNumRows() || this.B.getNumCols() != B.getNumCols() ) { this.B.reshape(A.getNumRows(),B.getNumCols()); } this.B.set(B); } B = this.B; } alg.solve(B,X); }
/** * Extract where the destination is reshaped to match the extracted region * @param src The original matrix which is to be copied. Not modified. * @param srcX0 Start column. * @param srcX1 Stop column+1. * @param srcY0 Start row. * @param srcY1 Stop row+1. * @param dst Where the submatrix are stored. Modified. */ public static void extract( DMatrix src, int srcY0, int srcY1, int srcX0, int srcX1, DMatrix dst ) { ((ReshapeMatrix)dst).reshape(srcY1-srcY0,srcX1-srcX0); extract(src,srcY0,srcY1,srcX0,srcX1,dst,0,0); }
@Override public void setFunction(FunctionNtoM function, FunctionNtoMxN<S> jacobian) { this.functionResiduals = function; if( jacobian == null ) this.functionJacobian = FactoryNumericalDerivative.jacobianForwards(function,(Class)this.jacobian.getClass()); else this.functionJacobian = jacobian; int M = functionResiduals.getNumOfOutputsM(); int N = functionResiduals.getNumOfInputsN(); ((ReshapeMatrix)this.jacobian).reshape(M,N); }
@Override public boolean setA(T A) { if( alg.modifiesA() ) { if( this.A == null ) { this.A = (T)A.copy(); } else { if( this.A.getNumRows() != A.getNumRows() || this.A.getNumCols() != A.getNumCols() ) { this.A.reshape(A.getNumRows(),A.getNumCols()); } this.A.set(A); } return alg.setA(this.A); } return alg.setA(A); }
@Override public void setFunction(FunctionNtoM function, FunctionNtoMxN<S> jacobian) { this.functionResiduals = function; if( jacobian == null ) this.functionJacobian = FactoryNumericalDerivative.jacobianForwards(function,(Class)this.jacobian.getClass()); else this.functionJacobian = jacobian; int M = functionResiduals.getNumOfOutputsM(); int N = functionResiduals.getNumOfInputsN(); ((ReshapeMatrix)this.jacobian).reshape(M,N); }
@Override public void initialize(double[] initial, int numberOfParameters, double minimumFunctionValue) { int M = functionResiduals.getNumOfOutputsM(); int N = functionResiduals.getNumOfInputsN(); residuals.reshape(M,1); // Set the hessian to identity. There are other potentially better methods hessian.init(numberOfParameters); ((ReshapeMatrix)jacobian).reshape(M,N); super.initialize(initial, numberOfParameters, minimumFunctionValue); }
@Override public void initialize(double[] initial, int numberOfParameters, double minimumFunctionValue) { int M = functionResiduals.getNumOfOutputsM(); int N = functionResiduals.getNumOfInputsN(); residuals.reshape(M,1); // Set the hessian to identity. There are other potentially better methods hessian.init(numberOfParameters); ((ReshapeMatrix)jacobian).reshape(M,N); super.initialize(initial, numberOfParameters, minimumFunctionValue); }
/** * 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)); } } }
/** * Generic, but slow, conversion function. * * @param input Input matrix. * @param output Output matrix. */ public static void convert(FMatrix input , FMatrix 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)); } } }