Skip to content

Commit 64e68c8

Browse files
authored
Merge pull request #19 from SymbolicML/fix-complex-printing
Fix printing of complex numbers
2 parents 1492c0e + 24e71c6 commit 64e68c8

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-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 = "0.4.3"
4+
version = "0.5.0"
55

66
[deps]
77
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

src/Equation.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ function string_tree(
344344
)::String where {T}
345345
if tree.degree == 0
346346
if tree.constant
347-
return string(tree.val::T)
347+
return string_constant(tree.val::T; bracketed=bracketed)
348348
else
349349
if varMap === nothing
350350
return "x$(tree.feature)"
@@ -362,6 +362,15 @@ function string_tree(
362362
end
363363
end
364364

365+
string_constant(val::T; bracketed::Bool) where {T<:Union{Real,AbstractArray}} = string(val)
366+
function string_constant(val; bracketed::Bool)
367+
if bracketed
368+
string(val)
369+
else
370+
"(" * string(val) * ")"
371+
end
372+
end
373+
365374
# Print an equation
366375
function print_tree(
367376
io::IO,

test/test_print.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,20 @@ for binop in [safe_pow, ^]
3838
minitree = Node(5, Node("x1"), Node("x2"))
3939
@test string_tree(minitree, opts) == "(x1 ^ x2)"
4040
end
41+
42+
@testset "Test printing of complex numbers" begin
43+
@eval my_custom_op(x, y) = x + y
44+
operators = OperatorEnum(;
45+
default_params...,
46+
binary_operators=(+, *, /, -, my_custom_op),
47+
unary_operators=(cos, sin),
48+
)
49+
@extend_operators operators
50+
x1, x2, x3 = [Node(; feature=i) for i in 1:3]
51+
tree = sin(x1 * 1.0)
52+
@test string_tree(tree, operators) == "sin(x1 * 1.0)"
53+
tree = sin(x1 * (1.0 + 2.0im))
54+
@test string_tree(tree, operators) == "sin(x1 * (1.0 + 2.0im))"
55+
tree = my_custom_op(x1, 1.0 + 2.0im)
56+
@test string_tree(tree, operators) == "my_custom_op(x1, 1.0 + 2.0im)"
57+
end

0 commit comments

Comments
 (0)