3
* interface code for some interpolation / approximation
14
#include "../stack-c.h"
19
#define min(a,b) ((a) < (b) ? (a) : (b))
20
#define max(a,b) ((a) < (b) ? (b) : (a))
24
extern int C2F(dset)();
25
extern int C2F(bicubicinterpwithgrad)();
26
extern int C2F(bicubicinterpwithgradandhes)();
27
extern int C2F(db3ink)();
28
extern int C2F(driverdb3val)();
29
extern int C2F(driverdb3valwithgrad)();
34
enum {NOT_A_KNOT, NATURAL, CLAMPED, PERIODIC, FAST, FAST_PERIODIC,
35
MONOTONE, BY_ZERO, C0, LINEAR, BY_NAN, UNDEFINED};
37
typedef struct { char *str_type; int type; } TableType;
39
#define NB_SPLINE_TYPE 7
40
static TableType SplineTable[NB_SPLINE_TYPE] = {
41
{ "not_a_knot" , NOT_A_KNOT },
42
{ "natural" , NATURAL },
43
{ "clamped" , CLAMPED },
44
{ "periodic" , PERIODIC },
45
{ "monotone" , MONOTONE },
47
{ "fast_periodic", FAST_PERIODIC }};
50
static TableType OutModeTable[NB_OUTMODE] = {
52
{ "by_zero" , BY_ZERO },
53
{ "natural" , NATURAL },
54
{ "periodic" , PERIODIC },
55
{ "by_nan" , BY_NAN },
56
{ "linear" , LINEAR }};
58
static int good_order(double x[], int n)
60
/* test if x[i-1] < x[i] */
65
if ( first ) { inf = 1.0 / (double) (first - first) ; first = 0; }
67
if (fabs(x[0]) == inf || x[n-1] == inf)
70
for ( i = 1 ; i < n ; i++ )
71
if ( ! (x[i-1] < x[i]) ) /* form of this test for detecting nan ... */
78
* Routines sp�ciales :
79
* r�cup�rer une hypermatrice r�elle
80
* r�cup�rer une string scilab sans convertion
81
* comparer une string scilab et une C string classique
82
* (elles ne modifient pas les variables correspondantes
83
* ce qui est fondamental pour que cette interface soit
84
* pass�e par r�f�rence).
87
#define GetRhsScalarString(num,n,t) if (!get_rhs_scalar_string(num,n,t)) { return 0;}
88
#define GetRhsRealHMat(pos,H) if (!get_rhs_real_hmat(pos,H)) { return 0;}
90
typedef struct realhypermat {
91
int dimsize; /* number of dimensions of the hyper matrix */
92
int size; /* total number of elements : size = dims[0]x... x dims[dimsize-1] */
93
int *dims; /* number of elements in each dimension */
94
double *R; /* points to the elements */
97
static int get_rhs_real_hmat(int num, RealHyperMat *H)
99
int il, il1, il2, il3,/* it, */lw;
101
lw = num + Top - Rhs;
102
il = iadr(*lstk( lw ));
104
il = iadr(*istk(il+1));
106
if ( *istk(il) != 17 )
108
else if ( *istk(il+1) != 3 ) /* a hm mlist must have 3 fields */
111
/* get the pointers for the 3 fields */
113
il2 = il1 + *istk(il+3) - 1;
114
il3 = il1 + *istk(il+4) - 1;
115
il1 = iadr(il1); il2 = iadr(il2); il3 = iadr(il3);
117
/* test if the first field is a matrix string with 3 components
118
* and that the first is "hm" (ie 17 22 in scilab char code)
120
if ( (*istk(il1) != 10) | ((*istk(il1+1))*(*istk(il1+2)) != 3) )
122
else if ( *istk(il1+5)-1 != 2 ) /* 1 str must have 2 chars */
124
else if ( *istk(il1+8) != 17 || *istk(il1+9) != 22 )
127
/* get the 2d field */
128
if ( (*istk(il2) != 8) | (*istk(il2+3) != 4) )
130
H->dimsize = (*istk(il2+1))*(*istk(il2+2));
131
H->dims = istk(il2+4);
133
/* get the 3d field */
134
if ( !( *istk(il3) == 1 && *istk(il3+3)==0) )
137
H->size = (*istk(il3+1))*(*istk(il3+2));
138
H->R = stk(sadr(il3+4));
140
/* needed for Jpc stuff (putlhsvar) */
141
Nbvars = Max(Nbvars,num);
142
C2F(intersci).ntypes[num-1] = '$';
143
C2F(intersci).iwhere[num-1] = *lstk(lw);
144
C2F(intersci).lad[num-1] = 0; /* a voir ? */
148
Scierror(999,"Argument %d is not a real hypermatrix\r\n", num);
152
static int get_rhs_scalar_string(int num, int *length, int **tabchar)
156
lw = num + Top - Rhs;
157
il = iadr(*lstk( lw ));
159
il = iadr(*istk(il+1));
161
if ( ! ( *istk(il) == 10 && (*istk(il+1))*(*istk(il+2)) == 1 ) )
163
/* we look for a scalar string */
164
Scierror(999,"Argument %d is not a scalar string\r\n", num);
167
*length = *istk(il+5)-1;
168
*tabchar = istk(il+6);
170
Nbvars = Max(Nbvars,num);
171
C2F(intersci).ntypes[num-1] = '$';
172
C2F(intersci).iwhere[num-1] = *lstk(lw);
173
C2F(intersci).lad[num-1] = 0;
177
static int equal_scistring_and_string(int length, int *scistr, char *str)
179
/* compare a scistring with a classic C string */
182
if ( strlen(str) != length )
186
while (res && i < length)
188
res = (scistr[i] == (int)C2F(getfastcode)(str+i,1L));
194
static int get_type(TableType *Tab, int dim_table, int *scistr, int strlength)
196
int i = 0, found = 0;
197
while ( !found && i < dim_table )
199
found = equal_scistring_and_string(strlength, scistr, Tab[i].str_type);
203
return ( Tab[i-1].type );
205
return ( UNDEFINED );
209
static int intsplin(char * fname)
211
int minrhs=2, maxrhs=4, minlhs=1, maxlhs=1;
213
int mx, nx, lx, my, ny, ly, mc, nc, lc, n, spline_type;
214
int *str_spline_type, ns;
215
int /*i,*/ ld/*, flag*/;
216
int mwk1, nwk1, lwk1, mwk2, nwk2, lwk2, mwk3, nwk3, lwk3, mwk4, nwk4, lwk4;
217
double *x, *y, *d, *c;
219
CheckRhs(minrhs,maxrhs);
220
CheckLhs(minlhs,maxlhs);
222
GetRhsVar(1,"d", &mx, &nx, &lx);
223
GetRhsVar(2,"d", &my, &ny, &ly);
225
if ( mx != my || nx != ny || mx != 1 && nx != 1 )
227
Scierror(999,"%s: arg1 and arg2 must be 2 vectors with same size\r\n", fname);
230
n = mx*nx; /* number of interpolation points */
233
Scierror(999,"%s: the number of interpolation points must be >= 2\r\n", fname);
236
x = stk(lx); y = stk(ly);
237
if (! good_order(x, n)) /* verify strict increasing abscissae */
239
Scierror(999,"%s: elts of arg 1 not (strictly) increasing or +-inf detected\r\n", fname);
243
if ( Rhs >= 3 ) /* get the spline type */
245
GetRhsScalarString(3, &ns, &str_spline_type);
246
spline_type = get_type(SplineTable, NB_SPLINE_TYPE, str_spline_type, ns);
247
if ( spline_type == UNDEFINED )
249
Scierror(999,"%s: unknown spline_type\n\r",fname);
254
spline_type = NOT_A_KNOT;
256
if ( spline_type == CLAMPED ) /* get arg 4 which contains the end point slopes */
260
Scierror(999,"%s: for a clamped spline you must give the endpoint slopes\n\r",fname);
263
GetRhsVar(4,"d", &mc, &nc, &lc);
266
Scierror(999,"%s: bad dimension for the 4 arg (endpoint slopes)\n\r",fname);
273
Scierror(999,"%s: 4 args are required only for a clamped spline\n\r",fname);
277
/* verify y(1) = y(n) for periodic splines */
278
if ( (spline_type == PERIODIC || spline_type == FAST_PERIODIC) && y[0] != y[n-1] )
280
Scierror(999,"%s: for a periodic spline y(1) must be equal to y(n)\n\r",fname);
284
CreateVar(Rhs+1, "d", &mx, &nx, &ld); /* memory for d (only argument returned) */
289
case(FAST) : case(FAST_PERIODIC) :
291
C2F(derivd) (x, y, d, &n, &nwk1, &spline_type);
296
C2F(dpchim) (&n, x, y, d, &nwk1);
299
case(NOT_A_KNOT) : case(NATURAL) : case(CLAMPED) : case(PERIODIC) :
300
/* (the wk4 work array is used only in the periodic case) */
301
mwk1 = n; nwk1 = 1; mwk2 = n-1; nwk2 = 1; mwk3 = n-1; nwk3 = 1; mwk4 = n-2; nwk4 = 1;
302
CreateVar(Rhs+2, "d", &mwk1, &nwk1, &lwk1);
303
CreateVar(Rhs+3, "d", &mwk2, &nwk2, &lwk2);
304
CreateVar(Rhs+4, "d", &mwk3, &nwk3, &lwk3);
306
if (spline_type == CLAMPED)
307
{ d[0] = c[0]; d[n-1] = c[1]; };
308
if (spline_type == PERIODIC)
309
CreateVar(Rhs+5, "d", &mwk4, &nwk4, &lwk4);
310
C2F(splinecub) (x, y, d, &n, &spline_type, stk(lwk1), stk(lwk2), stk(lwk3), stk(lwk4));
318
static int intlsq_splin(char * fname)
320
/* interface code for [y, d] = lsq_splin(xd, yd [, wd], x) */
322
int minrhs=3, maxrhs=4, minlhs=1, maxlhs=2;
324
int mxd, nxd, lxd, myd, nyd, lyd, mx, nx, lx, mwd, nwd, lwd;
325
int ly, ld, lwork, ndata, n, one=1, mwork, ierr;
328
CheckRhs(minrhs,maxrhs);
329
CheckLhs(minlhs,maxlhs);
331
GetRhsVar(1,"d", &mxd, &nxd, &lxd);
332
GetRhsVar(2,"d", &myd, &nyd, &lyd);
333
ndata = mxd*nxd; /* number of data points */
334
if ( ndata < 4 || mxd != myd || nxd != nyd || mxd != 1 && nxd != 1 )
336
Scierror(999,"%s: arg 1 and 2 must be vectors of same size with at least %d elements\r\n",
343
GetRhsVar(3,"d", &mwd, &nwd, &lwd);
344
if ( mxd != mwd || nxd != nwd )
346
Scierror(999,"%s: bad input for arg 3\r\n", fname);
350
GetRhsVar(Rhs,"d", &mx, &nx, &lx);
352
if ( n < 2 || mx != 1 && nx != 1 )
354
Scierror(999,"%s: bad input for x \r\n", fname);
358
if (! good_order(stk(lx), n)) /* verify strict increasing abscissae */
360
Scierror(999,"%s: elts of arg %d not (strictly) increasing or +-inf detected\r\n", fname, Rhs);
364
CreateVar(Rhs+1, "d", &mx, &nx, &ly);
365
CreateVar(Rhs+2, "d", &mx, &nx, &ld);
367
CreateVar(Rhs+3, "d", &mwork, &one, &lwork);
370
CreateVar(Rhs+4, "d", &mxd, &nxd, &lwd);
371
C2F(dset)( &ndata, &un, stk(lwd), &one); /* set all the weight = 1 */
374
C2F(spfit)(stk(lxd), stk(lyd), stk(lwd), &ndata, stk(lx), &n, stk(ly),
375
stk(ld), stk(lwork), &ierr);
379
Scierror(999,"%s: not enought points for the fit \r\n", fname);
383
sciprint("%s warning: rank deficiency of the least square matrix\r\n", fname);
391
static int intinterp1(char * fname)
393
int minrhs=4, maxrhs=5, minlhs=1, maxlhs=4;
395
int mt, nt, lt, mx, nx, lx, my, ny, ly, md, nd, ld, ns, *str_outmode;
396
int n, m, outmode, lst, ldst, lddst, ldddst;
399
CheckRhs(minrhs,maxrhs);
400
CheckLhs(minlhs,maxlhs);
402
GetRhsVar(1,"d", &mt, &nt, <);
403
GetRhsVar(2,"d", &mx, &nx, &lx);
404
GetRhsVar(3,"d", &my, &ny, &ly);
405
GetRhsVar(4,"d", &md, &nd, &ld);
407
if ( mx != my || nx != ny || md != mx || nd != nx || mx != 1 && nx != 1 || mx*nx < 2)
409
Scierror(999,"%s: bad inputs \r\n", fname);
412
n = mx*nx; /* number of interpolation points */
413
m = mt*nt; /* number of points to interpolate */
415
if ( Rhs == 5 ) /* get the outmode */
417
GetRhsScalarString(5, &ns, &str_outmode);
418
outmode = get_type(OutModeTable, NB_OUTMODE, str_outmode, ns);
419
if ( outmode == UNDEFINED )
421
Scierror(999,"%s: unknown outmode type\n\r",fname);
426
outmode = C0; /* default outmode */
428
/* memory for st, dst, ddst, dddst */
429
CreateVar(Rhs+1, "d", &mt, &nt, &lst);
430
CreateVar(Rhs+2, "d", &mt, &nt, &ldst);
431
CreateVar(Rhs+3, "d", &mt, &nt, &lddst);
432
CreateVar(Rhs+4, "d", &mt, &nt, &ldddst);
434
/* subroutine EvalPWHermite(t, st, dst, ddst, dddst, m, x, y, d, n, outmode)
435
* integer m, n, outmode
436
* double precision t(m), st(m), dst(m), ddst(m), dddst(m), x(n), y(n), d(n)
438
C2F(evalpwhermite) (stk(lt), stk(lst), stk(ldst), stk(lddst), stk(ldddst),
439
&m, stk(lx), stk(ly), stk(ld), &n, &outmode);
449
static int intlinear_interpn(char * fname)
451
/* interpolation lineaire n-dimensionnelle
453
* yp = linear_interpn(xp1, ..., xpn, x1, ..., xn, val, outmode)
455
int n, mxp, nxp, lxp, mxpn, nxpn, lxpn, mx, nx, lx, my, ny, ly, one=1;
456
int ns, *str_outmode, np, *k, *ad, m, l, i, outmode;
458
double **xp, **x, *val, *u, *v, *yp;
464
Scierror(999,"%s: too few arg \r\n", fname);
468
/* les points sur lesquels on evalue par interpolation */
469
/* l = I_UINT32; CreateVar(Rhs+1, "I", &n, &one, &l); */
470
/* xp = (double **) istk(l); */
471
CreateVar(Rhs+1, "d", &n, &one, &l); /* => lets store an array of pointers */
472
xp = (double **) stk(l); /* with size of 4 or 8 bytes */
474
GetRhsVar(1,"d", &mxp, &nxp, &lxp);
477
for ( i = 2 ; i <= n ; i++ )
479
GetRhsVar(i,"d", &mxpn, &nxpn, &lxpn);
480
if ( mxp != mxpn || nxp != nxpn )
482
Scierror(999,"%s: bad inputs for xp1, xp2, ...., \r\n", fname);
488
/* coordonn�es de la grille */
489
l = I_INT32; CreateVar(Rhs+2, "I", &n, &one, &l);
491
/* l = I_UINT32; CreateVar(Rhs+3, "I", &n, &one, &l); */
492
/* x = (double **) istk(l); */
493
CreateVar(Rhs+3, "d", &n, &one, &l); /* => lets store an array of pointers */
494
x = (double **) stk(l); /* with size(void *) = 4 or 8 bytes */
496
for ( i = 1 ; i <= n ; i++ )
498
GetRhsVar(n+i,"d", &mx, &nx, &lx);
499
if ( (mx != 1 && nx != 1) && mx*nx < 2)
501
Scierror(999,"%s: bad arg number %d \r\n", fname, n+i);
506
/* verify strict increasing order */
507
if ( !good_order(x[i-1], mx*nx) )
509
Scierror(999,"%s: grid abscissae of dim %d not in strict increasing order \r\n", fname, n+i);
514
/* les valeurs de la grille */
517
GetRhsRealHMat(2*n+1,&U);
518
if ( U.dimsize != n )
520
Scierror(999,"%s: U must be a real %d-dim hypermatrix \n", fname, n);
523
for ( i = 0 ; i < n ; i++ )
524
if ( U.dims[i] != dim[i] )
526
Scierror(999,"%s: size incompatibility between grid points and grid values in dim %d \n\r", fname, i+1);
531
else /* n = 1 or 2 */
533
GetRhsVar(2*n+1,"d", &my, &ny, &ly);
534
if ( n == 1 && my*ny != dim[0] )
536
Scierror(999,"%s: size incompatibility between grid points and values in dim 1 \n\r", fname);
539
if ( n == 2 && (my != dim[0] || ny != dim[1]) )
541
Scierror(999,"%s: size incompatibility between grid points and values in dim 1 or 2 \n\r", fname);
547
/* get the outmode */
548
if ( Rhs == 2*n + 2 )
550
GetRhsScalarString(Rhs, &ns, &str_outmode);
551
outmode = get_type(OutModeTable, NB_OUTMODE, str_outmode, ns);
552
if ( outmode == UNDEFINED || outmode == LINEAR )
554
Scierror(999,"%s: unsupported outmode type\n\r",fname);
561
CreateVar(Rhs+4, "d", &n, &one, &l); u = stk(l);
562
m = 1; for ( i = 1 ; i <= n ; i++) m = 2*m;
563
CreateVar(Rhs+5, "d", &m, &one, &l); v = stk(l);
564
l = 4; CreateVar(Rhs+6, "I", &m, &one, &l); ad = istk(l);
565
l = 4; CreateVar(Rhs+7, "I", &n, &one, &l); k = istk(l);
566
CreateVar(Rhs+8, "d", &mxp, &nxp, &l); yp = stk(l);
568
nlinear_interp(x, val, dim, n, xp, yp, np, outmode, u, v, ad, k);
572
/* correction Warning Allan CORNET */
573
/* warning C4715: 'intlinear_interpn' : not all control paths return a value */
578
static int intsplin2d(char * fname)
580
/* interface pour splin2d :
582
* C = splin2d(x, y, z [, type])
586
int minrhs=3, maxrhs=4, minlhs=1, maxlhs=1;
588
int mx, nx, lx, my, ny, ly, mz, nz, lz, ns, mc, nc, lc, lp, lq, lr;
589
int spline_type, *str_spline_type/*, i*/;
593
CheckRhs(minrhs,maxrhs);
594
CheckLhs(minlhs,maxlhs);
596
GetRhsVar(1,"d", &mx, &nx, &lx);
597
GetRhsVar(2,"d", &my, &ny, &ly);
598
GetRhsVar(3,"d", &mz, &nz, &lz);
600
if ( mx != 1 || my != 1 || mz != nx || nz != ny || nx < 2 || ny < 2)
602
Scierror(999,"%s: bad inputs \r\n", fname);
606
/* verify strict increasing order for x and y */
607
x = stk(lx); y = stk(ly);
608
if ( !good_order(x, nx) || !good_order(y, ny))
610
Scierror(999,"%s: x and/or y are not in strict increasing order (or +-inf detected) \r\n", fname);
614
/* get the spline type */
617
GetRhsScalarString(4, &ns, &str_spline_type);
618
spline_type = get_type(SplineTable, NB_SPLINE_TYPE, str_spline_type, ns);
619
if ( spline_type == UNDEFINED || spline_type == CLAMPED )
621
Scierror(999,"%s: unsupported spline type\n\r",fname);
626
spline_type = NOT_A_KNOT;
628
/* memory for the big C array */
629
mc = 16*(nx-1)*(ny-1); nc = 1;
630
CreateVar( Rhs+1, "d", &mc, &nc, &lc);
632
/* memory for work arrays */
633
CreateVar( Rhs+2, "d", &nx, &ny, &lp);
634
CreateVar( Rhs+3, "d", &nx, &ny, &lq);
635
CreateVar( Rhs+4, "d", &nx, &ny, &lr);
637
if (spline_type == MONOTONE || spline_type == FAST || spline_type == FAST_PERIODIC)
639
C2F(bicubicsubspline)(x, y, stk(lz), &nx, &ny, stk(lc),
640
stk(lp), stk(lq), stk(lr), &spline_type);
645
int lA_d, lA_sd, ld, lqdu, lutemp, nxy, nxym1, nxym2, lll;
647
nxy = max(nx,ny); nxym1 = nxy-1; nxym2 = nxy-2;
649
CreateVar( Rhs+5, "d", &nxy, &one, &lA_d);
650
CreateVar( Rhs+6, "d", &nxym1, &one, &lA_sd);
651
CreateVar( Rhs+7, "d", &ny, &one, &ld);
652
CreateVar( Rhs+8, "d", &nxy, &one, &lqdu);
653
CreateVar( Rhs+9, "d", &ny, &one, &lutemp);
655
if (spline_type == PERIODIC)
657
CreateVar(Rhs+10, "d", &nxym2, &one, &lll) ;
660
lll = lA_sd ; /* bidon ... */
661
C2F(bicubicspline)(x, y, stk(lz), &nx, &ny, stk(lc), stk(lp), stk(lq), stk(lr),
662
stk(lA_d), stk(lA_sd), stk(ld), stk(lll), stk(lqdu),
663
stk(lutemp), &spline_type);
671
static int intinterp2d(char * fname)
673
/* interface pour interp2d :
675
* [zp [, dzdxp, dzdyp [, d2zdx2, d2zdxy, d2zdy2]]] = interp2d(xp, yp, x, y, C[, outmode])
678
int minrhs=5, maxrhs=6, minlhs=1, maxlhs=6;
680
int mxp, nxp, lxp, myp, nyp, lyp, mx, nx, lx, my, ny, ly;
681
int mc, nc, lc, ns, *str_outmode, lzp, ldzdxp, ldzdyp, ld2zdx2p, ld2zdxyp, ld2zdy2p;
684
CheckRhs(minrhs,maxrhs);
685
CheckLhs(minlhs,maxlhs);
687
GetRhsVar(1,"d", &mxp, &nxp, &lxp);
688
GetRhsVar(2,"d", &myp, &nyp, &lyp);
689
GetRhsVar(3,"d", &mx, &nx, &lx);
690
GetRhsVar(4,"d", &my, &ny, &ly);
691
GetRhsVar(5,"d", &mc, &nc, &lc);
693
if ( mxp != myp || nxp != nyp || mx != 1 || my != 1 || nc != 1 || nx < 2 || ny < 2
694
|| mc != 16*(nx-1)*(ny-1) )
696
Scierror(999,"%s: bad inputs \r\n", fname);
700
/* get the outmode */
703
GetRhsScalarString(6, &ns, &str_outmode);
704
outmode = get_type(OutModeTable, NB_OUTMODE, str_outmode, ns);
705
if ( outmode == UNDEFINED || outmode == LINEAR )
707
Scierror(999,"%s: unsupported outmode type\n\r",fname);
715
CreateVar( Rhs+1, "d", &mxp, &nxp, &lzp);
720
/* subroutine BiCubicInterp(x, y, C, nx, ny, x_eval, y_eval, z_eval, m, outmode)
721
* integer nx, ny, m, outmode
722
* double precision x(nx), y(ny), C(4,4,nx-1,ny-1), x_eval(m), y_eval(m), z_eval(m)
724
C2F(bicubicinterp)(stk(lx), stk(ly), stk(lc), &nx, &ny, stk(lxp), stk(lyp), stk(lzp),
728
else /* got also the derivatives */
730
/* memory for dzdxp and dzdyp */
731
CreateVar( Rhs+2, "d", &mxp, &nxp, &ldzdxp);
732
CreateVar( Rhs+3, "d", &mxp, &nxp, &ldzdyp);
736
C2F(bicubicinterpwithgrad)(stk(lx), stk(ly), stk(lc), &nx, &ny, stk(lxp),
737
stk(lyp), stk(lzp), stk(ldzdxp), stk(ldzdyp),
743
else /* got also 2d derivatives */
745
CreateVar( Rhs+4, "d", &mxp, &nxp, &ld2zdx2p);
746
CreateVar( Rhs+5, "d", &mxp, &nxp, &ld2zdxyp);
747
CreateVar( Rhs+6, "d", &mxp, &nxp, &ld2zdy2p);
748
C2F(bicubicinterpwithgradandhes)(stk(lx), stk(ly), stk(lc), &nx, &ny, stk(lxp),
749
stk(lyp), stk(lzp), stk(ldzdxp), stk(ldzdyp),
750
stk(ld2zdx2p), stk(ld2zdxyp), stk(ld2zdy2p),
765
* interface sur le package cshep2d ....
768
static int intcshep2d(char * fname)
770
static char *Str[]={"cshep2d", "xyz", "lcell", "lnext", "grdim", "rmax", "rw", "a"};
771
int minrhs=1, maxrhs=1, minlhs=1, maxlhs=1;
772
int n, dim, nc, nw, nr, one=1, four=4, eight=8, nine=9, ier;
773
int lxyz, lxyzn, lcell, lnext, lgrid, lrmax, lrw, la, ltlist, lar;
775
double * xyz, * grid;
777
CheckRhs(minrhs,maxrhs);
778
CheckLhs(minlhs,maxlhs);
780
GetRhsVar(1,"d", &n, &dim, &lxyz);
781
if ( dim != 3 || n < 10 )
783
Scierror(999,"%s: xyz must be a (n,3) real matrix with n >= 10 \r\n", fname);
787
/* choix pour nc (peut etre futur parametre optionnel) */
791
/* choix pour nr (grille nr x nr) */
792
nr = (int) sqrt( n/3.0 ); /* comme n >= 10 nr >= 1 */
794
/* all the information for the "interpolant" will be stored
795
* in a tlist (which also contains the entry xyz)
797
CreateVar(2,"t", &eight, &one, <list);
798
CreateListVarFromPtr(2, 1, "S", &one, &eight, Str);
799
CreateListVarFrom(2, 2, "d", &n , &dim, &lxyzn, &lxyz);
801
CreateListVarFrom(2, 3, "I", &nr, &nr, &lcell, &lar); /* lcell */
803
CreateListVarFrom(2, 4, "I", &one, &n, &lnext, &lar); /* lnext */
805
CreateListVarFrom(2, 5, "d", &one, &four, &lgrid, &lar); /* xmin, ymin, dx, dy */
807
CreateListVarFrom(2, 6, "d", &one, &one, &lrmax, &lar); /* rmax */
809
CreateListVarFrom(2, 7, "d", &one, &n, &lrw, &lar); /* rw */
811
CreateListVarFrom(2, 8, "d", &nine, &n, &la, &lar); /* a */
815
/* SUBROUTINE CSHEP2 (N,X,Y,F,NC,NW,NR, LCELL,LNEXT,XMIN,
816
* YMIN,DX,DY,RMAX,RW,A,IER)
818
C2F(cshep2) ( &n, xyz, &xyz[n], &xyz[2*n], &nc, &nw, &nr, istk(lcell),
819
istk(lnext), grid, &grid[1], &grid[2], &grid[3], stk(lrmax),
820
stk(lrw), stk(la), &ier);
824
Scierror(999,"%s: duplicate nodes or all nodes colinears (ier = %d) \r\n", fname, ier);
833
static int inteval_cshep2d(char * fname)
836
* [f [,dfdx, dfdy [, dffdxx, dffdxy, dffdyy]]] = eval_cshep2d(xp, yp, tlcoef)
839
int minrhs=3, maxrhs=3, minlhs=1, maxlhs=6;
840
int mx, nx, lx, my, ny, ly, mt, nt, lt;
842
int m1, n1, m2, n2, m3, n3, m4, n4, m5, n5, m6, n6, m7, n7, m8, n8;
843
int lxyz, lgrid, lrmax, lrw, la;
844
double *xp, *yp, *xyz, *grid, *f, *dfdx, *dfdy, *dffdxx, *dffdyy, *dffdxy;
845
int i, ier, n, np, nr, lf, ldfdx, ldfdy, ldffdxx, ldffdyy, ldffdxy;
846
SciIntMat Cell, Next;
849
CheckRhs(minrhs,maxrhs);
850
CheckLhs(minlhs,maxlhs);
852
GetRhsVar(1,"d", &mx, &nx, &lx);
853
GetRhsVar(2,"d", &my, &ny, &ly);
854
if ( mx != my || nx != ny )
856
Scierror(999,"%s: xp and yp must have the same dimension \r\n", fname);
860
GetRhsVar(3,"t",&mt, &nt, <);
861
GetListRhsVar(3, 1, "S", &m1, &n1, &Str); /* m1 = 1, n1 = 8 ? a verifier */
862
if ( strcmp(Str[0],"cshep2d") != 0)
865
Scierror(999,"%s: Argument 2 is not an cshep2d tlist\r\n", fname);
869
GetListRhsVar(3, 2, "d", &m2, &n2, &lxyz); /* m2 = n , n2 = 3 */
870
GetListRhsVar(3, 3, "I", &m3, &n3, (int *)&Cell); /* m3 = nr, n3 = nr */
871
GetListRhsVar(3, 4, "I", &m4, &n4, (int *)&Next); /* m4 = 1 , n4 = n */
872
GetListRhsVar(3, 5, "d", &m5, &n5, &lgrid); /* m5 = 1 , n5 = 4 */
873
GetListRhsVar(3, 6, "d", &m6, &n6, &lrmax); /* m6 = 1 , n6 = 1 */
874
GetListRhsVar(3, 7, "d", &m7, &n7, &lrw); /* m7 = 1 , n7 = n */
875
GetListRhsVar(3, 8, "d", &m8, &n8, &la); /* m8 = 9 , n8 = n */
877
cell = (int *)Cell.D; next = (int *)Next.D;
878
xp = stk(lx); yp = stk(ly); np = mx*nx;
880
xyz = stk(lxyz); grid = stk(lgrid);
882
CreateVar(4, "d", &mx, &nx, &lf); f = stk(lf);
885
CreateVar(5, "d", &mx, &nx, &ldfdx); dfdx = stk(ldfdx);
886
CreateVar(6, "d", &mx, &nx, &ldfdy); dfdy = stk(ldfdy);
890
CreateVar(7, "d", &mx, &nx, &ldffdxx); dffdxx = stk(ldffdxx);
891
CreateVar(8, "d", &mx, &nx, &ldffdxy); dffdyy = stk(ldffdxy);
892
CreateVar(9, "d", &mx, &nx, &ldffdyy); dffdxy = stk(ldffdyy);
898
for ( i = 0 ; i < np ; i++ )
899
/* DOUBLE PRECISION FUNCTION CS2VAL (PX,PY,N,X,Y,F,NR,
900
* LCELL,LNEXT,XMIN,YMIN,DX,DY,RMAX,RW,A)
902
f[i] = C2F(cs2val)(&xp[i], &yp[i], &n, xyz, &xyz[n], &xyz[2*n], &nr,
903
cell, next, grid, &grid[1], &grid[2], &grid[3],
904
stk(lrmax), stk(lrw), stk(la));
910
for ( i = 0 ; i < np ; i++ )
911
/* SUBROUTINE CS2GRD (PX,PY,N,X,Y,F,NR,LCELL,LNEXT,XMIN,
912
*. YMIN,DX,DY,RMAX,RW,A, C,CX,CY,IER)
914
C2F(cs2grd) (&xp[i], &yp[i], &n, xyz, &xyz[n], &xyz[2*n], &nr,
915
cell, next, grid, &grid[1], &grid[2], &grid[3],
916
stk(lrmax), stk(lrw), stk(la), &f[i], &dfdx[i], &dfdy[i], &ier);
925
for ( i = 0 ; i < np ; i++ )
927
/* SUBROUTINE CS2HES (PX,PY,N,X,Y,F,NR,LCELL,LNEXT,XMIN,
928
*. YMIN,DX,DY,RMAX,RW,A, C,CX,CY,CXX,CXY,CYY,IER)
930
C2F(cs2hes) (&xp[i], &yp[i], &n, xyz, &xyz[n], &xyz[2*n], &nr,
931
cell, next, grid, &grid[1], &grid[2], &grid[3],
932
stk(lrmax), stk(lrw), stk(la), &f[i], &dfdx[i], &dfdy[i],
933
&dffdxx[i], &dffdxy[i], &dffdyy[i], &ier);
947
static int intsplin3d(char * fname)
950
* [tlist] = splin3d(x, y, z, v [,orderxyz])
952
static char *Str[]={"tensbs3d", "tx", "ty", "tz", "order", "bcoef", "xyzminmax"};
954
int minrhs=4, maxrhs=5, minlhs=1, maxlhs=1;
956
int mx, nx, lx, my, ny, ly, mz, nz, lz, mo, no, lo, kx, ky, kz;
957
int ntx, nty, ntz, ltx, lty, ltz, lbcoef, lxyzminmax, mwk, mwkx, mwky, mwkz;
958
int flag, one=1, three=3, six=6, seven=7,ltlist, nxyz, lwork, lar, lorder, *order;
959
double *x, *y, *z, *xyzminmax;
962
CheckRhs(minrhs,maxrhs);
963
CheckLhs(minlhs,maxlhs);
965
GetRhsVar(1,"d", &mx, &nx, &lx);
966
CheckVector(1, mx, nx); x = stk(lx);
967
GetRhsVar(2,"d", &my, &ny, &ly);
968
CheckVector(2, my, ny); y = stk(ly);
969
GetRhsVar(3,"d", &mz, &nz, &lz);
970
CheckVector(2, mz, nz); z = stk(lz);
972
nx = mx*nx; ny = my*ny; nz = mz*nz;
974
if ( nx < 3 || ny < 3 || nz < 3 )
976
Scierror(999,"%s: the x, y and z grids must have at least 3 points \r\n", fname);
980
GetRhsRealHMat(4, &V);
981
if ( V.dimsize != 3 )
983
Scierror(999,"%s: 4 th argument must be a real 3-dim hypermatrix \n", fname);
986
if ( V.dims[0] != nx || V.dims[1] != ny || V.dims[2] != nz )
988
Scierror(999,"%s: size incompatibility between grid points and grid values\n\r", fname);
994
GetRhsVar(5,"d", &mo, &no, &lo);
995
if ( (mo != 1 && no != 1) || mo*no != 3 )
997
Scierror(999,"%s: the 4 th arg must be a vector with 3 components \r\n", fname);
1000
kx = (int)*stk(lo); ky = (int)*stk(lo+1); kz = (int)*stk(lo+2);
1001
if ( kx < 2 || kx >= nx || ky < 2 || ky >= ny || kz < 2 || kz >= nz )
1003
Scierror(999,"%s: bad 5 th arg [kx ky kz]\n\r", fname);
1009
kx = 4; ky = 4; kz = 4;
1015
mwkx = kx*(nx+1); mwky = ky*(ny+1); mwkz = kz*(nz+1);
1016
mwkx = max(mwkx, mwky);
1017
mwk = nx*ny*nz + 2*(max(mwkx, mwkz));
1020
CreateVar(Rhs+1,"t", &seven, &one, <list);
1021
CreateListVarFromPtr(Rhs+1, 1, "S", &one, &seven, Str);
1022
lar = -1; CreateListVarFrom(Rhs+1, 2, "d", &ntx, &one, <x, &lar);
1023
lar = -1; CreateListVarFrom(Rhs+1, 3, "d", &nty, &one, <y, &lar);
1024
lar = -1; CreateListVarFrom(Rhs+1, 4, "d", &ntz, &one, <z, &lar);
1026
lar = -1; CreateListVarFrom(Rhs+1, 5, "I", &three, &one, &lorder, &lar);
1027
order = istk(lorder); order[0] = kx; order[1] = ky; order[2] = kz;
1028
lar = -1; CreateListVarFrom(Rhs+1, 6, "d", &nxyz, &one, &lbcoef, &lar);
1029
lar = -1; CreateListVarFrom(Rhs+1, 7, "d", &six, &one, &lxyzminmax, &lar);
1030
xyzminmax = stk(lxyzminmax);
1031
xyzminmax[0] = x[0]; xyzminmax[1] = x[nx-1];
1032
xyzminmax[2] = y[0]; xyzminmax[3] = y[ny-1];
1033
xyzminmax[4] = z[0]; xyzminmax[5] = z[nz-1];
1034
CreateVar(Rhs+2, "d", &mwk, &one, &lwork); /* work */
1037
C2F(db3ink) ( stk(lx), &nx, stk(ly), &ny, stk(lz), &nz, V.R,
1038
&nx, &ny, &kx, &ky, &kz, stk(ltx), stk(lty), stk(ltz),
1039
stk(lbcoef), stk(lwork), &flag);
1043
Scierror(999,"%s: problem : flag = %d \r\n", fname, flag);
1047
/* Return only the tlist */
1053
static int intinterp3d(char * fname) /* a suivre */
1056
* [f [, dfdx, dfdy, dfdz]] = interp3d(xp, yp, zp, tlcoef [,outmode])
1059
int minrhs=4, maxrhs=5, minlhs=1, maxlhs=4;
1061
int mxp, nxp, lxp, myp, nyp, lyp, mzp, nzp, lzp, mt, nt, lt, np;
1062
int zero=0, one=1, kx, ky, kz;
1063
int nx, ny, nz, nxyz, mtx, mty, mtz, m, n, ltx, lty, ltz, lbcoef, mwork, lwork, lfp;
1064
int lxyzminmax, nsix, outmode, ns, *str_outmode;
1065
int /*i,*/ m1, n1, ldfpdx, ldfpdy, ldfpdz;
1066
double *fp, *xp, *yp, *zp, *dfpdx, *dfpdy, *dfpdz;
1067
double *xyzminmax, xmin, xmax, ymin, ymax, zmin, zmax;
1068
SciIntMat Order; int *order;
1071
CheckRhs(minrhs,maxrhs);
1072
CheckLhs(minlhs,maxlhs);
1074
GetRhsVar(1,"d", &mxp, &nxp, &lxp); xp = stk(lxp);
1075
GetRhsVar(2,"d", &myp, &nyp, &lyp); yp = stk(lyp);
1076
GetRhsVar(3,"d", &mzp, &nzp, &lzp); zp = stk(lzp);
1077
if ( mxp != myp || nxp != nyp || mxp != mzp || nxp != nzp)
1079
Scierror(999,"%s: xp, yp and zp must have the same dimensions \r\n", fname);
1084
GetRhsVar(4,"t",&mt, &nt, <);
1085
GetListRhsVar(4, 1, "S", &m1, &n1, &Str);
1086
if ( strcmp(Str[0],"tensbs3d") != 0)
1089
Scierror(999,"%s: 4 th argument is not an tensbs3d tlist \r\n", fname);
1093
GetListRhsVar(4, 2, "d", &mtx, &n, <x);
1094
GetListRhsVar(4, 3, "d", &mty, &n, <y);
1095
GetListRhsVar(4, 4, "d", &mtz, &n, <z);
1096
GetListRhsVar(4, 5, "I", &m , &n, (int *)&Order);
1097
GetListRhsVar(4, 6, "d", &nxyz,&n, &lbcoef);
1098
GetListRhsVar(4, 7, "d", &nsix,&n, &lxyzminmax);
1099
xyzminmax = stk(lxyzminmax);
1100
xmin = xyzminmax[0]; xmax = xyzminmax[1];
1101
ymin = xyzminmax[2]; ymax = xyzminmax[3];
1102
zmin = xyzminmax[4]; zmax = xyzminmax[5];
1105
/* get the outmode */
1108
GetRhsScalarString(5, &ns, &str_outmode);
1109
outmode = get_type(OutModeTable, NB_OUTMODE, str_outmode, ns);
1110
if ( outmode == UNDEFINED || outmode == LINEAR || outmode == NATURAL )
1112
Scierror(999,"%s: unsupported outmode type\n\r",fname);
1119
CreateVar(Rhs+1, "d", &mxp, &nxp, &lfp); fp = stk(lfp);
1121
order = (int *)Order.D;
1122
kx = order[0]; ky = order[1]; kz = order[2];
1123
nx = mtx - kx; ny = mty - ky; nz = mtz - kz;
1125
mwork = ky*kz + 3*max(kx,max(ky,kz)) + kz;
1126
CreateVar(Rhs+2, "d", &mwork, &one, &lwork);
1130
C2F(driverdb3val)(xp,yp,zp,fp,&np,stk(ltx), stk(lty), stk(ltz),
1131
&nx, &ny, &nz, &kx, &ky, &kz, stk(lbcoef), stk(lwork),
1132
&xmin, &xmax, &ymin, &ymax, &zmin, &zmax, &outmode);
1137
CreateVar(Rhs+3, "d", &mxp, &nxp, &ldfpdx); dfpdx = stk(ldfpdx);
1138
CreateVar(Rhs+4, "d", &mxp, &nxp, &ldfpdy); dfpdy = stk(ldfpdy);
1139
CreateVar(Rhs+5, "d", &mxp, &nxp, &ldfpdz); dfpdz = stk(ldfpdz);
1140
C2F(driverdb3valwithgrad)(xp,yp,zp,fp,dfpdx, dfpdy, dfpdz, &np,
1141
stk(ltx), stk(lty), stk(ltz),
1142
&nx, &ny, &nz, &kx, &ky, &kz, stk(lbcoef), stk(lwork),
1143
&xmin, &xmax, &ymin, &ymax, &zmin, &zmax, &outmode);
1154
static int intbsplin3val(char * fname)
1157
* [fp] = bsplin3val(xp, yp, zp, tlcoef, der)
1160
int minrhs=5, maxrhs=5, minlhs=1, maxlhs=1;
1162
int mxp, nxp, lxp, myp, nyp, lyp, mzp, nzp, lzp, mt, nt, lt, m1, n1, np;
1163
int zero=0, one=1, kx, ky, kz;
1164
int nx, ny, nz, nxyz, mtx, mty, mtz, m, n, ltx, lty, ltz, lbcoef, mwork, lwork, lfp;
1165
int lxyzminmax, nsix;
1166
int i, mder,nder,lder, ox, oy, oz;
1167
double *fp, *xp, *yp, *zp, *der;
1168
double *xyzminmax, xmin, xmax, ymin, ymax, zmin, zmax;
1169
SciIntMat Order; int *order;
1172
CheckRhs(minrhs,maxrhs);
1173
CheckLhs(minlhs,maxlhs);
1175
GetRhsVar(1,"d", &mxp, &nxp, &lxp); xp = stk(lxp);
1176
GetRhsVar(2,"d", &myp, &nyp, &lyp); yp = stk(lyp);
1177
GetRhsVar(3,"d", &mzp, &nzp, &lzp); zp = stk(lzp);
1178
if ( mxp != myp || nxp != nyp || mxp != mzp || nxp != nzp)
1180
Scierror(999,"%s: xp, yp and zp must have the same dimensions \r\n", fname);
1185
GetRhsVar(4,"t",&mt, &nt, <);
1186
GetListRhsVar(4, 1, "S", &m1, &n1, &Str);
1187
if ( strcmp(Str[0],"tensbs3d") != 0)
1190
Scierror(999,"%s: 4 th argument is not an tensbs3d tlist \r\n", fname);
1194
GetListRhsVar(4, 2, "d", &mtx, &n, <x);
1195
GetListRhsVar(4, 3, "d", &mty, &n, <y);
1196
GetListRhsVar(4, 4, "d", &mtz, &n, <z);
1197
GetListRhsVar(4, 5, "I", &m , &n, (int *)&Order);
1198
GetListRhsVar(4, 6, "d", &nxyz,&n, &lbcoef);
1199
GetListRhsVar(4, 7, "d", &nsix,&n, &lxyzminmax);
1200
xyzminmax = stk(lxyzminmax);
1201
xmin = xyzminmax[0]; xmax = xyzminmax[1];
1202
ymin = xyzminmax[2]; ymax = xyzminmax[3];
1203
zmin = xyzminmax[4]; zmax = xyzminmax[5];
1205
GetRhsVar(5,"d", &mder, &nder, &lder);
1208
|| der[0] != floor(der[0]) || der[0] < 0.0
1209
|| der[1] != floor(der[1]) || der[1] < 0.0
1210
|| der[2] != floor(der[2]) || der[2] < 0.0 )
1212
Scierror(999,"%s: bad 5 th argument \r\n", fname);
1215
ox = (int) der[0]; oy = (int) der[1]; oz = (int) der[2];
1218
CreateVar(Rhs+1, "d", &mxp, &nxp, &lfp); fp = stk(lfp);
1220
order = (int *)Order.D;
1221
kx = order[0]; ky = order[1]; kz = order[2];
1222
nx = mtx - kx; ny = mty - ky; nz = mtz - kz;
1224
mwork = ky*kz + 3*max(kx,max(ky,kz)) + kz;
1225
CreateVar(Rhs+2, "d", &mwork, &one, &lwork);
1227
for ( i=0; i<np ; i++ )
1229
fp[i] = C2F(db3val)(&(xp[i]), &(yp[i]), &(zp[i]), &ox, &oy, &oz,
1230
stk(ltx), stk(lty), stk(lty), &nx, &ny, &nz,
1231
&kx, &ky, &kz, stk(lbcoef), stk(lwork));
1240
{intsplin, "splin"},
1241
{intlsq_splin, "lsq_splin"},
1242
{intinterp1, "interp"},
1243
{intlinear_interpn, "linear_interpn"},
1244
{intsplin2d, "splin2d"},
1245
{intinterp2d, "interp2d"},
1246
{intcshep2d, "cshep2d"},
1247
{inteval_cshep2d, "eval_cshep2d" },
1248
{intsplin3d, "splin3d"},
1249
{intinterp3d, "interp3d"},
1250
{intbsplin3val, "bsplin3val"}
1253
int C2F(intinterp)(void)
1256
(*(Tab[Fin-1].f))(Tab[Fin-1].name);