-
Notifications
You must be signed in to change notification settings - Fork 1
Use generated function rather than eval
#2
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,24 +16,17 @@ struct InverseTaylor{N} | |
| end | ||
| end | ||
|
|
||
| struct TaylorInverter{N} | ||
| f::Function | ||
| end | ||
|
|
||
| function TaylorInverter{N}() where {N} | ||
| f = create_expressions(N) | ||
| return TaylorInverter{N}(f) | ||
| end | ||
| struct TaylorInverter{N}; end | ||
|
|
||
| function invert(ti::TaylorInverter, a::Vector) | ||
| return ti.f([a]) | ||
| return ti([a]) | ||
| end | ||
|
|
||
| function invert(ti::TaylorInverter, taylor1::Taylor1) | ||
| order = taylor1.order | ||
| a = taylor1.coeffs[2:end] | ||
| substitution = Taylor1([-taylor1.coeffs[begin], 1], order) | ||
| return Taylor1([0; ti.f([a])], order)(substitution) | ||
| return Taylor1([0; ti([a])], order)(substitution) | ||
| end | ||
|
|
||
| function truncaterule(n, z) | ||
|
|
@@ -89,7 +82,6 @@ function process(k::Int, an::Num, it::InverseTaylor, truncrule::Fixpoint) | |
| end | ||
|
|
||
| function initial_substitution(it::InverseTaylor{N}) where {N} | ||
| @info "Intial substituion" | ||
| truncrule = truncaterule(N, it.z) | ||
| subbed = mapreduce(kan -> process(kan..., it, truncrule), +, enumerate(it.a)) |> expand | ||
|
|
||
|
|
@@ -101,7 +93,6 @@ end | |
|
|
||
| function further_substitution(it::InverseTaylor{N}, subbed::Num) where {N} | ||
| for i in 1:N | ||
| @info "Expanding term $i to create ivnersion expression" | ||
| divided = simplify(subbed / it.z) | ||
| subz = substitute(divided, Dict(it.z => 0)) | ||
| it.B[i] = solve_for(subz ~ i == 1 ? 1 : 0, it.A[i]) |> simplify | ||
|
|
@@ -111,13 +102,14 @@ function further_substitution(it::InverseTaylor{N}, subbed::Num) where {N} | |
| return subbed | ||
| end | ||
|
|
||
| function create_expressions(n::Int) | ||
| it = InverseTaylor{n}() | ||
| @generated function (::TaylorInverter{N})(var"ˍ₋arg1") where {N} | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it necessary to switch from a regular function to a callable struct? If so, why exactly? And if not, why do you choose to do so anyway?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not necessary, but it's the easiest way to get around to it.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. GIven the fact that the
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It's the (Symbolics.jl) mangled name of the symbolic argument which is defined by this package. The argument name is the same as the argument name of the function that you eval'd
The generator returns an AST.
The argument name is defined by this package when choosing the symbolic Num - if you change that, you get whatever symbol you want. |
||
| it = InverseTaylor{N}() | ||
| subbed = initial_substitution(it) | ||
| subbed = further_substitution(it, subbed) | ||
|
|
||
| f = build_function(it.B, [it.a])[1] # [1] because build function also provided an in-place version in [2] | ||
| return eval(f) | ||
| fdef = build_function(it.B, [it.a])[1] # [1] because build function also provided an in-place version in [2] | ||
| fbody = fdef.args[2] | ||
| return fbody | ||
| end | ||
|
|
||
| export TaylorInverter, invert | ||
|
|
||
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.
Do I understand correctly that these
@infos are removed for the sake of keeping the@generatedfunction pure?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.
Yes, printing is not allowed from inside generated functions.