C++ (Cpp) fabs Examples

C++ (Cpp) fabs - 30 examples found. These are the top rated real world C++ (Cpp) examples of fabs extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
Expr *ConstraintBase::VectorsParallel(int eq, ExprVector a, ExprVector b) {
    ExprVector r = a.Cross(b);
    // Hairy ball theorem screws me here. There's no clean solution that I
    // know, so let's pivot on the initial numerical guess. Our caller
    // has ensured that if one of our input vectors is already known (e.g.
    // it's from a previous group), then that one's in a; so that one's
    // not going to move, and we should pivot on that one.
    double mx = fabs((a.x)->Eval());
    double my = fabs((a.y)->Eval());
    double mz = fabs((a.z)->Eval());
    // The basis vector in which the vectors have the LEAST energy is the
    // one that we should look at most (e.g. if both vectors lie in the xy
    // plane, then the z component of the cross product is most important).
    // So find the strongest component of a and b, and that's the component
    // of the cross product to ignore.
    Expr *e0, *e1;
    if(mx > my && mx > mz) {
        e0 = r.y;
        e1 = r.z;
    } else if(my > mz) {
        e0 = r.z;
        e1 = r.x;
    } else {
        e0 = r.x;
        e1 = r.y;
    }

    if(eq == 0) return e0;
    if(eq == 1) return e1;
    ssassert(false, "Unexpected index of equation");
}
Example #2
0
double GaussianIntegral(double x) {
    int swap = 0, steps = 1;
    double vals[2] = {-2,-2};
    if (x > 0) {
        x = -x;
        swap = 1;
    }


    do {
        double C, v = 0.5;
        int i;

        steps *= 2;
        C = fabs(x/steps) * (M_1_SQRT2PI/6);


        for (i=0; i<steps; i++) {
            double a = x*i/steps;
            double b = x*(i+1)/steps;
            double m = (a+b)/2;
            v -= C * (exp(a*a*-0.5) + 4 * exp(m*m*-0.5) + exp(b*b*-0.5));
        }
        vals[0] = vals[1];
        vals[1] = v;
    }
    while (fabs(vals[1] - vals[0]) > 0.00000001 && steps < 1000);

    /* Shouldn't happen, in general, but just in case... */
    if (vals[1] < 0) vals[1] = 0;
    if (swap) vals[1] = 1.0-vals[1];
    return vals[1];
}
Example #3
0
static FP_TYPE* refine_f0(llsm_parameters param, int nfft, int fs, FP_TYPE* f0, int nf0,
                          FP_TYPE** spectrogram, FP_TYPE** phasegram, FP_TYPE** phasegram_d) {

    FP_TYPE* rf0 = calloc(nf0, sizeof(FP_TYPE));
    for(int t = 0; t < nf0; t ++) {
        if(f0[t] <= 0) {
            rf0[t] = 0;
            continue;
        }
        int i_l = floor(f0[t] / fs * nfft * 0.7);
        int i_h = ceil(f0[t] / fs * nfft * 1.3);
        int i_max = round(f0[t] / fs * nfft);
        FP_TYPE peak = spectrogram[t][i_max];

        for(int i = i_l; i <= i_h; i ++)
            if(spectrogram[t][i] > peak) {
                peak = spectrogram[t][i];
                i_max = i;
            }

        FP_TYPE p   = phasegram  [t][i_max];
        FP_TYPE p_d = phasegram_d[t][i_max];
        p   -= floor(p   / 2.0 / M_PI) * 2.0 * M_PI;
        p_d -= floor(p_d / 2.0 / M_PI) * 2.0 * M_PI;
        if(p_d < p)
            p_d += 2.0 * M_PI;

        rf0[t] = (p - p_d) / 2.0 / M_PI * fs;

        if(fabs(rf0[t] / fs * nfft - i_max) > 1 || fabs(rf0[t] - f0[t]) > 10)
            rf0[t] = f0[t];
    }

    return rf0;
}
inline void distanceCov(const typename Atom<dimension>::HomogenousVecD& pt1,
                        const typename Atom<dimension>::HomogenousSymMatrixD& pt1Cov,
                        const HyperPlane<dimension>& hp2, double& distance, double& cov )
{
    typedef typename Atom<dimension>::HomogenousVecD HomogenousVecD;
    typedef typename Atom<dimension>::HomogenousSymMatrixD HomogenousSymMatrixD;
    typedef typename Atom<dimension>::HomogenousMatrix1D HomogenousMatrix1D;
    JFR_ASSERT(hp2.hasCov(), "Second hyper plane need to have covariance");
    HomogenousVecD proj;
    HomogenousSymMatrixD projCov;
    hp2.projectionVectorCov( pt1, pt1Cov, proj, projCov );
    JFR_ASSERT( fabs( proj(dimension) - 1.0 ) < 1e-6 , "Homogenous coefficient need to be 0" );
    distance = ublas::norm_2(proj);
    if(fabs(distance) > 1e-6)
    {
        double inv_d = 1.0 / distance;
        HomogenousMatrix1D Jp;
        for(int i = 0; i < dimension; ++i)
        {
            Jp(0, i) = proj(i) * inv_d;
        }
        cov = (ublas::prod( HomogenousMatrix1D( ublas::prod( Jp, projCov) ), ublas::trans( Jp ) ))(0,0);
    } else {
        cov = details::sum(projCov);
    }
}
double DawsonF(double x)
// Numerical recipes in c;
// Returns Dawson’s integral function for any real x.
// This function is limited to single precision
{
    int i, n0;
    double d1,d2,e1,e2,sum,x2,xp,xx,ans;
    static double c[NMAX+1];
    static int init = 0; //  Flag is 0 if we need to initialize, else 1.
    if (init == 0) {
        init=1;
        for (i=1; i<=NMAX; i++) c[i]=exp(-SQR((2.0*i-1.0)*H));
    }
    if (fabs(x) < 0.2) { //  Use series expansion.
        x2=x*x;
        ans=x*(1.0-A1*x2*(1.0-A2*x2*(1.0-A3*x2)));
    } else {  //  Use sampling theorem representation.
        xx=fabs(x);
        n0=2*(int)(0.5*xx/H+0.5);
        xp=xx-n0*H;
        e1=exp(2.0*xp*H);
        e2=e1*e1;
        d1=n0+1;
        d2=d1-2.0;
        sum=0.0;
        for (i=1; i<=NMAX; i++,d1+=2.0,d2-=2.0,e1*=e2)
            sum += c[i]*(e1/d1+1.0/(d2*e1));
        ans=SIGN(exp(-xp*xp),x)*sum/SQRTPI; // Constant is 1/sqrt(pi)
    }
    return ans;
}
void CollisionSolver::SolveSelfCollision()
{
    D3DPERF_BeginEvent(D3DCOLOR(), L"CollisionSolver::SolveSelfCollision");

    auto cloth = GetCloth();
    auto& particles = cloth->GetParticles();

    for(unsigned int i = 0; i < particles.size(); ++i)
    {
        D3DXVECTOR3 centerToParticle;
        D3DXVECTOR3 center(particles[i]->GetPosition());
        const float radius = particles[i]->GetCollision()->GetRadius() * 2.0f;

        for(unsigned int j = i+1; j < particles.size(); ++j)
        {
            centerToParticle = particles[j]->GetPosition() - center;
            const float length = D3DXVec3Length(&centerToParticle);

            if (length < radius)
            {
                centerToParticle /= length;
                particles[i]->MovePosition(-centerToParticle*fabs(radius-length));
                particles[j]->MovePosition(centerToParticle*fabs(radius-length));
            }
        }
    }

    D3DPERF_EndEvent();
}