Skip to content

Commit df71293

Browse files
haampieandreasnoack
authored andcommitted
Modernize syntax (#153)
* Modernize w/ struct and where * Clean up some imports * Update deprecated / removed exception type and add a test for it * Remove compat * Add row number to SingularException * @fact_throws -> @test_throws
1 parent 27b87a9 commit df71293

19 files changed

+92
-83
lines changed

REQUIRE

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,3 @@ UnicodePlots
44
RecipesBase
55
SugarBLAS
66
LinearMaps
7-
Compat 0.18.0

src/IterativeSolvers.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ module IterativeSolvers
77

88
using SugarBLAS
99

10-
using Compat
11-
1210
include("common.jl")
1311
include("orthogonalize.jl")
1412
include("history.jl")

src/bicgstabl.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export bicgstabl, bicgstabl!, bicgstabl_iterator, bicgstabl_iterator!, BiCGStabI
22

33
import Base: start, next, done
44

5-
type BiCGStabIterable{precT, matT, vecT <: AbstractVector, smallMatT <: AbstractMatrix, realT <: Real, scalarT <: Number}
5+
mutable struct BiCGStabIterable{precT, matT, vecT <: AbstractVector, smallMatT <: AbstractMatrix, realT <: Real, scalarT <: Number}
66
A::matT
77
b::vecT
88
l::Int

src/cg.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Base: start, next, done
22

33
export cg, cg!, CGIterable, PCGIterable, cg_iterator, cg_iterator!
44

5-
type CGIterable{matT, vecT <: AbstractVector, numT <: Real}
5+
mutable struct CGIterable{matT, vecT <: AbstractVector, numT <: Real}
66
A::matT
77
x::vecT
88
b::vecT
@@ -16,7 +16,7 @@ type CGIterable{matT, vecT <: AbstractVector, numT <: Real}
1616
mv_products::Int
1717
end
1818

19-
type PCGIterable{precT, matT, vecT <: AbstractVector, numT <: Real, paramT <: Number}
19+
mutable struct PCGIterable{precT, matT, vecT <: AbstractVector, numT <: Real, paramT <: Number}
2020
Pl::precT
2121
A::matT
2222
x::vecT

src/chebyshev.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Base: next, start, done
22

33
export chebyshev, chebyshev!
44

5-
type ChebyshevIterable{precT, matT, vecT, realT <: Real}
5+
mutable struct ChebyshevIterable{precT, matT, vecT, realT <: Real}
66
Pl::precT
77
A::matT
88
b::vecT

src/common.jl

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import Base: eltype, length, ndims, real, size, *,
2-
A_mul_B!, Ac_mul_B, Ac_mul_B!
1+
import Base: A_ldiv_B!, \
32

4-
using LinearMaps, Compat
3+
using LinearMaps
54

6-
export A_mul_B, Identity
5+
export Identity
76

87
#### Type-handling
98
"""
@@ -47,10 +46,10 @@ solve(A::Function,b) = A(b)
4746

4847
solve(A,b) = A\b
4948

50-
solve!{T}(out::AbstractArray{T},A::Int,b::AbstractArray{T}) = scale!(out,b, 1/A)
49+
solve!(out::AbstractArray{T},A::Int,b::AbstractArray{T}) where {T} = scale!(out,b, 1/A)
5150

52-
solve!{T}(out::AbstractArray{T},A,b::AbstractArray{T}) = A_ldiv_B!(out,A,b)
53-
solve!{T}(out::AbstractArray{T},A::Function,b::AbstractArray{T}) = copy!(out,A(b))
51+
solve!(out::AbstractArray{T},A,b::AbstractArray{T}) where {T} = A_ldiv_B!(out,A,b)
52+
solve!(out::AbstractArray{T},A::Function,b::AbstractArray{T}) where {T} = copy!(out,A(b))
5453

5554
"""
5655
initrand!(v)
@@ -67,17 +66,9 @@ end
6766
_randn!(v::Array{Float64}) = randn!(v)
6867
_randn!(v) = copy!(v, randn(length(v)))
6968

70-
#### Errors
71-
export PosSemidefException
72-
73-
type PosSemidefException <: Exception
74-
msg :: AbstractString
75-
PosSemidefException(msg::AbstractString="Matrix was not positive semidefinite") = new(msg)
76-
end
77-
7869
# Identity preconditioner
79-
immutable Identity end
70+
struct Identity end
8071

81-
Base.:\(::Identity, x) = copy(x)
82-
Base.A_ldiv_B!(::Identity, x) = x
83-
Base.A_ldiv_B!(y, ::Identity, x) = copy!(y, x)
72+
\(::Identity, x) = copy(x)
73+
A_ldiv_B!(::Identity, x) = x
74+
A_ldiv_B!(y, ::Identity, x) = copy!(y, x)

src/factorization.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ and `P` computed by `idfact()`. See the documentation of `idfact()` for details.
1414
1515
\\cite{Cheng2005, Liberty2007}
1616
"""
17-
immutable Interpolative{T} <: Factorization{T}
17+
struct Interpolative{T} <: Factorization{T}
1818
B :: AbstractMatrix{T}
1919
P :: AbstractMatrix{T}
2020
end

src/gmres.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@ import Base: start, next, done
22

33
export gmres, gmres!
44

5-
type ArnoldiDecomp{T, matT}
5+
mutable struct ArnoldiDecomp{T, matT}
66
A::matT
77
V::Matrix{T} # Orthonormal basis vectors
88
H::Matrix{T} # Hessenberg matrix
99
end
1010

11-
ArnoldiDecomp{matT}(A::matT, order::Int, T::Type) = ArnoldiDecomp{T, matT}(
11+
ArnoldiDecomp(A::matT, order::Int, T::Type) where {matT} = ArnoldiDecomp{T, matT}(
1212
A,
1313
zeros(T, size(A, 1), order + 1),
1414
zeros(T, order + 1, order)
1515
)
1616

17-
type Residual{numT, resT}
17+
mutable struct Residual{numT, resT}
1818
current::resT # Current, absolute, preconditioned residual
1919
accumulator::resT # Used to compute the residual on the go
2020
nullvec::Vector{numT} # Vector in the null space of H to compute residuals
@@ -28,7 +28,7 @@ Residual(order, T::Type) = Residual{T, real(T)}(
2828
one(real(T))
2929
)
3030

31-
type GMRESIterable{preclT, precrT, vecT <: AbstractVector, arnoldiT <: ArnoldiDecomp, residualT <: Residual, resT <: Real}
31+
mutable struct GMRESIterable{preclT, precrT, vecT <: AbstractVector, arnoldiT <: ArnoldiDecomp, residualT <: Residual, resT <: Real}
3232
Pl::preclT
3333
Pr::precrT
3434
x::vecT
@@ -172,7 +172,7 @@ function update_residual!(r::Residual, arnoldi::ArnoldiDecomp, k::Int)
172172
r.current = r.β / r.accumulator
173173
end
174174

175-
function init!{T}(arnoldi::ArnoldiDecomp{T}, x, b, Pl, Ax; initially_zero::Bool = false)
175+
function init!(arnoldi::ArnoldiDecomp{T}, x, b, Pl, Ax; initially_zero::Bool = false) where {T}
176176
# Initialize the Krylov subspace with the initial residual vector
177177
# This basically does V[1] = Pl \ (b - A * x) and then normalize
178178

@@ -194,12 +194,12 @@ function init!{T}(arnoldi::ArnoldiDecomp{T}, x, b, Pl, Ax; initially_zero::Bool
194194
β
195195
end
196196

197-
@inline function init_residual!{numT,resT}(r::Residual{numT, resT}, β)
197+
@inline function init_residual!(r::Residual{numT, resT}, β) where {numT,resT}
198198
r.accumulator = one(resT)
199199
r.β = β
200200
end
201201

202-
function solve_least_squares!{T}(arnoldi::ArnoldiDecomp{T}, β, k::Int)
202+
function solve_least_squares!(arnoldi::ArnoldiDecomp{T}, β, k::Int) where {T}
203203
# Compute the least-squares solution to Hy = β e1 via Given's rotations
204204
rhs = zeros(T, k)
205205
rhs[1] = β
@@ -210,14 +210,14 @@ function solve_least_squares!{T}(arnoldi::ArnoldiDecomp{T}, β, k::Int)
210210
rhs
211211
end
212212

213-
function update_solution!{T}(x, y, arnoldi::ArnoldiDecomp{T}, Pr::Identity, k::Int, Ax)
213+
function update_solution!(x, y, arnoldi::ArnoldiDecomp{T}, Pr::Identity, k::Int, Ax) where {T}
214214
# Update x ← x + V * y
215215

216216
# TODO: find the SugarBLAS alternative
217217
BLAS.gemv!('N', one(T), view(arnoldi.V, :, 1 : k - 1), y, one(T), x)
218218
end
219219

220-
function update_solution!{T}(x, y, arnoldi::ArnoldiDecomp{T}, Pr, k::Int, Ax)
220+
function update_solution!(x, y, arnoldi::ArnoldiDecomp{T}, Pr, k::Int, Ax) where {T}
221221
# Computing x ← x + Pr \ (V * y) and use Ax as a work space
222222
A_mul_B!(Ax, view(arnoldi.V, :, 1 : k - 1), y)
223223
A_ldiv_B!(Pr, Ax)

src/hessenberg.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Base.LinAlg: Givens, givensAlgorithm
22
import Base.A_ldiv_B!
33

4-
type Hessenberg{T<:AbstractMatrix}
4+
mutable struct Hessenberg{T<:AbstractMatrix}
55
H::T # H is assumed to be Hessenberg of size (m + 1) × m
66
end
77

src/history.jl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ ploted as series and matrices as scatterplots.
5151
`Base`: `getindex`, `setindex!`, `push!`
5252
5353
"""
54-
type ConvergenceHistory{T,K}
54+
mutable struct ConvergenceHistory{T,K}
5555
mvps::Int
5656
mtvps::Int
5757
iters::Int
@@ -68,22 +68,22 @@ end
6868
"""
6969
Stores information of the current iteration.
7070
"""
71-
@compat const PartialHistory = ConvergenceHistory{false}
71+
const PartialHistory = ConvergenceHistory{false}
7272

7373
"""
7474
Stores the information of all the iterations.
7575
"""
76-
@compat const CompleteHistory = ConvergenceHistory{true}
76+
const CompleteHistory = ConvergenceHistory{true}
7777

7878
"""
7979
History without resets.
8080
"""
81-
@compat const UnrestartedHistory{T} = ConvergenceHistory{T, Void}
81+
const UnrestartedHistory{T} = ConvergenceHistory{T, Void}
8282

8383
"""
8484
History with resets.
8585
"""
86-
@compat const RestartedHistory{T} = ConvergenceHistory{T, Int}
86+
const RestartedHistory{T} = ConvergenceHistory{T, Int}
8787

8888

8989
#############
@@ -256,8 +256,8 @@ nrests(ch::RestartedHistory) = Int(ceil(ch.iters/ch.restart))
256256
Determine whether a collection `x` is plotable. Only vectors and matrices are
257257
such objects.
258258
"""
259-
plotable{T<:Real}(::VecOrMat{T})::Bool = true
260-
plotable(::Any)::Bool = false
259+
plotable(::VecOrMat{T}) where {T <: Real} = true
260+
plotable(::Any) = false
261261

262262
# inner plots
263263

@@ -286,10 +286,10 @@ Build a `UnicodePlot.Plot` object from the plotable collection `x`.
286286
If `x` is a vector, a series will be made. In case of being a matrix an scatterplot
287287
will be returned.
288288
"""
289-
function plot_collection{T<:Real}(vals::Vector{T}, iters::Int, gap::Int;
289+
function plot_collection(vals::Vector{T}, iters::Int, gap::Int;
290290
restarts=Int(ceil(iters/gap)), color::Symbol=:blue, name::AbstractString="",
291291
title::AbstractString="", left::Int=1
292-
)
292+
) where {T <: Real}
293293
maxy = round(maximum(vals),2)
294294
miny = round(minimum(vals),2)
295295
plot = lineplot([left],[miny],xlim=[left,iters],ylim=[miny,maxy],title=title,name=name)
@@ -303,10 +303,10 @@ function plot_collection{T<:Real}(vals::Vector{T}, iters::Int, gap::Int;
303303
end
304304
plot
305305
end
306-
function plot_collection{T<:Real}(vals::Matrix{T}, iters::Int, gap::Int;
306+
function plot_collection(vals::Matrix{T}, iters::Int, gap::Int;
307307
restarts=Int(ceil(iters/gap)), color::Symbol=:blue, name::AbstractString="",
308308
title::AbstractString="", left::Int=1
309-
)
309+
) where {T <: Real}
310310
n = size(vals,2)
311311
maxy = round(maximum(vals),2)
312312
miny = round(minimum(vals),2)

0 commit comments

Comments
 (0)