-
Notifications
You must be signed in to change notification settings - Fork 154
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
Generate Taylor series #1298
base: master
Are you sure you want to change the base?
Generate Taylor series #1298
Conversation
Codecov ReportAttention: Patch coverage is
❗ Your organization needs to install the Codecov GitHub app to enable full functionality. Additional details and impacted files@@ Coverage Diff @@
## master #1298 +/- ##
==========================================
+ Coverage 30.06% 38.16% +8.09%
==========================================
Files 47 48 +1
Lines 4576 4698 +122
==========================================
+ Hits 1376 1793 +417
+ Misses 3200 2905 -295 ☔ View full report in Codecov by Sentry. |
One problem is that the coefficients come out as floats (I want them to be rationals) because of julia> @variables x
1-element Vector{Num}:
x
julia> y = expand_derivatives(Differential(x)(sin(x)) / 2)
(1//2)*cos(x)
julia> substitute(y, x => 0)
0.5 Using julia> substitute(y, x => 0, fold=false)
(1//2)*cos(0)
julia> substitute(y, x => 0, fold=true)
0.5 The issue is that e.g. |
This does seem like a good thing for Symbolics. @shashi do you know how to make substitute not convert to float? |
One (far from perfect) solution is to |
There is also TaylorSeries.jl, which could perhaps compose with Symbolics.jl. |
0f40858
to
cb9343d
Compare
I have made quite a hefty rewrite of the perturbation example. My goal has been to significantly shorten it and better display the features of Symbolics.jl, and particularly the power of the new Taylor series functions. I hope I have not lost the spirit of the old tutorial. Please let me know if you want me to restore some of it. |
I would appreciate it if someone looks at this :) Suggestions are welcome! Particularly regarding how you think the public Taylor series interface should work, i.e. the current cc @karlwessel and #1292 and SciML/ModelingToolkit.jl#3098 |
I love to take a more closer look at this, but not before mid of next week. |
This is what makes evaluating the usefulness of this PR complicated for me.
What does TaylorSeries provide that wouldn't be easily doable using this PR?
What does Symbolics + this PR provide that wouldn't be easily doable with TaylorSeries?
In a perfect World:
In our world: Is it ok for them to just live happily side by side? If yes: is this really worth adding to Symbolics, or would it be enough to add a tutorial that shows how to expand a function as a Taylor series? |
z[0] + z[1]*(-2 + ϵ) + z[2]*((-2 + ϵ)^2) + z[3]*((-2 + ϵ)^3) | ||
``` | ||
""" | ||
function series(y, x, x0, ns; name = nameof(y)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This won't work for making a taylor series for functions, like for the perturbation theory for differential equations example. Maybe adding a method like this would work:
series(coeff, x) = sum([x^(i-1)*y for (i, y) in enumerate(coeff)])
That could be used like
@variables t, (y(t))[1:3] epsilon
series(y, epsilon)
I think that function could also be used to implement all of the other use cases.
- series with
$x_0 \ne 0$ :series(y .+ x0, epsilon)
- series with first coeff zero:
series([0; y], epsilon)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am mixed about the changes. A lot of the explanations in the text have become much clearer, especially on describing perturbation theory and its advantages (for CAS and in general).
It also does a good job to show of the new implemented features for working with Taylor series.
However, since the previous version had to implement all of the taylor specific features by hand it was easier for me to understand the procedure and adapt it for my own problem. Now if one of the new library methods fails for my own specific problem I wouldn't really know what to do. That of course wouldn't be a problem if the new methods just work for everything.
|
||
The “hello world!” analog of perturbation problems is to find a real solution $x$ to the quintic (fifth-order) equation | ||
```@example perturb | ||
using Symbolics # load Symbolics.jl |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the comments are not necessary here. This is self explanatory enough:
using Symbolics
@variables x
quintic = x^5 + x ~ 1
``` | ||
According to Abel's theorem, a general quintic equation does not have a closed form solution. But we can easily solve it numerically using Newton's method (here implemented for simplicity, and not performance): | ||
```@example perturb | ||
function solve_newton(eq, x, x₀; abstol=1e-8, maxiters=50) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think introducing a fully blown, automatic solver function is to much at this point. I think it would be better to keep solving it step by step and then maybe introduce and use this method to solve the second example.
Or maybe even do all of the steps again and let the reader realize the generality of the procedure and let them implement a generic method themself.
I would suggest to split this PR into multiple steps/stages. In the first step introduce and implement easy to understand simple versions of the Taylor series methods in the perturbation example, specific to the perturbation example. In the next step see how those methods can be used or adapted to work for the example on perturbation theory for ODEs in ModelingToolkit. And last, if those methods have proven to work in the examples and maybe have been used to solve other perturbation problems (multivariate equations, PDEs, field equations) we can use that experience to decide how and where to add those methods to the Symbolics library or its ecosystem. |
Is something like this suited for Symbolics.jl?