Skip to content

Commit 90e42f3

Browse files
authored
Merge pull request #268 from jverzani/symbolic-utils-extension_fix
Symbolic utils extension fix
2 parents 71c1e71 + a34c325 commit 90e42f3

File tree

4 files changed

+18
-26
lines changed

4 files changed

+18
-26
lines changed

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Compat = "0.63.0, 1, 2, 3, 4"
2222
RecipesBase = "0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 1.0"
2323
SpecialFunctions = "0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.10, 1, 2"
2424
SymEngine_jll = "0.9, 0.10"
25+
SymbolicUtils = "1.4"
2526
julia = "1.6"
2627

2728
[extras]

docs/Project.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
[deps]
22
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
33
SymEngine = "123dc426-2d89-5057-bbad-38513e3affd8"
4+
5+
[compat]
6+
Documenter = "1"

docs/src/basicUsage.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Vectors can be defined through list comprehension and string interpolation.
4343
julia> using SymEngine
4444
4545
julia> [symbols("α_$i") for i in 1:3]
46-
3-element Array{Basic,1}:
46+
3-element Vector{Basic}:
4747
α_1
4848
α_2
4949
α_3
@@ -56,7 +56,7 @@ In an analogous manner, matrices are declared with a combination of string inter
5656
julia> using SymEngine
5757
5858
julia> W = [symbols("W_$i$j") for i in 1:3, j in 1:4]
59-
3×4 Array{Basic,2}:
59+
3×4 Matrix{Basic}:
6060
W_11 W_12 W_13 W_14
6161
W_21 W_22 W_23 W_24
6262
W_31 W_32 W_33 W_34
@@ -70,13 +70,13 @@ Consider the canonical example of **matrix vector multiplication**.
7070
julia> using SymEngine
7171
7272
julia> W = [symbols("W_$i$j") for i in 1:3, j in 1:4]
73-
3×4 Array{Basic,2}:
73+
3×4 Matrix{Basic}:
7474
W_11 W_12 W_13 W_14
7575
W_21 W_22 W_23 W_24
7676
W_31 W_32 W_33 W_34
7777
7878
julia> W*[1.0, 2.0, 3.0, 4.0]
79-
3-element Array{Basic,1}:
79+
3-element Vector{Basic}:
8080
1.0*W_11 + 2.0*W_12 + 3.0*W_13 + 4.0*W_14
8181
1.0*W_21 + 2.0*W_22 + 3.0*W_23 + 4.0*W_24
8282
1.0*W_31 + 2.0*W_32 + 3.0*W_33 + 4.0*W_34

ext/SymEngineSymbolicUtilsExt.jl

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Check if x represents an expression tree. If returns true, it will be assumed th
3737
function SymbolicUtils.istree(x::SymEngine.SymbolicType)
3838
cls = SymEngine.get_symengine_class(x)
3939
cls == :Symbol && return false
40+
cls == :Constant && return false
4041
any(==(cls), SymEngine.number_types) && return false
4142
return true
4243
end
@@ -72,32 +73,19 @@ end
7273

7374
# Needed for some simplification routines
7475
# a total order <ₑ
75-
import SymbolicUtils: <ₑ, isterm, isadd, ismul, issym, cmp_mul_adds, cmp_term_term
76+
import SymbolicUtils: <ₑ, isterm, isadd, ismul, issym, get_degrees, monomial_lt, _arglen
7677
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)
78+
da, db = get_degrees(a), get_degrees(b)
79+
fw = monomial_lt(da, db)
80+
bw = monomial_lt(db, da)
81+
if fw === bw && !isequal(a, b)
82+
if _arglen(a) == _arglen(b)
83+
return (operation(a), arguments(a)...,) <ₑ (operation(b), arguments(b)...,)
9184
else
92-
return name(T) < nameof(S)
85+
return _arglen(a) < _arglen(b)
9386
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)
9987
else
100-
return !(b <ₑ a)
88+
return fw
10189
end
10290
end
10391

0 commit comments

Comments
 (0)