All Packages Class Hierarchy This Package Previous Next Index
Class matrix.Matrix
java.lang.Object
|
+----matrix.Matrix
- public final class Matrix
- extends Object
- implements Parsable
A Matrix is a matrix where each element is a double
(a vector if it has only 1 row or column)
that can perform normal matrix/vector operations. A Matrix is
actually just a view on a subset of a 1D array, so a particular
array might have two different Matrix objects pointing to it,
one of which treats it as a 2D matrix, and one of which treats
it as a 1D vector. Also, a Matrix may view part of an array,
so it is possible to define a matrix or vector that is a concatenation
of smaller matrices or vectors. The constructor automatically
creates the 1D array and the Matrix object pointing to it, unless
an existing array is passed in.
The submatrix method returns another Matrix object that points to the
same array. The toVector method returns another Matrix object that
points to the same array and treats it as a single vector (column matrix).
This code is (c) 1996,1997 Leemon Baird
<leemon@cs.cmu.edu>,
http://www.cs.cmu.edu/~baird
The source and object code may be redistributed freely.
If the code is modified, please state so in the comments.
- Version:
- 1.08, 3 May 97
- Author:
- Leemon Baird, Mance Harmon, Scott Weaver
-
Matrix()
- Create a new scalar (a 1x1 matrix)
-
Matrix(double[])
- View an existing array as a 1D vector (column matrix)
-
Matrix(double[], int, int, int, int, int)
- Directly set 6 data fields in an object.
-
Matrix(int)
- Create a new column vector with n elements
-
Matrix(int, int)
- Create a new 2D matrix with size (rows,cols)
-
Matrix(int, int, double[])
- View an existing array as a 2D matrix with size (rows,cols)
All elements in the same row are adjacent in the array.
-
add(double)
- each_element_of_this += k, return this
-
add(Matrix)
- this=this+m, return this
-
addMult(double, Matrix)
- this=this+k*m, return this.
-
BNF(int)
-
-
clone()
- Clone this object.
-
diag(double)
- replaces all elements of this with zeros, except for all k's
on the diagonal.
-
diag(Matrix)
- replaces all elements of this with zeros, except for the
diagonal which will be copied from the elements of row or column
vector v.
-
dot(Matrix)
- Return dot product of this vector with vector v.
-
duplicate()
- Duplicate this object/associated data array and return a
pointer to the copy.
-
equalEls(Matrix)
- Return whether this and m are the same size and shape and have the same
numbers for corresponding elements.
-
getParameters(int)
- Return a parameter array if BNF(), parse(), and unparse() are to be automated, null otherwise.
-
initialize(int)
- Initialize, either partially or completely.
-
MatToScalar()
- return the only element of a 1x1 Matrix object
-
mult(double)
- this=this*k, return this.
-
mult(Matrix, Matrix)
- this=x*y, return this.
-
multAdd(double, Matrix)
- this=this*k+m, return this.
-
multColMat(Matrix, int, Matrix)
- this=((col)th column, zero based, of x)*v, return this
(which will be a matrix).
-
multDiag(Matrix, Matrix)
- this=diag(v)*y, return this.
-
multEl(Matrix)
- multiply corresponding elements of this matrix and matrix m,
store answer in this matrix, and return this matrix
-
multK(double, Matrix)
- this=k*m, return this.
-
multMatCol(Matrix, Matrix, int)
- this=x*((col)th column, zero based, of y), return this
(which will be a column).
-
nCols()
- Return the number of columns in the matrix.
-
nRows()
- Return the number of rows in the matrix.
-
parse(Parser, int)
- Parse the input file to get the parameters for this object.
-
replace(Matrix)
- this=m, return this.
-
set(int, double)
- set the nth element of a 1D vector (row or column vector)
-
set(int, int, double)
- set a particular element of a matrix to a particular value
-
setRandom(double, double, Random)
- set every element of this matrix to a random number between
min and max using the random number generator r.
-
size()
- Return the number of elements in the vector.
-
sub(Matrix)
- this=this-m, return this
-
subFrom(Matrix)
- this=m-this, return this
-
submatrix(int, int, int)
- Return a new Matrix that views some of the elements of this vector
(elements n to n+rows*cols-1 inclusive, zero based)
as a matrix of shape (rows,cols).
-
submatrix(int, int, int, int)
- Return a new Matrix that is a contiguous, rectangular subset of this one.
-
toString()
- a rectangle of numbers (using newlines) representing the matrix
-
toString(boolean)
- All fields plus the rectangle
-
toVector()
- Return a new Matrix object that is a single vector (column matrix)
pointing to the entire data array of this object.
-
transpose()
- Transpose this matrix or vector, and return this.
-
unparse(Unparser, int)
- Output a description of this object that can be parsed with parse().
-
useNative(boolean)
- Stops using native code if parameter is false.
-
val(int)
- return the nth element of a 1D vector (column or row vector) (zero based)
-
val(int, int)
- return a particular element of a matrix (zero based)
Matrix
public Matrix()
- Create a new scalar (a 1x1 matrix)
Matrix
public Matrix(int n)
- Create a new column vector with n elements
Matrix
public Matrix(int rows,
int cols)
- Create a new 2D matrix with size (rows,cols)
Matrix
public Matrix(double fArray[])
- View an existing array as a 1D vector (column matrix)
Matrix
public Matrix(int rows,
int cols,
double fArray[]) throws MatrixException
- View an existing array as a 2D matrix with size (rows,cols)
All elements in the same row are adjacent in the array.
- Throws: MatrixException
- array wasn't same size as matrix
Matrix
public Matrix(double data[],
int first,
int nRows,
int nCols,
int nextRow,
int nextCol)
- Directly set 6 data fields in an object. Avoid using this if possible.
initialize
public void initialize(int level)
- Initialize, either partially or completely.
- See Also:
- initialize
useNative
public void useNative(boolean useIt)
- Stops using native code if parameter is false. If parameter is true, it
will use native code if available. The default for this class is as if
useNative(true) was called initially.
size
public final int size()
- Return the number of elements in the vector.
Returns -1 if the matrix is neither a row
or column vector.
nCols
public final int nCols()
- Return the number of columns in the matrix.
nRows
public final int nRows()
- Return the number of rows in the matrix.
toVector
public final Matrix toVector()
- Return a new Matrix object that is a single vector (column matrix)
pointing to the entire data array of this object. If this object
represents only a subset of that array, the vector returned will
still represent the entire array, not the subset.
submatrix
public final Matrix submatrix(int row,
int col,
int rows,
int cols) throws MatrixException
- Return a new Matrix that is a contiguous, rectangular subset of this one.
The data in the matrix is not copied.
The Matrix object returned contains a pointer to the same data.
The new matrix has shape (rows,cols), and its (0,0) element is
element (row,col) in the source matrix.
- Throws: MatrixException
- if called on a 2D matrix, or index out of bounds
submatrix
public final Matrix submatrix(int n,
int rows,
int cols) throws MatrixException
- Return a new Matrix that views some of the elements of this vector
(elements n to n+rows*cols-1 inclusive, zero based)
as a matrix of shape (rows,cols).
- Throws: MatrixException
- if this is not a vector or has too few elements
MatToScalar
public final double MatToScalar() throws MatrixException
- return the only element of a 1x1 Matrix object
- Throws: MatrixException
- if called if not 1x1
val
public final double val(int n) throws MatrixException
- return the nth element of a 1D vector (column or row vector) (zero based)
- Throws: MatrixException
- if called on a 2D matrix, or index out of bounds
val
public final double val(int row,
int col) throws MatrixException
- return a particular element of a matrix (zero based)
- Throws: MatrixException
- if index out of bounds
set
public final void set(int n,
double val) throws MatrixException
- set the nth element of a 1D vector (row or column vector)
- Throws: MatrixException
- if called on a 2D matrix, or index out of bounds
set
public final void set(int row,
int col,
double val) throws MatrixException
- set a particular element of a matrix to a particular value
- Throws: MatrixException
- if index out of bounds
transpose
public final Matrix transpose()
- Transpose this matrix or vector, and return this.
duplicate
public final Matrix duplicate()
- Duplicate this object/associated data array and return a
pointer to the copy.
clone
public final Object clone()
- Clone this object. The data array it points to is not cloned.
- Overrides:
- clone in class Object
getParameters
public final Object[][] getParameters(int lang)
- Return a parameter array if BNF(), parse(), and unparse() are to be automated, null otherwise.
- See Also:
- getParameters
BNF
public final String BNF(int lang)
unparse
public final void unparse(Unparser u,
int lang)
- Output a description of this object that can be parsed with parse().
- See Also:
- Parsable
parse
public final Object parse(Parser p,
int lang) throws ParserException
- Parse the input file to get the parameters for this object.
- Throws: ParserException
- parser didn't find the required token
toString
public final String toString()
- a rectangle of numbers (using newlines) representing the matrix
- Overrides:
- toString in class Object
toString
public final String toString(boolean full)
- All fields plus the rectangle
setRandom
public final Matrix setRandom(double min,
double max,
Random r)
- set every element of this matrix to a random number between
min and max using the random number generator r.
equalEls
public final boolean equalEls(Matrix m)
- Return whether this and m are the same size and shape and have the same
numbers for corresponding elements. Two matrices are still equal
if their data is stored in different arrays, and if corresponding
elements uparse to different symbolic expressions, as long as the
numeric values are the same.
add
public final Matrix add(Matrix m) throws MatrixException
- this=this+m, return this
- Throws: MatrixException
- if matrices are different sizes
sub
public final Matrix sub(Matrix m) throws MatrixException
- this=this-m, return this
- Throws: MatrixException
- if matrices are different shapes
subFrom
public final Matrix subFrom(Matrix m) throws MatrixException
- this=m-this, return this
- Throws: MatrixException
- if matrices are different shapes
multAdd
public final Matrix multAdd(double k,
Matrix m) throws MatrixException
- this=this*k+m, return this.
- Throws: MatrixException
- if matrices are different sizes
addMult
public final Matrix addMult(double k,
Matrix m) throws MatrixException
- this=this+k*m, return this.
- Throws: MatrixException
- if matrices are different sizes
dot
public final double dot(Matrix v) throws MatrixException
- Return dot product of this vector with vector v.
Both are row vectors, or both columns, or one of each.
- Throws: MatrixException
- different # elements, or 1 isn't a vector
replace
public final Matrix replace(Matrix m) throws MatrixException
- this=m, return this.
Replace this object's data with the data of matrix m,
then returns this. This copies the data in the array itself,
not just pointers to it. The matricies must be the same shape.
If both are vectors of the same size, then it is OK for one
to be a row vector and one a column.
- Throws: MatrixException
- if matricies are different shapes
add
public final Matrix add(double k)
- each_element_of_this += k, return this
diag
public final Matrix diag(double k) throws MatrixException
- replaces all elements of this with zeros, except for all k's
on the diagonal. this must be square.
- Throws: MatrixException
- if matrix is not square
diag
public final Matrix diag(Matrix v) throws MatrixException
- replaces all elements of this with zeros, except for the
diagonal which will be copied from the elements of row or column
vector v. this must be square.
- Throws: MatrixException
- if matrix is not square or v is not a vector of that size
mult
public final Matrix mult(Matrix x,
Matrix y) throws MatrixException
- this=x*y, return this.
x and y are matrices of appropriate shapes.
This object must already be the right shape to hold the answer.
Returns pointer to this object.
- Throws: MatrixException
- if shapes are wrong
mult
public final Matrix mult(double k)
- this=this*k, return this. If k=0, then it sets this equal to all zeros,
even if this contains some infinites or Not-a-Numbers.
multK
public final Matrix multK(double k,
Matrix m) throws MatrixException
- this=k*m, return this.
- Throws: MatrixException
- if this and m are different sizes
multEl
public final Matrix multEl(Matrix m) throws MatrixException
- multiply corresponding elements of this matrix and matrix m,
store answer in this matrix, and return this matrix
- Throws: MatrixException
- if this and m are different sizes
multColMat
public final Matrix multColMat(Matrix x,
int col,
Matrix v) throws MatrixException
- this=((col)th column, zero based, of x)*v, return this
(which will be a matrix).
x is a matrix and v is a row vector.
This object must already be the right shape to hold the answer
(row(x) by col(v)).
Returns pointer to this object.
- Throws: MatrixException
- if shapes are wrong
multMatCol
public final Matrix multMatCol(Matrix x,
Matrix y,
int col) throws MatrixException
- this=x*((col)th column, zero based, of y), return this
(which will be a column).
x and y are matrices of appropriate shapes.
This object must already be the right shape to hold the answer.
Returns pointer to this object.
- Throws: MatrixException
- if shapes are wrong
multDiag
public final Matrix multDiag(Matrix v,
Matrix y) throws MatrixException
- this=diag(v)*y, return this.
diag(v) is a diagonal matrix whose diagonal
is the elements of the column or row vector v.
y and this are matrices of the appropriate shape.
this must already be the right shape to hold the answer.
Returns pointer to this object.
- Throws: MatrixException
- if shapes are wrong
All Packages Class Hierarchy This Package Previous Next Index