Skip to content

Commit 2994064

Browse files
committed
docs: improve Expression example
1 parent 70d344b commit 2994064

File tree

2 files changed

+20
-26
lines changed

2 files changed

+20
-26
lines changed

docs/make.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ makedocs(;
6262
pages=[
6363
"Home" => "index.md",
6464
"Examples" => [
65-
"examples/base_operations.md",
66-
"examples/expression.md",
67-
"examples/structured_expression.md",
65+
"examples/base_operations.md", # Defined by `test/test_base_2.jl`
66+
"examples/expression.md", # Defined by `test/test_expression.jl`
67+
"examples/structured_expression.md", # Defined by `test/test_structured_expression.jl`
6868
],
6969
"Eval" => "eval.md",
7070
"Utils" => "utils.md",

test/test_expressions.jl

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,10 @@ end
292292
complex_expr(randn(rng, 2, 5))
293293

294294
#=
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:
298299
=#
299300
parsed_expr = parse_expression(
300301
:(sin(2.0 * x + exp(y + 5.0))); operators=operators, variable_names=variable_names
@@ -322,37 +323,30 @@ end
322323
with_contents(parsed_expr, Node(; op=2, l=get_contents(parsed_expr)))
323324

324325
#=
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):
341330
=#
342331
node_count = count_nodes(parsed_expr)
343332
println("Number of nodes: $node_count")
344333

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+
)
348342

349343
#=
350344
We can also perform more complex operations, like simplification:
351345
=#
352346
complex_expr = parse_expression(
353347
:((2.0 + x) + 3.0); operators=operators, variable_names=["x"]
354348
)
355-
simplified_expr = combine_operators(complex_expr)
349+
simplified_expr = combine_operators(copy(complex_expr))
356350
println("Original: ", complex_expr)
357351
println("Simplified: ", simplified_expr)
358352

0 commit comments

Comments
 (0)