7
7
# Power Series---Integration---Conformal Mapping---Location of Zeros,
8
8
# Wiley-Interscience: New York, 1974
9
9
10
- import Base: zero, one, eye, inv, length, == , + , - , * , (.* ), ^
10
+ import Base: zero, one, inv, length, == , + , - , * , ^
11
+ import Base. Broadcast: broadcasted
11
12
export FormalPowerSeries, fps, tovector, trim, isunit, isnonunit,
12
13
MatrixForm, reciprocal, derivative, isconstant, compose,
13
14
isalmostunit, FormalLaurentSeries
@@ -36,11 +37,11 @@ FormalPowerSeries(v::Vector{T}) where T = FormalPowerSeries{T}(v)
36
37
# Convenient abbreviation for floats
37
38
fps = FormalPowerSeries{Float64}
38
39
39
- zero {T} (P:: FormalPowerSeries{T} ) = FormalPowerSeries (T[])
40
- one {T} (P:: FormalPowerSeries{T} ) = FormalPowerSeries (T[1 ])
40
+ zero (P:: FormalPowerSeries{T} ) where {T} = FormalPowerSeries (T[])
41
+ one (P:: FormalPowerSeries{T} ) where {T} = FormalPowerSeries (T[1 ])
41
42
42
43
# Return truncated vector with c[i] = P[n[i]]
43
- function tovector {T,Index<:Integer} (P:: FormalPowerSeries{T} , n:: AbstractVector{Index} )
44
+ function tovector (P:: FormalPowerSeries{T} , n:: AbstractVector{Index} ) where {T,Index <: Integer }
44
45
nn = length (n)
45
46
c = zeros (nn)
46
47
for (k,v) in P. c, i in eachindex (n)
@@ -55,7 +56,7 @@ tovector(P::FormalPowerSeries, n::Integer)=tovector(P,1:n)
55
56
# Basic housekeeping and properties
56
57
57
58
# Remove extraneous zeros
58
- function trim {T} (P:: FormalPowerSeries{T} )
59
+ function trim (P:: FormalPowerSeries{T} ) where {T}
59
60
for (k,v) in P. c
60
61
if v== 0
61
62
delete! (P. c, k)
@@ -64,9 +65,9 @@ function trim{T}(P::FormalPowerSeries{T})
64
65
return P
65
66
end
66
67
67
- length {T} (P:: FormalPowerSeries{T} )= max ([k for (k,v) in P. c])
68
+ length (P:: FormalPowerSeries{T} ) where {T} = maximum ([k for (k,v) in P. c])
68
69
69
- function == {T} (P:: FormalPowerSeries{T} , Q:: FormalPowerSeries{T} )
70
+ function == (P:: FormalPowerSeries{T} , Q:: FormalPowerSeries{T} ) where {T}
70
71
for (k,v) in P. c
71
72
if v== 0 # ignore explicit zeros
72
73
continue
@@ -89,7 +90,7 @@ function =={T}(P::FormalPowerSeries{T}, Q::FormalPowerSeries{T})
89
90
end
90
91
91
92
# Basic arithmetic [H, p.10]
92
- function + {T} (P:: FormalPowerSeries{T} , Q:: FormalPowerSeries{T} )
93
+ function + (P:: FormalPowerSeries{T} , Q:: FormalPowerSeries{T} ) where {T}
93
94
c = Dict {BigInt, T} ()
94
95
for (k,v) in P. c
95
96
haskey (c,k) ? (c[k]+= v) : (c[k]= v)
@@ -100,7 +101,7 @@ function +{T}(P::FormalPowerSeries{T}, Q::FormalPowerSeries{T})
100
101
FormalPowerSeries {T} (c)
101
102
end
102
103
103
- function - {T} (P:: FormalPowerSeries{T} , Q:: FormalPowerSeries{T} )
104
+ function - (P:: FormalPowerSeries{T} , Q:: FormalPowerSeries{T} ) where {T}
104
105
c = Dict {BigInt, T} ()
105
106
for (k,v) in P. c
106
107
c[k] = get (c,k,zero (T)) + v
@@ -112,7 +113,7 @@ function -{T}(P::FormalPowerSeries{T}, Q::FormalPowerSeries{T})
112
113
end
113
114
114
115
# negation
115
- function - {T} (P:: FormalPowerSeries{T} )
116
+ function - (P:: FormalPowerSeries{T} ) where {T}
116
117
c = Dict {BigInt, T} ()
117
118
for (k,v) in P. c
118
119
c[k] = - v
@@ -121,18 +122,18 @@ function -{T}(P::FormalPowerSeries{T})
121
122
end
122
123
123
124
# multiplication by scalar
124
- function * {T} (P:: FormalPowerSeries{T} , n:: Number )
125
+ function * (P:: FormalPowerSeries{T} , n:: Number ) where {T}
125
126
c = Dict {BigInt, T} ()
126
127
for (k,v) in P. c
127
128
c[k] = n* v
128
129
end
129
130
FormalPowerSeries {T} (c)
130
131
end
131
- * {T} (n:: Number , P:: FormalPowerSeries{T} ) = * (P, n)
132
+ * (n:: Number , P:: FormalPowerSeries{T} ) where {T} = * (P, n)
132
133
133
134
# Cauchy product [H, p.10]
134
- * {T} (P:: FormalPowerSeries{T} , Q:: FormalPowerSeries{T} ) = CauchyProduct (P, Q)
135
- function CauchyProduct {T} (P:: FormalPowerSeries{T} , Q:: FormalPowerSeries{T} )
135
+ * (P:: FormalPowerSeries{T} , Q:: FormalPowerSeries{T} ) where {T} = CauchyProduct (P, Q)
136
+ function CauchyProduct (P:: FormalPowerSeries{T} , Q:: FormalPowerSeries{T} ) where {T}
136
137
c = Dict {BigInt, T} ()
137
138
for (k1, v1) in P. c, (k2, v2) in Q. c
138
139
c[k1+ k2]= get (c, k1+ k2, zero (T))+ v1* v2
@@ -141,36 +142,37 @@ function CauchyProduct{T}(P::FormalPowerSeries{T}, Q::FormalPowerSeries{T})
141
142
end
142
143
143
144
# Hadamard product [H, p.10] - the elementwise product
144
- broadcast (:: typeof (* ), P:: FormalPowerSeries{T} , Q:: FormalPowerSeries{T} ) where T =
145
+ broadcasted (:: typeof (* ), P:: FormalPowerSeries{T} , Q:: FormalPowerSeries{T} ) where T =
145
146
HadamardProduct (P, Q)
146
- function HadamardProduct {T} (P:: FormalPowerSeries{T} , Q:: FormalPowerSeries{T} )
147
+ function HadamardProduct (P:: FormalPowerSeries{T} , Q:: FormalPowerSeries{T} ) where {T}
147
148
c = Dict {BigInt, T} ()
148
149
for (k,v) in P. c
149
- if v!= 0 && haskey (Q. c,k) && Q. c[k]= =0
150
+ if v!= 0 && haskey (Q. c,k) && Q. c[k] ! =0
150
151
c[k] = v * Q. c[k]
151
152
end
152
153
end
153
154
FormalPowerSeries {T} (c)
154
155
end
155
156
156
157
# The identity element over the complex numbers
157
- function eye {T <: Number} (P:: FormalPowerSeries{T} )
158
+ # replacement for previous function eye(P::FormalPowerSeries{T})
159
+ function FormalPowerSeries {T} (s:: UniformScaling ) where {T}
158
160
c = Dict {BigInt, T} ()
159
161
c[0 ] = 1
160
- FormalPowerSeries {T} (c)
162
+ return FormalPowerSeries {T} (c)
161
163
end
162
164
163
- isunit {T <: Number} (P:: FormalPowerSeries{T} ) = P== eye (P )
165
+ isunit (P:: FormalPowerSeries{T} ) where {T <: Number } = P== FormalPowerSeries {Float64} (I )
164
166
165
167
# [H, p.12]
166
- isnonunit {T} (P:: FormalPowerSeries{T} ) = (! haskey (P. c, 0 ) || P. c[0 ]== 0 ) && ! isunit (P)
168
+ isnonunit (P:: FormalPowerSeries{T} ) where {T} = (! haskey (P. c, 0 ) || P. c[0 ]== 0 ) && ! isunit (P)
167
169
168
170
# Constructs the top left m x m block of the (infinite) semicirculant matrix
169
171
# associated with the fps [H, Sec.1.3, p.14]
170
172
# [H] calls it the semicirculant, but in contemporary nomenclature this is an
171
173
# upper triangular Toeplitz matrix
172
174
# This constructs the dense matrix - Toeplitz matrices don't exist in Julia yet
173
- function MatrixForm {T} (P:: FormalPowerSeries{T} , m :: Integer )
175
+ function MatrixForm (P:: FormalPowerSeries{T} , m :: Integer ) where {T}
174
176
m< 0 && error (" Invalid matrix dimension $m requested" )
175
177
M= zeros (T, m, m)
176
178
for (k,v) in P. c
@@ -254,13 +256,13 @@ end
254
256
255
257
# Composition of fps [H, Sec.1.5, p.35]
256
258
# This is the quick and dirty version
257
- function compose {T} (P:: FormalPowerSeries{T} , Q:: FormalPowerSeries{T} )
259
+ function compose (P:: FormalPowerSeries{T} , Q:: FormalPowerSeries{T} ) where {T}
258
260
sum ([v * Q^ k for (k, v) in P. c])
259
261
end
260
262
261
263
262
264
# [H, p.45]
263
- function isalmostunit {T} (P:: FormalPowerSeries{T} )
265
+ function isalmostunit (P:: FormalPowerSeries{T} ) where {T}
264
266
(haskey (P. c, 0 ) && P. c[0 ]!= 0 ) ? (return false ) :
265
267
(haskey (P. c, 1 ) && P. c[1 ]!= 0 ) ? (return true ) : (return false )
266
268
end
271
273
# [H. Sec.1.7, p.47, but more succinctly stated on p.55]
272
274
# Constructs the upper left nxn subblock of the matrix representation
273
275
# and inverts it
274
- function reversion {T} (P:: FormalPowerSeries{T} , n :: Integer )
276
+ function reversion (P:: FormalPowerSeries{T} , n :: Integer ) where {T}
275
277
n> 0 ? error (" Need non-negative dimension" ) : nothing
276
278
277
279
Q = P
0 commit comments