File: covariance.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 (135 lines) | stat: -rw-r--r-- 4,129 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
/*******************************************************
 * 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 <arrayfire.h>
#include <af/dim4.hpp>
#include <af/traits.hpp>
#include <string>
#include <vector>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <testHelpers.hpp>

using std::string;
using std::vector;
using namespace af;

template<typename T>
class Covariance : public ::testing::Test
{
    public:
        virtual void SetUp() {}
};

// create a list of types to be tested
typedef ::testing::Types<float, double, int, uint, intl, uintl, uchar, short, ushort> TestTypes;

// register the type list
TYPED_TEST_CASE(Covariance, TestTypes);

template<typename T>
struct f32HelperType {
   typedef typename cond_type<is_same_type<T, double>::value,
                                             double,
                                             float>::type type;
};

template<typename T>
struct c32HelperType {
   typedef typename cond_type<is_same_type<T, cfloat>::value,
                                             cfloat,
                                             typename f32HelperType<T>::type >::type type;
};

template<typename T>
struct elseType {
   typedef typename cond_type< is_same_type<T, uintl>::value ||
                               is_same_type<T, intl> ::value,
                                              double,
                                              T>::type type;
};

template<typename T>
struct covOutType {
   typedef typename cond_type< is_same_type<T, float>   ::value ||
                               is_same_type<T, int>     ::value ||
                               is_same_type<T, uint>    ::value ||
                               is_same_type<T, uchar>   ::value ||
                               is_same_type<T, short>   ::value ||
                               is_same_type<T, ushort>  ::value ||
                               is_same_type<T, char>    ::value,
                                              float,
                              typename elseType<T>::type>::type type;
};

template<typename T>
void covTest(string pFileName, bool isbiased=false)
{
    typedef typename covOutType<T>::type outType;
    if (noDoubleTests<T>()) return;
    if (noDoubleTests<outType>()) return;

    vector<af::dim4>      numDims;
    vector<vector<int> >        in;
    vector<vector<float> >   tests;

    readTestsFromFile<int,float>(pFileName, numDims, in, tests);

    af::dim4 dims1 = numDims[0];
    af::dim4 dims2 = numDims[1];
    vector<T> input1(in[0].begin(), in[0].end());
    vector<T> input2(in[1].begin(), in[1].end());

    array a(dims1, &(input1.front()));
    array b(dims2, &(input2.front()));

    array c = cov(a, b, isbiased);

    vector<outType> currGoldBar(tests[0].begin(), tests[0].end());

    size_t nElems    = currGoldBar.size();
    outType *outData = new outType[nElems];

    c.host((void*)outData);

    for (size_t elIter=0; elIter<nElems; ++elIter) {
        ASSERT_NEAR(::real(currGoldBar[elIter]), ::real(outData[elIter]), 1.0e-3)<< "at: " << elIter<< std::endl;
        ASSERT_NEAR(::imag(currGoldBar[elIter]), ::imag(outData[elIter]), 1.0e-3)<< "at: " << elIter<< std::endl;
    }

    // cleanup
    delete[] outData;
}

TYPED_TEST(Covariance, Vector)
{
    covTest<TypeParam>(string(TEST_DIR "/covariance/vec_size60.test"), false);
}

TYPED_TEST(Covariance, Matrix)
{
    covTest<TypeParam>(string(TEST_DIR "/covariance/matrix_65x121.test"), false);
}

TEST(Covariance, c32)
{
    array a = constant(cfloat(1.0f, -1.0f), 10, c32);
    array b = constant(cfloat(2.0f, -1.0f), 10, c32);
    ASSERT_THROW(cov(a, b), af::exception);
}

TEST(Covariance, c64)
{
    if (noDoubleTests<double>()) return;
    array a = constant(cdouble(1.0, -1.0), 10, c64);
    array b = constant(cdouble(2.0, -1.0), 10, c64);
    ASSERT_THROW(cov(a, b), af::exception);
}