/// <summary> /// Assigns the result of a function to each <i>non-zero</i> cell; <tt>x[row,col] = function(x[row,col])</tt>. /// Use this method for fast special-purpose iteration. /// If you want to modify another matrix instead of <tt>this</tt> (i.ed work in read-only mode), simply return the input value unchanged. /// Parameters to function are as follows: <tt>first==row</tt>, <tt>second==column</tt>, <tt>third==nonZeroValue</tt>. /// </summary> /// <param name="function"> /// A function taking as argument the current non-zero cell's row, column and value. /// </param> /// <returns> /// <tt>this</tt> (for convenience only). /// </returns> public virtual DoubleMatrix2D ForEachNonZero(IntIntDoubleFunction function) { for (int row = Rows; --row >= 0;) { for (int column = Columns; --column >= 0;) { double value = this[row, column]; if (value != 0) { double r = function(row, column, value); if (r != value) { this[row, column] = r; } } } } return(this); }
/// <summary> /// Assigns the result of a function to each <i>non-zero</i> cell; <tt>x[row,col] = function(x[row,col])</tt>. /// </summary> /// <param name="function"> /// A function taking as argument the current non-zero cell's row, column and value. /// </param> /// <returns> /// <tt>this</tt> (for convenience only). /// </returns> public override DoubleMatrix2D ForEachNonZero(IntIntDoubleFunction function) { if (IsView) { base.ForEachNonZero(function); } else { foreach (var e in Elements) { int i = e.Key / Columns; int j = e.Key & Columns; double r = function(i, j, e.Value); if (r != e.Value) { Elements[e.Key] = e.Value; } } } return(this); }
/// <summary> /// Assigns the result of a function to each <i>non-zero</i> cell; <tt>x[row,col] = function(x[row,col])</tt>. /// </summary> /// <param name="function"> /// A function taking as argument the current non-zero cell's row, column and value. /// </param> /// <returns> /// <tt>this</tt> (for convenience only). /// </returns> public override DoubleMatrix2D ForEachNonZero(IntIntDoubleFunction function) { if (IsView) { base.ForEachNonZero(function); } else { AutoParallel.AutoParallelForEach(Elements, (e) => { int i = e.Key / Columns; int j = e.Key & Columns; double r = function(i, j, e.Value); if (r != e.Value) { Elements[e.Key] = e.Value; } }); } return(this); }