Click or drag to resize

DoubleMatrixApply Method

Evaluates the specified function at each entry of this instance, and returns a matrix whose entries are given by the corresponding function values.

Namespace:  Novacta.Analytics
Assembly:  Novacta.Analytics (in Novacta.Analytics.dll) Version: 2.0.0
Syntax
public DoubleMatrix Apply(
	Func<double, double> func
)

Parameters

func
Type: SystemFuncDouble, Double
The function to apply to each matrix entry.

Return Value

Type: DoubleMatrix
A matrix whose entries are given by the values the specified function takes on at the entries of this instance.
Exceptions
ExceptionCondition
ArgumentNullExceptionfunc is null.
Remarks

Let LaTeX equation be a matrix, and consider its generic entry

LaTeX equation

where LaTeX equation and LaTeX equation are the number of rows and columns of LaTeX equation, respectively.

Let LaTeX equation the function represented by func. Then method Apply(FuncDouble, Double) returns a matrix, say LaTeX equation, having the same dimensions of LaTeX equation by setting LaTeX equation

Examples

In the following example, a new matrix is built by squaring all the entries in a given matrix.

C#
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               
// 

See Also