/** Constructs and returns a new <i>sub-range view</i> which is the sub matrix <tt>A[fromRow..toRow,fromColumn..toColumn]</tt>. The returned matrix is backed by this matrix, so changes in the returned matrix are reflected in this matrix, and vice-versa. Use idioms like <tt>result = subMatrix(...).copy()</tt> to generate an independent sub matrix. @param A the source matrix. @param fromRow The index of the first row (inclusive). @param toRow The index of the last row (inclusive). @param fromColumn The index of the first column (inclusive). @param toColumn The index of the last column (inclusive). @return a new sub-range view. @throws IndexOutOfBoundsException if <tt>fromColumn<0 || toColumn-fromColumn+1<0 || toColumn>=A.columns() || fromRow<0 || toRow-fromRow+1<0 || toRow>=A.rows()</tt> */ public DoubleMatrix2D subMatrix(DoubleMatrix2D A, int fromRow, int toRow, int fromColumn, int toColumn) { return A.viewPart(fromRow, fromColumn, toRow-fromRow+1, toColumn-fromColumn+1); } /**
/** Constructs and returns a new <i>sub-range view</i> which is the sub matrix <tt>A[fromRow..toRow,fromColumn..toColumn]</tt>. The returned matrix is backed by this matrix, so changes in the returned matrix are reflected in this matrix, and vice-versa. Use idioms like <tt>result = subMatrix(...).copy()</tt> to generate an independent sub matrix. @param A the source matrix. @param fromRow The index of the first row (inclusive). @param toRow The index of the last row (inclusive). @param fromColumn The index of the first column (inclusive). @param toColumn The index of the last column (inclusive). @return a new sub-range view. @throws IndexOutOfBoundsException if <tt>fromColumn<0 || toColumn-fromColumn+1<0 || toColumn>=A.columns() || fromRow<0 || toRow-fromRow+1<0 || toRow>=A.rows()</tt> */ public DoubleMatrix2D subMatrix(DoubleMatrix2D A, int fromRow, int toRow, int fromColumn, int toColumn) { return A.viewPart(fromRow, fromColumn, toRow-fromRow+1, toColumn-fromColumn+1); } /**
/** Returns the left singular vectors <tt>U</tt>. @return <tt>U</tt> */ public DoubleMatrix2D getU() { //return new DoubleMatrix2D(U,m,Math.min(m+1,n)); return DoubleFactory2D.dense.make(U).viewPart(0,0,m,Math.min(m+1,n)); } /**
/** Returns the left singular vectors <tt>U</tt>. @return <tt>U</tt> */ public DoubleMatrix2D getU() { //return new DoubleMatrix2D(U,m,Math.min(m+1,n)); return DoubleFactory2D.dense.make(U).viewPart(0,0,m,Math.min(m+1,n)); } /**
/** */ public static void doubleTest() { int rows = 4; int columns = 5; // make a 4*5 matrix DoubleMatrix2D master = new DenseDoubleMatrix2D(rows,columns); System.out.println(master); master.assign(1); // set all cells to 1 System.out.println("\n"+master); master.viewPart(2,1,2,3).assign(2); // set [2,1] .. [3,3] to 2 System.out.println("\n"+master); DoubleMatrix2D copyPart = master.viewPart(2,1,2,3).copy(); copyPart.assign(3); // modify an independent copy copyPart.set(0,0,4); System.out.println("\n"+copyPart); // has changed System.out.println("\n"+master); // master has not changed DoubleMatrix2D view1 = master.viewPart(0,3,4,2); // [0,3] .. [3,4] DoubleMatrix2D view2 = view1.viewPart(0,0,4,1); // a view from a view System.out.println("\n"+view1); System.out.println("\n"+view2); } /**
/** */ public static void doubleTest() { int rows = 4; int columns = 5; // make a 4*5 matrix DoubleMatrix2D master = new DenseDoubleMatrix2D(rows,columns); System.out.println(master); master.assign(1); // set all cells to 1 System.out.println("\n"+master); master.viewPart(2,1,2,3).assign(2); // set [2,1] .. [3,3] to 2 System.out.println("\n"+master); DoubleMatrix2D copyPart = master.viewPart(2,1,2,3).copy(); copyPart.assign(3); // modify an independent copy copyPart.set(0,0,4); System.out.println("\n"+copyPart); // has changed System.out.println("\n"+master); // master has not changed DoubleMatrix2D view1 = master.viewPart(0,3,4,2); // [0,3] .. [3,4] DoubleMatrix2D view2 = view1.viewPart(0,0,4,1); // a view from a view System.out.println("\n"+view1); System.out.println("\n"+view2); } /**
/** */ public static void doubleTest7() { int rows = 4; int columns = 5; // make a 4*5 matrix DoubleMatrix2D master = Factory2D.ascending(rows,columns); //master.assign(1); // set all cells to 1 System.out.println("\n"+master); //master.viewPart(2,0,2,3).assign(2); // set [2,1] .. [3,3] to 2 //System.out.println("\n"+master); int[] rowIndexes = {0,1,3,0}; int[] columnIndexes = {0,2}; DoubleMatrix2D view1 = master.viewSelection(rowIndexes,columnIndexes); System.out.println("view1="+view1); DoubleMatrix2D view2 = view1.viewPart(0,0,2,2); System.out.println("view2="+view2); view2.assign(-1); System.out.println("master replaced"+master); System.out.println("flip1 replaced"+view1); System.out.println("flip2 replaced"+view2); } /**
/** */ public static void doubleTest7() { int rows = 4; int columns = 5; // make a 4*5 matrix DoubleMatrix2D master = Factory2D.ascending(rows,columns); //master.assign(1); // set all cells to 1 System.out.println("\n"+master); //master.viewPart(2,0,2,3).assign(2); // set [2,1] .. [3,3] to 2 //System.out.println("\n"+master); int[] rowIndexes = {0,1,3,0}; int[] columnIndexes = {0,2}; DoubleMatrix2D view1 = master.viewSelection(rowIndexes,columnIndexes); System.out.println("view1="+view1); DoubleMatrix2D view2 = view1.viewPart(0,0,2,2); System.out.println("view2="+view2); view2.assign(-1); System.out.println("master replaced"+master); System.out.println("flip1 replaced"+view1); System.out.println("flip2 replaced"+view2); } /**
/** * * @param ASymm symm matrix filled in its subdiagonal elements * @param r the index of the row * @return */ public static double getRowInfinityNorm(final DoubleMatrix2D ASymm, final int r){ final double[] maxValueHolder = new double[]{-Double.MAX_VALUE}; IntIntDoubleFunction myFunct = new IntIntDoubleFunction() { @Override public double apply(int i, int j, double pij) { maxValueHolder[0] = Math.max(maxValueHolder[0], Math.abs(pij)); return pij; } }; //view A row from starting element to diagonal DoubleMatrix2D AR = ASymm.viewPart(r, 0, 1, r+1); AR.forEachNonZero(myFunct); //view A col from diagonal to final element DoubleMatrix2D AC = ASymm.viewPart(r, r, ASymm.rows()-r, 1); AC.forEachNonZero(myFunct); return maxValueHolder[0]; }
@Override public double apply(final int i, final int j, final double aij) { rowHolder[0] = i; colHolder[0] = j; valueHolder[0] = aij; //log.debug("a(" + i + "," + j + "): " + aij); DoubleMatrix2D AACol = AA.viewPart(0, j, AA.rows(), 1); //log.debug("ACol(" + j + "): " + ArrayUtils.toString(AACol.toArray())); // ACol.forEachNonZero(new IntIntDoubleFunction() { // public double apply(int r, int c, double AColrc) { // if (c < r + 1) { // logger.debug("sum " + AColrc + "*" + H.getQuick(j, j) + " to AHAT(" + r + "," + i + ")"); // ret.setQuick(r, i, ret.getQuick(r, i) + aij * AColrc * H.getQuick(j, j)); // } // return AColrc; // } // }); AACol.forEachNonZero(myFunc); return aij; } });
/** Modifies the matrix to be an upper triangular matrix. @return <tt>A</tt> (for convenience only). @see #triangulateLower(DoubleMatrix2D) */ protected DoubleMatrix2D upperTriangular(DoubleMatrix2D A) { int rows = A.rows(); int columns = A.columns(); int min = Math.min(rows,columns); for (int r = min; --r >= 0; ) { for (int c = min; --c >= 0; ) { if (r > c) A.setQuick(r,c, 0); } } if (columns<rows) A.viewPart(min,0,rows-min,columns).assign(0); return A; } /**
/** Modifies the matrix to be an upper triangular matrix. @return <tt>A</tt> (for convenience only). @see #triangulateLower(DoubleMatrix2D) */ protected DoubleMatrix2D upperTriangular(DoubleMatrix2D A) { int rows = A.rows(); int columns = A.columns(); int min = Math.min(rows,columns); for (int r = min; --r >= 0; ) { for (int c = min; --c >= 0; ) { if (r > c) A.setQuick(r,c, 0); } } if (columns<rows) A.viewPart(min,0,rows-min,columns).assign(0); return A; } /**
/** Constructs a diagonal block matrix from the given parts (the <i>direct sum</i> of two matrices). That is the concatenation <pre> A 0 0 B </pre> (The direct sum has <tt>A.rows()+B.rows()</tt> rows and <tt>A.columns()+B.columns()</tt> columns). Cells are copied. @return a new matrix which is the direct sum. */ public DoubleMatrix2D composeDiagonal(DoubleMatrix2D A, DoubleMatrix2D B) { int ar = A.rows(); int ac = A.columns(); int br = B.rows(); int bc = B.columns(); DoubleMatrix2D sum = make(ar+br, ac+bc); sum.viewPart(0,0,ar,ac).assign(A); sum.viewPart(ar,ac,br,bc).assign(B); return sum; } /**
/** Constructs a diagonal block matrix from the given parts (the <i>direct sum</i> of two matrices). That is the concatenation <pre> A 0 0 B </pre> (The direct sum has <tt>A.rows()+B.rows()</tt> rows and <tt>A.columns()+B.columns()</tt> columns). Cells are copied. @return a new matrix which is the direct sum. */ public DoubleMatrix2D composeDiagonal(DoubleMatrix2D A, DoubleMatrix2D B) { int ar = A.rows(); int ac = A.columns(); int br = B.rows(); int bc = B.columns(); DoubleMatrix2D sum = make(ar+br, ac+bc); sum.viewPart(0,0,ar,ac).assign(A); sum.viewPart(ar,ac,br,bc).assign(B); return sum; } /**
/** Constructs a diagonal block matrix from the given parts. The concatenation has the form <pre> A 0 0 0 B 0 0 0 C </pre> from the given parts. Cells are copied. */ public DoubleMatrix2D composeDiagonal(DoubleMatrix2D A, DoubleMatrix2D B, DoubleMatrix2D C) { DoubleMatrix2D diag = make(A.rows()+B.rows()+C.rows(), A.columns()+B.columns()+C.columns()); diag.viewPart(0,0,A.rows(),A.columns()).assign(A); diag.viewPart(A.rows(),A.columns(),B.rows(),B.columns()).assign(B); diag.viewPart(A.rows()+B.rows(),A.columns()+B.columns(),C.rows(),C.columns()).assign(C); return diag; } /**
/** Constructs a diagonal block matrix from the given parts. The concatenation has the form <pre> A 0 0 0 B 0 0 0 C </pre> from the given parts. Cells are copied. */ public DoubleMatrix2D composeDiagonal(DoubleMatrix2D A, DoubleMatrix2D B, DoubleMatrix2D C) { DoubleMatrix2D diag = make(A.rows()+B.rows()+C.rows(), A.columns()+B.columns()+C.columns()); diag.viewPart(0,0,A.rows(),A.columns()).assign(A); diag.viewPart(A.rows(),A.columns(),B.rows(),B.columns()).assign(B); diag.viewPart(A.rows()+B.rows(),A.columns()+B.columns(),C.rows(),C.columns()).assign(C); return diag; } /**
public Matrix[] lu() { if (getColumnCount() > getRowCount()) { throw new RuntimeException("only supported for m>=n"); } LUDecomposition lu = new LUDecomposition(matrix); Matrix l = new ColtDenseDoubleMatrix2D(lu.getL()); Matrix u = new ColtDenseDoubleMatrix2D(lu.getU().viewPart(0, 0, (int) getColumnCount(), (int) getColumnCount())); int[] piv = lu.getPivot(); int m = (int) getRowCount(); Matrix p = new ColtDenseDoubleMatrix2D(m, m); for (int i = 0; i < m; i++) { p.setAsDouble(1, i, piv[i]); } return new Matrix[] { l, u, p }; }
public Matrix[] lu() { if (getColumnCount() > getRowCount()) { throw new RuntimeException("only supported for m>=n"); } LUDecomposition lu = new LUDecomposition(matrix); Matrix l = new ColtDenseDoubleMatrix2D(lu.getL()); Matrix u = new ColtDenseDoubleMatrix2D(lu.getU().viewPart(0, 0, (int) getColumnCount(), (int) getColumnCount())); int[] piv = lu.getPivot(); int m = (int) getRowCount(); Matrix p = new ColtDenseDoubleMatrix2D(m, m); for (int i = 0; i < m; i++) { p.setAsDouble(1, i, piv[i]); } return new Matrix[] { l, u, p }; }
public Matrix[] lu() { if (getColumnCount() > getRowCount()) { throw new RuntimeException("only supported for m>=n"); } LUDecomposition lu = new LUDecomposition(matrix); Matrix l = new ColtDenseDoubleMatrix2D(lu.getL()); Matrix u = new ColtDenseDoubleMatrix2D(lu.getU().viewPart(0, 0, (int) getColumnCount(), (int) getColumnCount())); int[] piv = lu.getPivot(); int m = (int) getRowCount(); Matrix p = new ColtDenseDoubleMatrix2D(m, m); for (int i = 0; i < m; i++) { p.setAsDouble(1, i, piv[i]); } return new Matrix[] { l, u, p }; }
public Matrix[] lu() { if (getColumnCount() > getRowCount()) { throw new RuntimeException("only supported for m>=n"); } LUDecomposition lu = new LUDecomposition(matrix); Matrix l = new ColtDenseDoubleMatrix2D(lu.getL()); Matrix u = new ColtDenseDoubleMatrix2D(lu.getU().viewPart(0, 0, (int) getColumnCount(), (int) getColumnCount())); int[] piv = lu.getPivot(); int m = (int) getRowCount(); Matrix p = new ColtDenseDoubleMatrix2D(m, m); for (int i = 0; i < m; i++) { p.setAsDouble(1, i, piv[i]); } return new Matrix[] { l, u, p }; }