@@ -190,110 +190,148 @@ const OP_NAMES = Dict(
190
190
" safe_pow" => " ^" ,
191
191
)
192
192
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
195
206
end
196
207
197
208
function 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...
205
210
):: 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)
208
212
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 ... )
211
215
if bracketed
212
- return " $l $ op_name $r "
216
+ return l * " " * op_name * " " * r
213
217
else
214
- return " ($l $ op_name $r )"
218
+ return " (" * l * " " * op_name * " " * r * " )"
215
219
end
216
220
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]
220
249
end
221
250
end
222
251
223
252
"""
224
- string_tree(tree::Node, operators::AbstractOperatorEnum; kws... )
253
+ string_tree(tree::Node, operators::AbstractOperatorEnum[; bracketed, variable_names, f_variable, f_constant] )
225
254
226
255
Convert an equation to a string.
227
256
228
257
# 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.
232
266
"""
233
267
function string_tree (
234
268
tree:: Node{T} ,
235
269
operators:: AbstractOperatorEnum ;
236
270
bracketed:: Bool = false ,
271
+ f_variable:: F1 = string_variable,
272
+ f_constant:: F2 = string_constant,
237
273
variable_names:: Union{Array{String,1},Nothing} = nothing ,
238
274
# Deprecated
239
275
varMap= nothing ,
240
- ):: String where {T}
276
+ ):: String where {T,F1 <: Function ,F2 <: Function }
241
277
variable_names = deprecate_varmap (variable_names, varMap, :string_tree )
242
278
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 )
245
281
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)
251
283
end
252
284
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
+ )
255
295
else
256
296
return string_op (
297
+ Val (2 ),
257
298
operators. binops[tree. op],
258
299
tree,
259
300
operators;
260
- bracketed= bracketed,
261
- variable_names= variable_names,
301
+ bracketed,
302
+ f_variable,
303
+ f_constant,
304
+ variable_names,
262
305
)
263
306
end
264
307
end
265
308
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
-
275
309
# Print an equation
276
310
function print_tree (
277
311
io:: IO ,
278
312
tree:: Node ,
279
313
operators:: AbstractOperatorEnum ;
314
+ f_variable:: F1 = string_variable,
315
+ f_constant:: F2 = string_constant,
280
316
variable_names:: Union{Array{String,1},Nothing} = nothing ,
281
317
# Deprecated
282
318
varMap= nothing ,
283
- )
319
+ ) where {F1 <: Function ,F2 <: Function }
284
320
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))
286
322
end
287
323
288
324
function print_tree (
289
325
tree:: Node ,
290
326
operators:: AbstractOperatorEnum ;
327
+ f_variable:: F1 = string_variable,
328
+ f_constant:: F2 = string_constant,
291
329
variable_names:: Union{Array{String,1},Nothing} = nothing ,
292
330
# Deprecated
293
331
varMap= nothing ,
294
- )
332
+ ) where {F1 <: Function ,F2 <: Function }
295
333
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))
297
335
end
298
336
299
337
end
0 commit comments