eop_aux.hpp
Go to the documentation of this file.
1 // Copyright (C) 2010-2011 NICTA (www.nicta.com.au)
2 // Copyright (C) 2010-2011 Conrad Sanderson
3 //
4 // This file is part of the Armadillo C++ library.
5 // It is provided without any warranty of fitness
6 // for any purpose. You can redistribute this file
7 // and/or modify it under the terms of the GNU
8 // Lesser General Public License (LGPL) as published
9 // by the Free Software Foundation, either version 3
10 // of the License or (at your option) any later version.
11 // (see http://www.opensource.org/licenses for more info)
12 
13 
16 
17 
18 
19 template<typename eT>
21  {
23  operator eT ()
24  {
25  return eT(std::rand()) / eT(RAND_MAX);
26  }
27  };
28 
29 
30 
31 template<typename T>
32 struct eop_aux_randu< std::complex<T> >
33  {
35  operator std::complex<T> ()
36  {
37  return std::complex<T>( T(eop_aux_randu<T>()), T(eop_aux_randu<T>()) );
38  }
39  };
40 
41 
42 
43 template<typename eT>
45  {
46  // // rudimentary method, based on the central limit theorem
47  // // http://en.wikipedia.org/wiki/Central_limit_theorem
48  // inline
49  // operator eT () const
50  // {
51  // const uword N = 12; // N must be >= 12 and an even number
52  // const uword N2 = N/2;
53  //
54  // eT acc = eT(0);
55  //
56  // for(uword i=0; i<N2; ++i)
57  // {
58  // const eT tmp1 = eT(std::rand()) / eT(RAND_MAX);
59  // const eT tmp2 = eT(std::rand()) / eT(RAND_MAX);
60  // acc += tmp1+tmp2;
61  // }
62  //
63  // return acc - eT(N2);
64  // }
65 
66 
67  // polar form of the Box-Muller transformation
68  // http://en.wikipedia.org/wiki/Box-Muller_transformation
69  // http://en.wikipedia.org/wiki/Marsaglia_polar_method
70  inline
71  operator eT () const
72  {
73  // make sure we are internally using at least floats
74  typedef typename promote_type<eT,float>::result eTp;
75 
76  eTp tmp1;
77  eTp tmp2;
78  eTp w;
79 
80  do
81  {
82  tmp1 = eTp(2) * eTp(std::rand()) / eTp(RAND_MAX) - eTp(1);
83  tmp2 = eTp(2) * eTp(std::rand()) / eTp(RAND_MAX) - eTp(1);
84  w = tmp1*tmp1 + tmp2*tmp2;
85  }
86  while ( w >= eTp(1) );
87 
88  return eT( tmp1 * std::sqrt( (eTp(-2) * std::log(w)) / w) );
89  }
90 
91 
92  // other methods:
93  // http://en.wikipedia.org/wiki/Ziggurat_algorithm
94  //
95  // Marsaglia and Tsang Ziggurat technique to transform from a uniform to a normal distribution.
96  // G. Marsaglia, W.W. Tsang.
97  // "Ziggurat method for generating random variables",
98  // J. Statistical Software, vol 5, 2000.
99  // http://www.jstatsoft.org/v05/i08/
100  };
101 
102 
103 
104 template<typename T>
105 struct eop_aux_randn< std::complex<T> >
106  {
108  operator std::complex<T> () const
109  {
110  return std::complex<T>( T(eop_aux_randn<T>()), T(eop_aux_randn<T>()) );
111  }
112 
113  };
114 
115 
118 
119 class eop_aux
120  {
121  public:
122 
123  template<typename eT> arma_inline static typename arma_integral_only<eT>::result acos (const eT x) { return eT( std::acos(double(x)) ); }
124  template<typename eT> arma_inline static typename arma_integral_only<eT>::result asin (const eT x) { return eT( std::asin(double(x)) ); }
125  template<typename eT> arma_inline static typename arma_integral_only<eT>::result atan (const eT x) { return eT( std::atan(double(x)) ); }
126 
127  template<typename eT> arma_inline static typename arma_float_only<eT>::result acos (const eT x) { return std::acos(x); }
128  template<typename eT> arma_inline static typename arma_float_only<eT>::result asin (const eT x) { return std::asin(x); }
129  template<typename eT> arma_inline static typename arma_float_only<eT>::result atan (const eT x) { return std::atan(x); }
130 
131  template<typename eT> arma_inline static typename arma_cx_only<eT>::result acos (const eT x) { return arma_acos(x); }
132  template<typename eT> arma_inline static typename arma_cx_only<eT>::result asin (const eT x) { return arma_asin(x); }
133  template<typename eT> arma_inline static typename arma_cx_only<eT>::result atan (const eT x) { return arma_atan(x); }
134 
135  template<typename eT> arma_inline static typename arma_integral_only<eT>::result acosh (const eT x) { return eT( arma_acosh(double(x)) ); }
136  template<typename eT> arma_inline static typename arma_integral_only<eT>::result asinh (const eT x) { return eT( arma_asinh(double(x)) ); }
137  template<typename eT> arma_inline static typename arma_integral_only<eT>::result atanh (const eT x) { return eT( arma_atanh(double(x)) ); }
138 
139  template<typename eT> arma_inline static typename arma_float_or_cx_only<eT>::result acosh (const eT x) { return arma_acosh(x); }
140  template<typename eT> arma_inline static typename arma_float_or_cx_only<eT>::result asinh (const eT x) { return arma_asinh(x); }
141  template<typename eT> arma_inline static typename arma_float_or_cx_only<eT>::result atanh (const eT x) { return arma_atanh(x); }
142 
143  template<typename eT> arma_inline static typename arma_not_cx<eT>::result conj(const eT x) { return x; }
144  template<typename T> arma_inline static std::complex<T> conj(const std::complex<T>& x) { return std::conj(x); }
145 
146  template<typename eT> arma_inline static typename arma_integral_only<eT>::result sqrt (const eT x) { return eT( std::sqrt (double(x)) ); }
147  template<typename eT> arma_inline static typename arma_integral_only<eT>::result log10 (const eT x) { return eT( std::log10(double(x)) ); }
148  template<typename eT> arma_inline static typename arma_integral_only<eT>::result log (const eT x) { return eT( std::log (double(x)) ); }
149  template<typename eT> arma_inline static typename arma_integral_only<eT>::result exp (const eT x) { return eT( std::exp (double(x)) ); }
150  template<typename eT> arma_inline static typename arma_integral_only<eT>::result cos (const eT x) { return eT( std::cos (double(x)) ); }
151  template<typename eT> arma_inline static typename arma_integral_only<eT>::result sin (const eT x) { return eT( std::sin (double(x)) ); }
152  template<typename eT> arma_inline static typename arma_integral_only<eT>::result tan (const eT x) { return eT( std::tan (double(x)) ); }
153  template<typename eT> arma_inline static typename arma_integral_only<eT>::result cosh (const eT x) { return eT( std::cosh (double(x)) ); }
154  template<typename eT> arma_inline static typename arma_integral_only<eT>::result sinh (const eT x) { return eT( std::sinh (double(x)) ); }
155  template<typename eT> arma_inline static typename arma_integral_only<eT>::result tanh (const eT x) { return eT( std::tanh (double(x)) ); }
156 
157  template<typename eT> arma_inline static typename arma_float_or_cx_only<eT>::result sqrt (const eT x) { return std::sqrt (x); }
158  template<typename eT> arma_inline static typename arma_float_or_cx_only<eT>::result log10 (const eT x) { return std::log10(x); }
159  template<typename eT> arma_inline static typename arma_float_or_cx_only<eT>::result log (const eT x) { return std::log (x); }
160  template<typename eT> arma_inline static typename arma_float_or_cx_only<eT>::result exp (const eT x) { return std::exp (x); }
161  template<typename eT> arma_inline static typename arma_float_or_cx_only<eT>::result cos (const eT x) { return std::cos (x); }
162  template<typename eT> arma_inline static typename arma_float_or_cx_only<eT>::result sin (const eT x) { return std::sin (x); }
163  template<typename eT> arma_inline static typename arma_float_or_cx_only<eT>::result tan (const eT x) { return std::tan (x); }
164  template<typename eT> arma_inline static typename arma_float_or_cx_only<eT>::result cosh (const eT x) { return std::cosh (x); }
165  template<typename eT> arma_inline static typename arma_float_or_cx_only<eT>::result sinh (const eT x) { return std::sinh (x); }
166  template<typename eT> arma_inline static typename arma_float_or_cx_only<eT>::result tanh (const eT x) { return std::tanh (x); }
167 
168  template<typename eT> arma_inline static typename arma_unsigned_integral_only<eT>::result neg (const eT x) { return x; }
169  template<typename eT> arma_inline static typename arma_signed_only<eT>::result neg (const eT x) { return -x; }
170 
171  template<typename eT> arma_inline static typename arma_integral_only<eT>::result floor(const eT x) { return x; }
172  template<typename eT> arma_inline static typename arma_float_only<eT>::result floor(const eT x) { return std::floor(x); }
173  template<typename eT> arma_inline static typename arma_cx_only<eT>::result floor(const eT& x) { return eT( std::floor(x.real()), std::floor(x.imag()) ); }
174 
175  template<typename eT> arma_inline static typename arma_integral_only<eT>::result ceil(const eT x) { return x; }
176  template<typename eT> arma_inline static typename arma_float_only<eT>::result ceil(const eT x) { return std::ceil(x); }
177  template<typename eT> arma_inline static typename arma_cx_only<eT>::result ceil(const eT& x) { return eT( std::ceil(x.real()), std::ceil(x.imag()) ); }
178 
179  template<typename eT>
181  static
183  log2 (const eT x)
184  {
185  return eT( std::log(double(x))/ double(0.69314718055994530942) );
186  }
187 
188 
189  template<typename eT>
191  static
193  log2 (const eT x)
194  {
195  typedef typename get_pod_type<eT>::result T;
196  return std::log(x) / T(0.69314718055994530942);
197  }
198 
199 
200  template<typename eT>
202  static
204  exp10 (const eT x)
205  {
206  return eT( std::pow(double(10), double(x)) );
207  }
208 
209 
210  template<typename eT>
212  static
213  typename
215  exp10 (const eT x)
216  {
217  typedef typename get_pod_type<eT>::result T;
218  return std::pow( T(10), x);
219  }
220 
221 
222  template<typename eT>
224  static
226  exp2 (const eT x)
227  {
228  return eT( std::pow(double(2), double(x)) );
229  }
230 
231 
232  template<typename eT>
234  static
236  exp2 (const eT x)
237  {
238  typedef typename get_pod_type<eT>::result T;
239  return std::pow( T(2), x);
240  }
241 
242 
243  template<typename T1, typename T2>
245  static
247  pow(const T1 base, const T2 exponent)
248  {
249  return std::pow(base, exponent);
250  }
251 
252 
253 
254  template<typename T1, typename T2>
256  static
258  pow(const T1 base, const T2 exponent)
259  {
260  return T1( std::pow( double(base), double(exponent) ) );
261  }
262 
263 
264 
265  template<typename eT>
267  static
269  direct_eps(const eT)
270  {
271  return eT(0);
272  }
273 
274 
275 
276  template<typename eT>
277  inline
278  static
280  direct_eps(const eT x)
281  {
282  //arma_extra_debug_sigprint();
283 
284  // acording to IEEE Standard for Floating-Point Arithmetic (IEEE 754)
285  // the mantissa length for double is 53 bits = std::numeric_limits<double>::digits
286  // the mantissa length for float is 24 bits = std::numeric_limits<float >::digits
287 
288  //return std::pow( std::numeric_limits<eT>::radix, (std::floor(std::log10(std::abs(x))/std::log10(std::numeric_limits<eT>::radix))-(std::numeric_limits<eT>::digits-1)) );
289 
290  const eT radix_eT = eT(std::numeric_limits<eT>::radix);
291  const eT digits_m1_eT = eT(std::numeric_limits<eT>::digits - 1);
292 
293  // return std::pow( radix_eT, eT(std::floor(std::log10(std::abs(x))/std::log10(radix_eT)) - digits_m1_eT) );
294  return eop_aux::pow( radix_eT, eT(std::floor(std::log10(std::abs(x))/std::log10(radix_eT)) - digits_m1_eT) );
295  }
296 
297 
298 
299  template<typename T>
300  inline
301  static
303  direct_eps(const std::complex<T> x)
304  {
305  //arma_extra_debug_sigprint();
306 
307  //return std::pow( std::numeric_limits<T>::radix, (std::floor(std::log10(std::abs(x))/std::log10(std::numeric_limits<T>::radix))-(std::numeric_limits<T>::digits-1)) );
308 
309  const T radix_T = T(std::numeric_limits<T>::radix);
310  const T digits_m1_T = T(std::numeric_limits<T>::digits - 1);
311 
312  return std::pow( radix_T, T(std::floor(std::log10(std::abs(x))/std::log10(radix_T)) - digits_m1_T) );
313  }
314 
315 
316 
318  template<typename eT> arma_inline static
319  typename arma_unsigned_integral_only<eT>::result arma_abs(const eT x) { return x; }
320 
321  template<typename eT> arma_inline static
322  typename arma_signed_integral_only<eT>::result arma_abs(const eT x) { return std::abs(x); }
323 
324  template<typename eT> arma_inline static
325  typename arma_float_only<eT>::result arma_abs(const eT x) { return std::abs(x); }
326 
327  template<typename T> arma_inline static
328  typename arma_float_only<T>::result arma_abs(const std::complex<T> x) { return std::abs(x); }
329 
330  };
331 
332 
333 
335 
static arma_float_only< T >::result direct_eps(const std::complex< T > x)
Definition: eop_aux.hpp:303
is_promotable< T1, T2 >::result result
arma_inline eT arma_asinh(const eT x)
Definition: cmath_wrap.hpp:229
arma_inline const eOp< T1, eop_ceil > ceil(const Base< typename T1::elem_type, T1 > &A)
Definition: fn_elem.hpp:604
static arma_inline arma_integral_only< eT >::result atanh(const eT x)
Definition: eop_aux.hpp:137
static arma_inline arma_float_or_cx_only< eT >::result exp2(const eT x)
Definition: eop_aux.hpp:236
arma_inline const eOp< T1, eop_exp > exp(const Base< typename T1::elem_type, T1 > &A)
Definition: fn_elem.hpp:237
arma_inline const eOp< T1, eop_log10 > log10(const Base< typename T1::elem_type, T1 > &A)
Definition: fn_elem.hpp:210
static arma_inline arma_integral_only< eT >::result acos(const eT x)
Definition: eop_aux.hpp:123
arma_inline std::complex< T > arma_asin(const std::complex< T > &x)
Definition: cmath_wrap.hpp:156
static arma_inline arma_float_or_cx_only< eT >::result cos(const eT x)
Definition: eop_aux.hpp:161
arma_inline const eOp< T1, eop_sqrt > sqrt(const Base< typename T1::elem_type, T1 > &A)
Definition: fn_elem.hpp:403
static arma_inline arma_cx_only< eT >::result atan(const eT x)
Definition: eop_aux.hpp:133
static arma_inline arma_signed_only< eT >::result neg(const eT x)
Definition: eop_aux.hpp:169
static arma_inline arma_integral_only< eT >::result tan(const eT x)
Definition: eop_aux.hpp:152
arma_inline const eOp< T1, eop_cosh > cosh(const Base< typename T1::elem_type, T1 > &A)
Definition: fn_trig.hpp:84
static arma_inline arma_signed_integral_only< eT >::result arma_abs(const eT x)
Definition: eop_aux.hpp:322
static arma_inline arma_integral_only< eT >::result log(const eT x)
Definition: eop_aux.hpp:148
static arma_inline arma_float_only< eT >::result ceil(const eT x)
Definition: eop_aux.hpp:176
arma_inline const eOp< T1, eop_cos > cos(const Base< typename T1::elem_type, T1 > &A)
Definition: fn_trig.hpp:30
static arma_inline arma_float_only< eT >::result atan(const eT x)
Definition: eop_aux.hpp:129
arma_inline const eOp< T1, eop_tanh > tanh(const Base< typename T1::elem_type, T1 > &A)
Definition: fn_trig.hpp:300
static arma_inline arma_integral_only< eT >::result cosh(const eT x)
Definition: eop_aux.hpp:153
arma_inline const T1 & conj(const Base< typename T1::pod_type, T1 > &A)
Definition: fn_elem.hpp:430
arma_inline std::complex< T > arma_atan(const std::complex< T > &x)
Definition: cmath_wrap.hpp:174
static arma_inline arma_float_or_cx_only< eT >::result tan(const eT x)
Definition: eop_aux.hpp:163
arma_inline const eOp< T1, eop_sinh > sinh(const Base< typename T1::elem_type, T1 > &A)
Definition: fn_trig.hpp:192
static arma_inline arma_integral_only< eT >::result asin(const eT x)
Definition: eop_aux.hpp:124
arma_inline const eOp< T1, eop_tan > tan(const Base< typename T1::elem_type, T1 > &A)
Definition: fn_trig.hpp:246
static arma_inline arma_cx_only< eT >::result acos(const eT x)
Definition: eop_aux.hpp:131
static arma_inline arma_integral_only< eT >::result floor(const eT x)
Definition: eop_aux.hpp:171
static arma_inline arma_float_only< T >::result arma_abs(const std::complex< T > x)
Definition: eop_aux.hpp:328
static arma_inline arma_not_cx< eT >::result conj(const eT x)
Definition: eop_aux.hpp:143
static arma_inline arma_float_or_cx_only< eT >::result log10(const eT x)
Definition: eop_aux.hpp:158
static arma_inline arma_integral_only< eT >::result exp10(const eT x)
Definition: eop_aux.hpp:204
arma_inline const eOp< T1, eop_sin > sin(const Base< typename T1::elem_type, T1 > &A)
Definition: fn_trig.hpp:138
static arma_inline arma_float_only< eT >::result asin(const eT x)
Definition: eop_aux.hpp:128
static arma_inline arma_unsigned_integral_only< eT >::result neg(const eT x)
Definition: eop_aux.hpp:168
static arma_inline arma_integral_only< eT >::result exp2(const eT x)
Definition: eop_aux.hpp:226
static arma_inline arma_float_or_cx_only< eT >::result exp(const eT x)
Definition: eop_aux.hpp:160
static arma_inline arma_integral_only< eT >::result sin(const eT x)
Definition: eop_aux.hpp:151
static arma_inline arma_float_or_cx_only< eT >::result acosh(const eT x)
Definition: eop_aux.hpp:139
static arma_inline arma_float_or_cx_only< eT >::result cosh(const eT x)
Definition: eop_aux.hpp:164
static arma_inline arma_float_or_cx_only< eT >::result tanh(const eT x)
Definition: eop_aux.hpp:166
static arma_inline arma_float_or_cx_only< eT >::result log(const eT x)
Definition: eop_aux.hpp:159
arma_inline eT arma_acosh(const eT x)
Definition: cmath_wrap.hpp:192
static arma_inline arma_integral_only< eT >::result log2(const eT x)
Definition: eop_aux.hpp:183
arma_inline const eOp< T1, eop_atan > atan(const Base< typename T1::elem_type, T1 > &A)
Definition: fn_trig.hpp:273
static arma_inline arma_cx_only< eT >::result ceil(const eT &x)
Definition: eop_aux.hpp:177
static arma_inline arma_float_or_cx_only< eT >::result log2(const eT x)
Definition: eop_aux.hpp:193
static arma_inline arma_integral_only< eT >::result exp(const eT x)
Definition: eop_aux.hpp:149
static arma_float_only< eT >::result direct_eps(const eT x)
Definition: eop_aux.hpp:280
static arma_inline arma_integral_only< eT >::result atan(const eT x)
Definition: eop_aux.hpp:125
static arma_inline arma_integral_only< eT >::result acosh(const eT x)
Definition: eop_aux.hpp:135
static arma_inline arma_float_only< eT >::result arma_abs(const eT x)
Definition: eop_aux.hpp:325
static arma_inline arma_integral_only< eT >::result ceil(const eT x)
Definition: eop_aux.hpp:175
static arma_inline arma_integral_only< eT >::result tanh(const eT x)
Definition: eop_aux.hpp:155
static arma_inline arma_unsigned_integral_only< eT >::result arma_abs(const eT x)
work around a bug in GCC 4.4
Definition: eop_aux.hpp:319
arma_inline const eOp< T1, eop_abs > abs(const Base< typename T1::elem_type, T1 > &X, const typename arma_not_cx< typename T1::elem_type >::result *junk=0)
Definition: fn_elem.hpp:317
static arma_inline arma_integral_only< eT >::result log10(const eT x)
Definition: eop_aux.hpp:147
arma_inline const eOp< T1, eop_pow > pow(const Base< typename T1::elem_type, T1 > &A, const typename T1::elem_type exponent)
Definition: fn_elem.hpp:520
static arma_inline std::complex< T > conj(const std::complex< T > &x)
Definition: eop_aux.hpp:144
static arma_inline arma_float_or_cx_only< eT >::result asinh(const eT x)
Definition: eop_aux.hpp:140
arma_inline const eOp< T1, eop_asin > asin(const Base< typename T1::elem_type, T1 > &A)
Definition: fn_trig.hpp:165
static arma_inline arma_float_or_cx_only< eT >::result sinh(const eT x)
Definition: eop_aux.hpp:165
static arma_inline arma_integral_only< eT >::result sqrt(const eT x)
Definition: eop_aux.hpp:146
static arma_inline arma_float_or_cx_only< eT >::result exp10(const eT x)
Definition: eop_aux.hpp:215
arma_inline const eOp< T1, eop_acos > acos(const Base< typename T1::elem_type, T1 > &A)
Definition: fn_trig.hpp:57
#define arma_inline
static arma_inline arma_cx_only< eT >::result asin(const eT x)
Definition: eop_aux.hpp:132
static arma_inline arma_float_only< eT >::result acos(const eT x)
Definition: eop_aux.hpp:127
static arma_inline arma_integral_only< eT >::result sinh(const eT x)
Definition: eop_aux.hpp:154
arma_inline const eOp< T1, eop_log > log(const Base< typename T1::elem_type, T1 > &A)
Definition: fn_elem.hpp:156
arma_inline std::complex< T > arma_acos(const std::complex< T > &x)
Definition: cmath_wrap.hpp:138
arma_inline eT arma_atanh(const eT x)
Definition: cmath_wrap.hpp:252
static arma_inline arma_integral_only< eT >::result direct_eps(const eT)
Definition: eop_aux.hpp:269
static arma_inline arma_integral_only< eT >::result asinh(const eT x)
Definition: eop_aux.hpp:136
static arma_inline arma_integral_only< T1 >::result pow(const T1 base, const T2 exponent)
Definition: eop_aux.hpp:258
static arma_inline arma_integral_only< eT >::result cos(const eT x)
Definition: eop_aux.hpp:150
arma_inline const eOp< T1, eop_floor > floor(const Base< typename T1::elem_type, T1 > &A)
Definition: fn_elem.hpp:577
static arma_inline arma_cx_only< eT >::result floor(const eT &x)
Definition: eop_aux.hpp:173
static arma_inline arma_float_only< eT >::result floor(const eT x)
Definition: eop_aux.hpp:172
static arma_inline arma_float_or_cx_only< eT >::result sqrt(const eT x)
Definition: eop_aux.hpp:157
static arma_inline arma_float_or_cx_only< T1 >::result pow(const T1 base, const T2 exponent)
Definition: eop_aux.hpp:247
static arma_inline arma_float_or_cx_only< eT >::result sin(const eT x)
Definition: eop_aux.hpp:162
static arma_inline arma_float_or_cx_only< eT >::result atanh(const eT x)
Definition: eop_aux.hpp:141


armadillo_matrix
Author(s):
autogenerated on Fri Apr 16 2021 02:31:57