|
| 1 | +using LinearAlgebra, LinearMaps, Test |
| 2 | +using LinearMaps: VecOrMatMap, ScaledMap |
| 3 | +# using BenchmarkTools |
| 4 | + |
| 5 | +function test_getindex(A::LinearMap, M::AbstractMatrix) |
| 6 | + @assert size(A) == size(M) |
| 7 | + mask = rand(Bool, size(A)) |
| 8 | + imask = rand(Bool, size(A, 1)) |
| 9 | + jmask = rand(Bool, size(A, 2)) |
| 10 | + @test A[1,:] == M[1,:] |
| 11 | + @test A[:,1] == M[:,1] |
| 12 | + @test A[1:lastindex(A,1)-2,:] == M[1:lastindex(A,1)-2,:] |
| 13 | + @test A[:,1:4] == M[:,1:4] |
| 14 | + @test A[[2,1],:] == M[[2,1],:] |
| 15 | + @test A[:,[2,1]] == M[:,[2,1]] |
| 16 | + @test A[:,:] == M |
| 17 | + @test (lastindex(A, 1), lastindex(A, 2)) == size(A) |
| 18 | + if A isa VecOrMatMap || A isa ScaledMap{<:Any,<:Any,<:VecOrMatMap} |
| 19 | + @test A[:] == M[:] |
| 20 | + @test A[1,1] == M[1,1] |
| 21 | + @test A[1,1:3] == M[1,1:3] |
| 22 | + @test A[1:3,1] == M[1:3,1] |
| 23 | + @test A[2:end,1] == M[2:end,1] |
| 24 | + @test A[1:2,1:3] == M[1:2,1:3] |
| 25 | + @test A[[2,1],1:3] == M[[2,1],1:3] |
| 26 | + @test A[7] == M[7] |
| 27 | + @test A[3:7] == M[3:7] |
| 28 | + @test A[mask] == M[mask] |
| 29 | + @test A[findall(mask)] == M[findall(mask)] |
| 30 | + @test A[CartesianIndex(1,1)] == M[CartesianIndex(1,1)] |
| 31 | + @test A[imask, 1] == M[imask, 1] |
| 32 | + @test A[1, jmask] == M[1, jmask] |
| 33 | + @test A[imask, jmask] == M[imask, jmask] |
| 34 | + else |
| 35 | + @test_throws ErrorException A[:] |
| 36 | + @test_throws ErrorException A[1,1] |
| 37 | + @test_throws ErrorException A[1,1:3] |
| 38 | + @test_throws ErrorException A[1:3,1] |
| 39 | + @test_throws ErrorException A[2:end,1] |
| 40 | + @test_throws ErrorException A[1:2,1:3] |
| 41 | + @test_throws ErrorException A[[2,1],1:3] |
| 42 | + @test_throws ErrorException A[7] |
| 43 | + @test_throws ErrorException A[3:7] |
| 44 | + @test_throws ErrorException A[mask] |
| 45 | + @test_throws ErrorException A[findall(mask)] |
| 46 | + @test_throws ErrorException A[CartesianIndex(1,1)] |
| 47 | + @test_throws ErrorException A[imask, 1] |
| 48 | + @test_throws ErrorException A[1, jmask] |
| 49 | + @test_throws ErrorException A[imask, jmask] |
| 50 | + end |
| 51 | + @test_throws BoundsError A[lastindex(A,1)+1,1] |
| 52 | + @test_throws BoundsError A[1,lastindex(A,2)+1] |
| 53 | + @test_throws BoundsError A[2,1:lastindex(A,2)+1] |
| 54 | + @test_throws BoundsError A[1:lastindex(A,1)+1,2] |
| 55 | + @test_throws BoundsError A[ones(Bool, 2, 2)] |
| 56 | + @test_throws BoundsError A[[true, true], 1] |
| 57 | + @test_throws BoundsError A[1, [true, true]] |
| 58 | + return nothing |
| 59 | +end |
| 60 | + |
| 61 | +@testset "getindex" begin |
| 62 | + M = rand(4,6) |
| 63 | + A = LinearMap(M) |
| 64 | + test_getindex(A, M) |
| 65 | + test_getindex(2A, 2M) |
| 66 | + # @btime getindex($M, i) setup=(i = rand(1:24)); |
| 67 | + # @btime getindex($A, i) setup=(i = rand(1:24)); |
| 68 | + # @btime (getindex($M, i, j)) setup=(i = rand(1:4); j = rand(1:6)); |
| 69 | + # @btime (getindex($A, i, j)) setup=(i = rand(1:4); j = rand(1:6)); |
| 70 | + |
| 71 | + struct TwoMap <: LinearMaps.LinearMap{Float64} end |
| 72 | + Base.size(::TwoMap) = (5,5) |
| 73 | + LinearMaps._unsafe_mul!(y::AbstractVector, ::TwoMap, x::AbstractVector) = fill!(y, 2.0*sum(x)) |
| 74 | + T = TwoMap() |
| 75 | + @test_throws ErrorException T[1,:] |
| 76 | + |
| 77 | + Base.transpose(A::TwoMap) = A |
| 78 | + test_getindex(TwoMap(), fill(2.0, size(T))) |
| 79 | + |
| 80 | + MA = rand(ComplexF64, 5, 5) |
| 81 | + FA = LinearMap{ComplexF64}((y, x) -> mul!(y, MA, x), (y, x) -> mul!(y, MA', x), 5, 5) |
| 82 | + F = LinearMap{ComplexF64}(x -> MA*x, y -> MA'y, 5, 5) |
| 83 | + test_getindex(FA, MA) |
| 84 | + test_getindex([FA FA], [MA MA]) |
| 85 | + test_getindex([FA; FA], [MA; MA]) |
| 86 | + test_getindex(F, MA) |
| 87 | + test_getindex(3FA, 3MA) |
| 88 | + test_getindex(FA + FA, 2MA) |
| 89 | + test_getindex(transpose(FA), transpose(MA)) |
| 90 | + test_getindex(transpose(3FA), transpose(3MA)) |
| 91 | + test_getindex(3transpose(FA), transpose(3MA)) |
| 92 | + test_getindex(adjoint(FA), adjoint(MA)) |
| 93 | + test_getindex(adjoint(3FA), adjoint(3MA)) |
| 94 | + test_getindex(3adjoint(FA), adjoint(3MA)) |
| 95 | + |
| 96 | + test_getindex(FillMap(0.5, (5, 5)), fill(0.5, (5, 5))) |
| 97 | + test_getindex(LinearMap(0.5I, 5), Matrix(0.5I, 5, 5)) |
| 98 | +end |
0 commit comments