Skip to content

Commit 4d4edda

Browse files
committed
Clean up code
1 parent 8b85be0 commit 4d4edda

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

src/FormalPowerSeries.jl

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
# Power Series---Integration---Conformal Mapping---Location of Zeros,
88
# Wiley-Interscience: New York, 1974
99

10-
import Base.eye, Base.inv, Base.length,
11-
Base.==, Base.+, Base.-, Base.*, Base.(.*), Base.^
10+
import Base: zero, one, eye, inv, length, ==, +, -, *, (.*), ^
1211
export FormalPowerSeries, fps, tovector, trim, isunit, isnonunit,
1312
MatrixForm, reciprocal, derivative, isconstant, compose,
1413
isalmostunit, FormalLaurentSeries
@@ -39,10 +38,8 @@ fps = FormalPowerSeries{Float64}
3938
function tovector{T,Index<:Integer}(P::FormalPowerSeries{T}, n :: Vector{Index})
4039
nn = length(n)
4140
c = zeros(nn)
42-
for (k,v) in P.c
43-
for i=1:nn
44-
n[i]==k ? (c[i]=v) : nothing
45-
end
41+
for (k,v) in P.c, i=1:nn
42+
n[i]==k && (c[i]=v)
4643
end
4744
c
4845
end
@@ -101,10 +98,10 @@ end
10198
function -{T}(P::FormalPowerSeries{T}, Q::FormalPowerSeries{T})
10299
c = Dict{BigInt, T}()
103100
for (k,v) in P.c
104-
haskey(c,k) ? (c[k]+=v) : (c[k]=v)
101+
c[k] = get(c,k,zero(T)) + v
105102
end
106103
for (k,v) in Q.c
107-
haskey(c,k) ? (c[k]-=v) : (c[k]=-v)
104+
c[k] = get(c,k,zero(T)) - v
108105
end
109106
FormalPowerSeries{T}(c)
110107
end
@@ -132,10 +129,8 @@ end
132129
*{T}(P::FormalPowerSeries{T}, Q::FormalPowerSeries{T}) = CauchyProduct(P, Q)
133130
function CauchyProduct{T}(P::FormalPowerSeries{T}, Q::FormalPowerSeries{T})
134131
c = Dict{BigInt, T}()
135-
for (k1, v1) in P.c
136-
for (k2, v2) in Q.c
137-
haskey(c, k1+k2) ? (c[k1+k2]+=v1*v2) : (c[k1+k2]=v1*v2)
138-
end
132+
for (k1, v1) in P.c, (k2, v2) in Q.c
133+
c[k1+k2]=get(c, k1+k2, zero(T))+v1*v2
139134
end
140135
FormalPowerSeries{T}(c)
141136
end
@@ -170,7 +165,7 @@ isnonunit{T}(P::FormalPowerSeries{T}) = (!haskey(P.c, 0) || P.c[0]==0) && !isuni
170165
#upper triangular Toeplitz matrix
171166
#This constructs the dense matrix - Toeplitz matrices don't exist in Julia yet
172167
function MatrixForm{T}(P::FormalPowerSeries{T}, m :: Integer)
173-
m<0 ? error(sprintf("Invalid matrix dimension %d requested", m)) : true
168+
m<0 && error("Invalid matrix dimension $m requested")
174169
M=zeros(T, m, m)
175170
for (k,v) in P.c
176171
if k < m
@@ -215,7 +210,7 @@ end
215210
function derivative{T}(P::FormalPowerSeries{T})
216211
c = Dict{BigInt, T}()
217212
for (k,v) in P.c
218-
if k != 0 && v != 0
213+
if k != 0 && v != 0
219214
c[k-1] = k*v
220215
end
221216
end
@@ -260,14 +255,14 @@ end
260255

261256
#[H, p.45]
262257
function isalmostunit{T}(P::FormalPowerSeries{T})
263-
(haskey(P.c, 0) && P.c[0]!=0) ? (return false) : true
258+
(haskey(P.c, 0) && P.c[0]!=0) ? (return false) :
264259
(haskey(P.c, 1) && P.c[1]!=0) ? (return true) : (return false)
265260
end
266261

267262

268263
# Reversion of a series (inverse with respect to composition)
269264
# P^[-1]
270-
# [H. Sec.1.7, p.47, but more succintly stated on p.55]
265+
# [H. Sec.1.7, p.47, but more succinctly stated on p.55]
271266
# Constructs the upper left nxn subblock of the matrix representation
272267
# and inverts it
273268
function reversion{T}(P::FormalPowerSeries{T}, n :: Integer)
@@ -280,7 +275,7 @@ function reversion{T}(P::FormalPowerSeries{T}, n :: Integer)
280275
Q = P
281276
ai = tovector(Q, n) #Extract coefficients P[1]...P[n]
282277
A[i,:] = ai
283-
i<n ? Q *= P : nothing
278+
i<n && (Q *= P)
284279
end
285280

286281
#TODO I just need the first row of the inverse

0 commit comments

Comments
 (0)