Skip to content

Commit 2b3a9d8

Browse files
authored
Add missing builtins: combinatorial functions (#1134)
I pulled out just the combinatorial functions from #1133, knocks a couple of items off the list in #500
1 parent e0cffc0 commit 2b3a9d8

1 file changed

Lines changed: 125 additions & 0 deletions

File tree

mathics/builtin/intfns/combinatorial.py

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717
from mathics.core.atoms import Integer
1818
from mathics.core.attributes import (
1919
A_LISTABLE,
20+
A_N_HOLD_FIRST,
2021
A_NUMERIC_FUNCTION,
2122
A_ORDERLESS,
2223
A_PROTECTED,
2324
A_READ_PROTECTED,
2425
)
2526
from mathics.core.builtin import Builtin, MPMathFunction, SympyFunction
27+
from mathics.core.evaluation import Evaluation
2628
from mathics.core.expression import Expression
2729
from mathics.core.list import ListExpression
2830
from mathics.core.symbols import (
@@ -39,6 +41,36 @@
3941
SymbolSubsets = Symbol("Subsets")
4042

4143

44+
class BellB(SympyFunction):
45+
"""
46+
<url>
47+
:Bell number: https://en.wikipedia.org/wiki/Bell_number</url> (<url>
48+
:SymPy: https://docs.sympy.org/latest/modules/functions/combinatorial.html#sympy.functions.combinatorial.numbers.bell</url>, <url>
49+
:WMA: https://reference.wolfram.com/language/ref/BellB.html</url>)
50+
<dl>
51+
<dt>'BellB[$n$]'
52+
<dd>Bell number $B$_$n$.
53+
54+
<dt>'BellB[$n$, $x$]'
55+
<dd>Bell polynomial $B$_$n$($x$).
56+
</dl>
57+
58+
>> BellB[10]
59+
= 115975
60+
61+
>> BellB[5, x]
62+
= x + 15 x ^ 2 + 25 x ^ 3 + 10 x ^ 4 + x ^ 5
63+
"""
64+
65+
attributes = A_LISTABLE | A_N_HOLD_FIRST | A_PROTECTED | A_READ_PROTECTED
66+
summary_text = "Bell numbers"
67+
sympy_name = "bell"
68+
69+
def eval(self, z, evaluation: Evaluation):
70+
"%(name)s[z__]"
71+
return super().eval(z, evaluation)
72+
73+
4274
class _BooleanDissimilarity(Builtin):
4375
@staticmethod
4476
def _to_bool_vector(u):
@@ -184,6 +216,36 @@ def _compute(self, n, c_ff, c_ft, c_tf, c_tt):
184216
)
185217

186218

219+
class EulerE(SympyFunction):
220+
"""
221+
<url>
222+
:Euler numbers: https://en.wikipedia.org/wiki/Euler_numbers</url> (<url>
223+
:SymPy: https://docs.sympy.org/latest/modules/functions/combinatorial.html#sympy.functions.combinatorial.numbers.euler</url>, <url>
224+
:WMA: https://reference.wolfram.com/language/ref/EulerE.html</url>)
225+
<dl>
226+
<dt>'EulerE[$n$]'
227+
<dd>Euler number $E$_$n$.
228+
229+
<dt>'EulerE[$n$, $x$]'
230+
<dd>Euler polynomial $E$_$n$($x$).
231+
</dl>
232+
233+
>> Table[EulerE[k], {k, 0, 10}]
234+
= {1, 0, -1, 0, 5, 0, -61, 0, 1385, 0, -50521}
235+
236+
>> EulerE[5, z]
237+
= -1 / 2 + 5 z ^ 2 / 2 - 5 z ^ 4 / 2 + z ^ 5
238+
"""
239+
240+
attributes = A_LISTABLE | A_PROTECTED
241+
summary_text = "Euler numbers"
242+
sympy_name = "euler"
243+
244+
def eval(self, z, evaluation: Evaluation):
245+
"%(name)s[z__]"
246+
return super().eval(z, evaluation)
247+
248+
187249
class JaccardDissimilarity(_BooleanDissimilarity):
188250
"""
189251
<url>
@@ -214,6 +276,44 @@ def _compute(self, n, c_ff, c_ft, c_tf, c_tt):
214276
)
215277

216278

279+
class LucasL(SympyFunction):
280+
"""
281+
<url>
282+
:Lucas Number:
283+
https://en.wikipedia.org/wiki/Lucas_number</url> (<url>
284+
:SymPy:
285+
https://docs.sympy.org/latest/modules/functions/combinatorial.html#sympy.functions.combinatorial.numbers.lucas</url>, \
286+
<url>
287+
:WMA:
288+
https://reference.wolfram.com/language/ref/LucasL.html</url>)
289+
290+
<dl>
291+
<dt>'LucasL[$n$]'
292+
<dd>gives the $n$th Lucas number.
293+
</dl>
294+
295+
A list of the first five Lucas numbers:
296+
>> Table[LucasL[n], {n, 1, 5}]
297+
= {1, 3, 4, 7, 11}
298+
>> Series[LucasL[1/2, x], {x, 0, 5}]
299+
= 1 + 1 / 4 x + 1 / 32 x ^ 2 + (-1 / 128) x ^ 3 + (-5 / 2048) x ^ 4 + 7 / 8192 x ^ 5 + O[x] ^ 6
300+
"""
301+
302+
attributes = A_LISTABLE | A_NUMERIC_FUNCTION | A_PROTECTED | A_READ_PROTECTED
303+
304+
summary_text = "lucas number"
305+
sympy_name = "lucas"
306+
307+
rules = {
308+
"LucasL[n_, 1]": "LucasL[n]",
309+
"LucasL[n_, x_]": "(x/2 + Sqrt[1 + x^2 / 4])^n + Cos[n Pi] / (x/2 + Sqrt[1 + x^2 / 4])^n // Simplify",
310+
}
311+
312+
def eval_integer(self, n: Integer, evaluation):
313+
"LucasL[n_Integer]"
314+
return self.eval(n, evaluation)
315+
316+
217317
class MatchingDissimilarity(_BooleanDissimilarity):
218318
"""
219319
<url>:WMA link:https://reference.wolfram.com/language/ref/MatchingDissimilarity.html</url>
@@ -276,6 +376,31 @@ def eval(self, values, evaluation):
276376
return Expression(SymbolTimes, *elements)
277377

278378

379+
class PolygonalNumber(Builtin):
380+
"""
381+
<url>
382+
:Polygonal number: https://en.wikipedia.org/wiki/Polygonal_number</url> (<url>
383+
:WMA: https://reference.wolfram.com/language/ref/PolygonalNumber.html</url>)
384+
<dl>
385+
<dt>'PolygonalNumber[$r$, $n$]'
386+
<dd>gives the $n$th $r$-gonal number.
387+
</dl>
388+
389+
>> Table[PolygonalNumber[n], {n, 10}]
390+
= {1, 3, 6, 10, 15, 21, 28, 36, 45, 55}
391+
>> Table[PolygonalNumber[r, 10], {r, 3, 10}]
392+
= {55, 100, 145, 190, 235, 280, 325, 370}
393+
"""
394+
395+
attributes = A_LISTABLE | A_NUMERIC_FUNCTION | A_PROTECTED | A_READ_PROTECTED
396+
summary_text = "polygonal number"
397+
398+
rules = {
399+
"PolygonalNumber[n_Integer]": "PolygonalNumber[3, n]",
400+
"PolygonalNumber[r_Integer, n_Integer]": "(1/2) n (n (r - 2) - r + 4)",
401+
}
402+
403+
279404
class RogersTanimotoDissimilarity(_BooleanDissimilarity):
280405
"""
281406
<url>

0 commit comments

Comments
 (0)