-
Notifications
You must be signed in to change notification settings - Fork 552
Expand file tree
/
Copy pathcholesky_dense.cpp
More file actions
136 lines (110 loc) · 3.14 KB
/
cholesky_dense.cpp
File metadata and controls
136 lines (110 loc) · 3.14 KB
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
/*******************************************************
* Copyright (c) 2014, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#include <arrayfire.h>
#include <gtest/gtest.h>
#include <testHelpers.hpp>
#include <af/defines.h>
#include <af/dim4.hpp>
#include <af/traits.hpp>
#include <complex>
#include <string>
#include <vector>
using af::array;
using af::cdouble;
using af::cfloat;
using af::dim4;
using af::dtype;
using af::dtype_traits;
using af::identity;
using af::matmul;
using af::max;
using std::abs;
using std::endl;
using std::string;
using std::vector;
template<typename T>
void choleskyTester(const int n, double eps, bool is_upper) {
SUPPORTED_TYPE_CHECK(T);
LAPACK_ENABLED_CHECK();
dtype ty = (dtype)dtype_traits<T>::af_type;
// Prepare positive definite matrix
#if 1
array a = cpu_randu<T>(dim4(n, n));
#else
array a = randu(n, n, ty);
#endif
array b = 10 * n * identity(n, n, ty);
array in = matmul(a.H(), a) + b;
//! [ex_chol_reg]
array out;
cholesky(out, in, is_upper);
//! [ex_chol_reg]
array re = is_upper ? matmul(out.H(), out) : matmul(out, out.H());
ASSERT_NEAR(0, max<typename dtype_traits<T>::base_type>(abs(real(in - re))),
eps);
ASSERT_NEAR(0, max<typename dtype_traits<T>::base_type>(abs(imag(in - re))),
eps);
//! [ex_chol_inplace]
array in2 = in.copy();
choleskyInPlace(in2, is_upper);
//! [ex_chol_inplace]
array out2 = is_upper ? upper(in2) : lower(in2);
ASSERT_NEAR(0,
max<typename dtype_traits<T>::base_type>(abs(real(out2 - out))),
eps);
ASSERT_NEAR(0,
max<typename dtype_traits<T>::base_type>(abs(imag(out2 - out))),
eps);
}
template<typename T>
class Cholesky : public ::testing::Test {};
typedef ::testing::Types<float, cfloat, double, cdouble> TestTypes;
TYPED_TEST_SUITE(Cholesky, TestTypes);
template<typename T>
double eps();
template<>
double eps<float>() {
return 0.05f;
}
template<>
double eps<double>() {
return 1e-8;
}
template<>
double eps<cfloat>() {
return 0.05f;
}
template<>
double eps<cdouble>() {
return 1e-8;
}
TYPED_TEST(Cholesky, Upper) {
choleskyTester<TypeParam>(500, eps<TypeParam>(), true);
}
TYPED_TEST(Cholesky, UpperLarge) {
choleskyTester<TypeParam>(1000, eps<TypeParam>(), true);
}
TYPED_TEST(Cholesky, UpperMultipleOfTwo) {
choleskyTester<TypeParam>(512, eps<TypeParam>(), true);
}
TYPED_TEST(Cholesky, UpperMultipleOfTwoLarge) {
choleskyTester<TypeParam>(1024, eps<TypeParam>(), true);
}
TYPED_TEST(Cholesky, Lower) {
choleskyTester<TypeParam>(500, eps<TypeParam>(), false);
}
TYPED_TEST(Cholesky, LowerLarge) {
choleskyTester<TypeParam>(1000, eps<TypeParam>(), false);
}
TYPED_TEST(Cholesky, LowerMultipleOfTwo) {
choleskyTester<TypeParam>(512, eps<TypeParam>(), false);
}
TYPED_TEST(Cholesky, LowerMultipleOfTwoLarge) {
choleskyTester<TypeParam>(1024, eps<TypeParam>(), false);
}