org.apache.mahout.math
Class AbstractMatrix

java.lang.Object
  extended by org.apache.mahout.math.AbstractMatrix
All Implemented Interfaces:
java.lang.Cloneable, java.lang.Iterable<MatrixSlice>, Matrix, VectorIterable
Direct Known Subclasses:
DenseMatrix, MatrixView, SparseColumnMatrix, SparseMatrix, SparseRowMatrix, VectorList

public abstract class AbstractMatrix
extends java.lang.Object
implements Matrix

A few universal implementations of convenience functions


Nested Class Summary
protected  class AbstractMatrix.TransposeViewVector
           
 
Field Summary
protected  int[] cardinality
           
static int COL
           
protected  java.util.Map<java.lang.String,java.lang.Integer> columnLabelBindings
           
static int ROW
           
protected  java.util.Map<java.lang.String,java.lang.Integer> rowLabelBindings
           
 
Constructor Summary
AbstractMatrix()
           
 
Method Summary
 double aggregate(BinaryFunction combiner, UnaryFunction mapper)
          Collects the results of a function applied to each element of a matrix and then aggregated.
 Vector aggregateColumns(VectorFunction f)
          Collects the results of a function applied to each column of a matrix.
 Vector aggregateRows(VectorFunction f)
          Collects the results of a function applied to each row of a matrix.
 java.lang.String asFormatString()
           
 Matrix assign(double value)
          Assign the value to all elements of the receiver
 Matrix assign(double[][] values)
          Assign the values to the receiver
 Matrix assign(Matrix other)
          Assign the other vector values to the receiver
 Matrix assign(Matrix other, BinaryFunction function)
          Apply the function to each element of the receiver and the corresponding element of the other argument
 Matrix assign(UnaryFunction function)
          Apply the function to each element of the receiver
 Matrix clone()
          Return a copy of the recipient
 int columnSize()
           
static Matrix decodeMatrix(java.lang.String formatString)
           
 double determinant()
          Returns matrix determinator using Laplace theorem
 Matrix divide(double x)
          Return a new matrix containing the values of the recipient divided by the argument
 double get(int row, int column)
          Return the value at the given indexes
 double get(java.lang.String rowLabel, java.lang.String columnLabel)
          Return the value at the given labels
 java.util.Map<java.lang.String,java.lang.Integer> getColumnLabelBindings()
          Return a map of the current column label bindings of the receiver
 java.util.Map<java.lang.String,java.lang.Integer> getRowLabelBindings()
          Return a map of the current row label bindings of the receiver
 java.util.Iterator<MatrixSlice> iterateAll()
           
 java.util.Iterator<MatrixSlice> iterator()
           
 Matrix minus(Matrix other)
          Return a new matrix containing the element by element difference of the recipient and the argument
 int numCols()
           
 int numRows()
           
 int numSlices()
          Abstracted out for the iterator
 Matrix plus(double x)
          Return a new matrix containing the sum of each value of the recipient and the argument
 Matrix plus(Matrix other)
          Return a new matrix containing the element by element sum of the recipient and the argument
 int rowSize()
           
 void set(int row, double[] data)
           
 void set(int row, int column, double value)
          Set the value at the given index
 void set(java.lang.String rowLabel, double[] rowData)
          Sets the row values at the given row label
 void set(java.lang.String rowLabel, int row, double[] rowData)
          Sets the row values at the given row index and updates the row labels
 void set(java.lang.String rowLabel, java.lang.String columnLabel, double value)
          Set the value at the given index
 void set(java.lang.String rowLabel, java.lang.String columnLabel, int row, int column, double value)
          Set the value at the given index, updating the row and column label bindings
 void setColumnLabelBindings(java.util.Map<java.lang.String,java.lang.Integer> bindings)
          Sets a map of column label bindings in the receiver
 void setRowLabelBindings(java.util.Map<java.lang.String,java.lang.Integer> bindings)
          Sets a map of row label bindings in the receiver
 int[] size()
          Return the cardinality of the recipient (the maximum number of values)
protected  Vector slice(int index)
          Abstracted out for iterating over either rows or columns (default is rows).
 Matrix times(double x)
          Return a new matrix containing the product of each value of the recipient and the argument
 Matrix times(Matrix other)
          Return a new matrix containing the product of the recipient and the argument
 Vector times(Vector v)
          Return a new vector with cardinality equal to getNumRows() of this matrix which is the matrix product of the recipient and the argument
 Vector timesSquared(Vector v)
          Convenience method for producing this.transpose().times(this.times(v)), which can be implemented with only one pass over the matrix, without making the transpose() call (which can be expensive if the matrix is sparse)
 Matrix transpose()
          Return a new matrix that is the transpose of the receiver
 Vector viewColumn(int column)
          Returns a view of a row.
 Matrix viewPart(int rowOffset, int rowsRequested, int columnOffset, int columnsRequested)
          Return a new matrix containing the subset of the recipient
 Vector viewRow(int row)
          Returns a view of a row.
 double zSum()
          Return the sum of all the elements of the receiver
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.apache.mahout.math.Matrix
assignColumn, assignRow, getColumn, getNumNondefaultElements, getQuick, getRow, like, like, setQuick, viewPart
 

Field Detail

columnLabelBindings

protected java.util.Map<java.lang.String,java.lang.Integer> columnLabelBindings

rowLabelBindings

protected java.util.Map<java.lang.String,java.lang.Integer> rowLabelBindings

cardinality

protected int[] cardinality

COL

public static final int COL
See Also:
Constant Field Values

ROW

public static final int ROW
See Also:
Constant Field Values
Constructor Detail

AbstractMatrix

public AbstractMatrix()
Method Detail

columnSize

public int columnSize()
Specified by:
columnSize in interface Matrix
Returns:
The number of rows in the matrix.

rowSize

public int rowSize()
Specified by:
rowSize in interface Matrix
Returns:
Returns the number of rows in the matrix.

size

public int[] size()
Description copied from interface: Matrix
Return the cardinality of the recipient (the maximum number of values)

Specified by:
size in interface Matrix
Returns:
an int[2]

iterator

public java.util.Iterator<MatrixSlice> iterator()
Specified by:
iterator in interface java.lang.Iterable<MatrixSlice>

iterateAll

public java.util.Iterator<MatrixSlice> iterateAll()
Specified by:
iterateAll in interface VectorIterable

slice

protected Vector slice(int index)
Abstracted out for iterating over either rows or columns (default is rows).

Parameters:
index - the row or column number to grab as a vector (shallowly)
Returns:
the row or column vector at that index.

numSlices

public int numSlices()
Abstracted out for the iterator

Specified by:
numSlices in interface VectorIterable
Returns:
numRows() for row-based iterator, numColumns() for column-based.

get

public double get(java.lang.String rowLabel,
                  java.lang.String columnLabel)
Description copied from interface: Matrix
Return the value at the given labels

Specified by:
get in interface Matrix
Parameters:
rowLabel - a String row label
columnLabel - a String column label
Returns:
the double at the index

getColumnLabelBindings

public java.util.Map<java.lang.String,java.lang.Integer> getColumnLabelBindings()
Description copied from interface: Matrix
Return a map of the current column label bindings of the receiver

Specified by:
getColumnLabelBindings in interface Matrix
Returns:
a Map

getRowLabelBindings

public java.util.Map<java.lang.String,java.lang.Integer> getRowLabelBindings()
Description copied from interface: Matrix
Return a map of the current row label bindings of the receiver

Specified by:
getRowLabelBindings in interface Matrix
Returns:
a Map

set

public void set(java.lang.String rowLabel,
                double[] rowData)
Description copied from interface: Matrix
Sets the row values at the given row label

Specified by:
set in interface Matrix
Parameters:
rowLabel - a String row label
rowData - a double[] array of row data

set

public void set(java.lang.String rowLabel,
                int row,
                double[] rowData)
Description copied from interface: Matrix
Sets the row values at the given row index and updates the row labels

Specified by:
set in interface Matrix
Parameters:
rowLabel - the String row label
row - an int the row index
rowData - a double[] array of row data

set

public void set(java.lang.String rowLabel,
                java.lang.String columnLabel,
                double value)
Description copied from interface: Matrix
Set the value at the given index

Specified by:
set in interface Matrix
Parameters:
rowLabel - a String row label
columnLabel - a String column label
value - a double value to set

set

public void set(java.lang.String rowLabel,
                java.lang.String columnLabel,
                int row,
                int column,
                double value)
Description copied from interface: Matrix
Set the value at the given index, updating the row and column label bindings

Specified by:
set in interface Matrix
Parameters:
rowLabel - a String row label
columnLabel - a String column label
row - an int row index
column - an int column index
value - a double value

setColumnLabelBindings

public void setColumnLabelBindings(java.util.Map<java.lang.String,java.lang.Integer> bindings)
Description copied from interface: Matrix
Sets a map of column label bindings in the receiver

Specified by:
setColumnLabelBindings in interface Matrix
Parameters:
bindings - a Map of label bindings

setRowLabelBindings

public void setRowLabelBindings(java.util.Map<java.lang.String,java.lang.Integer> bindings)
Description copied from interface: Matrix
Sets a map of row label bindings in the receiver

Specified by:
setRowLabelBindings in interface Matrix
Parameters:
bindings - a Map of label bindings

numRows

public int numRows()
Specified by:
numRows in interface VectorIterable

numCols

public int numCols()
Specified by:
numCols in interface VectorIterable

decodeMatrix

public static Matrix decodeMatrix(java.lang.String formatString)

asFormatString

public java.lang.String asFormatString()
Specified by:
asFormatString in interface Matrix
Returns:
a formatted String suitable for output

assign

public Matrix assign(double value)
Description copied from interface: Matrix
Assign the value to all elements of the receiver

Specified by:
assign in interface Matrix
Parameters:
value - a double value
Returns:
the modified receiver

assign

public Matrix assign(double[][] values)
Description copied from interface: Matrix
Assign the values to the receiver

Specified by:
assign in interface Matrix
Parameters:
values - a double[] of values
Returns:
the modified receiver

assign

public Matrix assign(Matrix other,
                     BinaryFunction function)
Description copied from interface: Matrix
Apply the function to each element of the receiver and the corresponding element of the other argument

Specified by:
assign in interface Matrix
Parameters:
other - a Matrix containing the second arguments to the function
function - a BinaryFunction to apply
Returns:
the modified receiver

assign

public Matrix assign(Matrix other)
Description copied from interface: Matrix
Assign the other vector values to the receiver

Specified by:
assign in interface Matrix
Parameters:
other - a Matrix
Returns:
the modified receiver

assign

public Matrix assign(UnaryFunction function)
Description copied from interface: Matrix
Apply the function to each element of the receiver

Specified by:
assign in interface Matrix
Parameters:
function - a UnaryFunction to apply
Returns:
the modified receiver

aggregateRows

public Vector aggregateRows(VectorFunction f)
Collects the results of a function applied to each row of a matrix.

Specified by:
aggregateRows in interface Matrix
Parameters:
f - The function to be applied to each row.
Returns:
The vector of results.

viewRow

public Vector viewRow(int row)
Returns a view of a row. Changes to the view will affect the original.

Specified by:
viewRow in interface Matrix
Parameters:
row - Which row to return.
Returns:
A vector that references the desired row.

viewColumn

public Vector viewColumn(int column)
Returns a view of a row. Changes to the view will affect the original.

Specified by:
viewColumn in interface Matrix
Parameters:
column - Which column to return.
Returns:
A vector that references the desired column.

aggregateColumns

public Vector aggregateColumns(VectorFunction f)
Collects the results of a function applied to each column of a matrix.

Specified by:
aggregateColumns in interface Matrix
Parameters:
f - The function to be applied to each column.
Returns:
The vector of results.

aggregate

public double aggregate(BinaryFunction combiner,
                        UnaryFunction mapper)
Collects the results of a function applied to each element of a matrix and then aggregated.

Specified by:
aggregate in interface Matrix
Parameters:
combiner - A function that combines the results of the mapper.
mapper - A function to apply to each element.
Returns:
The result.

determinant

public double determinant()
Description copied from interface: Matrix
Returns matrix determinator using Laplace theorem

Specified by:
determinant in interface Matrix
Returns:
a matrix determinator

clone

public Matrix clone()
Description copied from interface: Matrix
Return a copy of the recipient

Specified by:
clone in interface Matrix
Overrides:
clone in class java.lang.Object
Returns:
a new Matrix

divide

public Matrix divide(double x)
Description copied from interface: Matrix
Return a new matrix containing the values of the recipient divided by the argument

Specified by:
divide in interface Matrix
Parameters:
x - a double value
Returns:
a new Matrix

get

public double get(int row,
                  int column)
Description copied from interface: Matrix
Return the value at the given indexes

Specified by:
get in interface Matrix
Parameters:
row - an int row index
column - an int column index
Returns:
the double at the index

minus

public Matrix minus(Matrix other)
Description copied from interface: Matrix
Return a new matrix containing the element by element difference of the recipient and the argument

Specified by:
minus in interface Matrix
Parameters:
other - a Matrix
Returns:
a new Matrix

plus

public Matrix plus(double x)
Description copied from interface: Matrix
Return a new matrix containing the sum of each value of the recipient and the argument

Specified by:
plus in interface Matrix
Parameters:
x - a double
Returns:
a new Matrix

plus

public Matrix plus(Matrix other)
Description copied from interface: Matrix
Return a new matrix containing the element by element sum of the recipient and the argument

Specified by:
plus in interface Matrix
Parameters:
other - a Matrix
Returns:
a new Matrix

set

public void set(int row,
                int column,
                double value)
Description copied from interface: Matrix
Set the value at the given index

Specified by:
set in interface Matrix
Parameters:
row - an int row index into the receiver
column - an int column index into the receiver
value - a double value to set

set

public void set(int row,
                double[] data)
Specified by:
set in interface Matrix

times

public Matrix times(double x)
Description copied from interface: Matrix
Return a new matrix containing the product of each value of the recipient and the argument

Specified by:
times in interface Matrix
Parameters:
x - a double argument
Returns:
a new Matrix

times

public Matrix times(Matrix other)
Description copied from interface: Matrix
Return a new matrix containing the product of the recipient and the argument

Specified by:
times in interface Matrix
Parameters:
other - a Matrix argument
Returns:
a new Matrix

times

public Vector times(Vector v)
Description copied from interface: VectorIterable
Return a new vector with cardinality equal to getNumRows() of this matrix which is the matrix product of the recipient and the argument

Specified by:
times in interface VectorIterable
Parameters:
v - a vector with cardinality equal to getNumCols() of the recipient
Returns:
a new vector (typically a DenseVector)

timesSquared

public Vector timesSquared(Vector v)
Description copied from interface: VectorIterable
Convenience method for producing this.transpose().times(this.times(v)), which can be implemented with only one pass over the matrix, without making the transpose() call (which can be expensive if the matrix is sparse)

Specified by:
timesSquared in interface VectorIterable
Parameters:
v - a vector with cardinality equal to getNumCols() of the recipient
Returns:
a new vector (typically a DenseVector) with cardinality equal to that of the argument.

transpose

public Matrix transpose()
Description copied from interface: Matrix
Return a new matrix that is the transpose of the receiver

Specified by:
transpose in interface Matrix
Returns:
the transpose

viewPart

public Matrix viewPart(int rowOffset,
                       int rowsRequested,
                       int columnOffset,
                       int columnsRequested)
Description copied from interface: Matrix
Return a new matrix containing the subset of the recipient

Specified by:
viewPart in interface Matrix
Parameters:
rowOffset - The first row of the view
rowsRequested - The number of rows in the view
columnOffset - The first column in the view
columnsRequested - The number of columns in the view
Returns:
a new Matrix that is a view of the original

zSum

public double zSum()
Description copied from interface: Matrix
Return the sum of all the elements of the receiver

Specified by:
zSum in interface Matrix
Returns:
a double


Copyright © 2008-2010 The Apache Software Foundation. All Rights Reserved.