-
Couldn't load subscription status.
- Fork 124
Optimize CSE: Transition to DAG Representation with Hash Consing for Faster Equality Checks #688
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
081e4ae
Topological sort directed acyclic graph
bowenszhu 3530de6
Refactor CSE by leveraging DAG structure
bowenszhu 5f1f8ff
Import `topological_sort` in CSE test
bowenszhu b38a2a6
Fix CSE tests due to new DAG implementation
bowenszhu eaf4446
Add tests for CSE DAG
bowenszhu f2673bf
Add docstring for `topological_sort`
bowenszhu 3010908
Remove outdated CSE functions
bowenszhu 48cc5a6
Import DocStringExtensions in SymbolicUtils.Code
bowenszhu 700a8c6
Add more tests for DAG CSE
bowenszhu 4426e88
Handle array symbolics in CSE `topological_sort`
bowenszhu 9c0c27c
Test CSE `topological_sort` on array of symbolics
bowenszhu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,10 +1,56 @@ | ||
| using SymbolicUtils, SymbolicUtils.Code, Test | ||
| using SymbolicUtils.Code: topological_sort | ||
|
|
||
| @testset "CSE" begin | ||
| @syms x | ||
| t = cse(hypot(hypot(cos(x), sin(x)), atan(cos(x), sin(x)))) | ||
|
|
||
| @test t isa Let | ||
| @test length(t.pairs) == 2 | ||
| @test occursin(t.pairs[1].lhs, t.body) | ||
| @test occursin(t.pairs[2].lhs, t.body) | ||
| @test length(t.pairs) == 4 | ||
| @test occursin(t.pairs[3].lhs, t.body) | ||
| @test occursin(t.pairs[4].lhs, t.body) | ||
| end | ||
|
|
||
| @testset "DAG CSE" begin | ||
| @syms a b | ||
| expr = sin(a + b) * (a + b) | ||
| sorted_nodes = topological_sort(expr) | ||
| @test length(sorted_nodes) == 3 | ||
| @test isequal(sorted_nodes[1].rhs, a + b) | ||
| @test isequal(sin(sorted_nodes[1].lhs), sorted_nodes[2].rhs) | ||
|
|
||
| expr = (a + b)^(a + b) | ||
| sorted_nodes = topological_sort(expr) | ||
| @test length(sorted_nodes) == 2 | ||
| @test isequal(sorted_nodes[1].rhs, a + b) | ||
| ab_node = sorted_nodes[1].lhs | ||
| @test isequal(ab_node^ab_node, sorted_nodes[2].rhs) | ||
| let_expr = cse(expr) | ||
| @test length(let_expr.pairs) == 1 | ||
| @test isequal(let_expr.pairs[1].rhs, a + b) | ||
| corresponding_sym = let_expr.pairs[1].lhs | ||
| @test isequal(let_expr.body, corresponding_sym^corresponding_sym) | ||
|
|
||
| expr = a + b | ||
| sorted_nodes = topological_sort(expr) | ||
| @test length(sorted_nodes) == 1 | ||
| @test isequal(sorted_nodes[1].rhs, a + b) | ||
| let_expr = cse(expr) | ||
| @test isempty(let_expr.pairs) | ||
| @test isequal(let_expr.body, a + b) | ||
|
|
||
| expr = a | ||
| sorted_nodes = topological_sort(expr) | ||
| @test isempty(sorted_nodes) | ||
| let_expr = cse(expr) | ||
| @test isempty(let_expr.pairs) | ||
| @test isequal(let_expr.body, a) | ||
|
|
||
| # array symbolics | ||
| # https://github.com/JuliaSymbolics/SymbolicUtils.jl/pull/688#pullrequestreview-2554931739 | ||
| @syms c | ||
| function foo end | ||
| ex = term(foo, [a^2 + b^2, b^2 + c], c; type = Real) | ||
| sorted_nodes = topological_sort(ex) | ||
| @test length(sorted_nodes) == 6 | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.