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
|
/*******************************************************
* 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/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 Rank : public ::testing::Test
{
};
template<typename T>
class Det : public ::testing::Test
{
};
typedef ::testing::Types<float, double, af::cfloat, af::cdouble> TestTypes;
TYPED_TEST_CASE(Rank, TestTypes);
TYPED_TEST_CASE(Det, TestTypes);
template<typename T>
void rankSmall()
{
if (noDoubleTests<T>()) return;
if (noLAPACKTests()) return;
T ha[] = {1, 4, 7, 2, 5, 8, 3, 6, 20};
af::array a(3, 3, ha);
ASSERT_EQ(3, (int)af::rank(a));
}
template<typename T>
void rankBig(const int num)
{
if (noDoubleTests<T>()) return;
if (noLAPACKTests()) return;
af::dtype dt = (af::dtype)af::dtype_traits<T>::af_type;
af::array a = af::randu(num, num, dt);
ASSERT_EQ(num, (int)af::rank(a));
af::array b = af::randu(num, num/2, dt);
ASSERT_EQ(num/2, (int)af::rank(b));
ASSERT_EQ(num/2, (int)af::rank(transpose(b)));
}
template<typename T>
void rankLow(const int num)
{
if (noDoubleTests<T>()) return;
if (noLAPACKTests()) return;
af::dtype dt = (af::dtype)af::dtype_traits<T>::af_type;
af::array a = af::randu(3 * num, num, dt);
af::array b = af::randu(3 * num, num, dt);
af::array c = a + 0.2 * b;
af::array in = join(1, a, b, c);
// The last third is just a linear combination of first and second thirds
ASSERT_EQ(2 * num, (int)af::rank(in));
}
TYPED_TEST(Rank, small)
{
rankSmall<TypeParam>();
}
TYPED_TEST(Rank, big)
{
rankBig<TypeParam>(1024);
}
TYPED_TEST(Rank, low)
{
rankBig<TypeParam>(512);
}
template<typename T>
void detTest()
{
if (noDoubleTests<T>()) return;
if (noLAPACKTests()) return;
af::dtype dt = (af::dtype)af::dtype_traits<T>::af_type;
vector<af::dim4> numDims;
vector<vector<float> > in;
vector<vector<float> > tests;
readTests<float,float,float>(string(TEST_DIR"/lapack/detSmall.test"),numDims,in,tests);
af::dim4 dims = numDims[0];
af::array input = af::array(dims, &(in[0].front())).as(dt);
T output = af::det<T>(input);
ASSERT_NEAR(abs((T)tests[0][0]), abs(output), 1e-6);
}
TYPED_TEST(Det, Small)
{
detTest<TypeParam>();
}
|