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

Compatibility with Symbolics.jl #847

Closed
benjione opened this issue Nov 30, 2022 · 3 comments
Closed

Compatibility with Symbolics.jl #847

benjione opened this issue Nov 30, 2022 · 3 comments

Comments

@benjione
Copy link

It would be nice to have better compatibility with Symbolics.jl.

What currently works:

julia> @variables a[1:10];

julia> p = Fun(Chebyshev(), a);

julia> p(0.1)
0.1a[2] + 0.48016000000000003a[6] + 0.9208a[5] + 0.6957452799999999a[9] + 0.7842626560000001a[10] + a[1] - 0.98a[3] - 0.29600000000000004a[4] - 0.824768a[7] - 0.6451136000000001a[8]

julia> p'
Fun(Chebyshev(), Num[3.0a[4] + 5.0a[6] + 7.0a[8] + 9.0a[10] + a[2], 4a[3] + 8.0a[5] + 12.0a[7] + 16.0a[9], 6a[4] + 10.0a[6] + 14.0a[8] + 18.0a[10], 8a[5] + 12.0a[7] + 16.0a[9], 10a[6] + 14.0a[8] + 18.0a[10], 12a[7] + 16.0a[9], 14a[8] + 18.0a[10], 16a[9], 18a[10]])

What currently does not work, but would be nice to have:

julia> @variables x;

julia> p(x) # expected: Polynomial written out with x and a[i]
ERROR: MethodError: in(::Num, ::ChebyshevInterval{Float64}) is ambiguous. Candidates:
  in(v, I::IntervalSets.TypedEndpointsInterval{:closed, :closed}) in IntervalSets at /home/benjamin/.julia/packages/IntervalSets/1KL4Q/src/IntervalSets.jl:121
  in(variable::Union{Num, SymbolicUtils.Sym, SymbolicUtils.Term}, domain::Domain) in Symbolics at /home/benjamin/.julia/packages/Symbolics/FGTCH/src/domains.jl:9
Possible fix, define
  in(::Union{Num, SymbolicUtils.Sym, SymbolicUtils.Term}, ::IntervalSets.TypedEndpointsInterval{:closed, :closed})
Stacktrace:
 [1] evaluate(f::Symbolics.Arr{Num, 1}, S::Chebyshev{ChebyshevInterval{Float64}, Float64}, x::Num)
   @ ApproxFunOrthogonalPolynomials ~/.julia/packages/ApproxFunOrthogonalPolynomials/HYgCM/src/Spaces/PolynomialSpace.jl:20
 [2] evaluate(f::Fun{Chebyshev{ChebyshevInterval{Float64}, Float64}, Real, Symbolics.Arr{Num, 1}}, x::Num)
   @ ApproxFunBase ~/.julia/packages/ApproxFunBase/3ch6w/src/Fun.jl:354
 [3] (::Fun{Chebyshev{ChebyshevInterval{Float64}, Float64}, Real, Symbolics.Arr{Num, 1}})(x::Num)
   @ ApproxFunBase ~/.julia/packages/ApproxFunBase/3ch6w/src/Fun.jl:358
 [4] top-level scope
   @ REPL[64]:1

@benjione
Copy link
Author

There is a dirty workaround, in this case with PolyVar instead of the type of Symbolics, but same works for Symbolics:

using SumOfSquares
using DynamicPolynomials
using DynamicPolynomials: PolyVar
using ApproxFun
using SCS
using IntervalSets

## define these functions as a trick:
IntervalSets._in(v::PolyVar, I::IntervalSets.TypedEndpointsInterval{:closed,:closed}) = true
IntervalSets._in(v::PolyVar, I::IntervalSets.TypedEndpointsInterval{:open,:open}) = true
IntervalSets._in(v::PolyVar, I::IntervalSets.TypedEndpointsInterval{:closed,:open}) = true
IntervalSets._in(v::PolyVar, I::IntervalSets.TypedEndpointsInterval{:open,:closed}) = true

## use it for something:
@polyvar x

model = SOSModel(SCS.Optimizer)
@variable(model, a[1:10])
p = Fun(Chebyshev(), a)
@constraint(model, p(x)>=0)  # this works now, would not have worked before

# function to approximate
f(x) = x^2-0.2
X = rand(1000)*2 .-1
Y = f.(X)

mse_p = sum((p.(X) .- Y).^2)  # also this works
...

@jishnub
Copy link
Member

jishnub commented Apr 13, 2023

This should probably be discussed in IntervalSets?

@benjione
Copy link
Author

I think this has to be fixed in MultivariatePolynomials.jl or DynamicPolynomials.jl.

I opened an issue here and will close this issue for now.

This issue was closed.
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