no.uib.cipr.matrix
Class Matrices

java.lang.Object
  extended by no.uib.cipr.matrix.Matrices

public final class Matrices
extends java.lang.Object

Static utility methods for matrices and vectors


Method Summary
static int cardinality(Matrix A)
          Returns the number of non-zero entries in the given matrix
static int cardinality(Vector x)
          Returns the number of non-zero entries in the given vector
static int[] columnBandwidth(Matrix A)
          Finds the number of non-zero entries on each column
static 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 vector
static 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 size
static 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 1
static Vector random(Vector x)
          Populates a vector with random numbers drawn from a uniform distribution between 0 and 1
static int[] rowBandwidth(Matrix A)
          Finds the number of non-zero entries on each row
static 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 of A equal zero, and puts diagonal on the diagonal of those columns.
static void zeroRows(Matrix A, double diagonal, int... row)
          Sets the selected rows of A equal zero, and puts diagonal on the diagonal of those rows.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

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 rows
numColumns - 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 the set(int, double) and add(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 the set(int, int, double) and add(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 the set(int, int, double) and add(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 the set(int, int, double) and add(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 a DenseMatrix.

Parameters:
A - Matrix to create view on
row - Rows to access. Must be within the bounds of A
column - Columns to access. Must be within the bounds of A
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 a DenseVector.

Parameters:
x - Vector to create view on
index - Indices to access. Must be within the bounds of x
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, inclusive
to - Stop, exclusive

index

public static int[] index(int from,
                          int stride,
                          int to)
Creates a strided linear index.

Parameters:
from - Start, inclusive
stride - stride=1 for continuous. Negative strides are allowed
to - 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 of A equal zero, and puts diagonal 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 of A equal zero, and puts diagonal on the diagonal of those columns. Useful for enforcing boundary conditions