All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class matrix.MatrixD

java.lang.Object
   |
   +----matrix.MatrixD

public final class MatrixD
extends Object
implements Parsable
A MatrixD 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 MatrixD is actually just a view on a subset of a 1D array, so a particular array might have two different MatrixD 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 MatrixD 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 MatrixD object pointing to it, unless an existing array is passed in. The submatrix method returns another MatrixD object that points to the same array. The toVector method returns another MatrixD 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

Variable Index

 o data
The 1D array that holds the data for this MatrixD, and perhaps others too.
 o first
element of array holding upper-left corner of matrix
 o last
element of array holding lower-right corner of matrix
 o nativeCodeLoaded
Is native code loaded?
 o nCols
# columns in the matrix
 o newCol
amount to add to an index to go from past the end of one column to top of next
 o newRow
amount to add to an index to go from past the end of one row to start of next
 o next
amount to add to index to get next element of this vector
 o nextCol
amount to add to an index to move to the next column
 o nextRow
amount to add to an index to move to the next row
 o nRows
# rows in the matrix
 o restartCol
amount to add to an index to go from past the end of one column back to its start
 o restartRow
amount to add to an index to go from past the end of one row back to its start
 o size
# elements in this vector (-1 if not a vector)
 o useNativeCode
Use the native code?

Constructor Index

 o MatrixD()
Create a new scalar (a 1x1 matrix)
 o MatrixD(double[])
View an existing array as a 1D vector (column matrix)
 o MatrixD(double[], int, int)
View an existing array as a 2D matrix with size (rows,cols) All elements in the same row are adjacent in the array.
 o MatrixD(double[], int, int, int, int, int)
Directly set 6 data fields in an object.
 o MatrixD(int)
Create a new column vector with n elements
 o MatrixD(int, int)
Create a new 2D matrix with size (rows,cols)

Method Index

 o add(double)
each_element_of_this += k, return this
 o add(MatrixD)
this=this+m, return this
 o addMult(double, MatrixD)
this=this+k*m, return this.
 o BNF(int)
 o clone()
Clone this object.
 o diag(double)
replaces all elements of this with zeros, except for all k's on the diagonal.
 o diag(MatrixD)
replaces all elements of this with zeros, except for the diagonal which will be copied from the elements of row or column vector v.
 o dot(MatrixD)
Return dot product of this vector with vector v.
 o duplicate()
Duplicate this object/associated data array and return a pointer to the copy.
 o equalEls(MatrixD)
Return whether this and m are the same size and shape and have the same numbers for corresponding elements.
 o getParameters(int)
Return a parameter array if BNF(), parse(), and unparse() are to be automated, null otherwise.
 o initialize(int)
Initialize, either partially or completely.
 o MatToScalar()
return the only element of a 1x1 MatrixD object
 o mult(double)
this=this*k, return this.
 o mult(MatrixD, MatrixD)
this=x*y, return this.
 o multAdd(double, MatrixD)
this=this*k+m, return this.
 o multColMat(MatrixD, int, MatrixD)
this=((col)th column, zero based, of x)*v, return this (which will be a matrix).
 o multDiag(MatrixD, MatrixD)
this=diag(v)*y, return this.
 o multEl(MatrixD)
multiply corresponding elements of this matrix and matrix m, store answer in this matrix, and return this matrix
 o multK(double, MatrixD)
this=k*m, return this.
 o multMatCol(MatrixD, MatrixD, int)
this=x*((col)th column, zero based, of y), return this (which will be a column).
 o parse(Parser, int)
Parse the input file to get the parameters for this object.
 o replace(MatrixD)
this=m, return this.
 o set(int, double)
set the nth element of a 1D vector (row or column vector)
 o set(int, int, double)
set a particular element of a matrix to a particular value
 o setRandom(double, double, Random)
set every element of this matrix to a random number between min and max using the random number generator r.
 o sub(MatrixD)
this=this-m, return this
 o subFrom(MatrixD)
this=m-this, return this
 o submatrix(int, int, int)
Return a new MatrixD 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).
 o submatrix(int, int, int, int)
Return a new MatrixD that is a contiguous, rectangular subset of this one.
 o toString()
a rectangle of numbers (using newlines) representing the matrix
 o toString(boolean)
All fields plus the rectangle
 o toVector()
Return a new MatrixD object that is a single vector (column matrix) pointing to the entire data array of this object.
 o transpose()
Transpose this matrix or vector, and return this.
 o unparse(Unparser, int)
Output a description of this object that can be parsed with parse().
 o useNative(boolean)
Stops using native code if parameter is false.
 o val(int)
return the nth element of a 1D vector (column or row vector) (zero based)
 o val(int, int)
return a particular element of a matrix (zero based)

Variables

 o nativeCodeLoaded
 public static boolean nativeCodeLoaded
Is native code loaded?

 o useNativeCode
 public static boolean useNativeCode
Use the native code?

 o data
 public double data[]
The 1D array that holds the data for this MatrixD, and perhaps others too.

 o nRows
 public int nRows
# rows in the matrix

 o nCols
 public int nCols
# columns in the matrix

 o nextRow
 public int nextRow
amount to add to an index to move to the next row

 o nextCol
 public int nextCol
amount to add to an index to move to the next column

 o first
 public int first
element of array holding upper-left corner of matrix

 o last
 public int last
element of array holding lower-right corner of matrix

 o newRow
 public int newRow
amount to add to an index to go from past the end of one row to start of next

 o newCol
 public int newCol
amount to add to an index to go from past the end of one column to top of next

 o restartRow
 public int restartRow
amount to add to an index to go from past the end of one row back to its start

 o restartCol
 public int restartCol
amount to add to an index to go from past the end of one column back to its start

 o size
 public int size
# elements in this vector (-1 if not a vector)

 o next
 public int next
amount to add to index to get next element of this vector

Constructors

 o MatrixD
 public MatrixD()
Create a new scalar (a 1x1 matrix)

 o MatrixD
 public MatrixD(int n)
Create a new column vector with n elements

 o MatrixD
 public MatrixD(int rows,
                int cols)
Create a new 2D matrix with size (rows,cols)

 o MatrixD
 public MatrixD(double fArray[])
View an existing array as a 1D vector (column matrix)

 o MatrixD
 public MatrixD(double fArray[],
                int rows,
                int cols) 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
 o MatrixD
 public MatrixD(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.

Methods

 o initialize
 public void initialize(int level)
Initialize, either partially or completely.

See Also:
initialize
 o 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.

 o toVector
 public MatrixD toVector()
Return a new MatrixD 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.

 o submatrix
 public MatrixD submatrix(int row,
                          int col,
                          int rows,
                          int cols) throws MatrixException
Return a new MatrixD that is a contiguous, rectangular subset of this one. * The data in the matrix is not copied. The MatrixD 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
 o submatrix
 public MatrixD submatrix(int n,
                          int rows,
                          int cols) throws MatrixException
Return a new MatrixD 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
 o MatToScalar
 public final double MatToScalar() throws MatrixException
return the only element of a 1x1 MatrixD object

Throws: MatrixException
if called if not 1x1
 o 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
 o 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
 o 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
 o 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
 o transpose
 public MatrixD transpose()
Transpose this matrix or vector, and return this.

 o duplicate
 public final MatrixD duplicate()
Duplicate this object/associated data array and return a pointer to the copy.

 o clone
 public Object clone()
Clone this object. The data array it points to is not cloned.

Overrides:
clone in class Object
 o getParameters
 public Object[][] getParameters(int lang)
Return a parameter array if BNF(), parse(), and unparse() are to be automated, null otherwise.

See Also:
getParameters
 o BNF
 public String BNF(int lang)
 o unparse
 public void unparse(Unparser u,
                     int lang)
Output a description of this object that can be parsed with parse().

See Also:
Parsable
 o parse
 public 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
 o toString
 public String toString()
a rectangle of numbers (using newlines) representing the matrix

Overrides:
toString in class Object
 o toString
 public String toString(boolean full)
All fields plus the rectangle

 o setRandom
 public MatrixD 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.

 o equalEls
 public final boolean equalEls(MatrixD 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.

 o add
 public final MatrixD add(MatrixD m) throws MatrixException
this=this+m, return this

Throws: MatrixException
if matrices are different sizes
 o sub
 public final MatrixD sub(MatrixD m) throws MatrixException
this=this-m, return this

Throws: MatrixException
if matrices are different shapes
 o subFrom
 public final MatrixD subFrom(MatrixD m) throws MatrixException
this=m-this, return this

Throws: MatrixException
if matrices are different shapes
 o multAdd
 public final MatrixD multAdd(double k,
                              MatrixD m) throws MatrixException
this=this*k+m, return this.

Throws: MatrixException
if matrices are different sizes
 o addMult
 public final MatrixD addMult(double k,
                              MatrixD m) throws MatrixException
this=this+k*m, return this.

Throws: MatrixException
if matrices are different sizes
 o dot
 public final double dot(MatrixD 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
 o replace
 public final MatrixD replace(MatrixD 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
 o add
 public final MatrixD add(double k)
each_element_of_this += k, return this

 o diag
 public final MatrixD 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
 o diag
 public final MatrixD diag(MatrixD 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
 o mult
 public final MatrixD mult(MatrixD x,
                           MatrixD 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
 o mult
 public final MatrixD 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.

 o multK
 public final MatrixD multK(double k,
                            MatrixD m) throws MatrixException
this=k*m, return this.

Throws: MatrixException
if this and m are different sizes
 o multEl
 public final MatrixD multEl(MatrixD 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
 o multColMat
 public final MatrixD multColMat(MatrixD x,
                                 int col,
                                 MatrixD 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
 o multMatCol
 public final MatrixD multMatCol(MatrixD x,
                                 MatrixD 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
 o multDiag
 public final MatrixD multDiag(MatrixD v,
                               MatrixD 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