You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the following set up we create a polynomial f with about 100,000 random terms (with coefficients equal 1) of degree 100 in 8 variables:
sage: IV = IntegerVectors(100,8)
sage: R = PolynomialRing(QQ,8,'x')
sage: x = R.gens()
sage: f = R({IV.random_element():1 for _ in range(10^5)})
Let's sum up the coefficients of f by accessing them via f[...]:
sage: %time sum( f[t.degrees()] for _,t in f )
CPU times: user 4min 41s, sys: 25 ms, total: 4min 41s
Wall time: 4min 41s
100000
We see that these operations take 4min 41s, that is 281 seconds.
Expected Behavior
Almost 5 minutes for a cycle with just 100,000 iterations already look suspicious. Let's show that with a bit of wrapping, we get the same result in about 1 second:
sage: %time D = { t.degrees():c for c,t in f }
CPU times: user 609 ms, sys: 35 ms, total: 644 ms
Wall time: 644 ms
sage: %time sum( D[t.degrees()] for _,t in f )
CPU times: user 418 ms, sys: 999 µs, total: 419 ms
Wall time: 419 ms
100000
Here I first turned f into a dict mapping each term degrees to the corresponding coefficient, and then summed up the coefficients of f in the same way, basically replacing f[t.degrees()], extracting the coefficient from f, with D[t.degrees()] extracting the coefficient from the dictionary instead. As the timing shows, constructing the dictionary D and extracting and summing coefficients from it altogether take about 1 second.
Actual Behavior
I assumed that internally f is represented as a kind-of dictionary with a fast access to the coefficients given a term degrees. However, the above experiments disprove that by showing that accessing coefficients in the standard way is almost 300 times slower than if an actual dictionary was used. Furthermore, as illustrated, this speed-up can be easily achieved in practice.
maxale
changed the title
accessing coefficients of a multivariate polynomial is extremely slow
accessing coefficients of a multivariate polynomial f via f[...] or f.coefficient(...) is extremely slow
Sep 7, 2024
@mantepse: I've tested with sparse=True in PolynomialRing, and it gave just about a 2-fold speed-up, which is still unacceptably slow. Also, I believe that access to the coefficients should be fast for both sparse and non-sparse polynomials.
Then fixing this issue would involve a tradeoff where you in parallel maintain a dictionary of coefficients and the Singular representation, which will kill performance in pretty much everything else.
Perhaps the best thing to do is to add in the documentation "Warning: this method takes linear time in the number of coefficients, unlike __getitem__ method of list, dict and pretty much anything else."
Steps To Reproduce
In the following set up we create a polynomial
f
with about 100,000 random terms (with coefficients equal 1) of degree 100 in 8 variables:Let's sum up the coefficients of
f
by accessing them viaf[...]
:We see that these operations take
4min 41s
, that is 281 seconds.Expected Behavior
Almost 5 minutes for a cycle with just 100,000 iterations already look suspicious. Let's show that with a bit of wrapping, we get the same result in about 1 second:
Here I first turned
f
into a dict mapping each term degrees to the corresponding coefficient, and then summed up the coefficients off
in the same way, basically replacingf[t.degrees()]
, extracting the coefficient fromf
, withD[t.degrees()]
extracting the coefficient from the dictionary instead. As the timing shows, constructing the dictionaryD
and extracting and summing coefficients from it altogether take about 1 second.Actual Behavior
I assumed that internally
f
is represented as a kind-of dictionary with a fast access to the coefficients given a term degrees. However, the above experiments disprove that by showing that accessing coefficients in the standard way is almost 300 times slower than if an actual dictionary was used. Furthermore, as illustrated, this speed-up can be easily achieved in practice.Additional Information
No response
Environment
Checklist
The text was updated successfully, but these errors were encountered: