This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
subring.py
1016 lines (736 loc) · 29 KB
/
subring.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
r"""
Subrings of the Symbolic Ring
TESTS:
In the following we have a couple of tests to see whether the coercion
framework works properly::
sage: from sage.symbolic.subring import SymbolicSubring
sage: V = var('a, r, x')
sage: A = SymbolicSubring(accepting_variables=(a,)); A
Symbolic Subring accepting the variable a
sage: R = SymbolicSubring(rejecting_variables=(r,)); R
Symbolic Subring rejecting the variable r
sage: C = SymbolicSubring(only_constants=True); C
Symbolic Constants Subring
::
sage: sage.categories.pushout.pushout(A, R)
Symbolic Subring rejecting the variable r
sage: sage.categories.pushout.pushout(R, C)
Symbolic Subring rejecting the variable r
sage: sage.categories.pushout.pushout(C, A)
Symbolic Subring accepting the variable a
sage: sage.categories.pushout.pushout(A, SR)
Symbolic Ring
sage: sage.categories.pushout.pushout(R, SR)
Symbolic Ring
sage: sage.categories.pushout.pushout(C, SR)
Symbolic Ring
::
sage: cm = sage.structure.element.get_coercion_model()
sage: cm.common_parent(A, R)
Symbolic Subring rejecting the variable r
sage: cm.common_parent(R, C)
Symbolic Subring rejecting the variable r
sage: cm.common_parent(C, A)
Symbolic Subring accepting the variable a
sage: cm.common_parent(A, SR)
Symbolic Ring
sage: cm.common_parent(R, SR)
Symbolic Ring
sage: cm.common_parent(C, SR)
Symbolic Ring
AUTHORS:
- Daniel Krenn (2015)
Classes and Methods
===================
"""
#*****************************************************************************
# Copyright (C) 2015 Daniel Krenn <dev@danielkrenn.at>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
# http://www.gnu.org/licenses/
#*****************************************************************************
from ring import SymbolicRing, SR
from sage.structure.factory import UniqueFactory
class SymbolicSubringFactory(UniqueFactory):
r"""
A factory creating a symbolic subring.
INPUT:
Choose one of the following keywords to create a subring.
- ``accepting_variables`` (default: ``None``) -- a tuple or other
iterable of variables. If specified, then a symbolic subring of
expressions in only these variables is created.
- ``rejecting_variables`` (default: ``None``) -- a tuple or other
iterable of variables. If specified, then a symbolic subring of
expressions in variables distinct to these variables is
created.
- ``only_constants`` (default: ``False``) -- a boolean. If set,
then a symbolic subring of constant expressions (i.e.,
expressions without a variable) is created.
EXAMPLES::
sage: from sage.symbolic.subring import SymbolicSubring
sage: V = var('a, b, c, r, s, t, x, y, z')
::
sage: A = SymbolicSubring(accepting_variables=(a, b, c)); A
Symbolic Subring accepting the variables a, b, c
sage: tuple((v, v in A) for v in V)
((a, True), (b, True), (c, True),
(r, False), (s, False), (t, False),
(x, False), (y, False), (z, False))
::
sage: R = SymbolicSubring(rejecting_variables=(r, s, t)); R
Symbolic Subring rejecting the variables r, s, t
sage: tuple((v, v in R) for v in V)
((a, True), (b, True), (c, True),
(r, False), (s, False), (t, False),
(x, True), (y, True), (z, True))
::
sage: C = SymbolicSubring(only_constants=True); C
Symbolic Constants Subring
sage: tuple((v, v in C) for v in V)
((a, False), (b, False), (c, False),
(r, False), (s, False), (t, False),
(x, False), (y, False), (z, False))
TESTS::
sage: SymbolicSubring(accepting_variables=tuple()) is C
True
::
sage: SymbolicSubring(rejecting_variables=tuple()) is SR
True
"""
def create_key_and_extra_args(
self, accepting_variables=None, rejecting_variables=None,
only_constants=False, **kwds):
r"""
Given the arguments and keyword, create a key that uniquely
determines this object.
See :class:`SymbolicSubringFactory` for details.
TESTS::
sage: from sage.symbolic.subring import SymbolicSubring
sage: SymbolicSubring.create_key_and_extra_args()
Traceback (most recent call last):
...
ValueError: Cannot create a symbolic subring since nothing specified.
sage: SymbolicSubring.create_key_and_extra_args(
....: accepting_variables=('a',), rejecting_variables=('r',))
Traceback (most recent call last):
...
ValueError: Cannot create a symbolic subring since input is ambiguous.
sage: SymbolicSubring.create_key_and_extra_args(
....: accepting_variables=('a',), only_constants=True)
Traceback (most recent call last):
...
ValueError: Cannot create a symbolic subring since input is ambiguous.
sage: SymbolicSubring.create_key_and_extra_args(
....: rejecting_variables=('r',), only_constants=True)
Traceback (most recent call last):
...
ValueError: Cannot create a symbolic subring since input is ambiguous.
"""
if accepting_variables is None and \
rejecting_variables is None and \
only_constants == False:
raise ValueError('Cannot create a symbolic subring '
'since nothing specified.')
if accepting_variables is not None and \
rejecting_variables is not None or \
rejecting_variables is not None and \
only_constants == True or \
only_constants == True and \
accepting_variables is not None:
raise ValueError('Cannot create a symbolic subring '
'since input is ambiguous.')
if accepting_variables is not None:
vars = tuple(accepting_variables)
if vars:
cls = SymbolicSubringAcceptingVars
else:
cls = SymbolicConstantsSubring
elif rejecting_variables is not None:
vars = tuple(rejecting_variables)
cls = SymbolicSubringRejectingVars
elif only_constants:
vars = tuple()
cls = SymbolicConstantsSubring
vars = tuple(sorted(iter(SR(v) for v in vars), key=str))
return (cls, vars), kwds
def create_object(self, version, key, **kwds):
r"""
Create an object from the given arguments.
See :class:`SymbolicSubringFactory` for details.
TESTS::
sage: from sage.symbolic.subring import SymbolicSubring
sage: SymbolicSubring(rejecting_variables=tuple()) is SR # indirect doctest
True
"""
cls, vars = key
if cls is SymbolicSubringRejectingVars and not vars:
return SR
return cls(vars, **kwds)
SymbolicSubring = SymbolicSubringFactory("SymbolicSubring")
class GenericSymbolicSubring(SymbolicRing):
def __init__(self, vars):
r"""
An abstract base class for a symbolic subring.
INPUT:
- ``vars`` -- a tuple of symbolic variables.
TESTS::
sage: from sage.symbolic.subring import SymbolicSubring
sage: SymbolicSubring(accepting_variables=('a',)) # indirect doctest
Symbolic Subring accepting the variable a
sage: SymbolicSubring(rejecting_variables=('r',)) # indirect doctest
Symbolic Subring rejecting the variable r
sage: SymbolicSubring(only_constants=True) # indirect doctest
Symbolic Constants Subring
sage: SymbolicSubring(rejecting_variables=tuple()) # indirect doctest
Symbolic Ring
"""
super(GenericSymbolicSubring, self).__init__()
self._vars_ = set(vars)
def _repr_variables_(self):
r"""
Return a representation string of the variables.
OUTPUT:
A string.
TESTS::
sage: from sage.symbolic.subring import SymbolicSubring
sage: SymbolicSubring(accepting_variables=tuple())._repr_variables_()
'no variable'
sage: SymbolicSubring(accepting_variables=('a',))._repr_variables_()
'the variable a'
sage: SymbolicSubring(accepting_variables=('a', 'b'))._repr_variables_()
'the variables a, b'
"""
if not self._vars_:
s = 'no variable'
elif len(self._vars_) == 1:
s = 'the variable '
else:
s = 'the variables '
return s + ', '.join(str(v) for v in sorted(self._vars_, key=str))
def is_variable_valid(self, variable):
r"""
Return whether the given ``variable`` is valid in this subring.
INPUT:
- ``variable`` -- a symbolic variable.
OUTPUT:
A boolean.
EXAMPLES::
sage: from sage.symbolic.subring import GenericSymbolicSubring
sage: GenericSymbolicSubring(vars=tuple()).is_variable_valid(x)
Traceback (most recent call last):
...
NotImplementedError: Not implemented in this abstract base class
"""
raise NotImplementedError('Not implemented in this abstract base class')
def _element_constructor_(self, x):
r"""
Creates the element of this subring specified by the input ``x``.
INPUT:
- ``x`` -- an object.
OUTPUT:
An element of this symbolic subring.
TESTS::
sage: from sage.symbolic.subring import SymbolicSubring
sage: S = SymbolicSubring(accepting_variables=('a',))
sage: S('a') # indirect doctest
a
sage: S('x') # indirect doctest
Traceback (most recent call last):
...
TypeError: x is not contained in Symbolic Subring accepting the variable a
"""
expression = SR(x)
if not all(self.is_variable_valid(var)
for var in expression.variables()):
raise TypeError('%s is not contained in %s' % (x, self))
return expression
def _coerce_map_from_(self, P):
r"""
Return whether ``P`` coerces into this symbolic subring.
INPUT:
- ``P`` -- a parent.
OUTPUT:
A boolean or ``None``.
TESTS::
sage: from sage.symbolic.subring import GenericSymbolicSubring
sage: GenericSymbolicSubring(vars=tuple()).has_coerce_map_from(SR) # indirect doctest # not tested see #19231
False
::
sage: from sage.symbolic.subring import SymbolicSubring
sage: C = SymbolicSubring(only_constants=True)
sage: C.has_coerce_map_from(ZZ) # indirect doctest
True
sage: C.has_coerce_map_from(QQ) # indirect doctest
True
sage: C.has_coerce_map_from(RR) # indirect doctest
True
sage: C.has_coerce_map_from(RIF) # indirect doctest
True
sage: C.has_coerce_map_from(CC) # indirect doctest
True
sage: C.has_coerce_map_from(CIF) # indirect doctest
True
sage: C.has_coerce_map_from(AA) # indirect doctest
True
sage: C.has_coerce_map_from(QQbar) # indirect doctest
True
sage: C.has_coerce_map_from(SR) # indirect doctest
False
"""
if P == SR:
# Workaround; can be deleted once #19231 is fixed
return False
from sage.rings.real_mpfr import mpfr_prec_min
from sage.rings.all import (ComplexField,
RLF, CLF, AA, QQbar, InfinityRing)
from sage.rings.real_mpfi import is_RealIntervalField
from sage.rings.complex_interval_field import is_ComplexIntervalField
if isinstance(P, type):
return SR._coerce_map_from_(P)
elif RLF.has_coerce_map_from(P) or \
CLF.has_coerce_map_from(P) or \
AA.has_coerce_map_from(P) or \
QQbar.has_coerce_map_from(P):
return True
elif (P is InfinityRing
or is_RealIntervalField(P) or is_ComplexIntervalField(P)):
return True
elif ComplexField(mpfr_prec_min()).has_coerce_map_from(P):
return P not in (RLF, CLF, AA, QQbar)
def __cmp__(self, other):
"""
Compare two symbolic subrings.
EXAMPLES::
sage: from sage.symbolic.subring import SymbolicSubring
sage: A = SymbolicSubring(accepting_variables=('a',))
sage: B = SymbolicSubring(accepting_variables=('b',))
sage: AB = SymbolicSubring(accepting_variables=('a', 'b'))
sage: A == A
True
sage: A == B
False
sage: A == AB
False
"""
c = cmp(type(self), type(other))
if c != 0:
return c
if self._vars_ == other._vars_:
return 0
return 1
from sage.categories.pushout import ConstructionFunctor
class GenericSymbolicSubringFunctor(ConstructionFunctor):
r"""
A base class for the functors constructing symbolic subrings.
INPUT:
- ``vars`` -- a tuple, set, or other iterable of symbolic variables.
EXAMPLES::
sage: from sage.symbolic.subring import SymbolicSubring
sage: SymbolicSubring(only_constants=True).construction()[0] # indirect doctest
Subring<accepting no variable>
.. SEEALSO::
:class:`sage.categories.pushout.ConstructionFunctor`.
"""
_functor_name = 'GenericSymbolicSubringFunctor'
rank = 11
# The symbolic subring construction returns an object admitting a
# coercion map into the original, not vice versa.
coercion_reversed = True
_repr_type_ = 'generic'
def __init__(self, vars):
r"""
See :class:`GenericSymbolicSubringFunctor` for details.
TESTS::
sage: from sage.symbolic.subring import SymbolicSubring
sage: SymbolicSubring(accepting_variables=('a',)).construction()[0] # indirect doctest
Subring<accepting a>
"""
self.vars = set(vars)
from sage.categories.rings import Rings
super(ConstructionFunctor, self).__init__(Rings(), Rings())
def _repr_variables_(self):
r"""
Return a representation string of the variables.
OUTPUT:
A string.
TESTS::
sage: from sage.symbolic.subring import SymbolicSubring
sage: F = SymbolicSubring(accepting_variables=('a',)).construction()[0]
sage: F._repr_variables_()
'a'
"""
return ', '.join(str(v) for v in sorted(self.vars, key=str))
def _repr_(self):
r"""
Return a representation string of this functor.
OUTPUT:
A string.
TESTS::
sage: from sage.symbolic.subring import SymbolicSubring
sage: SymbolicSubring(accepting_variables=('a',)) # indirect doctest
Symbolic Subring accepting the variable a
sage: SymbolicSubring(rejecting_variables=('r',)) # indirect doctest
Symbolic Subring rejecting the variable r
sage: SymbolicSubring(only_constants=True) # indirect doctest
Symbolic Constants Subring
"""
return 'Subring<%s%s%s>' % (
self._repr_type_, ' ' if self._repr_type_ else '',
self._repr_variables_() if self.vars else 'no variable')
def merge(self, other):
r"""
Merge this functor with ``other`` if possible.
INPUT:
- ``other`` -- a functor.
OUTPUT:
A functor or ``None``.
EXAMPLES::
sage: from sage.symbolic.subring import SymbolicSubring
sage: F = SymbolicSubring(accepting_variables=('a',)).construction()[0]
sage: F.merge(F) is F
True
"""
if self == other:
return self
def __eq__(self, other):
r"""
Return if this functor is equal to ``other``.
INPUT:
- ``other`` -- a functor.
OUTPUT:
A boolean.
EXAMPLES::
sage: from sage.symbolic.subring import SymbolicSubring
sage: F = SymbolicSubring(accepting_variables=('a',)).construction()[0]
sage: F == F
True
"""
return type(self) == type(other) and self.vars == other.vars
def __ne__(self, other):
r"""
Return if this functor is not equal to ``other``.
INPUT:
- ``other`` -- a functor.
OUTPUT:
A boolean.
EXAMPLES::
sage: from sage.symbolic.subring import SymbolicSubring
sage: F = SymbolicSubring(accepting_variables=('a',)).construction()[0]
sage: F != F
False
"""
return not self == other
class SymbolicSubringAcceptingVars(GenericSymbolicSubring):
r"""
The symbolic subring consisting of symbolic expressions in the given variables.
"""
def _repr_(self):
r"""
Return a representation string of this symbolic subring.
OUTPUT:
A string.
TESTS::
sage: from sage.symbolic.subring import SymbolicSubring
sage: SymbolicSubring(accepting_variables=('a',)) # indirect doctest
Symbolic Subring accepting the variable a
"""
return 'Symbolic Subring accepting %s' % \
(self._repr_variables_())
def is_variable_valid(self, variable):
r"""
Return whether the given ``variable`` is valid in this subring.
INPUT:
- ``variable`` -- a symbolic variable.
OUTPUT:
A boolean.
EXAMPLES::
sage: from sage.symbolic.subring import SymbolicSubring
sage: S = SymbolicSubring(accepting_variables=('a',))
sage: S.is_variable_valid('a')
True
sage: S.is_variable_valid('r')
False
sage: S.is_variable_valid('x')
False
"""
return SR(variable) in self._vars_
def construction(self):
r"""
Return the functorial construction of this symbolic subring.
OUTPUT:
A tuple whose first entry is a construction functor and its second
is the symbolic ring.
EXAMPLES::
sage: from sage.symbolic.subring import SymbolicSubring
sage: SymbolicSubring(accepting_variables=('a',)).construction()
(Subring<accepting a>, Symbolic Ring)
"""
return (SymbolicSubringAcceptingVarsFunctor(self._vars_), SR)
def _coerce_map_from_(self, P):
r"""
Return whether ``P`` coerces into this symbolic subring.
INPUT:
- ``P`` -- a parent.
OUTPUT:
A boolean or ``None``.
TESTS::
sage: from sage.symbolic.subring import SymbolicSubring
sage: A = SymbolicSubring(accepting_variables=('a',))
sage: AB = SymbolicSubring(accepting_variables=('a', 'b'))
sage: A.has_coerce_map_from(AB) # indirect doctest
False
sage: AB.has_coerce_map_from(A) # indirect doctest
True
"""
if isinstance(P, SymbolicSubringAcceptingVars):
return self._vars_ >= P._vars_
return super(SymbolicSubringAcceptingVars, self)._coerce_map_from_(P)
def _an_element_(self):
r"""
Return an element of this symbolic subring.
OUTPUT:
A symbolic expression.
TESTS::
sage: from sage.symbolic.subring import SymbolicSubring
sage: SymbolicSubring(accepting_variables=('a',)).an_element()
a
"""
return sorted(self._vars_, key=str)[0]
class SymbolicSubringAcceptingVarsFunctor(GenericSymbolicSubringFunctor):
_functor_name = 'SymbolicSubringAcceptingVarsFunctor'
_repr_type_ = 'accepting'
def merge(self, other):
r"""
Merge this functor with ``other`` if possible.
INPUT:
- ``other`` -- a functor.
OUTPUT:
A functor or ``None``.
EXAMPLES::
sage: from sage.symbolic.subring import SymbolicSubring
sage: F = SymbolicSubring(accepting_variables=('a',)).construction()[0]
sage: G = SymbolicSubring(rejecting_variables=('r',)).construction()[0]
sage: F.merge(F) is F
True
sage: F.merge(G) is G
True
"""
if self == other:
return self
elif type(self) == type(other):
return type(self)(self.vars | other.vars)
elif isinstance(other, SymbolicSubringRejectingVarsFunctor):
if not (self.vars & other.vars):
return other
def _apply_functor(self, R):
"""
Apply this functor to the given symbolic ring `R`.
INPUT:
- ``R`` -- a symbolic ring.
OUTPUT:
A subring of ``R``.
EXAMPLES::
sage: from sage.symbolic.subring import SymbolicSubring
sage: F, R = SymbolicSubring(accepting_variables=('a',)).construction()
sage: F(R) # indirect doctest
Symbolic Subring accepting the variable a
TESTS::
sage: F(F(R))
Traceback (most recent call last):
...
NotImplementedError: This functor can only be applied on the
symbolic ring but Symbolic Subring accepting the variable a given.
"""
if R is not SR:
raise NotImplementedError('This functor can only be applied on '
'the symbolic ring but %s given.' % (R,))
return SymbolicSubring(accepting_variables=self.vars)
class SymbolicSubringRejectingVars(GenericSymbolicSubring):
r"""
The symbolic subring consisting of symbolic expressions whose variables
are none of the given variables.
"""
def _repr_(self):
r"""
Return a representation string of this symbolic subring.
OUTPUT:
A string.
TESTS::
sage: from sage.symbolic.subring import SymbolicSubring
sage: SymbolicSubring(rejecting_variables=('r',)) # indirect doctest
Symbolic Subring rejecting the variable r
"""
return 'Symbolic Subring rejecting %s' % \
(self._repr_variables_())
def is_variable_valid(self, variable):
r"""
Return whether the given ``variable`` is valid in this subring.
INPUT:
- ``variable`` -- a symbolic variable.
OUTPUT:
A boolean.
EXAMPLES::
sage: from sage.symbolic.subring import SymbolicSubring
sage: S = SymbolicSubring(rejecting_variables=('r',))
sage: S.is_variable_valid('a')
True
sage: S.is_variable_valid('r')
False
sage: S.is_variable_valid('x')
True
"""
return SR(variable) not in self._vars_
def construction(self):
r"""
Return the functorial construction of this symbolic subring.
OUTPUT:
A tuple whose first entry is a construction functor and its second
is the symbolic ring.
EXAMPLES::
sage: from sage.symbolic.subring import SymbolicSubring
sage: SymbolicSubring(rejecting_variables=('r',)).construction()
(Subring<rejecting r>, Symbolic Ring)
"""
return (SymbolicSubringRejectingVarsFunctor(self._vars_), SR)
def _coerce_map_from_(self, P):
r"""
Return whether ``P`` coerces into this symbolic subring.
INPUT:
- ``P`` -- a parent.
OUTPUT:
A boolean or ``None``.
TESTS::
sage: from sage.symbolic.subring import SymbolicSubring
sage: R = SymbolicSubring(rejecting_variables=('r',))
sage: RS = SymbolicSubring(rejecting_variables=('r', 's'))
sage: RS.has_coerce_map_from(R) # indirect doctest
False
sage: R.has_coerce_map_from(RS) # indirect doctest
True
sage: A = SymbolicSubring(accepting_variables=('a',))
sage: R.has_coerce_map_from(A)
True
sage: A.has_coerce_map_from(R)
False
"""
if isinstance(P, SymbolicSubringRejectingVars):
return self._vars_ <= P._vars_
elif isinstance(P, SymbolicSubringAcceptingVars):
return not (self._vars_ & P._vars_)
return super(SymbolicSubringRejectingVars, self)._coerce_map_from_(P)
def _an_element_(self):
r"""
Return an element of this symbolic subring.
OUTPUT:
A symbolic expression.
TESTS::
sage: from sage.symbolic.subring import SymbolicSubring
sage: SymbolicSubring(rejecting_variables=('r',)).an_element()
some_variable
sage: SymbolicSubring(rejecting_variables=('some_variable',)).an_element()
some_some_variable
sage: SymbolicSubring(rejecting_variables=('some_some_variable',)).an_element()
some_variable
sage: SymbolicSubring(rejecting_variables=('some_variable','some_some_variable')).an_element()
some_some_some_variable
"""
v = SR.an_element()
while not self.is_variable_valid(v):
v = SR('some_' + str(v))
return v
class SymbolicSubringRejectingVarsFunctor(GenericSymbolicSubringFunctor):
_functor_name = 'SymbolicSubringRejectingVarsFunctor'
_repr_type_ = 'rejecting'
def merge(self, other):
r"""
Merge this functor with ``other`` if possible.
INPUT:
- ``other`` -- a functor.
OUTPUT:
A functor or ``None``.
EXAMPLES::
sage: from sage.symbolic.subring import SymbolicSubring
sage: F = SymbolicSubring(accepting_variables=('a',)).construction()[0]
sage: G = SymbolicSubring(rejecting_variables=('r',)).construction()[0]
sage: G.merge(G) is G
True
sage: G.merge(F) is G
True
"""
if self == other:
return self
elif type(self) == type(other):
return type(self)(self.vars & other.vars)
elif isinstance(other, SymbolicSubringAcceptingVarsFunctor):
if not (self.vars & other.vars):
return self
def _apply_functor(self, R):
"""
Apply this functor to the given symbolic ring `R`.
INPUT:
- ``R`` -- a symbolic ring.
OUTPUT:
A subring of ``R``.
EXAMPLES::
sage: from sage.symbolic.subring import SymbolicSubring
sage: F, R = SymbolicSubring(rejecting_variables=('r',)).construction()
sage: F(R) # indirect doctest
Symbolic Subring rejecting the variable r
TESTS::
sage: F(F(R))
Traceback (most recent call last):
...
NotImplementedError: This functor can only be applied on the
symbolic ring but Symbolic Subring rejecting the variable r given.
"""
if R is not SR:
raise NotImplementedError('This functor can only be applied on '
'the symbolic ring but %s given.' % (R,))
return SymbolicSubring(rejecting_variables=self.vars)
class SymbolicConstantsSubring(SymbolicSubringAcceptingVars):
r"""
The symbolic subring consisting of symbolic constants.
"""
def _repr_(self):
r"""
Return a representation string of this symbolic subring.
OUTPUT:
A string.
TESTS::
sage: from sage.symbolic.subring import SymbolicSubring
sage: SymbolicSubring(only_constants=True) # indirect doctest
Symbolic Constants Subring
"""
return 'Symbolic Constants Subring'
def is_variable_valid(self, variable):
r"""
Return whether the given ``variable`` is valid in this subring.
INPUT:
- ``variable`` -- a symbolic variable.
OUTPUT:
A boolean.
EXAMPLES::
sage: from sage.symbolic.subring import SymbolicSubring
sage: S = SymbolicSubring(only_constants=True)
sage: S.is_variable_valid('a')
False
sage: S.is_variable_valid('r')
False
sage: S.is_variable_valid('x')
False
"""
return False