/** * Construct and returns a new empty matrix <i>of the same dynamic type</i> as the receiver, having the same number of rows and columns. * For example, if the receiver is an instance of type <tt>DenseDoubleMatrix2D</tt> the new matrix must also be of type <tt>DenseDoubleMatrix2D</tt>, * if the receiver is an instance of type <tt>SparseDoubleMatrix2D</tt> the new matrix must also be of type <tt>SparseDoubleMatrix2D</tt>, etc. * In general, the new matrix should have internal parametrization as similar as possible. * * @return a new empty matrix of the same dynamic type. */ public DoubleMatrix2D like() { return like(rows,columns); } /**
LU = A.like(); solved = b.like();
/** * Construct and returns a new 2-d matrix <i>of the corresponding dynamic type</i>, entirelly independent of the receiver. * For example, if the receiver is an instance of type <tt>DenseDoubleMatrix1D</tt> the new matrix must be of type <tt>DenseDoubleMatrix2D</tt>, * if the receiver is an instance of type <tt>SparseDoubleMatrix1D</tt> the new matrix must be of type <tt>SparseDoubleMatrix2D</tt>, etc. * * @param rows the number of rows the matrix shall have. * @param columns the number of columns the matrix shall have. * @return a new matrix of the corresponding dynamic type. */ public DoubleMatrix2D like2D(int rows, int columns) { return content.like(rows,columns); } /**
/** * Construct and returns a new empty matrix <i>of the same dynamic type</i> as the receiver, having the same number of rows and columns. * For example, if the receiver is an instance of type <tt>DenseDoubleMatrix2D</tt> the new matrix must also be of type <tt>DenseDoubleMatrix2D</tt>, * if the receiver is an instance of type <tt>SparseDoubleMatrix2D</tt> the new matrix must also be of type <tt>SparseDoubleMatrix2D</tt>, etc. * In general, the new matrix should have internal parametrization as similar as possible. * * @return a new empty matrix of the same dynamic type. */ public DoubleMatrix2D like() { return like(rows,columns); } /**
/** * Construct and returns a new 2-d matrix <i>of the corresponding dynamic type</i>, entirelly independent of the receiver. * For example, if the receiver is an instance of type <tt>DenseDoubleMatrix1D</tt> the new matrix must be of type <tt>DenseDoubleMatrix2D</tt>, * if the receiver is an instance of type <tt>SparseDoubleMatrix1D</tt> the new matrix must be of type <tt>SparseDoubleMatrix2D</tt>, etc. * * @param rows the number of rows the matrix shall have. * @param columns the number of columns the matrix shall have. * @return a new matrix of the corresponding dynamic type. */ public DoubleMatrix2D like2D(int rows, int columns) { return content.like(rows,columns); } /**
/** * Construct and returns a new empty matrix <i>of the same dynamic type</i> as the receiver, having the specified number of rows and columns. * For example, if the receiver is an instance of type <tt>DenseDoubleMatrix2D</tt> the new matrix must also be of type <tt>DenseDoubleMatrix2D</tt>, * if the receiver is an instance of type <tt>SparseDoubleMatrix2D</tt> the new matrix must also be of type <tt>SparseDoubleMatrix2D</tt>, etc. * In general, the new matrix should have internal parametrization as similar as possible. * * @param rows the number of rows the matrix shall have. * @param columns the number of columns the matrix shall have. * @return a new empty matrix of the same dynamic type. */ public DoubleMatrix2D like(int rows, int columns) { return content.like(rows,columns); } /**
/** * Construct and returns a new empty matrix <i>of the same dynamic type</i> as the receiver, having the specified number of rows and columns. * For example, if the receiver is an instance of type <tt>DenseDoubleMatrix2D</tt> the new matrix must also be of type <tt>DenseDoubleMatrix2D</tt>, * if the receiver is an instance of type <tt>SparseDoubleMatrix2D</tt> the new matrix must also be of type <tt>SparseDoubleMatrix2D</tt>, etc. * In general, the new matrix should have internal parametrization as similar as possible. * * @param rows the number of rows the matrix shall have. * @param columns the number of columns the matrix shall have. * @return a new empty matrix of the same dynamic type. */ public DoubleMatrix2D like(int rows, int columns) { return content.like(rows,columns); } /**
/** * Constructs and returns a deep copy of the receiver. * <p> * <b>Note that the returned matrix is an independent deep copy.</b> * The returned matrix is not backed by this matrix, so changes in the returned matrix are not reflected in this matrix, and vice-versa. * * @return a deep copy of the receiver. */ public DoubleMatrix2D copy() { return like().assign(this); } /**
/** * Constructs and returns a deep copy of the receiver. * <p> * <b>Note that the returned matrix is an independent deep copy.</b> * The returned matrix is not backed by this matrix, so changes in the returned matrix are not reflected in this matrix, and vice-versa. * * @return a deep copy of the receiver. */ public DoubleMatrix2D copy() { return like().assign(this); } /**
/** * Copies the columns of the indicated rows into a new sub matrix. * <tt>sub[0..rowIndexes.length-1,0..columnTo-columnFrom] = A[rowIndexes(:),columnFrom..columnTo]</tt>; * The returned matrix is <i>not backed</i> by this matrix, so changes in the returned matrix are <i>not reflected</i> in this matrix, and vice-versa. * * @param A the source matrix to copy from. * @param rowIndexes the indexes of the rows to copy. May be unsorted. * @param columnFrom the index of the first column to copy (inclusive). * @param columnTo the index of the last column to copy (inclusive). * @return a new sub matrix; with <tt>sub.rows()==rowIndexes.length; sub.columns()==columnTo-columnFrom+1</tt>. * @throws IndexOutOfBoundsException if <tt>columnFrom<0 || columnTo-columnFrom+1<0 || columnTo+1>matrix.columns() || for any row=rowIndexes[i]: row < 0 || row >= matrix.rows()</tt>. */ private DoubleMatrix2D subMatrix(DoubleMatrix2D A, int[] rowIndexes, int columnFrom, int columnTo) { int width = columnTo-columnFrom+1; int rows = A.rows(); A = A.viewPart(0,columnFrom,rows,width); DoubleMatrix2D sub = A.like(rowIndexes.length, width); for (int r = rowIndexes.length; --r >= 0; ) { int row = rowIndexes[r]; if (row < 0 || row >= rows) throw new IndexOutOfBoundsException("Illegal Index"); sub.viewRow(r).assign(A.viewRow(row)); } return sub; } /**
/** * Copies the rows of the indicated columns into a new sub matrix. * <tt>sub[0..rowTo-rowFrom,0..columnIndexes.length-1] = A[rowFrom..rowTo,columnIndexes(:)]</tt>; * The returned matrix is <i>not backed</i> by this matrix, so changes in the returned matrix are <i>not reflected</i> in this matrix, and vice-versa. * * @param A the source matrix to copy from. * @param rowFrom the index of the first row to copy (inclusive). * @param rowTo the index of the last row to copy (inclusive). * @param columnIndexes the indexes of the columns to copy. May be unsorted. * @return a new sub matrix; with <tt>sub.rows()==rowTo-rowFrom+1; sub.columns()==columnIndexes.length</tt>. * @throws IndexOutOfBoundsException if <tt>rowFrom<0 || rowTo-rowFrom+1<0 || rowTo+1>matrix.rows() || for any col=columnIndexes[i]: col < 0 || col >= matrix.columns()</tt>. */ private DoubleMatrix2D subMatrix(DoubleMatrix2D A, int rowFrom, int rowTo, int[] columnIndexes) { if (rowTo-rowFrom >= A.rows()) throw new IndexOutOfBoundsException("Too many rows"); int height = rowTo-rowFrom+1; int columns = A.columns(); A = A.viewPart(rowFrom,0,height,columns); DoubleMatrix2D sub = A.like(height, columnIndexes.length); for (int c = columnIndexes.length; --c >= 0; ) { int column = columnIndexes[c]; if (column < 0 || column >= columns) throw new IndexOutOfBoundsException("Illegal Index"); sub.viewColumn(c).assign(A.viewColumn(column)); } return sub; } /**
/** * Copies the columns of the indicated rows into a new sub matrix. * <tt>sub[0..rowIndexes.length-1,0..columnTo-columnFrom] = A[rowIndexes(:),columnFrom..columnTo]</tt>; * The returned matrix is <i>not backed</i> by this matrix, so changes in the returned matrix are <i>not reflected</i> in this matrix, and vice-versa. * * @param A the source matrix to copy from. * @param rowIndexes the indexes of the rows to copy. May be unsorted. * @param columnFrom the index of the first column to copy (inclusive). * @param columnTo the index of the last column to copy (inclusive). * @return a new sub matrix; with <tt>sub.rows()==rowIndexes.length; sub.columns()==columnTo-columnFrom+1</tt>. * @throws IndexOutOfBoundsException if <tt>columnFrom<0 || columnTo-columnFrom+1<0 || columnTo+1>matrix.columns() || for any row=rowIndexes[i]: row < 0 || row >= matrix.rows()</tt>. */ private DoubleMatrix2D subMatrix(DoubleMatrix2D A, int[] rowIndexes, int columnFrom, int columnTo) { int width = columnTo-columnFrom+1; int rows = A.rows(); A = A.viewPart(0,columnFrom,rows,width); DoubleMatrix2D sub = A.like(rowIndexes.length, width); for (int r = rowIndexes.length; --r >= 0; ) { int row = rowIndexes[r]; if (row < 0 || row >= rows) throw new IndexOutOfBoundsException("Illegal Index"); sub.viewRow(r).assign(A.viewRow(row)); } return sub; } /**
/** * Copies the rows of the indicated columns into a new sub matrix. * <tt>sub[0..rowTo-rowFrom,0..columnIndexes.length-1] = A[rowFrom..rowTo,columnIndexes(:)]</tt>; * The returned matrix is <i>not backed</i> by this matrix, so changes in the returned matrix are <i>not reflected</i> in this matrix, and vice-versa. * * @param A the source matrix to copy from. * @param rowFrom the index of the first row to copy (inclusive). * @param rowTo the index of the last row to copy (inclusive). * @param columnIndexes the indexes of the columns to copy. May be unsorted. * @return a new sub matrix; with <tt>sub.rows()==rowTo-rowFrom+1; sub.columns()==columnIndexes.length</tt>. * @throws IndexOutOfBoundsException if <tt>rowFrom<0 || rowTo-rowFrom+1<0 || rowTo+1>matrix.rows() || for any col=columnIndexes[i]: col < 0 || col >= matrix.columns()</tt>. */ private DoubleMatrix2D subMatrix(DoubleMatrix2D A, int rowFrom, int rowTo, int[] columnIndexes) { if (rowTo-rowFrom >= A.rows()) throw new IndexOutOfBoundsException("Too many rows"); int height = rowTo-rowFrom+1; int columns = A.columns(); A = A.viewPart(rowFrom,0,height,columns); DoubleMatrix2D sub = A.like(height, columnIndexes.length); for (int c = columnIndexes.length; --c >= 0; ) { int column = columnIndexes[c]; if (column < 0 || column >= columns) throw new IndexOutOfBoundsException("Illegal Index"); sub.viewColumn(c).assign(A.viewColumn(column)); } return sub; } /**
/** Returns the upper triangular factor, <tt>R</tt>. @return <tt>R</tt> */ public DoubleMatrix2D getR() { DoubleMatrix2D R = QR.like(n,n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i < j) R.setQuick(i,j, QR.getQuick(i,j)); else if (i == j) R.setQuick(i,j, Rdiag.getQuick(i)); else R.setQuick(i,j, 0); } } return R; } /**
/** Returns the upper triangular factor, <tt>R</tt>. @return <tt>R</tt> */ public DoubleMatrix2D getR() { DoubleMatrix2D R = QR.like(n,n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i < j) R.setQuick(i,j, QR.getQuick(i,j)); else if (i == j) R.setQuick(i,j, Rdiag.getQuick(i)); else R.setQuick(i,j, 0); } } return R; } /**
DoubleMatrix2D B = A.like(); timer.reset().start(); System.out.print("now copying... ");
L = A.like(n,n); isSymmetricPositiveDefinite = (A.columns() == n);
L = A.like(n,n); isSymmetricPositiveDefinite = (A.columns() == n);
/** Generates and returns the (economy-sized) orthogonal factor <tt>Q</tt>. @return <tt>Q</tt> */ public DoubleMatrix2D getQ () { cern.jet.math.Functions F = cern.jet.math.Functions.functions; DoubleMatrix2D Q = QR.like(); //double[][] Q = X.getArray(); for (int k = n-1; k >= 0; k--) { DoubleMatrix1D QRcolk = QR.viewColumn(k).viewPart(k,m-k); Q.setQuick(k,k, 1); for (int j = k; j < n; j++) { if (QR.getQuick(k,k) != 0) { DoubleMatrix1D Qcolj = Q.viewColumn(j).viewPart(k,m-k); double s = QRcolk.zDotProduct(Qcolj); s = -s / QR.getQuick(k,k); Qcolj.assign(QRcolk, F.plusMult(s)); } } } return Q; } /**
/** Generates and returns the (economy-sized) orthogonal factor <tt>Q</tt>. @return <tt>Q</tt> */ public DoubleMatrix2D getQ () { cern.jet.math.Functions F = cern.jet.math.Functions.functions; DoubleMatrix2D Q = QR.like(); //double[][] Q = X.getArray(); for (int k = n-1; k >= 0; k--) { DoubleMatrix1D QRcolk = QR.viewColumn(k).viewPart(k,m-k); Q.setQuick(k,k, 1); for (int j = k; j < n; j++) { if (QR.getQuick(k,k) != 0) { DoubleMatrix1D Qcolj = Q.viewColumn(j).viewPart(k,m-k); double s = QRcolk.zDotProduct(Qcolj); s = -s / QR.getQuick(k,k); Qcolj.assign(QRcolk, F.plusMult(s)); } } } return Q; } /**