|
292 | 292 | complex_expr(randn(rng, 2, 5)) |
293 | 293 |
|
294 | 294 | #= |
295 | | - While creating expressions manually is possible, it can be cumbersome for more |
296 | | - complex expressions. DynamicExpressions provides a more convenient way to create |
297 | | - expressions using the `parse_expression` function: |
| 295 | + While creating expressions manually is faster, and should be preferred within packages, |
| 296 | + it can be cumbersome for quickly writing more complex expressions. |
| 297 | + DynamicExpressions provides a more convenient way to create expressions using |
| 298 | + the `parse_expression` function, which directly parses a Julia object: |
298 | 299 | =# |
299 | 300 | parsed_expr = parse_expression( |
300 | 301 | :(sin(2.0 * x + exp(y + 5.0))); operators=operators, variable_names=variable_names |
@@ -322,37 +323,30 @@ end |
322 | 323 | with_contents(parsed_expr, Node(; op=2, l=get_contents(parsed_expr))) |
323 | 324 |
|
324 | 325 | #= |
325 | | - One of the key features of `Expression` is that it can be evaluated |
326 | | - on data. Let's create some random input data and evaluate our expression: |
327 | | - =# |
328 | | - rng = Random.MersenneTwister(0) |
329 | | - X = rand(rng, 2, 5) # 2 variables, 5 data points |
330 | | - |
331 | | - result = parsed_expr(X) |
332 | | - @test size(result) == (1, 5) #src |
333 | | - |
334 | | - # We can verify this result against a direct calculation: |
335 | | - expected = @. sin(2.0 * X[1, :] + exp(X[2, :] + 5.0)) |
336 | | - @test result ≈ expected #src |
337 | | - |
338 | | - #= |
339 | | - `Expression` objects also support various tree operations. |
340 | | - For example, we can count the number of nodes: |
| 326 | + `Expression` objects support various operations defined on regular trees, |
| 327 | + which permits us to overload specific methods with modified behavior. |
| 328 | + For example, we can count the number of nodes, which simply forwards |
| 329 | + to the method as it is defined on [`Node`](@ref): |
341 | 330 | =# |
342 | 331 | node_count = count_nodes(parsed_expr) |
343 | 332 | println("Number of nodes: $node_count") |
344 | 333 |
|
345 | | - # Or find the depth of the expression tree: |
346 | | - depth = count_depth(parsed_expr) |
347 | | - println("Tree depth: $depth") |
| 334 | + #= |
| 335 | + The [`tree_mapreduce`] will by default call [`get_tree`](@ref) to get the tree, |
| 336 | + so it can be used with any expression type that overloads this method. |
| 337 | + For example, we can compute the depth of a tree: |
| 338 | + =# |
| 339 | + tree_mapreduce( |
| 340 | + leaf -> 1, branch -> 1, (parent, child...) -> parent + max(child...), parsed_expr |
| 341 | + ) |
348 | 342 |
|
349 | 343 | #= |
350 | 344 | We can also perform more complex operations, like simplification: |
351 | 345 | =# |
352 | 346 | complex_expr = parse_expression( |
353 | 347 | :((2.0 + x) + 3.0); operators=operators, variable_names=["x"] |
354 | 348 | ) |
355 | | - simplified_expr = combine_operators(complex_expr) |
| 349 | + simplified_expr = combine_operators(copy(complex_expr)) |
356 | 350 | println("Original: ", complex_expr) |
357 | 351 | println("Simplified: ", simplified_expr) |
358 | 352 |
|
|
0 commit comments