rand.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) 2015 Gael Guennebaud <gael.guennebaud@inria.fr>
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 typedef long long int64;
13 
14 template<typename Scalar> Scalar check_in_range(Scalar x, Scalar y)
15 {
16  Scalar r = internal::random<Scalar>(x,y);
17  VERIFY(r>=x);
18  if(y>=x)
19  {
20  VERIFY(r<=y);
21  }
22  return r;
23 }
24 
25 template<typename Scalar> void check_all_in_range(Scalar x, Scalar y)
26 {
27  Array<int,1,Dynamic> mask(y-x+1);
28  mask.fill(0);
29  long n = (y-x+1)*32;
30  for(long k=0; k<n; ++k)
31  {
32  mask( check_in_range(x,y)-x )++;
33  }
34  for(Index i=0; i<mask.size(); ++i)
35  if(mask(i)==0)
36  std::cout << "WARNING: value " << x+i << " not reached." << std::endl;
37  VERIFY( (mask>0).all() );
38 }
39 
40 template<typename Scalar> void check_histogram(Scalar x, Scalar y, int bins)
41 {
42  Array<int,1,Dynamic> hist(bins);
43  hist.fill(0);
44  int f = 100000;
45  int n = bins*f;
46  int64 range = int64(y)-int64(x);
47  int divisor = int((range+1)/bins);
48  assert(((range+1)%bins)==0);
49  for(int k=0; k<n; ++k)
50  {
51  Scalar r = check_in_range(x,y);
52  hist( int((int64(r)-int64(x))/divisor) )++;
53  }
54  VERIFY( (((hist.cast<double>()/double(f))-1.0).abs()<0.02).all() );
55 }
56 
57 void test_rand()
58 {
59  long long_ref = NumTraits<long>::highest()/10;
60  signed char char_offset = (std::min)(g_repeat,64);
61  signed char short_offset = (std::min)(g_repeat,16000);
62 
63  for(int i = 0; i < g_repeat*10000; i++) {
64  CALL_SUBTEST(check_in_range<float>(10,11));
65  CALL_SUBTEST(check_in_range<float>(1.24234523,1.24234523));
66  CALL_SUBTEST(check_in_range<float>(-1,1));
67  CALL_SUBTEST(check_in_range<float>(-1432.2352,-1432.2352));
68 
69  CALL_SUBTEST(check_in_range<double>(10,11));
70  CALL_SUBTEST(check_in_range<double>(1.24234523,1.24234523));
71  CALL_SUBTEST(check_in_range<double>(-1,1));
72  CALL_SUBTEST(check_in_range<double>(-1432.2352,-1432.2352));
73 
74  CALL_SUBTEST(check_in_range<int>(0,-1));
75  CALL_SUBTEST(check_in_range<short>(0,-1));
76  CALL_SUBTEST(check_in_range<long>(0,-1));
77  CALL_SUBTEST(check_in_range<int>(-673456,673456));
78  CALL_SUBTEST(check_in_range<int>(-RAND_MAX+10,RAND_MAX-10));
79  CALL_SUBTEST(check_in_range<short>(-24345,24345));
80  CALL_SUBTEST(check_in_range<long>(-long_ref,long_ref));
81  }
82 
83  CALL_SUBTEST(check_all_in_range<signed char>(11,11));
84  CALL_SUBTEST(check_all_in_range<signed char>(11,11+char_offset));
85  CALL_SUBTEST(check_all_in_range<signed char>(-5,5));
86  CALL_SUBTEST(check_all_in_range<signed char>(-11-char_offset,-11));
87  CALL_SUBTEST(check_all_in_range<signed char>(-126,-126+char_offset));
88  CALL_SUBTEST(check_all_in_range<signed char>(126-char_offset,126));
89  CALL_SUBTEST(check_all_in_range<signed char>(-126,126));
90 
91  CALL_SUBTEST(check_all_in_range<short>(11,11));
92  CALL_SUBTEST(check_all_in_range<short>(11,11+short_offset));
93  CALL_SUBTEST(check_all_in_range<short>(-5,5));
94  CALL_SUBTEST(check_all_in_range<short>(-11-short_offset,-11));
95  CALL_SUBTEST(check_all_in_range<short>(-24345,-24345+short_offset));
96  CALL_SUBTEST(check_all_in_range<short>(24345,24345+short_offset));
97 
98  CALL_SUBTEST(check_all_in_range<int>(11,11));
99  CALL_SUBTEST(check_all_in_range<int>(11,11+g_repeat));
100  CALL_SUBTEST(check_all_in_range<int>(-5,5));
101  CALL_SUBTEST(check_all_in_range<int>(-11-g_repeat,-11));
102  CALL_SUBTEST(check_all_in_range<int>(-673456,-673456+g_repeat));
103  CALL_SUBTEST(check_all_in_range<int>(673456,673456+g_repeat));
104 
105  CALL_SUBTEST(check_all_in_range<long>(11,11));
106  CALL_SUBTEST(check_all_in_range<long>(11,11+g_repeat));
107  CALL_SUBTEST(check_all_in_range<long>(-5,5));
108  CALL_SUBTEST(check_all_in_range<long>(-11-g_repeat,-11));
109  CALL_SUBTEST(check_all_in_range<long>(-long_ref,-long_ref+g_repeat));
110  CALL_SUBTEST(check_all_in_range<long>( long_ref, long_ref+g_repeat));
111 
112  CALL_SUBTEST(check_histogram<int>(-5,5,11));
113  int bins = 100;
114  CALL_SUBTEST(check_histogram<int>(-3333,-3333+bins*(3333/bins)-1,bins));
115  bins = 1000;
116  CALL_SUBTEST(check_histogram<int>(-RAND_MAX+10,-RAND_MAX+10+bins*(RAND_MAX/bins)-1,bins));
117  CALL_SUBTEST(check_histogram<int>(-RAND_MAX+10,-int64(RAND_MAX)+10+bins*(2*int64(RAND_MAX)/bins)-1,bins));
118 }
SCALAR Scalar
Definition: bench_gemm.cpp:33
Scalar * y
return int(ret)+1
#define min(a, b)
Definition: datatypes.h:19
Scalar check_in_range(Scalar x, Scalar y)
Definition: rand.cpp:14
int n
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:150
static int g_repeat
Definition: main.h:144
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
void test_rand()
Definition: rand.cpp:57
void check_all_in_range(Scalar x, Scalar y)
Definition: rand.cpp:25
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
#define CALL_SUBTEST(FUNC)
Definition: main.h:342
#define VERIFY(a)
Definition: main.h:325
long long int64
Definition: rand.cpp:12
General-purpose arrays with easy API for coefficient-wise operations.
Definition: Array.h:45
void check_histogram(Scalar x, Scalar y, int bins)
Definition: rand.cpp:40
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
#define abs(x)
Definition: datatypes.h:17


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:43:47