|
79 | 79 | end |
80 | 80 |
|
81 | 81 | @testitem "StructuredExpression Literate examples" begin |
82 | | - #literate_begin file="src/structured_expression.md" |
| 82 | + #literate_begin file="src/examples/structured_expression.md" |
83 | 83 | #= |
84 | 84 | # `StructuredExpression` example |
85 | 85 |
|
|
89 | 89 |
|
90 | 90 | Let's look at an example: |
91 | 91 | =# |
92 | | - using DynamicExpressions |
93 | | - using Test |
| 92 | + using DynamicExpressions, Random |
94 | 93 |
|
95 | 94 | # First, we will create some normal `Expression` objects. |
96 | 95 |
|
97 | 96 | operators = OperatorEnum(; unary_operators=(cos, exp), binary_operators=(+, -, *, /)) |
98 | 97 | variable_names = ["x", "y"] |
99 | 98 | x = Expression(Node{Float64}(; feature=1); operators, variable_names) |
100 | 99 | 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 |
102 | 101 |
|
103 | 102 | typeof(x) |
104 | 103 | #= |
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: |
108 | 106 | =# |
109 | 107 | f = x * x - cos(2.5f0 * y + -0.5f0) |
110 | 108 | g = exp(2.0 - y * y) |
|
117 | 115 | expressions during evaluation. |
118 | 116 | =# |
119 | 117 | ex = StructuredExpression((; f, g), nt -> nt.f + nt.g) |
120 | | - @test typeof(ex) <: AbstractExpression{Float64,<:Node{Float64}} #src |
121 | 118 | ex |
| 119 | + @test typeof(ex) <: AbstractExpression{Float64,<:Node{Float64}} #src |
122 | 120 | #= |
123 | 121 | Note that this is displayed as a single tree, with the `+` operator |
124 | 122 | used to combine them. Despite this, the expression is not actually |
|
130 | 128 | =# |
131 | 129 | length(get_tree(ex)) |
132 | 130 | @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 |
133 | 150 |
|
134 | 151 | #literate_end |
135 | 152 | end |
0 commit comments