int main() { char t; double a,b,c; double root1,root2; while(1) { cout<<"input q to quit, any other char to continue \n"; cin>>t; if(t=='q') break; cout<<"input the coefficients a b c of the form ax^2+bx+c\n"; cin>>a>>b>>c; double discriminant=compute_discriminant(a,b,c); if(a==0&&b!=0) { linear_equation(b,c,root1); cout<<"this is a linear equation with the root= "<<root1<<endl; cout<<"the computational error is "<<compute_error(a,b,c,root1)<<endl; } if(a==0&&b==0) { cout<<"this equation has no real roots\n"; } if(discriminant==0&&(a||b!=0)) { double_real_root(a,b,c,root1); cout<<"this is an equation with a double real root= "<<root1<<endl; cout<<"the computational error is "<<compute_error(a,b,c,root1)<<endl; } if(discriminant>0&&a!=0) { two_real_roots(a,b,c,discriminant,root1,root2); cout<<"this equation has two real roots = "<<root1<<" and "<<root2<<endl; cout<<"the computational error for root 1 is "<<compute_error(a,b,c,root1)<<endl; cout<<"the computational error for root 2 is "<<compute_error(a,b,c,root2)<<endl; } if(discriminant<0) { complex<double> com_root1; complex<double> com_root2; no_real_roots(a,b,c,discriminant,com_root1,com_root2); cout<<"this equation has two imaginary roots = "<<com_root1<<" and "<<com_root2<<endl; } } keep_window_open(); return 0; }
void Test_lin_elem_exp_derivative(CuTest * tc){ printf("Testing function: lin_elem_exp_deriv on (a,b)\n"); struct Fwrap * fw = fwrap_create(1,"general-vec"); fwrap_set_fvec(fw,Sin3xTx2,NULL); double lb = -2.0,ub = -1.0; double delta = 1e-4, hmin = 1e-3; struct LinElemExpAopts * opts = NULL; opts = lin_elem_exp_aopts_alloc_adapt(0,NULL,lb,ub,delta,hmin); le_t f1 = lin_elem_exp_approx(opts,fw); le_t der = lin_elem_exp_deriv(f1); // error double abs_err; double func_norm; compute_error(lb,ub,1000,der,funcderiv,NULL,&abs_err,&func_norm); double err = abs_err / func_norm; CuAssertDblEquals(tc, 0.0, err, 1e-5); LINELEM_FREE(f1); LINELEM_FREE(der); lin_elem_exp_aopts_free(opts); fwrap_destroy(fw); }
void ofxIcp::icp_step(const vector<cv::Point3d> & current, const vector<cv::Point3d> & target, vector<cv::Point3d> & output, cv::Mat & rotation, cv::Mat & translation, double & error){ vector<cv::Point3d> closest_current; vector<cv::Point3d> closest_target; find_closest_points(closest_points_count, current, target, closest_current, closest_target); cv::Mat centroid_current; cv::Mat centroid_target; cv::reduce(cv::Mat(closest_current, false), centroid_current, 0, CV_REDUCE_AVG); cv::reduce(cv::Mat(closest_target, false), centroid_target, 0, CV_REDUCE_AVG); centroid_current = centroid_current.reshape(1); centroid_target = centroid_target.reshape(1); compute_rigid_transformation(closest_current, centroid_current, closest_target, centroid_target, rotation, translation); vector<cv::Point3d> transformed_closest_current; vector<cv::Point3d> transformed_current; transform(closest_current, rotation, translation, transformed_closest_current); transform(current, rotation, translation, transformed_current); compute_error(transformed_closest_current, closest_target, error); output = transformed_current; }
void MainWindow::on_pushButton_3_clicked() { //Learning //Target Input & Ouput Selection QString test; for(int k=0;k<n_training;k++) { for(int i=n_history;i>0;i--) { learning_rate=0.01; setTarget(i,i-1); forward(); backward(); } } long long error_rate = compute_error(); QString moneytext; moneytext= QString("Amount Money : %1").arg(error_rate); debug(moneytext); }
void Test_pw_derivative(CuTest * tc){ printf("Testing function: piecewise_poly_deriv on (a,b)\n"); // function struct Fwrap * fw = fwrap_create(1,"general-vec"); fwrap_set_fvec(fw,Sin3xTx2,NULL); // approximation double lb = -2.0, ub = -1.0; struct PwPolyOpts * opts = pw_poly_opts_alloc(LEGENDRE,lb,ub); opoly_t cpoly = piecewise_poly_approx1_adapt(opts,fw); opoly_t der = piecewise_poly_deriv(cpoly); // error double abs_err; double func_norm; compute_error(lb,ub,1000,der,funcderiv,NULL,&abs_err,&func_norm); double err = abs_err / func_norm; CuAssertDblEquals(tc, 0.0, err, 1e-13); POLY_FREE(cpoly); POLY_FREE(der); pw_poly_opts_free(opts); fwrap_destroy(fw); }
void rarray_compare(fftw_real *A, fftw_real *B, int n) { double d = compute_error(A, 1, B, 1, n); if (d > TOLERANCE) { fflush(stdout); fprintf(stderr, "Found relative error %e\n", d); fftw_die("failure in Ergun's verification procedure\n"); } }
void Gauss_siedel(double *P, double *fn, double *a_x, double *a_y) { long i, j, indx, indx_rght, indx_frnt, indx_lft, indx_bck, indx_ax, indx_ay; double P_old; double error; double tol=1.0e-6; int iter = 0; for(;;) { boundary_pressure(); for(i=1; i < pmesh-1; i++) { for(j=1;j < pmesh-1;j++) { indx = i*pmesh + j; indx_rght = indx + 1; indx_frnt = indx + pmesh; indx_lft = indx - 1; indx_bck = indx - pmesh; indx_ax = (i-1)*(pmesh-1) +(j-1); indx_ay = (i-1)*(pmesh-2) +(j-1); if (((i+j)%2) == 0) { //P[indx] =-1.0*(P[indx_lft] + P[indx_rght] //+ P[indx_bck] + P[indx_frnt]) + deltax*deltax*fn[indx]; P[indx] = -1.0*(a_x[indx_ax]*P[indx_lft] + a_x[indx_ax+1]*P[indx_rght] + a_y[indx_ay]*P[indx_bck] + a_y[indx_ay+(pmesh-2)]*P[indx_frnt]) + deltax*deltax*fn[indx]; P[indx] /= -1.0*(a_x[indx_ax+1]+a_x[indx_ax]+a_y[indx_ay+(pmesh-2)]+a_y[indx_ay]); } } } for(i=1; i < pmesh-1; i++) { for(j=1;j < pmesh-1;j++) { indx = i*pmesh + j; indx_rght = indx + 1; indx_frnt = indx + pmesh; indx_lft = indx - 1; indx_bck = indx - pmesh; indx_ax = (i-1)*(pmesh-1) + (j-1); indx_ay = (i-1)*(pmesh-2) + (j-1); if (((i+j)%2) != 0) { //P[indx] =-1.0*(P[indx_lft] + P[indx_rght] //+ P[indx_bck] + P[indx_frnt]) + deltax*deltax*fn[indx]; P[indx] =-1.0*(a_x[indx_ax]*P[indx_lft] + a_x[indx_ax+1]*P[indx_rght] + a_y[indx_ay]*P[indx_bck] + a_y[indx_ay+(pmesh-2)]*P[indx_frnt]) + deltax*deltax*fn[indx]; P[indx] /= -1.0*(a_x[indx_ax]+a_x[indx_ax+1]+a_y[indx_ay]+a_y[indx_ay+(pmesh-2)]); } } } iter++; error = compute_error(a_x,a_y,P, fn); printf("iter=%d\terror=%lf\n",iter,error); if (fabs(error) < tol) { break; } } }
void processSpecialKeys(int key, int x, int y) { /* if (key == GLUT_KEY_F1) { log_comp_designTriStrip_read(xs, ys); vector_field_data->add_sketch(xs, ys); } else if (key == GLUT_KEY_F2) { } else if (key == GLUT_KEY_F3) { } else if (key == GLUT_KEY_F4) { //save sketches sketching_datas[current_sketch_data_idx].xsss.push_back( sketching_datas[current_sketch_data_idx].xss); sketching_datas[current_sketch_data_idx].ysss.push_back( sketching_datas[current_sketch_data_idx].yss); sketching_datas[current_sketch_data_idx].xss.clear(); sketching_datas[current_sketch_data_idx].yss.clear(); // xs.clear(); ys.clear(); vector_field_data->clear(); // vector_field_data->updated_vector_field(); } else if (key == GLUT_KEY_F5) { vector_field_data->add_sketch(xs, ys); sketching_datas[current_sketch_data_idx].xss.push_back(xs); sketching_datas[current_sketch_data_idx].yss.push_back(ys); }*/ if (key == GLUT_KEY_F8) { vector_field_data->interpolate(); void compute_error(VectorFieldDesignData<float> *vField); compute_error(vector_field_data); } if (key == GLUT_KEY_F9) { vector_field_data->show_design = !vector_field_data->show_design; } else if (key == GLUT_KEY_F10) { vector_field_data->clone_push(); } else if (key == GLUT_KEY_F11) { selecting = !selecting; vector_field_data->show_analysis(!selecting); } else if (key == GLUT_KEY_F12) { vector_field_data->show_mesh = !vector_field_data->show_mesh; } else if (key == GLUT_KEY_F7) { } update(); }
static double acmp(COMPLEX *A, COMPLEX *B, int n) { double tol = TOLERANCE; double d = compute_error(A, B, n); #ifdef EXIT_ON_FAIL if (d > tol) { printf("\n\nfailure in Ergun's verification procedure. Error: %e\n\n", d); exit(1); } #endif return d; }
void ofxIcp::compute_transformation(const vector<cv::Point3d> & input, const vector<cv::Point3d> & target, cv::Mat & transformation){ vector<cv::Point3d> transformed; cv::Mat rotation, translation; double error; vector<cv::Point3d> closest_current; vector<cv::Point3d> closest_target; int increment = input.size()/5; for(int i = 0; i < input.size(); i += increment){ closest_current.push_back(input[i]); closest_target.push_back(target[i]); } cv::Mat centroid_current; cv::Mat centroid_target; cv::reduce(cv::Mat(closest_current, false), centroid_current, 0, CV_REDUCE_AVG); cv::reduce(cv::Mat(closest_target, false), centroid_target, 0, CV_REDUCE_AVG); centroid_current = centroid_current.reshape(1); centroid_target = centroid_target.reshape(1); compute_rigid_transformation(closest_current, centroid_current, closest_target, centroid_target, rotation, translation); vector<cv::Point3d> transformed_closest_current; vector<cv::Point3d> transformed_current; transform(closest_current, rotation, translation, transformed_closest_current); compute_error(transformed_closest_current, closest_target, error); transformation = (cv::Mat_<double>(4,4) << 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1); cv::Mat rotation_tmp = transformation.colRange(0,3).rowRange(0,3); cv::Mat translation_tmp = transformation.colRange(3,4).rowRange(0,3); cv::Mat translation_t = translation.reshape(1).t(); rotation.copyTo(rotation_tmp); translation_t.copyTo(translation_tmp); }
template <typename PointSource, typename PointTarget, typename FeatureT> float pcl::SampleConsensusInitialAlignment<PointSource, PointTarget, FeatureT>::computeErrorMetric ( const PointCloudSource &cloud, float) { std::vector<int> nn_index (1); std::vector<float> nn_distance (1); const ErrorFunctor & compute_error = *error_functor_; float error = 0; for (int i = 0; i < static_cast<int> (cloud.points.size ()); ++i) { // Find the distance between cloud.points[i] and its nearest neighbor in the target point cloud tree_->nearestKSearch (cloud, i, 1, nn_index, nn_distance); // Compute the error error += compute_error (nn_distance[0]); } return (error); }
void Test_pw_linear(CuTest * tc){ printf("Testing functions: piecewise_poly_linear \n"); double lb = -2.0; double ub = 0.7; struct PwPolyOpts * opts = pw_poly_opts_alloc(LEGENDRE,lb,ub); struct PiecewisePoly * pw = piecewise_poly_linear(2.0,-0.2,opts); // compute error double abs_err; double func_norm; compute_error(lb,ub,1000,pw,pw_lin,NULL,&abs_err,&func_norm); double err = abs_err / func_norm; CuAssertDblEquals(tc, 0.0, err, 1e-13); POLY_FREE(pw); pw_poly_opts_free(opts); }
int main(int argc, char ** argv) { MPI_Init(&argc, &argv); int P, rank; MPI_Comm_size(MPI_COMM_WORLD, &P); MPI_Comm_rank(MPI_COMM_WORLD, &rank); if (isPowerOfTwo(P) == false) { if (rank == 0) { printf("The number of processes is not a power of two \n"); } MPI_Finalize(); return 1; } if (argc > 1) { if (rank == 0) { printf("No argument needed\n"); } MPI_Finalize (); return 1; } int k; int k_max = 15, k_min = 3; double *err_vec, *S_vec; err_vec = calloc( k_max - k_min, sizeof(double) ); S_vec = calloc( k_max - k_min, sizeof(double) ); for ( k = k_min; k < k_max; k++ ) { int n = (int) pow(2, k); int np = (int) (n - n%P)/P; double *v, S = 0.0; MPI_Status status; int i; if ( rank == 0 ) { FILE *f; f = fopen("Error_Ex03.txt", "w"); fprintf(f, "Error Ex03, P = %d\n", P); v = calloc(n, sizeof(double)); compute_v(n, v); for (i = 1; i < P; i++ ){ MPI_Send( &v[np*i], np, MPI_DOUBLE, i, 0, MPI_COMM_WORLD); } double S_n = compute_S(np, v); MPI_Reduce(&S_n, &S, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD); double err = compute_error(S); S_vec[k - k_min] = S; err_vec[k - k_min] = err; if (k == k_max-1) { print_vec(k_max-k_min, S_vec, err_vec); for ( i = 0; i < k_max-k_min; i++ ) { fprintf(f, "%1.16f\n", err_vec[i]); } } free(v); fclose(f); } else { double *v_n; if (rank == P-1) { v_n = calloc(np + n%P, sizeof(double)); } else { v_n = calloc(np, sizeof(double)); } MPI_Recv(v_n, np, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, &status); double S_n = compute_S(np, v_n); MPI_Reduce(&S_n, &S, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD); } } MPI_Finalize(); return 0; }
void _calculate_parameters(double h,my_point p[],double w[],int num) { double H,I,J,K,L,A0, A1; double x,y,d; if (num > MAX_POINTS_NUM) { fprintf(stderr,"Point number is larger than previous set!\n"); return; } is_set_ret = false; compute_Aj(h,w,num); H = compute_H(p,num); I = compute_I(p,num); J = compute_J(p,num); K = compute_K(p,num); L = compute_L(p,num); A0 = H -h*h*J*J-K+h*h*L*L; A1 = 2*(I-h*h*J*L); // printf("H=%.3lf I=%.3lf J=%.3lf K=%.3lf L=%.3lf A0=%.3lf A1=%.3lf\n", // H,I,J,K,L,A0,A1); // printf("Calculated as follows:\n"); if (0 == A0) { if (0 == A1) { // A0 A1 are0 // x,y could be any value #if 1 printf("The distribution of the given points is a circle.\n"); x = y = sqrt(2.0) / 2; d = -(h*h*(J*x+L*y)); compute_error(d,x,y,p,num); #else #endif } else { // A0 is 0 A1 is not 0,x2=1/2,x2+y2=1 double ar[2] = {sqrt(2.0)/2,-sqrt(2.0)/2};// possible values of x,y int i,j; for (i=0;i<2;i++) { x = ar[i]; for (j=0;j<2;j++) { y = ar[j]; d = -(h*h*(J*x+L*y)); compute_error(d,x,y,p,num); } } } } else if (0 == A1) { double x_ar[4] = {0,0,1,-1}; double y_ar[4] = {1,-1,0,0};//possible values of x,y int i; for (i=0;i<4;i++) { x = x_ar[i]; y = y_ar[i]; d = -(h*h*(J*x+L*y)); compute_error(d,x,y,p,num); } } else { // A0!=0 A1!=0 double t = A0 / sqrt (A1*A1+A0*A0); // 0 < t < 1 double x_ar[4] = {sqrt (0.5*(1+t)),sqrt (0.5*(1-t)), -sqrt (0.5*(1+t)),-sqrt (0.5*(1-t))}; // possible values of x , x2 ≠ 0 or 1 int i; for (i=0;i<4;i++) { x = x_ar[i]; y = (A1/A0)* (x - 0.5/x); d = -(h*h*(J*x+L*y)); compute_error(d,x,y,p,num); } } }
int main(int argc,const char *argv[]) { Net *net; Group *input,*hidden,*output,*bias; ExampleSet *examples; Example *ex; Connections *c1,*c2,*c3,*c4; char * fileName = "input"; int i,count,j; int inputCount = 0,outputCount = 0, hiddenCount = 200; Real error, correct; Real epsilon,range; /* don't buffer output */ setbuf(stdout,NULL); /* set seed to unique number */ mikenet_set_seed(0); /* a default learning rate */ epsilon=0.1; /* default weight range */ range=0.5; default_errorRadius=0.0; /* what are the command line arguments? */ for(i=1;i<argc;i++) { if (strcmp(argv[i],"-seed")==0) { mikenet_set_seed(atol(argv[i+1])); i++; } else if (strncmp(argv[i],"-epsilon",5)==0) { epsilon=atof(argv[i+1]); i++; } else if (strcmp(argv[i],"-range")==0) { range=atof(argv[i+1]); i++; } else if (strcmp(argv[i],"-errorRadius")==0) { default_errorRadius=atof(argv[i+1]); i++; } else if (strcmp(argv[i], "-f") == 0) { fileName = (char*)argv[i+1]; i++; } else if (strcmp(argv[i], "-i") == 0) { inputCount = atoi(argv[i+1]); i++; } else if (strcmp(argv[i], "-o") == 0) { outputCount = atoi(argv[i+1]); i++; } else if (strcmp(argv[i], "-h") == 0) { hiddenCount = atoi(argv[i+1]); i++; } else { fprintf(stderr,"unknown argument: %s\n",argv[i]); exit(-1); } } /* build a network, with TIME number of time ticks */ net=create_net(TIME); /* learning rate */ default_epsilon=epsilon; /* create our groups. format is: name, num of units, ticks */ input=init_group("Input",inputCount,TIME); hidden=init_group("hidden",hiddenCount,TIME); output=init_group("Output",outputCount,TIME); /* bias is special. format is: value, ticks */ bias=init_bias(1.0,TIME); /* now add our groups to the network object */ bind_group_to_net(net,input); bind_group_to_net(net,hidden); bind_group_to_net(net,output); bind_group_to_net(net,bias); /* now connect our groups, instantiating */ /* connection objects c1 through c4 */ c1=connect_groups(input,hidden); c2=connect_groups(hidden,output); c3=connect_groups(bias,hidden); c4=connect_groups(bias,output); /* add connections to our network */ bind_connection_to_net(net,c1); bind_connection_to_net(net,c2); bind_connection_to_net(net,c3); bind_connection_to_net(net,c4); /* randomize the weights in the connection objects. 2nd argument is weight range. */ randomize_connections(c1,range); randomize_connections(c2,range); randomize_connections(c3,range); randomize_connections(c4,range); /* how to load and save weights */ /* load_weights(net,"init.weights"); */ /* erase old initial weight file */ /* system("rm -f init.weights.Z"); */ /* save out our weights to file 'init.weights' */ /* save_weights(net,"init.weights"); */ /* load in our example set */ fprintf(stderr, "Reading %s\n",fileName); examples=load_examples(fileName,TIME); fprint(stderr, "Input: %d, Output: %d",); error=0.0; count=0; /* loop for ITER number of times */ for(i=0;i<ITER;i++) { /* get j'th example from exampleset */ ex=get_random_example(examples); /* do forward propagation */ bptt_forward(net,ex); /* backward pass: compute gradients */ bptt_compute_gradients(net,ex); /* sum up error for this example */ error+=compute_error(net,ex); /* online learning: apply the deltas from previous call to compute_gradients */ bptt_apply_deltas(net); /* is it time to write status? */ if (count==REP) { /* average error over last 'count' iterations */ error = error/(float)count; count=0; /* print a message about average error so far */ fprintf(stderr, "%d\t%f\n",i,error); if (error < 0.01) { break; } /* zero error; start counting again */ error=0.0; } count++; } /* done training. write out results for each example */ correct = 0; for(i=0;i<examples->numExamples;i++) { ex=&examples->examples[i]; bptt_forward(net,ex); int maxj = -1; Real maxx = 0; for(j=0 ; j < outputCount; j++){ if (output->outputs[TIME-1][j] > maxx){ maxj = j; maxx = output->outputs[TIME-1][j]; } /* printf("%d:%f ",j,output->outputs[TIME-1][j]); */ } /* printf("max:%d\n",maxj); */ if (get_value(ex->targets,output->index,TIME-1,maxj) == 1) correct += 1; printf("i:%d g:%f cor:%f\n",i, get_value(ex->targets,output->index,TIME-1,maxj),correct / (i+1)); /* printf("example %d\tinputs %f\t%f\toutput %f\ttarget %f\n", */ /* i, */ /* get_value(ex->inputs,input->index,TIME-1,0), */ /* get_value(ex->inputs,input->index,TIME-1,1), */ /* output->outputs[TIME-1][0], */ /* get_value(ex->targets,output->index,TIME-1,0)); */ } fprintf(stderr,"Acc:%f\n", correct / examples->numExamples); return 0; }
void testnd_in_place(int rank, int *n, fftwnd_plan validated_plan, int alternate_api, int specific) { int istride, ostride, howmany; int N, dim, i, j, k; int nc, nhc, nr; fftw_real *in1, *out3; fftw_complex *in2, *out1, *out2; fftwnd_plan p, ip; int flags = measure_flag | wisdom_flag | FFTW_IN_PLACE; if (coinflip()) flags |= FFTW_THREADSAFE; N = nc = nr = nhc = 1; for (dim = 0; dim < rank; ++dim) N *= n[dim]; if (rank > 0) { nr = n[rank - 1]; nc = N / nr; nhc = nr / 2 + 1; } in1 = (fftw_real *) fftw_malloc(2 * nhc * nc * MAX_STRIDE * sizeof(fftw_real)); out3 = in1; out1 = (fftw_complex *) in1; in2 = (fftw_complex *) fftw_malloc(N * sizeof(fftw_complex)); out2 = (fftw_complex *) fftw_malloc(N * sizeof(fftw_complex)); if (alternate_api && specific && (rank == 2 || rank == 3)) { if (rank == 2) { p = rfftw2d_create_plan_specific(n[0], n[1], FFTW_REAL_TO_COMPLEX, flags, in1, MAX_STRIDE, 0, 0); ip = rfftw2d_create_plan_specific(n[0], n[1], FFTW_COMPLEX_TO_REAL, flags, in1, MAX_STRIDE, 0, 0); } else { p = rfftw3d_create_plan_specific(n[0], n[1], n[2], FFTW_REAL_TO_COMPLEX, flags, in1, MAX_STRIDE, 0, 0); ip = rfftw3d_create_plan_specific(n[0], n[1], n[2], FFTW_COMPLEX_TO_REAL, flags, in1, MAX_STRIDE, 0, 0); } } else if (specific) { p = rfftwnd_create_plan_specific(rank, n, FFTW_REAL_TO_COMPLEX, flags, in1, MAX_STRIDE, in1, MAX_STRIDE); ip = rfftwnd_create_plan_specific(rank, n, FFTW_COMPLEX_TO_REAL, flags, in1, MAX_STRIDE, in1, MAX_STRIDE); } else if (alternate_api && (rank == 2 || rank == 3)) { if (rank == 2) { p = rfftw2d_create_plan(n[0], n[1], FFTW_REAL_TO_COMPLEX, flags); ip = rfftw2d_create_plan(n[0], n[1], FFTW_COMPLEX_TO_REAL, flags); } else { p = rfftw3d_create_plan(n[0], n[1], n[2], FFTW_REAL_TO_COMPLEX, flags); ip = rfftw3d_create_plan(n[0], n[1], n[2], FFTW_COMPLEX_TO_REAL, flags); } } else { p = rfftwnd_create_plan(rank, n, FFTW_REAL_TO_COMPLEX, flags); ip = rfftwnd_create_plan(rank, n, FFTW_COMPLEX_TO_REAL, flags); } CHECK(p != NULL && ip != NULL, "can't create plan"); for (i = 0; i < nc * nhc * 2 * MAX_STRIDE; ++i) out3[i] = 0; for (istride = 1; istride <= MAX_STRIDE; ++istride) { /* generate random inputs */ for (i = 0; i < nc; ++i) for (j = 0; j < nr; ++j) { c_re(in2[i * nr + j]) = DRAND(); c_im(in2[i * nr + j]) = 0.0; for (k = 0; k < istride; ++k) in1[(i * nhc * 2 + j) * istride + k] = c_re(in2[i * nr + j]); } fftwnd(validated_plan, 1, in2, 1, 1, out2, 1, 1); howmany = ostride = istride; WHEN_VERBOSE(2, printf("\n testing in-place stride %d...", istride)); if (howmany != 1 || istride != 1 || ostride != 1 || coinflip()) rfftwnd_real_to_complex(p, howmany, in1, istride, 1, out1, ostride, 1); else rfftwnd_one_real_to_complex(p, in1, NULL); for (i = 0; i < nc; ++i) for (k = 0; k < howmany; ++k) CHECK(compute_error_complex(out1 + i * nhc * ostride + k, ostride, out2 + i * nr, 1, nhc) < TOLERANCE, "in-place (r2c): wrong answer"); if (howmany != 1 || istride != 1 || ostride != 1 || coinflip()) rfftwnd_complex_to_real(ip, howmany, out1, ostride, 1, out3, istride, 1); else rfftwnd_one_complex_to_real(ip, out1, NULL); for (i = 0; i < nc * nhc * 2 * istride; ++i) out3[i] *= 1.0 / N; for (i = 0; i < nc; ++i) for (k = 0; k < howmany; ++k) CHECK(compute_error(out3 + i * nhc * 2 * istride + k, istride, (fftw_real *) (in2 + i * nr), 2, nr) < TOLERANCE, "in-place (c2r): wrong answer (check 2)"); } rfftwnd_destroy_plan(p); rfftwnd_destroy_plan(ip); fftw_free(out2); fftw_free(in2); fftw_free(in1); }
int main(int argc,char *argv[]) { char cmd[255]; int hcount=0,scount=0; float herr=0,serr=0,dice; Example *ex; int do_cg=0; float epsilon; int seed=1,first=1; float e,e0,lr,lrPrev; int start=1,i,j; char loadFile[255],fn[255]; setbuf(stdout,NULL); parallel_init(&argc,&argv); if (myid==0) { announce_version(); system("hostname"); } printf("pid %d\n",getpid()); loadFile[0]=0; /* what are the command line arguments? */ for(i=1;i<argc;i++) { if (strcmp(argv[i],"-seed")==0) { seed=atoi(argv[i+1]); i++; } else if (strcmp(argv[i],"-start")==0) { start=atoi(argv[i+1]); i++; } else if (strncmp(argv[i],"-epsilon",5)==0) { epsilon=atof(argv[i+1]); i++; } else if (strncmp(argv[i],"-load",5)==0) { strcpy(loadFile,argv[i+1]); i++; } else { fprintf(stderr,"unknown argument: %s\n",argv[i]); exit(1); } } default_epsilon=0.05; mikenet_set_seed(seed); build_hearing_model(SAMPLES); /* load in our example set */ phono_examples=load_examples("phono.pat",TICKS); sem_examples=load_examples("sem.pat",TICKS); hearing_examples=load_examples("ps.pat",TICKS); speaking_examples=load_examples("sp.pat",TICKS); phono_examples->numExamples=500; sem_examples->numExamples=500; hearing_examples->numExamples=500; speaking_examples->numExamples=500; myid=parallel_proc_id(); #ifdef DO_ONLINE_PRE_TRAIN if (start==1) { for(i=1;i<=10000;i++) { dice = mikenet_random(); if (dice <=0.2) { ex=get_random_example(phono_examples); crbp_forward(phono,ex); crbp_compute_gradients(phono,ex); crbp_apply_deltas(phono); } else if (dice <= 0.5) { ex=get_random_example(hearing_examples); crbp_forward(ps,ex); crbp_compute_gradients(ps,ex); herr += compute_error(ps,ex); crbp_apply_deltas(ps); } else if (dice <= 0.7) { ex=get_random_example(sem_examples); crbp_forward(sem,ex); crbp_compute_gradients(sem,ex); crbp_apply_deltas(sem); } else { ex=get_random_example(speaking_examples); crbp_forward(sp,ex); crbp_compute_gradients(sp,ex); serr+=compute_error(sp,ex); crbp_apply_deltas(sp); } if (i % 100 == 0) { printf("%d hear %f speak %f\n",i, herr/hcount,serr/scount); herr=0.0; serr=0.0; hcount=0; scount=0; } } sprintf(fn,"s%d_online_weights",seed); save_weights(hearing,fn); } #endif parallel_broadcast_weights(hearing); do_cg=1; if (do_cg && myid==0) printf("USING CG\n"); /* loop for ITER number of times */ for(i=1;i<5;i++) { parallel_sync(); store_all_weights(hearing); e=parallel_g(ps,hearing_examples,0.8) + parallel_g(sp,speaking_examples,0.8) + parallel_g(sem,sem_examples,0.2) + parallel_g(phono,phono_examples,0.2); if(do_cg) { if (first) init_cg(hearing); else cg(hearing); first=0; } e0=e; if (myid==0) printf("%d e0: %f\n",i,e); lr = 0.2/hearing_examples->numExamples; lrPrev=lr/10; for(j=1;j<10;j++) { e=sample(lr); if (myid==0) printf("\t\t%d %f %f\n",j,e,lr); if (e>e0) { if (myid==0) test_step(hearing,lrPrev); parallel_broadcast_weights(hearing); break; } e0=e; lrPrev=lr; lr *= 1.7; } zero_gradients(hearing); if (i % 5==0 && myid==0) { sprintf(fn,"s%d_%d_weights",seed,i); save_weights(hearing,fn); } } parallel_finish(); return 0; }
int main(int argc, char ** argv) { MPI_Init(&argc, &argv); int P, rank; MPI_Comm_size(MPI_COMM_WORLD, &P); // size = number of processes nprocs MPI_Comm_rank(MPI_COMM_WORLD, &rank); // Numbering of process if (argc > 1) { // Only one process needs to print usage output if (rank == 0) { printf("Argument k needed.\n"); } MPI_Finalize (); return 1; } double start = MPI_Wtime(); int k = 14; int n = (int) pow(2, k); int np = (int) (n - n%P)/P; double *v, S = 0.0; MPI_Status status; int i; if ( rank == 0 ) { v = calloc(n, sizeof(double)); compute_v(n, v); for (i = 1; i < P; i++ ){ MPI_Send( &v[np*i], np, MPI_DOUBLE, i, 0, MPI_COMM_WORLD); } double S_n = compute_S(np, v); MPI_Reduce(&S_n, &S, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD); double err = compute_error(S); printf("Error = %1.16f\n", err); free(v); } else { double *v_n; if (rank == P-1) { v_n = calloc(np + n%P, sizeof(double)); } else { v_n = calloc(np, sizeof(double)); } MPI_Recv(v_n, np, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, &status); double S_n = compute_S(np, v_n); free(v_n); MPI_Reduce(&S_n, &S, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD); } double end = MPI_Wtime(); if (rank == 0) printf("Elapsed time: %.16f seconds\n", end-start); MPI_Finalize(); return 0; }
/* Function kmeans. * Run kmeans clustering on a set of input descriptors. * * Inputs: * n : number of input descriptors * dim : dimension of each input descriptor * k : number of means to compute * restarts : number of random restarts to perform * v : array of pointers to dim-dimensional descriptors * * Output: * means : array of output means. The means should be * concatenated into one long array of length k*dim. * clustering : assignment of descriptors to means (should * range between 0 and k-1), stored as an array of * length n. clustering[i] contains the * cluster ID for point i */ double kmeans(int n, int dim, int k, int restarts, unsigned char **v, double *means, unsigned int *clustering) { int i; double min_error = DBL_MAX; double *means_curr, *means_new, *work; int *starts; unsigned int *clustering_curr; double changed_pct_threshold = 0.05; // 0.005; if (n <= k) { printf("[kmeans] Error: n <= k\n"); return -1; } means_curr = (double *) malloc(sizeof(double) * dim * k); means_new = (double *) malloc(sizeof(double) * dim * k); clustering_curr = (unsigned int *) malloc(sizeof(unsigned int) * n); starts = (int *) malloc(sizeof(int) * k); work = (double *) malloc(sizeof(double) * dim); if (means_curr == NULL) { printf("[kmeans] Error allocating means_curr\n"); exit(-1); } if (means_new == NULL) { printf("[kmeans] Error allocating means_new\n"); exit(-1); } if (clustering_curr == NULL) { printf("[kmeans] Error allocating clustering_curr\n"); exit(-1); } if (starts == NULL) { printf("[kmeans] Error allocating starts\n"); exit(-1); } if (work == NULL) { printf("[kmeans] Error allocating work\n"); exit(-1); } for (i = 0; i < restarts; i++) { int j; double max_change = 0.0; double error = 0.0; int round = 0; choose(n, k, starts); for (j = 0; j < k; j++) { fill_vector(means_curr + j * dim, v[starts[j]], dim); } /* Compute new assignments */ timeval start, stop; gettimeofday(&start, NULL); int changed = 0; changed = compute_clustering_kd_tree(n, dim, k, v, means_curr, clustering_curr, error); double changed_pct = (double) changed / n; do { gettimeofday(&stop, NULL); long seconds = stop.tv_sec - start.tv_sec; long useconds = stop.tv_usec - start.tv_usec; double etime = seconds + useconds * 0.000001; printf("Round %d: changed: %d\n", i, changed); printf("Round took %0.3lfs\n", etime); fflush(stdout); gettimeofday(&start, NULL); /* Recompute means */ max_change = compute_means(n, dim, k, v, clustering_curr, means_new); memcpy(means_curr, means_new, sizeof(double) * dim * k); /* Compute new assignments */ changed = compute_clustering_kd_tree(n, dim, k, v, means_curr, clustering_curr, error); changed_pct = (double) changed / n; round++; } while (changed_pct > changed_pct_threshold); max_change = compute_means(n, dim, k, v, clustering_curr, means_new); memcpy(means_curr, means_new, sizeof(double) * dim * k); if (error < min_error) { min_error = error; memcpy(means, means_curr, sizeof(double) * k * dim); memcpy(clustering, clustering_curr, sizeof(unsigned int) * n); } } free(means_curr); free(means_new); free(clustering_curr); free(starts); free(work); return compute_error(n, dim, k, v, means, clustering); }
// Computes the Euler spiral for the given params //if the global lookup table is available, it looks up the ES params first and then optimizes them //this should dramatically cut down in the time to optimize void EulerSpiral::compute_es_params () { //compute scaling distance double d = euc_distance(params.start_pt, params.end_pt); params.psi = angle0To2Pi(atan2(params.end_pt.y()-params.start_pt.y(),params.end_pt.x()-params.start_pt.x())); //degeneracy check if (d<eError) return; //first compute a biarc estimate _bi_arc_estimate.set_start_params(params.start_pt, params.start_angle); _bi_arc_estimate.set_end_params(params.end_pt, params.end_angle); _bi_arc_estimate.compute_biarc_params(); //get the total turning angle::This is an important parameter because //it defines the one solution out of many possible solutions params.turningAngle = _bi_arc_estimate.params.K1*_bi_arc_estimate.params.L1 + _bi_arc_estimate.params.K2*_bi_arc_estimate.params.L2; //From here on, normlize the parameters and use these to perform the optimization double k0_init_est = _bi_arc_estimate.params.K1*d; double L_init_est = _bi_arc_estimate.params.L()/d; double dstep = 0.1; //Alternately, we can get the initial values from the lookup table and perform //the optimization from there //double k0_init_est = globalEulerSpiralLookupTable->get_globalEulerSpiralLookupTable()->k0(CCW(params.psi, params.start_angle), CCW(params.psi, params.end_angle)); //double L_init_est = globalEulerSpiralLookupTable->get_globalEulerSpiralLookupTable()->L(CCW(params.psi, params.start_angle), CCW(params.psi, params.end_angle)); //double dstep = globalEulerSpiralLookupTable->get_globalEulerSpiralLookupTable()->dt()/4; //then perform a simple gradient descent to find the real solution double error = compute_error(k0_init_est, L_init_est); double prev_error = error; double k0 = k0_init_est; double L = L_init_est; double e1, e2, e3, e4 = 0; for (int i=0; i<MAX_NUM_ITERATIONS; i++) { if (error<eError) break; e1 = compute_error(k0 + dstep, L); e2 = compute_error(k0 - dstep, L); e3 = compute_error(k0, L + dstep); if (L>dstep) e4 = compute_error(k0, L - dstep); error = MIN2(MIN2(e1,e2),MIN2(e3,e4)); if (error>prev_error) { dstep = dstep/2; continue; } if (error==e1) k0 = k0 + dstep; else if (error==e2) k0 = k0 - dstep; else if (error==e3) L = L + dstep; else if (error==e4) L = L - dstep; prev_error = error; } //store the parameters params.K0 = k0/d; params.L = L*d; params.gamma = 2*(params.turningAngle - k0*L)/(L*L)/(d*d); params.K2 = (k0 + params.gamma*L)/d; params.error = error; }
int main(int argc, char ** argv) { MPI_Init(&argc, &argv); int P, rank; MPI_Comm_size(MPI_COMM_WORLD, &P); // P = number of processes MPI_Comm_rank(MPI_COMM_WORLD, &rank); // Rank = numbering of each process if (isPowerOfTwo(P) == false) { if (rank == 0) { printf("The number of processes is not a power of two \n"); } MPI_Finalize(); return 1; } if (argc < 2) { if (rank == 0) { printf("Usage: ex3 k, n=pow(2,k)\n"); printf("n = vector/matrix size\n"); } MPI_Finalize (); return 1; } int k = atoi(argv[1]); int n = (int) pow(2, k); int np = (int) (n - n%P)/P; double *v, S = 0.0; MPI_Status status; int i; if ( rank == 0 ) { v = calloc(n, sizeof(double)); compute_v(n, v); for (i = 1; i < P; i++ ){ MPI_Send(&v[np*i], np, MPI_DOUBLE, i, 0, MPI_COMM_WORLD); } double S_n = compute_S(np, v); MPI_Reduce(&S_n, &S, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD); printf("S = %1.16f\n", S); double err = compute_error(S); printf("Error = %1.16f\n", err); free(v); } else { double *v_n; if (rank == P-1) { v_n = calloc(np + n%P, sizeof(double)); } else { v_n = calloc(np, sizeof(double)); } MPI_Recv(v_n, np, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, &status); double S_n = compute_S(np, v_n); free(v_n); MPI_Reduce(&S_n, &S, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD); } MPI_Finalize(); return 0; }
void array_compare(fftw_real * A, fftw_real * B, int n) { CHECK(compute_error(A, 1, B, 1, n) < TOLERANCE, "failure in RFFTW verification"); }
int main() { DDRB |= _BV(PB0); DDRD = 0x00; setup_motors(); int change_pwm = 0; int prev_error = 0; int error = 0; int p = 0,i = 0,d = 0; while(1) { prev_error = error; error = compute_error(); if(check_all()) { for(int x = 0; x < 4; x++) { errors[x] = errors[x+1]; } errors[4] = error; } toggle_led(); if(!check_all()) { int r_count = 0, l_count = 0; for(int i = 0; i < 5; i++) { if(errors[i] < 0) l_count++; if(errors[i] > 0) r_count++; } if(l_count > r_count) motors_left(); else motors_right(); OCR1A = 255; OCR1B = 255; } else { p = error; change_pwm = kp*p; int right = tp + change_pwm; int left = tp - change_pwm; if(right > 255) right = 255; if(left > 255) left = 255; motors_straight(); OCR1A = right; OCR1B = left; } _delay_ms(5); } }
void test_in_place(int n, int istride, int howmany, fftw_direction dir, fftw_plan validated_plan, int specific) { fftw_complex *in2, *out2; fftw_real *in1, *out1, *out3; fftw_plan plan; int i, j; int ostride = istride; int flags = measure_flag | wisdom_flag | FFTW_IN_PLACE; if (coinflip()) flags |= FFTW_THREADSAFE; in1 = (fftw_real *) fftw_malloc(istride * n * sizeof(fftw_real) * howmany); in2 = (fftw_complex *) fftw_malloc(n * sizeof(fftw_complex)); out1 = in1; out2 = (fftw_complex *) fftw_malloc(n * sizeof(fftw_complex)); out3 = (fftw_real *) fftw_malloc(n * sizeof(fftw_real)); if (!specific) plan = rfftw_create_plan(n, dir, flags); else plan = rfftw_create_plan_specific(n, dir, flags, in1, istride, out1, ostride); CHECK(plan != NULL, "can't create plan"); /* generate random inputs */ fill_random(in1, n, istride); for (j = 1; j < howmany; ++j) for (i = 0; i < n; ++i) in1[(j * n + i) * istride] = in1[i * istride]; /* copy random inputs to complex array for comparison with fftw: */ if (dir == FFTW_REAL_TO_COMPLEX) for (i = 0; i < n; ++i) { c_re(in2[i]) = in1[i * istride]; c_im(in2[i]) = 0.0; } else { int n2 = (n + 1) / 2; c_re(in2[0]) = in1[0]; c_im(in2[0]) = 0.0; for (i = 1; i < n2; ++i) { c_re(in2[i]) = in1[i * istride]; c_im(in2[i]) = in1[(n - i) * istride]; } if (n2 * 2 == n) { c_re(in2[n2]) = in1[n2 * istride]; c_im(in2[n2]) = 0.0; ++i; } for (; i < n; ++i) { c_re(in2[i]) = c_re(in2[n - i]); c_im(in2[i]) = -c_im(in2[n - i]); } } /* * fill in other positions of the array, to make sure that * rfftw doesn't overwrite them */ for (j = 1; j < istride; ++j) for (i = 0; i < n * howmany; ++i) in1[i * istride + j] = i * istride + j; WHEN_VERBOSE(2, rfftw_print_plan(plan)); /* fft-ize */ if (howmany != 1 || istride != 1 || coinflip()) rfftw(plan, howmany, in1, istride, n * istride, 0, 0, 0); else rfftw_one(plan, in1, NULL); rfftw_destroy_plan(plan); /* check for overwriting */ for (j = 1; j < ostride; ++j) for (i = 0; i < n * howmany; ++i) CHECK(out1[i * ostride + j] == i * ostride + j, "output has been overwritten"); fftw(validated_plan, 1, in2, 1, n, out2, 1, n); if (dir == FFTW_REAL_TO_COMPLEX) { int n2 = (n + 1) / 2; out3[0] = c_re(out2[0]); for (i = 1; i < n2; ++i) { out3[i] = c_re(out2[i]); out3[n - i] = c_im(out2[i]); } if (n2 * 2 == n) out3[n2] = c_re(out2[n2]); } else { for (i = 0; i < n; ++i) out3[i] = c_re(out2[i]); } for (j = 0; j < howmany; ++j) CHECK(compute_error(out1 + j * n * ostride, ostride, out3, 1, n) < TOLERANCE, "test_in_place: wrong answer"); WHEN_VERBOSE(2, printf("OK\n")); fftw_free(in1); fftw_free(in2); fftw_free(out2); fftw_free(out3); }
int CybCamCalibration::calibration(CvSeq* image_points_seq, CvSize img_size, CvSize board_size, float square_size, float aspect_ratio, int flags, CvMat* camera_matrix, CvMat* dist_coeffs, CvMat** extr_params, CvMat** reproj_errs, double* avg_reproj_err) { int code; int image_count = image_points_seq->total; int point_count = board_size.width*board_size.height; CvMat* image_points = cvCreateMat( 1, image_count*point_count, CV_32FC2); CvMat* object_points = cvCreateMat( 1, image_count*point_count, CV_32FC3); CvMat* point_counts = cvCreateMat( 1, image_count, CV_32SC1); CvMat rot_vects, trans_vects; int i, j, k; CvSeqReader reader; cvStartReadSeq(image_points_seq, &reader); // initialize arrays of points for (i = 0; i < image_count; i++) { CvPoint2D32f* src_img_pt = (CvPoint2D32f*)reader.ptr; CvPoint2D32f* dst_img_pt = ((CvPoint2D32f*)image_points->data.fl) + i *point_count; CvPoint3D32f* obj_pt = ((CvPoint3D32f*)object_points->data.fl) + i *point_count; for (j = 0; j < board_size.height; j++) for (k = 0; k < board_size.width; k++) { *obj_pt++ = cvPoint3D32f(j*square_size, k*square_size, 0); *dst_img_pt++ = *src_img_pt++; } CV_NEXT_SEQ_ELEM( image_points_seq->elem_size, reader ); } cvSet(point_counts, cvScalar(point_count) ); *extr_params = cvCreateMat(image_count, 6, CV_32FC1); cvGetCols( *extr_params, &rot_vects, 0, 3); cvGetCols( *extr_params, &trans_vects, 3, 6); cvZero( camera_matrix ); cvZero( dist_coeffs ); if (flags & CV_CALIB_FIX_ASPECT_RATIO) { camera_matrix->data.db[0] = aspect_ratio; camera_matrix->data.db[4] = 1.; } cvCalibrateCamera2(object_points, image_points, point_counts, img_size, camera_matrix, dist_coeffs, &rot_vects, &trans_vects, flags); code = cvCheckArr(camera_matrix, CV_CHECK_QUIET) && cvCheckArr(dist_coeffs, CV_CHECK_QUIET) && cvCheckArr( *extr_params, CV_CHECK_QUIET); *reproj_errs = cvCreateMat( 1, image_count, CV_64FC1); *avg_reproj_err = compute_error(object_points, &rot_vects, &trans_vects, camera_matrix, dist_coeffs, image_points, point_counts, *reproj_errs); cvReleaseMat( &object_points); cvReleaseMat( &image_points); cvReleaseMat( &point_counts); return code; }
// level_refine {{{ int level_refine(int level,parameter &par,boost::shared_ptr<std::vector<id_type> > &result_data, double time) { int rc; int ghostwidth = par->ghostwidth; double ethreshold = par->ethreshold; double minx0 = par->minx0; double maxx0 = par->maxx0; double h = par->h; int refine_factor = 2; // local vars std::vector<int> b_minx,b_maxx; std::vector<int> ddminx,ddmaxx; int maxlevel = par->allowedl; if ( level == maxlevel ) { return 0; } double minx,maxx; double hl; int gi; if ( level == -1 ) { // we're creating the coarse grid minx = minx0; maxx = maxx0; gi = -1; hl = h; } else { // find the bounds of the level rc = level_find_bounds(level,minx,maxx,par); // find the grid index of the beginning of the level gi = level_return_start(level,par); // grid spacing for the level hl = par->gr_h[gi]; } int nxl = (int) ((maxx-minx)/hl+0.5); nxl++; std::vector<double> error,localerror; std::vector<int> flag; error.resize(nxl); flag.resize(nxl); if ( level == -1 ) { int numbox = nxl/par->grain_size; b_minx.resize(numbox); b_maxx.resize(numbox); std::size_t grain_size; grain_size = par->grain_size; for (int i=0;i<numbox;i++) { b_minx[i] = i*grain_size; if ( i == numbox-1 ) { grain_size = nxl - (numbox-1)*par->grain_size; } b_maxx[i] = b_minx[i] + grain_size; if (i == numbox-1 ) { // indicate the last of the level -- no more siblings par->gr_sibling.push_back(-1); } else { par->gr_sibling.push_back(i+1); } int nx = (b_maxx[i] - b_minx[i]); double lminx = minx + b_minx[i]*hl; double lmaxx = lminx + hl*(grain_size-1); par->gr_minx.push_back(lminx); par->gr_maxx.push_back(lmaxx); par->gr_h.push_back(hl); par->gr_lbox.push_back(false); par->gr_rbox.push_back(false); par->gr_left_neighbor.push_back(-1); par->gr_right_neighbor.push_back(-1); par->gr_nx.push_back(nx); } return 0; } else { // loop over all grids in level and get its error data gi = level_return_start(level,par); while ( grid_return_existence(gi,par) ) { int nx = par->gr_nx[gi]; localerror.resize(nx); double lminx = par->gr_minx[gi]; double lmaxx = par->gr_maxx[gi]; rc = compute_error(localerror,nx, lminx, lmaxx, par->gr_h[gi],time,gi,result_data,par); int mini = (int) ((lminx - minx)/hl+0.5); // sanity check if ( floatcmp(lminx-(minx + mini*hl),0.0) == 0 || floatcmp(lmaxx-(minx + (mini+nx-1)*hl),0.0) == 0 ) { std::cerr << " Index problem " << std::endl; std::cerr << " lminx " << lminx << " " << minx + mini*hl << std::endl; std::cerr << " lmaxx " << lminx << " " << minx + (mini+nx-1)*hl << std::endl; exit(0); } rc = level_combine(error,localerror, mini,nxl,nx); gi = par->gr_sibling[gi]; } } level_makeflag_simple(flag,error,nxl,ethreshold); // level_cluster int hgw = (ghostwidth+1)/2; int maxfind = 0; for (std::size_t i=0;i<flag.size();i++) { if ( flag[i] == 1 && maxfind == 0 ) { b_minx.push_back(i); maxfind = 1; } if ( flag[i] == 0 && maxfind == 1 ) { b_maxx.push_back(i-1); maxfind = 0; } } // add in ghostzones for (std::size_t i=0;i<b_maxx.size();i++) { b_minx[i] -= hgw; b_maxx[i] += hgw; } // Determine the domain decomposition for (std::size_t i=0;i<b_maxx.size();i++) { std::size_t grain_size; grain_size = par->grain_size; BOOST_ASSERT(b_maxx[i] > b_minx[i]); std::size_t nx = (b_maxx[i] - b_minx[i])*refine_factor+1; if ( par->grain_size > nx ) { ddminx.push_back(b_minx[i]); ddmaxx.push_back(b_maxx[i]); } else { int numddbox = nx/par->grain_size; // break up b_minx/b_maxx in numddbox portions int tnx = b_maxx[i] - b_minx[i]; int grain_size = tnx/numddbox; for (int j=0;j<numddbox;j++) { ddminx.push_back(j*grain_size + b_minx[i]); if ( j == numddbox-1 ) { grain_size = tnx - (numddbox-1)*grain_size; } ddmaxx.push_back(ddminx[ddminx.size()-1] + grain_size-1); } } } BOOST_ASSERT(ddmaxx.size() == ddminx.size()); int prev_tgi = 0; int tgi = 0; for (std::size_t i=0;i<ddmaxx.size();i++) { int nx = (ddmaxx[i] - ddminx[i])*refine_factor+1; double lminx = minx + ddminx[i]*hl; double lmaxx = minx + ddmaxx[i]*hl; if ( i != 0 ) { prev_tgi = tgi; } // look for matching grid on level tgi = increment_gi(level,nx,lminx,lmaxx,hl,refine_factor,par); if ( i == 0 ) { par->levelp[level+1] = tgi; } if (i == ddmaxx.size()-1) { par->gr_sibling[prev_tgi] = tgi; par->gr_sibling[tgi] = -1; } else if (i != 0 ) { par->gr_sibling[prev_tgi] = tgi; } } return 0; }
int main(int argc,const char *argv[]) { Net *net; Group *input,*hidden,*output,*bias; ExampleSet *examples, *test; Example *ex; Connections *c1,*c2,*c3,*c4; char * fileName = "input"; int i,count,j, dataCount =0, testCount = 0; int dataCountArray[6] = {0}; char * testFileArray[6] = {NULL}; int inputCount = 0,outputCount = 0, hiddenCount = 200; int batchFlag = 0; Real error, correct; Real epsilon,range, hiddenRatio = 0; unsigned long seed = 0; /* don't buffer output */ setbuf(stdout,NULL); /* set seed to unique number */ mikenet_set_seed(seed); /* a default learning rate */ epsilon=0.1; /* default weight range */ range=0.5; default_errorRadius=0.0; const char *usage = "mike_childes [options]\n" "-seed\t\tRandom seed (default to 0)\n" "-i\t\tNumer of input units (default to 0)\n" "-h\t\tNumer of hidden units (default to 200)\n" "-o\t\tNumer of output units (default to 0)\n" "-r\t\tRatio of input units / hidden units.(default to 200)\n" "-b\t\tEnables batch learning (default online learning)\n" "-epsilon\tBack probagation epsilon (Default 0.1)\n" "-help\t\tprints this help\n"; /* what are the command line arguments? */ if (argc == 1) { fprintf(stderr, "%s", usage); exit(1); } for(i=1;i<argc;i++) { if (strcmp(argv[i],"-seed")==0) { seed = atol(argv[i+1]); mikenet_set_seed(seed); i++; } else if (strncmp(argv[i],"-epsilon",5)==0) { epsilon=atof(argv[i+1]); i++; } else if (strcmp(argv[i],"-range")==0) { range=atof(argv[i+1]); i++; } else if (strcmp(argv[i],"-errorRadius")==0) { default_errorRadius=atof(argv[i+1]); i++; } else if (strcmp(argv[i],"-t")==0){ testFileArray[testCount] = (char *)argv[i+1]; testCount++; i++; } else if (strcmp(argv[i],"-d")==0){ dataCountArray[dataCount]= atoi(argv[i+1]); dataCount++; i++; } else if (strcmp(argv[i], "-iter")==0) { ITER = atoi(argv[i+1]); i++; } else if (strcmp(argv[i], "-f") == 0) { fileName = (char*)argv[i+1]; i++; } else if (strcmp(argv[i], "-v") == 0) { VERBOSE = 1; } else if (strcmp(argv[i], "-i") == 0) { inputCount = atoi(argv[i+1]); i++; } else if (strcmp(argv[i], "-o") == 0) { outputCount = atoi(argv[i+1]); i++; } else if (strcmp(argv[i], "-h") == 0) { hiddenCount = atoi(argv[i+1]); i++; } else if (strcmp(argv[i], "-r") == 0) { hiddenRatio = atof(argv[i+1]); i++; } else if (strcmp(argv[i], "-b") == 0) { batchFlag = 1; } else { fprintf(stderr,"unknown argument: %s\n%s\n",argv[i],usage); exit(-1); } } /* build a network, with TIME number of time ticks */ net=create_net(TIME); /* learning rate */ default_epsilon=epsilon; /* hidden ratio */ if (hiddenRatio > 0 && hiddenRatio <= 1) { hiddenCount = (int)inputCount * hiddenRatio; if(VERBOSE) fprintf(stderr, "number of hidden units is set to %d\n",hiddenCount); } else if(hiddenRatio > 1) { fprintf(stderr, "%s", usage); exit(1); } /* First stdout of this code is nummber of hidden units*/ printf("%d\t",hiddenCount); /* create our groups. format is: name, num of units, ticks */ input=init_group("Input",inputCount,TIME); hidden=init_group("hidden",hiddenCount,TIME); output=init_group("Output",outputCount,TIME); /* bias is special. format is: value, ticks */ bias=init_bias(1.0,TIME); /* now add our groups to the network object */ bind_group_to_net(net,input); bind_group_to_net(net,hidden); bind_group_to_net(net,output); bind_group_to_net(net,bias); /* now connect our groups, instantiating */ /* connection objects c1 through c4 */ c1=connect_groups(input,hidden); c2=connect_groups(hidden,output); c3=connect_groups(bias,hidden); c4=connect_groups(bias,output); /* add connections to our network */ bind_connection_to_net(net,c1); bind_connection_to_net(net,c2); bind_connection_to_net(net,c3); bind_connection_to_net(net,c4); /* randomize the weights in the connection objects. 2nd argument is weight range. */ randomize_connections(c1,range); randomize_connections(c2,range); randomize_connections(c3,range); randomize_connections(c4,range); /* how to load and save weights */ /* load_weights(net,"init.weights"); */ /* erase old initial weight file */ /* system("rm -f init.weights.Z"); */ /* save out our weights to file 'init.weights' */ /* load in our example set */ if (VERBOSE){ fprintf(stderr, "Reading %s Iter:%d ",fileName, ITER); for(i=0; i < 6; i++){ if (testFileArray[i] == NULL) break; fprintf(stderr, "TestSet:%s\n", testFileArray[i]); } } examples=load_examples(fileName,TIME); if (VERBOSE){ fprintf(stderr, "size:%d\n", examples->numExamples); for(i=0; i < dataCount; i++){ if (i == 0){ fprintf(stderr, "DataCounts[%d] start:0 end:%d size:%d\n", \ i,dataCountArray[i],dataCountArray[i]); }else{ fprintf(stderr, "DataCounts[%d] start:%d end:%d size:%d\n", \ i,dataCountArray[i - 1], dataCountArray[i], dataCountArray[i] - dataCountArray[i - 1]); } } } error=0.0; count=0; /* loop for ITER number of times */ /* Reset the seed to get same training set*/ fprintf(stderr, "Training: %s size:%d\n", fileName, examples->numExamples); mikenet_set_seed(seed); for(i=0;i<ITER;i++) { /* get j'th example from exampleset */ int ridx = (int) (mikenet_random() * (Real)examples->numExamples); ex = &examples->examples[ridx]; /*Original one*/ // ex = get_random_example(examples); /* do forward propagation */ bptt_forward(net,ex); /* backward pass: compute gradients */ bptt_compute_gradients(net,ex); /* sum up error for this example */ error+=compute_error(net,ex); /* online learning: apply the deltas from previous call to compute_gradients */ if(batchFlag == 0) bptt_apply_deltas(net); /* is it time to write status? */ if (count==REP) { /* average error over last 'count' iterations */ error = error/(float)count; count=0; /* print a message about average error so far */ if (VERBOSE) fprintf(stderr, "%d\t%f\n",i,error); if (error < 0.1) { break; } /* zero error; start counting again */ error=0.0; } count++; } if(batchFlag == 1) bptt_apply_deltas(net); /* done training. write out results for each example */ if (testCount == 0){ correct = 0; dataCount = 0; for(i=0;i<examples->numExamples;i++) { if (VERBOSE && i % 1000 == 0) fprintf(stderr,"."); if (dataCount && i == dataCountArray[dataCount]){ if (dataCount ==0){ printf("%f\t", correct / dataCountArray[dataCount]); }else{ printf("%f\t", correct / (dataCountArray[dataCount] - dataCountArray[dataCount - 1])); } correct = 0; dataCount++; } ex=&examples->examples[i]; bptt_forward(net,ex); int maxj = -1; Real maxx = 0; for(j=0 ; j < outputCount; j++){ if (output->outputs[TIME-1][j] > maxx){ maxj = j; maxx = output->outputs[TIME-1][j]; } /* printf("%d:%f ",j,output->outputs[TIME-1][j]); */ } if (get_value(ex->targets,output->index,TIME-1,maxj) == 1) correct += 1; } printf("%f\n", correct / (dataCountArray[dataCount] - dataCountArray[dataCount - 1])); } else{ int tt = 0, dc = 0; correct = 0; for(tt = 0; tt < testCount; tt++){ test = load_examples(testFileArray[tt],TIME); if (VERBOSE) fprintf(stderr,"Testing:%s size:%d\n",testFileArray[tt],test->numExamples); correct = 0; for(i=0;i<test->numExamples;i++){ if (dataCount && i == dataCountArray[dc]){ if (dc == 0) printf("%f\t", correct / dataCountArray[dc]); else printf("%f\t", correct / (dataCountArray[dc] - dataCountArray[dc - 1])); correct = 0; dc++; } if (VERBOSE && i % 1000 == 0) fprintf(stderr,"."); ex=&test->examples[i]; bptt_forward(net,ex); int maxj = -1; Real maxx = 0; int goldIdx = -1; for(j=0 ; j < outputCount; j++){ if (output->outputs[TIME-1][j] > maxx){ maxj = j; maxx = output->outputs[TIME-1][j]; } if (get_value(ex->targets,output->index,TIME-1,j) == 1) { if(goldIdx != -1) { fprintf(stderr,\ "Multiple active output unit: Instance:%d unit:%d in test set!"\ ,i,j); } goldIdx = j; } /* printf("%d:%f ",j,output->outputs[TIME-1][j]); */ } if (goldIdx != -1 || maxj != -1) { // prints the goldtag and answer tag fprintf(stderr, "%d %d\n", goldIdx, maxj); } else{ fprintf(stderr, "No active output units in test set"); exit(-1); } if (get_value(ex->targets,output->index,TIME-1,maxj) == 1) { correct += 1; } } if (dataCount == 0) printf("%f %d %d\t", correct / test->numExamples, (int)correct, test->numExamples); else printf("%f\n", correct / (dataCountArray[dc] - dataCountArray[dc - 1])); } } return 0; }
static uint64_t compress_latc_block(uint8_t block[16]) { // Just do a simple min/max but choose which of the // two palettes is better uint8_t maxVal = 0; uint8_t minVal = 255; for (int i = 0; i < 16; ++i) { maxVal = SkMax32(maxVal, block[i]); minVal = SkMin32(minVal, block[i]); } // Generate palettes uint8_t palettes[2][8]; // Straight linear ramp palettes[0][0] = maxVal; palettes[0][1] = minVal; for (int i = 1; i < 7; ++i) { palettes[0][i+1] = ((7-i)*maxVal + i*minVal) / 7; } // Smaller linear ramp with min and max byte values at the end. palettes[1][0] = minVal; palettes[1][1] = maxVal; for (int i = 1; i < 5; ++i) { palettes[1][i+1] = ((5-i)*maxVal + i*minVal) / 5; } palettes[1][6] = 0; palettes[1][7] = 255; // Figure out which of the two is better: // - accumError holds the accumulated error for each pixel from // the associated palette // - indices holds the best indices for each palette in the // bottom 48 (16*3) bits. uint32_t accumError[2] = { 0, 0 }; uint64_t indices[2] = { 0, 0 }; for (int i = 15; i >= 0; --i) { // For each palette: // 1. Retreive the result of this pixel // 2. Store the error in accumError // 3. Store the minimum palette index in indices. for (int p = 0; p < 2; ++p) { uint32_t result = compute_error(block[i], palettes[p]); accumError[p] += (result >> 8); indices[p] <<= 3; indices[p] |= result & 7; } } SkASSERT(indices[0] < (static_cast<uint64_t>(1) << 48)); SkASSERT(indices[1] < (static_cast<uint64_t>(1) << 48)); uint8_t paletteIdx = (accumError[0] > accumError[1]) ? 0 : 1; // Assemble the compressed block. uint64_t result = 0; // Jam the first two palette entries into the bottom 16 bits of // a 64 bit integer. Based on the palette that we chose, one will // be larger than the other and it will select the proper palette. result |= static_cast<uint64_t>(palettes[paletteIdx][0]); result |= static_cast<uint64_t>(palettes[paletteIdx][1]) << 8; // Jam the indices into the top 48 bits. result |= indices[paletteIdx] << 16; // We assume everything is little endian, if it's not then make it so. return SkEndian_SwapLE64(result); }
void Gauss_siedel(double *phi, double *mu, double *V) { long i, j, index, index_right, index_front, index_left, index_back; double V_old,A[MESHX*MESHY],B[MESHX*MESHY],C[MESHX*MESHY],E[MESHX*MESHY],F[MESHX*MESHY]; double error; double tol=1.0e-6; for(i=1; i < MESHX-1; i++) { for(j=1; j< MESHY-1; j++) { indx = i*MESHY + j; indx_rght = indx + 1; indx_frnt = indx + MESHY; indx_lft = indx - 1; indx_bck = indx - MESHY; A[index] = (zeta(phi[index_left]) + zeta(phi[index])); //left B[index] = (zeta(phi[index_right]) + zeta(phi[index])); //right C[index] = (zeta(phi[index_back]) + zeta(phi[index])); //back E[index] = (zeta(phi[index_front]) + zeta(phi[index])); //front F[index] = -(A[index] + B[index] + C[index] + E[index]); } } for(;;) { for(i=1; i < MESHX-1; i++) { for(j=1;j < MESHY-1;j++) { index = i*MESHY + j; index_right = index + 1; index_front = index + MESHY; index_left = index - 1; index_back = index - MESHY; if (((i+j)%2) == 0) { V_old = V[index]; V[index] = A[index]*V[index_left] + B[index]*V[index_right] + C[index]*V[index_back] + E[index]*V[index_front]; V[index] /= (-F[index]); } } } for(i=1; i < MESHX-1; i++) { for(j=1;j < MESHY-1; j++) { index = i*MESHY + j; index_right = index + 1; index_front = index + MESHY; index_left = index - 1; index_back = index - MESHY; if (((i+j)%2) != 0) { V_old = V[index]; V[index] = A[index]*V[index_left] + B[index]*V[index_right] + C[index]*V[index_back] + E[index]*V[index_front]; V[index] /= (-F[index]); } } } error = compute_error(A, B, C, E, F, V); if (fabs(error) < tol) { break; } } }
void testnd_out_of_place(int rank, int *n, fftwnd_plan validated_plan) { int istride, ostride; int N, dim, i, j, k; int nc, nhc, nr; fftw_real *in1, *out3; fftw_complex *in2, *out1, *out2; fftwnd_plan p, ip; int flags = measure_flag | wisdom_flag; if (coinflip()) flags |= FFTW_THREADSAFE; N = nc = nr = nhc = 1; for (dim = 0; dim < rank; ++dim) N *= n[dim]; if (rank > 0) { nr = n[rank - 1]; nc = N / nr; nhc = nr / 2 + 1; } in1 = (fftw_real *) fftw_malloc(N * MAX_STRIDE * sizeof(fftw_real)); out3 = (fftw_real *) fftw_malloc(N * MAX_STRIDE * sizeof(fftw_real)); out1 = (fftw_complex *) fftw_malloc(nhc * nc * MAX_STRIDE * sizeof(fftw_complex)); in2 = (fftw_complex *) fftw_malloc(N * sizeof(fftw_complex)); out2 = (fftw_complex *) fftw_malloc(N * sizeof(fftw_complex)); p = rfftwnd_create_plan(rank, n, FFTW_REAL_TO_COMPLEX, flags); ip = rfftwnd_create_plan(rank, n, FFTW_COMPLEX_TO_REAL, flags); CHECK(p != NULL && ip != NULL, "can't create plan"); for (istride = 1; istride <= MAX_STRIDE; ++istride) { /* generate random inputs */ for (i = 0; i < nc; ++i) for (j = 0; j < nr; ++j) { c_re(in2[i * nr + j]) = DRAND(); c_im(in2[i * nr + j]) = 0.0; for (k = 0; k < istride; ++k) in1[(i * nr + j) * istride + k] = c_re(in2[i * nr + j]); } for (i = 0; i < N * istride; ++i) out3[i] = 0.0; fftwnd(validated_plan, 1, in2, 1, 1, out2, 1, 1); for (ostride = 1; ostride <= MAX_STRIDE; ++ostride) { int howmany = (istride < ostride) ? istride : ostride; WHEN_VERBOSE(2, printf("\n testing stride %d/%d...", istride, ostride)); if (howmany != 1 || istride != 1 || ostride != 1 || coinflip()) rfftwnd_real_to_complex(p, howmany, in1, istride, 1, out1, ostride, 1); else rfftwnd_one_real_to_complex(p, in1, out1); for (i = 0; i < nc; ++i) for (k = 0; k < howmany; ++k) CHECK(compute_error_complex(out1 + i * nhc * ostride + k, ostride, out2 + i * nr, 1, nhc) < TOLERANCE, "out-of-place (r2c): wrong answer"); if (howmany != 1 || istride != 1 || ostride != 1 || coinflip()) rfftwnd_complex_to_real(ip, howmany, out1, ostride, 1, out3, istride, 1); else rfftwnd_one_complex_to_real(ip, out1, out3); for (i = 0; i < N * istride; ++i) out3[i] *= 1.0 / N; if (istride == howmany) CHECK(compute_error(out3, 1, in1, 1, N * istride) < TOLERANCE, "out-of-place (c2r): wrong answer"); for (i = 0; i < nc; ++i) for (k = 0; k < howmany; ++k) CHECK(compute_error(out3 + i * nr * istride + k, istride, (fftw_real *) (in2 + i * nr), 2, nr) < TOLERANCE, "out-of-place (c2r): wrong answer (check 2)"); } } rfftwnd_destroy_plan(p); rfftwnd_destroy_plan(ip); fftw_free(out3); fftw_free(out2); fftw_free(in2); fftw_free(out1); fftw_free(in1); }