# Batched matmul with einsum

**URL:** <https://discourse.julialang.org/t/batched-matmul-with-einsum/115448>\
**Category:** New to Julia\
**Created:** [June 11, 2024, 12:00am UTC](https://discourse.julialang.org/t/batched-matmul-with-einsum/115448 "2024-06-11T00:00:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![smartalecH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/smartalech/32/32379_2.png) [@smartalecH](https://discourse.julialang.org/u/smartalecH)\
**Post date:** [June 11, 2024, 12:00am UTC](https://discourse.julialang.org/t/batched-matmul-with-einsum/115448/1 "2024-06-11T00:00:02Z")

</div>

I’m performing a batched matmul using Einsum. Is there a clean way to infer leading dimensions, like I can with numpy?

For example, I’m currently doing this:

```julia
@einsum C[k, l, m, j] = A[i, j] * B[k, l, m, j]

```

But it’d be great to be able to do something like this…

```julia
@einsum C[..., m, j] = A[i, j] * B[m, j]

```

so that this can work with a `B` of arbitrary dimensions.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 11, 2024, 12:15am UTC](https://discourse.julialang.org/t/batched-matmul-with-einsum/115448/2 "2024-06-11T00:15:34Z")

</div>

I’m not sure if this can be inferred, but on a separate note you’d probably be better off making `k,l` the trailing dimensions, since Julia is column-major (unlike numpy).

> [@smartalecH](#):
>
> so that this can work with a `B` of arbitrary dimensions.

One option might be to use `reshape` to re-interpret any number of trailing dimensions into a single dimension.
