Skip to content

Commit 4899d21

Browse files
authored
Merge pull request #171 from haampie/fix-maxiter
Make maxiter the size of the matrix
2 parents 46b0525 + d90e350 commit 4899d21

File tree

10 files changed

+33
-27
lines changed

10 files changed

+33
-27
lines changed

src/bicgstabl.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ bicgstabl_iterator(A, b, l; kwargs...) = bicgstabl_iterator!(zerox(A, b), A, b,
2828

2929
function bicgstabl_iterator!(x, A, b, l::Int = 2;
3030
Pl = Identity(),
31-
max_mv_products = min(30, size(A, 1)),
31+
max_mv_products = size(A, 2),
3232
initial_zero = false,
3333
tol = sqrt(eps(real(eltype(b))))
3434
)
@@ -161,7 +161,7 @@ bicgstabl(A, b, l = 2; kwargs...) = bicgstabl!(zerox(A, b), A, b, l; initial_zer
161161
162162
## Keywords
163163
164-
- `max_mv_products::Int = min(30, size(A, 1))`: maximum number of matrix vector products.
164+
- `max_mv_products::Int = size(A, 2)`: maximum number of matrix vector products.
165165
For BiCGStab(l) this is a less dubious term than "number of iterations";
166166
- `Pl = Identity()`: left preconditioner of the method;
167167
- `tol::Real = sqrt(eps(real(eltype(b))))`: tolerance for stopping condition `|r_k| / |r_0| ≤ tol`.
@@ -182,7 +182,7 @@ For BiCGStab(l) this is a less dubious term than "number of iterations";
182182
"""
183183
function bicgstabl!(x, A, b, l = 2;
184184
tol = sqrt(eps(real(eltype(b)))),
185-
max_mv_products::Int = min(20, size(A, 1)),
185+
max_mv_products::Int = size(A, 2),
186186
log::Bool = false,
187187
verbose::Bool = false,
188188
Pl = Identity(),

src/cg.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ end
9696

9797
function cg_iterator!(x, A, b, Pl = Identity();
9898
tol = sqrt(eps(real(eltype(b)))),
99-
maxiter = min(20, length(b)),
99+
maxiter::Int = size(A, 2),
100100
initially_zero::Bool = false
101101
)
102102
u = zeros(x)
@@ -155,7 +155,7 @@ cg(A, b; kwargs...) = cg!(zerox(A, b), A, b; initially_zero = true, kwargs...)
155155
- `Pl = Identity()`: left preconditioner of the method. Should be symmetric,
156156
positive-definite like `A`.
157157
- `tol::Real = sqrt(eps(real(eltype(b))))`: tolerance for stopping condition `|r_k| / |r_0| ≤ tol`;
158-
- `maxiter::Integer = size(A,2)`: maximum number of iterations;
158+
- `maxiter::Int = size(A,2)`: maximum number of iterations;
159159
- `verbose::Bool = false`: print method information;
160160
- `log::Bool = false`: keep track of the residual norm in each iteration;
161161
@@ -177,7 +177,7 @@ cg(A, b; kwargs...) = cg!(zerox(A, b), A, b; initially_zero = true, kwargs...)
177177
"""
178178
function cg!(x, A, b;
179179
tol = sqrt(eps(real(eltype(b)))),
180-
maxiter::Integer = min(20, size(A, 1)),
180+
maxiter::Int = size(A, 2),
181181
log::Bool = false,
182182
verbose::Bool = false,
183183
Pl = Identity(),

src/chebyshev.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ chebyshev_iterable(A, b, λmin::Real, λmax::Real; kwargs...) =
5858
chebyshev_iterable!(zerox(A, b), A, b, λmin, λmax; kwargs...)
5959

6060
function chebyshev_iterable!(x, A, b, λmin::Real, λmax::Real;
61-
tol = sqrt(eps(real(eltype(b)))), maxiter = size(A, 1), Pl = Identity(), initially_zero = false)
61+
tol = sqrt(eps(real(eltype(b)))), maxiter = size(A, 2), Pl = Identity(), initially_zero = false)
6262

6363
λ_avg = (λmax + λmin) / 2
6464
λ_diff = (λmax - λmin) / 2
@@ -117,7 +117,7 @@ Solve Ax = b for symmetric, definite matrices A using Chebyshev iteration.
117117
matrix-vector product can be saved when computing the initial
118118
residual vector;
119119
- `tol`: tolerance for stopping condition `|r_k| / |r_0| ≤ tol`.
120-
- `maxiter::Int`: maximum number of inner iterations of GMRES;
120+
- `maxiter::Int = size(A, 2)`: maximum number of inner iterations of GMRES;
121121
- `Pl = Identity()`: left preconditioner;
122122
- `log::Bool = false`: keep track of the residual norm in each iteration;
123123
- `verbose::Bool = false`: print convergence information during the iterations.
@@ -136,7 +136,7 @@ Solve Ax = b for symmetric, definite matrices A using Chebyshev iteration.
136136
function chebyshev!(x, A, b, λmin::Real, λmax::Real;
137137
Pl = Identity(),
138138
tol::Real=sqrt(eps(real(eltype(b)))),
139-
maxiter::Int=size(A, 1),
139+
maxiter::Int=size(A, 2),
140140
log::Bool=false,
141141
verbose::Bool=false,
142142
initially_zero::Bool=false

src/gmres.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ function gmres_iterable!(x, A, b;
104104
Pl = Identity(),
105105
Pr = Identity(),
106106
tol = sqrt(eps(real(eltype(b)))),
107-
restart::Int = min(20, length(b)),
108-
maxiter::Int = restart,
107+
restart::Int = min(20, size(A, 2)),
108+
maxiter::Int = size(A, 2),
109109
initially_zero::Bool = false
110110
)
111111
T = eltype(x)
@@ -152,8 +152,8 @@ Solves the problem ``Ax = b`` with restarted GMRES.
152152
matrix-vector product can be saved when computing the initial
153153
residual vector;
154154
- `tol`: relative tolerance;
155-
- `restart::Int`: restarts GMRES after specified number of iterations;
156-
- `maxiter::Int`: maximum number of inner iterations of GMRES;
155+
- `restart::Int = min(20, size(A, 2))`: restarts GMRES after specified number of iterations;
156+
- `maxiter::Int = size(A, 2)`: maximum number of inner iterations of GMRES;
157157
- `Pl`: left preconditioner;
158158
- `Pr`: right preconditioner;
159159
- `log::Bool`: keep track of the residual norm in each iteration;
@@ -174,8 +174,8 @@ function gmres!(x, A, b;
174174
Pl = Identity(),
175175
Pr = Identity(),
176176
tol = sqrt(eps(real(eltype(b)))),
177-
restart::Int = min(20, length(b)),
178-
maxiter::Int = restart,
177+
restart::Int = min(20, size(A, 2)),
178+
maxiter::Int = size(A, 2),
179179
log::Bool = false,
180180
initially_zero::Bool = false,
181181
verbose::Bool = false

src/idrs.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ shadow space.
2323
2424
- `s::Integer = 8`: dimension of the shadow space;
2525
- `tol`: relative tolerance;
26-
- `maxiter::Int`: maximum number of iterations;
26+
- `maxiter::Int = size(A, 2)`: maximum number of iterations;
2727
- `log::Bool`: keep track of the residual norm in each iteration;
2828
- `verbose::Bool`: print convergence information during the iterations.
2929
@@ -39,7 +39,7 @@ shadow space.
3939
- `history`: convergence history.
4040
"""
4141
function idrs!(x, A, b;
42-
s = 8, tol=sqrt(eps(real(eltype(b)))), maxiter=length(x)^2,
42+
s = 8, tol=sqrt(eps(real(eltype(b)))), maxiter=size(A, 2),
4343
log::Bool=false, kwargs...
4444
)
4545
history = ConvergenceHistory(partial=!log)

src/lsmr.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ especially if ``A`` is ill-conditioned.
3939
Maximum precision can be obtained by setting
4040
- `atol` = `btol` = `conlim` = zero, but the number of iterations
4141
may then be excessive.
42-
- `maxiter::Integer = min(20,length(b))`: maximum number of iterations.
42+
- `maxiter::Int = maximum(size(A))`: maximum number of iterations.
4343
- `log::Bool`: keep track of the residual norm in each iteration;
4444
- `verbose::Bool`: print convergence information during the iterations.
4545
@@ -65,7 +65,7 @@ especially if ``A`` is ill-conditioned.
6565
- `:resnom` => `::Vector`: residual norm at each iteration.
6666
"""
6767
function lsmr!(x, A, b;
68-
maxiter::Integer = max(size(A,1), size(A,2)),
68+
maxiter::Int = maximum(size(A)),
6969
log::Bool=false, kwargs...
7070
)
7171
history = ConvergenceHistory(partial=!log)
@@ -87,7 +87,7 @@ end
8787

8888
function lsmr_method!(log::ConvergenceHistory, x, A, b, v, h, hbar;
8989
atol::Number = 1e-6, btol::Number = 1e-6, conlim::Number = 1e8,
90-
maxiter::Integer = max(size(A,1), size(A,2)), λ::Number = 0,
90+
maxiter::Int = maximum(size(A)), λ::Number = 0,
9191
verbose::Bool=false
9292
)
9393
verbose && @printf("=== lsmr ===\n%4s\t%7s\t\t%7s\t\t%7s\n","iter","anorm","cnorm","rnorm")

src/lsqr.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ especially if A is ill-conditioned.
3737
Maximum precision can be obtained by setting
3838
`atol` = `btol` = `conlim` = zero, but the number of iterations
3939
may then be excessive.
40-
- `maxiter::Integer = min(20,length(b))`: maximum number of iterations.
40+
- `maxiter::Int = maximum(size(A))`: maximum number of iterations.
4141
- `verbose::Bool = false`: print method information.
4242
- `log::Bool = false`: output an extra element of type `ConvergenceHistory`
4343
containing extra information of the method execution.
@@ -64,7 +64,7 @@ especially if A is ill-conditioned.
6464
- `:resnom` => `::Vector`: residual norm at each iteration.
6565
"""
6666
function lsqr!(x, A, b;
67-
maxiter::Int=max(size(A,1), size(A,2)), log::Bool=false,
67+
maxiter::Int=maximum(size(A)), log::Bool=false,
6868
kwargs...
6969
)
7070
T = Adivtype(A, b)
@@ -90,7 +90,7 @@ end
9090
function lsqr_method!(log::ConvergenceHistory, x, A, b;
9191
damp=0, atol=sqrt(eps(real(Adivtype(A,b)))), btol=sqrt(eps(real(Adivtype(A,b)))),
9292
conlim=real(one(Adivtype(A,b)))/sqrt(eps(real(Adivtype(A,b)))),
93-
maxiter::Int=max(size(A,1), size(A,2)), verbose::Bool=false,
93+
maxiter::Int=maximum(size(A)), verbose::Bool=false,
9494
)
9595
verbose && @printf("=== lsqr ===\n%4s\t%7s\t\t%7s\t\t%7s\t\t%7s\n","iter","resnorm","anorm","cnorm","rnorm")
9696
# Sanity-checking

src/minres.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function minres_iterable!(x, A, b;
4242
initially_zero::Bool = false,
4343
skew_hermitian::Bool = false,
4444
tol = sqrt(eps(real(eltype(b)))),
45-
maxiter = size(A, 1)
45+
maxiter = size(A, 2)
4646
)
4747
T = eltype(x)
4848
HessenbergT = skew_hermitian ? T : real(T)
@@ -173,7 +173,7 @@ Solve Ax = b for (skew-)Hermitian matrices A using MINRES.
173173
residual vector;
174174
- `skew_hermitian::Bool = false`: if `true` assumes that `A` is skew-symmetric or skew-Hermitian;
175175
- `tol`: tolerance for stopping condition `|r_k| / |r_0| ≤ tol`. Note that the residual is computed only approximately;
176-
- `maxiter::Int`: maximum number of inner iterations of GMRES;
176+
- `maxiter::Int = size(A, 2)`: maximum number of iterations;
177177
- `Pl`: left preconditioner;
178178
- `Pr`: right preconditioner;
179179
- `log::Bool = false`: keep track of the residual norm in each iteration;
@@ -195,7 +195,7 @@ function minres!(x, A, b;
195195
verbose::Bool = false,
196196
log::Bool = false,
197197
tol = sqrt(eps(real(eltype(b)))),
198-
maxiter::Int = min(30, size(A, 1)),
198+
maxiter::Int = size(A, 2),
199199
initially_zero::Bool = false
200200
)
201201
history = ConvergenceHistory(partial = !log)

test/idrs.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ srand(1234567)
2828
end
2929

3030
@testset "SparseMatrixCSC{$T}" for T in (Float64, Complex128)
31-
A = sprand(T, n, n, 0.5) + I
31+
A = sprand(T, n, n, 0.5) + n * I
3232
b = rand(T, n)
3333
tol = eps(real(T))
3434

test/lsmr.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ end
4545

4646
eltype(A::DampenedMatrix) = promote_type(eltype(A.A), eltype(A.diagonal))
4747

48+
function size(A::DampenedMatrix)
49+
m, n = size(A.A)
50+
l = length(A.diagonal)
51+
(m + l, n)
52+
end
53+
4854
function size(A::DampenedMatrix, dim::Integer)
4955
m, n = size(A.A)
5056
l = length(A.diagonal)

0 commit comments

Comments
 (0)