Skip to content

Commit 049e2e5

Browse files
committed
shorter lines
1 parent 91e42ed commit 049e2e5

File tree

5 files changed

+106
-92
lines changed

5 files changed

+106
-92
lines changed

src/chapter08.jl

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
"""
2-
poweriter(A,numiter)
2+
poweriter(A,numiter)
33
4-
Perform `numiter` power iterations with the matrix `A`, starting from a random vector,
5-
and return a vector of eigenvalue estimates and the final eigenvector approximation.
4+
Perform `numiter` power iterations with the matrix `A`, starting
5+
from a random vector, and return a vector of eigenvalue estimates
6+
and the final eigenvector approximation.
67
"""
78
function poweriter(A,numiter)
89
n = size(A,1)
@@ -19,11 +20,11 @@ function poweriter(A,numiter)
1920
end
2021

2122
"""
22-
inviter(A,s,numiter)
23+
inviter(A,s,numiter)
2324
24-
Perform `numiter` inverse iterations with the matrix `A` and shift `s`, starting
25-
from a random vector, and return a vector of eigenvalue estimates and the final
26-
eigenvector approximation.
25+
Perform `numiter` inverse iterations with the matrix `A` and shift
26+
`s`, starting from a random vector, and return a vector of
27+
eigenvalue estimates and the final eigenvector approximation.
2728
"""
2829
function inviter(A,s,numiter)
2930
n = size(A,1)
@@ -41,11 +42,11 @@ function inviter(A,s,numiter)
4142
end
4243

4344
"""
44-
arnoldi(A,u,m)
45+
arnoldi(A,u,m)
4546
46-
Perform the Arnoldi iteration for `A` starting with vector `u`, out to the Krylov
47-
subspace of degree `m`. Return the orthonormal basis (`m`+1 columns) and the upper
48-
Hessenberg `H` of size `m`+1 by `m`.
47+
Perform the Arnoldi iteration for `A` starting with vector `u`, out
48+
to the Krylov subspace of degree `m`. Return the orthonormal basis
49+
(`m`+1 columns) and the upper Hessenberg `H` of size `m`+1 by `m`.
4950
"""
5051
function arnoldi(A,u,m)
5152
n = length(u)
@@ -69,11 +70,11 @@ function arnoldi(A,u,m)
6970
end
7071

7172
"""
72-
arngmres(A,b,m)
73+
arngmres(A,b,m)
7374
74-
Do `m` iterations of GMRES for the linear system `A`*x=`b`. Return the final solution
75-
estimate x and a vector with the history of residual norms. (This function is for
76-
demo only, not practical use.)
75+
Do `m` iterations of GMRES for the linear system `A`*x=`b`. Return
76+
the final solution estimate x and a vector with the history of
77+
residual norms. (This function is for demo only, not practical use.)
7778
"""
7879
function arngmres(A,b,m)
7980
n = length(b)
@@ -105,10 +106,13 @@ function arngmres(A,b,m)
105106
return x,residual
106107
end
107108

109+
# These are not part of the text proper, but replace a MATLAB
110+
# function to generate random sparse symmetric matrices.
111+
108112
function sprandsym(n,density,lambda::Vector)
109113
function randjr(A)
110114
# Random Jacobi rotation similarity transformation.
111-
theta = 2pi*rand()
115+
theta = 2π*rand()
112116
c,s = cos(theta),sin(theta)
113117
i,j = rand(1:n,2)
114118
A[[i,j],:] = [c s;-s c]*A[[i,j],:]

src/chapter09.jl

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"""
2-
polyinterp(t,y)
2+
polyinterp(t,y)
33
4-
Return a callable polynomial interpolant through the points in vectors `t`,`y`. Uses
5-
the barycentric interpolation formula.
4+
Return a callable polynomial interpolant through the points in
5+
vectors `t`,`y`. Uses the barycentric interpolation formula.
66
"""
77
function polyinterp(t,y)
88
n = length(t)-1
@@ -33,9 +33,10 @@ function polyinterp(t,y)
3333
end
3434

3535
"""
36-
triginterp(t,y)
36+
triginterp(t,y)
3737
38-
Return trigonometric interpolant for points defined by vectors `t` and `y`.
38+
Return trigonometric interpolant for points defined by vectors `t`
39+
and `y`.
3940
"""
4041
function triginterp(t,y)
4142
N = length(t)
@@ -59,10 +60,11 @@ function triginterp(t,y)
5960
end
6061

6162
"""
62-
ccint(f,n)
63+
ccint(f,n)
6364
64-
Perform Clenshaw-Curtis integration for the function `f` on `n`+1 nodes in [-1,1]. Return
65-
integral and a vector of the nodes used. Note: `n` must be even.
65+
Perform Clenshaw-Curtis integration for the function `f` on `n`+1
66+
nodes in [-1,1]. Return integral and a vector of the nodes used.
67+
Note: `n` must be even.
6668
"""
6769
function ccint(f,n)
6870
# Find Chebyshev extreme nodes.
@@ -82,10 +84,10 @@ function ccint(f,n)
8284
end
8385

8486
"""
85-
glint(f,n)
87+
glint(f,n)
8688
87-
Perform Gauss-Legendre integration for the function `f` on `n` nodes in (-1,1). Return
88-
integral and a vector of the nodes used.
89+
Perform Gauss-Legendre integration for the function `f` on `n` nodes
90+
in (-1,1). Return integral and a vector of the nodes used.
8991
"""
9092
function glint(f,n)
9193
# Nodes and weights are found via a tridiagonal eigenvalue problem.
@@ -102,11 +104,11 @@ function glint(f,n)
102104
end
103105

104106
"""
105-
intde(f,h,M)
107+
intde(f,h,M)
106108
107-
Perform doubly-exponential integration of function `f` over (-Inf,Inf), using
108-
discretization size `h` and truncation point `M`. Return integral and a vector of the
109-
nodes used.
109+
Perform doubly-exponential integration of function `f` over
110+
(-Inf,Inf), using discretization size `h` and truncation point `M`.
111+
Return integral and a vector of the nodes used.
110112
"""
111113
function intde(f,h,M)
112114
# Find where to truncate the trapezoid sum.
@@ -122,11 +124,11 @@ function intde(f,h,M)
122124
end
123125

124126
"""
125-
intsing(f,h,delta)
127+
intsing(f,h,delta)
126128
127-
Integrate function `f` (possibly singular at 1 and -1) over [-1+`delta`,1-`delta`]
128-
using discretization size `h`. Return integral and a vector of the
129-
nodes used.
129+
Integrate function `f` (possibly singular at 1 and -1) over
130+
[-1+`delta`,1-`delta`] using discretization size `h`. Return
131+
integral and a vector of the nodes used.
130132
"""
131133
function intsing(f,h,delta)
132134
# Find where to truncate the trapezoid sum.

src/chapter10.jl

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
"""
22
shoot(phi,xspan,lval,lder,rval,rder,init)
33
4-
Use the shooting method to solve a two-point boundary value problem. The ODE is
5-
u'' = `phi`(x,u,u') for x in `xspan`. Specify a function value or derivative at
6-
the left endpoint using `lval` and `lder`, respectively, and similarly for the
7-
right endpoint using `rval` and `rder`. (Use an empty array to denote an
8-
unknown quantity.) The value `init` is an initial guess for whichever value is
9-
missing at the left endpoint.
10-
11-
Return vectors for the nodes, the values of u, and the values of u'.
12-
"""
4+
Use the shooting method to solve a two-point boundary value problem.
5+
The ODE is u'' = `phi`(x,u,u') for x in `xspan`. Specify a function
6+
value or derivative at the left endpoint using `lval` and `lder`,
7+
respectively, and similarly for the right endpoint using `rval` and
8+
`rder`. (Use an empty array to denote an unknown quantity.) The
9+
value `init` is an initial guess for whichever value is missing at
10+
the left endpoint.
11+
12+
Returns vectors for the nodes, the values of u, and the values of
13+
u'.
14+
"""
1315
function shoot(phi,xspan,lval,lder,rval,rder,init)
1416

1517
# Tolerances for IVP solver and rootfinder.
@@ -46,11 +48,11 @@ function shoot(phi,xspan,lval,lder,rval,rder,init)
4648
end
4749

4850
"""
49-
diffmat2(n,xspan)
51+
diffmat2(n,xspan)
5052
51-
Compute 2nd-order-accurate differentiation matrices on `n`+1 points in the
52-
interval `xspan`. Return a vector of nodes, and the matrices for the first
53-
and second derivatives.
53+
Compute 2nd-order-accurate differentiation matrices on `n`+1 points
54+
in the interval `xspan`. Return a vector of nodes, and the matrices
55+
for the first and second derivatives.
5456
"""
5557
function diffmat2(n,xspan)
5658
a,b = xspan
@@ -79,11 +81,11 @@ function diffmat2(n,xspan)
7981
end
8082

8183
"""
82-
diffcheb(n,xspan)
84+
diffcheb(n,xspan)
8385
8486
Compute Chebyshev differentiation matrices on `n`+1 points in the
85-
interval `xspan`. Return a vector of nodes, and the matrices for the first
86-
and second derivatives.
87+
interval `xspan`. Return a vector of nodes, and the matrices for the
88+
first and second derivatives.
8789
"""
8890
function diffcheb(n,xspan)
8991
x = [ -cos( k*pi/n ) for k=0:n ] # nodes in [-1,1]
@@ -110,14 +112,14 @@ function diffcheb(n,xspan)
110112
end
111113

112114
"""
113-
bvplin(p,q,r,xspan,lval,rval,n)
115+
bvplin(p,q,r,xspan,lval,rval,n)
114116
115-
Use finite differences to solve a linear bopundary value problem. The ODE is
116-
u''+`p`(x)u'+`q`(x)u = `r`(x) on the interval `xspan`, with endpoint function
117-
values given as `lval` and `rval`. There will be `n`+1 equally spaced nodes,
118-
including the endpoints.
117+
Use finite differences to solve a linear bopundary value problem.
118+
The ODE is u''+`p`(x)u'+`q`(x)u = `r`(x) on the interval `xspan`,
119+
with endpoint function values given as `lval` and `rval`. There will
120+
be `n`+1 equally spaced nodes, including the endpoints.
119121
120-
Return vectors of the nodes and the solution values.
122+
Returns vectors of the nodes and the solution values.
121123
"""
122124
function bvplin(p,q,r,xspan,lval,rval,n)
123125
x,Dx,Dxx = diffmat2(n,xspan)
@@ -138,16 +140,17 @@ function bvplin(p,q,r,xspan,lval,rval,n)
138140
end
139141

140142
"""
141-
bvp(phi,xspan,lval,lder,rval,rder,init)
143+
bvp(phi,xspan,lval,lder,rval,rder,init)
142144
143-
Use finite differences to solve a two-point boundary value problem. The ODE is
144-
u'' = `phi`(x,u,u') for x in `xspan`. Specify a function value or derivative at
145-
the left endpoint using `lval` and `lder`, respectively, and similarly for the
146-
right endpoint using `rval` and `rder`. (Use an empty array to denote an
147-
unknown quantity.) The value `init` is an initial guess for whichever value is
148-
missing at the left endpoint.
145+
Use finite differences to solve a two-point boundary value problem.
146+
The ODE is u'' = `phi`(x,u,u') for x in `xspan`. Specify a function
147+
value or derivative at the left endpoint using `lval` and `lder`,
148+
respectively, and similarly for the right endpoint using `rval` and
149+
`rder`. (Use an empty array to denote an unknown quantity.) The
150+
value `init` is an initial guess for whichever value is missing at
151+
the left endpoint.
149152
150-
Return vectors for the nodes and the values of u.
153+
Returns vectors for the nodes and the values of u.
151154
"""
152155
function bvp(phi,xspan,lval,lder,rval,rder,init)
153156
n = length(init) - 1
@@ -172,12 +175,12 @@ function bvp(phi,xspan,lval,lder,rval,rder,init)
172175
end
173176

174177
"""
175-
fem(c,s,f,a,b,n)
178+
fem(c,s,f,a,b,n)
176179
177-
Use a piecewise linear finite element method to solve a two-point boundary
178-
value problem. The ODE is (`c`(x)u')' + `s`(x)u = `f`(x) on the interval
179-
[`a`,`b`], and the boundary values are zero. The discretization uses `n` equal
180-
subintervals.
180+
Use a piecewise linear finite element method to solve a two-point
181+
boundary value problem. The ODE is (`c`(x)u')' + `s`(x)u = `f`(x) on
182+
the interval [`a`,`b`], and the boundary values are zero. The
183+
discretization uses `n` equal subintervals.
181184
182185
Return vectors for the nodes and the values of u.
183186
"""

src/chapter11.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
"""
2-
diffper(n,xspan)
2+
diffper(n,xspan)
33
4-
Construct 2nd-order differentiation matrices for functions with periodic end
5-
conditions, using `n` unique nodes in the interval `xspan`. Return a vector of
6-
nodes and the matrices for the first and second derivatives.
4+
Construct 2nd-order differentiation matrices for functions with
5+
periodic end conditions, using `n` unique nodes in the interval
6+
`xspan`. Return a vector of nodes and the matrices for the first
7+
and second derivatives.
78
"""
89
function diffper(n,xspan)
910
a,b = xspan

src/chapter13.jl

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
"""
2-
ndgrid(x,y,...)
2+
ndgrid(x,y,...)
33
4-
For d vector inputs, return d matrices representing the coordinate functions on the
5-
tensor product grid.
4+
For d vector inputs, return d matrices representing the coordinate
5+
functions on the tensor product grid.
66
"""
77
function ndgrid(x...)
88
I = CartesianIndices( fill(undef,length.(x)) )
99
[ [ x[d][i[d]] for i in I] for d = 1:length(x) ]
1010
end
1111

1212
"""
13-
rectdisc(m,xspan,n,yspan)
13+
rectdisc(m,xspan,n,yspan)
1414
15-
Create matrices and helpers for finite-difference discretization of a rectangle that is
16-
the tensor product of intervals `xspan` and `yspan`, using `m`+1 and `n`+1 points in
17-
the two coordinates.
15+
Create matrices and helpers for finite-difference discretization of
16+
a rectangle that is the tensor product of intervals `xspan` and
17+
`yspan`, using `m`+1 and `n`+1 points in the two coordinates.
1818
"""
1919
function rectdisc(m,xspan,n,yspan)
2020
# Initialize grid and finite differences.
@@ -40,14 +40,16 @@ function rectdisc(m,xspan,n,yspan)
4040
end
4141

4242
"""
43-
poissonfd(f,g,m,xspan,n,yspan)
43+
poissonfd(f,g,m,xspan,n,yspan)
4444
45-
Solve Poisson's equation on a rectangle by finite differences. Function `f` is the
46-
forcing function and function `g` gives the Dirichlet boundary condition. The rectangle
47-
is the tensor product of intervals `xspan` and `yspan`, and the discretization uses
48-
`m`+1 and `n`+1 points in the two coordinates.
45+
Solve Poisson's equation on a rectangle by finite differences.
46+
Function `f` is the forcing function and function `g` gives the
47+
Dirichlet boundary condition. The rectangle is the tensor product of
48+
intervals `xspan` and `yspan`, and the discretization uses `m`+1
49+
and `n`+1 points in the two coordinates.
4950
50-
Return matrices of the solution values, and the coordinate functions, on the grid.
51+
Returns matrices of the solution values, and the coordinate
52+
functions, on the grid.
5153
"""
5254
function poissonfd(f,g,m,xspan,n,yspan)
5355
# Initialize the rectangle discretization.
@@ -70,13 +72,15 @@ function poissonfd(f,g,m,xspan,n,yspan)
7072
end
7173

7274
"""
73-
newtonpde(f,g,m,xspan,n,yspan)
75+
newtonpde(f,g,m,xspan,n,yspan)
7476
75-
Newton's method with finite differences to solve the PDE `f`(u,x,y,disc)=0 on the
76-
rectangle `xspan` ``\times`` `yspan`, subject to `g`(x,y)=0 on the boundary. Use `m`+1
77-
points in x by `n`+1 points in y.
77+
Newton's method with finite differences to solve the PDE
78+
`f`(u,x,y,disc)=0 on the rectangle `xspan` ``\times`` `yspan`,
79+
subject to `g`(x,y)=0 on the boundary. Use `m`+1 points in x by
80+
`n`+1 points in y.
7881
79-
Return matrices of the solution values, and the coordinate functions, on the grid.
82+
Returns matrices of the solution values, and the coordinate
83+
functions, on the grid.
8084
"""
8185

8286
function newtonpde(f,g,m,xspan,n,yspan)

0 commit comments

Comments
 (0)