FUnifRoots.hpp 9.82 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// ===================================================================================
// Copyright ScalFmm 2011 INRIA, Olivier Coulaud, Bérenger Bramas, Matthias Messner
// olivier.coulaud@inria.fr, berenger.bramas@inria.fr
// This software is a computer program whose purpose is to compute the FMM.
//
// This software is governed by the CeCILL-C and LGPL licenses and
// abiding by the rules of distribution of free software.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public and CeCILL-C Licenses for more details.
// "http://www.cecill.info".
// "http://www.gnu.org/licenses".
// ===================================================================================
16 17 18
// Keep in private GIT
// @SCALFMM_PRIVATE

19 20 21 22 23 24 25 26
#ifndef FUNIFROOTS_HPP
#define FUNIFROOTS_HPP

#include <cmath>
#include <limits>
#include <cassert>

#include "../../Utils/FNoCopyable.hpp"
27
#include "../../Utils/FMath.hpp"
28

29
#include <array>
30 31 32 33 34 35 36 37 38 39 40 41 42 43

/**
 * @author Pierre Blanchard (pierre.blanchard@inria.fr)
 * Please read the license
 */

/**
 * @class FUnifRoots
 *
 * The class @p FUnifRoots provides the equispaced roots of order \f$\ell\f$
 * and the Lagrange polynomials \f$L_n(x)\f$.
 *
 * @tparam ORDER interpolation order \f$\ell\f$
 */
44
template < class FReal, int ORDER>
45 46
struct FUnifRoots : FNoCopyable
{
47
    enum {order = ORDER}; //!< interpolation order
48

49
    /**
50 51 52
   * Lagrange roots in [-1,1] computed as \f$\bar x_n =
   * -1 + 2\frac{n-1}{\ell}\f$ for \f$n=1,\dots,\ell\f$
   */
53
    const static std::array<FReal,ORDER> roots;
54

55
    /**
56 57 58
   * Lagrange polynomials \f$ L_n(x) = \Pi_{m=0 \atop m\neq n}^{\ell-1} \frac{x-\bar x_m}{\bar x_n-\bar x_m} \f$
   * Expression with reduced roundoff errors:
   * \f$ L_n(x) = \frac{(-1)^(\ell-n-1)(\ell-1)^(\ell-1)}{(2h)^(\ell-1)n!(\ell-n-1)!} \Pi_{m=0 \atop m\neq n}^{\ell-1} (x-\bar x_m) \f$
59 60 61 62 63
   *
   * @param[in] n index
   * @param[in] x coordinate in [-1,1]
   * @return function value
   */
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    static FReal L(const unsigned int n, FReal x)
    {
        assert(std::fabs(x)-1.<10.*std::numeric_limits<FReal>::epsilon());
        if (std::fabs(x)>1.) {
            //std::cout << "x=" << x << " out of bounds!" << std::endl;
            x = (x > FReal( 1.) ? FReal( 1.) : x);
            x = (x < FReal(-1.) ? FReal(-1.) : x);
        }

        // Specific precomputation of scale factor
        // in order to minimize round-off errors
        // NB: scale factor could be hardcoded (just as the roots)
        FReal scale;
        int omn = order-n-1;
        if(omn%2) scale=-1.; // (-1)^(n-1-(k+1)+1)=(-1)^(omn-1)
        else scale=1.;
        scale/=FMath::pow(2.,order-1)*FMath::factorial<FReal>(n)*FMath::factorial<FReal>(omn);

        // compute L
        FReal L=FReal(1.);
        for(unsigned int m=0;m<order;++m){
            if(m!=n){
                // previous version with risks of round-off error
                //L *= (x-FUnifRoots<order>::roots[m])/(FUnifRoots<order>::roots[n]-FUnifRoots<order>::roots[m]);

                // new version (reducing round-off)
                // regular grid on [-1,1] (h simplifies, only the size of the domain and a remains i.e. 2. and -1.)
                L *= ((order-1)*(x+1.)-2.*m);
            }
        }

        L*=scale;

        return FReal(L);
    }


    /**
102 103 104 105 106 107 108
   * For the derivation of the Lagrange polynomials
   * \f$ \frac{\mathrm{d} L_n(x)}{\mathrm{d}x} = ... \f$
   *
   * @param[in] n index
   * @param[in] x coordinate in [-1,1]
   * @return function value
   */
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
    static FReal dL(const unsigned int n, FReal x)
    {
        assert(std::fabs(x)-1.<10.*std::numeric_limits<FReal>::epsilon());
        if (std::fabs(x)>1.) {
            x = (x > FReal( 1.) ? FReal( 1.) : x);
            x = (x < FReal(-1.) ? FReal(-1.) : x);
        }

        // optimized variant
        FReal NdL=FReal(0.);// init numerator
        FReal DdL=FReal(1.);// init denominator
        FReal tmpNdL;
        for(unsigned int p=0;p<order;++p){
            if(p!=n){
                tmpNdL=FReal(1.);
                for(unsigned int m=0;m<order;++m)
                    if(m!=n && m!=p)
                        tmpNdL*=x-FUnifRoots<FReal, order>::roots[m];
                NdL+=tmpNdL;
                DdL*=FUnifRoots<FReal, order>::roots[n]-FUnifRoots<FReal, order>::roots[p];
            }//endif
        }// p

        return FReal(NdL/DdL);

134
    }
135
};
136

137 138
template<int ORDER>
struct FUnifRootsCore{};
139

140 141
template<class FReal, int ORDER>
const std::array<FReal,ORDER> FUnifRoots<FReal,ORDER>::roots = FUnifRootsCore<ORDER>::template Build<FReal>();
142 143 144


// order 2
145 146 147 148 149 150 151
template<>
struct FUnifRootsCore<2>{
    template <class FReal>
    std::array<FReal,2> Build(){
        return { -1., 1.};
    }
};
152 153

// order 3
154 155 156 157 158 159 160
template<>
struct FUnifRootsCore<3>{
    template <class FReal>
    std::array<FReal,3> Build(){
        return {-1.,           0.0,          1.};
    }
};
161 162

// order 4
163 164 165 166 167 168 169
template<>
struct FUnifRootsCore<4>{
    template <class FReal>
    std::array<FReal,4> Build(){
        return {-1.,            -0.333333333333333,            0.333333333333333,            1.};
    }
};
170 171

// order 5
172 173 174 175 176 177 178
template<>
struct FUnifRootsCore<5>{
    template <class FReal>
    std::array<FReal,5> Build(){
        return {-1.,       -0.5,         0.,          0.5,           1.};
    }
};
179 180

// order 6
181 182 183 184 185 186 187
template<>
struct FUnifRootsCore<6>{
    template <class FReal>
    std::array<FReal,6> Build(){
        return {-1.,           -0.6,          -0.2,            0.2,           0.6,            1.};
    }
};
188 189

// order 7
190 191 192 193 194 195 196 197
template<>
struct FUnifRootsCore<7>{
    template <class FReal>
    std::array<FReal,7> Build(){
        return {-1.,         -0.666666666666666,         -0.333333333333333,          0.,
            0.333333333333333,          0.666666666666666,           1.};
    }
};
198 199

// order 8
200 201 202 203 204 205 206 207 208 209 210 211 212 213
template<>
struct FUnifRootsCore<8>{
    template <class FReal>
    std::array<FReal,8> Build(){
        return {-1.,
            -0.714285714285714,
            -0.428571428571429,
            -0.142857142857143,
            0.142857142857143,
            0.428571428571429,
            0.714285714285714,
            1.};
    }
};
214 215

// order 9
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
template<>
struct FUnifRootsCore<9>{
    template <class FReal>
    std::array<FReal,9> Build(){
        return {-1.,
            -0.75,
            -0.5,
            -0.25,
            0.0,
            0.25,
            0.5,
            0.75,
            1.};
    }
};
231 232

// order 10
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
template<>
struct FUnifRootsCore<10>{
    template <class FReal>
    std::array<FReal,10> Build(){
        return {-1.,
            -0.777777777777777,
            -0.555555555555555,
            -0.333333333333333,
            -0.111111111111111,
            0.111111111111111,
            0.333333333333333,
            0.555555555555555,
            0.777777777777777,
            1.};
    }
};
249 250

// order 11
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
template<>
struct FUnifRootsCore<11>{
    template <class FReal>
    std::array<FReal,11> Build(){
        return {-1.,
            -0.8,
            -0.6,
            -0.4,
            -0.2,
            0.0,
            0.2,
            0.4,
            0.6,
            0.8,
            1.};
    }
};
268 269

// order 12
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
template<>
struct FUnifRootsCore<12>{
    template <class FReal>
    std::array<FReal,12> Build(){
        return {-1.,
            -0.818181818181818,
            -0.636363636363636,
            -0.454545454545455,
            -0.272727272727273,
            -0.090909090909091,
            0.090909090909091,
            0.272727272727273,
            0.454545454545455,
            0.636363636363636,
            0.818181818181818,
            1.};
    }
};

289 290

// order 13
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
template<>
struct FUnifRootsCore<13>{
    template <class FReal>
    std::array<FReal,13> Build(){
        return {-1.,
            -0.833333333333333,
            -0.666666666666666,
            -0.5,
            -0.333333333333333,
            -0.166666666666666,
            0.0,
            0.166666666666666,
            0.333333333333333,
            0.5,
            0.666666666666666,
            0.833333333333333,
            1.};
    }
};
310 311

// order 14
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
template<>
struct FUnifRootsCore<14>{
    template <class FReal>
    std::array<FReal,14> Build(){
        return {-1.,
            -0.846153846153846,
            -0.692307692307692,
            -0.538461538461538,
            -0.384615384615385,
            -0.230769230769231,
            -0.076923076923077,
            0.076923076923077,
            0.230769230769231,
            0.384615384615385,
            0.538461538461538,
            0.692307692307692,
            0.846153846153846,
            1.};
    }
};


// order 15
template<>
struct FUnifRootsCore<15>{
    template <class FReal>
    std::array<FReal,15> Build(){
        return {-1.0,
            -0.857142857142857,
            -0.714285714285714,
            -0.571428571428571,
            -0.428571428571429,
            -0.285714285714286,
            -0.142857142857143,
            0.0,
            0.142857142857143,
            0.285714285714286,
            0.428571428571429,
            0.571428571428571,
            0.714285714285714,
            0.857142857142857,
            1.0};
    }
};


// order 20
template<>
struct FUnifRootsCore<16>{
    template <class FReal>
    std::array<FReal,16> Build(){
        return {-1.0,
            -0.8947368421052632,
            -0.7894736842105263,
            -0.6842105263157895,
            -0.5789473684210527,
            -0.4736842105263158,
            -0.3684210526315789,
            -0.2631578947368421,
            -0.1578947368421053,
            -0.0526315789473684,
            0.0526315789473684,
            0.1578947368421053,
            0.2631578947368421,
            0.3684210526315789,
            0.4736842105263158,
            0.5789473684210527,
            0.6842105263157895,
            0.7894736842105263,
            0.8947368421052632,
            1.0};
    }
};
385 386 387


#endif