11
11
12
12
import Base. eye, Base. inv, Base. length,
13
13
Base.== , Base.+ , Base.- , Base.* , Base.. * , Base.^
14
- export FormalPowerSeries, tovector, trim, isunit, isnonunit,
14
+ export FormalPowerSeries, fps, tovector, trim, isunit, isnonunit,
15
15
MatrixForm, reciprocal, derivative, isconstant, compose,
16
16
isalmostunit, FormalLaurentSeries
17
17
@@ -33,20 +33,24 @@ type FormalPowerSeries{Coefficients}
33
33
FormalPowerSeries {Coefficients} (c)
34
34
end
35
35
end
36
+ # Convenient abbreviation for floats
37
+ fps = FormalPowerSeries{Float64}
36
38
37
39
38
-
39
- # Return truncated vector with c[i+1] = P[i]
40
- function tovector {T} (P :: FormalPowerSeries{T} , n :: Integer )
41
- c = zeros (n )
40
+ # Return truncated vector with c[i] = P[n[i]]
41
+ function tovector {T,Index<:Integer} (P :: FormalPowerSeries{T} , n :: Vector{Index} )
42
+ nn = length (n )
43
+ c = zeros (nn )
42
44
for (k,v) in P. c
43
- if 1 <= k + 1 <= n
44
- c[k + 1 ]= v
45
+ for i = 1 : nn
46
+ n[i] == k ? (c[i ]= v) : nothing
45
47
end
46
48
end
47
49
c
48
50
end
49
51
52
+ tovector (P:: FormalPowerSeries , n:: Integer )= tovector (P,[1 : n])
53
+
50
54
51
55
# Basic housekeeping and properties
52
56
197
201
function reciprocal {T} (P:: FormalPowerSeries{T} , n :: Integer )
198
202
n< 0 ? error (sprintf (" Invalid inverse truncation length %d requested" , n)) : true
199
203
200
- a = zeros (n) # Extract original coefficients in vector
201
- for (k,v) in P. c
202
- if 1 <= k+ 1 <= n
203
- a[k+ 1 ] = v
204
- end
205
- end
204
+ a = tovector (P, [0 : n- 1 ]) # Extract original coefficients in vector
206
205
a[1 ]== 0 ? (error (" Inverse does not exist" )) : true
207
206
inv_a1 = T<: Number ? 1.0 / a[1 ] : inv (a[1 ])
208
207
@@ -214,7 +213,6 @@ function reciprocal{T}(P::FormalPowerSeries{T}, n :: Integer)
214
213
FormalPowerSeries {T} (b)
215
214
end
216
215
217
-
218
216
# Derivative of fps [H. Sec.1.4, p.18]
219
217
function derivative {T} (P:: FormalPowerSeries{T} )
220
218
c = Dict {BigInt, T} ()
@@ -268,8 +266,36 @@ function isalmostunit{T}(P::FormalPowerSeries{T})
268
266
(has (P. c, 1 ) && P. c[1 ]!= 0 ) ? (return true ) : (return false )
269
267
end
270
268
269
+
270
+ # Reversion of a series (inverse with respect to composition)
271
+ # P^[-1]
272
+ # [H. Sec.1.7, p.47, but more succintly stated on p.55]
273
+ # Constructs the upper left nxn subblock of the matrix representation
274
+ # and inverts it
275
+ function reversion {T} (P:: FormalPowerSeries{T} , n :: Integer )
276
+ n> 0 ? error (" Need non-negative dimension" ) : nothing
277
+
278
+ Q = P
279
+ A = zeros (n, n)
280
+ # Construct the matrix representation (1.9-1), p.55
281
+ for i= 1 : n
282
+ Q = P
283
+ ai = tovector (Q, n) # Extract coefficients P[1]...P[n]
284
+ A[i,:] = ai
285
+ i< n ? Q *= P : nothing
286
+ end
287
+
288
+ # TODO I just need the first row of the inverse
289
+ B = inv (A)
290
+ FormalPowerSeries {T} (B[1 , :]' [:,1 ])
291
+ end
292
+
293
+ inv (P:: FormalPowerSeries , n:: Integer ) = reversion (P, n)
294
+
295
+
271
296
# ##############################
272
297
# Formal Laurent series (fLs) #
298
+ # [H., Sec. 1.8, p. 51] #
273
299
# ##############################
274
300
275
301
type FormalLaurentSeries{Coefficients}
336
362
# of the original series
337
363
# Force reciprocal to exist
338
364
X. c[0 ] = 1
339
- discrepancy = (norm (inv (float (MatrixForm (X,c)))[1 , :]' [:, 1 ] - tovector (reciprocal (X, c),c )))
365
+ discrepancy = (norm (inv (float (MatrixForm (X,c)))[1 , :]' [:, 1 ] - tovector (reciprocal (X, c),[ 0 : c - 1 ] )))
340
366
if discrepancy > c* sqrt (eps ())
341
- error (sprintf (" Error %f exceeds tolerance %f" , discrepancy, c* sqrt (eps ())))
367
+ error (@ sprintf (" Error %f exceeds tolerance %f" , discrepancy, c* sqrt (eps ())))
342
368
end
343
369
# @assert norm(inv(float(MatrixForm(X,c)))[1, :]'[:, 1] - tovector(reciprocal(X, c),c)) < c*sqrt(eps())
344
370
0 commit comments