Documentation for ‘block_sparse’ module¶
Documentation for the regrnd model class.
-
class
block_sparse.block_sparse(blocks, nonzero, submatrices, dtype=<type 'numpy.float32'>, row_names=None, col_names=None)[source]¶ Define a block-sparse matrix
Parameters: blocks :
listlist of [row_block_boundaries,col_block_boundaries], where each of row_block_boundaries and col_block_boundaries is a 1D
arrayof integers, beginning with 0, followed by the end boundaries of each block in increasing ordernonzero :
arrayboolean numpy array with number of rows equal to number of row blocks, and number of columns equal to number of col blocks. If entry [i,j] of nonzero is True, then the corresponding block is non-zero; if it is False, then the corresponding block is zero.
submatrices :
listlist of submatrices for non-zero blocks in row-major order; e.g., block (1,1), (1,2), (2,1), (2,2),... Each submatrix can be an
array, ablock_sparsematrix, or asymmetric_block_sparsematrix.dtype : numpy data type object
Set the default data type for the submatrices. Default
float32row_names :
arraynumpy array with names of the row-blocks. Default
Nonecol_names :
arraynumpy array with names of the col-blocks. Default
NoneReturns: matrix :
block_sparseblock-sparse matrix
Methods
add(A)Matrix addition of a matrix A to current matrix dot(A)Right multiply the current matrix with another block_sparsematrix,symmetric_block_sparsematrix, orarray, A.frobenius(A)Compute the frobenius inner product between the current matrix and matrix A get_submatrix(block)Retrieve a particular block of the matrix get_type(block)Retrieve the type of a particular block of the matrix norm()Compute the frobenius norm of the current matrix qform(y[, z])Computes quadratic form defined by current matrix and input vectors. to_dense()Return the current matrix as a standard (dense) numpy array transpose()Return the transpose of the block-sparse matrix -
add(A)[source]¶ Matrix addition of a matrix A to current matrix
Parameters: A : matrix
matrix A with same dimensions as current matrix. The matrix A can be an
array,block_sparsematrix, orsymmetric_block_sparsematrix. It must have the same block structure as the current matrix if the matrix is ablock_sparsematrix orsymmetric_block_sparsematrix.Returns: the block-sparse matrix formed by matrix addition of the current matrix to A
-
dot(A)[source]¶ Right multiply the current matrix with another
block_sparsematrix,symmetric_block_sparsematrix, orarray, A.Parameters: A : matrix
matrix A with compatible dimensions and block structure: i.e. the row blocks of A must match the column blocks of the current matrix, unless A is an
array.Returns: the block-sparse matrix formed by right multiplication of the current matrix by A
-
frobenius(A)[source]¶ Compute the frobenius inner product between the current matrix and matrix A
Parameters: A : matrix
matrix A with same dimensions as current matrix. The matrix A can be an
array,block_sparsematrix, orsymmetric_block_sparsematrix. It must have the same block structure as the current matrix if the matrix is ablock_sparsematrix orsymmetric_block_sparsematrix.Returns: the frobenius inner product between the current matrix and matrix A
-
get_submatrix(block)[source]¶ Retrieve a particular block of the matrix
Parameters: block :
tupletuple (i,j) giving the index of the block
Returns: block
either an
array, ablock_sparsematrix, or asymmetric_block_sparsematrix.
-
get_type(block)[source]¶ Retrieve the type of a particular block of the matrix
Parameters: block :
tupletuple (i,j) giving the index of the block
Returns: block type
either
array,block_sparse, orsymmetric_block_sparse.
-
norm()[source]¶ Compute the frobenius norm of the current matrix
Returns: the frobenius norm of the current matrix
-
qform(y, z=None)[source]¶ Computes quadratic form defined by current matrix and input vectors. Let X be the current
block_sparsematrix, and y and z column vectors. When it is defined, this computes the quadratic form y’Xz. If only y is provided, this computes the quadratic form y’Xy.Parameters: y :
array1D numpy array of same length as number of rows of current matrix
- z :
array 1D numpy array of same length as number of rows of current matrix. Default
None.
Returns: the value of the quadratic form y’Xz
- z :
-
transpose()[source]¶ Return the transpose of the block-sparse matrix
Returns: block_sparse
-
-
class
block_sparse.symmetric_block_sparse(blocks, nonzero, submatrices, dtype=<type 'numpy.float32'>, row_names=None, col_names=None)[source]¶ Define a symmetric block-sparse matrix. Inherits some methods from
block_sparse.Parameters: blocks :
array1D numpy integer array, starting at zero, followed by block boundaries, which are the same for both rows and columns
nonzero :
arraysymmetric boolean numpy array with number of rows equal to number of row blocks, which is equal to the number of col blocks. If entry [i,j] of nonzero is True, then the corresponding block is non-zero; if it is False, then the corresponding block is zero.
submatrices :
listlist of submatrices for non-zero blocks in row-major order, ignoring lower-triangular blocks; e.g., block (1,1), (1,2), (2,2),... Each submatrix can be a
array, ablock_sparsematrix, or asymmetric_block_sparsematrix.dtype : numpy data type object
Set the default data type for the submatrices. Default
float32row_names :
arraynumpy array with names of the row-blocks. Default
Nonecol_names :
arraynumpy array with names of the col-blocks. Default
NoneReturns: block-sparse matrix
Methods
add(A)Matrix addition of a matrix A to current matrix. dot(A)Right multiply the current matrix with another block_sparsematrix,symmetric_block_sparsematrix, orarray, A.frobenius(A)Compute the frobenius inner product between the current matrix and matrix A get_submatrix(block)Retrieve a particular block of the matrix get_type(block)Retrieve the type of a particular block of the matrix norm()Compute the frobenius norm of the current matrix qform(y[, z])Let X be the current symmetric_block_sparsematrix, and y and z column vectors.to_dense()Return the current matrix as a standard (dense) numpy array transpose()Return the transpose of the symmetric block-sparse matrix -
add(A)[source]¶ Matrix addition of a matrix A to current matrix.
Parameters: A : matrix
matrix A with same dimensions as current matrix. The matrix A can be a
array,block_sparsematrix, orsymmetric_block_sparsematrix. It must have the same block structure as the current matrix if the matrix is ablock_sparsematrix orsymmetric_block_sparsematrix.Returns: matrix
If A is
symmetric_block_sparse, returns asymmetric_block_sparsematrix. Otherwise, returns ablock_sparsematrix.
-
get_submatrix(block)[source]¶ Retrieve a particular block of the matrix
Parameters: block :
tupletuple (i,j) giving the index of the block
Returns: block
either a
array, ablock_sparsematrix, or asymmetric_block_sparsematrix.
-
get_type(block)[source]¶ Retrieve the type of a particular block of the matrix
Parameters: block :
tupletuple (i,j) giving the index of the block
Returns: block type
either
array,block_sparsematrix, orsymmetric_block_sparsematrix.
-
qform(y, z=None)[source]¶ Let X be the current
symmetric_block_sparsematrix, and y and z column vectors. When it is defined, this computes the quadratic form y’Xz. If only y is provided, this computes the quadratic form y’Xy.Parameters: y :
array1D numpy array of same length as number of rows of current matrix
- z :
array 1D numpy array of same length as number of rows of current matrix. Default
None.
Returns: the value of the quadratic form y’Xz
- z :
-
-
block_sparse.matmul(X, A)[source]¶ Matrix multiplication between
block_sparseandsymmetric_block_sparsematrices, as well as matrix multiplication between ablock_sparseorsymmetric_block_sparsematrix and anarray.Parameters: X : matrix
The matrix X can be a
block_sparsematrix, asymmetric_block_sparsematrix, or aarray.A : matrix
The matrix A can be a
block_sparsematrix, asymmetric_block_sparsematrix, or aarray. Note that the number of rows of A must match the number of columns of X. Furthermore, if X and A are bothblock_sparseorsymmetric_block_sparse, then the column blocks of X must match the row blocks of A.Returns: the block-sparse matrix formed by matrix multiplication XA
-
block_sparse.dense_to_block_sparse(dense, blocks, symmetric, dtype=<type 'numpy.float64'>)[source]¶ Convert a standard (dense) numpy array into a
block_sparseor asymmetric_block_sparsematrix. Note this simply imposes a block structure onto the matrix so that it can interact with other block matrices. It does not take advantage of any sparsity in the input matrix.Parameters: dense :
arrayinput matrix
- blocks :
list list of [row_block_boundaries,col_block_boundaries], where each of row_block_boundaries and col_block_boundaries is a 1D
arrayof integers, beginning with 0, followed by the end boundaries of each block in increasing order- symmetric :
bool if True, returns a
symmetric_block_sparsematrix; if False, returns ablock_sparsematrix- dtype : numpy data type
the default data type of the returned matrix
Returns: matrix
the current matrix as a
block_sparseor asymmetric_block_sparsematrix- blocks :