# Array multiplication

**URL:** <https://discourse.julialang.org/t/array-multiplication/48610>\
**Category:** Modelling & Simulations\
**Tags:** question\
**Created:** [October 19, 2020, 8:03am UTC](https://discourse.julialang.org/t/array-multiplication/48610 "2020-10-19T08:03:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [October 19, 2020, 8:04am UTC](https://discourse.julialang.org/t/array-multiplication/48610/1 "2020-10-19T08:04:00Z")

</div>

Why does this generate `DimensionMismatch`? The dimensions seem to be the same size when I break them out into individual vectors `u,v,w`.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/1/f13432e6767025fccdc50a0fc9567ada2be95e13.png)

```julia
### A Pluto.jl notebook ###
# v0.12.4

using Markdown
using InteractiveUtils

# ╔═╡ c3e65238-11ce-11eb-3965-e3c27f5b9a3b
using ModelingToolkit

# ╔═╡ 4d8374b0-11cf-11eb-3902-6b478e13fd4f
using LinearAlgebra: dot, ⋅

# ╔═╡ d1119614-11ce-11eb-302a-17035ad33e41
@variables a b c d e f x1 x2

# ╔═╡ e37f717a-11ce-11eb-21dd-a5765b29bf5b
A = [a b; c d; e f]

# ╔═╡ 09c8b80c-11cf-11eb-3fd0-81a2c75f78c7
x = [x1; x2]

# ╔═╡ 48986ea6-11cf-11eb-3152-b3f460fb497b
A * x

# ╔═╡ 6054de3a-11cf-11eb-2087-6766e0de8f1f
u,v,w = eachrow(A) 

# ╔═╡ c601360c-11cf-11eb-16d8-29d6a7a194cd
u .* x

# ╔═╡ d2310acc-11cf-11eb-3286-3168891ed8bf
u ⋅ x

# ╔═╡ dbb2a08a-11cf-11eb-36a4-a3808813ff93
u' * x

# ╔═╡ f619bfbc-11cf-11eb-0f7e-aba9db67fcbd
eachrow(A) .* x

# ╔═╡ 40b52200-11d0-11eb-26ec-cbc993d1eded
eachrow(A) ⋅ x

# ╔═╡ 06646baa-11d1-11eb-35bb-87d6667d6240
eachrow(A) .⋅ x

# ╔═╡ 61d08eca-11d0-11eb-3ee2-b7d91224a27f
size(first(eachrow(A)))

# ╔═╡ 6b8f0220-11d0-11eb-31c9-03c0556cd971
size(x)

# ╔═╡ Cell order:
# ╠═c3e65238-11ce-11eb-3965-e3c27f5b9a3b
# ╠═4d8374b0-11cf-11eb-3902-6b478e13fd4f
# ╠═d1119614-11ce-11eb-302a-17035ad33e41
# ╠═e37f717a-11ce-11eb-21dd-a5765b29bf5b
# ╠═09c8b80c-11cf-11eb-3fd0-81a2c75f78c7
# ╠═48986ea6-11cf-11eb-3152-b3f460fb497b
# ╠═6054de3a-11cf-11eb-2087-6766e0de8f1f
# ╠═c601360c-11cf-11eb-16d8-29d6a7a194cd
# ╠═d2310acc-11cf-11eb-3286-3168891ed8bf
# ╠═dbb2a08a-11cf-11eb-36a4-a3808813ff93
# ╠═f619bfbc-11cf-11eb-0f7e-aba9db67fcbd
# ╠═40b52200-11d0-11eb-26ec-cbc993d1eded
# ╠═06646baa-11d1-11eb-35bb-87d6667d6240
# ╠═61d08eca-11d0-11eb-3ee2-b7d91224a27f
# ╠═6b8f0220-11d0-11eb-31c9-03c0556cd971

```

---

<div class="post-metadata">

**Author:** ![dmolina](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmolina/32/5246_2.png) [@dmolina](https://discourse.julialang.org/u/dmolina)\
**Post date:** [October 19, 2020, 10:13am UTC](https://discourse.julialang.org/t/array-multiplication/48610/2 "2020-10-19T10:13:42Z")

</div>

Be careful, “\*” with arrays try the dot matrix, so they cannot have the same size. If you want to multiply element by element you should have done:

```julia
u' .* x

```

or for the matrix multiplication

```julia
u * x

```

depending of your aim .

Also:

```julia
size(eachrow(A))
julia> size(eachrow(A))
(3,)

julia> size(x)
(1, 2)

```

It is, A has 3 rows, and x has two elements, so the dot operation is not possible.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [October 19, 2020, 12:36pm UTC](https://discourse.julialang.org/t/array-multiplication/48610/3 "2020-10-19T12:36:32Z")

</div>

Is there anything ModelingToolkit in here? Looks like it’s just a question about multiplication of arrays.

---

<div class="post-metadata">

**Author:** ![mikkoku](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikkoku/32/16274_2.png) [@mikkoku](https://discourse.julialang.org/u/mikkoku)\
**Post date:** [October 19, 2020, 1:30pm UTC](https://discourse.julialang.org/t/array-multiplication/48610/4 "2020-10-19T13:30:26Z")

</div>

I don’t really know if this is what you are looking for, but atleast

```julia
eachrow(A) .⋅ Ref(x)

```

doesn’t error.

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [October 20, 2020, 7:57pm UTC](https://discourse.julialang.org/t/array-multiplication/48610/5 "2020-10-20T19:57:03Z")

</div>

Sorry for the noise, you’re right: this wasn’t specific to ModelingToolkit. mikkoku’s answer solves my problem.
