# Multiplication of 1-element arrays fails

**URL:** <https://discourse.julialang.org/t/multiplication-of-1-element-arrays-fails/15678>\
**Category:** New to Julia\
**Created:** [September 29, 2018, 10:59pm UTC](https://discourse.julialang.org/t/multiplication-of-1-element-arrays-fails/15678 "2018-09-29T22:59:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cshewmake2](https://avatars.discourse-cdn.com/v4/letter/c/9e8a1a/32.png) [@cshewmake2](https://discourse.julialang.org/u/cshewmake2)\
**Post date:** [September 29, 2018, 10:59pm UTC](https://discourse.julialang.org/t/multiplication-of-1-element-arrays-fails/15678/1 "2018-09-29T22:59:42Z")

</div>

Julia v1.0.0: I don’t know if anyone else has encountered this yet, but it seems that the native \* operator, while defined for two array objects with proper dimensions, is undefined for multiplication of 1x1 elements.

For example,

```julia
julia> a = [1]
1-element Array{Int64,1}:
 1

julia> b = [2]
1-element Array{Int64,1}:
 2

julia> a*b
ERROR: MethodError: no method matching *(::Array{Int64,1}, ::Array{Int64,1})
Closest candidates are:
  *(::Any, ::Any, ::Any, ::Any...) at operators.jl:502
  *(::LinearAlgebra.Adjoint{#s549,#s548} where #s548<:Union{DenseArray{T<:Union{Complex{Float32}, Complex{Float64}, Float32, Float64},2}, ReinterpretArray{T<:Union{Complex{Float32}, Complex{Float64}, Float32, Float64},2,S,A} where S where A<:Union{SubArray{T,N,A,I,true} where I<:Tuple{AbstractUnitRange,Vararg{Any,N} where N} where A<:DenseArray where N where T, DenseArray}, ReshapedArray{T<:Union{Complex{Float32}, Complex{Float64}, Float32, Float64},2,A,MI} where MI<:Tuple{Vararg{SignedMultiplicativeInverse{Int64},N} where N} where A<:Union{ReinterpretArray{T,N,S,A} where S where A<:Union{SubArray{T,N,A,I,true} where I<:Tuple{AbstractUnitRange,Vararg{Any,N} where N} where A<:DenseArray where N where T, DenseArray} where N where T, SubArray{T,N,A,I,true} where I<:Tuple{AbstractUnitRange,Vararg{Any,N} where N} where A<:DenseArray where N where T, DenseArray}, SubArray{T<:Union{Complex{Float32}, Complex{Float64}, Float32, Float64},2,A,I,L} where L where I<:Tuple{Vararg{Union{Int64, AbstractRange{Int64}, AbstractCartesianIndex},N} where N} where A<:Union{ReinterpretArray{T,N,S,A} where S where A<:Union{SubArray{T,N,A,I,true} where I<:Tuple{AbstractUnitRange,Vararg{Any,N} where N} where A<:DenseArray where N where T, DenseArray} where N where T, ReshapedArray{T,N,A,MI} where MI<:Tuple{Vararg{SignedMultiplicativeInverse{Int64},N} where N} where A<:Union{ReinterpretArray{T,N,S,A} where S where A<:Union{SubArray{T,N,A,I,true} where I<:Tuple{AbstractUnitRange,Vararg{Any,N} where N} where A<:DenseArray where N where T, DenseArray} where N where T, SubArray{T,N,A,I,true} where I<:Tuple{AbstractUnitRange,Vararg{Any,N} where N} where A<:DenseArray where N where T, DenseArray} where N where T, DenseArray}} where #s549, ::Union{DenseArray{S,1}, ReinterpretArray{S,1,S,A} where S where A<:Union{SubArray{T,N,A,I,true} where I<:Tuple{AbstractUnitRange,Vararg{Any,N} where N} where A<:DenseArray where N where T, DenseArray}, ReshapedArray{S,1,A,MI} where MI<:Tuple{Vararg{SignedMultiplicativeInverse{Int64},N} where N} where A<:Union{ReinterpretArray{T,N,S,A} where S where A<:Union{SubArray{T,N,A,I,true} where I<:Tuple{AbstractUnitRange,Vararg{Any,N} where N} where A<:DenseArray where N where T, DenseArray} where N where T, SubArray{T,N,A,I,true} where I<:Tuple{AbstractUnitRange,Vararg{Any,N} where N} where A<:DenseArray where N where T, DenseArray}, SubArray{S,1,A,I,L} where L where I<:Tuple{Vararg{Union{Int64, AbstractRange{Int64}, AbstractCartesianIndex},N} where N} where A<:Union{ReinterpretArray{T,N,S,A} where S where A<:Union{SubArray{T,N,A,I,true} where I<:Tuple{AbstractUnitRange,Vararg{Any,N} where N} where A<:DenseArray where N where T, DenseArray} where N where T, ReshapedArray{T,N,A,MI} where MI<:Tuple{Vararg{SignedMultiplicativeInverse{Int64},N} where N} where A<:Union{ReinterpretArray{T,N,S,A} where S where A<:Union{SubArray{T,N,A,I,true} where I<:Tuple{AbstractUnitRange,Vararg{Any,N} where N} where A<:DenseArray where N where T, DenseArray} where N where T, SubArray{T,N,A,I,true} where I<:Tuple{AbstractUnitRange,Vararg{Any,N} where N} where A<:DenseArray where N where T, DenseArray} where N where T, DenseArray}}) where {T<:Union{Complex{Float32}, Complex{Float64}, Float32, Float64}, S} at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v1.0/LinearAlgebra/src/matmul.jl:97
  *(::LinearAlgebra.Adjoint{#s549,#s548} where #s548<:LinearAlgebra.AbstractTriangular where #s549, ::AbstractArray{T,1} where T) at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v1.0/LinearAlgebra/src/triangular.jl:1805
  ...
Stacktrace:
 [1] top-level scope at none:0

julia> VERSION
v"1.0.0"

julia> ones(3,3) * ones(3,1)
3×1 Array{Float64,2}:
 3.0
 3.0
 3.0

```

Any thoughts on why this may be failing?

---

<div class="post-metadata">

**Author:** ![benninkrs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/benninkrs/32/3191_2.png) [@benninkrs](https://discourse.julialang.org/u/benninkrs)\
**Post date:** [September 29, 2018, 11:08pm UTC](https://discourse.julialang.org/t/multiplication-of-1-element-arrays-fails/15678/2 "2018-09-29T23:08:04Z")

</div>

Because a is a column vector (it has only one dimension). For a\*b the first dimension of b is contracted with the second dimension of a, but there is none.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 29, 2018, 11:09pm UTC](https://discourse.julialang.org/t/multiplication-of-1-element-arrays-fails/15678/3 "2018-09-29T23:09:04Z")

</div>

```julia
julia> ones(1,1) * ones(1,1) # matrix times matrix = matrix
1×1 Array{Float64,2}:
 1.0

julia> ones(1,1) * ones(1) # matrix times vector = vector
1-element Array{Float64,1}:
 1.0

julia> ones(1) * ones(1) # vector times vector, error
ERROR: MethodError: no method matching *(::Array{Float64,1}, ::Array{Float64,1})

```

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [September 30, 2018, 9:10am UTC](https://discourse.julialang.org/t/multiplication-of-1-element-arrays-fails/15678/4 "2018-09-30T09:10:25Z")

</div>

Maybe we should define that method and have it print a more informative error message.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 1, 2018, 12:45pm UTC](https://discourse.julialang.org/t/multiplication-of-1-element-arrays-fails/15678/5 "2018-10-01T12:45:55Z")

</div>

OTOH, for one coming from Matlab, R, or similar, and did yet not learn about broadcasting, there are going to be very many methods that just “don’t work”.

---

<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:** [October 1, 2018, 1:20pm UTC](https://discourse.julialang.org/t/multiplication-of-1-element-arrays-fails/15678/6 "2018-10-01T13:20:08Z")

</div>

Unlike Matlab, a 1-element column vector (1d array) is not the same thing as a 1x1 matrix (2d array), and neither is the same thing as a scalar.
