Skip to content

Commit 44ff4f1

Browse files
committed
update docs on interfacing
1 parent 22f818b commit 44ff4f1

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

docs/interface.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,27 +89,33 @@ ex = 1 + (:x - 2)
8989
\out{piracy1}
9090

9191
How can we use SymbolicUtils.jl to convert `ex` to `(-)(:x, 1)`? We simply implement `istree`,
92-
`operation`, `arguments` and `to_symbolic` and we'll be off to the races:
92+
`operation`, `arguments` and we'll be able to do rule-based rewriting on `Expr`s:
9393
```julia:piracy2
9494
using SymbolicUtils
95-
using SymbolicUtils: istree, operation, arguments, similarterm
9695
9796
SymbolicUtils.istree(ex::Expr) = ex.head == :call
9897
SymbolicUtils.operation(ex::Expr) = ex.args[1]
9998
SymbolicUtils.arguments(ex::Expr) = ex.args[2:end]
10099
101-
@show simplify(ex)
100+
@rule(~x => ~x - 1)(ex)
101+
```
102+
\out{piracy2}
102103

103-
dump(simplify(ex))
104+
However, this is not enough to get SymbolicUtils to use its own algebraic simplification system on `Expr`s:
105+
```julia:piracy3
106+
simplify(ex)
104107
```
108+
\out{piracy3}
105109

106-
There was no simplification, because by default SymbolicUtils assumes that the expressoins are of type Any and no particular rules apply. Let's change this by saying that the symbolic type (symtype) of an Expr or Symbol object is actually Real.
110+
The reason that the expression was not simplified is that the expression tree is untyped, so SymbolicUtils
111+
doesn't know what rules to apply to the expression. To mimic the behaviour of most computer algebra
112+
systems, the simplest thing to do would be to assume that all `Expr`s are of type `Number`:
107113

108114
```julia:piracy4
109-
SymbolicUtils.symtype(s::Expr) = Real
115+
SymbolicUtils.symtype(s::Expr) = Number
110116
111-
dump(simplify(ex))
117+
simplify(ex)
112118
```
113119
\out{piracy4}
114120

115-
Now SymbolicUtils is able to apply the Number simplification rule to Expr.
121+
Now SymbolicUtils is able to apply the `Number` simplification rule to `Expr`.

0 commit comments

Comments
 (0)