forked from fricas/fricas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs2ups.spad
1628 lines (1493 loc) · 70.3 KB
/
fs2ups.spad
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
)abbrev package FS2UPS2 FunctionSpaceToUnivariatePowerSeries2
++ Author: Clifton J. Williamson
++ Date Created: 21 March 1989
++ Basic Operations:
++ Related Domains:
++ Also See:
++ AMS Classifications:
++ Keywords: elementary function, power series
++ Examples:
++ References:
++ Description:
++ This package converts expressions in some function space to power
++ series in a variable x with coefficients in that function space.
++ The function \spadfun{exprToUPS} converts expressions to power series
++ whose coefficients do not contain the variable x. The function
++ \spadfun{exprToGenUPS} converts functional expressions to power series
++ whose coefficients may involve functions of \spad{log(x)}.
FunctionSpaceToUnivariatePowerSeries2(R, FE, Expon, UPS, TRAN, UTS, TEXP,
coerce_Ex : Expon -> FE, x) : _
Exports == Implementation where
R : Join(GcdDomain, Comparable, RetractableTo Integer,
LinearlyExplicitOver Integer)
FE : Join(AlgebraicallyClosedField, TranscendentalFunctionCategory,
Algebra(Fraction(Integer)), FunctionSpace R)
Expon : Join(OrderedAbelianMonoid, OrderedRing)
UPS : Join(UnivariatePowerSeriesCategory(FE, Expon), Field,
PowerSeriesCategory(FE, Expon, SingletonAsOrderedSet),
TranscendentalFunctionCategory)
TRAN : PartialTranscendentalFunctions UPS
UTS : UnivariateTaylorSeriesCategory(FE)
TEXP : TaylorSeriesExpansion(FE, Expon, SingletonAsOrderedSet, UPS, UTS)
x : Symbol
B ==> Boolean
BOP ==> BasicOperator
I ==> Integer
NNI ==> NonNegativeInteger
K ==> Kernel FE
L ==> List
RN ==> Fraction Integer
ATANFLAG ==> Union("complex", _
"real: two sides", _
"real: left side", _
"real: right side", _
"just do it")
PROBLEM ==> String
FUNCTIONNAME ==> String
SY ==> Symbol
PCL ==> PolynomialCategoryLifting(IndexedExponents K, K, R, SMP, FE)
POL ==> Polynomial R
SMP ==> SparseMultivariatePolynomial(R, K)
SUP ==> SparseUnivariatePolynomial Polynomial R
Problem ==> Record(func : String, prob : String)
Result ==> Union(%series : UPS, %problem : Problem)
SIGNEF ==> ElementaryFunctionSign(R, FE)
U1 ==> Union("invertible", "zero", "bad")
OPT_MAP ==> Union(FE -> Boolean, "none")
Cache_REC ==> Record(ker : K, ser : Result)
OPT_REC ==> Record(pos_Check? : Boolean, atan_Flag : ATANFLAG,
coeff_check? : OPT_MAP, inv_check? : OPT_MAP,
zero_check? : OPT_MAP, log_x_replace : Boolean,
log_x_val : FE, cache : List(Cache_REC))
Exports ==> with
exprToUPS : (FE, B, ATANFLAG) -> Result
++ exprToUPS(fcn, posCheck?, atanFlag) converts the expression
++ \spad{fcn} to a power series. If \spad{posCheck?} is true,
++ log's of negative numbers are not allowed nor are nth roots of
++ negative numbers with n even. If \spad{posCheck?} is false,
++ these are allowed. \spad{atanFlag} determines how the case
++ \spad{atan(f(x))}, where \spad{f(x)} has a pole, will be treated.
++ The possible values of \spad{atanFlag} are \spad{"complex"},
++ \spad{"real: two sides"}, \spad{"real: left side"},
++ \spad{"real: right side"}, and \spad{"just do it"}.
++ If \spad{atanFlag} is \spad{"complex"}, then no series expansion
++ will be computed because, viewed as a function of a complex
++ variable, \spad{atan(f(x))} has an essential singularity.
++ Otherwise, the sign of the leading coefficient of the series
++ expansion of \spad{f(x)} determines the constant coefficient
++ in the series expansion of \spad{atan(f(x))}. If this sign cannot
++ be determined, a series expansion is computed only when
++ \spad{atanFlag} is \spad{"just do it"}. When the leading term
++ in the series expansion of \spad{f(x)} is of odd degree (or is a
++ rational degree with odd numerator), then the constant coefficient
++ in the series expansion of \spad{atan(f(x))} for values to the
++ left differs from that for values to the right. If \spad{atanFlag}
++ is \spad{"real: two sides"}, no series expansion will be computed.
++ If \spad{atanFlag} is \spad{"real: left side"} the constant
++ coefficient for values to the left will be used and if \spad{atanFlag}
++ \spad{"real: right side"} the constant coefficient for values to the
++ right will be used.
++ If there is a problem in converting the function to a power series,
++ a record containing the name of the function that caused the problem
++ and a brief description of the problem is returned.
++ When expanding the expression into a series it is assumed that
++ the series is centered at 0. For a series centered at a, the
++ user should perform the substitution \spad{x -> x + a} before calling
++ this function.
exprToGenUPS : (FE, B, ATANFLAG) -> Result
++ exprToGenUPS(fcn, posCheck?, atanFlag) converts the expression
++ \spad{fcn} to a generalized power series. If \spad{posCheck?}
++ is true, log's of negative numbers are not allowed nor are nth roots
++ of negative numbers with n even. If \spad{posCheck?} is false,
++ these are allowed. \spad{atanFlag} determines how the case
++ \spad{atan(f(x))}, where \spad{f(x)} has a pole, will be treated.
++ The possible values of \spad{atanFlag} are \spad{"complex"},
++ \spad{"real: two sides"}, \spad{"real: left side"},
++ \spad{"real: right side"}, and \spad{"just do it"}.
++ If \spad{atanFlag} is \spad{"complex"}, then no series expansion
++ will be computed because, viewed as a function of a complex
++ variable, \spad{atan(f(x))} has an essential singularity.
++ Otherwise, the sign of the leading coefficient of the series
++ expansion of \spad{f(x)} determines the constant coefficient
++ in the series expansion of \spad{atan(f(x))}. If this sign cannot
++ be determined, a series expansion is computed only when
++ \spad{atanFlag} is \spad{"just do it"}. When the leading term
++ in the series expansion of \spad{f(x)} is of odd degree (or is a
++ rational degree with odd numerator), then the constant coefficient
++ in the series expansion of \spad{atan(f(x))} for values to the
++ left differs from that for values to the right. If \spad{atanFlag}
++ is \spad{"real: two sides"}, no series expansion will be computed.
++ If \spad{atanFlag} is \spad{"real: left side"} the constant
++ coefficient for values to the left will be used and if \spad{atanFlag}
++ \spad{"real: right side"} the constant coefficient for values to the
++ right will be used.
++ If there is a problem in converting the function to a power
++ series, we return a record containing the name of the function
++ that caused the problem and a brief description of the problem.
++ When expanding the expression into a series it is assumed that
++ the series is centered at 0. For a series centered at a, the
++ user should perform the substitution \spad{x -> x + a} before calling
++ this function.
exprToPS : (FE, B, ATANFLAG, B, FE) -> Result
++ exprToPS(fcn, posCheck?, atanFlag, log_flag, log_val) is
++ like exprToUPS, but if \spad{log_flag} is true it replaces
++ logarithms of expansion variable by \spad{log_val}
exprToPS : (FE, B, ATANFLAG, B, FE, FE -> B, FE -> B, FE -> B) -> Result
++ exprToPS(fcn, posCheck?, atanFlag, log_flag, log_val, coef_chk,
++ inv_chk, zero_chk) is like exprToPS(fcn, posCheck?, atanFlag,
++ log_flag, log_val), but fails if \spad{coef_chk} is false for some
++ coefficient of \spad{fcn} or if expansion process needs to
++ invert element of \spad{FE} for which \spad{inv_chk} is
++ false
localAbs : FE -> FE
++ localAbs(fcn) = \spad{abs(fcn)} or \spad{sqrt(fcn^2)} depending
++ on whether or not FE has a function \spad{abs}. This should be
++ a local function, but the compiler won't allow it.
atan1 : UPS -> UPS
++ atan1 should be local but conditional
integ : UPS -> UPS
++ integ should be local but conditional
integt : UTS -> UTS
++ integt should be local but conditional
integ_df : (UPS, UPS) -> UPS
++ integ_df should be local but conditional
powToUPS : (List(FE), OPT_REC) -> Result
++ powToUPS should be local but conditional
my_digamma : FE -> FE
++ my_digamma should be local but conditional
Implementation ==> add
ratIfCan : FE -> Union(RN,"failed")
carefulNthRootIfCan : (UPS, NNI, OPT_REC, B) -> Result
stateProblem : (PROBLEM, PROBLEM) -> Result
polyToUPS : (SUP, OPT_REC) -> Result
listToUPS : (L FE, (FE, OPT_REC) -> Result, OPT_REC,
UPS, (UPS, UPS) -> UPS) -> Result
isNonTrivPower : FE -> Union(Record(val:FE,exponent:I),"failed")
i_expr_to_PS : (FE, OPT_REC) -> Result
powerToUPS : (FE, I, OPT_REC) -> Result
kernelToUPS : (K, OPT_REC) -> Result
nthRootToUPS : (FE, NNI, OPT_REC) -> Result
logToUPS : (FE, OPT_REC) -> Result
atancotToUPS : (FE, OPT_REC, I) -> Result
applyIfCan : (UPS -> Union(UPS,"failed"), FE, FUNCTIONNAME,
OPT_REC) -> Result
tranToUPS : (K, FE, OPT_REC) -> Result
newElem : FE -> FE
smpElem : SMP -> FE
k2Elem : K -> FE
contOnReals? : FUNCTIONNAME -> B
bddOnReals? : FUNCTIONNAME -> B
iExprToGenUPS : (FE, OPT_REC) -> Result
opsInvolvingX : FE -> L BOP
opInOpList? : (SY, L BOP) -> B
exponential? : FE -> B
productOfNonZeroes? : FE -> B
powerToGenUPS : (FE, I, OPT_REC) -> Result
kernelToGenUPS : (K, OPT_REC) -> Result
nthRootToGenUPS : (FE, NNI, OPT_REC) -> Result
logToGenUPS : (FE, OPT_REC) -> Result
expToGenUPS : (FE, OPT_REC) -> Result
expGenUPS : (UPS, OPT_REC) -> Result
atancotToGenUPS : (FE, FE, OPT_REC, I) -> Result
genUPSApplyIfCan : (UPS -> Union(UPS,"failed"), FE, FUNCTIONNAME,
OPT_REC) -> Result
applyBddIfCan : (FE, UPS -> Union(UPS,"failed"), FE, FUNCTIONNAME,
OPT_REC) -> Result
tranToGenUPS : (K, FE, OPT_REC) -> Result
powToGenUPS : (L FE, OPT_REC) -> Result
ZEROCOUNT : I := 1000
-- number of zeroes to be removed when taking logs or nth roots
ratIfCan fcn == retractIfCan(fcn)@Union(RN,"failed")
check_inverse(coef : FE, opt_rec : OPT_REC) : B ==
if opt_rec.inv_check? case (FE -> B) then
((opt_rec.inv_check?)::(FE -> B))(coef)
else
true
check_zero(coef : FE, opt_rec : OPT_REC) : B ==
if opt_rec.zero_check? case (FE -> B) then
((opt_rec.zero_check?)::(FE -> B))(coef)
else
coef = 0
carefulNthRootIfCan(ups, n, opt_rec, rightOnly?) ==
-- similar to 'nthRootIfCan', but it is fussy about the series
-- it takes as an argument. If 'n' is EVEN and 'posCheck?'
-- is true then the leading coefficient of the series must
-- be POSITIVE. In this case, if 'rightOnly?' is false, the
-- order of the series must be zero. The idea is that the
-- series represents a real function of a real variable, and
-- we want a unique real nth root defined on a neighborhood
-- of zero.
posCheck? := opt_rec.pos_Check?
n < 1 => error "nthRoot: n must be positive"
deg := degree ups
deg1 := deg + ZEROCOUNT :: Expon
while check_zero(coef := coefficient(ups, deg), opt_rec)
and deg < deg1 repeat
if coef ~= 0 then ups := reductum(ups)
deg := order(ups, deg1)
(coef := coefficient(ups, deg)) = 0 =>
error "log of series with many leading zero coefficients"
-- if 'posCheck?' is true, we do not allow nth roots of negative
-- numbers when n in even
if even?(n :: I) then
if posCheck? and ((signum := sign(coef)$SIGNEF) case I) then
(signum@I) = -1 =>
return stateProblem("nth root","negative leading coefficient")
not rightOnly? and not zero? deg => -- nth root not unique
return stateProblem("nth root","series of non-zero order")
not(check_inverse(coef, opt_rec)) =>
stateProblem("nth root", "need to invert bad coefficient")
(ans := nthRootIfCan(ups,n)) case "failed" =>
stateProblem("nth root","no nth root")
[ans@UPS]
stateProblem(function, problem) ==
-- records the problem which occurred in converting an expression
-- to a power series
[[function, problem]]
exprToUPS(fcn, posCheck?, atanFlag) ==
i_expr_to_PS(fcn, [posCheck?, atanFlag, "none", "none",
"none", false, 0, []]$OPT_REC)
exprToPS(fcn, posCheck?, atanFlag, log_flag, log_val) ==
i_expr_to_PS(fcn, [posCheck?, atanFlag, "none", "none", "none",
log_flag, log_val, []]$OPT_REC)
exprToPS(fcn, posCheck?, atanFlag, log_flag, log_val,
coef_chk, inv_chk, zero_chk) ==
i_expr_to_PS(fcn, [posCheck?, atanFlag, coef_chk, inv_chk, zero_chk,
log_flag, log_val, []]$OPT_REC)
i_expr_to_PS(fcn, opt_rec) ==
-- converts a functional expression to a power series
--!! The following line is commented out so that expressions of
--!! the form a^b will be normalized to exp(b * log(a)) even if
--!! 'a' and 'b' do not involve the limiting variable 'x'.
--!! - cjw 1 Dec 94
--not member?(x, variables fcn) => [monomial(fcn, 0)]
(poly := retractIfCan(fcn)@Union(POL,"failed")) case POL =>
polyToUPS(univariate(poly@POL, x), opt_rec)
(sum := isPlus fcn) case L(FE) =>
listToUPS(sum, i_expr_to_PS, opt_rec, 0,
(y1, y2) +-> y1 + y2)
(prod := isTimes fcn) case L(FE) =>
listToUPS(prod, i_expr_to_PS, opt_rec, 1,
(y1, y2) +-> y1*y2)
(expt := isNonTrivPower fcn) case Record(val : FE, exponent : I) =>
power := expt@Record(val : FE, exponent : I)
powerToUPS(power.val, power.exponent, opt_rec)
(ker := retractIfCan(fcn)@Union(K,"failed")) case K =>
kernelToUPS(ker, opt_rec)
error "exprToUPS: neither a sum, product, power, nor kernel"
polyToUPS(poly, opt_rec) ==
-- converts a polynomial to a power series
zero? poly => [0$UPS]
-- we don't start with 'ans := 0' as this may lead to an
-- enormous number of leading zeroes in the power series
deg := degree poly
coef := leadingCoefficient(poly) :: FE
if opt_rec.coeff_check? case (FE -> B) then
if not((opt_rec.coeff_check?::(FE -> B))(coef)) then
return stateProblem("polyToUPS", "bad coeff")
ans := monomial(coef, deg :: Expon)$UPS
poly := reductum poly
while not zero? poly repeat
deg := degree poly
coef := leadingCoefficient(poly) :: FE
if opt_rec.coeff_check? case (FE -> B) then
if not((opt_rec.coeff_check?::(FE -> B))(coef)) then
return stateProblem("polyToUPS", "bad coeff")
ans := ans + monomial(coef, deg :: Expon)$UPS
poly := reductum poly
[ans]
listToUPS(list, feToUPS, opt_rec, ans, op) ==
-- converts each element of a list of expressions to a power
-- series and returns the sum of these series, when 'op' is +
-- and 'ans' is 0, or the product of these series, when 'op' is *
-- and 'ans' is 1
while not(empty?(list)) repeat
(term := feToUPS(first list, opt_rec)) case %problem =>
return term
ans := op(ans, term.%series)
list := rest list
[ans]
isNonTrivPower fcn ==
-- is the function a power with exponent other than 0 or 1?
(expt := isPower fcn) case "failed" => "failed"
power := expt :: Record(val : FE, exponent : I)
(power.exponent = 1) => "failed"
power
powerToUPS(fcn, n, opt_rec) ==
-- converts an integral power to a power series
(b := i_expr_to_PS(fcn, opt_rec)) case %problem => b
n > 0 => [(b.%series) ^ (n::PositiveInteger)]
-- check lowest order coefficient when n < 0
ups := b.%series; deg := degree ups
deg1 := deg + ZEROCOUNT :: Expon
while check_zero(coef := coefficient(ups, deg), opt_rec)
and deg < deg1 repeat
if coef ~= 0 then ups := reductum(ups)
deg := order(ups, deg1)
(coef := coefficient(ups, deg)) = 0 =>
error "inverse of series with many leading zero coefficients"
not(check_inverse(coef, opt_rec)) =>
stateProblem("power", "need to invert bad coefficient")
[ups ^ n]
ELITS := EllipticFunctionsUnivariateTaylorSeries(FE, UTS)
ARES ==> Record(osers : L(UPS), sers : L(UPS), sere : L(FE), sers0 : L(FE))
ARESU ==> Union(ARES, Result)
handle_args(args : L(FE), opt_rec : OPT_REC) : ARESU ==
losers : L(UPS) := []
lsers : L(UPS) := []
lsere : L(FE) := []
lcoef : L(FE) := []
for arg in args repeat
not(member?(x, variables(arg))) =>
losers := cons(arg::UPS, losers)
lsers := cons(0$UPS, lsers)
lsere := cons(0$FE, lsere)
lcoef := cons(arg, lcoef)
(nsu := i_expr_to_PS(arg, opt_rec)) case %problem =>
return nsu
ups := nsu.%series
order(ups, 0) < 0 =>
return stateProblem("handle_args", "argument not Taylor")
coef := coefficient(ups, 0)
losers := cons(ups, losers)
lcoef := cons(coef, lcoef)
lsere := cons(1$FE, lsere)
lsers := cons(ups - coef::UPS, lsers)
[reverse!(losers), reverse!(lsers), reverse!(lsere),
reverse!(lcoef)]$ARES
do_taylor_via_deriv(nf : UPS, lsyms : L(Symbol), lser : L(UPS)) : Result ==
lders : L(FE -> FE) := ([(c : FE) : FE +->
differentiate(c, sym) for sym in lsyms])
ups : UPS := taylor_via_deriv(nf, lser, lders)$TEXP
[map((c : FE) : FE +-> eval(c, [sym::FE = 0$FE for sym in lsyms]), ups)]
do_taylor_via_deriv2(nk : FE, lsyms : L(Symbol), lser : L(UPS)) : Result ==
lders : L(FE -> FE) := ([(c : FE) : FE +->
differentiate(c, sym) for sym in lsyms])
ups : UPS := taylor_via_deriv(nk, lser, lders)$TEXP
[map((c : FE) : FE +-> eval(c, [sym::FE = 0$FE for sym in lsyms]), ups)]
NARGS ==> Record(nargs0 : L(FE), sers : L(UPS), syms : L(Symbol))
convert_args(lsers : L(UPS), lsere : L(FE), lser0 : L(FE)) : NARGS ==
lsyms : List(Symbol) := []
lser : List(UPS) := []
nargs : List(FE) := []
for s in lsers for e in lsere for c in lser0 repeat
e = 0 =>
nargs := cons(c, nargs)
nsym := new()$Symbol
nargs := cons(c + nsym::FE, nargs)
lsyms := cons(nsym, lsyms)
lser := cons(s, lser)
nargs := reverse!(nargs)
[nargs, lser, lsyms]
do_ell(losers : L(UPS), lsers : L(UPS), lsere : L(FE),
lser0 : L(FE), ef : (UTS, L(FE)) -> UTS) : Result ==
cargs := convert_args(rest lsers, rest lsere, rest lser0)
nres := applyTaylor(f +-> ef(f, cargs.nargs0), losers(1))
do_taylor_via_deriv(nres, cargs.syms, cargs.sers)
do_ell2(losers : L(UPS), lsers : L(UPS), lsere : L(FE),
lser0 : L(FE), ef : (UTS, FE) -> UTS) : Result ==
do_ell(losers, lsers, lsere, lser0, (f, l) +-> ef(f, l(1)))
do_ell3(losers : L(UPS), lsers : L(UPS), lsere : L(FE),
lser0 : L(FE), ef : (UTS, FE, FE) -> UTS) : Result ==
do_ell(losers, lsers, lsere, lser0, (f, l) +-> ef(f, l(1), l(2)))
SupF ==> SparseUnivariatePolynomial(FE)
-- zvar := monomial(1, 1)$SupF
-- zvar := monomial(1, 1::Expon)$UPS
-- taylor_via_deriv is good enough for Airy functions
-- f'' - zf = 0
-- airyEq(lc : L(FE), z0 : FE) : L(UPS) ==
-- zvar := monomial(1, 1::Expon)$UPS + z0::UPS
-- [1, 0, zvar]
-- z^2f'' + zf' +(z^2 - v^2)f = 0
besselEq(lc : L(FE), z0 : FE) : L(UTS) ==
v := lc(1)::UTS
zvar := monomial(1, 1)$UTS + z0::UTS
[zvar^2, zvar, zvar^2 - v^2]
-- z^2f'' + zf' +(z^2 + v^2)f = 0
besselEqm(lc : L(FE), z0 : FE) : L(UTS) ==
v := lc(1)::UTS
zvar := monomial(1, 1)$UTS + z0::UTS
[zvar^2, zvar, zvar^2 + v^2]
-- z^4f^(4) + 2z^3f^(3) - (1 + 2v^2)z^2f'' + (1 + 2v^2)zf'
-- (v^2 - 4v^2 + z^4)f = 0
kelvinEq(lc : L(FE), z0 : FE) : L(UTS) ==
v := lc(1)::UTS
zvar := monomial(1, 1)$UTS + z0::UTS
v2 := 1$UTS + 2*v^2
[zvar^4, 2*zvar^3, -v2*zvar^2, v2*zvar, v^2 - 4*v^2 + zvar^4]
-- zf'' + (b-z)f' - af = 0
kummerEq(lc : L(FE), z0 : FE) : L(UTS) ==
a := lc(1)::UTS
b := lc(2)::UTS
zvar := monomial(1, 1)$UTS + z0::UTS
[zvar, (b - zvar), -a]
-- (1 - z^2)^2f'' -2z(1-z^2)f' + (nu(nu+1)(1-z^2) - mu)f = 0
legendreEq(lc : L(FE), z0 : FE) : L(UTS) ==
nu := lc(1)::UTS
mu := lc(2)::UTS
zvar := monomial(1, 1)$UTS + z0::UTS
z2 := 1 - zvar^2
[z2^2, -2*zvar*z2, nu*(nu + 1)*z2 - mu]
--z^2f'' + (1/4 - m^2 + kz - z^2/4)f = 0
whittakerEq(lc : L(FE), z0 : FE) : L(UTS) ==
k := lc(1)::UTS
m := lc(2)::UTS
o4 := (1/(4::R::FE))::UTS
zvar := monomial(1, 1)$UTS + z0::UTS
[zvar^2, 0, o4 - m^2 + k*zvar - zvar^2/(4::R::FE)]
do_diff_eq(ker : K, losers : L(UPS), lsers : L(UPS),
lsere : L(FE), lser0 : L(FE),
getEq : (L(FE), FE) -> L(UTS)) : Result ==
cargs := convert_args(lsers, lsere, lser0)
nargs := cargs.nargs0
last(lsere) = 0 =>
nker : FE := kernel(operator(ker), nargs)
do_taylor_via_deriv2(nker, cargs.syms, cargs.sers)
z0 := last(lser0)
ecl := getEq(nargs, z0)
cn := first(ecl)
cn1u := recip(cn)
cn1u case "failed" =>
error "do_diff_eq: called at singular point"
cn1 := -(cn1u@UTS)
ecl := [c*cn1 for c in rest(ecl)]
nker : FE := kernel(operator(ker), nargs)
lc : L(FE) := [nker]
lsyms := cargs.syms
sym := first(lsyms)
lsyms := rest(lsyms)
lsers := rest(cargs.sers)
for c in rest(ecl) repeat
nker := differentiate(nker, sym)
lc := cons(nker, lc)
lc := [eval(c, sym::FE = 0$FE) for c in reverse!(lc)]
ups := taylor_via_lode(reverse(ecl), first(cargs.sers), lc)
do_taylor_via_deriv(ups, lsyms, lsers)
SPFUTS ==> SpecialFunctionUnivariateTaylorSeries(FE, UTS)
lambertW0(arg : UPS) : UPS ==
applyTaylor(lambertW0$SPFUTS, arg)
do_weierstrass(losers : L(UPS), lsers : L(UPS), lsere : L(FE),
lser0 : L(FE), ef : (FE, FE, UTS) -> UTS, k : I, cz : I,
opt_rec : OPT_REC) : Result ==
lsere(3) = 0 => error "expansion at 0"
z_ser := losers(3)
z_inv : UPS
if cz ~= 0 then
deg := order(z_ser, ZEROCOUNT :: Expon)
(coef := coefficient(z_ser, deg)) = 0 =>
error "inverse of series with many leading zero coefficients"
if not(check_inverse(coef, opt_rec)) then
return stateProblem("weierstrass",
"need to invert bad coefficient")
z_inv := cz*z_ser^k
else
z_inv := 0
cargs := convert_args(lsers, lsere, lser0)
nargs := cargs.nargs0
nres := applyTaylor((f : UTS) : UTS +-> ef(nargs(1), nargs(2), f),
z_ser)
lsyms := rest(cargs.syms)
lsers := rest(cargs.sers)
nres2 := do_taylor_via_deriv(nres, lsyms, lsers)
nres2 case %problem => return nres2
[z_inv + nres2.%series]
make_taylor(f : Integer -> FE) : UTS ==
rn : Reference(Integer) := ref(1)
genc := (s : FE) : FE +->
n := deref(rn)
val := f(n)
setref(rn, n + 1)
val
sc : Stream(FE) := stream(genc, f(0))
series(sc)$UTS
my_pi : FE := pi()$FE
lpi : FE := log(2::FE*pi()$FE)/2::FE
sqrt_pi : FE := 1/sqrt(pi())
gen_erfs(i : Integer, rv : Reference(FE)) : FE ==
i = 1 => 1
even?(i) => 0
val := (2 - i)::FE/(2::FE)*deref(rv)
setref(rv, val)
val
do_erfs(iups : UPS) : Result ==
rv : Reference(FE) := ref(1)
lerfs := make_taylor((i : Integer) : FE +-> gen_erfs(i, rv))
[sqrt_pi*apply_taylor(lerfs, iups)]
gen_erfis(i : Integer, rv : Reference(FE)) : FE ==
i = 1 => 1
even?(i) => 0
val := (i - 2)::FE/(2::FE)*deref(rv)
setref(rv, val)
val
do_erfis(iups : UPS) : Result ==
rv : Reference(FE) := ref(1)
lerfs := make_taylor((i : Integer) : FE +-> gen_erfis(i, rv))
[sqrt_pi*apply_taylor(lerfs, iups)]
gen_eis(i : Integer, rv : Reference(FE)) : FE ==
i = 0 => 0
i = 1 => 1
val := (i - 1)::FE*deref(rv)
setref(rv, val)
val
do_eis(iups : UPS) : Result ==
rv : Reference(FE) := ref(1)
leis := make_taylor(i +-> gen_eis(i, rv))
[apply_taylor(leis, iups)]
replace_log(lc : FE, k : FE, lx : FE, ups1 : UPS) : UPS ==
c1 := k*lx + log(lc)
c1::UPS + log(ups1)
can_integrate_uts : Boolean := false
if UTS has integrate : UTS -> UTS then
can_integrate_uts := true
integt(f) == integrate(f)
else
integt(f) == error "can not integrate"
if FE has SpecialFunctionCategory then
my_digamma(xx) == digamma(xx)
else
my_digamma(xx) == error "need digamma"
do_Ei00(xx : UTS, exx : UTS, lc : FE, k : FE, lx : FE, ups1 : UPS,
ups : UPS) : Result ==
d_eiu := ((exx - 1$UTS) exquo xx)
d_eiu case "failed" => error "impossible: exquo failed"
d_ei := d_eiu@UTS
ei0 := integt(d_ei)
r1 := -my_digamma(1$FE)::UPS + replace_log(lc, k, lx, ups1)
r2 := apply_taylor(ei0, ups)
[r1 + r2]
do_Ei0(lc : FE, k : FE, lx : FE, ups1 : UPS, ups : UPS) : Result ==
xx := monomial(1, 1)$UTS
exx := exp(xx)$UTS
do_Ei00(xx, exx, lc, k, lx, ups1, ups)
do_Ci0(lc : FE, k : FE, lx : FE, ups1 : UPS, ups : UPS) : Result ==
xx := monomial(1, 1)$UTS
cxx := exp(xx)$UTS
do_Ei00(xx, cxx, lc, k, lx, ups1, ups)
do_Chi0(lc : FE, k : FE, lx : FE, ups1 : UPS, ups : UPS) : Result ==
xx := monomial(1, 1)$UTS
chxx := cosh(xx)$UTS
do_Ei00(xx, chxx, lc, k, lx, ups1, ups)
do_dilog0(lc : FE, k : FE, lx : FE, ups1 : UPS, ups : UPS) : Result ==
xx := monomial(1, 1)$UTS
lxx := log(1$UTS - xx)$UTS
l1 := apply_taylor(lxx, ups)
r1 := ((pi()$FE)^2/(6::FE))::UPS - replace_log(lc, k, lx, ups1)*l1
lx1u := (lxx exquo xx)
lx1u case "failed" => error "impossible: exquo failed"
lx1 := lx1u@UTS
lx2 := integt(lx1)
r2 := apply_taylor(lx2, ups)
[r1 + r2]
gen_lg(i : Integer) : FE ==
i = 0 => lpi
even?(i) => 0
k := i + 1
bernoulli(k)$IntegerNumberTheoryFunctions::FE/(k*i)::FE
do_log_gamma(lc : FE, k : FE, lx : FE, ups : UPS, ups1 : UPS,
iups : UPS) : Result ==
lgs := make_taylor(gen_lg)
l1 := replace_log(lc, k, lx, ups1)
r1 := (ups - (1/2::FE)::UPS) *$UPS l1 -$UPS ups
r2 : UPS := apply_taylor(lgs, iups)$TEXP
[r1 +$UPS r2]
do_digamma(lc : FE, k : FE, lx : FE, ups1 : UPS, iups : UPS
) : Result ==
lgs := make_taylor(gen_lg)
ldig := monomial(-1, 2)$UTS*differentiate(lgs)$UTS
l1 := replace_log(lc, k, lx, ups1)
r1 : UPS := l1 + (-1/2::FE)*iups
r2 : UPS := apply_taylor(ldig, iups)$TEXP
[r1 +$UPS r2]
do_polygamma_n(n : Integer, iups : UPS) : Result ==
lpol := make_taylor(gen_lg)
lpol := monomial(-1, 2)$UTS*differentiate(lpol)$UTS
lpol := monomial(1, 1)$UTS + monomial((1/2::FE), 2)$UTS +
monomial(-1, 2)$UTS*differentiate(lpol)$UTS
for i in 2..n repeat
lpol := monomial(-1, 2)$UTS*differentiate(lpol)$UTS
[apply_taylor(lpol, iups)$TEXP]
gen_plgi(i : Integer, s : Integer) : FE ==
i = 0 => 0
i = 1 => 1
(i::FE)^(-s)
gen_plgf(i : Integer, s : FE) : FE ==
i = 0 => 0
i = 1 => 1
exp(-s*log(i::FE))
do_polylog0(losers : L(UPS), lsers : L(UPS), lsere : L(FE),
lser0 : L(FE)) : Result ==
s0 := lser0(1)
lsere(1) = 0 =>
su := retractIfCan(s0)@Union(Integer, "failed")
su case Integer =>
si := su@Integer
its := make_taylor(i +-> gen_plgi(i, si))
[apply_taylor(its, lsers(2))]
fts := make_taylor(i +-> gen_plgf(i, s0))
[apply_taylor(fts, lsers(2))]
cargs := convert_args([lsers(1)], [lsere(1)], [s0])
fts := make_taylor(i +-> gen_plgf(i, (cargs.nargs0)(1)))
nres := apply_taylor(fts, lsers(2))
do_taylor_via_deriv(nres, cargs.syms, cargs.sers)
spec_to_UPS(ker : K, args : L(FE), opt_rec : OPT_REC) : Result ==
nm := name(ker)
nn : Integer
polygamma_n := (nm = 'polygamma) and
((nu := retractIfCan(args(1))@Union(Integer, "failed"))
case Integer)
and ((nn := nu@Integer) >= 0)
ei_ci_chi := (nm = 'Ei or nm = 'Ci or nm = 'Chi)
if nm = 'digamma or nm = '%logGamma or nm = '%eis or nm = '%erfs
or nm = '%erfis or ei_ci_chi or nm = 'dilog or polygamma_n then
arg1 := args(1)
if polygamma_n then
arg1 := args(2)
(nsu := i_expr_to_PS(arg1, opt_rec)) case %problem =>
return nsu
ups := nsu.%series
ord := order(ups, 0)
if (ei_ci_chi or nm = 'dilog) and ord = 0 and
(ord := order(ups, 1::Expon)) > 0 then return
not(opt_rec.log_x_replace) or not(can_integrate_uts) =>
stateProblem(string(nm), "expansion at 0")
ord := order(ups)
lc := coefficient(ups, ord)
(signum := sign(lc)$SIGNEF) case "failed" =>
stateProblem(string(nm), "branch problem")
signum = -1 =>
stateProblem(string(nm), "negative argument")
ups1 := monomial(1/lc, -ord)*ups
nm = 'Ei =>
do_Ei0(lc, coerce_Ex(ord), opt_rec.log_x_val, ups1, ups)
nm = 'Ci =>
do_Ci0(lc, coerce_Ex(ord), opt_rec.log_x_val, ups1, ups)
nm = 'Chi =>
do_Chi0(lc, coerce_Ex(ord), opt_rec.log_x_val, ups1, ups)
nm = 'dilog =>
do_dilog0(lc, coerce_Ex(ord), opt_rec.log_x_val, ups1, ups)
error "impossible"
else if ord < 0 then return
not(opt_rec.log_x_replace) =>
stateProblem(string(nm), "argument not Taylor")
lc := coefficient(ups, ord)
(signum := sign(lc)$SIGNEF) case "failed" =>
stateProblem(string(nm), "branch problem")
signum = -1 =>
stateProblem(string(nm), "expansion at - infinity")
iups := ups^(-1$Integer)
nm = '%eis => do_eis(iups)
nm = '%erfs => do_erfs(iups)
nm = '%erfis => do_erfis(iups)
ups1 := monomial(1/lc, -ord)*ups
nm = 'digamma or (polygamma_n and nn = 0) =>
do_digamma(lc, coerce_Ex(ord), opt_rec.log_x_val,
ups1, iups)
nm = '%logGamma =>
do_log_gamma(lc, coerce_Ex(ord), opt_rec.log_x_val,
ups, ups1, iups)
polygamma_n =>
do_polygamma_n(nn, iups)
else if ord = 0 and nm = 'dilog then
lc := coefficient(ups, ord)
if lc = 1 then return
ups := 1$UPS - ups
do_polylog0([2::FE::UPS, ups], [0$UPS, ups],
[0$FE, 1$FE], [2::FE, 0$FE])
(aresu := handle_args(args, opt_rec)) case Result =>
aresu
ares := aresu@ARES
losers := ares.osers
lsers := ares.sers
lsere := ares.sere
lser0 := ares.sers0
nm := name(ker)
nm = 'besselI or nm = 'besselK =>
lser0(2) = 0 => stateProblem(string(nm), "expansion at 0")
do_diff_eq(ker, losers, lsers, lsere, lser0, besselEqm)
nm = 'besselJ or nm = 'besselY or nm = 'hankelH1 or nm = 'hankelH2 =>
lser0(2) = 0 => stateProblem(string(nm), "expansion at 0")
do_diff_eq(ker, losers, lsers, lsere, lser0, besselEq)
nm = 'kummerM or nm = 'kummerU =>
lser0(3) = 0 => stateProblem(string(nm), "expansion at 0")
do_diff_eq(ker, losers, lsers, lsere, lser0, kummerEq)
nm = 'whittakerM or nm = 'whittakerW =>
lser0(3) = 0 => stateProblem(string(nm), "expansion at 0")
do_diff_eq(ker, losers, lsers, lsere, lser0, whittakerEq)
nm = 'kelvinBer or nm = 'kelvinBei or nm = 'kelvinKer
or nm = 'kelvinKei =>
lser0(2) = 0 => stateProblem(string(nm), "expansion at 0")
do_diff_eq(ker, losers, lsers, lsere, lser0, kelvinEq)
nm = 'legendreP or nm = 'legendreQ =>
lser0(3) = 1 or lser0(3) = -1 =>
stateProblem(string(nm), "expansion at +-1")
do_diff_eq(ker, losers, lsers, lsere, lser0, legendreEq)
nm = 'Beta3 and lser0(1) = 0 =>
stateProblem("Beta", "expansion at 0")
nm = 'lambertW and lser0(1) = 0 =>
[lambertW0(losers(1))]
)if false
nm = 'hypergeometricF =>
nm = 'meijerG =>
nm = 'struveH =>
nm = 'struveL =>
nm = 'lommelS1 =>
nm = 'lommelS2 =>
)endif
nm = 'jacobiSn =>
do_ell2(losers, lsers, lsere, lser0, jacobiSn$ELITS)
nm = 'jacobiCn =>
do_ell2(losers, lsers, lsere, lser0, jacobiCn$ELITS)
nm = 'jacobiDn =>
do_ell2(losers, lsers, lsere, lser0, jacobiDn$ELITS)
nm = 'ellipticE2 =>
do_ell2(losers, lsers, lsere, lser0, ellipticE$ELITS)
nm = 'ellipticF =>
do_ell2(losers, lsers, lsere, lser0, ellipticF$ELITS)
nm = 'ellipticPi =>
do_ell3(losers, lsers, lsere, lser0, ellipticPi$ELITS)
nm = 'weierstrassP and lser0(3) = 0 =>
do_weierstrass(losers, lsers, lsere, lser0,
weierstrassP0$SPFUTS, -2, 1, opt_rec)
nm = 'weierstrassPPrime and lser0(3) = 0 =>
do_weierstrass(losers, lsers, lsere, lser0,
weierstrassPPrime0$SPFUTS, -3, -2, opt_rec)
nm = 'weierstrassZeta and lser0(3) = 0 =>
do_weierstrass(losers, lsers, lsere, lser0,
weierstrassZeta0$SPFUTS, -1, 1, opt_rec)
nm = 'weierstrassSigma and lser0(3) = 0 =>
do_weierstrass(losers, lsers, lsere, lser0,
weierstrassSigma0$SPFUTS, 0, 0, opt_rec)
nm = 'polylog and lser0(2) = 0 =>
do_polylog0(losers, lsers, lsere, lser0)
if nm = 'Gamma then
if (ir := retractIfCan(lser0(1))@Union(R, "failed")) case R and
(ii := retractIfCan(ir)@Union(Integer, "failed")) case Integer
and ii <= 0 then
arg1 := args(1)
narg1 := 1 - arg1
nker := kernel(operator(ker), [narg1])
nexpr := pi()$FE/(nker::FE*sin(pi()$FE*arg1))
return i_expr_to_PS(nexpr, opt_rec)
cargs := convert_args(lsers, lsere, lser0)
nargs := cargs.nargs0
nker : FE := kernel(operator(ker), nargs)
do_taylor_via_deriv2(nker, cargs.syms, cargs.sers)
can_integrate : Boolean := false
if UPS has differentiate : UPS -> UPS and
UPS has integrate : UPS -> UPS then
can_integrate := true
atan1(ups) ==
y := differentiate(ups)$UPS/(1$UPS + ups*ups)
yCoef := coefficient(y, -1)
monomial(log yCoef, 0) + integrate(y - monomial(yCoef, -1)$UPS)
integ(f) == integrate(f)$UPS
integ_df(f : UPS, xs : UPS) : UPS ==
integrate(differentiate(xs)$UPS*f)$UPS
else
atan1(ups) == error "atan1 called, but no integrate"
integ(f) == error "integ called, but no integrate"
integ_df(f : UPS, xs : UPS) : UPS ==
error "integ_df called, but no integrate"
do_prim(ker : K, arg0 : FE, opt_rec : OPT_REC) : Result ==
can_integrate =>
c0 := operator(ker)(arg0)
nf := differentiate(ker::FE, x)
ns := i_expr_to_PS(nf, opt_rec)
ns case %problem => return ns
[(c0::UPS) + integ(ns.%series)]
spec_to_UPS(ker, argument(ker), opt_rec)
prim_to_UPS(ker : K, args : L(FE), opt_rec : OPT_REC) : Result ==
nm := name(ker)
#args ~= 1 =>
stateProblem(string nm, "multivariate primitive")
arg := first(args)
(nsu := i_expr_to_PS(arg, opt_rec)) case %problem =>
return nsu
ups := nsu.%series
order(ups, 0) < 0 =>
-- FIXME: handle expansions at infinity
return stateProblem("handle_args", "argument not Taylor")
coef := coefficient(ups, 0)
nm = 'Ei or nm = 'Ci or nm = 'Chi or nm = 'dilog =>
coef = 0 =>
can_integrate_uts and opt_rec.log_x_replace =>
spec_to_UPS(ker, argument(ker), opt_rec)
stateProblem(string(nm), "expansion at 0")
do_prim(ker, coef, opt_rec)
nm = 'li =>
coef = 0 => stateProblem(string(nm), "expansion at 0")
coef = 1 => stateProblem(string(nm), "expansion at 1")
do_prim(ker, coef, opt_rec)
nm = 'Si or nm = 'Shi or nm = 'erf or nm = 'erfi or
nm = 'fresnelS or nm = 'fresnelC =>
do_prim(ker, coef, opt_rec)
stateProblem(string name ker,"unimplemented")
unknown_to_UPS(ker : K, args : L(FE), opt_rec : OPT_REC) : Result ==
(aresu := handle_args(args, opt_rec)) case Result =>
aresu
ares := aresu@ARES
lsers := ares.sers
lsere := ares.sere
lser0 := ares.sers0
cargs := convert_args(lsers, lsere, lser0)
nargs := cargs.nargs0
nker : FE := kernel(operator(ker), nargs)
do_taylor_via_deriv2(nker, cargs.syms, cargs.sers)
kernel1_to_UPS(ker : K, opt_rec : OPT_REC) : Result ==
-- converts a kernel to a power series
args := argument(ker)
op := operator(ker)
has?(op, 'special) =>
spec_to_UPS(ker, args, opt_rec)
has?(op, 'prim) =>
prim_to_UPS(ker, args, opt_rec)
empty? rest args =>
arg := first args
is?(ker, 'abs) =>
nthRootToUPS(arg*arg, 2, opt_rec)
is?(ker, "%paren"::Symbol) => i_expr_to_PS(arg, opt_rec)
is?(ker, 'log) => logToUPS(arg, opt_rec)
is?(ker, 'exp) =>
applyIfCan(expIfCan, arg, "exp", opt_rec)
tranToUPS(ker, arg, opt_rec)
is?(ker, "%power"::Symbol) => powToUPS(args, opt_rec)
is?(ker, 'nthRoot) =>
n := retract(second args)@I
nthRootToUPS(first args, n::NNI, opt_rec)
unknown_to_UPS(ker, args, opt_rec)
kernelToUPS(ker, opt_rec) ==
-- converts a kernel to a power series
(sym := symbolIfCan(ker)) case Symbol =>
(sym@Symbol) = x => [monomial(1, 1)]
[monomial(ker::FE, 0)]
empty?(args := argument ker) => [monomial(ker::FE, 0)]
not member?(x, variables(ker::FE)) => [monomial(ker::FE, 0)]
for cr in opt_rec.cache repeat
if ker = cr.ker then return cr.ser
res1 := kernel1_to_UPS(ker, opt_rec)
opt_rec.cache := cons([ker, res1], opt_rec.cache)
res1
nthRootToUPS(arg, n, opt_rec) ==
-- converts an nth root to a power series
-- this is not used in the limit package, so the series may
-- have non-zero order, in which case nth roots may not be unique
(result := i_expr_to_PS(arg, opt_rec)) case %problem => result
ans := carefulNthRootIfCan(result.%series, n, opt_rec, false)
ans case %problem => ans
[ans.%series]
logToUPS(arg, opt_rec) ==
-- converts a logarithm log(f(x)) to a power series
-- f(x) must have order 0 and if 'posCheck?' is true,
-- then f(x) must have a non-negative leading coefficient
(result := i_expr_to_PS(arg, opt_rec)) case %problem => result
ups := result.%series
deg := degree ups
coef : FE
deg1 := deg + ZEROCOUNT :: Expon
while check_zero(coef := coefficient(ups, deg), opt_rec)
and deg < deg1 repeat
if coef ~= 0 then ups := reductum(ups)
deg := order(ups, deg1)
(coef := coefficient(ups, deg)) = 0 =>
error "log of series with many leading zero coefficients"
not (opt_rec.log_x_replace or zero? deg) =>
stateProblem("log","series of non-zero order")
-- if 'posCheck?' is true, we do not allow logs of negative numbers
if opt_rec.pos_Check? then
if ((signum := sign(coef)$SIGNEF) case I) then
(signum@I) = -1 =>
return stateProblem("log","negative leading coefficient")
if not(check_inverse(coef, opt_rec)) then
return stateProblem("log", "need to invert bad coefficient")
zero? deg => [logIfCan(ups)::UPS]
lt := monomial(coef, deg)$UPS
logTerm : FE := log(coef) + coerce_Ex(deg)*opt_rec.log_x_val
[monomial(logTerm, 0) + log(ups/lt)]
if FE has abs : FE -> FE then
localAbs fcn == abs fcn