Compressed Sparse Array

The csarray class represents a Compressed Sparse Array object which is essentially a 2d matrix or 1d vector with few nonzero elements. The underlying implementation uses the Eigen sparse matrix code. A csarray is initially specified using a size, data type (int, float etc.) and storage type (row or column major format).

As an example, a csarray of shape (5, 5) can be created in the following manner:

>>> import numpy
>>> from sppy import csarray
>>> #Create a new column major dynamic array of float type
>>> B = csarray((5, 5), storagetype="col", dtype=numpy.float)
>>> B[3, 3] = -0.2
>>> B[0, 4] = -1.23

Alternatively, one can create a csarray using a numpy array, a scipy sparse matrix, or a csarray:

>>> A = numpy.random.rand(5, 7)
>>> B = csarray(A, storagetype="col")
>>> C = csarray(B, storagetype="row")

In addition, one can generate arrays using a predefined structure, for example diagonal matrices or randomly generated. See the documentation below for details and also for information on the operations on these objects.

Generating Arrays

sppy.diag(a, storagetype='col')

Takes a 1d numpy array and creates a csarray with the corresponding diagonal elements.

Parameters:
  • a (numpy.ndarray) – A 1d numpy array
  • storagetype (str) – The storage type of the csarray (“row” or “col”)
sppy.eye(n, dtype=<type 'float'>)

Create the identity matrix of size n by n.

Parameters:
  • n (int) – The size of the output array
  • dtype – The data type of the output array (e.g. numpy.int)
sppy.ones(shape, dtype=<type 'float'>, storageType='col')

Create a ones matrix of the given shape and dtype. Generally a bad idea for large matrices.

Parameters:
  • shape – The shape of the output array
  • dtype – The data type of the output array (e.g. numpy.int)
  • storagetype (str) – The storage type of the csarray (“row” or “col”)
sppy.zeros(shape, dtype=<type 'float'>, storageType='col')

Create a zeros matrix of the given shape and dtype.

Parameters:
  • shape – The shape of the output array
  • dtype – The data type of the output array (e.g. numpy.int)
  • storagetype (str) – The storage type of the csarray (“row” or “col”)
sppy.rand(shape, density, dtype=<type 'float'>, storagetype='col')

Generate a random sparse matrix with m rows and n cols with given density and dtype.

Parameters:
  • shape – The shape of the output array (m, n)
  • density – The proportion of non zero elements to create
  • dtype – The data type of the output array (only supports floats at the moment)
  • storagetype (str) – The storage type of the csarray (“row” or “col”)

Methods

class sppy.csarray(self, S, dtype=numpy.float, storagetype='col')
T

csarray.transpose(self)

Swap the rows and columns of this matrix, i.e. perform a transpose operation.

ceil(self)

Take the ceil of the nonzero elements of this array, and return a new array.

clip(self, minVal, maxVal)

Given an interval, values outside the interval are clipped to the interval edges. For example, if an interval of [0, 1] is specified, values smaller than 0 become 0, and values larger than 1 become 1.

Parameters:
  • minVal – The minimum value to allow
  • maxVal – The maximum value to allow
compress(self)

Turn this matrix into compressed sparse format by freeing extra memory space in the buffer.

copy(self)

Return a copy of this array.

cos(self)

Take the cosine of the nonzero elements of this array, and return a new array.

diag(self)

Return a numpy array containing the diagonal entries of this matrix. If the matrix is non-square then the diagonal array is the same size as the smallest dimension.

dot(self, A)

Compute the dot product between this and either a csarray or numpy array A.

Parameters:A – The input numpy array or csarray.
dtype

csarray.__getDType(self)

floor(self)

Take the floor of the nonzero elements of this array, and return a new array.

getnnz(self)

Return the number of non-zero elements in the array

hadamard(self, A)

Compute the hadamard (element-wise) product between this array and A.

Parameters:A – The input numpy array or csarray.
max(self)

Find the maximum element of this array.

mean(self, axis=None)

Find the mean value of this array.

Parameters:axis – The axis of the array to compute the mean.
min(self)

Find the minimum element of this array.

nonzero(self)

Return a tuple of arrays corresponding to nonzero elements.

nonzeroRowsList(self)

Return a list such that the ith element is an array of nonzero elements in the ith row of this matrix.

nonzeroRowsPtr(self)

Returns two arrays indPtr, colInds, such that colInds[indPtr[i]:indPtr[i+1]] is the set of nonzero elements in the ith row of this matrix.

pdot(self, A)

Compute the dot product between this and either a csarray or numpy array using multithreading.

Parameters:A – The input numpy array or csarray.
power(self, n)

Returns a new array in which all nonzero elements in the array are raised to the nth power.

Parameters:n – The exponent of the power.
prune(self, double eps=1e-10, double precision=1e-20)

Suppresses all nonzeros which are much smaller in magnitude than eps under the tolerence precision.

put(self, data, rowInds, colInds, init=False)

Put some values into this matrix into the corresponding rowInds and colInds. We have A[rowInds[i], colInds[i]] = data[i]/data. Notice that this is faster if init=True but this setting is to be used only if the matrix has just been created.

Parameters:
  • vals – A scalar or numpy array with the same dimension as rowsInds and colInds
  • rowInds – A 1d numpy array of row indices.
  • colInds – A 1d numpy array of column indices.
reserve(self, int n)

Reserve n nonzero entries and turns the matrix into uncompressed mode.

Parameters:n (int) – The number of elements of space to reserve.
rowInds(self, int i)

Returns the non zero indices for the ith row.

Parameters:i (int) – The index of the row of the array.
sign(self)

Take the sign of the nonzero elements of this array, and return a new array.

sin(self)

Take the sine of the nonzero elements of this array, and return a new array.

std(self)

Return the standard deviation of the array elements.

submatrix(self, startRow, startCol, blockRows, blockCols)

Return a submatrix of the matrix given by A[startRow:startRows+blockRows, startCol:startCol+blockCols] in an efficient manner.

Parameters:
  • startRow (int) – The starting row index
  • startCol (int) – The starting column index
  • blockRows (int) – The number of rows to take.
  • blockCols (int) – The number of columns to take.
sum(self, axis=None)

Sum all of the elements in this array. If one specifies an axis then we sum along the axis.

Parameters:axis – The axis to sum along.
toScipyCsc(self)

Convert this matrix to a scipy sparse matrix. Returns a copy of the data in csc_matrix form.

toScipyCsr(self)

Convert this matrix to a scipy sparse matrix. Returns a copy of the data in csr_matrix form.

toarray(self)

Convert this sparse array into a numpy array.

trace(self)

Returns the trace of the array which is simply the sum of the diagonal entries.

transpose(self)

Swap the rows and columns of this matrix, i.e. perform a transpose operation.

values(self)

Return the values of this object according to the elements returned using nonzero.

var(self)

Return the variance of the elements of this array.

Table Of Contents

Previous topic

Sparse Matrices and Vectors

Next topic

Linear Algebra module

This Page