Skip to content

Commit b3ed0b6

Browse files
committed
feat: overload additional Base operators
1 parent 9c56542 commit b3ed0b6

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "DynamicExpressions"
22
uuid = "a40a106e-89c9-4ca8-8020-a735e8728b6b"
33
authors = ["MilesCranmer <miles.cranmer@gmail.com>"]
4-
version = "1.9.3"
4+
version = "1.9.4"
55

66
[deps]
77
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"

src/ExpressionAlgebra.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ end
154154
for op in (
155155
:*, :/, :+, :-, :^, :÷, :mod, :log,
156156
:atan, :atand, :copysign, :flipsign,
157-
:&, :|, :, ://, :\,
157+
:&, :|, :, ://, :\, :rem,
158+
:(>), :(<), :(>=), :(<=), :max, :min,
158159
)
159160
@eval @declare_expression_operator Base.$(op) 2
160161
end

test/test_expressions.jl

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,3 +454,48 @@ end
454454
@test get_variable_names(new_ex2, nothing) == ["x1"]
455455
@test get_operators(new_ex2, nothing) == new_operators
456456
end
457+
458+
@testitem "New binary operators" begin
459+
using DynamicExpressions
460+
461+
operators = OperatorEnum(;
462+
binary_operators=[+, -, *, /, >, <, >=, <=, max, min, rem],
463+
unary_operators=[sin, cos],
464+
)
465+
x1, x2 = [Node(Float64; feature=i) for i in 1:2]
466+
467+
# Test comparison operators string representation
468+
tree = x1 > x2
469+
@test string(tree) == "x1 > x2"
470+
471+
tree = x1 < x2
472+
@test string(tree) == "x1 < x2"
473+
474+
tree = x1 >= x2
475+
@test string(tree) == "x1 >= x2"
476+
477+
tree = x1 <= x2
478+
@test string(tree) == "x1 <= x2"
479+
480+
# Test max/min operators
481+
tree = max(x1, x2)
482+
X = [1.0 2.0; 3.0 1.0]' # Two points: (1,3) and (2,1)
483+
@test tree(X, operators) [2.0, 3.0]
484+
485+
tree = min(x1, x2)
486+
@test tree(X, operators) [1.0, 1.0]
487+
488+
# Test remainder operator
489+
tree = rem(x1, x2)
490+
X = [5.0 7.0; 3.0 2.0]' # Two points: (5,7) and (3,2)
491+
@test tree(X, operators) [5.0, 1.0]
492+
493+
# Test combinations string representation
494+
tree = max(x1, 2.0) > min(x2, 3.0)
495+
@test string(tree) == "max(x1, 2.0) > min(x2, 3.0)"
496+
497+
# Test with constants
498+
tree = rem(x1, 2.0)
499+
X = [5.0 7.0] # Two points: 5 and 7
500+
@test tree(X, operators) [1.0, 1.0]
501+
end

0 commit comments

Comments
 (0)