| 1 |
% \iffalse |
| 2 |
% ------------------------------------------------------------------- |
| 3 |
% |
| 4 |
% Copyright 2002--2004, Daniel H. Luecking |
| 5 |
% |
| 6 |
% Splines.mp may be distributed and/or modified under the conditions of the |
| 7 |
% LaTeX Project Public License, either version 1.3 of this license or (at |
| 8 |
% your option) any later version. The latest version of this license is in |
| 9 |
% <http://www.latex-project.org/lppl.txt> |
| 10 |
% and version 1.3 or later is part of all distributions of LaTeX version |
| 11 |
% 2003/12/01 or later. |
| 12 |
% |
| 13 |
% Splines has maintenance status "author-maintained". The Current Maintainer |
| 14 |
% is Daniel H. Luecking. The Base Interpreter is MetaPost (or Metafont). |
| 15 |
% |
| 16 |
%<*driver> |
| 17 |
\ProvidesFile{splines.dtx} |
| 18 |
[2005/02/05 v0.2 Metafont/post macros to compute splines.]% |
| 19 |
\documentclass[draft]{ltxdoc} |
| 20 |
\usepackage{docmfp} |
| 21 |
|
| 22 |
\addtolength{\textwidth}{.5878pt} |
| 23 |
|
| 24 |
\def\mytt{\upshape\mdseries\ttfamily} |
| 25 |
\renewcommand\marg[1]{{\mytt \{#1\}}} |
| 26 |
\renewcommand\oarg[1]{{\mytt [#1]}} |
| 27 |
\renewcommand\parg[1]{{\mytt (#1)}} |
| 28 |
\renewcommand{\meta}[1]{{$\langle$\rmfamily\itshape#1$\rangle$}} |
| 29 |
\DeclareRobustCommand\cs[1]{{\mytt\char`\\#1}} |
| 30 |
\def\prog#1{{\mdseries\scshape #1}} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
\def\MF{\prog{meta\-font}} |
| 36 |
\def\MP{\prog{meta\-post}} |
| 37 |
|
| 38 |
\def\CMF{\prog{Meta\-font}} |
| 39 |
\def\CMP{\prog{Meta\-post}} |
| 40 |
\def\opt#1{{\sffamily\upshape#1}} |
| 41 |
\def\mfc#1{{\mytt#1}} |
| 42 |
\let\env\mfc |
| 43 |
\let\file\mfc |
| 44 |
\let\gbc\mfc |
| 45 |
\renewcommand\{{{\mytt\char`\{}} |
| 46 |
\renewcommand\}{{\mytt\char`\}}} |
| 47 |
\renewcommand\|{${}\mathrel{|}{}$} |
| 48 |
|
| 49 |
\makeatletter |
| 50 |
\newcommand\bsl{{\mytt\@backslashchar}} |
| 51 |
% Stupid lists! |
| 52 |
\def\@listi{\leftmargin\leftmargini |
| 53 |
\parsep \z@ \@plus\p@ \@minus\z@ |
| 54 |
\topsep 4\p@ \@plus\p@ \@minus2\p@ |
| 55 |
\itemsep\parsep} |
| 56 |
\let\@listI\@listi \@listi |
| 57 |
\renewcommand\labelitemi{\normalfont\bfseries \textendash} |
| 58 |
\renewcommand\labelitemii{\textasteriskcentered} |
| 59 |
\renewcommand\labelitemiii{\textperiodcentered} |
| 60 |
\leftmargini\parindent |
| 61 |
% Stupid index! |
| 62 |
\def\usage#1{\textrm{#1}} |
| 63 |
\def\index@prologue{\section*{Index}\markboth{Index}{Index}Numbers |
| 64 |
refer to the page where the corresponding entry is described.} |
| 65 |
\def\IndexParms{% |
| 66 |
\parindent \z@ \columnsep 15pt |
| 67 |
\parskip 0pt plus 1pt |
| 68 |
\rightskip 5pt plus2em \mathsurround \z@ |
| 69 |
\parfillskip=-5pt \small |
| 70 |
% less hanging: |
| 71 |
\def\@idxitem{\par\hangindent 20pt}% |
| 72 |
\def\subitem{\@idxitem\hspace*{15pt}}% |
| 73 |
\def\subsubitem{\@idxitem\hspace*{25pt}}% |
| 74 |
\def\indexspace{\par\vspace{10pt plus 2pt minus 3pt}}} |
| 75 |
\renewcommand\routinestring{} |
| 76 |
\renewcommand\variablestring{\space(var.)} |
| 77 |
% Why does every command have to be indexed twice? |
| 78 |
\renewcommand\SpecialMfpIndex[3]{\@bsphack |
| 79 |
\index{% |
| 80 |
\string#1\actualchar |
| 81 |
\string\verb\quotechar*\verbatimchar\string#1\verbatimchar |
| 82 |
#2 \encapchar usage}% |
| 83 |
\@esphack} |
| 84 |
\def\close@crossref{\SpecialEscapechar{:}} |
| 85 |
\makeatother |
| 86 |
\def\VariableIndex#1{\SpecialMfpIndex{#1}{\variablestring}{}} |
| 87 |
\def\RoutineIndex #1{\SpecialMfpIndex{#1}{}{}} |
| 88 |
|
| 89 |
\title{Macros to Compute Splines\thanks{This file has version number |
| 90 |
\fileversion, last revised \filedate.}} |
| 91 |
\author{Dan Luecking} |
| 92 |
\date{\filedate} |
| 93 |
\SpecialEscapechar{:} |
| 94 |
\def\bslash{:} |
| 95 |
\DisableCrossrefs |
| 96 |
\CodelineIndex |
| 97 |
\AlsoImplementation |
| 98 |
|
| 99 |
\begin{document} |
| 100 |
\DeleteShortVerb{\|} |
| 101 |
\DocInput{splines.dtx} |
| 102 |
\end{document} |
| 103 |
%</driver> |
| 104 |
%\fi |
| 105 |
% |
| 106 |
% \CheckSum{25} |
| 107 |
% \CharacterTable |
| 108 |
% {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z |
| 109 |
% Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z |
| 110 |
% Digits \0\1\2\3\4\5\6\7\8\9 |
| 111 |
% Exclamation \! Double quote \" Hash (number) \# |
| 112 |
% Dollar \$ Percent \% Ampersand \& |
| 113 |
% Acute accent \' Left paren \( Right paren \) |
| 114 |
% Asterisk \* Plus \+ Comma \, |
| 115 |
% Minus \- Point \. Solidus \/ |
| 116 |
% Colon \: Semicolon \; Less than \< |
| 117 |
% Equals \= Greater than \> Question mark \? |
| 118 |
% Commercial at \@ Left bracket \[ Backslash \\ |
| 119 |
% Right bracket \] Circumflex \^ Underscore \_ |
| 120 |
% Grave accent \` Left brace \{ Vertical bar \| |
| 121 |
% Right brace \} Tilde \~} |
| 122 |
% |
| 123 |
% \catcode`\_=12 |
| 124 |
% \GetFileInfo{splines.dtx} |
| 125 |
% \maketitle |
| 126 |
% |
| 127 |
% \StopEventually{\PrintIndex} |
| 128 |
% A cubic spline through a set of points is a curve obtained by joining |
| 129 |
% each point to the next with a cubic parametrized curve, where adjoining |
| 130 |
% cubics must have matching first and second derivative at their common |
| 131 |
% point. |
| 132 |
% |
| 133 |
% It is possible for \MP{} to compute the necessary controls. |
| 134 |
% Unfortunately, the controls are not uniquely determined unless the curve |
| 135 |
% is required to be closed. For open curves, there is need for two |
| 136 |
% additional equations at the end points. A `relaxed spline' is produced |
| 137 |
% if we require that the second derivative is $(0,0)$ at those points. |
| 138 |
% For a closed curve, the equality of the first and second derivatives at |
| 139 |
% the common beginning/ending point gives the needed additional equations. |
| 140 |
% |
| 141 |
% Note that this equates \emph{time} derivatives, so this works best when |
| 142 |
% points are relatively evenly spaced and so the speed is relatively |
| 143 |
% uniform. If points are differently spaced then the relatively slower |
| 144 |
% speed between closely spaced points allows sharper turns without large |
| 145 |
% second derivatives. Curves produced tend to have a more natural look, |
| 146 |
% and relaxed splines are most suitable for smoothing data that is |
| 147 |
% obtained by taking observations at evenly spaced times. Still, the |
| 148 |
% technique is somewhat unstable when points are closely spaced, for |
| 149 |
% example when a small change in the position of one point can produce a |
| 150 |
% large change in its direction when viewed from another point. |
| 151 |
% |
| 152 |
% Start with version control information. |
| 153 |
% \begin{macrocode} |
| 154 |
%<*package> |
| 155 |
if known splines_fileversion: endinput fi; |
| 156 |
string splines_fileversion; |
| 157 |
splines_fileversion := "2006/09/25, v0.2a"; |
| 158 |
message "Loading splines.mp " & splines_fileversion; |
| 159 |
|
| 160 |
% \end{macrocode} |
| 161 |
% |
| 162 |
% \DescribeRoutine{list_to_array} |
| 163 |
% Now for a command that takes a variable name \gbc{(suffix arr)} and |
| 164 |
% copies a list of pairs to \mfc{arr1}, \mfc{arr2}, etc.. |
| 165 |
% The suffix must be declared by the calling program so that \mfc{arr} |
| 166 |
% is numeric but \mfc{arr[\,]} are pairs, for example by ``\mfc{save arr; pair |
| 167 |
% arr[\,];}''. |
| 168 |
% \begin{macrocode} |
| 169 |
def list_to_array (suffix arr) (text list) = |
| 170 |
arr := 0; |
| 171 |
for _itm = list : |
| 172 |
arr[incr arr] := _itm; |
| 173 |
endfor |
| 174 |
enddef; |
| 175 |
|
| 176 |
% \end{macrocode} |
| 177 |
% |
| 178 |
% \DescribeRoutine{compute_spline} |
| 179 |
% In this command we generate the equations common to all cubic |
| 180 |
% splines: the equality of derivatives at all interior points. |
| 181 |
% This command accepts three suffixes: \gbc{points}, \gbc{pr}, and |
| 182 |
% \gbc{po} which should represent previously declared arrays of pairs. |
| 183 |
% \gbc{points} is the array of points to be connected, and must be known. |
| 184 |
% Arrays \gbc{pr} and \gbc{po} \emph{must} be unknown and will hold the |
| 185 |
% computed control points. See the code of \mfc{mkrelaxedspline} for |
| 186 |
% an example of how this was arranged for the variables \mfc{rs_pr} and |
| 187 |
% \mfc{rs_po}. |
| 188 |
% |
| 189 |
% Contrary to the previous (unreleased) version, \mfc{compute_spline} |
| 190 |
% takes a boolean argument and appends the same equations at the first (= |
| 191 |
% last) point. |
| 192 |
% |
| 193 |
% \DescribeRoutine{mkrelaxedspline} |
| 194 |
% The first of these macros appends the necessary additional equations to |
| 195 |
% get zero second derivatives at the ends. |
| 196 |
% \DescribeRoutine{mkclosedspline} |
| 197 |
% The second simply calls \mfc{compute_spline} with the boolean set to |
| 198 |
% true. Both return the computed path. In theory the knowledgeable user |
| 199 |
% can call \gbc{compute_spline (false)}, append a choice of equations for |
| 200 |
% the ends, and then call \gbc{mksplinepath (false)}. |
| 201 |
% |
| 202 |
% \DescribeRoutine{dospline} |
| 203 |
% This version accepts a list of pairs and produces a spline through |
| 204 |
% them. It simply stores the list in an array and calls the appropriate |
| 205 |
% version that operates on an array. |
| 206 |
% \begin{macrocode} |
| 207 |
def compute_spline (expr closed) (suffix points, pr, po) = |
| 208 |
% interior equations: |
| 209 |
for j= 2 upto points - 1 : |
| 210 |
% equate first derivatives: |
| 211 |
po[j] + pr[j] = 2 points[j]; |
| 212 |
% and second derivatives: |
| 213 |
pr[j+1] + 2 pr[j] = 2 po[j] + po[j-1]; |
| 214 |
endfor |
| 215 |
% for a closed curve, the first and last are also interior: |
| 216 |
if closed: |
| 217 |
po 1 + pr 1 = 2 points 1; |
| 218 |
po[points] + pr[points] = 2 points[points]; |
| 219 |
pr 2 + 2 pr 1 = 2 po 1 + po[points]; |
| 220 |
pr 1 + 2 pr[points] = 2 po[points] + po[points-1]; |
| 221 |
fi |
| 222 |
enddef; |
| 223 |
|
| 224 |
vardef mksplinepath (expr closed) (suffix points, pr, po) = |
| 225 |
points1..controls po1 and |
| 226 |
for j = 2 upto points if not closed: -1 fi: |
| 227 |
pr[j]..points[j]..controls po[j] and |
| 228 |
endfor |
| 229 |
if closed: pr 1..cycle else: pr[points]..points[points] fi |
| 230 |
enddef; |
| 231 |
|
| 232 |
vardef mkrelaxedspline (suffix pnts) = |
| 233 |
save rs_pr, rs_po; |
| 234 |
pair rs_po[], rs_pr[]; |
| 235 |
% Equate second derivative to zero at both end points |
| 236 |
rs_pr 2 + pnts 1 = 2 rs_po 1 ; |
| 237 |
pnts[pnts] + rs_po[pnts-1] = 2 rs_pr[pnts]; |
| 238 |
compute_spline (false) (pnts, rs_pr, rs_po); |
| 239 |
mksplinepath (false) (pnts, rs_pr, rs_po) |
| 240 |
enddef; |
| 241 |
|
| 242 |
vardef mkclosedspline (suffix pnts) = |
| 243 |
save cs_pr, cs_po; |
| 244 |
pair cs_pr[], cs_po[]; |
| 245 |
compute_spline (true) (pnts, cs_pr, cs_po); |
| 246 |
mksplinepath (true) (pnts, cs_pr, cs_po) |
| 247 |
enddef; |
| 248 |
|
| 249 |
vardef dospline (expr closed) (text the_list) = |
| 250 |
save _sp; pair _sp[]; |
| 251 |
list_to_array (_sp) (the_list); |
| 252 |
if closed : |
| 253 |
mkclosedspline (_sp) |
| 254 |
else: |
| 255 |
mkrelaxedspline (_sp) |
| 256 |
fi |
| 257 |
enddef; |
| 258 |
|
| 259 |
% \end{macrocode} |
| 260 |
% The above computations produce a $2$-dimensional spline. A $1$-dimensional |
| 261 |
% cubic spline would be a function $f(t)$ with numeric values rather |
| 262 |
% than pair values. Such are often used to interpolate functions. That is, |
| 263 |
% given pairs $(x\sb j,y\sb{j})$, and assuming they lie on the graph of |
| 264 |
% some function (generally unknown), fill in the graph with $y = f(x)$ |
| 265 |
% where $f$ is a cubic function of $x$ in each interval $x\sb j < x < x\sb |
| 266 |
% {j+1}$, making sure that the resulting graph is as smooth as possible at |
| 267 |
% the points $x\sb j$. |
| 268 |
% |
| 269 |
% The requirements on our $2$-dimensional path are the following: |
| 270 |
% \begin{enumerate} |
| 271 |
% \item The $j$th link should connect $(x\sb{j},y\sb{j})$ to $(x\sb{j+1}, |
| 272 |
% y\sb{j+1})$. |
| 273 |
% \item The $x$-part of that link should increase linearly from $x\sb{j}$ to |
| 274 |
% $x\sb{j+1}$ as $t$ goes from $0$ to $1$. |
| 275 |
% \item The $y$-part should be a cubic $y = f(x)$. |
| 276 |
% \item The $x$-derivatives $df/dx$ and $d^2f/dx^2$ should match at the |
| 277 |
% connecting points. |
| 278 |
% \end{enumerate} |
| 279 |
% |
| 280 |
% Two necessary equations for converting between $x$ and $t$ coordinates |
| 281 |
% are: |
| 282 |
% \begin{equation}\label{first} |
| 283 |
% x = x\sb{j} + t \Delta x\sb{j} |
| 284 |
% \end{equation} |
| 285 |
% (where $\Delta x\sb{j} = x\sb{j+1} - x\sb{j}$) and |
| 286 |
% \begin{equation}\label{second} |
| 287 |
% \frac{df}{dt} = \frac{dx}{dt}\frac{df}{dx} = (x\sb{j+1} - |
| 288 |
% x\sb{j}) \frac{df}{dx}. |
| 289 |
% \end{equation} |
| 290 |
% Thus we want to choose controls so that (\ref{first}) is maintained and |
| 291 |
% so that $x$-derivatives match. It turns out that this requires controls |
| 292 |
% at |
| 293 |
% \begin{equation} |
| 294 |
% \begin{array}{c} |
| 295 |
% (x\sb{j}, y\sb{j}) - (\Delta x\sb{j-1}, s\sb{j} \Delta x\sb{j-1})/3\\ |
| 296 |
% (x\sb{j}, y\sb{j}) + (\Delta x\sb{j} , s\sb{j} \Delta x\sb{j} )/3 |
| 297 |
% \end{array} |
| 298 |
% \end{equation} |
| 299 |
% where $s\sb{j}$ is the slope (derivative) at $x\sb{j}$. These control |
| 300 |
% points will produce matching slopes regardless of the values |
| 301 |
% chosen for the $s\sb j$. To get matching second derivatives we need the |
| 302 |
% same conditions as in parametric splines. But those equations simplify |
| 303 |
% to the form: |
| 304 |
% \begin{displaymath} |
| 305 |
% s\sb{j+1} dx\sb{j} - 2s\sb{j} (dx\sb{j} + dx\sb{j-1}) + |
| 306 |
% s\sb{j-1}dx\sb{j-1} |
| 307 |
% = 3y\sb{j+1} - 3y\sb{j-1}. |
| 308 |
% \end{displaymath} |
| 309 |
% As with 2-D splines there can be almost any equations at the end points. |
| 310 |
% For a relaxed spline we equate the second derivatives to 0. To get a |
| 311 |
% periodic function, we equate the slope and second derivative at |
| 312 |
% beginning to those at the end. This makes it possible to put a shifted |
| 313 |
% copy of the graph with starting point at the end of the original and |
| 314 |
% have the same smoothness at that connection as at the other points. |
| 315 |
% |
| 316 |
% \DescribeRoutine{compute_fcnspline} |
| 317 |
% This issues the equation for the slopes (array \gbc{sl} of |
| 318 |
% \emph{unknown} numerics). The array \gbc{points} contains the $(x,y)$ |
| 319 |
% values and \gbc{dx} is a temporary numeric array which will be |
| 320 |
% overwritten if known. |
| 321 |
% |
| 322 |
% \DescribeRoutine{mkfcnsplinepath} |
| 323 |
% This simply assembles the path from the information computed by the |
| 324 |
% above equations (and the extra equations given in the calling command). |
| 325 |
% |
| 326 |
% \DescribeRoutine{mkrelaxedfcnspline} |
| 327 |
% This sets up arrays for the \gbc{dx} and \gbc{sl} parameters of |
| 328 |
% \mfc{compute_fcnspline}, emit the necessary endpoint equations (zero |
| 329 |
% second derivatives) and calls the previous two routines. |
| 330 |
% |
| 331 |
% \DescribeRoutine{mkperiodicfcnspline} |
| 332 |
% This does the same as the previous command, but the endpoint equations |
| 333 |
% make the first and second derivatives at the start equal to those at |
| 334 |
% the end. |
| 335 |
% |
| 336 |
% \DescribeRoutine{fcnspline} |
| 337 |
% Finally, this command copies a list of pairs into an array and calls the |
| 338 |
% appropriate command to process them. |
| 339 |
% \begin{macrocode} |
| 340 |
def compute_fcnspline (suffix points, dx, sl) = |
| 341 |
% Get delta_x: |
| 342 |
for j = 1 upto points - 1: dx[j] := xpart (points[j+1]-points[j]); |
| 343 |
endfor |
| 344 |
for j=2 upto points - 1: |
| 345 |
sl[j + 1] * dx[j] + 2sl[j]*(dx[j] + dx[j-1]) + sl[j-1]*dx[j-1] |
| 346 |
= 3*ypart(points[j+1] - points[j-1]); |
| 347 |
endfor |
| 348 |
enddef; |
| 349 |
|
| 350 |
vardef mkfcnsplinepath (suffix points, dx, sl) = |
| 351 |
points1..controls (points1 + (1, sl1)*dx1/3) and |
| 352 |
for j = 2 upto points - 1: |
| 353 |
(points[j] - (1, sl[j])*dx[j-1]/3) ..points[j].. |
| 354 |
controls (points[j] + (1,sl[j])*dx[j]/3) and |
| 355 |
endfor |
| 356 |
(points[points] - (1,sl[points])*dx[points-1]/3)..points[points] |
| 357 |
enddef; |
| 358 |
|
| 359 |
vardef mkperiodicfcnspline (suffix pnts) = |
| 360 |
save _sl, _dx; numeric _dx[], _sl[]; |
| 361 |
compute_fcnspline (pnts, _dx, _sl); |
| 362 |
% periodicity equations: |
| 363 |
_sl 1 = _sl[pnts]; |
| 364 |
_sl 2 * _dx 1 + 2 _sl 1 * _dx 1 + 2 _sl[pnts] * _dx[pnts-1] |
| 365 |
+ _sl[pnts-1] * _dx[pnts-1] |
| 366 |
= 3 * ypart(pnts[2] - pnts[pnts-1]); |
| 367 |
mkfcnsplinepath (pnts, _dx, _sl) |
| 368 |
enddef; |
| 369 |
|
| 370 |
vardef mkrelaxedfcnspline (suffix pnts) = |
| 371 |
save _sl, _dx; numeric _dx[], _sl[]; |
| 372 |
compute_fcnspline (pnts, _dx, _sl); |
| 373 |
% relaxation equations. |
| 374 |
_sl 2 * _dx 1 + 2 _sl1 * _dx 1 = 3 * ypart(pnts2 - pnts1); |
| 375 |
_sl[pnts-1] * _dx[pnts-1] + 2 _sl[pnts] * _dx[pnts-1] |
| 376 |
= 3 * ypart(pnts[pnts] - pnts[pnts-1]); |
| 377 |
mkfcnsplinepath (pnts, _dx, _sl) |
| 378 |
enddef; |
| 379 |
|
| 380 |
vardef fcnspline (expr periodic) (text the_list) = |
| 381 |
save _fs; pair _fs[]; |
| 382 |
list_to_array (_fs) (the_list); |
| 383 |
if periodic: |
| 384 |
mkperiodicfcnspline (_fs) |
| 385 |
else: |
| 386 |
mkrelaxedfcnspline (_fs) |
| 387 |
fi |
| 388 |
enddef; |
| 389 |
|
| 390 |
%</package> |
| 391 |
% \end{macrocode} |
| 392 |
% \clearpage |
| 393 |
%\Finale |