weka.core
Class Matrix

java.lang.Object
  extended byweka.core.Matrix
All Implemented Interfaces:
java.lang.Cloneable, java.io.Serializable
Direct Known Subclasses:
ConfusionMatrix, CostMatrix

public class Matrix
extends java.lang.Object
implements java.lang.Cloneable, java.io.Serializable

Class for performing operations on a matrix of floating-point values.

Version:
$Revision: 1.17 $
Author:
Gabi Schmidberger (gabi@cs.waikato.ac.nz), Yong Wang (yongwang@cs.waikato.ac.nz), Eibe Frank (eibe@cs.waikato.ac.nz), Len Trigg (eibe@cs.waikato.ac.nz)
See Also:
Serialized Form

Field Summary
protected  double[][] m_Elements
          The values of the matrix
 
Constructor Summary
Matrix(double[][] array)
          Constructs a matrix using a given array.
Matrix(int nr, int nc)
          Constructs a matrix and initializes it with default values.
Matrix(java.io.Reader r)
          Reads a matrix from a reader.
 
Method Summary
 Matrix add(Matrix other)
          Returns the sum of this matrix with another.
 void addElement(int rowIndex, int columnIndex, double value)
          Add a value to an element.
 java.lang.Object clone()
          Creates and returns a clone of this object.
 void eigenvalueDecomposition(double[][] V, double[] d)
          Performs Eigenvalue Decomposition using Householder QR Factorization This function is adapted from the CERN Jet Java libraries, for it the following copyright applies (see also, text on top of file) Copyright (C) 1999 CERN - European Organization for Nuclear Research.
 double[] getColumn(int index)
          Gets a column of the matrix and returns it as a double array.
 double getElement(int rowIndex, int columnIndex)
          Returns the value of a cell in the matrix.
 Matrix getL()
          Returns the L part of the matrix.
 double[] getRow(int index)
          Gets a row of the matrix and returns it as double array.
 Matrix getU()
          Returns the U part of the matrix.
protected static double hypot(double a, double b)
          Returns sqrt(a^2 + b^2) without under/overflow.
protected  void initialize()
          Resets the elements to default values (i.e. 0).
 boolean isSymmetric()
          Returns true if the matrix is symmetric.
 int[] LUDecomposition()
          Performs a LUDecomposition on the matrix.
static void main(java.lang.String[] ops)
          Main method for testing this class.
 Matrix multiply(Matrix b)
          Returns the multiplication of two matrices
 int numColumns()
          Returns the number of columns in the matrix.
 int numRows()
          Returns the number of rows in the matrix.
 double[] regression(Matrix y, double ridge)
          Performs a (ridged) linear regression.
 double[] regression(Matrix y, double[] w, double ridge)
          Performs a weighted (ridged) linear regression.
 void setColumn(int index, double[] newColumn)
          Sets a column of the matrix to the given column.
 void setElement(int rowIndex, int columnIndex, double value)
          Sets an element of the matrix to the given value.
 void setRow(int index, double[] newRow)
          Sets a row of the matrix to the given row.
 void solve(double[] bb)
          Solve A*X = B using backward substitution.
 boolean testEigen(Matrix V, double[] d, boolean verbose)
          Test eigenvectors and eigenvalues.
 java.lang.String toString()
          Converts a matrix to a string
private  void tql2(double[][] V, double[] d, double[] e, int n)
          Symmetric tridiagonal QL algorithm.
 Matrix transpose()
          Returns the transpose of a matrix.
private  void tred2(double[][] V, double[] d, double[] e, int n)
          Symmetric Householder reduction to tridiagonal form.
 void write(java.io.Writer w)
          Writes out a matrix.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

m_Elements

protected double[][] m_Elements
The values of the matrix

Constructor Detail

Matrix

public Matrix(int nr,
              int nc)
Constructs a matrix and initializes it with default values.

Parameters:
nr - the number of rows
nc - the number of columns

Matrix

public Matrix(double[][] array)
       throws java.lang.Exception
Constructs a matrix using a given array.

Parameters:
array - the values of the matrix

Matrix

public Matrix(java.io.Reader r)
       throws java.lang.Exception
Reads a matrix from a reader. The first line in the file should contain the number of rows and columns. Subsequent lines contain elements of the matrix.

Parameters:
r - the reader containing the matrix
Throws:
java.lang.Exception - if an error occurs
Method Detail

clone

public java.lang.Object clone()
                       throws java.lang.CloneNotSupportedException
Creates and returns a clone of this object.

Returns:
a clone of this instance.
Throws:
java.lang.CloneNotSupportedException - if an error occurs

write

public void write(java.io.Writer w)
           throws java.lang.Exception
Writes out a matrix.

Parameters:
w - the output Writer
Throws:
java.lang.Exception - if an error occurs

initialize

protected void initialize()
Resets the elements to default values (i.e. 0).


getElement

public final double getElement(int rowIndex,
                               int columnIndex)
Returns the value of a cell in the matrix.

Parameters:
rowIndex - the row's index
columnIndex - the column's index
Returns:
the value of the cell of the matrix

addElement

public final void addElement(int rowIndex,
                             int columnIndex,
                             double value)
Add a value to an element.

Parameters:
rowIndex - the row's index.
columnIndex - the column's index.
value - the value to add.

numRows

public final int numRows()
Returns the number of rows in the matrix.

Returns:
the number of rows

numColumns

public final int numColumns()
Returns the number of columns in the matrix.

Returns:
the number of columns

setElement

public final void setElement(int rowIndex,
                             int columnIndex,
                             double value)
Sets an element of the matrix to the given value.

Parameters:
rowIndex - the row's index
columnIndex - the column's index
value - the value

setRow

public final void setRow(int index,
                         double[] newRow)
Sets a row of the matrix to the given row. Performs a deep copy.

Parameters:
index - the row's index
newRow - an array of doubles

getRow

public double[] getRow(int index)
Gets a row of the matrix and returns it as double array.

Parameters:
index - the row's index
Returns:
an array of doubles

getColumn

public double[] getColumn(int index)
Gets a column of the matrix and returns it as a double array.

Parameters:
index - the column's index
Returns:
an array of doubles

setColumn

public final void setColumn(int index,
                            double[] newColumn)
Sets a column of the matrix to the given column. Performs a deep copy.

Parameters:
index - the column's index
newColumn - an array of doubles

toString

public java.lang.String toString()
Converts a matrix to a string

Returns:
the converted string

add

public final Matrix add(Matrix other)
Returns the sum of this matrix with another.

Returns:
a matrix containing the sum.

transpose

public final Matrix transpose()
Returns the transpose of a matrix.

Returns:
the transposition of this instance.

isSymmetric

public boolean isSymmetric()
Returns true if the matrix is symmetric.

Returns:
boolean true if matrix is symmetric.

multiply

public final Matrix multiply(Matrix b)
Returns the multiplication of two matrices

Parameters:
b - the multiplication matrix
Returns:
the product matrix

regression

public final double[] regression(Matrix y,
                                 double ridge)
Performs a (ridged) linear regression.

Parameters:
y - the dependent variable vector
ridge - the ridge parameter
Returns:
the coefficients
Throws:
java.lang.IllegalArgumentException - if not successful

regression

public final double[] regression(Matrix y,
                                 double[] w,
                                 double ridge)
Performs a weighted (ridged) linear regression.

Parameters:
y - the dependent variable vector
w - the array of data point weights
ridge - the ridge parameter
Returns:
the coefficients
Throws:
java.lang.IllegalArgumentException - if the wrong number of weights were provided.

getL

public Matrix getL()
            throws java.lang.Exception
Returns the L part of the matrix. This does only make sense after LU decomposition.

Returns:
matrix with the L part of the matrix;
Throws:
java.lang.Exception

getU

public Matrix getU()
            throws java.lang.Exception
Returns the U part of the matrix. This does only make sense after LU decomposition.

Returns:
matrix with the U part of a matrix;
Throws:
java.lang.Exception

LUDecomposition

public int[] LUDecomposition()
                      throws java.lang.Exception
Performs a LUDecomposition on the matrix. It changes the matrix into its LU decomposition using the Crout algorithm

Returns:
the indices of the row permutation
Throws:
java.lang.Exception - if the matrix is singular

solve

public void solve(double[] bb)
           throws java.lang.Exception
Solve A*X = B using backward substitution. A is current object (this) B parameter bb X returned in parameter bb

Parameters:
bb - first vektor B in above equation then X in same equation.
Throws:
matrix - is singulaer
java.lang.Exception

eigenvalueDecomposition

public void eigenvalueDecomposition(double[][] V,
                                    double[] d)
                             throws java.lang.Exception
Performs Eigenvalue Decomposition using Householder QR Factorization This function is adapted from the CERN Jet Java libraries, for it the following copyright applies (see also, text on top of file) Copyright (C) 1999 CERN - European Organization for Nuclear Research. Matrix must be symmetrical. Eigenvectors are return in parameter V, as columns of the 2D array. Eigenvalues are returned in parameter d.

Parameters:
V - double array in which the eigenvectors are returned
d - array in which the eigenvalues are returned
Throws:
if - matrix is not symmetric
java.lang.Exception

tred2

private void tred2(double[][] V,
                   double[] d,
                   double[] e,
                   int n)
            throws java.lang.Exception
Symmetric Householder reduction to tridiagonal form. This function is adapted from the CERN Jet Java libraries, for it the following copyright applies (see also, text on top of file) Copyright (C) 1999 CERN - European Organization for Nuclear Research.

Parameters:
V - containing a copy of the values of the matrix
d -
e -
n - size of matrix
Throws:
if - reduction doesn't work
java.lang.Exception

tql2

private void tql2(double[][] V,
                  double[] d,
                  double[] e,
                  int n)
           throws java.lang.Exception
Symmetric tridiagonal QL algorithm. This function is adapted from the CERN Jet Java libraries, for it the following copyright applies (see also, text on top of file) Copyright (C) 1999 CERN - European Organization for Nuclear Research.

Parameters:
V - tridiagonalized matrix
d -
e -
n - size of matrix
Throws:
java.lang.Exception

hypot

protected static double hypot(double a,
                              double b)
Returns sqrt(a^2 + b^2) without under/overflow. This function is adapted from the CERN Jet Java libraries, for it the following copyright applies (see also, text on top of file) Copyright (C) 1999 CERN - European Organization for Nuclear Research.

Parameters:
a - length of one side of rectangular triangle
b - length of other side of rectangular triangle
Returns:
lenght of third side

testEigen

public boolean testEigen(Matrix V,
                         double[] d,
                         boolean verbose)
                  throws java.lang.Exception
Test eigenvectors and eigenvalues. function is used for debugging

Parameters:
V - matrix with eigenvectors of A
d - array with eigenvalues of A
Throws:
if - new matrix cannot be made
java.lang.Exception

main

public static void main(java.lang.String[] ops)
Main method for testing this class.