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.
Takes a 1d numpy array and creates a csarray with the corresponding diagonal elements.
Parameters: |
|
---|
Create the identity matrix of size n by n.
Parameters: |
|
---|
Create a ones matrix of the given shape and dtype. Generally a bad idea for large matrices.
Parameters: |
|
---|
Create a zeros matrix of the given shape and dtype.
Parameters: |
|
---|
Generate a random sparse matrix with m rows and n cols with given density and dtype.
Parameters: |
|
---|
csarray.transpose(self)
Swap the rows and columns of this matrix, i.e. perform a transpose operation.
Take the ceil of the nonzero elements of this array, and return a new array.
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: |
|
---|
Turn this matrix into compressed sparse format by freeing extra memory space in the buffer.
Return a copy of this array.
Take the cosine of the nonzero elements of this array, and return a new array.
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.
Compute the dot product between this and either a csarray or numpy array A.
Parameters: | A – The input numpy array or csarray. |
---|
csarray.__getDType(self)
Take the floor of the nonzero elements of this array, and return a new array.
Return the number of non-zero elements in the array
Compute the hadamard (element-wise) product between this array and A.
Parameters: | A – The input numpy array or csarray. |
---|
Find the maximum element of this array.
Find the mean value of this array.
Parameters: | axis – The axis of the array to compute the mean. |
---|
Find the minimum element of this array.
Return a tuple of arrays corresponding to nonzero elements.
Return a list such that the ith element is an array of nonzero elements in the ith row of this matrix.
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.
Compute the dot product between this and either a csarray or numpy array using multithreading.
Parameters: | A – The input numpy array or csarray. |
---|
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. |
---|
Suppresses all nonzeros which are much smaller in magnitude than eps under the tolerence precision.
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: |
|
---|
Reserve n nonzero entries and turns the matrix into uncompressed mode.
Parameters: | n (int) – The number of elements of space to reserve. |
---|
Returns the non zero indices for the ith row.
Parameters: | i (int) – The index of the row of the array. |
---|
Take the sign of the nonzero elements of this array, and return a new array.
Take the sine of the nonzero elements of this array, and return a new array.
Return the standard deviation of the array elements.
Return a submatrix of the matrix given by A[startRow:startRows+blockRows, startCol:startCol+blockCols] in an efficient manner.
Parameters: |
|
---|
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. |
---|
Convert this matrix to a scipy sparse matrix. Returns a copy of the data in csc_matrix form.
Convert this matrix to a scipy sparse matrix. Returns a copy of the data in csr_matrix form.
Convert this sparse array into a numpy array.
Returns the trace of the array which is simply the sum of the diagonal entries.
Swap the rows and columns of this matrix, i.e. perform a transpose operation.
Return the values of this object according to the elements returned using nonzero.
Return the variance of the elements of this array.