Skip to content

Commit cae4c22

Browse files
committed
Update API on README
1 parent 155f7fc commit cae4c22

File tree

4 files changed

+44
-13
lines changed

4 files changed

+44
-13
lines changed

README.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,19 +159,28 @@ x1 = Node(String; feature=1)
159159
This node, will be used to index input data (whatever it may be) with either `data[feature]` (1D abstract arrays) or `selectdim(data, 1, feature)` (ND abstract arrays). Let's now define some operators to use:
160160

161161
```julia
162-
my_string_func(x::String) = "Hello $x"
162+
my_string_func(x::String) = "ello $x"
163163

164164
operators = GenericOperatorEnum(;
165165
binary_operators=[*],
166-
unary_operators=[my_string_func],
167-
extend_user_operators=true)
166+
unary_operators=[my_string_func]
167+
)
168+
```
169+
170+
Now, let's extend our operators to work with the
171+
expression types used by `DynamicExpressions.jl`:
172+
173+
```julia
174+
@extend_operators operators
168175
```
169176

170177
Now, let's create an expression:
171178

172179
```julia
173-
tree = x1 * " World!"
174-
tree(["Hello", "Me?"])
180+
tree = "H" * my_string_func(x1)
181+
# ^ `(H * my_string_func(x1))`
182+
183+
tree(["World!", "Me?"])
175184
# Hello World!
176185
```
177186

@@ -202,7 +211,8 @@ vec_add(x, y) = x .+ y
202211
vec_square(x) = x .* x
203212

204213
# Set up an operator enum:
205-
operators = GenericOperatorEnum(;binary_operators=[vec_add], unary_operators=[vec_square], extend_user_operators=true)
214+
operators = GenericOperatorEnum(;binary_operators=[vec_add], unary_operators=[vec_square])
215+
@extend_operators operators
206216

207217
# Construct the expression:
208218
tree = vec_add(vec_add(vec_square(x1), c2), c1)

docs/src/types.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,35 @@ OperatorEnum
1313
Construct this operator specification as follows:
1414

1515
```@docs
16-
OperatorEnum(; binary_operators, unary_operators, enable_autodiff)
16+
OperatorEnum(; binary_operators=[], unary_operators=[], enable_autodiff::Bool=false, define_helper_functions::Bool=true)
1717
```
1818

1919
This is just for scalar real operators. However, you can use
2020
the following for more general operators:
2121

2222
```@docs
23-
GenericOperatorEnum(; binary_operators=[], unary_operators=[], extend_user_operators::Bool=false)
23+
GenericOperatorEnum(; binary_operators=[], unary_operators=[], define_helper_functions::Bool=true)
2424
```
2525

26+
By default, these operators will define helper functions for constructing trees,
27+
so that you can write `Node(;feature=1) + Node(;feature=2)` instead of
28+
`Node(1, Node(;feature=1), Node(;feature=2))` (assuming `+` is the first operator).
29+
You can turn this off with `define_helper_functions=false`.
30+
31+
For other operators *not* found in `Base`, including user-defined functions, you may
32+
use the `@extend_operators` macro:
33+
34+
```@docs
35+
@extend_operators operators
36+
```
37+
38+
This will extend the operators you have passed to work with `Node` types, so that
39+
it is easier to construct expression trees.
40+
41+
Note that you are free to use the `Node` constructors directly.
42+
This is a more robust approach, and should be used when creating libraries
43+
which use `DynamicExpressions.jl`.
44+
2645
## Equations
2746

2847
Equations are specified as binary trees with the `Node` type, defined

src/OperatorEnumConstruction.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ function _extend_operators(operators, skip_user_operators, __module__::Module)
166166
end
167167

168168
"""
169-
@extend_operators(operators)
169+
@extend_operators operators
170170
171171
Extends all operators defined in this operator enum to work on the
172172
`Node` type. While by default this is already done for operators defined
@@ -187,7 +187,7 @@ macro extend_operators(operators)
187187
end
188188

189189
"""
190-
@extend_operators_base(operators)
190+
@extend_operators_base operators
191191
192192
Similar to `@extend_operators`, but only extends operators already
193193
defined in `Base`.

test/test_tree_construction.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,16 @@ function op1(x, y)
112112
return x + y
113113
end
114114
function op2(x, y)
115-
return x ^ 2 + 1/((y)^2 + 0.1)
115+
return x^2 + 1 / ((y)^2 + 0.1)
116116
end
117117
function op3(x)
118118
return sin(x) + cos(x)
119119
end
120-
operators = OperatorEnum(; default_params..., binary_operators=(op1, op2), unary_operators=(op3,))
120+
operators = OperatorEnum(;
121+
default_params..., binary_operators=(op1, op2), unary_operators=(op3,)
122+
)
121123
@extend_operators operators
122124
x1 = Node(; feature=1)
123125
x2 = Node(; feature=2)
124126
tree = op1(op2(x1, x2), op3(x1))
125-
@test repr(tree) == "op1(op2(x1, x2), op3(x1))"
127+
@test repr(tree) == "op1(op2(x1, x2), op3(x1))"

0 commit comments

Comments
 (0)