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,32 @@ 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 ,
133+ # Deprecated:
128134)
135+ variable_names = deprecate_varmap (variable_names, nothing , :convert )
129136 if ! SymbolicUtils. istree (expr)
130- varMap === nothing && return Node (String (expr. name))
131- return Node (String (expr. name), varMap )
137+ variable_names === nothing && return Node (String (expr. name))
138+ return Node (String (expr. name), variable_names )
132139 end
133140
134141 # First, we remove integer powers:
@@ -140,19 +147,21 @@ function convert(
140147 op = convert_to_function (SymbolicUtils. operation (expr), operators)
141148 args = SymbolicUtils. arguments (expr)
142149
143- length (args) > 2 && return split_eq (op, args, operators; varMap = varMap )
150+ length (args) > 2 && return split_eq (op, args, operators; variable_names = variable_names )
144151 ind = if length (args) == 2
145152 findoperation (op, operators. binops)
146153 else
147154 findoperation (op, operators. unaops)
148155 end
149156
150- return Node (ind, map (x -> convert (Node, x, operators; varMap= varMap), args)... )
157+ return Node (
158+ ind, map (x -> convert (Node, x, operators; variable_names= variable_names), args)...
159+ )
151160end
152161
153162"""
154163 node_to_symbolic(tree::Node, operators::AbstractOperatorEnum;
155- varMap ::Union{Array{String, 1}, Nothing}=nothing,
164+ variable_names ::Union{Array{String, 1}, Nothing}=nothing,
156165 index_functions::Bool=false)
157166
158167The interface to SymbolicUtils.jl. Passing a tree to this function
@@ -162,7 +171,7 @@ will generate a symbolic equation in SymbolicUtils.jl format.
162171
163172- `tree::Node`: The equation to convert.
164173- `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
174+ - `variable_names ::Union{Array{String, 1}, Nothing}=nothing`: What variable names to use for
166175 each feature. Default is [x1, x2, x3, ...].
167176- `index_functions::Bool=false`: Whether to generate special names for the
168177 operators, which then allows one to convert back to a `Node` format
@@ -172,19 +181,23 @@ will generate a symbolic equation in SymbolicUtils.jl format.
172181function node_to_symbolic (
173182 tree:: Node ,
174183 operators:: AbstractOperatorEnum ;
175- varMap :: Union{Array{String,1},Nothing} = nothing ,
184+ variable_names :: Union{Array{String,1},Nothing} = nothing ,
176185 index_functions:: Bool = false ,
186+ # Deprecated:
187+ varMap= nothing ,
177188)
189+ variable_names = deprecate_varmap (variable_names, varMap, :node_to_symbolic )
178190 expr = subs_bad (parse_tree_to_eqs (tree, operators, index_functions))
179191 # Check for NaN and Inf
180192 @assert isgood (expr) " The recovered equation contains NaN or Inf."
181- # Return if no varMap is given
182- varMap === nothing && return expr
193+ # Return if no variable_names is given
194+ variable_names === nothing && return expr
183195 # Create a substitution tuple
184196 subs = Dict (
185197 [
186198 SymbolicUtils. Sym {LiteralReal} (Symbol (" x$(i) " )) =>
187- SymbolicUtils. Sym {LiteralReal} (Symbol (varMap[i])) for i in 1 : length (varMap)
199+ SymbolicUtils. Sym {LiteralReal} (Symbol (variable_names[i])) for
200+ i in 1 : length (variable_names)
188201 ]. .. ,
189202 )
190203 return substitute (expr, subs)
193206function symbolic_to_node (
194207 eqn:: SymbolicUtils.Symbolic ,
195208 operators:: AbstractOperatorEnum ;
196- varMap:: Union{Array{String,1},Nothing} = nothing ,
209+ variable_names:: Union{Array{String,1},Nothing} = nothing ,
210+ # Deprecated:
211+ varMap= nothing ,
197212):: Node
198- return convert (Node, eqn, operators; varMap= varMap)
213+ variable_names = deprecate_varmap (variable_names, varMap, :symbolic_to_node )
214+ return convert (Node, eqn, operators; variable_names= variable_names)
199215end
200216
201217function multiply_powers (eqn:: Number ):: Tuple{SYMBOLIC_UTILS_TYPES,Bool}
0 commit comments