/** Sets each value of the matrix to 0.0. */ @Override public void setZero() { mat.zero(); }
public static DMatrixRMaj checkZeros(DMatrixRMaj A , int numRows , int numCols) { if( A == null ) { return new DMatrixRMaj(numRows,numCols); } else if( numRows != A.numRows || numCols != A.numCols ) throw new IllegalArgumentException("Input is not "+numRows+" x "+numCols+" matrix"); else A.zero(); return A; }
@Override public DMatrixRMaj getColPivotMatrix(DMatrixRMaj P) { if( P == null ) P = new DMatrixRMaj(numCols,numCols); else if( P.numRows != numCols ) throw new IllegalArgumentException("Number of rows must be "+numCols); else if( P.numCols != numCols ) throw new IllegalArgumentException("Number of columns must be "+numCols); else { P.zero(); } for( int i = 0; i < numCols; i++ ) { P.set(pivots[i],i,1); } return P; } }
/** * This is a total hack. Set B to a diagonal matrix where each diagonal element * is the value of the largest absolute value in B. This will be SPD and hopefully * not screw up the search. */ private void resetMatrixB() { // find the magnitude of the largest diagonal element double maxDiag = 0; for( int i = 0; i < N; i++ ) { double d = Math.abs(B.get(i,i)); if( d > maxDiag ) maxDiag = d; } B.zero(); for( int i = 0; i < N; i++ ) { B.set(i,i,maxDiag); } }
@Override public DMatrixRMaj getW(DMatrixRMaj W ) { int m = compact ? numSingular : numRows; int n = compact ? numSingular : numCols; if( W == null ) W = new DMatrixRMaj(m,n); else { W.reshape(m,n, false); W.zero(); } for( int i = 0; i < numSingular; i++ ) { W.data[i*W.numCols+i] = singularValues[i]; } return W; }
/** * output = [a , b] */ public static void concatColumns(DMatrixRMaj a , DMatrixRMaj b , DMatrixRMaj output ) { int rows = Math.max(a.numRows , b.numRows); int cols = a.numCols + b.numCols; output.reshape(rows,cols); output.zero(); insert(a,output,0,0); insert(b,output,0,a.numCols); }
/** * Creates a zeros matrix only if A does not already exist. If it does exist it will fill * the lower triangular portion with zeros. */ public static DMatrixRMaj checkZerosLT(DMatrixRMaj A , int numRows , int numCols) { if( A == null ) { return new DMatrixRMaj(numRows,numCols); } else if( numRows != A.numRows || numCols != A.numCols ) { A.reshape(numRows,numCols); A.zero(); } else { for( int i = 0; i < A.numRows; i++ ) { int index = i*A.numCols; int end = index + Math.min(i,A.numCols);; while( index < end ) { A.data[index++] = 0; } } } return A; }
/** * This is a total hack. Set B to a diagonal matrix where each diagonal element * is the value of the largest absolute value in B. This will be SPD and hopefully * not screw up the search. */ private void resetMatrixB() { // find the magnitude of the largest diagonal element double maxDiag = 0; for( int i = 0; i < N; i++ ) { double d = Math.abs(B.get(i,i)); if( d > maxDiag ) maxDiag = d; } B.zero(); for( int i = 0; i < N; i++ ) { B.set(i,i,maxDiag); } }
@Override public DMatrixRMaj getW(DMatrixRMaj W ) { int m = compact ? numSingular : numRows; int n = compact ? numSingular : numCols; if( W == null ) W = new DMatrixRMaj(m,n); else { W.reshape(m,n, false); W.zero(); } for( int i = 0; i < numSingular; i++ ) { W.data[i*W.numCols+i] = singularValues[i]; } return W; }
/** * output = [a ; b] */ public static void concatRows(DMatrixRMaj a , DMatrixRMaj b , DMatrixRMaj output ) { int rows = a.numRows + b.numRows; int cols = Math.max(a.numCols , b.numCols); output.reshape(rows,cols); output.zero(); insert(a,output,0,0); insert(b,output,a.numRows,0); }
public void initialize(double[] initial) { this.mode = 0; this.hasConverged = false; this.iterations = 0; // set the change in x to be zero s.zero(); // default to an initial inverse Hessian approximation as // the identity matrix. This can be overridden or improved by an heuristic below CommonOps_DDRM.setIdentity(B); // save the initial value of x System.arraycopy(initial, 0, x.data, 0, N); function.setInput(x.data); fx = function.computeFunction(); updated = false; if( verbose != null ) { verbose.println("Steps fx change |step| f-test g-test max-step "); verbose.printf("%-4d %9.3E %10.3E %9.3E %9.3E %9.3E %6.2f\n", iterations, fx, 0.0,0.0,0.0,0.0, 0.0); } }
public static DMatrixRMaj handleB(DMatrixRMaj B, boolean compact, int m , int n , int min ) { int w = n > m ? min + 1 : min; if( compact ) { if( B == null ) { B = new DMatrixRMaj(min,w); } else { B.reshape(min,w, false); B.zero(); } } else { if( B == null ) { B = new DMatrixRMaj(m,n); } else { B.reshape(m,n, false); B.zero(); } } return B; }
@Override public DMatrixRMaj getW(DMatrixRMaj W ) { int m = compact ? numSingular : numRows; int n = compact ? numSingular : numCols; if( W == null ) W = new DMatrixRMaj(m,n); else { W.reshape(m,n, false); W.zero(); } for( int i = 0; i < numSingular; i++ ) { W.unsafe_set(i,i, singularValues[i]); } return W; }
public static DMatrixRMaj convert(DMatrixSparseTriplet src , DMatrixRMaj dst ) { if( dst == null ) dst = new DMatrixRMaj(src.numRows, src.numCols); else { dst.reshape(src.numRows, src.numCols); dst.zero(); } for (int i = 0; i < src.nz_length; i++) { int row = src.nz_rowcol.data[i*2]; int col = src.nz_rowcol.data[i*2+1]; double value = src.nz_value.data[i]; dst.unsafe_set(row, col, value); } return dst; }
public static DMatrixRMaj convert(DMatrixSparseCSC src , DMatrixRMaj dst ) { if( dst == null ) dst = new DMatrixRMaj(src.numRows, src.numCols); else { dst.reshape(src.numRows, src.numCols); dst.zero(); } int idx0 = src.col_idx[0]; for (int j = 1; j <= src.numCols; j++) { int idx1 = src.col_idx[j]; for (int i = idx0; i < idx1; i++) { int row = src.nz_rows[i]; double val = src.nz_values[i]; dst.unsafe_set(row,j-1, val); } idx0 = idx1; } return dst; }
/** * Constructs the linear matrix that describes from the 3-point constraint with linear * dependent rows removed */ protected void createLinearSystem( List<AssociatedTriple> observations ) { int N = observations.size(); A.reshape(4*N,27); A.zero(); for( int i = 0; i < N; i++ ) { AssociatedTriple t = observations.get(i); N1.apply(t.p1,p1_norm); N2.apply(t.p2,p2_norm); N3.apply(t.p3,p3_norm); insert(i,0 , p1_norm.x); // tensor 1 insert(i,1 , p1_norm.y); // tensor 2 insert(i,2 ,1); // tensor 3 } }
/** * Override parent to initialize matrices */ @Override public void initialize(double[] initial, int numberOfParameters, double minimumFunctionValue) { super.initialize(initial, numberOfParameters,minimumFunctionValue); y.reshape(numberOfParameters,1); xPrevious.reshape(numberOfParameters,1); x.reshape(numberOfParameters,1); // set the previous gradient to zero gradientPrevious.reshape(numberOfParameters,1); gradientPrevious.zero(); firstIteration = true; }
/** * Override parent to initialize matrices */ @Override public void initialize(double[] initial, int numberOfParameters, double minimumFunctionValue) { super.initialize(initial, numberOfParameters,minimumFunctionValue); y.reshape(numberOfParameters,1); xPrevious.reshape(numberOfParameters,1); x.reshape(numberOfParameters,1); // set the previous gradient to zero gradientPrevious.reshape(numberOfParameters,1); gradientPrevious.zero(); firstIteration = true; }
/** * Specifies known portions of camera intrinsic parameters * @param skew skew * @param cx image center x * @param cy image center y * @param width Image width * @param height Image height */ public void setCamera( double skew , double cx , double cy , int width , int height ) { // Define normalization matrix // center points, remove skew, scale coordinates double d = Math.sqrt(width*width + height*height); V.zero(); V.set(0,0,d/2); V.set(0,1,skew); V.set(0,2,cx); V.set(1,1,d/2); V.set(1,2,cy); V.set(2,2,1); CommonOps_DDRM.invert(V,Vinv); }
/** * Computes the rectifying homography from the decomposed Q * * H = [K 0; -p'*K 1] see Pg 460 */ public boolean computeRectifyingHomography( DMatrixRMaj H ) { H.reshape(4,4); // insert the results into H // H = [K 0;-p'*K 1 ] H.zero(); for (int i = 0; i < 3; i++) { for (int j = i; j < 3; j++) { H.set(i,j,k.get(i,j)); } } // p and k have different scales, fix that H.set(3,0, -(p.a1*k.a11 + p.a2*k.a21 + p.a3*k.a31)); H.set(3,1, -(p.a1*k.a12 + p.a2*k.a22 + p.a3*k.a32)); H.set(3,2, -(p.a1*k.a13 + p.a2*k.a23 + p.a3*k.a33)); H.set(3,3,1); return true; }