Skip to content

Commit b1098e2

Browse files
committed
Fixes typos and adds new dependency on GSL
1 parent 20f39f5 commit b1098e2

File tree

4 files changed

+49
-21
lines changed

4 files changed

+49
-21
lines changed

REQUIRE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
Catalan
22
Distributions
3+
GSL
4+
ODE
35

src/FormalPowerSeries.jl

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import Base.eye, Base.inv, Base.length,
1313
Base.==, Base.+, Base.-, Base.*, Base..*, Base.^
14-
export FormalPowerSeries, tovector, trim, isunit, isnonunit,
14+
export FormalPowerSeries, fps, tovector, trim, isunit, isnonunit,
1515
MatrixForm, reciprocal, derivative, isconstant, compose,
1616
isalmostunit, FormalLaurentSeries
1717

@@ -33,20 +33,24 @@ type FormalPowerSeries{Coefficients}
3333
FormalPowerSeries{Coefficients}(c)
3434
end
3535
end
36+
#Convenient abbreviation for floats
37+
fps = FormalPowerSeries{Float64}
3638

3739

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)
4244
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
4547
end
4648
end
4749
c
4850
end
4951

52+
tovector(P::FormalPowerSeries, n::Integer)=tovector(P,[1:n])
53+
5054

5155
# Basic housekeeping and properties
5256

@@ -197,12 +201,7 @@ end
197201
function reciprocal{T}(P::FormalPowerSeries{T}, n :: Integer)
198202
n<0 ? error(sprintf("Invalid inverse truncation length %d requested", n)) : true
199203

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
206205
a[1]==0 ? (error("Inverse does not exist")) : true
207206
inv_a1 = T<:Number ? 1.0/a[1] : inv(a[1])
208207

@@ -214,7 +213,6 @@ function reciprocal{T}(P::FormalPowerSeries{T}, n :: Integer)
214213
FormalPowerSeries{T}(b)
215214
end
216215

217-
218216
#Derivative of fps [H. Sec.1.4, p.18]
219217
function derivative{T}(P::FormalPowerSeries{T})
220218
c = Dict{BigInt, T}()
@@ -268,8 +266,36 @@ function isalmostunit{T}(P::FormalPowerSeries{T})
268266
(has(P.c, 1) && P.c[1]!=0) ? (return true) : (return false)
269267
end
270268

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+
271296
###############################
272297
# Formal Laurent series (fLs) #
298+
# [H., Sec. 1.8, p. 51] #
273299
###############################
274300

275301
type FormalLaurentSeries{Coefficients}
@@ -336,9 +362,9 @@ end
336362
#of the original series
337363
#Force reciprocal to exist
338364
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])))
340366
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())))
342368
end
343369
#@assert norm(inv(float(MatrixForm(X,c)))[1, :]'[:, 1] - tovector(reciprocal(X, c),c)) < c*sqrt(eps())
344370

src/densities/Semicircle.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#Wigner semicircle distribution
22

33
importall Distributions
4-
using gsl #needed for hypergeom
54
using Catalan
5+
using GSL #needed for hypergeom
66
export Semicircle
77

88
type Semicircle <: ContinuousUnivariateDistribution
@@ -69,7 +69,7 @@ var(X::Semicircle)=std(X)^2
6969
function moment(X::Semicircle, order::Integer)
7070
a, r = X.mean, X.radius
7171
if X.mean != 0
72-
a^n hypergeom([(1-n)/2, -n/2], 2, (r/a)^2)
72+
a^n*hypergeom([(1-n)/2, -n/2], 2, (r/a)^2)
7373
else
7474
order%2 ? (0.5*r)^(2n) * catalan(div(order,2)) : 0
7575
end
@@ -121,7 +121,7 @@ end
121121
# Use relationship with beta distribution
122122
function rand(X::Semicircle)
123123
Y = rand(Beta(1.5, 1.5))
124-
X.mean + 2 X.radius * Y - X.radius
124+
X.mean + 2 * X.radius * Y - X.radius
125125
end
126126

127127

src/densities/TracyWidom.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ include("gradient.jl")
88
function TracyWidom(t::Real)
99
t0 = -8.0
1010

11-
t>t0 ? return 0.0 : nothing
12-
t>5 ? return 0.0 : nothing
11+
t>t0 ? (return 0.0) : nothing
12+
t>5 ? (return 0.0) : nothing
1313

1414
deq(t, y) = [y[2]; t*y[1]+2y[1]^3; y[4]; y[1]^2]
1515

0 commit comments

Comments
 (0)