2015-01-15 02:19:33 +08:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
|
|
|
// for linear algebra.
|
|
|
|
//
|
|
|
|
// Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla
|
|
|
|
// Public License v. 2.0. If a copy of the MPL was not distributed
|
|
|
|
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
|
|
|
|
#include <Eigen/CXX11/Tensor>
|
|
|
|
|
2020-11-18 07:32:44 +08:00
|
|
|
template<typename Scalar>
|
2015-01-15 02:19:33 +08:00
|
|
|
static void test_default()
|
|
|
|
{
|
2020-11-18 07:32:44 +08:00
|
|
|
Tensor<Scalar, 1> vec(6);
|
2015-01-15 02:19:33 +08:00
|
|
|
vec.setRandom();
|
|
|
|
|
|
|
|
// Fixme: we should check that the generated numbers follow a uniform
|
|
|
|
// distribution instead.
|
|
|
|
for (int i = 1; i < 6; ++i) {
|
|
|
|
VERIFY_IS_NOT_EQUAL(vec(i), vec(i-1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-18 07:32:44 +08:00
|
|
|
template<typename Scalar>
|
2015-01-15 02:19:33 +08:00
|
|
|
static void test_normal()
|
|
|
|
{
|
2020-11-18 07:32:44 +08:00
|
|
|
Tensor<Scalar, 1> vec(6);
|
|
|
|
vec.template setRandom<Eigen::internal::NormalRandomGenerator<Scalar>>();
|
2015-01-15 02:19:33 +08:00
|
|
|
|
|
|
|
// Fixme: we should check that the generated numbers follow a gaussian
|
|
|
|
// distribution instead.
|
|
|
|
for (int i = 1; i < 6; ++i) {
|
|
|
|
VERIFY_IS_NOT_EQUAL(vec(i), vec(i-1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct MyGenerator {
|
|
|
|
MyGenerator() { }
|
|
|
|
MyGenerator(const MyGenerator&) { }
|
|
|
|
|
|
|
|
// Return a random value to be used. "element_location" is the
|
|
|
|
// location of the entry to set in the tensor, it can typically
|
|
|
|
// be ignored.
|
|
|
|
int operator()(Eigen::DenseIndex element_location, Eigen::DenseIndex /*unused*/ = 0) const {
|
2015-02-28 18:37:41 +08:00
|
|
|
return static_cast<int>(3 * element_location);
|
2015-01-15 02:19:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Same as above but generates several numbers at a time.
|
2016-03-05 06:37:51 +08:00
|
|
|
internal::packet_traits<int>::type packetOp(
|
2015-01-15 02:19:33 +08:00
|
|
|
Eigen::DenseIndex packet_location, Eigen::DenseIndex /*unused*/ = 0) const {
|
|
|
|
const int packetSize = internal::packet_traits<int>::size;
|
2015-07-29 17:11:23 +08:00
|
|
|
EIGEN_ALIGN_MAX int values[packetSize];
|
2015-01-15 02:19:33 +08:00
|
|
|
for (int i = 0; i < packetSize; ++i) {
|
2015-02-28 18:37:41 +08:00
|
|
|
values[i] = static_cast<int>(3 * (packet_location + i));
|
2015-01-15 02:19:33 +08:00
|
|
|
}
|
|
|
|
return internal::pload<typename internal::packet_traits<int>::type>(values);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static void test_custom()
|
|
|
|
{
|
|
|
|
Tensor<int, 1> vec(6);
|
|
|
|
vec.setRandom<MyGenerator>();
|
|
|
|
|
|
|
|
for (int i = 0; i < 6; ++i) {
|
|
|
|
VERIFY_IS_EQUAL(vec(i), 3*i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-17 20:46:15 +08:00
|
|
|
EIGEN_DECLARE_TEST(cxx11_tensor_random)
|
2015-01-15 02:19:33 +08:00
|
|
|
{
|
2020-11-18 07:32:44 +08:00
|
|
|
CALL_SUBTEST((test_default<float>()));
|
|
|
|
CALL_SUBTEST((test_normal<float>()));
|
|
|
|
CALL_SUBTEST((test_default<double>()));
|
|
|
|
CALL_SUBTEST((test_normal<double>()));
|
|
|
|
CALL_SUBTEST((test_default<Eigen::half>()));
|
|
|
|
CALL_SUBTEST((test_normal<Eigen::half>()));
|
|
|
|
CALL_SUBTEST((test_default<Eigen::bfloat16>()));
|
|
|
|
CALL_SUBTEST((test_normal<Eigen::bfloat16>()));
|
2015-01-15 02:19:33 +08:00
|
|
|
CALL_SUBTEST(test_custom());
|
|
|
|
}
|