Skip to content

Commit 7734796

Browse files
committed
Add builtin PartitionsP[]
Extend combinatorica examples to page 64 or so.
1 parent df9f36d commit 7734796

3 files changed

Lines changed: 47 additions & 1 deletion

File tree

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ New builtins
1313
* ``CreateTemporary``
1414
* ``FileNames``
1515
* ``NIntegrate``
16+
* ``PartitionsP``
1617

1718
Enhancements
1819
++++++++++++

mathics/builtin/combinatorial.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import sympy
99
from sympy.functions.combinatorial.numbers import stirling
10+
from sympy.utilities.iterables import partitions
1011
from mathics.version import __version__ # noqa used in loading to check consistency.
1112

1213
from mathics.builtin.base import Builtin
@@ -105,6 +106,24 @@ def apply(self, n, evaluation):
105106
return Integer(sympy.fibonacci(n.get_int_value()))
106107

107108

109+
class PartitionsP(Builtin):
110+
"""
111+
<dl>
112+
<dt>'PartitionsP[$n$]'
113+
<dd>return the number $p$($n$) of unrestricted partitions of the integer $n$.
114+
</dl>
115+
116+
>> Table[PartitionsP[k], {k, 0, 12}]
117+
= {1, 1, 2, 3, 5, 7, 11, 15, 22, 30, 42, 56, 77}
118+
"""
119+
120+
attributes = ("Listable", "NumericFunction", "Orderless")
121+
122+
def apply(self, n, evaluation):
123+
"PartitionsP[n_Integer]"
124+
return Integer(len(list(partitions(n.get_int_value()))))
125+
126+
108127
class _NoBoolVector(Exception):
109128
pass
110129

test/test_combinatorica.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,33 @@ def test_combinations_1_5():
472472
"{Range[5]} ",
473473
"KSubsets[l, k] == Length(l)",
474474
),
475-
# Start here in section 2.1 ...
475+
):
476+
check_evaluation(str_expr, str_expected, message)
477+
478+
def test_2_1_to_2_3():
479+
480+
for str_expr, str_expected, message in (
481+
(
482+
# 2.1.1 - 2.1.3 are broken
483+
"PartitionsP[10]",
484+
"NumberOfPartitions[10]",
485+
"Counting Partitions 2.1.4, Page 57",
486+
),
487+
(
488+
"NumberOfCompositions[6,3]",
489+
"28",
490+
"Random Compositions 2.2.1, Page 60",
491+
),
492+
(
493+
"TableauQ[{{1,2,5}, {3,4,5}, {6}}]",
494+
"True",
495+
"Young Tableau 2.3, Page 63",
496+
),
497+
(
498+
"TableauQ[{{1,2,5,9,10}, {5,4,7,13}, {4,8,12},{11}}]",
499+
"False",
500+
"Young Tableau 2.3, Page 63",
501+
),
476502
):
477503
check_evaluation(str_expr, str_expected, message)
478504

0 commit comments

Comments
 (0)