File: diagonal.cpp

package info (click to toggle)
arrayfire 3.3.2%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 109,016 kB
  • sloc: cpp: 127,909; lisp: 6,878; python: 3,923; ansic: 1,051; sh: 347; makefile: 338; xml: 175
file content (129 lines) | stat: -rw-r--r-- 3,622 bytes parent folder | download
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
/*******************************************************
 * 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;
using std::abs;

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;
    }
}

TYPED_TEST(Diagonal, ExtractRect)
{
    if (noDoubleTests<TypeParam>()) return;

    try {
        static const int size0 = 1000, size1 = 900;
        vector<TypeParam> input (size0 * size1);
        for(int i = 0; i < size0 * size1; i++) {
            input[i] = i;
        }

        for(int jj = 10; jj < size0; jj += 100) {
            for(int kk = 10; kk < size1; kk += 90) {
                array data(jj, kk, &input.front(), afHost);
                array out = diag(data, 0);

                vector<TypeParam> h_out(out.elements());
                out.host(&h_out.front());

                ASSERT_EQ(out.dims(0), std::min(jj, kk));

                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);
    }
}