Skip to content
This repository was archived by the owner on Apr 26, 2021. It is now read-only.

Commit 7e31a5d

Browse files
committed
test Quaternions
1 parent ad9be33 commit 7e31a5d

File tree

6 files changed

+117
-92
lines changed

6 files changed

+117
-92
lines changed

README.md

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

33
[![Build Status](https://travis-ci.org/simonbyrne/GenericSVD.jl.svg?branch=master)](https://travis-ci.org/simonbyrne/GenericSVD.jl)
44

5-
Implements Singular Value Decomposition for generic number types, such as `BigFloat` and `Complex{BigFloat}`. It internally overloads several Base functions such that existing methods (`svd`, `svdfact` and `svdvals`) should work directly.
5+
Implements Singular Value Decomposition for generic number types, such as `BigFloat`, `Complex{BigFloat}` or [`Quaternion`s](https://github.com/JuliaGeometry/Quaternions.jl). It internally overloads several Base functions such that existing methods (`svd`, `svdfact` and `svdvals`) should work directly.
66

77
It uses a Golub-Kahan 2-stage algorithm of bidiagonalization with Householder reflections, followed by an implicit QR with shift.
88

@@ -12,4 +12,4 @@ Based on initial code by Andreas Noack.
1212

1313
## References
1414

15-
* Golub, G. H. and Van Loan, C. F. (1996), "§8.6.2 The SVD Algorithm", *Matrix Computations* (3rd Ed.), Johns Hopkins University Press, Baltimore, MD, USA.
15+
* Golub, G. H. and Van Loan, C. F. (2013), "§8.6.3 The SVD Algorithm", *Matrix Computations* (4th ed.), Johns Hopkins University Press, Baltimore, MD, USA.

test/REQUIRE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Quaternions

test/bigfloat.jl

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using GenericSVD
2+
using Base.Test
3+
4+
srand(1)
5+
6+
n,m = 100,20
7+
8+
X = randn(n,m)
9+
bX = big(X)
10+
bS = svdfact(bX)
11+
@test isapprox(full(bS), bX, rtol=1e3*eps(BigFloat))
12+
@test isapprox(svdvals(bX), svdvals(X), rtol=1e3*eps())
13+
@test bX == X # check we didn't modify the input
14+
15+
bY = big(randn(n))
16+
@test isapprox(qrfact(bX,Val{false}) \ bY, bS \ bY, rtol=1e3*eps(BigFloat))
17+
@test bX == X # check we didn't modify the input
18+
19+
bXt = bX'
20+
bSt = svdfact(bXt)
21+
@test isapprox(full(bSt), bXt, rtol=1e3*eps(BigFloat))
22+
@test isapprox(svdvals(bXt), svdvals(X), rtol=1e3*eps())
23+
@test bXt == X' # check we didn't modify the input
24+
25+
X = Float64[1 2 0; 0 1 2; 0 0 0]
26+
bX = big(X)
27+
bS = svdfact(bX)
28+
@test isapprox(full(bS), bX, rtol=1e3*eps(BigFloat))
29+
@test isapprox(svdvals(bX), svdvals(X), rtol=1e3*eps())
30+
@test bX == X # check we didn't modify the input
31+
32+
X = Float64[0 2 0; 0 1 2; 0 0 1]
33+
bX = big(X)
34+
bS = svdfact(bX)
35+
@test isapprox(full(bS), bX, rtol=1e3*eps(BigFloat))
36+
@test isapprox(svdvals(bX), svdvals(X), rtol=1e3*eps())
37+
@test bX == X # check we didn't modify the input
38+
39+
40+
bD = big(randn(m))
41+
bX = diagm(bD)
42+
bS = svdfact(bX)
43+
44+
@test isapprox(full(bS), bX, rtol=1e3*eps(BigFloat))
45+
@test bS.S == sort(abs(bD),rev=true)
46+
47+
48+
49+
50+
X = randn(n,m)+im*randn(n,m)
51+
bX = big(X)
52+
bS = svdfact(bX)
53+
@test isapprox(full(bS), bX, rtol=1e3*eps(BigFloat))
54+
@test isapprox(svdvals(bX), svdvals(X), rtol=1e3*eps())
55+
@test bX == X # check we didn't modify the input
56+
57+
bXt = bX'
58+
bSt = svdfact(bXt)
59+
@test isapprox(full(bSt), bXt, rtol=1e3*eps(BigFloat))
60+
@test isapprox(svdvals(bXt), svdvals(X), rtol=1e3*eps())
61+
@test bXt == X' # check we didn't modify the input

test/misc.jl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using GenericSVD
2+
using Base.Test
3+
4+
srand(1)
5+
6+
a = randexp()
7+
b = randexp()
8+
c = randn()
9+
U = [a c; 0 b]
10+
x,y = GenericSVD.svdvals2x2(a,b,c)
11+
12+
@test sort(sqrt(eigvals(U'*U))) [x,y]
13+
@test sort(svdvals(U)) [x,y]
14+
15+
U = eye(3)
16+
B = Bidiagonal([0.0,1.0,2.0],[3.0,4.0],true)
17+
B1 = copy(B)
18+
19+
GenericSVD.svd_zerodiag_row!(U,B,1,3)
20+
@test B[1,1] == 0
21+
@test B[1,2] == 0
22+
@test U*full(B) B1
23+
24+
Vt = eye(3)
25+
B = Bidiagonal([1.0,2.0,0.0],[3.0,4.0],true)
26+
B1 = copy(B)
27+
28+
GenericSVD.svd_zerodiag_col!(B,Vt,1,3)
29+
@test B[3,3] == 0
30+
@test B[2,3] == 0
31+
@test full(B)*Vt B1
32+

test/quaternions.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using GenericSVD
2+
using Quaternions
3+
using Base.Test
4+
5+
6+
n,m = 100,20
7+
8+
X = [quatrand() for i=1:n, j=1:m]
9+
10+
S = svdfact(X)
11+
@test isapprox(full(S), X, rtol=1e3*eps())
12+
13+
Xt = X'
14+
St = svdfact(Xt)
15+
@test isapprox(full(St), Xt, rtol=1e3*eps())

test/runtests.jl

Lines changed: 6 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,6 @@
1-
using GenericSVD
2-
using Base.Test
3-
4-
srand(1)
5-
6-
a = randexp()
7-
b = randexp()
8-
c = randn()
9-
U = [a c; 0 b]
10-
x,y = GenericSVD.svdvals2x2(a,b,c)
11-
12-
@test sort(sqrt(eigvals(U'*U))) [x,y]
13-
@test sort(svdvals(U)) [x,y]
14-
15-
U = eye(3)
16-
B = Bidiagonal([0.0,1.0,2.0],[3.0,4.0],true)
17-
B1 = copy(B)
18-
19-
GenericSVD.svd_zerodiag_row!(U,B,1,3)
20-
@test B[1,1] == 0
21-
@test B[1,2] == 0
22-
@test U*full(B) B1
23-
24-
Vt = eye(3)
25-
B = Bidiagonal([1.0,2.0,0.0],[3.0,4.0],true)
26-
B1 = copy(B)
27-
28-
GenericSVD.svd_zerodiag_col!(B,Vt,1,3)
29-
@test B[3,3] == 0
30-
@test B[2,3] == 0
31-
@test full(B)*Vt B1
32-
33-
34-
35-
n,m = 100,20
36-
37-
X = randn(n,m)
38-
bX = big(X)
39-
bS = svdfact(bX)
40-
@test isapprox(full(bS), bX, rtol=1e3*eps(BigFloat))
41-
@test isapprox(svdvals(bX), svdvals(X), rtol=1e3*eps())
42-
@test bX == X # check we didn't modify the input
43-
44-
bY = big(randn(n))
45-
@test isapprox(qrfact(bX,Val{false}) \ bY, bS \ bY, rtol=1e3*eps(BigFloat))
46-
@test bX == X # check we didn't modify the input
47-
48-
bXt = bX'
49-
bSt = svdfact(bXt)
50-
@test isapprox(full(bSt), bXt, rtol=1e3*eps(BigFloat))
51-
@test isapprox(svdvals(bXt), svdvals(X), rtol=1e3*eps())
52-
@test bXt == X' # check we didn't modify the input
53-
54-
X = Float64[1 2 0; 0 1 2; 0 0 0]
55-
bX = big(X)
56-
bS = svdfact(bX)
57-
@test isapprox(full(bS), bX, rtol=1e3*eps(BigFloat))
58-
@test isapprox(svdvals(bX), svdvals(X), rtol=1e3*eps())
59-
@test bX == X # check we didn't modify the input
60-
61-
X = Float64[0 2 0; 0 1 2; 0 0 1]
62-
bX = big(X)
63-
bS = svdfact(bX)
64-
@test isapprox(full(bS), bX, rtol=1e3*eps(BigFloat))
65-
@test isapprox(svdvals(bX), svdvals(X), rtol=1e3*eps())
66-
@test bX == X # check we didn't modify the input
67-
68-
69-
bD = big(randn(m))
70-
bX = diagm(bD)
71-
bS = svdfact(bX)
72-
73-
@test isapprox(full(bS), bX, rtol=1e3*eps(BigFloat))
74-
@test bS.S == sort(abs(bD),rev=true)
75-
76-
77-
78-
79-
X = randn(n,m)+im*randn(n,m)
80-
bX = big(X)
81-
bS = svdfact(bX)
82-
@test isapprox(full(bS), bX, rtol=1e3*eps(BigFloat))
83-
@test isapprox(svdvals(bX), svdvals(X), rtol=1e3*eps())
84-
@test bX == X # check we didn't modify the input
85-
86-
bXt = bX'
87-
bSt = svdfact(bXt)
88-
@test isapprox(full(bSt), bXt, rtol=1e3*eps(BigFloat))
89-
@test isapprox(svdvals(bXt), svdvals(X), rtol=1e3*eps())
90-
@test bXt == X' # check we didn't modify the input
1+
for f in ["misc.jl",
2+
"bigfloat.jl",
3+
"quaternions.jl"]
4+
println("Testing $f")
5+
include(f)
6+
end

0 commit comments

Comments
 (0)