JuliaApproximation / ApproxFun.jl

Julia package for function approximation
http://juliaapproximation.github.io/ApproxFun.jl/
Other
528 stars 70 forks source link

Cannot use `Integral` in Newton solver #928

Open eschnett opened 4 months ago

eschnett commented 4 months ago

I want to solve a nonlinear PDE where I impose, instead of a boundary condition, that the integral over the domain is zero. I don't know how to express this – the way I tried leads to an error (see below).

Things work if I impose that the solution is zero at a particular point (that's the second term u(0)). When I replace this by sum(Integral()*u), I receive an error about an unimplemented function.

julia> using ApproxFun

julia> newton(u -> [u(-1)-u(1), u(0), u''-1], Fun(x->1))
Fun(Chebyshev(), [0.25, 2.77556e-17, 0.25])

julia> newton(u -> [u(-1)-u(1), sum(Integral()*u), u''-1], Fun(x->1))
ERROR: MethodError: no method matching space(::ApproxFun.DualFun{Fun{Chebyshev{…}, Float64, Vector{…}}, ConstantOperator{Float64, Chebyshev{…}}})

Closest candidates are:
  space(::Fun)
   @ ApproxFunBase ~/.julia/packages/ApproxFunBase/1mBis/src/Fun.jl:339
  space(::ProductFun)
   @ ApproxFunBase ~/.julia/packages/ApproxFunBase/1mBis/src/Multivariate/ProductFun.jl:293
  space(::LowRankFun, ::Integer)
   @ ApproxFunBase ~/.julia/packages/ApproxFunBase/1mBis/src/Multivariate/LowRankFun.jl:348

I'd be happy to implement something, but as I'm new to the code I would need some pointers. For example, which function with what signature should be implemented?

MikaelSlevinsky commented 4 months ago

What about sum(u)?

eschnett commented 4 months ago

sum(u) works.

Is this how the definite integral should be calculated?

jishnub commented 4 months ago

Yes, I believe this comes from Chebfun

MikaelSlevinsky commented 4 months ago

It's in some sense just a synonym for DefiniteIntegral, but in this case the overrides for the autodiff live here https://github.com/JuliaApproximation/ApproxFun.jl/blob/master/src/Extras/autodifferentiation.jl, and this shows that sum, cumsum and integrate are more probably convenient.

dlfivefifty commented 4 months ago

I agree it's a confusing pun. An other confusing pun is conflating operators (which should be linear functions of functions) and multiplication.

The idea behind ContinuumArrays.jl/ClassicalOrthogonalPolynomials.jl was to separate the idea of "functions as vectors" and "functions as functions". The long term plan is to make ApproxFun.jl sit on top of these other projects and make a concrete distinction between these two languages. Unfortunately ApproxFun has a lot of features so it is taking a lot of time to realise this.