• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>PCHIP interpolation</title>
5<link rel="stylesheet" href="../math.css" type="text/css">
6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7<link rel="home" href="../index.html" title="Math Toolkit 2.12.0">
8<link rel="up" href="../interpolation.html" title="Chapter 12. Interpolation">
9<link rel="prev" href="makima.html" title="Modified Akima interpolation">
10<link rel="next" href="quintic_hermite.html" title="Quintic Hermite interpolation">
11</head>
12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13<table cellpadding="2" width="100%"><tr>
14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
15<td align="center"><a href="../../../../../index.html">Home</a></td>
16<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
19<td align="center"><a href="../../../../../more/index.htm">More</a></td>
20</tr></table>
21<hr>
22<div class="spirit-nav">
23<a accesskey="p" href="makima.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../interpolation.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="quintic_hermite.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
24</div>
25<div class="section">
26<div class="titlepage"><div><div><h2 class="title" style="clear: both">
27<a name="math_toolkit.pchip"></a><a class="link" href="pchip.html" title="PCHIP interpolation">PCHIP interpolation</a>
28</h2></div></div></div>
29<h4>
30<a name="math_toolkit.pchip.h0"></a>
31      <span class="phrase"><a name="math_toolkit.pchip.synopsis"></a></span><a class="link" href="pchip.html#math_toolkit.pchip.synopsis">Synopsis</a>
32    </h4>
33<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">math</span><span class="special">/</span><span class="identifier">interpolators</span><span class="special">/</span><span class="identifier">pchip</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
34
35<span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">interpolators</span> <span class="special">{</span>
36
37<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">RandomAccessContainer</span><span class="special">&gt;</span>
38<span class="keyword">class</span> <span class="identifier">pchip</span>
39<span class="special">{</span>
40<span class="keyword">public</span><span class="special">:</span>
41
42    <span class="keyword">using</span> <span class="identifier">Real</span> <span class="special">=</span> <span class="identifier">RandomAccessContainer</span><span class="special">::</span><span class="identifier">value_type</span><span class="special">;</span>
43
44    <span class="identifier">pchip</span><span class="special">(</span><span class="identifier">RandomAccessContainer</span><span class="special">&amp;&amp;</span> <span class="identifier">abscissas</span><span class="special">,</span> <span class="identifier">RandomAccessContainer</span><span class="special">&amp;&amp;</span> <span class="identifier">ordinates</span><span class="special">,</span>
45          <span class="identifier">Real</span> <span class="identifier">left_endpoint_derivative</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special">&lt;</span><span class="identifier">Real</span><span class="special">&gt;::</span><span class="identifier">quiet_NaN</span><span class="special">(),</span>
46          <span class="identifier">Real</span> <span class="identifier">right_endpoint_derivative</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special">&lt;</span><span class="identifier">Real</span><span class="special">&gt;::</span><span class="identifier">quiet_NaN</span><span class="special">());</span>
47
48    <span class="identifier">Real</span> <span class="keyword">operator</span><span class="special">()(</span><span class="identifier">Real</span> <span class="identifier">x</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span>
49
50    <span class="identifier">Real</span> <span class="identifier">prime</span><span class="special">(</span><span class="identifier">Real</span> <span class="identifier">x</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span>
51
52    <span class="keyword">void</span> <span class="identifier">push_back</span><span class="special">(</span><span class="identifier">Real</span> <span class="identifier">x</span><span class="special">,</span> <span class="identifier">Real</span> <span class="identifier">y</span><span class="special">);</span>
53
54    <span class="keyword">friend</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">ostream</span><span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">&lt;&lt;(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">ostream</span> <span class="special">&amp;</span> <span class="identifier">os</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">pchip</span> <span class="special">&amp;</span> <span class="identifier">m</span><span class="special">);</span>
55<span class="special">};</span>
56
57<span class="special">}</span> <span class="comment">// namespaces</span>
58</pre>
59<h4>
60<a name="math_toolkit.pchip.h1"></a>
61      <span class="phrase"><a name="math_toolkit.pchip.pchip_interpolation"></a></span><a class="link" href="pchip.html#math_toolkit.pchip.pchip_interpolation">PCHIP
62      Interpolation</a>
63    </h4>
64<p>
65      The PCHIP interpolant takes non-equispaced data and interpolates between them
66      via cubic Hermite polynomials whose slopes are chosen so that the resulting
67      interpolant is monotonic; see <a href="https://doi.org/10.1137/0717021" target="_top">Fritsch
68      and Carlson</a> for details. The interpolant is <span class="emphasis"><em>C</em></span><sup>1</sup> and
69      evaluation has ��(log(<span class="emphasis"><em>N</em></span>)) complexity. An example usage is
70      as follows:
71    </p>
72<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">x</span><span class="special">{</span><span class="number">1</span><span class="special">,</span> <span class="number">5</span><span class="special">,</span> <span class="number">9</span> <span class="special">,</span> <span class="number">12</span><span class="special">};</span>
73<span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">y</span><span class="special">{</span><span class="number">8</span><span class="special">,</span><span class="number">17</span><span class="special">,</span> <span class="number">4</span><span class="special">,</span> <span class="special">-</span><span class="number">3</span><span class="special">};</span>
74<span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">interpolators</span><span class="special">::</span><span class="identifier">pchip</span><span class="special">;</span>
75<span class="keyword">auto</span> <span class="identifier">spline</span> <span class="special">=</span> <span class="identifier">pchip</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">x</span><span class="special">),</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">y</span><span class="special">));</span>
76<span class="comment">// evaluate at a point:</span>
77<span class="keyword">double</span> <span class="identifier">z</span> <span class="special">=</span> <span class="identifier">spline</span><span class="special">(</span><span class="number">3.4</span><span class="special">);</span>
78<span class="comment">// evaluate derivative at a point:</span>
79<span class="keyword">double</span> <span class="identifier">zprime</span> <span class="special">=</span> <span class="identifier">spline</span><span class="special">.</span><span class="identifier">prime</span><span class="special">(</span><span class="number">3.4</span><span class="special">);</span>
80</pre>
81<p>
82      Periodically, it is helpful to see what data the interpolator has, and the
83      slopes it has chosen. This can be achieved via
84    </p>
85<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">spline</span> <span class="special">&lt;&lt;</span> <span class="string">"\n"</span><span class="special">;</span>
86</pre>
87<p>
88      Note that the interpolator is pimpl'd, so that copying the class is cheap,
89      and hence it can be shared between threads. (The call operator and <code class="computeroutput"><span class="special">.</span><span class="identifier">prime</span><span class="special">()</span></code>
90      are threadsafe; <code class="computeroutput"><span class="identifier">push_back</span></code> is
91      not.)
92    </p>
93<p>
94      This interpolant can be updated in constant time. Hence we can use <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">circular_buffer</span></code>
95      to do real-time interpolation:
96    </p>
97<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">circular_buffer</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
98<span class="special">...</span>
99<span class="identifier">boost</span><span class="special">::</span><span class="identifier">circular_buffer</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">initial_x</span><span class="special">{</span><span class="number">1</span><span class="special">,</span><span class="number">2</span><span class="special">,</span><span class="number">3</span><span class="special">,</span><span class="number">4</span><span class="special">};</span>
100<span class="identifier">boost</span><span class="special">::</span><span class="identifier">circular_buffer</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">initial_y</span><span class="special">{</span><span class="number">4</span><span class="special">,</span><span class="number">5</span><span class="special">,</span><span class="number">6</span><span class="special">,</span><span class="number">7</span><span class="special">};</span>
101<span class="keyword">auto</span> <span class="identifier">circular_pchip</span> <span class="special">=</span> <span class="identifier">pchip</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">initial_x</span><span class="special">),</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">initial_y</span><span class="special">));</span>
102<span class="comment">// interpolate via call operation:</span>
103<span class="keyword">double</span> <span class="identifier">y</span> <span class="special">=</span> <span class="identifier">circular_pchip</span><span class="special">(</span><span class="number">3.5</span><span class="special">);</span>
104<span class="comment">// add new data:</span>
105<span class="identifier">circular_pchip</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="number">5</span><span class="special">,</span> <span class="number">8</span><span class="special">);</span>
106<span class="comment">// interpolate at 4.5:</span>
107<span class="identifier">y</span> <span class="special">=</span> <span class="identifier">circular_pchip</span><span class="special">(</span><span class="number">4.5</span><span class="special">);</span>
108</pre>
109<p>
110      <span class="inlinemediaobject"><object type="image/svg+xml" data="../../graphs/pchip.svg"></object></span>
111    </p>
112<h4>
113<a name="math_toolkit.pchip.h2"></a>
114      <span class="phrase"><a name="math_toolkit.pchip.complexity_and_performance"></a></span><a class="link" href="pchip.html#math_toolkit.pchip.complexity_and_performance">Complexity
115      and Performance</a>
116    </h4>
117<p>
118      This interpolator chooses the slopes and forwards data to the cubic Hermite
119      interpolator, so the performance is stated in the documentation for <code class="computeroutput"><span class="identifier">cubic_hermite</span><span class="special">.</span><span class="identifier">hpp</span></code>.
120    </p>
121</div>
122<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
123<td align="left"></td>
124<td align="right"><div class="copyright-footer">Copyright © 2006-2019 Nikhar
125      Agrawal, Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos,
126      Hubert Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Matthew Pulver, Johan
127      Råde, Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg,
128      Daryle Walker and Xiaogang Zhang<p>
129        Distributed under the Boost Software License, Version 1.0. (See accompanying
130        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
131      </p>
132</div></td>
133</tr></table>
134<hr>
135<div class="spirit-nav">
136<a accesskey="p" href="makima.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../interpolation.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="quintic_hermite.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
137</div>
138</body>
139</html>
140