@Override public DMatrixSparseCSC createMatrix() { return new DMatrixSparseCSC(1,1); } }
@Override public void init(int numParameters) { hessian.reshape(numParameters,numParameters); }
@Override public void unsafe_set(int row, int col, double val) { int index = nz_index(row,col); if( index >= 0 ) { nz_values[index] = val; growMaxLength(nz_length*2+1, true);
dst = new DMatrixSparseCSC(src.numRows, src.numCols , src.nz_length); else dst.reshape(src.numRows, src.numCols, src.nz_length); dst.histogramToStructure(hist); System.arraycopy(dst.col_idx,0,hist,0,dst.numCols);
dst = new DMatrixSparseCSC(src.numRows, src.numCols, nonzero); else dst.reshape(src.numRows, src.numCols, nonzero); dst.nz_length = 0;
/** * Specifies shape and number of non-zero elements that can be stored. * * @param numRows Number of rows * @param numCols Number of columns * @param arrayLength Initial maximum number of non-zero elements that can be in the matrix */ public DMatrixSparseCSC(int numRows , int numCols , int arrayLength) { this.numRows = numRows; this.numCols = numCols; this.nz_length = 0; col_idx = new int[ numCols+1 ]; growMaxLength(arrayLength,false); }
@Override public void printNonZero() { String format = "%d %d "+ MatrixIO.DEFAULT_FLOAT_FORMAT+"\n"; System.out.println("Type = "+getType().name()+" , rows = "+numRows+" , cols = "+numCols +" , nz_length = "+ nz_length); for (int col = 0; col < numCols; col++) { int idx0 = col_idx[col]; int idx1 = col_idx[col+1]; for (int i = idx0; i < idx1; i++) { int row = nz_rows[i]; double value = nz_values[i]; System.out.printf(format,row,col,value); } } }
@Test public void simple() { // give it a function where one variable does not effect the output // to make the test more interesting SimpleFunction f = new SimpleFunction(); NumericalJacobianForward_DSCC alg = new NumericalJacobianForward_DSCC(f); DMatrixSparseCSC output = alg.declareMatrixMxN(); alg.process(new double[]{2,3,7},output); // one element should be zero assertEquals(5, output.nz_length); assertEquals(3, output.get(0,0), tol); assertEquals(-36, output.get(0,1), tol); assertEquals(0, output.get(0,2), tol); assertEquals(3, output.get(1,0), tol); assertEquals(2, output.get(1,1), tol); assertEquals(1, output.get(1,2), tol); }
@Override protected void setHessian(HessianMath alg, DMatrixRMaj H) { int M = 3; int N = H.numCols-M; HessianSchurComplement_DSCC hm = (HessianSchurComplement_DSCC)alg; DMatrixSparseCSC SH = new DMatrixSparseCSC(1,1); ConvertDMatrixStruct.convert(H,SH); hm.A.reshape(M,M); hm.B.reshape(M,N); hm.D.reshape(N,N); CommonOps_DSCC.extract(SH,0,M,0,M,hm.A,0,0); CommonOps_DSCC.extract(SH,0,M,M,M+N,hm.B,0,0); CommonOps_DSCC.extract(SH,M,M+N,M,M+N,hm.D,0,0); }
/** * Given the histogram of columns compute the col_idx for the matrix. nz_length is automatically set and * nz_values will grow if needed. * @param histogram histogram of column values in the sparse matrix. modified, see above. */ public void histogramToStructure(int histogram[] ) { col_idx[0] = 0; int index = 0; for (int i = 1; i <= numCols; i++) { col_idx[i] = index += histogram[i-1]; } nz_length = index; growMaxLength( nz_length , false); if( col_idx[numCols] != nz_length ) throw new RuntimeException("Egads"); }
public DSCC(SchurJacobian<DMatrixSparseCSC> function) { super(function); left = new DMatrixSparseCSC(1,1); right = new DMatrixSparseCSC(1,1); }
@Override public void reshape(int numRows, int numCols) { reshape(numRows, numCols,0); }
@Override public void setDiagonals(DMatrixRMaj diag) { for (int i = 0; i < hessian.numRows; i++) { hessian.set(i,i, diag.data[i]); } }
@Override public void reshape( int numRows , int numCols , int arrayLength ) { // OK so technically it is sorted, but forgetting to correctly set this flag is a common mistake so // decided to be conservative and mark it as unsorted so that stuff doesn't blow up this.indicesSorted = false; this.numRows = numRows; this.numCols = numCols; growMaxLength( arrayLength , false); this.nz_length = 0; if( numCols+1 > col_idx.length ) { col_idx = new int[ numCols+1 ]; } else { Arrays.fill(col_idx,0,numCols+1,0); } }
public DSCC(SchurJacobian<DMatrixSparseCSC> function) { super(function); left = new DMatrixSparseCSC(1,1); right = new DMatrixSparseCSC(1,1); }
@Override public void init(int numParameters) { hessian.reshape(numParameters,numParameters); }
@Override public void setDiagonals(DMatrixRMaj diag) { for (int i = 0; i < hessian.numRows; i++) { hessian.set(i,i, diag.data[i]); } }
@Override public DMatrixSparseCSC createMatrix() { return new DMatrixSparseCSC(1,1); } }
/** * Copies the non-zero structure of orig into "this" * @param orig Matrix who's structure is to be copied */ public void copyStructure( DMatrixSparseCSC orig ) { reshape(orig.numRows, orig.numCols, orig.nz_length); this.nz_length = orig.nz_length; System.arraycopy(orig.col_idx,0,col_idx,0,orig.numCols+1); System.arraycopy(orig.nz_rows,0,nz_rows,0,orig.nz_length); }