Skip to content

Commit cb25876

Browse files
committed
Update README to preferred syntax
1 parent d14718a commit cb25876

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ x2 = Node(; feature=2)
3636
expression = x1 * cos(x2 - 3.2)
3737

3838
X = randn(Float64, 2, 100);
39-
expression(X) # 100-element Vector{Float64}
39+
expression(X, operators) # 100-element Vector{Float64}
4040
```
4141

4242
(We can construct this expression with normal operators, since calling `OperatorEnum()` will `@eval` new functions on `Node` that use the specified enum.)
@@ -53,18 +53,18 @@ First, what happens if we naively use Julia symbols to define and then evaluate
5353
This is quite slow, meaning it will be hard to quickly search over the space of expressions. Let's see how DynamicExpressions.jl compares:
5454

5555
```julia
56-
@btime expression(X)
56+
@btime expression(X, operators)
5757
# 693 ns
5858
```
5959

60-
Much faster! And we didn't even need to compile it. (Internally, this is calling `eval_tree_array(expression, X, operators)` - where `operators` has been pre-defined when we called `OperatorEnum()`).
60+
Much faster! And we didn't even need to compile it. (Internally, this is calling `eval_tree_array(expression, X, operators)`).
6161

6262
If we change `expression` dynamically with a random number generator, it will have the same performance:
6363

6464
```julia
6565
@btime begin
6666
expression.op = rand(1:3) # random operator in [+, -, *]
67-
expression(X)
67+
expression(X, operators)
6868
end
6969
# 842 ns
7070
```
@@ -113,13 +113,13 @@ expression = x1 * cos(x2 - 3.2)
113113
We can take the gradient with respect to inputs with simply the `'` character:
114114

115115
```julia
116-
grad = expression'(X)
116+
grad = expression'(X, operators)
117117
```
118118

119119
This is quite fast:
120120

121121
```julia
122-
@btime expression'(X)
122+
@btime expression'(X, operators)
123123
# 2894 ns
124124
```
125125

@@ -128,7 +128,7 @@ and again, we can change this expression at runtime, without loss in performance
128128
```julia
129129
@btime begin
130130
expression.op = rand(1:3)
131-
expression'(X)
131+
expression'(X, operators)
132132
end
133133
# 3198 ns
134134
```
@@ -180,14 +180,14 @@ Now, let's create an expression:
180180
tree = "H" * my_string_func(x1)
181181
# ^ `(H * my_string_func(x1))`
182182

183-
tree(["World!", "Me?"])
183+
tree(["World!", "Me?"], operators)
184184
# Hello World!
185185
```
186186

187187
So indeed it works for arbitrary types. It is a bit slower due to the potential for type instability, but it's not too bad:
188188

189189
```julia
190-
@btime tree(["Hello", "Me?"])
190+
@btime tree(["Hello", "Me?"], operators)
191191
# 1738 ns
192192
```
193193

@@ -220,15 +220,15 @@ tree = vec_add(vec_add(vec_square(x1), c2), c1)
220220
X = [[-1.0, 5.2, 0.1], [0.0, 0.0, 0.0]]
221221

222222
# Evaluate!
223-
tree(X) # [2.0, 29.04, 3.01]
223+
tree(X, operators) # [2.0, 29.04, 3.01]
224224
```
225225

226226
Note that if an operator is not defined for the particular input, `nothing` will be returned instead.
227227

228228
This is all still pretty fast, too:
229229

230230
```julia
231-
@btime tree(X)
231+
@btime tree(X, operators)
232232
# 2,949 ns
233233
@btime eval(:(vec_add(vec_add(vec_square(X[1]), [1.0, 2.0, 3.0]), 0.0)))
234234
# 115,000 ns

0 commit comments

Comments
 (0)