long sobel_sharpness(cv::Mat color){ long integral=0; cv::Mat gray=rgb2gray(color); cv::Mat sobel=gray.clone(); int max_abs=0; for(int i=1;i<gray.rows-1;i++){ for(int j=1;j<gray.cols-1;j++){ int gx=(int)(gray.at<uchar>(i+1,j+1)+2*gray.at<uchar>(i,j+1)+gray.at<uchar>(i-1,j+1) - gray.at<uchar>(i-1,j-1)-2*gray.at<uchar>(i,j-1)-gray.at<uchar>(i+1,j-1)); int gy=(int)(gray.at<uchar>(i+1,j+1)+2*gray.at<uchar>(i+1,j)+gray.at<uchar>(i+1,j-1) - gray.at<uchar>(i-1,j-1)-2*gray.at<uchar>(i-1,j)-gray.at<uchar>(i-1,j+1)); integral+=(long)(absol(gx)+absol(gy))/2; //int abs=sqrt(gx*gx+gy*gy); int abs=(int)(absol(gx)+absol(gy))/2; if(abs>max_abs)max_abs=abs; sobel.at<uchar>(i,j)=abs; } } for(int i=1;i<sobel.rows-1;i++){ for(int j=1;j<sobel.cols-1;j++){ sobel.at<uchar>(i,j)=sobel.at<uchar>(i,j)*255/max_abs; } } cv::imshow("sobel",sobel); cv::waitKey(0); return integral; }
Edge *Edges::bisect(Site *s1, Site *s2) { double dx,dy,adx,ady; Edge *newedge; newedge = fedges.alloc(); newedge -> reg[0] = s1; newedge -> reg[1] = s2; sites.ref(s1); sites.ref(s2); newedge -> ep[0] = (Site *) 0; newedge -> ep[1] = (Site *) 0; dx = s2->coord.x - s1->coord.x; dy = s2->coord.y - s1->coord.y; adx = absol(dx); ady = absol(dy); newedge -> c = s1->coord.x * dx + s1->coord.y * dy + (dx*dx + dy*dy)*0.5; if (adx>ady) { newedge -> a = 1.0; newedge -> b = dy/dx; newedge -> c /= dx;} else { newedge -> b = 1.0; newedge -> a = dx/dy; newedge -> c /= dy;}; newedge -> edgenbr = nedges; nedges += 1; return(newedge); }
main() { extern long k, n; extern double t; double cumbin(), /* = function to calculate the cumulative binomial */ absol(); /* = function to calculate absolute value */ double p, /* = binomial parameter (component reliability) estimate */ pnew, /* = next estimate of binomial parameter */ v, /* = input system reliability */ s, /* = system reliability based on p, s is the estimate for v */ deriv, /* = derivative of the reliability curve at p */ eps; /* = input allowable error in the answer or the system rel. */ long count; /* = number of iterations to calculate p */ char yorn; /* = y to continue the program */ printf("This program calculates the common component reliability p\n"); printf("required to yield a given system reliability V of a k-out-of-n\n"); printf("system within a given error Epsilon.\n"); yorn = 'y'; while (yorn == 'y') { printf("Enter N = "); /* N2: Get input values to use within */ scanf("%ld", &n); /* the program. */ printf("Enter K = "); scanf("%ld", &k); printf("Enter V = "); scanf("%lf", &v); printf("Enter Epsilon = "); scanf("%lf", &eps); count = 1; pnew = (k - 1.0) / (n - 1.0); /* N3: Start with inflection point. */ do /* N4: Iterate by Newton's method using */ { /* pnew and cumbin() to get s and a */ p = pnew; /* new value of pnew. Repeat until */ s = cumbin(p); /* the error is acceptable. */ if (p >= 0.5) deriv = k * t / p; else deriv = (n - k + 1.0) * t / (1.0 - p); pnew = p - (s - v) / deriv; /* N5: The Newton step. */ count++; } while (absol(p - pnew) > eps || absol(s - v) > eps); /* N6: Print the results. */ printf("\nThe required component reliability is %16.14lf\n", pnew); printf("%ld iterations were required for the calculation.\n\n", count); printf("Would you like to run another case (y or n)?"); scanf("%s", &yorn); } }
void draw_diamond(char * map, int currpos, int radius, char put) { int i, l; if (currpos > ((radius*map_size_y)+radius)) { for (i = -radius; i <= radius; i++) { for (l = -(radius-absol(i)); l <= (radius-absol(i)); l++) { map[currpos+(i*map_size_y)+l] = put; //printf("> \t%d/%d\n", i, l); } } } return; }
static mode_t newmode(const char *ms, const mode_t pm) { register mode_t o, m, b; int lock, setsgid = 0, cleared = 0, copy = 0; mode_t nm, om, mm; nm = om = pm; m = absol(&ms); if (!*ms) { nm = m; goto out; } if ((lock = (nm&S_IFMT) != S_IFDIR && (nm&(S_ENFMT|S_IXGRP)) == S_ENFMT) == 01) nm &= ~(mode_t)S_ENFMT; do { m = who(&ms, &mm); while (o = what(&ms)) { b = where(&ms, nm, &lock, ©, pm); switch (o) { case '+': nm |= b & m & ~mm; if (b & S_ISGID) setsgid = 1; if (lock & 04) lock |= 02; break; case '-': nm &= ~(b & m & ~mm); if (b & S_ISGID) setsgid = 1; if (lock & 04) lock = 0; break; case '=': nm &= ~m; nm |= b & m & ~mm; lock &= ~01; if (lock & 04) lock |= 02; om = 0; if (copy == 0) cleared = 1; break; } lock &= ~04; } } while (*ms++ == ','); if (*--ms) failed(&badumask[4], badumask); out: if (pm & S_IFDIR) { if ((pm & S_ISGID) && setsgid == 0) nm |= S_ISGID; else if ((nm & S_ISGID) && setsgid == 0) nm &= ~(mode_t)S_ISGID; } return(nm); }
/* * Implements the vi "G" command. * * Move to a particular line (the argument). Count from bottom of file if * argument is negative. */ int gotoline(int f, int n) { int status; /* status return */ if (f == FALSE) { status = gotoeob(f, n); } else { status = vl_gotoline(n); if (status != TRUE) mlwarn("[Not that many lines in buffer: %ld]", absol((long) n)); } return status; }
Position Line::YIntersection(double y) { if(!size()) return Position(); for(unsigned i = 0; i < size(); i += degree) if(at(i).y == y) return Position(at(i)); int seg = GetSeg(y); if(seg < 0) return Position(); double high,low; if(at(seg).y < at(seg+degree).y) { high = 1.0; low = 0.0; } else { high = 0.0; low = 1.0; } double close = DBL_MAX; Coord ret; do { double t = (high + low) / 2.0; Coord p = Bezier(&at(seg),degree,t); double d = absol(p.y - y); if(d < close) { ret = p; close = d; } if(p.y > y) high = t; else low = t; } while(absol(high - low) > .01); /* should be adaptive */ return Position(ret); }
/* move howmany lines starting at from to to */ static void tcapscroll_delins(int from, int to, int n) { int i; if (to == from) return; if (DL && AL) { if (to < from) { tcapmove(to,0); putpad(tgoto(DL,0,from-to)); tcapmove(to+n,0); putpad(tgoto(AL,0,from-to)); } else { tcapmove(from+n,0); putpad(tgoto(DL,0,to-from)); tcapmove(from,0); putpad(tgoto(AL,0,to-from)); } } else { /* must be dl and al */ #if OPT_PRETTIER_SCROLL if (absol(from-to) > 1) { tcapscroll_delins(from, (from<to) ? to-1:to+1, n); if (from < to) from = to-1; else from = to+1; } #endif if (to < from) { tcapmove(to,0); for (i = from - to; i > 0; i--) putpad(dl); tcapmove(to+n,0); for (i = from - to; i > 0; i--) putpad(al); } else { tcapmove(from+n,0); for (i = to - from; i > 0; i--) putpad(dl); tcapmove(from,0); for (i = to - from; i > 0; i--) putpad(al); } } }
int main() { /* MIRACL rational calculator */ int i,j,k,p,q,c,hpos; BOOL over,help; screen(); #if MIRACL==16 mip=mirsys(10,0); /*** 16-bit computer ***/ #else mip=mirsys(6,0); /*** 32-bit computer ***/ #endif mip->ERCON=TRUE; x=mirvar(0); for (i=0;i<=top;i++) y[i]=mirvar(0); m=mirvar(0); t=mirvar(0); radeg=mirvar(0); loge2=mirvar(0); loge10=mirvar(0); eps=mirvar(0); mip->pi=mirvar(0); cinstr(mip->pi,cpi); /* read in constants */ fpmul(mip->pi,1,180,radeg); cinstr(loge2,clg2); cinstr(loge10,clg10); cinstr(eps,ceps); help=OFF; show(TRUE); p=6; q=0; flag=OFF; newx=OFF; over=FALSE; setopts(); clrall(); drawit(); while (!over) { /* main loop */ if (mip->ERNUM) { aprint(ORDINARY,4+5*p,6+3*q,keys[q][p]); p=5,q=0; } if (width==80 || !help) { aprint(INVER,4+5*p,6+3*q,keys[q][p]); curser(1,24); c=gethit(); aprint(ORDINARY,4+5*p,6+3*q,keys[q][p]); } else while ((c=gethit())!='H') ; result=TRUE; if ((k=arrow(c))!=0) { /* arrow key hit */ if (k==1 && q>0) q--; if (k==2 && q<5) q++; if (k==3 && p<6) p++; if (k==4 && p>0) p--; continue; } if (c=='H') { /* switch help on/off */ help=!help; for (i=1;i<=24;i++) { if (width==80) hpos=41; else hpos=1; if (help) aprint(HELPCOL,hpos,i,htext[i-1]); else lclr(hpos,i); } if (width==40 && !help) drawit(); continue; } if (c>='A' && c<='F') { /* hex only */ if (!next(c)) putchar(BELL); else show(FALSE); continue; } for (j=0;j<6;j++) for (i=0;i<7;i++) if (c==qkeys[j][i]) p=i,q=j,c=' '; if (c==8 || c==127) p=6,q=1,c=' '; /* aliases */ if (c==',' || c=='a') p=5,q=5,c=' '; if (c=='O' || c==ESC) p=6,q=0,c=' '; if (c==13) p=6,q=5,c=' '; if (c=='[' || c=='{') p=3,q=5,c=' '; if (c==']' || c=='}') p=4,q=5,c=' '; if (c=='d') p=5,q=2,c=' '; if (c=='b') p=5,q=3,c=' '; if (c=='^') p=3,q=2,c=' '; if (c==' ') over=act(p,q); else continue; absol(x,t); if (fcomp(t,eps)<0) zero(x); if (result) { /* output result to display */ cotstr(x,mip->IOBUFF); just((char *)mip->IOBUFF); if (mip->ERNUM<0) { /* convert to radix and try again */ mip->ERNUM=0; mip->RPOINT=ON; cotstr(x,mip->IOBUFF); putchar(BELL); just((char *)mip->IOBUFF); } clr(); } if (newx) { /* update display */ getstat(); show(FALSE); } } curser(1,24); restore(); return 0; }
BOOL gauss(flash A[][50],flash b[],int n) { /* solve Ax=b using Gaussian elimination * * solution x returned in b */ int i,j,k,m; BOOL ok; flash w,s; w=mirvar(0); s=mirvar(0); ok=TRUE; for (i=0;i<n;i++) copy(b[i],A[i][n]); for (i=0;i<n;i++) { /* Gaussian elimination */ m=i; for (j=i+1;j<n;j++) { absol(A[j][i],w); absol(A[m][i],s); if (fcomp(w,s)>0) m=j; } if (m!=i) for (k=i;k<=n;k++) { copy(A[i][k],w); copy(A[m][k],A[i][k]); copy(w,A[m][k]); } if (size(A[i][i])==0) { ok=FALSE; break; } for (j=i+1;j<n;j++) { fdiv(A[j][i],A[i][i],s); for (k=n;k>=i;k--) { fmul(s,A[i][k],w); fsub(A[j][k],w,A[j][k]); } } } if (ok) for (j=n-1;j>=0;j--) { /* Backward substitution */ zero(s); for (k=j+1;k<n;k++) { fmul(A[j][k],b[k],w); fadd(s,w,s); } fsub(A[j][n],s,w); if (size(A[j][j])==0) { ok=FALSE; break; } fdiv(w,A[j][j],b[j]); } mirkill(s); mirkill(w); return ok; }
/* The main function for the seven-parameter diffusion model */ double cdfdif(double t, int x, double *par, double *prob) { double a = par[0], Ter = par[1], eta = par[2], z = par[3], sZ = par[4], st = par[5], nu = par[6], a2 = a*a, Z_U = (1-x)*z+x*(a-z)+sZ/2, /* upper boundary of z distribution */ Z_L = (1-x)*z+x*(a-z)-sZ/2, /* lower boundary of z distribution */ lower_t = Ter-st/2, /* lower boundary of Ter distribution */ upper_t, /* upper boundary of Ter distribution */ delta = 1e-29, /* convergence values for terms added to partial sum */ epsilon = 1e-7, /* value to check deviation from zero */ min_RT=0.001; /* realistic minimum rt to complete decision process */ int v_max = 5000; /* maximum number of terms in a partial sum */ /* approximating infinite series */ double Fnew, sum_z=0, sum_nu=0, p1, p0, sum_hist[3]={0,0,0}, denom, sifa, upp, low, fact, exdif, su, sl, zzz, ser; double nr_nu = 20, nr_z = 20; double gk[20]={-5.3874808900112327592,-4.6036824495507442379,-3.9447640401156252032,-3.3478545673832162954,-2.7888060584281304521,-2.2549740020892756753,-1.7385377121165861425,-1.2340762153953230840,-.73747372854539439135,-.24534070830090126680,.24534070830090126680,.73747372854539439135,1.2340762153953230840,1.7385377121165861425,2.2549740020892756753,2.7888060584281304521,3.3478545673832162954,3.9447640401156252032,4.6036824495507442379,5.3874808900112327592}, w_gh[20]={.22293936455341646528e-12,.43993409922731809293e-9,.10860693707692821796e-6,.78025564785320632605e-5,.22833863601635307687e-3,.32437733422378566862e-2,.24810520887340880430e-1,.10901720602002162863,.28667550536283409324,.46224366960061014087,.46224366960061014087,.28667550536283409324,.10901720602002162863,.24810520887340880430e-1,.32437733422378566862e-2,.22833863601635307687e-3,.78025564785320632605e-5,.10860693707692821796e-6,.43993409922731809293e-9,.22293936455341646528e-12}, gz[20]={-.99312859919393192687,-.96397192725643798816,-.91223442827299427993,-.83911697180954192277,-.74633190646438019034,-.63605368072610402042,-.51086700195056844453,-.37370608871551552754,-.22778585114163685255,-.76526521133497449334e-1,.76526521133497338312e-1,.22778585114163671377,.37370608871551153074,.51086700195060519292,.63605368072587842310,.74633190646520863876,.83911697180775823846,.91223442827524447996,.96397192725477587327,.99312859919448925883}, w_g[20]={.17614007115893910022e-1,.40601429824919738065e-1,.62672048327592072559e-1,.83276741580984969815e-1,.10193011981663034626,.11819453196160842334,.13168863844919270756,.14209610931837018954,.14917298647260451849,.15275338713072578178,.15275338713072586505,.14917298647260440747,.14209610931836397230,.13168863844922096273,.11819453196171304798,.10193011981627890516,.83276741581628954680e-1,.62672048318034509484e-1,.40601429821751071347e-1,.17614007115704332501e-1}; int i,m,v; for(i=0; i<nr_nu; i++) { gk[i] = 1.41421356237309505*gk[i]*eta+nu; /* appropriate scaling of GH quadrature nodes based on normal kernel */ w_gh[i] = w_gh[i]/1.772453850905515882; /* appropriate weighting of GH quadrature weights based on normal kernel */ } for(i=0; i<nr_z; i++) gz[i] = (.5*sZ*gz[i])+z; /* appropriate scaling of Gaussian quadrature nodes */ /* numerical integration with respect to z_0 */ for (i=0; i<nr_z; i++) { sum_nu=0; /* numerical integration with respect to xi */ for (m=0; m<nr_nu; m++) { if (absol(gk[m])>epsilon) /* test for gk(m) being close to zero */ { sum_nu+=(exp(-200*gz[i]*gk[m])-1)/(exp(-200*a*gk[m])-1)*w_gh[m]; } else { sum_nu+=gz[i]/a*w_gh[m]; } } sum_z+=sum_nu*w_g[i]/2; } *prob=sum_z; /* end of prob */ if (t-Ter+st/2>min_RT)/* is t larger than lower boundary Ter distribution? */ { upper_t = t<Ter+st/2 ? t : Ter+st/2; p1=*prob*(upper_t-lower_t)/st; /* integrate probability with respect to t */ p0=(1-*prob)*(upper_t-lower_t)/st; if (t>Ter+st/2) /* is t larger than upper boundary Ter distribution? */ { sum_hist[0] = 0; sum_hist[1] = 0; sum_hist[2] = 0; for (v=0;v<v_max;v++) /* infinite series */ { sum_hist[0]=sum_hist[1]; sum_hist[1]=sum_hist[2]; sum_nu=0; sifa=PI*v/a; for (m=0;m<nr_nu;m++) /* numerical integration with respect to xi */ { denom=(100*gk[m]*gk[m]+(PI*PI)*(v*v)/(100*a2)); upp=exp((2*x-1)*Z_U*gk[m]*100-3*log(denom)+log(w_gh[m])-2*log(100)); low=exp((2*x-1)*Z_L*gk[m]*100-3*log(denom)+log(w_gh[m])-2*log(100)); fact=upp*((2*x-1)*gk[m]*sin(sifa*Z_U)*100-sifa*cos(sifa*Z_U))- low*((2*x-1)*gk[m]*sin(sifa*Z_L)*100-sifa*cos(sifa*Z_L)); exdif=exp((-.5*denom*(t-upper_t))+ log(1-exp(-.5*denom*(upper_t-lower_t)))); sum_nu+=fact*exdif; } sum_hist[2]=sum_hist[1]+v*sum_nu; if ((absol(sum_hist[0]-sum_hist[1])<delta) && (absol(sum_hist[1]-sum_hist[2])<delta) && (sum_hist[2]>0)) break; } Fnew=(p0*(1-x)+p1*x)-sum_hist[2]*4*PI/(a2*sZ*st); /* cumulative distribution function for t and x */ } else if (t<=Ter+st/2) /* is t lower than upper boundary Ter distribution? */ { sum_nu=0; for (m=0;m<nr_nu;m++) { if (absol(gk[m])>epsilon) { sum_z=0; for (i=0;i<nr_z;i++) { zzz=(a-gz[i])*x+gz[i]*(1-x); ser=-((a*a2)/((1-2*x)*gk[m]*PI*.01))*sinh(zzz*(1-2*x)*gk[m]/.01)/ (sinh((1-2*x)*gk[m]*a/.01)*sinh((1-2*x)*gk[m]*a/.01)) +(zzz*a2)/((1-2*x)*gk[m]*PI*.01)*cosh((a-zzz)*(1-2*x) *gk[m]/.01)/sinh((1-2*x)*gk[m]*a/.01); sum_hist[0] = 0; sum_hist[1] = 0; sum_hist[2] = 0; for (v=0;v<v_max;v++) { sum_hist[0]=sum_hist[1]; sum_hist[1]=sum_hist[2]; sifa=PI*v/a; denom=(gk[m]*gk[m]*100+(PI*v)*(PI*v)/(a2*100)); sum_hist[2]=sum_hist[1]+v*sin(sifa*zzz)* exp(-.5*denom*(t-lower_t)-2*log(denom)); if ((absol(sum_hist[0]-sum_hist[1])<delta) && (absol(sum_hist[1]-sum_hist[2])<delta) && (sum_hist[2]>0)) break; } sum_z+=.5*w_g[i]*(ser-4*sum_hist[2])*(PI/100)/ (a2*st)*exp((2*x-1)*zzz*gk[m]*100); } } else { sum_hist[0] = 0; sum_hist[1] = 0; sum_hist[2] = 0; su=-(Z_U*Z_U)/(12*a2)+(Z_U*Z_U*Z_U)/ (12*a*a2)-(Z_U*Z_U*Z_U*Z_U)/(48*a2*a2); sl=-(Z_L*Z_L)/(12*a2)+(Z_L*Z_L*Z_L)/ (12*a*a2)-(Z_L*Z_L*Z_L*Z_L)/(48*a2*a2); for (v=1;v<v_max;v++) { sum_hist[0]=sum_hist[1]; sum_hist[1]=sum_hist[2]; sifa=PI*v/a; denom=(PI*v)*(PI*v)/(a2*100); sum_hist[2]=sum_hist[1]+1/(PI*PI*PI*PI*v*v*v*v)*(cos(sifa*Z_L)- cos(sifa*Z_U))*exp(-.5*denom*(t-lower_t)); if ((absol(sum_hist[0]-sum_hist[1])<delta) && (absol(sum_hist[1]-sum_hist[2])<delta) && (sum_hist[2]>0)) break; } sum_z=400*a2*a*(sl-su-sum_hist[2])/(st*sZ); } sum_nu+=sum_z*w_gh[m]; } Fnew=(p0*(1-x)+p1*x)-sum_nu; } } else if (t-Ter+st/2<=min_RT) /* is t lower than lower boundary Ter distr? */ { Fnew=0; } Fnew = Fnew>delta ? Fnew : 0; return Fnew; }
static void go_ray(struct initializations &init, Spectrum &spec, Metric &metric, Particle &particle, EMField &emfield, string &plotfilename) { myfloat dtau = init.dtau; ofstream plotfile(plotfilename.c_str()); ofstream dtaufile("globaldtau.dat"); ofstream wrongfile("globalwrong.dat"); plotfile << scientific; dtaufile << scientific; wrongfile << scientific; // Print important information to plotfile plotfile << "# Metric: " << metric.name << endl; plotfile << "# m = " << metric.m << endl << "# a = " << metric.a << endl << "# q = " << metric.q << endl; plotfile << "# particle:" << endl << "# m = " << init.teilchen_masse << endl << "# q = " << init.teilchen_ladung << endl; plotfile << "# length_4velocity = " << scalar(metric, particle.x, particle.u, particle.u) << endl << "# dtau = " << dtau << endl << "# tau_max = " << init.tau_max << endl << "# max_wrongness = " << init.max_wrongness << endl << "# max_x1 = " << init.max_x1 << endl; plotfile << "# x3 x1 x2 x0 wrong u3 u1 u2 u0" << endl; myfloat wrong = wrongness(metric, &init, particle.x, particle.u); myfloat wrong_old = wrong; // Iterate int absorb_last = 0; int taun = -1; myfloat tau = 0.0; while ((absol(wrong) <= init.max_wrongness) && (tau <= init.tau_max + dtau)) { dtau = init.dtau * pow(absol(particle.x[1] - 2 * init.m), sqrt(3.0)); if (++taun % 256 == 0) { info(metric, taun, tau, dtau, wrong, particle); if (taun >= 10000) { cerr << "WARNING: Aborting prematurely: taking too long" << endl; break; } } // print to file plotfile<< particle.x[3] << ' ' << particle.x[1] << ' ' << particle.x[2] << ' ' << particle.x[0] << '\t' << wrong << '\t' << particle.u[3] << ' ' << particle.u[1] << ' ' << particle.u[2] << ' ' << particle.u[0] << '\t' << tau << '\t' << dtau << endl; dtaufile << tau << "\t" << dtau << endl; wrongfile << tau << "\t" << wrong << endl; tau += dtau; // Berechne x, u x_and_u(metric, emfield, dtau, particle); // Update spectrum absorb += spec.inc_cnts(particle.x, particle.u); wrong_old = wrong; wrong = wrongness(metric, &init, particle.x, particle.u); if (absorb) { if (absorb >= absorb_max) break; if (absorb == absorb_last) // error, currently break; absorb_last = absorb; } if ((particle.x[1] > init.max_x1) || (particle.x[1] < init.min_x1)) break; } plotfile << endl; plotfile << "# length_4velocity = " << scalar(metric, particle.x, particle.u, particle.u) << endl; plotfile.close(); dtaufile.close(); wrongfile.close(); info(metric, taun, tau, dtau, wrong, particle); append_file(taun); }
/* The main function for the seven-parameter diffusion model */ double cdfdif(double t, int x, double *par, double *prob) { double a = par[0], Ter = par[1], eta = par[2], z = par[3], sZ = par[4], st = par[5], nu = par[6], a2 = a*a, Z_U = (1-x)*z+x*(a-z)+sZ/2, /* upper boundary of z distribution */ Z_L = (1-x)*z+x*(a-z)-sZ/2, /* lower boundary of z distribution */ lower_t = Ter-st/2, /* lower boundary of Ter distribution */ upper_t, /* upper boundary of Ter distribution */ delta = 1e-29, /* convergence values for terms added to partial sum */ epsilon = 1e-7, /* value to check deviation from zero */ min_RT=0.001; /* realistic minimum rt to complete decision process */ int v_max = 5000; /* maximum number of terms in a partial sum */ /* approximating infinite series */ double Fnew, sum_z=0, sum_nu=0, p1, p0, sum_hist[3]={0,0,0}, denom, sifa, upp, low, fact, exdif, su, sl, zzz, ser; double nr_nu = 10, nr_z = 10; double gk[10]={-3.4361591188377378359,-2.5327316742327896648,-1.7566836492998818553,-1.0366108297895135770,-.34290132722370464391,.34290132722370464391,1.0366108297895135770,1.7566836492998818553,2.5327316742327896648,3.4361591188377378359}, w_gh[10]={.76404328552326426869e-5,.13436457467812345894e-2,.33874394455481036947e-1,.24013861108231340791,.61086263373532512233,.61086263373532512233,.24013861108231340791,.33874394455481036947e-1,.13436457467812345894e-2,.76404328552326426869e-5}, gz[10]={-.97390652851716952298,-.86506336668898531350,-.67940956829902787728,-.43339539412924654727,-.14887433898163121571,.14887433898163096591,.43339539412924582562,.67940956829902887648,.86506336668898320408,.97390652851716963401}, w_g[10]={.66671344308693342162e-1,.14945134915057770031,.21908636251598398448,.26926671930999629412,.29552422471475281451,.29552422471475292554,.26926671930999596105,.21908636251597693456,.14945134915057806113,.66671344308693036851e-1}; int i,m,v; for(i=0; i<nr_nu; i++) { gk[i] = 1.41421356237309505*gk[i]*eta+nu; /* appropriate scaling of GH quadrature nodes based on normal kernel */ w_gh[i] = w_gh[i]/1.772453850905515882; /* appropriate weighting of GH quadrature weights based on normal kernel */ } for(i=0; i<nr_z; i++) gz[i] = (.5*sZ*gz[i])+z; /* appropriate scaling of Gaussian quadrature nodes */ /* numerical integration with respect to z_0 */ for (i=0; i<nr_z; i++) { sum_nu=0; /* numerical integration with respect to xi */ for (m=0; m<nr_nu; m++) { if (absol(gk[m])>epsilon) /* test for gk(m) being close to zero */ { sum_nu+=(exp(-200*gz[i]*gk[m])-1)/(exp(-200*a*gk[m])-1)*w_gh[m]; } else { sum_nu+=gz[i]/a*w_gh[m]; } } sum_z+=sum_nu*w_g[i]/2; } *prob=sum_z; /* end of prob */ if (t-Ter+st/2>min_RT)/* is t larger than lower boundary Ter distribution? */ { upper_t = t<Ter+st/2 ? t : Ter+st/2; p1=*prob*(upper_t-lower_t)/st; /* integrate probability with respect to t */ p0=(1-*prob)*(upper_t-lower_t)/st; if (t>Ter+st/2) /* is t larger than upper boundary Ter distribution? */ { sum_hist[0] = 0; sum_hist[1] = 0; sum_hist[2] = 0; for (v=0;v<v_max;v++) /* infinite series */ { sum_hist[0]=sum_hist[1]; sum_hist[1]=sum_hist[2]; sum_nu=0; sifa=PI*v/a; for (m=0;m<nr_nu;m++) /* numerical integration with respect to xi */ { denom=(100*gk[m]*gk[m]+(PI*PI)*(v*v)/(100*a2)); upp=exp((2*x-1)*Z_U*gk[m]*100-3*log(denom)+log(w_gh[m])-2*log(100)); low=exp((2*x-1)*Z_L*gk[m]*100-3*log(denom)+log(w_gh[m])-2*log(100)); fact=upp*((2*x-1)*gk[m]*sin(sifa*Z_U)*100-sifa*cos(sifa*Z_U))- low*((2*x-1)*gk[m]*sin(sifa*Z_L)*100-sifa*cos(sifa*Z_L)); exdif=exp((-.5*denom*(t-upper_t))+ log(1-exp(-.5*denom*(upper_t-lower_t)))); sum_nu+=fact*exdif; } sum_hist[2]=sum_hist[1]+v*sum_nu; if ((absol(sum_hist[0]-sum_hist[1])<delta) && (absol(sum_hist[1]-sum_hist[2])<delta) && (sum_hist[2]>0)) break; if (v==v_max) mexPrintf("V_MAX reached!\n"); } Fnew=(p0*(1-x)+p1*x)-sum_hist[2]*4*PI/(a2*sZ*st); /* cumulative distribution function for t and x */ } else if (t<=Ter+st/2) /* is t lower than upper boundary Ter distribution? */ { sum_nu=0; for (m=0;m<nr_nu;m++) { if (absol(gk[m])>epsilon) { sum_z=0; for (i=0;i<nr_z;i++) { zzz=(a-gz[i])*x+gz[i]*(1-x); ser=-((a*a2)/((1-2*x)*gk[m]*PI*.01))*sinh(zzz*(1-2*x)*gk[m]/.01)/ (sinh((1-2*x)*gk[m]*a/.01)*sinh((1-2*x)*gk[m]*a/.01)) +(zzz*a2)/((1-2*x)*gk[m]*PI*.01)*cosh((a-zzz)*(1-2*x) *gk[m]/.01)/sinh((1-2*x)*gk[m]*a/.01); sum_hist[0] = 0; sum_hist[1] = 0; sum_hist[2] = 0; for (v=0;v<v_max;v++) { sum_hist[0]=sum_hist[1]; sum_hist[1]=sum_hist[2]; sifa=PI*v/a; denom=(gk[m]*gk[m]*100+(PI*v)*(PI*v)/(a2*100)); sum_hist[2]=sum_hist[1]+v*sin(sifa*zzz)* exp(-.5*denom*(t-lower_t)-2*log(denom)); if ((absol(sum_hist[0]-sum_hist[1])<delta) && (absol(sum_hist[1]-sum_hist[2])<delta) && (sum_hist[2]>0)) break; } sum_z+=.5*w_g[i]*(ser-4*sum_hist[2])*(PI/100)/ (a2*st)*exp((2*x-1)*zzz*gk[m]*100); } } else { sum_hist[0] = 0; sum_hist[1] = 0; sum_hist[2] = 0; su=-(Z_U*Z_U)/(12*a2)+(Z_U*Z_U*Z_U)/ (12*a*a2)-(Z_U*Z_U*Z_U*Z_U)/(48*a2*a2); sl=-(Z_L*Z_L)/(12*a2)+(Z_L*Z_L*Z_L)/ (12*a*a2)-(Z_L*Z_L*Z_L*Z_L)/(48*a2*a2); for (v=1;v<v_max;v++) { sum_hist[0]=sum_hist[1]; sum_hist[1]=sum_hist[2]; sifa=PI*v/a; denom=(PI*v)*(PI*v)/(a2*100); sum_hist[2]=sum_hist[1]+1/(PI*PI*PI*PI*v*v*v*v)*(cos(sifa*Z_L)- cos(sifa*Z_U))*exp(-.5*denom*(t-lower_t)); if ((absol(sum_hist[0]-sum_hist[1])<delta) && (absol(sum_hist[1]-sum_hist[2])<delta) && (sum_hist[2]>0)) break; } sum_z=400*a2*a*(sl-su-sum_hist[2])/(st*sZ); } sum_nu+=sum_z*w_gh[m]; } Fnew=(p0*(1-x)+p1*x)-sum_nu; } } else if (t-Ter+st/2<=min_RT) /* is t lower than lower boundary Ter distr? */ { Fnew=0; } Fnew = Fnew>delta ? Fnew : 0; return Fnew; }
/* * function encodeByteArrayToPoint : Encodes the given byte array into a point. * If the given byte array can not be encoded to a point, returns 0. * param dlog : Pointer to the native Dlog object. * param binaryString : The byte array to encode. * param k : k is the maximum length of a string to be converted to a Group Element of this group. * If a string exceeds the k length it cannot be converted. * return : The created point or 0 if the point cannot be created. */ JNIEXPORT jlong JNICALL Java_edu_biu_scapi_primitives_dlog_miracl_MiraclDlogECFp_encodeByteArrayToPoint (JNIEnv * env, jobject, jlong m, jbyteArray binaryString, jint k){ //Pseudo-code: /*If the length of binaryString exceeds k then throw IndexOutOfBoundsException. Let L be the length in bytes of p Choose a random byte array r of length L – k – 2 bytes Prepare a string newString of the following form: r || binaryString || binaryString.length (where || denotes concatenation) (i.e., the least significant byte of newString is the length of binaryString in bytes) Convert the result to a BigInteger (bIString) Compute the elliptic curve equation for this x and see if there exists a y such that (x,y) satisfies the equation. If yes, return (x,y) Else, go back to step 3 (choose a random r etc.) up to 80 times (This is an arbitrary hard-coded number). If did not find y such that (x,y) satisfies the equation after 80 trials then return null. */ /* convert the accepted parameters to MIRACL parameters*/ miracl* mip = (miracl*)m; jbyte* string = (jbyte*) env->GetByteArrayElements(binaryString, 0); int len = env->GetArrayLength(binaryString); if (len > k){ env ->ReleaseByteArrayElements(binaryString, string, 0); return 0; } big x, p; x = mirvar(mip, 0); p = mip->modulus; int l = logb2(mip, p)/8; char* randomArray = new char[l-k-2]; char* newString = new char[l - k - 1 + len]; memcpy(newString+l-k-2, string, len); newString[l - k - 2 + len] = (char) len; int counter = 0; bool success = 0; csprng rng; srand(time(0)); long seed; char raw = rand(); time((time_t*)&seed); strong_init(&rng,1,&raw,seed); do{ for (int i=0; i<l-k-2; i++){ randomArray[i] = strong_rng(&rng); } memcpy(newString, randomArray, l-k-2); bytes_to_big(mip, l - k - 1 + len, newString, x); //If the number is negative, make it positive. if(exsign(x)== -1){ absol(x, x); } //epoint_x returns true if the given x value leads to a valid point on the curve. //if failed, go back to choose a random r etc. success = epoint_x(mip, x); counter++; } while((!success) && (counter <= 80)); //we limit the amount of times we try to 80 which is an arbitrary number. epoint* point = 0; if (success){ point = epoint_init(mip); epoint_set(mip, x, x, 0, point); } char* temp = new char[l - k - 1 + len]; big_to_bytes(mip,l - k - 1 + len , x, temp, 1); //Delete the allocated memory. env ->ReleaseByteArrayElements(binaryString, string, 0); mirkill(x); delete(randomArray); delete(newString); //Return the created point. return (long) point; }