ReadOnlyDoubleMatrixApply Method |
Namespace: Novacta.Analytics
Exception | Condition |
---|---|
ArgumentNullException | func is null. |
Let be a matrix, and consider its generic entry
where and
are the
number of rows and columns of , respectively.
Let the function represented by func. Then method Apply(FuncDouble, Double) returns a matrix, say , having the same dimensions of by setting
In the following example, a new matrix is built by squaring all the entries in a given matrix.
using System; namespace Novacta.Analytics.CodeExamples { public class OutPlaceApplyExample0 { public void Main() { // Create a matrix. var data = new double[20] { 1, 8, -3, 6, -2, 2, 2, 2, 0, 7, -3, 9, 3, 2, 9, 5, 2, -5, -1, -4 }; var matrix = DoubleMatrix.Dense(4, 5, data, StorageOrder.RowMajor); Console.WriteLine("The data matrix:"); Console.WriteLine(matrix); // Return a matrix obtained by adding 1 to each entry // of the initial matrix. var plusOneMatrix = matrix.Apply((x) => x + 1); Console.WriteLine(); Console.WriteLine("Matrix obtained by adding 1 to each entry:"); Console.WriteLine(plusOneMatrix); // Add one using a read-only wrapper of the data matrix. ReadOnlyDoubleMatrix readOnlyMatrix = matrix.AsReadOnly(); var plusOneReadOnlyMatrix = readOnlyMatrix.Apply((x) => x + 1); Console.WriteLine(); Console.WriteLine("Adding 1 to each entry of a read only matrix:"); Console.WriteLine(plusOneReadOnlyMatrix); } } } // Executing method Main() produces the following output: // // The data matrix: // 1 8 -3 6 -2 // 2 2 2 0 7 // -3 9 3 2 9 // 5 2 -5 -1 -4 // // // // Matrix obtained by adding 1 to each entry: // 2 9 -2 7 -1 // 3 3 3 1 8 // -2 10 4 3 10 // 6 3 -4 0 -3 // // // // Adding 1 to each entry of a read only matrix: // 2 9 -2 7 -1 // 3 3 3 1 8 // -2 10 4 3 10 // 6 3 -4 0 -3 //