-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathtest_subarray.jl
130 lines (103 loc) · 5.04 KB
/
test_subarray.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
module TestSubArray
using BandedMatrices, FillArrays, ArrayLayouts, Test, LinearAlgebra
import LinearAlgebra: axpy!
import BandedMatrices: BandedColumns, bandeddata, MemoryLayout, BandedSubBandedMatrix, _BandedMatrix, sub_materialize, isbanded
include("mymatrix.jl")
@testset "BandedMatrix SubArray" begin
@testset "BandedMatrix SubArray interface" begin
A = brand(10,10,1,2)
V = view(A,2:4,3:6)
@test isbanded(V)
@test bandwidths(V) == (2,1)
@test MemoryLayout(V) isa BandedColumns{DenseColumnMajor}
@test all(Matrix(_BandedMatrix(bandeddata(V), size(V,1), bandwidths(V)...)) .===
Matrix(V))
@test sub_materialize(V) isa BandedMatrix
@test sub_materialize(MemoryLayout(V), V, (Base.Slice(1:3), Base.OneTo(4))) isa BandedMatrix
# if 2nd argument is not Base.OneTo, cannot convert to BandedMatrix so take adjoint
@test sub_materialize(MemoryLayout(V), V, (Base.OneTo(3), Base.Slice(1:4))) isa Adjoint{<:Any,<:BandedMatrix}
# if neither argument is Base.OneTo, we give up
@test sub_materialize(MemoryLayout(V), V, (Base.Slice(1:3), Base.Slice(1:4))) isa Matrix
end
@testset "BandedMatrix SubArray arithmetic" begin
let A = brand(10,10,1,2), B = brand(20,20,1,2)
@test isa(view(B,1:10,1:10),BandedSubBandedMatrix{Float64})
@test MemoryLayout(typeof(view(B,1:10,1:10))) isa BandedColumns{DenseColumnMajor}
B2 = copy(B)
@test B == B2
@test (2.0A+B[1:10,1:10]) ≈ axpy!(2.0,A,view(B2,1:10,1:10))
@test (2.0A+B[1:10,1:10]) ≈ B2[1:10,1:10]
A2 = copy(A)
@test (2.0B[1:10,1:10]+A) ≈ axpy!(2.0,view(B,1:10,1:10),A2)
@test (2.0B[1:10,1:10]+A) ≈ A2
end
let A = brand(20,20,1,2), B = brand(20,20,1,2)
@test isa(view(B,1:10,1:10),BandedSubBandedMatrix{Float64})
B2 = copy(B)
@test (2.0A[1:10,1:10]+B[1:10,1:10]) ≈ axpy!(2.0,view(A,1:10,1:10),view(B2,1:10,1:10))
@test (2.0A[1:10,1:10]+B[1:10,1:10]) ≈ B2[1:10,1:10]
B2 = copy(B)
@test (2.0A[1:10,:]+B[1:10,:]) ≈ axpy!(2.0,view(A,1:10,:),view(B2,1:10,:))
@test (2.0A[1:10,:]+B[1:10,:]) ≈ B2[1:10,:]
B2 = copy(B)
@test (2.0A[:,1:10]+B[:,1:10]) ≈ axpy!(2.0,view(A,:,1:10),view(B2,:,1:10))
@test (2.0A[:,1:10]+B[:,1:10]) ≈ B2[:,1:10]
B2 = copy(B)
@test (2.0A[:,:]+B[:,:]) ≈ axpy!(2.0,view(A,:,:),view(B2,:,:))
@test (2.0A[:,:]+B[:,:]) ≈ B2[:,:]
end
let A = brand(10,10,1,2), B = brand(20,10,1,2)
B2 = copy(B)
@test (2.0A+B[1:10,1:10]) ≈ axpy!(2.0,A,view(B2,1:10,:))
@test (2.0A+B[1:10,1:10]) ≈ B2[1:10,1:10]
A2 = copy(A)
@test (2.0B[1:10,1:10]+A) ≈ axpy!(2.0,view(B,1:10,:),A2)
@test (2.0B[1:10,1:10]+A) ≈ A2
end
let A = brand(10,10,1,2), B = brand(10,20,1,2)
B2 = copy(B)
@test (2.0A+B[1:10,1:10]) ≈ axpy!(2.0,A,view(B2,:,1:10))
@test (2.0A+B[1:10,1:10]) ≈ B2[1:10,1:10]
A2 = copy(A)
@test (2.0B[1:10,1:10]+A) ≈ axpy!(2.0,view(B,:,1:10),A2)
@test (2.0B[1:10,1:10]+A) ≈ A2
end
end
@testset "BandedMatrix SubArray *" begin
let A=brand(10,10,1,2), v=rand(20)
@test A*view(v,1:10) ≈ Matrix(A)*v[1:10]
@test A*view(v,1:2:20) ≈ Matrix(A)*v[1:2:20]
M=rand(20,20)
@test A*view(M,1:10,1:10) ≈ Matrix(A)*M[1:10,1:10]
@test A*view(M,1:2:20,1:2:20) ≈ Matrix(A)*M[1:2:20,1:2:20]
end
let A=brand(10,10,1,2), B=brand(20,10,1,2), C=brand(10,20,1,2),
D=brand(20,20,1,2), M=rand(10,10), V=view(rand(20,20),1:2:20,1:2:20)
for S in (view(A,:,:),view(B,1:10,:),view(C,:,1:10),view(D,1:10,1:10),
view(D,2:11,1:10),view(D,1:10,2:11),view(D,11:20,11:20))
@test A*S ≈ Matrix(A)*Matrix(S)
@test S*A ≈ Matrix(S)*Matrix(A)
@test S*S ≈ Matrix(S)*Matrix(S)
@test M*S ≈ M*Matrix(S)
@test S*M ≈ Matrix(S)*M
@test V*S ≈ Matrix(V)*Matrix(S)
@test S*V ≈ Matrix(S)*Matrix(V)
end
end
end
@testset "BandedMatrix SubArray conversion" begin
@testset "Container $C" for C in (Matrix{Int32}, MyMatrix{Int32})
matrix = convert(C, rand(Int32, 10, 12))
T = eltype(matrix)
banded = @inferred BandedMatrix{T, C}(matrix, (1, 1))
indices = 2:8, 3:9, 5:9
@testset "view $kr, $jr" for kr in indices, jr in indices
bview = @inferred view(banded, kr, jr)
@test bview isa BandedMatrices.BandedSubBandedMatrix{T, C}
@test @inferred(convert(BandedMatrix, bview)) isa BandedMatrix{T, C}
@test convert(BandedMatrix, bview) == banded[kr, jr]
end
end
end
end
end # module