Skip to content

Commit 08cc5f2

Browse files
committed
adopt to v1.6+ syntax and features
1 parent 6ef9abb commit 08cc5f2

File tree

9 files changed

+60
-91
lines changed

9 files changed

+60
-91
lines changed

src/LinearMaps.jl

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,7 @@ using LinearAlgebra
88
import LinearAlgebra: mul!
99
using SparseArrays
1010

11-
if VERSION < v"1.2-"
12-
import Base: has_offset_axes
13-
require_one_based_indexing(A...) = !has_offset_axes(A...) || throw(ArgumentError("offset arrays are not supported but got an array with index other than 1"))
14-
else
15-
import Base: require_one_based_indexing
16-
end
11+
using Base: require_one_based_indexing
1712

1813
abstract type LinearMap{T} end
1914

@@ -33,11 +28,7 @@ MulStyle(::ThreeArg, ::FiveArg) = ThreeArg()
3328
MulStyle(::FiveArg, ::ThreeArg) = ThreeArg()
3429
MulStyle(::ThreeArg, ::ThreeArg) = ThreeArg()
3530
MulStyle(::LinearMap) = ThreeArg() # default
36-
@static if VERSION v"1.3.0-alpha.115"
37-
MulStyle(::AbstractVecOrMat) = FiveArg()
38-
else
39-
MulStyle(::AbstractVecOrMat) = ThreeArg()
40-
end
31+
MulStyle(::AbstractVecOrMat) = FiveArg()
4132
MulStyle(A::LinearMap, As::LinearMap...) = MulStyle(MulStyle(A), MulStyle(As...))
4233

4334
Base.isreal(A::LinearMap) = eltype(A) <: Real
@@ -115,9 +106,8 @@ function Base.:(*)(A::LinearMap, x::AbstractVector)
115106
y = similar(x, T, axes(A)[1])
116107
return mul!(y, A, x)
117108
end
118-
if VERSION v"1.3"
119-
(L::LinearMap)(x::AbstractVector) = L*x
120-
end
109+
110+
(L::LinearMap)(x::AbstractVector) = L*x
121111

122112
"""
123113
mul!(Y::AbstractVecOrMat, A::LinearMap, B::AbstractVector) -> Y
@@ -223,13 +213,9 @@ function mul!(Y::AbstractMatrix, A::LinearMap, X::AbstractMatrix, α::Number, β
223213
end
224214

225215
function _generic_mapmat_mul!(Y, A, X, α=true, β=false)
226-
@views for i in 1:size(X, 2)
227-
_unsafe_mul!(Y[:, i], A, X[:, i], α, β)
216+
for (Xi, Yi) in zip(eachcol(X), eachcol(Y))
217+
mul!(Yi, A, Xi, α, β)
228218
end
229-
# starting from Julia v1.1, we could use the `eachcol` iterator
230-
# for (Xi, Yi) in zip(eachcol(X), eachcol(Y))
231-
# mul!(Yi, A, Xi, α, β)
232-
# end
233219
return Y
234220
end
235221

src/composition.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ struct CompositeMap{T, As<:LinearMapTuple} <: LinearMap{T}
55
for n in 2:N
66
check_dim_mul(maps[n], maps[n-1])
77
end
8-
for TA in Base.Generator(eltype, maps)
9-
# like lazy map; could use Base.Iterators.map in Julia >= 1.6
8+
for TA in Base.Iterators.map(eltype, maps)
109
promote_type(T, TA) == T ||
1110
error("eltype $TA cannot be promoted to $T in CompositeMap constructor")
1211
end

src/kronecker.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
struct KroneckerMap{T, As<:LinearMapTuple} <: LinearMap{T}
22
maps::As
33
function KroneckerMap{T}(maps::LinearMapTuple) where {T}
4-
for TA in Base.Generator(eltype, maps)
4+
for TA in Base.Iterators.map(eltype, maps)
55
promote_type(T, TA) == T ||
66
error("eltype $TA cannot be promoted to $T in KroneckerMap constructor")
77
end
@@ -221,7 +221,7 @@ struct KroneckerSumMap{T, As<:Tuple{LinearMap, LinearMap}} <: LinearMap{T}
221221
A1, A2 = maps
222222
(size(A1, 1) == size(A1, 2) && size(A2, 1) == size(A2, 2)) ||
223223
throw(ArgumentError("operators need to be square in Kronecker sums"))
224-
for TA in Base.Generator(eltype, maps)
224+
for TA in Base.Iterators.map(eltype, maps)
225225
promote_type(T, TA) == T ||
226226
error("eltype $TA cannot be promoted to $T in KroneckerSumMap constructor")
227227
end

src/linearcombination.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ MulStyle(A::LinearCombination) = MulStyle(A.maps...)
1818

1919
# basic methods
2020
Base.size(A::LinearCombination) = size(A.maps[1])
21-
Base.axes(A::LinearMaps.LinearCombination) = axes(A.maps[1])
21+
Base.axes(A::LinearCombination) = axes(A.maps[1])
2222
# following conditions are sufficient but not necessary
2323
LinearAlgebra.issymmetric(A::LinearCombination) = all(issymmetric, A.maps)
2424
LinearAlgebra.ishermitian(A::LinearCombination) = all(ishermitian, A.maps)

src/wrappedmap.jl

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -87,40 +87,38 @@ mul!(Y::AbstractMatrix, X::AbstractMatrix, A::VecOrMatMap) = mul!(Y, X, A.lmap)
8787
# the following method is needed for disambiguation with left-multiplication
8888
mul!(Y::AbstractMatrix, X::TransposeAbsVecOrMat, A::VecOrMatMap) = mul!(Y, X, A.lmap)
8989

90-
if VERSION v"1.3.0-alpha.115"
91-
for (In, Out) in ((AbstractVector, AbstractVecOrMat), (AbstractMatrix, AbstractMatrix))
92-
@eval begin
93-
function _unsafe_mul!(y::$Out, A::WrappedMap, x::$In, α::Number, β::Number)
94-
return _unsafe_mul!(y, A.lmap, x, α, β)
95-
end
96-
function _unsafe_mul!(y::$Out, At::TransposeMap{<:Any,<:WrappedMap}, x::$In,
97-
α::Number, β::Number)
98-
A = At.lmap
99-
return (issymmetric(A) || (isreal(A) && ishermitian(A))) ?
100-
_unsafe_mul!(y, A.lmap, x, α, β) :
101-
_unsafe_mul!(y, transpose(A.lmap), x, α, β)
102-
end
103-
function _unsafe_mul!(y::$Out, Ac::AdjointMap{<:Any,<:WrappedMap}, x::$In, α::Number, β::Number)
104-
A = Ac.lmap
105-
return ishermitian(A) ?
106-
_unsafe_mul!(y, A.lmap, x, α, β) :
107-
_unsafe_mul!(y, adjoint(A.lmap), x, α, β)
108-
end
90+
for (In, Out) in ((AbstractVector, AbstractVecOrMat), (AbstractMatrix, AbstractMatrix))
91+
@eval begin
92+
function _unsafe_mul!(y::$Out, A::WrappedMap, x::$In, α::Number, β::Number)
93+
return _unsafe_mul!(y, A.lmap, x, α, β)
94+
end
95+
function _unsafe_mul!(y::$Out, At::TransposeMap{<:Any,<:WrappedMap}, x::$In,
96+
α::Number, β::Number)
97+
A = At.lmap
98+
return (issymmetric(A) || (isreal(A) && ishermitian(A))) ?
99+
_unsafe_mul!(y, A.lmap, x, α, β) :
100+
_unsafe_mul!(y, transpose(A.lmap), x, α, β)
101+
end
102+
function _unsafe_mul!(y::$Out, Ac::AdjointMap{<:Any,<:WrappedMap}, x::$In, α::Number, β::Number)
103+
A = Ac.lmap
104+
return ishermitian(A) ?
105+
_unsafe_mul!(y, A.lmap, x, α, β) :
106+
_unsafe_mul!(y, adjoint(A.lmap), x, α, β)
109107
end
110108
end
109+
end
111110

112-
mul!(X::AbstractMatrix, Y::AbstractMatrix, A::VecOrMatMap, α::Number, β::Number) =
113-
mul!(X, Y, A.lmap, α, β)
114-
# the following method is needed for disambiguation with left-multiplication
115-
function mul!(Y::AbstractMatrix{<:RealOrComplex}, X::AbstractMatrix{<:RealOrComplex}, A::VecOrMatMap{<:RealOrComplex},
116-
α::RealOrComplex, β::RealOrComplex)
117-
return mul!(Y, X, A.lmap, α, β)
118-
end
119-
function mul!(Y::AbstractMatrix{<:RealOrComplex}, X::TransposeAbsVecOrMat{<:RealOrComplex}, A::VecOrMatMap{<:RealOrComplex},
120-
α::RealOrComplex, β::RealOrComplex)
121-
return mul!(Y, X, A.lmap, α, β)
122-
end
123-
end # VERSION
111+
mul!(X::AbstractMatrix, Y::AbstractMatrix, A::VecOrMatMap, α::Number, β::Number) =
112+
mul!(X, Y, A.lmap, α, β)
113+
# the following 2 methods are needed for disambiguation with left-multiplication
114+
function mul!(Y::AbstractMatrix{<:RealOrComplex}, X::AbstractMatrix{<:RealOrComplex}, A::VecOrMatMap{<:RealOrComplex},
115+
α::RealOrComplex, β::RealOrComplex)
116+
return mul!(Y, X, A.lmap, α, β)
117+
end
118+
function mul!(Y::AbstractMatrix{<:RealOrComplex}, X::TransposeAbsVecOrMat{<:RealOrComplex}, A::VecOrMatMap{<:RealOrComplex},
119+
α::RealOrComplex, β::RealOrComplex)
120+
return mul!(Y, X, A.lmap, α, β)
121+
end
124122

125123
# combine LinearMap and Matrix objects: linear combinations and map composition
126124
Base.:(+)(A₁::LinearMap, A₂::AbstractMatrix) = +(A₁, WrappedMap(A₂))

test/linearcombination.jl

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,33 +38,29 @@ using Test, LinearMaps, LinearAlgebra, SparseArrays, BenchmarkTools
3838
B = rand(ComplexF64, size(A)...)
3939
M = @inferred LinearMap(A)
4040
N = @inferred LinearMap(B)
41-
@test @inferred(LinearMaps.MulStyle(M)) === matrixstyle
42-
@test @inferred(LinearMaps.MulStyle(N)) === matrixstyle
41+
@test @inferred(LinearMaps.MulStyle(M)) === FiveArg()
42+
@test @inferred(LinearMaps.MulStyle(N)) === FiveArg()
4343
LC = @inferred M + N
44-
@test @inferred(LinearMaps.MulStyle(LC)) === matrixstyle
45-
@test @inferred(LinearMaps.MulStyle(LC + I)) === matrixstyle
46-
@test @inferred(LinearMaps.MulStyle(LC + 2.0*I)) === matrixstyle
44+
@test @inferred(LinearMaps.MulStyle(LC)) === FiveArg()
45+
@test @inferred(LinearMaps.MulStyle(LC + I)) === FiveArg()
46+
@test @inferred(LinearMaps.MulStyle(LC + 2.0*I)) === FiveArg()
4747
@test sparse(LC) == Matrix(LC) == A+B
4848
v = rand(ComplexF64, 10)
4949
w = similar(v)
5050
b = @benchmarkable mul!($w, $M, $v)
5151
@test run(b, samples=3).allocs == 0
52-
if VERSION >= v"1.3.0-alpha.115"
53-
b = @benchmarkable mul!($w, $LC, $v)
52+
b = @benchmarkable mul!($w, $LC, $v)
53+
@test run(b, samples=3).allocs == 0
54+
for α in (false, true, rand(ComplexF64)), β in (false, true, rand(ComplexF64))
55+
b = @benchmarkable mul!($w, $LC, $v, $α, $β)
5456
@test run(b, samples=3).allocs == 0
55-
for α in (false, true, rand(ComplexF64)), β in (false, true, rand(ComplexF64))
56-
if testallocs
57-
b = @benchmarkable mul!($w, $LC, $v, $α, $β)
58-
@test run(b, samples=3).allocs == 0
59-
b = @benchmarkable mul!($w, $(I + LC), $v, $α, $β)
60-
@test run(b, samples=3).allocs == 0
61-
b = @benchmarkable mul!($w, $(LC + I), $v, $α, $β)
62-
@test run(b, samples=3).allocs == 0
63-
end
64-
y = rand(ComplexF64, size(v))
65-
@test mul!(copy(y), LC, v, α, β) Matrix(LC)*v*α + y*β
66-
@test mul!(copy(y), LC+I, v, α, β) Matrix(LC + I)*v*α + y*β
67-
end
57+
b = @benchmarkable mul!($w, $(I + LC), $v, $α, $β)
58+
@test run(b, samples=3).allocs == 0
59+
b = @benchmarkable mul!($w, $(LC + I), $v, $α, $β)
60+
@test run(b, samples=3).allocs == 0
61+
y = rand(ComplexF64, size(v))
62+
@test mul!(copy(y), LC, v, α, β) Matrix(LC)*v*α + y*β
63+
@test mul!(copy(y), LC+I, v, α, β) Matrix(LC + I)*v*α + y*β
6864
end
6965
# @test_throws ErrorException LinearMaps.LinearCombination{ComplexF64}((M, N), (1, 2, 3))
7066
@test @inferred size(3M + 2.0N) == size(A)

test/linearmaps.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ LinearAlgebra.mul!(y::AbstractVector, A::Union{SimpleFunctionMap,SimpleComplexFu
6161
α = rand(ComplexF64); β = rand(ComplexF64)
6262
v = rand(ComplexF64, 10); V = rand(ComplexF64, 10, 3)
6363
w = rand(ComplexF64, 10); W = rand(ComplexF64, 10, 3)
64-
if VERSION v"1.3"
65-
F(v) == F*v
66-
end
64+
F(v) == F*v
6765
@test mul!(w, F, v) === w == F * v
6866
@test_throws ErrorException F' * v
6967
@test_throws ErrorException transpose(F) * v

test/runtests.jl

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
using Test, LinearMaps, Aqua
2-
import LinearMaps: FiveArg, ThreeArg
3-
4-
const matrixstyle = VERSION v"1.3.0-alpha.115" ? FiveArg() : ThreeArg()
5-
6-
const testallocs = VERSION v"1.4-"
2+
using LinearMaps: FiveArg, ThreeArg
73

84
@testset "code quality" begin
95
Aqua.test_all(LinearMaps)
@@ -37,6 +33,4 @@ include("left.jl")
3733

3834
include("fillmap.jl")
3935

40-
if VERSION v"1.1"
41-
include("nontradaxes.jl")
42-
end
36+
include("nontradaxes.jl")

test/uniformscalingmap.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@ using Test, LinearMaps, LinearAlgebra, BenchmarkTools
3030
Λ = @inferred LinearMap*I, 10)
3131
x = rand(Float64, sz)
3232
y = rand(Float64, sz)
33-
if testallocs
34-
b = @benchmarkable mul!($y, $Λ, $x, $α, $β)
35-
@test run(b, samples=3).allocs == 0
36-
end
33+
b = @benchmarkable mul!($y, $Λ, $x, $α, $β)
34+
@test run(b, samples=3).allocs == 0
3735
y = deepcopy(x)
3836
@inferred mul!(y, Λ, x, α, β)
3937
@test y λ * x * α + β * x

0 commit comments

Comments
 (0)