예제 #1
0
void evalTest(const std::size_t p_size, const std::size_t p_samples) {
    Polynomial<T,Dim> P;
    Index<Dim> polyIdx;
    { 
        typename Shape<Dim>::type polyShape;
        polyShape.fill(p_size);
        P.reshape(polyShape).applyToCoefficients([](T& ak, const Index<Dim>& idx){ 
                ak = T(idx())/T(idx.maxId());
        });
    }

    std::array<T,Dim> X;
    T dX;
    {
        const T a = T(0);
        const T b = T(1);
        dX = (b-a)/(p_samples-1);   
    }
    
    typename Shape<Dim>::type sampleShape;
    sampleShape.fill(p_samples);
    Index<Dim> sampleIdx(sampleShape);
    while(!sampleIdx.atMaxId()) {
        for (std::size_t d=0; d < Dim; d++)
            X[d] = sampleIdx[d]*dX;
        T lhs, rhs;
        lhs = P(X); 
        rhs = T(0);
        polyIdx.reset(P.shape());
        while(!polyIdx.atMaxId()) {
            T val = T(1);
            for (std::size_t d=0; d<Dim; d++)
                val *= std::pow(X[d],polyIdx[d]);
            rhs += T(polyIdx())/T(polyIdx.maxId())*val;
            ++polyIdx;
        }
        ASSERT_LE(std::abs(rhs-lhs),std::pow(10,Dim)*std::numeric_limits<T>::epsilon());
        ++sampleIdx;
    }
}