# multiply a matrix by a vector in the Python Calculator filter

**URL:** https://discourse.paraview.org/t/multiply-a-matrix-by-a-vector-in-the-python-calculator-filter/12711
**Category:** ParaView Support
**Tags:** python
**Created:** [August 17, 2023, 5:03pm UTC](https://discourse.paraview.org/t/multiply-a-matrix-by-a-vector-in-the-python-calculator-filter/12711 "2023-08-17T17:03:02Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Andy\_Bauer](https://discourse.paraview.org/user_avatar/discourse.paraview.org/andy_bauer/32/5442_2.png) [@Andy\_Bauer](https://discourse.paraview.org/u/Andy_Bauer)
#### Post date: [August 17, 2023, 5:03pm UTC](https://discourse.paraview.org/t/multiply-a-matrix-by-a-vector-in-the-python-calculator-filter/12711/1 "2023-08-17T17:03:03Z")

</div>

How do I multiply a matrix (array with number of components = 9) with a vector (array with number of components = 3) in the Python Calculator? I have a somewhat more complex expression that I want to do but it’s failing in the matrix vector multiplication. I’ve tried:

- matrix\*vector – this results in a 9 component array
- multiply(matrix,vector) – also results in a 9 component array
- numpy.matmul(matrix,vector) – results in an error message about unaligned shapes – ValueError: shapes (9261,3,3) and (9261,3) not aligned: 3 (dim 2) != 9261 (dim 0)

Thanks,  
Andy

---

<div class="post-metadata">

### Author: ![Christos\_Tsolakis](https://discourse.paraview.org/user_avatar/discourse.paraview.org/christos_tsolakis/32/4450_2.png) [@Christos\_Tsolakis](https://discourse.paraview.org/u/Christos_Tsolakis)
#### Post date: [August 17, 2023, 5:43pm UTC](https://discourse.paraview.org/t/multiply-a-matrix-by-a-vector-in-the-python-calculator-filter/12711/2 "2023-08-17T17:43:17Z")

</div>

I think you need `dot(matrix,vector)` in this case.

---

<div class="post-metadata">

### Author: ![wascott](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/w/8e8cbc/32.png) [@wascott](https://discourse.paraview.org/u/wascott)
#### Post date: [August 17, 2023, 5:50pm UTC](https://discourse.paraview.org/t/multiply-a-matrix-by-a-vector-in-the-python-calculator-filter/12711/3 "2023-08-17T17:50:14Z")

</div>

OK, that’s weird. Wouldn’t that return a dot product (i.e., a scalar)?

---

<div class="post-metadata">

### Author: ![Andy\_Bauer](https://discourse.paraview.org/user_avatar/discourse.paraview.org/andy_bauer/32/5442_2.png) [@Andy\_Bauer](https://discourse.paraview.org/u/Andy_Bauer)
#### Post date: [August 17, 2023, 5:57pm UTC](https://discourse.paraview.org/t/multiply-a-matrix-by-a-vector-in-the-python-calculator-filter/12711/4 "2023-08-17T17:57:23Z")

</div>

Nope, not dot(matrix, vector) either. I tried that and got the following:

```auto
Traceback (most recent call last):
  File "/home/acbauer/Code/ParaView/debug/lib64/python3.6/site-packages/paraview/detail/calculator.py", line 212, in execute
    retVal = compute(inputs, expression, ns=variables)
  File "/home/acbauer/Code/ParaView/debug/lib64/python3.6/site-packages/paraview/detail/calculator.py", line 150, in compute
    retVal = eval(subEx, globals(), mylocals)
  File "<string>", line 1, in <module>
  File "/home/acbauer/Code/ParaView/debug/lib64/python3.6/site-packages/vtkmodules/numpy_interface/algorithms.py", line 99, in new_dfunc
    return apply_dfunc(dfunc, array1, val2)
  File "/home/acbauer/Code/ParaView/debug/lib64/python3.6/site-packages/vtkmodules/numpy_interface/algorithms.py", line 91, in apply_dfunc
    return dfunc(l[0], l[1])
  File "/home/acbauer/Code/ParaView/debug/lib64/python3.6/site-packages/vtkmodules/numpy_interface/internal_algorithms.py", line 292, in dot
    if a1.DataSet == a2.DataSet : va.DataSet = a1.DataSet
AttributeError: 'numpy.ndarray' object has no attribute 'DataSet'

```

---

<div class="post-metadata">

### Author: ![Christos\_Tsolakis](https://discourse.paraview.org/user_avatar/discourse.paraview.org/christos_tsolakis/32/4450_2.png) [@Christos\_Tsolakis](https://discourse.paraview.org/u/Christos_Tsolakis)
#### Post date: [August 17, 2023, 5:58pm UTC](https://discourse.paraview.org/t/multiply-a-matrix-by-a-vector-in-the-python-calculator-filter/12711/5 "2023-08-17T17:58:16Z")

</div>

It would be a scalar if both components were vectors ( 3 components). If I am not wrong the convention comes from numpy [itself](https://numpy.org/doc/stable/reference/generated/numpy.dot.html)

> - If _a_ is an N-D array and _b_ is a 1-D array, it is a sum product over the last axis of _a_ and _b_.

But I agree it is not intuitive or expected unless you have used numpy before.

---

<div class="post-metadata">

### Author: ![Christos\_Tsolakis](https://discourse.paraview.org/user_avatar/discourse.paraview.org/christos_tsolakis/32/4450_2.png) [@Christos\_Tsolakis](https://discourse.paraview.org/u/Christos_Tsolakis)
#### Post date: [August 17, 2023, 6:01pm UTC](https://discourse.paraview.org/t/multiply-a-matrix-by-a-vector-in-the-python-calculator-filter/12711/6 "2023-08-17T18:01:20Z")

</div>

Hm not sure what is wrong. I just tried the following:

- Create fast uniform grid source
- Python Calculator with expression `strain(Swirl)` to get a matrix (Array Name = M)
- Python Calculator with expression `dot(M,Swirl)`

---

<div class="post-metadata">

### Author: ![Andy\_Bauer](https://discourse.paraview.org/user_avatar/discourse.paraview.org/andy_bauer/32/5442_2.png) [@Andy\_Bauer](https://discourse.paraview.org/u/Andy_Bauer)
#### Post date: [August 17, 2023, 6:08pm UTC](https://discourse.paraview.org/t/multiply-a-matrix-by-a-vector-in-the-python-calculator-filter/12711/7 "2023-08-17T18:08:32Z")

</div>

I think there are multiple bugs in the `Python Calculator`. I think there’s matrix transpose that shouldn’t be happening – [https://gitlab.kitware.com/vtk/vtk/-/merge\_requests/10414](https://gitlab.kitware.com/vtk/vtk/-/merge_requests/10414). I’m a bit concerned about how this works with AOS arrays too. I haven’t gotten there yet to verify functionality though.

---

<div class="post-metadata">

### Author: ![Andy\_Bauer](https://discourse.paraview.org/user_avatar/discourse.paraview.org/andy_bauer/32/5442_2.png) [@Andy\_Bauer](https://discourse.paraview.org/u/Andy_Bauer)
#### Post date: [August 17, 2023, 6:20pm UTC](https://discourse.paraview.org/t/multiply-a-matrix-by-a-vector-in-the-python-calculator-filter/12711/8 "2023-08-17T18:20:23Z")

</div>

Hmm, more weirdness – doing “mag(matrix)” results in a vector. I’m not sure that is what’s expected either.

---

<div class="post-metadata">

### Author: ![Andy\_Bauer](https://discourse.paraview.org/user_avatar/discourse.paraview.org/andy_bauer/32/5442_2.png) [@Andy\_Bauer](https://discourse.paraview.org/u/Andy_Bauer)
#### Post date: [August 17, 2023, 7:54pm UTC](https://discourse.paraview.org/t/multiply-a-matrix-by-a-vector-in-the-python-calculator-filter/12711/9 "2023-08-17T19:54:53Z")

</div>

For the original issue of matrix-vector products it looks like I can use np.einsum() – `np.einsum('...jk,...j', matrix, vector)`
