Пример #1
0
int main()
{
    std::vector<number> P;
    P.push_back(1);
    P.push_back(1);
    number curr = 1;
    while (curr)
    {
        number accum = 0;
        number k = 1;
        while (1)
        {
            number g = get_g(k);
            if (g > P.size())
            {
                break;
            }
            accum += ((k % 2 == 0)?-1:1) * P[P.size() - g];
            k = next_k(k);
        }
        curr = accum % bound;
        P.push_back(curr);
    }
    std::cout << P.size() - 1<< std::endl;
}
vector<bool> Receiver::open(Integer vp) {

    Integer v = get_g(com.n, vp);

    Integer u = a_exp_b_mod_c(v, Integer::Power2(com.l), com.n);

    if (u != com.u) {
        throw ProtocolException("u != com.u");
    }

    return decode(v);
}
void Receiver::accept_commitment(const Commitment &_com) {
    this->com = _com;
    if (com.g != get_g(com.n, com.h)) {
        throw ProtocolException("g != get_g");
    }
    if (com.W.size() != com.K+1) {
        throw ProtocolException("W.size() != K+1");
    }

    if (com.W[0] != (com.g*com.g % com.n) || com.W[com.K] != com.u) {
        throw ProtocolException("invalid W");
    }
}
Пример #4
0
quan::three_d::vect<DATA_STRUCT::force>
DATA_STRUCT::single_particle_forces(DATA_STRUCT::particle const& x){ 
 
    quan::three_d::vect<DATA_STRUCT::force> result(
        DATA_STRUCT::force(0),
#ifdef USE_QUAN
       DATA_STRUCT::force(- x.mass * get_quan_g()),
#else
        DATA_STRUCT::force(- x.mass * get_g()),
#endif
        DATA_STRUCT::force(0)
    );
    return result;
}  
Пример #5
0
void Analyze::writeGrid () {
  std::ofstream fout ("/Users/fred.christensen/Dropbox/school/Parallel/genetic/result.ppm");
  fout << "P3\n640 420\n255\n";
  for (int y = 0; y < height; ++y) {
    for (int x = 0; x < width; ++x) {
      double bias = (double)grid[x][y] / (double)max;
      fout << get_r(bias) << " " << get_g(bias) << " " << get_b(bias);
      if (x < width -1) {
        fout << "\t";
      } else {
        fout << std::endl;
      }
    }
  }
}
Commitment Commiter::commit(const unsigned K, const vector<bool> &m) {
    unsigned available_primes;
    Integer g, h;
    Integer a0, a, u;
    vector<bool> S;
    vector<Integer> W;
    unsigned l;

    // sanity check on m
    for(bool b: m) {
        if (b != 0 && b != 1) {
            throw SanityException("not binary m");
        }
    }
    l = m.size();

    h = Integer(common::rng(), 2, n-1);
    g = get_g(n, h);
    order = find_order(p, q, g);

    a0 = a_exp_b_mod_c(2, Integer::Power2(K) - l, phi);
    a = a_exp_b_mod_c(2, Integer::Power2(K), phi);
    u = a_exp_b_mod_c(g, a0, n);

    S.resize(l);
    for(unsigned i = 0; i < l; ++i) {
        S[i] = m[i] ^ u.GetBit(0);
        u = a_times_b_mod_c(u, u, n);
    }

    // sanity check on u
    if (u != a_exp_b_mod_c(g, a, n)) {
        throw SanityException("u != g ** a");
    }

    W.resize(K+1);
    for(unsigned i = 0; i <= K; ++i) {
        Integer exp = a_exp_b_mod_c(2, Integer::Power2(i) , phi);
        W[i] = a_exp_b_mod_c(g, exp, n);
    }

    // sanity check on W
    if (W[0] != (g*g % n) || W[1] != (g*g*g*g % n) || W[K] != u) {
        throw SanityException("invalid W");
    }
    com = Commitment(K, n, h, g, u, S, W, m.size());
    return com;
}
Пример #7
0
uint calc(int x, int y){
	
	// r,g,b color values around center 'c11'
	// 00 10 20
	// 01 11 21
	// 02 12 22
	
	uint c00=get(x-1,y-1);
	uint c01=get(x-1,y);
	uint c02=get(x-1,y+1);
	uint c10=get(x,y-1);
	uint c11=get(x,y);
	uint c12=get(x,y+1);
	uint c20=get(x+1,y-1);
	uint c21=get(x+1,y);
	uint c22=get(x+1,y+1);
	
	// red
	uint r00=get_r(x-1,y-1);
	uint r01=get_r(x-1,y)  ;
	uint r02=get_r(x-1,y+1);
	uint r10=get_r(x,y-1)  ;
	uint r11=get_r(x,y)	;
	uint r12=get_r(x,y+1)  ;
	uint r20=get_r(x+1,y-1);
	uint r21=get_r(x+1,y)  ;
	uint r22=get_r(x+1,y+1);
	
	// green
	uint g00=get_g(x-1,y-1);
	uint g01=get_g(x-1,y)  ;
	uint g02=get_g(x-1,y+1);
	uint g10=get_g(x,y-1)  ;
	uint g11=get_g(x,y)	;
	uint g12=get_g(x,y+1)  ;
	uint g20=get_g(x+1,y-1);
	uint g21=get_g(x+1,y)  ;
	uint g22=get_g(x+1,y+1);
	
	// blue
	uint b00=get_b(x-1,y-1);
	uint b01=get_b(x-1,y)  ;
	uint b02=get_b(x-1,y+1);
	uint b10=get_b(x,y-1)  ;
	uint b11=get_b(x,y)	;
	uint b12=get_b(x,y+1)  ;
	uint b20=get_b(x+1,y-1);
	uint b21=get_b(x+1,y)  ;
	uint b22=get_b(x+1,y+1);
	
	// center colors of THIS dot
	uint r=r11;
	uint g=g11;
	uint b=b11;
	
	// random colors
	uint zr=rand()%256;
	uint zg=rand()%256;
	uint zb=rand()%256;	
	
	// mean color value of surrounding
	uint r0=((r10+r01+r21+r12)/4);
	uint g0=((g10+g01+g21+g12)/4);
	uint b0=(b10+b01+b21+b12)/4;
	
	// Including this dot
	// uint r0=((r10+r01+r11+r21+r12)/5);
	// uint g0=((g10+g01+g11+g21+g12)/5);
	// uint b0=(b10+b01+b11+b21+b12)/5;
	
	float fr=r0/256.0;
	float fg=g0/256.0;
	float fb=b0/256.0;
	
	uint k=128;
	float h=100.0;

//////////
// done with initialization, 
// now updated the dot's color based on some crazy experimental whatever function
// feel free to wildly experiment with this algorithm!

// POPULATION UPDATE

	//  inspired by Conway's game of life:

	//  if there is a sufficient population, then grow in numbers:
	if(b0>=100)b=b0*1.01;
	//  if the population is small then shrink
	if(b0<100)b=b0*(0.99-fr/100);
	//  if the population is too big then collapse
	if(b0>240)b=0;
	//  repopulate collapsed populations
	if(b0<3)b=120;//zb*((1.5-f)+fr*f);
	
	if(r0>=100)r=r0*1.01;
	if(r0<100)r=r0*(0.99-fg/100);
	if(r11>240)r=0;
	if(r11<3)r=120*(1.2-fr*fg);//zb*((1.5-f)+fr*f);
	
	if(g0>=100)g=g0*1.01;
	if(g0<100)g=g0*(0.99-f*fb/100);
	if(g11>240){g=0;b=b/2;}
	if(g11<3)g=120*(1-fb*fr);//zb*((1.5-f)+fr*f);

// END OF POPULATION UPDATE 

	if(r>255)r=255;
	if(g>255)g=255;
	if(b>255)b=255;
	return (r<<16)+(g<<8)+b;
}
Пример #8
0
void
ZcrossPath::start_sil(Bface *f )
{

   /*
    *
    *  the face class accessor to vertices, edges, etc 
    *  f->v() , f->e(), etc. all take a point from 1 to 3
    *  this is a huge pain when you are trying to move around
    *  the triangle, so i store my values , - g , ex1, ex2, 
    *  in the 0 to 2 range 
    *
    */



   /* the call to has_sil returns if mesh has been looked at
    * and ALWAYS marks it as such. be careful..
    *
    *  this should really be called sil_marked(); or something
    */
   if (!has_sil(f))
      return; //f has been zx_checked this frame


   double g[3]; //
   int ex1, ex2 , nex1, nex2; //indices of cross edges, in 0-2;
   //ex1  is vertex 1 of edge 1 ( and index to edge )
   //nex1 is vertex 2 of edge 1
   bool bgrad;

   get_g(f, g, ex1, ex2 ); //calculates g values, and where

   Bface *f1;
   Bface *f2;

   Bface *iter_f; //iterator for mesh traversal;
   nex1 = (ex1+1)%3;
   nex2 = (ex2+1)%3;

   // new iterators for mesh traversal
   Bvert * svrt[3];
   double sg[3];

   //never check across a crease boundary ()
   //it's a discontinuity in the isosurface

   f1 = ( f->e(ex1+1)->is_crease()) ? nullptr : f->nbr(ex1+1);
   f2 = ( f->e(ex2+1)->is_crease()) ? nullptr : f->nbr(ex2+1);


   //case 1 - we search and close the loop
   //case 2 - we do not close loop -> so search backward, fix

   double alph1 = -g[ex1] / (g[nex1] - g[ex1]);
   Wpt pt1 = interp ( f->v(ex1+1)->loc(), f->v(nex1+1)->loc(), alph1);

   double alph2 = -g[ex2] / (g[nex2] - g[ex2]);
   Wpt pt2 = interp ( f->v(ex2+1)->loc(), f->v(nex2+1)->loc(), alph2);


   // gradient test;
   int gmax = ( g[0] > g[1] ) ? 0 : 1;
   gmax = ( g[2] > g[gmax] ) ? 2 : gmax;

   Wvec v_iso = pt1 - pt2;
   Wvec v_max = f->v(gmax+1)->loc() - pt2;
   Wvec v_grad = v_max.orthogonalized(v_iso);
   bgrad = ( ( _eye - pt2 ) * v_grad  > 0 ) ;

   //we always move clockwise
   // more specifically, if gradient is up, we always
   // check in the same direction

   // you only have to check once

   if ( cross( v_grad , f->norm()) * v_iso > 0 ) {
      swap ( pt1, pt2);
      swap ( alph1, alph2);
      swap ( f1, f2);
      swap ( ex1, ex2 );
      swap ( nex1, nex2);
   }

   //if everything switches, so does bgrad!

   //vert is the front ( according to gradient ) vertex on the list
   //we were going to use it for visibility, but at the moment it's
   //useless.

   // store our own barycentric coordinate

   Wvec bc;
   bc[ex2] = 1.0-alph2;
   bc[nex2] = alph2;


   //start search on f1



   int cstart = num(); // index start of this chain (for case 2 );

   iter_f = f;
   add_seg ( f , pt2, f->v(ex2+1), bgrad, f);

   if (f1) {

      svrt  [0]   = f->v(ex2+1) ;     //initialize cache values
      sg    [0]   = g[ex2];        //for sil_search
      svrt  [1]   = f->v(nex2+1);     //skee-daddle
      sg    [1]   = g[nex2];

      //we start at f, having already added the previous point
      while ( iter_f ) {

         iter_f = sil_walk_search(iter_f, svrt, sg);

         if ( iter_f == f ) {  //closed loop

            if ( !( _segs[cstart].p()  == _segs.back().p() ) ) {
               //cerr << "XXX ZcrossPath::start_sil():: points are not equal" << endl;
               //                 Wpt a = _segs[cstart].p();
               //                 Wpt b = _segs.last().p();
               //cerr << "diff:" << (a-b).length() << endl;
               _segs.back().p() = _segs[cstart].p() ;
            }
            _segs.back().setf( nullptr );
            _segs.back().set_end();
            _segs.back().set_bary(f2);

            return;
         }

      }
   } else {
      add_seg ( nullptr , pt1, f->v(ex1+1), bgrad, f);
   }

   //if we are this far, we have a section of
   //silhouette in the forward direction, ( it may only cross one triangle tho )
   //which ran across a discontinuity

   //now check for the stretch in the opposite direction

   int chain2start = num(); // index of start of next chain

   if ( !f2 )
      return; // first face in reverse direction is nullptr, we are done.

   //find second chain, which needs to be reversed
   //starts at pt2;


   iter_f  = f2;
   add_seg ( f2 , pt2, f->v(ex2+1), bgrad, f2);

   svrt[0] = f->v(ex2+1) ;     //initialize cached values
   sg[0]  = g[ex2];  //for sil_search
   svrt[1] = f->v(nex2+1);
   sg[1]  = g[nex2];

   while ( iter_f ) {
      iter_f = sil_walk_search ( iter_f, svrt, sg );
   }
   int chain2end = num();
   // second chain is found in wrong order, so we flip it


   // reversal code


   int chain2num = (chain2end - chain2start) -1 ;
   if ( chain2num < 0 )
      return;

   _segs.insert(_segs.begin() + cstart, chain2num, ZXseg());
   int last_ind = num()-1;

   int j=0;
   for ( ; j < chain2num ; j++ ) {
      _segs[last_ind-j].setf( _segs[last_ind-(j+1)].f() );      //shift the faces over by one
      //AND any per-face values
      _segs[last_ind-j].setg(_segs[last_ind-(j+1)].g());        //grad associates with face
      _segs[last_ind-j].set_bary(_segs[last_ind-j].f());        //recalc barycentric
   }
   for ( j=0; j < chain2num ; j++) {
      _segs[cstart+j] = _segs.back();
      _segs.pop_back();
   }

   _segs.pop_back(); //last point was the duplicate

   return;
}
Пример #9
0
main(int argc,char *argv[])
{
   int  i,l,nrho,lmax0,lmax,lmmax;
   int  icube,nfft,nfft3d,n2ft3d;
   int  ispin,ne[2];
   REAL unit,zv,amass,drho,rc[9];
   REAL *vnlnrm,*vl,*vnl,*g;
   REAL omega,unita[9],unitg[9];
   REAL *rho,*vp,*wp;
   char atom[2];
   char *pspname;
   FILE *fp;

   printf("\ngenerating formated pseudopotential\n");
   printf(REALTYPE);printf("\n");
   pspname = argv[1];

  /* read in aimd.param */
   fp = fopen("aimd.param","r");
     fscanf(fp,"%d",&icube);
     fscanf(fp,FMT1,&unit);
     fscanf(fp,"%d",&nfft);
     fscanf(fp,"%d %d %d",&ispin,&(ne[0]),&(ne[1]));
   fclose(fp);
   nfft3d = (nfft/2+1)*nfft*nfft;
   n2ft3d = (nfft+2)*nfft*nfft;

   /* preparation of index vector */
   get_cube(icube,unit,&omega,unita,unitg);

   g  = (REAL *) malloc(3*nfft3d*sizeof(REAL));
   get_g(nfft,unitg,g);

   printf("icube=%d\n",icube);
   printf("unit =");printf(FMT1,unit);printf("\n");
   printf("nfft =%d nfft3d=%d\n",nfft,nfft3d);
   printf("omega = %f\n",omega);
   printf("a1 = <"); printf(FMT10,unita[0],unita[1],unita[2]); printf(">\n");
   printf("a2 = <"); printf(FMT10,unita[3],unita[4],unita[5]); printf(">\n");
   printf("a3 = <"); printf(FMT10,unita[6],unita[7],unita[8]); printf(">\n");
   printf("g1 = <"); printf(FMT10,unitg[0],unitg[1],unitg[2]); printf(">\n");
   printf("g2 = <"); printf(FMT10,unitg[3],unitg[4],unitg[5]); printf(">\n");
   printf("g3 = <"); printf(FMT10,unitg[6],unitg[7],unitg[8]); printf(">\n");
   printf("\n");


   /* pseudopotential data */
   fp = fopen(pspname,"r");
     fscanf(fp,"%s",atom);
     fscanf(fp,FMT1,&zv);
     fscanf(fp,FMT1,&amass);
     fscanf(fp,"%d",&lmax);
     for (l=0; l<=lmax; ++l)
        fscanf(fp,FMT1,&rc[l]);
     fscanf(fp,"%d",&nrho);
     fscanf(fp,FMT1,&drho);

     rho = (REAL *) malloc(nrho*sizeof(REAL));
     vp  = (REAL *) malloc((lmax+1)*nrho*sizeof(REAL));
     wp  = (REAL *) malloc((lmax+1)*nrho*sizeof(REAL));
     for (i=0; i<nrho; ++i)
     {
        fscanf(fp,FMT1,&rho[i]);
        for (l=0; l<=lmax; ++l)
           fscanf(fp,FMT1,&vp[i+l*nrho]);
     }
     for (i=0; i<nrho; ++i)
     {
        fscanf(fp,FMT1,&rho[i]);
        for (l=0; l<=lmax; ++l)
           fscanf(fp,FMT1,&wp[i+l*nrho]);
     }
   fclose(fp);
   lmmax = lmax*lmax;

   printf("atom =%s\n",atom);
   printf("lmax =%d lmmax=%d\n",lmax,lmmax);
   printf("zv   =");printf(FMT1,zv);printf("\n");
   printf("rc   =");
   for (l=0; l<=lmax; ++l){printf(FMT1,rc[l]); printf(" ");}
   printf("\n");
   printf("amass=");printf(FMT1,amass);printf("\n");
   printf("nrho =%d\n",nrho);
   printf("drho =");printf(FMT1,drho);printf("\n\n");
   printf("Lmax = %d\n",lmax);
   printf("enter the highest L desired =>");
   scanf("%d",&lmax0);
   if (lmax0<lmax) lmax = lmax0;

   lmmax = lmax*lmax;
   vnlnrm = (REAL *) malloc(lmmax*sizeof(REAL));
   vl     = (REAL *) malloc(nfft3d*sizeof(REAL));
   vnl    = (REAL *) malloc(lmmax*nfft3d*sizeof(REAL));

   kbpp(zv,lmax,nrho,drho,rho,vp,wp, nfft3d,g,vl,vnl,vnlnrm);

   fp = fopen("DATAOUT","wb");
   fwrite(&icube,sizeof(int),1,fp);
   fwrite(&nfft,sizeof(int),1,fp);
   fwrite(&unit,sizeof(REAL),1,fp);
   fwrite(atom,sizeof(char),2,fp);
   fwrite(&amass,sizeof(REAL),1,fp);
   fwrite(&zv,sizeof(REAL),1,fp);
   fwrite(&lmax,sizeof(int),1,fp);
   fwrite(rc,sizeof(REAL),lmax,fp);
   fwrite(vnlnrm,sizeof(REAL),lmmax,fp);
   fwrite(vl,sizeof(REAL),nfft3d,fp);
   fwrite(vnl,sizeof(REAL),lmmax*nfft3d,fp);
   fclose(fp);

   printf("\n vnlnrm      =");
   for (l=0; l<lmmax; ++l) {printf(FMT1,vnlnrm[l]);printf(" ");}
   printf("\n");
   printf(" vl(G=0)     =");printf(FMT1,vl[0]);  printf("\n");
   printf(" vnl(G=0,l=0)=");printf(FMT1,vnl[0]); printf("\n");
   free(g);
   free(rho);
   free(vp);
   free(wp);
   free(vnl);
   free(vl);
   free(vnlnrm);
}
Пример #10
0
///////////////////////////////////////////////////////////////////////
// Class				:	CWinDisplay
// Method				:	data
// Description			:	Draw the data onto the screen
// Return Value			:	-
// Comments				:
int	CWinDisplay::data(int x,int y,int w,int h,float *d) {
	int	i,j;

	clampData(w,h,d);

	for (i=0;i<h;i++) {
		const float		*src	=	&d[i*w*numSamples];
		unsigned int	*dest	=	&imageData[((height-(i+y)-1)*width+x)];

		switch(numSamples) {
		case 0:
			break;
		case 1:
			for (j=0;j<w;j++) {
				unsigned char	d	=	(unsigned char) (src[0]*255);

				*dest++				=	color(d,d,d,d);
				src++;
			}
			break;
		case 2:
			for (j=0;j<w;j++) {
				const float		r	=	src[0]*src[1]*255	+ (1-src[1])*get_r(dest[0]);
				const float		a	=	src[1]*255			+ (1-src[1])*get_a(dest[0]);
				unsigned char	dr	=	(unsigned char) r;
				unsigned char	da	=	(unsigned char) a;

				*dest++				=	color(dr,dr,dr,da);

				src					+=	2;
			}
			break;
		case 3:
			for (j=0;j<w;j++) {
				unsigned char	dr	=	(unsigned char) (src[0]*255);
				unsigned char	dg	=	(unsigned char) (src[1]*255);
				unsigned char	db	=	(unsigned char) (src[2]*255);

				*dest++				=	color(dr,dg,db,(unsigned char) 255);

				src					+=	3;
			}
			break;
		case 4:
			for (j=0;j<w;j++) {
				const float		r	=	src[0]*src[3]*255	+ (1-src[3])*get_r(dest[0]);
				const float		g	=	src[1]*src[3]*255	+ (1-src[3])*get_g(dest[0]);
				const float		b	=	src[2]*src[3]*255	+ (1-src[3])*get_b(dest[0]);
				const float		a	=	src[3]*255			+ (1-src[3])*get_a(dest[0]);
				unsigned char	dr	=	(unsigned char) r;
				unsigned char	dg	=	(unsigned char) g;
				unsigned char	db	=	(unsigned char) b;
				unsigned char	da	=	(unsigned char) a;

				*dest++				=	color(dr,dg,db,da);

				src					+=	4;
			}
			break;
		default:
			for (j=0;j<w;j++) {
				float			r	=	src[0]*src[3]*255	+ (1-src[3])*get_r(*dest);
				float			g	=	src[1]*src[3]*255	+ (1-src[3])*get_g(*dest);
				float			b	=	src[2]*src[3]*255	+ (1-src[3])*get_b(*dest);
				float			a	=	src[3]*255			+ (1-src[3])*get_a(*dest);
				unsigned char	dr	=	(unsigned char) r;
				unsigned char	dg	=	(unsigned char) g;
				unsigned char	db	=	(unsigned char) b;
				unsigned char	da	=	(unsigned char) a;

				*dest++				=	color(dr,dg,db,da);

				src					+=	numSamples;
			}
			break;
		}
	}

	if (active) {
		if (willRedraw == FALSE) {
			// Pump messages
			willRedraw	=	TRUE;
			PostMessage(hWnd,WM_PAINT,(WPARAM) this,0);
		}
	}

	return active;
}