/** * 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 ObjectMatrix2D 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 ObjectMatrix2D copy() { return like().assign(this); } /**
/** * Constructs a matrix with the given shape, each cell initialized with the given value. */ public ObjectMatrix2D make(int rows, int columns, Object initialValue) { if (initialValue == null) return make(rows,columns); return make(rows,columns).assign(initialValue); } /**
/** * Constructs a matrix with the given shape, each cell initialized with the given value. */ public ObjectMatrix2D make(int rows, int columns, Object initialValue) { if (initialValue == null) return make(rows,columns); return make(rows,columns).assign(initialValue); } /**
/** * 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 (this.isNoView) { if (values.length != rows) throw new IllegalArgumentException("Must have same number of rows: rows="+values.length+"rows()="+rows()); int i = columns*(rows-1); 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()); System.arraycopy(currentRow, 0, this.elements, i, columns); i -= columns; } } else { super.assign(values); } 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 (this.isNoView) { if (values.length != rows) throw new IllegalArgumentException("Must have same number of rows: rows="+values.length+"rows()="+rows()); int i = columns*(rows-1); 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()); System.arraycopy(currentRow, 0, this.elements, i, columns); i -= columns; } } else { super.assign(values); } return this; } /**
/** 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||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; } }
return super.assign(y, function);
return super.assign(y, function);
/** 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; } /**
/** 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; } /**