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

does not work with Symbols.jl due to type casting (it seems) #33

Closed
mmikhasenko opened this issue Apr 3, 2021 · 6 comments
Closed

does not work with Symbols.jl due to type casting (it seems) #33

mmikhasenko opened this issue Apr 3, 2021 · 6 comments

Comments

@mmikhasenko
Copy link

using Symbolics
using ClassicalOrthogonalPolynomials
P = Legendre()
@variables z
P[z,1:10]

breaks with

MethodError: no method matching Float64(::Symbolics.Num)

Closest candidates are:
(::Type{T})(::Real, !Matched::RoundingMode) where T<:AbstractFloat at rounding.jl:200
(::Type{T})(::T) where T<:Number at boot.jl:760
(::Type{T})(!Matched::AbstractChar) where T<:Union{AbstractChar, Number} at char.jl:50
...
convert(::Type{Float64}, ::Symbolics.Num)@number.jl:7
to_quasi_index(::Type{Float64}, ::Symbolics.Num)@indices.jl:103
to_quasi_index(::ClassicalOrthogonalPolynomials.Legendre{Float64}, ::Type, ::Symbolics.Num)@indices.jl:111
to_indices@indices.jl:118[inlined]
to_indices@multidimensional.jl:413[inlined]
view@subquasiarray.jl:67[inlined]
layout_getindex@ArrayLayouts.jl:108[inlined]
getindex(::ClassicalOrthogonalPolynomials.Legendre{Float64}, ::Symbolics.Num, ::UnitRange{Int64})@clenshaw.jl:67
top-level scope@Local: 1[inlined]
@dlfivefifty
Copy link
Member

dlfivefifty commented Apr 3, 2021

Note you should use legendrep(0:9,z), which is equivalent to Base.unsafe_getindex(Legendre{typeof(z)}(), z, 1:10).

But that still doesn't solve the issue since we can't make ranges:

julia> zero(typeof(z)):10
ERROR: MethodError: no method matching round(::Num, ::RoundingMode{:Down})
Closest candidates are:
  round(::Real, ::RoundingMode; digits, sigdigits, base) at floatfuncs.jl:129
  round(::Complex, ::RoundingMode) at complex.jl:1038
  round(::Complex, ::RoundingMode, ::RoundingMode; kwargs...) at complex.jl:1038
  ...
Stacktrace:
 [1] round(x::Num, r::RoundingMode{:Down}; digits::Nothing, sigdigits::Nothing, base::Nothing)
   @ Base ./floatfuncs.jl:135
 [2] round(x::Num, r::RoundingMode{:Down})
   @ Base ./floatfuncs.jl:131
 [3] floor(x::Num; kwargs::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ Base ./floatfuncs.jl:155
 [4] floor(x::Num)
   @ Base ./floatfuncs.jl:155
 [5] unitrange_last(start::Num, stop::Num)
   @ Base ./range.jl:294
 [6] UnitRange{Num}(start::Num, stop::Num)
   @ Base ./range.jl:287
 [7] (::Colon)(start::Num, stop::Num)
   @ Base ./range.jl:5
 [8] (::Colon)(a::Num, b::Int64)
   @ Base ./range.jl:3
 [9] top-level scope
   @ REPL[24]:1

julia> zero(typeof(z)):∞
ERROR: TypeError: non-boolean (Num) used in boolean context
Stacktrace:
 [1] Colon
   @ ~/.julia/packages/InfiniteArrays/3DoXV/src/infrange.jl:19 [inlined]
 [2] (::Colon)(a::Num, b::Infinities.Infinity)
   @ InfiniteArrays ~/.julia/packages/InfiniteArrays/3DoXV/src/infrange.jl:16
 [3] top-level scope
   @ REPL[25]:1

Here's a work around:

julia> Base.:(:)(a::Num, ::Infinities.Infinity) = InfiniteArrays.InfUnitRange(a)


julia> legendrep.(0:9,z)
10-element Vector{Num}:
 1
 z
 1.5(z^2) - 0.5
 1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)
 1.75z*(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)) - (0.75(1.5(z^2) - 0.5))
 1.8z*(1.75z*(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)) - (0.75(1.5(z^2) - 0.5))) - (0.8(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)))
 1.8333333333333333z*(1.8z*(1.75z*(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)) - (0.75(1.5(z^2) - 0.5))) - (0.8(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)))) - (0.8333333333333334(1.75z*(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)) - (0.75(1.5(z^2) - 0.5))))
 1.8571428571428572z*(1.8333333333333333z*(1.8z*(1.75z*(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)) - (0.75(1.5(z^2) - 0.5))) - (0.8(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)))) - (0.8333333333333334(1.75z*(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)) - (0.75(1.5(z^2) - 0.5))))) - (0.8571428571428571(1.8z*(1.75z*(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)) - (0.75(1.5(z^2) - 0.5))) - (0.8(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)))))
 1.875z*(1.8571428571428572z*(1.8333333333333333z*(1.8z*(1.75z*(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)) - (0.75(1.5(z^2) - 0.5))) - (0.8(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)))) - (0.8333333333333334(1.75z*(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)) - (0.75(1.5(z^2) - 0.5))))) - (0.8571428571428571(1.8z*(1.75z*(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)) - (0.75(1.5(z^2) - 0.5))) - (0.8(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)))))) - (0.875(1.8333333333333333z*(1.8z*(1.75z*(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)) - (0.75(1.5(z^2) - 0.5))) - (0.8(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)))) - (0.8333333333333334(1.75z*(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)) - (0.75(1.5(z^2) - 0.5))))))
 1.8888888888888888z*(1.875z*(1.8571428571428572z*(1.8333333333333333z*(1.8z*(1.75z*(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)) - (0.75(1.5(z^2) - 0.5))) - (0.8(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)))) - (0.8333333333333334(1.75z*(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)) - (0.75(1.5(z^2) - 0.5))))) - (0.8571428571428571(1.8z*(1.75z*(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)) - (0.75(1.5(z^2) - 0.5))) - (0.8(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)))))) - (0.875(1.8333333333333333z*(1.8z*(1.75z*(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)) - (0.75(1.5(z^2) - 0.5))) - (0.8(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)))) - (0.8333333333333334(1.75z*(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)) - (0.75(1.5(z^2) - 0.5))))))) - (0.8888888888888888(1.8571428571428572z*(1.8333333333333333z*(1.8z*(1.75z*(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)) - (0.75(1.5(z^2) - 0.5))) - (0.8(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)))) - (0.8333333333333334(1.75z*(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)) - (0.75(1.5(z^2) - 0.5))))) - (0.8571428571428571(1.8z*(1.75z*(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)) - (0.75(1.5(z^2) - 0.5))) - (0.8(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)))))))

@dlfivefifty
Copy link
Member

The issue may be in Symbolics.jl: is this really desirable behaviour?

julia> sign(one(z))
sign(1)

@mmikhasenko
Copy link
Author

Thanks for the quick check!
Great to see that it is potentially working

@dlfivefifty
Copy link
Member

Now works!

julia> legendrep.(0:9,z)
10-element Vector{Num}:
 1
 z
 1.5(z^2) - 0.5
 1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)
 0.375 + 1.75z*(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)) - (1.125(z^2))
 0.5333333333333333z + 1.8z*(0.375 + 1.75z*(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)) - (1.125(z^2))) - (1.3333333333333335z*(1.5(z^2) - 0.5))
 0.9375(z^2) + 1.8333333333333333z*(0.5333333333333333z + 1.8z*(0.375 + 1.75z*(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)) - (1.125(z^2))) - (1.3333333333333335z*(1.5(z^2) - 0.5))) - 0.3125 - (1.4583333333333335z*(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)))
 1.142857142857143z*(1.5(z^2) - 0.5) + 1.8571428571428572z*(0.9375(z^2) + 1.8333333333333333z*(0.5333333333333333z + 1.8z*(0.375 + 1.75z*(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)) - (1.125(z^2))) - (1.3333333333333335z*(1.5(z^2) - 0.5))) - 0.3125 - (1.4583333333333335z*(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)))) - (0.45714285714285713z) - (1.5428571428571427z*(0.375 + 1.75z*(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)) - (1.125(z^2))))
 0.2734375 + 1.2760416666666667z*(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)) + 1.875z*(1.142857142857143z*(1.5(z^2) - 0.5) + 1.8571428571428572z*(0.9375(z^2) + 1.8333333333333333z*(0.5333333333333333z + 1.8z*(0.375 + 1.75z*(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)) - (1.125(z^2))) - (1.3333333333333335z*(1.5(z^2) - 0.5))) - 0.3125 - (1.4583333333333335z*(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)))) - (0.45714285714285713z) - (1.5428571428571427z*(0.375 + 1.75z*(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)) - (1.125(z^2))))) - (0.8203125(z^2)) - (1.6041666666666665z*(0.5333333333333333z + 1.8z*(0.375 + 1.75z*(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)) - (1.125(z^2))) - (1.3333333333333335z*(1.5(z^2) - 0.5))))
 0.4063492063492063z + 1.3714285714285712z*(0.375 + 1.75z*(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)) - (1.125(z^2))) + 1.8888888888888888z*(0.2734375 + 1.2760416666666667z*(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)) + 1.875z*(1.142857142857143z*(1.5(z^2) - 0.5) + 1.8571428571428572z*(0.9375(z^2) + 1.8333333333333333z*(0.5333333333333333z + 1.8z*(0.375 + 1.75z*(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)) - (1.125(z^2))) - (1.3333333333333335z*(1.5(z^2) - 0.5))) - 0.3125 - (1.4583333333333335z*(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)))) - (0.45714285714285713z) - (1.5428571428571427z*(0.375 + 1.75z*(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)) - (1.125(z^2))))) - (0.8203125(z^2)) - (1.6041666666666665z*(0.5333333333333333z + 1.8z*(0.375 + 1.75z*(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)) - (1.125(z^2))) - (1.3333333333333335z*(1.5(z^2) - 0.5))))) - (1.015873015873016z*(1.5(z^2) - 0.5)) - (1.6507936507936507z*(0.9375(z^2) + 1.8333333333333333z*(0.5333333333333333z + 1.8z*(0.375 + 1.75z*(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)) - (1.125(z^2))) - (1.3333333333333335z*(1.5(z^2) - 0.5))) - 0.3125 - (1.4583333333333335z*(1.6666666666666667z*(1.5(z^2) - 0.5) - (0.6666666666666666z)))))

@dlfivefifty
Copy link
Member

Do you know if it's possible to tell z to work over the rationals instead of Float64? Then it'd be completely symbolic

@mmikhasenko
Copy link
Author

great to see it working with Floats. I do not know about the rational

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

2 participants