Skip to content

Commit e29d784

Browse files
committed
Improve test coverage for RFP code and fix the broken parts
1 parent 09ffcb5 commit e29d784

File tree

2 files changed

+88
-10
lines changed

2 files changed

+88
-10
lines changed

src/rectfullpacked.jl

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
import Base: \
44
import Base.LinAlg: BlasFloat
5+
import ..LinearAlgebra.LAPACK2: trttf!
56

6-
immutable HermitianRFP{T<:BlasFloat} <: AbstractMatrix{T}
7+
struct HermitianRFP{T<:BlasFloat} <: AbstractMatrix{T}
78
data::Vector{T}
89
transr::Char
910
uplo::Char
@@ -83,14 +84,37 @@ end
8384

8485
Base.copy(A::HermitianRFP) = HermitianRFP(copy(A.data), A.transr, A.uplo)
8586

86-
immutable TriangularRFP{T<:BlasFloat} <: AbstractMatrix{T}
87+
struct TriangularRFP{T<:BlasFloat} <: AbstractMatrix{T}
8788
data::Vector{T}
8889
transr::Char
8990
uplo::Char
9091
end
91-
TriangularRFP(A::Matrix) = TriangularRFP(trttf!('N', 'U', A), 'N', 'U')
9292

93-
function full(A::TriangularRFP)
93+
function TriangularRFP(A::StridedMatrix, uplo::Symbol = :U)
94+
if uplo == :U
95+
return TriangularRFP(trttf!('N', 'U', A), 'N', 'U')
96+
elseif uplo == :L
97+
return TriangularRFP(trttf!('N', 'L', A), 'N', 'L')
98+
else
99+
throw(ArgumentError("uplo must be either :U or :L but was :$uplo"))
100+
end
101+
end
102+
103+
function Base.size(A::TriangularRFP, i::Integer)
104+
if i == 1 || i == 2
105+
return (isqrt(8*length(A.data) + 1) - 1) >> 1
106+
elseif i > 2
107+
return 1
108+
else
109+
return size(A.data, i)
110+
end
111+
end
112+
function Base.size(A::TriangularRFP)
113+
n = size(A, 1)
114+
return (n, n)
115+
end
116+
117+
function Base.full(A::TriangularRFP)
94118
C = LAPACK2.tfttr!(A.transr, A.uplo, A.data)
95119
if A.uplo == 'U'
96120
return triu!(C)
@@ -99,7 +123,7 @@ function full(A::TriangularRFP)
99123
end
100124
end
101125

102-
type CholeskyRFP{T<:BlasFloat} <: Factorization{T}
126+
struct CholeskyRFP{T<:BlasFloat} <: Factorization{T}
103127
data::Vector{T}
104128
transr::Char
105129
uplo::Char
@@ -109,10 +133,12 @@ Base.LinAlg.cholfact!{T<:BlasFloat}(A::HermitianRFP{T}) = CholeskyRFP(LAPACK2.pf
109133
Base.LinAlg.cholfact{T<:BlasFloat}(A::HermitianRFP{T}) = cholfact!(copy(A))
110134
Base.LinAlg.factorize(A::HermitianRFP) = cholfact(A)
111135

136+
Base.copy(F::CholeskyRFP{T}) where T = CholeskyRFP{T}(copy(F.data), F.transr, F.uplo)
137+
112138
# Solve
113139
\(A::CholeskyRFP, B::StridedVecOrMat) = LAPACK2.pftrs!(A.transr, A.uplo, A.data, copy(B))
114140
\(A::HermitianRFP, B::StridedVecOrMat) = cholfact(A)\B
115141

116142
inv!(A::CholeskyRFP) = HermitianRFP(LAPACK2.pftri!(A.transr, A.uplo, A.data), A.transr, A.uplo)
117-
Base.LinAlg.inv(A::CholeskyRFP) = inv(copy(A))
143+
Base.LinAlg.inv(A::CholeskyRFP) = inv!(copy(A))
118144
Base.LinAlg.inv(A::HermitianRFP) = inv!(cholfact(A))

test/rectfullpacked.jl

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,70 @@
11
using Base.Test
2-
using LinearAlgebra
2+
import LinearAlgebra
3+
import LinearAlgebra: Ac_mul_A_RFP, TriangularRFP
34

45
@testset "Rectuangular Full Pack Format" begin
56

6-
@testset "Element type: $elty. Problem size: $n" for elty in (Float32, Float64, Complex{Float32}, Complex{Float64}),
7-
n in (6, 7)
7+
@testset "Core generic functionality" for n in (6, 7),
8+
uplo in (:U, :L)
9+
10+
A = rand(10, n)
11+
12+
@testset "Hermitian" begin
13+
AcA_RFP = Ac_mul_A_RFP(A, uplo)
14+
15+
@test size(AcA_RFP, 1) == n
16+
@test size(AcA_RFP, 2) == n
17+
@test size(AcA_RFP, 3) == 1
18+
@test_throws BoundsError AcA_RFP[0, 1]
19+
@test_throws BoundsError AcA_RFP[1, 0]
20+
@test_throws BoundsError AcA_RFP[n + 1, 1]
21+
@test_throws BoundsError AcA_RFP[1, n + 1]
22+
23+
@test AcA_RFP[2, 1] == AcA_RFP[1, 2]
24+
@test AcA_RFP[end - 2, end - 1] == AcA_RFP[end - 1, end - 2]
25+
end
26+
27+
@testset "Triangular" begin
28+
Atr_RFP = TriangularRFP(triu(A'A), uplo)
29+
@test size(Atr_RFP, 1) == n
30+
@test size(Atr_RFP, 2) == n
31+
@test size(Atr_RFP, 3) == 1
32+
33+
# Indexing not implemented yet for Atr_RFP
34+
# @test_throws BoundsError Atr_RFP[0, 1]
35+
# @test_throws BoundsError Atr_RFP[1, 0]
36+
# @test_throws BoundsError Atr_RFP[n + 1, 1]
37+
# @test_throws BoundsError Atr_RFP[1, n + 1]
38+
39+
@test_broken Atr_RFP[2, 1] == Atr_RFP[1, 2]
40+
@test_broken Atr_RFP[end - 2, end - 1] == Atr_RFP[end - 1, end - 2]
41+
end
42+
end
43+
44+
@testset "Hermitian with element type: $elty. Problem size: $n" for elty in (Float32, Float64, Complex{Float32}, Complex{Float64}),
45+
n in (6, 7),
46+
uplo in (:L, :U)
847

948
A = rand(elty, 10, n)
1049
AcA = A'A
11-
AcA_RFP = LinearAlgebra.Ac_mul_A_RFP(A)
50+
AcA_RFP = Ac_mul_A_RFP(A, uplo)
1251
o = ones(elty, n)
1352

1453
@test AcA A'A
1554
@test AcA\o AcA_RFP\o
1655
@test inv(AcA) inv(AcA_RFP)
56+
@test inv(cholfact(AcA)) inv(factorize(AcA_RFP))
57+
end
58+
59+
@testset "Hermitian with element type: $elty. Problem size: $n" for elty in (Float32, Float64, Complex{Float32}, Complex{Float64}),
60+
n in (6, 7),
61+
uplo in (:L, :U)
62+
63+
A = triu(rand(elty, n, n))
64+
A_RFP = TriangularRFP(A)
65+
o = ones(elty, n)
66+
67+
@test_broken A A_RFP
68+
@test A full(A_RFP)
1769
end
1870
end

0 commit comments

Comments
 (0)