forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsvd_dense.cpp
More file actions
101 lines (80 loc) · 1.96 KB
/
svd_dense.cpp
File metadata and controls
101 lines (80 loc) · 1.96 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
/*******************************************************
* Copyright (c) 2015, 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 <gtest/gtest.h>
#include <arrayfire.h>
#include <af/dim4.hpp>
#include <af/defines.h>
#include <af/traits.hpp>
#include <vector>
#include <iostream>
#include <complex>
#include <string>
#include <testHelpers.hpp>
using std::vector;
using std::string;
using std::cout;
using std::endl;
using std::abs;
using af::cfloat;
using af::cdouble;
template<typename T>
class svd : public ::testing::Test
{
};
typedef ::testing::Types<float, double, cfloat, cdouble> TestTypes;
TYPED_TEST_CASE(svd, TestTypes);
template<typename T>
inline double get_val(T val)
{
return val;
}
template<> inline double get_val<cfloat>(cfloat val)
{
return abs(val);
}
template<> double get_val<cdouble>(cdouble val)
{
return abs(val);
}
template<typename T>
void svdTest(const int M, const int N)
{
if (noDoubleTests<T>()) return;
if (noLAPACKTests()) return;
af::dtype ty = (af::dtype)af::dtype_traits<T>::af_type;
af::array A = af::randu(M, N, ty);
//! [ex_svd_reg]
af::array U, S, Vt;
af::svd(U, S, Vt, A);
const int MN = std::min(M, N);
af::array UU = U(af::span, af::seq(MN));
af::array SS = af::diag(S, 0, false).as(ty);
af::array VV = Vt(af::seq(MN), af::span);
af::array AA = matmul(UU, SS, VV);
//! [ex_svd_reg]
std::vector<T> hA(M * N);
std::vector<T> hAA(M * N);
A.host(&hA[0]);
AA.host(&hAA[0]);
for (int i = 0; i < M * N; i++) {
ASSERT_NEAR(get_val(hA[i]), get_val(hAA[i]), 1E-3);
}
}
TYPED_TEST(svd, Square)
{
svdTest<TypeParam>(500, 500);
}
TYPED_TEST(svd, Rect0)
{
svdTest<TypeParam>(500, 300);
}
TYPED_TEST(svd, Rect1)
{
svdTest<TypeParam>(300, 500);
}