File: Trigfun.hpp

package info (click to toggle)
geographiclib 2.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,572 kB
  • sloc: cpp: 27,765; sh: 5,463; makefile: 695; python: 12; ansic: 10
file content (638 lines) | stat: -rw-r--r-- 28,837 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
102
103
104
105
106
107
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/**
 * \file Trigfun.hpp
 * \brief Header for GeographicLib::Trigfun class
 *
 * Copyright (c) Charles Karney (2024-2025) <karney@alum.mit.edu> and licensed
 * under the MIT/X11 License.  For more information, see
 * https://geographiclib.sourceforge.io/
 **********************************************************************/

#if !defined(GEOGRAPHICLIB_TRIGFUN_HPP)
#define GEOGRAPHICLIB_TRIGFUN_HPP 1

#include <GeographicLib/Constants.hpp>

#include <functional>
#include <utility>

#if defined(_MSC_VER)
// Squelch warnings about dll vs vector
#  pragma warning (push)
#  pragma warning (disable: 4251)
#endif

namespace GeographicLib {
  namespace Triaxial {
  class GeodesicLine3;
  class Conformal3;
  }
  /**
   * \brief Representing a function by a Fourier series
   *
   * This class mimic the functionality of Chebfun's 'trig' representation of
   * periodic functions.  Key differences are:
   *
   * - Only odd or even functions are allowed (i.e., only sine of only cosine
   *   terms in the Fourier series).
   * - Can specify that the function has symmetry about the quarter period
   *   point so that the Fourier series only includes odd harmonics.
   * - The integral of a Trigfun is counted as a Trigfun even if it includes a
   *   secular term.
   * - The inverse function of the integral is also a Trigfun (only makes sense
   *   if the original function is either strictly positive or strictly
   *   negative).
   *
   * Assuming the half period = \e h, the function f(x) is represented as
   * follows, here \f$ = (\pi/h) x\f$:
   * - \e sym = false,
   *   - \e odd = true,
   *     \f$ f(x) = C_0 y + \sum_{m = 1}^{M-1} C_m \sin my \f$;
   *   - \e odd = false,
   *     \f$ f(x) = \sum_{m = 0}^{M-1} C_m \cos my \f$;
   * - \e sym = true,
   *   - \e odd = true,
   *     \f$ f(x) = \sum_{m = 0}^{M-1} C_m \sin 2(m+\frac12) y \f$;
   *   - \e odd = false,
   *     \f$ f(x) = \sum_{m = 0}^{M-1} C_m \cos 2(m+\frac12) y \f$.
   * .
   *
   * Here we compute FFTs using the kissfft package
   * https://github.com/mborgerding/kissfft by Mark Borgerding.
   *
   * Example of use:
   * \include example-Trigfun.cpp
   **********************************************************************/
  class GEOGRAPHICLIB_EXPORT Trigfun {
  private:
    /// \cond SKIP
    friend class TrigfunExt;              // For access to root sig 2
    friend class Triaxial::GeodesicLine3; // For access to root sig 2
    friend class Triaxial::Conformal3;    // For access to root sig 2
    /// \endcond
    using real = Math::real;
    static constexpr bool debug_ = false;
    static constexpr bool throw_ = true; // exception on convergence failure
    static constexpr int maxit_ = 300;
    int _m,                     // Number of coefficients in series
      _n;                       // Number of samples in half/quarter period
    bool _odd, _sym;
    std::vector<real> _coeff;
    real _h, _q;                // half, quarter, whole period
    mutable real _max;
    static int chop(const std::vector<real>& c, real tol, real scale = -1);
    static real tolerance(real tol) {
      static const real eps = std::numeric_limits<real>::epsilon();
      return tol <= 0 ? eps : tol;
    }
    // Function samples over half/quarter period of !sym/sym
    // odd sym cent  samples            nF  nC
    //  f   f   f    |-|-|-|-|-|-|-|-|  n+1 n+1 (4)
    //  t   f   f    --|-|-|-|-|-|-|-|  n   n+1 (1), (2), (3), (7)
    //  f   t   f    |-|-|-|-|-|-|-|--  n   n   (1)
    //  t   t   f    --|-|-|-|-|-|-|-|  n   n   (1)
    //  f   f   t    -|-|-|-|-|-|-|-|-  n   n+1 (4), (6)
    //  t   f   t    -|-|-|-|-|-|-|-|-  n   n+1 (5)
    //  f   t   t    -|-|-|-|-|-|-|-|-  n   n
    //  t   t   t    -|-|-|-|-|-|-|-|-  n   n
    //
    // (1) missing end terms presumed zero
    // (2) included last term is usually zero, if non zero, gives secular term
    // (3) zeroth coeff used for secular term
    // (4) zeroth coeff gives constant.
    // (5) secular term should have been removed from samples
    // (6) last coeff is zero (but not for centerp)
    // (7) last coeff is zero (but not for !centerp)
    // Function is represented by (y = pi/h * x)
    // sym = false, sample in f_i = f(h * i/n)
    // odd = true (n samples, n+1 coeffs)
    //    f_0 = 0, need f_i for i in (0, n], f_n defines linear contrib
    // f(x) = c[0] * y + sum(c[k] * sin(k * y), k, 1, n)
    // F(x) = 0 + (-h/pi) * sum( c[k]/k * cos(k * y), k, 1, n)
    // odd = false (n+1 samples, n+1 coeffs)
    //   need f_i for i in [0, n]
    // f(x) = c[0] + sum(c[k] * cos(k * y), k, 1, n)
    // F(x) = (h/pi) * (c[0] * y + sum( c[k]/k * sin(k * y), k, 1, n))
    // sym = true, sample in f_i = f(q * i/n)
    // odd = true (n samples, n coeffs)
    //   f_0 = 0, need f_i for i in (0, n] (n samples)
    // f(x) = sum(c[k] * sin(2*(k+1/2) * y), k, 0, n - 1)
    // F(x) = -(q/pi) * sum(c[k]/(k+1/2) * cos(2*(k+1/2) * y), k, 0, n - 1)
    // odd = false (n samples, n coeffs)
    //   f_n = 0, need f_i for i in [0, n) (n samples)
    // f(x) = sum(c[k] * cos(2*(k+1/2) * y), k, 0, n - 1)
    // F(x) = (q/pi) * sum(c[k]/(k+1/2) * sin(2*(k+1/2) * y), k, 0, n - 1)

    Trigfun(const std::vector<real>& c, bool odd, bool sym, real h)
      : _m(int(c.size()))
      , _n(sym ? _m : _m - 1)
      , _odd(odd)
      , _sym(sym)
      , _coeff(c)
      , _h(h)
      , _q(_h/2)
      , _max(-1)
    {}
    void refine(const Trigfun& tb);
    real check(const std::vector<real>& F, bool centerp, real tol) const;
    // Given z, return dx = finv(z) - nslope * z
    // dx0 is an estimate of dx (NaN means no information)
    // the "p" in the function name mean periodic (vs secular)
    real inversep(real z, const std::function<real(real)>& fp,
                  real dx0, int* countn, int* countb, real tol) const;
    static Trigfun initbysamples(const std::vector<real>& F,
                                 bool odd, bool sym, real halfp, bool centerp);
    /**
     * Tags to indicate which routine is invoking root().  Because the root
     * functions may be called recursively, each invocation is tagged by an
     * indicator value \e ind.  This is merely an aid to debugging.
     **********************************************************************/
    enum ind {
      NONE = 0,
      INV1,
      INV2,
      ARCPOS0,
      FFUNROOT,
      GFUNROOT,
      INVERSEP,
      PIINV,
      FINV,
      KINV,
      OTHER,
    };

    /**
     * Given \e z, find \e x, such that \e z = \e f(\e x).
     *
     * @param[in] indicator a numeric indicator to track this call (can be
     *   safely set to Trigfun::OTHER).
     * @param[in] z the value of \e f(\e x).
     * @param[in] fp the derivative of \e f(\e x).
     * @param[in] x0 an estimate of the solution, i.e., \e z &asymp; \e f(\e
     *   x0).  Use Math::NaN() to indicate that no estimate is known.
     * @param[in] countn if not nullptr, a pointer to an integer that gets
     *   incremented by the number of iterations.
     * @param[in] countb if not nullptr, a pointer to an integer that gets
     *   incremented by the number of bisection steps (which indicates how well
     *   Newton's method is working).
     * @param[in] tol the tolerance using in terminating the root finding.  \e
     *   tol = 0 (the default) mean to use the machine epsilon.
     * @return the root \e x = \e f <sup>&minus;1</sup>(\e z).
     *
     * Newton's method is used to find the root.  At each step the bounds are
     * adjusted.  If any Newton step gives a result which lies outside the
     * bounds, a bisection step is taken instead.
     *
     * \warning The routine assumes that there's a unique root.  This, in turn,
     *   requires that \e f include a secular term.
     **********************************************************************/
    // root sig 2
    real root(ind indicator, real z, const std::function<real(real)>& fp,
              real x0, int* countn, int* countb, real tol) const;
    /**
     * A general purpose Newton solver for \e z = \e f(\e x).
     *
     * @param[in] indicator a numeric indicator to track this call (can be
     *   safely set to Trigfun::OTHER).
     * @param[in] ffp a function returning \e f(\e x) and \e f'(\e x) as a
     *   pair.
     * @param[in] z the value of \e f(\e x).
     * @param[in] x0 an estimate of the solution, i.e., \e z &cong; \e f(\e
     *   x0).
     * @param[in] xa a lower estimate of the solution.
     * @param[in] xb an upper estimate of the solution.
     * @param[in] xscale a representative scale for \e x.
     * @param[in] zscale a representative scale for \e z.
     * @param[in] s &plusmn;1 depending on whether \e f is an increasing or
     *   decreasing function.
     * @param[in] countn if not nullptr, a pointer to an integer that gets
     *   incremented by the number of iterations.
     * @param[in] countb if not nullptr, a pointer to an integer that gets
     *   incremented by the number of bisection steps (which indicates how well
     *   Newton's method is working).
     * @param[in] tol the tolerance using in terminating the root finding.  \e
     *   tol = 0 (the default) mean to use the machine epsilon.
     * @return the root \e x = \e f <sup>&minus;1</sup>(\e z).
     *
     * This is a static function, so \e f(\e x) need not be a Trigfun.  \e ffp
     * provides both the function an its derivative in one function call to
     * accommodate the (common) situation where the two values can be
     * efficiently computed together.
     *
     * Newton's method is used to find the inverse function.  At each step the
     * bounds are adjusted.  If any Newton step gives a result which lies
     * outside the bounds, a bisection step is taken instead.
     *
     * \warning The routine assumes that there's a unique root lying in the
     *   interval [\e xa, \e xb] and that \e x0 lies in the same interval.
     **********************************************************************/
    // root sig 4
    static real root(ind indicator,
                     const std::function<std::pair<real, real>(real)>& ffp,
                     real z,
                     real x0, real xa, real xb,
                     real xscale = 1, real zscale = 1, int s = 1,
                     int* countn = nullptr, int* countb = nullptr,
                     real tol = 0);
    /**
     * Produce a Trigfun for the inverse of \e f.
     *
     * @param[in] fp the derivative of \e f(\e x).
     * @param[in] countn if not nullptr, a pointer to an integer that gets
     *   incremented by the number of iterations need to create the inverse.
     * @param[in] countb if not nullptr, a pointer to an integer that gets
     *   incremented by the number of bisection steps (which indicates how well
     *   Newton's method is working) needed to create the inverse.
     * @param[in] nmax the maximum number of points in a quarter period
     *   (default 2^16 = 65536).
     * @param[in] tol the tolerance using in terminating the root finding.  \e
     *   tol = 0 (the default) mean to use the machine epsilon.
     * @param[in] scale; if \e scale is negative (the default), \e tol sets the
     *   error relative to the largest Fourier coefficient.  Otherwise, the error
     *   is relative to the maximum of the largest Fourier coefficient and \e
     *   scale.
     * @return the Trigfun representation of \e f <sup>&minus;1</sup>(\e z).
     *
     * As with the normal constructor this routine successively doubles the
     * number of sample points, which are computed using Newton's method.  A
     * good starting guess for Newton's method is provided by the previous
     * Fourier approximation.  As a result the average number of Newton
     * iterations per sample point is about 1 or 2.
     *
     * \note Computing the inverse is only possible with a Trigfun with a
     *   secular term.
     **********************************************************************/
    Trigfun inverse(const std::function<real(real)>& fp,
                    int* countn, int* countb,
                    int nmax, real tol, real scale) const;
  public:
    /**
     * Default constructor specifying with the function \e f(\e x) = 0.
     **********************************************************************/
    Trigfun()
      : _m(1)
      , _n(0)
      , _odd(false)
      , _sym(false)
      , _coeff(1, 0)
      , _h(Math::pi())
      , _q(_h/2)
      , _max(-1)
    {}
    /**
     * Construct a Trigfun with a given number of samples a function.
     *
     * @param[in] n the number of samples.
     * @param[in] f the function.
     * @param[in] odd is the function odd?  If it's not odd, then it is even.
     * @param[in] sym is the function symmetric about the quarter period
     *   point (so it contains only odd Fourier harmonics)?
     * @param[in] halfp the half period.
     * @param[in] centerp whether to sample on a centered grid (default false).
     *
     * For \e sym = false, \e n is the number of samples in a half period, and
     * spacing between the samples is \e halfp/\e n.  (If \e centerp = false
     * and \e oddp = false, the function is, in fact sampled \e n + 1 times.)
     * The number of points given to the FFT routine is 2\e n.
     *
     * For \e sym = true, \e n is the number of samples in a quarter period, and
     * spacing between the samples is \e halfp/(2\e n).  The number of points
     * given to the FFT routine is 4\e n.
     *
     * \note In order for the FFT method to operate efficiently, \e n should be
     * the product of a small factors (typically a power of 2).
     *
     * \warning \e f must be a periodic function and it must be either even or
     *   odd.  With \e odd = true and \e sym = false, the secular term can be
     *   set with setsecular().
     **********************************************************************/
    Trigfun(int n, const std::function<real(real)>& f,
            bool odd, bool sym, real halfp, bool centerp = false);
    /**
     * Construct a Trigfun from a function of one argument.
     *
     * @param[in] f the function.
     * @param[in] odd is the function odd?  If it's not odd, then it is even.
     * @param[in] sym is the function symmetric about the quarter period
     *   point (so it contains only odd Fourier harmonics)?
     * @param[in] halfp the half period.
     * @param[in] nmax the maximum number of points in a half/quarter period
     *   (default 2^16 = 65536).
     * @param[in] tol the tolerance, the default value 0 means use the machine
     *   epsilon.
     * @param[in] scale; if \e scale is negative (the default), \e tol sets the
     *   error relative to the largest Fourier coefficient.  Otherwise, the error
     *   is relative to the maximum of the largest Fourier coefficient and \e
     *   scale.
     *
     * The constructor successively doubles the number of sample points and
     * updating the Fourier coefficients accordingly until the high order
     * coefficients become sufficiently small.  At that point Fourier series is
     * truncated discarding some of the trailing coefficients.  This mimics the
     * method used by Chebfun.  In particular, the method used to truncate the
     * series is taken from Aurentz and L. N. Trefethen,
     * <a href="https://doi.org/10.1145/2998442"> Chopping a Chebyshev
     * series</a> (2017); <a href="https://arxiv.org/abs/1512.01803">
     * preprint</a>.
     *
     * \warning \e f must be a periodic function and it must be either even or
     *   odd.  With \e odd = true and \e sym = false, the secular term can be
     *   set with setsecular().
     **********************************************************************/
    Trigfun(const std::function<real(real)>& f, bool odd, bool sym,
            real halfp, int nmax = 1 << 16,
            real tol = 0,
            real scale = -1);
    /**
     * Construct a Trigfun from a function of two arguments.
     *
     * @param[in] f the function.
     * @param[in] odd is the function odd?  If it's not odd, then it is even.
     * @param[in] sym is the function symmetric about the quarter period
     *   point (so it contains only odd Fourier harmonics)?
     * @param[in] halfp the half period.
     * @param[in] nmax the maximum number of points in a half/quarter period
     *   (default 2^16 = 65536).
     * @param[in] tol the tolerance, the default value 0 means use the machine
     *   epsilon.
     * @param[in] scale; if \e scale is negative (the default), \e tol sets the
     *   error relative to the largest Fourier coefficient.  Otherwise, the error
     *   is relative to the maximum of the largest Fourier coefficient and \e
     *   scale.
     *
     * This accommodates the situation where the inverse of a Trigfun \e g is
     * being computed using inverse().  In this case \e f(\e x, \e y0) returns
     * the value \e y such that \e g(\e y) = \e x.  This is typically found
     * using Newton's method which requires a starting guess \e y0.  In the
     * implementation of inverse(), the Fourier representation is successively
     * refined by doubling the number samples.  At each stage, a good estimate
     * of the function values at the new points is found by using the current
     * Fourier representation.
     *
     * \warning \e f must be a periodic function and it must be either even or
     *   odd.  With \e odd = true and \e sym = false, the secular term can be
     *   set with setsecular().
     **********************************************************************/
    Trigfun(const std::function<real(real, real)>& f, bool odd, bool sym,
            real halfp, int nmax = 1 << 16,
            real tol = 0,
            real scale = -1);
    /**
     * Set the coefficient of the secular term
     *
     * @param[in] f0 the value of \e f(\e halfp).
     *
     * \warning This throws an error unless \e odd = true and \e sym = false.
     **********************************************************************/
    void setsecular(real f0);
    /**
     * Evaluate the Trigfun.
     *
     * @param[in] x the function argument.
     * @return the function value \e f(\e x).
     **********************************************************************/
    real operator()(real x) const;
    // For support of Angle
    // real eval(Angle phi) const;

    /**
     * The integral of a Trigfun.
     *
     * @return the integral.
     *
     * \warning The secular term (only present with \e odd = true and \e sym =
     *   false) is ignored when taking the integral.
     **********************************************************************/
    Trigfun integral() const;
    /**
     * Produce a Trigfun for the inverse of \e f.
     *
     * @param[in] fp the derivative of \e f(\e x).
     * @param[in] nmax the maximum number of points in a quarter period
     *   (default 2^16 = 65536).
     * @param[in] tol the tolerance using in terminating the root finding.  \e
     *   tol = 0 (the default) mean to use the machine epsilon.
     * @param[in] scale; if \e scale is negative (the default), \e tol sets the
     *   error relative to the largest Fourier coefficient.  Otherwise, the error
     *   is relative to the maximum of the largest Fourier coefficient and \e
     *   scale.
     * @return the Trigfun representation of \e f <sup>&minus;1</sup>(\e z).
     *
     * As with the normal constructor this routine successively doubles the
     * number of sample points, which are computed using Newton's method.  A
     * good starting guess for Newton's method is provided by the previous
     * Fourier approximation.  As a result the average number of Newton
     * iterations per sample point is about 1 or 2.
     *
     * \e scale is used when \e f(\e x) is a correction term added to a larger
     * contribution; and it would then be the magnitude of the larger
     * contribution.

     * \note Computing the inverse is only possible with a Trigfun with a
     *   secular term.
     **********************************************************************/
    Trigfun inverse(const std::function<real(real)>& fp,
                    int nmax = 1 << 16, real tol = 0, real scale = -1) const {
      return inverse(fp, nullptr, nullptr, nmax, tol, scale);
    }

    /**
     * @return whether the function is odd or not.  If it's not odd, then it is
     *   even.
     **********************************************************************/
    bool Odd() const { return _odd; }
    /**
     * @return whether the function is symmetric about the quarter period
     *   point.  If it is it, then the Fourier series has only odd terms.
     **********************************************************************/
    bool Symmetric() const { return _sym; }
    /**
     * @return the half period of the function.
     **********************************************************************/
    real HalfPeriod() const { return _h; }
    /**
     * @return the number of terms in the  Fourier series.
     **********************************************************************/
    int NCoeffs() const { return _m; }
    /**
     * @return an estimate of the amplitude of the oscillating component of \e
     *   f.
     *
     * \note This estimate excludes any constant or secular terms in the
     *   series.  The estimate is found by summing the absolute values of the
     *   remaining coefficients (and is thus an overestimate).
     **********************************************************************/
    real Max() const;
    /**
     * @return the (approximate) half range of the function.
     *
     * For a Trigfun containing a secular contribution this is the value of the
     * function at the half perioid.  Otherwise Max() is returned.
     **********************************************************************/
    real HalfRange() const {
      return _odd && !_sym ? _coeff[0] * Math::pi() : Max();
    }
    /**
     * @return the average slope of the function.
     *
     * For a Trigfun containing a secular contribution this is the slope of the
     * secular component.  Otherwise 0 is returned..
     **********************************************************************/
    real Slope() const {
      return _odd && !_sym ? HalfRange() / HalfPeriod() : 0;
    }
  };

  /**
   * \brief A function defined by its derivative and its inverse.
   *
   * This is an extension of Trigfun which allows a function to be defined by
   * its derivative.  In this case the derivative must be even, so that its
   * integral is odd (and taken to be zero at the origin).
   *
   * In addition, this class offers a flexible interface to computing the
   * inverse of the function.  If the inverse is only required at a few points
   *
   * Example of use:
   * \include example-TrigfunExt.cpp
   **********************************************************************/
  class GEOGRAPHICLIB_EXPORT TrigfunExt {
  private:
    /// \cond SKIP
    friend class Triaxial::GeodesicLine3; // For access internal inv, inv1
    /// \endcond
    using real = Math::real;
    std::function<real(real)> _fp;
    bool _sym;
    Trigfun _f, _finv;
    real _tol;
    int _nmax;
    bool _invp;
    int _countn, _countb;

    // Approximate inverse using _finv
    real inv0(real z) const {
      if (!_invp) return Math::NaN();
      return _sym ? Math::NaN() : _finv(z);
    }
    // Accurate inverse by direct Newton (not using _finv)
    real inv1(real z, int* countn, int* countb) const {
      return _sym ? Math::NaN() : _f.root(Trigfun::INV1, z, _fp, Math::NaN(),
                                          countn, countb, 0);
    }
    // Accurate inverse correcting result from _finv
    real inv2(real z, int* countn, int* countb) const {
      if (!_invp) return Math::NaN();
      return _sym ? Math::NaN() :
        _f.root(Trigfun::INV2, z, _fp, _finv(z), countn, countb, 0);
    }
    real inv(real z, int* countn, int* countb) const {
      return _invp ? inv2(z, countn, countb) : inv1(z, countn, countb);
    }
  public:
    TrigfunExt() {}
    /**
     * Constructor specifying the derivative, an even periodic function
     *
     * @param[in] fp the derivative function, \e fp(\e x) = \e f'(\e x).
     * @param[in] halfp the half period.
     * @param[in] sym is \e fp symmetric about the quarter period point (so it
     *   contains only odd Fourier harmonics)?
     * @param[in] scale; this is passed to the Trigfun constructor when finding
     *   the Fourier series for \e fp.
     *
     * \warning \e fp must be an even periodic function.  In addition \e fp
     *   must be nonnegative for the inverse of \e f to be computed (in this
     *   case, \e f is a monotonically increasing function).  The inverse is
     *   undefined for \e sym = true.
     **********************************************************************/
    TrigfunExt(const std::function<real(real)>& fp, real halfp,
               bool sym = false, real scale = -1);
    /**
     * Evaluate the TrigfunExt.
     *
     * @param[in] x the function argument.
     * @return the function value \e f(\e x).
     **********************************************************************/
    real operator()(real x) const { return _f(x); }
    /**
     * Evaluate the derivative for TrigfunExt.
     *
     * @param[in] x the function argument.
     * @return the value of the derivative \e fp(\e x).  This uses the function
     *   object passed to the constructor.
     **********************************************************************/
    real deriv(real x) const { return _fp(x); }
    /**
     * Evaluate the inverse of \e f
     *
     * @param[in] z the value of \e f(\e x)
     * @return the value of \e x = \e f <sup>&minus;1</sup>(\e z).
     *
     * This compute the inverse using Newton's method with the derivative
     * function \e fp supplied on construction.  Initially, the starting guess
     * is based on just the secular component of \e f(\e x).  However, if
     * ComputeInverse() is called, a rough Trigfun approximation to the inverse
     * is found and this is used as the starting point for Newton's method.
     **********************************************************************/
    real inv(real z) const {
      return _invp ? inv2(z, nullptr, nullptr) : inv1(z, nullptr, nullptr);
    }
    /**
     * Compute a coarse Fourier series approximation of the inverse.
     *
     * This is used to provide a better starting guess for Newton's method in
     * inv().  Because ComputeInverse() is fairly expensive, this only makes
     * sense if inv() will be called many times.  In order to limit the expense
     * in computing this approximate inverse, the number of Fourier components
     * in the Trigfun for the inverse is limited to 3/2 of the number of
     * components for \e f and the tolerance is set to the square root of the
     * machine epsilon.
     **********************************************************************/
    void ComputeInverse() {
      if (!_invp && !_sym) {
        _countn = _countb = 0;
        _finv = _f.inverse(_fp, &_countn, &_countb, _nmax, _tol, -1);
        _invp = true;
      }
    }
    /**
     * @return whether the function is symmetric about the quarter period
     *   point.  If it is it, then the Fourier series has only odd harmonics.
     **********************************************************************/
    bool Symmetric() const { return _sym; }
    /**
     * @return the number of terms in the  Fourier series for \e f.
     **********************************************************************/
    int NCoeffs() const { return _f.NCoeffs(); }
    /**
     * @return the number of terms in the Fourier series for
     *   \e f<sup>&minus;1</sup>.
     **********************************************************************/
    int NCoeffsInv() const {
      if (!_invp) return -1;
      return _finv.NCoeffs(); }
    /**
     * @return Max() for the underlying Trigfun.
     **********************************************************************/
    real Max() const { return _f.Max(); }
    /**
     * @return HalfPeriod() for the underlying Trigfun.
     **********************************************************************/
    real HalfPeriod() const { return _f.HalfPeriod(); }
    /**
     * @return HalfRange() for the underlying Trigfun.
     **********************************************************************/
    real HalfRange() const { return _f.HalfRange(); }
    /**
     * @return Slope() for the underlying Trigfun.
     **********************************************************************/
    real Slope() const { return _f.Slope(); }
  };

} // namespace GeographicLib

#if defined(_MSC_VER)
#  pragma warning (pop)
#endif

#endif  // GEOGRAPHICLIB_TRIGFUN_HPP