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

Various errors with zero-size vectors and matrices #91

Closed
rdeits opened this issue Mar 28, 2016 · 9 comments
Closed

Various errors with zero-size vectors and matrices #91

rdeits opened this issue Mar 28, 2016 · 9 comments

Comments

@rdeits
Copy link
Contributor

rdeits commented Mar 28, 2016

Fixed-size vectors and matrices with one or more dimensions of zero length can cause various different issues when constructed:

julia> using FixedSizeArrays

julia> m = Mat{0, 0, Float64}()
FixedSizeArrays.Mat{0,0,Float64}(
)


julia> m = Mat{0, 0, Float64}(0)
ERROR: type Union has no field name
 in fsa_abstract at /home/rdeits/.julia/v0.4/FixedSizeArrays/src/core.jl:46
 in size_or at /home/rdeits/.julia/v0.4/FixedSizeArrays/src/core.jl:52
 in call at /home/rdeits/.julia/v0.4/FixedSizeArrays/src/constructors.jl:77
 in call at /home/rdeits/.julia/v0.4/FixedSizeArrays/src/FixedSizeArrays.jl:18

julia> m = Mat{0, 1, Float64}()
ERROR: MethodError: `convert` has no method matching convert(::Type{Tuple{Tuple{}}}, ::Tuple{})
This may have arisen from a call to the constructor Tuple{Tuple{}}(...),
since type constructors fall back to convert methods.
Closest candidates are:
  call{T}(::Type{T}, ::Any)
  convert{T<:Tuple{Any,Vararg{Any}}}(::Type{T<:Tuple{Any,Vararg{Any}}}, ::T<:Tuple{Any,Vararg{Any}})
  convert{T<:Tuple{Any,Vararg{Any}}}(::Type{T<:Tuple{Any,Vararg{Any}}}, ::Tuple{Any,Vararg{Any}})
  ...
 in call at /home/rdeits/.julia/v0.4/FixedSizeArrays/src/constructors.jl:92

julia> m = Mat{0, 1, Float64}(0)
ERROR: type Union has no field name
 in fsa_abstract at /home/rdeits/.julia/v0.4/FixedSizeArrays/src/core.jl:46
 in size_or at /home/rdeits/.julia/v0.4/FixedSizeArrays/src/core.jl:52
 in call at /home/rdeits/.julia/v0.4/FixedSizeArrays/src/constructors.jl:77
 in call at /home/rdeits/.julia/v0.4/FixedSizeArrays/src/FixedSizeArrays.jl:18

julia> m = Mat{1, 0, Float64}()
FixedSizeArrays.Mat{1,0,Float64}(
Error showing value of type FixedSizeArrays.Mat{1,0,Float64}:
ERROR: TypeError: show: in new, expected FixedSizeArrays.Mat{1,0,Float64}, got FixedSizeArrays.Mat{0,1,Union{}}
 in show at /home/rdeits/.julia/v0.4/FixedSizeArrays/src/FixedSizeArrays.jl:44
 in anonymous at show.jl:1299
 in with_output_limit at ./show.jl:1276
 in showlimited at show.jl:1298
 in writemime at replutil.jl:4
 in display at REPL.jl:114
 in display at REPL.jl:117
 [inlined code] from multimedia.jl:151
 in display at multimedia.jl:163
 in print_response at REPL.jl:134
 in print_response at REPL.jl:121
 in anonymous at REPL.jl:624
 in anonymous at REPL.jl:827
 in anonymous at LineEdit.jl:767
 in prompt! at ./LineEdit.jl:1636
 in run_interface at ./LineEdit.jl:1605
 in run_frontend at ./REPL.jl:863
 in run_repl at ./REPL.jl:167
 in _start at ./client.jl:420

julia> m = Mat{1, 0, Float64}(0)
ERROR: type Union has no field name
 in fsa_abstract at /home/rdeits/.julia/v0.4/FixedSizeArrays/src/core.jl:46
 in size_or at /home/rdeits/.julia/v0.4/FixedSizeArrays/src/core.jl:52
 in call at /home/rdeits/.julia/v0.4/FixedSizeArrays/src/constructors.jl:77
 in call at /home/rdeits/.julia/v0.4/FixedSizeArrays/src/FixedSizeArrays.jl:18

julia> m = Vec{0, Float64}()
FixedSizeArrays.Vec{0,Float64}(())

julia> m = Vec{0, Float64}(0)
ERROR: type Union has no field name
 in fsa_abstract at /home/rdeits/.julia/v0.4/FixedSizeArrays/src/core.jl:46
 in size_or at /home/rdeits/.julia/v0.4/FixedSizeArrays/src/core.jl:52
 in call at /home/rdeits/.julia/v0.4/FixedSizeArrays/src/constructors.jl:77
 in call at /home/rdeits/.julia/v0.4/FixedSizeArrays/src/FixedSizeArrays.jl:26

I'll see if I can tell what's going on.

@rdeits
Copy link
Contributor Author

rdeits commented Mar 28, 2016

Actually, the 1x0 Mat constructor with no args works now with FixedSizeArrays master.

@rdeits
Copy link
Contributor Author

rdeits commented Mar 28, 2016

I don't understand quite what's going on here. The simplest failing case is:

julia> using FixedSizeArrays

julia> Vec{0, Int}(0)
ERROR: type Union has no field name
 in fsa_abstract at /home/rdeits/.julia/v0.4/FixedSizeArrays/src/core.jl:46
 in size_or at /home/rdeits/.julia/v0.4/FixedSizeArrays/src/core.jl:52
 in call at /home/rdeits/.julia/v0.4/FixedSizeArrays/src/constructors.jl:78
 in call at /home/rdeits/.julia/v0.4/FixedSizeArrays/src/FixedSizeArrays.jl:26

It fails because the typevar FSA in call() is Union{}, not any actual FSA type.

@SimonDanisch
Copy link
Owner

May I ask why you would like to do this? I agree, the error messages should be clearer, but is there a case to make this possible at all?
Vec{0, Int}(0) should clearly throw an ArgumentError, no?

@rdeits
Copy link
Contributor Author

rdeits commented Mar 30, 2016

I'm not actually sure. I came across this bug while working on some code for representing linear systems (i.e. systems of the form xdot = A*x), and I occasionally end up with systems in which the length of x is 0. Being able to initialize 0-element vectors the same way I initialize vectors of any other length just lets me remove some special cases from my code.

I think it's also somewhat analogous to the built-in zeros(0), which returns a 0-element Array. In my mind, asking for a zero-element vector full of zeros is a well-formed (if slightly odd) thing to do.

That said, it's also easy for me to work around this by adding a few extra definitions to my code, e.g.:

call{T}(::Type{Mat{0, 0, T}}, x::Number) = Mat{0,0,T}()
call{N, T}(::Type{Mat{0, N, T}}) = Mat{0, N, T}(tuple([tuple() for i in 1:N]...))
call{N, T}(::Type{Mat{0, N, T}}, x::Number) = Mat{0, N, T}()
call{M, T}(::Type{Mat{M, 0, T}}, x::Number) = Mat{M, 0, T}()

and so on.

@SimonDanisch
Copy link
Owner

If you're convinced that this is the correct behaviour, I wouldn't mind a PR ;)
I'm not that convinced, that Mat{0,0,T}(0) is correct behaviour, though :D
There is zero for that:

zero(Mat{0,0, Float32})
zero(Mat{0,10, Float32})

I guess this discussion is slightly similar to this one: #75 (comment)

@rdeits
Copy link
Contributor Author

rdeits commented Apr 1, 2016

Ah, of course! I'd forgotten about the overloaded zero(). I'll switch to using that in my code, which should solve my problem nicely. Thanks!

For what it's worth, I started out using Vec{N,T}(0) instead of zero(Vec{N, T}) because I saw the former in the readme and assumed that was the best way to construct a vector of zeros. But using zero() does make a Julian kind of sense.

@tkoolen
Copy link

tkoolen commented Jun 11, 2016

I'm encountering a related issue with the transpose of an Nx0 Mat:

julia> a = Mat{3, 0, Float64}()
FixedSizeArrays.Mat{3,0,Float64}(



)

julia> a'
FixedSizeArrays.Mat{3,1,Tuple{}}(
    ()
    ()
    ()
)

@SimonDanisch
Copy link
Owner

I fixed it in #126

@tkoolen
Copy link

tkoolen commented Jun 11, 2016

Great, thank you!

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