|
Groovy Documentation | |||||||
FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | PROPERTY | CONSTR | METHOD | DETAIL: FIELD | PROPERTY | CONSTR | METHOD |
java.lang.ObjectExpando
Matrix
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] == 4The 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])
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 |
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 |
---|
static final int DISPLAY_ROWS
@Delegate Array2DRowRealMatrix matrix
java.util.List names
Constructor Detail |
---|
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 Detail |
---|
@CompileStatic MatrixColumn col(int n)
@CompileStatic java.lang.Object collect(groovy.lang.Closure c)
collect
: if 1 arg then
just pass the row, if 2 args, pass the row AND the index.
c Closure
- to execute for each row in the matrix
Matrix divide(double d)
@CompileStatic void eachRow(groovy.lang.Closure c)
@CompileStatic java.util.List findIndexValues(groovy.lang.Closure c)
@CompileStatic java.lang.Object getAt(java.lang.Object n)
m[4]
returns 5th row of matrix.
m[4][5]
returns 6th column of 4th row.
m[][6]
returns 7th column
Matrix m = new Matrix([1..80], 10, 8) m[2..4] == [ [ 9..16 ], [17..24], [25..32] ]
java.util.List getColumns(java.util.List names)
java.util.List getColumns()
@CompileStatic Matrix grep(groovy.lang.Closure c)
c
- a Closure to evaluate
@CompileStatic void initFromList(int rows, int columns, java.util.List data)
@CompileStatic 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)
@CompileStatic void putAt(int n, java.lang.Object values)
@CompileStatic 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)
@CompileStatic double subsetRows(java.lang.Iterable i)
java.lang.String toString()
@CompileStatic Matrix transform(groovy.lang.Closure c)
c
- A closure taking 1 or 3 arguments (data value, or data value, row,
column)
@CompileStatic Matrix transformRows(groovy.lang.Closure c)
c
- Closure to process the data with.
Matrix transpose()
java.util.List which(groovy.lang.Closure c)
Groovy Documentation