org.apache.mahout.math
Class AbstractMatrix

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

public abstract class AbstractMatrix
extends Object
implements Matrix

A few universal implementations of convenience functions


Nested Class Summary
protected static class AbstractMatrix.TransposeViewVector
           
 
Field Summary
static int COL
           
protected  Map<String,Integer> columnLabelBindings
           
protected  int columns
           
static int ROW
           
protected  Map<String,Integer> rowLabelBindings
           
protected  int rows
           
 
Constructor Summary
protected AbstractMatrix(int rows, int columns)
           
 
Method Summary
 double aggregate(DoubleDoubleFunction combiner, DoubleFunction 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.
 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(DoubleFunction function)
          Apply the function to each element of the receiver
 Matrix assign(Matrix other)
          Assign the other vector values to the receiver
 Matrix assign(Matrix other, DoubleDoubleFunction function)
          Apply the function to each element of the receiver and the corresponding element of the other argument
 Matrix clone()
          Return a copy of the recipient
 int columnSize()
           
 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(String rowLabel, String columnLabel)
          Return the value at the given labels
 Map<String,Integer> getColumnLabelBindings()
          Return a map of the current column label bindings of the receiver
 int[] getNumNondefaultElements()
          Return the number of values in the recipient
 Map<String,Integer> getRowLabelBindings()
          Return a map of the current row label bindings of the receiver
 Iterator<MatrixSlice> iterateAll()
           
 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(String rowLabel, double[] rowData)
          Sets the row values at the given row label
 void set(String rowLabel, int row, double[] rowData)
          Sets the row values at the given row index and updates the row labels
 void set(String rowLabel, String columnLabel, double value)
          Set the value at the given index
 void set(String rowLabel, String columnLabel, int row, int column, double value)
          Set the value at the given index, updating the row and column label bindings
 void setColumnLabelBindings(Map<String,Integer> bindings)
          Sets a map of column label bindings in the receiver
 void setRowLabelBindings(Map<String,Integer> bindings)
          Sets a map of row label bindings in the receiver
 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)
 String toString()
           
 Matrix transpose()
          Return a new matrix that is the transpose of the receiver
 Vector viewColumn(int column)
          Returns a view of a row.
 Vector viewDiagonal()
          Provides a view of the diagonal of a matrix.
 Matrix viewPart(int[] offset, int[] size)
          Return a view into part of a matrix.
 Matrix viewPart(int rowOffset, int rowsRequested, int columnOffset, int columnsRequested)
          Return a view into part of a matrix.
 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, wait, wait, wait
 
Methods inherited from interface org.apache.mahout.math.Matrix
assignColumn, assignRow, getQuick, like, like, setQuick
 

Field Detail

columnLabelBindings

protected Map<String,Integer> columnLabelBindings

rowLabelBindings

protected Map<String,Integer> rowLabelBindings

rows

protected int rows

columns

protected int columns

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

protected AbstractMatrix(int rows,
                         int columns)
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.

iterator

public Iterator<MatrixSlice> iterator()
Specified by:
iterator in interface Iterable<MatrixSlice>

iterateAll

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

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(String rowLabel,
                  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 Map<String,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 Map<String,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(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(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(String rowLabel,
                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(String rowLabel,
                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(Map<String,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(Map<String,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

asFormatString

public 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,
                     DoubleDoubleFunction 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 DoubleDoubleFunction 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(DoubleFunction function)
Description copied from interface: Matrix
Apply the function to each element of the receiver

Specified by:
assign in interface Matrix
Parameters:
function - a DoubleFunction 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.

viewDiagonal

public Vector viewDiagonal()
Provides a view of the diagonal of a matrix.

Specified by:
viewDiagonal in interface Matrix
Returns:
A vector that shares storage with the original matrix.

aggregate

public double aggregate(DoubleDoubleFunction combiner,
                        DoubleFunction 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.

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.

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 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 view into part of a matrix. Changes to the view will change the original matrix.

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 matrix that shares storage with part of the original matrix.

viewPart

public Matrix viewPart(int[] offset,
                       int[] size)
Description copied from interface: Matrix
Return a view into part of a matrix. Changes to the view will change the original matrix.

Specified by:
viewPart in interface Matrix
Parameters:
offset - an int[2] offset into the receiver
size - the int[2] size of the desired result
Returns:
a matrix that shares storage with part of the original matrix.

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

getNumNondefaultElements

public int[] getNumNondefaultElements()
Description copied from interface: Matrix
Return the number of values in the recipient

Specified by:
getNumNondefaultElements in interface Matrix
Returns:
an int[2] containing [row, column] count

toString

public String toString()
Overrides:
toString in class Object


Copyright © 2008–2014 The Apache Software Foundation. All rights reserved.