Skip to content

Commit 2b7fde4

Browse files
committed
Allow latest variable names set
1 parent 2fa8ecd commit 2b7fde4

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
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.12.0"
4+
version = "0.12.1"
55

66
[deps]
77
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"

src/OperatorEnumConstruction.jl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,22 @@ const ALREADY_DEFINED_UNARY_OPERATORS = (;
2727
const ALREADY_DEFINED_BINARY_OPERATORS = (;
2828
operator_enum=Dict{Function,Bool}(), generic_operator_enum=Dict{Function,Bool}()
2929
)
30+
const LATEST_VARIABLE_NAMES = Ref{Union{Nothing,Vector{String}}}(nothing)
3031

3132
function Base.show(io::IO, tree::Node)
3233
latest_operators_type = LATEST_OPERATORS_TYPE.x
3334
if latest_operators_type == IsNothing
34-
return print(io, string_tree(tree))
35+
return print(io, string_tree(tree; variable_names=LATEST_VARIABLE_NAMES.x))
3536
elseif latest_operators_type == IsOperatorEnum
3637
latest_operators = LATEST_OPERATORS.x::OperatorEnum
37-
return print(io, string_tree(tree, latest_operators))
38+
return print(
39+
io, string_tree(tree, latest_operators; variable_names=LATEST_VARIABLE_NAMES.x)
40+
)
3841
else
3942
latest_operators = LATEST_OPERATORS.x::GenericOperatorEnum
40-
return print(io, string_tree(tree, latest_operators))
43+
return print(
44+
io, string_tree(tree, latest_operators; variable_names=LATEST_VARIABLE_NAMES.x)
45+
)
4146
end
4247
end
4348
function (tree::Node)(X; kws...)

test/test_print.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,16 @@ end
9898
f_constant(val::Float64, args...) = string(round(val; digits=4))
9999
@test string_tree(tree, operators; f_constant=f_constant) == "((x1 * x1) + 3.1416)"
100100
end
101+
102+
@testset "Test variable names" begin
103+
operators = OperatorEnum(;
104+
binary_operators=[+, *, /, -], unary_operators=[cos, sin],
105+
)
106+
@extend_operators operators
107+
x1, x2, x3 = [Node(Float64; feature=i) for i in 1:3]
108+
DynamicExpressions.OperatorEnumConstructionModule.LATEST_VARIABLE_NAMES.x = ["k1", "k2", "k3"]
109+
tree = x1 * x2 + x3
110+
@test string(tree) == "((k1 * k2) + k3)"
111+
DynamicExpressions.OperatorEnumConstructionModule.LATEST_VARIABLE_NAMES.x = nothing
112+
@test string(tree) == "((x1 * x2) + x3)"
113+
end

0 commit comments

Comments
 (0)