sparse.sputils

Utility functions for sparse matrix module

Module Contents

Classes

IndexMixin() This class simply exists to hold the methods necessary for fancy indexing.

Functions

upcast(*args) Returns the nearest supported sparse dtype for the
upcast_char(*args) Same as upcast but taking dtype.char as input (faster).
upcast_scalar(dtype,scalar) Determine data type for binary operation between an array of
downcast_intp_index(arr) Down-cast index array to np.intp dtype if it is of a larger dtype.
to_native(A)
getdtype(dtype,a=None,default=None) Function used to simplify argument processing. If ‘dtype’ is not
get_index_dtype(arrays=tuple,maxval=None,check_contents=False) Based on input (integer) arrays a, determine a suitable index data
get_sum_dtype(dtype) Mimic numpy’s casting for np.sum
isscalarlike(x) Is x either a scalar, an array scalar, or a 0-dim array?
isintlike(x) Is x appropriate as an index into a sparse matrix? Returns True
isshape(x) Is x a valid 2-tuple of dimensions?
issequence(t)
ismatrix(t)
isdense(x)
validateaxis(axis)
upcast(*args)

Returns the nearest supported sparse dtype for the combination of one or more types.

upcast(t0, t1, …, tn) -> T where T is a supported dtype

>>> upcast('int32')
<type 'numpy.int32'>
>>> upcast('bool')
<type 'numpy.bool_'>
>>> upcast('int32','float32')
<type 'numpy.float64'>
>>> upcast('bool',complex,float)
<type 'numpy.complex128'>
upcast_char(*args)

Same as upcast but taking dtype.char as input (faster).

upcast_scalar(dtype, scalar)

Determine data type for binary operation between an array of type dtype and a scalar.

downcast_intp_index(arr)

Down-cast index array to np.intp dtype if it is of a larger dtype.

Raise an error if the array contains a value that is too large for intp.

to_native(A)
getdtype(dtype, a=None, default=None)

Function used to simplify argument processing. If ‘dtype’ is not specified (is None), returns a.dtype; otherwise returns a np.dtype object created from the specified dtype argument. If ‘dtype’ and ‘a’ are both None, construct a data type out of the ‘default’ parameter. Furthermore, ‘dtype’ must be in ‘allowed’ set.

get_index_dtype(arrays=tuple, maxval=None, check_contents=False)

Based on input (integer) arrays a, determine a suitable index data type that can hold the data in the arrays.

arrays : tuple of array_like
Input arrays whose types/contents to check
maxval : float, optional
Maximum value needed
check_contents : bool, optional
Whether to check the values in the arrays and not just their types. Default: False (check only the types)
dtype : dtype
Suitable index data type (int32 or int64)
get_sum_dtype(dtype)

Mimic numpy’s casting for np.sum

isscalarlike(x)

Is x either a scalar, an array scalar, or a 0-dim array?

isintlike(x)

Is x appropriate as an index into a sparse matrix? Returns True if it can be cast safely to a machine int.

isshape(x)

Is x a valid 2-tuple of dimensions?

issequence(t)
ismatrix(t)
isdense(x)
validateaxis(axis)
class IndexMixin

This class simply exists to hold the methods necessary for fancy indexing.

_slicetoarange(j, shape)

Given a slice object, use numpy arange to change it to a 1D array.

_unpack_index(index)

Parse index. Always return a tuple of the form (row, col). Where row/col is a integer, slice, or array of integers.

_check_ellipsis(index)

Process indices with Ellipsis. Returns modified index.

_check_boolean(row, col)
_boolean_index_to_array(i)
_index_to_arrays(i, j)