File: s6sratder.c

package info (click to toggle)
sisl 4.6.0-2
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, buster, sid, trixie
  • size: 11,560 kB
  • sloc: ansic: 84,814; cpp: 4,717; makefile: 5
file content (418 lines) | stat: -rw-r--r-- 13,281 bytes parent folder | download | duplicates (2)
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
 * Copyright (C) 1998, 2000-2007, 2010, 2011, 2012, 2013 SINTEF ICT,
 * Applied Mathematics, Norway.
 *
 * Contact information: E-mail: tor.dokken@sintef.no                      
 * SINTEF ICT, Department of Applied Mathematics,                         
 * P.O. Box 124 Blindern,                                                 
 * 0314 Oslo, Norway.                                                     
 *
 * This file is part of SISL.
 *
 * SISL is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version. 
 *
 * SISL 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with SISL. If not, see
 * <http://www.gnu.org/licenses/>.
 *
 * In accordance with Section 7(b) of the GNU Affero General Public
 * License, a covered work must retain the producer line in every data
 * file that is created or manipulated using SISL.
 *
 * Other Usage
 * You can be released from the requirements of the license by purchasing
 * a commercial license. Buying such a license is mandatory as soon as you
 * develop commercial activities involving the SISL library without
 * disclosing the source code of your own applications.
 *
 * This file may be used in accordance with the terms contained in a
 * written agreement between you and SINTEF ICT. 
 */

#include "sisl-copyright.h"

/*
 *
 * $Id: s6sratder.c,v 1.3 2001-03-19 15:59:02 afr Exp $
 *
 */


#define S6SRATDER

#include "sislP.h"

#if defined(SISLNEEDPROTOTYPES)
void 
s6sratder(double eder[],int idim,int ider1,int ider2,double gder[],int *jstat)
#else
void s6sratder(eder,idim,ider1,ider2,gder,jstat)
     double eder[];
     int    idim;
     int    ider1;
     int    ider2;
     double gder[];
     int    *jstat;
#endif
/*
*********************************************************************
*                                                                   
* PURPOSE    : To calculate the value and ider1*ider2 derivatives of 
*              a rational B-spline surface.
*
* INPUT      : eder    - Double array of dimenson [(ider2+1)*(ider1+1)*(idim+1)]
*                        containing the position and the derivative vectors
*                        of the surface at the point with parameter value
*                        (epar[0],epar[1]).
*                        (idim+1 is the number of components of each B-spline
*                        coefficient, i.e. the dimension of the homogemous
*                        space in which the surface lies.)
*                        These vectors are stored in the following order:
*                        First the idim+1 components of the position vector,
*                        then the idim+1 components of the D(1,0) vector,
*                        and so on up to the idim+1 components of the D(ider1,0)
*                        vector,
*                        then the idim+1 components of the D(0,1) vector etc.
*                        Equivalently, if eder is considered to be a
*                        three dimensional array, then its declaration in C
*                        would be eder[ider2+1,ider1+1,idim+1]
*              idim    - The dimension of the non homogenous space
*              ider1   - The number of input derivatives with respect
*                        to the first parameter direction.
*              ider2   - The number of input derivatives with respect
*                        to the second parameter direction.
*
*
* OUTPUT     : jstat   - Status message
*                                        >0      : Warning
*                                        =0      : ok
*                                        <0      : Error
*              gder    - Double array of dimension [(ider2+1)*(ider1+1)*idim]
*                        containing the position and the derivative vectors
*                        of the surface at the point with parameter value
*                        (epar[0],epar[1]).
*                        (idim is the number of components of each B-spline
*                        coefficient, i.e. the dimension of the Euclidean
*                        space in which the surface lies.)
*                        These vectors are stored in the following order:
*                        First the idim components of the position vector,
*                        then the idim components of the D(1,0) vector,
*                        and so on up to the idim components of the D(ider1,0)
*                        vector,
*                        then the idim components of the D(0,1) vector etc.
*                        Equivalently, if eder is considered to be a
*                        three dimensional array, then its declaration in C
*                        would be gder[ider2+1,ider1+1,idim]
*
*
* METHOD     :  The surface P(u,v) can be written as the quotient
*               P(u,v) = T(u,v) / w(u,v) where T and w are ordinary splines.
*               The dimensions of T and w are idim and 1
*               respectively. The array eder contains position
*               and derivatives of the idim+1 dimensional surface
*               (T(u,v),w(u,v)).
*
*               Now, since wP = T, we find, by the Leibnitz formula,
*
*      k   l
*                  k!       l!     (k-i,l-j) (i,j)         (k,l)
*     sum sum   -------- -------- w         P         =   T       .
*               i!(k-i)! j!(l-j)!
*     i=0 j=0
*
*               Therefore
*               
*
*              --            k   l                                     --
*      (k,l)   |   (k,l)                k!       l!     (k-i,l-j) (i,j) |    
*     P      = |  T      -  sum sum  -------- -------- w         P      | / w .
*              |                     i!(k-i)! j!(l-j)!                  |
*              --           i=0 j=0                                    --
*                               i+j<k+l
*
*               This formula is applied recursively to evaluate P's derivatives.
*
*                                                          MF.
*
*
*
* CALLS      :
*
* WRITTEN BY : Per Evensen, SI, Oslo, Norway. 1989-apr-1989
* REVISED BY : Michael Floater, 30/9/91 Bug fix for derivatives.
* REWRITTEN BY : Michael Floater, SI, 16/12/91. New algorithm.
* REWRITTEN BY : Michael Floater, SI, 25/8/92. Extend to arbitrary
*                   number of derivatives (by Leibnitz). Finally!
* REVISED BY : Michael Floater, SI, 8/9/92. Optimised.
*
*********************************************************************
*/
{
  int kpos=0;          /* Position of error.                     */
  double w0;           /* The denominator.                       */
  int ki;              /* Count through dimensions.              */
  int idu;             /* Count through derivatives in u.        */
  int idv;             /* Count through derivatives in v.        */
  int *binom=SISL_NULL;     /* Array for binomial coefficients.       */
  int *binomu=SISL_NULL;    /* Pointer to binomial coefficients in u. */
  int *binomv=SISL_NULL;    /* Pointer to binomial coefficients in v. */
  double *sum1=SISL_NULL;   /* Leibnitz expansion in u                */
  double *sum2=SISL_NULL;   /* Leibnitz expansion in u and v.         */
  double sumdum1[4];   /* Fixed space for sum1.                  */
  double sumdum2[4];   /* Fixed space for sum2.                  */
  int idimp1;          /* idim + 1.                              */
  int iw;              /* Pointer to a weight.                   */
  int iwbase;          /* Starting value of iw on each row.      */
  int igder;           /* Pointer to already calculated derivs.  */
  int igbase;          /* Starting value of igder on each row.   */
  int i,iu,iv,j,k;     /* Counters.                              */
  int ider1p1;         /* ider1 + 1.                             */
  int ider2p1;         /* ider2 + 1.                             */
  int igrow;           /* (ider1+1) * idim.                      */  
  int iwrow;           /* (ider1+1) * idimp1.                    */  
  int iwfix;           /* Index of initial weight in sum.        */
  int bidum[10];       /* Array for storing binomial coeffs.     */
  int idermax;         /* maximum of ider1 and ider2.            */
  double temp;         /* Temporary multiple.                    */
  
  if (ider1<0 || ider2<0) goto err178;
  if (idim<1) goto err102;
  
  *jstat = 0;

  /* Find denominator. */ 
  
  w0 = eder[idim];
  if (DEQUAL(w0,DZERO)) w0 = (double)1.0;

  /* If we're only asked for position, we'll do it
     now and exit for the sake of speed. */

  if(ider1 == 0 && ider2 == 0)
  {
      for(ki=0; ki<idim; ki++)
      {
          gder[ki] = eder[ki] / w0;
      }

      goto out;
  }

  /* Set up some constants. */

  idimp1  = idim + 1;
  ider1p1 = ider1 + 1;
  ider2p1 = ider2 + 1;
  igrow   = ider1p1 * idim;
  iwrow   = igrow + ider1p1;  /* = iderp1 * idimp1  */


  /* Set up  binomial coefficients.
     Use new array only when ider1 > 3 or ider2 > 3. */

  idermax = max(ider1,ider2);

  if (idermax > 3)
  { 
      binom = newarray(((idermax+1)*(idermax+2)) >> 1, INT);
      if(binom == SISL_NULL) goto err179;
  }
  else
  { 
      binom = bidum;
  }

  for(j=0,k=0; j<=idermax; j++,k+=j)
  {
      /* Calculate the new row of binomial coefficients. */
  
      binom[k] = 1;
  
      for(i=k+1; i<k+j; i++)
      {
          binom[i] = binom[i-j-1] + binom[i-j];
      }

      binom[k+j] = 1;
  }
  

  /* Set up space for sum1 and sum2 if necessary.
     Use new arrays only when idim > 4. */

  if (idim > 4)
  { 
      sum1 = newarray(idim, DOUBLE);
      if(sum1 == SISL_NULL) goto err179;
      sum2 = newarray(idim, DOUBLE);
      if(sum2 == SISL_NULL) goto err179;
  }
  else
  { 
      sum1=sumdum1;
      sum2=sumdum2;
  }


  /* Loop through derivatives in u and v. */

  for(idv=0,binomv=binom,j=0,k=0; idv<=ider2; idv++,binomv+=idv)
  {

      for(idu=0,binomu=binom; idu<=ider1; idu++,k++,binomu+=idu)
      {

    
          if(idu == 0 && idv == 0)
          {
  	      /* Position is a special case. */
    
              for(ki=0; ki<idim; ki++,j++,k++)
              {
                  gder[j] = eder[k] / w0;
              }
    
          }
          else
          {
    
              /* Calculate each coefficient of the (idu,idv)'th
		 derivative of the rational surface (in gder).
        
  	       This requires calculating the Liebnitz sum from
  	       the subarray of gder (0,..,idu, 0,...,idv) and
  	       the subarray of eder (0,..,idu, 0,...,idv). */
  
  	      iwfix = k + idim;
  
  	      /* Calculate the Leibnitz sum. */
        
              for(ki=0; ki<idim; ki++)
	      {
  	          sum2[ki] = (double)0.0;
	      }
        
              for(iv=0,igbase=0,iwbase=iwfix;
  	      iv<=idv;
  	      iv++,igbase+=igrow,iwbase-=iwrow)
              {
        
                  for(ki=0; ki<idim; ki++)
	          {
  	              sum1[ki] = (double)0.0;
	          }
 
                  for(iu=0,igder=igbase,iw=iwbase;
  	          iu<=idu;
  	          iu++,iw-=idimp1)
                  {
  		      /* Add the next Leibnitz term unless we
  		         have reached the last one (the unknown). */
  
  	              if(iu<idu || iv<idv)
  		      {
  			  /* If iu=0 or iu=idu, the u binomial
  			     coefficient is 1 so don't multiply. */
  
  		          if(iu>0 && iu<idu)
  			  {
  			    temp = (double)binomu[iu] * eder[iw];

                            for(ki=0; ki<idim; ki++,igder++)
                            {
  			      sum1[ki] += temp * gder[igder];
  			    }
  			  }
  			  else
  			  {
                            for(ki=0; ki<idim; ki++,igder++)
                            {
  			      sum1[ki] += eder[iw] * gder[igder];
  			    }
  			  }
  
  		      }
                  }
  
  		  /* If iv=0 or iv=idv, the v binomial
  		     coefficient is 1 so don't multiply. */
  
  		  if(iv>0 && iv<idv)
  		  {
                    for(ki=0; ki<idim; ki++)
		    {
  		        sum2[ki] += (double)binomv[iv] * sum1[ki];
		    }
  		  }
  		  else
  		  {
                    for(ki=0; ki<idim; ki++)
		    {
  		        sum2[ki] += sum1[ki];
		    }
  		  }
  
              }
        
              for(ki=0; ki<idim; ki++,j++,k++)
	      {
  	          gder[j] = (eder[k] - sum2[ki]) / w0;
	      }
        
          }
  
      }
    
  }


  /* Free arrays. */

  if (idermax > 3)
  { 
      freearray(binom);
  }
  
  if (idim > 4)
  { 
      freearray(sum1);
      freearray(sum2);
  }
  


  /* Done. */

  
  goto out;


/* idim less than 1. */
 err102: *jstat = -102;
         s6err("s6ratder",*jstat,kpos);
         goto out;

/* Derivative negative */
 err178: *jstat = -178;
         s6err("s6ratder",*jstat,kpos);
         goto out;


/* Not enough memory */
 err179: *jstat = -179;
         s6err("s6ratder",*jstat,kpos);
         goto out;


out: 
return;
}