// Calculates the moments Sm for each slice double CollimatorWakeProcess::CalculateSm(int mode, int slice) { double x=0; for(ParticleBunch::iterator p=bunchSlices[slice]; p!=bunchSlices[slice+1]; p++) { double r = sqrt(powd(p->x(),2)+powd(p->y(),2)); double theta = atan2(p->y(),p->x()); x += powd(r,mode)*sin(mode*theta); } return x; }
int main(void) { int no; printf("整数を入力してください。"); scanf("%d", &no); printf("その数の四乗値は%dです。\n", powd(no)); return(0); }
void Try(ll i){ ll d=1,j; if(i==tlen){ for(j=0;j<tlen;++j){ d=d*powd(t[j],m[j]); } res=(res*(powmod(3,d)-1))%M; }else{ for(j=0;j<=c[i];++j){ m[i]=j; Try(i+1); } } }
int main(int ac, char **av) { int i,m,l,h; int temp; short value[16][8]; char Hexa[3]; int val; short j[16],k; int idx; char buf[17]; int len = 0; struct timeval start,finish; unsigned int sec,sec2,sec3,sec4; int usec,usec2,usec3,usec4; if( ac != 2 ){ printf("error ac != 2\n"); exit(1); } strncpy( buf, av[1], 16); buf[16] = 0x00; len = strlen(buf); if( len != 16 ){ printf("str len != 16[%d][%s]\n",len,buf); exit(1); } printf("original logic >>>\n"); memset( value, 0x00, 16*8 ); gettimeofday(&start,NULL); for ( i=0 ; i < 8 ;i++) { memcpy( Hexa, &buf[idx], 2 ); Hexa[2] = 0x00; idx += 2; sscanf( Hexa, "%x", &val ); j[i] = val; for( k = 0; k<8 ; k++ ) { value[i][k] = j[i]&powd(2,k); } printf_value(value[i]); } gettimeofday(&finish,NULL); sec = finish.tv_sec - start.tv_sec; usec = finish.tv_usec - start.tv_usec; if( usec < 0 ){ sec--; usec += 1000000; } printf("my logic >>>\n"); memset( value, 0x00, 16*8 ); gettimeofday(&start,NULL); for( i=0,m=0; i< 8; i++,m=m+2 ){ for(h=0,l=1;h< 8;h++){ temp = getItoH(buf[m])*16+getItoH(buf[m+1]); value[i][h]=l&temp; l*=2; } printf_value(value[i]); } gettimeofday(&finish,NULL); memset( value, 0x00, 16*8 ); sec2 = finish.tv_sec - start.tv_sec; usec2 = finish.tv_usec - start.tv_usec; if( usec2 < 0 ){ sec2--; usec2 += 1000000; } printf("my logic2[ 16* <<4 ] >>>\n"); gettimeofday(&start,NULL); for( i=0,m=0; i< 8; i++,m=m+2 ){ for(h=0,l=1;h< 8;h++){ temp = (getItoH(buf[m])<<4)+getItoH(buf[m+1]); value[i][h]=l&temp; l*=2; } printf_value(value[i]); } gettimeofday(&finish,NULL); memset( value, 0x00, 16*8 ); sec3 = finish.tv_sec - start.tv_sec; usec3 = finish.tv_usec - start.tv_usec; if( usec3 < 0 ){ sec3--; usec3 += 1000000; } printf("my logic3[ deleted temp ] >>>\n"); gettimeofday(&start,NULL); for( i=0,m=0; i< 8; i++,m=m+2 ){ for(h=0,l=1;h< 8;h++){ value[i][h]=l&( (getItoH(buf[m])*16 )+getItoH(buf[m+1]) ); l*=2; } printf_value(value[i]); } gettimeofday(&finish,NULL); sec4 = finish.tv_sec - start.tv_sec; usec4 = finish.tv_usec - start.tv_usec; if( usec4 < 0 ){ sec4--; usec4 += 1000000; } printf("\n result >>>\n"); printf(" original - elapsed time :%u.%06d\n",sec,usec); printf(" my - elapsed time :%u.%06d\n",sec2,usec2); printf(" my2 - elapsed time :%u.%06d\n",sec3,usec3); printf(" my3 - elapsed time :%u.%06d\n",sec4,usec4); return 0; }
// powd(A,N) = A ^ N int powd(const int A, const int N) { if (N > 0) return A * powd(A,N-1); else return 1; }
void CollimatorWakeProcess::ApplyWakefield(double ds)// int nmodes) { collimator_wake=(CollimatorWakePotentials*) currentWake; for(int m=1; m<=nmodes; m++) { for (size_t n=0; n<nbins; n++) { Cm[m][n]=CalculateCm(m,n); Sm[m][n]=CalculateSm(m,n); } } double wake_x,wake_y,wake_z; double macrocharge=currentBunch->GetTotalCharge()/currentBunch->size(); double a0 = macrocharge*ElectronCharge*Volt; a0 /= 4*pi*FreeSpacePermittivity; double p0 = currentBunch->GetReferenceMomentum(); if(recalc) { Init(); } double bload=0; #define WAKE_GRADIENT(wake) ((wake[currmode][nslice+1]-wake[currmode][nslice])/dz); for(int currmode=1; currmode<=nmodes; currmode++) { CalculateWakeT(dz, currmode); CalculateWakeL(dz, currmode); double z=zmin; int iparticle=0; for(size_t nslice = 0; nslice<nbins; nslice++) { double g_ct = WAKE_GRADIENT(wake_ct); double g_st = WAKE_GRADIENT(wake_st); double g_cl = WAKE_GRADIENT(wake_cl); double g_sl = WAKE_GRADIENT(wake_sl); g_ct=g_st=g_cl=g_sl=0; int number_particles=0; for(ParticleBunch::iterator p=bunchSlices[nslice]; p!=bunchSlices[nslice+1]; p++) { number_particles++; double r = sqrt (powd(p->x(),2) + powd(p->y(),2)); double theta = atan2(p->y(),p->x()); double zz = p->ct()-z; double wxc = cos((currmode-1)*theta)*(wake_ct[currmode][nslice]+g_ct*zz); double wxs = sin((currmode-1)*theta)*(wake_st[currmode][nslice]+g_st*zz); double wys = cos((currmode-1)*theta)*(wake_st[currmode][nslice]+g_st*zz); double wyc = sin((currmode-1)*theta)*(wake_ct[currmode][nslice]+g_ct*zz); wake_x = currmode*powd(r,currmode-1)*(wxc+wxs); wake_y = currmode*powd(r,currmode-1)*(wys-wyc); wake_x*=a0; wake_y*=a0; double wzc = cos(currmode*theta)*(wake_cl[currmode][nslice]+g_cl*zz); double wzs = sin(currmode*theta)*(wake_sl[currmode][nslice]+g_sl*zz); wake_z = powd(r,currmode)*(wzc-wzs); wake_z*= a0; double ddp = -wake_z/p0; p->dp() += ddp; bload += ddp; double dxp = inc_tw? wake_x/p0 : 0; double dyp = inc_tw? wake_y/p0 : 0; p->xp() = (p->xp()+dxp)/(1+ddp); double oldpy=p->yp(); p->yp() = (p->yp()+dyp)/(1+ddp); } z+=dz; } } }
local void object_spiral(int npars, real *pars) { int i,nx = Nx(iptr); int j,ny = Ny(iptr); int l, lmax; real A = 1.0; real h = 1.0; real k = 1.0; /* wave number */ real p = 1.0; /* 1/2 power of cos */ real r0 = 0.0; /* starting radius */ real p0 = 0.0; /* starting angle */ int m = 2; /* number of arms */ real x1,y1,x2,y2,x3,y3,r,arg,value; real amp,phi,sum; bool Qint, pint; if (npar > 0) A = pars[0]; if (npar > 1) h = pars[1]; if (npar > 2) k = pars[2]; if (npar > 3) p = pars[3]; if (npar > 4) m = (int) pars[4]; /* note rounding ? */ if (npar > 5) r0 = pars[5]; if (npar > 6) p0 = pars[6]; dprintf(0,"spiral: %g %g %g %g %d %g %g\n",A,h,k,p,m,r0,p0); if (A==0) return; p0 *= PI/180.0; /* convert from degrees to radians */ k *= TWO_PI; /* angles are 2.PI.k.r , so absorb 2.PI.k in one */ pint = (int) p; Qint = (p-pint == 0); if (Qint) warning("Integer power p = %d\n",pint); lmax = Qtotflux ? 2 : 1; for (l=0; l<lmax; l++) { /* 1st loop: sum up the flux if in 2nd loop: normalize */ if (l==0) sum = 0.0; else { A /= sum; dprintf(0,"spiral: A->%g\n",A); } for (j=0; j<ny; j++) { y1 = (j-center[1])*Dy(iptr) + Ymin(iptr); for (i=0; i<nx; i++) { x1 = (i-center[0])*Dx(iptr) + Xmin(iptr); x2 = -x1*sinp - y1*cosp; y2 = (x1*cosp - y1*sinp)/cosi; r = sqrt(x2*x2+y2*y2); if (r < r0) value = 0.0; else { /* ? should match this up better so we can connect bar and spiral ? */ phi = atan2(y2,x2) + k*(r-r0) + p0; if (Qint) { amp = powi(cos(m*phi),pint); /* these can come out negative for odd p's !! */ } else amp = powd(cos(m*phi),(double)p); arg = r/h; value = (arg < 80) ? A * amp * exp(-arg) : 0.0; } if (Qtotflux) { if (l==0) sum += value; else MapValue(iptr,i,j) = factor*MapValue(iptr,i,j) + value; } else MapValue(iptr,i,j) = factor*MapValue(iptr,i,j) + value; } /* i */ } /* j */ } /* k */ }