diff --git a/src/bigintegerR.cc b/src/bigintegerR.cc index 090d613466fa2f7c1f11dd4e2a8a3e9cc9904ff1..f44e4235aaf459d48460d7b7288a49dcfde0eeb5 100644 --- a/src/bigintegerR.cc +++ b/src/bigintegerR.cc @@ -566,23 +566,6 @@ SEXP biginteger_c(SEXP args) return bigintegerR::create_SEXP(result); } -SEXP biginteger_cbind(SEXP args) -{ - // if(TYPEOF(args) != VECSXP) error(_("should be a list")); - bigvec result = bigintegerR::create_bignum(VECTOR_ELT(args,0)); - if(result.nrow <= 0) - result.nrow = result.size(); - - for(int i = 1; i < LENGTH(args);i++) - { - bigvec v = bigintegerR::create_bignum(VECTOR_ELT(args,i)); - for(unsigned int j=0; j< v.size() ; j++) - result.push_back(v[j]); - v.clear(); - } - - return bigintegerR::create_SEXP(result); -} SEXP biginteger_rep(SEXP x, SEXP times) { diff --git a/src/bigvec.cc b/src/bigvec.cc index c76059c2df367f3ce4903002a64d00772fc4a91f..d6c9ee3820c98b269ce5978adc3822db89f33823 100644 --- a/src/bigvec.cc +++ b/src/bigvec.cc @@ -67,7 +67,7 @@ std::string bigvec::str(int i,int b) const } bigmod & bigvec::get(unsigned int row, unsigned int col) { - return (*this)[row + col*nrow]; + return (*this)[row + col*nRows()]; } @@ -86,7 +86,7 @@ const bigmod & bigvec::operator[] (unsigned int i) const } void bigvec::set(unsigned int row, unsigned int col, const bigmod & val) { - set( row + col*nrow,val); + set( row + col*nRows(),val); } void bigvec::checkValuesMod() { diff --git a/src/bigvec_q.cc b/src/bigvec_q.cc index 8143535e9f84d5780603d5cd66a04f1947f6e85f..51c4480db00f41e18472da84fb0b5e32dd9e5769 100644 --- a/src/bigvec_q.cc +++ b/src/bigvec_q.cc @@ -57,24 +57,19 @@ bigrational & bigvec_q::operator[] (unsigned int i) } bigrational & bigvec_q::get(unsigned int row, unsigned int col) { - return (*this)[row + col*nrow]; + bigrational & val = (*this)[row + col*nRows()]; + return val; } void bigvec_q::set(unsigned int row, unsigned int col, const bigrational & val) { - set( row + col*nrow,val); + set( row + col*nRows(),val); } void bigvec_q::set(unsigned int i,const bigrational & val) { - //DEBUG !! - if(i>=value.size()) - { - Rprintf("t nul a bigvec_q_set\n"); - return; - } value[i] = val; } void bigvec_q::set(unsigned int i,const mpq_t & val) @@ -82,7 +77,7 @@ void bigvec_q::set(unsigned int i,const mpq_t & val) if(i>=value.size()) { - Rprintf("t nul a bigvec_q_set_mpq __LINE__ \n"); + Rprintf("ERROR at bigvec_q_set_mpq __LINE__ \n"); return; } diff --git a/src/matrix.cc b/src/matrix.cc index a29502374ac258a6a3a27b0147f6eb131a77fcec..551f381d25a1f97514d26457342a306a1ae1fb44 100644 --- a/src/matrix.cc +++ b/src/matrix.cc @@ -5,7 +5,7 @@ * \version 1 * * \date Created: 19/02/06 - * \date Last modified: Time-stamp: <2022-12-09 09:51:38 (antoine)> + * \date Last modified: Time-stamp: <2022-12-09 15:55:54 (antoine)> * * \author A. Lucas * @@ -489,6 +489,45 @@ SEXP biginteger_rbind(SEXP args) return bigintegerR::create_SEXP(result); } +/** + * arg = v1, v2, v3.... row + * return matrix + * out =[ v1 v2 v3 ] + */ +SEXP biginteger_cbind(SEXP args) +{ + int i=0,j=0; + bigvec result; + bigvec v; + vector<bigvec> source; + unsigned int maxSize=0; + for(int i = 0 ; i < LENGTH(args) ; i++){ + v = bigintegerR::create_bignum(VECTOR_ELT(args,i)); + + if(v.size() == 0) continue; + if(v.nrow <0) v.nrow = v.size(); + for(int col = 0 ; col < v.nCols(); col++){ + bigvec column ; + for (int row = 0 ; row < v.nRows(); row++){ + column.push_back(v.get(row,col)); + } + source.push_back(column); + maxSize = std::max(maxSize,column.size()); + } + } + + for(int i = 0 ; i < source.size() ; i++){ + bigvec u = source[i]; + for (int j = 0 ; j < maxSize; j++){ + if(u.size() == 0) result.push_back(bigmod()); + else result.push_back(u[j % u.size()]); + } + } + result.nrow = result.size() / source.size(); + + return bigintegerR::create_SEXP(result); +} + namespace matrixz diff --git a/src/matrix.h b/src/matrix.h index b003fa96b208210a8899be04817e89e2e586d16a..9be6825393a62a4ad1dc3ceddd6ef90bd307e877 100644 --- a/src/matrix.h +++ b/src/matrix.h @@ -4,7 +4,7 @@ * \version 1 * * \date Created: 25/05/06 - * \date Last modified: Time-stamp: <2022-02-21 15:57:36 (antoine)> + * \date Last modified: Time-stamp: <2022-12-09 14:56:06 (antoine)> * * \author A. Lucas * @@ -45,6 +45,10 @@ extern "C" */ SEXP biginteger_rbind(SEXP args) ; + /** \brief for function rbind + */ + SEXP biginteger_cbind(SEXP args) ; + } diff --git a/src/matrixq.cc b/src/matrixq.cc index d8ecd9f8947c49745bd29a9ec7dfda0eab8fa186..8563cc95d4a76fc763282254a942753093bf6519 100644 --- a/src/matrixq.cc +++ b/src/matrixq.cc @@ -5,7 +5,7 @@ * \version 1 * * \date Created: 19/02/06 - * \date Last modified: Time-stamp: <2022-08-04 15:52:51 (antoine)> + * \date Last modified: Time-stamp: <2022-12-09 15:55:39 (antoine)> * * \author A. Lucas * @@ -394,27 +394,67 @@ SEXP bigrational_rbind(SEXP args) int i=0,j=0; bigvec_q result; bigvec_q v; + vector<bigvec_q> source; + unsigned int maxSize=0; - result = bigrationalR::create_bignum(VECTOR_ELT(args,0)); - if(result.nrow <=0) - result.nrow = result.size(); - - result = matrixq::bigq_transpose(result); - for(i=1; i< LENGTH(args); i++) { + for(int i = 0 ; 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); - - for(j=0; j< (int)v.size(); j++) - result.push_back(v[j]); - v.clear(); + if(v.size() == 0) continue; + for (int row = 0 ; row < v.nRows(); row++){ + bigvec_q line ; + for(int col = 0 ; col < v.nCols(); col++){ + line.push_back(v.get(row,col)); + } + source.push_back(line); + maxSize = std::max(maxSize,line.size()); + } } + + for (int j = 0 ; j < maxSize; j++){ + for(int i = 0 ; i < source.size() ; i++){ + bigvec_q u = source[i]; + if(u.size() == 0) result.push_back(bigrational()); + else result.push_back(u[j % u.size()]); + } + } + result.nrow = source.size(); - result = matrixq::bigq_transpose(result); return bigrationalR::create_SEXP(result); } +SEXP bigrational_cbind(SEXP args){ + int i=0,j=0; + bigvec_q result; + bigvec_q v; + vector<bigvec_q> source; + unsigned int maxSize=0; + for(int i = 0 ; i < LENGTH(args) ; i++){ + v = bigrationalR::create_bignum(VECTOR_ELT(args,i)); + + if(v.size() == 0) continue; + if(v.nrow <0) v.nrow = v.size(); + for(int col = 0 ; col < v.nCols(); col++){ + bigvec_q column ; + for (int row = 0 ; row < v.nRows(); row++){ + column.push_back(v.get(row,col)); + } + source.push_back(column); + maxSize = std::max(maxSize,column.size()); + } + } + + for(int i = 0 ; i < source.size() ; i++){ + bigvec_q u = source[i]; + for (int j = 0 ; j < maxSize; j++){ + if(u.size() == 0) result.push_back(bigrational()); + else result.push_back(u[j % u.size()]); + } + } + result.nrow = result.size() / source.size(); + + return bigrationalR::create_SEXP(result); + +} bigvec_q matrixq::bigq_transpose (const bigvec_q & mat) { diff --git a/src/matrixq.h b/src/matrixq.h index 1ce88e0996ccfb56ee33d036edde4e270b74f83b..c3f2c41090ca1973685cdf92957e3c681f20b109 100644 --- a/src/matrixq.h +++ b/src/matrixq.h @@ -2,7 +2,7 @@ * \brief header for rational matrix functions set * * \date Created: 2005 - * \date Last modified: Time-stamp: <2022-02-21 15:43:22 (antoine)> + * \date Last modified: Time-stamp: <2022-12-09 15:15:01 (antoine)> * * * \note Licence: GPL (>= 2) @@ -31,6 +31,8 @@ extern "C" /** \brief for function rbind */ SEXP bigrational_rbind(SEXP args) ; + + SEXP bigrational_cbind(SEXP args) ; } /** diff --git a/src/templateMatrix.h b/src/templateMatrix.h index 5107c9d830c860f3377b261189b8f033c3b5df09..c7fceed371cc681032501cb27e610384145e24c8 100644 --- a/src/templateMatrix.h +++ b/src/templateMatrix.h @@ -122,7 +122,7 @@ namespace math{ template<class T> unsigned int Matrix<T>::nCols() const{ - + if(nRows() <=0) return this->size(); return this->size() / nRows(); } diff --git a/tests/gmp-test.R b/tests/gmp-test.R index 9d3148a27c129459501807ff31ccd0798a84b8e0..02af889d7119e1d4e81683c236c86000d7f1eec7 100644 --- a/tests/gmp-test.R +++ b/tests/gmp-test.R @@ -39,7 +39,7 @@ test <- function(FUN, x, xlabs, out = "str", unary = FALSE) else if(length(e) == 0) e <- numeric() ## we don't test standard R floating operations. - if( (classNameI == "numeric" || classNameI == "integer") && ( classNameJ == "numeric" || classNameJ == "integer") && class(e) == "numeric") e <- "-" + if( (classNameI[1] == "numeric" || classNameI[1] == "integer") && ( classNameJ[1] == "numeric" || classNameJ[1] == "integer") && class(e)[1] == "numeric") e <- "-" ## ## now, for some functions also compute the corresponding numeric values if(length(e) > 0 && is.double(e[1]) && is.finite(e[1])) @@ -57,7 +57,7 @@ allfunctionid <- c("as.bigz","+","-","*", "inv.bigz", "gcd.bigz", "gcdex", "lcm.bigz", "as.bigq", "chooseZ", - "max","min","|","&","xor") + "max","min","|","&","xor","c","cbind","rbind") unaryfunctionid <- c("log","log2","log10","c", "isprime","nextprime", "factorialZ", "sizeinbase","fibnum","fibnum2","lucnum","lucnum2", diff --git a/tests/gmp-test.Rout.save b/tests/gmp-test.Rout.save index 69158ead6fe25f8cd9f3d702321b8da7206bf488..151d32d706a47738fe580ebaa270cfb7d2eaba60 100644 --- a/tests/gmp-test.Rout.save +++ b/tests/gmp-test.Rout.save @@ -63,7 +63,7 @@ The following objects are masked from 'package:base': + else if(length(e) == 0) + e <- numeric() + ## we don't test standard R floating operations. -+ if( (classNameI == "numeric" || classNameI == "integer") && ( classNameJ == "numeric" || classNameJ == "integer") && class(e) == "numeric") e <- "-" ++ if( (classNameI[1] == "numeric" || classNameI[1] == "integer") && ( classNameJ[1] == "numeric" || classNameJ[1] == "integer") && class(e)[1] == "numeric") e <- "-" + + ## ## now, for some functions also compute the corresponding numeric values + if(length(e) > 0 && is.double(e[1]) && is.finite(e[1])) @@ -81,7 +81,7 @@ The following objects are masked from 'package:base': + "inv.bigz", "gcd.bigz", "gcdex", "lcm.bigz", + "as.bigq", + "chooseZ", -+ "max","min","|","&","xor") ++ "max","min","|","&","xor","c","cbind","rbind") > unaryfunctionid <- c("log","log2","log10","c", + "isprime","nextprime", "factorialZ", + "sizeinbase","fibnum","fibnum2","lucnum","lucnum2", @@ -112,8 +112,8 @@ The following objects are masked from 'package:base': > sapply(allfunctionid, numericFunName) as.bigz + - * divq.bigz / %% ^ inv.bigz gcd.bigz gcdex lcm.bigz "as.bigz" "+" "-" "*" "%/%" "/" "%%" "^" "inv.bigz" "gcd" "gcdex" "lcm" - as.bigq chooseZ max min | & xor - "as.bigq" "choose" "max" "min" "|" "&" "xor" + as.bigq chooseZ max min | & xor c cbind rbind + "as.bigq" "choose" "max" "min" "|" "&" "xor" "c" "cbind" "rbind" > sapply(unaryfunctionid, numericFunName) log log2 log10 c isprime nextprime factorialZ sizeinbase fibnum fibnum2 "log" "log2" "log10" "c" "isprime" "nextprime" "factorial" "sizeinbase" "fibnum" "fibnum2" @@ -1114,11 +1114,167 @@ NULL <NA> <NA> <NA> <NA> <NA> NA <NA> <NA> <NA> <NA> <NA> -3L FALSE FALSE <NA> <NA> FALSE -There were 3685 warnings (use warnings() to see them) +------------------------------------------ +c +-> all.equal(target = res, current = F(<numeric x>)): 'is.NA' value mismatch: 256 in current 63 in target + 23 as.bigz(23) as.bigq(23) c(3,23) as.bigz(c(3,23)) as.bigq(c(3,23)) 25 2.3 -4 4 +23 - 23 23 - 23 23 23 - - - +as.bigz(23) 23 23 23 23 23 23 23 23 23 23 +as.bigq(23) 23 23 23 23 23 23 23 23 23 23 +c(3,23) - 3 3 - 3 3 3 - - - +as.bgz((3,23)) 3 3 3 3 3 3 3 3 3 3 +as.bgq((3,23)) 3 3 3 3 3 3 3 3 3 3 +25 25 25 25 25 25 25 25 25 25 25 +2.3 - 2.3 2.3 - 2.3 2.3 2.3 - - - +-4 - -4 -4 - -4 -4 -4 - - - +4 - 4 4 - 4 4 4 - - 4 +0 - 0 0 - 0 0 0 - - - +as.bigz(34) 34 34 34 34 34 34 34 34 34 34 +as.bg(32,7) 32/7 32/7 32/7 32/7 32/7 32/7 32/7 32/7 32/7 32/7 +as.b(31,45) (31 %% 45) (31 %% 45) 31 (31 %% 45) (31 %% 45) 31 (31 %% 45) (31 %% 45) (31 %% 45) (31 %% 45) +NULL 23 01 01 3 02 02 25 2.3 -4 4 +NA <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> +-3L - -3 -3 - -3 -3 -3 - - -3 + 0 as.bigz(34) as.bigq(32,7) as.bigz(31,45) NULL NA -3L +23 - 23 23 23 23 23 - +as.bigz(23) 23 23 23 23 23 23 23 +as.bigq(23) 23 23 23 23 23 23 23 +c(3,23) - 3 3 3 3 3 - +as.bgz((3,23)) 3 3 3 3 3 3 3 +as.bgq((3,23)) 3 3 3 3 3 3 3 +25 25 25 25 25 25 25 25 +2.3 - 2.3 2.3 2.3 2.3 2.3 - +-4 - -4 -4 -4 -4 -4 - +4 - 4 4 4 4 4 4 +0 - 0 0 0 0 0 - +as.bigz(34) 34 34 34 34 34 34 34 +as.bg(32,7) 32/7 32/7 32/7 32/7 32/7 32/7 32/7 +as.b(31,45) (31 %% 45) (31 %% 45) 31 (31 %% 45) (31 %% 45) (31 %% 45) (31 %% 45) +NULL 0 01 01 01 <NA> <NA> -3 +NA <NA> <NA> <NA> <NA> <NA> <NA> <NA> +-3L - -3 -3 -3 -3 -3 -3 + +------------------------------------------ +cbind +-> all.equal(target = res, current = F(<numeric x>)): Mean relative difference: 0.01075094 + 23 as.bigz(23) as.bigq(23) c(3,23) as.bigz(c(3,23)) as.bigq(c(3,23)) +23 23 23 23 23 23 23 +as.bigz(23) 23 23 23 23 23 23 +as.bigq(23) 23 23 23 23 23 23 +c(3,23) 3 3 3 3 3 3 +as.bgz((3,23)) 3 3 3 3 3 3 +as.bgq((3,23)) 3 3 3 3 3 3 +25 25 25 25 25 25 25 +2.3 2.3 2 2589569785738035/1125899906842624 2.3 2 2589569785738035/1125899906842624 +-4 -4 -4 -4 -4 -4 -4 +4 4 4 4 4 4 4 +0 0 0 0 0 0 0 +as.bigz(34) 34 34 34 34 34 34 +as.bg(32,7) 32/7 32/7 32/7 32/7 32/7 32/7 +as.b(31,45) (31 %% 45) (31 %% 45) (31 %% 45) (31 %% 45) (31 %% 45) (31 %% 45) +NULL 23 23 23 3 3 3 +NA <NA> NA NA <NA> NA NA +-3L -3 -3 -3 -3 -3 -3 + 25 2.3 -4 4 0 as.bigz(34) as.bigq(32,7) as.bigz(31,45) +23 23 23 23 23 23 23 23 23 +as.bigz(23) 23 23 23 23 23 23 23 23 +as.bigq(23) 23 23 23 23 23 23 23 23 +c(3,23) 3 3 3 3 3 3 3 3 +as.bgz((3,23)) 3 3 3 3 3 3 3 3 +as.bgq((3,23)) 3 3 3 3 3 3 3 3 +25 25 25 25 25 25 25 25 25 +2.3 2.3 2.3 2.3 2.3 2.3 2 2589569785738035/1125899906842624 2 +-4 -4 -4 -4 -4 -4 -4 -4 -4 +4 4 4 4 4 4 4 4 4 +0 0 0 0 0 0 0 0 0 +as.bigz(34) 34 34 34 34 34 34 34 34 +as.bg(32,7) 32/7 32/7 32/7 32/7 32/7 32/7 32/7 32/7 +as.b(31,45) (31 %% 45) (31 %% 45) (31 %% 45) (31 %% 45) (31 %% 45) (31 %% 45) (31 %% 45) (31 %% 45) +NULL 25 2.3 -4 4 0 34 32/7 (31 %% 45) +NA <NA> <NA> <NA> <NA> <NA> NA NA NA +-3L -3 -3 -3 -3 -3 -3 -3 -3 + NULL NA -3L +23 23 23 23 +as.bigz(23) 23 23 23 +as.bigq(23) 23 23 23 +c(3,23) 3 3 3 +as.bgz((3,23)) 3 3 3 +as.bgq((3,23)) 3 3 3 +25 25 25 25 +2.3 2.3 2.3 2.3 +-4 -4 -4 -4 +4 4 4 4 +0 0 0 0 +as.bigz(34) 34 34 34 +as.bg(32,7) 32/7 32/7 32/7 +as.b(31,45) (31 %% 45) (31 %% 45) (31 %% 45) +NULL <NA> <NA> -3 +NA <NA> <NA> <NA> +-3L -3 -3 -3 + +------------------------------------------ +rbind +-> all.equal(target = res, current = F(<numeric x>)): Mean relative difference: 0.01075094 + 23 as.bigz(23) as.bigq(23) c(3,23) as.bigz(c(3,23)) as.bigq(c(3,23)) +23 23 23 23 23 23 23 +as.bigz(23) 23 23 23 23 23 23 +as.bigq(23) 23 23 23 23 23 23 +c(3,23) 3 3 3 3 3 3 +as.bgz((3,23)) 3 3 3 3 3 3 +as.bgq((3,23)) 3 3 3 3 3 3 +25 25 25 25 25 25 25 +2.3 2.3 2 2589569785738035/1125899906842624 2.3 2 2589569785738035/1125899906842624 +-4 -4 -4 -4 -4 -4 -4 +4 4 4 4 4 4 4 +0 0 0 0 0 0 0 +as.bigz(34) 34 34 34 34 34 34 +as.bg(32,7) 32/7 32/7 32/7 32/7 32/7 32/7 +as.b(31,45) (31 %% 45) (31 %% 45) (31 %% 45) (31 %% 45) (31 %% 45) (31 %% 45) +NULL 23 23 23 3 3 3 +NA <NA> NA NA <NA> NA NA +-3L -3 -3 -3 -3 -3 -3 + 25 2.3 -4 4 0 as.bigz(34) as.bigq(32,7) as.bigz(31,45) +23 23 23 23 23 23 23 23 23 +as.bigz(23) 23 23 23 23 23 23 23 23 +as.bigq(23) 23 23 23 23 23 23 23 23 +c(3,23) 3 3 3 3 3 3 3 3 +as.bgz((3,23)) 3 3 3 3 3 3 3 3 +as.bgq((3,23)) 3 3 3 3 3 3 3 3 +25 25 25 25 25 25 25 25 25 +2.3 2.3 2.3 2.3 2.3 2.3 2 2589569785738035/1125899906842624 2 +-4 -4 -4 -4 -4 -4 -4 -4 -4 +4 4 4 4 4 4 4 4 4 +0 0 0 0 0 0 0 0 0 +as.bigz(34) 34 34 34 34 34 34 34 34 +as.bg(32,7) 32/7 32/7 32/7 32/7 32/7 32/7 32/7 32/7 +as.b(31,45) (31 %% 45) (31 %% 45) (31 %% 45) (31 %% 45) (31 %% 45) (31 %% 45) (31 %% 45) (31 %% 45) +NULL 25 2.3 -4 4 0 34 32/7 (31 %% 45) +NA <NA> <NA> <NA> <NA> <NA> NA NA NA +-3L -3 -3 -3 -3 -3 -3 -3 -3 + NULL NA -3L +23 23 23 23 +as.bigz(23) 23 23 23 +as.bigq(23) 23 23 23 +c(3,23) 3 3 3 +as.bgz((3,23)) 3 3 3 +as.bgq((3,23)) 3 3 3 +25 25 25 25 +2.3 2.3 2.3 2.3 +-4 -4 -4 -4 +4 4 4 4 +0 0 0 0 +as.bigz(34) 34 34 34 +as.bg(32,7) 32/7 32/7 32/7 +as.b(31,45) (31 %% 45) (31 %% 45) (31 %% 45) +NULL <NA> <NA> -3 +NA <NA> <NA> <NA> +-3L -3 -3 -3 + +There were 3988 warnings (use warnings() to see them) > > summary(warnings()) # ideally *not* platform dependent -Summary of (a total of 3685) warning messages: -3260x : In sortie(e) : NAs introduced by coercion +Summary of (a total of 3988) warning messages: +3563x : In sortie(e) : NAs introduced by coercion 35x : In FUN(x[[i]], x[[j]]) : returning NA for (modulus) 0 in RHS 5x : In `/.bigz`(x[[i]], x[[j]]) : pow(x, -|n|) returning NA as x has no inverse wrt modulus @@ -1380,4 +1536,4 @@ Big Integer ('bigz') 3 x 4 matrix: > > proc.time() user system elapsed - 0.913 0.060 0.971 + 0.952 0.068 1.027