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

Missing method convert{T}(Vector{T}, ::Range) #13532

Closed
kuanxu opened this issue Oct 10, 2015 · 9 comments
Closed

Missing method convert{T}(Vector{T}, ::Range) #13532

kuanxu opened this issue Oct 10, 2015 · 9 comments
Labels
good first issue Indicates a good issue for first-time contributors to Julia help wanted Indicates that a maintainer wants help on an issue or pull request

Comments

@kuanxu
Copy link

kuanxu commented Oct 10, 2015

I'm not sure if this is a bug or I am missing something in v0.4.0:

julia> x = linspace(0, 2, 4)
linspace(0.0,2.0,4)

julia> convert(Array{BigFloat}, x)
ERROR: MethodError: `convert` has no method matching convert(::Type{Array{BigFloat,N}}, ::LinSpace{Float64})
This may have arisen from a call to the constructor Array{BigFloat,N}(...),
since type constructors fall back to convert methods.
Closest candidates are:
  call{T}(::Type{T}, ::Any)
  convert{T,n}(::Type{Array{T,N}}, ::Array{T,n})
  convert{T,n,S}(::Type{Array{T,N}}, ::Array{S,n})

Compare with v0.3.8:

julia> x = linspace(0, 2, 4)
4-element Array{Float64,1}:
 0.0     
 0.666667
 1.33333
 2.0     

julia> convert(Array{BigFloat}, x)
4-element Array{BigFloat,1}:
 0e+00                                                     
 6.6666666666666662965923251249478198587894439697265625e-01
 1.3333333333333332593184650249895639717578887939453125e+00
 2e+00  

I'm new to Julia. So, bear me if I'm wrong.

@pao
Copy link
Member

pao commented Oct 10, 2015

linspace() produces an object that is not an array. There was recently an extensive discussion about this on julia-users: https://groups.google.com/forum/#!topic/julia-users/qPqgJS-usrU

So you will be fine if you collect(x) before converting.

If you have follow-up questions, please post them to the mailing list. Thanks!

@pao pao closed this as completed Oct 10, 2015
@simonster
Copy link
Member

We currently only have convert{T}(Vector{T}, ::Range{T}). We could implement convert{T}(Vector{T}, ::Range) and convert{T}(Array{T}, ::Range) if we wanted to, though. Is there any reason not to?

@nalimilan
Copy link
Member

Indeed, the recent thread about this on julia-users made it clear that LinSpace should behave as much as possible like a standard array. This seems to include conversion.

@andreasnoack andreasnoack reopened this Oct 10, 2015
@ivarne ivarne changed the title Bug in linspace() or convert? Missing method convert{T}(Vector{T}, ::Range) Oct 10, 2015
@ivarne ivarne added help wanted Indicates that a maintainer wants help on an issue or pull request good first issue Indicates a good issue for first-time contributors to Julia labels Oct 10, 2015
@jiahao
Copy link
Member

jiahao commented Oct 10, 2015

Cross-ref #10064

@stevengj
Copy link
Member

Shouldn't we just have

convert{T}(::Type{Array{T}}, X::AbstractArray) = copy!(Array(T, size(X)), X)

? We shouldn't need a convert method specifically for ranges.

@simonster
Copy link
Member

Yes, that seems right. We also probably want
convert{T,S,N}(::Type{Array{T,N}}, X::AbstractArray{S,N}) = ...

@gdevanla
Copy link
Contributor

@simonster Shouldn't the second case just be:
convert{T, N}(::Type{Array{T,N}}, X::AbstractArray) = copy!(Array{T}(N), X)

I believe AbstractArray in the second argument should take care of all ranks of arrays. The only other reason we would want the definition you suggested would be to enforce the equality of rank. Is that correct?

Also, can I add this method to array.jl?

@stevengj
Copy link
Member

@gdevania, @simonster was suggesting the case where you specify the dimensionality of the destination array explicitly, in which case the convert function should require that it matches the dimensionality of X (which it does, in @simonster's type signature).

Also, note that Array{T}(N) creates an array of length N, not dimension N; you want Array(T, size(X)) as in my example.

Basically, we just need a PR that adds two methods:

convert{T}(::Type{Array{T}}, X::AbstractArray) = copy!(Array(T, size(X)), X)
convert{T,S,N}(::Type{Array{T,N}}, X::AbstractArray{S,N}) = copy!(Array(T, size(X)), X)

@JeffBezanson
Copy link
Member

Closed by #13683

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Indicates a good issue for first-time contributors to Julia help wanted Indicates that a maintainer wants help on an issue or pull request
Projects
None yet
Development

No branches or pull requests

10 participants