-
-
Notifications
You must be signed in to change notification settings - Fork 481
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
Computing Partitions(...).cardinality()
with set min_length=
can be significantly improved by complementation and using max_length=
instead
#38897
Comments
Permutations(...).cardinality()
with set min_length=
can be significantly improved by complementation and using max_length=
insteadPartitions(...).cardinality()
with set min_length=
can be significantly improved by complementation and using max_length=
instead
If I made no mistake, We do have a dedicated cardinality function for partitions of given length, though.
|
PS: I appreciate your reports a lot! |
It's unfortunate that On a related note, I think Sage should issue a warning (at verbosity = 0 or 1 level) whenever it knowingly uses a non-efficient algorithm (such as enumeration via generation). I use to believe that whenever a certain functionality exists it implements the best available algorithm. Knowing when this is not the case will greatly help to find bottlenecks and avoid using slow algorithms when performance is an issue. |
Any combinations of any subset of
I think verbosity is hard to maintain. However, I use
|
@mantepse: thank you for the improvements! A nice testcase is the Sage code that I gave https://oeis.org/A333148 - how fast can it compute terms for |
Apparently, I missed a case, because |
@maxale, I have now coded that, too. It is not completely satisfying, because if you call With some effort, one can bypass the problem as follows: from sage.combinat.partition import number_of_partitions_length_max_part
def P(n, a, b, k, m):
"""
Return the number of partitions of n with parts between a and b and length between k and m.
sage: def D(n):
....: s1 = number_of_partitions(n)
....: s2 = sum(P(m, 0, l, k, k)
....: * P(n-m-l^2, 0, Infinity, k+2*l, Infinity)
....: for l in range(1, (n+1).isqrt())
....: for m in range((n-l^2-2*l)*l//(l+1)+1)
....: for k in range(ceil(m/l), min(m, n-m-l^2-2*l)+1))
....: return s1 + s2
"""
n = ZZ(n)
if a <= 0: a = 1
if b > n: b = n
if k < 0: k = 0
if m > n: m = n
# print(n,a,b,k,m)
if not k and m == n and a == 1:
# unrestricted length, parts smaller b
return ZZ.sum(number_of_partitions_length(n, i) for i in range(b + 1))
return ZZ.sum(number_of_partitions_length_max_part(n - (a - 1)*l, l, b - a + 1)
for l in range(k, m+1)) Doing so, |
@mantepse: Thanks for checking. My hope is to have an efficient and more or less universal |
Problem Description
Check this out:
Proposed Solution
As the code example shows, computing cardinality with
min_length=
appears to be much slower than that withmax_length=
. Hence, it'd be beneficial to convert one into the other by complementation (in the set of all partitions) as illustrated by the example.Alternatives Considered
I believe the cardinalities in both cases can be computed via dynamic programming, but I did not check if Sage uses it wisely and why in one case it's much slower than in the other.
Additional Information
No response
Is there an existing issue for this?
The text was updated successfully, but these errors were encountered: