Maths
Class SquareMatrix

java.lang.Object
  extended by Maths.SquareMatrix
All Implemented Interfaces:
java.io.Serializable

public class SquareMatrix
extends java.lang.Object
implements java.io.Serializable

Represents a square matrix.

Version:
1.0
See Also:
Serialized Form

Nested Class Summary
static class SquareMatrix.Calculation
          Enumeration of the possible ways of calculating the matrix exponential
 class SquareMatrix.SquareMatrixException
          Exception thrown when there is a problem with a SquareMatrix operation
 
Constructor Summary
SquareMatrix(double[][] m)
          Default constructor
 
Method Summary
 SquareMatrix add(SquareMatrix o)
          Adds a matrix to this one and returns a new matrix as the result
 SquareMatrix divide(SquareMatrix B)
          Divides this matrix by another (this / B) and returns a new matrix as the result
 double[] eigValues()
          Calculates the Eigenvalues.
 SquareMatrix eigValuesDiag()
          Calculates the Eigenvalues.
 SquareMatrix eigVectors()
          Calculates the Eigenvectors.
 boolean equals(java.lang.Object ob)
           
 SquareMatrix exp()
          Calculates e^A where A is the current matrix and returns a new matrix as the result
 SquareMatrix expMult(double x)
          Calculates e^Ax where A is this matrix and returns a new matrix as the result.
 double[][] getArray()
          Gets the matrix as an array of doubles
 double getPosition(int i, int j)
          Returns the value in position (i,j) of the matrix
 int hashCode()
           
 SquareMatrix inverse()
          Calculates the inverse of this matrix and returns a new matrix as the result.
 SquareMatrix multiply(SquareMatrix o)
          Multiplies this matrix by another (this × o) and returns a new matrix as a result
 double norm()
          Calculates the maximum absolute column sum norm
 SquareMatrix scalarDivide(double n)
          Divides the matrix by a scalar and returns a new matrix as the result
 SquareMatrix scalarMultiply(double n)
          Multiplies the matrix by a scalar and returns a new matrix as the result
static void setExpMethod(SquareMatrix.Calculation i)
          Sets the method to be used for Exponentiation
static void setForce(int f)
          Sets the minimum number of repeating squaring that will be performed when using the Taylor method
static void setNoThreads(int number)
          Sets the number of threads to be used when doing matrix multiplication
 int size()
          Gets the size of the matrix.
 SquareMatrix square()
          Sqaures the matrix and returns a new Matrix that is the result
 java.lang.String toString()
           
 SquareMatrix transpose()
          Transposes the matrix and returns a new matrix as the result
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SquareMatrix

public SquareMatrix(double[][] m)
             throws SquareMatrix.SquareMatrixException
Default constructor

Parameters:
m - Array representing the matrix
Throws:
SquareMatrix.SquareMatrixException - Thrown if the input array isn't square
Method Detail

square

public SquareMatrix square()
Sqaures the matrix and returns a new Matrix that is the result

Returns:
The resulting matrix

scalarMultiply

public SquareMatrix scalarMultiply(double n)
Multiplies the matrix by a scalar and returns a new matrix as the result

Parameters:
n - The scalar to multiply by
Returns:
The resulting matrix

scalarDivide

public SquareMatrix scalarDivide(double n)
Divides the matrix by a scalar and returns a new matrix as the result

Parameters:
n - The scalar to divide by
Returns:
The resulting matrix

add

public SquareMatrix add(SquareMatrix o)
                 throws SquareMatrix.SquareMatrixException
Adds a matrix to this one and returns a new matrix as the result

Parameters:
o - The matrix to add
Returns:
The resulting matrix
Throws:
SquareMatrix.SquareMatrixException - Thrown if the matrices are not the same size

multiply

public SquareMatrix multiply(SquareMatrix o)
                      throws SquareMatrix.SquareMatrixException
Multiplies this matrix by another (this × o) and returns a new matrix as a result

Parameters:
o - The matrix to multiply by
Returns:
The resulting matrix
Throws:
SquareMatrix.SquareMatrixException - Thrown if the matrices are not the same size

expMult

public SquareMatrix expMult(double x)
                     throws SquareMatrix.SquareMatrixException
Calculates e^Ax where A is this matrix and returns a new matrix as the result. Method used depends on the last call to setExpMethod(Maths.SquareMatrix.Calculation).

Parameters:
x - x in the above equation
Returns:
The resulting matrix
Throws:
SquareMatrix.SquareMatrixException - Thrown if the calculation can't be performed as the Eigenvalues can't be computed

exp

public SquareMatrix exp()
                 throws SquareMatrix.SquareMatrixException
Calculates e^A where A is the current matrix and returns a new matrix as the result

Returns:
The reuslting matrix
Throws:
SquareMatrix.SquareMatrixException - Thrown if the calculation can't be performed as the Eigenvalues can't be computed

norm

public double norm()
Calculates the maximum absolute column sum norm

Returns:
The maximum absolute column sum norm

inverse

public SquareMatrix inverse()
Calculates the inverse of this matrix and returns a new matrix as the result. Uses the technique of augmenting the original matrix (L) with the identity matrix (I) and then reducing the original to the identity doing the same operation on both. What I has become is the inverse.

Returns:
The resulting matrix

getPosition

public double getPosition(int i,
                          int j)
Returns the value in position (i,j) of the matrix

Parameters:
i - The row position
j - The column position
Returns:
The value at (i,j)

size

public int size()
Gets the size of the matrix.

Returns:
The size of the matrix

getArray

public double[][] getArray()
Gets the matrix as an array of doubles

Returns:
the matrix as an array

setExpMethod

public static void setExpMethod(SquareMatrix.Calculation i)
Sets the method to be used for Exponentiation

Parameters:
i - The method to be used

setForce

public static void setForce(int f)
Sets the minimum number of repeating squaring that will be performed when using the Taylor method

Parameters:
f -

setNoThreads

public static void setNoThreads(int number)
Sets the number of threads to be used when doing matrix multiplication

Parameters:
number -

equals

public boolean equals(java.lang.Object ob)
Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

eigVectors

public SquareMatrix eigVectors()
                        throws EigenvalueDecomposition.ConvergenceException
Calculates the Eigenvectors. The actual decompisition is cahced so only performed once for each matrix.

Returns:
The eigenvectors as a square matrix
Throws:
EigenvalueDecomposition.ConvergenceException - Thrown if the eigendeompisition does not converge

eigValues

public double[] eigValues()
                   throws EigenvalueDecomposition.ConvergenceException
Calculates the Eigenvalues. The actual decompisition is cahced so only performed once for each matrix.

Returns:
The eigenvalues as a array of doubles
Throws:
EigenvalueDecomposition.ConvergenceException - Thrown if the eigendeompisition does not converge

eigValuesDiag

public SquareMatrix eigValuesDiag()
                           throws EigenvalueDecomposition.ConvergenceException
Calculates the Eigenvalues. The actual decompisition is cahced so only performed once for each matrix.

Returns:
A SquareMatrix with the eigenvalues on the diagonal.
Throws:
EigenvalueDecomposition.ConvergenceException

transpose

public SquareMatrix transpose()
Transposes the matrix and returns a new matrix as the result

Returns:
The resulting matrix

divide

public SquareMatrix divide(SquareMatrix B)
Divides this matrix by another (this / B) and returns a new matrix as the result

Parameters:
B - The matrix to divide by
Returns:
The resulting matrix

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object