-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathlazymultests.jl
196 lines (157 loc) · 10.2 KB
/
lazymultests.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
using LazyArrays, LinearAlgebra
import LazyArrays: @lazymul, @lazylmul, @lazyldiv, materialize!, MemoryLayout, triangulardata, LazyLayout, LazyArrayApplyStyle, UnknownLayout
# used to test general matrix backends
struct MyMatrix{T} <: AbstractMatrix{T}
A::Matrix{T}
end
MyMatrix{T}(::UndefInitializer, n::Int, m::Int) where T = MyMatrix{T}(Array{T}(undef, n, m))
MyMatrix(A::AbstractMatrix{T}) where T = MyMatrix{T}(Matrix{T}(A))
Base.convert(::Type{MyMatrix{T}}, A::MyMatrix{T}) where T = A
Base.convert(::Type{MyMatrix{T}}, A::MyMatrix) where T = MyMatrix(convert(AbstractArray{T}, A.A))
Base.convert(::Type{MyMatrix}, A::MyMatrix)= A
Base.convert(::Type{AbstractArray{T}}, A::MyMatrix) where T = MyMatrix(convert(AbstractArray{T}, A.A))
Base.convert(::Type{AbstractMatrix{T}}, A::MyMatrix) where T = MyMatrix(convert(AbstractArray{T}, A.A))
Base.convert(::Type{MyMatrix{T}}, A::AbstractArray{T}) where T = MyMatrix{T}(A)
Base.convert(::Type{MyMatrix{T}}, A::AbstractArray) where T = MyMatrix{T}(convert(AbstractArray{T}, A))
Base.convert(::Type{MyMatrix}, A::AbstractArray{T}) where T = MyMatrix{T}(A)
Base.getindex(A::MyMatrix, kj...) = A.A[kj...]
Base.getindex(A::MyMatrix, ::Colon, j::AbstractVector) = MyMatrix(A.A[:,j])
Base.setindex!(A::MyMatrix, v, kj...) = setindex!(A.A, v, kj...)
Base.size(A::MyMatrix) = size(A.A)
Base.similar(::Type{MyMatrix{T}}, m::Int, n::Int) where T = MyMatrix{T}(undef, m, n)
Base.similar(::MyMatrix{T}, m::Int, n::Int) where T = MyMatrix{T}(undef, m, n)
Base.similar(::MyMatrix, ::Type{T}, m::Int, n::Int) where T = MyMatrix{T}(undef, m, n)
LinearAlgebra.factorize(A::MyMatrix) = factorize(A.A)
@lazymul MyMatrix
@lazyldiv MyMatrix
struct MyUpperTriangular{T} <: AbstractMatrix{T}
A::UpperTriangular{T,Matrix{T}}
end
MyUpperTriangular{T}(::UndefInitializer, n::Int, m::Int) where T = MyUpperTriangular{T}(UpperTriangular(Array{T}(undef, n, m)))
MyUpperTriangular(A::AbstractMatrix{T}) where T = MyUpperTriangular{T}(UpperTriangular(Matrix{T}(A)))
Base.convert(::Type{MyUpperTriangular{T}}, A::MyUpperTriangular{T}) where T = A
Base.convert(::Type{MyUpperTriangular{T}}, A::MyUpperTriangular) where T = MyUpperTriangular(convert(AbstractArray{T}, A.A))
Base.convert(::Type{MyUpperTriangular}, A::MyUpperTriangular)= A
Base.convert(::Type{AbstractArray{T}}, A::MyUpperTriangular) where T = MyUpperTriangular(convert(AbstractArray{T}, A.A))
Base.convert(::Type{AbstractMatrix{T}}, A::MyUpperTriangular) where T = MyUpperTriangular(convert(AbstractArray{T}, A.A))
Base.convert(::Type{MyUpperTriangular{T}}, A::AbstractArray{T}) where T = MyUpperTriangular{T}(A)
Base.convert(::Type{MyUpperTriangular{T}}, A::AbstractArray) where T = MyUpperTriangular{T}(convert(AbstractArray{T}, A))
Base.convert(::Type{MyUpperTriangular}, A::AbstractArray{T}) where T = MyUpperTriangular{T}(A)
Base.getindex(A::MyUpperTriangular, kj...) = A.A[kj...]
Base.getindex(A::MyUpperTriangular, ::Colon, j::AbstractVector) = MyUpperTriangular(A.A[:,j])
Base.setindex!(A::MyUpperTriangular, v, kj...) = setindex!(A.A, v, kj...)
Base.size(A::MyUpperTriangular) = size(A.A)
Base.similar(::Type{MyUpperTriangular{T}}, m::Int, n::Int) where T = MyUpperTriangular{T}(undef, m, n)
Base.similar(::MyUpperTriangular{T}, m::Int, n::Int) where T = MyUpperTriangular{T}(undef, m, n)
Base.similar(::MyUpperTriangular, ::Type{T}, m::Int, n::Int) where T = MyUpperTriangular{T}(undef, m, n)
LinearAlgebra.factorize(A::MyUpperTriangular) = factorize(A.A)
MemoryLayout(::Type{MyUpperTriangular{T}}) where T = MemoryLayout(UpperTriangular{T,Matrix{T}})
triangulardata(A::MyUpperTriangular) = triangulardata(A.A)
@lazylmul MyUpperTriangular
struct MyLazyArray{T,N} <: AbstractArray{T,N}
data::Array{T,N}
end
Base.size(A::MyLazyArray) = size(A.data)
Base.getindex(A::MyLazyArray, j::Int...) = A.data[j...]
LazyArrays.MemoryLayout(::Type{<:MyLazyArray}) = LazyLayout()
LinearAlgebra.factorize(A::MyLazyArray) = factorize(A.data)
@testset "lazymul/ldiv tests" begin
@testset "*/" begin
A = randn(5,5)
B = randn(5,5)
x = randn(5)
@test MyMatrix(A)*x ≈ apply(*,MyMatrix(A),x) ≈ A*x
@test all(MyMatrix(A)*MyMatrix(A) .=== apply(*,MyMatrix(A),MyMatrix(A)))
@test all(MyMatrix(A)*A .=== apply(*,MyMatrix(A),A))
@test all(A*MyMatrix(A) .=== apply(*,A,MyMatrix(A)))
@test MyMatrix(A)*MyMatrix(A) ≈ MyMatrix(A)*A ≈ A*MyMatrix(A) ≈ A^2
@test MyMatrix(A)*MyMatrix(A)*MyMatrix(A) ≈ apply(*,MyMatrix(A),MyMatrix(A),MyMatrix(A)) ≈ A^3
@test all(UpperTriangular(A) * MyMatrix(A) .=== apply(*,UpperTriangular(A), MyMatrix(A)))
@test all(MyMatrix(A) * UpperTriangular(A) .=== apply(*, MyMatrix(A),UpperTriangular(A)))
@test all(Diagonal(A) * MyMatrix(A) .=== apply(*,Diagonal(A), MyMatrix(A)))
@test all(MyMatrix(A) * Diagonal(A) .=== apply(*, MyMatrix(A),Diagonal(A)))
@test all(MyMatrix(A)' * x .=== apply(*,MyMatrix(A)',x))
@test all(MyMatrix(A)' * MyMatrix(A)' .=== apply(*,MyMatrix(A)', MyMatrix(A)'))
@test all(MyMatrix(A)' * A' .=== apply(*,MyMatrix(A)', A'))
@test all(A' * MyMatrix(A)' .=== apply(*,MyMatrix(A)', MyMatrix(A)'))
@test all(MyMatrix(A)' * MyMatrix(A) .=== apply(*,MyMatrix(A)', MyMatrix(A)))
@test all(MyMatrix(A)' * A .=== apply(*,MyMatrix(A)', A))
@test all(MyMatrix(A) * MyMatrix(A)' .=== apply(*,MyMatrix(A), MyMatrix(A)'))
@test all(A * MyMatrix(A)' .=== apply(*,A, MyMatrix(A)'))
@test all(UpperTriangular(A) * MyMatrix(A) .=== apply(*,UpperTriangular(A), MyMatrix(A)))
@test all(MyMatrix(A) * UpperTriangular(A) .=== apply(*, MyMatrix(A),UpperTriangular(A)))
@test all(Diagonal(A) * MyMatrix(A)' .=== apply(*,Diagonal(A), MyMatrix(A)'))
@test all(MyMatrix(A)' * Diagonal(A) .=== apply(*,MyMatrix(A)',Diagonal(A)))
@test all(UpperTriangular(A) * MyMatrix(A)' .=== apply(*,UpperTriangular(A), MyMatrix(A)'))
@test all(MyMatrix(A)' * UpperTriangular(A) .=== apply(*,MyMatrix(A)',UpperTriangular(A)))
@test MyMatrix(A)\x ≈ apply(\,MyMatrix(A),x) ≈ copyto!(similar(x),Ldiv(A,copy(x))) ≈ A\x
@test eltype(applied(\,MyMatrix(A),x)) == eltype(apply(\,MyMatrix(A),x)) == eltype(MyMatrix(A)\x) == Float64
@test MyMatrix(A)\MyMatrix(B) ≈ MyMatrix(A)\B ≈ apply(\,MyMatrix(A),B) ≈ copyto!(similar(B),Ldiv(A,copy(B))) ≈ A\B
@test eltype(applied(\,MyMatrix(A),B)) == eltype(apply(\,MyMatrix(A),B)) == eltype(MyMatrix(A)\B) == Float64
@test MyMatrix(A) * ApplyArray(exp,B) ≈ apply(*, MyMatrix(A),ApplyArray(exp,B)) ≈ A*exp(B)
@test ApplyArray(exp,A) * MyMatrix(B) ≈ apply(*, ApplyArray(exp,A), MyMatrix(B)) ≈ exp(A)*B
@test ApplyArray(exp,A) * ApplyArray(exp,B) ≈ apply(*, ApplyArray(exp,A),ApplyArray(exp,B)) ≈ exp(A)*exp(B)
@test MyMatrix(A) * BroadcastArray(exp,B) ≈ apply(*, MyMatrix(A),BroadcastArray(exp,B)) ≈ A*exp.(B)
@test BroadcastArray(exp,A) * MyMatrix(B) ≈ apply(*, BroadcastArray(exp,A), MyMatrix(B)) ≈ exp.(A)*B
@test BroadcastArray(exp,A) * BroadcastArray(exp,B) ≈ apply(*, BroadcastArray(exp,A),BroadcastArray(exp,B)) ≈ exp.(A)*exp.(B)
end
@testset "lmul!" begin
A = randn(5,5)
B = randn(5,5)
x = randn(5)
@test lmul!(MyUpperTriangular(A), copy(x)) ≈ MyUpperTriangular(A) * x
@test lmul!(MyUpperTriangular(A), copy(B)) ≈ MyUpperTriangular(A) * B
@test_skip lmul!(MyUpperTriangular(A),view(copy(B),collect(1:5),1:5)) ≈ MyUpperTriangular(A) * B
end
@testset "\\" begin
A = randn(5,5)
B = randn(5,5)
x = randn(5)
@test MyMatrix(A) \ x == apply(\, MyMatrix(A), x)
@test ldiv!(MyMatrix(A), copy(x)) == materialize!(Ldiv(MyMatrix(A), copy(x)))
@test MyMatrix(A) \ x ≈ ldiv!(MyMatrix(A), copy(x)) ≈ A\x
@test MyMatrix(A) \ B == apply(\, MyMatrix(A), B)
@test ldiv!(MyMatrix(A), copy(B)) == materialize!(Ldiv(MyMatrix(A), copy(B)))
@test MyMatrix(A) \ B ≈ MyMatrix(A) \ MyMatrix(B) ≈ ldiv!(MyMatrix(A), copy(B)) ≈ A\B
@test_broken ldiv!(MyMatrix(A), MyMatrix(copy(B))) ≈ A\B
C = randn(5,3)
@test all(MyMatrix(C)\x .=== apply(\,MyMatrix(C),x))
@test MyMatrix(C)\x ≈ C\x
@test all(MyMatrix(C)\B .=== apply(\,MyMatrix(C),B))
@test MyMatrix(C)\B ≈ C\B
@test_throws DimensionMismatch apply(\,MyMatrix(C),randn(4))
@test_throws DimensionMismatch apply(\,MyMatrix(C),randn(4,3))
end
@testset "Lazy" begin
A = MyLazyArray(randn(2,2))
B = MyLazyArray(randn(2,2))
x = MyLazyArray(randn(2))
@test apply(*,A,x) isa ApplyVector
@test apply(*,A,Array(x)) isa ApplyVector
@test apply(*,Array(A),x) isa ApplyVector
@test apply(*,A,x) == apply(*,Array(A),x) == apply(*,A,Array(x)) == Array(A)*Array(x)
@test apply(*,A,B) isa ApplyMatrix
@test apply(*,A,Array(B)) isa ApplyMatrix
@test apply(*,Array(A),B) isa ApplyMatrix
@test apply(*,A,B) == apply(*,Array(A),B) == apply(*,A,Array(B)) == Array(A)*Array(B)
@test apply(\,A,x) isa ApplyVector
@test apply(\,A,Array(x)) isa ApplyVector
@test apply(\,Array(A),x) isa ApplyVector
@test apply(\,A,x) ≈ apply(\,Array(A),x) ≈ apply(\,A,Array(x)) ≈ Array(A)\Array(x)
@test apply(\,A,B) isa ApplyMatrix
@test apply(\,A,Array(B)) isa ApplyMatrix
@test apply(\,Array(A),B) isa ApplyMatrix
@test apply(\,A,B) ≈ apply(\,Array(A),B) ≈ apply(\,A,Array(B)) ≈ Array(A)\Array(B)
Ap = applied(*,A,x)
@test Ap isa Applied{LazyArrayApplyStyle}
@test copyto!(similar(Ap), Ap) == A*x
@test copyto!(similar(Ap,BigFloat), Ap) == A*x
@test MemoryLayout(typeof(Diagonal(x))) isa DiagonalLayout{LazyLayout}
@test MemoryLayout(typeof(Diagonal(ApplyArray(+,x,x)))) isa DiagonalLayout{LazyLayout}
@test MemoryLayout(typeof(Diagonal(1:6))) isa DiagonalLayout{UnknownLayout}
@test MemoryLayout(typeof(A')) isa LazyLayout
@test MemoryLayout(typeof(transpose(A))) isa LazyLayout
@test MemoryLayout(typeof(view(A,1:2,1:2))) isa LazyLayout
@test MemoryLayout(typeof(reshape(A,4))) isa LazyLayout
end
end