File: nonc.c

package info (click to toggle)
yorick 1.4-14
  • links: PTS
  • area: main
  • in suites: potato
  • size: 5,948 kB
  • ctags: 6,609
  • sloc: ansic: 63,898; yacc: 889; makefile: 605; sh: 65; lisp: 60; fortran: 19
file content (358 lines) | stat: -rw-r--r-- 8,805 bytes parent folder | download | duplicates (3)
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
/*
    NONC.C
    Implement a few math functions not in the standard C library:

    hypot   - optional hypotenuse for systems which don't have it

    sqrtZ   - square root of complex
    expZ    - exponential of complex
    logZ    - logarithm of complex
    powZZ   - complex raised to complex power

    powLL   - long raised to long power
    powDL   - double raised to long power
    powZL   - complex raised to long power

    sinZ, cosZ, tanZ, asinZ, acosZ, atanZ, sinhZ, coshZ, tanhZ
            - complex version of other math.h functions
    signZ   - closest point on unit circle (+1 for 0)

    $Id: nonc.c,v 1.1 1993/08/27 18:32:09 munro Exp $
 */
/*    Copyright (c) 1994.  The Regents of the University of California.
                    All rights reserved.  */

/* NOTE: All complex function algorithms work when the destination
   array (z) is identical to the source array(s) (x).  */

/*--------------------------------------------------------------------------*/

/* functions defined in this file */
extern void sqrtZ(double z[2], double x[2]);
extern void expZ(double z[2], double x[2]);
extern void logZ(double z[2], double x[2]);
extern void powZZ(double z[2], double x[2], double y[2]);
extern long powLL(long b, long p);
extern double powDL(double b, long p);
extern void powZL(double z[2], double b[2], long p);

extern void sinZ(double z[2], double x[2]);
extern void cosZ(double z[2], double x[2]);
extern void tanZ(double z[2], double x[2]);
extern void asinZ(double z[2], double x[2]);
extern void acosZ(double z[2], double x[2]);
extern void atanZ(double z[2], double x[2]);
extern void sinhZ(double z[2], double x[2]);
extern void coshZ(double z[2], double x[2]);
extern void tanhZ(double z[2], double x[2]);
extern void signZ(double z[2], double x[2]);

static void SquareZ(double z[2], double x[2]);
static void CubeZ(double z[2], double x[2]);
static void MultZ(double z[2], double x[2]);
static void RecipZ(double z[2]);
static void SideZ(double z[2], double x[2]);

/* definitely in standard math library */
extern double sqrt(double);
extern double exp(double);
extern double log(double);
extern double sin(double);
extern double cos(double);
extern double atan2(double, double);

extern double asin(double);
extern double acos(double);
extern double atan(double);
extern double sinh(double);
extern double cosh(double);
extern double tanh(double);
/* may be in standard math library, maybe not */
extern double hypot(double, double);

/*--------------------------------------------------------------------------*/

#include "sysdep.h"

#ifdef NO_HYPOT
/* hypot(x,y) is sqrt(x*x+y*y)
   The problem is that without some care, the domain of x and y will be only
   half (square root) of the complete range of floating point numbers.  */
double hypot(double x, double y)
{
  if (x<0) x= -x;
  if (y<0) y= -y;
  if (x<y) x/= y;
  else if (x==0) return 0.0;
  else {
    register double tmp= x;
    x= y/x;
    y= tmp;
  }
  return y*sqrt(1.0+x*x);
}
#endif

/*--------------------------------------------------------------------------*/

void sqrtZ(double z[2], double x[2])
{
  register double x0= 0.5*x[0];
  register double x1= 0.5*x[1];
  register double r= x1? hypot(x0, x1) : (x0>0.0? x0 : -x0);
  if (x0 > 0.0) {
    z[0]= sqrt(r+x0);
    z[1]= x1/z[0];
  } else if ( (r-= x0) ) {
    z[1]= x1>=0.0? sqrt(r) : -sqrt(r);
    z[0]= x1/z[1];
  } else {
    z[0]= z[1]= 0.0;
  }
}

void expZ(double z[2], double x[2])
{
  register double r= exp(x[0]);
  z[0]= r*cos(x[1]);
  z[1]= r*sin(x[1]);
}

void logZ(double z[2], double x[2])
{
  register double x0= x[0];
  z[0]= log(hypot(x0, x[1]));
  z[1]= atan2(x[1], x0);
}

void powZZ(double z[2], double x[2], double y[2])
{
  double logx[2], z0;
  logZ(logx, x);
  z0=   y[0]*logx[0] - y[1]*logx[1];
  z[1]= y[0]*logx[1] + y[1]*logx[0];
  z[0]= z0;
  expZ(z, z);
}

long powLL(long b, long p)
{
  if (p==0) return b? 1 : 1/powLL(0L,1L);
  else if (p==1) return b;
  else if (p==2) return b*b;
  else if (p==3) return b*b*b;
  else if (p>0) {
    register long c, q= (p&3);
    p>>= 2;
    if (p==1) c= b;
    else if (p==2) c= b*b;
    else if (p==3) c= b*b*b;
    else c= powLL(b, p);      /* recurse for large powers (p>15) */
    c*= c;
    if (q&2) c*= b;
    if (q&1) return c*c*b;
    else return c*c;
  } else /* (p<0) */ {
    if (b==1) return 1;
    else if (b==-1) return (p&1)? -1 : 1; /* assumes 2s complement */
    else return 0;  /* incorrect if b==0... */
  }
}

double powDL(double b, long p)
{
  if (p==0) return b? 1.0 : 1.0/powDL(0.0, 1L);
  else if (p==1) return b;
  else if (p==2) return b*b;
  else if (p==3) return b*b*b;
  else if (p>0) {
    register double c;
    register long q= (p&3);
    p>>= 2;
    if (p==1) c= b;
    else if (p==2) c= b*b;
    else if (p==3) c= b*b*b;
    else c= powDL(b, p);      /* recurse for large powers (p>15) */
    c*= c;
    if (q&2) c*= b;
    if (q&1) return c*c*b;
    else return c*c;
  } else /* (p<0) */ {
    if (p==-p) p+= 1;        /* p=1<<31 (or 63) is <0, but p==-p */
    return 1.0/powDL(b, -p);
  }
}

static void SquareZ(double z[2], double x[2])
{
  register double x0= x[0];
  z[0]= x0*x0 - x[1]*x[1];
  z[1]= 2.0*x0*x[1];
}

static void CubeZ(double z[2], double x[2])
{
  register double x0= x[0];
  z[0]= x0*x0*x0 - 3.0*x0*x[1]*x[1];
  z[1]= 3.0*x0*x0*x[1] - x[1]*x[1]*x[1];
}

static void MultZ(double z[2], double x[2])
{
  register double x0= x[0];
  register double z0= z[0];
  z[0]= x0*z0 - x[1]*z[1];
  z[1]= x[1]*z0 + x0*z[1];
}

static void RecipZ(double z[2])
{
  register double z0= z[0];
  register double z1= z[1];
  register double nrm;
  if ((z0>0?z0:-z0)>(z1>0?z1:-z1)) {
    nrm= z0;  z0= 1.0;  z1/= nrm;  nrm*= 1.0+z1*z1;
  } else {
    nrm= z1;  z0/= nrm;  z1= 1.0;  nrm*= 1.0+z0*z0;
  }
  z[0]= z0/nrm;
  z[1]= -z1/nrm;
}

void powZL(double z[2], double b[2], long p)
{
  if (p==0) { z[0]= (b[0]||b[1])? 1.0 : 1.0/powDL(0.0, 1L);  z[1]= 0.0; }
  else if (p==1) { z[0]= b[0];  z[1]= b[1]; }
  else if (p==2) { SquareZ(z, b); return; }
  else if (p==3) { CubeZ(z, b); return; }
  else if (p>0) {
    double c[2];
    register long q= (p&3);
    p>>= 2;
    if (p==1) { c[0]= b[0];  c[1]= b[1]; }
    else if (p==2) SquareZ(c, b);
    else if (p==3) { CubeZ(c, b); }
    else powZL(c, b, p);      /* recurse for large powers (p>15) */
    SquareZ(c, c);
    if (q&2) MultZ(c, b);
    SquareZ(c, c);
    if (q&1) MultZ(c, b);
    z[0]= c[0];
    z[1]= c[1];
  } else /* (p<0) */ {
    powZL(z, b, -p);
    RecipZ(z);
  }
}

/*--------------------------------------------------------------------------*/

void sinZ(double z[2], double x[2])
{
  register double sinx= sin(x[0]);
  register double cosx= cos(x[0]);
  z[0]= sinx*cosh(x[1]);
  z[1]= cosx*sinh(x[1]);
}

void cosZ(double z[2], double x[2])
{
  register double sinx= sin(x[0]);
  register double cosx= cos(x[0]);
  z[0]= cosx*cosh(x[1]);
  z[1]= -sinx*sinh(x[1]);
}

void tanZ(double z[2], double x[2])
{
  register double tanh2y= tanh(2*x[1]);
  register double sech2y= sqrt(1.0-tanh2y*tanh2y); /* cosh can overflow */
  register double denom= 1.0 + cos(2*x[0])*sech2y;
  z[0]= sin(2*x[0])*sech2y/denom;
  z[1]= tanh2y/denom;
}

void sinhZ(double z[2], double x[2])
{
  register double sinhx= sinh(x[0]);
  register double coshx= cosh(x[0]);
  z[0]= sinhx*cos(x[1]);
  z[1]= coshx*sin(x[1]);
}

void coshZ(double z[2], double x[2])
{
  register double sinhx= sinh(x[0]);
  register double coshx= cosh(x[0]);
  z[0]= coshx*cos(x[1]);
  z[1]= sinhx*sin(x[1]);
}

void tanhZ(double z[2], double x[2])
{
  register double tanh2x= tanh(2*x[0]);
  register double sech2x= sqrt(1.0-tanh2x*tanh2x); /* cosh can overflow */
  register double denom= 1.0 + cos(2*x[1])*sech2x;
  z[0]= tanh2x/denom;
  z[1]= sin(2*x[1])*sech2x/denom;
}

static void SideZ(double z[2], double x[2])
{
  register double x0= x[0];
  z[0]= 1.0 - (x0*x0 - x[1]*x[1]);
  z[1]= -2.0*x0*x[1];
  sqrtZ(z, z);    /* sqrt(1-x^2) */
}

void asinZ(double z[2], double x[2])
{
  double side[2];
  SideZ(side, x);
  side[0]-= x[1];
  side[1]+= x[0];
  logZ(side, side);
  z[0]= side[1];
  z[1]= -side[0];
}

void acosZ(double z[2], double x[2])
{
  register double tmp;
  double side[2];
  SideZ(side, x);
  tmp= side[0];
  side[0]= x[0]-side[1];
  side[1]= x[1]+tmp;
  logZ(side, side);
  z[0]= side[1];
  z[1]= -side[0];
}

void atanZ(double z[2], double x[2])
{
  double side[2];
  z[0]= -x[0];
  z[1]= 1.0-x[1];
  RecipZ(z);
  side[0]= x[0];
  side[1]= 1.0+x[1];
  MultZ(side, z);
  logZ(side, side);
  z[0]= -0.5*side[1];
  z[1]= 0.5*side[0];
}

void signZ(double z[2], double x[2])
{
  register double r= hypot(x[0], x[1]);
  if (r!=0.0) {
    z[0]/= r;
    z[1]/= r;
  } else {
    z[0]= 1.0;
    z[1]= 0.0;
  }
}

/*--------------------------------------------------------------------------*/