Skip to content

Commit ed558cd

Browse files
committed
STaylor1: add log
1 parent 1778a92 commit ed558cd

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

src/functions.jl

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ for T in (:Taylor1, :TaylorN)
147147
end
148148
end
149149

150-
# Functions for StaticTaylor
150+
# Functions for STaylor1
151151
@generated function exp(a::STaylor1{N,T}) where {N, T <: Number}
152152
ex_calc = quote end
153153
append!(ex_calc.args, Any[nothing for i in 1:N])
@@ -180,6 +180,40 @@ end
180180
end
181181
end
182182

183+
@generated function log(a::STaylor1{N,T}) where {N, T <: Number}
184+
ex_calc = quote end
185+
append!(ex_calc.args, Any[nothing for i in 1:N])
186+
syms = Symbol[Symbol("c$i") for i in 1:N]
187+
188+
(N >= 1) && (ex_calc.args[1] = :($(syms[1]) = log(constant_term(a))))
189+
(N >= 2) && (ex_calc.args[2] = :($(syms[2]) = a[1]/constant_term(a)))
190+
191+
for k in 2:(N-1)
192+
ex_line = :($(k-1)*a[1]*$(syms[k]))
193+
@inbounds for i = 2:k-1
194+
ex_line = :($ex_line + $(k-i)*a[$i] * $(syms[k+1-i]))
195+
end
196+
ex_line = :((a[$k] - ($ex_line)/$(convert(T,k)))/constant_term(a))
197+
ex_line = :($(syms[k+1]) = $ex_line)
198+
ex_calc.args[k+1] = ex_line
199+
end
200+
201+
exout = :(($(syms[1]),))
202+
for i = 2:N
203+
push!(exout.args, syms[i])
204+
end
205+
206+
return quote
207+
Base.@_inline_meta
208+
iszero(constant_term(a)) && throw(ArgumentError("""
209+
The 0-th order `STaylor1` coefficient must be non-zero
210+
in order to expand `log` around 0.
211+
"""))
212+
$ex_calc
213+
return STaylor1{N,T}($exout)
214+
end
215+
end
216+
183217
# Recursive functions (homogeneous coefficients)
184218
for T in (:Taylor1, :TaylorN)
185219
@eval begin

0 commit comments

Comments
 (0)