File: interpol.mac

package info (click to toggle)
maxima 5.27.0-3
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 120,648 kB
  • sloc: lisp: 322,503; fortran: 14,666; perl: 14,343; tcl: 11,031; sh: 4,146; makefile: 2,047; ansic: 471; awk: 24; sed: 10
file content (333 lines) | stat: -rw-r--r-- 17,078 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
/*               COPYRIGHT NOTICE

Copyright (C) 2005-2010 Mario Rodriguez Riotorto

This program is free software; you can redistribute
it and/or modify it under the terms of the
GNU General Public License as published by
the Free Software Foundation; either version 2 
of the License, or (at your option) any later version. 

This program is distributed in the hope that it
will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the 
GNU General Public License for more details at
http://www.gnu.org/copyleft/gpl.html
*/


/*             INTRODUCTION

This package defines some interpolation techniques.

For questions, suggestions, bugs and the like, feel free
to contact me at

mario @@@ edu DOT xunta DOT es

*/



/* Returns de input in the form of a list of pairs, ordered wrt the first */
/* coordinate. The argument must be either:                               */
/*  a) a two column matrix, p:matrix([2,4],[5,6],[9,3])                   */
/*  b) a list of pairs, p: [[2,4],[5,6],[9,3]]                            */
/*  c) a list of numbers, p: [4,6,3], in which case the abscissas will be */
/*     assigned automatically to 1, 2, 3, etc.                            */
/* This is an uxiliary function for the 'interpol' package.               */
interpol_check_input(data,funame):=
 block([n,out],
   if not listp(data) and not matrixp(data)
      then error("Argument to '",funame,"' must be a list or matrix"),
   n: length(data),
   if n<2
      then error("Argument to '",funame,"' has too few sample points")
   elseif listp(data) and every('identity,map(lambda([x], listp(x) and length(x)=2),data))
      then out: sort(data)
   elseif matrixp(data) and length(data[1]) = 2
      then out: sort(args(data))
   elseif listp(data) and every('identity,map(lambda([x], not listp(x)),data)) 
      then out: makelist([i,data[i]],i,1,n)
      else error("Error in arguments to '",funame,"' function"),
   /* controlling duplicated x's */
   for i:2 thru n do
      if out[i-1][1] = out[i][1]
         then error("Duplicated abscissas are not allowed"),
   out )$



/* Lagrangian interpolation. The argument must be either:                      */
/*  a) a two column matrix, p:matrix([2,4],[5,6],[9,3])                        */
/*  b) a list of pairs, p: [[2,4],[5,6],[9,3]]                                 */
/*  c) a list of numbers, p: [4,6,3], in which case the abscissas will be      */
/*     assigned automatically to 1, 2, 3, etc.                                 */
/* In cases a) and b) the pairs are ordered wrt the 1st. coordinate before any */
/* computation is made. Option:                                                */
/*   'varname='x: the name of the independent variable                         */
/* Sample session:                                                             */
/* load(interpol);                                                             */
/* p:[[7,2],[8,2],[1,5],[3,2],[6,7]]$                                          */
/* lagrange(p);                                                                */
/* f(x):=''%;                                                                  */
/* map(f,[2.3,5/7,%pi]);                                                       */
/* load(draw)$;                                                                */
/* draw2d(                                                                     */
/*    explicit(f(x),x,0,9),                                                    */
/*    point_size = 3,                                                          */
/*    points(p)) $                                                             */
lagrange(tab,[select]) := block([n,sum:0,prod,options, defaults,ratprint:false],
   tab: interpol_check_input(tab,"lagrange"),
   options:  ['varname],
   defaults: ['x],
   for i in select do(
      aux: ?position(lhs(i),options),
      if numberp(aux) and aux <= length(options) and aux >= 1
        then defaults[aux]: rhs(i)),
   if not symbolp(defaults[1])
      then error("Option 'varname' is not correct"),

   /* constructing the interpolating polynomial */
   n: length(tab),
   for i:1 thru n do(
      prod: 1,
      for k:1 thru n do
         if k#i then prod: prod * (defaults[1]-tab[k][1]) / (tab[i][1]-tab[k][1]),
      sum: sum + prod * tab[i][2] ),
   sum )$



/* Characteristic function for intervals. Returns true iif  */
/* z belongs to [l1, l2). This is an auxiliary function to  */
/* be called from linearinterpol and cspline                */
charfun2(z,l1,l2):= charfun(l1 <= z and z < l2)$



/* Linear interpolation. The argument must be either:                          */
/*  a) a two column matrix, p:matrix([2,4],[5,6],[9,3])                        */
/*  b) a list of pairs, p: [[2,4],[5,6],[9,3]]                                 */
/*  c) a list of numbers, p: [4,6,3], in which case the abscissas will be      */
/*     assigned automatically to 1, 2, 3, etc.                                 */
/* In cases a) and b) the pairs are ordered wrt the 1st. coordinate before any */
/* computation is made. Option:                                                */
/*   'varname='x: the name of the independent variable                         */
/* Sample session:                                                             */
/* load(interpol);                                                             */
/* p:[[7,2],[8,2],[1,5],[3,2],[6,7]]$                                          */
/* linearinterpol(p);                                                          */
/* f(x):=''%;                                                                  */
/* map(f,[2.3,5/7,%pi]);                                                       */
/* load(draw)$;                                                                */
/* draw2d(                                                                     */
/*    explicit(f(x),x,0,9),                                                    */
/*    point_size = 3,                                                          */
/*    points(p)) $                                                             */
linearinterpol(tab,[select]) := block([n,s:0,a,b,options, defaults,ratprint:false],
   tab: interpol_check_input(tab,"linearinterpol"),
   options:  ['varname],
   defaults: ['x],
   for i in select do(
      aux: ?position(lhs(i),options),
      if numberp(aux) and aux <= length(options) and aux >= 1
        then defaults[aux]: rhs(i)),
   if not symbolp(defaults[1])
      then error("Option 'varname' is not correct"),

   /* constructing the interpolating polynomial */
   n: length(tab),
   if n=2 /* case of two points */
      then s: tab[2][2] + (tab[2][2]-tab[1][2]) *
                          (defaults[1]-tab[2][1]) /
                          (tab[2][1]-tab[1][1])
      else for i:2 thru n do(
               if i=2
                  then (a: 'minf, b: tab[i][1])
                  else if i=n
                          then (a: tab[i-1][1], b: 'inf)
                          else (a: tab[i-1][1], b: tab[i][1]),
               s: s + funmake('charfun2,[defaults[1], a, b]) *
                      expand( tab[i][2] + (tab[i][2]-tab[i-1][2]) *
                                    (defaults[1]-tab[i][1]) /
                                    (tab[i][1]-tab[i-1][1]) )   ),
   s )$



/* Cubic splines interpolation. The argument must be either:                           */
/*  a) a two column matrix, p:matrix([2,4],[5,6],[9,3])                                */
/*  b) a list of pairs, p: [[2,4],[5,6],[9,3]]                                         */
/*  c) a list of numbers, p: [4,6,3], in which case the abscissas will be              */
/*     assigned automatically to 1, 2, 3, etc.                                         */
/* In cases a) and b) the pairs are ordered wrt the 1st. coordinate before any         */
/* computation is made. Options:                                                       */
/*   'd1='unknown: 1st derivative at x_1; if it is 'unknown, the second derivative     */
/*         at x_1 is made equal to 0 (natural cubic spline); if it is equal to a       */
/*         number, the second derivative is estimated based on this number             */
/*   'd2='unknown: 1st derivative at x_n; if it is 'unknown, the second derivative     */
/*         at x_n is made equal to 0 (natural cubic spline); if it is equal to a       */
/*         number, the second derivative is estimated based on this number             */
/*   'varname='x: the name of the independent variable                                 */
/* Reference: this algorithm is based on 'Numerical Recipes in C', section 3.3         */
/* Sample session:                                                                     */
/* load(interpol);                                                                     */
/* p:[[7,2],[8,2],[1,5],[3,2],[6,7]]$                                                  */
/* cspline(p); ==> natural cubic spline (second derivatives are zero in both extremes) */
/* f(x):=''%;                                                                          */
/* map(f,[2.3,5/7,%pi]);                                                               */
/* load(draw)$;                                                                        */
/* draw2d(                                                                             */
/*    explicit(f(x),x,0,9),                                                            */
/*    point_size = 3,                                                                  */
/*    points(p)) $                                                                     */
/* cspline(p,d1=0,dn=0);                                                               */
/* g(x):=''%;                                                                          */
/* draw2d(                                                                             */
/*    explicit(g(x),x,0,9),                                                            */
/*    point_size = 3,                                                                  */
/*    points(p)) $                                                                     */
cspline(tab,[select]):= block([options, defaults, n, aux, y2, u, sig, p,
                               qn, un, a, b, s:0, aj, bj, cj, dj, aux,ratprint:false],
   tab: interpol_check_input(tab,"cspline"),
   options:  ['d1, 'dn, 'varname],
   defaults: ['unknown, 'unknown, 'x],
   for i in select do(
      aux: ?position(lhs(i),options),
      if numberp(aux) and aux <= length(options) and aux >= 1
        then defaults[aux]: rhs(i)),
   if not numberp(defaults[1]) and  defaults[1] # 'unknown
      then error("Option 'd1' is not correct"),
   if not numberp(defaults[2]) and  defaults[2] # 'unknown
      then error("Option 'dn' is not correct"),
   if not symbolp(defaults[3])
      then error("Option 'varname' is not correct"),

   /* if tab contains only two points, linear interpolation */
   n: length(tab),
   if n=2 /* case of two points */
      then return(ratsimp( tab[2][2] + (tab[2][2]-tab[1][2]) *
                                       (defaults[3]-tab[2][1]) /
                                       (tab[2][1]-tab[1][1]))),


   /* constructing the interpolating polynomial */
   y2: makelist(0,i,1,n),
   u: makelist(0,i,1,n-1),

   /* controlling the lower boundary condition */
   if /*d1*/ defaults[1] = 'unknown
      then (y2[1]: 0,
            u[1]: 0)
      else (y2[1]: -1/2,
            u[1]: 3 / (tab[2][1]-tab[1][1]) *
                      ((tab[2][2] - tab[1][2])/(tab[2][1] - tab[1][1]) - defaults[1]) ),

   /* decomposition loop of the triangular algorithm */
   for i:2 thru n-1 do (
      sig: (tab[i][1] - tab[i-1][1]) / (tab[i+1][1] - tab[i-1][1]),
      p: sig * y2[i-1] + 2,
      y2[i]: (sig - 1) / p,
      u[i]: (tab[i+1][2] - tab[i][2]) /(tab[i+1][1] - tab[i][1]) -
            (tab[i][2] - tab[i-1][2]) /(tab[i][1] - tab[i-1][1]),
      u[i]: (6 * u[i] / (tab[i+1][1] - tab[i-1][1]) - sig * u[i-1]) / p ) ,

   /* controlling the upper boundary condition */
   if /*dn*/ defaults[2] = 'unknown
      then (qn: 0,
            un: 0)
      else (qn: 1/2,
            un: 3 / (tab[n][1] - tab[n-1][1]) *
                (defaults[2] - (tab[n][2] - tab[n-1][2]) / (tab[n][1] - tab[n-1][1]))),
   y2[n]: (un - qn * u[n-1]) / (qn * y2[n-1] + 1),

   /* backsubstitution loop of the tridiagonal algorithm */
   for k: n-1 thru 1 step -1 do
      y2[k]: y2[k] * y2[k+1] + u[k],

   /* constructing the cubic splines */
   for j:2 thru n do (
      if j=2
          then (a: 'minf, b: tab[j][1] )
          else if j=n
                  then (a: tab[j-1][1], b: 'inf)
                  else (a: tab[j-1][1], b: tab[j][1]),
      /* in the following sentences, defaults[3] is variable's name */
      aux: (tab[j][1] - tab[j-1][1]),
      aj: (tab[j][1] - defaults[3]) / aux,
      bj: (defaults[3] - tab[j-1][1]) / aux,
      aux: aux * aux /6,
      cj: (aj^3 - aj) * aux,
      dj: (bj^3 - bj) * aux,

      s: s + funmake('charfun2,[defaults[3], a, b]) *
             expand(aj * tab[j-1][2] + bj * tab[j][2] + cj * y2[j-1] + dj * y2[j])  ),
   s )$



/* Rational interpolation, with interpolating function of the form             */
/*               r                                                             */
/*           p  x  + ... + p  x + p                                            */
/*            r             1      0                                           */
/*    R(x) = ------------------------                                          */
/*               m                                                             */
/*           q  x  + ... + q  x + q                                            */
/*            m             1      0                                           */
/* The 2nd. argument r is the degree of the numerator (r < sample size). The   */
/* degree of the denominator is calculated as m: sample_size - r - 1.          */
/* The 1st. argument must be either:                                           */
/*  a) a two column matrix, p:matrix([2,4],[5,6],[9,3])                        */
/*  b) a list of pairs, p: [[2,4],[5,6],[9,3]]                                 */
/*  c) a list of numbers, p: [4,6,3], in which case the abscissas will be      */
/*     assigned automatically to 1, 2, 3, etc.                                 */
/* In cases a) and b) the pairs are ordered wrt the 1st. coordinate before any */
/* computation is made. Option:                                                */
/*   'varname='x: the name of the independent variable                         */
/*   'denterm=1 : value for q_0 in the denominator                             */
/* Sample session:                                                             */
/* load(interpol)$                                                             */
/* load(draw)$                                                                 */
/* p: [[7.2, 2.5], [8.5, 2.1], [1.6, 5.1], [3.4, 2.4], [6.7, 7.9]]$            */
/* for k:0 thru length(p)-1 do                                                 */
/*   draw2d(explicit(ratinterpol(p,k),x,0,9),                                  */
/*          point_size = 3,                                                    */
/*          points(p),                                                         */
/*          title = concat("Degree of numerator = ",k),                        */
/*          yrange=[0,10])$                                                    */
ratinterpol(tab,r,[select]) :=
  block([n,m,coef,indep,pq,p,q,options,defaults,ratprint:false],
    tab: interpol_check_input(tab,"ratinterpol"),
    options:  ['varname, 'denterm],
    defaults: ['x, 1],
    for i in select do(
       aux: ?position(lhs(i),options),
       if numberp(aux) and aux <= length(options) and aux >= 1
         then defaults[aux]: rhs(i)),
    if not symbolp(defaults[1])
       then error("Option 'varname' is not correct"),
    if listp(defaults[2]) or matrixp(defaults[2])
       then error("Option 'denterm' must be a number or mathematical expression"),

    /* constructing the interpolating rational function */
    n: length(tab),
    if not integerp(r) or r > n-1 or r < 0
      then error("Degree of numerator must be a positive integer less than sample size"),
    m: n - r - 1, /* degree of denominator */
   
    /* coef matrix */
    coef: apply(matrix,
                makelist(block([x,y],
                           [x,y]: p,
                           append([1],
                                  makelist(x^k,k,1,r),
                                  makelist(-y*x^k,k,1,m))),
                         p, tab)),
    indep: map(second, tab) * defaults[2],
    pq: first(transpose(first(linsolve_by_lu(coef, indep)))),
    p: makelist(pq[k],k,1,r+1),
    q: cons(defaults[2], makelist(pq[k],k,r+2,n)),
    p.makelist(defaults[1]^k,k,0,r) / q.makelist(defaults[1]^k,k,0,m)  )$