/** * Sets all cells to the state specified by <tt>values</tt>. * <tt>values</tt> is required to have the form <tt>values[row][column]</tt> * and have exactly the same number of rows and columns as the receiver. * <p> * The values are copied. So subsequent changes in <tt>values</tt> are not reflected in the matrix, and vice-versa. * * @param values the values to be filled into the cells. * @return <tt>this</tt> (for convenience only). * @throws IllegalArgumentException if <tt>values.length != rows() || for any 0 <= row < rows(): values[row].length != columns()</tt>. */ public ObjectMatrix2D assign(Object[][] values) { if (values.length != rows) throw new IllegalArgumentException("Must have same number of rows: rows="+values.length+"rows()="+rows()); for (int row=rows; --row >= 0;) { Object[] currentRow = values[row]; if (currentRow.length != columns) throw new IllegalArgumentException("Must have same number of columns in every row: columns="+currentRow.length+"columns()="+columns()); for (int column=columns; --column >= 0;) { setQuick(row,column,currentRow[column]); } } return this; } /**
/** * Sets all cells to the state specified by <tt>values</tt>. * <tt>values</tt> is required to have the form <tt>values[row][column]</tt> * and have exactly the same number of rows and columns as the receiver. * <p> * The values are copied. So subsequent changes in <tt>values</tt> are not reflected in the matrix, and vice-versa. * * @param values the values to be filled into the cells. * @return <tt>this</tt> (for convenience only). * @throws IllegalArgumentException if <tt>values.length != rows() || for any 0 <= row < rows(): values[row].length != columns()</tt>. */ public ObjectMatrix2D assign(Object[][] values) { if (values.length != rows) throw new IllegalArgumentException("Must have same number of rows: rows="+values.length+"rows()="+rows()); for (int row=rows; --row >= 0;) { Object[] currentRow = values[row]; if (currentRow.length != columns) throw new IllegalArgumentException("Must have same number of columns in every row: columns="+currentRow.length+"columns()="+columns()); for (int column=columns; --column >= 0;) { setQuick(row,column,currentRow[column]); } } return this; } /**
/** 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; } /**
/** * Returns a string representations of all cells; no alignment considered. */ protected String[][] format(ObjectMatrix2D matrix) { String[][] strings = new String[matrix.rows()][matrix.columns()]; for (int row=matrix.rows(); --row >= 0; ) strings[row] = formatRow(matrix.viewRow(row)); return strings; } /**
/** * Returns a string representations of all cells; no alignment considered. */ protected String[][] format(ObjectMatrix2D matrix) { String[][] strings = new String[matrix.rows()][matrix.columns()]; for (int row=matrix.rows(); --row >= 0; ) strings[row] = formatRow(matrix.viewRow(row)); return strings; } /**
/** Constructs a new vector consisting of the diagonal elements of <tt>A</tt>. Cells values are copied. The new vector is not a view. Example: <pre> 5 0 0 9 0 4 0 9 0 0 3 9 --> 5 4 3 </pre> @param A the matrix, need not be square. @return a new vector. */ public ObjectMatrix1D diagonal(ObjectMatrix2D A) { int min = Math.min(A.rows(),A.columns()); ObjectMatrix1D diag = make1D(min); for (int i=min; --i >= 0; ) { diag.setQuick(i, A.getQuick(i,i)); } return diag; } /**
/** Constructs a new vector consisting of the diagonal elements of <tt>A</tt>. Cells values are copied. The new vector is not a view. Example: <pre> 5 0 0 9 0 4 0 9 0 0 3 9 --> 5 4 3 </pre> @param A the matrix, need not be square. @return a new vector. */ public ObjectMatrix1D diagonal(ObjectMatrix2D A) { int min = Math.min(A.rows(),A.columns()); ObjectMatrix1D diag = make1D(min); for (int i=min; --i >= 0; ) { diag.setQuick(i, A.getQuick(i,i)); } return diag; } /**
/** 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; } /**
/** 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; } }
if (column < 0 || column >= matrix.columns()) throw new IndexOutOfBoundsException("column="+column+", matrix="+Formatter.shape(matrix));
if (column < 0 || column >= matrix.columns()) throw new IndexOutOfBoundsException("column="+column+", matrix="+Formatter.shape(matrix));
ObjectMatrix2D other = (ObjectMatrix2D) otherObj; if (rows!=other.rows()) return false; if (columns!=other.columns()) return false;
ObjectMatrix2D other = (ObjectMatrix2D) otherObj; if (rows!=other.rows()) return false; if (columns!=other.columns()) return false;