/* * Get the button's status. */ char AnaButtons::getStatus() { // if we have raised one status and the interval is not over just ignore word tdiff = millis() - lastMillis; if ((lastPressed != NOBUTTON) & (tdiff < intervalMillis)) { return NOBUTTON; } // read the analog pin analogReference(DEFAULT); word value = analogRead(pin); // get the delta averaged amove = lastValue; amove -= value; amove = word(abs(amove)); tdiff = ave(amove); // is the value stable? if (tdiff > THRESHOLD) { // no, it's not lastValue = value; // just return no change return debounce(NOBUTTON); } else { // it's stable, parse the defined values if ((value > 430) & (value < 674)) return debounce(ABUTTON1_PRESS); if ((value > 256) & (value < 421)) return debounce(ABUTTON2_PRESS); if ((value > 140) & (value < 237)) return debounce(ABUTTON3_PRESS); if ((value > 75) & (value < 122)) return debounce(ABUTTON4_PRESS); if ((value > 36) & (value < 60)) return debounce(ABUTTON5_PRESS); if (value < 10) return debounce(ABUTTON6_PRESS); // if you get here, you are on a undefined range, // so you get a "no button pressed at all" return debounce(NOBUTTON); } }
int main(int argc, char *argv[]) /* Process command line. */ { optionHash(&argc, argv); if (argc != 2) usage(); col = optionInt("col", col); tableOut = optionExists("tableOut"); noQuartiles = optionExists("noQuartiles"); if (noQuartiles) aveNoQuartiles(argv[1]); else ave(argv[1]); return 0; }
int main(int argc, char *argv[]) { float f; flo ave = average; argv++; f = ave((*(argv[0])-'0'), (*(argv[1])-'0'), (*(argv[2])-'0')); //f = ave((*(argv[0])-'0'), ((argv[0][1])-'0'), ((argv[0][2])-'0')); printf("\nThe average of three number is %f\n", f); print_t fun = print_matrix; printf("\nThe rank of matrix is %s\n",*argv); fun((*(argv[0])-'0')); //(fun)(**(argv)-'0'); return 0; }
void Backpropagation::trainBatch(Mlp& network, MATRIX& trainingInputs, VECTOR& trainingTargets, MATRIX& testingInputs, VECTOR& testingTargets) { VECTOR trainingOutputs (trainingTargets.size(), 0.0); VECTOR meanInput = ave(trainingInputs); while (epoch < maxEpochs) { trainingOutputs = network(trainingInputs); error = sse(trainingOutputs, trainingTargets); getWeightUpdates(network, meanInput, error); applyWeightUpdates(network); ++epoch; } }
void CorrelatedGaussianParameters::DiagonalizePars(gslpp::matrix<double> Corr) { unsigned int size = Pars.size(); if (Corr.size_i() != size || Corr.size_j() != size) throw std::runtime_error("The size of the correlated parameters in " + name + " does not match the size of the correlation matrix!"); Cov = new gslpp::matrix<double>(size, size, 0.); for (unsigned int i = 0; i < size; i++) for (unsigned int j = 0; j < size; j++) (*Cov)(i, j) = Pars.at(i).errg * Corr(i, j) * Pars.at(j).errg; *Cov = Cov->inverse(); gslpp::matrix<gslpp::complex>vv(size, size, 0.); gslpp::vector<gslpp::complex>ee(size, 0.); Cov->eigensystem(vv, ee); v = new gslpp::matrix<double>(size, size, 0.); e = new gslpp::vector<double>(size, 0.); *v = vv.real(); *e = ee.real(); gslpp::vector<double> ave_in(size, 0.); int ind = 0; for(std::vector<ModelParameter>::iterator it = Pars.begin(); it != Pars.end(); it++) { ave_in(ind) = it->ave; ind++; } gslpp::vector<double> ave = v->transpose() * ave_in; for (int i = 0; i < size; i++) { std::stringstream ss; ss << (i+1); std::string namei = name + ss.str(); ModelParameter current(namei,ave(i),1./sqrt((*e)(i)),0.); current.setCgp_name(name); DiagPars.push_back(current); } }
int main() { double x=5; double y=7; double z=10; double xy=ave(x,y); double xz=ave(x,z); printf("%lf\n",ave(xy,xz)); printf("%lf\n",ave(x,(y+z)/2)); printf("%lf\n",ave(x,ave(y,z))); return 0; }
int main() { float stu[M + 3][N + 4]; int i, kc, rs; cover(); //调用软件封面函数 password(); //调用密码检验函数 printf("老师,你好!欢迎使用本系统\n"); printf("请问本学期一共进行了几门考试:"); scanf("%d", &kc); printf("这几门课分别是 :", kc); for (i = 0; i < kc; i++) scanf("%s", course[i]); printf("请输入该班级的学生人数:"); scanf("%d", &rs); printf("请对照学号输入学生姓名:\n"); printf("学号\t姓名\n"); getchar(); for (i = 0; i < rs; i++) { printf("%d\t", i + 1); gets(name[i]); } input(stu, rs, kc); while (1) { i = select(); switch (i) { case 0:compute(stu, rs, kc); break; case 1:show(stu, rs, kc); break; case 2:ave(stu, rs, kc); break; case 3:sort(stu, rs, kc); break; case 4:pass(stu, rs, kc); break; case 5:fail(stu, rs, kc); break; case 6:printf("谢谢使用,再见...\n"); exit(0); } printf("本操作完成,单击任意键继续...\n"); } }
void alignMesh( Polyhedron & poly ) { int num = poly.size_of_vertices(); // initialization here is very important!! Point3 ave( 0.0, 0.0, 0.0 ); for ( Vertex_iterator vi = poly.vertices_begin(); vi != poly.vertices_end(); ++vi ) { ave = ave + ( vi->point() - CGAL::ORIGIN ); } ave = CGAL::ORIGIN + ( ave - CGAL::ORIGIN )/( double )num; unsigned int dim = 3; double * data = new double [ num*dim ]; int nPoints = 0; for ( Vertex_iterator vi = poly.vertices_begin(); vi != poly.vertices_end(); ++vi ) { data[ nPoints * dim + 0 ] = vi->point().x() - ave.x(); data[ nPoints * dim + 1 ] = vi->point().y() - ave.y(); data[ nPoints * dim + 2 ] = vi->point().z() - ave.z(); nPoints++; } assert( nPoints == ( int )num ); /***************************************** analyze the engenstructure of X^T X *****************************************/ /* define the matrix X */ gsl_matrix_view X = gsl_matrix_view_array( data, num, dim ); /* memory reallocation */ // proj = ( double * )realloc( proj, sizeof( double ) * PDIM * num ); /* calculate the covariance matrix B */ gsl_matrix * B = gsl_matrix_alloc( dim, dim ); gsl_blas_dgemm( CblasTrans, CblasNoTrans, 1.0, &(X.matrix), &(X.matrix), 0.0, B ); /* divided by the number of samples */ gsl_matrix_scale( B, 1.0/(double)num ); gsl_vector * eVal = gsl_vector_alloc( dim ); gsl_matrix * eVec = gsl_matrix_alloc( dim, dim ); gsl_eigen_symmv_workspace * w = gsl_eigen_symmv_alloc( dim ); // eigenanalysis of the matrix B gsl_eigen_symmv( B, eVal, eVec, w ); // release the memory of w gsl_eigen_symmv_free( w ); // sort eigenvalues in a descending order gsl_eigen_symmv_sort( eVal, eVec, GSL_EIGEN_SORT_VAL_DESC ); // #ifdef MYDEBUG for ( unsigned int i = 0; i < dim; ++i ) { cerr << "Eigenvalue No. " << i << " = " << gsl_vector_get( eVal, i ) << endl; cerr << "Eigenvector No. " << i << endl; double length = 0.0; for ( unsigned int j = 0; j < dim; ++j ) { cerr << gsl_matrix_get( eVec, i, j ) << " "; length += gsl_matrix_get( eVec, i, j )*gsl_matrix_get( eVec, i, j ); } cerr << " length = " << length << endl; } // #endif // MYDEBUG // 0 1 2, 0 2 1, Transformation3 map; map = Transformation3( gsl_matrix_get(eVec,1,0), gsl_matrix_get(eVec,0,0), gsl_matrix_get(eVec,2,0), gsl_matrix_get(eVec,1,1), gsl_matrix_get(eVec,0,1), gsl_matrix_get(eVec,2,1), gsl_matrix_get(eVec,1,2), gsl_matrix_get(eVec,0,2), gsl_matrix_get(eVec,2,2) ); if ( map.is_odd() ) { cerr << " Transformation matrix reflected" << endl; map = Transformation3( gsl_matrix_get(eVec,1,0), gsl_matrix_get(eVec,0,0), -gsl_matrix_get(eVec,2,0), gsl_matrix_get(eVec,1,1), gsl_matrix_get(eVec,0,1), -gsl_matrix_get(eVec,2,1), gsl_matrix_get(eVec,1,2), gsl_matrix_get(eVec,0,2), -gsl_matrix_get(eVec,2,2) ); } for ( unsigned int i = 0; i < dim; ++i ) { cerr << "| "; for ( unsigned int j = 0; j < dim; ++j ) { cerr << map.cartesian( i, j ) << " "; } cerr << "|" << endl; } transformMesh( poly, map ); return; }
//------------------------------------------------------------------------------ // Align the mesh //------------------------------------------------------------------------------ Vector3 principalAxis( Polyhedron & poly ) { int num = poly.size_of_vertices(); // initialization here is very important!! Point3 ave( 0.0, 0.0, 0.0 ); for ( Vertex_iterator vi = poly.vertices_begin(); vi != poly.vertices_end(); ++vi ) { ave = ave + ( vi->point() - CGAL::ORIGIN ); } ave = CGAL::ORIGIN + ( ave - CGAL::ORIGIN )/( double )num; unsigned int dim = 3; double * data = new double [ num*dim ]; int nPoints = 0; for ( Vertex_iterator vi = poly.vertices_begin(); vi != poly.vertices_end(); ++vi ) { data[ nPoints * dim + 0 ] = vi->point().x() - ave.x(); data[ nPoints * dim + 1 ] = vi->point().y() - ave.y(); data[ nPoints * dim + 2 ] = vi->point().z() - ave.z(); nPoints++; } assert( nPoints == ( int )num ); /***************************************** analyze the engenstructure of X^T X *****************************************/ /* define the matrix X */ gsl_matrix_view X = gsl_matrix_view_array( data, num, dim ); /* memory reallocation */ // proj = ( double * )realloc( proj, sizeof( double ) * PDIM * num ); /* calculate the covariance matrix B */ gsl_matrix * B = gsl_matrix_alloc( dim, dim ); gsl_blas_dgemm( CblasTrans, CblasNoTrans, 1.0, &(X.matrix), &(X.matrix), 0.0, B ); /* divided by the number of samples */ gsl_matrix_scale( B, 1.0/(double)num ); gsl_vector * eVal = gsl_vector_alloc( dim ); gsl_matrix * eVec = gsl_matrix_alloc( dim, dim ); gsl_eigen_symmv_workspace * w = gsl_eigen_symmv_alloc( dim ); // eigenanalysis of the matrix B gsl_eigen_symmv( B, eVal, eVec, w ); // release the memory of w gsl_eigen_symmv_free( w ); // sort eigenvalues in a descending order gsl_eigen_symmv_sort( eVal, eVec, GSL_EIGEN_SORT_VAL_DESC ); #ifdef MYDEBUG for ( unsigned int i = 0; i < dim; ++i ) { cerr << "Eigenvalue No. " << i << " = " << gsl_vector_get( eVal, i ) << endl; cerr << "Eigenvector No. " << i << endl; for ( unsigned int j = 0; j < dim; ++j ) { length += gsl_matrix_get( eVec, i, j )*gsl_matrix_get( eVec, i, j ); } cerr << " length = " << length << endl; } #endif // MYDEBUG Vector3 ref( gsl_matrix_get( eVec, 0, 0 ), gsl_matrix_get( eVec, 0, 1 ), gsl_matrix_get( eVec, 0, 2 ) ); return ref; #ifdef DEBUG gsl_vector_view eachVec = gsl_matrix_column( eigenVec, 0 ); double cosRot = gsl_matrix_get( eigenVec, 0, 1 ); double sinRot = gsl_matrix_get( eigenVec, 1, 1 ); #ifdef DEBUG cerr << " 2nd axis : " << cosRot << " , " << sinRot << endl; #endif // DEBUG Transformation2 rotate( CGAL::ROTATION, -sinRot, cosRot ); for ( unsigned int i = 0; i < subpatch.size(); ++i ) { subpatch[ i ]->triangle() = subpatch[ i ]->triangle().transform( rotate ); } #endif // DEBUG }
int Observable2D::ParseObservable2D(std::string& type, boost::tokenizer<boost::char_separator<char> >* tok, boost::tokenizer<boost::char_separator<char> >::iterator& beg, std::string& infilename, std::ifstream& ifile, int lineNo, int rank) { if (infilename.find("\\/") == std::string::npos) filepath = infilename.substr(0, infilename.find_last_of("\\/") + 1); if (std::distance(tok->begin(), tok->end()) < 12) { setName(*beg); ++beg; if (std::distance(tok->begin(), tok->end()) < 4) { if(rank == 0) throw std::runtime_error("ERROR: lack of information on " + name + " in " + infilename + " at line number" + boost::lexical_cast<std::string>(lineNo)); else sleep (2); } std::string toMCMC = *beg; if (toMCMC.compare("MCMC") == 0) setTMCMC(true); else if (toMCMC.compare("noMCMC") == 0) setTMCMC(false); else { if (rank == 0) throw std::runtime_error("ERROR: wrong MCMC flag in Observable2D" + name + " at line number:" + boost::lexical_cast<std::string>(lineNo) + " of file " + infilename + ".\n"); else sleep(2); } ++beg; setDistr(*beg); if (distr.compare("file") == 0) { if (std::distance(tok->begin(), tok->end()) < 6) { if(rank == 0) throw std::runtime_error("ERROR: lack of information on "+ *beg + " in " + infilename); else sleep (2); } setFilename(filepath + *(++beg)); setHistoname(*(++beg)); } std::vector<double> min(2, 0.); std::vector<double> max(2, 0.); std::vector<double> ave(2, 0.); std::vector<double> errg(2, 0.); std::vector<double> errf(2, 0.); std::vector<std::string> thname(2, ""); std::vector<std::string> label(2, ""); std::vector<std::string> type2D(2, ""); std::string line; size_t pos = 0; boost::char_separator<char> sep(" \t"); for (int i = 0; i < 2; i++) { IsEOF = getline(ifile, line).eof(); if (line.empty() || line.at(0) == '#') { if (rank == 0) throw std::runtime_error("ERROR: no comments or empty lines in Observable2D please! In file " + infilename + " at line number:" + boost::lexical_cast<std::string>(lineNo) + ".\n"); else sleep(2); } lineNo++; boost::tokenizer<boost::char_separator<char> > mytok(line, sep); beg = mytok.begin(); type2D[i] = *beg; if (type2D[i].compare("Observable") != 0 && type2D[i].compare("BinnedObservable") != 0 && type2D[i].compare("FunctionObservable") != 0) { if (rank == 0) throw std::runtime_error("ERROR: in line no." + boost::lexical_cast<std::string>(lineNo) + " of file " + infilename + ", expecting an Observable or BinnedObservable or FunctionObservable type here...\n"); else sleep(2); } ++beg; thname[i] = *beg; ++beg; label[i] = *beg; while ((pos = label[i].find("~", pos)) != std::string::npos) label[i].replace(pos++, 1, " "); ++beg; min[i] = atof((*beg).c_str()); ++beg; max[i] = atof((*beg).c_str()); if (distr.compare("weight") == 0) { ++beg; ave[i] = atof((*beg).c_str()); ++beg; errg[i] = atof((*beg).c_str()); ++beg; errf[i] = atof((*beg).c_str()); if (errg[i] == 0. && errg[i] == 0.) { if (rank == 0) throw std::runtime_error("ERROR: The Gaussian and flat error in weight for " + name + " cannot both be 0. in the " + infilename + " file, line number:" + boost::lexical_cast<std::string>(lineNo) + ".\n"); else sleep(2); } } else if (distr.compare("noweight") == 0 || distr.compare("file") == 0) { if (type2D[i].compare("BinnedObservable") == 0 || type2D[i].compare("FunctionObservable") == 0) { ++beg; ++beg; ++beg; } } else { if (rank == 0) throw std::runtime_error("ERROR: wrong distribution flag in " + name + " in file " + infilename + ".\n"); else sleep(2); } if (type2D[i].compare("BinnedObservable") == 0) { ++beg; bin_min[i] = atof((*beg).c_str()); ++beg; bin_max[i] = atof((*beg).c_str()); } else if (type2D[i].compare("FunctionObservable") == 0) { ++beg; bin_min[i] = atof((*beg).c_str()); ++beg; } } setObsType(type2D[0]); obsType2 = type2D[1]; setThname(thname[0]); thname2 = thname[1]; setLabel(label[0]); label2 = label[1]; setMin(min[0]); min2 = min[1]; setMax(max[0]); max2= max[1]; setAve(ave[0]); ave2 = ave[1]; setErrg(errg[0]); errg2 = errg[1]; setErrf(errf[0]); errf2 = errf[1]; if (distr.compare("file") == 0) { setLikelihoodFromHisto(filename, histoname); if (rank == 0) std::cout << "added input histogram " << filename << "/" << histoname << std::endl; } return lineNo; } else { beg = ParseObservable(type, tok, beg, filepath, filename, rank); ++beg; std::string distr = *beg; if (distr.compare("file") == 0) { if (std::distance(tok->begin(), tok->end()) < 14) { if (rank == 0) throw std::runtime_error("ERROR: lack of information on " + *beg + " in " + infilename + ".\n"); else sleep(2); } setFilename(filepath + *(++beg)); setHistoname(*(++beg)); setLikelihoodFromHisto(filename, histoname); if (rank == 0) std::cout << "added input histogram " << filename << "/" << histoname << std::endl; } else if (distr.compare("noweight") == 0) { } else { if (rank == 0) throw std::runtime_error("ERROR: wrong distribution flag in " + name); else sleep(2); } setDistr(distr); ++beg; thname2 = *beg; ++beg; std::string label = *beg; size_t pos = 0; while ((pos = label.find("~", pos)) != std::string::npos) label.replace(pos, 1, " "); label2 = label; ++beg; min2 = atof((*beg).c_str()); ++beg; max2 = atof((*beg).c_str()); ++beg; if (beg != tok->end()) if (rank == 0) std::cout << "WARNING: unread information in observable2D " << name << std::endl; return lineNo; } }
/* * compute_vm_averages: [exported] * * This function is to be called once a second to calculate average paging * demand and average numbers of free pages for use by the task swapper. * Can also be used to wake up task swapper at desired thresholds. * * NOTE: this function is single-threaded, and requires locking if * ever there are multiple callers. */ void compute_vm_averages(void) { extern unsigned long vm_page_grab_count; long grab_count, pageout_count; int i; ave(vm_page_free_avg, vm_page_free_count, short_avg_interval); ave(vm_page_free_longavg, vm_page_free_count, long_avg_interval); /* * NOTE: the vm_page_grab_count and vm_stat structure are * under control of vm_page_queue_free_lock. We're simply reading * memory here, and the numbers don't depend on each other, so * no lock is taken. */ grab_count = vm_page_grab_count; pageout_count = 0; for (i = 0; i < NCPUS; i++) { pageout_count += vm_stat[i].pageouts; } ave(vm_pageout_rate_avg, pageout_count - last_pageout_count, short_avg_interval); ave(vm_pageout_rate_longavg, pageout_count - last_pageout_count, long_avg_interval); ave(vm_grab_rate_avg, grab_count - last_grab_count, short_avg_interval); last_grab_count = grab_count; last_pageout_count = pageout_count; /* * Adjust swap_{start,stop}_pageout_rate to the paging rate peak. * This is an attempt to find the optimum paging rates at which * to trigger task swapping on or off to regulate paging activity, * depending on the hardware capacity. */ if (vm_pageout_rate_avg > vm_pageout_rate_peakavg) { unsigned int desired_max; vm_pageout_rate_peakavg = vm_pageout_rate_avg; swap_start_pageout_rate = vm_pageout_rate_peakavg * swap_pageout_high_water_mark / 100; swap_stop_pageout_rate = vm_pageout_rate_peakavg * swap_pageout_low_water_mark / 100; } #if TASK_SW_DEBUG /* * For measurements, allow fixed values. */ if (fixed_swap_start_pageout_rate) swap_start_pageout_rate = fixed_swap_start_pageout_rate; if (fixed_swap_stop_pageout_rate) swap_stop_pageout_rate = fixed_swap_stop_pageout_rate; #endif /* TASK_SW_DEBUG */ #if TASK_SW_DEBUG if (task_swap_stats) printf("vm_avgs: pageout_rate: %d %d (on/off: %d/%d); page_free: %d %d (tgt: %d)\n", vm_pageout_rate_avg / AVE_SCALE, vm_pageout_rate_longavg / AVE_SCALE, swap_start_pageout_rate / AVE_SCALE, swap_stop_pageout_rate / AVE_SCALE, vm_page_free_avg / AVE_SCALE, vm_page_free_longavg / AVE_SCALE, vm_page_free_target); #endif /* TASK_SW_DEBUG */ if (vm_page_free_avg / AVE_SCALE <= vm_page_free_target) { if (task_swap_on) { /* The following is a delicate attempt to balance the * need for reasonably rapid response to system * thrashing, with the equally important desire to * prevent the onset of swapping simply because of a * short burst of paging activity. */ if ((vm_pageout_rate_longavg > swap_stop_pageout_rate) && (vm_pageout_rate_avg > swap_start_pageout_rate) || (vm_pageout_rate_avg > vm_pageout_rate_peakavg) || (vm_grab_rate_avg > max_grab_rate)) wake_task_swapper(FALSE); } } else /* page demand is low; should consider swapin */ { if (tasks_swapped_out != 0) wake_task_swapper(TRUE); } }
int main( int argc, char *argv[] ) { int i, j ; float **a1, ans1[ 3 ], ans2[ 5 ] ; FILE *fin ; fin = fopen( "textfile93", "r" ) ; a1 = ( float ** ) malloc( sizeof( float * ) * 3 ) ; for( i = 0 ; i < 3 ; i++ ) { for( j = 0 ; j < 5 ; j++ ) { a1[ i ] = ( float * ) malloc( sizeof( float ) ) ; } } for( i = 0 ; i < 3 ; i++ ) { for( j = 0 ; j < 5 ; j++ ) { fscanf( fin, "%f", &a1[ i ][ j ] ) ; } } printf( "\nThe numbers in the array are.\n" ) ; for( i = 0 ; i < 3 ; i++ ) { for( j = 0 ; j < 5 ; j++ ) { printf( " %.2f", a1[ i ][ j ] ) ; } printf( "\n" ) ; } ave( a1, ans1, ans2 ) ; printf( "\nThe average values for the three rows are.\n" ) ; for( i = 0 ; i < 3 ; i++ ) { printf( " %f", ans1[ i ] ) ; } printf( "\nThe average values for the five columns are.\n" ) ; for( i = 0 ; i < 5 ; i++ ) { printf( " %f ", ans2[ i ] ) ; } fclose( fin ) ; return 0 ; }
Vector mean(const Matrix &m){ int nr = nrow(m); Vector ave(nr, 1.0/nr); Vector ans = ave * m; return ans; }