/// <summary> /// Assigns the result of a function to each cell; <i>x[i] = function(x[i])</i>. /// (Iterates downwards from<i>[Size-1]</i> to<i>[0]</i>). /// <p> /// <b>Example:</b> /// <pre> /// // change each cell to its sine /// matrix = 0.5 1.5 2.5 3.5 /// matrix.assign(Cern.jet.math.Functions.sin); /// --> /// matrix == 0.479426 0.997495 0.598472 -0.350783 /// </pre> /// For further examples, see the <see cref="Function.ObjectFunction{C}"/> doc</a>. /// </summary> /// <param name="function">a function object taking as argument the current cell's value.</param> /// <returns><i>this</i> (for convenience only).</returns> /// <see cref="Cern.Jet.Math.Functions"/> /// <exception cref=""></exception> public virtual ObjectMatrix1D Assign(Cern.Colt.Function.ObjectFunction <Object> function) { for (int i = Size; --i >= 0;) { this[i] = function(this[i]); } return(this); }
/// <summary> /// Assigns the result of a function to each cell; <i>x[row,col] = function(x[row,col])</i>. /// <p> /// <b>Example:</b> /// <pre> /// matrix = 2 x 2 matrix /// 0.5 1.5 /// 2.5 3.5 /// /// // change each cell to its sine /// matrix.assign(cern.jet.math.Functions.sin); /// --> /// 2 x 2 matrix /// 0.479426 0.997495 /// 0.598472 -0.350783 /// </pre> /// For further examples, see the <see cref="Function.ObjectFunction{C}"/> doc</a>. /// </summary> /// <param name="function">a function object taking as argument the current cell's value.</param> /// <returns><i>this</i> (for convenience only).</returns> /// <see cref="Cern.Jet.Math.Functions"></see> public virtual ObjectMatrix2D Assign(Cern.Colt.Function.ObjectFunction <Object> function) { for (int row = Rows; --row >= 0;) { for (int column = Columns; --column >= 0;) { this[row, column] = function(this[row, column]); } } return(this); }
/// <summary> /// Assigns the result of a function to each cell; <i>x[i] = function(x[i])</i>. /// (Iterates downwards from <i>[size()-1]</i> to <i>[0]</i>). /// <p> /// <b>Example:</b> /// <pre> /// // change each cell to its sine /// matrix = 0.5 1.5 2.5 3.5 /// matrix.assign(Cern.Jet.Math.Functions.sin); /// --> /// matrix == 0.479426 0.997495 0.598472 -0.350783 /// </pre> /// For further examples, see the <a href="package-summary.html#FunctionObjects">package doc</a>. /// </summary> /// <param name="function">a function object taking as argument the current cell's value.</param> /// <returns><i>this</i> (for convenience only).</returns> /// <see cref="Cern.Jet.Math.Functions"></see> public override ObjectMatrix1D Assign(Cern.Colt.Function.ObjectFunction <Object> function) { int s = Stride; int i = Index(0); Object[] elems = this.Elements; if (Elements == null) { throw new NullReferenceException(); } // the general case x[i] = f(x[i]) for (int k = Size; --k >= 0;) { elems[i] = function(elems[i]); i += s; } return(this); }
/// <summary> /// Assigns the result of a function to each cell; <i>x[row,col] = function(x[row,col])</i>. /// <p> /// <b>Example:</b> /// <pre> /// matrix = 2 x 2 matrix /// 0.5 1.5 /// 2.5 3.5 /// /// // change each cell to its sine /// matrix.assign(Cern.Jet.Math.Functions.sin); /// --> /// 2 x 2 matrix /// 0.479426 0.997495 /// 0.598472 -0.350783 /// </pre> /// For further examples, see the <a href="package-summary.html#FunctionObjects">package doc</a>. /// </summary> /// <param name="function">a function object taking as argument the current cell's value.</param> /// <param name=""><i>this</i> (for convenience only).</param> /// <returns></returns> /// <see cref="Cern.Jet.Math.Functions"></see> public override ObjectMatrix2D Assign(Cern.Colt.Function.ObjectFunction <Object> function) { Object[] elems = this.Elements; if (elems == null) { throw new NullReferenceException(); } int index = base.Index(0, 0); int cs = this.ColumnStride; int rs = this.RowStride; // the general case x[i] = f(x[i]) for (int row = Rows; --row >= 0;) { for (int i = index, column = Columns; --column >= 0;) { elems[i] = function(elems[i]); i += cs; } index += rs; } 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 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); }