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 amutiply()
method.- Returns:
product (
object
) – The product of all input objects as returned by the objects’mutiply()
method.- Raises:
ValueError – If
mdtools.scipy_helper_functions.multiple_multiply()
is called without arguments.AttributeError – If the input ojects don’t have a
mutiply()
method.
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, butmultiple_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]])