Skip to content

Commit d734ee5

Browse files
authored
Some minor simplifications (#35)
* comp TMap with AMap is always false, simplify comparison function * add tests to increase coverage
1 parent 814a3fb commit d734ee5

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

src/transpose.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ LinearAlgebra.isposdef(A::Union{TransposeMap, AdjointMap}) = isposdef(A.lmap)
2222
# comparison of TransposeMap objects
2323
Base.:(==)(A::TransposeMap, B::TransposeMap) = A.lmap == B.lmap
2424
Base.:(==)(A::AdjointMap, B::AdjointMap) = A.lmap == B.lmap
25-
Base.:(==)(A::TransposeMap, B::AdjointMap) = isreal(B) && A.lmap == B.lmap
26-
Base.:(==)(A::AdjointMap, B::TransposeMap) = isreal(A) && A.lmap == B.lmap
25+
Base.:(==)(A::TransposeMap, B::AdjointMap) = false # isreal(B) && A.lmap == B.lmap # isreal(::AdjointMap) == false
26+
Base.:(==)(A::AdjointMap, B::TransposeMap) = false # isreal(A) && A.lmap == B.lmap # isreal(::AdjointMap) == false
2727
Base.:(==)(A::TransposeMap, B::LinearMap) = issymmetric(B) && A.lmap == B
2828
Base.:(==)(A::AdjointMap, B::LinearMap) = ishermitian(B) && A.lmap == B
2929
Base.:(==)(A::LinearMap, B::TransposeMap) = issymmetric(A) && B.lmap == A

test/runtests.jl

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,16 @@ end
8888
@test !(adjoint(M) == transpose(M))
8989
@test transpose(M') * v transpose(A') * v
9090
@test transpose(LinearMap(M')) * v transpose(A') * v
91-
@test LinearMap(transpose(M))' * v copy(transpose(A))' * v
91+
@test LinearMap(transpose(M))' * v transpose(A)' * v
9292
@test transpose(LinearMap(transpose(M))) * v Av
9393
@test adjoint(LinearMap(adjoint(M))) * v Av
94+
95+
@test mul!(copy(w), transpose(LinearMap(M')), v) transpose(A') * v
96+
@test mul!(copy(w), LinearMap(transpose(M))', v) transpose(A)' * v
97+
@test mul!(copy(w), transpose(LinearMap(transpose(M))), v) Av
98+
@test mul!(copy(w), adjoint(LinearMap(adjoint(M))), v) Av
9499
@test mul!(copy(V), transpose(M), W) transpose(A) * W
100+
@test mul!(copy(V), adjoint(M), W) A' * W
95101

96102
B = LinearMap(Symmetric(rand(10, 10)))
97103
@test transpose(B) == B
@@ -129,11 +135,17 @@ end
129135
@test Matrix(CS) == [1. 0.; 1. 1.]
130136
@test Array(CS) == [1. 0.; 1. 1.]
131137
CS = LinearMap(cumsum, 10; ismutating=false)
132-
v = rand(ComplexF64, 10)
138+
v = rand(10)
133139
cv = cumsum(v)
134140
@test CS * v == cv
135141
@test *(CS, v) == cv
136142
@test_throws ErrorException CS' * v
143+
CS = LinearMap(cumsum, x -> cumsum(reverse(x)), 10; ismutating=false)
144+
cv = cumsum(v)
145+
@test CS * v == cv
146+
@test *(CS, v) == cv
147+
@test CS' * v == cumsum(reverse(v))
148+
@test mul!(similar(v), transpose(CS), v) == cumsum(reverse(v))
137149

138150
CS! = LinearMap(cumsum!, 10; ismutating=true)
139151
@test LinearMaps.ismutating(CS!)
@@ -143,6 +155,24 @@ end
143155
@test_throws ErrorException CS!'v
144156
@test_throws ErrorException transpose(CS!) * v
145157

158+
CS! = LinearMap{ComplexF64}(cumsum!, 10; ismutating=true)
159+
v = rand(ComplexF64, 10)
160+
cv = cumsum(v)
161+
@test LinearMaps.ismutating(CS!)
162+
@test CS! * v == cv
163+
@test *(CS!, v) == cv
164+
@test mul!(similar(v), CS!, v) == cv
165+
@test_throws ErrorException CS!'v
166+
@test_throws ErrorException adjoint(CS!) * v
167+
CS! = LinearMap{ComplexF64}(cumsum!, x -> cumsum!(reverse!(x)), 10; ismutating=true)
168+
@test LinearMaps.ismutating(CS!)
169+
@test CS! * v == cv
170+
@test *(CS!, v) == cv
171+
@test mul!(similar(v), CS!, v) == cv
172+
@test CS' * v == cumsum(reverse(v))
173+
@test mul!(similar(v), transpose(CS), v) == cumsum(reverse(v))
174+
@test mul!(similar(v), adjoint(CS), v) == cumsum(reverse(v))
175+
146176
# Test fallback methods:
147177
L = LinearMap(x -> x, x -> x, 10)
148178
v = randn(10)
@@ -205,10 +235,13 @@ struct SimpleFunctionMap <: LinearMap{Float64}
205235
f::Function
206236
N::Int
207237
end
208-
Base.size(A::SimpleFunctionMap) = (A.N, A.N)
209-
LinearAlgebra.issymmetric(A::SimpleFunctionMap) = false
210-
*(A::SimpleFunctionMap, v::Vector) = A.f(v)
211-
mul!(y::Vector, A::SimpleFunctionMap, x::Vector) = copyto!(y, *(A, x))
238+
struct SimpleComplexFunctionMap <: LinearMap{Complex{Float64}}
239+
f::Function
240+
N::Int
241+
end
242+
Base.size(A::Union{SimpleFunctionMap,SimpleComplexFunctionMap}) = (A.N, A.N)
243+
*(A::Union{SimpleFunctionMap,SimpleComplexFunctionMap}, v::Vector) = A.f(v)
244+
mul!(y::Vector, A::Union{SimpleFunctionMap,SimpleComplexFunctionMap}, x::Vector) = copyto!(y, *(A, x))
212245

213246
@testset "composition" begin
214247
F = LinearMap(cumsum, 10; ismutating=false)
@@ -249,11 +282,15 @@ mul!(y::Vector, A::SimpleFunctionMap, x::Vector) = copyto!(y, *(A, x))
249282
@test w LF * v
250283

251284
# test new type
252-
253285
F = SimpleFunctionMap(cumsum, 10)
286+
FC = SimpleComplexFunctionMap(cumsum, 10)
254287
@test ndims(F) == 2
255288
@test size(F, 1) == 10
256289
@test length(F) == 100
290+
@test !issymmetric(F)
291+
@test !ishermitian(F)
292+
@test !ishermitian(FC)
293+
@test !isposdef(F)
257294
w = similar(v)
258295
mul!(w, F, v)
259296
@test w == F * v
@@ -328,6 +365,8 @@ v = rand(ComplexF64, 10)
328365
w = similar(v)
329366
@testset "identity map" begin
330367
Id = LinearMaps.IdentityMap(10)
368+
@test_throws ErrorException LinearMaps.IdentityMap(10, 20)
369+
@test_throws ErrorException LinearMaps.IdentityMap((10, 20))
331370
@test size(Id) == (10, 10)
332371
@test isreal(Id)
333372
@test issymmetric(Id)

0 commit comments

Comments
 (0)