matrixq.cc 10.6 KB
 Antoine Lucas committed Jan 06, 2017 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 /************************************************************/ /*! \file matrixq.cc * \brief C++ function to add matrix support * * \version 1 * * \date Created: 19/02/06 * \date Last modified: Time-stamp: <2008-02-17 19:54:11 antoine> * * \author A. Lucas * * \note * as usually, matrix x[i,j] (n x p) is represented by a vector * x[i + j x n] (i=0..n-1 ; j=0..p-1) *  Martin Maechler committed May 31, 2020 16  * \note Licence: GPL (>= 2)  Antoine Lucas committed Jan 06, 2017 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36  */ //#include #include using namespace std; #include "Rgmp.h" #include "bigrational.h" #include "bigrationalR.h" #include "matrixq.h" // function called by matrix.bigz() SEXP as_matrixq (SEXP x, SEXP nrR, SEXP ncR, SEXP byrowR, SEXP den) { /* get "bigz" vector, this makes all conversion int to bigz etc...*/ bigvec_q mat = bigrationalR::create_bignum(x), denominator = bigrationalR::create_bignum(den);  antoine lucas committed Mar 11, 2019 37 38 39 40 41  int nc= INTEGER(ncR)[0]; int nr= INTEGER(nrR)[0]; int byrow= INTEGER(byrowR)[0]; int lendat = mat.size();  Antoine Lucas committed Jan 06, 2017 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105  if(denominator.value.size()>0) // should be allways the case if(!denominator.value[0].isNA()) { for (unsigned int i = 0; i < mat.size(); i++) if(mat.value[i].isNA() && (denominator.value[i%denominator.size()].sgn() != 0) ) mat.value[i].setDenValue (denominator.value[i%denominator.size()].getValueTemp()); } /* A copy from R project to get correct dimension * all warnings... */ if (nr == NA_INTEGER) /* This is < 0 */ error(_("matrix: invalid 'nrow' value (too large or NA)")); if (nr < 0) error(_("matrix: invalid 'nrow' value (< 0)")); if (nc < 0) error(_("matrix: invalid 'ncol' value (< 0)")); if (nc == NA_INTEGER) error(_("matrix: invalid 'ncol' value (too large or NA)")); if(lendat > 0 ) { if (lendat > 1 && (nr * nc) % lendat != 0) { if (((lendat > nr) && (lendat / nr) * nr != lendat) || ((lendat < nr) && (nr / lendat) * lendat != nr)) warning(_("data length [%d] is not a sub-multiple or multiple of the number of rows [%d] in matrix"), lendat, nr); else if (((lendat > nc) && (lendat / nc) * nc != lendat) || ((lendat < nc) && (nc / lendat) * lendat != nc)) warning(_("data length [%d] is not a sub-multiple or multiple of the number of columns [%d] in matrix"), lendat, nc); } else if ((lendat > 1) && (nr * nc == 0)){ warning(_("data length exceeds size of matrix")); } } /* update dimension parameters */ if(nr == 1) nr = (int)ceil(lendat / (double) nc); if(nc == 1) nc = (int)ceil(lendat / (double)nr); /* when we extend "x" */ if(nc*nr > lendat) { mat.value.resize(nr*nc); for(int i = lendat; i < nr*nc; i++) mat.value[i] = mat.value[i % lendat]; } mat.nrow = nr; if(byrow) { bigvec_q mat2 = matrixq::bigq_transpose (mat, nc,nr); mat2.nrow = nr; return( bigrationalR::create_SEXP (mat2)); } return( bigrationalR::create_SEXP (mat)); } // function called by t(m) when m is a bigrational SEXP bigq_transposeR(SEXP x) {  antoine lucas committed Mar 11, 2019 106 107 108 109  SEXP strAttr = Rf_mkString("nrow"); PROTECT(strAttr); SEXP dimAttr = Rf_getAttrib(x, strAttr); PROTECT(dimAttr);  Antoine Lucas committed Jan 06, 2017 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124  bigvec_q mat = bigrationalR::create_bignum(x); int nr, n = mat.size(); if (dimAttr == R_NilValue) { // vector nr = n; } else if (TYPEOF(dimAttr) == INTSXP) { nr = INTEGER(dimAttr)[0]; } else { error(_("argument must be a matrix of class \"bigq\"")); nr = -1;// -Wall } int nc = (int) n / nr; bigvec_q mat_transp = matrixq::bigq_transpose(mat, nr,nc); mat_transp.nrow = nc; // FIXME - needed ?  antoine lucas committed Mar 11, 2019 125  UNPROTECT(2);  Antoine Lucas committed Jan 06, 2017 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432  return( bigrationalR::create_SEXP( mat_transp)); } /* \brief matrix cross product * * returns crossprod(a) := t(a) %*% a [p x p] or * tcrossprod(a) := a %*% t(a) [n x n] * \param a matrix (n x p) * \param trans if(trans), compute tcrossprod(), else crossprod() */ SEXP matrix_crossp_q (SEXP a, SEXP trans) { bool tr = (bool)Rf_asLogical(trans); bigvec_q mat_a = bigrationalR::create_bignum(a); int a_nrow = mat_a.nrow, a_len = mat_a.size(); // in case of a vector; crossprod() returns scalar product, // whereas tcrossprod() gives n x n matrix. if(a_nrow < 0) a_nrow = a_len; int a_ncol = a_len / a_nrow; // Result R is R[1..m, 1..m] -- and R_{ij} = sum_{k=1}^p A.. B.. int m, p; if(tr) { // tcrossprod() m= a_nrow; p= a_ncol; } else { // crossprod() m= a_ncol; p= a_nrow; } bigvec_q res(m*m); res.nrow= m; mpq_t R_ij, tt; mpq_init(R_ij); mpq_init(tt); // here the computation: for(int i=0; i < m; i++) for(int j=0; j < m; j++) { mpq_set_ui(R_ij, 0,1); bool isna = false; #define K_LOOP \ for(int k=0; k < p; k++) { \ /* R_ij = \sum_{k=1}^p a_{ik} b_{kj} */ \ if( !(A_I_K.isNA() || B_K_J.isNA())) { \ mpq_mul(tt, A_I_K.getValueTemp(), B_K_J.getValueTemp()); \ mpq_add(R_ij, tt,R_ij); \ } \ else { \ isna = true; break; \ } \ } if(tr) {//------------- tcrossprod --------------------------- #define A_I_K mat_a[ i + k *a_nrow] #define B_K_J mat_a[ j + k *a_nrow] K_LOOP #undef A_I_K #undef B_K_J } else {//------------- crossprod --------------------------- #define A_I_K mat_a[ k + i *a_nrow] #define B_K_J mat_a[ k + j *a_nrow] K_LOOP #undef A_I_K #undef B_K_J } if(isna) { res.value[i + j*m].setValue(0); res.value[i + j*m].NA(true); } else res.value[i + j*m].setValue(R_ij); } mpq_clear(R_ij); mpq_clear(tt); return( bigrationalR::create_SEXP (res)); } // matrix_crossp_q() /** \brief matrix multiplication * * returns matrix multiplication T(a) %*% b or b %*% T(a) * \param a is of dimension n x p * \param b is of dimension p x m * \param op operation code: 0: %*%, 1: crossprod, 2: tcrossprod * (same codes as in R's do_matprod() in src/main/array.c ) */ SEXP matrix_mul_q (SEXP a, SEXP b, SEXP op) { int o_ = Rf_asInteger(op); // INTEGER(op)[0] bigvec_q mat_a = bigrationalR::create_bignum(a), mat_b = bigrationalR::create_bignum(b); int a_nrow = mat_a.nrow, a_len = mat_a.size(), b_nrow = mat_b.nrow, b_len = mat_b.size(), a_ncol = -1, b_ncol = -1;// -Wall // distinguish cases of vectors / matrices --------------------- if(a_nrow < 0) { if(b_nrow < 0) { // *both* are vectors if(o_ == 0) { a_nrow = 1; a_ncol = a_len; } else { a_nrow = a_len; a_ncol = 1; } b_nrow = b_len; b_ncol = 1; } else { // a : vector, b : matrix b_ncol = b_len / b_nrow; if(o_ == 0) { if (a_len == b_nrow) { /* x as row vector */ a_nrow = 1; a_ncol = b_nrow; /* == a_len */ } else if (b_nrow == 1) { /* x as col vector */ a_nrow = a_len; a_ncol = 1; } } else if(o_ == 1) { /* crossprod() */ if (a_len == b_nrow) { /* x is a col vector */ a_nrow = b_nrow; /* == a_len */ a_ncol = 1; } /* else if (b_nrow == 1) ... not being too tolerant to treat x as row vector, as t(x) *is* row vector */ } else { // o_ == 2 -- tcrossprod() if (a_len == b_ncol) { /* x as row vector */ a_nrow = 1; a_ncol = b_ncol; /* == a_len */ } else if (b_ncol == 1) { /* x as col vector */ a_nrow = a_len; a_ncol = 1; } } } } else if (b_nrow < 0) { // a : matrix, b : vector a_ncol = a_len / a_nrow; if (o_ == 0) { if (b_len == a_ncol) { /* y as col vector */ b_nrow = a_ncol; b_ncol = 1; } else if (a_ncol == 1) { /* y as row vector */ b_nrow = 1; b_ncol = b_len; } } else if (o_ == 1) { /* crossprod() */ if (b_len == a_nrow) { /* y is a col vector */ b_nrow = a_nrow; b_ncol = 1; } } else { /* tcrossprod -- y is a col vector */ b_nrow = b_len; b_ncol = 1; } } else { // a, b *both* matrices a_ncol = a_len / a_nrow; b_ncol = b_len / b_nrow; } if(((o_ == 0) && (a_ncol != b_nrow)) || ((o_ == 1) && (a_nrow != b_nrow)) || // crossprod() ((o_ == 2) && (a_ncol != b_ncol)) // tcrossprod() ) error(_("Matrix dimensions do not match")); // Result R is R[1..n, 1..m] -- and R_{ij} = sum_{k=1} ^ p A.. B.. int n,m, p; if(o_ == 0) { n= a_nrow; m= b_ncol; p= a_ncol;// = b_nrow }else if (o_ == 1) { n= a_ncol; m= b_ncol; p= a_nrow;// = b_nrow }else if (o_ == 2) { n= a_nrow; m= b_nrow; p= a_ncol;// = b_ncol }else { error(_("invalid 'op' code in matrix_mul_z()")); n = m = p = -1;// -Wall } bigvec_q res(n*m); res.nrow=n; mpq_t tt, R_ij; mpq_init(R_ij); mpq_init(tt); // here the computation: for(int i=0; i < n; i++) for(int j=0; j < m; j++) { mpq_set_ui(R_ij, 0,1); bool isna = false; #define K_LOOP \ for(int k=0; k < p; k++) { \ /* R_ij = \sum_{k=1}^p a_{ik} b_{kj} */ \ if( !(A_I_K.isNA() || B_K_J.isNA())) { \ mpq_mul(tt, A_I_K.getValueTemp(), B_K_J.getValueTemp()); \ mpq_add(R_ij, tt,R_ij); \ } \ else { \ isna = true; break; \ } \ } if(o_ == 0) { //------------- %*% -------------------------------------- #define A_I_K mat_a[ i + k *a_nrow] #define B_K_J mat_b[ k + j *b_nrow] K_LOOP #undef A_I_K #undef B_K_J } else if(o_ == 1){//------------- crossprod --------------------------- #define A_I_K mat_a[ k + i *a_nrow] #define B_K_J mat_b[ k + j *b_nrow] K_LOOP #undef A_I_K #undef B_K_J } else {//(o_ == 2) ------------- tcrossprod --------------------------- #define A_I_K mat_a[ i + k *a_nrow] #define B_K_J mat_b[ j + k *b_nrow] K_LOOP #undef A_I_K #undef B_K_J } if(isna) { res.value[i + j*n].setValue(0); res.value[i + j*n].NA(true); } else res.value[i + j*n].setValue(R_ij); } mpq_clear(R_ij); mpq_clear(tt); return( bigrationalR::create_SEXP (res)); } // matrix_mul_q() #undef K_LOOP SEXP bigrational_rbind(SEXP args) { int i=0,j=0; bigvec_q result; bigvec_q v; result = bigrationalR::create_bignum(VECTOR_ELT(args,0)); if(result.nrow ==0) result.nrow = result.size(); result = matrixq::bigq_transpose(result, result.nrow, result.size() / result.nrow); for(i=1; i< LENGTH(args); i++) { v = bigrationalR::create_bignum(VECTOR_ELT(args,i)); if(v.nrow == 0 ) v.nrow = v.size(); v = matrixq::bigq_transpose(v,v.nrow,v.size() / v.nrow); for(j=0; j< (int)v.size(); j++) result.push_back(v[j]); v.clear(); } result = matrixq::bigq_transpose(result, result.nrow, result.size() / result.nrow); return bigrationalR::create_SEXP(result); } bigvec_q matrixq::bigq_transpose (const bigvec_q & mat,int nr,int nc) { int i,j; bigvec_q matbis (nr * nc); matbis.nrow = nc; /* we compute transpose */ for(i=0; i < nr; i++) for(j=0; j < nc; j++) matbis.value[j+i*nc].setValue(mat.value[i+j*nr]); return(matbis); }