# Mul! with 3-dimensional array

**URL:** <https://discourse.julialang.org/t/mul-with-3-dimensional-array/115263>\
**Category:** New to Julia\
**Tags:** linearalgebra\
**Created:** [June 5, 2024, 10:18pm UTC](https://discourse.julialang.org/t/mul-with-3-dimensional-array/115263 "2024-06-05T22:18:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sihyun\_Kim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sihyun_kim/32/207216_2.png) [@Sihyun\_Kim](https://discourse.julialang.org/u/Sihyun_Kim)\
**Post date:** [June 5, 2024, 10:18pm UTC](https://discourse.julialang.org/t/mul-with-3-dimensional-array/115263/1 "2024-06-05T22:18:46Z")

</div>

I am new to Julia. I need to multiply 3 dimensional array by 2 dimensional array. The simplified form is here:

> vR = fill(1.0, (251, 51, 21));  
> Π = fill(1.0, (21, 21));

> function mat\_mul(A, B)  
> C = similar(A)  
> @inbounds for i in 1:51  
> C[:, i, :] = A[:, i, :] \* B’  
> end  
> return C  
> end

> EvR = mat\_mul(vR, Π)

Is there a faster way? I wish something like `mul!`, but can be applied to 3-dimensional array.

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [June 5, 2024, 10:27pm UTC](https://discourse.julialang.org/t/mul-with-3-dimensional-array/115263/2 "2024-06-05T22:27:56Z")

</div>

This isn’t a full answer, but try to look at [GitHub - mcabbott/Tullio.jl: ⅀](https://github.com/mcabbott/Tullio.jl) and other packages by mcabbott. They support the nifty Einstein notation ([Einstein notation - Wikipedia](https://en.wikipedia.org/wiki/Einstein_notation))

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [June 5, 2024, 11:35pm UTC](https://discourse.julialang.org/t/mul-with-3-dimensional-array/115263/3 "2024-06-05T23:35:09Z")

</div>

[NNlib.batched\_mul!](https://fluxml.ai/NNlib.jl/dev/reference/#NNlib.batched_mul!) could also be used, you will need to reorder your axis though as it assumes that the batch dimensions are last.

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [June 6, 2024, 3:08pm UTC](https://discourse.julialang.org/t/mul-with-3-dimensional-array/115263/4 "2024-06-06T15:08:09Z")

</div>

The libraries people have suggested are probably your best bets if you really need top performance. But you probably get most of the way there with

```julia
using LinearAlgebra

function mat_mul(A, B)
	C = similar(A)
	for i in axes(A, 2)
		@views mul!(C[:, i, :], A[:, i, :], B')
	end
	return C
end

```

One can also just use

```julia
mat_mul2(A, B) = mapslices(a -> a * B', A; dims=(1,3))

```

but this is rather inefficient due to the intermediate allocations.
