min.huang
min.huang

Reputation: 21

multiply() in numpy python

It seems to me there are two versions of numpy function multiply():

  1. c = multiply( a, b )
  2. multiply(a, b, c )

My questions is two fold:

  1. What is the difference between the two versions?
  2. I need to use dot() function, and I know c = dot( a, b) works. But dot(a, b, c) does not.

Upvotes: 2

Views: 690

Answers (2)

Sven Marnach
Sven Marnach

Reputation: 601649

  1. The difference between the two versions of multiply():

    c = multilpy(a, b)
    

    multiplies the arrays a and b element-wise, creating a new array as result. The name c is bound to this new array. If c pointed to a different array before, this might or might not trigger garbage collection of the array c pointed to before, depending on whether other references to this array still exist.

    multilpy(a, b, c)
    

    multiplies the arrays a and b element-wise, storing the result in the existing array c (which must have suitable dimensions). No new array object is created, but rather an existing array is changed. Apart from the different semantics, this variant is faster if c already points to an array of suitable type and dimension, because no new array is allocated. This variant also can reduce memory usage.

  2. Isn't actually a question. Yes, dot() does not have a three parameter form. It isn't a ufunc and doesn't follow the usual broadcasting rules -- it can't because of the semantics of a dot product.

    Edit: Starting with NumPy 1.6, dot() actually does have a three-parameter form with similar semantics as explained above. (For what it's worth, it is still not a ufunc.)

Upvotes: 6

talonmies
talonmies

Reputation: 72349

There is only one version of all of the standard numpy ufuncs -- using dot as an example

Type:           builtin_function_or_method
Base Class:     <type 'builtin_function_or_method'>
String Form:    <built-in function dot>
Namespace:      Interactive
Docstring:
    dot(a, b, out=None)

Dot product of two arrays.

For 2-D arrays it is equivalent to matrix multiplication, and for 1-D
arrays to inner product of vectors (without complex conjugation). For
N dimensions it is a sum product over the last axis of `a` and
the second-to-last of `b`::

    dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m])

Parameters
----------
a : array_like
    First argument.
b : array_like
    Second argument.
out : ndarray, optional
    Output argument. This must have the exact kind that would be returned
    if it was not used. In particular, it must have the right type, must be
    C-contiguous, and its dtype must be the dtype that would be returned
    for `dot(a,b)`. This is a performance feature. Therefore, if these
    conditions are not met, an exception is raised, instead of attempting
    to be flexible.

If you provide the optional third agrument with the correct dtype and storage order, it will work:

In [78]: a=np.array([1.,2.,3.,4.])

In [79]: b=np.diag(a)

In [80]: c=np.empty_like(a)

In [81]: np.dot(a,b,c)
Out[81]: array([  1.,   4.,   9.,  16.])

In [82]: np.dot(a,b)
Out[82]: array([  1.,   4.,   9.,  16.])

Upvotes: 1

Related Questions