11module DynamicExpressionsSymbolicUtilsExt
22
33import Base: convert
4+ # ! format: off
45if isdefined (Base, :get_extension )
56 using SymbolicUtils
67 import DynamicExpressions. EquationModule: Node, DEFAULT_NODE_TYPE
78 import DynamicExpressions. OperatorEnumModule: AbstractOperatorEnum
8- import DynamicExpressions. UtilsModule: isgood, isbad, @return_on_false
9+ import DynamicExpressions. UtilsModule: isgood, isbad, @return_on_false , deprecate_varmap
910 import DynamicExpressions. ExtensionInterfaceModule: node_to_symbolic, symbolic_to_node
1011else
1112 using .. SymbolicUtils
1213 import .. DynamicExpressions. EquationModule: Node, DEFAULT_NODE_TYPE
1314 import .. DynamicExpressions. OperatorEnumModule: AbstractOperatorEnum
14- import .. DynamicExpressions. UtilsModule: isgood, isbad, @return_on_false
15+ import .. DynamicExpressions. UtilsModule: isgood, isbad, @return_on_false , deprecate_varmap
1516 import .. DynamicExpressions. ExtensionInterfaceModule: node_to_symbolic, symbolic_to_node
1617end
18+ # ! format: on
1719
1820const SYMBOLIC_UTILS_TYPES = Union{<: Number ,SymbolicUtils. Symbolic{<: Number }}
1921
@@ -77,8 +79,11 @@ function split_eq(
7779 op,
7880 args,
7981 operators:: AbstractOperatorEnum ;
80- varMap:: Union{Array{String,1},Nothing} = nothing ,
82+ variable_names:: Union{Array{String,1},Nothing} = nothing ,
83+ # Deprecated:
84+ varMap= nothing ,
8185)
86+ variable_names = deprecate_varmap (variable_names, varMap, :split_eq )
8287 ! (op ∈ (sum, prod, + , * )) && throw (error (" Unsupported operation $op in expression!" ))
8388 if Symbol (op) == Symbol (sum)
8489 ind = findoperation (+ , operators. binops)
@@ -89,8 +94,8 @@ function split_eq(
8994 end
9095 return Node (
9196 ind,
92- convert (Node, args[1 ], operators; varMap = varMap ),
93- convert (Node, op (args[2 : end ]. .. ), operators; varMap = varMap ),
97+ convert (Node, args[1 ], operators; variable_names = variable_names ),
98+ convert (Node, op (args[2 : end ]. .. ), operators; variable_names = variable_names ),
9499 )
95100end
96101
@@ -105,30 +110,31 @@ function convert(
105110 :: typeof (SymbolicUtils. Symbolic),
106111 tree:: Node ,
107112 operators:: AbstractOperatorEnum ;
108- varMap :: Union{Array{String,1},Nothing} = nothing ,
113+ variable_names :: Union{Array{String,1},Nothing} = nothing ,
109114 index_functions:: Bool = false ,
115+ # Deprecated:
116+ varMap= nothing ,
110117)
111- return node_to_symbolic (tree, operators; varMap= varMap, index_functions= index_functions)
118+ variable_names = deprecate_varmap (variable_names, varMap, :convert )
119+ return node_to_symbolic (
120+ tree, operators; variable_names= variable_names, index_functions= index_functions
121+ )
112122end
113123
114- function convert (
115- :: typeof (Node),
116- x:: Number ,
117- operators:: AbstractOperatorEnum ;
118- varMap:: Union{Array{String,1},Nothing} = nothing ,
119- )
124+ function convert (:: typeof (Node), x:: Number , operators:: AbstractOperatorEnum ; kws... )
120125 return Node (; val= DEFAULT_NODE_TYPE (x))
121126end
122127
123128function convert (
124129 :: typeof (Node),
125130 expr:: SymbolicUtils.Symbolic ,
126131 operators:: AbstractOperatorEnum ;
127- varMap :: Union{Array{String,1},Nothing} = nothing ,
132+ variable_names :: Union{Array{String,1},Nothing} = nothing ,
128133)
134+ variable_names = deprecate_varmap (variable_names, nothing , :convert )
129135 if ! SymbolicUtils. istree (expr)
130- varMap === nothing && return Node (String (expr. name))
131- return Node (String (expr. name), varMap )
136+ variable_names === nothing && return Node (String (expr. name))
137+ return Node (String (expr. name), variable_names )
132138 end
133139
134140 # First, we remove integer powers:
@@ -140,19 +146,21 @@ function convert(
140146 op = convert_to_function (SymbolicUtils. operation (expr), operators)
141147 args = SymbolicUtils. arguments (expr)
142148
143- length (args) > 2 && return split_eq (op, args, operators; varMap = varMap )
149+ length (args) > 2 && return split_eq (op, args, operators; variable_names = variable_names )
144150 ind = if length (args) == 2
145151 findoperation (op, operators. binops)
146152 else
147153 findoperation (op, operators. unaops)
148154 end
149155
150- return Node (ind, map (x -> convert (Node, x, operators; varMap= varMap), args)... )
156+ return Node (
157+ ind, map (x -> convert (Node, x, operators; variable_names= variable_names), args)...
158+ )
151159end
152160
153161"""
154162 node_to_symbolic(tree::Node, operators::AbstractOperatorEnum;
155- varMap ::Union{Array{String, 1}, Nothing}=nothing,
163+ variable_names ::Union{Array{String, 1}, Nothing}=nothing,
156164 index_functions::Bool=false)
157165
158166The interface to SymbolicUtils.jl. Passing a tree to this function
@@ -162,7 +170,7 @@ will generate a symbolic equation in SymbolicUtils.jl format.
162170
163171- `tree::Node`: The equation to convert.
164172- `operators::AbstractOperatorEnum`: OperatorEnum, which contains the operators used in the equation.
165- - `varMap ::Union{Array{String, 1}, Nothing}=nothing`: What variable names to use for
173+ - `variable_names ::Union{Array{String, 1}, Nothing}=nothing`: What variable names to use for
166174 each feature. Default is [x1, x2, x3, ...].
167175- `index_functions::Bool=false`: Whether to generate special names for the
168176 operators, which then allows one to convert back to a `Node` format
@@ -172,19 +180,23 @@ will generate a symbolic equation in SymbolicUtils.jl format.
172180function node_to_symbolic (
173181 tree:: Node ,
174182 operators:: AbstractOperatorEnum ;
175- varMap :: Union{Array{String,1},Nothing} = nothing ,
183+ variable_names :: Union{Array{String,1},Nothing} = nothing ,
176184 index_functions:: Bool = false ,
185+ # Deprecated:
186+ varMap= nothing ,
177187)
188+ variable_names = deprecate_varmap (variable_names, varMap, :node_to_symbolic )
178189 expr = subs_bad (parse_tree_to_eqs (tree, operators, index_functions))
179190 # Check for NaN and Inf
180191 @assert isgood (expr) " The recovered equation contains NaN or Inf."
181- # Return if no varMap is given
182- varMap === nothing && return expr
192+ # Return if no variable_names is given
193+ variable_names === nothing && return expr
183194 # Create a substitution tuple
184195 subs = Dict (
185196 [
186197 SymbolicUtils. Sym {LiteralReal} (Symbol (" x$(i) " )) =>
187- SymbolicUtils. Sym {LiteralReal} (Symbol (varMap[i])) for i in 1 : length (varMap)
198+ SymbolicUtils. Sym {LiteralReal} (Symbol (variable_names[i])) for
199+ i in 1 : length (variable_names)
188200 ]. .. ,
189201 )
190202 return substitute (expr, subs)
193205function symbolic_to_node (
194206 eqn:: SymbolicUtils.Symbolic ,
195207 operators:: AbstractOperatorEnum ;
196- varMap:: Union{Array{String,1},Nothing} = nothing ,
208+ variable_names:: Union{Array{String,1},Nothing} = nothing ,
209+ # Deprecated:
210+ varMap= nothing ,
197211):: Node
198- return convert (Node, eqn, operators; varMap= varMap)
212+ variable_names = deprecate_varmap (variable_names, varMap, :symbolic_to_node )
213+ return convert (Node, eqn, operators; variable_names= variable_names)
199214end
200215
201216function multiply_powers (eqn:: Number ):: Tuple{SYMBOLIC_UTILS_TYPES,Bool}
0 commit comments