char *ft_strtrim(char const *s) { char *str; size_t size_str; size_t *t_start; size_t *t_end; size_t loop; t_start = malloc(sizeof(t_start)); t_end = malloc(sizeof(t_end)); if (s == NULL || t_start == NULL || t_end == NULL) return (NULL); size_str = ft_strlen(s); find_limits(s, t_start, t_end, size_str); if (*t_start == *t_end && *t_start != 0) return (NULL); loop = *t_start; printf("begin %zu - end %zu - size %zu\n", *t_start, *t_end, size_str); str = ft_memalloc(size_str - *t_end - *t_start + 1); if (str == NULL) return (NULL); while (loop < (size_str - *t_end)) { printf("%d\n", *(str + loop - *t_start)); *(str + (loop - *t_start)) = *(s + loop); loop++; } //*(str + loop - *t_start) = '\0'; return (str); }
//---------------------------------------------------------------------- // univariate slice sampling to set each element void CS::draw_one() { double oldr = R_(i_, j_); double logp_star = logp(R_(i_, j_)); double u = logp_star - rexp_mt(rng(), 1); find_limits(); if (lo_ >= hi_) { set_r(0); return; } // const double eps(100*std::numeric_limits<double>::epsilon()); const double eps(1e-6); check_limits(oldr, eps); while (1) { double cand = runif_mt(rng(), lo_, hi_); double logp_cand = logp(cand); if (logp_cand > u) { // found something inside slice set_r(cand); return; } else { // contract slice if (cand > oldr) { hi_ = cand; } else { lo_ = cand; } } if (fabs(hi_ - lo_) < eps) { set_r(hi_); return; } } }
VecDoub Actions_Spherical::angles_and_freqs(const VecDoub &x){ // call actions before double E = Pot->H(x), L = Pot->L(x), Lz = Pot->Lz(x); if(E>0.){ return {std::numeric_limits<double>::infinity(),L}; } double r = sqrt(x[0]*x[0]+x[1]*x[1]+x[2]*x[2]); VecDoub limits = find_limits(r,E,L); double taubar = 0.5*(limits[0]+limits[1]); double Delta = 0.5*(limits[1]-limits[0]); Actions_Spherical_data_struct Act(Pot,E,L,Lz,Delta,taubar); double OmegaR = Delta*GaussLegendreQuad(&dJrdH,-PI/2.,PI/2.,&Act,8)/PI; OmegaR = 1./OmegaR; double Omegap = Delta*GaussLegendreQuad(&dJrdL,-PI/2.,PI/2.,&Act,8)/PI; Omegap*=-OmegaR; VecDoub SPol = conv::CartesianToSphericalPolar(x); double thetaLim = asin(MAX(-1.,MIN(1.,(SPol[0]-taubar)/Delta))); double dSRdH=sign(SPol[3])*Delta*GaussLegendreQuad(&dJrdH,-PI/2.,thetaLim,&Act,8); double dSRdL=sign(SPol[3])*Delta*GaussLegendreQuad(&dJrdL,-PI/2.,thetaLim,&Act); double ThetaR = dSRdH*OmegaR; double dStdL=sign(SPol[5])*GaussLegendreQuad(&dLdL,PI/2.,SPol[2],&Act,8); // printVector(x); double Thetap=dSRdL+dStdL+dSRdH*Omegap; double LR=fabs(Lz)/L; double sinu = LR/sqrt(1.-LR*LR)/tan(SPol[2]); double u = 0.; if(sinu>1.) u=PI/2.; else if(sinu<-1.) u = -PI/2.; else u = asin(sinu); if(SPol[5]>0.) u=PI-u; double Thetat=SPol[1]-u+sign(Lz)*Thetap; if(SPol[5]>0.) Thetat-=PI; if(SPol[5]<0.) Thetap+=PI; if(ThetaR>2.*PI) ThetaR-=2.*PI; if(ThetaR<0.) ThetaR+=2.*PI; if(Thetap>2.*PI) Thetap-=2.*PI; if(Thetap<0.) Thetap+=2.*PI; if(Thetat>2.*PI) Thetat-=2.*PI; if(Thetat<0.) Thetat+=2.*PI; if(Thetat<0.) Thetat+=2.*PI; return {ThetaR,Thetat,Thetap,OmegaR,sign(Lz)*Omegap,Omegap}; }
bool edit_graphics_rep::find_graphical_region (SI& x1, SI& y1, SI& x2, SI& y2) { point lim1, lim2; find_limits (lim1, lim2); if (lim1 == point ()) return false; frame f= find_frame (); if (is_nil (f)) return false; point p1= f (point (lim1[0], lim1[1])); point p2= f (point (lim2[0], lim2[1])); x1= (SI) p1[0]; y1= (SI) p1[1]; x2= (SI) p2[0]; y2= (SI) p2[1]; return true; }
VecDoub Actions_Spherical::actions(const VecDoub &x, void *params){ double r = sqrt(x[0]*x[0]+x[1]*x[1]+x[2]*x[2]); double E = Pot->H(x), L = Pot->L(x), Lz = Pot->Lz(x); if(E>0.){ return {std::numeric_limits<double>::infinity(),Lz,L-fabs(Lz)}; } VecDoub limits = find_limits(r,E,L); double taubar = 0.5*(limits[0]+limits[1]); double Delta = 0.5*(limits[1]-limits[0]); Actions_Spherical_data_struct Act(Pot,E,L,Lz,Delta,taubar); double JR = Delta*GaussLegendreQuad(&J_r,-PI/2.,PI/2.,&Act,8)/PI; return {JR,Lz,L-fabs(Lz)}; }
bool edit_graphics_rep::over_graphics (SI x, SI y) { frame f= find_frame (); if (!is_nil (f)) { point lim1, lim2; find_limits (lim1, lim2); point p = adjust (f [point (x, y)]); // cout << type << " at " << p << " [" << lim1 << ", " << lim2 << "]\n"; if (N(lim1) == 2) if ((p[0]<lim1[0]) || (p[0]>lim2[0]) || (p[1]<lim1[1]) || (p[1]>lim2[1])) return false; return true; } return false; }
Vector SliceSampler::draw(const Vector &theta) { last_position_ = theta; initialize(); find_limits(); Vector candidate; double logp_candidate = log_p_slice_ - 1; do { double lambda = runif_mt(rng(), -lo_, hi_); candidate = last_position_ + lambda * random_direction_; logp_candidate = logp_(candidate); if (logp_candidate < log_p_slice_) contract(lambda, logp_candidate); } while (logp_candidate < log_p_slice_); scale_ = hi_ + lo_; // both hi_ and lo_ > 0 return candidate; }
//---------------------------------------------------------------------- // driver function to draw a single element of the correlation // matrix conditional on the variances. void SepStratSampler::draw_R(int i, int j){ i_ = i; j_ = j; double oldr = R_(i,j); double slice = logp_slice_R(oldr) - rexp(); find_limits(); double rcand = runif(lo_, hi_); while(logp_slice_R(rcand) < slice && hi_ > lo_){ if(rcand > oldr) hi_ = rcand; else lo_ = rcand; rcand = runif(lo_,hi_); } set_R(rcand); }
Vec SliceSampler::draw(const Vec &t){ theta = t; z = t; initialize(); pstar = f(theta) - rexp(1); find_limits(); Vec tstar(theta.size(), 0.0); double p = pstar -1; do{ double lam = runif_mt(rng(), -lo, hi); tstar = theta + lam*z; // randomly chosen point in the slice p = f(tstar); if(p<pstar) contract(lam,p); else theta = tstar; }while(p < pstar); scale = hi+lo; // both hi and lo >0 return theta; }
double SSS::draw(double x){ find_limits(x); double logp_cand = 0; int number_of_tries = 0; do{ double x_cand = runif_mt(rng(), lo_, hi_); logp_cand = logf_(x_cand); if(logp_cand < logp_slice_){ contract(x,x_cand, logp_cand); ++number_of_tries; } else return x_cand; if(number_of_tries > 100){ ostringstream err; err << "number of tries exceeded. candidate value is " << x_cand << " with logp_cand = " << logp_cand << endl; handle_error(err.str(), x); } }while(logp_cand < logp_slice_); handle_error("should never get here", x); return 0; }
VecDoub Actions_Spherical::Hessian(const VecDoub &x){ std::cerr<<"Currently not working!!"<<std::endl; VecDoub Freq(2,0), AF = angles_and_freqs(x); Freq[0]=AF[3];Freq[1]=AF[4]; double E = Pot->H(x), L = Pot->L(x), Lz = Pot->Lz(x); double r = sqrt(x[0]*x[0]+x[1]*x[1]+x[2]*x[2]); VecDoub limits = find_limits(r,E,L); double taubar = 0.5*(limits[0]+limits[1]); double Delta = 0.5*(limits[1]-limits[0]); double drpdH = dr0dH(limits[0],L); double dradH = dr0dH(limits[1],L); double dDelta=0.5*(dradH-drpdH); double dtaubar=0.5*(dradH+drpdH); Actions_Spherical_data_struct Act(Pot,E,L,Lz,Delta,taubar,dDelta,dtaubar); double d2JdH = Delta*GaussLegendreQuad(&d2JdH2,-PI/2.,PI/2.,&Act)/PI; double drpdL = dr0dL(limits[0],L); double dradL = dr0dL(limits[1],L); dDelta=0.5*(dradL-drpdL); dtaubar=0.5*(dradL+drpdL); // double d2JdHdL2 = Delta*GaussLegendreQuad(&d2JdHdL,-PI/2.,PI/2.,&Act)/PI; // d2JdH+=Delta*(dr0dH(limits[1])*dJrdH(PI/2.,&Act)-dr0dH(limits[0])*dJrdH(-PI/2.,&Act))/PI; double dOmegadJR = -Freq[0]*Freq[0]*Freq[0]*d2JdH; return {dOmegadJR}; }