void Threads::FnBac(const Complex & cpx, int *res) const { int iter =0; Complex tmpp; Complex tmp; Complex tmpn(cpx); for (iter = 0; tmpn.Abs2() < 4 && iter < maxIter ;iter++){ tmp = tmpn; tmpn = tmp * tmp + tmpp.Re() + tmpp.Im() + julia_c; tmpp = tmp; } *res = iter; }
void Threads::FnPhoenix(const Complex & cpx, int *res) const { int iter =0; Complex tmpp; Complex tmp; Complex tmpn(cpx); for (iter = 0; tmpn.Abs2() < 4 && iter < maxIter ;iter++){ tmp = tmpn; tmpn = tmp * tmp + julia_c.Re() + Complex(0,julia_c.Im()) * tmpp; tmpp = tmp; } *res = iter; }
List log_EMx(const arma::mat& transition_, const arma::cube& emission_, const arma::vec& init_, const arma::ucube& obs, const arma::uvec& nSymbols, const arma::mat& coef_, const arma::mat& X, const arma::uvec& numberOfStates, int itermax, double tol, int trace, unsigned int threads) { // Make sure we don't alter the original vec/mat/cube // needed for cube, in future maybe in other cases as well arma::cube emission = log(emission_); arma::mat transition = log(transition_); arma::vec init = log(init_); arma::mat coef(coef_); coef.col(0).zeros(); arma::mat weights = exp(X * coef).t(); if (!weights.is_finite()) { return List::create(Named("error") = 3); } weights.each_row() /= sum(weights, 0); weights = log(weights); arma::mat initk(emission.n_rows, obs.n_slices); for (unsigned int k = 0; k < obs.n_slices; k++) { initk.col(k) = init + reparma(weights.col(k), numberOfStates); } arma::cube alpha(emission.n_rows, obs.n_cols, obs.n_slices); //m,n,k arma::cube beta(emission.n_rows, obs.n_cols, obs.n_slices); //m,n,k log_internalForwardx(transition, emission, initk, obs, alpha, threads); log_internalBackward(transition, emission, obs, beta, threads); arma::vec ll(obs.n_slices); #pragma omp parallel for if(obs.n_slices >= threads) schedule(static) num_threads(threads) \ default(none) shared(obs, alpha, ll) for (unsigned int k = 0; k < obs.n_slices; k++) { ll(k) = logSumExp(alpha.slice(k).col(obs.n_cols - 1)); } double sumlogLik = sum(ll); if (trace > 0) { Rcout << "Log-likelihood of initial model: " << sumlogLik << std::endl; } // // //EM-algorithm begins // double change = tol + 1.0; int iter = 0; arma::uvec cumsumstate = cumsum(numberOfStates); while ((change > tol) & (iter < itermax)) { iter++; arma::mat ksii(emission.n_rows, emission.n_rows, arma::fill::zeros); arma::cube gamma(emission.n_rows, emission.n_cols, emission.n_slices, arma::fill::zeros); arma::vec delta(emission.n_rows, arma::fill::zeros); for (unsigned int k = 0; k < obs.n_slices; k++) { delta += exp(alpha.slice(k).col(0) + beta.slice(k).col(0) - ll(k)); } #pragma omp parallel for if(obs.n_slices>=threads) schedule(static) num_threads(threads) \ default(none) shared(transition, obs, ll, alpha, beta, emission, ksii, gamma, nSymbols) for (unsigned int k = 0; k < obs.n_slices; k++) { if (obs.n_cols > 1) { for (unsigned int j = 0; j < emission.n_rows; j++) { for (unsigned int i = 0; i < emission.n_rows; i++) { if (transition(i, j) > -arma::datum::inf) { arma::vec tmpnm1(obs.n_cols - 1); for (unsigned int t = 0; t < (obs.n_cols - 1); t++) { tmpnm1(t) = alpha(i, t, k) + transition(i, j) + beta(j, t + 1, k); for (unsigned int r = 0; r < obs.n_rows; r++) { tmpnm1(t) += emission(j, obs(r, t + 1, k), r); } } #pragma omp atomic ksii(i, j) += exp(logSumExp(tmpnm1) - ll(k)); } } } } for (unsigned int r = 0; r < emission.n_slices; r++) { for (unsigned int l = 0; l < nSymbols[r]; l++) { for (unsigned int i = 0; i < emission.n_rows; i++) { if (emission(i, l, r) > -arma::datum::inf) { arma::vec tmpn(obs.n_cols); for (unsigned int t = 0; t < obs.n_cols; t++) { if (l == (obs(r, t, k))) { tmpn(t) = alpha(i, t, k) + beta(i, t, k); } else tmpn(t) = -arma::datum::inf; } #pragma omp atomic gamma(i, l, r) += exp(logSumExp(tmpn) - ll(k)); } } } } } unsigned int error = log_optCoef(weights, obs, emission, initk, beta, ll, coef, X, cumsumstate, numberOfStates, trace); if (error != 0) { return List::create(Named("error") = error); } if (obs.n_cols > 1) { ksii.each_col() /= sum(ksii, 1); transition = log(ksii); } for (unsigned int r = 0; r < emission.n_slices; r++) { gamma.slice(r).cols(0, nSymbols(r) - 1).each_col() /= sum( gamma.slice(r).cols(0, nSymbols(r) - 1), 1); emission.slice(r).cols(0, nSymbols(r) - 1) = log(gamma.slice(r).cols(0, nSymbols(r) - 1)); } for (unsigned int i = 0; i < numberOfStates.n_elem; i++) { delta.subvec(cumsumstate(i) - numberOfStates(i), cumsumstate(i) - 1) /= arma::as_scalar( arma::accu(delta.subvec(cumsumstate(i) - numberOfStates(i), cumsumstate(i) - 1))); } init = log(delta); for (unsigned int k = 0; k < obs.n_slices; k++) { initk.col(k) = init + reparma(weights.col(k), numberOfStates); } log_internalForwardx(transition, emission, initk, obs, alpha, threads); log_internalBackward(transition, emission, obs, beta, threads); for (unsigned int k = 0; k < obs.n_slices; k++) { ll(k) = logSumExp(alpha.slice(k).col(obs.n_cols - 1)); } double tmp = sum(ll); change = (tmp - sumlogLik) / (std::abs(sumlogLik) + 0.1); sumlogLik = tmp; if (!arma::is_finite(sumlogLik)) { return List::create(Named("error") = 6); } if (trace > 1) { Rcout << "iter: " << iter; Rcout << " logLik: " << sumlogLik; Rcout << " relative change: " << change << std::endl; } } if (trace > 0) { if (iter == itermax) { Rcpp::Rcout << "EM algorithm stopped after reaching the maximum number of " << iter << " iterations." << std::endl; } else { Rcpp::Rcout << "EM algorithm stopped after reaching the relative change of " << change; Rcpp::Rcout << " after " << iter << " iterations." << std::endl; } Rcpp::Rcout << "Final log-likelihood: " << sumlogLik << std::endl; } return List::create(Named("coefficients") = wrap(coef), Named("initialProbs") = wrap(exp(init)), Named("transitionMatrix") = wrap(exp(transition)), Named("emissionArray") = wrap(exp(emission)), Named("logLik") = sumlogLik, Named("iterations") = iter, Named("change") = change, Named("error") = 0); }
void TriangleType::draw(RenderMode::WhichColorMode& wcm, RenderMode::RenderType& r,const Matrix44& adjust_matrix, const Vec3& bias) { if (!visible_) { return; } // std::vector<Vertex*>& vs = sample_.vertices_; //r = RenderMode::PointMode; switch(r) { case RenderMode::PointMode:{ break; } case RenderMode::FlatMode:{ glBegin(GL_TRIANGLES); for( int i = 0 ;i <3 ;++i) { if(this->i_norm[i]!=-1) { Vec4 tmpn( sample_[this->i_norm[i]].nx() , sample_[this->i_norm[i]].ny() ,sample_[this->i_norm[i]].nz() ,1.); Vec4 normal_to_show = tmpn;//adjust_matrix * tmpn; glNormal3f( normal_to_show(0), normal_to_show(1), normal_to_show(2)); //Logger<<"NORMAL: "<<(float)vs[this->i_norm[i]]->nx()<<" "<< // (float)vs[this->i_norm[i]]->ny()<<" "<< // (float)vs[this->i_norm[i]]->nz()<<std::endl; } ColorType color2 = Color_Utility::span_color_from_table(sample_[this->i_vertex[i]].label()); glColor3f( color2(0) ,color2(1) ,color2(2) /* (GLfloat) vs[this->i_vertex[i]]->r(), (GLfloat) vs[this->i_vertex[i]]->g(), (GLfloat) vs[this->i_vertex[i]]->b()*/ ); //Logger<<"COLOR: "<<vs[this->i_vertex[i]]->r()<<" "<< // vs[this->i_vertex[i]]->g()<<" "<< // vs[this->i_vertex[i]]->b()<<std::endl; Vec4 tmpv( sample_[this->i_vertex[i]].x() , sample_[this->i_vertex[i]].y() ,sample_[this->i_vertex[i]].z() ,1.); Vec4 point_to_show = adjust_matrix * tmpv; glVertex3f( point_to_show(0)+ bias(0), point_to_show(1)+ bias(1), point_to_show(2)+ bias(2) ); //Logger<<"VERTEX: "<<vs[this->i_vertex[i]]->x()<<" "<< // vs[this->i_vertex[i]]->y()<<" "<< // vs[this->i_vertex[i]]->z()<<std::endl; } glEnd(); break; } case RenderMode::WireMode:{ glLineWidth(2.0f); glBegin(GL_LINE_LOOP); for( int i = 0 ;i <3 ;++i) { if(this->i_norm[i]!=-1) { Vec4 tmpn( sample_[this->i_norm[i]].nx() , sample_[this->i_norm[i]].ny() ,sample_[this->i_norm[i]].nz() ,1.); Vec4 normal_to_show = adjust_matrix * tmpn; glNormal3f( normal_to_show(0), normal_to_show(1), normal_to_show(2)); } ColorType color2 = Color_Utility::span_color_from_table(sample_[this->i_vertex[i]].label()); glColor3f( color2(0) ,color2(1) ,color2(2) /*(GLfloat) vs[this->i_vertex[i]]->r(), (GLfloat) vs[this->i_vertex[i]]->g(), (GLfloat) vs[this->i_vertex[i]]->b() */); Vec4 tmpv( sample_[this->i_vertex[i]].x() , sample_[this->i_vertex[i]].y() ,sample_[this->i_vertex[i]].z() ,1.); Vec4 point_to_show = adjust_matrix * tmpv; glVertex3f( point_to_show(0)+ bias(0), point_to_show(1)+ bias(1), point_to_show(2)+ bias(2) ); } glEnd(); break; } case RenderMode::FlatWireMode:{ glBegin(GL_TRIANGLES); for( int i = 0 ;i <3 ;++i) { if(this->i_norm[i]!=-1) { Vec4 tmpn( sample_[this->i_norm[i]].nx() , sample_[this->i_norm[i]].ny() ,sample_[this->i_norm[i]].nz() ,1.); Vec4 normal_to_show = tmpn;//adjust_matrix * tmpn; glNormal3f( normal_to_show(0), normal_to_show(1), normal_to_show(2)); //Logger<<"NORMAL: "<<(float)vs[this->i_norm[i]]->nx()<<" "<< // (float)vs[this->i_norm[i]]->ny()<<" "<< // (float)vs[this->i_norm[i]]->nz()<<std::endl; } ColorType color2 = Color_Utility::span_color_from_table(sample_[this->i_vertex[i]].label()); glColor3f( color2(0) ,color2(1) ,color2(2) /* (GLfloat) vs[this->i_vertex[i]]->r(), (GLfloat) vs[this->i_vertex[i]]->g(), (GLfloat) vs[this->i_vertex[i]]->b()*/ ); //Logger<<"COLOR: "<<vs[this->i_vertex[i]]->r()<<" "<< // vs[this->i_vertex[i]]->g()<<" "<< // vs[this->i_vertex[i]]->b()<<std::endl; Vec4 tmpv( sample_[this->i_vertex[i]].x() , sample_[this->i_vertex[i]].y() ,sample_[this->i_vertex[i]].z() ,1.); Vec4 point_to_show = adjust_matrix * tmpv; glVertex3f( point_to_show(0)+ bias(0), point_to_show(1)+ bias(1), point_to_show(2)+ bias(2) ); //Logger<<"VERTEX: "<<vs[this->i_vertex[i]]->x()<<" "<< // vs[this->i_vertex[i]]->y()<<" "<< // vs[this->i_vertex[i]]->z()<<std::endl; } glEnd(); glLineWidth(2.0f); glBegin(GL_LINE_LOOP); for( int i = 0 ;i <3 ;++i) { if(this->i_norm[i]!=-1) { Vec4 tmpn( sample_[this->i_norm[i]].nx() , sample_[this->i_norm[i]].ny() ,sample_[this->i_norm[i]].nz() ,1.); Vec4 normal_to_show = adjust_matrix * tmpn; glNormal3f( normal_to_show(0), normal_to_show(1), normal_to_show(2)); } glColor3f( 0.5f,0.5f,0.5f /*(GLfloat) vs[this->i_vertex[i]]->r(), (GLfloat) vs[this->i_vertex[i]]->g(), (GLfloat) vs[this->i_vertex[i]]->b() */); Vec4 tmpv( sample_[this->i_vertex[i]].x() , sample_[this->i_vertex[i]].y() ,sample_[this->i_vertex[i]].z() ,1.); Vec4 point_to_show = adjust_matrix * tmpv; glVertex3f( point_to_show(0)+ bias(0), point_to_show(1)+ bias(1), point_to_show(2)+ bias(2) ); } glEnd(); break; } case RenderMode::SmoothMode:{ break;} case RenderMode::TextureMode:{ break;} case RenderMode::SelectMode:{ break;} default:{} } }