forked from hunter-packages/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiagonal.cpp
More file actions
97 lines (81 loc) · 2.64 KB
/
diagonal.cpp
File metadata and controls
97 lines (81 loc) · 2.64 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
/*******************************************************
* 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 <gtest/gtest.h>
#include <testHelpers.hpp>
#include <arrayfire.h>
#include <iostream>
using namespace af;
using std::vector;
template<typename T>
class Diagonal : public ::testing::Test
{
};
typedef ::testing::Types<float, double, int, uint, char, unsigned char> TestTypes;
TYPED_TEST_CASE(Diagonal, TestTypes);
TYPED_TEST(Diagonal, Create)
{
if (noDoubleTests<TypeParam>()) return;
try {
static const int size = 1000;
vector<TypeParam> input (size * size);
for(int i = 0; i < size; i++) {
input[i] = i;
}
for(int jj = 10; jj < size; jj+=100) {
array data(jj, &input.front(), afHost);
array out = diag(data, 0, false);
vector<TypeParam> h_out(out.elements());
out.host(&h_out.front());
for(int i =0; i < (int)out.dims(0); i++) {
for(int j =0; j < (int)out.dims(1); j++) {
if(i == j) ASSERT_EQ(input[i], h_out[i * out.dims(0) + j]);
else ASSERT_EQ(TypeParam(0), h_out[i * out.dims(0) + j]);
}
}
}
} catch (const af::exception& ex) {
FAIL() << ex.what() << std::endl;
}
}
TYPED_TEST(Diagonal, Extract)
{
if (noDoubleTests<TypeParam>()) return;
try {
static const int size = 1000;
vector<TypeParam> input (size * size);
for(int i = 0; i < size * size; i++) {
input[i] = i;
}
for(int jj = 10; jj < size; jj+=100) {
array data(jj, jj, &input.front(), afHost);
array out = diag(data, 0);
vector<TypeParam> h_out(out.elements());
out.host(&h_out.front());
for(int i =0; i < (int)out.dims(0); i++) {
ASSERT_EQ(input[i * data.dims(0) + i], h_out[i]);
}
}
} catch (const af::exception& ex) {
FAIL() << ex.what() << std::endl;
}
}
TEST(Diagonal, ExtractGFOR)
{
dim4 dims = dim4(100, 100, 3);
array A = round(100 * randu(dims));
array B = constant(0, 100, 1, 3);
gfor(seq ii, 3) {
B(span, span, ii) = diag(A(span, span, ii));
}
for(int ii = 0; ii < 3; ii++) {
array c_ii = diag(A(span, span, ii));
array b_ii = B(span, span, ii);
ASSERT_EQ(max<double>(abs(c_ii - b_ii)) < 1E-5, true);
}
}