multiple_multiply

mdtools.scipy_helper_functions.multiple_multiply(*args)[source]

Multiply multiple objects that have a mutiply() method at once.

Parameters:

args (object) – Arbitrary number of objects that should be multiplied. The objects must have a mutiply() method.

Returns:

product (object) – The product of all input objects as returned by the objects’ mutiply() method.

Raises:

Notes

This function was originally designed to multiply multiple SciPy sparse matrices at once element-wise. With e.g. scipy.sparse.csr_matrix.multiply() you can multiply only two matrices at once, but multiple_multiply() takes an arbitrary number of arguments.

Examples

>>> from scipy import sparse
>>> a = np.arange(6).reshape(2,3)
>>> a = sparse.csr_matrix(a)
>>> a.toarray()
array([[0, 1, 2],
       [3, 4, 5]])
>>> mdt.sph.multiple_multiply(a).toarray()
array([[0, 1, 2],
       [3, 4, 5]])
>>> mdt.sph.multiple_multiply(a, a).toarray()
array([[ 0,  1,  4],
       [ 9, 16, 25]])
>>> mdt.sph.multiple_multiply(a, a, a).toarray()
array([[  0,   1,   8],
       [ 27,  64, 125]])
>>> mdt.sph.multiple_multiply(*[a,]*5).toarray()
array([[   0,    1,   32],
       [ 243, 1024, 3125]])