TEST(RingRRR, power) { RRR *R = RRR::create(100); mpz_t gmp1; mpz_init(gmp1); RingElementGenerator<RRR> gen(*R); for (int i=0; i<ntrials; i++) { ring_elem a = gen.nextElement(); //TODO: what should the answer here be? //EXPECT_TRUE(R->is_equal(R->power(a, 0), R->one())); // 0^0 == 1 too? EXPECT_TRUE(R->is_equal(R->power(a, 1), a)); int e1 = rawRandomInt(10) + 1; int e2 = rawRandomInt(10) + 1; //std::cout << "(" << e1 << "," << e2 << ")" << std::endl; ring_elem b = R->power(a, e1); ring_elem c = R->power(a, e2); ring_elem d = R->power(a, e1+e2); EXPECT_TRUE(almostEqual(R,96,R->mult(b,c),d)); // Make sure that powers via mpz work (at least for small exponents) mpz_set_si(gmp1, e1); ring_elem b1 = R->power(a, gmp1); EXPECT_TRUE(R->is_equal(b1, b)); } mpz_clear(gmp1); }
void rawMutableMatrixFillRandomDensity(MutableMatrix *M, double density, int special_type) /* special_type: 0 is general, 1 is (strictly) upper triangular. */ { bool doing_fraction = false; int threshold = 0; int nrows = M->n_rows(); int ncols = M->n_cols(); const Ring *R = M->get_ring(); if (density != 1.0) { doing_fraction = true; threshold = static_cast<int>(density * 10000); } if (special_type == 0) { for (int i=0; i<ncols; i++) for (int j=0; j<nrows; j++) { if (doing_fraction) { int32_t u = rawRandomInt((int32_t)10000); if (u > threshold) continue; } ring_elem a = R->random(); if (!R->is_zero(a)) M->set_entry(j,i,a); } } else if (special_type == 1) { for (int i=0; i<ncols; i++) { int top = (i>=nrows ? nrows : i); for (int j=0; j<top; j++) { if (doing_fraction) { int32_t u = rawRandomInt((int32_t)10000); if (u > threshold) continue; } ring_elem a = R->random(); if (!R->is_zero(a)) M->set_entry(j,i,a); } } } }
void rawMutableMatrixFillRandom(MutableMatrix *M, long nelems) { int nrows = M->n_rows(); int ncols = M->n_cols(); const Ring *R = M->get_ring(); for (long i=0; i<nelems; i++) { int r = rawRandomInt(nrows); int c = rawRandomInt(ncols); ring_elem a = R->random(); if (!R->is_zero(a)) M->set_entry(r,c,R->random()); } }
TEST(ARingCC, power_and_invert) { M2::ARingCC C; auto nbits = C.get_precision(); ARingElementGenerator<M2::ARingCC> gen(C); M2::ARingCC::ElementType a,b,c,d; C.init(a); C.init(b); C.init(c); C.init(d); mpz_t gmp1; mpz_init(gmp1); for (int i=0; i<ntrials; i++) { gen.nextElement(a); //TODO: what should the answer here be? //EXPECT_TRUE(R->is_equal(R->power(a, 0), R->one())); // 0^0 == 1 too? C.power(b,a,1); EXPECT_TRUE(C.is_equal(b,a)); int e1 = rawRandomInt(10) + 1; int e2 = rawRandomInt(10) + 1; C.power(b, a, e1); C.power(c, a, e2); C.power(d, a, e1+e2); C.mult(c,b,c); EXPECT_TRUE(almostEqual(C,nbits-11,c,d)); /* exponentiantion gives relatively small number of correct digits */ // Make sure that powers via mpz work (at least for small exponents) mpz_set_si(gmp1, e1); C.power_mpz(d, a, gmp1); EXPECT_TRUE(C.is_equal(d, b)); } mpz_clear(gmp1); C.clear(d); C.clear(c); C.clear(b); C.clear(a); }
TEST(ARingRR, power_and_invert) { M2::ARingRR R; auto nbits = R.get_precision(); ARingElementGenerator<M2::ARingRR> gen(R); M2::ARingRR::ElementType a,b,c,d; R.init(a); R.init(b); R.init(c); R.init(d); mpz_t gmp1; mpz_init(gmp1); for (int i=0; i<ntrials; i++) { gen.nextElement(a); //TODO: what should the answer here be? //EXPECT_TRUE(R->is_equal(R->power(a, 0), R->one())); // 0^0 == 1 too? R.power(b,a,1); EXPECT_TRUE(R.is_equal(b,a)); int e1 = rawRandomInt(10) + 1; int e2 = rawRandomInt(10) + 1; R.power(b, a, e1); R.power(c, a, e2); R.power(d, a, e1+e2); R.mult(c,b,c); EXPECT_TRUE(almostEqual(R,nbits-4,c,d)); // Make sure that powers via mpz work (at least for small exponents) mpz_set_si(gmp1, e1); R.power_mpz(d, a, gmp1); EXPECT_TRUE(R.is_equal(d, b)); } mpz_clear(gmp1); R.clear(d); R.clear(c); R.clear(b); R.clear(a); }
void getElement<M2::ARingGFGivaro>(const M2::ARingGFGivaro& R, int index, M2::ARingGFGivaro::ElementType& result) { M2::ARingGFGivaro::ElementType gen; R.init(gen); R.getGenerator(gen); if (index >= nelements) R.power(result, gen, rawRandomInt(static_cast<int32_t>(R.cardinality()))); else R.power(result, gen, randomVals[index]); R.clear(gen); }
TEST(RingCCC, power) { CCC *R = CCC::create(100); mpz_t gmp1; mpz_init(gmp1); RingElementGenerator<CCC> gen(*R); for (int i = 0; i < ntrials; i++) { ring_elem a = gen.nextElement(); // TODO: what should the answer here be? // EXPECT_TRUE(R->is_equal(R->power(a, 0), R->one())); // 0^0 == 1 too? EXPECT_TRUE(R->is_equal(R->power(a, 1), a)); int e1 = rawRandomInt(10) + 1; int e2 = rawRandomInt(10) + 1; // std::cout << "(" << e1 << "," << e2 << ")" << std::endl; ring_elem b = R->power(a, e1); ring_elem c = R->power(a, e2); ring_elem d = R->power(a, e1 + e2); #if 0 ring_elem e = R->mult(b,c); mpfr_printf("b=(%.30Rf,%.30Rf)\n",BIGCC_RE(b), BIGCC_IM(b)); mpfr_printf("c=(%.30Rf,%.30Rf)\n",BIGCC_RE(c), BIGCC_IM(c)); mpfr_printf("d=(%.30Rf,%.30Rf)\n",BIGCC_RE(d), BIGCC_IM(d)); mpfr_printf("e=(%.30Rf,%.30Rf)\n",BIGCC_RE(e), BIGCC_IM(e)); #endif EXPECT_TRUE(almostEqual(R, 80, R->mult(b, c), d)); // Make sure that powers via mpz work (at least for small exponents) mpz_set_si(gmp1, e1); ring_elem b1 = R->power(a, gmp1); EXPECT_TRUE(R->is_equal(b1, b)); } mpz_clear(gmp1); }
void random(ElementType &result) const { result = rawRandomInt((int32_t)p); }
ring_elem GF::random() const { int exp = rawRandomInt((int32_t)Q_); return ring_elem(exp); }
ring_elem Z_mod::random() const { int exp = rawRandomInt((int32_t)P); return ring_elem(exp); }
void ZZp_RANDOM(long charac, long &result) { result = rawRandomInt(charac); }
void random(ElementType &result) const { result = rawRandomInt((int32_t)characteristic()); }