Click or drag to resize

StatSum Method (ReadOnlyDoubleMatrix, DataOperation)

Returns the sum of each row or column in the specified data.

Namespace:  Novacta.Analytics
Assembly:  Novacta.Analytics (in Novacta.Analytics.dll) Version: 2.0.0
Syntax
public static DoubleMatrix Sum(
	ReadOnlyDoubleMatrix data,
	DataOperation dataOperation
)

Parameters

data
Type: Novacta.AnalyticsReadOnlyDoubleMatrix
The data.
dataOperation
Type: Novacta.AnalyticsDataOperation
A constant to specify if the sum is to be computed for rows or columns.

Return Value

Type: DoubleMatrix
The sum of each row or column in the specified data.
Exceptions
ExceptionCondition
ArgumentNullExceptiondata is null.
ArgumentExceptiondataOperation is not a field of DataOperation.
Remarks

This method returns the sum of data rows or columns. Let LaTeX equation and LaTeX equation be the data number of rows and columns, respectively, and define

LaTeX equation

Operating on rows

If dataOperation is OnRows, then the method returns a column vector whose length equals the number of rows of data. The i-th entry of the returned column exposes the total of the i-th data row.

The sum of the i-th row can be represented by the expression

LaTeX equation

Operating on columns

If dataOperation is OnColumns, then the method returns a row vector whose length is the data number of columns. The j-th entry of the returned row exposes the total of the j-th data column.

The sum of the j-th column can be represented by the expression

LaTeX equation

Examples

In the following example, row and column sums in a data matrix are computed.

C#
using System;

namespace Novacta.Analytics.CodeExamples
{
    public class SumExample0  
    {
        public void Main()
        {
            // Create a matrix.
            var data = new double[6] {
                1, 2,
                2, 3,
                3, 4
            };
            var matrix = DoubleMatrix.Dense(3, 2, data, StorageOrder.RowMajor);
            Console.WriteLine("The data matrix:");
            Console.WriteLine(matrix);

            // Compute the sum on columns.
            var sumOnColumns = Stat.Sum(matrix, DataOperation.OnColumns);

            Console.WriteLine();
            Console.WriteLine("Sum on columns:");
            Console.WriteLine(sumOnColumns);

            // Sum is overloaded to accept data as a read-only matrix:
            // compute the sum on rows using a read-only wrapper of the data matrix.
            ReadOnlyDoubleMatrix readOnlyMatrix = matrix.AsReadOnly();
            var sumOnRows = Stat.Sum(readOnlyMatrix, DataOperation.OnRows);

            Console.WriteLine();
            Console.WriteLine("Sum on rows:");
            Console.WriteLine(sumOnRows);
        }
    }
}

// Executing method Main() produces the following output:
// 
// The data matrix:
// 1                2                
// 2                3                
// 3                4                
// 
// 
// 
// Sum on columns:
// 6                9                
// 
// 
// 
// Sum on rows:
// 3                
// 5                
// 7                
// 

See Also