jv.vecmath
Class PdMatrix
- java.lang.Object
-
- jv.vecmath.PdMatrix
-
- All Implemented Interfaces:
- java.io.Serializable, PdMatrixIf
public class PdMatrix extends java.lang.Object implements java.io.Serializable, PdMatrixIf
Linear algebra class for small matrices of doubles. For methods on large matrices see jvx.numeric.PnMatrix.- See Also:
- Serialized Form
- Author:
- Konrad Polthier
- Version:
- 28.12.03, 1.40 revised (kp) New methods getRow() and getColumn().
12.03.02, 1.30 revised (kp) Two new method leftMult and rightMult for partial multiplication.
08.11.00, 1.20 revised (kp) Replace string formatting routines of Fmt with PuString.
07.09.99, 1.10 revised (kp) Some new methods added.
00.00.97, 1.00 created (kp)
-
-
Field Summary
Fields Modifier and Type Field and Description double[][]m_dataData array containing the components of the vector.
-
Constructor Summary
Constructors Constructor and Description PdMatrix()Create a new matrix of zero size.PdMatrix(double[][] x)Constructs a matrix having same size and given initial data as given array.PdMatrix(int aSize)Create a square matrix with given dimension.PdMatrix(int iSize, int jSize)Create a non-square matrix with given dimensions.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method and Description voidadd(PdMatrix m)Add given matrix to this, this = this+m.voidadd(PdMatrix m1, PdMatrix m2)Add two matrices and assign to this, this = m1+m2.voidaddEntry(int i, int j, double value)Add value to component of matrix, and possibly enlarge matrix if too small.booleanadjoint(PdVector v)Computethis = v * v^tusing adjoint(v, v).booleanadjoint(PdVector v, PdVector w)Computethis = v * w^t.voidblend(double a, PdMatrix u, double b, PdMatrix v, double c, PdMatrix w)Interpolate between three matrices with given weights, i.e.voidblend(PdBary bary, PdMatrix u, PdMatrix v, PdMatrix w)Interpolate between three matrices with given barycentric weights.voidclear()Set all entries to zero.java.lang.Objectclone()Duplicate matrix and data array.voidcopy(PdMatrix m)Copy given matrix to this, this = m.static booleancopy(PdMatrix[] dataDest, int destInd, PdMatrix[] dataSrc, int srcInd, int size)Copy PdMatrix array.static PdMatrixcopyNew(PdMatrix mat)Create a new matrix as clone of argument matrix.static PdMatrix[]copyNew(PdMatrix[] data)Duplicate PdMatrix array.static PdMatrix[]copyNew(PdMatrix[] data, int size)Duplicate data PdMatrix array to new PdMatrix array of given size.doubledet()Compute determinant square matrix with dimensions less or equal four.doubledet33()Compute determinant of (3*3) left upper block of matrix.static PdMatrixexpandComponents(PdMatrix mat, int dim)Create a bigger matrix such that each original entry expands to a (dim, dim)-diagonal matrix.PdVectorgetColumn(int aColumn)Get column vector of matrix.double[][]getEntries()Return a copy of content of matrix as array of arrays of doubles.doublegetEntry(int i, int j)Get the component of matrix at (i-th row, j-th column).intgetISize()Get number of rows of matrix.intgetJSize()Get number of columns of matrix.intgetNumCols()Get number of columns of matrix.intgetNumRows()Get number of rows of matrix.PdVectorgetRow(int aRow)Get row vector of matrix.intgetSize()Get dimension of square matrix, return -1 for non-square matrices.booleaninvert()Invert a square matrix of dimension less than five bythis = inverse(this).booleaninvert(PdMatrix m)Invert a square matrix of dimension less than five bythis = inverse(m).booleaninvert34(PdMatrix m)Invert the top-left 3x4 square of a matrix44 in place.booleaninvertTopLeft(PdMatrix m, int aSubSize)Invert the top-left subSize x subSize square of a matrix m in place and assign full matrix m to this matrix.booleanisSquare()Check if matrix is square.voidleftMult(PdMatrix m)Multiply with square matrix from left of same sizes usingthis = m*this.voidleftMult(PdMatrix m, int size)Multiply a submatrix of this matrix with a submatrix of the parameter matrix.PdVectorleftMultMatrix(PdVector out, PdVector in)Multiply this matrix with vector and fill output vector,out = this*in.voidmult(PdMatrix mat1, PdMatrix mat2)Multiply two matrices of possibly different sizes usingthis = mat1*mat2.doublemultQuadratic(PdVector left, PdVector right)Computeleft^T*this*right.voidmultScalar(double scalar)Multiply all matrix components with scalar.voidmultScalar(PdMatrix m, double scalar)Multiply all matrix components of m with scalar s, this = m*s.static PdMatrix[]realloc(PdMatrix[] data, int arraySize)Allocate array of PdMatrix.static PdMatrix[]realloc(PdMatrix[] data, int arraySize, int iSize, int jSize)Allocate an array of PdMatrix with given length and matrix dimensions.voidrightMult(PdMatrix m)Multiply with square matrix from right of same sizes usingthis = this*m.voidrightMult(PdMatrix m, int size)Multiply a submatrix of this matrix with a submatrix of the parameter matrix..PdVectorrightMultMatrix(PdVector out, PdVector in)Multiply this matrix with vector and fill output vector,out = in*this.voidset(double[] x)Assign values of array assuming length of argument array is equal or bigger than available matrix entries.voidset(double[][] x)Assign values of array, and adjust length of matrix if different from length of array.voidsetColumn(int aColumn, PdVector aVector)Set column vector of matrix.voidsetColumns(PdVector[] aVector)Fill matrix with array of column vectors.voidsetConstant(double aValue)Set all matrix entries to a constant value.voidsetDiagonal(double diag)Set diagonal entries of a square matrix to given double value.voidsetDiagonal(PdVector diag)Set diagonal entries of a square matrix to components of a given vector.voidsetEntry(int i, int j, double value)Assign value to component of matrix, and possibly enlarge matrix if too small.voidsetIdentity()Initialize square matrix to be identity matrix.voidsetRow(int aRow, PdVector aVector)Set row vector of matrix.voidsetRows(PdVector[] aVector)Fill matrix with array of row vectors.voidsetSize(int size)Resize square matrix with new dimension (size, size).voidsetSize(int iSize, int jSize)Resize matrix to new dimension (iSize, jSize) with iSize rows and jSize columns.voidsub(PdMatrix m)Subtract given matrix from this, this = this-m.voidsub(PdMatrix m1, PdMatrix m2)Subtract two matrices and assign to this, this = m1-m2.java.lang.StringtoShortString()Create a short string representation of matrix with all instance variables.java.lang.StringtoString()Create a multi-line string representation with detailed information about all instance variables.voidtranspose()Transpose a square matrix bythis = transpose(this).voidtranspose(PdMatrix m)Transpose a matrix bythis = transpose(m).
-
-
-
Field Detail
-
m_data
public double[][] m_data
Data array containing the components of the vector. Array has public access for efficiency reasons in numerical computations, although method access should be preferred. Do not modify length of array directly.
-
-
Constructor Detail
-
PdMatrix
public PdMatrix()
Create a new matrix of zero size.- Version:
- 27.12.01, 1.10 revised (kp) Now allocate arrays with zero length.
-
PdMatrix
public PdMatrix(int aSize)
Create a square matrix with given dimension.
-
PdMatrix
public PdMatrix(int iSize, int jSize)Create a non-square matrix with given dimensions.
-
PdMatrix
public PdMatrix(double[][] x)
Constructs a matrix having same size and given initial data as given array.- Version:
- 20.07.00, 1.00 created (kp).
-
-
Method Detail
-
realloc
public static PdMatrix[] realloc(PdMatrix[] data, int arraySize, int iSize, int jSize)
Allocate an array of PdMatrix with given length and matrix dimensions. If existing data is given then method tries to reused existing data, otherwise missing data is newly allocated.- Parameters:
data- null or existing array of matricesarraySize- length of array to be allocatediSize- number of rowsjSize- number of columns
-
realloc
public static PdMatrix[] realloc(PdMatrix[] data, int arraySize)
Allocate array of PdMatrix.
-
clone
public java.lang.Object clone()
Duplicate matrix and data array. Method just returns result ofcopyNew(PdMatrix).- Overrides:
clonein classjava.lang.Object- See Also:
copyNew(PdMatrix)- Version:
- 05.07.00, 1.00 created (kp)
-
copyNew
public static PdMatrix[] copyNew(PdMatrix[] data, int size)
Duplicate data PdMatrix array to new PdMatrix array of given size.
-
copyNew
public static PdMatrix copyNew(PdMatrix mat)
Create a new matrix as clone of argument matrix.- Version:
- 29.04.01, 1.10 revised (kp) Do not break if source matrix is empty.
-
copy
public static boolean copy(PdMatrix[] dataDest, int destInd, PdMatrix[] dataSrc, int srcInd, int size)
Copy PdMatrix array.
-
set
public void set(double[][] x)
Assign values of array, and adjust length of matrix if different from length of array.- Version:
- 20.07.00 created (kp)
-
set
public void set(double[] x)
Assign values of array assuming length of argument array is equal or bigger than available matrix entries. That means, the first entries of the vector are used to completely fill the matrix. Size of matrix is not changed.- Since:
- JavaView v4.00.009
- Version:
- 26.11.12 created (kp)
-
getNumRows
public int getNumRows()
Get number of rows of matrix.- Specified by:
getNumRowsin interfacePdMatrixIf
-
getNumCols
public int getNumCols()
Get number of columns of matrix.- Specified by:
getNumColsin interfacePdMatrixIf
-
getISize
public int getISize()
Get number of rows of matrix.
-
getJSize
public int getJSize()
Get number of columns of matrix.
-
getSize
public int getSize()
Get dimension of square matrix, return -1 for non-square matrices.
-
setSize
public void setSize(int size)
Resize square matrix with new dimension (size, size). Existing content of matrix is kept as much as possible.
-
setSize
public void setSize(int iSize, int jSize)Resize matrix to new dimension (iSize, jSize) with iSize rows and jSize columns. Existing content of matrix is kept as much as possible.- Version:
- 11.12.02, 2.00 revised (kp) Matrix now keeps original content.
-
getEntries
public double[][] getEntries()
Return a copy of content of matrix as array of arrays of doubles.
-
getEntry
public double getEntry(int i, int j)Get the component of matrix at (i-th row, j-th column).- Specified by:
getEntryin interfacePdMatrixIf
-
setEntry
public void setEntry(int i, int j, double value)Assign value to component of matrix, and possibly enlarge matrix if too small.- Specified by:
setEntryin interfacePdMatrixIf- Version:
- 09.08.10, 1.10 revised (ur) Enlarge matrix size for i and j separately. 17.07.00, 1.00 revised (kp) . 17.07.00, 1.00 created (kp)
-
addEntry
public void addEntry(int i, int j, double value)Add value to component of matrix, and possibly enlarge matrix if too small.- Specified by:
addEntryin interfacePdMatrixIf- Version:
- 20.03.10, 1.00 revised (kp) . 20.03.10, 1.00 created (kp)
-
isSquare
public boolean isSquare()
Check if matrix is square.- Specified by:
isSquarein interfacePdMatrixIf
-
setIdentity
public void setIdentity()
Initialize square matrix to be identity matrix.- Version:
- 12.07.13, 1.10 revised (kp) Use methods setConstant and setDiagonal.
-
setConstant
public void setConstant(double aValue)
Set all matrix entries to a constant value.
-
clear
public void clear()
Set all entries to zero.- Specified by:
clearin interfacePdMatrixIf
-
setDiagonal
public void setDiagonal(double diag)
Set diagonal entries of a square matrix to given double value. No other values of matrix are modified, i.e. used in conjunction e.g. with setConstant().- Parameters:
diag- Value of diagonal components- Since:
- JavaView 4.04.009
- Version:
- 12.07.13, 1.00 created (kp).
-
setDiagonal
public void setDiagonal(PdVector diag)
Set diagonal entries of a square matrix to components of a given vector. No other values of matrix are modified, i.e. used in conjunction e.g. with setConstant(). Size of vector must be equal or larger than size of square matrix.- Parameters:
diag- Vector with diagonal components
-
getRow
public PdVector getRow(int aRow)
Get row vector of matrix. Method returns null if row index out of bounds.- Version:
- 28.12.03, 1.00 created (kp)
-
setRow
public void setRow(int aRow, PdVector aVector)Set row vector of matrix. Length of vector must be equal or greater than number of columns. Dimension of matrix is not modified.
-
getColumn
public PdVector getColumn(int aColumn)
Get column vector of matrix. Method returns null if column index out of bounds.- Version:
- 28.12.03, 1.00 created (kp)
-
setColumn
public void setColumn(int aColumn, PdVector aVector)Set column vector of matrix. Length of vector must be equal or greater than number of rows. Dimension of matrix is not modified.
-
setRows
public void setRows(PdVector[] aVector)
Fill matrix with array of row vectors. Number of vectors and length of each vector must be equal or greater than corresponding sizes of matrix. Dimension of matrix is not modified.
-
setColumns
public void setColumns(PdVector[] aVector)
Fill matrix with array of column vectors. Number of vectors and length of each vector must be equal or greater than corresponding sizes of matrix. Dimension of matrix is not modified.
-
add
public void add(PdMatrix m)
Add given matrix to this, this = this+m. Dimensions of argument matrix must be equal or larger than of this.
-
add
public void add(PdMatrix m1, PdMatrix m2)
Add two matrices and assign to this, this = m1+m2. Dimensions of argument matrices must be equal or larger than of this.
-
sub
public void sub(PdMatrix m)
Subtract given matrix from this, this = this-m. Dimensions of argument matrix must be equal or larger than of this.
-
sub
public void sub(PdMatrix m1, PdMatrix m2)
Subtract two matrices and assign to this, this = m1-m2. Dimensions of argument matrices must be equal or larger than of this.
-
copy
public void copy(PdMatrix m)
Copy given matrix to this, this = m. Dimensions of argument matrix must be equal or larger than of this.
-
multScalar
public void multScalar(double scalar)
Multiply all matrix components with scalar.- Specified by:
multScalarin interfacePdMatrixIf
-
multScalar
public void multScalar(PdMatrix m, double scalar)
Multiply all matrix components of m with scalar s, this = m*s. Dimensions of argument matrix must be equal or larger than of this.
-
det
public double det()
Compute determinant square matrix with dimensions less or equal four.- Version:
- 24.01.05, 1.10 revised (kp) Determinant of 4*4 matrix implemented.
-
det33
public double det33()
Compute determinant of (3*3) left upper block of matrix.
-
leftMult
public void leftMult(PdMatrix m, int size)
Multiply a submatrix of this matrix with a submatrix of the parameter matrix. Size of 'this' does not change. Matrices must be larger than size of submatrix.- Parameters:
m- parameter matrixsize- size of square sub matrix affected within this.- Version:
- 15.08.03, 1.10 revised (kp) Reduce memory allocation to single row from full matrix.
-
leftMult
public void leftMult(PdMatrix m)
Multiply with square matrix from left of same sizes usingthis = m*this. Size of 'this' does not change.- Version:
- 10.08.10, 1.20 revised (ur) Bugfix - always use the correct dimensions.
15.08.03, 1.10 revised (kp) Reduce memory allocation to single row from full matrix.
-
rightMult
public void rightMult(PdMatrix m, int size)
Multiply a submatrix of this matrix with a submatrix of the parameter matrix.. Size of 'this' does not change. Matrices must be larger than size of submatrix.- Parameters:
m- parameter matrixsize- size of square sub matrix affected within this.- Version:
- 15.08.03, 1.10 revised (kp) Reduce memory allocation to single row from full matrix.
-
rightMult
public void rightMult(PdMatrix m)
Multiply with square matrix from right of same sizes usingthis = this*m. Size of 'this' does not change.- Version:
- 10.08.10, 1.20 revised (ur) Always use the correct matrix dimensions.
15.08.03, 1.10 revised (kp) Reduce memory allocation to single row from full matrix.
-
mult
public void mult(PdMatrix mat1, PdMatrix mat2)
Multiply two matrices of possibly different sizes usingthis = mat1*mat2. Size of 'this' is adjusted to have number of rows equal to first factor and number of columns equal to second factor.- Version:
- 10.08.10, 1.60 revised (ur) Added security check for compatible matrix dimensions.
21.07.02, 1.50 revised (kp) Optimized if first argument matrix is sparse.
-
leftMultMatrix
public PdVector leftMultMatrix(PdVector out, PdVector in)
Multiply this matrix with vector and fill output vector,out = this*in.. Matrix need not be square. Input vector must have same size as number of columns of this matrix. Method modifies size of output vector to number of rows of this matrix. If output vector is null then it is created.Method calls
PdVector.leftMultMatrix(PdMatrix, PdVector).- Specified by:
leftMultMatrixin interfacePdMatrixIf- Version:
- 20.03.10, 1.00 created (kp) Created to sync with PnMatrixIf and PnSparseMatrix.
-
rightMultMatrix
public PdVector rightMultMatrix(PdVector out, PdVector in)
Multiply this matrix with vector and fill output vector,out = in*this.. Matrix need not be square. Input vector must have same size as number of rows of this matrix. Method modifies size of output vector to number of columns of this matrix. If output vector is null then it is created.Method calls
PdVector.rightMultMatrix(PdVector, PdMatrix).- Specified by:
rightMultMatrixin interfacePdMatrixIf- Version:
- 20.03.10, 1.00 created (kp) Created to sync with PnMatrixIf and PnSparseMatrix.
-
transpose
public void transpose()
Transpose a square matrix bythis = transpose(this).- Specified by:
transposein interfacePdMatrixIf
-
transpose
public void transpose(PdMatrix m)
Transpose a matrix bythis = transpose(m).- Version:
- 23.12.05, 1.10 revised (kp) Allow non-square matrices.
-
invert
public boolean invert()
Invert a square matrix of dimension less than five bythis = inverse(this).
-
invert
public boolean invert(PdMatrix m)
Invert a square matrix of dimension less than five bythis = inverse(m).- Returns:
falseif error occurred, and if arg is singular call this.setIdentity().- Version:
- 14.03.06, 1.31 revised (kp) Bug in 4*4 inversion corrected which led to return of id matrix.
27.01.05, 1.30 revised (ep) Removed warning that was thrown when matrix is not invertible.
24.01.05, 1.20 revised (kp) Check size of this and argument, if arg is singular call this.setIdentity().
02.06.04, 1.10 revised (kp) Check det against EPS instead of ZERO.
-
invertTopLeft
public boolean invertTopLeft(PdMatrix m, int aSubSize)
Invert the top-left subSize x subSize square of a matrix m in place and assign full matrix m to this matrix.In case of error set this to identity.
- Version:
- 2019-10-07, 1.10 revised (kp) Initialize with original matrix only of submatrix is smaller.
-
invert34
public boolean invert34(PdMatrix m)
Invert the top-left 3x4 square of a matrix44 in place.
-
adjoint
public boolean adjoint(PdVector v)
Computethis = v * v^tusing adjoint(v, v).- Version:
- 02.06.04, 1.10 revised (kp) Use adjoint(v,v).
-
adjoint
public boolean adjoint(PdVector v, PdVector w)
Computethis = v * w^t.- Version:
- 02.06.04, 1.10 revised (kp) Better check of sizes.
-
toString
public java.lang.String toString()
Create a multi-line string representation with detailed information about all instance variables.- Overrides:
toStringin classjava.lang.Object
-
toShortString
public java.lang.String toShortString()
Create a short string representation of matrix with all instance variables.
-
expandComponents
public static PdMatrix expandComponents(PdMatrix mat, int dim)
Create a bigger matrix such that each original entry expands to a (dim, dim)-diagonal matrix.
-
blend
public void blend(double a, PdMatrix u, double b, PdMatrix v, double c, PdMatrix w)Interpolate between three matrices with given weights, i.e.this = u*a + v*b + w*c. If the matrix 'this' is empty, the size of this is adjusted equal to the size of the matrix u. Otherwise interpolation is done up to length of result vector.- Parameters:
a- weight of first matrixu- first matrixb- weight of second matrixv- second matrixc- weight of third matrixw- third matrix- Author:
- Klaus Hildebrandt
- Version:
- 02.12.07, 1.00 created (kh)
-
blend
public void blend(PdBary bary, PdMatrix u, PdMatrix v, PdMatrix w)
Interpolate between three matrices with given barycentric weights. Usesthis = u*bary.getEntry(0) + v*bary.getEntry(1) + w*bary.getEntry(2). If the matrix 'this' is empty, the size of this is adjusted equal to the size of the matrix u. Otherwise interpolation is done up to length of result vector.- Parameters:
bary- barycentric weights of three matricesu- first matrixv- second matrixw- third matrix- Author:
- Klaus Hildebrandt
- Version:
- 13.01.08, 1.01 revised (kp) Doc corrected.
02.12.07, 1.00 created (kh)
-
multQuadratic
public double multQuadratic(PdVector left, PdVector right)
Computeleft^T*this*right. The matrix is interpreted as a quadratic form.- Specified by:
multQuadraticin interfacePdMatrixIf- Parameters:
left- The left argument.right- The right argument.- Returns:
left^T*this*right.
-
-
"