Skip to content

Commit 7ab6428

Browse files
kshyattandreasnoack
authored andcommitted
A few mini tests for linalg (#20613)
* Tests for missing broadcast methods * Tests for a few more ConjArray features * More tests for LU too * Broadcast and rowvec tests for bidiag * Add throw for RowVectors and choleskys and tests * Add type stability tests for tridiag * copy! and logabsdet tests for triangular
1 parent e948b41 commit 7ab6428

File tree

6 files changed

+61
-2
lines changed

6 files changed

+61
-2
lines changed

test/linalg/bidiag.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ srand(1)
134134
@test_throws DimensionMismatch T \ ones(elty,n+1,2)
135135
@test_throws DimensionMismatch T.' \ ones(elty,n+1,2)
136136
@test_throws DimensionMismatch T' \ ones(elty,n+1,2)
137+
138+
@test_throws DimensionMismatch T \ RowVector(ones(elty,n+1))
139+
@test_throws DimensionMismatch T.' \ RowVector(ones(elty,n+1))
140+
@test_throws DimensionMismatch T' \ RowVector(ones(elty,n+1))
141+
@test_throws DimensionMismatch Base.LinAlg.At_ldiv_B(T, RowVector(ones(elty,n+1)))
142+
@test_throws DimensionMismatch Base.LinAlg.Ac_ldiv_B(T, RowVector(ones(elty,n+1)))
137143
let bb = b, cc = c
138144
for atype in ("Array", "SubArray")
139145
if atype == "Array"
@@ -168,6 +174,14 @@ srand(1)
168174
@test isa(round.(Int, T), Bidiagonal)
169175
@test ceil.(Int,T) == Bidiagonal(ceil.(Int,T.dv), ceil.(Int,T.ev), T.isupper)
170176
@test isa(ceil.(Int,T), Bidiagonal)
177+
@test floor.(T) == Bidiagonal(floor.(T.dv), floor.(T.ev), T.isupper)
178+
@test isa(floor.(T), Bidiagonal)
179+
@test trunc.(T) == Bidiagonal(trunc.(T.dv), trunc.(T.ev), T.isupper)
180+
@test isa(trunc.(T), Bidiagonal)
181+
@test round.(T) == Bidiagonal(round.(T.dv), round.(T.ev), T.isupper)
182+
@test isa(round.(T), Bidiagonal)
183+
@test ceil.(T) == Bidiagonal(ceil.(T.dv), ceil.(T.ev), T.isupper)
184+
@test isa(ceil.(T), Bidiagonal)
171185
end
172186
end
173187

test/linalg/cholesky.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ debug && println("\ntype of a: ", eltya, " type of b: ", eltyb, "\n")
169169
@test norm(apd * (lapd\b) - b)/norm(b) <= ε*κ*n
170170
@test norm(apd * (lapd\b[1:n]) - b[1:n])/norm(b[1:n]) <= ε*κ*n
171171
end
172+
@test_throws DimensionMismatch lapd\RowVector(ones(n))
172173

173174
debug && println("pivoted Cholesky decomposition")
174175
if eltya != BigFloat && eltyb != BigFloat # Note! Need to implement pivoted Cholesky decomposition in julia
@@ -179,6 +180,8 @@ debug && println("pivoted Cholesky decomposition")
179180
lpapd = cholfact(apd, :L, Val{true})
180181
@test norm(apd * (lpapd\b) - b)/norm(b) <= ε*κ*n # Ad hoc, revisit
181182
@test norm(apd * (lpapd\b[1:n]) - b[1:n])/norm(b[1:n]) <= ε*κ*n
183+
184+
@test_throws BoundsError lpapd\RowVector(ones(n))
182185
end
183186
end
184187
end

test/linalg/conjarray.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22

33
@testset "Core" begin
44
m = [1+im 2; 2 4-im]
5-
cm = ConjArray(m)
5+
cm = ConjMatrix(m)
66
@test cm[1,1] == 1-im
7+
@test cm[1] == 1-im
78
@test trace(cm*m) == 27
9+
@test cm' == m
10+
11+
cm[:,2] = [3; 3-im] #setindex! with a vector
12+
@test conj(cm) == [1+im 3; 2 3+im]
813

914
v = [[1+im], [1-im]]
10-
cv = ConjArray(v)
15+
cv = ConjVector(v)
1116
@test cv[1] == [1-im]
1217
end
1318

test/linalg/lu.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ debug && println("(Automatic) Square LU decomposition. eltya: $eltya, eltyb: $el
6464
@test l*u a[p,:]
6565
@test (l*u)[invperm(p),:] a
6666
@test a * inv(lua) eye(n)
67+
68+
lstring = sprint(show,l)
69+
ustring = sprint(show,u)
70+
@test sprint(show,lua) == "$(typeof(lua)) with factors L and U:\n$lstring\n$ustring"
6771
let Bs = b, Cs = c
6872
for atype in ("Array", "SubArray")
6973
if atype == "Array"
@@ -102,6 +106,7 @@ debug && println("Tridiagonal LU")
102106
@test_throws DimensionMismatch lud\f
103107
@test_throws DimensionMismatch lud.'\f
104108
@test_throws DimensionMismatch lud'\f
109+
@test_throws DimensionMismatch Base.LinAlg.At_ldiv_B!(lud, f)
105110
let Bs = b
106111
for atype in ("Array", "SubArray")
107112
if atype == "Array"
@@ -113,6 +118,9 @@ debug && println("Tridiagonal LU")
113118
@test norm(d*(lud\b) - b, 1) < ε*κd*n*2 # Two because the right hand side has two columns
114119
if eltya <: Real
115120
@test norm((lud.'\b) - Array(d.')\b, 1) < ε*κd*n*2 # Two because the right hand side has two columns
121+
if eltya != Int && eltyb != Int
122+
@test norm(Base.LinAlg.At_ldiv_B!(lud, copy(b)) - Array(d.')\b, 1) < ε*κd*n*2
123+
end
116124
end
117125
if eltya <: Complex
118126
@test norm((lud'\b) - Array(d')\b, 1) < ε*κd*n*2 # Two because the right hand side has two columns

test/linalg/triangular.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ for elty1 in (Float32, Float64, BigFloat, Complex64, Complex128, Complex{BigFloa
4545
@test isa(similar(A1, (3,2)), Matrix{elty1})
4646
@test isa(similar(A1, Int, (3,2)), Matrix{Int})
4747

48+
#copy!
49+
simA1 = similar(A1)
50+
copy!(simA1, A1)
51+
@test simA1 == A1
52+
4853
# getindex
4954
## Linear indexing
5055
for i = 1:length(A1)
@@ -226,6 +231,10 @@ for elty1 in (Float32, Float64, BigFloat, Complex64, Complex128, Complex{BigFloa
226231
# Determinant
227232
@test det(A1) det(lufact(full(A1))) atol=sqrt(eps(real(float(one(elty1)))))*n*n
228233
@test logdet(A1) logdet(lufact(full(A1))) atol=sqrt(eps(real(float(one(elty1)))))*n*n
234+
lada, ladb = logabsdet(A1)
235+
flada, fladb = logabsdet(lufact(full(A1)))
236+
@test lada flada atol=sqrt(eps(real(float(one(elty1)))))*n*n
237+
@test ladb fladb atol=sqrt(eps(real(float(one(elty1)))))*n*n
229238

230239
# Matrix square root
231240
@test sqrtm(A1) |> t -> t*t A1

test/linalg/tridiag.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,29 @@ for elty in (Float32, Float64, Complex64, Complex128, Int)
5050
@test ctranspose(T) == Tridiagonal(conj(du), conj(d), conj(dl))
5151

5252
@test abs.(T) == Tridiagonal(abs.(dl),abs.(d),abs.(du))
53+
if elty <: Real
54+
@test round.(T) == Tridiagonal(round.(dl),round.(d),round.(du))
55+
@test isa(round.(T), Tridiagonal)
56+
@test trunc.(T) == Tridiagonal(trunc.(dl),trunc.(d),trunc.(du))
57+
@test isa(trunc.(T), Tridiagonal)
58+
@test floor.(T) == Tridiagonal(floor.(dl),floor.(d),floor.(du))
59+
@test isa(floor.(T), Tridiagonal)
60+
@test ceil.(T) == Tridiagonal(ceil.(dl),ceil.(d),ceil.(du))
61+
@test isa(ceil.(T), Tridiagonal)
62+
end
5363
@test real(T) == Tridiagonal(real(dl),real(d),real(du))
5464
@test imag(T) == Tridiagonal(imag(dl),imag(d),imag(du))
5565
@test abs.(Ts) == SymTridiagonal(abs.(d),abs.(dl))
66+
if elty <: Real
67+
@test round.(Ts) == SymTridiagonal(round.(d),round.(dl))
68+
@test isa(round.(Ts), SymTridiagonal)
69+
@test trunc.(Ts) == SymTridiagonal(trunc.(d),trunc.(dl))
70+
@test isa(trunc.(Ts), SymTridiagonal)
71+
@test floor.(Ts) == SymTridiagonal(floor.(d),floor.(dl))
72+
@test isa(floor.(Ts), SymTridiagonal)
73+
@test ceil.(Ts) == SymTridiagonal(ceil.(d),ceil.(dl))
74+
@test isa(ceil.(Ts), SymTridiagonal)
75+
end
5676
@test real(Ts) == SymTridiagonal(real(d),real(dl))
5777
@test imag(Ts) == SymTridiagonal(imag(d),imag(dl))
5878

0 commit comments

Comments
 (0)