# Array multiplication

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

``````### 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, ⋅

@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
# ╠═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
``````

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:

``````u'  .* x
``````

or for the matrix multiplication

``````u * x
``````

Also:

``````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.

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

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

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

doesn’t error.

1 Like

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