forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinverse_dense.cpp
More file actions
92 lines (76 loc) · 2.13 KB
/
inverse_dense.cpp
File metadata and controls
92 lines (76 loc) · 2.13 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
/*******************************************************
* 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
********************************************************/
// NOTE: Tests are known to fail on OSX when utilizing the CPU and OpenCL
// backends for sizes larger than 128x128 or more. You can read more about it on
// issue https://github.com/arrayfire/arrayfire/issues/1617
#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 <iostream>
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;
template<typename T>
void inverseTester(const int m, const int n, double eps) {
SUPPORTED_TYPE_CHECK(T);
if (noLAPACKTests()) return;
#if 1
array A = cpu_randu<T>(dim4(m, n));
#else
array A = randu(m, n, (dtype)dtype_traits<T>::af_type);
#endif
//! [ex_inverse]
array IA = inverse(A);
array I = matmul(A, IA);
//! [ex_inverse]
array I2 = identity(m, n, (dtype)dtype_traits<T>::af_type);
ASSERT_NEAR(0, max<typename dtype_traits<T>::base_type>(abs(real(I - I2))),
eps);
ASSERT_NEAR(0, max<typename dtype_traits<T>::base_type>(abs(imag(I - I2))),
eps);
}
template<typename T>
class Inverse : public ::testing::Test {};
template<typename T>
double eps();
template<>
double eps<float>() {
return 0.01;
}
template<>
double eps<double>() {
return 1e-5;
}
template<>
double eps<cfloat>() {
return 0.015;
}
template<>
double eps<cdouble>() {
return 1e-5;
}
typedef ::testing::Types<float, cfloat, double, cdouble> TestTypes;
TYPED_TEST_CASE(Inverse, TestTypes);
TYPED_TEST(Inverse, Square) {
inverseTester<TypeParam>(1000, 1000, eps<TypeParam>());
}
TYPED_TEST(Inverse, SquareMultiplePowerOfTwo) {
inverseTester<TypeParam>(2048, 2048, eps<TypeParam>());
}