/** * Constructs a 1d matrix of the right dynamic type. */ protected ObjectMatrix1D make1D(int size) { return make(0,0).like1D(size); } /**
/** * Constructs a 1d matrix of the right dynamic type. */ protected ObjectMatrix1D make1D(int size) { return make(0,0).like1D(size); } /**
/** Construct a matrix from a one-dimensional column-major packed array, ala Fortran. Has the form <tt>matrix.get(row,column) == values[row + column*rows]</tt>. The values are copied. @param values One-dimensional array of Objects, packed by columns (ala Fortran). @param rows the number of rows. @exception IllegalArgumentException <tt>values.length</tt> must be a multiple of <tt>rows</tt>. */ public ObjectMatrix2D make(Object values[], int rows) { int columns = (rows != 0 ? values.length/rows : 0); if (rows*columns != values.length) throw new IllegalArgumentException("Array length must be a multiple of m."); ObjectMatrix2D matrix = make(rows,columns); for (int row=0; row < rows; row++) { for (int column=0; column < columns; column++) { matrix.setQuick(row,column, values[row + column*rows]); } } return matrix; } /**
/** Construct a matrix from a one-dimensional column-major packed array, ala Fortran. Has the form <tt>matrix.get(row,column) == values[row + column*rows]</tt>. The values are copied. @param values One-dimensional array of Objects, packed by columns (ala Fortran). @param rows the number of rows. @exception IllegalArgumentException <tt>values.length</tt> must be a multiple of <tt>rows</tt>. */ public ObjectMatrix2D make(Object values[], int rows) { int columns = (rows != 0 ? values.length/rows : 0); if (rows*columns != values.length) throw new IllegalArgumentException("Array length must be a multiple of m."); ObjectMatrix2D matrix = make(rows,columns); for (int row=0; row < rows; row++) { for (int column=0; column < columns; column++) { matrix.setQuick(row,column, values[row + column*rows]); } } return matrix; } /**
/** Constructs a new diagonal matrix whose diagonal elements are the elements of <tt>vector</tt>. Cells values are copied. The new matrix is not a view. Example: <pre> 5 4 3 --> 5 0 0 0 4 0 0 0 3 </pre> @return a new matrix. */ public ObjectMatrix2D diagonal(ObjectMatrix1D vector) { int size = vector.size(); ObjectMatrix2D diag = make(size,size); for (int i=size; --i >= 0; ) { diag.setQuick(i,i, vector.getQuick(i)); } return diag; } /**
/** Constructs a new diagonal matrix whose diagonal elements are the elements of <tt>vector</tt>. Cells values are copied. The new matrix is not a view. Example: <pre> 5 4 3 --> 5 0 0 0 4 0 0 0 3 </pre> @return a new matrix. */ public ObjectMatrix2D diagonal(ObjectMatrix1D vector) { int size = vector.size(); ObjectMatrix2D diag = make(size,size); for (int i=size; --i >= 0; ) { diag.setQuick(i,i, vector.getQuick(i)); } return diag; } /**
/** C = A||A||..||A; Constructs a new matrix which is duplicated both along the row and column dimension. Example: <pre> 0 1 2 3 repeat(2,3) --> 0 1 0 1 0 1 2 3 2 3 2 3 0 1 0 1 0 1 2 3 2 3 2 3 </pre> */ public ObjectMatrix2D repeat(ObjectMatrix2D A, int rowRepeat, int columnRepeat) { int r = A.rows(); int c = A.columns(); ObjectMatrix2D matrix = make(r*rowRepeat, c*columnRepeat); for (int i=rowRepeat; --i >= 0; ) { for (int j=columnRepeat; --j >= 0; ) { matrix.viewPart(r*i,c*j,r,c).assign(A); } } return matrix; } }
/** C = A||A||..||A; Constructs a new matrix which is duplicated both along the row and column dimension. Example: <pre> 0 1 2 3 repeat(2,3) --> 0 1 0 1 0 1 2 3 2 3 2 3 0 1 0 1 0 1 2 3 2 3 2 3 </pre> */ public ObjectMatrix2D repeat(ObjectMatrix2D A, int rowRepeat, int columnRepeat) { int r = A.rows(); int c = A.columns(); ObjectMatrix2D matrix = make(r*rowRepeat, c*columnRepeat); for (int i=rowRepeat; --i >= 0; ) { for (int j=columnRepeat; --j >= 0; ) { matrix.viewPart(r*i,c*j,r,c).assign(A); } } return matrix; } }
/** Returns a string representation of the given matrix with axis as well as rows and columns labeled. Pass <tt>null</tt> to one or more parameters to indicate that the corresponding decoration element shall not appear in the string converted matrix. @param matrix The matrix to format. @param rowNames The headers of all rows (to be put to the left of the matrix). @param columnNames The headers of all columns (to be put to above the matrix). @param rowAxisName The label of the y-axis. @param columnAxisName The label of the x-axis. @param title The overall title of the matrix to be formatted. @return the matrix converted to a string. */ protected String toTitleString(DoubleMatrix2D matrix, String[] rowNames, String[] columnNames, String rowAxisName, String columnAxisName, String title) { if (matrix.size()==0) return "Empty matrix"; String[][] s = format(matrix); //String oldAlignment = this.alignment; //this.alignment = DECIMAL; align(s); //this.alignment = oldAlignment; return new cern.colt.matrix.objectalgo.Formatter().toTitleString(cern.colt.matrix.ObjectFactory2D.dense.make(s), rowNames,columnNames,rowAxisName,columnAxisName,title); } /**
/** Returns a string representation of the given matrix with axis as well as rows and columns labeled. Pass <tt>null</tt> to one or more parameters to indicate that the corresponding decoration element shall not appear in the string converted matrix. @param matrix The matrix to format. @param rowNames The headers of all rows (to be put to the left of the matrix). @param columnNames The headers of all columns (to be put to above the matrix). @param rowAxisName The label of the y-axis. @param columnAxisName The label of the x-axis. @param title The overall title of the matrix to be formatted. @return the matrix converted to a string. */ protected String toTitleString(DoubleMatrix2D matrix, String[] rowNames, String[] columnNames, String rowAxisName, String columnAxisName, String title) { if (matrix.size()==0) return "Empty matrix"; String[][] s = format(matrix); //String oldAlignment = this.alignment; //this.alignment = DECIMAL; align(s); //this.alignment = oldAlignment; return new cern.colt.matrix.objectalgo.Formatter().toTitleString(cern.colt.matrix.ObjectFactory2D.dense.make(s), rowNames,columnNames,rowAxisName,columnAxisName,title); } /**
/** 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 ObjectMatrix2D composeDiagonal(ObjectMatrix2D A, ObjectMatrix2D B) { int ar = A.rows(); int ac = A.columns(); int br = B.rows(); int bc = B.columns(); ObjectMatrix2D 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 ObjectMatrix2D composeDiagonal(ObjectMatrix2D A, ObjectMatrix2D B) { int ar = A.rows(); int ac = A.columns(); int br = B.rows(); int bc = B.columns(); ObjectMatrix2D sum = make(ar+br, ac+bc); sum.viewPart(0,0,ar,ac).assign(A); sum.viewPart(ar,ac,br,bc).assign(B); return sum; } /**
/** C = A||B; Constructs a new matrix which is the column-wise concatenation of two other matrices. <pre> 0 1 2 3 4 5 appendColumns 6 7 8 9 --> 0 1 2 6 7 3 4 5 8 9 </pre> */ public ObjectMatrix2D appendColumns(ObjectMatrix2D A, ObjectMatrix2D B) { // force both to have maximal shared number of rows. if (B.rows() > A.rows()) B = B.viewPart(0,0,A.rows(),B.columns()); else if (B.rows() < A.rows()) A = A.viewPart(0,0,B.rows(),A.columns()); // concatenate int ac = A.columns(); int bc = B.columns(); int r = A.rows(); ObjectMatrix2D matrix = make(r,ac+bc); matrix.viewPart(0,0,r,ac).assign(A); matrix.viewPart(0,ac,r,bc).assign(B); return matrix; } /**
/** C = A||B; Constructs a new matrix which is the column-wise concatenation of two other matrices. <pre> 0 1 2 3 4 5 appendColumns 6 7 8 9 --> 0 1 2 6 7 3 4 5 8 9 </pre> */ public ObjectMatrix2D appendColumns(ObjectMatrix2D A, ObjectMatrix2D B) { // force both to have maximal shared number of rows. if (B.rows() > A.rows()) B = B.viewPart(0,0,A.rows(),B.columns()); else if (B.rows() < A.rows()) A = A.viewPart(0,0,B.rows(),A.columns()); // concatenate int ac = A.columns(); int bc = B.columns(); int r = A.rows(); ObjectMatrix2D matrix = make(r,ac+bc); matrix.viewPart(0,0,r,ac).assign(A); matrix.viewPart(0,ac,r,bc).assign(B); return matrix; } /**
int br = B.rows(); int c = A.columns(); ObjectMatrix2D matrix = make(ar+br, c); matrix.viewPart(0,0,ar,c).assign(A); matrix.viewPart(ar,0,br,c).assign(B);
int br = B.rows(); int c = A.columns(); ObjectMatrix2D matrix = make(ar+br, c); matrix.viewPart(0,0,ar,c).assign(A); matrix.viewPart(ar,0,br,c).assign(B);
/** 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 ObjectMatrix2D composeDiagonal(ObjectMatrix2D A, ObjectMatrix2D B, ObjectMatrix2D C) { ObjectMatrix2D 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 ObjectMatrix2D composeDiagonal(ObjectMatrix2D A, ObjectMatrix2D B, ObjectMatrix2D C) { ObjectMatrix2D 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; } /**