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
|
// Copyright (c) the JPEG XL Project Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
#include "lib/jxl/linalg.h"
#include "gtest/gtest.h"
#include "lib/jxl/image_test_utils.h"
namespace jxl {
namespace {
template <typename T>
Plane<T> RandomMatrix(const size_t xsize, const size_t ysize, Rng& rng,
const T vmin, const T vmax) {
Plane<T> A(xsize, ysize);
GenerateImage(rng, &A, vmin, vmax);
return A;
}
template <typename T>
Plane<T> RandomSymmetricMatrix(const size_t N, Rng& rng, const T vmin,
const T vmax) {
Plane<T> A = RandomMatrix<T>(N, N, rng, vmin, vmax);
for (size_t i = 0; i < N; ++i) {
for (size_t j = 0; j < i; ++j) {
A.Row(j)[i] = A.Row(i)[j];
}
}
return A;
}
void VerifyMatrixEqual(const ImageD& A, const ImageD& B, const double eps) {
ASSERT_EQ(A.xsize(), B.xsize());
ASSERT_EQ(A.ysize(), B.ysize());
for (size_t y = 0; y < A.ysize(); ++y) {
for (size_t x = 0; x < A.xsize(); ++x) {
ASSERT_NEAR(A.Row(y)[x], B.Row(y)[x], eps);
}
}
}
void VerifyOrthogonal(const ImageD& A, const double eps) {
VerifyMatrixEqual(Identity<double>(A.xsize()), MatMul(Transpose(A), A), eps);
}
void VerifyTridiagonal(const ImageD& T, const double eps) {
ASSERT_EQ(T.xsize(), T.ysize());
for (size_t i = 0; i < T.xsize(); ++i) {
for (size_t j = i + 2; j < T.xsize(); ++j) {
ASSERT_NEAR(T.Row(i)[j], 0.0, eps);
ASSERT_NEAR(T.Row(j)[i], 0.0, eps);
}
}
}
void VerifyUpperTriangular(const ImageD& R, const double eps) {
ASSERT_EQ(R.xsize(), R.ysize());
for (size_t i = 0; i < R.xsize(); ++i) {
for (size_t j = i + 1; j < R.xsize(); ++j) {
ASSERT_NEAR(R.Row(i)[j], 0.0, eps);
}
}
}
TEST(LinAlgTest, ConvertToTridiagonal) {
{
ImageD I = Identity<double>(5);
ImageD T, U;
ConvertToTridiagonal(I, &T, &U);
VerifyMatrixEqual(I, T, 1e-15);
VerifyMatrixEqual(I, U, 1e-15);
}
{
ImageD A = Identity<double>(5);
A.Row(0)[1] = A.Row(1)[0] = 2.0;
A.Row(0)[4] = A.Row(4)[0] = 3.0;
A.Row(2)[3] = A.Row(3)[2] = 2.0;
A.Row(3)[4] = A.Row(4)[3] = 2.0;
ImageD U, d;
ConvertToDiagonal(A, &d, &U);
VerifyOrthogonal(U, 1e-12);
VerifyMatrixEqual(A, MatMul(U, MatMul(Diagonal(d), Transpose(U))), 1e-12);
}
Rng rng(0);
for (int N = 2; N < 100; ++N) {
ImageD A = RandomSymmetricMatrix(N, rng, -1.0, 1.0);
ImageD T, U;
ConvertToTridiagonal(A, &T, &U);
VerifyOrthogonal(U, 1e-12);
VerifyTridiagonal(T, 1e-12);
VerifyMatrixEqual(A, MatMul(U, MatMul(T, Transpose(U))), 1e-12);
}
}
TEST(LinAlgTest, ConvertToDiagonal) {
{
ImageD I = Identity<double>(5);
ImageD U, d;
ConvertToDiagonal(I, &d, &U);
VerifyMatrixEqual(I, U, 1e-15);
for (int k = 0; k < 5; ++k) {
ASSERT_NEAR(d.Row(0)[k], 1.0, 1e-15);
}
}
{
ImageD A = Identity<double>(5);
A.Row(0)[1] = A.Row(1)[0] = 2.0;
A.Row(2)[3] = A.Row(3)[2] = 2.0;
A.Row(3)[4] = A.Row(4)[3] = 2.0;
ImageD U, d;
ConvertToDiagonal(A, &d, &U);
VerifyOrthogonal(U, 1e-12);
VerifyMatrixEqual(A, MatMul(U, MatMul(Diagonal(d), Transpose(U))), 1e-12);
}
Rng rng(0);
for (int N = 2; N < 100; ++N) {
ImageD A = RandomSymmetricMatrix(N, rng, -1.0, 1.0);
ImageD U, d;
ConvertToDiagonal(A, &d, &U);
VerifyOrthogonal(U, 1e-12);
VerifyMatrixEqual(A, MatMul(U, MatMul(Diagonal(d), Transpose(U))), 1e-12);
}
}
TEST(LinAlgTest, ComputeQRFactorization) {
{
ImageD I = Identity<double>(5);
ImageD Q, R;
ComputeQRFactorization(I, &Q, &R);
VerifyMatrixEqual(I, Q, 1e-15);
VerifyMatrixEqual(I, R, 1e-15);
}
Rng rng(0);
for (int N = 2; N < 100; ++N) {
ImageD A = RandomMatrix(N, N, rng, -1.0, 1.0);
ImageD Q, R;
ComputeQRFactorization(A, &Q, &R);
VerifyOrthogonal(Q, 1e-12);
VerifyUpperTriangular(R, 1e-12);
VerifyMatrixEqual(A, MatMul(Q, R), 1e-12);
}
}
} // namespace
} // namespace jxl
|