int main( int ac, char * av[] ) { std::vector< double > smooth, derivative; noise noise( 0.0, 0.05 ); adportable::SGFilter smoother( M, adportable::SGFilter::Smoothing ); adportable::SGFilter d1cubic( M, adportable::SGFilter::Derivative1 ); adportable::SGFilter d1quadratic( M, adportable::SGFilter::Derivative1, adportable::SGFilter::Quadratic ); std::vector<double> waveform; double mean = double(N) / 2; for ( int i = 0; i < N; ++i ) { boost::math::normal_distribution< double > nd( mean, double(N)/16 /* sd */); double y = boost::math::pdf( nd, i ) / boost::math::pdf( nd, mean ) + noise(); waveform.push_back( y ); } double a = waveform[0]; for ( int i = M / 2; i < N - M; ++i ) { double s = smoother( waveform.data() + i ); double dc = d1cubic( waveform.data() + i ); double dq = d1quadratic( waveform.data() + i ); std::cout << i << "\t" << std::fixed << std::setprecision(7) << waveform[i] << "\t" << s << "\t" << dc << "\t" << dq << std::endl; } }
double get_stagger_coef(double t, double stagger_prc, int num_xforms, int this_xform) { /* max_stag is the spacing between xform start times if stagger_prc = 1.0 */ double max_stag = (double)(num_xforms-1)/num_xforms; /* scale the spacing by stagger_prc */ double stag_scaled = stagger_prc * max_stag; /* t ranges from 1 to 0 (the contribution of cp[0] to the blend) */ /* the first line below makes the first xform interpolate first */ /* the second line makes the last xform interpolate first */ double st = stag_scaled * (num_xforms - 1 - this_xform) / (num_xforms-1); // double st = stag_scaled * (this_xform) / (num_xforms-1); double et = st + (1-stag_scaled); // printf("t=%f xf:%d st=%f et=%f : : %f\n",t,this_xform,st,et,smoother((t-st)/(1-stag_scaled))); if (t <= st) return (0); else if (t >= et) return (1); else return ( smoother((t-st)/(1-stag_scaled)) ); }
// Tests interface to Ifpack2's Gauss-Seidel preconditioner. TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(Ifpack2Smoother, HardCodedResult_GaussSeidel, Scalar, LocalOrdinal, GlobalOrdinal, Node) { # include <MueLu_UseShortNames.hpp> MUELU_TESTING_SET_OSTREAM; MUELU_TESTING_LIMIT_EPETRA_SCOPE(Scalar,GlobalOrdinal,Node); MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) { if (Teuchos::ScalarTraits<Scalar>::name().find("complex") != std::string::npos) { out << "Skipping Tpetra for SC type \"complex\"" << std::endl; return; } Teuchos::ParameterList paramList; paramList.set("relaxation: type", "Gauss-Seidel"); paramList.set("relaxation: sweeps", (int) 1); paramList.set("relaxation: damping factor", (double) 1.0); paramList.set("relaxation: zero starting solution", false); Ifpack2Smoother smoother("RELAXATION", paramList); typename Teuchos::ScalarTraits<SC>::magnitudeType residualNorms = testApply_A125_X1_RHS0(smoother, out, success); RCP<const Teuchos::Comm<int> > comm = TestHelpers::Parameters::getDefaultComm(); switch (comm->getSize()) { case 1: case 4: TEST_FLOATING_EQUALITY(residualNorms,5.773502691896257e-01,1e-12); break; default: out << "Pass/Fail is checked only for 1 and 4 processes." << std::endl; break; } // switch } } // GaussSeidel
// Tests interface to Ifpack2's Chebyshev preconditioner TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(Ifpack2Smoother, HardCodedResult_Chebyshev, Scalar, LocalOrdinal, GlobalOrdinal, Node) { # include <MueLu_UseShortNames.hpp> MUELU_TESTING_SET_OSTREAM; MUELU_TESTING_LIMIT_EPETRA_SCOPE(Scalar,GlobalOrdinal,Node); MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) { if (Teuchos::ScalarTraits<Scalar>::name().find("complex") != std::string::npos) { out << "Skipping Tpetra for SC type \"complex\"" << std::endl; return; } Teuchos::ParameterList paramList; paramList.set("chebyshev: degree", (int) 3); paramList.set("chebyshev: max eigenvalue", (double) 1.98476); paramList.set("chebyshev: min eigenvalue", (double) 1.0); paramList.set("chebyshev: ratio eigenvalue", (double) 20); paramList.set("chebyshev: zero starting solution", false); Ifpack2Smoother smoother("CHEBYSHEV",paramList); typename Teuchos::ScalarTraits<SC>::magnitudeType residualNorms = testApply_A125_X1_RHS0(smoother, out, success); TEST_FLOATING_EQUALITY(residualNorms, 5.269156e-01, 1e-7); // Compare to residual reported by ML } } // Chebyshev
void HMM::smoother_all(double* c, double* beta, double* Z) { assert(o != nullptr); for(int iseq = 0; iseq < o->nseq; iseq++) { int si = o->sind[iseq]; // start index of the current sequence smoother(iseq, c + si, beta + si*hs, Z); } }
int SmoothingShader::shade(Stroke &ioStroke) const { // cerr << " Smoothing a stroke " << endl; Smoother smoother(ioStroke); smoother.smooth(_nbIterations, _factorPoint, _factorCurvature, _factorCurvatureDifference, _anisoPoint, _anisoNormal, _anisoCurvature, _carricatureFactor); return 0; }
void HMM::Estep(int iseq, double* phi, double* c, double* beta, double* post, double* N, double* Naux1, double* Naux2, double* M, double* NU, double* loglik) { int si = o->sind[iseq]; // start index of the current sequence int sl = o->slen[iseq]; // sequence length uint32_t* y_ = o->y + si; // pointer to the current sequence filter(iseq, phi, c, Naux1); smoother(iseq, c, beta, Naux1); vmul(phi, 1, beta, 1, post, 1, hs * sl); // beta[:, 1:] = beta[:, 1:]*g[:, np.int_(y[1:])]/np.tile(c[1:], [k, 1]) for(int t = 1; t < sl; t++) { double* slice = beta + t*hs; vmul(slice, 1, g + y_[t], os, slice, 1, hs); vsdiv(slice, 1, c + t, slice, 1, hs); } // a = np.dot(phi[:, 0:-1], beta[:, 1:]) double* A = phi; double* B = beta + hs; double* C = Naux1; cblas_dgemm(CblasColMajor, CblasNoTrans, CblasTrans, hs, hs, sl-1, 1, A, hs, B, hs, 0, C, hs); // a = np.transpose(a) * Q double* D = Naux2; mtrans(C, 1, D, 1, hs, hs); vmul(D, 1, Q, 1, D, 1, hs * hs); // N += a vadd(N, 1, D, 1, N, 1, hs*hs); // for each t, update only the col y_[t], with the expected number of states for(int t = 0; t < sl; t++) { vadd(M + y_[t], os, post + t*hs, 1, M + y_[t], os, hs); } // accumulate the expected number of states at t=0 vadd(NU, 1, post, 1, NU, 1, hs); // loglik (destructive for c) double loglik_; vlog(c, c, &sl); sve(c, 1, &loglik_, sl); *loglik += loglik_; }
// Tests interface to Ifpack2's ILU(0) preconditioner. TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(Ifpack2Smoother, HardCodedResult_ILU, Scalar, LocalOrdinal, GlobalOrdinal, Node) { # include <MueLu_UseShortNames.hpp> MUELU_TESTING_SET_OSTREAM; MUELU_TESTING_LIMIT_EPETRA_SCOPE(Scalar,GlobalOrdinal,Node); MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) { //FIXME this will probably fail in parallel b/c it becomes block Jacobi Teuchos::ParameterList paramList; Ifpack2Smoother smoother("ILUT",paramList); typename Teuchos::ScalarTraits<SC>::magnitudeType residualNorms = testApply_A125_X0_RandomRHS(smoother, out, success); RCP<const Teuchos::Comm<int> > comm = TestHelpers::Parameters::getDefaultComm(); if (comm->getSize() == 1) { TEST_EQUALITY(residualNorms < 1e-10, true); } else { out << "Pass/Fail is only checked in serial." << std::endl; } } } // ILU
// Tests two sweeps of ILUT in Ifpack2 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(Ifpack2Smoother, ILU_TwoSweeps, Scalar, LocalOrdinal, GlobalOrdinal, Node) { # include <MueLu_UseShortNames.hpp> MUELU_TESTING_SET_OSTREAM; MUELU_TESTING_LIMIT_EPETRA_SCOPE(Scalar,GlobalOrdinal,Node); typedef typename Teuchos::ScalarTraits<SC>::magnitudeType magnitude_type; MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) { //FIXME this will probably fail in parallel b/c it becomes block Jacobi Teuchos::ParameterList paramList; Ifpack2Smoother smoother("ILUT",paramList); //I don't use the testApply infrastructure because it has no provision for an initial guess. Teuchos::RCP<Matrix> A = TestHelpers::TestFactory<SC, LO, GO, NO>::Build1DPoisson(125); Level level; TestHelpers::TestFactory<SC,LO,GO,NO>::createSingleLevelHierarchy(level); level.Set("A", A); smoother.Setup(level); RCP<MultiVector> X = MultiVectorFactory::Build(A->getDomainMap(),1); RCP<MultiVector> RHS = MultiVectorFactory::Build(A->getRangeMap(),1); // Random X X->setSeed(846930886); X->randomize(); // Normalize X Array<magnitude_type> norms(1); X->norm2(norms); X->scale(1/norms[0]); // Compute RHS corresponding to X A->apply(*X,*RHS, Teuchos::NO_TRANS,(SC)1.0,(SC)0.0); // Reset X to 0 X->putScalar((SC) 0.0); RHS->norm2(norms); out << "||RHS|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl; out << "solve with zero initial guess" << std::endl; Teuchos::Array<magnitude_type> initialNorms(1); X->norm2(initialNorms); out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl; smoother.Apply(*X, *RHS, true); //zero initial guess Teuchos::Array<magnitude_type> finalNorms(1); X->norm2(finalNorms); Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*A, *X, *RHS); out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl; out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl; out << "solve with random initial guess" << std::endl; X->randomize(); X->norm2(initialNorms); out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl; smoother.Apply(*X, *RHS, false); //nonzero initial guess X->norm2(finalNorms); Teuchos::Array<magnitude_type> residualNorm2 = Utilities::ResidualNorm(*A, *X, *RHS); out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm2[0] << std::endl; out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl; RCP<const Teuchos::Comm<int> > comm = TestHelpers::Parameters::getDefaultComm(); if (comm->getSize() == 1) { //TEST_EQUALITY(residualNorms < 1e-10, true); TEST_EQUALITY(residualNorm1[0] != residualNorm2[0], true); } else { out << "Pass/Fail is only checked in serial." << std::endl; } } } // ILU
/* combine forward/backward solutions and output results ---------------------*/ static void combres(FILE *fp, const prcopt_t *popt, const solopt_t *sopt) { gtime_t time={0}; sol_t sols={{0}},sol={{0}}; double tt,Qf[9],Qb[9],Qs[9],rbs[3]={0},rb[3]={0}; int i,j,k,solstatic,pri[]={0,1,2,3,4,5,1,6}; trace(3,"combres : isolf=%d isolb=%d\n",isolf,isolb); solstatic=sopt->solstatic&& (popt->mode==PMODE_STATIC||popt->mode==PMODE_PPP_STATIC); for (i=0,j=isolb-1;i<isolf&&j>=0;i++,j--) { if ((tt=timediff(solf[i].time,solb[j].time))<-DTTOL) { sols=solf[i]; for (k=0;k<3;k++) rbs[k]=rbf[k+i*3]; j++; } else if (tt>DTTOL) { sols=solb[j]; for (k=0;k<3;k++) rbs[k]=rbb[k+j*3]; i--; } else if (solf[i].stat<solb[j].stat) { sols=solf[i]; for (k=0;k<3;k++) rbs[k]=rbf[k+i*3]; } else if (solf[i].stat>solb[j].stat) { sols=solb[j]; for (k=0;k<3;k++) rbs[k]=rbb[k+j*3]; } else { sols=solf[i]; sols.time=timeadd(sols.time,-tt/2.0); if ((popt->mode==PMODE_KINEMA||popt->mode==PMODE_MOVEB)&& sols.stat==SOLQ_FIX) { /* degrade fix to float if validation failed */ if (!valcomb(solf+i,solb+j)) sols.stat=SOLQ_FLOAT; } for (k=0;k<3;k++) { Qf[k+k*3]=solf[i].qr[k]; Qb[k+k*3]=solb[j].qr[k]; } Qf[1]=Qf[3]=solf[i].qr[3]; Qf[5]=Qf[7]=solf[i].qr[4]; Qf[2]=Qf[6]=solf[i].qr[5]; Qb[1]=Qb[3]=solb[j].qr[3]; Qb[5]=Qb[7]=solb[j].qr[4]; Qb[2]=Qb[6]=solb[j].qr[5]; if (smoother(solf[i].rr,Qf,solb[j].rr,Qb,3,sols.rr,Qs)) continue; sols.qr[0]=(float)Qs[0]; sols.qr[1]=(float)Qs[4]; sols.qr[2]=(float)Qs[8]; sols.qr[3]=(float)Qs[1]; sols.qr[4]=(float)Qs[5]; sols.qr[5]=(float)Qs[2]; } if (!solstatic) { outsol(fp,&sols,rbs,sopt); } else if (time.time==0||pri[sols.stat]<=pri[sol.stat]) { sol=sols; for (k=0;k<3;k++) rb[k]=rbs[k]; if (time.time==0||timediff(sols.time,time)<0.0) { time=sols.time; } } } if (solstatic&&time.time!=0.0) { sol.time=time; outsol(fp,&sol,rb,sopt); } }
Vector& operator()(Vector& x, const RHSVector& b) const { for (std::size_t i= 0; i < N; i++) smoother(x, b); return x; }
int main(int argc, char *argv[]) { init(); dbgoutf("Initialisation terminee! Chargement des planetes..."); bool planets_loaded = false; if(argc > 1 && argv[1] && *argv[1] != '\0') { char *filename = argv[1]; planets_file = newstring(argv[1]); dbgoutf("Lecture du fichier \"%s\"", filename); planets_loaded = load_planets(filename); } if(!planets_loaded) { planets_loaded = load_planets(DEFAULT_PLANETS_FILE); if(!planets_loaded) { dbgoutf("Impossible de charger les planetes. Fermeture du programme."); callback_quit(); return EXIT_FAILURE; } } compute_thread = SDL_CreateThread(compute_thread_func, NULL); SDL_Event event; if(invecrange(planets, 0)) curplanet = planets[0]; render_millis.maxfps = MAXFPS; compute_millis.maxfps = MAXCPS; // main loop for(;;) { render_millis.last = render_millis.current; render_millis.current = SDL_GetTicks(); render_millis.diff = render_millis.current-render_millis.last; SDL_PollEvent(&event); bool pressed = false; switch(event.type) { case SDL_KEYDOWN: { pressed = true; // a key has been pressed switch (event.key.keysym.sym) { case SDLK_ESCAPE: callback_quit(); break; case SDLK_s: start_pause(1); break; case SDLK_p: start_pause(0); break; case SDLK_7: case SDLK_KP7: if(keyreleased) precision /= 1.1; break; case SDLK_8: case SDLK_KP8: if(keyreleased) precision *= 1.1; break; case SDLK_F1: if(keyreleased) smoother(); break; case SDLK_F2: if(keyreleased) harder(); break; case SDLK_o: { if(keyreleased) save_planets(); } break; case SDLK_F4: { if(keyreleased) save_datas(); } break; //case SDLK_F11: { if((keyreleased || 1) && !print_frame) print_frame = true; } break; case SDLK_F12: { char buf[64] = ""; sprintf(buf, "../images/screenshot_%d.bmp", render_millis.current); takeScreenshot(buf); } break; case SDLK_KP_PLUS: if(keyreleased) zoom_in(); break; case SDLK_KP_MINUS: if(keyreleased) zoom_out(); break; case SDLK_KP0: if(keyreleased) skip_hists = !skip_hists; break; case SDLK_F3: if(keyreleased) draw_area = !draw_area; break; case SDLK_q: case SDLK_a: if(keyreleased) draw_accel = !draw_accel; break; case SDLK_f: SDL_WM_ToggleFullScreen(screen); break; case SDLK_4: case SDLK_KP4: if(keyreleased) increase_speed(); break; case SDLK_5: case SDLK_KP5: if(keyreleased) lower_speed(); break; case SDLK_RETURN: if(keyreleased) studied(); break; case SDLK_t: if(keyreleased) curplanet->showhist = !curplanet->showhist; break; case SDLK_1: case SDLK_KP1: if(keyreleased) prev_ref(); break; case SDLK_2: case SDLK_KP2: if(keyreleased) next_ref(); break; case SDLK_RIGHT: { angle.y -= 20.0*double(render_millis.diff)/1000.0; angle.y = angle.y > 360 ? int(angle.y) % 360 : angle.y; } break; case SDLK_LEFT: { angle.y += 20.0*double(render_millis.diff)/1000.0; angle.y = angle.y > 360 ? int(angle.y) % 360 : angle.y; } case SDLK_UP: { angle.z += 20.0*double(render_millis.diff)/1000.0; angle.z = angle.z > 360 ? int(angle.z) % 360 : angle.z; } break; case SDLK_DOWN: { angle.z -= 20.0*double(render_millis.diff)/1000.0; angle.z = angle.z > 360 ? int(angle.z) % 360 : angle.z; } default: { } } } break; case SDL_QUIT: callback_quit(); break; case SDL_VIDEORESIZE: { w = event.resize.w; h = event.resize.h; glViewport(0, 0, w, h); start_pause(0); } case SDL_MOUSEBUTTONDOWN: { if(!mousedown && event.button.button == SDL_BUTTON_LEFT || event.button.button == SDL_BUTTON_RIGHT) mouse_click(event.button.button, event.button.x, h-event.button.y, true); if(event.button.button == SDL_BUTTON_LEFT || event.button.button == SDL_BUTTON_RIGHT) mousedown = true; } break; case SDL_MOUSEBUTTONUP: { if(event.button.button == SDL_BUTTON_LEFT || event.button.button == SDL_BUTTON_RIGHT && mousedown) { mousedown = false; mouse_click(event.button.button, event.button.x, h-event.button.y, false); } } break; case SDL_MOUSEMOTION: { mouse_x = event.motion.x; mouse_y = h-event.motion.y; } break; } if(pressed && keyreleased) keyreleased = false; else if(!pressed && !keyreleased) keyreleased = true; limitfps(render_millis); render(); } system("pause"); callback_quit(); return EXIT_SUCCESS; }