Skip to content

Commit 688010c

Browse files
committed
docs: expand structured expression example
1 parent f68a8ab commit 688010c

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

docs/make.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@ index_content = let r = read(readme, String)
4040
bottom_part = """
4141
## Contents
4242
43-
```@contents
44-
Pages = ["utils.md", "api.md", "eval.md"]
45-
```
4643
"""
4744

4845
join((top_part, r, bottom_part), "\n")
@@ -62,6 +59,13 @@ makedocs(;
6259
format=Documenter.HTML(;
6360
canonical="https://symbolicml.org/DynamicExpressions.jl/stable"
6461
),
62+
pages=[
63+
"Home" => "index.md",
64+
"Eval" => "eval.md",
65+
"Examples" => ["examples/structured_expression.md"],
66+
"Utils" => "utils.md",
67+
"API" => "api.md",
68+
],
6569
)
6670

6771
# Forward links from old docs:

docs/utils.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ function process_literate_block(output_file, content, source_file)
4848
write(temp_file, content)
4949

5050
# Process the temporary file with Literate.markdown
51-
output_dir = joinpath(@__DIR__, "src")
51+
output_dir = joinpath(@__DIR__, "src", "examples")
5252
base_name = first(splitext(basename(output_file))) # Remove any existing extension
5353

5454
markdown(temp_file, output_dir; name=base_name, documenter=true)
5555

5656
# Generate the relative path for EditURL
57-
edit_path = relpath(source_file, joinpath(@__DIR__, "src"))
57+
edit_path = relpath(source_file, output_dir)
5858

5959
# Read the generated markdown file
6060
md_file = joinpath(output_dir, base_name * ".md")

test/test_structured_expression.jl

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ end
7979
end
8080

8181
@testitem "StructuredExpression Literate examples" begin
82-
#literate_begin file="src/structured_expression.md"
82+
#literate_begin file="src/examples/structured_expression.md"
8383
#=
8484
# `StructuredExpression` example
8585
@@ -89,22 +89,20 @@ end
8989
9090
Let's look at an example:
9191
=#
92-
using DynamicExpressions
93-
using Test
92+
using DynamicExpressions, Random
9493

9594
# First, we will create some normal `Expression` objects.
9695

9796
operators = OperatorEnum(; unary_operators=(cos, exp), binary_operators=(+, -, *, /))
9897
variable_names = ["x", "y"]
9998
x = Expression(Node{Float64}(; feature=1); operators, variable_names)
10099
y = Expression(Node{Float64}(; feature=2); operators, variable_names)
101-
@test typeof(x) <: AbstractExpression{Float64,<:Node{Float64}}
100+
@test typeof(x) <: AbstractExpression{Float64,<:Node{Float64}} #src
102101

103102
typeof(x)
104103
#=
105-
`AbstractExpression` can be composed together using standard Julia
106-
operations. For example, let's create two complex expressions from these,
107-
and check that they have the same type:
104+
Any `AbstractExpression`, such as this `Expression` object, can be composed together
105+
using standard Julia math operations. For example, let's some complex expressions from these:
108106
=#
109107
f = x * x - cos(2.5f0 * y + -0.5f0)
110108
g = exp(2.0 - y * y)
@@ -117,8 +115,8 @@ end
117115
expressions during evaluation.
118116
=#
119117
ex = StructuredExpression((; f, g), nt -> nt.f + nt.g)
120-
@test typeof(ex) <: AbstractExpression{Float64,<:Node{Float64}} #src
121118
ex
119+
@test typeof(ex) <: AbstractExpression{Float64,<:Node{Float64}} #src
122120
#=
123121
Note that this is displayed as a single tree, with the `+` operator
124122
used to combine them. Despite this, the expression is not actually
@@ -130,6 +128,25 @@ end
130128
=#
131129
length(get_tree(ex))
132130
@test length(get_tree(ex)) == 17 #src
131+
#=
132+
Evaluation of an `AbstractExpression` is set up to forward through
133+
`get_tree`, so this will work automatically.
134+
135+
Let's try to evaluate this on some random data:
136+
=#
137+
rng = Random.MersenneTwister(0)
138+
X = randn(rng, Float64, 2, 5)
139+
X
140+
#=
141+
Followed by the evaluation. Since we have stored the operators directly
142+
in the expression object, we do not need to pass the operators explicitly:
143+
=#
144+
ex(X)
145+
#=
146+
Which we can verify against the individual expressions:
147+
=#
148+
f(X) + g(X)
149+
@test ex(X) f(X) + g(X) #src
133150

134151
#literate_end
135152
end

0 commit comments

Comments
 (0)