From c731c69f0247592bd207a853645628b901271201 Mon Sep 17 00:00:00 2001 From: dcerkoney Date: Thu, 26 Oct 2023 01:41:33 -0400 Subject: [PATCH] Bugfix --- src/computational_graph/abstractgraph.jl | 2 ++ src/computational_graph/conversions.jl | 14 +++++------ test/computational_graph.jl | 32 ++++++++++++------------ 3 files changed, 25 insertions(+), 23 deletions(-) diff --git a/src/computational_graph/abstractgraph.jl b/src/computational_graph/abstractgraph.jl index fa3210ad..66651d27 100644 --- a/src/computational_graph/abstractgraph.jl +++ b/src/computational_graph/abstractgraph.jl @@ -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)) diff --git a/src/computational_graph/conversions.jl b/src/computational_graph/conversions.jl index d45c68a0..13feae30 100644 --- a/src/computational_graph/conversions.jl +++ b/src/computational_graph/conversions.jl @@ -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!") diff --git a/test/computational_graph.jl b/test/computational_graph.jl index 09622555..0bc50a70 100644 --- a/test/computational_graph.jl +++ b/test/computational_graph.jl @@ -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 @@ -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