Tyche Trading

Matrix Dot Product

Bar Info

Debugging

Derivatives

Matrix Handling

Price Rounding

Regression Lines

Statistics

User Tracking

Example Strategies

Matrix Dot Product

Description:

Calculate the dot product of two matrices.

 

Syntax:

DotProduct(double[,] matrix1, double[,] matrix2);

 

Arguments:

matrix1: The first 2d array.

matrix2: The second 2d array.

 

Notes:

In order to calculate the dot product of tow matricies, the number of columns of the first matrix must equal the number of rows of the second matrix.

				
					/// <summary>
/// Calculates the dot product of two 2d martrices.
/// </summary>
/// <param name="matrix1">The first 2d array.</param>
/// <param name="matrix2">The second 2d array.</param>
/// <remarks>The first arrays number of columns must be equal to the second arrays number of rows.</remarks>
/// <returns> 
/// The dot product of two 2d matrices as a 2d array.
/// </returns>
private static double[,] DotProduct(double[,] matrix1, double[,] matrix2)
{
    var Rows1 = matrix1.GetLength(0);
    var Cols1 = matrix1.GetLength(1);
    var Rows2 = matrix2.GetLength(0);
    var Cols2 = matrix2.GetLength(1);
    
    if (Cols1 != Rows2)
        throw new InvalidOperationException
            ("Dot product is undefined. The number of columns of first matrix must equal to the number of rows of second matrix.");

    double[,] product = new double[Rows1, Cols2];
    
    for (int row1 = 0; row1 < Rows1; row1++) // iterate through the first matrices rows
        for (int col2 = 0; col2 < Cols2; col2++) // iterate through the second matrices columns
            for (int col1 = 0; col1 < Cols1; col1++) // iterate through the first matrices columns
                product[row1, col2] += matrix1[row1, col1] * matrix2[col1, col2];

    return product;
}