Skip to content

Commit

Permalink
STaylor1: add log
Browse files Browse the repository at this point in the history
  • Loading branch information
mewilhel committed Apr 23, 2020
1 parent 1778a92 commit ed558cd
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion src/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ for T in (:Taylor1, :TaylorN)
end
end

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

@generated function log(a::STaylor1{N,T}) where {N, T <: Number}
ex_calc = quote end
append!(ex_calc.args, Any[nothing for i in 1:N])
syms = Symbol[Symbol("c$i") for i in 1:N]

(N >= 1) && (ex_calc.args[1] = :($(syms[1]) = log(constant_term(a))))
(N >= 2) && (ex_calc.args[2] = :($(syms[2]) = a[1]/constant_term(a)))

for k in 2:(N-1)
ex_line = :($(k-1)*a[1]*$(syms[k]))
@inbounds for i = 2:k-1
ex_line = :($ex_line + $(k-i)*a[$i] * $(syms[k+1-i]))
end
ex_line = :((a[$k] - ($ex_line)/$(convert(T,k)))/constant_term(a))
ex_line = :($(syms[k+1]) = $ex_line)
ex_calc.args[k+1] = ex_line
end

exout = :(($(syms[1]),))
for i = 2:N
push!(exout.args, syms[i])
end

return quote
Base.@_inline_meta
iszero(constant_term(a)) && throw(ArgumentError("""
The 0-th order `STaylor1` coefficient must be non-zero
in order to expand `log` around 0.
"""))
$ex_calc
return STaylor1{N,T}($exout)
end
end

# Recursive functions (homogeneous coefficients)
for T in (:Taylor1, :TaylorN)
@eval begin
Expand Down

0 comments on commit ed558cd

Please sign in to comment.