Matrix Operations
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

The signature for the add method of the MatrixMath class is shown here.
public static Matrix add(final Matrix a, final Matrix b)
The process begins with the creation of a new array to hold the results of the operation.
final double result[][] = new double[a.getRows()][a.getCols()];
Next, we loop through the cells of each row and column in the source matrixes.
for (int resultRow = 0; resultRow < a.getRows(); resultRow++) {
for (int resultCol = 0; resultCol < a.getCols(); resultCol++) {The result of each summation is then placed in the corresponding cell in the result array.
result[resultRow][resultCol] = a.get(resultRow, resultCol)
+ b.get(resultRow, resultCol);
}
}Finally, a new matrix is created from the result array.
return new Matrix(result);
This add method can be used to add any two matrixes of the same size.
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

The signature for the matrix divide function is shown here.
public static Matrix divide(final Matrix a, final double b)
First, a result array is created to hold the results of the division. This array is exactly the same size as the array to be divided.
final double result[][] = new double[a.getRows()][a.getCols()];
To perform the division, the divide method must loop through every cell in the matrix.
for (int row = 0; row < a.getRows(); row++) {
for (int col = 0; col < a.getCols(); col++) {Every cell in the matrix is divided by the specified number and placed into the result array.
result[row][col] = a.get(row, col) / b; } }
Finally, a new Matrix object is created from the result array.
return new Matrix(result);
This matrix is returned. It is the result of the division.
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

The signature for the dotProduct method is shown here.
public static double dotProduct(final Matrix a, final Matrix b)
First, two temporary arrays are created to hold the values of the packed matrix. The packed matrix is a simple one dimensional array that holds both dimensions, so the matrix is a flat linear list.
final double aArray[] = a.toPackedArray(); final double bArray[] = b.toPackedArray();
A result variable is declared to hold the sum of the dot product, as it will be computed.
double result = 0; final int length = aArray.length;
The method then loops through the corresponding cells of the two matrixes, adding the result of each multiplication operation to the result variable.
for (int i = 0; i < length; i++) {
result += aArray[i] * bArray[i];
}Finally, the resulting number is returned.
return result;
This result variable is assigned the dot product of the two matrixes.
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

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

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

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

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(final int size)
This method will create an identity matrix of the size specified by the size parameter. First, a new matrix is created that corresponds to the specified size.
final Matrix result = new Matrix(size, size);
Next, a for loop is used to set the northwest to southeast diagonal to one.
for (int i = 0; i < size; i++) {
result.set(i, i, 1);
}Finally, the resulting identity matrix is returned.
return result;
The method returns the result.
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

The signature for the multiply by a scalar method is shown here.
public static Matrix multiply(final Matrix a, final double b)
First, a result array is created to hold the results of the multiplication operation.
final double result[][] = new double[a.getRows()][a.getCols()];
The multiply method then loops through every cell in the original array, multiplies it by the scalar and then stores the result in the new result array.
for (int row = 0; row < a.getRows(); row++) {
for (int col = 0; col < a.getCols(); col++) {
result[row][col] = a.get(row, col) * b;
}
}Finally, a new Matrix object is created from the result array.
return new Matrix(result);
This Matrix object is then 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

The signature for the subtract method of the MatrixMath class is shown here.
public static Matrix subtract(final Matrix a, final Matrix b)
First, a new array is created to hold the results of the subtraction operations.
final double result[][] = new double[a.getRows()][a.getCols()];
Next, we must loop through each row and column in the source matrixes.
for (int resultRow = 0; resultRow < a.getRows(); resultRow++) {
for (int resultCol = 0; resultCol < a.getCols(); resultCol++) {The results of the subtraction operation for a particular pair of cells in the source matrixes are placed in the corresponding cell in the result array.
result[resultRow][resultCol] = a.get(resultRow, resultCol)
- b.get(resultRow, resultCol);
}
}Finally, a new matrix is created from the result array.
return new Matrix(result);
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
![]()
The signature for the transpose method is shown here.
public static Matrix transpose(final Matrix input)
A new inverseMatrix array is created with rows and columns equal in size to the inverse of those of the original matrix.
final double inverseMatrix[][] = new double[input.getCols()][input .getRows()];
Next, we loop through all of the cells in the original matrix. The value of each cell is copied to the location in the result array identified by the inverse row and column of the original cell.
for (int r = 0; r < input.getRows(); r++) {
for (int c = 0; c < input.getCols(); c++) {
inverseMatrix[c][r] = input.get(r, c);
}
}Finally, a new Matrix object is created from the inverseMatrix array.
return new Matrix(inverseMatrix);
This newly created Matrix object 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

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( final Matrix input )
First, an array of the individual cells is created using the toPackedArray function. This function returns the matrix as a simple array of scalars. This allows either a column or row-based vector matrix length to be calculated, since they will both become simple arrays of scalars.
double v[] = input.toPackedArray(); double rtn = 0.0 ;
Next, we loop through the packed array and sum the square of every number.
for ( int i=0;i<v.length;i++ ) {
rtn += Math.pow(v[i],2);
}Finally, the square root of the sum is returned.
return Math.sqrt(rtn);
The length of a vector will be particularly important for the self-organizing maps that will be presented in chapter 8.

Comments
Error with Equation 2.16 and 2.17
Equation 2.16 and Equation 2.17 should be exchanged.
Equation 2.16 defines vector length and Equation 2.17 is an example of a transposed matrix.