-
Notifications
You must be signed in to change notification settings - Fork 0
/
formal_powerseries.py
4126 lines (3397 loc) · 132 KB
/
formal_powerseries.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"""
Cached formal powerseries and formal Laurant series in one variable
<Paragraph description>
EXAMPLES::
<Lots and lots of examples>
AUTHORS:
- Henryk Trappmann (2022-08-23): initial version
"""
# ****************************************************************************
# Copyright (C) 2022 Henryk Trappmann <your email>
#
# 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.
# https://www.gnu.org/licenses/
# ****************************************************************************
from sage.calculus.functional import diff
from sage.functions.log import log,exp
from sage.functions.other import sqrt
from sage.matrix.constructor import matrix, identity_matrix
from sage.misc.misc_c import prod
from sage.misc.functional import n as num
from sage.functions.other import factorial
from sage.functions.other import binomial as buggybinomial
from sage.rings.integer import Integer
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
from sage.rings.polynomial.polynomial_ring import PolynomialRing_field
from sage.rings.polynomial.polynomial_element import Polynomial
from sage.rings.power_series_ring import PowerSeriesRing
from sage.rings.power_series_ring_element import PowerSeries
from sage.rings.rational_field import QQ, RationalField
from sage.rings.rational import Rational
from sage.rings.real_mpfr import RR, RealField, RealField_class
from sage.rings.real_mpfr import RealLiteral
from sage.rings.ring import Ring
from sage.structure.element import RingElement
from sage.structure.sage_object import SageObject
from sage.symbolic.expression import Expression
from sage.symbolic.ring import SymbolicRing
from sage.categories.action import Action
from operator import mul, truediv, pow
from sage.symbolic.constants import pi
from sage.rings.qqbar import QQbar
from sage.symbolic.relation import solve
from sage.calculus.var import var
HintType = PolynomialRing(QQ, 't')
def binomial(x, y):
if type(x) is RealLiteral and x == int(x):
res = buggybinomial(int(x), y)
else:
res = buggybinomial(x, y)
# if isinstance(x,FormalPowerSeries):
# res = x.parent().One
# for n in xrange(y):
# res *= x-n
# return res.lmul(x.parent().base_ring().one_element()/factorial(y))
if isinstance(x, SageObject):
X = x.parent()
XB = X.base()
if isinstance(y, SageObject):
Y = y.parent()
elif type(y) is int:
Y = Integer(1).parent()
R = res.parent()
# print X.is_ring(), not X.is_field(), XB.is_field(), XB.has_coerce_map_from(Y)
if X.is_ring() and not X.is_field() and XB.is_field() \
and XB.has_coerce_map_from(Y) and R.is_field():
# we want the result being of the same type as X
# not the FractionField that buggybinomial returns
res = X(res)
return res
def decidable0(K):
"""
Returns true if K has a decidable 0.
For example powerseries have no decidable 0.
sage: from sage.rings.formal_powerseries import FormalPowerSeriesRing,decidable0
sage: decidable0(QQ)
True
sage: decidable0(FormalPowerSeriesRing(QQ))
False
"""
if K == Integer or K == int:
return True
if K == Rational:
return True
if isinstance(K, RationalField):
return True
if isinstance(K, RealField_class):
return True
if isinstance(K, PolynomialRing_field):
return True
# if isinstance(K,RealField):
# return true
# if isinstance(K,ComplexField_class):
# return true
return False
def _isnat(n):
"""
Returns True if n is a non-negative int or Integer.
sage: from sage.rings.formal_powerseries import _isnat
sage: _isnat(5r)
True
sage: _isnat(5)
True
sage: _isnat(0)
True
sage: _isnat(-1r)
False
"""
if isinstance(n, int) or isinstance(n, Integer):
if n >= 0:
return True
return False
def _assert_nat(n):
"""
Throws an exception if not _isnat(n).
sage: from sage.rings.formal_powerseries import _assert_nat
sage: #try:
sage: # _assert_nat(-1)
sage: #except AssertionError:
sage: # print 'bang'
"""
assert _isnat(n), repr(n) + " must be natural number."
def mul_factorial(c, n):
return c * factorial(n)
def div_factorial(c, n):
return c / factorial(n)
class FormalPowerSeriesRing(Ring):
def __init__(self, base_ring, is_decidable=None):
"""
Returns the powerseries ring over base_ring.
sage: from sage.rings.formal_powerseries import FormalPowerSeriesRing
sage: FormalPowerSeriesRing(QQ)
FormalPowerSeriesRing over Rational Field
"""
Ring.__init__(self,base=base_ring)
if base_ring is None:
return
self.K = base_ring
if is_decidable is None:
self.is_decidable = decidable0(base_ring)
# self.is_decidable.__doc__ = \
# "is_one and is_zero can be decided in the base_ring"
if not self.is_decidable:
# even if the base_ring is undecidable we want predict in
# certain cases the first two elements of the powerseries
# particularly to decide whether the first item 0 is 0
# and whether item 1 is 1
self.hint_type = HintType
def PSS(seq):
""" sage: None # indirect doctest """
return self.by_list(seq)
if self.K == int:
self.K = Integer
K = self.K
if self.K == SymbolicRing:
self.K0 = Integer(0)
self.K1 = Integer(1)
else:
self.K0 = K.zero()
self.K1 = K.one()
self.Zero = Zero(self)
self.One = One(self)
self.Id = Id(self, min_index=1)
self.Inc = Inc(self)
self.Dec = Dec(self)
self.Exp = Exp(self)
self.Dec_exp = Dec_exp(self, min_index=1)
self.Log_inc = Log_inc(self, min_index=1)
self.Sin = Sin(self, min_index=1)
self.Cos = Cos(self)
self.Arcsin = Arcsin(self, min_index=1)
self.Arctan = Arctan(self, min_index=1)
self.Sinh = Sinh(self, min_index=1)
self.Cosh = Cosh(self)
self.Arcsinh = Arcsinh(self, min_index=1)
self.Arctanh = Arctanh(self, min_index=1)
self.Bernoulli = (self.Id / self.Exp.dec()).apply(mul_factorial)
self.Bernoulli.__doc__ = """
The n-th Bernoulli number is equal to
the n-th derivative of 1/(exp(x)-1) at 0.
"""
self.Tan = Tan(self)
self.Tanh = Tanh(self, min_index=1)
self.Xexp = Xexp(self, min_index=1)
self.Lambert_w = Lambert_w(self, min_index=1)
self.Sqrt_inc = Sqrt_inc(self)
# dont go into a recursion defining stirling1
if isinstance(K, FormalPowerSeriesRing):
return
self.Stirling1 = Stirling1(self)
# def lehmer_comtet(n,k): #A008296
# """ sage: None # indirect doctest """
# r = 0
# for l in range(k,n+1):
# r += binomial(l, k)*k**(l-k)*self.Stirling1[n][l]
# return K(r)
self.Lehmer_comtet = Lehmer_comtet(self)
self.A000248 = self.Lehmer_comtet
# self.selfpower_inc = PSF(lambda n: K(sum([ lehmer_comtet(n,k) for k in range(0,n+1))/factorial(n),K0))
self.Selfpower_inc = Selfpower_inc(self)
self.Superroot_inc = Superroot_inc(self)
self.A003725 = A003725(self)
self.Selfroot_inc = Selfroot_inc(self)
self.Inv_selfroot_inc = Inv_selfroot_inc(self)
# Mittag-Leffler polynomial cache
self.mlpc = {}
def is_zero(self):
return False
#return sum([abs(self[k]) for k in range(100)]) == self.K0
# class FPSAction(Action):
# def __init__(self, G, S, is_left=True, op=None):
# super().__init__(G, S, is_left=is_left, op=op)
# self.S = S
# # print("G",self.G,"is_left", self.is_left())
#
# def left_domain(self):
# if self.is_left():
# return self.S
# else:
# return self.G
#
# def right_domain(self):
# if self.is_left():
# return self.G
# else:
# return self.S
#
# def codomain(self):
# return self.S
#
# def _act_(self,g,x):
# if self.op == mul:
# if self.is_left():
# return g.rmul(x)
# else:
# return g.lmul(x)
# if self.op == truediv:
# if self.is_left():
# return g.rmul(1/x)
# else:
# return g.reciprocal().lmul(x)
# if self.op == pow and not self.is_left():
# return g.lmul(log(x)).exp()
#
# def _get_action_(self, S, op, self_on_left):
# if self.K.has_coerce_map_from(S):
# print(op,pow)
# if op == mul or op == truediv or op is pow:
# return self.FPSAction(S, self, is_left=self_on_left, op=op)
def __call__(self, p1=None, p2=None, p3=None, **kwargs):
"""
Initialization by finite sequence of coefficients:
Examples:
sage: from sage.rings.formal_powerseries import FormalPowerSeriesRing
sage: PQ = FormalPowerSeriesRing(QQ)
sage: PQ([1,2,3])
[1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
sage: PQ([])
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
Initialization by coefficient function:
Example:
sage: PQ(lambda n: 1/factorial(n))
[1, 1, 1/2, 1/6, 1/24, 1/120, 1/720, 1/5040, 1/40320, 1/362880, 1/3628800, ...]
Initialization by expresion:
Examples:
sage: PQ(1+2*x+3*x^2,x)
[1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
sage: PQ(exp(x),x)
[1, 1, 1/2, 1/6, 1/24, 1/120, 1/720, 1/5040, 1/40320, 1/362880, 1/3628800, ...]
sage: PQ(ln(x),x,1)
[0, 1, -1/2, 1/3, -1/4, 1/5, -1/6, 1/7, -1/8, 1/9, -1/10, 1/11, -1/12, ...]
Note: This is much slower than directly providing the coefficient function.
See also methods: by_const, by_undef, by_list, by_taylor, by_lambda
Some base rings of a FormalPowerSeries are decidable, some not
if you think, that the automatic detection is wrong
you can explicitely specify with is_decidable option
sage: FormalPowerSeriesRing(QQ).is_decidable
True
sage: FormalPowerSeriesRing(PolynomialRing(QQ,'x')).is_decidable
True
sage: FormalPowerSeriesRing(FormalPowerSeriesRing(QQ)).is_decidable
False
"""
if isinstance(p1, Integer) or isinstance(p1, int) or isinstance(p1, Rational):
return self.by_constant(p1)
if isinstance(p1, SageObject) and self.K.has_coerce_map_from(p1.parent()):
return self.by_constant(p1)
if isinstance(p1, list):
if p2 is None:
return self.by_list(p1, **kwargs)
return self.by_list(p1, p2)
if isinstance(p1, Expression):
if p3 is None:
return self.by_taylor(p1, p2)
return self.by_taylor(p1, p2, p3)
if isinstance(p1, Polynomial):
return self.by_polynomial(p1)
if isinstance(p1, PowerSeries):
return self.by_powerseries(p1)
# TODO generator if isinstance(p1,
if isinstance(p1, type(lambda n: 0)):
if p2 is None:
return self.by_lambda(p1, **kwargs)
return self.by_lambda(p1, p2)
if type(p1) is FormalPowerSeries:
return self.by_
if p1 is None:
return self.by_undefined(p2)
raise TypeError("unrecognized initialization input " + repr(type(p1)))
def by_lambda(self, f, min_index=0):
"""
Returns the powerseries with coefficients f(n).
Alternative expression:
self.by_lambda(f) == self(f)
self.by_lambda(f,min_index) == self(f,min_index)
sage: from sage.rings.formal_powerseries import FormalPowerSeriesRing
sage: P = FormalPowerSeriesRing(QQ)
sage: P.by_lambda(lambda n: 1/factorial(n))
[1, 1, 1/2, 1/6, 1/24, 1/120, 1/720, 1/5040, 1/40320, 1/362880, 1/3628800, ...]
Initialization as formal Laurant series:
sage: P.by_lambda(lambda n: n,-3)
[-3, -2, -1; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, ...]
Corruptly setting min_index=3
sage: P.by_lambda(lambda n: n,3)
[0, 0, 0, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, ...]
Note that functions can not be serialized/pickled.
If you want to have a serializable/picklable object, you can derive
from FormalPowerSeries and define the method coeffs.
sage: from sage.rings.formal_powerseries import FormalPowerSeries
sage: #class F(FormalPowerSeries):^J def coeffs(self,n): return n
sage: #F(P)
"""
res = FormalPowerSeries(self, f, min_index)
if min_index != 0:
return res.extinct_before(min_index)
return res
def by_iterator(self, g, min_index=0):
"""
Returns a powerseries from the generator g.
The powerseries coefficients start at min_index
which is allowed be negative obtaining a formal Laurant series.
sage: from sage.rings.formal_powerseries import FormalPowerSeriesRing
sage: P = FormalPowerSeriesRing(QQ)
sage: P.by_iterator(iter(ZZ),-2)
[0, 1; -1, 2, -2, 3, -3, 4, -4, 5, -5, 6, -6, 7, -7, 8, -8, 9, -9, 10, -10, ...]
"""
return Iterated(self, g, min_index)
def by_undefined(self, min_index=0):
"""
Returns an undefined powerseries. This is useful to use method `define'.
Alternative expressions:
self.by_undefined() == self()
self.by_undefined(m) == self(None,m)
sage: from sage.rings.formal_powerseries import FormalPowerSeriesRing
sage: P = FormalPowerSeriesRing(QQ)
sage: a = P.by_undefined()
sage: a
Undefined
sage: P.by_undefined(2).min_index
2
"""
return FormalPowerSeries(self, min_index=min_index)
def by_list(self, the_list, start=0):
"""
Returns the powerseries with coefficients p[n] where
p[n]==0 for 0<=n<start, p[m+start]==list[m] for all list indices m,
and p[n]==0 for all later indices n.
Alternative expression:
self.by_list(list) == self(list)
self.by_list(list,start) == self(list,start)
sage: from sage.rings.formal_powerseries import FormalPowerSeriesRing
sage: P = FormalPowerSeriesRing(QQ)
sage: f = P.by_list([1,2,3,4,5])
sage: f
[1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
sage: P.by_list([1,2,3],5)
[0, 0, 0, 0, 0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
"""
return List(self, the_list, start).reclass()
def by_polynomial(self, p):
"""
Returns the FormalPowerSeries from the given Polynomial.
Alternative expression: self.by_polynomial(p) == self(p)
sage: from sage.rings.formal_powerseries import FormalPowerSeriesRing
sage: PP = PolynomialRing(QQ,x)
sage: P = FormalPowerSeriesRing(QQ)
sage: pp = PP([1,2,3])
sage: P.by_polynomial(pp)
[1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
sage: x = PP(x)
sage: P(2*x+x**2)
[0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
"""
if p.parent() == self.base_ring():
return self.by_constant(p)
return self.by_list(p.padded_list())
def by_powerseries(self, p):
"""
Returns the FormalPowerSeries from the given PowerSeries.
Alternative expression: self.py_powerseries(ps)==self(ps)
sage: from sage.rings.formal_powerseries import FormalPowerSeriesRing
sage: PS = PowerSeriesRing(QQ,x)
sage: FPS = FormalPowerSeriesRing(QQ)
sage: FPS.by_powerseries(PS([0,1,2,3]))
[0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
"""
return self.by_polynomial(p.polynomial())
def by_formal_powerseries(self, p, conv=lambda x: x):
"""
Returns the FormalPowerseries converted from p by conv.
"""
return self.by_lambda(lambda n: conv(p[n]), p.min_index)
def by_taylor(self, expr, v, at=0):
"""
Returns the taylor series of `expr' with respect to `v' at `at'.
Alternative expressions:
self.by_taylor(expr,v) == self(expr,v)
self.by_taylor(expr,v,at) == self(expr,v,at)
sage: from sage.rings.formal_powerseries import FormalPowerSeriesRing
sage: P = FormalPowerSeriesRing(QQ)
sage: f = P.by_taylor(ln(x),x,1)
sage: f
[0, 1, -1/2, 1/3, -1/4, 1/5, -1/6, 1/7, -1/8, 1/9, -1/10, 1/11, -1/12, ...]
"""
# print "after",min_index
return Taylor(self, expr, v, at).reclass()
def by_constant(self, c):
"""
Returns the powerseries with coefficients [c,0,0,...].
Alternative expression: self.by_constant(c) == self(c)
sage: from sage.rings.formal_powerseries import FormalPowerSeriesRing
sage: P = FormalPowerSeriesRing(QQ)
sage: f = P.by_constant(42)
sage: f
[42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
"""
if c == self.K0:
return Zero(self)
if c == self.K1:
return One(self)
return Constant(self, c)
def is_field(self):
"""
Returns True if self is a field, i.e. if it can be used as
formal laurant series.
sage: from sage.rings.formal_powerseries import FormalPowerSeriesRing
sage: FormalPowerSeriesRing(IntegerRing()).is_field()
False
sage: FormalPowerSeriesRing(QQ).is_field()
True
"""
return self.K.is_field()
def is_commutative(self):
"""
The powerseries is commutative if the base ring is.
sage: from sage.rings.formal_powerseries import FormalPowerSeriesRing
sage: FormalPowerSeriesRing(IntegerRing()).is_commutative()
True
"""
return self.K.is_commutative()
def is_exact(self):
"""
The powerseries is exact if the base ring is.
sage: from sage.rings.formal_powerseries import FormalPowerSeriesRing
sage: FormalPowerSeriesRing(RR).is_exact()
False
sage: FormalPowerSeriesRing(QQ).is_exact()
True
"""
return self.K.is_exact()
def is_finite(self):
"""
Powerseries ring is never finite (except the base ring has only 1 element).
sage: from sage.rings.formal_powerseries import FormalPowerSeriesRing
sage: FormalPowerSeriesRing(QQ).is_finite()
False
"""
return False
def zero(self):
"""
Returns the zero element of this power series ring.
sage: from sage.rings.formal_powerseries import FormalPowerSeriesRing
sage: P = FormalPowerSeriesRing(QQ)
sage: P.zero()
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
"""
return self.Zero
def one(self):
"""
Returns the one element of this power series ring.
sage: from sage.rings.formal_powerseries import FormalPowerSeriesRing
sage: P = FormalPowerSeriesRing(QQ)
sage: P.one()
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
"""
return self.One
def _repr_(self):
"""
Description of this FormalPowerSeriesRing.
sage: from sage.rings.formal_powerseries import FormalPowerSeriesRing
sage: FormalPowerSeriesRing(QQ)._repr_()
'FormalPowerSeriesRing over Rational Field'
"""
return "FormalPowerSeriesRing over " + repr(self.K)
# def _coerce_map_from_(self, T):
# """
# Returns true if type T can be coerced to a FormalPowerSeries
# with self as parent. This can be done always when
# T can be coerced to self.base_ring().
# This is used for operations like _lmul_ and _rmul_.
#
# sage: from sage.rings.formal_powerseries import FormalPowerSeriesRing
# sage: P = FormalPowerSeriesRing(RR)
# sage: P._coerce_map_from_(QQ)
# True
# sage: P._coerce_map_from_(int)
# True
# sage: P = FormalPowerSeriesRing(QQ)
# sage: P._coerce_map_from_(int)
# True
# sage: P._coerce_map_from_(RR)
# False
# """
# # print self.K, T,not self.K.coerce_map_from(T) == None
# if self.K.coerce_map_from(T) is not None:
# return True
# if isinstance(T, FormalPowerSeriesRing):
# return self.K.coerce_map_from(T.K) is not None
# return False
def base_ring(self):
"""
Returns the base ring of the powerseries ring.
sage: from sage.rings.formal_powerseries import FormalPowerSeriesRing
sage: FormalPowerSeriesRing(QQ).base_ring() == QQ
True
"""
return self.K
def _test(self):
"""
sage: from sage.rings.formal_powerseries import FormalPowerSeriesRing
sage: P = FormalPowerSeriesRing(QQ)
sage: P.Bernoulli
[1, -1/2, 1/6, 0, -1/30, 0, 1/42, 0, -1/30, 0, 5/66, 0, -691/2730, 0, 7/6, ...]
# #takes too long:
# sage: P(exp(x),x)-P.Exp
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
# sage: P(log(x+1),x) - P.Log_inc
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
# sage: P(cos(x),x)-P.Cos
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
# sage: P(sin(x),x)-P.Sin
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
# sage: P(arcsin(x),x) - P.Arcsin
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
# sage: P(arctan(x),x) - P.Arctan
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
# sage: P(sinh(x),x)-P.Sinh
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
# sage: P(cosh(x),x)-P.Cosh
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
# sage: P(arcsinh(x),x)-P.Arcsinh
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
# sage: P(arctanh(x),x)-P.Arctanh
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
# sage: P(sqrt(x+1),x)-P.Sqrt_inc
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
# sage: P(x*exp(x),x)-P.Xexp
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
# sage: P(exp(x)-1,x)-P.Exp.dec()
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
sage: P.Exp.dec().reclass() | P.Log_inc
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
sage: P.Sin | P.Arcsin
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
sage: P.Tan | P.Arctan
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
sage: P.Tanh | P.Arctanh
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
sage: P.Xexp | P.Lambert_w
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
sage: P.Superroot_inc.dec().reclass() | P.Selfpower_inc
[1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
sage: P.Inv_selfroot_inc.dec().reclass() | P.Selfroot_inc
[1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
sage: P.Superroot_inc ** P.Superroot_inc
[1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
sage: P.Tan - P.Sin / P.Cos
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
sage: P.Sin*P.Sin + P.Cos*P.Cos
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
sage: p = P([3,2,1])
sage: p.rcp()*p
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
sage: P._test()
"""
pass
# class UndefinedFormalPowerSeries(RingElement):
# """
# Undefined powerseries.
# """
# coeffs = None
# def _repr_(a):
# return "Undefined"
class FormalPowerSeries(RingElement):
"""
Formal power series:
A powerseries p is basically seen as an infinite sequence of coefficients
The n-th coefficient is retrieved by p[n].
EXAMPLES:
sage: from sage.rings.formal_powerseries import FormalPowerSeriesRing
sage: PQ = FormalPowerSeriesRing(QQ)
sage: #Predefined PowerSeries
sage: expps = PQ.Exp
sage: expps.polynomial(10,x)
1/362880*x^9 + 1/40320*x^8 + 1/5040*x^7 + 1/720*x^6 + 1/120*x^5 + 1/24*x^4 + 1/6*x^3 + 1/2*x^2 + x + 1
sage: expps
[1, 1, 1/2, 1/6, 1/24, 1/120, 1/720, 1/5040, 1/40320, 1/362880, 1/3628800, ...]
sage: PQ.Zero
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
sage: PQ.One
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
sage: PQ.Id
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
sage: #finite powerseries
sage: p = PQ([1,2,3,4])
sage: p.polynomial(10,x)
4*x^3 + 3*x^2 + 2*x + 1
sage: p
[1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
sage: one = PQ([1])
sage: id = PQ([0,1])
sage: #power series are just functions that map the index to the coefficient
sage: expps[30]
1/265252859812191058636308480000000
Formal Laurant Series can have negative minimum index
sage: PQ(lambda n: n,-5)
[-5, -4, -3, -2, -1; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, ...]
Power series operations
sage: p+p
[2, 4, 6, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
sage: p-p
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
sage: p*p
[1, 4, 10, 20, 25, 24, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
sage: one/p
[1, -2, 1, 0, 5, -14, 13, -4, 25, -90, 121, -72, 141, -550, 965, -844, 993, ...]
sage: p.rcp()/p*p*p
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
sage: p**2
[1, 4, 10, 20, 25, 24, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
sage: #composition only works for coefficient 0 being 0 in the second operand
sage: dexp = (expps - one).reclass()
sage: expps(dexp)
[1, 1, 1, 5/6, 5/8, 13/30, 203/720, 877/5040, 23/224, 1007/17280, ...]
sage: #we come into interesting regions ...
sage: dexp(dexp)
[0, 1, 1, 5/6, 5/8, 13/30, 203/720, 877/5040, 23/224, 1007/17280, ...]
sage: dexp.nit(2)
[0, 1, 1, 5/6, 5/8, 13/30, 203/720, 877/5040, 23/224, 1007/17280, ...]
sage: dexp.it(-1)
[0, 1, -1/2, 1/3, -1/4, 1/5, -1/6, 1/7, -1/8, 1/9, -1/10, 1/11, -1/12, ...]
sage: dexp.it(-1)(dexp)
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
sage: hdexp = dexp.it(1/2)
sage: hdexp
[0, 1, 1/4, 1/48, 0, 1/3840, -7/92160, 1/645120, 53/3440640, -281/30965760, ...]
sage: #verifying that shoudl be Zero
sage: hdexp.it(2) - dexp
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
sage: #symbolic (parabolic) iteration
sage: dexp.it(x)[5].coefficients()
[[-1/180, 1], [1/24, 2], [-13/144, 3], [1/16, 4]]
sage: q = dexp.it(1/x).it(x)
sage: expand(q[3])
1/6
sage: dexp[3]
1/6
sage: #you can initialize power series with arbitrary functions on natural numbers
sage: #for example the power series of sqrt(2)^x can be given as
sage: bsrt = FormalPowerSeriesRing(SR).by_taylor(sqrt(2)^x,x)
sage: #making the 0-th coefficient 0 to get the decremented exponential
sage: dbsrt = bsrt.set_item(0,0)
sage: #and now starting hyperbolic iteration
sage: dbsrt2 = dbsrt.it(x).it(1/x)
sage: #Sage is not able to simplify
sage: #simplify(dbsrt2[3])
sage: #but numerically we can verify equality
sage: RR(dbsrt2[3](x=0.73)-dbsrt[3]) < 10**(-18)
True
"""
is0 = False
is01 = False
def __init__(self, parent, f=None, min_index=None, base_ring=None):
"""
Returns the formal powerseries.
This method should only be called from FormalPowerSeriesRing.
See the description there how to obtain a FormalPowerSeries.
sage: from sage.rings.formal_powerseries import FormalPowerSeriesRing,FormalPowerSeries
sage: FormalPowerSeries(FormalPowerSeriesRing(QQ))
Undefined
"""
if parent is None:
if base_ring is None:
parent = FormalPowerSeriesRing(QQ)
else:
parent = FormalPowerSeriesRing(base_ring)
RingElement.__init__(self, parent)
if 'coeffs' not in self.__class__.__dict__:
self.coeffs = f
if min_index is None:
min_index = 0
self.min_index = min_index # the minimal non-zero index
self._memo = {}
self._powMemo = {}
self._itMemo = {}
self.K = self.parent().K
self.K0 = self.parent().K0
self.K1 = self.parent().K1
self.min_index = min_index
if self.min_index > 0:
self._subclass(FormalPowerSeries0)
# if not f==None:
# self.reclass()
# def new(self,f=None,min_index=0,**kwargs):
# return type(self)(self.parent(),f,**kwargs)
def item_is_zero(self, n):
"""
In the case of a decidable base_ring just returns whether
self[n] == 0. In the case of non-decidable base_ring tries to
guess.
"""
if self.parent().is_decidable:
return self[n] == 0
return self.hint[n] == 0
def item_is_one(self, n):
"""
In the case of a decidable base_ring just returns whether
self[n] == 1. In the case of non-decidable base_ring tries to
guess.
"""
if self.parent().is_decidable:
return self[n] == 1
return self.hint[n] == 1
def _subclass(self, T):
"""
Makes the methods in T available to self.
sage: from sage.rings.formal_powerseries import FormalPowerSeriesRing
sage: P = FormalPowerSeriesRing(QQ)
sage: #class B:
sage: # def f(self): return 'B'
sage: #b = P.Exp._subclass(B)
sage: #b.f()
"""
if isinstance(self, T):
return self
# if issubclass(T,self.__class__):
# self.__class__ = T
# return self
#
# bs = ()
# for C in self.__class__.__bases__:
# if issubclass(T,C):
# assert not T == C
# bs += (T,)
# else:
# bs += (C,)
# self.__class__.__bases__ = bs
class Sub(T, self.__class__):
pass
self.__class__ = Sub
return self
def new(self, f=None, min_index=None, **kwargs):
"""
Returns a FormalPowerSeries from the same parent and class.
sage: from sage.rings.formal_powerseries import FormalPowerSeriesRing
sage: p = FormalPowerSeriesRing(QQ)([1,2])
sage: p.new(lambda n: n).parent() == p.parent()
True
sage: p = FormalPowerSeriesRing(QQ)([0,2])
sage: type(p.new(lambda n: n)) == type(p)
True
sage: p = FormalPowerSeriesRing(QQ)([0,1])
sage: type(p.new()) == type(p)
True
"""
res = FormalPowerSeries(self.parent(), f, min_index, **kwargs)
if min_index is None:
res.__class__ = self.__class__
if isinstance(self, FormalPowerSeries0):
res.min_index = 1
return res
# def __nonzero__(self):
# """
# Returns always None,
# as it is not decidable whether a powerseries is 0.
# sage: from sage.rings.formal_powerseries import FormalPowerSeriesRing
# sage: FormalPowerSeriesRing(QQ)(0).is_zero() == None
# """
# return None
# def is_one(self):
# """
# Returns always None,
# as it is not decidable whether a powerseries is 1.
# """
# return None
def define(self, p):
"""
Defines the power series self by another powerseries p
which is allowed to refer to self.
For example one can define exp by taking the integral of its
derivative which is again exp:
sage: from sage.rings.formal_powerseries import FormalPowerSeriesRing
sage: P = FormalPowerSeriesRing(QQ)
sage: f = P.by_undefined()
sage: f.define(integral(f,1))
sage: f
[1, 1, 1/2, 1/6, 1/24, 1/120, 1/720, 1/5040, 1/40320, 1/362880, 1/3628800, ...]
Or generally one can define g = exp o f by taking the integral of
g * f'. For example for f(x)=x**2, [0,0,1]:
sage: g = P()
sage: f = P([0,0,1])
sage: fd = f.diff()
sage: g.define(integral(g*fd,1))
sage: g - (f | P.Exp)
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]
"""
self.coeffs = p.coeffs
def __getitem__(self, n):
"""
Returns the n-th coefficient of this powerseries: self[n].
This is the coefficient of x^n.
sage: from sage.rings.formal_powerseries import FormalPowerSeriesRing
sage: P = FormalPowerSeriesRing(QQ)
sage: P([1,2,3])[1] == 2
True
"""
if isinstance(n, slice):
return [self[ii] for ii in range(0 if n.start is None else n.start, n.stop, 1 if n.step is None else n.step)]
# return self.__getslice__(slice.start,slice.stop)
if n not in self._memo: