Skip to content

IntegerVectors efficiency and corner cases #36816

@jukkakohonen

Description

@jukkakohonen

Problem Description

A few things, so closely interrelated that it makes sense to collect them together:

  1. IntegerVectors_nk has no cardinality method, so it defaults to the slow method of iterating: IntegerVectors(200,5).cardinality() takes something like a minute to compute 70058751.
  2. IntegerVectors_nk has no unrank method: similarly IntegerVectors(200,5).unrank(70000000) takes a minute.
  3. IntegerVectors_n and IntegerVectors_k also have no unrank, making it slow.
  4. IntegerVectors_n and IntegerVectors_k always claim to be InfiniteEnumeratedSets with cardinality +Infinity. More appropriately IntegerVectors_n(0) and IntegerVectors_k(0) should be finite sets of cardinality 1, because they contain only the empty vector.
  5. IntegerVectors(k=0)[1] loops forever, while it should just raise IndexError because the set has only one element.

Proposed Solution

  1. Compute cardinality by stars and bars. Then IntegerVectors(200,5).cardinality() will take something like a millisecond.
  2. Use a fast method based on 1.
  3. Similar to the previous, but slightly more complicated. Still a fast unrank should be doable.
  4. Both cases should give a FiniteEnumeratedSets with cardinality 1, if the parameter is zero.
  5. This bug would be fixed by the solution to 3.

Alternatives Considered

For 5., a simpler fix (without doing 3. fully) would be just to check for the corner case.

Additional Information

1., 2. and 3. are efficiency issues, but 4. and 5. are in fact bugs.

IntegerVectors has also other possible parameters, like max_slope and min_slope. This issue is not meant to change functionality in those cases (it would be more complicated), but only the base case with parameters n and/or k.

Related to item 1., a fast rank was already implemented in #15609.

Is there an existing issue for this?

  • I have searched the existing issues for a bug report that matches the one I want to file, without success.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions