cxx11_tensor_random.cpp
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #include "main.h"
11 
12 #include <Eigen/CXX11/Tensor>
13 
14 template<typename Scalar>
15 static void test_default()
16 {
17  Tensor<Scalar, 1> vec(6);
18  vec.setRandom();
19 
20  // Fixme: we should check that the generated numbers follow a uniform
21  // distribution instead.
22  for (int i = 1; i < 6; ++i) {
23  VERIFY_IS_NOT_EQUAL(vec(i), vec(i-1));
24  }
25 }
26 
27 template<typename Scalar>
28 static void test_normal()
29 {
30  Tensor<Scalar, 1> vec(6);
31  vec.template setRandom<Eigen::internal::NormalRandomGenerator<Scalar>>();
32 
33  // Fixme: we should check that the generated numbers follow a gaussian
34  // distribution instead.
35  for (int i = 1; i < 6; ++i) {
36  VERIFY_IS_NOT_EQUAL(vec(i), vec(i-1));
37  }
38 }
39 
40 
41 struct MyGenerator {
44 
45  // Return a random value to be used. "element_location" is the
46  // location of the entry to set in the tensor, it can typically
47  // be ignored.
48  int operator()(Eigen::DenseIndex element_location, Eigen::DenseIndex /*unused*/ = 0) const {
49  return static_cast<int>(3 * element_location);
50  }
51 
52  // Same as above but generates several numbers at a time.
54  Eigen::DenseIndex packet_location, Eigen::DenseIndex /*unused*/ = 0) const {
55  const int packetSize = internal::packet_traits<int>::size;
56  EIGEN_ALIGN_MAX int values[packetSize];
57  for (int i = 0; i < packetSize; ++i) {
58  values[i] = static_cast<int>(3 * (packet_location + i));
59  }
61  }
62 };
63 
64 
65 static void test_custom()
66 {
67  Tensor<int, 1> vec(6);
68  vec.setRandom<MyGenerator>();
69 
70  for (int i = 0; i < 6; ++i) {
71  VERIFY_IS_EQUAL(vec(i), 3*i);
72  }
73 }
74 
75 EIGEN_DECLARE_TEST(cxx11_tensor_random)
76 {
77  CALL_SUBTEST((test_default<float>()));
78  CALL_SUBTEST((test_normal<float>()));
79  CALL_SUBTEST((test_default<double>()));
80  CALL_SUBTEST((test_normal<double>()));
81  CALL_SUBTEST((test_default<Eigen::half>()));
82  CALL_SUBTEST((test_normal<Eigen::half>()));
83  CALL_SUBTEST((test_default<Eigen::bfloat16>()));
84  CALL_SUBTEST((test_normal<Eigen::bfloat16>()));
86 }
Eigen::Tensor
The tensor class.
Definition: Tensor.h:63
test_custom
static void test_custom()
Definition: cxx11_tensor_random.cpp:65
gtsam.examples.DogLegOptimizerExample.type
type
Definition: DogLegOptimizerExample.py:111
MyGenerator::MyGenerator
MyGenerator(const MyGenerator &)
Definition: cxx11_tensor_random.cpp:43
VERIFY_IS_EQUAL
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:386
MyGenerator::MyGenerator
MyGenerator()
Definition: cxx11_tensor_random.cpp:42
size
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
EIGEN_ALIGN_MAX
#define EIGEN_ALIGN_MAX
Definition: ConfigureVectorization.h:157
MyGenerator::operator()
int operator()(Eigen::DenseIndex element_location, Eigen::DenseIndex=0) const
Definition: cxx11_tensor_random.cpp:48
test_normal
static void test_normal()
Definition: cxx11_tensor_random.cpp:28
Eigen::Triplet< double >
MyGenerator
Definition: cxx11_tensor_random.cpp:41
Eigen::TensorBase< Tensor< Scalar_, NumIndices_, Options_, IndexType_ > >::setRandom
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Tensor< Scalar_, NumIndices_, Options_, IndexType_ > & setRandom()
Definition: TensorBase.h:996
EIGEN_DECLARE_TEST
EIGEN_DECLARE_TEST(cxx11_tensor_random)
Definition: cxx11_tensor_random.cpp:75
VERIFY_IS_NOT_EQUAL
#define VERIFY_IS_NOT_EQUAL(a, b)
Definition: main.h:387
main.h
Eigen::DenseIndex
EIGEN_DEFAULT_DENSE_INDEX_TYPE DenseIndex
Definition: Meta.h:66
leaf::values
leaf::MyValues values
MyGenerator::packetOp
internal::packet_traits< int >::type packetOp(Eigen::DenseIndex packet_location, Eigen::DenseIndex=0) const
Definition: cxx11_tensor_random.cpp:53
test_default
static void test_default()
Definition: cxx11_tensor_random.cpp:15
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
CALL_SUBTEST
#define CALL_SUBTEST(FUNC)
Definition: main.h:399


gtsam
Author(s):
autogenerated on Tue Jun 25 2024 03:00:45