@@ -190,110 +190,148 @@ const OP_NAMES = Dict(
190190 " safe_pow" => " ^" ,
191191)
192192
193- function get_op_name (op:: String )
194- return get (OP_NAMES, op, op)
193+ @generated function get_op_name (op:: F ) where {F}
194+ try
195+ # Bit faster to just cache the name of the operator:
196+ op_s = string (F. instance)
197+ out = get (OP_NAMES, op_s, op_s)
198+ return :($ out)
199+ catch
200+ end
201+ return quote
202+ op_s = string (op)
203+ out = get (OP_NAMES, op_s, op_s)
204+ return out
205+ end
195206end
196207
197208function string_op (
198- op:: F ,
199- tree:: Node ,
200- operators:: AbstractOperatorEnum ;
201- bracketed:: Bool = false ,
202- variable_names:: Union{Array{String,1},Nothing} = nothing ,
203- # Deprecated
204- varMap= nothing ,
209+ :: Val{2} , op:: F , tree:: Node , args... ; bracketed, kws...
205210):: String where {F}
206- variable_names = deprecate_varmap (variable_names, varMap, :string_op )
207- op_name = get_op_name (string (op))
211+ op_name = get_op_name (op)
208212 if op_name in [" +" , " -" , " *" , " /" , " ^" ]
209- l = string_tree (tree. l, operators ; bracketed= false , variable_names = variable_names )
210- r = string_tree (tree. r, operators ; bracketed= false , variable_names = variable_names )
213+ l = string_tree (tree. l, args ... ; bracketed= false , kws ... )
214+ r = string_tree (tree. r, args ... ; bracketed= false , kws ... )
211215 if bracketed
212- return " $l $ op_name $r "
216+ return l * " " * op_name * " " * r
213217 else
214- return " ($l $ op_name $r )"
218+ return " (" * l * " " * op_name * " " * r * " )"
215219 end
216220 else
217- l = string_tree (tree. l, operators; bracketed= true , variable_names= variable_names)
218- r = string_tree (tree. r, operators; bracketed= true , variable_names= variable_names)
219- return " $op_name ($l , $r )"
221+ l = string_tree (tree. l, args... ; bracketed= true , kws... )
222+ r = string_tree (tree. r, args... ; bracketed= true , kws... )
223+ # return "$op_name($l, $r)"
224+ return op_name * " (" * l * " , " * r * " )"
225+ end
226+ end
227+ function string_op (
228+ :: Val{1} , op:: F , tree:: Node , args... ; bracketed, kws...
229+ ):: String where {F}
230+ op_name = get_op_name (op)
231+ l = string_tree (tree. l, args... ; bracketed= true , kws... )
232+ return op_name * " (" * l * " )"
233+ end
234+
235+ function string_constant (val, bracketed:: Bool )
236+ does_not_need_brackets = (typeof (val) <: Union{Real,AbstractArray} )
237+ if does_not_need_brackets || bracketed
238+ string (val)
239+ else
240+ " (" * string (val) * " )"
241+ end
242+ end
243+
244+ function string_variable (feature, variable_names)
245+ if variable_names === nothing
246+ return " x" * string (feature)
247+ else
248+ return variable_names[feature]
220249 end
221250end
222251
223252"""
224- string_tree(tree::Node, operators::AbstractOperatorEnum; kws... )
253+ string_tree(tree::Node, operators::AbstractOperatorEnum[; bracketed, variable_names, f_variable, f_constant] )
225254
226255Convert an equation to a string.
227256
228257# Arguments
229-
230- - `variable_names::Union{Array{String, 1}, Nothing}=nothing`: what variables
231- to print for each feature.
258+ - `tree`: the tree to convert to a string
259+ - `operators`: the operators used to define the tree
260+
261+ # Keyword Arguments
262+ - `bracketed`: (optional) whether to put brackets around the outside.
263+ - `f_variable`: (optional) function to convert a variable to a string, of the form `(feature::Int, variable_names)`.
264+ - `f_constant`: (optional) function to convert a constant to a string, of the form `(val, bracketed::Bool)`
265+ - `variable_names::Union{Array{String, 1}, Nothing}=nothing`: (optional) what variables to print for each feature.
232266"""
233267function string_tree (
234268 tree:: Node{T} ,
235269 operators:: AbstractOperatorEnum ;
236270 bracketed:: Bool = false ,
271+ f_variable:: F1 = string_variable,
272+ f_constant:: F2 = string_constant,
237273 variable_names:: Union{Array{String,1},Nothing} = nothing ,
238274 # Deprecated
239275 varMap= nothing ,
240- ):: String where {T}
276+ ):: String where {T,F1 <: Function ,F2 <: Function }
241277 variable_names = deprecate_varmap (variable_names, varMap, :string_tree )
242278 if tree. degree == 0
243- if tree. constant
244- return string_constant (tree. val :: T ; bracketed = bracketed )
279+ if ! tree. constant
280+ return f_variable (tree. feature, variable_names )
245281 else
246- if variable_names === nothing
247- return " x$(tree. feature) "
248- else
249- return variable_names[tree. feature]
250- end
282+ return f_constant (tree. val:: T , bracketed)
251283 end
252284 elseif tree. degree == 1
253- op_name = get_op_name (string (operators. unaops[tree. op]))
254- return " $(op_name) ($(string_tree (tree. l, operators, bracketed= true , variable_names= variable_names)) )"
285+ return string_op (
286+ Val (1 ),
287+ operators. unaops[tree. op],
288+ tree,
289+ operators;
290+ bracketed,
291+ f_variable,
292+ f_constant,
293+ variable_names,
294+ )
255295 else
256296 return string_op (
297+ Val (2 ),
257298 operators. binops[tree. op],
258299 tree,
259300 operators;
260- bracketed= bracketed,
261- variable_names= variable_names,
301+ bracketed,
302+ f_variable,
303+ f_constant,
304+ variable_names,
262305 )
263306 end
264307end
265308
266- string_constant (val:: T ; bracketed:: Bool ) where {T<: Union{Real,AbstractArray} } = string (val)
267- function string_constant (val; bracketed:: Bool )
268- if bracketed
269- string (val)
270- else
271- " (" * string (val) * " )"
272- end
273- end
274-
275309# Print an equation
276310function print_tree (
277311 io:: IO ,
278312 tree:: Node ,
279313 operators:: AbstractOperatorEnum ;
314+ f_variable:: F1 = string_variable,
315+ f_constant:: F2 = string_constant,
280316 variable_names:: Union{Array{String,1},Nothing} = nothing ,
281317 # Deprecated
282318 varMap= nothing ,
283- )
319+ ) where {F1 <: Function ,F2 <: Function }
284320 variable_names = deprecate_varmap (variable_names, varMap, :print_tree )
285- return println (io, string_tree (tree, operators; variable_names = variable_names))
321+ return println (io, string_tree (tree, operators; f_variable, f_constant, variable_names))
286322end
287323
288324function print_tree (
289325 tree:: Node ,
290326 operators:: AbstractOperatorEnum ;
327+ f_variable:: F1 = string_variable,
328+ f_constant:: F2 = string_constant,
291329 variable_names:: Union{Array{String,1},Nothing} = nothing ,
292330 # Deprecated
293331 varMap= nothing ,
294- )
332+ ) where {F1 <: Function ,F2 <: Function }
295333 variable_names = deprecate_varmap (variable_names, varMap, :print_tree )
296- return println (string_tree (tree, operators; variable_names = variable_names))
334+ return println (string_tree (tree, operators; f_variable, f_constant, variable_names))
297335end
298336
299337end
0 commit comments