source: TRACY3/trunk/tracy/tracy/src/tpsa_lin.cc @ 3

Last change on this file since 3 was 3, checked in by zhangj, 11 years ago

Initiale import

  • Property svn:executable set to *
File size: 11.6 KB
Line 
1/* Tracy-2
2
3   J. Bengtsson, CBP, LBL      1990 - 1994   Pascal version
4                 SLS, PSI      1995 - 1997
5   M. Boege      SLS, PSI      1998          C translation
6   L. Nadolski   SOLEIL        2002          Link to NAFF, Radia field maps
7   J. Bengtsson  NSLS-II, BNL  2004 -       
8
9   Note, the operators "operator*=()", etc. requires
10   the use of a local variable
11
12*/
13
14
15#define danamlen  10
16#define dafunlen   4
17
18typedef char    danambuf[danamlen];
19typedef char    funnambuf[dafunlen];
20typedef int     iVector[ss_dim];
21
22extern const int  nv_tps, nd_tps, ndpt_tps, iref_tps;
23extern int        no_tps;
24extern double     eps_tps;
25
26
27void daeps_(const double eps) { eps_tps = eps; }
28
29
30void danot_(const int no) {
31
32  if (no != 1) {
33    printf("danot_: max order exceeded %d (%d)\n", no, 1);
34    exit_(0);
35  }
36}
37
38
39void daini_(int no, int nv, int fio)
40{
41
42  eps_tps = 1e-14;
43  if (no != 1) {
44    printf("daini_: max order exceeded %d (%d)\n", no, 1);
45    exit_(1);
46  }
47  if ((nv < 1) || (nv > ss_dim))
48    printf("daini_: to many dimensions %d(%d)\n", nv, ss_dim);
49}
50
51
52void lieini(const int no, const int nv, const int nd2i)
53{
54
55  if ((nv < 1) || (nv > ss_dim))
56    printf("lieini: max dim exceeded %d (%d)\n", nv, ss_dim);
57}
58
59/**********************************************
60void daall_(tps_buf &x, const int nd2, const char *daname,
61           const int no, const int nv)
62           
63  Purpose:
64     DA initialization 6-D coordinate vector x     
65
66***********************************************/
67void daall_(tps_buf &x, const int nd2, const char *daname,
68           const int no, const int nv)
69{
70  int  j;
71
72  for (j = 0; j <= ss_dim; j++)
73     x[j] = 0.0;
74}
75
76
77/********************************************
78void dadal_(tps_buf &x, const int ss_dim)
79
80  Purpose:
81    None
82   
83*********************************************/
84void dadal_(tps_buf &x, const int ss_dim) { }
85
86
87
88/**********************************************
89void davar_(tps_buf &x, const double r, const int i)
90           
91  Purpose:
92     DA           
93
94***********************************************/
95void davar_(tps_buf &x, const double r, const int i)
96{
97  int  j;
98
99  x[0] = r;
100  for (j = 1; j <= ss_dim; j++)  x[j] = 0.0;
101 
102  x[i] = 1.0;
103}
104
105
106/*****************************************
107void dacon_(tps_buf &x, const double r)
108
109 Purpose:
110   Initialized 6-D vector x
111
112*****************************************/
113void dacon_(tps_buf &x, const double r)
114{
115  int  j;
116
117  x[0] = r;
118  for (j = 1; j <= ss_dim; j++)
119    x[j] = 0.0;
120}
121
122
123void dapek_(const tps_buf &x, const int jj[], double &r)
124{
125  int  i, nzero;
126
127  nzero  = 0;
128  for (i = 0; i < ss_dim; i++) {
129    if (jj[i] == 0)
130      nzero++;
131    else if (jj[i] == 1)
132      r = x[i+1];
133    else
134      printf("Invalid jj in dapek\n");
135  }
136
137  if (nzero == ss_dim)
138    r = x[0];
139  else if (nzero < ss_dim-1)
140    printf("Invalid jj in dapek\n");
141}
142
143
144void dapok_(tps_buf &x, const int jj[], const double r)
145{
146  int  i, nzero;
147
148  nzero = 0;
149  for (i = 0; i < ss_dim; i++) {
150    if (jj[i] == 0)
151      nzero++;
152    else if (jj[i] == 1)
153      x[i+1] = r;
154    else
155      printf("Invalid jj in dapek\n");
156  }
157  if (nzero == ss_dim)
158    x[0] = r;
159  else if (nzero < ss_dim-1)
160    printf("Invalid jj in dapek\n");
161}
162
163
164double getmat(const ss_vect<tps> &map, const int i, const int j)
165{
166
167  return map[i-1].ltps[j];
168}
169
170
171void putmat(ss_vect<tps> &map, const int i, const int j, const double r)
172{
173
174  map[i-1].ltps[j] = r;
175}
176
177
178void getlinmat(const int nv, const ss_vect<tps> &map, Matrix &mat)
179{
180  int  j, k;
181
182  for (j = 1; j <= nv; j++)
183    for (k = 1; k <= nv; k++)
184      mat[j-1][k-1] = getmat(map, j, k);
185}
186
187
188void putlinmat(const int nv, const Matrix &mat, ss_vect<tps> &map)
189{
190  /* Puts zeroes in constant part of da map */
191  int j, k;
192
193  for (j = 1; j <= nv; j++) {
194    for (k = 0; k <= nv; k++) {
195      if (k == 0)
196        putmat(map, j, k, 0.0);
197      else
198        putmat(map, j, k, mat[j-1][k-1]);
199    }
200  }
201}
202
203
204void dacop_(const tps_buf &x, tps_buf &z)
205{
206  int  i;
207
208  for (i = 0; i <= ss_dim; i++)
209    z[i] = x[i];
210}
211
212
213void daadd_(const tps_buf &x, const tps_buf &y, tps_buf &z)
214{
215  int  i;
216
217  for (i = 0; i <= ss_dim; i++)
218    z[i] = x[i] + y[i];
219}
220
221
222void dasub_(const tps_buf &x, const tps_buf &y, tps_buf &z)
223{
224  int  i;
225
226  for (i = 0; i <= ss_dim; i++)
227    z[i] = x[i] - y[i];
228}
229
230
231void damul_(const tps_buf &x, const tps_buf &y, tps_buf &z)
232{
233  int      i;
234  tps_buf  u;
235
236  u[0] = x[0]*y[0];
237  for (i = 1; i <= ss_dim; i++)
238    u[i] = x[0]*y[i] + x[i]*y[0];
239  dacop_(u, z);
240}
241
242
243void dafun_(const char *fun, const tps_buf &x, tps_buf &z);
244
245void dadiv_(const tps_buf &x, const tps_buf &y, tps_buf &z)
246{
247  tps_buf  yinv;
248
249  dafun_("INV ", y, yinv); damul_(x, yinv, z);
250}
251
252
253void dacad_(const tps_buf &x, const double y, tps_buf &z)
254{
255  int  i;
256
257  z[0] = x[0] + y;
258  for (i = 1; i <= ss_dim; i++)
259    z[i] = x[i];
260}
261
262
263void dacsu_(const tps_buf &x, const double y, tps_buf &z)
264{
265
266  dacad_(x, -y, z);
267}
268
269
270void dacmu_(const tps_buf &x, const double y, tps_buf &z)
271{
272  int  i;
273
274  for (i = 0; i <= ss_dim; i++)
275    z[i] = x[i]*y;
276}
277
278
279void dasuc_(const tps_buf &x, const double y, tps_buf &z)
280{
281  tps_buf  negx;
282
283  dacmu_(x, -1.0, negx); dacad_(negx, y, z);
284}
285
286
287void dacdi_(const tps_buf &x, const double y, tps_buf &z)
288{
289
290  dacmu_(x, 1.0/y, z);
291}
292
293
294void dadic_(const tps_buf &x, const double y, tps_buf &z)
295{
296  tps_buf  xinv;
297
298  dafun_("INV ", x, xinv); dacmu_(xinv, y, z);
299}
300
301
302void dapos_(const tps_buf &x, tps_buf &z)
303{
304
305  if (x[0] < 0.0) dacmu_(x, -1.0, z);
306}
307
308
309void dasqr_(const tps_buf &x, tps_buf &z) { damul_(x, x, z); }
310
311
312void dacma_(const tps_buf &x, const tps_buf &y, const double rb, tps_buf &z)
313{
314  tps_buf  x1;
315
316  dacmu_(y, rb, x1);  /* x1=y*rb */
317  daadd_(x, x1, z);   /* z =x+x1 */
318}
319
320
321void dalin_(const tps_buf &x, const double ra, const tps_buf &y,
322            const double rb, tps_buf &z)
323{
324  tps_buf  x1, x2;
325
326  dacmu_(x, ra, x1); dacmu_(y, rb, x2); daadd_(x1, x2, z);
327}
328
329
330void dainv_(const tps_buf &x, tps_buf &z)
331{
332  int     i;
333  double  a;
334
335  a = -1.0/sqr(x[0]); z[0] = 1.0/x[0];
336  for (i = 1; i <= ss_dim; i++)
337    z[i] = a*x[i];
338}
339
340
341void dasqrt(const tps_buf &x, tps_buf &z)
342{
343  int     i;
344  double  a;
345
346  a = sqrt(x[0]); z[0] = a; a = 0.5/a;
347  for (i = 1; i <= ss_dim; i++)
348    z[i] = a*x[i];
349}
350
351
352void daexp(const tps_buf &x, tps_buf &z)
353{
354  int     i;
355  double  a;
356
357  a = exp(x[0]); z[0] = a;
358  for (i = 1; i <= ss_dim; i++)
359    z[i] = a*x[i];
360}
361
362
363void dalog(const tps_buf &x, tps_buf &z)
364{
365  int i;
366
367  z[0] = log(x[0]);
368  for (i = 1; i <= ss_dim; i++)
369    z[i] = x[i]/x[0];
370}
371
372
373void dasin(const tps_buf &x, tps_buf &z)
374{
375  int     i;
376  double  a;
377
378  z[0] = sin(x[0]); a = cos(x[0]);
379  for (i = 1; i <= ss_dim; i++)
380    z[i] = a*x[i];
381}
382
383
384void dacos(const tps_buf &x, tps_buf &z)
385{
386  int     i;
387  double  a;
388
389  z[0] = cos(x[0]); a = -sin(x[0]);
390  for (i = 1; i <= ss_dim; i++)
391    z[i] = a*x[i];
392}
393
394
395void dasinh(const tps_buf &x, tps_buf &z)
396{
397  int     i;
398  double  a;
399
400  z[0] = sinh(x[0]); a = cosh(x[0]);
401  for (i = 1; i <= ss_dim; i++)
402    z[i] = a*x[i];
403}
404
405
406void dacosh(const tps_buf &x, tps_buf &z)
407{
408  int     i;
409  double  a;
410
411  z[0] = cos(x[0]); a = sinh(x[0]);
412  for (i = 1; i <= ss_dim; i++)
413    z[i] = a*x[i];
414}
415
416/****************************
417 * tan(x)
418 * *************************/
419void datan(const tps_buf &x, tps_buf &z)
420{
421  tps_buf  c, s;
422
423  dacos(x, c); dasin(x, s); dadiv_(s, c, z);
424}
425
426/*************************
427 *  cotan(x)
428 * ***********************/
429void dactan(const tps_buf &x, tps_buf &z)
430{
431  tps_buf  c, s;
432
433  dacos(x, c); dasin(x, s); dadiv_(c, s, z);
434}
435
436/*************************************
437 *
438 * Purpose:
439 *     Get the derivatives of the arcsin(x)
440 *   
441 *    d(arcsin(u))/dx = (1/sqrt(1-u^2))*du/dx;
442 *
443 *  Input:
444 *         x: u
445 *  Output:
446 *        z: the differential of x.
447 *
448 *       
449 * Comments:
450 *           13/11/2012
451 *          Written by Jianfeng Nadolski @ LAL
452 *                     
453 * ***********************************/
454void daarcsin(const tps_buf &x, tps_buf &z)
455{
456  int     i;
457  double  a;  //differential of x; a = sqrt(1/1-u^2)
458
459  a = x[0]; z[0] = asin(a); a = 1.0/sqrt(1.0-sqr(a));
460  for (i = 1; i <= ss_dim; i++)
461    z[i] = a*x[i];
462}
463
464/*************************************
465 *
466 * Purpose:
467 *     Get the derivatives of the arccos(x)
468 *   
469 *    d(arccos(u))/dx = -(1/sqrt(1-u^2))*du/dx;
470 *
471 *  Input:
472 *         x: u
473 *  Output:
474 *        z: the differential of x.
475 *
476 *       
477 * Comments:
478 *           13/11/2012
479 *          Written by Jianfeng Nadolski @ LAL
480 *                     
481 * ***********************************/
482void daarccos(const tps_buf &x, tps_buf &z)
483{
484int     i;
485  double  a;  //differential of x; a = -sqrt(1/1-u^2)
486
487  a = x[0]; z[0] = asin(a); a = -1.0/sqrt(1.0-sqr(a));
488  for (i = 1; i <= ss_dim; i++)
489    z[i] = a*x[i];
490 
491}
492/*************************************
493 *
494 * Purpose:
495 *     Get the derivatives of the arctan(x)
496 *   
497 *    d(arctan(u))/dx = (1/1+u^2)*du/dx;
498 *
499 *  Input:
500 *         x: u
501 *  Output:
502 *        z: the differential of x.
503 *
504 *         
505 * ***********************************/
506void daarctan(const tps_buf &x, tps_buf &z)
507{
508  int     i;
509  double  a;  //differential of x; a = (1/1+u^2)
510
511  a = x[0]; z[0] = atan(a); a = 1.0/(1.0+sqr(a));
512  for (i = 1; i <= ss_dim; i++)
513    z[i] = a*x[i];
514}
515
516/*************************************
517 *
518 * Purpose:
519 *     Get the derivatives of the arcctan(x)
520 *   
521 *    d(arcctan(u))/dx = -(1/1+u^2)*du/dx;
522 *
523 *  Input:
524 *         x: u
525 *  Output:
526 *        z: the differential of x.
527 *
528 *         
529 * ***********************************/
530void daarcctan(const tps_buf &x, tps_buf &z)
531{
532  int     i;
533  double  a;  //differential of x; a = -(1/1+u^2)
534
535  a = x[0]; z[0] = atan(a); a = -1.0/(1.0+sqr(a));
536  for (i = 1; i <= ss_dim; i++)
537    z[i] = a*x[i];
538}
539
540void dafun_(const char *fun, const tps_buf &x, tps_buf &z)
541{
542  tps_buf  u;
543 
544  if (!strncmp(fun, "INV ", dafunlen))
545    dainv_(x, u);
546  else if (!strncmp(fun, "SQRT", dafunlen))
547    dasqrt(x, u);
548  else if (!strncmp(fun, "EXP ", dafunlen))
549    daexp(x, u);
550  else if (!strncmp(fun, "LOG ", dafunlen))
551    dalog(x, u);
552  else if (!strncmp(fun, "SIN ", dafunlen))
553    dasin(x, u);
554  else if (!strncmp(fun, "COS ", dafunlen))
555    dacos(x, u);
556  else if (!strncmp(fun, "TAN ", dafunlen))
557    datan(x, u);
558  else if (!strncmp(fun, "CTAN ", dafunlen))
559    dactan(x, u);
560  else if (!strncmp(fun, "SINH ", dafunlen))
561    dasinh(x, u);
562  else if (!strncmp(fun, "COSH ", dafunlen))
563    dacosh(x, u);
564  else if (!strncmp(fun, "ASIN", dafunlen))
565    daarcsin(x, u);
566  else if (!strncmp(fun, "ACOS", dafunlen))
567    daarccos(x, u);
568  else if (!strncmp(fun, "ATAN", dafunlen))
569    daarctan(x, u);
570  else if (!strncmp(fun, "ACTAN", dafunlen))
571    daarcctan(x, u);
572  else {
573    printf("dafun: illegal function %s\n", fun);
574    exit_(0);
575  }
576
577  dacop_(u, z);
578}
579
580
581void dacct_(const ss_vect<tps> &x, const int i,
582            const ss_vect<tps> &y, const int j, ss_vect<tps> &z, const int k)
583{
584  int           l, m, n;
585  ss_vect<tps>  u;
586
587  for (l = 0; l < k; l++) {
588    u[l].ltps[0] = x[l].ltps[0];
589    for (m = 1; m <= j; m++)
590      u[l].ltps[m] = 0.0;
591    for (m = 0; m <= j; m++)
592      for (n = 1; n <= i; n++)
593        u[l].ltps[m] += x[l].ltps[n]*y[n-1].ltps[m];
594  }
595  for (l = 0; l < k; l++)
596    z[l] = u[l];
597}
598
599
600void dainv_(const ss_vect<tps> &x, const int i, ss_vect<tps> &z, const int k)
601{
602  Matrix  mat;
603
604  getlinmat(i, x, mat);  /* gets linear matrix from x */
605  if (InvMat(i, mat))    /* inverses matrix of rank i */
606    putlinmat(k, mat, z);/* puts linear matrix into z */
607  else
608    printf("dainv: map is singular\n");
609}
610
611
612void Rotmap(const int n, ss_vect<tps> &map, const Matrix &R)
613{
614  Matrix  mat;
615
616  getlinmat(n, map, mat); MulRMat(n, mat, R); putlinmat(n, mat, map);
617}
618
619
620void daabs_(const tps_buf &x, double &r)
621{
622  int     k;
623
624  r = 0.0;
625  for (k = 0; k <= ss_dim; k++)
626    r += fabs(x[k]);
627}
628
629
630void daabs2_(const tps_buf &x, double &r)
631{
632  int     k;
633
634  r = 0.0;
635  for (k = 0; k <= ss_dim; k++)
636    r += sqr(x[k]);
637}
Note: See TracBrowser for help on using the repository browser.