Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no method for scalar divsion by Array{} #25640

Closed
brossetti opened this issue Jan 19, 2018 · 5 comments
Closed

no method for scalar divsion by Array{} #25640

brossetti opened this issue Jan 19, 2018 · 5 comments

Comments

@brossetti
Copy link

brossetti commented Jan 19, 2018

It appears that there is no method for scalar division by an Array{}. Here is a quick example:

julia> x = 1.0:4.0
1.0:1.0:4.0

julia> y = [1.0, 2.0, 3.0, 4.0]
4-element Array{Float64,1}:
 1.0
 2.0
 3.0
 4.0

julia> 1+x
2.0:1.0:5.0

julia> 1+y
4-element Array{Float64,1}:
 2.0
 3.0
 4.0
 5.0

julia> 1-x
0.0:-1.0:-3.0

julia> 1-y
4-element Array{Float64,1}:
  0.0
 -1.0
 -2.0
 -3.0

julia> 1*x
1.0:1.0:4.0

julia> 1*y
4-element Array{Float64,1}:
 1.0
 2.0
 3.0
 4.0

julia> 1/x
4-element Array{Float64,1}:
 1.0
 0.5
 0.333333
 0.25

julia> 1/y
ERROR: MethodError: no method matching /(::Int64, ::Array{Float64,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

As you can see, / works when using StepRangeLen{}, but not for Array{}. It might be worth noting that 1./y works as expected.

I observed this behavior on:

Julia Version 0.6.2
Commit d386e40c17 (2017-12-13 18:08 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin14.5.0)
  CPU: Intel(R) Core(TM) i5-4288U CPU @ 2.60GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.9.1 (ORCJIT, haswell)

I'd be surprised if I'm the first person to notice this, so my apologies if this is a known issue.

@brossetti brossetti changed the title Scalar divsion by Array{} undefined no method for scalar divsion by Array{} Jan 19, 2018
@KristofferC
Copy link
Sponsor Member

KristofferC commented Jan 19, 2018

On master:

julia> 1/x
1×4 Transpose{Float64,Array{Float64,1}}:
 0.0333333  0.0666667  0.1  0.133333

julia> 1/y
1×4 Transpose{Float64,Array{Float64,1}}:
 0.0333333  0.0666667  0.1  0.13333

Ok to close?

@brossetti
Copy link
Author

Looks good to me. Thanks!

@mbauman
Copy link
Sponsor Member

mbauman commented Jan 19, 2018

Welcome! Just a quick note: this now means something very different from 1 ./ x. If you have more questions about this, please feel free to open a topic about this over at the Discourse discussion board.

@brossetti
Copy link
Author

Thanks for the note, @mbauman. As a quick clarification, are you referring to the changes noted in the 0.6.0 release?

For every binary operator ⨳, a .⨳ b is now automatically equivalent to the broadcast call (⨳).(a, b). Hence, one no longer defines methods for .* etcetera. This also means that "dot operations" automatically fuse into a single loop, along with other dot calls f.(x) (#17623). Similarly for unary operators (#20249).

Operations like .+ and .* on Range objects are now generic broadcast calls (see above) and produce an Array. If you want a Range result, use + and *, etcetera (#17623).

@mbauman
Copy link
Sponsor Member

mbauman commented Jan 21, 2018

No, I mean to say that 1 / (1:4) is no longer equivalent to 1 ./ (1:4), and it returns something completely different in 0.7 than it did in 0.6. This is a new breaking change between 0.6 and 0.7.

Julia 0.6: Ranges have a special non-idiomatic behavior where 1/r is the same as 1 ./ r — it means elementwise division. Division of scalars by all other vectors is a missing method error, as you reported.

julia> 1/(1:4) # 0.6
4-element Array{Float64,1}:
 1.0
 0.5
 0.333333
 0.25

Julia 0.7: 1/x for any vector x computes the multiplicative inverse of the vector itself, akin to solving the equation 1 ≈ y*x for y. This is now consistent between ranges and all other vector types.

julia> y = 1/(1:4) # 0.7
1×4 LinearAlgebra.Transpose{Float64,Array{Float64,1}}:
 0.0333333  0.0666667  0.1  0.133333

julia> y*(1:4)
1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants