test_partitions.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. from sympy.core.sorting import ordered, default_sort_key
  2. from sympy.combinatorics.partitions import (Partition, IntegerPartition,
  3. RGS_enum, RGS_unrank, RGS_rank,
  4. random_integer_partition)
  5. from sympy.testing.pytest import raises
  6. from sympy.utilities.iterables import partitions
  7. from sympy.sets.sets import Set, FiniteSet
  8. def test_partition_constructor():
  9. raises(ValueError, lambda: Partition([1, 1, 2]))
  10. raises(ValueError, lambda: Partition([1, 2, 3], [2, 3, 4]))
  11. raises(ValueError, lambda: Partition(1, 2, 3))
  12. raises(ValueError, lambda: Partition(*list(range(3))))
  13. assert Partition([1, 2, 3], [4, 5]) == Partition([4, 5], [1, 2, 3])
  14. assert Partition({1, 2, 3}, {4, 5}) == Partition([1, 2, 3], [4, 5])
  15. a = FiniteSet(1, 2, 3)
  16. b = FiniteSet(4, 5)
  17. assert Partition(a, b) == Partition([1, 2, 3], [4, 5])
  18. assert Partition({a, b}) == Partition(FiniteSet(a, b))
  19. assert Partition({a, b}) != Partition(a, b)
  20. def test_partition():
  21. from sympy.abc import x
  22. a = Partition([1, 2, 3], [4])
  23. b = Partition([1, 2], [3, 4])
  24. c = Partition([x])
  25. l = [a, b, c]
  26. l.sort(key=default_sort_key)
  27. assert l == [c, a, b]
  28. l.sort(key=lambda w: default_sort_key(w, order='rev-lex'))
  29. assert l == [c, a, b]
  30. assert (a == b) is False
  31. assert a <= b
  32. assert (a > b) is False
  33. assert a != b
  34. assert a < b
  35. assert (a + 2).partition == [[1, 2], [3, 4]]
  36. assert (b - 1).partition == [[1, 2, 4], [3]]
  37. assert (a - 1).partition == [[1, 2, 3, 4]]
  38. assert (a + 1).partition == [[1, 2, 4], [3]]
  39. assert (b + 1).partition == [[1, 2], [3], [4]]
  40. assert a.rank == 1
  41. assert b.rank == 3
  42. assert a.RGS == (0, 0, 0, 1)
  43. assert b.RGS == (0, 0, 1, 1)
  44. def test_integer_partition():
  45. # no zeros in partition
  46. raises(ValueError, lambda: IntegerPartition(list(range(3))))
  47. # check fails since 1 + 2 != 100
  48. raises(ValueError, lambda: IntegerPartition(100, list(range(1, 3))))
  49. a = IntegerPartition(8, [1, 3, 4])
  50. b = a.next_lex()
  51. c = IntegerPartition([1, 3, 4])
  52. d = IntegerPartition(8, {1: 3, 3: 1, 2: 1})
  53. assert a == c
  54. assert a.integer == d.integer
  55. assert a.conjugate == [3, 2, 2, 1]
  56. assert (a == b) is False
  57. assert a <= b
  58. assert (a > b) is False
  59. assert a != b
  60. for i in range(1, 11):
  61. next = set()
  62. prev = set()
  63. a = IntegerPartition([i])
  64. ans = {IntegerPartition(p) for p in partitions(i)}
  65. n = len(ans)
  66. for j in range(n):
  67. next.add(a)
  68. a = a.next_lex()
  69. IntegerPartition(i, a.partition) # check it by giving i
  70. for j in range(n):
  71. prev.add(a)
  72. a = a.prev_lex()
  73. IntegerPartition(i, a.partition) # check it by giving i
  74. assert next == ans
  75. assert prev == ans
  76. assert IntegerPartition([1, 2, 3]).as_ferrers() == '###\n##\n#'
  77. assert IntegerPartition([1, 1, 3]).as_ferrers('o') == 'ooo\no\no'
  78. assert str(IntegerPartition([1, 1, 3])) == '[3, 1, 1]'
  79. assert IntegerPartition([1, 1, 3]).partition == [3, 1, 1]
  80. raises(ValueError, lambda: random_integer_partition(-1))
  81. assert random_integer_partition(1) == [1]
  82. assert random_integer_partition(10, seed=[1, 3, 2, 1, 5, 1]
  83. ) == [5, 2, 1, 1, 1]
  84. def test_rgs():
  85. raises(ValueError, lambda: RGS_unrank(-1, 3))
  86. raises(ValueError, lambda: RGS_unrank(3, 0))
  87. raises(ValueError, lambda: RGS_unrank(10, 1))
  88. raises(ValueError, lambda: Partition.from_rgs(list(range(3)), list(range(2))))
  89. raises(ValueError, lambda: Partition.from_rgs(list(range(1, 3)), list(range(2))))
  90. assert RGS_enum(-1) == 0
  91. assert RGS_enum(1) == 1
  92. assert RGS_unrank(7, 5) == [0, 0, 1, 0, 2]
  93. assert RGS_unrank(23, 14) == [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 2, 2]
  94. assert RGS_rank(RGS_unrank(40, 100)) == 40
  95. def test_ordered_partition_9608():
  96. a = Partition([1, 2, 3], [4])
  97. b = Partition([1, 2], [3, 4])
  98. assert list(ordered([a,b], Set._infimum_key))