Package me.yixqiao.jlearn.matrix
Class Matrix
- java.lang.Object
-
- me.yixqiao.jlearn.matrix.Matrix
-
- All Implemented Interfaces:
java.io.Serializable
public class Matrix extends java.lang.Object implements java.io.Serializable
Matrix class and operations.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Matrix.Init
Initialization methods for Matrix.
-
Constructor Summary
Constructors Constructor Description Matrix(double[][] mat)
Create a matrix from an existing 2d array of doubles.Matrix(int rows, int cols)
Creates a new, empty matrix.Matrix(int rows, int cols, Matrix.Init init)
Create a new matrix using anMatrix.Init
object.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description Matrix
add(double x)
Add a scalar to all elements.Matrix
add(Matrix m2)
Add another matrix element by element.void
addIP(double x)
Add a scalar to all elements, in place.void
addIP(Matrix m2)
Add another matrix element by element, in place.Matrix
applyEach(java.util.function.ToDoubleFunction<java.lang.Double> function)
Apply a function to each element.Matrix
applyEach(java.util.function.ToDoubleFunction<java.lang.Double> function, boolean useThreading)
Apply a function to each element.void
applyEachIP(java.util.function.ToDoubleFunction<java.lang.Double> function)
Apply a function to each element, in place.void
applyEachIP(java.util.function.ToDoubleFunction<java.lang.Double> function, boolean useThreading)
Apply a function to each element, in place.Matrix
cloneMatrix()
Create a clone of the matrix.void
crossOver(Matrix m2, double weightSelf)
Deprecated.As of v0.2.0, this method has no use.void
divideIP(Matrix m2)
Divide by another matrix element by element, in place.Matrix
dot(Matrix m2)
Calculate the dot product.Matrix
dot(Matrix m2, boolean useThreading)
Calculate the dot product.double
getMaxValue()
Get the greatest value in the matrix.double
getSum()
Get the sum of the elements.Matrix
getTranspose()
Get the matrix transpose.Matrix
multiply(double x)
Multiply each matrix element by a scalar.Matrix
multiply(Matrix m2)
Multiply by another matrix element by element.void
multiplyIP(double x)
Multiply each matrix element by a scalar, in place.void
multiplyIP(Matrix m2)
Multiply by another matrix element by element, in place.void
printMatrix()
Print out the matrix to stdout.void
readFromFile(java.io.DataInputStream dis)
Read the matrix from a file.Matrix
subtract(double x)
Subtract a scalar from all elements.Matrix
subtract(Matrix m2)
Subtract another matrix element by element.void
writeToFile(java.io.DataOutputStream dos)
Write the matrix to a file.
-
-
-
Constructor Detail
-
Matrix
public Matrix(int rows, int cols, Matrix.Init init)
Create a new matrix using anMatrix.Init
object.- Parameters:
rows
- number of rowscols
- number of columnsinit
- instance of init object
-
Matrix
public Matrix(int rows, int cols)
Creates a new, empty matrix.- Parameters:
rows
- number of rowscols
- number of columns
-
Matrix
public Matrix(double[][] mat)
Create a matrix from an existing 2d array of doubles.Note that this constructor does not create a copy of the array passed in.
- Parameters:
mat
- the array to make the matrix with
-
-
Method Detail
-
dot
public Matrix dot(Matrix m2)
Calculate the dot product.- Parameters:
m2
- the other matrix- Returns:
- the result
-
dot
public Matrix dot(Matrix m2, boolean useThreading)
Calculate the dot product.- Parameters:
m2
- the other matrixuseThreading
- whether to use threading- Returns:
- the result
-
add
public Matrix add(double x)
Add a scalar to all elements.- Parameters:
x
- number to add- Returns:
- new matrix
-
addIP
public void addIP(double x)
Add a scalar to all elements, in place.- Parameters:
x
- number to add
-
add
public Matrix add(Matrix m2)
Add another matrix element by element.- Parameters:
m2
- other matrix with same dimensions- Returns:
- new matrix
-
addIP
public void addIP(Matrix m2)
Add another matrix element by element, in place.- Parameters:
m2
- other matrix with same dimensions
-
subtract
public Matrix subtract(double x)
Subtract a scalar from all elements.- Parameters:
x
- number to subtract- Returns:
- new matrix
-
subtract
public Matrix subtract(Matrix m2)
Subtract another matrix element by element.- Parameters:
m2
- other matrix with the same dimensions- Returns:
- the resulting matrix
-
multiply
public Matrix multiply(double x)
Multiply each matrix element by a scalar.- Parameters:
x
- the number to multiply by- Returns:
- the resulting matrix
-
multiplyIP
public void multiplyIP(double x)
Multiply each matrix element by a scalar, in place.- Parameters:
x
- the number to multiply by
-
multiply
public Matrix multiply(Matrix m2)
Multiply by another matrix element by element.- Parameters:
m2
- other matrix with the same dimensions- Returns:
- the resulting matrix
-
multiplyIP
public void multiplyIP(Matrix m2)
Multiply by another matrix element by element, in place.- Parameters:
m2
- other matrix with the same dimensions
-
divideIP
public void divideIP(Matrix m2)
Divide by another matrix element by element, in place.- Parameters:
m2
- other matrix
-
applyEach
public Matrix applyEach(java.util.function.ToDoubleFunction<java.lang.Double> function)
Apply a function to each element.- Parameters:
function
- the function to apply- Returns:
- the resulting matrix
-
applyEach
public Matrix applyEach(java.util.function.ToDoubleFunction<java.lang.Double> function, boolean useThreading)
Apply a function to each element.- Parameters:
function
- the function to applyuseThreading
- whether to use threading- Returns:
- the resulting matrix
-
applyEachIP
public void applyEachIP(java.util.function.ToDoubleFunction<java.lang.Double> function)
Apply a function to each element, in place.- Parameters:
function
- the function to apply
-
applyEachIP
public void applyEachIP(java.util.function.ToDoubleFunction<java.lang.Double> function, boolean useThreading)
Apply a function to each element, in place.- Parameters:
function
- the function to applyuseThreading
- whether to use threading
-
getSum
public double getSum()
Get the sum of the elements.- Returns:
- the sum
-
getMaxValue
public double getMaxValue()
Get the greatest value in the matrix.- Returns:
- the max value
-
getTranspose
public Matrix getTranspose()
Get the matrix transpose.- Returns:
- the transposed matrix
-
writeToFile
public void writeToFile(java.io.DataOutputStream dos)
Write the matrix to a file.- Parameters:
dos
- stream to write to
-
readFromFile
public void readFromFile(java.io.DataInputStream dis)
Read the matrix from a file.- Parameters:
dis
- stream to read from
-
crossOver
@Deprecated public void crossOver(Matrix m2, double weightSelf)
Deprecated.As of v0.2.0, this method has no use.Cross over with another matrix.- Parameters:
m2
- other matrixweightSelf
- weight given to itself
-
printMatrix
public void printMatrix()
Print out the matrix to stdout.
-
cloneMatrix
public Matrix cloneMatrix()
Create a clone of the matrix.- Returns:
- the clone
-
-