Skip to content

Commit aeb31de

Browse files
committed
Merge pull request #15354 from pkofod/pkm/SubArrays
Add tests for SubArrays in test/linalg/bunchkaufman.jl
2 parents 1933dc7 + fde21d3 commit aeb31de

File tree

5 files changed

+116
-77
lines changed

5 files changed

+116
-77
lines changed

base/linalg/dense.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ function inv{T}(A::StridedMatrix{T})
381381
return convert(typeof(parent(Ai)), Ai)
382382
end
383383

384-
function factorize{T}(A::Matrix{T})
384+
function factorize{T}(A::StridedMatrix{T})
385385
m, n = size(A)
386386
if m == n
387387
if m == 1 return A[1] end

base/linalg/factorization.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ inv{T}(F::Factorization{T}) = A_ldiv_B!(F, eye(T, size(F,1)))
2424
# With a real lhs and complex rhs with the same precision, we can reinterpret
2525
# the complex rhs as a real rhs with twice the number of columns
2626
function (\){T<:BlasReal}(F::Factorization{T}, B::AbstractVector{Complex{T}})
27-
c2r = reshape(transpose(reinterpret(T, B, (2, length(B)))), size(B, 1), 2*size(B, 2))
27+
c2r = reshape(transpose(reinterpret(T, parent(B), (2, length(B)))), size(B, 1), 2*size(B, 2))
2828
x = A_ldiv_B!(F, c2r)
2929
return reinterpret(Complex{T}, transpose(reshape(x, div(length(x), 2), 2)), (size(F,2),))
3030
end
3131
function (\){T<:BlasReal}(F::Factorization{T}, B::AbstractMatrix{Complex{T}})
32-
c2r = reshape(transpose(reinterpret(T, B, (2, length(B)))), size(B, 1), 2*size(B, 2))
32+
c2r = reshape(transpose(reinterpret(T, parent(B), (2, length(B)))), size(B, 1), 2*size(B, 2))
3333
x = A_ldiv_B!(F, c2r)
3434
return reinterpret(Complex{T}, transpose(reshape(x, div(length(x), 2), 2)), (size(F,2), size(B,2)))
3535
end

test/linalg/bunchkaufman.jl

Lines changed: 63 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,41 +23,62 @@ bimg = randn(n,2)/2
2323
for eltya in (Float32, Float64, Complex64, Complex128, Int)
2424
a = eltya == Int ? rand(1:7, n, n) : convert(Matrix{eltya}, eltya <: Complex ? complex(areal, aimg) : areal)
2525
a2 = eltya == Int ? rand(1:7, n, n) : convert(Matrix{eltya}, eltya <: Complex ? complex(a2real, a2img) : a2real)
26-
asym = a'+a # symmetric indefinite
27-
apd = a'*a # symmetric positive-definite
28-
ε = εa = eps(abs(float(one(eltya))))
29-
30-
for eltyb in (Float32, Float64, Complex64, Complex128, Int)
31-
b = eltyb == Int ? rand(1:5, n, 2) : convert(Matrix{eltyb}, eltyb <: Complex ? complex(breal, bimg) : breal)
32-
εb = eps(abs(float(one(eltyb))))
33-
ε = max(εa,εb)
34-
35-
debug && println("\ntype of a: ", eltya, " type of b: ", eltyb, "\n")
36-
37-
debug && println("(Automatic) Bunch-Kaufman factor of indefinite matrix")
38-
bc1 = factorize(asym)
39-
@test_approx_eq inv(bc1) * asym eye(n)
40-
@test_approx_eq_eps asym * (bc1\b) b 1000ε
41-
for rook in (false, true)
42-
@test_approx_eq inv(bkfact(a.' + a, :U, true, rook)) * (a.' + a) eye(n)
43-
@test size(bc1) == size(bc1.LD)
44-
@test size(bc1,1) == size(bc1.LD,1)
45-
@test size(bc1,2) == size(bc1.LD,2)
46-
if eltya <: BlasReal
47-
@test_throws ArgumentError bkfact(a)
48-
end
26+
for atype in ("Array", "SubArray")
27+
asym = a'+a # symmetric indefinite
28+
apd = a'*a # symmetric positive-definite
29+
if atype == "Array"
30+
a = a
31+
a2 = a2
32+
else
33+
a = sub(a, 1:n, 1:n)
34+
a2 = sub(a2, 1:n, 1:n)
35+
asym = sub(asym, 1:n, 1:n)
36+
apd = sub(apd, 1:n, 1:n)
4937
end
38+
ε = εa = eps(abs(float(one(eltya))))
39+
40+
for eltyb in (Float32, Float64, Complex64, Complex128, Int)
41+
b = eltyb == Int ? rand(1:5, n, 2) : convert(Matrix{eltyb}, eltyb <: Complex ? complex(breal, bimg) : breal)
42+
for btype in ("Array", "SubArray")
43+
if btype == "Array"
44+
b = b
45+
else
46+
b = sub(b, 1:n, 1:2)
47+
end
48+
49+
εb = eps(abs(float(one(eltyb))))
50+
ε = max(εa,εb)
51+
52+
debug && println("\neltype of a: ", eltya, " eltype of b: ", eltyb)
53+
debug && println(" type of a: ", atype, " type of b: ", btype, "\n")
54+
55+
debug && println("(Automatic) Bunch-Kaufman factor of indefinite matrix")
56+
bc1 = factorize(asym)
57+
@test_approx_eq inv(bc1) * asym eye(n)
58+
@test_approx_eq_eps asym * (bc1\b) b 1000ε
59+
for rook in (false, true)
60+
@test_approx_eq inv(bkfact(a.' + a, :U, true, rook)) * (a.' + a) eye(n)
61+
@test size(bc1) == size(bc1.LD)
62+
@test size(bc1,1) == size(bc1.LD,1)
63+
@test size(bc1,2) == size(bc1.LD,2)
64+
if eltya <: BlasReal
65+
@test_throws ArgumentError bkfact(a)
66+
end
67+
end
5068

51-
debug && println("Bunch-Kaufman factors of a pos-def matrix")
52-
for rook in (false, true)
53-
bc2 = bkfact(apd, :U, issymmetric(apd), rook)
54-
@test_approx_eq inv(bc2) * apd eye(n)
55-
@test_approx_eq_eps apd * (bc2\b) b 150000ε
56-
@test ishermitian(bc2) == !issymmetric(bc2)
69+
debug && println("Bunch-Kaufman factors of a pos-def matrix")
70+
for rook in (false, true)
71+
bc2 = bkfact(apd, :U, issymmetric(apd), rook)
72+
@test_approx_eq inv(bc2) * apd eye(n)
73+
@test_approx_eq_eps apd * (bc2\b) b 150000ε
74+
@test ishermitian(bc2) == !issymmetric(bc2)
75+
end
76+
end
5777
end
5878
end
5979
end
6080

81+
6182
debug && println("Bunch-Kaufman factors of a singular matrix")
6283
let
6384
As1 = ones(n, n)
@@ -67,11 +88,19 @@ let
6788
As3[1, end] -= im
6889

6990
for As = (As1, As2, As3)
70-
for rook in (false, true)
71-
F = bkfact(As, :U, issymmetric(As), rook)
72-
@test det(F) == 0
73-
@test_throws LinAlg.SingularException inv(F)
74-
@test_throws LinAlg.SingularException F \ ones(size(As, 1))
91+
for Astype in ("Array", "SubArray")
92+
if Astype == "Array"
93+
As = As
94+
else
95+
As = sub(As, 1:n, 1:n)
96+
end
97+
98+
for rook in (false, true)
99+
F = bkfact(As, :U, issymmetric(As), rook)
100+
@test det(F) == 0
101+
@test_throws LinAlg.SingularException inv(F)
102+
@test_throws LinAlg.SingularException F \ ones(size(As, 1))
103+
end
75104
end
76105
end
77106
end

test/linalg/givens.jl

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,50 +15,60 @@ for elty in (Float32, Float64, Complex64, Complex128)
1515
else
1616
A = convert(Matrix{elty}, complex(randn(10,10),randn(10,10)))
1717
end
18-
Ac = copy(A)
19-
R = Base.LinAlg.Rotation(Base.LinAlg.Givens{elty}[])
20-
for j = 1:8
21-
for i = j+2:10
22-
G, _ = givens(A, j+1, i, j)
23-
A_mul_B!(G, A)
24-
A_mul_Bc!(A, G)
25-
A_mul_B!(G, R)
18+
for Atype in ("Array", "SubArray")
19+
if Atype == "Array"
20+
A = A
21+
else
22+
A = sub(A, 1:10, 1:10)
23+
end
24+
Ac = copy(A)
25+
R = Base.LinAlg.Rotation(Base.LinAlg.Givens{elty}[])
26+
for j = 1:8
27+
for i = j+2:10
28+
G, _ = givens(A, j+1, i, j)
29+
A_mul_B!(G, A)
30+
A_mul_Bc!(A, G)
31+
A_mul_B!(G, R)
2632

27-
@test A_mul_B!(G,eye(elty,10,10)) == [G[i,j] for i=1:10,j=1:10]
33+
@test A_mul_B!(G,eye(elty,10,10)) == [G[i,j] for i=1:10,j=1:10]
2834

29-
# test transposes
30-
@test_approx_eq ctranspose(G)*G*eye(10) eye(elty, 10)
31-
@test_approx_eq ctranspose(R)*(R*eye(10)) eye(elty, 10)
32-
@test_throws ErrorException transpose(G)
33-
@test_throws ErrorException transpose(R)
35+
# test transposes
36+
@test_approx_eq ctranspose(G)*G*eye(10) eye(elty, 10)
37+
@test_approx_eq ctranspose(R)*(R*eye(10)) eye(elty, 10)
38+
@test_throws ErrorException transpose(G)
39+
@test_throws ErrorException transpose(R)
40+
end
3441
end
35-
end
36-
@test_throws ArgumentError givens(A, 3, 3, 2)
37-
@test_throws ArgumentError givens(one(elty),zero(elty),2,2)
38-
G, _ = givens(one(elty),zero(elty),11,12)
39-
@test_throws DimensionMismatch A_mul_B!(G, A)
40-
@test_throws DimensionMismatch A_mul_Bc!(A,G)
41-
@test_approx_eq abs(A) abs(hessfact(Ac)[:H])
42-
@test_approx_eq norm(R*eye(elty, 10)) one(elty)
42+
@test_throws ArgumentError givens(A, 3, 3, 2)
43+
@test_throws ArgumentError givens(one(elty),zero(elty),2,2)
44+
G, _ = givens(one(elty),zero(elty),11,12)
45+
@test_throws DimensionMismatch A_mul_B!(G, A)
46+
@test_throws DimensionMismatch A_mul_Bc!(A,G)
47+
@test_approx_eq abs(A) abs(hessfact(Ac)[:H])
48+
@test_approx_eq norm(R*eye(elty, 10)) one(elty)
4349

44-
G, _ = givens(one(elty),zero(elty),9,10)
45-
@test_approx_eq ctranspose(G*eye(elty,10))*(G*eye(elty,10)) eye(elty, 10)
46-
K, _ = givens(zero(elty),one(elty),9,10)
47-
@test_approx_eq ctranspose(K*eye(elty,10))*(K*eye(elty,10)) eye(elty, 10)
50+
G, _ = givens(one(elty),zero(elty),9,10)
51+
@test_approx_eq ctranspose(G*eye(elty,10))*(G*eye(elty,10)) eye(elty, 10)
52+
K, _ = givens(zero(elty),one(elty),9,10)
53+
@test_approx_eq ctranspose(K*eye(elty,10))*(K*eye(elty,10)) eye(elty, 10)
4854

49-
# test that Givens * work for vectors
50-
x = A[:,1]
51-
G, r = givens(x[2], x[4], 2, 4)
52-
@test (G*x)[2] r
53-
@test abs((G*x)[4]) < eps(real(elty))
55+
# test that Givens * work for vectors
56+
if Atype == "Array"
57+
x = A[:, 1]
58+
else
59+
x = sub(A, 1:10, 1)
60+
end
61+
G, r = givens(x[2], x[4], 2, 4)
62+
@test (G*x)[2] r
63+
@test abs((G*x)[4]) < eps(real(elty))
5464

55-
x = A[:,1]
56-
G, r = givens(x, 2, 4)
57-
@test (G*x)[2] r
58-
@test abs((G*x)[4]) < eps(real(elty))
65+
G, r = givens(x, 2, 4)
66+
@test (G*x)[2] r
67+
@test abs((G*x)[4]) < eps(real(elty))
5968

60-
G, r = givens(x, 4, 2)
61-
@test (G*x)[4] r
62-
@test abs((G*x)[2]) < eps(real(elty))
69+
G, r = givens(x, 4, 2)
70+
@test (G*x)[4] r
71+
@test abs((G*x)[2]) < eps(real(elty))
72+
end
6373
end
6474
end #let

test/linalg/svd.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ a2img = randn(n,n)/2
2121
for eltya in (Float32, Float64, Complex64, Complex128, Int)
2222
aa = eltya == Int ? rand(1:7, n, n) : convert(Matrix{eltya}, eltya <: Complex ? complex(areal, aimg) : areal)
2323
aa2 = eltya == Int ? rand(1:7, n, n) : convert(Matrix{eltya}, eltya <: Complex ? complex(a2real, a2img) : a2real)
24+
asym = aa'+aa # symmetric indefinite
25+
apd = aa'*aa # symmetric positive-definite
2426
for atype in ("Array", "SubArray")
2527
if atype == "Array"
2628
a = aa
@@ -29,8 +31,6 @@ for eltya in (Float32, Float64, Complex64, Complex128, Int)
2931
a = sub(aa, 1:n, 1:n)
3032
a2 = sub(aa2, 1:n, 1:n)
3133
end
32-
asym = a'+a # symmetric indefinite
33-
apd = a'*a # symmetric positive-definite
3434
ε = εa = eps(abs(float(one(eltya))))
3535

3636
debug && println("\ntype of a: ", eltya, "\n")

0 commit comments

Comments
 (0)