|
42 | 42 | end
|
43 | 43 | end
|
44 | 44 |
|
| 45 | +@testitem "Parse with operator aliases" begin |
| 46 | + using DynamicExpressions |
| 47 | + using DynamicExpressions: DynamicExpressions as DE |
| 48 | + using Test |
| 49 | + |
| 50 | + ## UNARY |
| 51 | + safe_sqrt(x) = x < 0 ? convert(typeof(x), NaN) : sqrt(x) |
| 52 | + DE.declare_operator_alias(::typeof(safe_sqrt), ::Val{1}) = sqrt |
| 53 | + |
| 54 | + operators = OperatorEnum(1 => [safe_sqrt, sin, cos], 2 => [+, -, *, /]) |
| 55 | + |
| 56 | + ex = parse_expression( |
| 57 | + "sqrt(x) + sin(y)"; operators=operators, variable_names=["x", "y"] |
| 58 | + ) |
| 59 | + |
| 60 | + @test typeof(ex) <: Expression |
| 61 | + @test ex.tree.op == 1 |
| 62 | + @test ex.tree.children[1].x.op == 1 |
| 63 | + @test ex.tree.children[2].x.op == 2 |
| 64 | + |
| 65 | + ## BINARY |
| 66 | + safe_pow(x, y) = x < 0 && y != round(y) ? NaN : x^y |
| 67 | + DE.declare_operator_alias(::typeof(safe_pow), ::Val{2}) = ^ |
| 68 | + |
| 69 | + operators = OperatorEnum(1 => [sin], 2 => [+, -, safe_pow, *]) |
| 70 | + ex = parse_expression("x^2 + sin(y)"; operators=operators, variable_names=["x", "y"]) |
| 71 | + |
| 72 | + @test typeof(ex) <: Expression |
| 73 | + @test ex.tree.op == 1 |
| 74 | + @test ex.tree.children[1].x.op == 3 # safe_pow |
| 75 | + @test ex.tree.children[1].x.children[2].x.val == 2.0 |
| 76 | + @test ex.tree.children[2].x.op == 1 |
| 77 | +end |
| 78 | + |
45 | 79 | @testitem "Can also parse just a float" begin
|
46 | 80 | using DynamicExpressions
|
47 | 81 | operators = OperatorEnum() # Tests empty operators
|
|
0 commit comments