-
Notifications
You must be signed in to change notification settings - Fork 116
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
constructor-level simplification #154
Conversation
@YingboMa what was your benchmark big expression? |
To generate a big expression, I recommend to use julia> using LinearAlgebra, ModelingToolkit
julia> @variables X[1:5, 1:5];
julia> det(lu(X)) |
…umber Before: ```julia julia> -1 + x + -1 + 1 1 + -1 + -1 + x ``` After: ```julia julia> -1 + x + -1 + 1 -1 + x ```
@shashi there was +(a::Number, b::Add) = iszero(a) ? b : Add(a, make_add_dict(1, b)) before and I changed it to +(a::Number, b::Add) = iszero(a) ? b : Add(a + b.coeff, b.dict) When |
BTW, we used macro bigexpr()
ex = :(x + y + z + z * y + o * x + x)
bigex = Expr(:call,)
bigex.args = [:+; mapreduce(_->ex.args[2:end], vcat, 1:200)];
esc(bigex)
end to benchmark construction speed before. SymEngine: julia> using SymEngine
julia> @vars x y z o
(x, y, z, o)
julia> @time @bigexpr
0.001075 seconds (2.80 k allocations: 62.609 KiB)
400*x + 200*y + 200*z + 200*o*x + 200*y*z SymbolicUtils: julia> using SymbolicUtils
julia> @syms x y z o
(x, y, z, o)
julia> @time @bigexpr
0.001439 seconds (35.57 k allocations: 2.088 MiB)
200o * x + 200y + 200z + 200y * z + 400x SymbolicUtils master: julia> @time simplify(expr, polynorm=true)
0.162938 seconds (3.59 M allocations: 64.250 MiB, 1.61% gc time)
(400 * x) + (200 * (y + z)) + (200 * o * x) + (200 * y * z) PS: I did run twice to make sure that there's no compile time. |
Co-authored-by: "Yingbo Ma" <[email protected]>
Co-authored-by: "Yingbo Ma" <[email protected]>
Co-authored-by: "Yingbo Ma" <[email protected]>
Co-authored-by: "Yingbo Ma" <[email protected]>
Codecov Report
@@ Coverage Diff @@
## master #154 +/- ##
==========================================
+ Coverage 91.55% 91.93% +0.37%
==========================================
Files 11 11
Lines 687 818 +131
==========================================
+ Hits 629 752 +123
- Misses 58 66 +8
Continue to review full report at Codecov.
|
fixes #151 |
Co-authored-by: "Yingbo Ma" <[email protected]>
Co-authored-by: "Yingbo Ma" <[email protected]>
Co-authored-by: "Yingbo Ma" <[email protected]>
Co-authored-by: "Yingbo Ma" <[email protected]>
Co-authored-by: "Shashi Gowda" <[email protected]>
@cscherrer @david-pl pinging you both since you have been using this library! +, -, *, /, ^ on The interface of If you never access the fields of a term or never dispatch on Now instead of dispatching on EDIT:
Hopefully this helps, feel free to ping me for help any time! |
This should have been restricted to |
Thanks @shashi! This reminds me of a question. It's a little bit of a tangent, so I'll ask in the Zulip and edit this message with a link. EDIT: Here it is: https://julialang.zulipchat.com/#narrow/stream/236639-symbolic-programming/topic/Constructor-level.20simplification/near/222106581 |
It certainly surprised me, so maybe I should put a reference to this PR with information about that behaviour in manual/page? How are the perspectives for solving this issue looking? I'm wondering whether to put a note about that behaviour in manual as for me it's certainly surprising that e.g. julia> @syms f() g()
(f()::Number, g()::Number)
julia> 2g + g*f + f*g + g
3g()::Number + 2f()::Number*g()::Number of course I suppose that it's mostly ok for the functions that return numbers (at least can't think of the reason why it wouldn't be ATM), since |
For those looking at this now, it was fixed in #259 👍 |
This design is taken from SymEngine. It is similar to what I had going on here: https://github.com/shashi/ModelingToolkit.jl/blob/s/simplification/src/polyform.jl with optimizations for the simplest cases.
fixes #151
todo: