forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbidiag.jl
284 lines (260 loc) · 11.4 KB
/
bidiag.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# This file is a part of Julia. License is MIT: http://julialang.org/license
using Base.Test
import Base.LinAlg: BlasReal, BlasFloat
debug = false
n = 10 #Size of test matrix
srand(1)
debug && println("Bidiagonal matrices")
for relty in (Int, Float32, Float64, BigFloat), elty in (relty, Complex{relty})
debug && println("elty is $(elty), relty is $(relty)")
if relty <: AbstractFloat
dv = convert(Vector{elty}, randn(n))
ev = convert(Vector{elty}, randn(n-1))
if (elty <: Complex)
dv += im*convert(Vector{elty}, randn(n))
ev += im*convert(Vector{elty}, randn(n-1))
end
elseif relty <: Integer
dv = convert(Vector{elty}, rand(1:10, n))
ev = convert(Vector{elty}, rand(1:10, n-1))
if (elty <: Complex)
dv += im*convert(Vector{elty}, rand(1:10, n))
ev += im*convert(Vector{elty}, rand(1:10, n-1))
end
end
debug && println("Test constructors")
@test Bidiagonal(dv,ev,'U') == Bidiagonal(dv,ev,true)
@test_throws ArgumentError Bidiagonal(dv,ev,'R')
@test_throws DimensionMismatch Bidiagonal(dv,ones(elty,n),true)
@test_throws ArgumentError Bidiagonal(dv,ev)
debug && println("getindex, setindex!, size, and similar")
BD = Bidiagonal(dv, ev, true)
@test_throws BoundsError BD[n+1,1]
@test BD[2,2] == dv[2]
@test BD[2,3] == ev[2]
@test_throws ArgumentError BD[2,1] = 1
@test_throws ArgumentError BD[3,1] = 1
cBD = copy(BD)
cBD[2,2] = BD[2,2]
@test BD == cBD
@test_throws ArgumentError size(BD,0)
@test size(BD,3) == 1
@test isa(similar(BD), Bidiagonal{elty})
@test isa(similar(BD, Int), Bidiagonal{Int})
@test isa(similar(BD, Int, (3,2)), Matrix{Int})
debug && println("show")
dstring = sprint(Base.print_matrix,BD.dv')
estring = sprint(Base.print_matrix,BD.ev')
@test sprint(show,BD) == "$(summary(BD)):\n diag:$dstring\n super:$estring"
BD = Bidiagonal(dv,ev,false)
@test sprint(show,BD) == "$(summary(BD)):\n diag:$dstring\n sub:$estring"
debug && println("Test upper and lower bidiagonal matrices")
for isupper in (true, false)
debug && println("isupper is: $(isupper)")
T = Bidiagonal(dv, ev, isupper)
@test size(T, 1) == size(T, 2) == n
@test size(T) == (n, n)
@test full(T) == diagm(dv) + diagm(ev, isupper?1:-1)
@test Bidiagonal(full(T), isupper) == T
@test big(T) == T
@test full(abs(T)) == abs(diagm(dv)) + abs(diagm(ev, isupper?1:-1))
@test full(real(T)) == real(diagm(dv)) + real(diagm(ev, isupper?1:-1))
@test full(imag(T)) == imag(diagm(dv)) + imag(diagm(ev, isupper?1:-1))
z = zeros(elty, n)
debug && println("Idempotent tests")
for func in (conj, transpose, ctranspose)
@test func(func(T)) == T
end
debug && println("triu and tril")
@test istril(Bidiagonal(dv,ev,'L'))
@test !istril(Bidiagonal(dv,ev,'U'))
@test tril!(Bidiagonal(dv,ev,'U'),-1) == Bidiagonal(zeros(dv),zeros(ev),'U')
@test tril!(Bidiagonal(dv,ev,'L'),-1) == Bidiagonal(zeros(dv),ev,'L')
@test tril!(Bidiagonal(dv,ev,'U'),-2) == Bidiagonal(zeros(dv),zeros(ev),'U')
@test tril!(Bidiagonal(dv,ev,'L'),-2) == Bidiagonal(zeros(dv),zeros(ev),'L')
@test tril!(Bidiagonal(dv,ev,'U'),1) == Bidiagonal(dv,ev,'U')
@test tril!(Bidiagonal(dv,ev,'L'),1) == Bidiagonal(dv,ev,'L')
@test tril!(Bidiagonal(dv,ev,'U')) == Bidiagonal(dv,zeros(ev),'U')
@test tril!(Bidiagonal(dv,ev,'L')) == Bidiagonal(dv,ev,'L')
@test_throws ArgumentError tril!(Bidiagonal(dv,ev,'U'),n+1)
@test istriu(Bidiagonal(dv,ev,'U'))
@test !istriu(Bidiagonal(dv,ev,'L'))
@test triu!(Bidiagonal(dv,ev,'L'),1) == Bidiagonal(zeros(dv),zeros(ev),'L')
@test triu!(Bidiagonal(dv,ev,'U'),1) == Bidiagonal(zeros(dv),ev,'U')
@test triu!(Bidiagonal(dv,ev,'U'),2) == Bidiagonal(zeros(dv),zeros(ev),'U')
@test triu!(Bidiagonal(dv,ev,'L'),2) == Bidiagonal(zeros(dv),zeros(ev),'L')
@test triu!(Bidiagonal(dv,ev,'U'),-1) == Bidiagonal(dv,ev,'U')
@test triu!(Bidiagonal(dv,ev,'L'),-1) == Bidiagonal(dv,ev,'L')
@test triu!(Bidiagonal(dv,ev,'L')) == Bidiagonal(dv,zeros(ev),'L')
@test triu!(Bidiagonal(dv,ev,'U')) == Bidiagonal(dv,ev,'U')
@test_throws ArgumentError triu!(Bidiagonal(dv,ev,'U'),n+1)
if relty <: AbstractFloat
c = convert(Matrix{elty}, randn(n,n))
b = convert(Matrix{elty}, randn(n, 2))
if (elty <: Complex)
b += im*convert(Matrix{elty}, randn(n, 2))
end
elseif relty <: Integer
c = convert(Matrix{elty}, rand(1:10, n, n))
b = convert(Matrix{elty}, rand(1:10, n, 2))
if (elty <: Complex)
b += im*convert(Matrix{elty}, rand(1:10, n, 2))
end
end
Tfull = full(T)
condT = cond(map(Complex128,Tfull))
promty = typeof((zero(relty)*zero(relty) + zero(relty)*zero(relty))/one(relty))
if relty != BigFloat
x = T.'\c.'
tx = Tfull.' \ c.'
elty <: AbstractFloat && @test norm(x-tx,Inf) <= 4*condT*max(eps()*norm(tx,Inf), eps(promty)*norm(x,Inf))
@test_throws DimensionMismatch T.'\b.'
x = T'\c.'
tx = Tfull' \ c.'
@test norm(x-tx,Inf) <= 4*condT*max(eps()*norm(tx,Inf), eps(promty)*norm(x,Inf))
@test_throws DimensionMismatch T'\b.'
x = T\c.'
tx = Tfull\c.'
@test norm(x-tx,Inf) <= 4*condT*max(eps()*norm(tx,Inf), eps(promty)*norm(x,Inf))
@test_throws DimensionMismatch T\b.'
end
@test_throws DimensionMismatch T \ ones(elty,n+1,2)
@test_throws DimensionMismatch T.' \ ones(elty,n+1,2)
@test_throws DimensionMismatch T' \ ones(elty,n+1,2)
let bb = b, cc = c
for atype in ("Array", "SubArray")
if atype == "Array"
b = bb
c = cc
else
b = sub(bb, 1:n)
c = sub(cc, 1:n, 1:2)
end
end
debug && println("Linear solver")
x = T \ b
tx = Tfull \ b
@test_throws DimensionMismatch Base.LinAlg.naivesub!(T,ones(elty,n+1))
@test norm(x-tx,Inf) <= 4*condT*max(eps()*norm(tx,Inf), eps(promty)*norm(x,Inf))
debug && println("Generic Mat-vec ops")
@test_approx_eq T*b Tfull*b
@test_approx_eq T'*b Tfull'*b
if relty != BigFloat # not supported by pivoted QR
@test_approx_eq T/b' Tfull/b'
end
end
debug && println("Round,float,trunc,ceil")
if elty <: BlasReal
@test floor(Int,T) == Bidiagonal(floor(Int,T.dv),floor(Int,T.ev),T.isupper)
@test isa(floor(Int,T), Bidiagonal)
@test trunc(Int,T) == Bidiagonal(trunc(Int,T.dv),trunc(Int,T.ev),T.isupper)
@test isa(trunc(Int,T), Bidiagonal)
@test round(Int,T) == Bidiagonal(round(Int,T.dv),round(Int,T.ev),T.isupper)
@test isa(round(Int,T), Bidiagonal)
@test ceil(Int,T) == Bidiagonal(ceil(Int,T.dv),ceil(Int,T.ev),T.isupper)
@test isa(ceil(Int,T), Bidiagonal)
end
debug && println("Diagonals")
@test diag(T,2) == zeros(elty, n-2)
@test_throws ArgumentError diag(T,n+1)
debug && println("Eigensystems")
if relty <: AbstractFloat
d1, v1 = eig(T)
d2, v2 = eig(map(elty<:Complex ? Complex128 : Float64,Tfull))
@test_approx_eq isupper?d1:reverse(d1) d2
if elty <: Real
Test.test_approx_eq_modphase(v1, isupper?v2:v2[:,n:-1:1])
end
end
debug && println("Singular systems")
if (elty <: BlasReal)
@test_approx_eq full(svdfact(T)) full(svdfact!(copy(Tfull)))
@test_approx_eq svdvals(Tfull) svdvals(T)
u1, d1, v1 = svd(Tfull)
u2, d2, v2 = svd(T)
@test_approx_eq d1 d2
if elty <: Real
Test.test_approx_eq_modphase(u1, u2)
Test.test_approx_eq_modphase(v1, v2)
end
@test_approx_eq_eps 0 vecnorm(u2*diagm(d2)*v2'-Tfull) n*max(n^2*eps(relty), vecnorm(u1*diagm(d1)*v1' - Tfull))
@inferred svdvals(T)
@inferred svd(T)
end
debug && println("Binary operations")
@test -T == Bidiagonal(-T.dv,-T.ev,T.isupper)
@test convert(elty,-1.0) * T == Bidiagonal(-T.dv,-T.ev,T.isupper)
@test T * convert(elty,-1.0) == Bidiagonal(-T.dv,-T.ev,T.isupper)
for isupper2 in (true, false)
dv = convert(Vector{elty}, relty <: AbstractFloat ? randn(n) : rand(1:10, n))
ev = convert(Vector{elty}, relty <: AbstractFloat ? randn(n-1) : rand(1:10, n-1))
T2 = Bidiagonal(dv, ev, isupper2)
Tfull2 = full(T2)
for op in (+, -, *)
@test_approx_eq full(op(T, T2)) op(Tfull, Tfull2)
end
end
debug && println("Inverse")
@test_approx_eq inv(T)*Tfull eye(elty,n)
end
@test Matrix{Complex{Float64}}(BD) == BD
end
# Issue 10742 and similar
let A = Bidiagonal([1,2,3], [0,0], true)
@test istril(A)
@test isdiag(A)
end
# test construct from range
@test Bidiagonal(1:3, 1:2, true) == [1 1 0; 0 2 2; 0 0 3]
#test promote_rule
A = Bidiagonal(ones(Float32,10),ones(Float32,9),true)
B = rand(Float64,10,10)
C = Tridiagonal(rand(Float64,9),rand(Float64,10),rand(Float64,9))
@test promote_rule(Matrix{Float64}, Bidiagonal{Float64}) == Matrix{Float64}
@test promote(B,A) == (B,convert(Matrix{Float64},full(A)))
@test promote(C,A) == (C,Tridiagonal(zeros(Float64,9),convert(Vector{Float64},A.dv),convert(Vector{Float64},A.ev)))
import Base.LinAlg: fillslots!, UnitLowerTriangular
let #fill!
let # fillslots!
A = Tridiagonal(randn(2), randn(3), randn(2))
@test fillslots!(A, 3) == Tridiagonal([3, 3.], [3, 3, 3.], [3, 3.])
B = Bidiagonal(randn(3), randn(2), true)
@test fillslots!(B, 2) == Bidiagonal([2.,2,2], [2,2.], true)
S = SymTridiagonal(randn(3), randn(2))
@test fillslots!(S, 1) == SymTridiagonal([1,1,1.], [1,1.])
Ult = UnitLowerTriangular(randn(3,3))
@test fillslots!(Ult, 3) == UnitLowerTriangular([1 0 0; 3 1 0; 3 3 1])
end
let # fill!(exotic, 0)
exotic_arrays = Any[Tridiagonal(randn(3), randn(4), randn(3)),
Bidiagonal(randn(3), randn(2), rand(Bool)),
SymTridiagonal(randn(3), randn(2)),
sparse(randn(3,4)),
Diagonal(randn(5)),
sparse(rand(3)),
LowerTriangular(randn(3,3)),
UpperTriangular(randn(3,3))
]
for A in exotic_arrays
fill!(A, 0)
for a in A
@test a == 0
end
end
end
let # fill!(small, x)
val = randn()
b = Bidiagonal(randn(1,1), true)
st = SymTridiagonal(randn(1,1))
for x in (b, st)
@test full(fill!(x, val)) == fill!(full(x), val)
end
b = Bidiagonal(randn(2,2), true)
st = SymTridiagonal(randn(3), randn(2))
t = Tridiagonal(randn(3,3))
for x in (b, t, st)
@test_throws ArgumentError fill!(x, val)
@test full(fill!(x, 0)) == fill!(full(x), 0)
end
end
end