|
123 | 123 | rand(rng, NodeSampler(; tree, filter=node -> node.degree == 1)) |
124 | 124 |
|
125 | 125 | #literate_end |
126 | | - |
127 | | - # y = Node(Float64; feature=2) # Represents variable y |
128 | | - # const_3 = Node(3.0) # Constant node with value 3 |
129 | | - # expr_tree = (x + y) * const_3 - sin(x) |
130 | | - |
131 | | - # println("Original expression tree:") |
132 | | - # println(expr_tree) |
133 | | - |
134 | | - # # Now, let's demonstrate some operations on this tree |
135 | | - |
136 | | - # # 1. Counting nodes |
137 | | - # node_count = count_nodes(expr_tree) |
138 | | - # println("\nNumber of nodes in the tree: ", node_count) |
139 | | - |
140 | | - # # 2. Finding all constant nodes |
141 | | - # constant_nodes = filter(t -> t.degree == 0 && t.constant, expr_tree) |
142 | | - # println("\nConstant nodes in the tree:") |
143 | | - # for node in constant_nodes |
144 | | - # println(node) |
145 | | - # end |
146 | | - |
147 | | - # # 3. Mapping: Double all constant values |
148 | | - # map(expr_tree) do t |
149 | | - # if t.degree == 0 && t.constant |
150 | | - # t.val *= 2 |
151 | | - # end |
152 | | - # end |
153 | | - |
154 | | - # println("\nExpression after doubling constants:") |
155 | | - # println(expr_tree) |
156 | | - |
157 | | - # # 4. Checking if a node is in the tree |
158 | | - # println("\nIs x in the tree? ", x in expr_tree) |
159 | | - # println("Is Node(4.0) in the tree? ", Node(4.0) in expr_tree) |
160 | | - |
161 | | - # # 5. Sum of all constant values |
162 | | - # const_sum = sum(t -> t.degree == 0 && t.constant ? t.val : 0.0, expr_tree) |
163 | | - # println("\nSum of all constant values: ", const_sum) |
164 | | - |
165 | | - # # This example showcases how to create expression trees using `Node`, |
166 | | - # # and how to use various operations like count, filter, map, in, and sum |
167 | | - # # to analyze and modify the tree structure. |
168 | 126 | end |
0 commit comments