/// <summary> /// Applies a function to each corresponding cell of two matrices and aggregates the results. /// Returns a value <i>v</i> such that <i>v==a(Size)</i> where <i>a(i) == aggr( a(i-1), f(get(row,column),other.Get(row,column)) )</i> and terminators are <i>a(1) == f(get(0,0),other.Get(0,0)), a(0)==null</i>. /// <p> /// <b>Example:</b> /// <pre> /// cern.jet.math.Functions F = cern.jet.math.Functions.Functions; /// x == 2 x 2 matrix /// 0 1 /// 2 3 /// /// y == 2 x 2 matrix /// 0 1 /// 2 3 /// /// // Sum( x[row,col] * y[row,col] ) /// x.aggregate(y, F.plus, F.mult); /// --> 14 /// /// // Sum( (x[row,col] + y[row,col])^2 ) /// x.aggregate(y, F.plus, F.chain(F.square,F.plus)); /// --> 56 /// </pre> /// For further examples, see the <a href="package-summary.html#FunctionObjects">package doc</a>. /// </summary> /// <param name="aggr">an aggregation function taking as first argument the current aggregation and as second argument the transformed current cell value.</param> /// <param name="f">a function transforming the current cell value.</param> /// <returns>the aggregated measure.</returns> /// <exception cref="ArgumentException">if <i>Columns != other.Columns || Rows != other.Rows</i></exception> /// <see cref="Cern.Jet.Math.Functions"/> public virtual Object Aggregate(ObjectMatrix2D other, Cern.Colt.Function.ObjectObjectFunction <Object> aggr, Cern.Colt.Function.ObjectObjectFunction <Object> f) { CheckShape(other); if (Size == 0) { return(null); } Object a = f(this[Rows - 1, Columns - 1], other[Rows - 1, Columns - 1]); int d = 1; // last cell already done for (int row = Rows; --row >= 0;) { for (int column = Columns - d; --column >= 0;) { a = aggr(a, f(this[row, column], other[row, column])); } d = 0; } return(a); }
/// <summary> /// Replaces all cell values of the receiver with the values of another matrix. /// Both matrices must have the same number of Rows and Columns. /// If both matrices share the same cells (as is the case if they are views derived from the same matrix) and intersect in an ambiguous way, then replaces <i>as if</i> using an intermediate auxiliary deep copy of <i>other</i>. /// </summary> /// <param name="other">the source matrix to copy from (may be identical to the receiver).</param> /// <returns><i>this</i> (for convenience only).</returns> /// <exception cref="ArgumentException">if <i>Columns != other.Columns || Rows != other.Rows</i></exception> public virtual ObjectMatrix2D Assign(ObjectMatrix2D other) { if (other == this) { return(this); } CheckShape(other); if (HaveSharedCells(other)) { other = other.Copy(); } for (int row = Rows; --row >= 0;) { for (int column = Columns; --column >= 0;) { this[row, column] = other[row, column]; } } return(this); }
public ObjectMatrix2D AppendRows(ObjectMatrix2D A, ObjectMatrix2D B) { // force both to have maximal shared number of columns. if (B.Columns > A.Columns) { B = B.ViewPart(0, 0, B.Rows, A.Columns); } else if (B.Columns < A.Columns) { A = A.ViewPart(0, 0, A.Rows, B.Columns); } // concatenate int ar = A.Rows; 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); return(matrix); }
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); }
public void Decompose(ObjectMatrix2D[][] parts, ObjectMatrix2D matrix) { CheckRectangularShape(parts); int rows = parts.Length; int columns = 0; if (parts.Length > 0) { columns = parts.GetLength(1); } if (rows == 0 || columns == 0) { return; } // determine maximum column width of each column int[] maxWidths = new int[columns]; for (int column = columns; --column >= 0;) { int maxWidth = 0; for (int row = rows; --row >= 0;) { ObjectMatrix2D part = parts[row][column]; if (part != null) { int width = part.Columns; if (maxWidth > 0 && width > 0 && width != maxWidth) { throw new ArgumentException("Different number of columns."); } maxWidth = System.Math.Max(maxWidth, width); } } maxWidths[column] = maxWidth; } // determine row height of each row int[] maxHeights = new int[rows]; for (int row = rows; --row >= 0;) { int maxHeight = 0; for (int column = columns; --column >= 0;) { ObjectMatrix2D part = parts[row][column]; if (part != null) { int height = part.Rows; if (maxHeight > 0 && height > 0 && height != maxHeight) { throw new ArgumentException("Different number of rows."); } maxHeight = System.Math.Max(maxHeight, height); } } maxHeights[row] = maxHeight; } // shape of result parts int resultRows = 0; for (int row = rows; --row >= 0;) { resultRows += maxHeights[row]; } int resultCols = 0; for (int column = columns; --column >= 0;) { resultCols += maxWidths[column]; } if (matrix.Rows < resultRows || matrix.Columns < resultCols) { throw new ArgumentException("Parts larger than matrix."); } // copy int r = 0; for (int row = 0; row < rows; row++) { int c = 0; for (int column = 0; column < columns; column++) { ObjectMatrix2D part = parts[row][column]; if (part != null) { part.Assign(matrix.ViewPart(r, c, part.Rows, part.Columns)); } c += maxWidths[column]; } r += maxHeights[row]; } }
public ObjectMatrix2D Compose(ObjectMatrix2D[][] parts) { CheckRectangularShape(parts); int rows = parts.Length; int columns = 0; if (parts.Length > 0) { columns = parts.GetLength(1); } ObjectMatrix2D empty = Make(0, 0); if (rows == 0 || columns == 0) { return(empty); } // determine maximum column width of each column int[] maxWidths = new int[columns]; for (int column = columns; --column >= 0;) { int maxWidth = 0; for (int row = rows; --row >= 0;) { ObjectMatrix2D part = parts[row][column]; if (part != null) { int width = part.Columns; if (maxWidth > 0 && width > 0 && width != maxWidth) { throw new ArgumentException(Cern.LocalizedResources.Instance().Exception_DifferentNumberOfColumns); } maxWidth = System.Math.Max(maxWidth, width); } } maxWidths[column] = maxWidth; } // determine row height of each row int[] maxHeights = new int[rows]; for (int row = rows; --row >= 0;) { int maxHeight = 0; for (int column = columns; --column >= 0;) { ObjectMatrix2D part = parts[row][column]; if (part != null) { int height = part.Rows; if (maxHeight > 0 && height > 0 && height != maxHeight) { throw new ArgumentException(Cern.LocalizedResources.Instance().Exception_DifferentNumberOfRows); } maxHeight = System.Math.Max(maxHeight, height); } } maxHeights[row] = maxHeight; } // shape of result int resultRows = 0; for (int row = rows; --row >= 0;) { resultRows += maxHeights[row]; } int resultCols = 0; for (int column = columns; --column >= 0;) { resultCols += maxWidths[column]; } ObjectMatrix2D matrix = Make(resultRows, resultCols); // copy int r = 0; for (int row = 0; row < rows; row++) { int c = 0; for (int column = 0; column < columns; column++) { ObjectMatrix2D part = parts[row][column]; if (part != null) { matrix.ViewPart(r, c, part.Rows, part.Columns).Assign(part); } c += maxWidths[column]; } r += maxHeights[row]; } return(matrix); }
/// <summary> /// Returns <i>true</i> if both matrices share at least one identical cell. /// </summary> protected Boolean HaveSharedCellsRaw(ObjectMatrix2D other) { return(false); }