Skip to content

Commit

Permalink
fix typo and api
Browse files Browse the repository at this point in the history
  • Loading branch information
fsxbhyy committed Nov 3, 2023
1 parent 45ccbe8 commit d5e3993
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 75 deletions.
45 changes: 4 additions & 41 deletions example/taylor_expansion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ using FeynmanDiagram.Taylor
using FeynmanDiagram.ComputationalGraphs:
eval!, forwardAD, node_derivative, backAD, build_all_leaf_derivative
using FeynmanDiagram.Utility:
taylorexpansion!, taylorexpansion, build_derivative_backAD!, count_operation
taylorexpansion!, build_derivative_backAD!, count_operation

function banchmark_AD(glist::Vector{T}) where {T<:Graph}
function benchmark_AD(glist::Vector{T}) where {T<:Graph}
taylormap = Dict{Int,TaylorSeries{T}}()
totaloperation = [0, 0]
taylorlist = Vector{TaylorSeries{T}}()
Expand All @@ -20,9 +20,6 @@ function banchmark_AD(glist::Vector{T}) where {T<:Graph}
t_compare = build_derivative_backAD!(g)
for (order, coeff) in (t_compare.coeffs)
@assert (eval!(coeff)) == (eval!(Taylor.taylor_factorial(order) * t.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
end

Expand All @@ -39,49 +36,15 @@ g6 = Graph([])
G3 = g1
G4 = 1.0 * g1 * g2
G5 = 1.0 * (3.0 * G3 + 0.5 * G4)
#G5 = g5 * g6
#G6 = (0.5 * g1 * g2 + 0.5 * g2 * g3)
#G6 = (g1 + g2) * (0.5 * g1 + g3) * g1 # (0.5 * g1 + g3)
#G6 = g1 * g2 * g3 * g4 * g5 * g6
G6 = (1.0 * g1 + 2.0 * g2) * (g1 + g3)
#G6 = 1.5 * g1*g1 + 0.5 * g2 * 1.5 * g1 + 0.5*g2*g3

using FeynmanDiagram.Taylor:
TaylorSeries, getcoeff, set_variables


# set_variables("x y", order=3)
# @time T5 = taylorexpansion(G5)
# print(T5)
set_variables("x y", orders=[3, 2])
#set_variables("x y z a", order=[1, 2, 3, 2])


#@time taylormap = taylorexpansion(G6)


banchmark_AD([G3, G4, G5, G6])


# T5 = taylormap[G6.id]
# #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

# print("TaylorSeries $(T5)\n")
benchmark_AD([G3, G4, G5, G6])

# @time T5_compare = build_derivative_backAD!(G6)
# print("$(count_operation(T5_compare.coeffs))\n")
# for (order, coeff) in (T5_compare.coeffs)
# @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



Expand Down
4 changes: 2 additions & 2 deletions src/frontend/diagtree.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ end
# Arguments:
- `map::Dict{Int,DiagTree.DiagramId}`: A dictionary mapping graph ids to DiagramIds. DiagramId stores the diagram information of the corresponding graph.
- `ID`: The particular type of ID that has the given variable dependence.
- `numvars`: The number of varibles which the diagram depends on.
- `numvars`: The number of variables which the diagram depends on.
"""
function extract_var_dependence(map::Dict{Int,DiagTree.DiagramId}, ::Type{ID}, numvars::Int) where {ID<:PropagatorId}
function extract_var_dependence(map::Dict{Int,DiagTree.DiagramId}, ::Type{ID}; numvars::Int=1) where {ID<:PropagatorId}
var_dependence = Dict{Int,Vector{Bool}}()
for (id, diagID) in map
if diagID isa ID
Expand Down
33 changes: 7 additions & 26 deletions src/utility.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ using ..Taylor


# Internal function that performs taylor expansion on a single graph node recursively.
function _taylorexpansion!(graph::G, var_dependence::Dict{Int,Vector{Bool}}=Dict{Int,Vector{Bool}}(); taylormap::Dict{Int,TaylorSeries{G}}=Dict{Int,TaylorSeries{G}}()) where {G<:Graph}
function taylorexpansion!(graph::G, var_dependence::Dict{Int,Vector{Bool}}=Dict{Int,Vector{Bool}}(); taylormap::Dict{Int,TaylorSeries{G}}=Dict{Int,TaylorSeries{G}}()) where {G<:Graph}
if haskey(taylormap, graph.id) #If already exist, use taylor series in taylormap.
return taylormap[graph.id]
return taylormap[graph.id], taylormap

elseif isleaf(graph)
maxorder = get_orders()
Expand All @@ -28,10 +28,10 @@ function _taylorexpansion!(graph::G, var_dependence::Dict{Int,Vector{Bool}}=Dict
result.coeffs[o] = coeff
end
taylormap[graph.id] = result
return result
return result, taylormap
else
taylormap[graph.id] = apply(graph.operator, [_taylorexpansion!(sub, var_dependence; taylormap=taylormap) for sub in graph.subgraphs], graph.subgraph_factors)
return taylormap[graph.id]
taylormap[graph.id] = apply(graph.operator, [taylorexpansion!(sub, var_dependence; taylormap=taylormap)[1] for sub in graph.subgraphs], graph.subgraph_factors)
return taylormap[graph.id], taylormap
end
end

Expand All @@ -50,37 +50,18 @@ end
- `taylormap::Dict{Int,TaylorSeries{G}}` The taylor series correponding to graph nodes. Should map each graph node ID (does not need to belong to input graph) to a taylor series.
All new taylor series generated by taylor expansion will be added into the expansion.
"""
function taylorexpansion!(graph::G, var_dependence::Dict{Int,Vector{Bool}}=Dict{Int,Vector{Bool}}(); taylormap::Dict{Int,TaylorSeries{G}}=Dict{Int,TaylorSeries{G}}()) where {G<:Graph}
_taylorexpansion!(graph, var_dependence; taylormap=taylormap)
return taylormap[graph.id], taylormap
end


function taylorexpansion!(graphs::Vector{G}, var_dependence::Dict{Int,Vector{Bool}}=Dict{Int,Vector{Bool}}(); taylormap::Dict{Int,TaylorSeries{G}}=Dict{Int,TaylorSeries{G}}()) where {G<:Graph}
result = Vector{TaylorSeries{G}}()
for graph in graphs
push!(result, _taylorexpansion!(graph, var_dependence; taylormap=taylormap))
taylor, _ = taylorexpansion!(graph, var_dependence; taylormap=taylormap)
push!(result, taylor)
end
return result, taylormap
end


function taylorexpansion(graph::G, var_dependence::Dict{Int,Vector{Bool}}=Dict{Int,Vector{Bool}}()) where {G<:Graph}
taylormap = Dict{Int,TaylorSeries{G}}()
_taylorexpansion!(graph, var_dependence; taylormap=taylormap)
return taylormap[graph.id], taylormap
end


function taylorexpansion(graphs::Vector{G}, var_dependence::Dict{Int,Vector{Bool}}=Dict{Int,Vector{Bool}}()) where {G<:Graph}
taylormap = Dict{Int,TaylorSeries{G}}()
result = Vector{TaylorSeries{G}}()
for graph in graphs
push!(result, _taylorexpansion!(graph, var_dependence; taylormap=taylormap))
end
return taylormap[graph.id], taylormap
end

"""
taylorexpansion_withmap(g::G; coeffmode=true, var::Vector{Int}=collect(1:get_numvars())) where {G<:Graph}
Expand Down
12 changes: 6 additions & 6 deletions test/taylor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ using FeynmanDiagram: Taylor as Taylor
using FeynmanDiagram.ComputationalGraphs:
eval!, forwardAD, node_derivative, backAD, build_all_leaf_derivative, count_operation
using FeynmanDiagram.Utility:
taylorexpansion, build_derivative_backAD!
taylorexpansion!, build_derivative_backAD!
g1 = Graph([])
g2 = Graph([])
g3 = Graph([], factor=2.0)
Expand All @@ -34,7 +34,7 @@ using FeynmanDiagram: Taylor as Taylor

set_variables("x y z", orders=[2, 3, 2])
for G in [G3, G4, G5, G6]
T, taylormap = taylorexpansion(G)
T, taylormap = taylorexpansion!(G)
T_compare = build_derivative_backAD!(G)
for (order, coeff) in T_compare.coeffs
@test eval!(coeff) == eval!(taylor_factorial(order) * T.coeffs[order])
Expand Down Expand Up @@ -122,17 +122,17 @@ end

set_variables("x"; orders=[2])
g, map = FrontEnds.Graph!(root)
var_dependence = FrontEnds.extract_var_dependence(map, BareGreenId, 1)
t, taylormap = taylorexpansion(g, var_dependence)
var_dependence = FrontEnds.extract_var_dependence(map, BareGreenId)
t, taylormap = taylorexpansion!(g, var_dependence)
order = [0]
@test eval!(taylormap[g.id].coeffs[order]) (-2 + spin) * factor

order = [1]
@test eval!(taylormap[g.id].coeffs[order]) (-2 + spin) * factor * 2 * taylor_factorial(order)

var_dependence = FrontEnds.extract_var_dependence(map, BareInteractionId, 1)
var_dependence = FrontEnds.extract_var_dependence(map, BareInteractionId)

t, taylormap = taylorexpansion(g, var_dependence)
t, taylormap = taylorexpansion!(g, var_dependence)
order = [0]
@test eval!(taylormap[g.id].coeffs[order]) (-2 + spin) * factor

Expand Down

0 comments on commit d5e3993

Please sign in to comment.