File: test_qr.cpp

package info (click to toggle)
amgcl 1.4.4-1
  • links: PTS, VCS
  • area: contrib
  • in suites: sid
  • size: 5,676 kB
  • sloc: cpp: 34,043; python: 747; pascal: 258; f90: 196; makefile: 20
file content (209 lines) | stat: -rw-r--r-- 6,249 bytes parent folder | download | duplicates (3)
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
#define BOOST_TEST_MODULE TestQR
#include <boost/test/unit_test.hpp>

#include <vector>
#include <random>
#include <boost/multi_array.hpp>

#include <amgcl/detail/qr.hpp>
#include <amgcl/value_type/interface.hpp>
#include <amgcl/value_type/complex.hpp>
#include <amgcl/value_type/static_matrix.hpp>

template <class T>
struct make_random {
    static T get() {
        static std::mt19937 gen;
        static std::uniform_real_distribution<T> rnd;

        return rnd(gen);
    }
};

template <class T>
T random() {
    return make_random<T>::get();
}

template <class T>
struct make_random< std::complex<T> > {
    static std::complex<T> get() {
        return std::complex<T>( random<T>(), random<T>() );
    }
};

template <class T, int N, int M>
struct make_random< amgcl::static_matrix<T,N,M> > {
    typedef amgcl::static_matrix<T,N,M> matrix;
    static matrix get() {
        matrix A = amgcl::math::zero<matrix>();
        for(int i = 0; i < N; ++i)
            for(int j = 0; j < M; ++j)
                A(i,j) = make_random<T>::get();
        return A;
    }
};

template <class value_type, amgcl::detail::storage_order order>
void qr_factorize(int n, int m) {
    std::cout << "factorize " << n << " " << m << std::endl;
    typedef typename std::conditional<order == amgcl::detail::row_major,
            boost::c_storage_order,
            boost::fortran_storage_order
            >::type ma_storage_order;

    boost::multi_array<value_type, 2> A0(boost::extents[n][m], ma_storage_order());

    for(int i = 0; i < n; ++i)
        for(int j = 0; j < m; ++j)
            A0[i][j] = random<value_type>();

    boost::multi_array<value_type, 2> A = A0;

    amgcl::detail::QR<value_type> qr;

    qr.factorize(n, m, A.data(), order);

    // Check that A = QR
    int p = std::min(n, m);
    for(int i = 0; i < n; ++i) {
        for(int j = 0; j < m; ++j) {
            value_type sum = amgcl::math::zero<value_type>();

            for(int k = 0; k < p; ++k)
                sum += qr.Q(i,k) * qr.R(k,j);

            sum -= A0[i][j];

            BOOST_CHECK_SMALL(amgcl::math::norm(sum), 1e-8);
        }
    }
}

template <class value_type, amgcl::detail::storage_order order>
void qr_solve(int n, int m) {
    std::cout << "solve " << n << " " << m << std::endl;
    typedef typename std::conditional<order == amgcl::detail::row_major,
            boost::c_storage_order,
            boost::fortran_storage_order
            >::type ma_storage_order;

    typedef typename amgcl::math::rhs_of<value_type>::type rhs_type;

    boost::multi_array<value_type, 2> A0(boost::extents[n][m], ma_storage_order());

    for(int i = 0; i < n; ++i)
        for(int j = 0; j < m; ++j)
            A0[i][j] = random<value_type>();

    boost::multi_array<value_type, 2> A = A0;

    amgcl::detail::QR<value_type> qr;

    std::vector<rhs_type> f0(n, amgcl::math::constant<rhs_type>(1));
    std::vector<rhs_type> f = f0;

    std::vector<rhs_type> x(m);

    qr.solve(n, m, A.data(), f.data(), x.data(), order);

    std::vector<rhs_type> Ax(n);
    for(int i = 0; i < n; ++i) {
        rhs_type sum = amgcl::math::zero<rhs_type>();
        for(int j = 0; j < m; ++j)
            sum += A0[i][j] * x[j];

        Ax[i] = sum;

        if (n < m) {
            BOOST_CHECK_SMALL(amgcl::math::norm(sum - f0[i]), 1e-8);
        }
    }

    if (n >= m) {
        for(int i = 0; i < m; ++i) {
            rhs_type sumx = amgcl::math::zero<rhs_type>();
            rhs_type sumf = amgcl::math::zero<rhs_type>();

            for(int j = 0; j < n; ++j) {
                sumx += amgcl::math::adjoint(A0[j][i]) * Ax[j];
                sumf += amgcl::math::adjoint(A0[j][i]) * f0[j];
            }

            rhs_type delta = sumx - sumf;

            BOOST_CHECK_SMALL(amgcl::math::norm(delta), 1e-8);
        }
    }
}

BOOST_AUTO_TEST_SUITE( test_qr )

BOOST_AUTO_TEST_CASE( test_qr_factorize ) {
    const int shape[][2] = {
        {3, 3},
        {3, 5},
        {5, 3},
        {5, 5}
    };

    const int n = sizeof(shape) / sizeof(shape[0]);

    for(int i = 0; i < n; ++i) {
        qr_factorize<double,                             amgcl::detail::row_major>(shape[i][0], shape[i][1]);
        qr_factorize<double,                             amgcl::detail::col_major>(shape[i][0], shape[i][1]);
        qr_factorize<std::complex<double>,               amgcl::detail::row_major>(shape[i][0], shape[i][1]);
        qr_factorize<std::complex<double>,               amgcl::detail::col_major>(shape[i][0], shape[i][1]);
        qr_factorize<amgcl::static_matrix<double, 2, 2>, amgcl::detail::row_major>(shape[i][0], shape[i][1]);
        qr_factorize<amgcl::static_matrix<double, 2, 2>, amgcl::detail::col_major>(shape[i][0], shape[i][1]);
    }
}

BOOST_AUTO_TEST_CASE( test_qr_solve ) {
    const int shape[][2] = {
        {3, 3},
        {3, 5},
        {5, 3},
        {5, 5}
    };

    const int n = sizeof(shape) / sizeof(shape[0]);

    for(int i = 0; i < n; ++i) {
        qr_solve<double,                             amgcl::detail::row_major>(shape[i][0], shape[i][1]);
        qr_solve<double,                             amgcl::detail::col_major>(shape[i][0], shape[i][1]);
        qr_solve<std::complex<double>,               amgcl::detail::row_major>(shape[i][0], shape[i][1]);
        qr_solve<std::complex<double>,               amgcl::detail::col_major>(shape[i][0], shape[i][1]);
        qr_solve<amgcl::static_matrix<double, 2, 2>, amgcl::detail::row_major>(shape[i][0], shape[i][1]);
        qr_solve<amgcl::static_matrix<double, 2, 2>, amgcl::detail::col_major>(shape[i][0], shape[i][1]);
    }
}

BOOST_AUTO_TEST_CASE( qr_issue_39 ) {
    boost::multi_array<double, 2> A0(boost::extents[2][2]);
    A0[0][0] = 1e+0;
    A0[0][1] = 1e+0;
    A0[1][0] = 1e-8;
    A0[1][1] = 1e+0;

    boost::multi_array<double, 2> A = A0;

    amgcl::detail::QR<double> qr;

    qr.factorize(2, 2, A.data());

    // Check that A = QR
    for(int i = 0; i < 2; ++i) {
        for(int j = 0; j < 2; ++j) {
            double sum = 0;
            for(int k = 0; k < 2; ++k)
                sum += qr.Q(i,k) * qr.R(k,j);

            sum -= A0[i][j];

            BOOST_CHECK_SMALL(sum, 1e-8);
        }
    }
}

BOOST_AUTO_TEST_SUITE_END()