public abstract class Matrix extends Object implements Cloneable, Serializable
Matrix gramHat = gram.subtract(Matrix.eye(gram.rows()));
gram
would be the matrix A in the documentation.
Constructor and Description |
---|
Matrix() |
Modifier and Type | Method and Description |
---|---|
Matrix |
add(double c)
Creates a new Matrix that stores the result of
A+c |
Matrix |
add(double c,
ExecutorService threadPool)
Creates a new Matrix that stores the result of
A+c |
Matrix |
add(Matrix B)
Creates a new Matrix that stores the result of
A+B |
Matrix |
add(Matrix B,
ExecutorService threadPool)
Creates a new Matrix that stores the result of
A+B |
boolean |
canBeMutated()
Indicates whether or not this matrix can be mutated.
|
static boolean |
canMultiply(Matrix A,
Matrix B)
Convenience method that will return
true only if the two input
matrices have dimensions compatible for multiplying A*B |
abstract void |
changeSize(int newRows,
int newCols)
This method alters the size of a matrix, either adding or subtracting
rows from the internal structure of the matrix.
|
abstract Matrix |
clone() |
abstract int |
cols()
Returns the number of columns stored in this matrix
|
void |
copyTo(Matrix other)
Copes the values of this matrix into the other matrix of the same dimensions
|
static Matrix |
diag(Vec a)
Returns a new dense square matrix such that the main diagonal contains
the values given in a
|
static void |
diagMult(Matrix A,
Vec b)
Alters the matrix A so that it contains the result of A
times a sparse matrix represented by only its diagonal values or
A = A*diag(b).
|
static void |
diagMult(Vec b,
Matrix A)
Alters the matrix A so that it contains the result of
sparse matrix represented by only its diagonal values times A or
A = diag(b)*A.
|
boolean |
equals(Object obj) |
boolean |
equals(Object obj,
double range)
Performs the same as
equals(java.lang.Object) , but allows a
leniency in the differences between matrix values. |
static DenseMatrix |
eye(int k)
Creates a new dense identity matrix with k rows and columns.
|
abstract double |
get(int i,
int j)
Returns the value stored at at the matrix position Ai,j
|
Vec |
getColumn(int j)
Creates a vector that has a copy of the values in column j of this
matrix.
|
Vec |
getColumnView(int j)
Obtains a vector that is backed by this, at very little memory
cost.
|
Vec |
getRow(int r)
Creates a vector that has a copy of the values in row i of this
matrix.
|
Vec |
getRowView(int r)
Obtains a vector that is backed by this, at very little memory
cost.
|
void |
increment(int i,
int j,
double value)
Alters the current matrix at index (i,j) to be equal to
Ai,j = Ai,j + value
|
abstract boolean |
isSparce()
Returns
true if the matrix is sparse, false otherwise |
boolean |
isSquare()
|
static boolean |
isSymmetric(Matrix A)
Checks to see if the given input is a perfectly symmetric matrix
|
static boolean |
isSymmetric(Matrix A,
double eps)
Checks to see if the given input is approximately symmetric.
|
abstract Matrix[] |
lup() |
abstract Matrix[] |
lup(ExecutorService threadPool) |
Matrix |
multiply(double c)
Creates a new Matrix that stores A*c
|
Matrix |
multiply(double c,
ExecutorService threadPool)
Creates a new Matrix that stores A*c
|
Matrix |
multiply(Matrix B)
Creates a new matrix that stores A*B
|
Matrix |
multiply(Matrix B,
ExecutorService threadPool)
Creates a new matrix that stores A*B
|
abstract void |
multiply(Matrix B,
Matrix C)
Alters the matrix C to be equal to C = C+A*B
|
abstract void |
multiply(Matrix B,
Matrix C,
ExecutorService threadPool)
Alters the matrix C to be equal to C = C+A*B
|
Vec |
multiply(Vec b)
Creates a new vector that is equal to A*b
|
abstract void |
multiply(Vec b,
double z,
Vec c)
If this matrix is Am x n, and b has a length of n, and c has a length of m,
then this will mutate c to store c = c + A*b*z
|
Matrix |
multiplyTranspose(Matrix B)
Returns the new matrix C that is C = A*BT
|
Matrix |
multiplyTranspose(Matrix B,
ExecutorService threadPool)
Returns the new matrix C that is C = A*BT
|
abstract void |
multiplyTranspose(Matrix B,
Matrix C)
Alters the matrix C to be equal to C = C+A*BT
|
abstract void |
multiplyTranspose(Matrix B,
Matrix C,
ExecutorService threadPool)
Alters the matrix C to be equal to C = C+A*BT
|
abstract void |
mutableAdd(double c)
Alters the current matrix to store the value A+c
|
abstract void |
mutableAdd(double c,
ExecutorService threadPool)
Alters the current matrix to store the value A+c
|
abstract void |
mutableAdd(double c,
Matrix B)
Alters the current matrix to store the value A+c*B
|
abstract void |
mutableAdd(double c,
Matrix B,
ExecutorService threadPool)
Alters the current matrix to store the value A+c*B
|
void |
mutableAdd(Matrix B)
Alters the current matrix to store the value A+B
|
void |
mutableAdd(Matrix B,
ExecutorService threadpool)
Alters the current matrix to store the value A+B
|
abstract void |
mutableMultiply(double c)
Alters the current matrix to be equal to A*c
|
abstract void |
mutableMultiply(double c,
ExecutorService threadPool)
Alters the current matrix to be equal to A*c
|
void |
mutableSubtract(double c)
Alters the current matrix to store A-c
|
void |
mutableSubtract(double c,
ExecutorService threadPool)
Alters the current matrix to store A-c
|
void |
mutableSubtract(double c,
Matrix B)
Alters the current matrix to store A-c*B
|
void |
mutableSubtract(double c,
Matrix B,
ExecutorService threadPool)
Alters the current matrix to store A-c*B
|
void |
mutableSubtract(Matrix B)
Alters the current matrix to store A-B
|
void |
mutableSubtract(Matrix B,
ExecutorService threadpool)
Alters the current matrix to store A-B
|
abstract void |
mutableTranspose()
Transposes the current matrix in place, altering its value.
|
long |
nnz()
Returns the number of non zero values stored in this matrix.
|
static void |
OuterProductUpdate(Matrix A,
Vec x,
Vec y,
double c)
Alters the matrix A such that,
A = A + c * x * y'
|
static void |
OuterProductUpdate(Matrix A,
Vec x,
Vec y,
double c,
ExecutorService threadpool)
Alters the matrix A such that,
A = A + c * x * y'
|
static Matrix |
pascal(int size)
Creates a new square matrix that is a pascal matrix.
|
abstract Matrix[] |
qr() |
abstract Matrix[] |
qr(ExecutorService threadPool) |
static DenseMatrix |
random(int rows,
int cols,
Random rand)
Creates a new dense matrix filled with random values from
Random.nextDouble() |
abstract int |
rows()
Returns the number of rows stored in this matrix
|
static boolean |
sameDimensions(Matrix A,
Matrix B)
Convenience method that will return
true only if the two input
matrices have the exact same dimensions. |
abstract void |
set(int i,
int j,
double value)
Sets the value stored at at the matrix position Ai,j
|
Matrix |
subtract(double c)
Creates a new Matrix that stores the result of A-c
|
Matrix |
subtract(double c,
ExecutorService threadPool)
Creates a new Matrix that stores the result of A-c
|
Matrix |
subtract(Matrix B)
Creates a new Matrix that stores the result of A-B
|
Matrix |
subtract(Matrix B,
ExecutorService threadPool)
Creates a new Matrix that stores the result of A-B
|
abstract void |
swapRows(int r1,
int r2)
Alters the current matrix by swapping the values stored in two different
rows.
|
String |
toString() |
Matrix |
transpose()
Returns a new matrix that is the transpose of this matrix.
|
abstract void |
transpose(Matrix C)
Overwrites the values stored in matrix C to store the value of
A'
|
Vec |
transposeMultiply(double c,
Vec b)
Creates a new vector equal to x = A'*b*c
|
abstract void |
transposeMultiply(double c,
Vec b,
Vec x)
Alters the vector x to be equal to x = x + A'*b*c
|
Matrix |
transposeMultiply(Matrix B)
Creates a new matrix equal to A'*B, or the same result as
A. |
Matrix |
transposeMultiply(Matrix B,
ExecutorService threadPool)
Computes the result matrix of A'*B, or the same result as
A. |
abstract void |
transposeMultiply(Matrix B,
Matrix C)
Alters the matrix C so that C = C + A'*B
|
abstract void |
transposeMultiply(Matrix B,
Matrix C,
ExecutorService threadPool)
Alters the matrix C so that C = C + A'*B
|
void |
updateRow(int i,
double c,
Vec b)
Alters row i of this matrix, such that
A[i,:] = A[i,:] + c*b
|
abstract void |
zeroOut()
Alters the current matrix so that all values are equal to zero.
|
public Matrix add(Matrix B)
A+B
B
- the matrix to add this thisA+B
public Matrix add(Matrix B, ExecutorService threadPool)
A+B
B
- the matrix to add this thisthreadPool
- the source of threads to do computation in parallelA+B
public Matrix add(double c)
A+c
c
- the scalar to add to each value in thisA+c
public Matrix add(double c, ExecutorService threadPool)
A+c
c
- the scalar to add to each value in thisthreadPool
- the source of threads to do computation in parallelA+B
public void mutableAdd(Matrix B)
B
- the matrix to add this thispublic abstract void mutableAdd(double c, Matrix B)
c
- the scalar constant to multiple B byB
- the matrix to add to thispublic void mutableAdd(Matrix B, ExecutorService threadpool)
B
- the matrix to add to thisthreadpool
- the source of threads to do computation in parallelpublic abstract void mutableAdd(double c, Matrix B, ExecutorService threadPool)
c
- the scalar constant to multiple B byB
- the matrix to add to thisthreadPool
- the source of threads to do computation in parallelpublic abstract void mutableAdd(double c)
c
- the scalar constant to add to thispublic abstract void mutableAdd(double c, ExecutorService threadPool)
c
- the scalar constant to add to thisthreadPool
- the source of threads to do computation in parallelpublic boolean canBeMutated()
false
, any method that contains "mutate" will not work.
true
true
if the matrix supports being altered, false
other wise.public Matrix subtract(Matrix B)
B
- the matrix to subtract from this.public Matrix subtract(Matrix B, ExecutorService threadPool)
B
- the matrix to subtract from this.threadPool
- the source of threads to do computation in parallelpublic Matrix subtract(double c)
c
- the scalar constant to subtract from thispublic Matrix subtract(double c, ExecutorService threadPool)
c
- the scalar constant to subtract from thisthreadPool
- the source of threads to do computation in parallelpublic void mutableSubtract(Matrix B)
B
- the matrix to subtract from this.public void mutableSubtract(double c, Matrix B)
c
- the scalar constant to multiply B byB
- the matrix to subtract from this.public void mutableSubtract(Matrix B, ExecutorService threadpool)
B
- the matrix to subtract from this.threadpool
- the source of threads to do computation in parallelpublic void mutableSubtract(double c, Matrix B, ExecutorService threadPool)
c
- the scalar constant to multiply B byB
- the matrix to subtract from this.threadPool
- the source of threads to do computation in parallelpublic void mutableSubtract(double c)
c
- the scalar constant to subtract from thispublic void mutableSubtract(double c, ExecutorService threadPool)
c
- the scalar constant to subtract from thisthreadPool
- the source of threads to do computation in parallelpublic abstract void multiply(Vec b, double z, Vec c)
b
- the vector to be treated as a colum vectorz
- the constant to multiply the A*b value by.c
- where to place the result by additionArithmeticException
- if the dimensions of A, b, or c do not all agreepublic Vec multiply(Vec b)
b
- the vector to multiply bypublic Matrix multiply(Matrix B)
B
- the matrix to multiply bypublic Matrix multiply(Matrix B, ExecutorService threadPool)
B
- the matrix to multiply bythreadPool
- the source of threads to do computation in parallelpublic abstract void multiply(Matrix B, Matrix C)
B
- the matrix to multiply this withC
- the matrix to add the result topublic abstract void multiply(Matrix B, Matrix C, ExecutorService threadPool)
B
- the matrix to multiply this withC
- the matrix to add the result tothreadPool
- the source of threads to do computation in parallelpublic abstract void multiplyTranspose(Matrix B, Matrix C)
B
- the matrix to multiply this withC
- the matrix to add the result topublic Matrix multiplyTranspose(Matrix B)
B
- the matrix to multiply by the transpose ofpublic abstract void multiplyTranspose(Matrix B, Matrix C, ExecutorService threadPool)
B
- the matrix to multiply this withC
- the matrix to add the result tothreadPool
- the source of threads to do computation in parallelpublic Matrix multiplyTranspose(Matrix B, ExecutorService threadPool)
B
- the matrix to multiply by the transpose ofthreadPool
- the source of threads to do computation in parallelpublic Matrix multiply(double c)
c
- the scalar constant to multiply bypublic Matrix multiply(double c, ExecutorService threadPool)
c
- the scalar constant to multiply bythreadPool
- the source of threads to do computation in parallelpublic abstract void mutableMultiply(double c)
c
- the scalar constant to multiply bypublic abstract void mutableMultiply(double c, ExecutorService threadPool)
c
- the scalar constant to multiply bythreadPool
- the source of threads to do computation in parallelpublic abstract Matrix[] lup()
public abstract Matrix[] lup(ExecutorService threadPool)
public abstract Matrix[] qr()
public abstract Matrix[] qr(ExecutorService threadPool)
public abstract void changeSize(int newRows, int newCols)
newRows
- the new number of rows, must be positivenewCols
- the new number of columns, must be positive.public abstract void mutableTranspose()
public Matrix transpose()
public abstract void transpose(Matrix C)
C
- the matrix to store the transpose of the current matrixArithmeticException
- if the dimensions of C do not match
the dimensions of this'public Matrix transposeMultiply(Matrix B)
A.transpose()
.multiply(B)
B
- the other Matrixpublic abstract void transposeMultiply(Matrix B, Matrix C)
B
- the matrix to multiply byC
- the matrix to add the result topublic Matrix transposeMultiply(Matrix B, ExecutorService threadPool)
A.transpose()
.multiply(B)
B
- the matrix to multiply bythreadPool
- the source of threads to do computation in parallelpublic abstract void transposeMultiply(Matrix B, Matrix C, ExecutorService threadPool)
B
- the matrix to multiply byC
- the matrix to place the results inthreadPool
- the source of threads to do computation in parallelpublic abstract void transposeMultiply(double c, Vec b, Vec x)
c
- the scalar constant to multiply byb
- the vector to multiply byx
- the vector the add the result topublic Vec transposeMultiply(double c, Vec b)
c
- the scalar constant to multiply byb
- the vector to multiply bypublic abstract double get(int i, int j)
i
- the row, starting from 0j
- the column, starting from 0public abstract void set(int i, int j, double value)
i
- the row, starting from 0j
- the column, starting from 0value
- the value to place at Ai,jpublic void increment(int i, int j, double value)
i
- the row, starting from 0j
- the column, starting from 0value
- the value to add to the matrix coordinatepublic abstract int rows()
public abstract int cols()
public abstract boolean isSparce()
true
if the matrix is sparse, false
otherwisetrue
if the matrix is sparse, false
otherwisepublic long nnz()
public boolean isSquare()
true
if this matrix is square, false
if it is
rectangular.public abstract void swapRows(int r1, int r2)
r1
- the first row to swapr2
- the second row to swappublic Vec getColumn(int j)
j
- the column to copyVec
public Vec getColumnView(int j)
j
- the column to obtain a view ofpublic Vec getRow(int r)
r
- the row to copyVec
public Vec getRowView(int r)
r
- the row to obtain a view ofpublic static boolean sameDimensions(Matrix A, Matrix B)
true
only if the two input
matrices have the exact same dimensions.A
- the first matrixB
- the second matrixtrue
if they have the exact same dimensions,
false
otherwise.public static boolean canMultiply(Matrix A, Matrix B)
true
only if the two input
matrices have dimensions compatible for multiplying A*BA
- the first matrixB
- the second matrixtrue
if they have dimensions allowing multiplication,
false
otherwise.public boolean equals(Object obj, double range)
equals(java.lang.Object)
, but allows a
leniency in the differences between matrix values. This is useful for
when some amount of numerical error is expectedobj
- the other matrixrange
- the max acceptable difference between two cell valuestrue
if the difference between the values of each pair of
matrix elements are less than or equal to rangepublic abstract void zeroOut()
public void copyTo(Matrix other)
other
- the matrix to overwrite the values ofpublic void updateRow(int i, double c, Vec b)
i
- the index of the row to updatec
- the scalar constant to multiply the vector byb
- the vector to add to the specified rowpublic static void OuterProductUpdate(Matrix A, Vec x, Vec y, double c)
A
- the matrix to updatex
- the first vectory
- the second vectorc
- the scalar constant to multiply the outer product byArithmeticException
- if the vector dimensions are not compatible
with the matrix Apublic static void OuterProductUpdate(Matrix A, Vec x, Vec y, double c, ExecutorService threadpool)
A
- the matrix to updatex
- the first vectory
- the second vectorc
- the scalar constant to multiply the outer product bythreadpool
- the source of threads to do computation in parallelpublic static DenseMatrix eye(int k)
k
- the number of rows / columnspublic static DenseMatrix random(int rows, int cols, Random rand)
Random.nextDouble()
rows
- the number of rows for the matrixcols
- the number of columns for the matrixrand
- the source of randomnesspublic static Matrix diag(Vec a)
a
- the diagonal values of a matrixpublic static void diagMult(Matrix A, Vec b)
A = A.multiply
(diag
(b))
A
- the square matrix to updateb
- the diagonal value vectorpublic static void diagMult(Vec b, Matrix A)
b.multiply
(diag
(A))
b
- the diagonal value vectorA
- the square matrix to updatepublic static boolean isSymmetric(Matrix A, double eps)
A
- the input matrixeps
- the maximum tolerable difference between two entriestrue
if the matrix is approximately symmetricpublic static boolean isSymmetric(Matrix A)
A
- the input matrixtrue
if it is perfectly symmetric.public static Matrix pascal(int size)
size
- the number of rows and columns for the matrixCopyright © 2017. All rights reserved.