FUnifRoots.hpp 12.3 KB
Newer Older
1
// ===================================================================================
2
// Copyright ScalFmm 2011 INRIA, Olivier Coulaud, Berenger Bramas, Matthias Messner
3 4 5 6 7 8 9 10 11 12 13 14 15
// 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
// Keep in private GIT

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

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

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

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

/**
 * @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$
 */
43
template < class FReal, int ORDER>
44 45
struct FUnifRoots : FNoCopyable
{
46
    enum {order = ORDER}; //!< interpolation order
47

48
    /**
49 50 51
   * 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$
   */
52
    const static std::array<FReal,ORDER> roots;
53

54
    /**
55 56 57
   * 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$
58 59 60 61 62
   *
   * @param[in] n index
   * @param[in] x coordinate in [-1,1]
   * @return function value
   */
63 64 65 66 67 68 69 70 71 72 73 74 75 76
    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;
77 78 79
        if(omn%2) scale=FReal(-1.); // (-1)^(n-1-(k+1)+1)=(-1)^(omn-1)
        else scale=FReal(1.);
        scale/=FReal(FMath::pow(FReal(2.),order-1)*FMath::factorial<FReal>(n)*FMath::factorial<FReal>(omn));
80 81 82 83 84 85 86 87 88 89

        // 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.)
90
                L *= (FReal(order-1)*(x+FReal(1.))-FReal(2.)*FReal(m));
91 92 93 94 95 96 97 98 99 100
            }
        }

        L*=scale;

        return FReal(L);
    }


    /**
101 102 103 104 105 106 107
   * 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
   */
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
    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);

133
    }
134
};
135

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

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


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

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

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

// order 5
177 178 179
template<>
struct FUnifRootsCore<5>{
    template <class FReal>
180
    static std::array<FReal,5> Build(){
181 182 183 184 185
        return { {FReal(-1. ),       
                  FReal(-0.5),         
                  FReal( 0. ),          
                  FReal( 0.5),           
                  FReal( 1. )} };
186 187
    }
};
188 189

// order 6
190 191 192
template<>
struct FUnifRootsCore<6>{
    template <class FReal>
193
    static std::array<FReal,6> Build(){
194 195 196 197 198 199
        return { {FReal(-1. ),           
                  FReal(-0.6),          
                  FReal(-0.2),            
                  FReal( 0.2),           
                  FReal( 0.6),            
                  FReal( 1. )} };
200 201
    }
};
202 203

// order 7
204 205 206
template<>
struct FUnifRootsCore<7>{
    template <class FReal>
207
    static std::array<FReal,7> Build(){
208 209 210 211 212 213 214
        return { {FReal(-1.),         
                  FReal(-0.666666666666666),         
                  FReal(-0.333333333333333),          
                  FReal( 0.               ),
                  FReal( 0.333333333333333),          
                  FReal( 0.666666666666666),           
                  FReal( 1.               )} };
215 216
    }
};
217 218

// order 8
219 220 221
template<>
struct FUnifRootsCore<8>{
    template <class FReal>
222
    static std::array<FReal,8> Build(){
223 224 225 226 227 228 229 230
        return { {FReal(-1.               ),
                  FReal(-0.714285714285714),
                  FReal(-0.428571428571429),
                  FReal(-0.142857142857143),
                  FReal( 0.142857142857143),
                  FReal( 0.428571428571429),
                  FReal( 0.714285714285714),
                  FReal( 1.               ) } };
231 232
    }
};
233 234

// order 9
235 236 237
template<>
struct FUnifRootsCore<9>{
    template <class FReal>
238
    static std::array<FReal,9> Build(){
239 240 241 242 243 244 245 246 247
        return { {FReal(-1.  ),
                  FReal(-0.75),
                  FReal(-0.5 ),
                  FReal(-0.25),
                  FReal( 0.0 ),
                  FReal( 0.25),
                  FReal( 0.5 ),
                  FReal( 0.75),
                  FReal( 1.  )} };
248 249
    }
};
250 251

// order 10
252 253 254
template<>
struct FUnifRootsCore<10>{
    template <class FReal>
255
    static std::array<FReal,10> Build(){
256 257 258 259 260 261 262 263 264 265
        return { {FReal(-1.               ),
                  FReal(-0.777777777777777),
                  FReal(-0.555555555555555),
                  FReal(-0.333333333333333),
                  FReal(-0.111111111111111),
                  FReal( 0.111111111111111),
                  FReal( 0.333333333333333),
                  FReal( 0.555555555555555),
                  FReal( 0.777777777777777),
                  FReal( 1.               )} };
266 267
    }
};
268 269

// order 11
270 271 272
template<>
struct FUnifRootsCore<11>{
    template <class FReal>
273
    static std::array<FReal,11> Build(){
274 275 276 277 278 279 280 281 282 283 284
        return { {FReal(-1. ),
                  FReal(-0.8),
                  FReal(-0.6),
                  FReal(-0.4),
                  FReal(-0.2),
                  FReal( 0.0),
                  FReal( 0.2),
                  FReal( 0.4),
                  FReal( 0.6),
                  FReal( 0.8),
                  FReal( 1. )} };
285 286
    }
};
287 288

// order 12
289 290 291
template<>
struct FUnifRootsCore<12>{
    template <class FReal>
292
    static std::array<FReal,12> Build(){
293 294 295 296 297 298 299 300 301 302 303 304
        return { {FReal(-1.               ),
                  FReal(-0.818181818181818),
                  FReal(-0.636363636363636),
                  FReal(-0.454545454545455),
                  FReal(-0.272727272727273),
                  FReal(-0.090909090909091),
                  FReal( 0.090909090909091),
                  FReal( 0.272727272727273),
                  FReal( 0.454545454545455),
                  FReal( 0.636363636363636),
                  FReal( 0.818181818181818),
                  FReal( 1.               )} };
305 306 307
    }
};

308 309

// order 13
310 311 312
template<>
struct FUnifRootsCore<13>{
    template <class FReal>
313
    static std::array<FReal,13> Build(){
314 315 316 317 318 319 320 321 322 323 324 325 326
        return { {FReal(-1.               ),
                  FReal(-0.833333333333333),
                  FReal(-0.666666666666666),
                  FReal(-0.5              ),
                  FReal(-0.333333333333333),
                  FReal(-0.166666666666666),
                  FReal( 0.0              ),
                  FReal( 0.166666666666666),
                  FReal( 0.333333333333333),
                  FReal( 0.5              ),
                  FReal( 0.666666666666666),
                  FReal( 0.833333333333333),
                  FReal( 1.               )} };
327 328
    }
};
329 330

// order 14
331 332 333
template<>
struct FUnifRootsCore<14>{
    template <class FReal>
334
    static std::array<FReal,14> Build(){
335 336 337 338 339 340 341 342 343 344 345 346 347 348
        return { {FReal(-1.               ),
                  FReal(-0.846153846153846),
                  FReal(-0.692307692307692),
                  FReal(-0.538461538461538),
                  FReal(-0.384615384615385),
                  FReal(-0.230769230769231),
                  FReal(-0.076923076923077),
                  FReal( 0.076923076923077),
                  FReal( 0.230769230769231),
                  FReal( 0.384615384615385),
                  FReal( 0.538461538461538),
                  FReal( 0.692307692307692),
                  FReal( 0.846153846153846),
                  FReal( 1.               )} };
349 350 351 352 353 354 355 356
    }
};


// order 15
template<>
struct FUnifRootsCore<15>{
    template <class FReal>
357
    static std::array<FReal,15> Build(){
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
        return { {FReal(-1.0              ),
                  FReal(-0.857142857142857),
                  FReal(-0.714285714285714),
                  FReal(-0.571428571428571),
                  FReal(-0.428571428571429),
                  FReal(-0.285714285714286),
                  FReal(-0.142857142857143),
                  FReal( 0.0              ),
                  FReal( 0.142857142857143),
                  FReal( 0.285714285714286),
                  FReal( 0.428571428571429),
                  FReal( 0.571428571428571),
                  FReal( 0.714285714285714),
                  FReal( 0.857142857142857),
                  FReal( 1.0              )} };
373 374 375 376 377 378 379 380
    }
};


// order 20
template<>
struct FUnifRootsCore<16>{
    template <class FReal>
381
    static std::array<FReal,16> Build(){
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
        return { {FReal(-1.0               ),
                  FReal(-0.8947368421052632),
                  FReal(-0.7894736842105263),
                  FReal(-0.6842105263157895),
                  FReal(-0.5789473684210527),
                  FReal(-0.4736842105263158),
                  FReal(-0.3684210526315789),
                  FReal(-0.2631578947368421),
                  FReal(-0.1578947368421053),
                  FReal(-0.0526315789473684),
                  FReal( 0.0526315789473684),
                  FReal( 0.1578947368421053),
                  FReal( 0.2631578947368421),
                  FReal( 0.3684210526315789),
                  FReal( 0.4736842105263158),
                  FReal( 0.5789473684210527),
                  FReal( 0.6842105263157895),
                  FReal( 0.7894736842105263),
                  FReal( 0.8947368421052632),
                  FReal( 1.0               )} };
402 403
    }
};
404 405

#endif