Skip to content

Commit

Permalink
add documentation; use random number in some test.
Browse files Browse the repository at this point in the history
  • Loading branch information
fsxbhyy committed Nov 12, 2023
1 parent b62b821 commit e778e58
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 14 deletions.
68 changes: 58 additions & 10 deletions src/utility.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,16 @@ using ..Taylor
@inline apply(::Type{DiagTree.Prod}, diags::Vector{T}, factors::Vector{F}) where {T<:TaylorSeries,F<:Number} = prod(d * f for (d, f) in zip(diags, factors))


# 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}
Return a taylor series of graph g, together with a map of between nodes of g and correponding taylor series.
# Arguments:
- `graph` Target graph
- `var_dependence::Dict{Int,Vector{Bool}}` A dictionary that specifies the variable dependence of target graph leaves. Should map the id of each leaf to a Bool vector.
The length of the vector should be the same as number of variables.
- `taylormap::Dict{Int,TaylorSeries}` A dicitonary that maps id of each node of target graph to its correponding taylor series.
"""
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], taylormap
Expand Down Expand Up @@ -48,6 +57,16 @@ function taylorexpansion!(graph::G, var_dependence::Dict{Int,Vector{Bool}}=Dict{
end
end

"""
function taylorexpansion!(graph::FeynmanGraph{F,W}, var_dependence::Dict{Int,Vector{Bool}}=Dict{Int,Vector{Bool}}(); taylormap::Dict{Int,TaylorSeries{G}}=Dict{Int,TaylorSeries{G}}()) where {F,W}
Return a taylor series of FeynmanGraph g, together with a map of between nodes of g and correponding taylor series.
# Arguments:
- `graph` Target FeynmanGraph
- `var_dependence::Dict{Int,Vector{Bool}}` A dictionary that specifies the variable dependence of target graph leaves. Should map the id of each leaf to a Bool vector.
The length of the vector should be the same as number of variables.
- `taylormap::Dict{Int,TaylorSeries}` A dicitonary that maps id of each node of target graph to its correponding taylor series.
"""
function taylorexpansion!(graph::FeynmanGraph{F,W}, var_dependence::Dict{Int,Vector{Bool}}=Dict{Int,Vector{Bool}}(); taylormap::Dict{Int,TaylorSeries{Graph{F,W}}}=Dict{Int,TaylorSeries{Graph{F,W}}}()) where {F,W}
if haskey(taylormap, graph.id) #If already exist, use taylor series in taylormap.
return taylormap[graph.id], taylormap
Expand All @@ -73,8 +92,16 @@ function taylorexpansion!(graph::FeynmanGraph{F,W}, var_dependence::Dict{Int,Vec
end
end



"""
function taylorexpansion!(graph::Diagram{W}, var_dependence::Dict{Int,Vector{Bool}}=Dict{Int,Vector{Bool}}(); taylormap::Dict{Int,TaylorSeries{G}}=Dict{Int,TaylorSeries{G}}()) where {W}
Return a taylor series of Diagram g, together with a map of between nodes of g and correponding taylor series.
# Arguments:
- `graph` Target diagram
- `var_dependence::Dict{Int,Vector{Bool}}` A dictionary that specifies the variable dependence of target diagram leaves. Should map the id of each leaf to a Bool vector.
The length of the vector should be the same as number of variables.
- `taylormap::Dict{Int,TaylorSeries}` A dicitonary that maps id of each node of target diagram to its correponding taylor series.
"""
function taylorexpansion!(graph::Diagram{W}, var_dependence::Dict{Int,Vector{Bool}}=Dict{Int,Vector{Bool}}(); taylormap::Dict{Int,TaylorSeries{Graph{W,W}}}=Dict{Int,TaylorSeries{Graph{W,W}}}()) where {W}
if haskey(taylormap, graph.hash) #If already exist, use taylor series in taylormap.
return taylormap[graph.hash], taylormap
Expand All @@ -100,13 +127,25 @@ function taylorexpansion!(graph::Diagram{W}, var_dependence::Dict{Int,Vector{Boo
end
end

"""
function taylorexpansion!(graph::FeynmanGraph{F,W}, propagator_var::Tuple{Vector{Bool},Vector{Bool}}, label::Tuple{LabelProduct,LabelProduct}; taylormap::Dict{Int,TaylorSeries{Graph{F,W}}}=Dict{Int,TaylorSeries{Graph{F,W}}}()) where {F,W}
Return a taylor series of FeynmanGraph g, together with a map of between nodes of g and correponding taylor series. In this set up, the leaves that are the same type of propagators (fermi or bose) depend on the same set of variables,
whereas other types of Feynman diagrams (such as vertices) depends on no variables that need to be differentiated (for AD purpose, they are just constants).
# Arguments:
- `graph` Target FeynmanGraph
- `propagator_var::Tuple{Vector{Bool},Vector{Bool}}` A Tuple that specifies the variable dependence of fermi (first element) and bose propagator (second element).
The dependence is given by a vector of the length same as the number of variables.
- `label::Tuple{LabelProduct,LabelProduct}` A Tuple fermi (first element) and bose LabelProduct (second element).
- `taylormap::Dict{Int,TaylorSeries}` A dicitonary that maps id of each node of target diagram to its correponding taylor series.
"""
function taylorexpansion!(graph::FeynmanGraph{F,W}, propagator_var::Tuple{Vector{Bool},Vector{Bool}}, label::Tuple{LabelProduct,LabelProduct}; taylormap::Dict{Int,TaylorSeries{Graph{F,W}}}=Dict{Int,TaylorSeries{Graph{F,W}}}()) where {F,W}
var_dependence = Dict{Int,Vector{Bool}}()
for leaf in Leaves(graph)
if ComputationalGraphs.diagram_type(leaf) == ComputationalGraphs.Propagator
In = leaf.properties.vertices[2][1].label
if isfermionic(leaf.properties.vertices[1])
if label[1][In][2] >= 0
if label[1][In][2] >= 0 #For fake propagator, this label is smaller than zero, and those propagators should not be differentiated.
var_dependence[leaf.id] = [propagator_var[1][idx] ? true : false for idx in 1:get_numvars()]
end
else
Expand All @@ -117,13 +156,22 @@ function taylorexpansion!(graph::FeynmanGraph{F,W}, propagator_var::Tuple{Vector
return taylorexpansion!(graph, var_dependence; taylormap=taylormap)
end

function taylorexpansion!(graph::Diagram{W}, propagator_var::Tuple{Vector{Bool},Vector{Bool}}; taylormap::Dict{Int,TaylorSeries{Graph{W,W}}}=Dict{Int,TaylorSeries{Graph{W,W}}}()) where {W}
"""
function taylorexpansion!(graph::Diagram{W}, propagator_var::Dict{DataType,Vector{Bool}}; taylormap::Dict{Int,TaylorSeries{Graph{W,W}}}=Dict{Int,TaylorSeries{Graph{W,W}}}()) where {W}
Return a taylor series of Diagram g, together with a map of between nodes of g and correponding taylor series. In this set up, the leaves that are the same type of diagrams (such as Green functions) depend on the same set of variables.
# Arguments:
- `graph` Target Diagram
- `propagator_var::Dict{DataType,Vector{Bool}}` A dictionary that specifies the variable dependence of different types of diagrams. Should be a map between DataTypes in DiagramID and Bool vectors.
The dependence is given by a vector of the length same as the number of variables.
- `taylormap::Dict{Int,TaylorSeries}` A dicitonary that maps id of each node of target diagram to its correponding taylor series.
"""
function taylorexpansion!(graph::Diagram{W}, propagator_var::Dict{DataType,Vector{Bool}}; taylormap::Dict{Int,TaylorSeries{Graph{W,W}}}=Dict{Int,TaylorSeries{Graph{W,W}}}()) where {W}
var_dependence = Dict{Int,Vector{Bool}}()
for leaf in Leaves(graph)
if leaf.id isa BareGreenId
var_dependence[leaf.hash] = [propagator_var[1][idx] ? true : false for idx in 1:get_numvars()]
elseif leaf.id isa BareInteractionId
var_dependence[leaf.hash] = [propagator_var[2][idx] ? true : false for idx in 1:get_numvars()]
if haskey(propagator_var, typeof(leaf.id))
var_dependence[leaf.hash] = [propagator_var[typeof(leaf.id)][idx] ? true : false for idx in 1:get_numvars()]
end
end
return taylorexpansion!(graph, var_dependence; taylormap=taylormap)
Expand Down Expand Up @@ -230,7 +278,7 @@ function build_derivative_backAD!(g::G, leaftaylor::Dict{Int,TaylorSeries{G}}=Di
end
current_func = new_func
end
return result
return result, leaftaylor
end


Expand Down
30 changes: 26 additions & 4 deletions test/taylor.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
using FeynmanDiagram
using FeynmanDiagram: Taylor as Taylor

using ..DiagTree
function assign_random_numbers(g, taylormap1, taylormap2) #Benchmark taylor expansion generated by two different methods
leafmap1 = Dict{Int,Int}()
leafvec1 = Vector{Float64}()
leafmap2 = Dict{Int,Int}()
leafvec2 = Vector{Float64}()
idx = 0
for leaf in Leaves(g)
taylor1 = taylormap1[leaf.id]
taylor2 = taylormap2[leaf.id]
for (order, coeff) in taylor1.coeffs
idx += 1
num = rand()
push!(leafvec1, num)
push!(leafvec2, num)
leafmap1[coeff.id] = idx
leafmap2[taylor2.coeffs[order].id] = idx
#print("assign $(order) $(coeff.id) $(taylor_factorial(order)) $(leafvec[idx])\n")
end
end
return leafmap1, leafvec1, leafmap2, leafvec2
end
@testset verbose = true "TaylorSeries" begin
using FeynmanDiagram.Taylor:
getcoeff, set_variables, taylor_factorial, get_numvars
Expand Down Expand Up @@ -42,9 +63,10 @@ using FeynmanDiagram: Taylor as Taylor
end
end
T, taylormap = taylorexpansion!(G, var_dependence)
T_compare = build_derivative_backAD!(G)
T_compare, taylormap_compare = build_derivative_backAD!(G)
leafmap1, leafvec1, leafmap2, leafvec2 = assign_random_numbers(G, taylormap, taylormap_compare)
for (order, coeff) in T_compare.coeffs
@test eval!(coeff) == eval!(taylor_factorial(order) * T.coeffs[order])
@test eval!(coeff, leafmap2, leafvec2) taylor_factorial(order) * eval!(T.coeffs[order], leafmap1, leafvec1)
end
end

Expand Down Expand Up @@ -183,7 +205,7 @@ end

set_variables("x y"; orders=[2, 2])

propagator_var = ([true, false], [false, true]) # Specify variable dependence of fermi (first element) and bose (second element) particles.
propagator_var = Dict(DiagTree.BareGreenId => [true, false], DiagTree.BareInteractionId => [false, true]) # Specify variable dependence of fermi (first element) and bose (second element) particles.
t, taylormap = taylorexpansion!(root, propagator_var)
taylorleafmap, taylorleafvec = assign_leaves(root, taylormap)
@test eval!(t.coeffs[[0, 0]], taylorleafmap, taylorleafvec) root.weight
Expand Down

0 comments on commit e778e58

Please sign in to comment.