org.encog.mathutil.matrices
Class Matrix
- java.lang.Object
-
- org.encog.mathutil.matrices.Matrix
-
- All Implemented Interfaces:
- java.io.Serializable, java.lang.Cloneable
public class Matrix extends java.lang.Object implements java.lang.Cloneable, java.io.SerializableThis class implements a mathematical matrix. Matrix math is very important to neural network processing. Many of the neural network classes make use of the matrix classes in this package.- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor and Description Matrix(boolean[][] sourceMatrix)Construct a bipolar matrix from an array of booleans.Matrix(double[][] sourceMatrix)Create a matrix from an array of doubles.Matrix(int rows, int cols)Create a blank array with the specified number of rows and columns.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method and Description voidadd(int row, int col, double value)Add a value to one cell in the matrix.voidadd(Matrix theMatrix)Add the specified matrix to this matrix.voidclear()Set all rows and columns to zero.Matrixclone()Create a copy of the matrix.static MatrixcreateColumnMatrix(double[] input)Turn an array of doubles into a column matrix.static MatrixcreateRowMatrix(double[] input)Turn an array of doubles into a row matrix.booleanequals(Matrix theMatrix, int precision)Compare to matrixes with the specified level of precision.booleanequals(java.lang.Object other)Check to see if this matrix equals another, using default precision.intfromPackedArray(double[] array, int index)Create a matrix from a packed array.doubleget(int row, int col)Read the specified cell in the matrix.double[][]getArrayCopy()MatrixgetCol(int col)Read one entire column from the matrix as a sub-matrix.intgetCols()Get the columns in the matrix.double[][]getData()MatrixgetMatrix(int[] r, int[] c)Get a submatrix.MatrixgetMatrix(int[] r, int j0, int j1)Get a submatrix.MatrixgetMatrix(int i0, int i1, int[] c)Get a submatrix.MatrixgetMatrix(int i0, int i1, int j0, int j1)Get a submatrix.MatrixgetRow(int row)Get the specified row as a sub-matrix.intgetRows()Get the number of rows in the matrix.inthashCode()Compute a hash code for this matrix.Matrixinverse()booleanisSquare()booleanisVector()Determine if the matrix is a vector.booleanisZero()Return true if every value in the matrix is zero.voidmultiply(double value)Multiply every value in the matrix by the specified value.voidmultiply(double[] vector, double[] result)Multiply every row by the specified vector.voidrandomize(double min, double max)Randomize the matrix.voidset(double value)Set every value in the matrix to the specified value.voidset(int row, int col, double value)Set an individual cell in the matrix to the specified value.voidset(Matrix theMatrix)Set this matrix's values to that of another matrix.voidsetMatrix(int[] r, int[] c, Matrix x)Set a submatrix.voidsetMatrix(int[] r, int j0, int j1, Matrix x)Set a submatrix.voidsetMatrix(int i0, int i1, int[] c, Matrix x)Set a submatrix.voidsetMatrix(int i0, int i1, int j0, int j1, Matrix x)Set a submatrix.intsize()Get the size of the array.Matrixsolve(Matrix b)Solve A*X = B.doublesum()Sum all of the values in the matrix.double[]toPackedArray()Convert the matrix into a packed array.java.lang.StringtoString()
-
-
-
Constructor Detail
-
Matrix
public Matrix(boolean[][] sourceMatrix)
Construct a bipolar matrix from an array of booleans.- Parameters:
sourceMatrix- The booleans to create the matrix from.
-
Matrix
public Matrix(double[][] sourceMatrix)
Create a matrix from an array of doubles.- Parameters:
sourceMatrix- An array of doubles.
-
Matrix
public Matrix(int rows, int cols)Create a blank array with the specified number of rows and columns.- Parameters:
rows- How many rows in the matrix.cols- How many columns in the matrix.
-
-
Method Detail
-
createColumnMatrix
public static Matrix createColumnMatrix(double[] input)
Turn an array of doubles into a column matrix.- Parameters:
input- A double array.- Returns:
- A column matrix.
-
createRowMatrix
public static Matrix createRowMatrix(double[] input)
Turn an array of doubles into a row matrix.- Parameters:
input- A double array.- Returns:
- A row matrix.
-
add
public void add(int row, int col, double value)Add a value to one cell in the matrix.- Parameters:
row- The row to add to.col- The column to add to.value- The value to add to the matrix.
-
add
public void add(Matrix theMatrix)
Add the specified matrix to this matrix. This will modify the matrix to hold the result of the addition.- Parameters:
theMatrix- The matrix to add.
-
clear
public void clear()
Set all rows and columns to zero.
-
clone
public Matrix clone()
Create a copy of the matrix.- Overrides:
clonein classjava.lang.Object- Returns:
- A colne of the matrix.
-
equals
public boolean equals(Matrix theMatrix, int precision)
Compare to matrixes with the specified level of precision.- Parameters:
theMatrix- The other matrix to compare to.precision- How much precision to use.- Returns:
- True if the two matrixes are equal.
-
equals
public boolean equals(java.lang.Object other)
Check to see if this matrix equals another, using default precision.- Overrides:
equalsin classjava.lang.Object- Parameters:
other- The other matrix to compare.- Returns:
- True if the two matrixes are equal.
-
fromPackedArray
public int fromPackedArray(double[] array, int index)Create a matrix from a packed array.- Parameters:
array- The packed array.index- Where to start in the packed array.- Returns:
- The new index after this matrix has been read.
-
get
public double get(int row, int col)Read the specified cell in the matrix.- Parameters:
row- The row to read.col- The column to read.- Returns:
- The value at the specified row and column.
-
getArrayCopy
public double[][] getArrayCopy()
- Returns:
- A COPY of this matrix as a 2d array.
-
getCol
public Matrix getCol(int col)
Read one entire column from the matrix as a sub-matrix.- Parameters:
col- The column to read.- Returns:
- The column as a sub-matrix.
-
getCols
public int getCols()
Get the columns in the matrix.- Returns:
- The number of columns in the matrix.
-
getData
public double[][] getData()
- Returns:
- Get the 2D matrix array.
-
getMatrix
public Matrix getMatrix(int i0, int i1, int j0, int j1)
Get a submatrix.- Parameters:
i0- Initial row index.i1- Final row index.j0- Initial column index.j1- Final column index.- Returns:
- The specified submatrix.
-
getMatrix
public Matrix getMatrix(int i0, int i1, int[] c)
Get a submatrix.- Parameters:
i0- Initial row index.i1- Final row index.c- Array of column indices.- Returns:
- The specified submatrix.
-
getMatrix
public Matrix getMatrix(int[] r, int j0, int j1)
Get a submatrix.- Parameters:
r- Array of row indices.j0- Initial column indexj1- Final column index- Returns:
- The specified submatrix.
-
getMatrix
public Matrix getMatrix(int[] r, int[] c)
Get a submatrix.- Parameters:
r- Array of row indices.c- Array of column indices.- Returns:
- The specified submatrix.
-
getRow
public Matrix getRow(int row)
Get the specified row as a sub-matrix.- Parameters:
row- The row to get.- Returns:
- A matrix.
-
getRows
public int getRows()
Get the number of rows in the matrix.- Returns:
- The number of rows in the matrix.
-
hashCode
public int hashCode()
Compute a hash code for this matrix.- Overrides:
hashCodein classjava.lang.Object- Returns:
- The hash code.
-
inverse
public Matrix inverse()
- Returns:
- The matrix inverted.
-
isVector
public boolean isVector()
Determine if the matrix is a vector. A vector is has either a single number of rows or columns.- Returns:
- True if this matrix is a vector.
-
isZero
public boolean isZero()
Return true if every value in the matrix is zero.- Returns:
- True if the matrix is all zeros.
-
multiply
public void multiply(double value)
Multiply every value in the matrix by the specified value.- Parameters:
value- The value to multiply the matrix by.
-
multiply
public void multiply(double[] vector, double[] result)Multiply every row by the specified vector.- Parameters:
vector- The vector to multiply by.result- The result to hold the values.
-
randomize
public void randomize(double min, double max)Randomize the matrix.- Parameters:
min- Minimum random value.max- Maximum random value.
-
set
public void set(double value)
Set every value in the matrix to the specified value.- Parameters:
value- The value to set the matrix to.
-
set
public void set(int row, int col, double value)Set an individual cell in the matrix to the specified value.- Parameters:
row- The row to set.col- The column to set.value- The value to be set.
-
set
public void set(Matrix theMatrix)
Set this matrix's values to that of another matrix.- Parameters:
theMatrix- The other matrix.
-
setMatrix
public void setMatrix(int i0, int i1, int j0, int j1, Matrix x)Set a submatrix.- Parameters:
i0- Initial row indexi1- Final row indexj0- Initial column indexj1- Final column indexx- A(i0:i1,j0:j1)
-
setMatrix
public void setMatrix(int i0, int i1, int[] c, Matrix x)Set a submatrix.- Parameters:
i0- Initial row indexi1- Final row indexc- Array of column indices.x- The submatrix.
-
setMatrix
public void setMatrix(int[] r, int j0, int j1, Matrix x)Set a submatrix.- Parameters:
r- Array of row indices.j0- Initial column indexj1- Final column indexx- A(r(:),j0:j1)
-
setMatrix
public void setMatrix(int[] r, int[] c, Matrix x)Set a submatrix.- Parameters:
r- Array of row indices.c- Array of column indices.x- The matrix to set.
-
size
public int size()
Get the size of the array. This is the number of elements it would take to store the matrix as a packed array.- Returns:
- The size of the matrix.
-
solve
public Matrix solve(Matrix b)
Solve A*X = B.- Parameters:
b- right hand side.- Returns:
- Solution if A is square, least squares solution otherwise.
-
sum
public double sum()
Sum all of the values in the matrix.- Returns:
- The sum of the matrix.
-
toPackedArray
public double[] toPackedArray()
Convert the matrix into a packed array.- Returns:
- The matrix as a packed array.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
isSquare
public boolean isSquare()
-
-
DMelt 3.0 © DataMelt by jWork.ORG