48
48
49
49
from sage .categories .sets_cat import Sets
50
50
from sage .categories .enumerated_sets import EnumeratedSets
51
+ from sage .categories .finite_enumerated_sets import FiniteEnumeratedSets
51
52
52
53
import sage .rings .infinity
53
54
@@ -85,7 +86,7 @@ def has_finite_length(obj):
85
86
return True
86
87
87
88
88
- def Set (X = None ):
89
+ def Set (X = None , category = None ):
89
90
r"""
90
91
Create the underlying set of ``X``.
91
92
@@ -187,22 +188,22 @@ def Set(X=None):
187
188
if X is None :
188
189
X = []
189
190
elif isinstance (X , CategoryObject ):
190
- if isinstance (X , Set_generic ):
191
+ if isinstance (X , Set_generic ) and category is None :
191
192
return X
192
193
elif X in Sets ().Finite ():
193
- return Set_object_enumerated (X )
194
+ return Set_object_enumerated (X , category = category )
194
195
else :
195
- return Set_object (X )
196
+ return Set_object (X , category = category )
196
197
197
198
if isinstance (X , Element ) and not isinstance (X , Set_base ):
198
199
raise TypeError ("Element has no defined underlying set" )
199
200
200
201
try :
201
202
X = frozenset (X )
202
203
except TypeError :
203
- return Set_object (X )
204
+ return Set_object (X , category = category )
204
205
else :
205
- return Set_object_enumerated (X )
206
+ return Set_object_enumerated (X , category = category )
206
207
207
208
208
209
class Set_base ():
@@ -474,7 +475,7 @@ def __init__(self, X, category=None):
474
475
sage: type(Set(QQ))
475
476
<class 'sage.sets.set.Set_object_with_category'>
476
477
sage: Set(QQ).category()
477
- Category of sets
478
+ Category of infinite sets
478
479
479
480
TESTS::
480
481
@@ -492,6 +493,15 @@ def __init__(self, X, category=None):
492
493
493
494
if category is None :
494
495
category = Sets ()
496
+
497
+ if isinstance (X , CategoryObject ):
498
+ if X in Sets ().Finite ():
499
+ category = category .Finite ()
500
+ elif X in Sets ().Infinite ():
501
+ category = category .Infinite ()
502
+ if X in Sets ().Enumerated ():
503
+ category = category .Enumerated ()
504
+
495
505
Parent .__init__ (self , category = category )
496
506
self .__object = X
497
507
@@ -666,6 +676,9 @@ def cardinality(self):
666
676
sage: Set(GF(5^2,'a')).cardinality()
667
677
25
668
678
"""
679
+ if self in Sets ().Infinite ():
680
+ return sage .rings .infinity .infinity
681
+
669
682
if not self .is_finite ():
670
683
return sage .rings .infinity .infinity
671
684
@@ -680,7 +693,7 @@ def cardinality(self):
680
693
except TypeError :
681
694
pass
682
695
683
- raise NotImplementedError ( "computation of cardinality of %s not yet implemented" % self . __object )
696
+ return super (). cardinality ( )
684
697
685
698
def is_empty (self ):
686
699
"""
@@ -733,6 +746,10 @@ def is_finite(self):
733
746
sage: Set([1,'a',ZZ]).is_finite()
734
747
True
735
748
"""
749
+ if self in Sets ().Finite ():
750
+ return True
751
+ if self in Sets ().Infinite ():
752
+ return False
736
753
obj = self .__object
737
754
try :
738
755
is_finite = obj .is_finite
@@ -830,7 +847,7 @@ class Set_object_enumerated(Set_object):
830
847
"""
831
848
A finite enumerated set.
832
849
"""
833
- def __init__ (self , X ):
850
+ def __init__ (self , X , category = None ):
834
851
r"""
835
852
Initialize ``self``.
836
853
@@ -839,12 +856,12 @@ def __init__(self, X):
839
856
sage: S = Set(GF(19)); S
840
857
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18}
841
858
sage: S.category()
842
- Category of finite sets
859
+ Category of finite enumerated sets
843
860
sage: print(latex(S))
844
861
\left\{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18\right\}
845
862
sage: TestSuite(S).run()
846
863
"""
847
- Set_object .__init__ (self , X , category = Sets ().Finite ( ))
864
+ Set_object .__init__ (self , X , category = FiniteEnumeratedSets ().or_subcategory ( category ))
848
865
849
866
def random_element (self ):
850
867
r"""
@@ -1267,7 +1284,7 @@ def __classcall__(cls, X, Y, *args, **kwds):
1267
1284
Y = Set (Y )
1268
1285
return type .__call__ (cls , X , Y , * args , ** kwds )
1269
1286
1270
- def __init__ (self , X , Y , op , latex_op ):
1287
+ def __init__ (self , X , Y , op , latex_op , category = None ):
1271
1288
r"""
1272
1289
Initialization.
1273
1290
@@ -1284,7 +1301,7 @@ def __init__(self, X, Y, op, latex_op):
1284
1301
self ._Y = Y
1285
1302
self ._op = op
1286
1303
self ._latex_op = latex_op
1287
- Set_object .__init__ (self , self )
1304
+ Set_object .__init__ (self , self , category = category )
1288
1305
1289
1306
def _repr_ (self ):
1290
1307
r"""
@@ -1338,7 +1355,7 @@ class Set_object_union(Set_object_binary):
1338
1355
"""
1339
1356
A formal union of two sets.
1340
1357
"""
1341
- def __init__ (self , X , Y ):
1358
+ def __init__ (self , X , Y , category = None ):
1342
1359
r"""
1343
1360
Initialize ``self``.
1344
1361
@@ -1348,13 +1365,23 @@ def __init__(self, X, Y):
1348
1365
sage: T = Set(ZZ)
1349
1366
sage: X = S.union(T); X
1350
1367
Set-theoretic union of Set of elements of Vector space of dimension 2 over Rational Field and Set of elements of Integer Ring
1368
+ sage: X.category()
1369
+ Category of infinite sets
1351
1370
1352
1371
sage: latex(X)
1353
1372
\Bold{Q}^{2} \cup \Bold{Z}
1354
1373
1355
1374
sage: TestSuite(X).run()
1356
1375
"""
1357
- Set_object_binary .__init__ (self , X , Y , "union" , "\\ cup" )
1376
+ if category is None :
1377
+ category = Sets ()
1378
+ if all (S in Sets ().Enumerated () for S in (X , Y )):
1379
+ category = category .Enumerated ()
1380
+ if any (S in Sets ().Infinite () for S in (X , Y )):
1381
+ category = category .Infinite ()
1382
+ elif all (S in Sets ().Finite () for S in (X , Y )):
1383
+ category = category .Finite ()
1384
+ Set_object_binary .__init__ (self , X , Y , "union" , "\\ cup" , category = category )
1358
1385
1359
1386
def is_finite (self ):
1360
1387
r"""
@@ -1481,7 +1508,7 @@ class Set_object_intersection(Set_object_binary):
1481
1508
"""
1482
1509
Formal intersection of two sets.
1483
1510
"""
1484
- def __init__ (self , X , Y ):
1511
+ def __init__ (self , X , Y , category = None ):
1485
1512
r"""
1486
1513
Initialize ``self``.
1487
1514
@@ -1491,15 +1518,32 @@ def __init__(self, X, Y):
1491
1518
sage: T = Set(ZZ)
1492
1519
sage: X = S.intersection(T); X
1493
1520
Set-theoretic intersection of Set of elements of Vector space of dimension 2 over Rational Field and Set of elements of Integer Ring
1521
+ sage: X.category()
1522
+ Category of enumerated sets
1494
1523
sage: latex(X)
1495
1524
\Bold{Q}^{2} \cap \Bold{Z}
1496
1525
1497
1526
sage: X = Set(IntegerRange(100)).intersection(Primes())
1498
1527
sage: X.is_finite()
1499
1528
True
1529
+ sage: X.cardinality()
1530
+ 25
1531
+ sage: X.category()
1532
+ Category of finite enumerated sets
1533
+ sage: TestSuite(X).run()
1534
+
1535
+ sage: X = Set(Primes(), category=Sets()).intersection(Set(IntegerRange(200)))
1536
+ sage: X.cardinality()
1537
+ 46
1500
1538
sage: TestSuite(X).run()
1501
1539
"""
1502
- Set_object_binary .__init__ (self , X , Y , "intersection" , "\\ cap" )
1540
+ if category is None :
1541
+ category = Sets ()
1542
+ if any (S in Sets ().Finite () for S in (X , Y )):
1543
+ category = category .Finite ()
1544
+ if any (S in Sets ().Enumerated () for S in (X , Y )):
1545
+ category = category .Enumerated ()
1546
+ Set_object_binary .__init__ (self , X , Y , "intersection" , "\\ cap" , category = category )
1503
1547
1504
1548
def is_finite (self ):
1505
1549
r"""
@@ -1643,7 +1687,7 @@ class Set_object_difference(Set_object_binary):
1643
1687
"""
1644
1688
Formal difference of two sets.
1645
1689
"""
1646
- def __init__ (self , X , Y ):
1690
+ def __init__ (self , X , Y , category = None ):
1647
1691
r"""
1648
1692
Initialize ``self``.
1649
1693
@@ -1653,12 +1697,22 @@ def __init__(self, X, Y):
1653
1697
sage: T = Set(ZZ)
1654
1698
sage: X = S.difference(T); X
1655
1699
Set-theoretic difference of Set of elements of Rational Field and Set of elements of Integer Ring
1700
+ sage: X.category()
1701
+ Category of sets
1656
1702
sage: latex(X)
1657
1703
\Bold{Q} - \Bold{Z}
1658
1704
1659
1705
sage: TestSuite(X).run()
1660
1706
"""
1661
- Set_object_binary .__init__ (self , X , Y , "difference" , "-" )
1707
+ if category is None :
1708
+ category = Sets ()
1709
+ if X in Sets ().Enumerated ():
1710
+ category = category .Enumerated ()
1711
+ if X in Sets ().Finite ():
1712
+ category = category .Finite ()
1713
+ elif X in Sets ().Infinite () and Y in Sets ().Finite ():
1714
+ category = category .Infinite ()
1715
+ Set_object_binary .__init__ (self , X , Y , "difference" , "-" , category = category )
1662
1716
1663
1717
def is_finite (self ):
1664
1718
r"""
@@ -1787,13 +1841,17 @@ def _sympy_(self):
1787
1841
Set-theoretic difference of
1788
1842
Set of elements of Rational Field and
1789
1843
Set of elements of Integer Ring
1844
+ sage: X.category()
1845
+ Category of sets
1790
1846
sage: X._sympy_()
1791
1847
Complement(Rationals, Integers)
1792
1848
1793
1849
sage: X = Set(ZZ).difference(Set(QQ)); X
1794
1850
Set-theoretic difference of
1795
1851
Set of elements of Integer Ring and
1796
1852
Set of elements of Rational Field
1853
+ sage: X.category()
1854
+ Category of enumerated sets
1797
1855
sage: X._sympy_()
1798
1856
EmptySet
1799
1857
"""
@@ -1807,7 +1865,7 @@ class Set_object_symmetric_difference(Set_object_binary):
1807
1865
"""
1808
1866
Formal symmetric difference of two sets.
1809
1867
"""
1810
- def __init__ (self , X , Y ):
1868
+ def __init__ (self , X , Y , category = None ):
1811
1869
r"""
1812
1870
Initialize ``self``.
1813
1871
@@ -1817,12 +1875,20 @@ def __init__(self, X, Y):
1817
1875
sage: T = Set(ZZ)
1818
1876
sage: X = S.symmetric_difference(T); X
1819
1877
Set-theoretic symmetric difference of Set of elements of Rational Field and Set of elements of Integer Ring
1878
+ sage: X.category()
1879
+ Category of sets
1820
1880
sage: latex(X)
1821
1881
\Bold{Q} \bigtriangleup \Bold{Z}
1822
1882
1823
1883
sage: TestSuite(X).run()
1824
1884
"""
1825
- Set_object_binary .__init__ (self , X , Y , "symmetric difference" , "\\ bigtriangleup" )
1885
+ if category is None :
1886
+ category = Sets ()
1887
+ if all (S in Sets ().Finite () for S in (X , Y )):
1888
+ category = category .Finite ()
1889
+ if all (S in Sets ().Enumerated () for S in (X , Y )):
1890
+ category = category .Enumerated ()
1891
+ Set_object_binary .__init__ (self , X , Y , "symmetric difference" , "\\ bigtriangleup" , category = category )
1826
1892
1827
1893
def is_finite (self ):
1828
1894
r"""
0 commit comments