From e4c328103ece494eaaab2a265524a64bfbe43dbd Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Sun, 23 Jun 2024 11:41:35 -0400 Subject: [PATCH] Automatically unwrap and wrap Nums for rule application (#1176) --- src/rewrite-helpers.jl | 2 ++ test/rewrite_helpers.jl | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/src/rewrite-helpers.jl b/src/rewrite-helpers.jl index 2afc6d4b0..f3279f8bd 100644 --- a/src/rewrite-helpers.jl +++ b/src/rewrite-helpers.jl @@ -121,6 +121,8 @@ function filterchildren!(r::Any, y, acc) end end +(f::SymbolicUtils.Rule)(x::Num) = wrap(f(unwrap(x))) + module RewriteHelpers import Symbolics: replacenode, hasnode, filterchildren, unwrap export replacenode, hasnode, filterchildren, unwrap diff --git a/test/rewrite_helpers.jl b/test/rewrite_helpers.jl index 1a804ac5a..61c7fc21d 100644 --- a/test/rewrite_helpers.jl +++ b/test/rewrite_helpers.jl @@ -115,3 +115,9 @@ let @test isequal(filterchildren(is_derivative, ex4), [D(Y), D(my_f(1,Z))]) end +# https://github.com/JuliaSymbolics/Symbolics.jl/issues/1175 +let + @variables w z α::Real β::Real; + r3 = @rule ~x * +(~~ys) => sum(map(y-> ~x * y, ~~ys)); + @test r3(2 * (w+w+α+β)) isa Num +end \ No newline at end of file