Skip to content

Commit

Permalink
docs: improve @parse_expression docs
Browse files Browse the repository at this point in the history
  • Loading branch information
MilesCranmer committed Apr 29, 2024
1 parent 4b02dd2 commit 7b63aa0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
6 changes: 6 additions & 0 deletions docs/src/utils.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Node utilities

## Creating trees

```@docs
@parse_expression
```

## `Base`

Various functions in `Base` are overloaded to treat an `AbstractNode` as a
Expand Down
29 changes: 25 additions & 4 deletions src/Parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,21 @@ using ..ExpressionModule: AbstractExpression, Expression
Parse a symbolic expression `expr` into a computational graph where nodes represent operations or variables.
## Arguments
- `expr`: An expression to parse into an `AbstractExpression`.
## Keyword Arguments
- `operators`: An instance of `OperatorEnum` specifying the available unary and binary operators.
- `variable_names`: A list of variable names as strings or symbols that are allowed in the expression.
- `evaluate_on`: A list of external functions to evaluate explicitly when encountered.
## Keyword Arguments
- `node_type`: The type of the nodes in the resulting expression tree. Defaults to `Node`.
## Usage
The macro is used to convert a high-level symbolic expression into a structured expression tree that can be manipulated or evaluated. Here are some examples of how to use `parse_expression`:
- Parsing a simple mathematical expression involving custom operations:
### Parsing from a custom operator
```julia
julia> my_custom_op(x, y) = x + y^3;
Expand All @@ -44,7 +47,7 @@ julia> ex(ones(2, 1))
2.487286478935302
```
- Handling expressions with symbolic variable names, and custom node types:
### Handling expressions with symbolic variable names
```julia
julia> ex = @parse_expression(
Expand All @@ -58,6 +61,24 @@ cos(exp(α))
julia> typeof(ex.tree)
GraphNode{Float32}
```
### Using external functions and variables
```
julia> c = 5.0
5.0
julia> show_type(x) = (@show typeof(x); x);
julia> ex = @parse_expression(
c * 2.5 - show_type(cos(x)),
operators = OperatorEnum(; binary_operators=[*, -], unary_operators=[cos]),
variable_names = [:x],
evaluate_on = [show_type],
)
typeof(x) = Node{Float32}
(5.0 * 2.5) - cos(x)
```
"""
macro parse_expression(ex, kws...)
# Initialize default values for operators and variable_names
Expand Down

0 comments on commit 7b63aa0

Please sign in to comment.