Groovy Documentation

[Groovy] Class Matrix

java.lang.Object
  Expando
      Matrix
All Implemented Interfaces:
java.lang.Iterable

class Matrix
extends Expando

Wraps an Apache-Commons-Math matrix of double values with a Groovy interface for convenient access. Because it wraps the underlying matrix as a delegate, all the original methods of the Commons-Math implementation are available directly, along with Groovy-enhanced versions.

The most basic enhancements come in the form of random access operators that allowthe Matrix class to be referenced using square-bracket notation:

 Matrix m = new Matrix(2,2,[1,2,3,4])
 assert m[0][0] == 2
 assert m[1][1] == 4
 
The rows of the Matrix are directly accessible simply by using square-bracket indexing:
 assert m[0] == [1,2]
 assert m[1] == [3,4]
 
The columns are accessed by using an empty first index:
 assert m[][0] == [1,3]
 assert m[][1] == [2,4]
 
Rows and columns can both be treated as normal Groovy collections:
 assert m[0].collect { it.any { it > 2 }  } == [ false, true ]
 assert m[][0].collect { it > 1 } == [ false, true ]
 
Note that in the above code, both row-wise and column-wise access occurs without copying any data.

Transforming the whole matrix can be done using transform:

 assert m.transform { it * 2 } == Matrix(2,2,[2,4,6,8])
 
As an option, row and column indexes are available as well:
 assert m.transform { value, row, column -> value * 2 } == Matrix(2,2,[2,4,6,8])
 
Authors:
simon.sadedin@mcri.edu.au


Property Summary
static int DISPLAY_ROWS

How many rows are displayed in toString() and other calls that format output

Array2DRowRealMatrix matrix

java.util.List names

 
Constructor Summary
Matrix(int rows, int columns)

Matrix(MatrixColumn... sourceColumns)

Matrix(java.lang.Iterable rows)

Matrix(double values)

Matrix(int rows, int columns, java.util.List data)

Matrix(int rows, int columns, double[] matrixData)

Matrix(Array2DRowRealMatrix m)

 
Method Summary
MatrixColumn col(int n)

java.lang.Object collect(groovy.lang.Closure c)

Specialization of collect: if 1 arg then just pass the row, if 2 args, pass the row AND the index.

Matrix divide(double d)

void eachRow(groovy.lang.Closure c)

java.util.List findIndexValues(groovy.lang.Closure c)

java.lang.Object getAt(java.lang.Object n)

Implementation of the [] operator.

java.util.List getColumns(java.util.List names)

java.util.List getColumns()

Matrix grep(groovy.lang.Closure c)

Filter the rows of this matrix and return a Matrix as a result

void initFromList(int rows, int columns, java.util.List data)

java.util.Iterator iterator()

static Matrix load(java.lang.String fileName)

Matrix minus(Matrix m)

Matrix minus(double x)

Matrix multiply(double d)

Matrix multiply(Matrix m)

Matrix plus(Matrix m)

Matrix plus(double x)

void putAt(int n, java.lang.Object values)

double[] row(int n)

void save(java.lang.String fileName)

void save(Writer w)

void setColumnNames(java.util.List names)

void setNames(java.util.List names)

double subsetRows(java.lang.Iterable i)

Return a subset of the rows indicated by the indices in the given iterable (Note that the indices don't need to be consecutive or monotonic).

java.lang.String toString()

Matrix transform(groovy.lang.Closure c)

Transforms a matrix by processing each element through the given closure.

Matrix transformRows(groovy.lang.Closure c)

Transform the given matrix by passing each row to the given closure.

Matrix transpose()

java.util.List which(groovy.lang.Closure c)

Shorthand to give a familiar function to R users

 

Property Detail

DISPLAY_ROWS

static final int DISPLAY_ROWS
How many rows are displayed in toString() and other calls that format output


matrix

@Delegate
Array2DRowRealMatrix matrix


names

java.util.List names


 
Constructor Detail

Matrix

Matrix(int rows, int columns)


Matrix

Matrix(MatrixColumn... sourceColumns)


Matrix

Matrix(java.lang.Iterable rows)


Matrix

Matrix(double values)


Matrix

Matrix(int rows, int columns, java.util.List data)


Matrix

Matrix(int rows, int columns, double[] matrixData)


Matrix

Matrix(Array2DRowRealMatrix m)


 
Method Detail

col

@CompileStatic
MatrixColumn col(int n)


collect

@CompileStatic
java.lang.Object collect(groovy.lang.Closure c)
Specialization of collect: if 1 arg then just pass the row, if 2 args, pass the row AND the index.
Parameters:
c Closure - to execute for each row in the matrix
Returns:
results collected


divide

Matrix divide(double d)


eachRow

@CompileStatic
void eachRow(groovy.lang.Closure c)


findIndexValues

@CompileStatic
java.util.List findIndexValues(groovy.lang.Closure c)


getAt

@CompileStatic
java.lang.Object getAt(java.lang.Object n)
Implementation of the [] operator. Adds several different behaviors:
 Matrix m = new Matrix([1..80], 10, 8)
 m[2..4] == [ [ 9..16 ], [17..24], [25..32] ] 
Parameters:
n
Returns:


getColumns

java.util.List getColumns(java.util.List names)


getColumns

java.util.List getColumns()


grep

@CompileStatic
Matrix grep(groovy.lang.Closure c)
Filter the rows of this matrix and return a Matrix as a result
Parameters:
c - a Closure to evaluate
Returns:
Matrix for which the closure c returns a non-false value


initFromList

@CompileStatic
void initFromList(int rows, int columns, java.util.List data)


iterator

@CompileStatic
java.util.Iterator iterator()


load

static Matrix load(java.lang.String fileName)


minus

Matrix minus(Matrix m)


minus

Matrix minus(double x)


multiply

Matrix multiply(double d)


multiply

Matrix multiply(Matrix m)


plus

Matrix plus(Matrix m)


plus

Matrix plus(double x)


putAt

@CompileStatic
void putAt(int n, java.lang.Object values)


row

@CompileStatic
double[] row(int n)


save

void save(java.lang.String fileName)


save

void save(Writer w)


setColumnNames

void setColumnNames(java.util.List names)


setNames

void setNames(java.util.List names)


subsetRows

@CompileStatic
double subsetRows(java.lang.Iterable i)
Return a subset of the rows indicated by the indices in the given iterable (Note that the indices don't need to be consecutive or monotonic).
Parameters:
i
Returns:


toString

java.lang.String toString()


transform

@CompileStatic
Matrix transform(groovy.lang.Closure c)
Transforms a matrix by processing each element through the given closure. The closure must take either one argument or three arguments. The one argument version is only passed data values, while the three argument version is passed the data value and also the row and column position.
Parameters:
c - A closure taking 1 or 3 arguments (data value, or data value, row, column)
Returns:
A matrix reflecting the transformed data values


transformRows

@CompileStatic
Matrix transformRows(groovy.lang.Closure c)
Transform the given matrix by passing each row to the given closure. If the closure accepts two arguments then the row index is passed as well. The closure must return a double array to replace the array passed in. If null is returned then the data is left unchanged.
Parameters:
c - Closure to process the data with.
Returns:
transformed Matrix


transpose

Matrix transpose()


which

java.util.List which(groovy.lang.Closure c)
Shorthand to give a familiar function to R users


 

Groovy Documentation