1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Cardinal Quintic B-spline 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="cardinal_quadratic_b.html" title="Cardinal Quadratic B-spline interpolation"> 10<link rel="next" href="whittaker_shannon.html" title="Whittaker-Shannon 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="cardinal_quadratic_b.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="whittaker_shannon.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.cardinal_quintic_b"></a><a class="link" href="cardinal_quintic_b.html" title="Cardinal Quintic B-spline interpolation">Cardinal Quintic B-spline 28 interpolation</a> 29</h2></div></div></div> 30<h4> 31<a name="math_toolkit.cardinal_quintic_b.h0"></a> 32 <span class="phrase"><a name="math_toolkit.cardinal_quintic_b.synopsis"></a></span><a class="link" href="cardinal_quintic_b.html#math_toolkit.cardinal_quintic_b.synopsis">Synopsis</a> 33 </h4> 34<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</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">cardinal_quintic_b_spline</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 35</pre> 36<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">math</span><span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">interpolators</span> <span class="special">{</span> 37 38<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Real</span><span class="special">></span> 39<span class="keyword">class</span> <span class="identifier">cardinal_quintic_b_spline</span> 40<span class="special">{</span> 41<span class="keyword">public</span><span class="special">:</span> 42 <span class="comment">// If you don't know the value of the derivative at the endpoints, leave them as NaNs and the routine will estimate them.</span> 43 <span class="comment">// y[0] = y(a), y[n - 1] = y(b), step_size = (b - a)/(n -1).</span> 44 <span class="identifier">cardinal_quintic_b_spline</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">Real</span><span class="special">*</span> <span class="keyword">const</span> <span class="identifier">y</span><span class="special">,</span> 45 <span class="identifier">size_t</span> <span class="identifier">n</span><span class="special">,</span> 46 <span class="identifier">Real</span> <span class="identifier">t0</span> <span class="comment">/* initial time, left endpoint */</span><span class="special">,</span> 47 <span class="identifier">Real</span> <span class="identifier">h</span> <span class="comment">/*spacing, stepsize*/</span><span class="special">,</span> 48 <span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special"><</span><span class="identifier">Real</span><span class="special">,</span> <span class="identifier">Real</span><span class="special">></span> <span class="identifier">left_endpoint_derivatives</span> <span class="special">=</span> <span class="special">{</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special"><</span><span class="identifier">Real</span><span class="special">>::</span><span class="identifier">quiet_NaN</span><span class="special">(),</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limit</span><span class="special"><</span><span class="identifier">Real</span><span class="special">>::</span><span class="identifier">quiet_NaN</span><span class="special">()},</span> 49 <span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special"><</span><span class="identifier">Real</span><span class="special">,</span> <span class="identifier">Real</span><span class="special">></span> <span class="identifier">right_endpoint_derivatives</span> <span class="special">=</span> <span class="special">{</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special"><</span><span class="identifier">Real</span><span class="special">>::</span><span class="identifier">quiet_NaN</span><span class="special">(),</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limit</span><span class="special"><</span><span class="identifier">Real</span><span class="special">>::</span><span class="identifier">quiet_NaN</span><span class="special">()})</span> 50 51 <span class="identifier">cardinal_quintic_b_spline</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span><span class="identifier">Real</span><span class="special">></span> <span class="keyword">const</span> <span class="special">&</span> <span class="identifier">y</span><span class="special">,</span> 52 <span class="identifier">Real</span> <span class="identifier">t0</span> <span class="comment">/* initial time, left endpoint */</span><span class="special">,</span> 53 <span class="identifier">Real</span> <span class="identifier">h</span> <span class="comment">/*spacing, stepsize*/</span><span class="special">,</span> 54 <span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special"><</span><span class="identifier">Real</span><span class="special">,</span> <span class="identifier">Real</span><span class="special">></span> <span class="identifier">left_endpoint_derivatives</span> <span class="special">=</span> <span class="special">{</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special"><</span><span class="identifier">Real</span><span class="special">>::</span><span class="identifier">quiet_NaN</span><span class="special">(),</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limit</span><span class="special"><</span><span class="identifier">Real</span><span class="special">>::</span><span class="identifier">quiet_NaN</span><span class="special">()},</span> 55 <span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special"><</span><span class="identifier">Real</span><span class="special">,</span> <span class="identifier">Real</span><span class="special">></span> <span class="identifier">right_endpoint_derivatives</span> <span class="special">=</span> <span class="special">{</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special"><</span><span class="identifier">Real</span><span class="special">>::</span><span class="identifier">quiet_NaN</span><span class="special">(),</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limit</span><span class="special"><</span><span class="identifier">Real</span><span class="special">>::</span><span class="identifier">quiet_NaN</span><span class="special">()})</span> 56 57 <span class="identifier">Real</span> <span class="keyword">operator</span><span class="special">()(</span><span class="identifier">Real</span> <span class="identifier">t</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span> 58 59 <span class="identifier">Real</span> <span class="identifier">prime</span><span class="special">(</span><span class="identifier">Real</span> <span class="identifier">t</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span> 60 61 <span class="identifier">Real</span> <span class="identifier">double_prime</span><span class="special">(</span><span class="identifier">Real</span> <span class="identifier">t</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span> 62 63<span class="special">};</span> 64<span class="special">}}}</span> 65</pre> 66<h4> 67<a name="math_toolkit.cardinal_quintic_b.h1"></a> 68 <span class="phrase"><a name="math_toolkit.cardinal_quintic_b.cardinal_quintic_b_spline_interp"></a></span><a class="link" href="cardinal_quintic_b.html#math_toolkit.cardinal_quintic_b.cardinal_quintic_b_spline_interp">Cardinal 69 Quintic B-Spline Interpolation</a> 70 </h4> 71<p> 72 The cardinal quintic B-spline interpolator is very nearly the same as the cubic 73 B-spline interpolator, with the modification that the basis functions are constructed 74 by convolving a box function with itself five times, rather than three times 75 as is done with the cubic B-spline. 76 </p> 77<p> 78 The basis functions of the quintic B-spline interpolator are more smooth than 79 the cubic <span class="emphasis"><em>B</em></span>-spline interpolator, and hence this is very 80 useful for computing second derivatives. For example, the second derivative 81 of the cubic spline interpolator is a piecewise linear function, whereas the 82 second derivative of the quintic <span class="emphasis"><em>B</em></span>-spline is a cubic spline. 83 The graph of the second derivative of the quintic <span class="emphasis"><em>B</em></span>-spline 84 is therefore more visually appealing, though whether it is in fact more accurate 85 depends on the smoothness of your data. 86 </p> 87<p> 88 And example usage is as follows: 89 </p> 90<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</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">cardinal_quintic_b_spline</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 91<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">cardinal_quintic_b_spline</span><span class="special">;</span> 92<span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span><span class="keyword">double</span><span class="special">></span> <span class="identifier">v</span><span class="special">(</span><span class="number">512</span><span class="special">);</span> 93<span class="comment">// fill v with data . . .</span> 94<span class="keyword">double</span> <span class="identifier">t0</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="comment">// initial time</span> 95<span class="keyword">double</span> <span class="identifier">h</span> <span class="special">=</span> <span class="number">0.125</span><span class="special">;</span> <span class="comment">// spacing</span> 96<span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special"><</span><span class="keyword">double</span><span class="special">,</span> <span class="keyword">double</span><span class="special">></span> <span class="identifier">left_endpoint_derivatives</span><span class="special">{</span><span class="identifier">first_derivative_at_t0</span><span class="special">,</span> <span class="identifier">second_derivative_at_t0</span><span class="special">};</span> 97<span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special"><</span><span class="keyword">double</span><span class="special">,</span> <span class="keyword">double</span><span class="special">></span> <span class="identifier">right_endpoint_derivatives</span><span class="special">{</span><span class="identifier">first_derivative_at_tf</span><span class="special">,</span> <span class="identifier">second_derivative_at_tf</span><span class="special">};</span> 98<span class="keyword">auto</span> <span class="identifier">qs</span> <span class="special">=</span> <span class="identifier">cardinal_quintic_b_spline</span><span class="special"><</span><span class="keyword">double</span><span class="special">>(</span><span class="identifier">v</span><span class="special">,</span> <span class="identifier">t0</span><span class="special">,</span> <span class="identifier">h</span><span class="special">,</span> <span class="identifier">left_endpoint_derivatives</span><span class="special">,</span> <span class="identifier">right_endpoint_derivatives</span><span class="special">);</span> 99 100<span class="comment">// Evaluate the interpolant at a point:</span> 101<span class="keyword">double</span> <span class="identifier">y</span> <span class="special">=</span> <span class="identifier">qs</span><span class="special">(</span><span class="number">0.1</span><span class="special">);</span> 102<span class="comment">// Evaluate the derivative of the interpolant:</span> 103<span class="keyword">double</span> <span class="identifier">yp</span> <span class="special">=</span> <span class="identifier">qs</span><span class="special">.</span><span class="identifier">prime</span><span class="special">(</span><span class="number">0.1</span><span class="special">);</span> 104<span class="comment">// Evaluate the second derivative of the interpolant:</span> 105<span class="keyword">double</span> <span class="identifier">ypp</span> <span class="special">=</span> <span class="identifier">qs</span><span class="special">.</span><span class="identifier">double_prime</span><span class="special">(</span><span class="number">0.1</span><span class="special">);</span> 106</pre> 107<p> 108 This routine will estimate the endpoint derivatives if they are not provided. 109 <span class="emphasis"><em>Try to avoid this if possible.</em></span> The endpoint derivatives 110 must be evaluated by finite differences and this is not robust again perturbations 111 in the data. So if you have some way of knowing the endpoint derivatives, make 112 sure to provide them. 113 </p> 114<h4> 115<a name="math_toolkit.cardinal_quintic_b.h2"></a> 116 <span class="phrase"><a name="math_toolkit.cardinal_quintic_b.references"></a></span><a class="link" href="cardinal_quintic_b.html#math_toolkit.cardinal_quintic_b.references">References</a> 117 </h4> 118<p> 119 Cox, Maurice G. <span class="emphasis"><em>Numerical methods for the interpolation and approximation 120 of data by spline functions.</em></span> Diss. City, University of London, 1975. 121 </p> 122</div> 123<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 124<td align="left"></td> 125<td align="right"><div class="copyright-footer">Copyright © 2006-2019 Nikhar 126 Agrawal, Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, 127 Hubert Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Matthew Pulver, Johan 128 Råde, Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg, 129 Daryle Walker and Xiaogang Zhang<p> 130 Distributed under the Boost Software License, Version 1.0. (See accompanying 131 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>) 132 </p> 133</div></td> 134</tr></table> 135<hr> 136<div class="spirit-nav"> 137<a accesskey="p" href="cardinal_quadratic_b.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="whittaker_shannon.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> 138</div> 139</body> 140</html> 141