/** Constructs and returns a new view which is the transposition of the given matrix <tt>A</tt>. Equivalent to {@link DoubleMatrix2D#viewDice A.viewDice()}. This is a zero-copy transposition, taking O(1), i.e. constant time. The returned view is backed by this matrix, so changes in the returned view are reflected in this matrix, and vice-versa. Use idioms like <tt>result = transpose(A).copy()</tt> to generate an independent matrix. <p> <b>Example:</b> <table border="0"> <tr nowrap> <td valign="top">2 x 3 matrix: <br> 1, 2, 3<br> 4, 5, 6 </td> <td>transpose ==></td> <td valign="top">3 x 2 matrix:<br> 1, 4 <br> 2, 5 <br> 3, 6</td> <td>transpose ==></td> <td valign="top">2 x 3 matrix: <br> 1, 2, 3<br> 4, 5, 6 </td> </tr> </table> @return a new transposed view. */ public DoubleMatrix2D transpose(DoubleMatrix2D A) { return A.viewDice(); } /**
/** Constructs and returns a new view which is the transposition of the given matrix <tt>A</tt>. Equivalent to {@link DoubleMatrix2D#viewDice A.viewDice()}. This is a zero-copy transposition, taking O(1), i.e. constant time. The returned view is backed by this matrix, so changes in the returned view are reflected in this matrix, and vice-versa. Use idioms like <tt>result = transpose(A).copy()</tt> to generate an independent matrix. <p> <b>Example:</b> <table border="0"> <tr nowrap> <td valign="top">2 x 3 matrix: <br> 1, 2, 3<br> 4, 5, 6 </td> <td>transpose ==></td> <td valign="top">3 x 2 matrix:<br> 1, 4 <br> 2, 5 <br> 3, 6</td> <td>transpose ==></td> <td valign="top">2 x 3 matrix: <br> 1, 2, 3<br> 4, 5, 6 </td> </tr> </table> @return a new transposed view. */ public DoubleMatrix2D transpose(DoubleMatrix2D A) { return A.viewDice(); } /**
public void setParameters(DoubleMatrix2D A, DoubleMatrix2D B) { super.setParameters(A.viewDice(),B); // transposed --> faster (memory aware) iteration in correlation algo } public void apply(cern.colt.Timer timer) {
/** Modifies the given matrix <tt>A</tt> such that it's columns are permuted as specified; Useful for pivoting. Column <tt>A[i]</tt> will go into column <tt>A[indexes[i]]</tt>. Equivalent to <tt>permuteRows(transpose(A), indexes, work)</tt>. @param A the matrix to permute. @param indexes the permutation indexes, must satisfy <tt>indexes.length==A.columns() && indexes[i] >= 0 && indexes[i] < A.columns()</tt>; @param work the working storage, must satisfy <tt>work.length >= A.columns()</tt>; set <tt>work==null</tt> if you don't care about performance. @return the modified <tt>A</tt> (for convenience only). @throws IndexOutOfBoundsException if <tt>indexes.length != A.columns()</tt>. */ public DoubleMatrix2D permuteColumns(DoubleMatrix2D A, int[] indexes, int[] work) { return permuteRows(A.viewDice(), indexes, work); } /**
/** * Demonstrates usage of this class. */ public static void demo3(VectorVectorFunction norm) { double[][] values = { { -0.9611052, -0.25421095 }, { 0.4308269, -0.69932648 }, { -1.2071029, 0.62030596 }, { 1.5345166, 0.02135884}, {-1.1341542, 0.20388430} }; System.out.println("\n\ninitializing..."); DoubleFactory2D factory = DoubleFactory2D.dense; DoubleMatrix2D A = factory.make(values).viewDice(); System.out.println("\nA="+A.viewDice()); System.out.println("\ndist="+distance(A,norm).viewDice()); } /**
/** Modifies the given matrix <tt>A</tt> such that it's columns are permuted as specified; Useful for pivoting. Column <tt>A[i]</tt> will go into column <tt>A[indexes[i]]</tt>. Equivalent to <tt>permuteRows(transpose(A), indexes, work)</tt>. @param A the matrix to permute. @param indexes the permutation indexes, must satisfy <tt>indexes.length==A.columns() && indexes[i] >= 0 && indexes[i] < A.columns()</tt>; @param work the working storage, must satisfy <tt>work.length >= A.columns()</tt>; set <tt>work==null</tt> if you don't care about performance. @return the modified <tt>A</tt> (for convenience only). @throws IndexOutOfBoundsException if <tt>indexes.length != A.columns()</tt>. */ public DoubleMatrix2D permuteColumns(DoubleMatrix2D A, int[] indexes, int[] work) { return permuteRows(A.viewDice(), indexes, work); } /**
public void setParameters(DoubleMatrix2D A, DoubleMatrix2D B) { super.setParameters(A.viewDice(),B); // transposed --> faster (memory aware) iteration in correlation algo } public void apply(cern.colt.Timer timer) {
/** * Demonstrates usage of this class. */ public static void demo3(VectorVectorFunction norm) { double[][] values = { { -0.9611052, -0.25421095 }, { 0.4308269, -0.69932648 }, { -1.2071029, 0.62030596 }, { 1.5345166, 0.02135884}, {-1.1341542, 0.20388430} }; System.out.println("\n\ninitializing..."); DoubleFactory2D factory = DoubleFactory2D.dense; DoubleMatrix2D A = factory.make(values).viewDice(); System.out.println("\nA="+A.viewDice()); System.out.println("\ndist="+distance(A,norm).viewDice()); } /**
/** */ public static void doubleTest8() { int rows = 2; int columns = 3; // 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); DoubleMatrix2D view1 = master.viewDice(); System.out.println("view1="+view1); DoubleMatrix2D view2 = view1.viewDice(); 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 doubleTest8() { int rows = 2; int columns = 3; // 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); DoubleMatrix2D view1 = master.viewDice(); System.out.println("view1="+view1); DoubleMatrix2D view2 = view1.viewDice(); 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); } /**
/** * A matrix <tt>A</tt> is <i>symmetric</i> if <tt>A = tranpose(A)</tt>, that is <tt>A[i,j] == A[j,i]</tt>. * @throws IllegalArgumentException if <tt>!isSquare(A)</tt>. */ public boolean isSymmetric(DoubleMatrix2D A) { checkSquare(A); return equals(A,A.viewDice()); } /**
/** * A matrix <tt>A</tt> is <i>symmetric</i> if <tt>A = tranpose(A)</tt>, that is <tt>A[i,j] == A[j,i]</tt>. * @throws IllegalArgumentException if <tt>!isSquare(A)</tt>. */ public boolean isSymmetric(DoubleMatrix2D A) { checkSquare(A); return equals(A,A.viewDice()); } /**
/** * Linear algebraic matrix-vector multiplication; <tt>z = alpha * A * y + beta*z</tt>. * <tt>z[i] = alpha*Sum(A[i,j] * y[j]) + beta*z[i], i=0..A.rows()-1, j=0..y.size()-1</tt>. * Where <tt>A == this</tt>. * <br> * Note: Matrix shape conformance is checked <i>after</i> potential transpositions. * * @param y the source vector. * @param z the vector where results are to be stored. Set this parameter to <tt>null</tt> to indicate that a new result vector shall be constructed. * @return z (for convenience only). * * @throws IllegalArgumentException if <tt>A.columns() != y.size() || A.rows() > z.size())</tt>. */ public DoubleMatrix1D zMult(DoubleMatrix1D y, DoubleMatrix1D z, double alpha, double beta, boolean transposeA) { if (transposeA) return viewDice().zMult(y,z,alpha,beta,false); //boolean ignore = (z==null); if (z==null) z = new DenseDoubleMatrix1D(this.rows); if (columns != y.size() || rows > z.size()) throw new IllegalArgumentException("Incompatible args: "+toStringShort()+", "+y.toStringShort()+", "+z.toStringShort()); for (int i = rows; --i >= 0; ) { double s = 0; for (int j=columns; --j >= 0; ) { s += getQuick(i,j) * y.getQuick(j); } z.setQuick(i,alpha*s + beta*z.getQuick(i)); } return z; } /**
/** * Linear algebraic matrix-vector multiplication; <tt>z = alpha * A * y + beta*z</tt>. * <tt>z[i] = alpha*Sum(A[i,j] * y[j]) + beta*z[i], i=0..A.rows()-1, j=0..y.size()-1</tt>. * Where <tt>A == this</tt>. * <br> * Note: Matrix shape conformance is checked <i>after</i> potential transpositions. * * @param y the source vector. * @param z the vector where results are to be stored. Set this parameter to <tt>null</tt> to indicate that a new result vector shall be constructed. * @return z (for convenience only). * * @throws IllegalArgumentException if <tt>A.columns() != y.size() || A.rows() > z.size())</tt>. */ public DoubleMatrix1D zMult(DoubleMatrix1D y, DoubleMatrix1D z, double alpha, double beta, boolean transposeA) { if (transposeA) return viewDice().zMult(y,z,alpha,beta,false); //boolean ignore = (z==null); if (z==null) z = new DenseDoubleMatrix1D(this.rows); if (columns != y.size() || rows > z.size()) throw new IllegalArgumentException("Incompatible args: "+toStringShort()+", "+y.toStringShort()+", "+z.toStringShort()); for (int i = rows; --i >= 0; ) { double s = 0; for (int j=columns; --j >= 0; ) { s += getQuick(i,j) * y.getQuick(j); } z.setQuick(i,alpha*s + beta*z.getQuick(i)); } return z; } /**
public void dsymv(boolean isUpperTriangular, double alpha, DoubleMatrix2D A, DoubleMatrix1D x, double beta, DoubleMatrix1D y) { if (isUpperTriangular) A = A.viewDice(); Property.DEFAULT.checkSquare(A); int size = A.rows(); if (size != x.size() || size!=y.size()) { throw new IllegalArgumentException(A.toStringShort() + ", " + x.toStringShort() + ", " + y.toStringShort()); } DoubleMatrix1D tmp = x.like(); for (int i = 0; i < size; i++) { double sum = 0; for (int j = 0; j <= i; j++) { sum += A.getQuick(i,j) * x.getQuick(j); } for (int j = i + 1; j < size; j++) { sum += A.getQuick(j,i) * x.getQuick(j); } tmp.setQuick(i, alpha * sum + beta * y.getQuick(i)); } y.assign(tmp); } public void dtrmv(boolean isUpperTriangular, boolean transposeA, boolean isUnitTriangular, DoubleMatrix2D A, DoubleMatrix1D x) {
public void dsymv(boolean isUpperTriangular, double alpha, DoubleMatrix2D A, DoubleMatrix1D x, double beta, DoubleMatrix1D y) { if (isUpperTriangular) A = A.viewDice(); Property.DEFAULT.checkSquare(A); int size = A.rows(); if (size != x.size() || size!=y.size()) { throw new IllegalArgumentException(A.toStringShort() + ", " + x.toStringShort() + ", " + y.toStringShort()); } DoubleMatrix1D tmp = x.like(); for (int i = 0; i < size; i++) { double sum = 0; for (int j = 0; j <= i; j++) { sum += A.getQuick(i,j) * x.getQuick(j); } for (int j = i + 1; j < size; j++) { sum += A.getQuick(j,i) * x.getQuick(j); } tmp.setQuick(i, alpha * sum + beta * y.getQuick(i)); } y.assign(tmp); } public void dtrmv(boolean isUpperTriangular, boolean transposeA, boolean isUnitTriangular, DoubleMatrix2D A, DoubleMatrix1D x) {
public void dtrmv(boolean isUpperTriangular, boolean transposeA, boolean isUnitTriangular, DoubleMatrix2D A, DoubleMatrix1D x) { if (transposeA) { A = A.viewDice(); isUpperTriangular = !isUpperTriangular;
public void dtrmv(boolean isUpperTriangular, boolean transposeA, boolean isUnitTriangular, DoubleMatrix2D A, DoubleMatrix1D x) { if (transposeA) { A = A.viewDice(); isUpperTriangular = !isUpperTriangular;