labplot  2.8.2
About: LabPlot is an application for plotting and analysis of 2D and 3D functions and data. It is a complete rewrite of LabPlot1 and lacks in the first release a lot of features available in the predecessor. On the other hand, the GUI and the usability is more superior.
  Fossies Dox: labplot-2.8.2.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

nsl_interp.c
Go to the documentation of this file.
1 /***************************************************************************
2  File : nsl_interp.c
3  Project : LabPlot
4  Description : NSL interpolation functions
5  --------------------------------------------------------------------
6  Copyright : (C) 2016 by Stefan Gerlach (stefan.gerlach@uni.kn)
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  * This program is distributed in the hope that it will be useful, *
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19  * GNU General Public License for more details. *
20  * *
21  * You should have received a copy of the GNU General Public License *
22  * along with this program; if not, write to the Free Software *
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
24  * Boston, MA 02110-1301 USA *
25  * *
26  ***************************************************************************/
27 
28 #include "nsl_interp.h"
29 #include "nsl_common.h"
30 
31 const char* nsl_interp_type_name[] = { i18n("linear"), i18n("polynomial"), i18n("cubic spline (natural)"), i18n("cubic spline (periodic)"),
32  i18n("Akima-spline (natural)"), i18n("Akima-spline (periodic)"), i18n("Steffen spline"), i18n("cosine"), i18n("exponential"),
33  i18n("piecewise cubic Hermite (PCH)"), i18n("rational functions") };
34 const char* nsl_interp_pch_variant_name[] = { i18n("finite differences"), i18n("Catmull-Rom"), i18n("cardinal"), i18n("Kochanek-Bartels (TCB)")};
35 const char* nsl_interp_evaluate_name[] = { i18n("function"), i18n("derivative"), i18n("second derivative"), i18n("integral")};
36 
37 int nsl_interp_ratint(double *x, double *y, int n, double xn, double *v, double *dv) {
38  int i, a = 0, b = n-1;
39  while (b-a > 1) { /* find interval using bisection */
40  int j = (int)floor((a+b)/2.);
41  if (x[j] > xn)
42  b = j;
43  else
44  a = j;
45  }
46 
47  int ns=a; /* nearest index */
48  if (fabs(xn-x[a]) > fabs(xn-x[b]))
49  ns=b;
50 
51  if (xn == x[ns]) { /* exact point */
52  *v = y[ns];
53  *dv = 0;
54  return 1;
55  }
56 
57  double *c = (double*)malloc(n*sizeof(double));
58  double *d = (double*)malloc(n*sizeof(double));
59  for (i=0; i < n; i++)
60  c[i] = d[i] = y[i];
61 
62  *v = y[ns--];
63 
64  double t, dd;
65  int m;
66  for (m=1; m < n; m++) {
67  for (i=0; i < n-m; i++) {
68  t = (x[i]-xn)*d[i]/(x[i+m]-xn);
69  dd = t-c[i+1];
70  if (dd == 0.0) /* pole */
71  dd += DBL_MIN;
72  dd = (c[i+1]-d[i])/dd;
73  d[i] = c[i+1]*dd;
74  c[i] = t*dd;
75  }
76 
77  *dv = (2*(ns+1) < (n-m) ? c[ns+1] : d[ns--]);
78  *v += *dv;
79  }
80 
81  free(c); free(d);
82 
83  return 0;
84 }
#define i18n(m)
Definition: nsl_common.h:38
int nsl_interp_ratint(double *x, double *y, int n, double xn, double *v, double *dv)
Definition: nsl_interp.c:37
const char * nsl_interp_pch_variant_name[]
Definition: nsl_interp.c:34
const char * nsl_interp_type_name[]
Definition: nsl_interp.c:31
const char * nsl_interp_evaluate_name[]
Definition: nsl_interp.c:35