From 65ba024cf879b14f769e9708ae0ec081ab49ffd5 Mon Sep 17 00:00:00 2001 From: Keno Fischer Date: Wed, 20 Nov 2024 02:59:01 +0000 Subject: [PATCH] Use generated function rather than `eval` Since the lowered code being generated does not have external inputs, a generated function is preferred. Otherwise, it can be tricky to deal with the world age issues. As it stands, an `invokelatest` is required in several cases (and several more in 1.12 - see https://github.com/JuliaLang/julia/pull/56509). --- src/TaylorInversion.jl | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/src/TaylorInversion.jl b/src/TaylorInversion.jl index 10a80c6..8975022 100644 --- a/src/TaylorInversion.jl +++ b/src/TaylorInversion.jl @@ -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} + 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