Skip to content

Commit 59356e5

Browse files
committed
minor changes to chapter 1 demos for speed and updates to julia0.2
1 parent ea750f9 commit 59356e5

File tree

4 files changed

+69
-40
lines changed

4 files changed

+69
-40
lines changed

demos/book/1/bellcurve.jl

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
t=1000000
1010
dx=.2
1111
v=randn(t)
12-
grid=[-4:dx:4]
13-
count=hist(v,grid)/(t*dx)
12+
grid=-4:dx:4
13+
count=hist(v,grid)[2]/(t*dx)
1414

1515
## Theory
1616
x=grid
@@ -19,8 +19,13 @@ y=exp(-x.^2/2)/sqrt(2*pi)
1919
## Plot
2020
using Winston
2121
p = FramedPlot()
22-
h = Histogram(count, dx)
23-
h.x0 = grid[1]
22+
h = Histogram(count, step(grid))
23+
h.x0 = first(grid)
2424
add(p, h)
2525
add(p, Curve(x, y, "color", "blue", "linewidth", 2))
26-
file(p, "bellcurve.png")
26+
if isinteractive()
27+
Winston.display(p)
28+
else
29+
file(p, "bellcurve.png")
30+
end
31+

demos/book/1/largeeig.jl

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,40 @@
77

88
include("tracywidom.jl")
99

10-
## Parameters
11-
n=100 # matrix size
12-
t=5000 # trials
13-
v=[] # eigenvalue samples
14-
dx=.2 # binsize
15-
grid=[-5:dx:2]
16-
## Experiment
17-
for i=1:t
18-
a=randn(n,n)+im*randn(n,n) # random nxn complex matrix
19-
s=(a+a')/2 # Hermitian matrix
20-
v=[v; max(eig(s)[1])] # Largest Eigenvalue
21-
end
22-
v=n^(1/6)*(v-2*sqrt(n)) # normalized eigenvalues
10+
function largeeig_experiment(
11+
## Parameters
12+
n=100, # matrix size
13+
t=5000, # trials
14+
dx=.2, # binsize
15+
)
16+
## Experiment
17+
v=Float64[] # eigenvalue samples
18+
for i=1:t
19+
a=randn(n,n)+im*randn(n,n) # random nxn complex matrix
20+
s=(a+a')/2 # Hermitian matrix
21+
push!(v,max(eig(s)[1])) # Largest Eigenvalue
22+
end
23+
v=n^(1/6)*(v-2*sqrt(n)) # normalized eigenvalues
2324

24-
count=hist(v,grid)/(t*dx)
25+
grid=-5:dx:2
26+
count=hist(v,grid)[2]/(t*dx)
27+
(grid, count)
28+
end
29+
(grid, count) = largeeig_experiment()
2530

2631
## Theory
2732
(t, f) = tracywidom(5., -8.)
2833

2934
## Plot
3035
using Winston
3136
p = FramedPlot()
32-
h = Histogram(count, dx)
33-
h.x0 = grid[1]
37+
h = Histogram(count, step(grid))
38+
h.x0 = first(grid)
3439
add(p, h)
35-
add(p, Curve(t+5, f, "linewidth", 2, "color", "blue"))
36-
file(p, "largeeig.png")
40+
add(p, Curve(t, f, "linewidth", 2, "color", "blue"))
41+
if isinteractive()
42+
Winston.display(p)
43+
else
44+
file(p, "largeeig.png")
45+
end
3746

demos/book/1/semicircle.jl

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,39 @@
44
#Experiment: Sample random symmetric Gaussian matrices
55
#Plot: Histogram of the eigenvalues
66
#Theory: Semicircle as n->infinity
7-
## Parameters
8-
n=1000 # matrix size
9-
t=10 # trials
10-
v=[] # eigenvalue samples
11-
dx=.2 # binsize
12-
grid=[-2.4:dx:2.4]
13-
## Experiment
14-
for i = 1:t
15-
a=randn(n,n) # n by n matrix of random Gaussians
16-
s=(a+a')/sqrt(2*n)# symmetrize and normalize matrix
17-
v=[v;eig(s)[1]] # eigenvalues
7+
8+
function semicircle_experiment(
9+
## Parameters
10+
n=1000, # matrix size
11+
t=10, # trials
12+
dx=.2, # binsize
13+
)
14+
## Experiment
15+
v=Float64[] # eigenvalue samples
16+
for i = 1:t
17+
a=randn(n,n) # n by n matrix of random Gaussians
18+
s=(a+a')/sqrt(2*n)# symmetrize and normalize matrix
19+
append!(v,eig(s)[1])# eigenvalues
20+
end
21+
grid=-2.4:dx:2.4
22+
count=hist(v,grid)[2]/(n*t*dx)
23+
(grid, count)
1824
end
19-
count=hist(grid)/(n*t*dx)
25+
grid,count = semicircle_experiment()
26+
2027
## Theory
2128
x=[-2:dx:2]
2229
y=sqrt(4-x.^2)/(2*pi)
2330

2431
## Plot
2532
using Winston
2633
p = FramedPlot()
27-
h = Histogram(count, dx)
28-
h.x0 = grid[1]
34+
h = Histogram(count, step(grid))
35+
h.x0 = first(grid)
2936
add(p, h)
30-
add(p, Curve(x+2.4+dx/2, y, "color", "blue", "linewidth", 2))
31-
file(p, "semicircle.png")
37+
add(p, Curve(x, y, "color", "blue", "linewidth", 2))
38+
if isinteractive()
39+
Winston.display(p)
40+
else
41+
file(p, "semicircle.png")
42+
end

demos/book/1/tracywidom.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,9 @@ end
2828
using Winston
2929
p = FramedPlot()
3030
add(p, Curve(t, f2, "linewidth", 2))
31-
file(p, "tracywidom.png")
31+
if isinteractive()
32+
Winston.display(p)
33+
else
34+
file(p, "tracywidom.png")
35+
end
3236

0 commit comments

Comments
 (0)