[6] | 1 | // |
---|
| 2 | // C++ Implementation: itpp_ext |
---|
| 3 | // |
---|
[145] | 4 | // Description: |
---|
[6] | 5 | // |
---|
| 6 | // |
---|
| 7 | // Author: smidl <smidl@utia.cas.cz>, (C) 2008 |
---|
| 8 | // |
---|
| 9 | // Copyright: See COPYING file that comes with this distribution |
---|
| 10 | // |
---|
| 11 | // |
---|
| 12 | |
---|
[32] | 13 | #include "itpp_ext.h" |
---|
[6] | 14 | |
---|
[37] | 15 | // from algebra/lapack.h |
---|
| 16 | |
---|
| 17 | extern "C" { /* QR factorization of a general matrix A */ |
---|
[145] | 18 | void dgeqrf_ ( int *m, int *n, double *a, int *lda, double *tau, double *work, |
---|
| 19 | int *lwork, int *info ); |
---|
[37] | 20 | }; |
---|
| 21 | |
---|
[6] | 22 | namespace itpp { |
---|
[145] | 23 | Array<int> to_Arr ( const ivec &indices ) { |
---|
| 24 | Array<int> a ( indices.size() ); |
---|
| 25 | for ( int i = 0; i < a.size(); i++ ) { |
---|
| 26 | a ( i ) = indices ( i ); |
---|
| 27 | } |
---|
| 28 | return a; |
---|
| 29 | } |
---|
[6] | 30 | |
---|
[145] | 31 | ivec linspace ( int from, int to ) { |
---|
| 32 | int n=to-from+1; |
---|
| 33 | int i; |
---|
| 34 | it_assert_debug ( n>0,"wrong linspace" ); |
---|
| 35 | ivec iv ( n ); for ( i=0;i<n;i++ ) iv ( i ) =from+i; |
---|
| 36 | return iv; |
---|
| 37 | }; |
---|
[86] | 38 | |
---|
[145] | 39 | void set_subvector ( vec &ov, const ivec &iv, const vec &v ) |
---|
| 40 | { |
---|
| 41 | it_assert_debug ( ( iv.length() <=v.length() ), |
---|
| 42 | "Vec<>::set_subvector(ivec, vec<Num_T>): Indexing out " |
---|
| 43 | "of range of v" ); |
---|
| 44 | for ( int i = 0; i < iv.length(); i++ ) { |
---|
| 45 | it_assert_debug ( iv ( i ) <ov.length(), |
---|
| 46 | "Vec<>::set_subvector(ivec, vec<Num_T>): Indexing out " |
---|
| 47 | "of range of v" ); |
---|
| 48 | ov ( iv ( i ) ) = v ( i ); |
---|
| 49 | } |
---|
| 50 | } |
---|
| 51 | |
---|
[180] | 52 | vec get_vec(const vec &v, const ivec &indexlist){ |
---|
| 53 | int size = indexlist.size(); |
---|
| 54 | vec temp(size); |
---|
| 55 | for (int i = 0; i < size; ++i) { |
---|
| 56 | temp(i) = v._data()[indexlist(i)]; |
---|
| 57 | } |
---|
| 58 | return temp; |
---|
| 59 | } |
---|
| 60 | |
---|
[32] | 61 | //Gamma |
---|
[11] | 62 | |
---|
[145] | 63 | Gamma_RNG::Gamma_RNG ( double a, double b ) { |
---|
| 64 | setup ( a,b ); |
---|
| 65 | } |
---|
[32] | 66 | |
---|
[145] | 67 | |
---|
| 68 | bvec operator& ( const bvec &a, const bvec &b ) { |
---|
| 69 | it_assert_debug ( b.size() ==a.size(), "operator&(): Vectors of different lengths" ); |
---|
| 70 | |
---|
| 71 | bvec temp ( a.size() ); |
---|
| 72 | for ( int i = 0;i < a.size();i++ ) { |
---|
| 73 | temp ( i ) = a ( i ) & b ( i ); |
---|
| 74 | } |
---|
| 75 | return temp; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | bvec operator| ( const bvec &a, const bvec &b ) { |
---|
| 79 | it_assert_debug ( b.size() !=a.size(), "operator&(): Vectors of different lengths" ); |
---|
| 80 | |
---|
| 81 | bvec temp ( a.size() ); |
---|
| 82 | for ( int i = 0;i < a.size();i++ ) { |
---|
| 83 | temp ( i ) = a ( i ) | b ( i ); |
---|
| 84 | } |
---|
| 85 | return temp; |
---|
| 86 | } |
---|
[32] | 87 | #define log std::log |
---|
| 88 | #define exp std::exp |
---|
| 89 | #define sqrt std::sqrt |
---|
| 90 | #define R_FINITE std::isfinite |
---|
| 91 | |
---|
[145] | 92 | double Gamma_RNG::sample() { |
---|
| 93 | //A copy of rgamma code from the R package!! |
---|
| 94 | // |
---|
[32] | 95 | |
---|
[145] | 96 | /* Constants : */ |
---|
| 97 | const static double sqrt32 = 5.656854; |
---|
| 98 | const static double exp_m1 = 0.36787944117144232159;/* exp(-1) = 1/e */ |
---|
[32] | 99 | |
---|
[145] | 100 | /* Coefficients q[k] - for q0 = sum(q[k]*a^(-k)) |
---|
| 101 | * Coefficients a[k] - for q = q0+(t*t/2)*sum(a[k]*v^k) |
---|
| 102 | * Coefficients e[k] - for exp(q)-1 = sum(e[k]*q^k) |
---|
| 103 | */ |
---|
| 104 | const static double q1 = 0.04166669; |
---|
| 105 | const static double q2 = 0.02083148; |
---|
| 106 | const static double q3 = 0.00801191; |
---|
| 107 | const static double q4 = 0.00144121; |
---|
| 108 | const static double q5 = -7.388e-5; |
---|
| 109 | const static double q6 = 2.4511e-4; |
---|
| 110 | const static double q7 = 2.424e-4; |
---|
[32] | 111 | |
---|
[145] | 112 | const static double a1 = 0.3333333; |
---|
| 113 | const static double a2 = -0.250003; |
---|
| 114 | const static double a3 = 0.2000062; |
---|
| 115 | const static double a4 = -0.1662921; |
---|
| 116 | const static double a5 = 0.1423657; |
---|
| 117 | const static double a6 = -0.1367177; |
---|
| 118 | const static double a7 = 0.1233795; |
---|
[32] | 119 | |
---|
[145] | 120 | /* State variables [FIXME for threading!] :*/ |
---|
| 121 | static double aa = 0.; |
---|
| 122 | static double aaa = 0.; |
---|
| 123 | static double s, s2, d; /* no. 1 (step 1) */ |
---|
| 124 | static double q0, b, si, c;/* no. 2 (step 4) */ |
---|
[32] | 125 | |
---|
[145] | 126 | double e, p, q, r, t, u, v, w, x, ret_val; |
---|
| 127 | double a=alpha; |
---|
| 128 | double scale=1.0/beta; |
---|
[32] | 129 | |
---|
[145] | 130 | if ( !R_FINITE ( a ) || !R_FINITE ( scale ) || a < 0.0 || scale <= 0.0 ) |
---|
| 131 | {it_error ( "Gamma_RNG wrong parameters" );} |
---|
[32] | 132 | |
---|
[145] | 133 | if ( a < 1. ) { /* GS algorithm for parameters a < 1 */ |
---|
| 134 | if ( a == 0 ) |
---|
| 135 | return 0.; |
---|
| 136 | e = 1.0 + exp_m1 * a; |
---|
| 137 | for ( ;; ) { //VS repeat |
---|
| 138 | p = e * unif_rand(); |
---|
| 139 | if ( p >= 1.0 ) { |
---|
| 140 | x = -log ( ( e - p ) / a ); |
---|
| 141 | if ( exp_rand() >= ( 1.0 - a ) * log ( x ) ) |
---|
| 142 | break; |
---|
| 143 | } |
---|
| 144 | else { |
---|
| 145 | x = exp ( log ( p ) / a ); |
---|
| 146 | if ( exp_rand() >= x ) |
---|
| 147 | break; |
---|
| 148 | } |
---|
| 149 | } |
---|
| 150 | return scale * x; |
---|
| 151 | } |
---|
[32] | 152 | |
---|
[145] | 153 | /* --- a >= 1 : GD algorithm --- */ |
---|
[32] | 154 | |
---|
[145] | 155 | /* Step 1: Recalculations of s2, s, d if a has changed */ |
---|
| 156 | if ( a != aa ) { |
---|
| 157 | aa = a; |
---|
| 158 | s2 = a - 0.5; |
---|
| 159 | s = sqrt ( s2 ); |
---|
| 160 | d = sqrt32 - s * 12.0; |
---|
| 161 | } |
---|
| 162 | /* Step 2: t = standard normal deviate, |
---|
| 163 | x = (s,1/2) -normal deviate. */ |
---|
[32] | 164 | |
---|
[145] | 165 | /* immediate acceptance (i) */ |
---|
| 166 | t = norm_rand(); |
---|
| 167 | x = s + 0.5 * t; |
---|
| 168 | ret_val = x * x; |
---|
| 169 | if ( t >= 0.0 ) |
---|
| 170 | return scale * ret_val; |
---|
[32] | 171 | |
---|
[145] | 172 | /* Step 3: u = 0,1 - uniform sample. squeeze acceptance (s) */ |
---|
| 173 | u = unif_rand(); |
---|
| 174 | if ( ( d * u ) <= ( t * t * t ) ) |
---|
| 175 | return scale * ret_val; |
---|
[32] | 176 | |
---|
[145] | 177 | /* Step 4: recalculations of q0, b, si, c if necessary */ |
---|
[32] | 178 | |
---|
[145] | 179 | if ( a != aaa ) { |
---|
| 180 | aaa = a; |
---|
| 181 | r = 1.0 / a; |
---|
| 182 | q0 = ( ( ( ( ( ( q7 * r + q6 ) * r + q5 ) * r + q4 ) * r + q3 ) * r |
---|
| 183 | + q2 ) * r + q1 ) * r; |
---|
[32] | 184 | |
---|
[145] | 185 | /* Approximation depending on size of parameter a */ |
---|
| 186 | /* The constants in the expressions for b, si and c */ |
---|
| 187 | /* were established by numerical experiments */ |
---|
[32] | 188 | |
---|
[145] | 189 | if ( a <= 3.686 ) { |
---|
| 190 | b = 0.463 + s + 0.178 * s2; |
---|
| 191 | si = 1.235; |
---|
| 192 | c = 0.195 / s - 0.079 + 0.16 * s; |
---|
| 193 | } |
---|
| 194 | else if ( a <= 13.022 ) { |
---|
| 195 | b = 1.654 + 0.0076 * s2; |
---|
| 196 | si = 1.68 / s + 0.275; |
---|
| 197 | c = 0.062 / s + 0.024; |
---|
| 198 | } |
---|
| 199 | else { |
---|
| 200 | b = 1.77; |
---|
| 201 | si = 0.75; |
---|
| 202 | c = 0.1515 / s; |
---|
| 203 | } |
---|
| 204 | } |
---|
| 205 | /* Step 5: no quotient test if x not positive */ |
---|
[32] | 206 | |
---|
[145] | 207 | if ( x > 0.0 ) { |
---|
| 208 | /* Step 6: calculation of v and quotient q */ |
---|
| 209 | v = t / ( s + s ); |
---|
| 210 | if ( fabs ( v ) <= 0.25 ) |
---|
| 211 | q = q0 + 0.5 * t * t * ( ( ( ( ( ( a7 * v + a6 ) * v + a5 ) * v + a4 ) * v |
---|
| 212 | + a3 ) * v + a2 ) * v + a1 ) * v; |
---|
| 213 | else |
---|
| 214 | q = q0 - s * t + 0.25 * t * t + ( s2 + s2 ) * log ( 1.0 + v ); |
---|
[32] | 215 | |
---|
| 216 | |
---|
[145] | 217 | /* Step 7: quotient acceptance (q) */ |
---|
| 218 | if ( log ( 1.0 - u ) <= q ) |
---|
| 219 | return scale * ret_val; |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | for ( ;; ) { //VS repeat |
---|
| 223 | /* Step 8: e = standard exponential deviate |
---|
| 224 | * u = 0,1 -uniform deviate |
---|
| 225 | * t = (b,si)-double exponential (laplace) sample */ |
---|
| 226 | e = exp_rand(); |
---|
| 227 | u = unif_rand(); |
---|
| 228 | u = u + u - 1.0; |
---|
| 229 | if ( u < 0.0 ) |
---|
| 230 | t = b - si * e; |
---|
| 231 | else |
---|
| 232 | t = b + si * e; |
---|
| 233 | /* Step 9: rejection if t < tau(1) = -0.71874483771719 */ |
---|
| 234 | if ( t >= -0.71874483771719 ) { |
---|
| 235 | /* Step 10: calculation of v and quotient q */ |
---|
| 236 | v = t / ( s + s ); |
---|
| 237 | if ( fabs ( v ) <= 0.25 ) |
---|
| 238 | q = q0 + 0.5 * t * t * |
---|
| 239 | ( ( ( ( ( ( a7 * v + a6 ) * v + a5 ) * v + a4 ) * v + a3 ) * v |
---|
| 240 | + a2 ) * v + a1 ) * v; |
---|
| 241 | else |
---|
| 242 | q = q0 - s * t + 0.25 * t * t + ( s2 + s2 ) * log ( 1.0 + v ); |
---|
| 243 | /* Step 11: hat acceptance (h) */ |
---|
| 244 | /* (if q not positive go to step 8) */ |
---|
| 245 | if ( q > 0.0 ) { |
---|
| 246 | // TODO: w = expm1(q); |
---|
| 247 | w = exp ( q )-1; |
---|
| 248 | /* ^^^^^ original code had approximation with rel.err < 2e-7 */ |
---|
| 249 | /* if t is rejected sample again at step 8 */ |
---|
| 250 | if ( ( c * fabs ( u ) ) <= ( w * exp ( e - 0.5 * t * t ) ) ) |
---|
| 251 | break; |
---|
| 252 | } |
---|
| 253 | } |
---|
| 254 | } /* repeat .. until `t' is accepted */ |
---|
| 255 | x = s + 0.5 * t; |
---|
| 256 | return scale * x * x; |
---|
[32] | 257 | } |
---|
| 258 | |
---|
[37] | 259 | |
---|
[145] | 260 | bool qr ( const mat &A, mat &R ) { |
---|
| 261 | int info; |
---|
| 262 | int m = A.rows(); |
---|
| 263 | int n = A.cols(); |
---|
| 264 | int lwork = n; |
---|
| 265 | int k = std::min ( m, n ); |
---|
| 266 | vec tau ( k ); |
---|
| 267 | vec work ( lwork ); |
---|
[37] | 268 | |
---|
[145] | 269 | R = A; |
---|
[37] | 270 | |
---|
[145] | 271 | // perform workspace query for optimum lwork value |
---|
| 272 | int lwork_tmp = -1; |
---|
| 273 | dgeqrf_ ( &m, &n, R._data(), &m, tau._data(), work._data(), &lwork_tmp, |
---|
| 274 | &info ); |
---|
| 275 | if ( info == 0 ) { |
---|
| 276 | lwork = static_cast<int> ( work ( 0 ) ); |
---|
| 277 | work.set_size ( lwork, false ); |
---|
| 278 | } |
---|
| 279 | dgeqrf_ ( &m, &n, R._data(), &m, tau._data(), work._data(), &lwork, &info ); |
---|
[37] | 280 | |
---|
[145] | 281 | // construct R |
---|
| 282 | for ( int i=0; i<m; i++ ) |
---|
| 283 | for ( int j=0; j<std::min ( i,n ); j++ ) |
---|
| 284 | R ( i,j ) = 0; |
---|
[37] | 285 | |
---|
[145] | 286 | return ( info==0 ); |
---|
| 287 | } |
---|
[37] | 288 | |
---|
[263] | 289 | |
---|
| 290 | std::string num2str(double d){ |
---|
| 291 | char tmp[20];//that should do |
---|
| 292 | sprintf(tmp,"%f",d); |
---|
| 293 | return std::string(tmp); |
---|
| 294 | }; |
---|
| 295 | std::string num2str(int i){ |
---|
| 296 | char tmp[10];//that should do |
---|
| 297 | sprintf(tmp,"%d",i); |
---|
| 298 | return std::string(tmp); |
---|
| 299 | }; |
---|
[32] | 300 | } |
---|