0

I have defined couple of arrays in Python but I am having problem in calculation of the product.

import numpy as np
phi = np.array([[ 1.,  1.],[ 0.,  1.]])
P = np.array([[ 999.,    0.],[   0.,  999.]])
np.dot(phi, P, phi.T)

I get the error: ValueError: output array is not acceptable (must have the right type, nr dimensions, and be a C-Array)

But I do not know what is the problem, since the size of matrix or array is 2 by 2

3 Answers3

4

As the documentation explains, numpy.dot only multiplies two matrices. The third, optional argument is an array in which to store the results. If you want to multiply three matrices, you will need to call dot twice:

numpy.dot(numpy.dot(phi, P), phi.T)

Note that arrays have a dot method that does the same thing as numpy.dot, which can make things easier to read:

phi.dot(P).dot(phi.T)
user2357112
  • 260,549
  • 28
  • 431
  • 505
1

phi.T is the same as phi.transpose() (as stated in the docs). It is basically a return value of a class method. Therefore you can't use it as an output storage for the dot product.

Update
It appears that there is an additional problem here, that can be seen if saving the transposed matrix into new variable and using it as an output:

>>> g = phi.T
>>> np.dot(phi, P, g)

is still giving an error. The problem seem to be with the way the result of transpose is stored in the memory. The output parameter for the dot product has to be C-contiguous array, but in this case g is not like that. To overcome this issue the numpy.ascontiguousarray method can be used, which solves the problem:

>>> g = np.ascontiguousarray(phi.T)
>>> np.dot(phi, P, g)
array([[ 999.,  999.],
       [   0.,  999.]])
Eugene Sh.
  • 17,802
  • 8
  • 40
  • 61
  • I stored the transpose matrix in another variable like g and after that I executed np.dot(phi, P, g) but I am still getting error. So I guess thats not the problem. – Cristopher Van Paul Apr 02 '15 at 18:28
  • I am sure, its not about transpose matrix. Try this: a=np.array([[1,2],[3,4]]) b=np.array([[1,1],[1,0]]) c=np.array([[1.5,5],[4,1]]) and later try np.dot(a,b,c). You will get that error again. – Cristopher Van Paul Apr 02 '15 at 18:34
1

The error message points that there can be 3 reasons why it cannot perform np.dot(phi, P, out=phi.T):

  1. "must have the right type": That is ok in the first example, since all the elements of P and phi are floating numbers. But not with the other example mentioned at the comments, where the c[0,0] element is floating point number, but the output array wants to be an integer at all positions since both 'a' and 'b' contains integers everywhere.

  2. "nr dimensions":2x2 is the expected dimension of the output array, so the problem is definitely not with the dimensions.

  3. "must be a C-Array": This actually means that the output array must be C-contingous. There is a very good description what actually C and F contingous mean: difference between C and F contingous arrays. To make the long story short, if phi is C-contingous (and by default it is) than phi.T will be F-contingous.

You can check it by checking the flag attributes:

    >>> phi.flags
    C_CONTIGUOUS : True
    F_CONTIGUOUS : False
    OWNDATA : True
    ...

    >>> phi.T.flags
    C_CONTIGUOUS : False
    F_CONTIGUOUS : True
    OWNDATA : False
    ...
Community
  • 1
  • 1
tarpista
  • 99
  • 10