/// <summary> /// Assigns the result of a function to each cell; <i>x[i] = function(x[i],y[i])</i>. /// (Iterates downwards from <i>[size()-1]</i> to <i>[0]</i>). /// <p> /// <b>Example:</b> /// <pre> /// // assign x[i] = x[i]<sup>y[i]</sup> /// m1 = 0 1 2 3; /// m2 = 0 2 4 6; /// m1.assign(m2, Cern.Jet.Math.Functions.pow); /// --> /// m1 == 1 1 16 729 /// /// // for non-standard functions there is no shortcut: /// m1.assign(m2, /// new ObjectObjectFunction() { /// public Object apply(Object x, Object y) { return System.Math.Pow(x,y); } /// } /// ); /// </pre> /// For further examples, see the <a href="package-summary.html#FunctionObjects">package doc</a>. /// </summary> /// <param name="y">the secondary matrix to operate on.</param> /// <param name="function">a function object taking as first argument the current cell's value of <i>this</i>, and as second argument the current cell's value of <i>y</i>,</param> /// <returns><i>this</i> (for convenience only).</returns> /// <exception cref="ArgumentException">if <i>size() != y.Count</i>.</exception> /// <see cref="Cern.Jet.Math.Functions"/> public override ObjectMatrix1D Assign(ObjectMatrix1D y, Cern.Colt.Function.ObjectObjectFunction <Object> function) { // overriden for performance only if (!(y is DenseObjectMatrix1D)) { return(base.Assign(y, function)); } DenseObjectMatrix1D other = (DenseObjectMatrix1D)y; CheckSize(y); Object[] elems = this.Elements; Object[] otherElems = other.Elements; if (Elements == null || otherElems == null) { throw new NullReferenceException(); } int s = this.Stride; int ys = other.Stride; int index = base.Index(0); int otherIndex = other.Index(0); // the general case x[i] = f(x[i],y[i]) for (int k = Size; --k >= 0;) { elems[index] = function(elems[index], otherElems[otherIndex]); index += s; otherIndex += ys; } return(this); }
/// <summary> /// Assigns the result of a function to each cell; <i>x[i] = function(x[i], y[i])</i>. /// <p> /// <b>Example:</b> /// <pre> /// // assign x[i] = x[i]<sup>y[i]</sup> /// m1 = 0 1 2 3; /// m2 = 0 2 4 6; /// m1.assign(m2, Cern.jet.math.Functions.pow); /// --> /// m1 == 1 1 16 729 /// </pre> /// For further examples, see the <see cref="Function.ObjectFunction{C}"/> doc</a>. /// </summary> /// <param name="y">the secondary matrix to operate on.</param> /// <param name="function">a function object taking as first argument the current cell's value of <i>this</i>,and as second argument the current cell's value of <i>y</i>,</param> /// <returns><i>this</i> (for convenience only).</returns> /// <exception cref="ArgumentException">if <i>Size != y.Count</i>.</exception> /// <see cref="Cern.Jet.Math.Functions"/> public virtual ObjectMatrix1D Assign(ObjectMatrix1D y, Cern.Colt.Function.ObjectObjectFunction <Object> function) { CheckSize(y); for (int i = Size; --i >= 0;) { this[i] = function(this[i], y[i]); } return(this); }
/// <summary> /// Assigns the result of a function to each cell; <i>x[row,col] = function(x[row,col],y[row,col])</i>. /// <p> /// <b>Example:</b> /// <pre> /// // assign x[row,col] = x[row,col]<sup>y[row,col]</sup> /// m1 = 2 x 2 matrix /// 0 1 /// 2 3 /// /// m2 = 2 x 2 matrix /// 0 2 /// 4 6 /// /// m1.assign(m2, cern.jet.math.Functions.pow); /// --> /// m1 == 2 x 2 matrix /// 1 1 /// 16 729 /// </pre> /// For further examples, see the <see cref="Function.ObjectFunction{C}"/> doc</a>. /// </summary> /// <param name="y">the secondary matrix to operate on.</param> /// <param name="function">a function object taking as first argument the current cell's value of <i>this</i>, and as second argument the current cell's value of <i>y</i>,</param> /// <returns><i>this</i> (for convenience only).</returns> /// <exception cref="ArgumentException">if <i>Columns != other.Columns || Rows != other.Rows</i></exception> /// <see cref="Cern.Jet.Math.Functions"></see> public virtual ObjectMatrix2D Assign(ObjectMatrix2D y, Cern.Colt.Function.ObjectObjectFunction <Object> function) { CheckShape(y); for (int row = Rows; --row >= 0;) { for (int column = Columns; --column >= 0;) { this[row, column] = function(this[row, column], y[row, column]); } } return(this); }
/// <summary> /// Applies a function to each cell and aggregates the results. /// Returns a value<tt> v</tt> such that <tt>v==a(size())</tt> where<tt> a(i) == aggr(a(i-1), f(get(i)) )</tt> and terminators are<tt> a(1) == f(get(0)), a(0)==null</tt>. /// <p> /// <b>Example:</b> /// <pre> /// cern.jet.math.Functions F = cern.jet.math.Functions.functions; /// matrix = 0 1 2 3 /// /// Sum( x[i]*x[i] ) /// matrix.aggregate(F.plus, F.square); /// --> 14 /// </pre> /// For further examples, see the <see cref="Function.ObjectFunction{C}"/> 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> public virtual Object Aggregate(Cern.Colt.Function.ObjectObjectFunction <Object> aggr, Cern.Colt.Function.ObjectFunction <Object> f) { if (Size == 0) { return(null); } Object a = f(this[Size - 1]); for (int i = Size - 1; --i >= 0;) { a = aggr(a, f(this[i])); } return(a); }
/// <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(i), other.Get(i)) )</i> and terminators are<i> a(1) == f(get(0),other.Get(0)), a(0)==null</i>. /// <p> /// <b>Example:</b> /// <pre> /// Cern.jet.math.Functions F = Cern.jet.math.Functions.Functions; /// x = 0 1 2 3 /// y = 0 1 2 3 /// /// // Sum( x[i]*y[i] ) /// x.aggregate(y, F.plus, F.mult); /// --> 14 /// /// // Sum( (x[i]+y[i])^2 ) /// x.aggregate(y, F.plus, F.chain(F.square, F.plus)); /// --> 56 /// </pre> /// For further examples, see the <see cref="Function.ObjectFunction{C}"/> doc</a>. /// </summary> /// <param name="aggr">an aggregation function taking as first argument the current aggregation and as second argument the transformed current cell values.</param> /// <param name="f">a function transforming the current cell value.</param> /// <returns>the aggregated measure.</returns> /// <exception cref="ArgumentException">if <i>Size != other.Count</i>.</exception> public virtual Object Aggregate(ObjectMatrix1D other, Cern.Colt.Function.ObjectObjectFunction <Object> aggr, Cern.Colt.Function.ObjectObjectFunction <Object> f) { CheckSize(other); if (Size == 0) { return(null); } Object a = f(this[Size - 1], other[Size - 1]); for (int i = Size - 1; --i >= 0;) { a = aggr(a, f(this[i], other[i])); } return(a); }
/// <summary> /// Applies a function to each cell 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)) )</i> and terminators are <i>a(1) == f(get(0,0)), a(0)==null</i>. /// <p> /// <b>Example:</b> /// <pre> /// cern.jet.math.Functions F = cern.jet.math.Functions.Functions; /// 2 x 2 matrix /// 0 1 /// 2 3 /// /// // Sum( x[row,col]*x[row,col] ) /// matrix.aggregate(F.plus,F.square); /// --> 14 /// </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> /// <see cref="Cern.Jet.Math.Functions"/> public virtual Object Aggregate(Cern.Colt.Function.ObjectObjectFunction <Object> aggr, Cern.Colt.Function.ObjectFunction <Object> f) { if (Size == 0) { return(null); } Object a = f(this[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])); } d = 0; } return(a); }
/// <summary> /// Assigns the result of a function to each cell; <i>x[row,col] = function(x[row,col],y[row,col])</i>. /// <p> /// <b>Example:</b> /// <pre> /// // assign x[row,col] = x[row,col]<sup>y[row,col]</sup> /// m1 = 2 x 2 matrix /// 0 1 /// 2 3 /// /// m2 = 2 x 2 matrix /// 0 2 /// 4 6 /// /// m1.assign(m2, Cern.Jet.Math.Functions.pow); /// --> /// m1 == 2 x 2 matrix /// 1 1 /// 16 729 /// </pre> /// For further examples, see the <a href="package-summary.html#FunctionObjects">package doc</a>. /// </summary> /// <param name="y">the secondary matrix to operate on.</param> /// <param name="function">a function object taking as first argument the current cell's value of <i>this</i>, and as second argument the current cell's value of <i>y</i>,</param> /// <returns><i>this</i> (for convenience only).</returns> /// <exception cref="ArgumentException">if <i>columns() != other.columns() || rows() != other.rows()</i></exception> /// <see cref="Cern.Jet.Math.Functions"/> public override ObjectMatrix2D Assign(ObjectMatrix2D y, Cern.Colt.Function.ObjectObjectFunction <Object> function) { // overriden for performance only if (!(y is DenseObjectMatrix2D)) { return(base.Assign(y, function)); } DenseObjectMatrix2D other = (DenseObjectMatrix2D)y; CheckShape(y); Object[] elems = this.Elements; Object[] otherElems = other.Elements; if (elems == null || otherElems == null) { throw new NullReferenceException(); } int cs = this.ColumnStride; int ocs = other.ColumnStride; int rs = this.RowStride; int ors = other.RowStride; int otherIndex = other.Index(0, 0); int index = base.Index(0, 0); // the general case x[i] = f(x[i],y[i]) for (int row = Rows; --row >= 0;) { for (int i = index, j = otherIndex, column = Columns; --column >= 0;) { elems[i] = function(elems[i], otherElems[j]); i += cs; j += ocs; } index += rs; otherIndex += ors; } return(this); }