Skip to content

Commit 71c1e71

Browse files
authored
Merge pull request #265 from jverzani/symbolic-utils-extension
Symbolic utils extension
2 parents dde2542 + c9a219c commit 71c1e71

File tree

4 files changed

+167
-1
lines changed

4 files changed

+167
-1
lines changed

Project.toml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
1111
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
1212
SymEngine_jll = "3428059b-622b-5399-b16f-d347a77089a4"
1313

14+
[weakdeps]
15+
SymbolicUtils = "d1185830-fcd6-423d-90d6-eec64667417b"
16+
17+
[extensions]
18+
SymEngineSymbolicUtilsExt = "SymbolicUtils"
19+
1420
[compat]
1521
Compat = "0.63.0, 1, 2, 3, 4"
1622
RecipesBase = "0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 1.0"
@@ -20,6 +26,7 @@ julia = "1.6"
2026

2127
[extras]
2228
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
29+
SymbolicUtils = "d1185830-fcd6-423d-90d6-eec64667417b"
2330

2431
[targets]
25-
test = ["Test"]
32+
test = ["SymbolicUtils", "Test"]

ext/SymEngineSymbolicUtilsExt.jl

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
module SymEngineSymbolicUtilsExt
2+
3+
using SymEngine
4+
using SymbolicUtils
5+
import SymEngine: SymbolicType
6+
7+
#
8+
function is_number(a::SymEngine.Basic)
9+
cls = SymEngine.get_symengine_class(a)
10+
any(==(cls), SymEngine.number_types) && return true
11+
false
12+
end
13+
14+
15+
λ(x::SymEngine.SymbolicType) = λ(Val(SymEngine.get_symengine_class(x)))
16+
λ(::Val{T}) where {T} = getfield(Main, Symbol(lowercase(string(T))))
17+
18+
λ(::Val{:Add}) = +; λ(::Val{:Sub}) = -
19+
λ(::Val{:Mul}) = *; λ(::Val{:Div}) = /
20+
λ(::Val{:Pow}) = ^
21+
λ(::Val{:re}) = real; λ(::Val{:im}) = imag
22+
λ(::Val{:Abs}) = abs
23+
λ(::Val{:Log}) = log
24+
λ(::Val{:Sin}) = sin; λ(::Val{:Cos}) = cos; λ(::Val{:Tan}) = tan
25+
λ(::Val{:Csc}) = csc; λ(::Val{:Sec}) = sec; λ(::Val{:Cot}) = cot
26+
λ(::Val{:Asin}) = asin; λ(::Val{:Acos}) = acos; λ(::Val{:Atan}) = atan
27+
λ(::Val{:Acsc}) = acsc; λ(::Val{:Asec}) = asec; λ(::Val{:Acot}) = acot
28+
λ(::Val{:Sinh}) = sinh; λ(::Val{:Cosh}) = cosh; λ(::Val{:Tanh}) = tanh
29+
λ(::Val{:Csch}) = csch; λ(::Val{:Sech}) = sech; λ(::Val{:Coth}) = coth
30+
λ(::Val{:Asinh}) = asinh; λ(::Val{:Acosh}) = acosh; λ(::Val{:Atanh}) = atanh
31+
λ(::Val{:Acsch}) = acsch; λ(::Val{:Asech}) = asech; λ(::Val{:Acoth}) = acoth
32+
λ(::Val{:Gamma}) = gamma; λ(::Val{:Zeta}) = zeta; λ(::Val{:LambertW}) = lambertw
33+
34+
#==
35+
Check if x represents an expression tree. If returns true, it will be assumed that operation(::T) and arguments(::T) methods are defined. Definining these three should allow use of SymbolicUtils.simplify on custom types. Optionally symtype(x) can be defined to return the expected type of the symbolic expression.
36+
==#
37+
function SymbolicUtils.istree(x::SymEngine.SymbolicType)
38+
cls = SymEngine.get_symengine_class(x)
39+
cls == :Symbol && return false
40+
any(==(cls), SymEngine.number_types) && return false
41+
return true
42+
end
43+
44+
SymbolicUtils.issym(x::SymEngine.SymbolicType) = SymEngine.get_symengine_class(x) == :Symbol
45+
Base.nameof(x::SymEngine.SymbolicType) = Symbol(x)
46+
47+
# no metadata(x), metadata(x, data)
48+
49+
#==
50+
Returns the head (a function object) performed by an expression tree. Called only if istree(::T) is true. Part of the API required for simplify to work. Other required methods are arguments and istree
51+
==#
52+
function SymbolicUtils.operation(x::SymEngine.SymbolicType)
53+
istree(x) || error("$(typeof(x)) doesn't have an operation!")
54+
return λ(x)
55+
end
56+
57+
58+
#==
59+
Returns the arguments (a Vector) for an expression tree. Called only if istree(x) is true. Part of the API required for simplify to work. Other required methods are operation and istree
60+
==#
61+
function SymbolicUtils.arguments(x::SymEngine.SymbolicType)
62+
get_args(x)
63+
end
64+
65+
#==
66+
Construct a new term with the operation f and arguments args, the term should be similar to t in type. if t is a SymbolicUtils.Term object a new Term is created with the same symtype as t. If not, the result is computed as f(args...). Defining this method for your term type will reduce any performance loss in performing f(args...) (esp. the splatting, and redundant type computation). T is the symtype of the output term. You can use SymbolicUtils.promote_symtype to infer this type. The exprhead keyword argument is useful when creating Exprs.
67+
==#
68+
function SymbolicUtils.similarterm(t::SymEngine.SymbolicType, f, args, symtype=nothing;
69+
metadata=nothing, exprhead=:call)
70+
f(args...) # default
71+
end
72+
73+
# Needed for some simplification routines
74+
# a total order <ₑ
75+
import SymbolicUtils: <ₑ, isterm, isadd, ismul, issym, cmp_mul_adds, cmp_term_term
76+
function SymbolicUtils.:<(a::SymEngine.Basic, b::SymEngine.Basic)
77+
if isterm(a) && !isterm(b)
78+
return false
79+
elseif isterm(b) && !isterm(a)
80+
return true
81+
elseif (isadd(a) || ismul(a)) && (isadd(b) || ismul(b))
82+
return cmp_mul_adds(a, b)
83+
elseif issym(a) && issym(b)
84+
nameof(a) < nameof(b)
85+
elseif !istree(a) && !istree(b)
86+
T = typeof(a)
87+
S = typeof(b)
88+
if T == S
89+
is_number(a) && is_number(b) && return N(a) < N(b)
90+
return hash(a) < hash(b)
91+
else
92+
return name(T) < nameof(S)
93+
end
94+
#return T===S ? (T <: Number ? isless(a, b) : hash(a) < hash(b)) : nameof(T) < nameof(S)
95+
elseif istree(b) && !istree(a)
96+
return true
97+
elseif istree(a) && istree(b)
98+
return cmp_term_term(a,b)
99+
else
100+
return !(b <ₑ a)
101+
end
102+
end
103+
104+
end

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ using Test
44
using Serialization
55
import Base: MathConstants.γ, MathConstants.e, MathConstants.φ, MathConstants.catalan
66

7+
VERSION >= v"1.9" && include("test-SymbolicUtils.jl")
78
include("test-dense-matrix.jl")
89

910
x = symbols("x")

test/test-SymbolicUtils.jl

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using Test
2+
using SymEngine
3+
import SymbolicUtils: simplify, @rule, @acrule, Chain, Fixpoint
4+
5+
6+
@testset "SymbolicUtils" begin
7+
# from SymbolicUtils.jl docs
8+
# https://symbolicutils.juliasymbolics.org/rewrite/#rule-based_rewriting
9+
@vars w x y z
10+
@vars α β
11+
@vars a b c d
12+
13+
@test simplify(cos(x)^2 + sin(x)^2) == 1
14+
15+
r1 = @rule sin(2(~x)) => 2sin(~x)*cos(~x)
16+
@test r1(sin(2z)) == 2*cos(z)*sin(z)
17+
@test r1(sin(3z)) === nothing
18+
@test r1(sin(2*(w-z))) == 2cos(w - z)*sin(w - z)
19+
@test r1(sin(2*(w+z)*+β))) === nothing
20+
21+
r2 = @rule sin(~x + ~y) => sin(~x)*cos(~y) + cos(~x)*sin(~y);
22+
@test r2(sin+β)) == sin(α)*cos(β) + cos(α)*sin(β)
23+
24+
xs = @rule(+(~~xs) => ~~xs)(x + y + z) # segment variable
25+
@test Set(xs) == Set([x,y,z])
26+
27+
r3 = @rule ~x * +(~~ys) => sum(map(y-> ~x * y, ~~ys));
28+
@test r3(2 * (w+w+α+β)) == 4w + 2α + 2β
29+
30+
r4 = @rule ~x + ~~y::(ys->iseven(length(ys))) => "odd terms"; # Predicates for matching
31+
32+
@test r4(a + b + c + d) == nothing
33+
@test r4(b + c + d) == "odd terms"
34+
@test r4(b + c + b) == nothing
35+
@test r4(a + b) == nothing
36+
37+
sqexpand = @rule (~x + ~y)^2 => (~x)^2 + (~y)^2 + 2 * ~x * ~y
38+
@test sqexpand((cos(x) + sin(x))^2) == cos(x)^2 + sin(x)^2 + 2cos(x)*sin(x)
39+
40+
pyid = @rule sin(~x)^2 + cos(~x)^2 => 1
41+
@test_broken pyid(cos(x)^2 + sin(x)^2) === nothing # order should matter, but this works
42+
43+
acpyid = @acrule sin(~x)^2 + cos(~x)^2 => 1 # acrule is commutative
44+
@test acpyid(cos(x)^2 + sin(x)^2 + 2cos(x)*sin(x)) == 1 + 2cos(x)*sin(x)
45+
46+
csa = Chain([sqexpand, acpyid]) # chain composes rules
47+
@test csa((cos(x) + sin(x))^2) == 1 + 2cos(x)*sin(x)
48+
49+
cas = Chain([acpyid, sqexpand]) # order matters
50+
@test cas((cos(x) + sin(x))^2) == cos(x)^2 + sin(x)^2 + 2cos(x)*sin(x)
51+
52+
@test Fixpoint(cas)((cos(x) + sin(x))^2) == 1 + 2cos(x)*sin(x)
53+
54+
end

0 commit comments

Comments
 (0)