void fincr(_MIPD_ flash x,int n,int d,flash y) { /* increment x by small fraction n/d - y=x+(n/d) */ #ifdef MR_OS_THREADS miracl *mr_mip=get_mip(); #endif if (mr_mip->ERNUM) return; MR_IN(43) if (d<0) { d=(-d); n=(-n); } numer(_MIPP_ x,mr_mip->w1); denom(_MIPP_ x,mr_mip->w2); mr_mip->check=OFF; premult(_MIPP_ mr_mip->w1,d,mr_mip->w5); premult(_MIPP_ mr_mip->w2,d,mr_mip->w6); premult(_MIPP_ mr_mip->w2,n,mr_mip->w0); add(_MIPP_ mr_mip->w5,mr_mip->w0,mr_mip->w5); mr_mip->check=ON; if (d==1 && fit(mr_mip->w5,mr_mip->w6,mr_mip->nib)) fpack(_MIPP_ mr_mip->w5,mr_mip->w6,y); else mround(_MIPP_ mr_mip->w5,mr_mip->w6,y); MR_OUT }
// ---------------------------------------------------------------- intmod_t intmod_from_rat(intrat_t r, int p) { intmod_t numer(r.get_numerator(), p); intmod_t denom(r.get_denominator(), p); return numer / denom; }
void divmod(const bigint &num, const bigint &denom, bigint &div, bigint &mod) { bigint numer(num); if (denom == 0) raise(SIGTRAP); for (div = mod = 0; numer >= denom; div++) numer -= denom; mod = numer; }
/* do measurements: load density, ploop, etc. and phases onto lattice */ void measure() { register int i,j,k, c, is_even; register site *s; int dx,dy,dz; /* separation for correlated observables */ int dir; /* direction of separation */ msg_tag *tag; register complex cc,dd; /*scratch*/ complex ztr, zcof, znum, zdet, TC, zd, density, zphase; complex p[4]; /* probabilities of n quarks at a site */ complex np[4]; /* probabilities at neighbor site */ complex pp[4][4]; /* joint probabilities of n here and m there */ complex zplp, plp_even, plp_odd; Real locphase, phase; /* First make T (= timelike P-loop) from s->ploop_t T stored in s->tempmat1 */ ploop_less_slice(nt-1,EVEN); ploop_less_slice(nt-1,ODD); phase = 0.; density = plp_even = plp_odd = cmplx(0.0, 0.0); for(j=0;j<4;j++){ p[j]=cmplx(0.0,0.0); for(k=0;k<4;k++)pp[j][k]=cmplx(0.0,0.0); } FORALLSITES(i,s) { if(s->t != nt-1) continue; if( ((s->x+s->y+s->z)&0x1)==0 ) is_even=1; else is_even=0; mult_su3_nn(&(s->link[TUP]), &(s->ploop_t), &(s->tempmat1)); zplp = trace_su3(&(s->tempmat1)); if( is_even){CSUM(plp_even, zplp)} else {CSUM(plp_odd, zplp)} ztr = trace_su3(&(s->tempmat1)); CONJG(ztr, zcof); if(is_even){ for(c=0; c<3; ++c) s->tempmat1.e[c][c].real += C; zdet = det_su3(&(s->tempmat1)); znum = numer(C, ztr, zcof); CDIV(znum, zdet, zd); CSUM(density, zd); /* store n_quark probabilities at this site in lattice variable qprob[], accumulate sum over lattice in p[] */ cc= cmplx(C*C*C,0.0); CDIV(cc,zdet,s->qprob[0]); CSUM(p[0],s->qprob[0]); CMULREAL(ztr,C*C,cc); CDIV(cc,zdet,s->qprob[1]); CSUM(p[1],s->qprob[1]); CMULREAL(zcof,C,cc); CDIV(cc,zdet,s->qprob[2]); CSUM(p[2],s->qprob[2]); cc = cmplx(1.0,0.0); CDIV(cc,zdet,s->qprob[3]); CSUM(p[3],s->qprob[3]); locphase = carg(&zdet); phase += locphase; } }
mpf_class MpfFromRational(Signal &sig, const Rational &ratio) { if (ratio.denom == 0) { Operator::SetError_DivideByZero(sig); return mpf_class(0); } mpq_class numer(ratio.numer); mpq_class denom(ratio.denom); return numer / denom; }
int fcomp(_MIPD_ flash x,flash y) { /* compares two Flash numbers * * returns -1 if y>x; +1 if x>y; 0 if x=y */ #ifdef MR_OS_THREADS miracl *mr_mip=get_mip(); #endif if (mr_mip->ERNUM) return 0; MR_IN(39) numer(_MIPP_ x,mr_mip->w1); denom(_MIPP_ y,mr_mip->w2); mr_mip->check=OFF; multiply(_MIPP_ mr_mip->w1,mr_mip->w2,mr_mip->w5); numer(_MIPP_ y,mr_mip->w1); denom(_MIPP_ x,mr_mip->w2); multiply(_MIPP_ mr_mip->w1,mr_mip->w2,mr_mip->w0); mr_mip->check=ON; MR_OUT return (mr_compare(mr_mip->w5,mr_mip->w0)); }
void frecip(_MIPD_ flash x,flash y) { /* set y=1/x */ #ifdef MR_OS_THREADS miracl *mr_mip=get_mip(); #endif if (mr_mip->ERNUM) return; MR_IN(41) numer(_MIPP_ x,mr_mip->w1); denom(_MIPP_ x,mr_mip->w2); fpack(_MIPP_ mr_mip->w2,mr_mip->w1,y); MR_OUT }
void ftrunc(_MIPD_ flash x,big y,flash z) { /* sets y=int(x), z=rem(x) - returns * * y only for ftrunc(x,y,y) */ #ifdef MR_OS_THREADS miracl *mr_mip=get_mip(); #endif if (mr_mip->ERNUM) return; MR_IN(45) numer(_MIPP_ x,mr_mip->w1); denom(_MIPP_ x,mr_mip->w2); divide(_MIPP_ mr_mip->w1,mr_mip->w2,mr_mip->w3); copy(mr_mip->w3,y); if (y!=z) fpack(_MIPP_ mr_mip->w1,mr_mip->w2,z); MR_OUT }
int main() { MultiDigit numer(2), numer2, denom[2], item[2], pi, m, m2; item[0].from_number(2); denom[0].from_number(1); int which=0; int a; //for(a=1;a<=33250;a++) for(a=1;a<=3350;a++) { if (a % 100 == 0) { printf("%i\n", a); } m.from_number(2*a+1); multiply_multi_digit(denom[which], m, &denom[1-which]); m2.from_number(a); multiply_multi_digit(item[which], m2, &item[1-which]); multiply_multi_digit(numer, m, &numer2); add_multi_digit(item[1-which], numer2, &numer); which = 1-which; } int offset = 250; numer.length += offset; numer.digits = (multi_digit_t*)realloc(numer.digits, sizeof(multi_digit_t)*numer.length); memmove(numer.digits+offset,numer.digits,sizeof(multi_digit_t)*(numer.length-offset)); for(a=0;a<offset;a++) { numer.digits[a] = 0; } divide_multi_digit(numer, denom[which], &pi); FILE * f = fopen("dump.txt", "w"); printf("pi is "); pi.print(); fprintf(f,"pi is "); pi.print(f); fclose(f); return 0; }
void fpmul(_MIPD_ flash x,int n,int d,flash y) { /* multiply x by small fraction n/d - y=x*n/d */ int r,g; #ifdef MR_OS_THREADS miracl *mr_mip=get_mip(); #endif if (mr_mip->ERNUM) return; if (n==0 || size(x)==0) { zero(y); return; } if (n==d) { copy(x,y); return; } MR_IN(42) if (d<0) { d=(-d); n=(-n); } numer(_MIPP_ x,mr_mip->w1); denom(_MIPP_ x,mr_mip->w2); r=subdiv(_MIPP_ mr_mip->w1,d,mr_mip->w3); g=igcd(d,r); r=subdiv(_MIPP_ mr_mip->w2,n,mr_mip->w3); g*=igcd(n,r); mr_mip->check=OFF; premult(_MIPP_ mr_mip->w1,n,mr_mip->w5); premult(_MIPP_ mr_mip->w2,d,mr_mip->w6); subdiv(_MIPP_ mr_mip->w5,g,mr_mip->w5); subdiv(_MIPP_ mr_mip->w6,g,mr_mip->w6); mr_mip->check=ON; if (fit(mr_mip->w5,mr_mip->w6,mr_mip->nib)) fpack(_MIPP_ mr_mip->w5,mr_mip->w6,y); else mround(_MIPP_ mr_mip->w5,mr_mip->w6,y); MR_OUT }
double fdsize(_MIPD_ flash w) { /* express flash number as double. */ int i,s,en,ed; double n,d,b,BIGGEST; #ifdef MR_OS_THREADS miracl *mr_mip=get_mip(); #endif if (mr_mip->ERNUM || size(w)==0) return (0.0); MR_IN(11) BIGGEST=pow(2.0,(double)(1<<(MR_EBITS-4))); mr_mip->EXACT=FALSE; n=0.0; d=0.0; if (mr_mip->base==0) b=pow(2.0,(double)MIRACL); else b=(double)mr_mip->base; numer(_MIPP_ w,mr_mip->w1); s=exsign(mr_mip->w1); insign(PLUS,mr_mip->w1); en=(int)mr_mip->w1->len; for (i=0;i<en;i++) n=(double)mr_mip->w1->w[i]+(n/b); denom(_MIPP_ w,mr_mip->w1); ed=(int)mr_mip->w1->len; for (i=0;i<ed;i++) d=(double)mr_mip->w1->w[i]+(d/b); n/=d; while (en!=ed) { if (en>ed) { ed++; if (BIGGEST/b<n) { mr_berror(_MIPP_ MR_ERR_DOUBLE_FAIL); MR_OUT return (0.0); } n*=b; }
#include "boost/math/common_factor_rt.hpp" //----------------------------------------------------------------------------- #define cons std::make_pair const auto car = [](auto x) { return x.first; }; const auto cdr = [](auto x) { return x.second; }; //----------------------------------------------------------------------------- // (define(add - rat x y) // (make-rat (+ (*(numer x) (denom y)) // (*(numer y) (denom x))) // (* (denom x) (denom y)))) const auto add_rat = [](auto x, auto y) { return make_rat( (numer(x) * denom(y)) + (numer(y) * denom(x)), denom(x) * denom(y)); }; //----------------------------------------------------------------------------- // (define(sub - rat x y) // (make-rat (- (*(numer x) (denom y)) // (*(numer y) (denom x))) // (* (denom x) (denom y)))) const auto sub_rat = [](auto x, auto y) { return make_rat( (numer(x) * denom(y)) - (numer(y) * denom(x)), denom(x) * denom(y)); }; //-----------------------------------------------------------------------------
/* do measurements: load density, ploop, etc. and phases onto lattice */ void measure() { register int i,j,k, c; register site *s; int dx,dy,dz; /* separation for correlated observables */ int dir; /* direction of separation */ msg_tag *tag; register complex cc,dd; /*scratch*/ complex ztr, zcof, znum, zdet, TC, zd, density, zphase; complex p[4]; /* probabilities of n quarks at a site */ complex np[4]; /* probabilities at neighbor site */ complex pp[4][4]; /* joint probabilities of n here and m there */ complex zplp, plp; Real locphase, phase; /* First make T (= timelike P-loop) from s->ploop_t T stored in s->tempmat1 */ ploop_less_slice(nt-1,EVEN); ploop_less_slice(nt-1,ODD); phase = 0.; density = plp = cmplx(0.0, 0.0); for(j=0;j<4;j++){ p[j]=cmplx(0.0,0.0); for(k=0;k<4;k++)pp[j][k]=cmplx(0.0,0.0); } FORALLSITES(i,s) { if(s->t != nt-1) continue; mult_su3_nn(&(s->link[TUP]), &(s->ploop_t), &(s->tempmat1)); zplp = trace_su3(&(s->tempmat1)); CSUM(plp, zplp); ztr = trace_su3(&(s->tempmat1)); CONJG(ztr, zcof); for(c=0; c<3; ++c) s->tempmat1.e[c][c].real += C; zdet = det_su3(&(s->tempmat1)); znum = numer(C, ztr, zcof); CDIV(znum, zdet, zd); CSUM(density, zd); /* store n_quark probabilities at this site in lattice variable qprob[], accumulate sum over lattice in p[] */ cc = cmplx(C*C*C,0.0); CDIV(cc,zdet,s->qprob[0]); CSUM(p[0],s->qprob[0]); CMULREAL(ztr,C*C,cc); CDIV(cc,zdet,s->qprob[1]); CSUM(p[1],s->qprob[1]); CMULREAL(zcof,C,cc); CDIV(cc,zdet,s->qprob[2]); CSUM(p[2],s->qprob[2]); cc = cmplx(1.0,0.0); CDIV(cc,zdet,s->qprob[3]); CSUM(p[3],s->qprob[3]); locphase = carg(&zdet); phase += locphase; } g_floatsum( &phase ); g_complexsum( &density ); g_complexsum( &p[0] ); g_complexsum( &p[1] ); g_complexsum( &p[2] ); g_complexsum( &p[3] ); g_complexsum( &plp ); CDIVREAL(density,(Real)(nx*ny*nz),density); CDIVREAL(p[0],(Real)(nx*ny*nz),p[0]); CDIVREAL(p[1],(Real)(nx*ny*nz),p[1]); CDIVREAL(p[2],(Real)(nx*ny*nz),p[2]); CDIVREAL(p[3],(Real)(nx*ny*nz),p[3]); CDIVREAL(plp,(Real)(nx*ny*nz),plp); zphase = ce_itheta(phase); if(this_node == 0) { printf("ZMES\t%e\t%e\t%e\t%e\t%e\t%e\n", zphase.real, zphase.imag, density.real, density.imag, plp.real, plp.imag); printf("PMES\t%e\t%e\t%e\t%e\t%e\t%e\t%e\t%e\n", p[0].real, p[0].imag, p[1].real, p[1].imag, p[2].real, p[2].imag, p[3].real, p[3].imag ); } #ifdef PPCORR dx=1; dy=0; dz=0; /* Temporary - right now we just do nearest neighbor */ for(dir=XUP;dir<=ZUP;dir++){ tag = start_gather_site( F_OFFSET(qprob[0]), 4*sizeof(complex), dir, EVENANDODD, gen_pt[0] ); wait_gather(tag); FORALLSITES(i,s)if(s->t==nt-1){ for(j=0;j<4;j++)for(k=0;k<4;k++){ CMUL( (s->qprob)[j],((complex *)gen_pt[0][i])[k],cc); CSUM(pp[j][k],cc); } } cleanup_gather(tag); } /* density correlation format: PP dx dy dz n1 n2 real imag */ for(j=0;j<4;j++)for(k=0;k<4;k++){ g_complexsum( &pp[j][k] ); CDIVREAL(pp[j][k],(Real)(3*nx*ny*nz),pp[j][k]); if(this_node==0) printf("PP %d %d %d %d %d %e %e\n",dx,dy,dz,j,k, pp[j][k].real,pp[j][k].imag); } #endif /*PPCORR*/ }
void l0Smooth(InputArray src, OutputArray dst, double lambda, double kappa) { Mat S = src.getMat(); CV_Assert(!S.empty()); CV_Assert(S.depth() == CV_8U || S.depth() == CV_16U || S.depth() == CV_32F || S.depth() == CV_64F); dst.create(src.size(), src.type()); if(S.data == dst.getMat().data){ S = S.clone(); } if(S.depth() == CV_8U) { S.convertTo(S, CV_32F, 1/255.0f); } else if(S.depth() == CV_16U) { S.convertTo(S, CV_32F, 1/65535.0f); }else if(S.depth() == CV_64F){ S.convertTo(S, CV_32F); } const double betaMax = 100000; // gradient operators in frequency domain Mat otfFx, otfFy; float kernel[2] = {-1, 1}; float kernel_inv[2] = {1,-1}; psf2otf(Mat(1,2,CV_32FC1, kernel_inv), otfFx, S.rows, S.cols); psf2otf(Mat(2,1,CV_32FC1, kernel_inv), otfFy, S.rows, S.cols); vector<Mat> denomConst; Mat tmp = pow2absComplex(otfFx) + pow2absComplex(otfFy); for(int i = 0; i < S.channels(); i++){ denomConst.push_back(tmp); } // input image in frequency domain vector<Mat> numerConst; dftMultiChannel(S, numerConst); /********************************* * solver *********************************/ double beta = 2 * lambda; while(beta < betaMax){ // h, v subproblem Mat h, v; filter2D(S, h, -1, Mat(1, 2, CV_32FC1, kernel), Point(0, 0), 0, BORDER_REPLICATE); filter2D(S, v, -1, Mat(2, 1, CV_32FC1, kernel), Point(0, 0), 0, BORDER_REPLICATE); Mat hvMag = h.mul(h) + v.mul(v); Mat mask; if(S.channels() == 1) { threshold(hvMag, mask, lambda/beta, 1, THRESH_BINARY); } else if(S.channels() > 1) { Mat *channels = new Mat[S.channels()]; split(hvMag, channels); hvMag = channels[0]; for(int i = 1; i < S.channels(); i++){ hvMag = hvMag + channels[i]; } threshold(hvMag, mask, lambda/beta, 1, THRESH_BINARY); Mat in[] = {mask, mask, mask}; merge(in, 3, mask); delete[] channels; } h = h.mul(mask); v = v.mul(mask); // S subproblem vector<Mat> denom(S.channels()); for(int i = 0; i < S.channels(); i++){ denom[i] = beta * denomConst[i] + 1; } Mat hGrad, vGrad; filter2D(h, hGrad, -1, Mat(1, 2, CV_32FC1, kernel_inv)); filter2D(v, vGrad, -1, Mat(2, 1, CV_32FC1, kernel_inv)); vector<Mat> hvGradFreq; dftMultiChannel(hGrad+vGrad, hvGradFreq); vector<Mat> numer(S.channels()); for(int i = 0; i < S.channels(); i++){ numer[i] = numerConst[i] + hvGradFreq[i] * beta; } vector<Mat> sFreq(S.channels()); divComplexByRealMultiChannel(numer, denom, sFreq); idftMultiChannel(sFreq, S); beta = beta * kappa; } Mat D = dst.getMat(); if(D.depth() == CV_8U) { S.convertTo(D, CV_8U, 255); } else if(D.depth() == CV_16U) { S.convertTo(D, CV_16U, 65535); }else if(D.depth() == CV_64F){ S.convertTo(D, CV_64F); }else{ S.copyTo(D); } }
// ---------------------------------------------------------------- bit_t bit_from_rat(intrat_t r) { bit_t numer(r.get_numerator()); bit_t denom(r.get_denominator()); return numer / denom; }
void t_FRAC_to_QQ ( mpq_t value, GEN g ) { t_INT_to_ZZ(mpq_numref(value), numer(g)); t_INT_to_ZZ(mpq_denref(value), denom(g)); }
void flop(_MIPD_ flash x,flash y,int *op,flash z) { /* Do basic flash operation - depending on * * op[]. Performs operations of the form A.f1(x,y) + B.f2(x,y) ------------------- C.f3(x,y) + D.f4(x,y) * Four functions f(x,y) are supported and * * coded thus * * 00 - Nx.Ny * * 01 - Nx.Dy * * 10 - Dx.Ny * * 11 - Dx.Dy * * where Nx is numerator of x, Dx denominator * * of x, etc. * * op[0] contains the codes for f1-f4 in last * * eight bits = 00000000f1f2f3f4 * * op[1], op[2], op[3], op[4] contain the * * single precision multipliers A, B, C, D */ int i,code; #ifdef MR_OS_THREADS miracl *mr_mip=get_mip(); #endif if (mr_mip->ERNUM) return; MR_IN(69) numer(_MIPP_ x,mr_mip->w1); denom(_MIPP_ x,mr_mip->w2); numer(_MIPP_ y,mr_mip->w3); denom(_MIPP_ y,mr_mip->w4); mr_mip->check=OFF; for (i=1;i<=4;i++) { zero(mr_mip->w0); if (op[i]==0) continue; code=(op[0]>>(2*(4-i)))&3; switch (code) { case 0 : if (x==y) multiply(_MIPP_ mr_mip->w1,mr_mip->w1,mr_mip->w0); else multiply(_MIPP_ mr_mip->w1,mr_mip->w3,mr_mip->w0); break; case 1 : multiply(_MIPP_ mr_mip->w1,mr_mip->w4,mr_mip->w0); break; case 2 : multiply(_MIPP_ mr_mip->w2,mr_mip->w3,mr_mip->w0); break; case 3 : if(x==y) multiply(_MIPP_ mr_mip->w2,mr_mip->w2,mr_mip->w0); else multiply(_MIPP_ mr_mip->w2,mr_mip->w4,mr_mip->w0); break; } premult(_MIPP_ mr_mip->w0,op[i],mr_mip->w0); switch (i) { case 1 : copy(mr_mip->w0,mr_mip->w5); break; case 2 : add(_MIPP_ mr_mip->w5,mr_mip->w0,mr_mip->w5); break; case 3 : copy(mr_mip->w0,mr_mip->w6); break; case 4 : add(_MIPP_ mr_mip->w6,mr_mip->w0,mr_mip->w6); break; } } mr_mip->check=ON; mround(_MIPP_ mr_mip->w5,mr_mip->w6,z); MR_OUT }