File: complex.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 (142 lines) | stat: -rw-r--r-- 9,169 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
130
131
132
133
134
135
136
137
138
139
140
141
142
/*******************************************************
 * 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 <af/array.h>
#include <af/arith.h>
#include <af/data.h>
#include <testHelpers.hpp>

using namespace std;
using namespace af;

const int num = 10;

#define CPLX(TYPE)  af_c##TYPE

#define COMPLEX_TESTS(Ta, Tb, Tc)                                       \
    TEST(ComplexTests, Test_##Ta##_##Tb)                                \
    {                                                                   \
        if (noDoubleTests<Ta>()) return;                                \
        if (noDoubleTests<Tb>()) return;                                \
        if (noDoubleTests<Tc>()) return;                                \
                                                                        \
        af_dtype ta = (af_dtype)dtype_traits<Ta>::af_type;              \
        af_dtype tb = (af_dtype)dtype_traits<Tb>::af_type;              \
        af::array a = randu(num, ta);                                   \
        af::array b = randu(num, tb);                                   \
        af::array c = af::complex(a, b);                                \
        Ta *h_a = a.host<Ta>();                                         \
        Tb *h_b = b.host<Tb>();                                         \
        CPLX(Tc) *h_c = c.host< CPLX(Tc) >();                           \
        for (int i = 0; i < num; i++)                                   \
            ASSERT_EQ(h_c[i], CPLX(Tc)(h_a[i], h_b[i])) <<              \
                "for values: " << h_a[i]  << "," << h_b[i] << std::endl; \
        delete[] h_a;                                                   \
        delete[] h_b;                                                   \
        delete[] h_c;                                                   \
    }                                                                   \
    TEST(ComplexTests, Test_cplx_##Ta##_##Tb##_left)                    \
    {                                                                   \
        if (noDoubleTests<Ta>()) return;                                \
        if (noDoubleTests<Tb>()) return;                                \
                                                                        \
        af_dtype ta = (af_dtype)dtype_traits<Ta>::af_type;              \
        af::array a = randu(num, ta);                                   \
        Tb h_b = 0.3;                                                   \
        af::array c = af::complex(a, h_b);                              \
        Ta *h_a = a.host<Ta>();                                         \
        CPLX(Ta) *h_c = c.host<CPLX(Ta) >();                            \
        for (int i = 0; i < num; i++)                                   \
            ASSERT_EQ(h_c[i], CPLX(Ta)(h_a[i], h_b)) <<                 \
                "for values: " << h_a[i]  << "," << h_b << std::endl;   \
        delete[] h_a;                                                   \
        delete[] h_c;                                                   \
    }                                                                   \
                                                                        \
    TEST(ComplexTests, Test_cplx_##Ta##_##Tb##_right)                   \
    {                                                                   \
        if (noDoubleTests<Ta>()) return;                                \
        if (noDoubleTests<Tb>()) return;                                \
                                                                        \
        af_dtype tb = (af_dtype)dtype_traits<Tb>::af_type;              \
        Ta h_a = 0.3;                                                   \
        af::array b = randu(num, tb);                                   \
        af::array c = af::complex(h_a, b);                              \
        Tb *h_b = b.host<Tb>();                                         \
        CPLX(Tb) *h_c = c.host<CPLX(Tb) >();                            \
        for (int i = 0; i < num; i++)                                   \
            ASSERT_EQ(h_c[i], CPLX(Tb)(h_a, h_b[i])) <<                 \
                "for values: " << h_a  << "," << h_b[i] << std::endl;   \
        delete[] h_b;                                                   \
        delete[] h_c;                                                   \
    }                                                                   \
    TEST(ComplexTests, Test_##Ta##_##Tb##_Real)                         \
    {                                                                   \
        if (noDoubleTests<Ta>()) return;                                \
        if (noDoubleTests<Tb>()) return;                                \
        if (noDoubleTests<Tc>()) return;                                \
                                                                        \
        af_dtype ta = (af_dtype)dtype_traits<Ta>::af_type;              \
        af_dtype tb = (af_dtype)dtype_traits<Tb>::af_type;              \
        af::array a = randu(num, ta);                                   \
        af::array b = randu(num, tb);                                   \
        af::array c = af::complex(a, b);                                \
        af::array d = af::real(c);                                      \
        Ta *h_a = a.host<Ta>();                                         \
        Tc *h_d = d.host<Tc>();                                         \
        for (int i = 0; i < num; i++)                                   \
            ASSERT_EQ(h_d[i], h_a[i]) << "at: " << i << std::endl;      \
        delete[] h_a;                                                   \
        delete[] h_d;                                                   \
    }                                                                   \
    TEST(ComplexTests, Test_##Ta##_##Tb##_Imag)                         \
    {                                                                   \
        if (noDoubleTests<Ta>()) return;                                \
        if (noDoubleTests<Tb>()) return;                                \
        if (noDoubleTests<Tc>()) return;                                \
                                                                        \
        af_dtype ta = (af_dtype)dtype_traits<Ta>::af_type;              \
        af_dtype tb = (af_dtype)dtype_traits<Tb>::af_type;              \
        af::array a = randu(num, ta);                                   \
        af::array b = randu(num, tb);                                   \
        af::array c = af::complex(a, b);                                \
        af::array d = af::imag(c);                                      \
        Tb *h_b = b.host<Tb>();                                         \
        Tc *h_d = d.host<Tc>();                                         \
        for (int i = 0; i < num; i++)                                   \
            ASSERT_EQ(h_d[i], h_b[i])  << "at: " << i << std::endl;     \
        delete[] h_b;                                                   \
        delete[] h_d;                                                   \
    }                                                                   \
    TEST(ComplexTests, Test_##Ta##_##Tb##_Conj)                         \
    {                                                                   \
        if (noDoubleTests<Ta>()) return;                                \
        if (noDoubleTests<Tb>()) return;                                \
        if (noDoubleTests<Tc>()) return;                                \
                                                                        \
        af_dtype ta = (af_dtype)dtype_traits<Ta>::af_type;              \
        af_dtype tb = (af_dtype)dtype_traits<Tb>::af_type;              \
        af::array a = randu(num, ta);                                   \
        af::array b = randu(num, tb);                                   \
        af::array c = af::complex(a, b);                                \
        af::array d = af::conjg(c);                                     \
        CPLX(Tc) *h_c = c.host<CPLX(Tc) >();                            \
        CPLX(Tc) *h_d = d.host<CPLX(Tc) >();                            \
        for (int i = 0; i < num; i++)                                   \
            ASSERT_EQ(conj(h_c[i]), h_d[i])                             \
                << "at: " << i << std::endl;                            \
        delete[] h_c;                                                   \
        delete[] h_d;                                                   \
    }                                                                   \


COMPLEX_TESTS(float, float, float)
COMPLEX_TESTS(double, double, double)
COMPLEX_TESTS(float, double, double)