public class Ginv extends AbstractDoubleCalculation
This class implements some matrix operations that I need for other things I'm doing. Why not use existing packages? Well, I've tried, but they seem to not have simple linear algebra things like inverting singular or non-square inverses. Maybe I've just been looking in the wrong places.
Anyway I wrote this algorithm in 1989 using Ada and Fortran77. In 1992 I converted it to C++, and in 2003 I made it a C++ template to be used for absolutely anything that you'd want.
I attempted to convert this to a Java template, but I need to make objects of the parameter type, and without doing some inspection or copy tricks, I can't get there from here. I only use this for doubles anyway, so here's the version that I use. The C++ version is still available.
The matrix inversion was in from the start, as the only really useful part of the Class. I added the bandwidth reduction routine in 1991 - I was stuck in SOS (USAF school) at the time and was thinking about optimizing the bandwidth of a matrix made from a finite-element grid by renumbering the nodes of the grid.
Changes by Holger Arndt: The original code has been adapted for the Universal Java Matrix Package. Methods for different matrix implementations have been added.
Calculation.Ret| Modifier and Type | Method and Description |
|---|---|
static void |
addColTimes(DenseDoubleMatrix2D matrix,
long diag,
long fromRow,
long col,
double factor)
Add a factor times one column to another column
|
static void |
addColTimes(double[][] matrix,
int diag,
int fromRow,
int col,
double factor)
Add a factor times one column to another column
|
static void |
addColTimes(Matrix matrix,
long diag,
long fromRow,
long col,
double factor)
Add a factor times one column to another column
|
static void |
addRowTimes(DenseDoubleMatrix2D matrix,
long diag,
long fromCol,
long row,
double factor)
Add a factor times one row to another row
|
static void |
addRowTimes(double[][] matrix,
int diag,
int fromCol,
int row,
double factor)
Add a factor times one row to another row
|
static void |
addRowTimes(Matrix matrix,
long diag,
long fromCol,
long row,
double factor)
Add a factor times one row to another row
|
static Matrix |
arbitrariness(Matrix source,
Matrix inverse)
Calculate the arbitrariness of the solution.
|
DoubleMatrix |
calcLink() |
DenseDoubleMatrix2D |
calcNew() |
DenseDoubleMatrix2D |
calcOrig() |
static boolean |
canSwapCols(Matrix matrix,
int col1,
int col2,
int row1)
Check to see if columns can be swapped - part of the bandwidth reduction
algorithm.
|
static boolean |
canSwapRows(Matrix matrix,
int row1,
int row2,
int col1)
Check to see if a non-zero and a zero value in the rows leading up to
this column can be swapped.
|
static void |
divideRowBy(DenseDoubleMatrix2D matrix,
long aRow,
long fromCol,
double value)
Divide the row from this column position by this value
|
static void |
divideRowBy(double[][] matrix,
int aRow,
int fromCol,
double value)
Divide the row from this column position by this value
|
static void |
divideRowBy(Matrix matrix,
long aRow,
long fromCol,
double value)
Divide the row from this column position by this value
|
double |
getDouble(long... coordinates) |
static DenseDoubleMatrix2D |
inverse(DenseDoubleMatrix2D matrix)
Same as
inverse(Matrix) but optimized for 2D dense double
matrices |
static DenseDoubleMatrix2D |
inverse(double[][] matrix)
Same as
inverse(Matrix) but optimized for 2D double arrays |
static DenseDoubleMatrix2D |
inverse(Matrix matrix)
Matrix inversion is the reason this Class exists - this method creates a
generalized matrix inverse of the current matrix.
|
static Matrix |
reduce(Matrix source)
Mathematical operator to reduce the bandwidth of a HusoMatrix.
|
static Matrix |
reduce(Matrix source,
Matrix response) |
static void |
swapCols(DenseDoubleMatrix2D matrix,
long col1,
long col2)
Swap components in the two columns.
|
static void |
swapCols(double[][] matrix,
int col1,
int col2)
Swap components in the two columns.
|
static void |
swapCols(Matrix matrix,
long col1,
long col2)
Swap components in the two columns.
|
static void |
swapPivot(DenseDoubleMatrix2D source,
long diag,
DenseDoubleMatrix2D s,
DenseDoubleMatrix2D t)
Swap the matrices so that the largest value is on the pivot
|
static void |
swapPivot(double[][] source,
int diag,
double[][] s,
double[][] t)
Swap the matrices so that the largest value is on the pivot
|
static void |
swapPivot(Matrix source,
long diag,
Matrix s,
Matrix t)
Swap the matrices so that the largest value is on the pivot
|
static void |
swapRows(DenseDoubleMatrix2D matrix,
long row1,
long row2)
Swap components in the two rows.
|
static void |
swapRows(double[][] matrix,
int row1,
int row2)
Swap components in the two rows.
|
static void |
swapRows(Matrix matrix,
long row1,
long row2)
Swap components in the two rows.
|
static DenseDoubleMatrix2D |
times(DenseDoubleMatrix2D matrix1,
DenseDoubleMatrix2D matrix2,
long timesInner)
This routine performs the matrix multiplication.
|
static double[][] |
times(double[][] matrix1,
double[][] matrix2,
int timesInner)
This routine performs the matrix multiplication.
|
getValueType, setDoubleavailableCoordinates, calc, containsCoordinates, getColumnCount, getDimension, getMetaData, getRowCount, getSize, getSource, getSources, setMetaDataclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitavailableCoordinates, calc, containsCoordinates, getColumnCount, getDimension, getMetaData, getRowCount, getSize, getSource, getSources, setMetaDatapublic Ginv(Matrix source)
public static DenseDoubleMatrix2D times(DenseDoubleMatrix2D matrix1, DenseDoubleMatrix2D matrix2, long timesInner)
matrix1 - the first matrixmatrix2 - the second matrixtimesInner - number of rows/columns to processpublic static double[][] times(double[][] matrix1,
double[][] matrix2,
int timesInner)
matrix1 - the first matrixmatrix2 - the second matrixtimesInner - number of rows/columns to processpublic static void swapCols(Matrix matrix, long col1, long col2)
matrix - the matrix to modifycol1 - the first rowcol2 - the second rowpublic static void swapCols(DenseDoubleMatrix2D matrix, long col1, long col2)
matrix - the matrix to modifycol1 - the first rowcol2 - the second rowpublic static void swapCols(double[][] matrix,
int col1,
int col2)
matrix - the matrix to modifycol1 - the first rowcol2 - the second rowpublic static void swapRows(Matrix matrix, long row1, long row2)
matrix - the matrix to modifyrow1 - the first rowrow2 - the second rowpublic static void swapRows(DenseDoubleMatrix2D matrix, long row1, long row2)
matrix - the matrix to modifyrow1 - the first rowrow2 - the second rowpublic static void swapRows(double[][] matrix,
int row1,
int row2)
matrix - the matrix to modifyrow1 - the first rowrow2 - the second rowpublic static DenseDoubleMatrix2D inverse(Matrix matrix)
Matrix inversion is the reason this Class exists - this method creates a generalized matrix inverse of the current matrix. The result is returned in a new matrix.
Matrices may be square, non-square, or singular. The operations will be identical. If the matrix has a single possible inverse, there will be no arbitrariness to the solution. The method here was suggested to me by John Jones Jr. at AFIT in the 1980s, and this algorithm is an original creation of mine to implement his method.
A matrix inverse has some properties described here. Let
A be the original matrix. Let the inverse, as
calculated here be A12 (an inverse with properties 1
and 2 - being left side inverse and right side inverse). An inverse times
the original matrix should yield an identity matrix.
A x = b is the usual equation where
A is the matrix, x is a vector of
the unknowns and b is a vector of the constant
values:
Given these equations:
C x + D y + E z = b1 F x + G y + H z = b1
(The usual programs available don't handle more unknowns than equations, and will stop at this point)
The A matrix is:
| C D E | | F G H |
The x vector is:
| x | | y | | z |
The b vector is: | b1 | | b2 |
A * x = b
The generalized inverse A12 in this case will be of
size (3,2): | J K | | L M | | N P |
The left-hand inverse is defined such that the product of the
(generalized) inverse times the original matrix times the (generalized)
inverse will yield the (generalized) inverse again:
A12 * (A * A12) = A12
The right-hand inverse is defined similarly:
(A * A12) * A = A
If a matrix (A12) meets these criteria, it's
considered to be a generalized inverse of the A
matrix, even though it may not be square, or the product of
A * A12 or A12 * A may not be the
identity matrix! (Won't be if the input A matrix is
non-square or singular)
In the case of (A * A12) being the identity matrix,
the product of (A12 *
A) will also be the identity matrix, and the solution will be
unique: A12 will be the exact and only solution to
the equation.
Refer to http://mjollnir.com/matrix/ for the best description.
matrix - matrix to invertpublic static DenseDoubleMatrix2D inverse(DenseDoubleMatrix2D matrix)
inverse(Matrix) but optimized for 2D dense double
matricesmatrix - the matrix to invertpublic static DenseDoubleMatrix2D inverse(double[][] matrix)
inverse(Matrix) but optimized for 2D double arraysmatrix - the matrix to invertpublic static void addColTimes(Matrix matrix, long diag, long fromRow, long col, double factor)
matrix - the matrix to modifydiag - coordinate on the diagonalfromRow - first row to processcol - column to processfactor - factor to multiplypublic static void addColTimes(double[][] matrix,
int diag,
int fromRow,
int col,
double factor)
matrix - the matrix to modifydiag - coordinate on the diagonalfromRow - first row to processcol - column to processfactor - factor to multiplypublic static void addColTimes(DenseDoubleMatrix2D matrix, long diag, long fromRow, long col, double factor)
matrix - the matrix to modifydiag - coordinate on the diagonalfromRow - first row to processcol - column to processfactor - factor to multiplypublic static void addRowTimes(DenseDoubleMatrix2D matrix, long diag, long fromCol, long row, double factor)
matrix - the matrix to modifydiag - coordinate on the diagonalfromCol - first column to processrow - row to processfactor - factor to multiplypublic static void addRowTimes(double[][] matrix,
int diag,
int fromCol,
int row,
double factor)
matrix - the matrix to modifydiag - coordinate on the diagonalfromCol - first column to processrow - row to processfactor - factor to multiplypublic static void addRowTimes(Matrix matrix, long diag, long fromCol, long row, double factor)
matrix - the matrix to modifydiag - coordinate on the diagonalfromCol - first column to processrow - row to processfactor - factor to multiplypublic static void divideRowBy(Matrix matrix, long aRow, long fromCol, double value)
matrix - the matrix to modifyaRow - the row to processfromCol - starting from columnvalue - the value to dividepublic static void divideRowBy(DenseDoubleMatrix2D matrix, long aRow, long fromCol, double value)
matrix - the matrix to modifyaRow - the row to processfromCol - starting from columnvalue - the value to dividepublic static void divideRowBy(double[][] matrix,
int aRow,
int fromCol,
double value)
matrix - the matrix to modifyaRow - the row to processfromCol - starting from columnvalue - the value to dividepublic static void swapPivot(Matrix source, long diag, Matrix s, Matrix t)
source - the matrix to modifydiag - the position on the diagonals - the matrix st - the matrix tpublic static void swapPivot(DenseDoubleMatrix2D source, long diag, DenseDoubleMatrix2D s, DenseDoubleMatrix2D t)
source - the matrix to modifydiag - the position on the diagonals - the matrix st - the matrix tpublic static void swapPivot(double[][] source,
int diag,
double[][] s,
double[][] t)
source - the matrix to modifydiag - the position on the diagonals - the matrix st - the matrix tpublic static boolean canSwapRows(Matrix matrix, int row1, int row2, int col1)
matrix - the matrix to checkrow1 - the first rowrow2 - the second rowcol1 - the columnpublic static boolean canSwapCols(Matrix matrix, int col1, int col2, int row1)
matrix - the matrix to checkcol1 - the first columncol2 - the second columnrow1 - the rowpublic static Matrix reduce(Matrix source)
public static Matrix arbitrariness(Matrix source, Matrix inverse)
public double getDouble(long... coordinates)
public DoubleMatrix calcLink()
calcLink in interface CalculationcalcLink in class AbstractDoubleCalculationpublic DenseDoubleMatrix2D calcNew()
calcNew in interface CalculationcalcNew in class AbstractDoubleCalculationpublic DenseDoubleMatrix2D calcOrig()
calcOrig in interface CalculationcalcOrig in class AbstractDoubleCalculationCopyright © 2015 Universal Java Matrix Package. All rights reserved.