This section will review some of the matrix operations that are provided by the MatrixMath class. Each matrix operation will be explained, as well as the code that is used to perform the operation. You can try some of the matrix operations online at the following URL.

http://www.heatonresearch.com/examples/math/matrix

Matrix Addition

    Matrix addition is a relatively simple procedure. The corresponding cells of two matrixes of exactly the same size are summed. The results are returned in a new matrix of equal size. Equation 2.7 describes the process of matrix addition.

Equation 2.7: Matrix Addition

A Threshold and Weight Matrix

    The signature for the Add method of the MatrixMath class is shown here.

public static Matrix Add(Matrix a, Matrix b)

    To add two matrixes they must have the same number of rows and columns. First check the rows, if they do not match, then throw an error.

if (a.Rows != b.Rows)
{
  throw new MatrixError(
    "To add the matrixes they must have the same number of rows and columns.  Matrix a has "
    + a.Rows
    + " rows and matrix b has "
    + b.Rows + " rows.");
}

    Next, check the columns, if they do not match then throw an error.

if (a.Cols != b.Cols)
{
  throw new MatrixError(
  "To add the matrixes they must have the same number of rows and columns.  Matrix a has "
    + a.Cols
    + " cols and matrix b has "
    + b.Cols + " cols.");
}

    Allocate a new 2D array to hold the newly created matrix. The new matrix will contain the result of the addition.

double[,] result = new double[a.Rows, a.Cols];

    Loop over every row and column in the matrix.

for (int resultRow = 0; resultRow < a.Rows; resultRow++)
{
  for (int resultCol = 0; resultCol < a.Cols; resultCol++)
  {

    Add the each cell in the two matrixes and store the result in the newly created matrix stored in the result variable.

    result[resultRow, resultCol] = a[resultRow, resultCol]
    + b[resultRow, resultCol];
  }
}

    Construct a new Matrix from the result variable.

return new Matrix(result);

    This new matrix is returned.

Matrix Division by a Scalar

    A matrix can be divided by a single number, or scalar. For example, to divide a matrix by the number 10, each cell in the matrix would be divided by 10. Equation 2.8 describes the process of dividing a matrix by a scalar.

Equation 2.8: Matrix Division by a Scalar

A Threshold and Weight Matrix

    The signature for the matrix Divide function is shown here.

public static Matrix Divide(Matrix a, double b)

    First, allocate a new array to hold the result of the division.

double[,] result = new double[a.Rows, a.Cols];

    Loop over every cell in the matrix that is to be divided by the scalar.

for (int row = 0; row < a.Rows; row++)
{
  for (int col = 0; col < a.Cols; col++)
  {

    Divide every cell by the specified scalar. The result of the division is placed in the result array.

    result[row, col] = a[row, col] / b;
  }
}

    Finally, a new matrix is created to hold the result.

return new Matrix(result);

    This new matrix is returned to the caller.

Compute the Dot Product

    The dot product can be computed from two vector matrixes. A vector matrix contains either a single row or a single column. To determine the dot product of two matrixes, they must have the same number of cells. It is not necessary that the cells in the two matrixes be oriented the same way. It is only necessary that they have the same number of cells.

    The dot product is a scalar, a single number, not another matrix. To calculate the dot product, each cell, taken in order and beginning at the top-left side of the matrix, is multiplied by the corresponding cell in the other matrix. The results of these multiplication operations are then summed. Equation 2.9 describes the process of computing the dot product.

Equation 2.9: Dot Product

A Threshold and Weight Matrix

    The signature for the DotProduct method is shown here.

public static double DotProduct(Matrix a, Matrix b)

    To take the dot product of two matrixes one, or both, must be a vector. If this is not the case, then throw an error.

if (!a.IsVector() || !b.IsVector())
{
  throw new MatrixError(
"To take the dot product, both matrixes must be vectors.");
}

    Convert both matrixes into flat arrays.

Double[] aArray = a.ToPackedArray();
Double[] bArray = b.ToPackedArray();

    If both matrixes are not of the same length, then the dot product cannot be taken. If this is the case, throw an exception.

if (aArray.Length != bArray.Length)
{
  throw new MatrixError(
    "To take the dot product, both matrixes must be of the same length.");
}

    Setup an result variable and calculate the length. The result variable will hold the addition of each array element.

double result = 0;
int length = aArray.Length;

    Loop over each element in the arrays.

for (int i = 0; i < length; i++)
{

    Add up the product of each of the two arrays.

  result += aArray[i] * bArray[i];
}

    Finally, return the result.

return result;

    The dot product is returned to the calling method.

Matrix Multiplication and the Identity Matrix

    Matrix multiplication can only be performed if two matrixes have compatible dimensions. Compatible dimensions mean that the number of columns of the first matrix must be equal to the number of rows of the second matrix. This means that it is legal to multiply a 2x3 matrix by a 3x2 matrix. However, it is not legal to multiply a 2x3 matrix by a 2x6 matrix!

    Next, we will see how to multiply two matrixes. Equation 2.10 describes how a 2x3 matrix is multiplied by a 3x2 matrix.

Equation 2.10: Matrix Multiplication

A Threshold and Weight Matrix

    It is also important to note that matrix multiplication is not commutative. The result of 2*6 is the same as 6*2. This is because multiplication is commutative when dealing with scalars—not so with matrixes. The result of multiplying a 1x3 matrix by a 3x2 matrix is not at all the same as multiplying a 3x2 matrix by a 1x3 matrix. In fact, it is not even valid to multiply a 3x2 matrix by a 1x3. Recall in Equation 2.10 we multiplied a 2x3 matrix by a 3x2 matrix. Equation 2.11 illustrates the results of multiplying a 3x2 matrix by a 2x3 matrix. This operation produces a completely different result than Equation 2.10.

Equation 2.11: Non-Commutative Matrix Multiplication

A Threshold and Weight Matrix

    An identity matrix is a matrix that when multiplied by another matrix produces the same matrix. Think of this as multiplying a number by 1. Equation 2.12 describes the identity matrix.

Equation 2.12: Identity Matrix

A Threshold and Weight Matrix

    An identity matrix is always perfectly square. A matrix that is not square does not have an identity matrix. As you can see from Equation 2.12, the identity matrix is created by starting with a matrix that has only zero values. The cells in the diagonal from the northwest corner to the southeast corner are then set to one.

    Equation 2.13 describes an identity matrix being multiplied by another matrix.

Equation 2.13: Multiply by an Identity Matrix

A Threshold and Weight Matrix

    The resulting matrix in Equation 2.13 is the same as the matrix that was multiplied by the identity matrix.

    The signature for the Identity method is shown here.

public static Matrix Identity(int size)

    To take the identity the size must be greater than one. If this is not the case then throw an exception.

if (size < 1)
{
  throw new MatrixError("Identity matrix must be at least of size 1.");
}

    Create a new matrix to hold the identify. Identity matrixes are always square.

Matrix result = new Matrix(size, size);

    Loop across the diagonal of the matrix.

for (int i = 0; i < size; i++)
{

    Set each element of the diagonal to one.

  result[i, i] = 1;
}

    The result of the identity is stored in the result variable.

return result;

    The identity is returned to the calling method.

Matrix Multiplication by a Scalar

    Matrixes can also be multiplied by a scalar. Matrix multiplication by a scalar is very simple to perform—every cell in the matrix is multiplied by the specified scalar. Equation 2.14 shows how this is done.

Equation 2.14: Matrix Multiplication by a Scalar

A Threshold and Weight Matrix

    The signature for the Multiply by a scalar method is shown here.

public static Matrix Multiply(Matrix a, double b)

    First, create a new matrix to hold the result of the multiplication.

double[,] result = new double[a.Rows, a.Cols];

    Loop over every cell in the a matrix.

for (int row = 0; row < a.Rows; row++)
{
  for (int col = 0; col < a.Cols; col++)
  {

    Assign each cell in the result matrix to the product of something in the a matrix and the b scalar.

    result[row, col] = a[row, col] * b;
  }
}

    Finally, create a new matrix based on the result variable.

return new Matrix(result);

    The result of the multiplication is returned to the calling method.

Matrix Subtraction

    Matrix subtraction is a relatively simple procedure, also. The two matrixes on which the subtraction operation will be performed must be exactly the same size. Each cell in the resulting matrix is the difference of the two corresponding cells from the source matrixes. Equation 2.15 describes the process of matrix subtraction.

Equation 2.15: Matrix Subtraction

A Threshold and Weight Matrix

    The signature for the Subtract method of the MatrixMath class is shown here.

public static Matrix Subtract(Matrix a, Matrix b)

    Both matrixes should have the same number of rows and columns. First check to see if they have the same number of rows. If they do not, then throw an exception.

if (a.Rows != b.Rows)
{
  throw new MatrixError(
"To subtract the matrixes they must have the same number of rows and columns.  Matrix a has "
    + a.Rows
    + " rows and matrix b has "
    + b.Rows + " rows.");
}

    Next check to see if they have the same number of columns. If they do not, then throw an exception.

if (a.Cols != b.Cols)
{
  throw new MatrixError(
"To subtract the matrixes they must have the same number of rows and columns.  Matrix a has "
  + a.Cols
  + " cols and matrix b has "
  + b.Cols + " cols.");
}

    Create a new array that is large enough to hold the results of the subtraction.

double[,] result = new double[a.Rows, a.Cols];

    Loop over every cell in the matrix.

for (int resultRow = 0; resultRow < a.Rows; resultRow++)
{
  for (int resultCol = 0; resultCol < a.Cols; resultCol++)
  {

    For each cell store the result of the subtraction in the result array.

    result[resultRow, resultCol] = a[resultRow, resultCol]
      - b[resultRow, resultCol];
  }
}

    Finally, create a new matrix based on the result variable.

return new Matrix(result);

    The result of the subtraction is returned to the calling method.

Transpose a Matrix

    Matrix transposition occurs when the rows and columns of a matrix are interchanged. Equation 2.16 describes the transposition of a matrix.

Equation 2.16: Matrix Transpose

A Threshold and Weight Matrix

    The signature for the transpose method is shown here.

public static Matrix Transpose(Matrix input)

    Create a new array that is large enough to hold the results of the transposition.

double[,] inverseMatrix = new double[input.Cols, 
  input.Rows];

    Loop over every cell in the matrix.

for (int r = 0; r < input.Rows; r++)
{
  for (int c = 0; c < input.Cols; c++)
  {

    For each cell store the result of the transposition in the result array.

    inverseMatrix[c, r] = input[r, c];
  }
}

    Finally, create a new matrix based on the result variable.

return new Matrix(inverseMatrix);

    The result of the subtraction is returned to the calling method.

Vector Length

    The length of a vector matrix is defined to be the square root of the squared sums of every cell in the matrix. Equation 2.17 describes how the vector length for a vector matrix is calculated.

Equation 2.17: Calculate Vector Length

A Threshold and Weight Matrix

    The MatrixMath class provides the VectorLength function which can be used to calculate this length. The signature for the VectorLength function is shown here.

public static double VectorLength(Matrix input)

    To take the vector length of a matrix, that matrix must be a vector. First check to see if the matrix is actually a vector. If the matrix is not a vector, then throw an exception.

if (!input.IsVector())
{
  throw new MatrixError(
    "Can only take the vector length of a vector.");
}

    Convert the matrix to a packed array. This allows us to view the matrix as a linear array of numbers.

Double[] v = input.ToPackedArray();
double rtn = 0.0;

    Loop over every element in the packed array.

for (int i = 0; i < v.Length; i++)
{

    Take the square of each each array element. Sum these values into the rtn variable.

  rtn += Math.Pow(v[i], 2);
}

    Finally, return the square root of this sum.

return Math.Sqrt(rtn);

    This square root will be returned to the calling method. The length of a vector will be particularly important for the self-organizing maps that will be presented in chapter 8.


Copyright 2005 - 2010 by Heaton Research, Inc.. Heaton Research™ and Encog™ are trademarks of Heaton Research. Click here for copyright and trademark information.