Class Matrix
- java.lang.Object
-
- libai.common.matrix.Matrix
-
- All Implemented Interfaces:
java.io.Serializable
public class Matrix extends java.lang.Object implements java.io.Serializable
Matrix implementation. This class can handle the basic operations of matrix space, add, subtract, product scalar product, transpose, and other useful operations for this API. For performance reasons, precondition validations are done using asserts instead of the more clear and safe exceptions.- See Also:
- Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(Matrix a, Matrix b)
Adds two matrices.void
apply(Function f, Matrix a)
Apply one function over the elements of the matrix and left the result on a.void
applyInIdentity(Function f, Matrix a)
Apply one function over the elements of the matrix and left on the main diagonal of a identity matrix 'a'.void
copy(Matrix a)
Copy this matrix to another matrix a.double
dotProduct(Matrix a)
Calculate the dot product between this matrix and another.boolean
equals(java.lang.Object b1)
Check if two matrix are equals position to position with a precision of 1e-7.void
fill()
Fill the matrix with random values between (-1, 1).void
fill(boolean signed)
Fill the matrix with random values between [0, 1) ifsigned
isis false
, and (-1, 1) iftrue
.void
fill(boolean signed, java.util.Random r)
Fill the matrix with random values between [0, 1) ifsigned
isis false
, and (-1, 1) iftrue
.double[]
getCol(int index)
Return an array with the values of the specified column.int
getColumns()
static java.util.Random
getDefaultRandom()
double[]
getRow(int index)
Return an array with the values of the specified row.int
getRows()
int
hashCode()
void
increment(int i, int j, double v)
Increments the value of one position by v.void
multiply(double a, Matrix b)
Multiply this matrix by an scalar.void
multiply(Matrix a, Matrix b)
Multiply two matrix.void
multiplyAndAdd(double a, Matrix b, Matrix result)
Return in result the value of (this*a + b) for each This(i,j)double
position(int i, int j)
Return the value on the position (i,j).void
position(int i, int j, double v)
Set the value v on the position (i,j).static Matrix
random(int r, int c)
Create a new Matrix filled with low random numbers.static Matrix
random(int r, int c, boolean signed)
Create a new Matrix filled with low random numbers.static Matrix
random(int r, int c, boolean signed, java.util.Random rand)
Create a new Matrix filled with low random numbers.void
scale(int i, int j, double v)
Scales the value of one position by v.void
setRow(int index, double[] data)
Replace one row of values of this matrix.void
setValue(double v)
Set a value on each position of the matrix.Column
solve(Column rhs)
Return the reduced echelon form of this matrix using the argument as a right hand side colum.void
subtract(Matrix a, Matrix b)
Subtract two matrices.void
subtractAndCopy(Matrix b, Matrix resultSubtract, Matrix resultCopy)
Subtract the value of this with the value of b, and let the result on resultSubstract Also, copy the original value of this into resultCopy.void
swap(int i1, int i2)
Swaps two rows.java.lang.String
toString()
Return the string representation of this matrix.java.lang.String
toString(int precision)
Matrix
transpose()
Return the transpose of this matrix.void
transpose(Matrix a)
Transpose this matrix and left the result on a.
-
-
-
Constructor Detail
-
Matrix
public Matrix(int r, int c, boolean identity)
Constructor. Allocated the matrix and could initialize with the identity. By default, the matrix is created filled with zeroes.- Parameters:
r
- number of rowsc
- number of columnsidentity
- if you need initialized with an identity.- Throws:
java.lang.IllegalArgumentException
- if eitherr
orc
are less or equal than zero.
-
Matrix
public Matrix(int r, int c)
Constructor alias for Matrix(r,c,false).- Parameters:
r
- number of rowsc
- number of columns- Throws:
java.lang.IllegalArgumentException
- if eitherr
orc
are less or equal than zero.
-
Matrix
public Matrix(int r, int c, double[] data)
Constructor.Creates a matrix and initialize with the data on
data
.The values are read as row based: data -> row1, row2, ..., rown
- Parameters:
r
- number of rowsc
- number of columnsdata
- values to initialize the matrix (length must ber * c
).- Throws:
java.lang.IllegalArgumentException
- if eitherr
orc
are less or equal than zero or ifdata
isnull
or has the wrong dimension.
-
Matrix
public Matrix(Matrix copy)
Copy constructor. Creates a copy of a matrix using another matrix. Constructor alias for Matrix(copy.rows, copy.columns, copy.matrix).- Parameters:
copy
- Source of the copy
-
-
Method Detail
-
getDefaultRandom
public static java.util.Random getDefaultRandom()
-
random
public static Matrix random(int r, int c, boolean signed)
Create a new Matrix filled with low random numbers.- Parameters:
r
- number of rowsc
- number of columnssigned
-true
if the matrix should be filled with positive and negative numbersfalse
if the numbers should be greater or equal than zero.- Returns:
- a new matrix filled with low random numbers.
- Throws:
java.lang.IllegalArgumentException
- if eitherr
orc
are less or equal than zero.- See Also:
fill(boolean, java.util.Random)
-
random
public static Matrix random(int r, int c, boolean signed, java.util.Random rand)
Create a new Matrix filled with low random numbers.- Parameters:
r
- number of rowsc
- number of columnssigned
-true
if the matrix should be filled with positive and negative numbersfalse
if the numbers should be greater or equal than zero.rand
- TheRandom
object used to fill the matrix, ifnull
it will fallback toThreadLocalRandom.current()
- Returns:
- a new matrix filled with low random numbers.
- Throws:
java.lang.IllegalArgumentException
- if eitherr
orc
are less or equal than zero.- See Also:
fill(boolean, java.util.Random)
-
random
public static Matrix random(int r, int c)
Create a new Matrix filled with low random numbers.- Parameters:
r
- number of rowsc
- number of columns- Returns:
- a new matrix filled with low random numbers.
- Throws:
java.lang.IllegalArgumentException
- if eitherr
orc
are less or equal than zero.- See Also:
random(int, int, boolean)
,random(int, int, boolean, java.util.Random)
-
add
public void add(Matrix a, Matrix b)
Adds two matrices.b = this + a
The matrix
b
must be created and has the same dimension ofthis
anda
.NOTE: Assertions of the dimensions are made with
assert
statement. You must enable this on runtime to be effective.- Parameters:
a
- The matrix to addb
- The matrix to put the result
-
subtract
public void subtract(Matrix a, Matrix b)
Subtract two matrices.b = this - a
.The matrix
b
must be created and has the same dimension ofthis
anda
.NOTE: Assertions of the dimensions are made with
assert
statement. You must enable this on runtime to be effective.- Parameters:
a
- The matrix to subtractb
- The matrix to put the result
-
multiply
public void multiply(double a, Matrix b)
Multiply this matrix by an scalar.b = this * a
.The matrix
b
must be created and has the same dimension ofthis
.NOTE: Assertions of the dimensions are made with
assert
statement. You must enable this on runtime to be effective.- Parameters:
a
- The scalar to multiplyb
- The matrix to put the result
-
multiply
public void multiply(Matrix a, Matrix b)
Multiply two matrix.b = this * a
The matrix
a
must be created and has the right dimensions, that isa.rows = this.columns
.The matrix
b
must be created and has the right dimensions, that isb.rows = this.rows
andb.columns = a.columns
.NOTE: Assertions of the dimensions are made with
assert
statement. You must enable this on runtime to be effective.- Parameters:
a
- The matrix to multiplyb
- The matrix to put the result
-
apply
public void apply(Function f, Matrix a)
Apply one function over the elements of the matrix and left the result on a. For each element on thisa(i,j) = F(this(i,j))
.The matrix
a
must have the same dimension asthis
.NOTE: Assertions of the dimensions are made with
assert
statement. You must enable this on runtime to be effective.- Parameters:
f
- function to apply.a
- The matrix to put the result.
-
applyInIdentity
public void applyInIdentity(Function f, Matrix a)
Apply one function over the elements of the matrix and left on the main diagonal of a identity matrix 'a'. For each element on this a(i,i) = F(this(i,j)). The matrix this must be a column matrix. The matrix a must be created and has the same dimension of this and a. NOTE: Assertions of the dimensions are made with assert statement. You must enable this on runtime to be effective.- Parameters:
f
- function to apply.a
- The matrix to put the result.
-
fill
public void fill()
Fill the matrix with random values between (-1, 1). Alias forfill(true)
.- See Also:
fill(boolean)
,fill(boolean, java.util.Random)
-
fill
public void fill(boolean signed)
Fill the matrix with random values between [0, 1) ifsigned
isis false
, and (-1, 1) iftrue
.- Parameters:
signed
-false
if all the numbers should be positive,false
otherwise- See Also:
fill(boolean, java.util.Random)
-
fill
public void fill(boolean signed, java.util.Random r)
Fill the matrix with random values between [0, 1) ifsigned
isis false
, and (-1, 1) iftrue
.This method is based only in
Random.nextDouble()
, so in case other intervals are needed the only thing that's needed is a custom implementation ofnextDouble()
, for instance:Random myRand = new Random(){ public double nextDouble() { return super.nextDouble() / 1000.; } }
- Parameters:
signed
-false
if all the numbers should be positive,false
otherwiser
- TheRandom
object used to fill the matrix, ifnull
it will fallback toThreadLocalRandom.current()
-
copy
public void copy(Matrix a)
Copy this matrix to another matrix a.The matrix
a
must have the same dimension asthis
.NOTE: Assertions of the dimensions are made with
assert
statement. You must enable this on runtime to be effective.- Parameters:
a
- The matrix to put the result.
-
dotProduct
public double dotProduct(Matrix a)
Calculate the dot product between this matrix and another. This method can safely calculate the dot product between row-column, row-row, column-column matrices.- Parameters:
a
- The matrix to multiply- Returns:
- the scalar of the dot product.
- Throws:
java.lang.IllegalArgumentException
- if eitherthis
ora
are neither row nor column matrices.
-
transpose
public void transpose(Matrix a)
Transpose this matrix and left the result on a.The matrix a must be created and has the right dimensions.
NOTE: Assertions of the dimensions are made with
assert
statement. You must enable this on runtime to be effective.- Parameters:
a
- The matrix to put the result.
-
transpose
public Matrix transpose()
Return the transpose of this matrix.- Returns:
- A new matrix with the transpose of
this
.
-
setRow
public void setRow(int index, double[] data)
Replace one row of values of this matrix.The number of values of
data
must match with the number of columns ofthis
.NOTE: Assertions of the dimensions and indexes are made with
assert
statement. You must enable this on runtime to be effective.- Parameters:
index
- The index of the row to place the values.data
- The values to put in that row.
-
setValue
public void setValue(double v)
Set a value on each position of the matrix.- Parameters:
v
- The value to put in the matrix.
-
getRow
public double[] getRow(int index)
Return an array with the values of the specified row.NOTE: Assertions of the indexes are made with
assert
statement. You must enable this on runtime to be effective.- Parameters:
index
- The index of the row to return.- Returns:
- the array with the values.
-
getCol
public double[] getCol(int index)
Return an array with the values of the specified column.NOTE: Assertions of the indexes are made with
assert
statement. You must enable this on runtime to be effective.- Parameters:
index
- The index of the column to return.- Returns:
- the array with the values.
-
position
public final double position(int i, int j)
Return the value on the position (i,j).NOTE: Assertions of the indexes are made with
assert
statement. You must enable this on runtime to be effective.- Parameters:
i
- index of the rowj
- index of the column- Returns:
- the value of that position
-
position
public final void position(int i, int j, double v)
Set the value v on the position (i,j).NOTE: Assertions of the indexes are made with
assert
statement. You must enable this on runtime to be effective.- Parameters:
i
- index of the rowj
- index of the columnv
- the value to put into.
-
subtractAndCopy
public void subtractAndCopy(Matrix b, Matrix resultSubtract, Matrix resultCopy)
Subtract the value of this with the value of b, and let the result on resultSubstract Also, copy the original value of this into resultCopy.- Parameters:
b
- Matrix to subtractresultSubtract
- Matrix to hold the result of the subtractionresultCopy
- Matrix to hold the copy of this.
-
multiplyAndAdd
public void multiplyAndAdd(double a, Matrix b, Matrix result)
Return in result the value of (this*a + b) for each This(i,j)- Parameters:
a
- constant to multiplyb
- matrix to addresult
- Matrix to hold the result of the operation.
-
increment
public final void increment(int i, int j, double v)
Increments the value of one position by v.NOTE: Assertions of the indexes are made with
assert
statement. You must enable this on runtime to be effective.- Parameters:
i
- index of the rowj
- index of the columnv
- value to increment.
-
scale
public final void scale(int i, int j, double v)
Scales the value of one position by v.NOTE: Assertions of the indexes are made with
assert
statement. You must enable this on runtime to be effective.- Parameters:
i
- index of the rowj
- index of the columnv
- value to scale.
-
swap
public void swap(int i1, int i2)
Swaps two rows.NOTE: Assertions of the indexes are made with
assert
statement. You must enable this on runtime to be effective.- Parameters:
i1
- index of the first rowi2
- index of the second row
-
solve
public Column solve(Column rhs)
Return the reduced echelon form of this matrix using the argument as a right hand side colum.- Parameters:
rhs
- Column to use as right hand side- Returns:
rref([this rhs])(:,end
)- Since:
- 1.2.0-RIDDLER
-
equals
public boolean equals(java.lang.Object b1)
Check if two matrix are equals position to position with a precision of 1e-7. If the dimensions mismatch they aren't equals. If one position differs by more than 1e-7 then are different.- Overrides:
equals
in classjava.lang.Object
- Parameters:
b1
- The matrix to compare- Returns:
true
if are equals,false
otherwise.
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
toString
public java.lang.String toString()
Return the string representation of this matrix. Useful to write on a file or for debugging.- Overrides:
toString
in classjava.lang.Object
- Returns:
- An string with the values of the matrix.
-
toString
public java.lang.String toString(int precision)
-
getRows
public int getRows()
- Returns:
- The number of rows
-
getColumns
public int getColumns()
- Returns:
- The number of columns
-
-