void lup_decomposition(int n, double a[][n], int pi[]) { int i, j, k; for(i=0;i<n;i++) pi[i] = i; for(k=0;k<n;k++){ int max = k; for(i=k+1;i<n;i++) if(abs_double(a[i][k]) > abs_double(a[k][k])){ max = i; } if(a[max][k] == 0){ fprintf(stderr, "singular matrix\n"); return; } swap_element(pi, max, k); swap_row(n, a, max, k); for(i=k+1;i<n;i++) a[i][k] = a[i][k]/a[k][k]; for(i=k+1;i<n;i++) for(j=k+1;j<n;j++) a[i][j] = a[i][j] - a[i][k]*a[k][j]; } }
double descriptor34::ClebshGordan(double l, double m, double l1, double m1, double l2, double m2){ double j = l, j1 = l1, j2 = l2; if (!equal(m,(m1 + m2))) return 0; double min_possible, max_possible; min_possible = abs_double(j1 - j2); max_possible = j1 + j2; if (larger(j, max_possible)) return 0; if (larger(min_possible, j)) return 0; double first_sqrt = sqrt((2 * j + 1) * factorial[lrint(j + j1 - j2)] * factorial[lrint(j - j1 + j2)] * factorial[lrint(j1 + j2 - j)] / factorial[lrint(j1 + j2 + j + 1)]); double second_sqrt = sqrt(factorial[lrint(j + m)] * factorial[lrint(j - m)] * factorial[lrint(j1 - m1)] * factorial[lrint(j1 + m1)] * factorial[lrint(j2 - m2)] * factorial[lrint(j2 + m2)]); int maxk = min(lrint(j1 + j2 - j), lrint(j1 - m1)); maxk = min(maxk, lrint(j2 + m2)); int mink = 0; mink = max(mink, lrint(j2 - j - m1)); mink = max(mink, lrint(j1 + m2 - j)); double sum = 0; double a; for (int k = mink; k <= maxk; k = k + 1){ a = factorial[k] * factorial[lrint(j1 + j2 - j) - k] * factorial[lrint(j1 - m1) - k] * factorial[lrint(j2 + m2) - k] * factorial[lrint(j - j2 + m1) + k] * factorial[lrint(j - j1 - m2) + k]; a = 1.0/a; if (k % 2 == 1) a = -a; sum = sum + a; } return sum * first_sqrt * second_sqrt; }
int main (int argc, const char * argv[]) { int cases; //freopen("test.txt", "r", stdin); scanf("%d",&cases); while (cases--) { int i; double p1[3],v1[3],p2[3],v2[3]; double newPos1[3]; double newPos2[3]; double R; double time,time2; scanf("%lf%lf%lf",&p1[0],&p1[1],&p1[2]); scanf("%lf%lf%lf",&v1[0],&v1[1],&v1[2]); scanf("%lf%lf%lf",&p2[0],&p2[1],&p2[2]); scanf("%lf%lf%lf",&v2[0],&v2[1],&v2[2]); scanf("%lf",&R); double dis; double lefttime=0; double righttime=100000000; time=(lefttime+righttime)/2; time2=righttime; while (abs_double(time-time2)>PRE) { time=(lefttime+righttime)/2; time2=(time+righttime)/2; for (i=0; i<3; i++) { newPos1[i]=p1[i]+v1[i]*time; newPos2[i]=p2[i]+v2[i]*time; } double newDis1=sqrt((newPos1[0]-newPos2[0])*(newPos1[0]-newPos2[0])+ (newPos1[1]-newPos2[1])*(newPos1[1]-newPos2[1])+ (newPos1[2]-newPos2[2])*(newPos1[2]-newPos2[2])); for (i=0; i<3; i++) { newPos1[i]=p1[i]+v1[i]*time2; newPos2[i]=p2[i]+v2[i]*time2; } double newDis2=sqrt((newPos1[0]-newPos2[0])*(newPos1[0]-newPos2[0])+ (newPos1[1]-newPos2[1])*(newPos1[1]-newPos2[1])+ (newPos1[2]-newPos2[2])*(newPos1[2]-newPos2[2])); if (newDis2>newDis1) { dis=newDis1; righttime=time2; }else { dis=newDis2; lefttime=time; } if (dis<=R) { break; } } //printf("time=%lf,time2=%lf,dis=%lf\n",time,time2,dis); if (dis<=R) { printf("YES\n"); }else { printf("NO\n"); } } return 0; }
int main(int argc, char **argv) { FILE *fp; cos_sin_type s = 0; //sin output cos_sin_type c = 0; //cos output theta_type radian; //radian input double zs, zc; // sin and cos values calculated from math.h. //Error checking double Total_Error_Sin = 0.0; double Total_Error_Cos = 0.0; double error_sin = 0.0, error_cos = 0.0; fp = fopen("out.dat","w"); // Compute sin/cos values for angles up to NUM_DEGREE for (int i = 1; i < NUM_DEGREE; i++) { radian = i*M_PI/180; cordic(radian, s, c); zs = sin((double)radian); zc = cos((double)radian); error_sin = (abs_double((double)s-zs)/zs)*100.0; error_cos = (abs_double((double)c-zc)/zc)*100.0; Total_Error_Sin = Total_Error_Sin + error_sin*error_sin; Total_Error_Cos = Total_Error_Cos + error_cos*error_cos; fprintf(fp, "degree=%d, radian=%f, cos=%f:%f, sin=%f:%f, cos_error=%f\%, sin_error=%f\%\n", i, (double)radian, (double)c, zc, (double)s, zs, error_cos, error_sin); } fclose(fp); // Print out root mean square error (RMSE) in percentage printf ("Overall_Error_Sin=%f\%, Overall_Error_Cos=%f\%\n", sqrt(Total_Error_Sin/(NUM_DEGREE-1)), sqrt(Total_Error_Cos/(NUM_DEGREE-1))); return 0; }
point3D orthogonal_vector(point3D pole) { point3D o; if (equal(abs_double(pole.z), 1)) { o.x = 1; o.y = 0; o.z = 0; } else { o.x = -pole.y; o.y = pole.x; o.z = 0; o = normalize(o); } return o; }
t_surface *is_in_light(t_surface *surface, t_scene *scene, t_light *light, double *dot_light) { t_vector light_ray; t_double3 light_distance; t_surface *light_intersect; double dot_light_dir; light_ray.pos = surface->point; light_distance = v_minus_v(light->pos, surface->point); light_ray.dir = normalize(light_distance); *dot_light = dot_product(light_ray.dir, surface->normal); *dot_light = max_double(0, *dot_light); if (length_v(light->dir) > 0.01) { dot_light_dir = abs_double(dot_product(light_ray.dir, normalize(light->dir))); dot_light_dir = max_double(0, dot_light_dir); *dot_light *= dot_light_dir; } light_intersect = intersect(light_ray, scene, surface->object); if (light_intersect->object != NULL) light_intersect->distance -= length_v(light_distance); return (light_intersect); }
//-------------------------------------- // main function //-------------------------------------- int main(int argc, char** argv) { // Open channels to the FPGA board. // These channels appear as files to the Linux OS int fdr = open("/dev/xillybus_read_32", O_RDONLY); int fdw = open("/dev/xillybus_write_32", O_WRONLY); // Check that the channels are correctly opened if ((fdr < 0) || (fdw < 0)) { fprintf (stderr, "Failed to open Xillybus device channels\n"); exit(-1); } // sin output cos_sin_type s = 0; // cos output cos_sin_type c = 0; // radian input double radian; // sin & cos calculated by math.h double m_s = 0.0, m_c = 0.0; // Error terms double err_ratio_sin = 0.0; double err_ratio_cos = 0.0; double accum_err_sin = 0.0; double accum_err_cos = 0.0; // arrays to store the output double c_array[NUM_DEGREE]; double s_array[NUM_DEGREE]; int nbytes; Timer timer("CORDIC fpga (batch)"); timer.start(); //---------------------------------------------------------------------- // Send all values to the module //---------------------------------------------------------------------- for (int i = 1; i < NUM_DEGREE; ++i) { radian = i * M_PI / 180; // Convert double value to fixed-point representation theta_type theta(radian); // Convert fixed-point to int64 bit64_t theta_i; theta_i(theta.length()-1,0) = theta(theta.length()-1,0); int64_t input = theta_i; // Send bytes through the write channel // and assert that the right number of bytes were sent nbytes = write (fdw, (void*)&input, sizeof(input)); assert (nbytes == sizeof(input)); } //---------------------------------------------------------------------- // Read all results //---------------------------------------------------------------------- for (int i = 1; i < NUM_DEGREE; ++i) { // Receive bytes through the read channel // and assert that the right number of bytes were recieved int64_t c_out, s_out; nbytes = read (fdr, (void*)&c_out, sizeof(c_out)); assert (nbytes == sizeof(c_out)); nbytes = read (fdr, (void*)&s_out, sizeof(s_out)); assert (nbytes == sizeof(s_out)); // convert int64 to fixed point bit64_t c_i = c_out; bit64_t s_i = s_out; c(c.length()-1,0) = c_i(c.length()-1,0); s(s.length()-1,0) = s_i(s.length()-1,0); // Store to array c_array[i] = c; s_array[i] = s; } timer.stop(); //------------------------------------------------------------ // Check results //------------------------------------------------------------ for (int i = 1; i < NUM_DEGREE; ++i) { // Load the stored result c = c_array[i]; s = s_array[i]; // Call math lib radian = i * M_PI / 180; m_s = sin( radian ); m_c = cos( radian ); // Calculate normalized error err_ratio_sin = ( abs_double( (double)s - m_s) / (m_s) ) * 100.0; err_ratio_cos = ( abs_double( (double)c - m_c) / (m_c) ) * 100.0; // Accumulate error ratios accum_err_sin += err_ratio_sin * err_ratio_sin; accum_err_cos += err_ratio_cos * err_ratio_cos; } //------------------------------------------------------------ // Write out root mean squared error (RMSE) of error ratios //------------------------------------------------------------ // Print to screen std::cout << "#------------------------------------------------\n" << "Overall_Error_Sin = " << RMSE(accum_err_sin) << "\n" << "Overall_Error_Cos = " << RMSE(accum_err_cos) << "\n" << "#------------------------------------------------\n"; // Clean up close(fdr); close(fdw); return 0; }
void getSTB(double *mat, int *nCol, int *nRow, double *alpha, double *tol, int *max_iter, int *nCpu, double *Q, double *cov) { int check=1, iter=0, i, j; double include, alpha_old, tmp, best_cov; // best_alpha; double *tmp_col, *lower, *upper, *best_Q; lower = calloc(*nCol, sizeof(double)); upper = calloc(*nCol, sizeof(double)); best_Q = calloc(2*(*nCol), sizeof(double)); include = 1-(*alpha); /* requested simultaneous tolerance limit, i.e. coverage */ alpha_old = *alpha; *alpha = (*alpha)/2; best_cov=1.0; #ifdef _OPENMP omp_set_num_threads(*nCpu); /* set number of cores */ #endif while(check==1) { iter += 1; #pragma omp parallel for\ shared(mat, lower, upper, Q)\ private(i,j,tmp_col) for(i=0; i<*nCol; i++) /* pont-wise tolerance limits */ { tmp_col = calloc(*nRow, sizeof(double)); for(j=0; j<(*nRow); j++) { tmp_col[j] = mat[i*(*nRow)+j]; /* copy i-th col of the matrix 'mat'*/ } Q[i*2]=SASquantile(tmp_col, *alpha, *nRow, EPS); /* 1st row */ lower[i]=Q[i*2]; Q[i*2+1]=SASquantile(tmp_col, 1-(*alpha), *nRow, EPS); /* 2nd row */ upper[i]=Q[i*2+1]; free(tmp_col); } *cov=coverage(mat, lower, upper, *nCol, *nRow, *nCpu); /* compute coverage of current STB */ if(*cov >= include) /* at least 100(1-alpha)% coverage */ { if(*cov < best_cov) /* coverage better than former result */ { //best_alpha = *alpha; best_Q = Q; best_cov = *cov; } } if( abs_double((*alpha) - alpha_old)/2 == 0 ) /* terminate if alpha cannot become smaller */ { check = 0; } if( iter == *max_iter ) /* terminate if max number of iterations reached */ { check = 0; } if( abs_double(*cov - include) <= *tol && (*cov - include) >= 0 ) { check = 0; /* convergence tolerance reached */ } else /* bisection step */ { if( (*cov - include) < 0) { tmp = *alpha; *alpha = *alpha - abs_double(alpha_old-(*alpha))/2; alpha_old = tmp; } else { tmp = *alpha; *alpha = *alpha + abs_double(alpha_old-(*alpha))/2; alpha_old = tmp; } } } *cov = best_cov; /* Use best values from iteration history */ Q = best_Q; }
bool equal(double a, double b){ if (abs_double(a - b) < epsilonClebshGordan) return true; return false; }
void experiment(void) { long i, j, k; double Temp, Tstep, b, h, e, e2; double h_max = 0.0; newlattice(); Tstep = (Tmax - Tmin) / (samples - 1); if (!coldstart) { Temp = Tmax; Tstep = - Tstep; } else Temp = Tmin; // zero the averaging arrays for (i = 0; i < samples; i++) { energy_list[i] = 0.0; energy2_list[i] = 0.0; magnetiz_list[i] = 0.0; } // iterate over samples, runs, and Monte-Carlo steps for (i = 0; i < samples; i++) { //printf("\tTemp = %f\n",Temp); Temp_list[i] = Temp; beta = 1.0 / Temp; for (k = 0; k < equlibriate; k++) metropolis(); for (j = 0; j < runcount; j++) { for (k = 0; k < runsteps; k++) metropolis(); stats(); energy_list[i] += energy; energy2_list[i] += energy2; magnetiz_list[i] += abs_double(magnetiz); } Temp += Tstep; } // complete averaging and calculate heat capacities for (i = 0; i < samples; i++) { b = 1.0 / Temp_list[i]; e = energy_list[i] / runcount; e2 = energy2_list[i] / runcount; magnetiz_list[i] /= runcount; energy_list[i] = e; energy2_list[i] = e2; h = Boltzmann * b * b * (e2 - e * e); heatcapacity_list[i] = h; // screening for the critical point if (h > h_max) { h_max = h; sample_critical = i; } } }
//average SGD implemented by Julius 2014.08.27 void LLC_SGD(double *w, double *x, double *centers, int *knn_idx, int knn, int d) { int i, j, iter=0, iter0=-1; float GAMMA = 2, adaGAMMA=GAMMA; memset(w, 0.01, knn*sizeof(double)); //minimizing 0.5*||x - w*centers||2,2 + 0.5*lambda*||w||2,2 srand(time(NULL)); float past_grad[64] = {0}; //w[0] = w[1] = 0.25; init_w(w, knn); eval_obj_iter = 0; while(iter < knn*30) { for(i=0; i<BATCH_SIZE; i++) batch_idx[i] = rand()%d; //[0, 127] adaGAMMA = GAMMA*pow(1+GAMMA*BETA_SGD*(iter0+1), -0.75); //adaGAMMA = GAMMA; if(iter > knn*8) iter0++; //if(iter0 == 0) // printf("============Start averaging!!============\n"); //printf("adaGAMMA[%d]: %f\n", iter, adaGAMMA); float wcenters[BATCH_SIZE]={0}, sub_grad=0; //mini-batch SGD, if BATCH_SIZE = 1, then it is shrunk to SGD if(BATCH_SIZE > 1) { //mini-batch SGD for(j=0; j<BATCH_SIZE; j++) { int idx = batch_idx[j]; for(i=0; i<knn; i++) wcenters[j] += w[i]*centers[knn_idx[i]*d+idx]; wcenters[j] = x[idx] - wcenters[j]; } for(i=0; i<knn; i++) { sub_grad=0; for(j=0; j<BATCH_SIZE; j++) { int idx = batch_idx[j]; sub_grad += wcenters[j]*centers[knn_idx[i]*d+idx]; } sub_grad /= BATCH_SIZE; //average over mini-batch gradients sub_grad += BETA_SGD*w[i]; sub_grad *= adaGAMMA; if(abs_float(sub_grad) < 5e-4) //neglect too small update continue; w[i] += (sub_grad); w[i] = (w[i] > 1) ? 1: w[i]; w[i] = (w[i] < -1) ? -1: w[i]; } } else { //SGD int idx = batch_idx[0]; for(i=0; i<knn; i++) wcenters[0] += w[i]*centers[knn_idx[i]*d+idx]; for(i=0; i<knn; i++) { sub_grad = (x[idx] - wcenters[0])*centers[knn_idx[i]*d+idx]; //calculate sub_grad[0~127] sub_grad += BETA_SGD*w[i]; sub_grad *= adaGAMMA; if(abs_float(sub_grad) < 5e-4) //neglect too small update continue; double lower_bound, upper_bound; double tentaive_w = w[i] + sub_grad; if(w[i] > 0) { lower_bound = 0; //avg_w - 2*avg < x < avg_w + 2*avg upper_bound = 2*w[i]; } else { lower_bound = 2*w[i]; upper_bound = 0; } w[i] += (sub_grad); w[i] = (tentaive_w > upper_bound) ? upper_bound: tentaive_w; w[i] = (tentaive_w < lower_bound) ? lower_bound: tentaive_w; w[i] = (w[i] > 1) ? 1: w[i]; w[i] = (w[i] < -1) ? -1: w[i]; } } if(iter0 == 0) { for(i=0; i<knn; i++) avg_w[i] = w[i]; } if(iter0 >= 0) { for(i=0; i<knn; i++) { double past_w = avg_w[i]; avg_w[i] = w[i] + (double)iter0/(iter0+1)*(avg_w[i]-w[i]); past_grad[i] = avg_w[i] - past_w; double tentaive_w = avg_w[i] + 2*past_grad[i]; //momentum is used double lower_bound, upper_bound; if(avg_w[i] > 0) { lower_bound = -2*avg_w[i]; //avg_w - 3*avg < x < avg_w + 3*avg upper_bound = 4*avg_w[i]; } else { lower_bound = 4*avg_w[i]; upper_bound = -2*avg_w[i]; } avg_w[i] = (tentaive_w > upper_bound) ? upper_bound: tentaive_w; avg_w[i] = (tentaive_w < lower_bound) ? lower_bound: tentaive_w; avg_w[i] = (avg_w[i] > 1) ? 1: avg_w[i]; avg_w[i] = (avg_w[i] < -1) ? -1: avg_w[i]; //printf("[%d](w, grad) = (%f, %f)\n", i, avg_w[i], past_grad[i]); } #ifdef SGD_DEBUGGING eval_obj(avg_w, x, centers, knn_idx, knn, d); #endif } //norm_w(w, d); else { #ifdef SGD_DEBUGGING eval_obj(w, x, centers, knn_idx, knn, d); #endif } iter++; } double sum=0; for(i=0; i<knn; i++) sum += avg_w[i]; for(i=0; i<knn; i++) { avg_w[i] /= sum; if(abs_double(avg_w[i]) < TOLERANCE) //cut-off small values avg_w[i] = 0; } #ifdef SGD_DEBUGGING printf("LLC_SGD w: ("); for(i=0; i<knn; i++) printf("%f ", avg_w[i]); printf(")\n"); #endif }
//-------------------------------------- // main function of TB //-------------------------------------- int main(int argc, char** argv) { // sin output double ans = 0; // cos output // sin & cos calculated by math.h double m_an = 0.0; // Error terms double err_ratio; double accum_err; // arrays to store the output double array[NUM_DEGREE]; // HLS streams for communicating with the cordic block //hls::stream<bit32_t> cordic_in; hls::stream<bit32_t> cordic_out; //------------------------------------------------------------ // Send data to CORDIC sim //------------------------------------------------------------ for (int i = 1; i < NUM_DEGREE; i++) { bit32_t input = i; // Send both words to the HLS module // cordic_in.write( input ); //cordic_in.write( input+1 ); } //------------------------------------------------------------ // Execute CORDIC sim and store results //------------------------------------------------------------ for (int i = 1; i < NUM_DEGREE; i++) { // Run the HLS function dut( cordic_out ); // Read the two 32-bit cosine output words, low word first bit32_t output; output = cordic_out.read(); // Store to array array[i] = output; } //------------------------------------------------------------ // Check results //------------------------------------------------------------ for (int i = 1; i < NUM_DEGREE; ++i) { // Load the stored result ans = array[i]; m_an = i; // Calculate normalized error err_ratio = ( abs_double( (double)ans - m_an) / (m_an) ) * 100.0; // Accumulate error ratios accum_err += err_ratio * err_ratio; std::cout << "ans = "<< ans <<" m_an = "<<m_an<<"\n"; } //------------------------------------------------------------ // Write out root mean squared error (RMSE) of error ratios //------------------------------------------------------------ // Print to screen std::cout << "#------------------------------------------------\n" << "Overall_Error_Test = " << RMSE(accum_err) << "\n" << "#------------------------------------------------\n"; return 0; }