File tree Expand file tree Collapse file tree 2 files changed +49
-4
lines changed Expand file tree Collapse file tree 2 files changed +49
-4
lines changed Original file line number Diff line number Diff line change 33Base. @pure Base. isless (v1:: AbstractVariable , v2:: AbstractVariable ) = name (v1) > name (v2)
44Base. isless (m1:: AbstractTermLike , m2:: AbstractTermLike ) = isless (promote (m1, m2)... )
55
6+ # Implement this to make coefficients be compared with terms.
7+ function isless_coefficient (a:: Real , b:: Real )
8+ return a < b
9+ end
10+ function isless_coefficient (a:: Number , b:: Number )
11+ return abs (a) < abs (b)
12+ end
13+ # By default, coefficients are not comparable so `a` is not strictly
14+ # less than `b`, they are considered sort of equal.
15+ isless_coefficient (a, b) = false
16+
617function Base. isless (t1:: AbstractTerm , t2:: AbstractTerm )
718 if monomial (t1) < monomial (t2)
8- true
19+ return true
920 elseif monomial (t1) == monomial (t2)
10- abs (coefficient (t1)) < abs ( coefficient (t2))
21+ return isless_coefficient (coefficient (t1), coefficient (t2))
1122 else
12- false
23+ return false
1324 end
1425end
1526
Original file line number Diff line number Diff line change 1+ struct CoefNotComparable
2+ end
3+ Base. iszero (:: CoefNotComparable ) = false
4+
15@testset " Term" begin
26 Mod. @polyvar x
37 @test convert (Any, 1 x) == 1 x
4953 @test_throws InexactError push! ([1 ], 2 x)
5054 @test_throws ErrorException push! ([x^ 2 ], 2 x)
5155
52-
5356 @testset " Effective variables" begin
5457 T = variable_union_type (x)
5558 @test x isa T
6164 @test T[y] == @inferred effective_variables (x^ 0 * y)
6265 @test T[y] == @inferred effective_variables (y * x^ 0 )
6366 end
67+
68+ @testset " Compare terms" begin
69+ t1 = 1 * x
70+ t2 = 2 * x
71+ @test t1 < t2
72+ @test t1 <= t2
73+ @test ! (t1 > t2)
74+ @test ! (t1 >= t2)
75+
76+ t1 = (1 + 1im ) * x
77+ t2 = (2 + 2im ) * x
78+ @test t1 < t2
79+ @test t1 <= t2
80+ @test ! (t1 > t2)
81+ @test ! (t1 >= t2)
82+
83+ a = CoefNotComparable ()
84+ b = CoefNotComparable ()
85+ t1 = a * x
86+ t2 = b * y
87+ @test t1 > t2
88+ @test t1 >= t2
89+ @test ! (t1 < t2)
90+ @test ! (t1 <= t2)
91+ t1 = a * x
92+ t2 = b * x
93+ @test ! (t1 > t2)
94+ @test t1 >= t2
95+ @test ! (t1 < t2)
96+ @test t1 <= t2
97+ end
6498end
You can’t perform that action at this time.
0 commit comments