I am new to Julia and trying out the ModelingToolking package. I have a basic understanding of the type system and the dynamic dispatch, but couldn’t figure out why this didn’t work.
This works perfectly - almost magic to me:
>>> using ModelingToolkit
>>> using Distributions
>>> @variables t
>>> d = Normal()
>>> pdf(d, t)
(exp((-abs2((t - 0.0) / 1.0)) / 2) * invsqrt2π) / 1.0
Then this didn’t work.
>>> cdf(d, t)
MethodError: no method matching AbstractFloat(::Num)
Closest candidates are:
AbstractFloat(::Real, !Matched::RoundingMode) where T<:AbstractFloat at rounding.jl:200
AbstractFloat(::T) where T<:Number at boot.jl:716
AbstractFloat(!Matched::Bool) at float.jl:258
If I check the Distributions package documentation, both functions have the same function signature:
Symbolic values get passed through normal Julia code fine, but in this case erfc is implemented in C and Julia attempts to convert the symbolic value t to a floating point number to pass to the C routine. This can’t work, so you get the traceback. The solution is to tell ModelingToolkit that erfc is a fundamental operation and that it shouldn’nt bother trying to look inside it.
Oh, yikes, I’m surprised that worked! For a function like erfc that is implemented in a C library, you also need to register a derivative if you want to differentiate it. It looks like a derivative was registered for erfc, but the function itself was not registered, and I’m not sure where it should have been.
I don’t know how Matlab handles this, but ModelingToolkit defines the derivatives of some base set of functions and then works to express derivatives of your expression in terms of that base set.