# Will the matmul method be implemented in jitclass?

**URL:** <https://numba.discourse.group/t/will-the-matmul-method-be-implemented-in-jitclass/1860>\
**Category:** Development\
**Created:** [March 27, 2023, 1:22pm UTC](https://numba.discourse.group/t/will-the-matmul-method-be-implemented-in-jitclass/1860 "2023-03-27T13:22:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![louisamand](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/louisamand/32/810_2.png) [@louisamand](https://numba.discourse.group/u/louisamand)\
**Post date:** [March 27, 2023, 1:22pm UTC](https://numba.discourse.group/t/will-the-matmul-method-be-implemented-in-jitclass/1860/1 "2023-03-27T13:22:07Z")

</div>

Will the ` __matmul__ ` method be implemented in `jitclass` in a future release ?

---

<div class="post-metadata">

**Author:** ![gmarkall](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/gmarkall/32/42_2.png) [@gmarkall](https://numba.discourse.group/u/gmarkall)\
**Post date:** [March 28, 2023, 8:47am UTC](https://numba.discourse.group/t/will-the-matmul-method-be-implemented-in-jitclass/1860/2 "2023-03-28T08:47:06Z")

</div>

If you edit the list of supported dunders to include it, does it “just work”:

> <https://github.com/numba/numba/blob/24b7628e954e1862889fa10caccaaa0d853d5e0a/numba/experimental/jitclass/boxing.py#L100>

?

---

<div class="post-metadata">

**Author:** ![louisamand](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/louisamand/32/810_2.png) [@louisamand](https://numba.discourse.group/u/louisamand)\
**Post date:** [March 29, 2023, 9:06am UTC](https://numba.discourse.group/t/will-the-matmul-method-be-implemented-in-jitclass/1860/3 "2023-03-29T09:06:44Z")

</div>

Thanks a lot, it does indeed work ! (for reflected dunders ` __r...__ ` too)

Just out of curiosity/ignorance, what is needed for dunders to be officially supported ?

---

<div class="post-metadata">

**Author:** ![gmarkall](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/gmarkall/32/42_2.png) [@gmarkall](https://numba.discourse.group/u/gmarkall)\
**Post date:** [March 29, 2023, 8:12pm UTC](https://numba.discourse.group/t/will-the-matmul-method-be-implemented-in-jitclass/1860/4 "2023-03-29T20:12:03Z")

</div>

It would need the above change making to the dunders list, plus an addition to the tests to test it and an update to the documentation - would you be keen to make a PR that makes this change an adds tests / docs?

You should be able to just augment the existing jitclass dunder method tests with an extra case for ` __matmul__ `, but let me know if you attempt this and are having trouble seeing where to make the addition.

---

<div class="post-metadata">

**Author:** ![jsbryaniv](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/jsbryaniv/32/815_2.png) [@jsbryaniv](https://numba.discourse.group/u/jsbryaniv)\
**Post date:** [April 7, 2023, 12:55am UTC](https://numba.discourse.group/t/will-the-matmul-method-be-implemented-in-jitclass/1860/5 "2023-04-07T00:55:40Z")

</div>

What would be needed to do this? I am interested in helping if I can. This functionality would help me a lot so Id like to see it implemented.

---

<div class="post-metadata">

**Author:** ![gmarkall](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/gmarkall/32/42_2.png) [@gmarkall](https://numba.discourse.group/u/gmarkall)\
**Post date:** [April 11, 2023, 9:38am UTC](https://numba.discourse.group/t/will-the-matmul-method-be-implemented-in-jitclass/1860/7 "2023-04-11T09:38:31Z")

</div>

@jsbryaniv There’s a PR from @louisamand here: [Reflected dunder methods for jitclass by louisamand · Pull Request #8863 · numba/numba · GitHub](https://github.com/numba/numba/pull/8863) - you could have a look and see if you can suggest a way to resolve the issue with it described in [this comment](https://github.com/numba/numba/pull/8863#issuecomment-1491699857)

@louisamand it might also be worth breaking that PR into two - one for ` __matmul__ ` and one for ` __r*__ ` to make it easier to finish / progress.

---

<div class="post-metadata">

**Author:** ![jsbryaniv](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/jsbryaniv/32/815_2.png) [@jsbryaniv](https://numba.discourse.group/u/jsbryaniv)\
**Post date:** [April 11, 2023, 7:01pm UTC](https://numba.discourse.group/t/will-the-matmul-method-be-implemented-in-jitclass/1860/8 "2023-04-11T19:01:25Z")

</div>

The issue does not seem to be with the ` __matmul__ ` dunder directly, but actually the ‘@’ operator within numba. I can create a minimal failure example for ` __add__ ` if I redefine addition like this

```auto

        spec = {"x": types.Array(float64, 2, 'C')}
        @jitclass(spec)
        class MinFail:
            def __init__ (self, x):
                self.x = x
            
            def __add__ (self, other):
                return self.x @ other.x
            
        testvals = float64(np.arange(4).reshape(2, 2))
        a = MinFail(testvals)
        b = MinFail(testvals)
        c = a + b
        print(c)

```

Additionally I can create a working ` __matmul__ ` like this

```auto
        spec = {"x": types.Array(float64, 2, 'C')}
        @jitclass(spec)
        class MinFail:
            def __init__ (self, x):
                self.x = x
            
            def __matmul__ (self, other):
                output = np.zeros((self.x.shape[0], other.x.shape[1]))
                for i in range(self.x.shape[0]):
                    for k in range(self.x.shape[1]):
                        for j in range(other.x.shape[1]):
                            output[i, j] += self.x[i, k] * other.x[k, j]
                return output
            
        testvals = float64(np.arange(4).reshape(2, 2))
        a = MinFail(testvals)
        b = MinFail(testvals)
        c = a @ b
        print(c)

```
