/* ** Wigner d-matrix <jm|exp(-iJ_y*a)|jn> ** j2 = j*2, m2 = m*2, n2 = n*2 */ double WignerDMatrix(double a, int j2, int m2, int n2) { double b, c, ca, sa, x; int k, kmin, kmax; a *= 0.5; kmin = Max(0, (m2+n2)/2); kmax = Min((j2+m2)/2, (j2+n2)/2); ca = cos(a); sa = sin(a); x = 0.0; for (k = kmin; k <= kmax; k++) { b = pow(ca, (2*k-(m2+n2)/2)); b *= pow(sa, (j2+(m2+n2)/2-2*k)); c = LnFactorial(k); c += LnFactorial((j2+m2)/2-k); c += LnFactorial((j2+n2)/2-k); c += LnFactorial(k-(m2+n2)/2); b /= exp(c); if (IsOdd(k)) b = -b; x += b; } c = LnFactorial((j2+m2)/2); c += LnFactorial((j2-m2)/2); c += LnFactorial((j2+n2)/2); c += LnFactorial((j2-n2)/2); c = exp(0.5*c); if (IsOdd((j2+m2)/2)) c = -c; x *= c; return x; }
static void normalize1(RR& z, const ZZ& y_x, long y_e, long prec, long residual) { long len = NumBits(y_x); if (len > prec) { long correction = ZZ_RoundCorrection(y_x, len - prec, residual); RightShift(z.x, y_x, len - prec); if (correction) add(z.x, z.x, correction); z.e = y_e + len - prec; } else if (len == 0) { clear(z.x); z.e = 0; } else { z.x = y_x; z.e = y_e; } if (!IsOdd(z.x)) z.e += MakeOdd(z.x); if (z.e >= NTL_OVFBND) ResourceError("RR: overflow"); if (z.e <= -NTL_OVFBND) ResourceError("RR: underflow"); }
//--------------------------------------------------------- int main() { std::vector<int> v, w; std::vector<Base*> x, y, z; std::vector<Base*>* u=new std::vector<Base*>(); for(int i=0; i<10; ++i) v.push_back(i); for(int i=0; i<10; ++i) x.push_back(new Base(i)); /////////////////////////////////////////////////// copy_if(v.begin(), v.end(), back_inserter(w), IsOdd()); copy_if(x.begin(), x.end(), back_inserter(y), BaseIsOdd()); copy_if(x.begin(), x.end(), back_inserter(*u), BaseIsOdd()); copy_if(x.begin(), x.end(), back_inserter(z), std::not1(BaseIsOdd())); /////////////////////////////////////////////////// for(std::vector<int>::iterator it=w.begin(); it!=w.end(); ++it) std::cout<<*it<<" "; std::cout<<std::endl; for(std::vector<Base*>::iterator it=y.begin(); it!=y.end(); ++it) std::cout<<(*it)->_n<<" "; std::cout<<std::endl; for(std::vector<Base*>::iterator it=z.begin(); it!=z.end(); ++it) std::cout<<(*it)->_n<<" "; std::cout<<std::endl; for(std::vector<Base*>::iterator it=u->begin(); it!=u->end(); ++it) std::cout<<(*it)->_n<<" "; std::cout<<std::endl; /////////////////////////////////////////////////// //std::vector<int>::back_insert_iterator it=copy_if(v.begin(), v.end(), back_inserter(w), IsOdd()); /////////////////////////////////////////////////// return 0; }
/* ** FUNCTION: ReducedCL. ** PURPOSE: calculate the reduced matrix element ** of the normalized spherical harmonics <ja||C^L||jb> ** INPUT: {int ja}, ** angular momentum. ** {int k }, ** rank of the spherical harmonics. ** {int jb}, ** angular momentum. ** RETURN: {double}, ** reduced matrix element. ** SIDE EFFECT: ** NOTE: it does not check for the triangular delta ** involving the orbital angular momenta. */ double ReducedCL(int ja, int k, int jb) { double r; r = sqrt((ja+1.0)*(jb+1.0))*W3j(ja, k, jb, 1, 0, -1); if (IsOdd((ja+1)/2)) r = -r; return r; }
/* ** FUNCTION: ClebschGordan. ** PURPOSE: claculate the Clebsch Gordan coeff. ** INPUT: {int j1}, ** angular momentum. ** {int m1}, ** projection of j1. ** {int j2}, ** angular momentum. ** {int m2}, ** projection of j2. ** {int jf}, ** angular momentum, final result ** of the coupling of j1 and j2. ** {int mf}, ** projection of jf. ** RETURN: {double}, ** CG coefficients. ** SIDE EFFECT: ** NOTE: */ double ClebschGordan(int j1, int m1, int j2, int m2, int jf, int mf) { double r; r = sqrt(jf+1.0); r *= W3j(j1, j2, jf, m1, m2, -mf); if (IsOdd((j1-j2+mf)/2)) r = -r; return r; }
bool IsEven(unsigned n) { if (0 == n) return true; else return (IsOdd(n - 1)); }
CryptoPP::Integer RecoverX (const CryptoPP::Integer& y) const { auto y2 = y.Squared (); auto xx = (y2 - CryptoPP::Integer::One())*(d*y2 + CryptoPP::Integer::One()).InverseMod (q); auto x = a_exp_b_mod_c (xx, (q + CryptoPP::Integer (3)).DividedBy (8), q); if (!(x.Squared () - xx).Modulo (q).IsZero ()) x = a_times_b_mod_c (x, I, q); if (x.IsOdd ()) x = q - x; return x; }
/* ** FUNCTION: WignerEckartFactor. ** PURPOSE: calculate the geometric prefactor in ** Wigner Eckart theorem, ** (-1)^{jf-mf}sqrt(2*jf+1)W3j(jf, k, ji, -mf, q, mi) ** INPUT: {int jf}, ** angular momentum. ** {int k }, ** angular momentum. ** {int ji}, ** angular momentum. ** {int mf}, ** projection of jf. ** {int q }, ** projection of k. ** {int mi}, ** projection of mi. ** RETURN: {double}, ** prefactor. ** SIDE EFFECT: ** NOTE: */ double WignerEckartFactor(int jf, int k, int ji, int mf, int q, int mi) { double r; if (!Triangle(jf, k, ji)) return 0.0; if (mi + q - mf) return 0.0; r = sqrt(jf + 1.0); if (IsOdd((jf-mf)/2)) r = -r; r *= W3j(jf, k, ji, -mf, q, mi); return r; }
int main() { while(1) { if(IsOdd(i) == true) { printf("%d is an odd number\n", i); i++; } else { i++; } } }
/* ** FUNCTION: W9j. ** PURPOSE: calculate the 9j symbol. ** INPUT: {int j1}, ** angular momentum. ** {int j2}, ** angular momentum. ** {int j3}, ** angular momentum. ** {int i1}, ** angular momentum. ** {int i2}, ** angular momentum. ** {int i3}, ** angular momentum. ** {int k1}, ** angular momentum. ** {int k2}, ** angular momentum. ** {int k3}, ** angular momentum. ** RETURN: {double}, ** 9j symbol. ** SIDE EFFECT: ** NOTE: */ double W9j(int j1, int j2, int j3, int i1, int i2, int i3, int k1, int k2, int k3) { int j, jmin, jmax; double r; #ifdef PERFORM_STATISTICS clock_t start, stop; start = clock(); #endif if (!Triangle(j1, j2, j3) || !Triangle(i1, i2, i3) || !Triangle(k1, k2, k3) || !Triangle(j1, i1, k1) || !Triangle(j2, i2, k2) || !Triangle(j3, i3, k3)) return 0.0; jmin = Max(abs(j1-k3), abs(j2-i3)); jmin = Max(jmin, abs(k2-i1)); jmax = Min(j1+k3, j2+i3); jmax = Min(jmax, k2+i1); r = 0.0; for (j = jmin; j <= jmax; j += 2) { r = r + ((j+1.0) * W6j(j1, i1, k1, k2, k3, j) * W6j(j2, i2, k2, i1, j, i3) * W6j(j3, i3, k3, j, j1, j2)); } if (IsOdd(jmin)) r = -r; #ifdef PERFORM_STATISTICS stop = clock(); timing.w9j += stop - start; #endif return r; }
void FindRoot(ZZ_pE& root, const ZZ_pEX& ff) // finds a root of ff. // assumes that ff is monic and splits into distinct linear factors { ZZ_pEXModulus F; ZZ_pEX h, h1, f; ZZ_pEX r; f = ff; if (!IsOne(LeadCoeff(f))) LogicError("FindRoot: bad args"); if (deg(f) == 0) LogicError("FindRoot: bad args"); while (deg(f) > 1) { build(F, f); random(r, deg(F)); if (IsOdd(ZZ_pE::cardinality())) { PowerMod(h, r, RightShift(ZZ_pE::cardinality(), 1), F); sub(h, h, 1); } else { AbsTraceMap(h, r, F); } GCD(h, h, f); if (deg(h) > 0 && deg(h) < deg(f)) { if (deg(h) > deg(f)/2) div(f, f, h); else f = h; } } negate(root, ConstTerm(f)); }
static void RecFindRoots(vec_ZZ_pE& x, const ZZ_pEX& f) { if (deg(f) == 0) return; if (deg(f) == 1) { long k = x.length(); x.SetLength(k+1); negate(x[k], ConstTerm(f)); return; } ZZ_pEX h; ZZ_pEX r; { ZZ_pEXModulus F; build(F, f); do { random(r, deg(F)); if (IsOdd(ZZ_pE::cardinality())) { PowerMod(h, r, RightShift(ZZ_pE::cardinality(), 1), F); sub(h, h, 1); } else { AbsTraceMap(h, r, F); } GCD(h, h, f); } while (deg(h) <= 0 || deg(h) == deg(f)); } RecFindRoots(x, h); div(h, f, h); RecFindRoots(x, h); }
int main() { setbuf(stdout, NULL); SetSeed(ZZ(0)); for (long l = 256; l <= 16384; l *= 2) { // for (long n = 256; n <= 16384; n *= 2) { for (long idx = 0; idx < 13; idx ++) { long n = 256*(1L << idx/2); if (idx & 1) n += n/2; SetSeed((ZZ(l) << 64) + ZZ(n)); ZZ p; RandomLen(p, l); if (!IsOdd(p)) p++; ZZ_p::init(p); ZZ_pX a, c, f; random(a, n); random(f, n); SetCoeff(f, n); ZZ_pXModulus F(f); double t; SqrMod(c, a, F); long iter = 1; do { t = GetTime(); for (long i = 0; i < iter; i++) SqrMod(c, a, F); t = GetTime() - t; iter *= 2; } while (t < 3); iter /= 2; t = GetTime(); for (long i = 0; i < iter; i++) SqrMod(c, a, F); t = GetTime()-t; double NTLTime = t; FlintZZ_pX f_a(a), f_c, f_f(f), f_finv; fmpz_mod_poly_reverse(f_finv.value, f_f.value, f_f.value->length); fmpz_mod_poly_inv_series_newton(f_finv.value, f_finv.value, f_f.value->length); fmpz_mod_poly_mulmod_preinv(f_c.value, f_a.value, f_a.value, f_f.value, f_finv.value); t = GetTime(); for (long i = 0; i < iter; i++) fmpz_mod_poly_mulmod_preinv(f_c.value, f_a.value, f_a.value, f_f.value, f_finv.value); t = GetTime()-t; double FlintTime = t; printf("%8.2f", FlintTime/NTLTime); } printf("\n"); } }
int main() { #if (defined(NTL_CRT_ALTCODE) && !(defined(NTL_HAVE_LL_TYPE) && NTL_ZZ_NBITS == NTL_BITS_PER_LONG)) { printf("999999999999999 "); print_flag(); return 0; } #endif SetSeed(ZZ(0)); long n, k; n = 1024; k = 30*NTL_SP_NBITS; ZZ p; RandomLen(p, k); if (!IsOdd(p)) p++; ZZ_p::init(p); // initialization ZZ_pX f, g, h, r1, r2, r3; random(g, n); // g = random polynomial of degree < n random(h, n); // h = " " random(f, n); // f = " " SetCoeff(f, n); // Sets coefficient of X^n to 1 // For doing arithmetic mod f quickly, one must pre-compute // some information. ZZ_pXModulus F; build(F, f); PlainMul(r1, g, h); // this uses classical arithmetic PlainRem(r1, r1, f); MulMod(r2, g, h, F); // this uses the FFT MulMod(r3, g, h, f); // uses FFT, but slower // compare the results... if (r1 != r2) { printf("999999999999999 "); print_flag(); return 0; } else if (r1 != r3) { printf("999999999999999 "); print_flag(); return 0; } double t; long i; long iter; ZZ_pX a, b, c; random(a, n); random(b, n); long da = deg(a); long db = deg(b); long dc = da + db; long l = NextPowerOfTwo(dc+1); FFTRep arep, brep, crep; ToFFTRep(arep, a, l, 0, da); ToFFTRep(brep, b, l, 0, db); mul(crep, arep, brep); ZZ_pXModRep modrep; FromFFTRep(modrep, crep); FromZZ_pXModRep(c, modrep, 0, dc); iter = 1; do { t = GetTime(); for (i = 0; i < iter; i++) { FromZZ_pXModRep(c, modrep, 0, dc); } t = GetTime() - t; iter = 2*iter; } while(t < 1); iter = iter/2; iter = long((3/t)*iter) + 1; double tvec[5]; long w; for (w = 0; w < 5; w++) { t = GetTime(); for (i = 0; i < iter; i++) { FromZZ_pXModRep(c, modrep, 0, dc); } t = GetTime() - t; tvec[w] = t; } t = clean_data(tvec); t = floor((t/iter)*1e12); // The following is just to test some tuning Wizard logic -- // be sure to get rid of this!! #if (defined(NTL_CRT_ALTCODE)) // t *= 1.12; #endif if (t < 0 || t >= 1e15) printf("999999999999999 "); else printf("%015.0f ", t); printf(" [%ld] ", iter); print_flag(); return 0; }
double CIRadialQkMSub(cfac_t *cfac, int J0, int M0, int J1, int M1, int k0, int k1, double e1, double e2, double e0) { ORBITAL *orb0, *orb1; int jk0, jk1, t, kl1, j1, kappa1, k, ks[4]; int Jmax, Jmin, kp, Jpmax, Jpmin, J, Jp; int j2, kl2, kappa2, j0max, j0min, j0pmax, j0pmin; int j0, kl0, kappa0, j0p, kl0p, kappa0p, m0, q, M, kmax, m1, m2; int j2max, j2min, j2pmax, j2pmin; double y[MAXNKL], r, rp, d, ph0, ph0p, sd, se, c, d1, d2; double w6j1, w6j2, w3j1, w3j2, w3j3, w3j4, w3j5, w3j6, w3j7, w3j8; orb0 = GetOrbital(cfac, k0); jk0 = GetJFromKappa(orb0->kappa); orb1 = GetOrbital(cfac, k1); jk1 = GetJFromKappa(orb1->kappa); for (t = 0; t < pw_scratch.nkl; t++) { y[t] = 0.0; kl1 = 2*pw_scratch.kl[t]; for (j1 = kl1-1; j1 <= kl1+1; j1 += 2) { if (j1 < 0) continue; kappa1 = GetKappaFromJL(j1, kl1); ks[3] = OrbitalIndex(cfac, 0, kappa1, e1); for (k = 0; k <= pw_scratch.max_k; k += 2) { for (kp = 0; kp <= pw_scratch.max_k; kp += 2) { kmax = Min(k, kp); j2max = jk0 + k; j2min = abs(jk0 - k); j2pmax = jk1 + kp; j2pmin = abs(jk1 - kp); j2max = Min(j2max, j2pmax); j2min = Max(j2min, j2pmin); for (j2 = j2min; j2 <= j2max; j2 += 2) { for (kl2 = j2-1; kl2 <= j2+1; kl2 += 2) { if (kl2/2 > pw_scratch.max_kl_eject) continue; kappa2 = GetKappaFromJL(j2, kl2); ks[2] = OrbitalIndex(cfac, 0, kappa2, e2); j0max = j1 + k; j0min = abs(j1 - k); j0pmax = j1 + kp; j0pmin = abs(j1 - kp); for (j0 = j0min; j0 <= j0max; j0 += 2) { for (kl0 = j0 - 1; kl0 <= j0 + 1; kl0 += 2) { kappa0 = GetKappaFromJL(j0, kl0); ks[1] = OrbitalIndex(cfac, 0, kappa0, e0); ph0 = GetPhaseShift(cfac, ks[1]); ks[0] = k0; SlaterTotal(cfac, &sd, &se, NULL, ks, k, 1); r = sd + se; for (j0p = j0pmin; j0p <= j0pmax; j0p += 2) { for (kl0p = j0p - 1; kl0p <= j0p + 1; kl0p += 2) { kappa0p = GetKappaFromJL(j0p, kl0p); ks[1] = OrbitalIndex(cfac, 0, kappa0p, e0); ph0p = GetPhaseShift(cfac, ks[1]); ks[0] = k1; SlaterTotal(cfac, &sd, &se, NULL, ks, kp, 1); rp = sd + se; Jmin = abs(J0 - k); Jmax = J0 + k; j2pmin = abs(J1 - j2); j2pmax = J1 + j2; Jmin = Max(Jmin, j2pmin); Jmax = Min(Jmax, j2pmax); Jpmin = abs(J0 - kp); Jpmax = J0 + kp; Jpmin = Max(Jpmin, j2pmin); Jpmax = Min(Jpmax, j2pmax); c = 0.0; d1 = sqrt((kl0+1.0)*(j0+1.0)*(kl0p+1.0)*(j0p+1.0)); for (J = Jmin; J <= Jmax; J += 2) { w6j1 = W6j(J1, j2, J, k, J0, jk0); for (Jp = Jpmin; Jp <= Jpmax; Jp += 2) { w6j2 = W6j(J1, j2, Jp, kp, J0, jk1); d2 = (J+1.0)*(Jp+1.0)*w6j1*w6j2; for (m0 = -1; m0 <= 1; m0 += 2) { w3j1 = W3j(j0, 1, kl0, -m0, m0, 0); w3j2 = W3j(j0p, 1, kl0p, -m0, m0, 0); for (q = -kmax; q <= kmax; q += 2) { m1 = m0 + q; if (m1 > j1) continue; M = M0 - q; if (M > J || M > Jp) continue; m2 = M-M1; if (m2 > j2) continue; w3j3 = W3j(j0, k, j1, -m0, -q, m1); w3j5 = W3j(J0, k, J, -M0, q, M); w3j7 = W3j(J1, j2, J, M1, m2, -M); w3j4 = W3j(j0p, kp, j1, -m0, -q, m1); w3j6 = W3j(J0, kp, Jp, -M0, q, M); w3j8 = W3j(J1, j2, Jp, M1, m2, -M); d = w3j1*w3j2*w3j3*w3j4*w3j5*w3j6*w3j7*w3j8; d *= d1*d2; if (IsOdd(abs(jk0-jk1)/2)) d = -d; c += d; } } } } y[t] += c*r*rp*cos(ph0-ph0p); } } } } } } } } } } r = y[0]; for (t = 1; t < pw_scratch.nkl; t++) { r += y[t]; kl0 = pw_scratch.kl[t-1]; kl1 = pw_scratch.kl[t]; for (kl2 = kl0+1; kl2 < kl1; kl2++) { rp = LnInteger(kl2); UVIP3P(pw_scratch.nkl, pw_scratch.log_kl, y, 1, &rp, &d); r += d; } } r *= 16.0; return r; }
int IsEven() const { return !IsOdd(); }
int main() { SetSeed(ZZ(0)); cerr << "This is NTL version " << NTL_VERSION << "\n"; cerr << "Hardware charactersitics:\n"; cerr << "NTL_BITS_PER_LONG = " << NTL_BITS_PER_LONG << "\n"; cerr << "NTL_ZZ_NBITS = " << NTL_ZZ_NBITS << "\n"; cerr << "NTL_SP_NBITS = " << NTL_SP_NBITS << "\n"; #ifdef NTL_HAVE_LL_TYPE cerr << "NTL_HAVE_LL_TYPE\n"; #endif #ifdef NTL_LONGDOUBLE_SP_MULMOD cerr << "NTL_LONGDOUBLE_SP_MULMOD\n"; #endif #ifdef NTL_LONGLONG_SP_MULMOD cerr << "NTL_LONGLONG_SP_MULMOD\n"; #endif cerr << "\n"; cerr << "Basic Configuration Options:\n"; #ifdef NTL_LEGACY_NO_NAMESPACE cerr << "NTL_LEGACY_NO_NAMESPACE\n"; #endif #ifdef NTL_LEGACY_INPUT_ERROR cerr << "NTL_LEGACY_INPUT_ERROR\n"; #endif #ifdef NTL_THREADS cerr << "NTL_THREADS\n"; #endif #ifdef NTL_EXCEPTIONS cerr << "NTL_EXCEPTIONS\n"; #endif #ifdef NTL_THREAD_BOOST cerr << "NTL_THREAD_BOOST\n"; #endif #ifdef NTL_LEGACY_SP_MULMOD cout << "NTL_LEGACY_SP_MULMOD\n"; #endif #ifdef NTL_DISABLE_LONGDOUBLE cout << "NTL_DISABLE_LONGDOUBLE\n"; #endif #ifdef NTL_DISABLE_LONGLONG cout << "NTL_DISABLE_LONGLONG\n"; #endif #ifdef NTL_MAXIMIZE_SP_NBITS cout << "NTL_MAXIMIZE_SP_NBITS\n"; #endif #ifdef NTL_GMP_LIP cerr << "NTL_GMP_LIP\n"; #endif #ifdef NTL_GF2X_LIB cerr << "NTL_GF2X_LIB\n"; #endif #ifdef NTL_PCLMUL cerr << "NTL_PCLMUL\n"; #endif #ifdef NTL_LONG_LONG_TYPE cerr << "NTL_LONG_LONG_TYPE: "; cerr << make_string(NTL_LONG_LONG_TYPE) << "\n"; #endif #ifdef NTL_UNSIGNED_LONG_LONG_TYPE cerr << "NTL_UNSIGNED_LONG_LONG_TYPE: "; cerr << make_string(NTL_UNSIGNED_LONG_LONG_TYPE) << "\n"; #endif #ifdef NTL_X86_FIX cerr << "NTL_X86_FIX\n"; #endif #ifdef NTL_NO_X86_FIX cerr << "NTL_NO_X86_FIX\n"; #endif #ifdef NTL_NO_INIT_TRANS cerr << "NTL_NO_INIT_TRANS\n"; #endif #ifdef NTL_CLEAN_INT cerr << "NTL_CLEAN_INT\n"; #endif #ifdef NTL_CLEAN_PTR cerr << "NTL_CLEAN_PTR\n"; #endif #ifdef NTL_RANGE_CHECK cerr << "NTL_RANGE_CHECK\n"; #endif cerr << "\n"; cerr << "Resolution of double-word types:\n"; cerr << make_string(NTL_LL_TYPE) << "\n"; cerr << make_string(NTL_ULL_TYPE) << "\n"; cerr << "\n"; cerr << "Performance Options:\n"; #ifdef NTL_LONG_LONG cerr << "NTL_LONG_LONG\n"; #endif #ifdef NTL_AVOID_FLOAT cerr << "NTL_AVOID_FLOAT\n"; #endif #ifdef NTL_SPMM_ULL cerr << "NTL_SPMM_ULL\n"; #endif #ifdef NTL_SPMM_ASM cerr << "NTL_SPMM_ASM\n"; #endif #ifdef NTL_AVOID_BRANCHING cerr << "NTL_AVOID_BRANCHING\n"; #endif #ifdef NTL_FFT_BIGTAB cout << "NTL_FFT_BIGTAB\n"; #endif #ifdef NTL_FFT_LAZYMUL cout << "NTL_FFT_LAZYMUL\n"; #endif #ifdef NTL_TBL_REM cerr << "NTL_TBL_REM\n"; #endif #ifdef NTL_TBL_REM_LL cerr << "NTL_TBL_REM_LL\n"; #endif #ifdef NTL_CRT_ALTCODE cerr << "NTL_CRT_ALTCODE\n"; #endif #ifdef NTL_CRT_ALTCODE_SMALL cerr << "NTL_CRT_ALTCODE_SMALL\n"; #endif #ifdef NTL_GF2X_ALTCODE cerr << "NTL_GF2X_ALTCODE\n"; #endif #ifdef NTL_GF2X_ALTCODE1 cerr << "NTL_GF2X_ALTCODE1\n"; #endif #ifdef NTL_GF2X_NOINLINE cerr << "NTL_GF2X_NOINLINE\n"; #endif cerr << "\n\n"; cerr << "running tests"; long n, k, i; n = 250; k = 16000; ZZ p; for (i = 0; i < 15; i++) { // cerr << n << "/" << k; cerr << "."; RandomLen(p, k); ZZ_p::init(p); ZZ_pX a, b, c, c1; random(a, n); random(b, n); FFTMul(c, a, b); //cerr << ZZ_pInfo->FFTInfo->NumPrimes; c1 = conv<ZZ_pX>( KarMul( conv<ZZX>(a), conv<ZZX>(b) ) ); if (c1 != c) { cerr << "ZZ_pX mul failed!\n"; return 1; } n = long(n * 1.35); k = long(k / 1.414); } // small prime tests...I've made some changes in v5.3 // that should be checked on various platforms, so // we might as well check them here. if (SmallModulusTest(17, 1000)) { cerr << "first SmallModulusTest failed!!\n"; return 1; } if (SmallModulusTest((1L << (NTL_SP_NBITS))-1, 1000)) { cerr << "second SmallModulusTest failed!!\n"; return 1; } // Test gf2x code.... if (GF2X_test()) { cerr << "GF2X test failed!\n"; return 1; } cerr << "OK\n"; ZZ x1, x2, x3, x4; double t; RandomLen(x1, 1024); RandomBnd(x2, x1); RandomBnd(x3, x1); mul(x4, x2, x3); t = GetTime(); for (i = 0; i < 100000; i++) mul(x4, x2, x3); t = GetTime()-t; cerr << "time for 1024-bit mul: " << t*10 << "us"; cerr << "\n"; rem(x2, x4, x1); t = GetTime(); for (i = 0; i < 100000; i++) rem(x2, x4, x1); t = GetTime()-t; cerr << "time for 2048/1024-bit rem: " << t*10 << "us"; cerr << "\n"; GenPrime(p, 1024); RandomBnd(x1, p); if (IsZero(x1)) set(x1); InvMod(x2, x1, p); t = GetTime(); for (i = 0; i < 1000; i++) InvMod(x2, x1, p); t = GetTime()-t; cerr << "time for 1024-bit modular inverse: " << t*1000 << "us"; cerr << "\n"; // test modulus switching n = 1024; k = 1024; RandomLen(p, k); ZZ_p::init(p); if (!IsOdd(p)) p++; ZZ_pX j1, j2, j3; random(j1, n); random(j2, n); mul(j3, j1, j2); t = GetTime(); for (i = 0; i < 200; i++) mul(j3, j1, j2); t = GetTime()-t; cerr << "time to multiply degree 1023 polynomials\n modulo a 1024-bit number: "; cerr << (t/200) << "s"; cerr << "\n"; GF2X_time(); return 0; }
/* ** FUNCTION: W6j. ** PURPOSE: calculate the 6j symbol. ** INPUT: {int j1}, ** angular momentum. ** {int j2}, ** angular momentum. ** {int j3}, ** angular momentum. ** {int i1}, ** angular momentum. ** {int i2}, ** angular momentum. ** {int i3}, ** angular momentum. ** RETURN: {double}, ** 6j symbol. ** SIDE EFFECT: ** NOTE: */ double W6j(int j1, int j2, int j3, int i1, int i2, int i3) { int n1, n2, n3, n4, n5, n6, n7, k, kmin, kmax, ic, ki; double r, a; #ifdef PERFORM_STATISTICS clock_t start, stop; start = clock(); #endif if (!(Triangle(j1, j2, j3) && Triangle(j1, i2, i3) && Triangle(i1, j2, i3) && Triangle(i1, i2, j3))) return 0.0; n1 = (j1 + j2 + j3) / 2; n2 = (i2 + i1 + j3) / 2; n3 = (j1 + i2 + i3) / 2; n4 = (j2 + i1 + i3) / 2; n5 = (j1 + j2 + i2 + i1) / 2; n6 = (j1 + i1 + j3 + i3) / 2; n7 = (j2 + i2 + j3 + i3) / 2; kmin = Max(n1, n2); kmin = Max(kmin, n3); kmin = Max(kmin, n4) + 1; kmax = Min(n5, n6); kmax = Min(kmax, n7) + 1; r = 1.0; ic = 0; for (k = kmin + 1; k <= kmax; k++) { ki = kmax -ic; r = 1.0 - (r * ki * (n5-ki+2.0) * (n6-ki+2.0) * (n7-ki+2.0))/ ((ki-1.0-n1) * (ki-1.0-n2) * (ki-1.0-n3) * (ki - 1.0 - n4)); ic++; } a = (LnFactorial(kmin) - LnFactorial(kmin-n1-1) - LnFactorial(kmin-n2-1) - LnFactorial(kmin-n3-1) - LnFactorial(kmin-n4-1) - LnFactorial(n5+1-kmin) - LnFactorial(n6+1-kmin) - LnFactorial(n7+1-kmin)) + ((LnFactorial(n1-j1) + LnFactorial(n1-j2) + LnFactorial(n1-j3) - LnFactorial(n1+1) + LnFactorial(n2-i2) + LnFactorial(n2-i1) + LnFactorial(n2-j3) - LnFactorial(n2+1) + LnFactorial(n3-j1) + LnFactorial(n3-i2) + LnFactorial(n3-i3) - LnFactorial(n3+1) + LnFactorial(n4-j2) + LnFactorial(n4-i1) + LnFactorial(n4-i3) - LnFactorial(n4+1))/2.0); r = r * exp(a); if (IsEven(n5+kmin)) r = -r; if (IsOdd(((j1+j2+i1+i2)/2))) r = -r; #ifdef PERFORM_STATISTICS stop = clock(); timing.w6j += stop - start; #endif return r; }
/* ** FUNCTION: W3j. ** PURPOSE: calculate the Wigner 3j symbol. ** INPUT: {int j1}, ** angular momentum. ** {int j2}, ** angular momentum. ** {int j3}, ** angular momentum. ** {int m1}, ** projection of j1. ** {int m2}, ** projection of j2. ** {int m3}, ** projection of j3. ** RETURN: {double}, ** 3j coefficients. ** SIDE EFFECT: ** NOTE: the _sumk array is used to store all ** summation terms to avoid overflow. ** the predefined MAXTERM=512 allows the ** maximum angular momentum of about 500. ** if this limit is exceeded, the routine ** issues a warning. */ double W3j(int j1, int j2, int j3, int m1, int m2, int m3) { int i, k, kmin, kmax, ik[14]; double delta, qsum, a, b; #ifdef PERFORM_STATISTICS clock_t start, stop; start = clock(); #endif if (m1 + m2 + m3) return 0.0; if (!Triangle(j1, j2, j3)) return 0.0; if (abs(m1) > j1) return 0.0; if (abs(m2) > j2) return 0.0; if (abs(m3) > j3) return 0.0; ik[0] = j1 + j2 - j3; ik[1] = j1 - j2 + j3; ik[2] = -j1 + j2 + j3; ik[3] = j1 + j2 + j3 + 2; ik[4] = j1 - m1; ik[5] = j1 + m1; ik[6] = j2 - m2; ik[7] = j2 + m2; ik[8] = j3 - m3; ik[9] = j3 + m3; ik[10] = j2 - j3 - m1; ik[11] = j1 - j3 + m2; ik[12] = j3 - j2 + m1; ik[13] = j1 - j2 - m3; for (i = 0; i < 14; i++) { ik[i] = ik[i] / 2; } delta = - LnFactorial(ik[3]); for (i = 0; i < 3; i++) delta += LnFactorial(ik[i]); for (i = 4; i < 10; i++) delta += LnFactorial(ik[i]); kmin = Max(0, ik[10]); kmin = Max(kmin, ik[11]); kmax = Min(ik[0], ik[4]); kmax = Min(kmax, ik[7]); qsum = 0.0; a = 1E30; for (k = kmin, i = 0; k <= kmax && i < MAXTERM; k++, i++) { _sumk[i] = LnFactorial(k) + LnFactorial(ik[0]-k) + LnFactorial(ik[4]-k) + LnFactorial(ik[7]-k) + LnFactorial(ik[12]+k) + LnFactorial(k-ik[11]); if (_sumk[i] < a) a = _sumk[i]; } if (i == MAXTERM) { printf("Maximum terms in the 3j symbol sum reached\n"); printf("Results may be inaccurate\n"); } for (k = kmin, i = 0; k <= kmax; k++, i++) { b = exp(-(_sumk[i]-a)); if (IsOdd(k)) b = -b; qsum += b; } if (IsOdd(ik[13])) qsum = -qsum; b = exp(0.5*delta-a); b *= qsum; #ifdef PERFORM_STATISTICS stop = clock(); timing.w3j += stop -start; #endif return b; }
ST_ErrorCode_t BLAST_RC6ADecode(U32 *UserBuf_p, STBLAST_Symbol_t *SymbolBuf_p, U32 SymbolsAvailable, U32 *NumDecoded_p, U32 *SymbolsUsed_p, const STBLAST_ProtocolParams_t *ProtocolParams_p) { U8 i; U8 j; U32 SymbolsLeft; U8 DurationArray[200]; U8 LevelArraySize; U8 LevelArray[400]; /* check these values */ U32 DecodedValue = 0; U8 CustCodeDurationOneIndex; /* generate correct interpretation from inverted IR receive input */ #if defined (IR_INVERT) { STBLAST_Symbol_t *LastSymbol_p; if (InvertedInputCompensate(SymbolBuf_p, (MID_6T), SymbolsAvailable) == FALSE) { *NumDecoded_p = 0; return ST_NO_ERROR; } SymbolsAvailable = SymbolsAvailable-1; LastSymbol_p = SymbolBuf_p + (SymbolsAvailable-1); LastSymbol_p->SymbolPeriod = 0xFFFF; } #endif /* Set number of symbols allowed */ SymbolsLeft = SymbolsAvailable; *SymbolsUsed_p = 0; *NumDecoded_p = 0; /* parse symbol buffer with a ticker. Each segment is of duration 2T */ /* Generate array of durations as multiples of T */ DurationTransform(SymbolBuf_p, SymbolsAvailable, DurationArray); /*** HEADER VALIDATION ***/ CustCodeDurationOneIndex = ValidateHeader(LevelArray, DurationArray, 2*SymbolsAvailable); if(CustCodeDurationOneIndex == 0) { *NumDecoded_p = 0; return ST_NO_ERROR; } /* Convert remaining durations into high/low levels */ j = 1; for(i=CustCodeDurationOneIndex; i<(2*SymbolsAvailable); i++) { if(DurationArray[i] == 1) { LevelArray[j] = !LevelArray[j-1]; j = j+1; } else if(DurationArray[i] == 2) { LevelArray[j] = LevelArray[j+1] = !LevelArray[j-1]; j = j+2; } else if(DurationArray[i] == 0xFF) /* end of stream */ { if(IsOdd(j)) /* an even number of levels are required */ { LevelArray[j] = 0; j = j+1; } } else { *NumDecoded_p = 0; return ST_NO_ERROR; } } LevelArraySize = j; /* check remaining number of levels is 2*(custcode size + payloadsize) */ if (LevelArraySize != 2*(8 + ProtocolParams_p->RC6A.NumberPayloadBits)) { *NumDecoded_p = 0; return ST_NO_ERROR; } /*** CUSTOM CODE VALIDATION ***/ { U8 CustCode=0; /* Parse this array of levels and output data */ /* Take each pair of levels and determine bit from transition */ for (j=0; j<16; j=j+2) { CustCode <<= 1; CustCode |= LevelArray[j]; /* only need to check the level of the first half-bit */ } if (CustCode != ProtocolParams_p->RC6A.CustomerCode ) { *NumDecoded_p = 0; return ST_NO_ERROR; } } /*** PAYLOAD ENUMERATION ***/ /* Parse this array of levels and output data */ /* Take each pair of levels and determine bit from transition */ for (j=16; j<LevelArraySize; j=j+2) { DecodedValue <<= 1; DecodedValue |= LevelArray[j]; /* is this robust enough? */ } *NumDecoded_p = 1; *UserBuf_p = DecodedValue; return ST_NO_ERROR; }