Skip to content

Commit

Permalink
taylorAD PR
Browse files Browse the repository at this point in the history
  • Loading branch information
fsxbhyy committed Oct 23, 2023
1 parent 071ce89 commit fc48c3d
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 62 deletions.
23 changes: 14 additions & 9 deletions example/taylor_expansion.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FeynmanDiagram
using FeynmanDiagram.Taylor
using FeynmanDiagram.ComputationalGraphs:
eval!, forwardAD, node_derivative, backAD, build_all_leaf_derivative, count_operation
using FeynmanDiagram.Utility:
Expand All @@ -24,25 +25,29 @@ using FeynmanDiagram.Taylor:
# set_variables("x y", order=3)
# @time T5 = taylorexpansion!(G5)
# print(T5)
set_variables("x", order=3)
set_variables("x y z a", order=7)
@time T5 = taylorexpansion!(G6)
#order = [0, 0, 0, 0, 0, 0]
#@time print(T5.coeffs[order])
print("$(count_operation(T5.coeffs))\n")
for (order, coeff) in (T5.coeffs)
#gs = Compilers.to_julia_str([coeff,], name="eval_graph!")
#println("$(order) ", gs, "\n")
print("$(order) $(eval!(coeff)) $(eval!(getcoeff(T5,order))) $(coeff.id) $(count_operation(coeff))\n")
end
# for (order, coeff) in (T5.coeffs)
# #gs = Compilers.to_julia_str([coeff,], name="eval_graph!")
# #println("$(order) ", gs, "\n")
# print("$(order) $(eval!(coeff)) $(eval!(getcoeff(T5,order))) $(coeff.id) $(count_operation(coeff))\n")
# end

print("TaylorSeries $(T5)\n")

@time T5_compare = build_derivative_backAD!(G6)
print("$(count_operation(T5_compare.coeffs))\n")
for (order, coeff) in (T5_compare.coeffs)
gs = Compilers.to_julia_str([coeff,], name="eval_graph!")
println("$(order) ", gs, "\n")
print("$(order) $(eval!(coeff)) $(eval!(getderivative(T5,order))) $(count_operation(coeff))\n")
@assert (eval!(coeff)) == (eval!(Taylor.taylor_factorial(order) * T5.coeffs[order]))
# gs = Compilers.to_julia_str([coeff,], name="eval_graph!")
# println("$(order) ", gs, "\n")
# print("$(order) $(eval!(coeff)) $(eval!(getderivative(T5,order))) $(count_operation(coeff))\n")
end





4 changes: 2 additions & 2 deletions src/FeynmanDiagram.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export optimize!, optimize, merge_all_chains!, merge_all_linear_combinations!, r
include("TaylorSeries/TaylorSeries.jl")
using .Taylor
export Taylor
export TaylorSeries, set_variables, taylor_factorial, getcoeff, getderivative
export TaylorSeries, set_variables, taylor_factorial, getcoeff


include("backend/compiler.jl")
Expand Down Expand Up @@ -181,7 +181,7 @@ export evalNaive, showTree
include("utility.jl")
using .Utility
export Utility
export taylorexpansion!, build_derivative_backAD!
export taylorexpansion!
##################### precompile #######################
# precompile as the final step of the module definition:
if ccall(:jl_generating_output, Cint, ()) == 1 # if we're precompiling the package
Expand Down
2 changes: 1 addition & 1 deletion src/TaylorSeries/TaylorSeries.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export get_order, get_numvars,
get_variable_names, get_variable_symbols,
# jacobian, hessian, jacobian!, hessian!,
displayBigO, use_show_default,
getcoeff, getderivative, taylor_factorial
getcoeff, taylor_factorial
# function __init__()
# @static if !isdefined(Base, :get_extension)
# @require IntervalArithmetic = "d1acc4aa-44c8-5952-acd4-ba5d80a2a253" begin
Expand Down
96 changes: 79 additions & 17 deletions src/TaylorSeries/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function Base.:*(c1::Number, g2::TaylorSeries{T}) where {T}
end

"""
function Base.:+(g1::TaylorSeries{T,V}, g2::TaylorSeries{T,V}) where {T,V}
function Base.:+(g1::TaylorSeries{T}, g2::TaylorSeries{T}) where {T}
Returns a taylor series `g1 + g2` representing the addition of `g2` with `g1`.
Expand All @@ -57,13 +57,13 @@ end


"""
function Base.:+(g1::TaylorSeries{T,V}, g2::TaylorSeries{T,V}) where {T,V}
Returns a taylor series `g1 + g2` representing the addition of `g2` with `g1`.
function Base.:+(g1::TaylorSeries{T}, c::S) where {T,S<:Number}
Returns a taylor series `g1 + c` representing the addition of constant `c` with `g1`.
# Arguments:
- `g1` First taylor series
- `g2` Second taylor series
- `g1` Taylor series
- `c` Constant
"""
function Base.:+(g1::TaylorSeries{T}, c::S) where {T,S<:Number}
g = TaylorSeries{T}()
Expand All @@ -79,6 +79,16 @@ function Base.:+(g1::TaylorSeries{T}, c::S) where {T,S<:Number}
return g
end


"""
function Base.:+(c::S, g1::TaylorSeries{T}) where {S<:Number,T}
Returns a taylor series `g1 + c` representing the addition of constant `c` with `g1`.
# Arguments:
- `g1` Taylor series
- `c` Constant
"""
function Base.:+(c::S, g1::TaylorSeries{T}) where {S<:Number,T}
g = TaylorSeries{T}()
g.coeffs = copy(g1.coeffs)
Expand Down Expand Up @@ -110,7 +120,15 @@ function Base.:-(g1::TaylorSeries{T}, c::S) where {T,S<:Number}
return g1 + (-1 * c)
end

"""
function taylor_binomial(o1::Array{Int,1}, o2::Array{Int,1})
Return the taylor binomial prefactor when product two high-order derivatives with order o1 and o2.
# Arguments:
- `o1` Order of first derivative
- `o2` Order of second derivative
"""
function taylor_binomial(o1::Array{Int,1}, o2::Array{Int,1})
@assert length(o1) == length(o2)
result = 1
Expand All @@ -123,15 +141,25 @@ function taylor_binomial(o1::Array{Int,1}, o2::Array{Int,1})
return result
end


"""
function taylor_factorial(o::Array{Int,1})
Return the taylor factorial prefactor with order o.
# Arguments:
- `o` Order of the taylor coefficient
"""
function taylor_factorial(o::Array{Int,1})
result = 1
for i in eachindex(o)
result *= factorial(o[i])
end
return result
end

"""
function Base.:*(g1::TaylorSeries{T,V}, g2::TaylorSeries{T,V}) where {T,V}
function Base.:*(g1::TaylorSeries{T}, g2::TaylorSeries{T}) where {T}
Returns a taylor series `g1 * g2` representing the product of `g2` with `g1`.
Expand Down Expand Up @@ -159,34 +187,66 @@ function Base.:*(g1::TaylorSeries{T}, g2::TaylorSeries{T}) where {T}
return g
end

# function findidx(a::TaylorSeries, o::Array{Int,1})
# @assert length(o) == get_numvars()
# return findfirst(isequal(o), a.order)
# end

"""
function getcoeff(g::TaylorSeries, order::Array{Int,1})
Return the taylor coefficients with given order in taylor series g.
# Arguments:
- `g` Taylor series
- `order` Order of target coefficients
"""
function getcoeff(g::TaylorSeries, order::Array{Int,1})
if haskey(g.coeffs, order)
return g.coeffs[order]
#return 1 / taylor_factorial(order) * g.coeffs[order]
else
return nothing
end
end

"""
function getderivative(g::TaylorSeries, order::Array{Int,1})
Return the derivative with given order in taylor series g.
# Arguments:
- `g` Taylor series
- `order` Order of derivative
"""
function getderivative(g::TaylorSeries, order::Array{Int,1})
if haskey(g.coeffs, order)
#return g.coeffs[order]
return taylor_factorial(order) * g.coeffs[order]
else
return nothing
end
end

function Base.one(x::TaylorSeries{T}) where {T}
g = TaylorSeries{T}()
g.coeffs[zeros(Int, get_numvars)] = one(T)
return g

"""
function Base.one(g::TaylorSeries{T}) where {T}
Return a constant one for a given taylor series.
# Arguments:
- `g` Taylor series
"""
function Base.one(g::TaylorSeries{T}) where {T}
unity = TaylorSeries{T}()
unity.coeffs[zeros(Int, get_numvars)] = one(T)
return unity
end


"""
function Base.:^(x::TaylorSeries, p::Integer)
Return the power of taylor series x^p, where p is an integer.
# Arguments:
- `x` Taylor series
- 'p' Power index
"""
function Base.:^(x::TaylorSeries, p::Integer)
p == 1 && return copy(x)
p == 0 && return one(x)
Expand All @@ -198,6 +258,8 @@ end
function square(x::TaylorSeries)
return x * x
end

# power_by_squaring; slightly modified from base/intfuncs.jl
function power_by_squaring(x::TaylorSeries, p::Integer)
p == 1 && return copy(x)
p == 0 && return one(x)
Expand Down
6 changes: 3 additions & 3 deletions src/TaylorSeries/parameter.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""
ParamsTaylor
DataType holding the current parameters for `TaylorSeries`. This part of code
DataType holding the current parameters for `TaylorSeries`.
This part of code is adopted from TaylorSeries.jl (https://github.com/JuliaDiff/TaylorSeries.jl)
**Fields:**
Expand Down Expand Up @@ -39,11 +40,10 @@ end
"""
set_variables([T::Type], names::String; [order=get_order(), numvars=-1])
Return a `Taylor{T}` vector with each entry representing an
Return a `TaylorSeries{T}` vector with each entry representing an
independent variable. `names` defines the output for each variable
(separated by a space). The default type `T` is `Float64`,
and the default for `order` is the one defined globally.
Changing the `order` or `numvars` resets the hash_tables.
If `numvars` is not specified, it is inferred from `names`. If only
one variable name is defined and `numvars>1`, it uses this name with
Expand Down
2 changes: 1 addition & 1 deletion src/TaylorSeries/print.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This file is part of the TaylorSeries.jl Julia package, MIT license
#This part of code is adopted from https://github.com/JuliaDiff/TaylorSeries.jl

# Control the display of the big 𝒪 notation
const bigOnotation = Bool[true]
Expand Down
Loading

0 comments on commit fc48c3d

Please sign in to comment.