weka.core
Class Matrix

java.lang.Object
  |
  +--weka.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. Some of the code is adapted from Numerical Recipes in C.

Author:
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 data in the matrix.
 
Constructor Summary
Matrix(int nr, int nc)
          Constructs a matrix.
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.
 double getElement(int rowIndex, int columnIndex)
          Returns the value of a cell in the matrix.
protected  void initialize()
          Resets the elements to default values (i.e.
 void lubksb(int[] indx, double[] b)
          Performs LU backward substitution.
 int[] ludcmp()
          Performs LU decomposition.
static void main(java.lang.String[] ops)
          Main method for testing this class.
 Matrix multiply(Matrix b)
          Reurns 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)
          Performs a (ridged) linear regression.
 double[] regression(Matrix y, double[] w)
          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.
 java.lang.String toString()
          Converts a matrix to a string
 Matrix transpose()
          Returns the transpose of a matrix.
 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 data in the matrix.
Constructor Detail

Matrix

public Matrix(int nr,
              int nc)
Constructs a matrix.
Parameters:
nr - the number of rows
nc - the number of columns

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.
Overrides:
clone in class java.lang.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

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

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
Overrides:
toString in class java.lang.Object
Returns:
the converted string

add

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

transpose

public final Matrix transpose()
Returns the transpose of a matrix.
Returns:
the transposition of this instance).

multiply

public final Matrix multiply(Matrix b)
Reurns the multiplication of two matrices
Parameters:
b - the multiplication matrix
Returns:
the product matrix

regression

public final double[] regression(Matrix y)
Performs a (ridged) linear regression.
Parameters:
y - the dependent variable vector
Returns:
the coefficients
Throws:
java.lang.IllegalArgumentException - if not successful

regression

public final double[] regression(Matrix y,
                                 double[] w)
Performs a weighted (ridged) linear regression.
Parameters:
y - the dependent variable vector
w - the array of data point weights
Returns:
the coefficients
Throws:
java.lang.IllegalArgumentException - if the wrong number of weights were provided.

lubksb

public final void lubksb(int[] indx,
                         double[] b)
Performs LU backward substitution. Adapted from Numerical Recipes in C.
Parameters:
indx - the indices of the permutation
b - the double vector, storing constant terms in the equation set; it later stores the computed coefficients' values

ludcmp

public final int[] ludcmp()
                   throws java.lang.Exception
Performs LU decomposition. Adapted from Numerical Recipes in C.
Returns:
the indices of the permutation
Throws:
java.lang.Exception - if the matrix is singular

main

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