Skip to content

Commit

Permalink
feat: overload additional Base operators
Browse files Browse the repository at this point in the history
  • Loading branch information
MilesCranmer committed Feb 6, 2025
1 parent 9c56542 commit b3ed0b6
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "DynamicExpressions"
uuid = "a40a106e-89c9-4ca8-8020-a735e8728b6b"
authors = ["MilesCranmer <[email protected]>"]
version = "1.9.3"
version = "1.9.4"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
3 changes: 2 additions & 1 deletion src/ExpressionAlgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ end
for op in (
:*, :/, :+, :-, :^, :÷, :mod, :log,
:atan, :atand, :copysign, :flipsign,
:&, :|, :, ://, :\,
:&, :|, :, ://, :\, :rem,
:(>), :(<), :(>=), :(<=), :max, :min,
)
@eval @declare_expression_operator Base.$(op) 2
end
Expand Down
45 changes: 45 additions & 0 deletions test/test_expressions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -454,3 +454,48 @@ end
@test get_variable_names(new_ex2, nothing) == ["x1"]
@test get_operators(new_ex2, nothing) == new_operators
end

@testitem "New binary operators" begin
using DynamicExpressions

operators = OperatorEnum(;
binary_operators=[+, -, *, /, >, <, >=, <=, max, min, rem],
unary_operators=[sin, cos],
)
x1, x2 = [Node(Float64; feature=i) for i in 1:2]

# Test comparison operators string representation
tree = x1 > x2
@test string(tree) == "x1 > x2"

tree = x1 < x2
@test string(tree) == "x1 < x2"

tree = x1 >= x2
@test string(tree) == "x1 >= x2"

tree = x1 <= x2
@test string(tree) == "x1 <= x2"

# Test max/min operators
tree = max(x1, x2)
X = [1.0 2.0; 3.0 1.0]' # Two points: (1,3) and (2,1)
@test tree(X, operators) [2.0, 3.0]

tree = min(x1, x2)
@test tree(X, operators) [1.0, 1.0]

# Test remainder operator
tree = rem(x1, x2)
X = [5.0 7.0; 3.0 2.0]' # Two points: (5,7) and (3,2)
@test tree(X, operators) [5.0, 1.0]

# Test combinations string representation
tree = max(x1, 2.0) > min(x2, 3.0)
@test string(tree) == "max(x1, 2.0) > min(x2, 3.0)"

# Test with constants
tree = rem(x1, 2.0)
X = [5.0 7.0] # Two points: 5 and 7
@test tree(X, operators) [1.0, 1.0]
end

2 comments on commit b3ed0b6

@MilesCranmer
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/124494

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.9.4 -m "<description of version>" b3ed0b6823f799ebb82ecb6268b95a1b7f9faa1b
git push origin v1.9.4

Please sign in to comment.