-
Notifications
You must be signed in to change notification settings - Fork 63
/
LaurentSeries.jl
1934 lines (1730 loc) · 54.4 KB
/
LaurentSeries.jl
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
###############################################################################
#
# LaurentSeries.jl : Generic Laurent series over rings and fields,
# capped relative precision
#
###############################################################################
###############################################################################
#
# Data type and parent object methods
#
###############################################################################
@doc raw"""
O(a::Generic.LaurentSeriesElem{T}) where T <: RingElement
Return $0 + O(x^\mathrm{val}(a))$. Usually this function is called with $x^n$
as parameter. Then the function returns the power series $0 + O(x^n)$, which
can be used to set the precision of a power series when constructing it.
"""
function O(a::LaurentSeriesElem{T}) where T <: RingElement
val = valuation(a)
return parent(a)(Vector{T}(undef, 0), 0, val, val, 1)
end
parent_type(::Type{LaurentSeriesRingElem{T}}) where T <: RingElement = LaurentSeriesRing{T}
parent_type(::Type{LaurentSeriesFieldElem{T}}) where T <: FieldElement = LaurentSeriesField{T}
parent(a::LaurentSeriesElem) = a.parent
elem_type(::Type{LaurentSeriesRing{T}}) where T <: RingElement = LaurentSeriesRingElem{T}
elem_type(::Type{LaurentSeriesField{T}}) where T <: FieldElement = LaurentSeriesFieldElem{T}
base_ring_type(::Type{LaurentSeriesRing{T}}) where T <: RingElement = parent_type(T)
base_ring_type(::Type{LaurentSeriesField{T}}) where T <: FieldElement = parent_type(T)
base_ring(R::LaurentSeriesRing{T}) where T <: RingElement = R.base_ring::parent_type(T)
base_ring(R::LaurentSeriesField{T}) where T <: FieldElement = R.base_ring::parent_type(T)
function is_domain_type(::Type{T}) where {S <: RingElement, T <: LaurentSeriesElem{S}}
return is_domain_type(S)
end
is_exact_type(a::Type{T}) where T <: LaurentSeriesElem = false
@doc raw"""
var(a::LaurentSeriesRing)
Return the internal name of the generator of the power series ring. Note that
this is returned as a `Symbol` not a `String`.
"""
var(a::LaurentSeriesRing) = a.S
@doc raw"""
var(a::LaurentSeriesField)
Return the internal name of the generator of the power series ring. Note that
this is returned as a `Symbol` not a `String`.
"""
var(a::LaurentSeriesField) = a.S
###############################################################################
#
# Basic manipulation
#
###############################################################################
function Base.hash(a::LaurentSeriesElem, h::UInt)
b = 0xb163af5694734274%UInt
for i in 0:pol_length(a) - 1
b = xor(b, hash(polcoeff(a, i), h))
b = (b << 1) | (b >> (sizeof(Int)*8 - 1))
end
b = xor(b, hash(scale(a), h))
return b
end
@doc raw"""
pol_length(a::Generic.LaurentSeriesElem)
Return the length of the polynomial underlying the given power series. This
will be zero if the power series has no nonzero terms.
"""
pol_length(a::LaurentSeriesElem) = a.length
@doc raw"""
precision(a::Generic.LaurentSeriesElem)
Return the precision of the given power series in absolute terms. This will
be the sum of the valuation and the length of the underlying polynomial.
"""
precision(a::LaurentSeriesElem) = a.prec
@doc raw"""
valuation(a::Generic.LaurentSeriesElem)
Return the valuation of the given power series, i.e. the degree of the first
nonzero term (or the precision if it is arithmetically zero).
"""
valuation(a::LaurentSeriesElem) = a.val
@doc raw"""
scale(a::Generic.LaurentSeriesElem)
Return the scale factor of the polynomial underlying the given power series.
"""
scale(a::LaurentSeriesElem) = a.scale
@doc raw"""
max_precision(R::LaurentSeriesRing)
Return the maximum relative precision of power series in the given power
series ring.
"""
max_precision(R::LaurentSeriesRing) = R.prec_max
@doc raw"""
max_precision(R::LaurentSeriesField)
Return the maximum relative precision of power series in the given power
series ring.
"""
max_precision(R::LaurentSeriesField) = R.prec_max
@doc raw"""
exp_gcd(a::Generic.LaurentSeriesElem)
Return the GCD of the exponents of the polynomial underlying the given Laurent series.
"""
function exp_gcd(a::LaurentSeriesElem)
n = 0
s = scale(a)
for i = 1:pol_length(a) - 1
if n == 1
return n
end
if polcoeff(a, i) != 0
n = gcd(n, i)
end
end
return n
end
function normalise(a::LaurentSeriesElem, len::Int)
while len > 0 && iszero(a.coeffs[len])
len -= 1
end
return len
end
function set_length!(a::LaurentSeriesElem, len::Int)
a.length = len
return a
end
function set_precision!(a::LaurentSeriesElem, prec::Int)
a.prec = prec
return a
end
function set_valuation!(a::LaurentSeriesElem, val::Int)
a.val = val
return a
end
@doc raw"""
set_scale!(a::Generic.LaurentSeriesElem, scale::Int)
Set the scale factor of the polynomial underlying the given series to the given value.
"""
function set_scale!(a::LaurentSeriesElem, scale::Int)
a.scale = scale
return a
end
function polcoeff(a::LaurentSeriesElem, n::Int)
n < 0 && throw(DomainError(n, "n must be >= 0"))
return n >= pol_length(a) ? zero(base_ring(a)) : a.coeffs[n + 1]
end
function coeff(a::LaurentSeriesElem, n::Int)
if n < valuation(a)
return base_ring(a)()
else
i = n - valuation(a)
if mod(i, scale(a)) != 0
return base_ring(a)()
else
return polcoeff(a, div(i, scale(a)))
end
end
end
@doc raw"""
rescale!(a::Generic.LaurentSeriesElem)
Rescale the polynomial underlying the series so that the GCD of its exponents is 1.
This is only used internally, since the result of every user facing function is a
rescaled series.
"""
function rescale!(a::LaurentSeriesElem)
s = exp_gcd(a)
if s > 1
zlen = div(pol_length(a) - 1, s) + 1
for i = 1:zlen - 1
t = polcoeff(a, i)
a = setcoeff!(a, i, polcoeff(a, i*s))
a = setcoeff!(a, i*s, t)
end
a = set_scale!(a, s*scale(a))
a = set_length!(a, zlen)
elseif pol_length(a) <= 1
a = set_scale!(a, 1)
end
return a
end
@doc raw"""
downscale(a::Generic.LaurentSeriesElem{T}, n::Int) where T <: RingElement
Inflate the underlying polynomial by a factor of $n$. This inserts zero coefficients
for padding. It is assumed that the scale factor of $a$ is divisible by $n$.
"""
function downscale(a::LaurentSeriesElem{T}, n::Int) where T <: RingElement
n <= 0 && throw(DomainError(n, "n must be > 0"))
lena = pol_length(a)
if n == 1 || lena == 0
return a
end
R = base_ring(a)
lenz = (lena - 1)*n + 1
d = Vector{T}(undef, lenz)
j = 0
pn = 0
for i = 0:lenz - 1
if i == pn
d[i + 1] = polcoeff(a, j)
j += 1
pn += n
else
d[i + 1] = R()
end
end
S = typeof(a)
z = S(d, lenz, precision(a), valuation(a), div(scale(a), n))
z.parent = parent(a)
return z
end
@doc raw"""
upscale(a::Generic.LaurentSeriesElem{T}, n::Int) where T <: RingElement
Deflate the underlying polynomial by a factor of $n$. This removes zero coefficients
that existed for padding. It is assumed that the spacing of nonzero coefficients of
$a$ is divisible by $n$.
"""
function upscale(a::LaurentSeriesElem{T}, n::Int) where T <: RingElement
n <= 0 && throw(DomainError(n, "n must be > 0"))
lena = pol_length(a)
if n == 1 || lena == 0
return a
end
R = base_ring(a)
lenz = div(lena - 1, n) + 1
d = Vector{T}(undef, lenz)
j = 0
for i = 1:lenz
d[i] = polcoeff(a, j)
j += n
end
S = typeof(a)
z = S(d, lenz, precision(a), valuation(a), scale(a)*n)
z.parent = parent(a)
return z
end
zero(R::LaurentSeriesRing) = R(0)
zero(R::LaurentSeriesField) = R(0)
one(R::LaurentSeriesField) = R(1)
one(R::LaurentSeriesRing) = R(1)
@doc raw"""
gen(R::LaurentSeriesRing)
Return the generator of the power series ring, i.e. $x + O(x^{n + 1})$ where
$n$ is the maximum precision of the power series ring $R$.
"""
function gen(R::LaurentSeriesRing)
S = base_ring(R)
return R([one(S)], 1, max_precision(R) + 1, 1, 1)
end
@doc raw"""
gen(R::LaurentSeriesField)
Return the generator of the power series ring, i.e. $x + O(x^{n + 1})$ where
$n$ is the maximum precision of the power series ring $R$.
"""
function gen(R::LaurentSeriesField)
S = base_ring(R)
return R([one(S)], 1, max_precision(R) + 1, 1, 1)
end
iszero(a::LaurentSeriesElem) = pol_length(a) == 0
function isone(a::LaurentSeriesElem)
return valuation(a) == 0 && pol_length(a) == 1 && isone(polcoeff(a, 0))
end
@doc raw"""
is_gen(a::Generic.LaurentSeriesElem)
Return `true` if the given power series is arithmetically equal to the
generator of its power series ring to its current precision, otherwise return
`false`.
"""
function is_gen(a::LaurentSeriesElem)
return valuation(a) == 1 && pol_length(a) == 1 && isone(polcoeff(a, 0))
end
is_unit(a::LaurentSeriesElem) = valuation(a) == 0 && is_unit(polcoeff(a, 0))
@doc raw"""
modulus(a::Generic.LaurentSeriesElem{T}) where {T <: ResElem}
Return the modulus of the coefficients of the given power series.
"""
modulus(a::LaurentSeriesElem{T}) where {T <: ResElem} = modulus(base_ring(a))
function deepcopy_internal(a::LaurentSeriesElem{T}, dict::IdDict) where {T <: RingElement}
coeffs = Vector{T}(undef, pol_length(a))
for i = 1:pol_length(a)
coeffs[i] = deepcopy_internal(polcoeff(a, i - 1), dict)
end
return parent(a)(coeffs, pol_length(a), precision(a), valuation(a), scale(a))
end
function renormalize!(z::LaurentSeriesElem)
i = 0
zlen = pol_length(z)
zval = valuation(z)
zprec = precision(z)
while i < zlen && iszero(polcoeff(z, i))
i += 1
end
z = set_precision!(z, zprec)
if i == zlen
z = set_length!(z, 0)
z = set_valuation!(z, zprec)
z = set_scale!(z, 1)
elseif i != 0
z = set_valuation!(z, zval + i*scale(z))
for j = 1:zlen - i
z = setcoeff!(z, j - 1, polcoeff(z, j + i - 1))
end
z = set_length!(z, zlen - i)
end
return nothing
end
function characteristic(a::LaurentSeriesRing{T}) where T <: RingElement
return characteristic(base_ring(a))
end
###############################################################################
#
# Similar and zero
#
###############################################################################
function similar(x::LaurentSeriesElem, R::Ring, max_prec::Int,
s::Symbol; cached::Bool=true)
TT = elem_type(R)
V = Vector{TT}(undef, 0)
p = Generic.LaurentSeriesRingElem{TT}(V, 0, max_prec, max_prec, 1)
# Default similar is supposed to return a Generic series
if base_ring(x) === R && s == var(parent(x)) &&
x isa Generic.LaurentSeriesRingElem{TT} &&
max_precision(parent(x)) == max_prec
# steal parent in case it is not cached
p.parent = parent(x)
else
p.parent = Generic.LaurentSeriesRing{TT}(R, max_prec, s, cached)
end
return p
end
function similar(x::LaurentSeriesElem, R::Field, max_prec::Int,
s::Symbol; cached::Bool=true)
TT = elem_type(R)
V = Vector{TT}(undef, 0)
p = Generic.LaurentSeriesFieldElem{TT}(V, 0, max_prec, max_prec, 1)
# Default similar is supposed to return a Generic series
if base_ring(x) === R && s == var(parent(x)) &&
x isa Generic.LaurentSeriesFieldElem{TT} &&
max_precision(parent(x)) == max_prec
# steal parent in case it is not cached
p.parent = parent(x)
else
p.parent = Generic.LaurentSeriesField{TT}(R, max_prec, s, cached)
end
return p
end
similar(x::LaurentSeriesElem, R::Ring, max_prec::Int,
var::VarName=var(parent(x)); cached::Bool=true) =
similar(x, R, max_prec, Symbol(var); cached)
similar(x::LaurentSeriesElem, R::Ring,
var::VarName=var(parent(x)); cached::Bool=true) =
similar(x, R, max_precision(parent(x)), Symbol(var); cached)
similar(x::LaurentSeriesElem, max_prec::Int,
var::VarName=var(parent(x)); cached::Bool=true) =
similar(x, base_ring(x), max_prec, Symbol(var); cached)
similar(x::LaurentSeriesElem, var::VarName=var(parent(x)); cached::Bool=true) =
similar(x, base_ring(x), max_precision(parent(x)), Symbol(var); cached)
zero(a::LaurentSeriesElem, R::Ring, max_prec::Int,
var::VarName=var(parent(a)); cached::Bool=true) =
similar(a, R, max_prec, Symbol(var); cached)
zero(a::LaurentSeriesElem, R::Ring,
var::VarName=var(parent(a)); cached::Bool=true) =
similar(a, R, Symbol(var); cached)
zero(a::LaurentSeriesElem, max_prec::Int,
var::VarName=var(parent(a)); cached::Bool=true) =
similar(a, max_prec, Symbol(var); cached)
zero(a::LaurentSeriesElem, var::VarName=var(parent(a)); cached::Bool=true) =
similar(a, Symbol(var); cached)
###############################################################################
#
# laurent_series constructor
#
###############################################################################
function laurent_series(R::Ring, arr::Vector{T}, len::Int, prec::Int, val::Int, scale::Int, var::VarName=:x; max_precision::Int=prec, cached::Bool=true) where T
scale <= 0 && error("Scale must be positive")
prec < (len - 1)*scale + val + 1 && error("Precision too small for given data")
TT = elem_type(R)
coeffs = T == Any && length(arr) == 0 ? elem_type(R)[] : map(R, arr)
p = Generic.LaurentSeriesRingElem{TT}(coeffs, len, prec, val, scale)
# Default is supposed to return a Generic Laurent series
p.parent = Generic.LaurentSeriesRing{TT}(R, max_precision, Symbol(var), cached)
return p
end
function laurent_series(R::Field, arr::Vector{T}, len::Int, prec::Int, val::Int, scale::Int, var::VarName=:x; max_precision::Int=prec, cached::Bool=true) where T
scale <= 0 && error("Scale must be positive")
prec < (len - 1)*scale + val + 1 && error("Precision too small for given data")
TT = elem_type(R)
coeffs = T == Any && length(arr) == 0 ? elem_type(R)[] : map(R, arr)
p = Generic.LaurentSeriesFieldElem{TT}(coeffs, len, prec, val, scale)
# Default is supposed to return a Generic Laurent series
p.parent = Generic.LaurentSeriesField{TT}(R, max_precision, Symbol(var), cached)
return p
end
###############################################################################
#
# AbstractString I/O
#
###############################################################################
function AbstractAlgebra.expressify(a::LaurentSeriesElem,
x = var(parent(a)); context = nothing)
sum = Expr(:call, :+)
v = valuation(a)
sc = scale(a)
len = pol_length(a)
for i in 0:len - 1
c = polcoeff(a, i)
expo = i * sc + v
if !iszero(c)
if expo == 0
xk = 1
elseif expo == 1
xk = x
else
xk = Expr(:call, :^, x, expo)
end
if isone(c)
push!(sum.args, Expr(:call, :*, xk))
else
push!(sum.args, Expr(:call, :*, expressify(c, context = context), xk))
end
end
end
push!(sum.args, Expr(:call, :O, Expr(:call, :^, x, precision(a))))
return sum
end
function Base.show(io::IO, ::MIME"text/plain", a::LaurentSeriesElem)
print(io, AbstractAlgebra.obj_to_string(a, context = io))
end
function Base.show(io::IO, a::LaurentSeriesElem)
print(io, AbstractAlgebra.obj_to_string(a, context = io))
end
function show(io::IO, p::LaurentSeriesRing)
@show_name(io, p)
@show_special(io, p)
if is_terse(io)
print(io, "Laurent series ring")
else
io = pretty(io)
print(io, "Laurent series ring in ", var(p), " over ")
print(terse(io), Lowercase(), base_ring(p))
end
end
function show(io::IO, p::LaurentSeriesField)
@show_name(io, p)
@show_special(io, p)
if is_terse(io)
print(io, "Laurent series field")
else
io = pretty(io)
print(io, "Laurent series field in ", var(p), " over ")
print(terse(io), Lowercase(), base_ring(p))
end
end
###############################################################################
#
# Map coefficients
#
###############################################################################
function _make_parent(g::T, p::LaurentSeriesElem, cached::Bool) where T
R = parent(g(zero(base_ring(p))))
S = parent(p)
sym = var(S)
max_prec = max_precision(S)
return AbstractAlgebra.laurent_series_ring(R, max_prec, sym; cached=cached)[1]
end
function map_coefficients(g::T, p::LaurentSeriesElem{<:RingElement};
cached::Bool = true,
parent::Ring = _make_parent(g, p, cached)) where {T}
return _map(g, p, parent)
end
function _map(g::T, p::LaurentSeriesElem, Rx) where T
R = base_ring(Rx)
new_coefficients = elem_type(R)[let c = polcoeff(p, i)
iszero(c) ? zero(R) : R(g(c))
end for i in 0:pol_length(p) - 1]
res = Rx(new_coefficients, pol_length(p), precision(p), valuation(p), scale(p), false)
res = set_length!(res, normalise(res, pol_length(res)))
renormalize!(res)
res = rescale!(res)
return res
end
################################################################################
#
# Change base ring
#
################################################################################
function _change_laurent_series_ring(R, Rx, cached)
P, _ = AbstractAlgebra.laurent_series_ring(R, max_precision(Rx),
var(Rx), cached = cached)
return P
end
function change_base_ring(R::Ring, p::LaurentSeriesElem{T};
cached::Bool = true, parent::Ring =
_change_laurent_series_ring(R, parent(p), cached)) where T <: RingElement
return _map(R, p, parent)
end
###############################################################################
#
# Unary operators
#
###############################################################################
function -(a::LaurentSeriesElem)
len = pol_length(a)
z = parent(a)()
z = set_precision!(z, precision(a))
z = set_valuation!(z, valuation(a))
z = set_scale!(z, scale(a))
fit!(z, len)
for i = 1:len
z = setcoeff!(z, i - 1, -polcoeff(a, i - 1))
end
return z
end
###############################################################################
#
# Binary operators
#
###############################################################################
function +(a::LaurentSeriesElem{T}, b::LaurentSeriesElem{T}) where {T <: RingElement}
check_parent(a, b)
lena = pol_length(a)
lenb = pol_length(b)
vala = valuation(a)
valb = valuation(b)
valz = min(vala, valb)
prec = min(precision(a), precision(b))
sa = scale(a)
sb = scale(b)
if lena == 1
sa = sb
elseif lenb == 1
sb = sa
end
sz = gcd(gcd(sa, sb), abs(vala - valb))
mina = min(vala + lena*sa, prec)
minb = min(valb + lenb*sb, prec)
lenz = max(mina, minb) - valz
lenz = div(lenz + sz - 1, sz)
R = base_ring(a)
z = parent(a)()
fit!(z, lenz)
z = set_precision!(z, prec)
z = set_valuation!(z, valz)
z = set_scale!(z, sz)
pa = vala
pb = valb
j = 0
k = 0
for i = 0: lenz - 1
pi = valz + sz*i
if pi == pa && pi < mina
if pi == pb && pi < minb
z = setcoeff!(z, i, polcoeff(a, j) + polcoeff(b, k))
pb += sb
k += 1
else
z = setcoeff!(z, i, deepcopy(polcoeff(a, j)))
end
j += 1
pa += sa
elseif pi == pb && pi < minb
z = setcoeff!(z, i, deepcopy(polcoeff(b, k)))
k += 1
pb += sb
else
z = setcoeff!(z, i, R())
end
end
z = set_length!(z, normalise(z, lenz))
renormalize!(z)
z = rescale!(z)
return z
end
function -(a::LaurentSeriesElem{T}, b::LaurentSeriesElem{T}) where {T <: RingElement}
check_parent(a, b)
lena = pol_length(a)
lenb = pol_length(b)
vala = valuation(a)
valb = valuation(b)
valz = min(vala, valb)
prec = min(precision(a), precision(b))
sa = scale(a)
sb = scale(b)
if lena == 1
sa = sb
elseif lenb == 1
sb = sa
end
sz = gcd(gcd(sa, sb), abs(vala - valb))
mina = min(vala + lena*sa, prec)
minb = min(valb + lenb*sb, prec)
lenz = max(mina, minb) - valz
lenz = div(lenz + sz - 1, sz)
R = base_ring(a)
z = parent(a)()
fit!(z, lenz)
z = set_precision!(z, prec)
z = set_valuation!(z, valz)
z = set_scale!(z, sz)
pa = vala
pb = valb
j = 0
k = 0
for i = 0: lenz - 1
pi = valz + sz*i
if pi == pa && pi < mina
if pi == pb && pi < minb
z = setcoeff!(z, i, polcoeff(a, j) - polcoeff(b, k))
pb += sb
k += 1
else
z = setcoeff!(z, i, deepcopy(polcoeff(a, j)))
end
j += 1
pa += sa
elseif pi == pb && pi < minb
z = setcoeff!(z, i, -polcoeff(b, k))
k += 1
pb += sb
else
z = setcoeff!(z, i, R())
end
end
z = set_length!(z, normalise(z, lenz))
renormalize!(z)
z = rescale!(z)
return z
end
function *(a::LaurentSeriesElem{T}, b::LaurentSeriesElem{T}) where {T <: RingElement}
check_parent(a, b)
lena = pol_length(a)
lenb = pol_length(b)
if lena > lenb
return b*a
end
aval = valuation(a)
bval = valuation(b)
zval = aval + bval
prec = min(precision(a) - aval, precision(b) - bval)
sa = scale(a)
sb = scale(b)
if lena == 1
sa = sb
elseif lenb == 1
sb = sa
end
sz = gcd(sa, sb)
lena = min(lena*sa, prec)
lenb = min(lenb*sb, prec)
if lena == 0 || lenb == 0
return parent(a)(Vector{T}(undef, 0), 0, prec + zval, zval, 1)
end
t = base_ring(a)()
da = div(sa, sz)
db = div(sb, sz)
a = downscale(a, da)
b = downscale(b, db)
lena = pol_length(a)
lenb = pol_length(b)
lenz = min(lena + lenb - 1, div(prec + sz - 1, sz))
d = Vector{T}(undef, lenz)
for i = 1:min(lena, lenz)
d[i] = polcoeff(a, i - 1)*polcoeff(b, 0)
end
if lenz > lena
for j = 2:min(lenb, lenz - lena + 1)
d[lena + j - 1] = polcoeff(a, lena - 1)*polcoeff(b, j - 1)
end
end
for i = 1:lena - 1
if lenz > i
ai = polcoeff(a, i - 1)
if ai != 0
for j = 2:min(lenb, lenz - i + 1)
t = mul!(t, ai, polcoeff(b, j - 1))
d[i + j - 1] = addeq!(d[i + j - 1], t)
end
end
end
end
z = parent(a)(d, lenz, prec + zval, zval, sz)
z = set_length!(z, normalise(z, lenz))
renormalize!(z)
z = rescale!(z)
return z
end
###############################################################################
#
# Ad hoc binary operators
#
###############################################################################
function *(a::T, b::LaurentSeriesElem{T}) where {T <: RingElem}
len = pol_length(b)
z = parent(b)()
fit!(z, len)
z = set_precision!(z, precision(b))
z = set_valuation!(z, valuation(b))
z = set_scale!(z, scale(b))
for i = 1:len
z = setcoeff!(z, i - 1, a*polcoeff(b, i - 1))
end
z = set_length!(z, normalise(z, len))
renormalize!(z)
z = rescale!(z)
return z
end
function *(a::Union{Integer, Rational, AbstractFloat}, b::LaurentSeriesElem)
len = pol_length(b)
z = parent(b)()
fit!(z, len)
z = set_precision!(z, precision(b))
z = set_valuation!(z, valuation(b))
z = set_scale!(z, scale(b))
for i = 1:len
z = setcoeff!(z, i - 1, a*polcoeff(b, i - 1))
end
z = set_length!(z, normalise(z, len))
renormalize!(z)
z = rescale!(z)
return z
end
*(a::LaurentSeriesElem{T}, b::T) where {T <: RingElem} = b*a
*(a::LaurentSeriesElem, b::Union{Integer, Rational, AbstractFloat}) = b*a
###############################################################################
#
# Shifting
#
###############################################################################
@doc raw"""
shift_left(x::Generic.LaurentSeriesElem{T}, n::Int) where {T <: RingElement}
Return the power series $x$ shifted left by $n$ terms, i.e. multiplied by
$x^n$.
"""
function shift_left(x::LaurentSeriesElem{T}, n::Int) where {T <: RingElement}
z = deepcopy(x)
z = set_precision!(z, precision(x) + n)
z = set_valuation!(z, valuation(x) + n)
return z
end
@doc raw"""
shift_right(x::Generic.LaurentSeriesElem{T}, n::Int) where {T <: RingElement}
Return the power series $x$ shifted right by $n$ terms, i.e. divided by
$x^n$.
"""
function shift_right(x::LaurentSeriesElem{T}, n::Int) where {T <: RingElement}
z = deepcopy(x)
z = set_precision!(z, precision(x) - n)
z = set_valuation!(z, valuation(x) - n)
return z
end
###############################################################################
#
# Truncation
#
###############################################################################
@doc raw"""
truncate(a::Generic.LaurentSeriesElem{T}, n::Int) where {T <: RingElement}
Return $a$ truncated to (absolute) precision $n$.
"""
function truncate(a::LaurentSeriesElem{T}, n::Int) where {T <: RingElement}
alen = pol_length(a)
aprec = precision(a)
aval = valuation(a)
if aprec <= n
return a
end
z = parent(a)()
z = set_precision!(z, n)
if n <= aval
z = set_length!(z, 0)
z = set_valuation!(z, n)
z = set_scale!(z, 1)
else
sa = scale(a)
zlen = div(n - aval + sa - 1, sa)
zlen = min(zlen, alen)
fit!(z, zlen)
z = set_valuation!(z, aval)
for i = 0:zlen - 1
z = setcoeff!(z, i, polcoeff(a, i))
end
z = set_length!(z, normalise(z, zlen))
z = set_scale!(z, sa)
z = rescale!(z)
end
return z
end
# Intended only for internal use, does not renormalize, assumes n >= 0
# Requires valuation(a) == valuation(b) == 0 and scale(a) == scale(b)
function mullow(a::LaurentSeriesElem{T}, b::LaurentSeriesElem{T}, n::Int) where {T <: RingElement}
lena = pol_length(a)
lenb = pol_length(b)
if lena == 0 || lenb == 0
z = zero(parent(a))
zprec = valuation(a) + valuation(b)
z = set_valuation!(z, zprec)
z = set_precision!(z, zprec)
z = set_scale!(z, scale(a))
return z
end
s = scale(a)
prec = min(precision(a), precision(b))
t = base_ring(a)()
lenz = min(lena + lenb - 1, div(n + s - 1, s))
d = Vector{T}(undef, lenz)
for i = 1:min(lena, lenz)
d[i] = polcoeff(a, i - 1)*polcoeff(b, 0)
end
if lenz > lena
for j = 2:min(lenb, lenz - lena + 1)
d[lena + j - 1] = polcoeff(a, lena - 1)*polcoeff(b, j - 1)
end
end
for i = 1:lena - 1
if lenz > i
for j = 2:min(lenb, lenz - i + 1)
t = mul!(t, polcoeff(a, i - 1), polcoeff(b, j - 1))
d[i + j - 1] = addeq!(d[i + j - 1], t)
end
end
end
z = parent(a)(d, lenz, prec, 0, s, false)
z = set_length!(z, normalise(z, lenz))
return z
end
###############################################################################
#
# Inflation/deflation
#
###############################################################################
function inflate(a::LaurentSeriesElem{T}, b::Int) where {T <: RingElement}
return parent(a)(deepcopy(a.coeffs), pol_length(a), b*a.prec, b*a.val, b*a.scale)
end
function deflate(a::LaurentSeriesElem{T}, b::Int) where {T <: RingElement}
return parent(a)(deepcopy(a.coeffs), pol_length(a), div(a.prec, b), div(a.val, b), div(a.scale, b))
end
###############################################################################
#
# Powering
#
###############################################################################
@doc raw"""
^(a::Generic.LaurentSeriesElem{T}, b::Int) where {T <: RingElement}
Return $a^b$. We require $b \geq 0$.
"""
function ^(a::LaurentSeriesElem{T}, b::Int) where {T <: RingElement}
# special case powers of x for constructing power series efficiently
if b == 0
# in fact, the result would be exact 1 if we had exact series
z = one(parent(a))
return z
elseif pol_length(a) == 0
z = parent(a)()
z = set_precision!(z, b*valuation(a))
z = set_valuation!(z, b*valuation(a))
z = set_scale!(z, 1)
return z
elseif is_gen(a)
z = parent(a)()
fit!(z, 1)
z = set_precision!(z, b + precision(a) - 1)
z = set_valuation!(z, b)
z = setcoeff!(z, 0, deepcopy(polcoeff(a, 0)))
z = set_scale!(z, 1)
z = set_length!(z, 1)
return z
elseif pol_length(a) == 1
c = polcoeff(a, 0)^b
z = parent(a)(c)
z = set_precision!(z, (b - 1)*valuation(a) + precision(a))
z = set_valuation!(z, iszero(c) ? precision(z) : b*valuation(a))
z = set_scale!(z, 1)
return z
elseif b == 1
return deepcopy(a)
elseif b == -1
return inv(a)
end
if b < 0
a = inv(a)
b = -b
end
bit = ~((~UInt(0)) >> 1)