@@ -364,9 +364,9 @@ def bell_number(n, algorithm='flint', **options) -> Integer:
364
364
365
365
TESTS::
366
366
367
- sage: all(bell_number(n) == bell_number(n,'dobinski') for n in range(200 ))
367
+ sage: all(bell_number(n) == bell_number(n,'dobinski') for n in range(100 ))
368
368
True
369
- sage: all(bell_number(n) == bell_number(n,'gap') for n in range(200 ))
369
+ sage: all(bell_number(n) == bell_number(n,'gap') for n in range(100 ))
370
370
True
371
371
sage: all(bell_number(n) == bell_number(n,'mpmath', prec=500) for n in range(200, 220))
372
372
True
@@ -854,16 +854,23 @@ def lucas_number2(n, P, Q):
854
854
return libgap .Lucas (P , Q , n )[1 ].sage ()
855
855
856
856
857
- def stirling_number1 (n , k ) -> Integer :
857
+ def stirling_number1 (n , k , algorithm = "gap" ) -> Integer :
858
858
r"""
859
859
Return the `n`-th Stirling number `S_1(n,k)` of the first kind.
860
860
861
861
This is the number of permutations of `n` points with `k` cycles.
862
862
863
- This wraps GAP's ``Stirling1``.
864
-
865
863
See :wikipedia:`Stirling_numbers_of_the_first_kind`.
866
864
865
+ INPUT:
866
+
867
+ - ``n`` -- nonnegative machine-size integer
868
+ - ``k`` -- nonnegative machine-size integer
869
+ - ``algorithm``:
870
+
871
+ * ``"gap"`` (default) -- use GAP's ``Stirling1`` function
872
+ * ``"flint"`` -- use flint's ``arith_stirling_number_1u`` function
873
+
867
874
EXAMPLES::
868
875
869
876
sage: stirling_number1(3,2)
@@ -876,31 +883,48 @@ def stirling_number1(n, k) -> Integer:
876
883
269325
877
884
878
885
Indeed, `S_1(n,k) = S_1(n-1,k-1) + (n-1)S_1(n-1,k)`.
886
+
887
+ TESTS::
888
+
889
+ sage: stirling_number1(10,5, algorithm='flint')
890
+ 269325
891
+
892
+ sage: s_sage = stirling_number1(50,3, algorithm="mutta")
893
+ Traceback (most recent call last):
894
+ ...
895
+ ValueError: unknown algorithm: mutta
879
896
"""
880
897
n = ZZ (n )
881
898
k = ZZ (k )
882
- from sage .libs .gap .libgap import libgap
883
- return libgap .Stirling1 (n , k ).sage ()
899
+ if algorithm == 'gap' :
900
+ from sage .libs .gap .libgap import libgap
901
+ return libgap .Stirling1 (n , k ).sage ()
902
+ if algorithm == 'flint' :
903
+ import sage .libs .flint .arith
904
+ return sage .libs .flint .arith .stirling_number_1 (n , k )
905
+ raise ValueError ("unknown algorithm: %s" % algorithm )
884
906
885
907
886
908
def stirling_number2 (n , k , algorithm = None ) -> Integer :
887
909
r"""
888
- Return the `n`-th Stirling number `S_2(n,k)` of the second
889
- kind.
910
+ Return the `n`-th Stirling number `S_2(n,k)` of the second kind.
890
911
891
912
This is the number of ways to partition a set of `n` elements into `k`
892
913
pairwise disjoint nonempty subsets. The `n`-th Bell number is the
893
914
sum of the `S_2(n,k)`'s, `k=0,...,n`.
894
915
916
+ See :wikipedia:`Stirling_numbers_of_the_second_kind`.
917
+
895
918
INPUT:
896
919
897
- * ``n`` - nonnegative machine-size integer
898
- * ``k`` - nonnegative machine-size integer
899
- * ``algorithm``:
920
+ - ``n`` - - nonnegative machine-size integer
921
+ - ``k`` - - nonnegative machine-size integer
922
+ - ``algorithm``:
900
923
901
- * None (default) - use native implementation
902
- * ``"maxima"`` - use Maxima's stirling2 function
903
- * ``"gap"`` - use GAP's Stirling2 function
924
+ * ``None`` (default) -- use native implementation
925
+ * ``"flint"`` -- use flint's ``arith_stirling_number_2`` function
926
+ * ``"gap"`` -- use GAP's ``Stirling2`` function
927
+ * ``"maxima"`` -- use Maxima's ``stirling2`` function
904
928
905
929
EXAMPLES:
906
930
@@ -922,10 +946,10 @@ def stirling_number2(n, k, algorithm=None) -> Integer:
922
946
923
947
Stirling numbers satisfy `S_2(n,k) = S_2(n-1,k-1) + kS_2(n-1,k)`::
924
948
925
- sage: 5*stirling_number2(9,5) + stirling_number2(9,4)
926
- 42525
927
- sage: stirling_number2(10,5)
928
- 42525
949
+ sage: 5*stirling_number2(9,5) + stirling_number2(9,4)
950
+ 42525
951
+ sage: stirling_number2(10,5)
952
+ 42525
929
953
930
954
TESTS::
931
955
@@ -961,58 +985,57 @@ def stirling_number2(n, k, algorithm=None) -> Integer:
961
985
1900842429486
962
986
sage: type(n)
963
987
<class 'sage.rings.integer.Integer'>
964
- sage: n = stirling_number2(20,11,algorithm='maxima ')
988
+ sage: n = stirling_number2(20,11,algorithm='flint ')
965
989
sage: n
966
990
1900842429486
967
991
sage: type(n)
968
992
<class 'sage.rings.integer.Integer'>
969
993
970
- Sage's implementation splitting the computation of the Stirling
971
- numbers of the second kind in two cases according to `n`, let us
972
- check the result it gives agree with both maxima and gap.
973
-
974
- For `n<200`::
994
+ Sage's implementation splitting the computation of the Stirling
995
+ numbers of the second kind in two cases according to `n`, let us
996
+ check the result it gives agree with both flint and gap.
975
997
976
- sage: for n in Subsets(range(100,200), 5).random_element():
977
- ....: for k in Subsets(range(n), 5).random_element():
978
- ....: s_sage = stirling_number2(n,k)
979
- ....: s_maxima = stirling_number2(n,k, algorithm = "maxima")
980
- ....: s_gap = stirling_number2(n,k, algorithm = "gap")
981
- ....: if not (s_sage == s_maxima and s_sage == s_gap):
982
- ....: print("Error with n<200")
998
+ For `n<200`::
983
999
984
- For `n\geq 200`::
1000
+ sage: for n in Subsets(range(100,200), 5).random_element():
1001
+ ....: for k in Subsets(range(n), 5).random_element():
1002
+ ....: s_sage = stirling_number2(n,k)
1003
+ ....: s_flint = stirling_number2(n,k, algorithm = "flint")
1004
+ ....: s_gap = stirling_number2(n,k, algorithm = "gap")
1005
+ ....: if not (s_sage == s_flint and s_sage == s_gap):
1006
+ ....: print("Error with n<200")
985
1007
986
- sage: for n in Subsets(range(200,300), 5).random_element():
987
- ....: for k in Subsets(range(n), 5).random_element():
988
- ....: s_sage = stirling_number2(n,k)
989
- ....: s_maxima = stirling_number2(n,k, algorithm = "maxima")
990
- ....: s_gap = stirling_number2(n,k, algorithm = "gap")
991
- ....: if not (s_sage == s_maxima and s_sage == s_gap):
992
- ....: print("Error with n<200")
1008
+ For `n\geq 200`::
993
1009
1010
+ sage: for n in Subsets(range(200,300), 5).random_element():
1011
+ ....: for k in Subsets(range(n), 5).random_element():
1012
+ ....: s_sage = stirling_number2(n,k)
1013
+ ....: s_flint = stirling_number2(n,k, algorithm = "flint")
1014
+ ....: s_gap = stirling_number2(n,k, algorithm = "gap")
1015
+ ....: if not (s_sage == s_flint and s_sage == s_gap):
1016
+ ....: print("Error with n<200")
994
1017
995
- TESTS:
1018
+ sage: stirling_number2(20,3, algorithm="maxima")
1019
+ 580606446
996
1020
997
- Checking an exception is raised whenever a wrong value is given
998
- for ``algorithm``::
999
-
1000
- sage: s_sage = stirling_number2(50,3, algorithm = "CloudReading")
1001
- Traceback (most recent call last):
1002
- ...
1003
- ValueError: unknown algorithm: CloudReading
1021
+ sage: s_sage = stirling_number2(5,3, algorithm="namba")
1022
+ Traceback (most recent call last):
1023
+ ...
1024
+ ValueError: unknown algorithm: namba
1004
1025
"""
1005
1026
n = ZZ (n )
1006
1027
k = ZZ (k )
1007
1028
if algorithm is None :
1008
1029
return _stirling_number2 (n , k )
1009
- elif algorithm == 'gap' :
1030
+ if algorithm == 'gap' :
1010
1031
from sage .libs .gap .libgap import libgap
1011
1032
return libgap .Stirling2 (n , k ).sage ()
1012
- elif algorithm == 'maxima' :
1033
+ if algorithm == 'flint' :
1034
+ import sage .libs .flint .arith
1035
+ return sage .libs .flint .arith .stirling_number_2 (n , k )
1036
+ if algorithm == 'maxima' :
1013
1037
return ZZ (maxima .stirling2 (n , k )) # type:ignore
1014
- else :
1015
- raise ValueError ("unknown algorithm: %s" % algorithm )
1038
+ raise ValueError ("unknown algorithm: %s" % algorithm )
1016
1039
1017
1040
1018
1041
def polygonal_number (s , n ):
@@ -1404,8 +1427,6 @@ def __bool__(self) -> bool:
1404
1427
"""
1405
1428
return bool (self ._list )
1406
1429
1407
-
1408
-
1409
1430
def __len__ (self ) -> Integer :
1410
1431
"""
1411
1432
EXAMPLES::
@@ -2442,7 +2463,6 @@ def __iter__(self):
2442
2463
2443
2464
##############################################################################
2444
2465
from sage .sets .image_set import ImageSubobject
2445
- from sage .categories .map import is_Map
2446
2466
2447
2467
2448
2468
class MapCombinatorialClass (ImageSubobject , CombinatorialClass ):
0 commit comments