# Array/Vector multiplication for single element arrays

**URL:** <https://discourse.julialang.org/t/array-vector-multiplication-for-single-element-arrays/39377>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [May 13, 2020, 1:26am UTC](https://discourse.julialang.org/t/array-vector-multiplication-for-single-element-arrays/39377 "2020-05-13T01:26:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![willcclarke](https://avatars.discourse-cdn.com/v4/letter/w/4bbf92/32.png) [@willcclarke](https://discourse.julialang.org/u/willcclarke)\
**Post date:** [May 13, 2020, 1:26am UTC](https://discourse.julialang.org/t/array-vector-multiplication-for-single-element-arrays/39377/1 "2020-05-13T01:26:24Z")

</div>

Hi, I’m a long term Matlab user just starting to wade into the world of Julia, so forgive me if my question is naive.

I’m am currently writing some code to perform a number of matrix manipulation tasks. I came across what to me seems like a limitation of the \* operator on Arrays. Since I am new to Julia, I think the limitation is probably my understanding, not the language.

Say I have the following function

```julia
function double(A::Array{<:Real}, B::Array{<:Real})
    C = A*B
    2*C
end

```

which I evaluate as follows

```julia
julia> A = [1 2;3 4]
2×2 Array{Int64,2}:
 1 2
 3 4

julia> B = [5;6]
2-element Array{Int64,1}:
 5
 6

julia> double(A,B)
2-element Array{Int64,1}:
 34
 78

```

This all works as expected. Now what if A and B are defined as shown below

```julia
julia> A = [1;2]
2-element Array{Int64,1}:
 1
 2

julia> B = [3]
1-element Array{Int64,1}:
 3

julia> double(A,B)
ERROR: MethodError: no method matching *(::Array{Int64,1}, ::Array{Int64,1})

```

So even though this multiplication is mathematically valid it doesn’t evaluate successfully. I have solved this by extending the the \* operator using the following function

```julia
function Base.:*(A::Array{<:Number,1}, B::Array{<:Number,1})
    if length(A) == 1
        A[1]*B
    elseif length(B) == 1
        A*B[1]
    else
        error("Array dimensions not suitable for multiplication.")
    end
end

```

This works fine, however, it feels to me like this is not good practice. I was hoping someone could provide some advice on a more “Julian” way of dong this, or if this is indeed a suitable approach.

Thanks in advance, Will.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [May 13, 2020, 1:29am UTC](https://discourse.julialang.org/t/array-vector-multiplication-for-single-element-arrays/39377/2 "2020-05-13T01:29:45Z")

</div>

So the problem here is you have confused `*` and `.*` the first is matrix multiplication. The second is vectorized multiplication which is what you want. In general, to apply an operation element-wise, use `f.` (the dot goes before for infix functions).

---

<div class="post-metadata">

**Author:** ![willcclarke](https://avatars.discourse-cdn.com/v4/letter/w/4bbf92/32.png) [@willcclarke](https://discourse.julialang.org/u/willcclarke)\
**Post date:** [May 13, 2020, 1:55am UTC](https://discourse.julialang.org/t/array-vector-multiplication-for-single-element-arrays/39377/3 "2020-05-13T01:55:58Z")

</div>

Oscar, thanks for the prompt reply.

Matrix multiplication is indeed what I want, not element-wise multiplication. Specifically, I want to evaluate the multiplication of matrices A and B where A has dimensions n x m and B has dimensions m x 1 (or m x p for that matter). Both n and m can be any positive integer, including 1.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [May 13, 2020, 2:24am UTC](https://discourse.julialang.org/t/array-vector-multiplication-for-single-element-arrays/39377/4 "2020-05-13T02:24:49Z")

</div>

Sincerely, I think this post could have been avoided and your day to be more productive [if the change of error message discussed in this post was already implemented](https://discourse.julialang.org/t/eachrow-over-array-any-2-does-not-return-rows-why/34601/17). So you would have alternatives already presented to you.

Are you sure that what you want is not:

```julia
> A = [1;2]; B = [3];
> A * B'
2×1 Array{Int64,2}:
 3
 6

```

In other words, this operation is not really defined for vectors (`Array{T, 1}`), it is defined for matrices (`Array{T, 2}`). You should use the right type.

---

<div class="post-metadata">

**Author:** ![lhnguyen-vn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lhnguyen-vn/32/15449_2.png) [@lhnguyen-vn](https://discourse.julialang.org/u/lhnguyen-vn)\
**Post date:** [May 13, 2020, 2:26am UTC](https://discourse.julialang.org/t/array-vector-multiplication-for-single-element-arrays/39377/5 "2020-05-13T02:26:53Z")

</div>

Hi there,  
Welcome to the Julia community! While Julia might look very familiar to MatLab users, there are some subtle differences. Notice that in your second example, both A and B are column vectors (array with only one dimension) and therefore no multiplication method is defined since it’s unclear whether you want the inner or outer product.  
To achieve what you want, you can do `A * transpose(B)` or `A * B'` (the latter is actually the adjoint of `B`, but has the same result). This is mathematically consistent, since what you want is indeed matrix multiplication and `transpose(B)` or `B'` can be thought as 1 by 1 matrices.  
However, Oscar’s recommendation to broadcast multiplication is probably the most convenient way.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [May 13, 2020, 2:27am UTC](https://discourse.julialang.org/t/array-vector-multiplication-for-single-element-arrays/39377/6 "2020-05-13T02:27:37Z")

</div>

No, my suggestion was wrong if the generalized desired behavior is matrix multiplication.

---

<div class="post-metadata">

**Author:** ![willcclarke](https://avatars.discourse-cdn.com/v4/letter/w/4bbf92/32.png) [@willcclarke](https://discourse.julialang.org/u/willcclarke)\
**Post date:** [May 13, 2020, 2:39am UTC](https://discourse.julialang.org/t/array-vector-multiplication-for-single-element-arrays/39377/7 "2020-05-13T02:39:45Z")

</div>

So what about the other case in my original post, when

```julia
julia> A = [1 2;3 4];B = [5;6];

julia> A*B'
ERROR: DimensionMismatch("matrix A has dimensions (2,2), matrix B has dimensions (1,2)")

```

Unsurprisingly, this gives an error as the dimensions don’t agree. So it would appear that this approach is not suitable in general for A with dimensions n x m and B with dimensions m x 1?

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [May 13, 2020, 3:04am UTC](https://discourse.julialang.org/t/array-vector-multiplication-for-single-element-arrays/39377/8 "2020-05-13T03:04:14Z")

</div>

Note the types:

```julia
> A = [1 2; 3 4]
2×2 Array{Int64,2}:
 1 2
 3 4

> B = [5;6]
2-element Array{Int64,1}:
 5
 6

> A = [1;2]
2-element Array{Int64,1}:
 1
 2

> B = [3]
1-element Array{Int64,1}:
 3

```

In other words, in the first case, the multiplication works because the two are matrices `Array{T, 2}` of the right dimensions. In the second case they are not matrices of the right dimensions, for them to be matrices of the right dimensions you need to declare them correctly from start or use the `'` operator I used to get the second operand (`B`) with the right type.

```julia
> A = reshape([1, 2], 2, 1)
2×1 Array{Int64,2}:
 1
 2
> B = reshape([3], 1, 1)
1×1 Array{Int64,2}:
 3
> A * B
2×1 Array{Int64,2}:
 3
 6

```

This gets both `A` and `B` with the right types and then matrix multiplication is defined for them.

---

<div class="post-metadata">

**Author:** ![willcclarke](https://avatars.discourse-cdn.com/v4/letter/w/4bbf92/32.png) [@willcclarke](https://discourse.julialang.org/u/willcclarke)\
**Post date:** [May 13, 2020, 3:15am UTC](https://discourse.julialang.org/t/array-vector-multiplication-for-single-element-arrays/39377/9 "2020-05-13T03:15:11Z")

</div>

Thank you, Henrique. I wasn’t aware of the reshape() function. That solves my problems.
