@@ -3,33 +3,40 @@ module TestLayoutArray
3
3
using ArrayLayouts, LinearAlgebra, FillArrays, Test, SparseArrays, Random
4
4
using ArrayLayouts: sub_materialize, MemoryLayout, ColumnNorm, RowMaximum, CRowMaximum, @_layoutlmul , Mul
5
5
import ArrayLayouts: triangulardata
6
+ import LinearAlgebra: Diagonal, Bidiagonal, Tridiagonal, SymTridiagonal
6
7
7
- struct MyMatrix <: LayoutMatrix{Float64 }
8
- A:: Matrix{Float64}
8
+ struct MyMatrix{T,M <: AbstractMatrix{T} } <: LayoutMatrix{T }
9
+ A:: M
9
10
end
10
11
11
12
Base. getindex (A:: MyMatrix , k:: Int , j:: Int ) = A. A[k,j]
12
13
Base. setindex! (A:: MyMatrix , v, k:: Int , j:: Int ) = setindex! (A. A, v, k, j)
13
14
Base. size (A:: MyMatrix ) = size (A. A)
14
15
Base. strides (A:: MyMatrix ) = strides (A. A)
15
- Base. elsize (:: Type{MyMatrix} ) = sizeof (Float64 )
16
- Base. cconvert (:: Type{Ptr{Float64 }} , A:: MyMatrix ) = A. A
17
- Base. unsafe_convert (:: Type{Ptr{Float64 }} , A:: MyMatrix ) = Base. unsafe_convert (Ptr{Float64 }, A. A)
18
- MemoryLayout (:: Type{MyMatrix} ) = DenseColumnMajor ( )
16
+ Base. elsize (:: Type{<: MyMatrix{T}} ) where {T} = sizeof (T )
17
+ Base. cconvert (:: Type{Ptr{T }} , A:: MyMatrix{T} ) where {T} = Base . cconvert (Ptr{T}, A. A)
18
+ Base. unsafe_convert (:: Type{Ptr{T }} , A:: MyMatrix{T} ) where {T} = Base. unsafe_convert (Ptr{T }, A. A)
19
+ MemoryLayout (:: Type{MyMatrix{T,M}} ) where {T,M} = MemoryLayout (M )
19
20
Base. copy (A:: MyMatrix ) = MyMatrix (copy (A. A))
21
+ ArrayLayouts. bidiagonaluplo (M:: MyMatrix ) = ArrayLayouts. bidiagonaluplo (M. A)
22
+ for MT in (:Diagonal , :Bidiagonal , :Tridiagonal , :SymTridiagonal )
23
+ @eval $ MT (M:: MyMatrix ) = $ MT (M. A)
24
+ end
20
25
21
- struct MyVector{T} <: LayoutVector{T}
22
- A:: Vector{T}
26
+ struct MyVector{T,V <: AbstractVector{T} } <: LayoutVector{T}
27
+ A:: V
23
28
end
24
29
30
+ MyVector (M:: MyVector ) = MyVector (M. A)
25
31
Base. getindex (A:: MyVector , k:: Int ) = A. A[k]
26
32
Base. setindex! (A:: MyVector , v, k:: Int ) = setindex! (A. A, v, k)
27
33
Base. size (A:: MyVector ) = size (A. A)
28
34
Base. strides (A:: MyVector ) = strides (A. A)
29
- Base. elsize (:: Type{MyVector} ) = sizeof (Float64 )
30
- Base. cconvert (:: Type{Ptr{T}} , A:: MyVector{T} ) where {T} = A. A
35
+ Base. elsize (:: Type{<: MyVector{T}} ) where {T} = sizeof (T )
36
+ Base. cconvert (:: Type{Ptr{T}} , A:: MyVector{T} ) where {T} = Base . cconvert (Ptr{T}, A. A)
31
37
Base. unsafe_convert (:: Type{Ptr{T}} , A:: MyVector{T} ) where T = Base. unsafe_convert (Ptr{T}, A. A)
32
- MemoryLayout (:: Type{MyVector} ) = DenseColumnMajor ()
38
+ MemoryLayout (:: Type{MyVector{T,V}} ) where {T,V} = MemoryLayout (V)
39
+ Base. copy (A:: MyVector ) = MyVector (copy (A. A))
33
40
34
41
# These need to test dispatch reduces to ArrayLayouts.mul, etc.
35
42
@testset " LayoutArray" begin
@@ -42,7 +49,7 @@ MemoryLayout(::Type{MyVector}) = DenseColumnMajor()
42
49
@test a[1 : 3 ] == a. A[1 : 3 ]
43
50
@test a[:] == a
44
51
@test (a' )[1 ,:] == (a' )[1 ,1 : 3 ] == a
45
- @test sprint (show, " text/plain" , a) == " 3-element $MyVector {Float64} :\n 1.0\n 2.0\n 3.0"
52
+ @test sprint (show, " text/plain" , a) == " $( summary (a)) :\n 1.0\n 2.0\n 3.0"
46
53
@test B* a ≈ B* a. A
47
54
@test B' * a ≈ B' * a. A
48
55
@test transpose (B)* a ≈ transpose (B)* a. A
@@ -104,8 +111,8 @@ MemoryLayout(::Type{MyVector}) = DenseColumnMajor()
104
111
@test_throws ErrorException qr! (A)
105
112
@test lu! (copy (A)). factors ≈ lu (A. A). factors
106
113
b = randn (5 )
107
- @test A \ b == A. A \ b == A. A \ MyVector (b) == ldiv! (lu (A. A), copy (MyVector (b) ))
108
- @test A \ b == ldiv! (lu (A), copy (MyVector (b))) == ldiv! ( lu (A), copy ( b))
114
+ @test A \ b == A. A \ b == A. A \ MyVector (b) == ldiv! (lu (A. A), copy (b ))
115
+ @test A \ b == ldiv! (lu (A), copy (b))
109
116
@test lu (A). L == lu (A. A). L
110
117
@test lu (A). U == lu (A. A). U
111
118
@test lu (A). p == lu (A. A). p
@@ -120,7 +127,7 @@ MemoryLayout(::Type{MyVector}) = DenseColumnMajor()
120
127
@test cholesky! (deepcopy (S), CRowMaximum ()). U ≈ cholesky (Matrix (S), CRowMaximum ()). U
121
128
@test cholesky (S) \ b ≈ cholesky (Matrix (S)) \ b ≈ cholesky (Matrix (S)) \ MyVector (b)
122
129
@test cholesky (S, CRowMaximum ()) \ b ≈ cholesky (Matrix (S), CRowMaximum ()) \ b
123
- @test cholesky (S, CRowMaximum ()) \ b ≈ ldiv! (cholesky (Matrix (S), CRowMaximum ()), copy (MyVector (b) ))
130
+ @test cholesky (S, CRowMaximum ()) \ b ≈ ldiv! (cholesky (Matrix (S), CRowMaximum ()), copy (b ))
124
131
@test cholesky (S) \ b ≈ Matrix (S) \ b ≈ Symmetric (Matrix (S)) \ b
125
132
@test cholesky (S) \ b ≈ Symmetric (Matrix (S)) \ MyVector (b)
126
133
if VERSION >= v " 1.9"
@@ -140,18 +147,9 @@ MemoryLayout(::Type{MyVector}) = DenseColumnMajor()
140
147
141
148
@testset " ldiv!" begin
142
149
c = MyVector (randn (5 ))
143
- if VERSION < v " 1.9"
144
- @test_broken ldiv! (lu (A), MyVector (copy (c))) ≈ A \ c
145
- else
146
- @test ldiv! (lu (A), MyVector (copy (c))) ≈ A \ c
147
- end
148
- if VERSION < v " 1.9" || VERSION >= v " 1.10-"
149
- @test_throws ErrorException ldiv! (qr (A), MyVector (copy (c)))
150
- else
151
- @test_throws MethodError ldiv! (qr (A), MyVector (copy (c)))
152
- end
150
+ @test ldiv! (lu (A), MyVector (copy (c))) ≈ A \ c
153
151
@test_throws ErrorException ldiv! (eigen (randn (5 ,5 )), c)
154
- @test ArrayLayouts. ldiv! (svd (A. A), copy (c)) ≈ ArrayLayouts. ldiv! (similar (c), svd (A. A), c) ≈ A \ c
152
+ @test ArrayLayouts. ldiv! (svd (A. A), Vector (c)) ≈ ArrayLayouts. ldiv! (similar (c), svd (A. A), c) ≈ A \ c
155
153
if VERSION ≥ v " 1.8"
156
154
@test ArrayLayouts. ldiv! (similar (c), transpose (lu (A. A)), copy (c)) ≈ A' \ c
157
155
end
@@ -213,8 +211,8 @@ MemoryLayout(::Type{MyVector}) = DenseColumnMajor()
213
211
@test B == Ones (5 ,5 )* A + 2.0 Bin
214
212
end
215
213
216
- C = MyMatrix ([1 2 ; 3 4 ])
217
- @test sprint (show, " text/plain" , C) == " 2×2 $MyMatrix :\n 1.0 2.0\n 3.0 4.0"
214
+ C = MyMatrix (Float64 [1 2 ; 3 4 ])
215
+ @test sprint (show, " text/plain" , C) == " $( summary (C)) :\n 1.0 2.0\n 3.0 4.0"
218
216
219
217
@testset " layoutldiv" begin
220
218
A = MyMatrix (randn (5 ,5 ))
@@ -337,6 +335,33 @@ MemoryLayout(::Type{MyVector}) = DenseColumnMajor()
337
335
@test B̃\ D ≈ B̃\ Matrix (D)
338
336
@test D\ D̃ ≈ D̃\ D
339
337
@test B̃/ D ≈ B̃/ Matrix (D)
338
+
339
+ @testset " Diagonal * Bidiagonal/Tridiagonal with structured diags" begin
340
+ n = size (D,1 )
341
+ B = Bidiagonal (map (MyVector, (rand (n), rand (n- 1 )))... , :U )
342
+ MB = MyMatrix (B)
343
+ S = SymTridiagonal (map (MyVector, (rand (n), rand (n- 1 )))... )
344
+ MS = MyMatrix (S)
345
+ T = Tridiagonal (map (MyVector, (rand (n- 1 ), rand (n), rand (n- 1 )))... )
346
+ MT = MyMatrix (T)
347
+ DA, BA, SA, TA = map (Array, (D, B, S, T))
348
+ if VERSION >= v " 1.11"
349
+ @test D * B ≈ DA * BA
350
+ @test B * D ≈ BA * DA
351
+ @test D * MB ≈ DA * BA
352
+ @test MB * D ≈ BA * DA
353
+ end
354
+ if VERSION >= v " 1.12.0-DEV.824"
355
+ @test D * S ≈ DA * SA
356
+ @test D * MS ≈ DA * SA
357
+ @test D * T ≈ DA * TA
358
+ @test D * MT ≈ DA * TA
359
+ @test S * D ≈ SA * DA
360
+ @test MS * D ≈ SA * DA
361
+ @test T * D ≈ TA * DA
362
+ @test MT * D ≈ TA * DA
363
+ end
364
+ end
340
365
end
341
366
342
367
@testset " Adj/Trans" begin
@@ -400,7 +425,7 @@ MemoryLayout(::Type{MyVector}) = DenseColumnMajor()
400
425
401
426
@testset " dot" begin
402
427
a = MyVector (randn (5 ))
403
- @test dot (a, Zeros (5 )) ≡ dot (Zeros (5 ), a) ≡ 0.0
428
+ @test dot (a, Zeros (5 )) ≡ dot (Zeros (5 ), a) == 0.0
404
429
end
405
430
406
431
@testset " layout_getindex scalar" begin
0 commit comments