-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #538 from JuliaSymbolics/s/inspect
inspect & pluck
- Loading branch information
Showing
13 changed files
with
161 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import AbstractTrees | ||
|
||
const inspect_metadata = Ref{Bool}(false) | ||
function AbstractTrees.nodevalue(x::Symbolic) | ||
istree(x) ? operation(x) : x | ||
end | ||
|
||
function AbstractTrees.nodevalue(x::BasicSymbolic) | ||
str = if !istree(x) | ||
string(exprtype(x), "(", x, ")") | ||
elseif isadd(x) | ||
string(exprtype(x), | ||
(scalar=x.coeff, coeffs=Tuple(k=>v for (k,v) in x.dict))) | ||
elseif ismul(x) | ||
string(exprtype(x), | ||
(scalar=x.coeff, powers=Tuple(k=>v for (k,v) in x.dict))) | ||
elseif isdiv(x) || ispow(x) | ||
string(exprtype(x)) | ||
else | ||
string(exprtype(x),"{", operation(x), "}") | ||
end | ||
|
||
if inspect_metadata[] && !isnothing(metadata(x)) | ||
str *= string(" metadata=", Tuple(k=>v for (k, v) in metadata(x))) | ||
end | ||
Text(str) | ||
end | ||
|
||
function AbstractTrees.children(x::Symbolic) | ||
istree(x) ? arguments(x) : () | ||
end | ||
|
||
""" | ||
inspect([io::IO=stdout], expr; hint=true, metadata=false) | ||
Inspect an expression tree `expr`. Uses AbstractTrees to print out an expression. | ||
BasicSymbolic expressions will print the Unityper type (ADD, MUL, DIV, POW, SYM, TERM) and the relevant internals as the head, and the children in the subsequent lines as accessed by `arguments`. Other types will get printed as subtrees. Set `metadata=true` to print any metadata carried by the nodes. | ||
Line numbers will be shown, use `pluck(expr, line_number)` to get the sub expression or leafnode starting at line_number. | ||
""" | ||
function inspect end | ||
|
||
function inspect(io::IO, x::Symbolic; | ||
hint=true, | ||
metadata=inspect_metadata[]) | ||
|
||
prev_state = inspect_metadata[] | ||
inspect_metadata[] = metadata | ||
lines = readlines(IOBuffer(sprint(io->AbstractTrees.print_tree(io, x)))) | ||
inspect_metadata[] = prev_state | ||
digits = ceil(Int, log10(length(lines))) | ||
line_numbers = lpad.(string.(1:length(lines)), digits) | ||
print(io, join(string.(line_numbers, " ", lines), "\n")) | ||
hint && print(io, "\n\nHint: call SymbolicUtils.pluck(expr, line_number) to get the subexpression starting at line_number") | ||
end | ||
|
||
function inspect(x; hint=true, metadata=inspect_metadata[]) | ||
inspect(stdout, x; hint=hint, metadata=metadata) | ||
end | ||
|
||
inspect(io::IO, x; kw...) = println(io, "Not Symbolic: $x") | ||
|
||
""" | ||
pluck(expr, n) | ||
Pluck the `n`th subexpression from `expr` as given by pre-order DFS. | ||
This is the same as the node numbering in `inspect`. | ||
""" | ||
function pluck(x, item) | ||
collect(Iterators.take(AbstractTrees.PreOrderDFS(x), item))[end] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
1 DIV | ||
2 ├─ MUL(scalar = 1, powers = (z => 1, 1 + 2x + 3y => 2)) | ||
3 │ ├─ SYM(z) | ||
4 │ └─ POW | ||
5 │ ├─ ADD(scalar = 1, coeffs = (y => 3, x => 2)) | ||
6 │ │ ├─ 1 | ||
7 │ │ ├─ MUL(scalar = 2, powers = (x => 1,)) | ||
8 │ │ │ ├─ 2 | ||
9 │ │ │ └─ SYM(x) | ||
10 │ │ └─ MUL(scalar = 3, powers = (y => 1,)) | ||
11 │ │ ├─ 3 | ||
12 │ │ └─ SYM(y) metadata=(Integer => 42,) | ||
13 │ └─ 2 | ||
14 └─ ADD(scalar = 0, coeffs = (z => 1, x => 2)) | ||
15 ├─ SYM(z) | ||
16 └─ MUL(scalar = 2, powers = (x => 1,)) | ||
17 ├─ 2 | ||
18 └─ SYM(x) | ||
|
||
Hint: call SymbolicUtils.pluck(expr, line_number) to get the subexpression starting at line_number |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
1 DIV | ||
2 ├─ MUL(scalar = 1, powers = (z => 1, 1 + 2x + 3y => 2)) | ||
3 │ ├─ SYM(z) | ||
4 │ └─ POW | ||
5 │ ├─ ADD(scalar = 1, coeffs = (y => 3, x => 2)) | ||
6 │ │ ├─ 1 | ||
7 │ │ ├─ MUL(scalar = 2, powers = (x => 1,)) | ||
8 │ │ │ ├─ 2 | ||
9 │ │ │ └─ SYM(x) | ||
10 │ │ └─ MUL(scalar = 3, powers = (y => 1,)) | ||
11 │ │ ├─ 3 | ||
12 │ │ └─ SYM(y) | ||
13 │ └─ 2 | ||
14 └─ ADD(scalar = 0, coeffs = (z => 1, x => 2)) | ||
15 ├─ SYM(z) | ||
16 └─ MUL(scalar = 2, powers = (x => 1,)) | ||
17 ├─ 2 | ||
18 └─ SYM(x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
1 DIV | ||
2 ├─ MUL(scalar = 1, powers = (z => 1, 1 + 2x + 3y => 2)) | ||
3 │ ├─ SYM(z) | ||
4 │ └─ POW | ||
5 │ ├─ ADD(scalar = 1, coeffs = (y => 3, x => 2)) | ||
6 │ │ ├─ 1 | ||
7 │ │ ├─ MUL(scalar = 2, powers = (x => 1,)) | ||
8 │ │ │ ├─ 2 | ||
9 │ │ │ └─ SYM(x) | ||
10 │ │ └─ MUL(scalar = 3, powers = (y => 1,)) | ||
11 │ │ ├─ 3 | ||
12 │ │ └─ SYM(y) | ||
13 │ └─ 2 | ||
14 └─ ADD(scalar = 0, coeffs = (z => 1, x => 2)) | ||
15 ├─ SYM(z) | ||
16 └─ MUL(scalar = 2, powers = (x => 1,)) | ||
17 ├─ 2 | ||
18 └─ SYM(x) | ||
|
||
Hint: call SymbolicUtils.pluck(expr, line_number) to get the subexpression starting at line_number |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
1 MUL(scalar = 3, powers = (y => 1,)) | ||
2 ├─ 3 | ||
3 └─ SYM(y) | ||
|
||
Hint: call SymbolicUtils.pluck(expr, line_number) to get the subexpression starting at line_number |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
1 ADD(scalar = 0, coeffs = (z => 1, x => 2)) | ||
2 ├─ SYM(z) | ||
3 └─ MUL(scalar = 2, powers = (x => 1,)) | ||
4 ├─ 2 | ||
5 └─ SYM(x) | ||
|
||
Hint: call SymbolicUtils.pluck(expr, line_number) to get the subexpression starting at line_number |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters