# Calling matrix multiplication from python

**URL:** <https://forum.opencv.org/t/calling-matrix-multiplication-from-python/10103>\
**Category:** Python\
**Tags:** core\
**Created:** [September 7, 2022, 5:17pm UTC](https://forum.opencv.org/t/calling-matrix-multiplication-from-python/10103 "2022-09-07T17:17:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![hmaarrfk](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/hmaarrfk/32/6000_2.png) [@hmaarrfk](https://forum.opencv.org/u/hmaarrfk)\
**Post date:** [September 7, 2022, 5:17pm UTC](https://forum.opencv.org/t/calling-matrix-multiplication-from-python/10103/1 "2022-09-07T17:17:09Z")

</div>

I’m wondering if there is a way to access the matrix multiply operators from python.

Specifically, the C++ documentation mentions the ability to use `matrix multplication` that is different than `mul`.

[https://docs.opencv.org/4.x/d3/d63/classcv\_1\_1Mat.html#a385c09827713dc3e6d713bfad8460706](https://docs.opencv.org/4.x/d3/d63/classcv_1_1Mat.html#a385c09827713dc3e6d713bfad8460706)

Unfortunately, I cannot find any documentation on the function being exported to the `cv2` namespace in python.

The main advantage I find is it would help bring users to C++ performance to python.

I believe that for my application, I found the `cv2::transform` function,  
[https://docs.opencv.org/4.x/d2/de8/group\_\_core\_\_array.html#ga393164aa54bb9169ce0a8cc44e08ff22](https://docs.opencv.org/4.x/d2/de8/group __core__ array.html#ga393164aa54bb9169ce0a8cc44e08ff22)

Maybe all that is needed a little more cross referencing, but I’m hoping that `matmul` might be a good thing to expose in python:

- Convert to opencv `cv2::Mat`
- Apply the `*` operator
- Return result

Is there a version of the `*` operator that supports an output parameter? Is that `gemm`?

---

<div class="post-metadata">

**Author:** ![laurent.berger](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@laurent.berger](https://forum.opencv.org/u/laurent.berger)\
**Post date:** [September 7, 2022, 7:42pm UTC](https://forum.opencv.org/t/calling-matrix-multiplication-from-python/10103/2 "2022-09-07T19:42:42Z")

</div>

there is no export because numpy is a good library for matrix operations  
If you want in C++:

```auto
    Mat A = (Mat_<float>(2, 3) << 0, 2, 3, 1, 5, 7);
    Mat B = (Mat_<float>(2, 3) << 1, 2, 3, 3, 2, 1);
    Mat C = A.mul(2 / B);

```

python code is

```auto
import numpy as np
A = np.array([[0, 2, 3],[1, 5, 7]], dtype=np.float32)
B = np.array([[1, 2, 3],[3, 2, 1]], dtype=np.float32)
C = A*2/B

```

---

<div class="post-metadata">

**Author:** ![Eduardo](https://avatars.discourse-cdn.com/v4/letter/e/6a8cbe/32.png) [@Eduardo](https://forum.opencv.org/u/Eduardo)\
**Post date:** [September 8, 2022, 12:02pm UTC](https://forum.opencv.org/t/calling-matrix-multiplication-from-python/10103/3 "2022-09-08T12:02:45Z")

</div>

About GEMM and performance, see:

- [Accelerated BLAS/LAPACK libraries in NumPy](https://numpy.org/devdocs/user/building.html#accelerated-blas-lapack-libraries)
- [OpenBLAS](https://github.com/xianyi/OpenBLAS) for an optimized open-source library for BLAS operations

Also, with NumPy do not write iteration loops but rather use the different NumPy functions instead and install a BLAS library.

(Actually it is similar in C++. If you are doing large matrix multiplication, you better have to install a BLAS library like Intel MKL or OpenBLAS.)

---

<div class="post-metadata">

**Author:** ![cudawarped](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@cudawarped](https://forum.opencv.org/u/cudawarped)\
**Post date:** [September 8, 2022, 12:32pm UTC](https://forum.opencv.org/t/calling-matrix-multiplication-from-python/10103/4 "2022-09-08T12:32:27Z")

</div>

You can also call the BLAS routines directly from OpenCV but as previously stated there isn’t any point because to get the same performance as numpy you need to build OpenCv against a BLAS library and as images are stored as numpy arrays in python you can just use numpy. See below for a comparison of GEMM from python

> **[OpenCV MKL/TBB vs cuBLAS - James Bowley](https://jamesbowley.co.uk/opencv-mkl-tbb-vs-cublas/#python_cpu)**
>
> To investigate the impact of building OpenCV with Intel MKL/TBB, I have \[…\]

---

<div class="post-metadata">

**Author:** ![hmaarrfk](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/hmaarrfk/32/6000_2.png) [@hmaarrfk](https://forum.opencv.org/u/hmaarrfk)\
**Post date:** [September 11, 2022, 5:53pm UTC](https://forum.opencv.org/t/calling-matrix-multiplication-from-python/10103/5 "2022-09-11T17:53:37Z")

</div>

Sorry for the late reply on my behalf. this is a new forum for me. I really do appreciate all your replies.

Ultimately, I feel like numpy is well versed for floating point operations, but falls short for integer operations when overflow and memory consumption is a concern (a very common concern for image processing).

Honestly, I didn’t know that numpy was able to use gemm with syntax like:

```auto
npMat3 = npMat4 = npMat5 = npTmp + npTmp*1j
%timeit npMat3.T @ npMat4 + npMat5

```

I’ll have to get back to you all with real benchmarks it seems.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [September 11, 2022, 8:21pm UTC](https://forum.opencv.org/t/calling-matrix-multiplication-from-python/10103/6 "2022-09-11T20:21:27Z")

</div>

if you are concerned about numpy producing a lot of temporary data, just write your kernels in plain python and then use `@numba.njit`

or write OpenCL kernels and run them with pyopencl

or use OpenCV functions, with cv.UMat, but then you’re back to intermediate results for a bunch of stuff, same as with numpy.

I hear there exist GPU-accelerated flavors of numpy too
