Home > Back-end >  Python numpy matrix multiplication mismatch in core dimension
Python numpy matrix multiplication mismatch in core dimension

Time:10-29

I am trying to matrix multiply a 2x2 matrix with a 2x1 matrix. Both matrices have entries which are linspaces such that the resulting 2x1 matrix gives me a value for each value of the linspace.

I get this dimensionality error however.

matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 1 is different from 2)

For readability I am not posting the whole code but what's necessary.

I have also replaced linspace values with indicative text.

Matrix "L" is a result of other 2x2 multiplications which contain constants, thus no errors there.

The matrix B (2x2) gives the desired result, so the problem comes down to the multiplication between B and C.

import numpy as np
from sympy import *

# Defining range of values
z = np.linspace(initial, final, 10)
g = np.linspace(initial, final, 10)
y = np.linspace(initial, final, 10)

# Matrix operations
A = np.array([[1, z], [0, 1]], dtype=object)
B = np.matmul(L,A)
C = np.array([[y],[g]])

D = np.matmul(B, C)

print(total)

An alternative POV of what I am trying to do, is that for the matrix "B" when multiplied with the 2x1 "C" which contains unknowns, to calculate those unknowns "y" and "g"

Many thanks,

P.S; For an array "C" with single value entries, the multiplication runs as expected.

Edit; As per mozway's suggestion, I am providing the prints of array "A" and "M" which will make stuff clearer, but let M = B

enter image description here

CodePudding user response:

Not sure what you're trying to do (you should provide a reproducible example, there are currently many missing variables), and the expected output.

Nevertheless, the definition of A is fundamentally wrong. I imagine you expect a 2x2 array, but as z is a (10,) shaped array, you will end up with A being a weird object array whose element (0,1) is an array.

This prevents you do to any further mathematical operation.

CodePudding user response:

In [66]: initial, final = 0,1
In [67]: z = np.linspace(initial,final,11)
In [68]: z
Out[68]: array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ])

A is (2,2), but contains a mix of array and scalars

In [69]: A = np.array([[1,z],[0,1]], object)
In [70]: A
Out[70]: 
array([[1,
        array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ])],
       [0, 1]], dtype=object)
In [71]: A.shape
Out[71]: (2, 2)

Now make a (2,2) numeric array:

In [72]: L = np.eye(2)
In [75]: L[1,1] = 2
In [76]: np.matmul(L,A)
Out[76]: 
array([[1.0,
        array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ])],
       [0.0, array([2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2.])]],
      dtype=object)

matmul does work with object dtype arrays, provided the elements implement the necessary and *. The result is still (2,2), but the (1,1) term 2*z.

Now for the C:

In [77]: C = np.array([[z],[z]])
In [78]: C
Out[78]: 
array([[[0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ]],

       [[0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ]]])
In [79]: C.shape
Out[79]: (2, 1, 11)

This is float dtype, 3d array.

In [81]: B=Out[76]
In [82]: np.matmul(B,C)
Traceback (most recent call last):
  File "<ipython-input-82-5eababb7341e>", line 1, in <module>
    np.matmul(B,C)
ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 1 is different from 2)

In [83]: B.shape
Out[83]: (2, 2)
In [84]: C.shape
Out[84]: (2, 1, 11)

There's a mismatch in shapes. But change C definition so it is a 2d array:

In [85]: C = np.array([z,z])
In [86]: C.shape
Out[86]: (2, 11)
In [87]: np.matmul(B,C)
Out[87]: 
array([[array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]),
        array([0.1 , 0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.2 ]),
 ...
        array([1.8, 1.8, 1.8, 1.8, 1.8, 1.8, 1.8, 1.8, 1.8, 1.8, 1.8]),
        array([2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2.])]],
      dtype=object)
In [88]: _.shape
Out[88]: (2, 11)

Here the (2,2) B matmuls with (2,11) just fine producing (2,11). But each element is itself a (11,) array - because of the z used in defining A.

But you say you want a (2,1) C. To get that we have to use:

In [91]: C = np.empty((2,1), object)
In [93]: C[:,0]=[z,z]
In [94]: C
Out[94]: 
array([[array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ])],
       [array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ])]],
      dtype=object)

Be very careful when trying to create object dtype arrays. Things might not be what you expect.

Now matmul of (2,2) with (2,1) => (2,1), object dtype

In [95]: D = np.matmul(B,C)
In [96]: D.shape
Out[96]: (2, 1)
In [99]: D
Out[99]: 
array([[array([0.  , 0.11, 0.24, 0.39, 0.56, 0.75, 0.96, 1.19, 1.44, 1.71, 2.  ])],
       [array([0. , 0.2, 0.4, 0.6, 0.8, 1. , 1.2, 1.4, 1.6, 1.8, 2. ])]],
      dtype=object)

Keep in mind that matmul is very fast with working with numeric dtype arrays. It does work with object dtype arrays, but speed is much slower, more like using list comprehensions.

  • Related