fatdragon
fatdragon

Reputation: 2299

division operator with array broadcasting in Julia

I am able to multiple a constant into an array but was unable to do the same for the division operator. Expected?

julia> 2 * [1,2,3]
3-element Array{Int64,1}:
 2
 4
 6

julia> 2 / [1,2,3]
ERROR: MethodError: no method matching /(::Int64, ::Array{Int64,1})
Closest candidates are:
  /(::Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8}, ::Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8}) at int.jl:38
  /(::Union{Int16, Int32, Int64, Int8, UInt16, UInt32, UInt64, UInt8}, ::BigInt) at gmp.jl:381
  /(::T<:Integer, ::T<:Integer) where T<:Integer at int.jl:36
  ...

Upvotes: 3

Views: 5306

Answers (1)

xiaodai
xiaodai

Reputation: 16004

Not sure if it's expected but it's common to multiply a vector by a scalar in matrix algebra. But dividing a number by a vector is not defined, but what you want is achieved with broadcasting syntax, just put a dot in front of / so it becomes ./ which means apply the division element-wise.

2 ./ [1,2,3]

Upvotes: 6

Related Questions