Class Matrices
- java.lang.Object
-
- no.uib.cipr.matrix.Matrices
-
public final class Matrices extends java.lang.Object
Static utility methods for matrices and vectors
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static int
cardinality(Matrix A)
Returns the number of non-zero entries in the given matrixstatic int
cardinality(Vector x)
Returns the number of non-zero entries in the given vectorstatic int[]
columnBandwidth(Matrix A)
Finds the number of non-zero entries on each columnstatic double[][]
getArray(Matrix A)
Returns an array of arrays containing a copy of the given matrix.static double[]
getArray(Vector x)
Returns a dense array containing a copy of the given vectorstatic int
getNumSubDiagonals(Matrix A)
Finds the number of diagonals below the main diagonal.static int
getNumSuperDiagonals(Matrix A)
Finds the number of diagonals above the main diagonal.static Matrix
getSubMatrix(Matrix A, int[] row, int[] column)
Returns a view into the given matrix.static Vector
getSubVector(Vector x, int[] index)
Returns a view into the given vector.static DenseMatrix
identity(int size)
Returns the identity matrix of the given sizestatic int[]
index(int from, int to)
Creates a continuous linear index.static int[]
index(int from, int stride, int to)
Creates a strided linear index.static Vector
random(int size)
Creates a random vector.static Matrix
random(int numRows, int numColumns)
Creates a random matrix.static Matrix
random(Matrix A)
Populates a matrix with random numbers drawn from a uniform distribution between 0 and 1static Vector
random(Vector x)
Populates a vector with random numbers drawn from a uniform distribution between 0 and 1static int[]
rowBandwidth(Matrix A)
Finds the number of non-zero entries on each rowstatic Matrix
synchronizedMatrix(Matrix A)
Returns a synchronized matrix which wraps the given matrix.static Matrix
synchronizedMatrixByColumns(Matrix A)
Returns a synchronized matrix which wraps the given matrix.static Matrix
synchronizedMatrixByRows(Matrix A)
Returns a synchronized matrix which wraps the given matrix.static Vector
synchronizedVector(Vector x)
Returns a synchronized vector which wraps the given vector.static void
zeroColumns(Matrix A, double diagonal, int... column)
Sets the selected columns ofA
equal zero, and putsdiagonal
on the diagonal of those columns.static void
zeroRows(Matrix A, double diagonal, int... row)
Sets the selected rows ofA
equal zero, and putsdiagonal
on the diagonal of those rows.
-
-
-
Method Detail
-
cardinality
public static int cardinality(Vector x)
Returns the number of non-zero entries in the given vector
-
cardinality
public static int cardinality(Matrix A)
Returns the number of non-zero entries in the given matrix
-
getArray
public static double[][] getArray(Matrix A)
Returns an array of arrays containing a copy of the given matrix. Each array contains one row.
-
getArray
public static double[] getArray(Vector x)
Returns a dense array containing a copy of the given vector
-
identity
public static DenseMatrix identity(int size)
Returns the identity matrix of the given size- Parameters:
size
- Number of rows/columns of the matrix- Returns:
- Matrix of the given size, with ones on the main diagonal
-
random
public static Vector random(int size)
Creates a random vector. Numbers are drawn from a uniform distribution between 0 and 1- Parameters:
size
- Size of the vector
-
random
public static Vector random(Vector x)
Populates a vector with random numbers drawn from a uniform distribution between 0 and 1- Parameters:
x
- Vector to populate
-
random
public static Matrix random(int numRows, int numColumns)
Creates a random matrix. Numbers are drawn from a uniform distribution between 0 and 1- Parameters:
numRows
- Number of rowsnumColumns
- Number of columns
-
random
public static Matrix random(Matrix A)
Populates a matrix with random numbers drawn from a uniform distribution between 0 and 1- Parameters:
A
- Matrix to populate
-
synchronizedVector
public static Vector synchronizedVector(Vector x)
Returns a synchronized vector which wraps the given vector. Only theset(int, double)
andadd(int, double)
methods and their blocked versions are synchronized.Note: Do not use the wrapped vector for any operations besides matrix assembly, as these operations may be very slow.
- Parameters:
x
- Vector to be wrapped- Returns:
- A thin wrapper around
x
-
synchronizedMatrix
public static Matrix synchronizedMatrix(Matrix A)
Returns a synchronized matrix which wraps the given matrix. Only theset(int, int, double)
andadd(int, int, double)
methods and their blocked versions are synchronized.Note: Do not use the wrapped matrix for any operations besides matrix assembly, as these operations may be very slow.
- Parameters:
A
- Matrix to be wrapped- Returns:
- A thin wrapper around
A
-
synchronizedMatrixByRows
public static Matrix synchronizedMatrixByRows(Matrix A)
Returns a synchronized matrix which wraps the given matrix. Only theset(int, int, double)
andadd(int, int, double)
methods and their blocked versions are synchronized.The locking provided is finer than the locking of the whole matrix, as different threads can access different rows simultaneous, while only one thread can access a given row at a time. Use this for row-major matrices, not for column-major matrices.
Note: Do not use the wrapped matrix for any operations besides matrix assembly, as these operations may be very slow.
- Parameters:
A
- Matrix to be wrapped- Returns:
- A thin wrapper around
A
. Individual rows are locked
-
synchronizedMatrixByColumns
public static Matrix synchronizedMatrixByColumns(Matrix A)
Returns a synchronized matrix which wraps the given matrix. Only theset(int, int, double)
andadd(int, int, double)
methods and their blocked versions are synchronized.The locking provided is finer than the locking of the whole matrix, as different threads can access different columns simultaneous, while only one thread can access a given column at a time. Use this for column-major matrices, not for row-major matrices.
Note: Do not use the wrapped matrix for any operations besides matrix assembly, as these operations may be very slow.
- Parameters:
A
- Matrix to be wrapped- Returns:
- A thin wrapper around
A
. Individual columns are locked
-
getSubMatrix
public static Matrix getSubMatrix(Matrix A, int[] row, int[] column)
Returns a view into the given matrix. This view is only for easing some matrix-assembly cases, not for general use. To extract a more higher-performing and general matrix, create a copy of the submatrix. The result is aDenseMatrix
.- Parameters:
A
- Matrix to create view onrow
- Rows to access. Must be within the bounds ofA
column
- Columns to access. Must be within the bounds ofA
- Returns:
- Submatrix of
A
. Changing it will change the backing matrix
-
getSubVector
public static Vector getSubVector(Vector x, int[] index)
Returns a view into the given vector. This view is only for easing some vector-assembly cases, not for general use. To extract a more higher-performing and general vector, create a copy of the subvector. The result is aDenseVector
.- Parameters:
x
- Vector to create view onindex
- Indices to access. Must be within the bounds ofx
- Returns:
- Submatrix of
x
. Changing it will change the backing matrix
-
index
public static int[] index(int from, int to)
Creates a continuous linear index.- Parameters:
from
- Start, inclusiveto
- Stop, exclusive
-
index
public static int[] index(int from, int stride, int to)
Creates a strided linear index.- Parameters:
from
- Start, inclusivestride
-stride=1
for continuous. Negative strides are allowedto
- Stop, exclusive
-
rowBandwidth
public static int[] rowBandwidth(Matrix A)
Finds the number of non-zero entries on each row
-
columnBandwidth
public static int[] columnBandwidth(Matrix A)
Finds the number of non-zero entries on each column
-
getNumSubDiagonals
public static int getNumSubDiagonals(Matrix A)
Finds the number of diagonals below the main diagonal. Useful for converting a general matrix into a banded matrix
-
getNumSuperDiagonals
public static int getNumSuperDiagonals(Matrix A)
Finds the number of diagonals above the main diagonal. Useful for converting a general matrix into a banded matrix
-
zeroRows
public static void zeroRows(Matrix A, double diagonal, int... row)
Sets the selected rows ofA
equal zero, and putsdiagonal
on the diagonal of those rows. Useful for enforcing boundary conditions
-
zeroColumns
public static void zeroColumns(Matrix A, double diagonal, int... column)
Sets the selected columns ofA
equal zero, and putsdiagonal
on the diagonal of those columns. Useful for enforcing boundary conditions
-
-