19#include <gtest/gtest.h>
24#include "arccore/common/NumArray.h"
26#include "arccore/alina/QRFactorizationImpl.h"
27#include "arccore/alina/ValueTypeInterface.h"
28#include "arccore/alina/ValueTypeComplex.h"
29#include "arccore/alina/StaticMatrix.h"
38 static std::mt19937 gen;
39 static std::uniform_real_distribution<T> rnd;
48 return make_random<T>::get();
54 static std::complex<T> get()
56 return std::complex<T>(random<T>(), random<T>());
60template <
class T,
int N,
int M>
66 matrix A = Alina::math::zero<matrix>();
67 for (
int i = 0; i < N; ++i)
68 for (
int j = 0; j < M; ++j)
69 A(i, j) = make_random<T>::get();
74template <
class value_type, Alina::detail::storage_order order>
75void qr_factorize(
int n,
int m)
77 std::cout <<
"factorize " << n <<
" " << m << std::endl;
78 typedef typename std::conditional<order == Alina::detail::row_major,
84 for (
int i = 0; i < n; ++i)
85 for (
int j = 0; j < m; ++j)
86 A0(i, j) = random<value_type>();
92 qr.factorize(n, m, A.
data(), order);
95 int p = std::min(n, m);
96 for (
int i = 0; i < n; ++i) {
97 for (
int j = 0; j < m; ++j) {
98 value_type sum = Alina::math::zero<value_type>();
100 for (
int k = 0; k < p; ++k)
101 sum += qr.Q(i, k) * qr.R(k, j);
105 ASSERT_NEAR(Alina::math::norm(sum), 0.0, 1e-8);
110template <
class value_type, Alina::detail::storage_order order>
111void qr_solve(
int n,
int m)
113 std::cout <<
"solve " << n <<
" " << m << std::endl;
114 typedef typename std::conditional<order == Alina::detail::row_major,
118 typedef typename Alina::math::rhs_of<value_type>::type rhs_type;
122 for (
int i = 0; i < n; ++i)
123 for (
int j = 0; j < m; ++j)
124 A0(i, j) = random<value_type>();
130 std::vector<rhs_type> f0(n, Alina::math::constant<rhs_type>(1));
131 std::vector<rhs_type> f = f0;
133 std::vector<rhs_type> x(m);
135 qr.solve(n, m, A.
data(), f.data(), x.data(), order);
137 std::vector<rhs_type> Ax(n);
138 for (
int i = 0; i < n; ++i) {
139 rhs_type sum = Alina::math::zero<rhs_type>();
140 for (
int j = 0; j < m; ++j)
141 sum += A0(i, j) * x[j];
146 ASSERT_NEAR(Alina::math::norm(sum - f0[i]), 0.0, 1e-8);
151 for (
int i = 0; i < m; ++i) {
152 rhs_type sumx = Alina::math::zero<rhs_type>();
153 rhs_type sumf = Alina::math::zero<rhs_type>();
155 for (
int j = 0; j < n; ++j) {
156 sumx += Alina::math::adjoint(A0(j, i)) * Ax[j];
157 sumf += Alina::math::adjoint(A0(j, i)) * f0[j];
160 rhs_type delta = sumx - sumf;
162 ASSERT_NEAR(Alina::math::norm(delta), 0.0, 1e-8);
167TEST(alina_test_qr, test_qr_factorize)
169 const int shape[][2] = {
176 const int n =
sizeof(shape) /
sizeof(shape[0]);
178 for (
int i = 0; i < n; ++i) {
179 qr_factorize<double, Alina::detail::row_major>(shape[i][0], shape[i][1]);
180 qr_factorize<double, Alina::detail::col_major>(shape[i][0], shape[i][1]);
181 qr_factorize<std::complex<double>, Alina::detail::row_major>(shape[i][0], shape[i][1]);
182 qr_factorize<std::complex<double>, Alina::detail::col_major>(shape[i][0], shape[i][1]);
183 qr_factorize<Alina::StaticMatrix<double, 2, 2>, Alina::detail::row_major>(shape[i][0], shape[i][1]);
184 qr_factorize<Alina::StaticMatrix<double, 2, 2>, Alina::detail::col_major>(shape[i][0], shape[i][1]);
188TEST(alina_test_qr, test_qr_solve)
190 const int shape[][2] = {
197 const int n =
sizeof(shape) /
sizeof(shape[0]);
199 for (
int i = 0; i < n; ++i) {
200 qr_solve<double, Alina::detail::row_major>(shape[i][0], shape[i][1]);
201 qr_solve<double, Alina::detail::col_major>(shape[i][0], shape[i][1]);
202 qr_solve<std::complex<double>, Alina::detail::row_major>(shape[i][0], shape[i][1]);
203 qr_solve<std::complex<double>, Alina::detail::col_major>(shape[i][0], shape[i][1]);
204 qr_solve<Alina::StaticMatrix<double, 2, 2>, Alina::detail::row_major>(shape[i][0], shape[i][1]);
205 qr_solve<Alina::StaticMatrix<double, 2, 2>, Alina::detail::col_major>(shape[i][0], shape[i][1]);
209TEST(alina_test_qr, qr_issue_39)
221 qr.factorize(2, 2, A.
data());
224 for (
int i = 0; i < 2; ++i) {
225 for (
int j = 0; j < 2; ++j) {
227 for (
int k = 0; k < 2; ++k)
228 sum += qr.Q(i, k) * qr.R(k, j);
232 ASSERT_NEAR(sum, 0.0, 1e-8);
Multi-dimensional arrays for numerical types accessible on accelerators.
DataType * data()
Base pointer of the array.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --