Click or drag to resize

DoubleMatrixSubtract Method (DoubleMatrix, DoubleMatrix)

Determines the difference between two matrices.

Namespace:  Novacta.Analytics
Assembly:  Novacta.Analytics (in Novacta.Analytics.dll) Version: 2.0.0
Syntax
public static DoubleMatrix Subtract(
	DoubleMatrix left,
	DoubleMatrix right
)

Parameters

left
Type: Novacta.AnalyticsDoubleMatrix
The left operand.
right
Type: Novacta.AnalyticsDoubleMatrix
The right operand.

Return Value

Type: DoubleMatrix
The result of subtracting right from left.
Exceptions
ExceptionCondition
ArgumentNullExceptionleft is null.
-or-
right is null.
ArgumentException Both left and right are not scalar matrices, and they have not the same number of rows and columns.
Remarks

Let LaTeX equation and LaTeX equation be the left number of rows and columns, respectively, and consider its generic entry

LaTeX equation

Analogously, Let LaTeX equation and LaTeX equation be the right number of rows and columns, respectively, and let its generic entry given by

LaTeX equation

The method operates as follows.

  • If left is scalar, then the method returns a matrix having the same dimensions of right, whose generic entry is:

    LaTeX equation

  • If right is scalar, then the method returns a matrix having the same dimensions of left, whose generic entry is:

    LaTeX equation

  • If neither left nor right is scalar, then the method returns a matrix whose generic entry is

    LaTeX equation

    provided that the dimensions of left agree with those of right; otherwise, an exception is thrown.

Examples

In the following example, the difference between two matrices is computed.

C#
using System;

namespace Novacta.Analytics.CodeExamples
{
    public class SubtractionExample0  
    {
        public void Main()
        {
            // Create the left operand.
            var data = new double[6] {
                0,  2,  4,
                1,  3,  5,
            };
            var left = DoubleMatrix.Dense(2, 3, data, StorageOrder.RowMajor);
            Console.WriteLine("left =");
            Console.WriteLine(left);

            // Create the right operand.
            data = new double[6] {
                0,  20,  40,
               10,  30,  50,
            };
            var right = DoubleMatrix.Dense(2, 3, data, StorageOrder.RowMajor);
            Console.WriteLine("right =");
            Console.WriteLine(right);

            // Subtract right from left.
            var result = left - right;

            Console.WriteLine();
            Console.WriteLine("left - right =");
            Console.WriteLine(result);

            // In .NET languages that do not support overloaded operators,
            // you can use the alternative methods named Subtract.
            result = DoubleMatrix.Subtract(left, right);

            Console.WriteLine();
            Console.WriteLine("DoubleMatrix.Subtract(left, right) returns");
            Console.WriteLine();
            Console.WriteLine(result);

            // Both operators and alternative methods are overloaded to 
            // support read-only matrix arguments.
            // Compute the subtraction using a read-only wrapper of left.
            ReadOnlyDoubleMatrix readOnlyLeft = left.AsReadOnly();
            result = readOnlyLeft - right;

            Console.WriteLine();
            Console.WriteLine("readOnlyLeft - right =");
            Console.WriteLine(result);
        }
    }
}

// Executing method Main() produces the following output:
// 
// left =
// 0                2                4                
// 1                3                5                
// 
// 
// right =
// 0                20               40               
// 10               30               50               
// 
// 
// 
// left - right =
// 0                -18              -36              
// -9               -27              -45              
// 
// 
// 
// DoubleMatrix.Subtract(left, right) returns
// 
// 0                -18              -36              
// -9               -27              -45              
// 
// 
// 
// readOnlyLeft - right =
// 0                -18              -36              
// -9               -27              -45              
// 

See Also