Skip to content
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

k-Combinations with negative k #39411

Open
2 tasks done
MarcusAichmayr opened this issue Jan 30, 2025 · 11 comments
Open
2 tasks done

k-Combinations with negative k #39411

MarcusAichmayr opened this issue Jan 30, 2025 · 11 comments
Labels

Comments

@MarcusAichmayr
Copy link

MarcusAichmayr commented Jan 30, 2025

Steps To Reproduce

Call

sage: list(Combinations(3, -1))

Expected Behavior

sage: list(Combinations(3, -1))
[]

Actual Behavior

Instead, we obtain a ValueError:

sage: list(Combinations(3, -1))
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/tmp/ipykernel_8533/142333014.py in <module>
----> 1 list(Combinations(Integer(3), -Integer(1)))

/usr/lib/python3/dist-packages/sage/combinat/combination.py in _iterator(self, items, n)
    451             [[1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]
    452         """
--> 453         for combination in itertools.combinations(items, n):
    454             yield list(combination)
    455 

ValueError: r must be non-negative

Additional Information

How many k-combinations of {1, ..., n} exist for k = -1? According to Wikipedia, this number is equal to the binomial coefficient (n, k). For k = -1, we have (n, -1) = 0. Hence, there are no -1-combinations and we should obtain the empty list.

Environment

  • OS: Ubuntu 22.04 (CoCalc)
  • Sage Version: 10.4

Checklist

  • I have searched the existing issues for a bug report that matches the one I want to file, without success.
  • I have read the documentation and troubleshoot guide
@pawani27
Copy link

@MarcusAichmayr For this we can insert a condition to check for negative values and return an empty list if this condition is true. I will add a PR with the required changes.

@abhishekdubey369
Copy link

@MarcusAichmayr I would like to work on this assign me

@DaveWitteMorris
Copy link
Member

A user who passes a negative number for r has probably made a mistake, so I think the ValueError is reasonable. (I think the error comes from itertools.combinations, and I think staying consistent with that has value.) If this is going to be changed, I think there will need to be a deprecation period first. But I personally am not at all convinced that it should be changed, and I think just documenting the behaviour might be best.

@abhishekdubey369
Copy link

abhishekdubey369 commented Jan 31, 2025 via email

@pawani27
Copy link

@DaveWitteMorris shouldn't the error be also returned in the case of list(Combinations(-1,3))? This is just an example. Though the output I'm getting is an empty list.

@DaveWitteMorris
Copy link
Member

Yes, I think an error would be better. However, this is a change in behaviour that is not fixing a clear bug, so there would need to be a deprecation. For the first year, the function needs to behave as it always did, but print a warning message that the default behaviour will change in the future.

This could be handled by adding a keyword argument allow_negative=None. If the user passes a negative value, then this argument needs to be checked:

  • If allow_negative is None, then [] is returned and a brief deprecation warning is printed, telling them that the default value of allow_negative will be false in the future, but they can use allow_negative=True.
  • If allow_negative is True, then [] is returned and no warning is printed.
  • If allow_negative is False, then a ValueError is raised.

See Deprecation and sage.misc.superseded.deprecation for more information about deprecation.

@pawani27
Copy link

pawani27 commented Feb 3, 2025

Okay I will work on this.

@DaveWitteMorris
Copy link
Member

@pawani27: Didn't abhishekdubey369 reply before you did that they were going to work on this issue? Please coordinate with them so there isn't wasted effort.

@pawani27
Copy link

pawani27 commented Feb 3, 2025

I actually did start working on this as you can see from my very first comment on this issue but no problem if @abhishekdubey369 has already made progress on this. @abhishekdubey369 Please let me know as to your preference.

@abhishekdubey369
Copy link

abhishekdubey369 commented Feb 3, 2025 via email

@pawani27
Copy link

pawani27 commented Feb 3, 2025

@abhishekdubey369 Yeah sure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants