Skip to content

Commit

Permalink
Bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
dcerkoney committed Oct 26, 2023
1 parent 91e447f commit c731c69
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
2 changes: 2 additions & 0 deletions src/computational_graph/abstractgraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ end
errmsg(G::Type) = "Method not yet implemented for user-defined graph type $G."
linear_combination(g1::G, g2::G, c1, c2) where {G<:AbstractGraph} = error(errmsg(G))
linear_combination(graphs::AbstractVector{G}, constants::AbstractVector) where {G<:AbstractGraph} = error(errmsg(G))
multi_product(g1::G, g2::G, c1, c2) where {G<:AbstractGraph} = error(errmsg(G))
multi_product(graphs::AbstractVector{G}, constants::AbstractVector) where {G<:AbstractGraph} = error(errmsg(G))
Base.:*(c1, g2::G) where {G<:AbstractGraph} = error(errmsg(G))
Base.:*(g1::G, c2) where {G<:AbstractGraph} = error(errmsg(G))
Base.:*(g1::G, g2::G) where {G<:AbstractGraph} = error(errmsg(G))
Expand Down
14 changes: 7 additions & 7 deletions src/computational_graph/conversions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ function Base.convert(::Type{FeynmanGraph}, g::Graph{F,W}) where {F,W}
)
end

# Automatically promote FeynmanGraph to Graph for arithmetic operations
# Automatically promote FeynmanGraph to Graph for vectors v::Vector{Union{Graph,FeynmanGraph}} and arithmetic operations
Base.promote_rule(::Type{Graph{F,W}}, ::Type{FeynmanGraph{F,W}}) where {F,W} = Graph{F,W}

# Arithmetic operations for mixed Graph/FeynmanGraph types
linear_combination(g1::Graph{F,W}, g2::FeynmanGraph{F,W}, c1=F(1), c2=F(1)) where {F,W} = linear_combination(Base.promote(g1, g2)..., c1, c2)
linear_combination(g1::FeynmanGraph{F,W}, g2::Graph{F,W}, c1=F(1), c2=F(1)) where {F,W} = linear_combination(Base.promote(g1, g2)..., c1, c2)
linear_combination(graphs::Vector{Union{Graph{F,W},FeynmanGraph{F,W}}}, constants::AbstractVector=ones(F, length(graphs))) where {F,W} = linear_combination(Base.promote(graphs)..., constants)
multi_product(g1::Graph{F,W}, g2::FeynmanGraph{F,W}, c1=F(1), c2=F(1)) where {F,W} = multi_product(Base.promote(g1, g2)..., c1, c2)
multi_product(g1::FeynmanGraph{F,W}, g2::Graph{F,W}, c1=F(1), c2=F(1)) where {F,W} = multi_product(Base.promote(g1, g2)..., c1, c2)
multi_product(graphs::Vector{Union{Graph{F,W},FeynmanGraph{F,W}}}, constants::AbstractVector=ones(F, length(graphs))) where {F,W} = multi_product(Base.promote(graphs)..., constants)
Base.:*(g1::Graph, g2::FeynmanGraph) = error("Multiplication of Feynman graphs is not well defined!")
Base.:*(g1::FeynmanGraph, g2::Graph) = error("Multiplication of Feynman graphs is not well defined!")
Base.:+(g1::Graph{F,W}, g2::FeynmanGraph{F,W}) where {F,W} = Base.:+(Base.promote(g1, g2)...)
Base.:+(g1::FeynmanGraph{F,W}, g2::Graph{F,W}) where {F,W} = Base.:+(Base.promote(g1, g2)...)
Base.:-(g1::Graph{F,W}, g2::FeynmanGraph{F,W}) where {F,W} = Base.:-(Base.promote(g1, g2)...)
Base.:-(g1::FeynmanGraph{F,W}, g2::Graph{F,W}) where {F,W} = Base.:-(Base.promote(g1, g2)...)

# No auto-promotion for graph products
multi_product(g1::Graph{F,W}, g2::FeynmanGraph{F,W}, c1=F(1), c2=F(1)) where {F,W} = error("Multiplication of Feynman graphs is not well defined!")
multi_product(g1::FeynmanGraph{F,W}, g2::Graph{F,W}, c1=F(1), c2=F(1)) where {F,W} = error("Multiplication of Feynman graphs is not well defined!")
Base.:*(g1::Graph{F,W}, g2::FeynmanGraph{F,W}) where {F,W} = error("Multiplication of Feynman graphs is not well defined!")
Base.:*(g1::FeynmanGraph{F,W}, g2::Graph{F,W}) where {F,W} = error("Multiplication of Feynman graphs is not well defined!")
32 changes: 16 additions & 16 deletions test/computational_graph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -868,38 +868,30 @@ end

# Test automatic FeynmanGraph -> Graph promotion in arithmetic operations
conversion_successful = true
local l1, l2, l3, l4, l5, l6, l7, r7, l8, r8
local l1, l2, l3, l4, l5
try
l1 = g_feyn + g1
l2 = g_feyn - g1
l3 = linear_combination(g_feyn, g1, 2, 3)
l4 = linear_combination(g_feyn, g, 2, 3)
l5 = linear_combination([g_feyn, g, g1, g2], [2, 5, 3, 9])
l6 = multi_product(g_feyn, g1, 2, 3)
l7 = multi_product(g_feyn, g, 2, 3)
l8 = multi_product([g_feyn, g, g1, g2], [2, 5, 3, 9])
r7 = multi_product(g, g, 2, 3)
r8 = multi_product([g, g, g1, g2], [2, 5, 3, 9])
catch
conversion_successful = false
end
@test conversion_successful
Graphs.optimize!([l1, l2, l3, l4, l5, l6, l7, r7, l8, r8]) # cache unique leaves

Graphs.optimize!([l1, l2, l3, l4, l5]) # cache unique leaves
@test isequiv(l1, g + g1, :id)
@test isequiv(l2, g - g1, :id)
@test isequiv(l3, 2 * g + 3 * g1, :id)
@test isequiv(l4, linear_combination([g], [5]), :id)
@test isequiv(l5, linear_combination([g, g1, g2], [7, 3, 9]), :id)
@test isequiv(l6, multi_product(g, g1, 2, 3), :id)

# TODO: Refine multiple Prod -> Power conversion
@test_broken isequiv(l7, r7, :id)
@test_broken isequiv(l8, r8, :id)

# FeynmanGraph multiplication is undefined
err1 = AssertionError()
err2 = AssertionError()
err3 = AssertionError()
err4 = AssertionError()
try
g * g_feyn
catch err1
Expand All @@ -908,10 +900,18 @@ end
g_feyn * g
catch err2
end
@test err1 isa ErrorException
@test err2 isa ErrorException
@test err1.msg == "Multiplication of Feynman graphs is not well defined!"
@test err2.msg == "Multiplication of Feynman graphs is not well defined!"
try
multi_product(g_feyn, g1, 2, 3)
catch err3
end
try
multi_product(g, g_feyn, 2, 3)
catch err4
end
errs = [err1, err2, err3, err4]
errmsg = "Multiplication of Feynman graphs is not well defined!"
@test all(err isa ErrorException for err in errs)
@test all(err.msg == errmsg for err in errs)
end

@testset verbose = true "Evaluation" begin
Expand Down

0 comments on commit c731c69

Please sign in to comment.