Elliott Hughes | ab52807 | 2018-07-24 00:01:52 +0000 | [diff] [blame] | 1 | /*- |
| 2 | * Copyright (c) 2008 Stephen L. Moshier <steve@moshier.net> |
| 3 | * |
| 4 | * Permission to use, copy, modify, and distribute this software for any |
| 5 | * purpose with or without fee is hereby granted, provided that the above |
| 6 | * copyright notice and this permission notice appear in all copies. |
| 7 | * |
| 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 | */ |
| 16 | |
| 17 | /* cpow |
| 18 | * |
| 19 | * Complex power function |
| 20 | * |
| 21 | * |
| 22 | * |
| 23 | * SYNOPSIS: |
| 24 | * |
| 25 | * double complex cpow(); |
| 26 | * double complex a, z, w; |
| 27 | * |
| 28 | * w = cpow (a, z); |
| 29 | * |
| 30 | * |
| 31 | * |
| 32 | * DESCRIPTION: |
| 33 | * |
| 34 | * Raises complex A to the complex Zth power. |
| 35 | * Definition is per AMS55 # 4.2.8, |
| 36 | * analytically equivalent to cpow(a,z) = cexp(z clog(a)). |
| 37 | * |
| 38 | * ACCURACY: |
| 39 | * |
| 40 | * Relative error: |
| 41 | * arithmetic domain # trials peak rms |
| 42 | * IEEE -10,+10 30000 9.4e-15 1.5e-15 |
| 43 | * |
| 44 | */ |
| 45 | |
Elliott Hughes | ab52807 | 2018-07-24 00:01:52 +0000 | [diff] [blame] | 46 | #include <complex.h> |
| 47 | #include <float.h> |
| 48 | #include <math.h> |
Elliott Hughes | 2d1a2aa | 2019-02-04 13:43:48 -0800 | [diff] [blame] | 49 | #include "math_private.h" |
Elliott Hughes | ab52807 | 2018-07-24 00:01:52 +0000 | [diff] [blame] | 50 | |
| 51 | double complex |
| 52 | cpow(double complex a, double complex z) |
| 53 | { |
| 54 | double complex w; |
| 55 | double x, y, r, theta, absa, arga; |
| 56 | |
| 57 | x = creal (z); |
| 58 | y = cimag (z); |
| 59 | absa = cabs (a); |
| 60 | if (absa == 0.0) { |
Elliott Hughes | 2d1a2aa | 2019-02-04 13:43:48 -0800 | [diff] [blame] | 61 | return (CMPLX(0.0, 0.0)); |
Elliott Hughes | ab52807 | 2018-07-24 00:01:52 +0000 | [diff] [blame] | 62 | } |
| 63 | arga = carg (a); |
| 64 | r = pow (absa, x); |
| 65 | theta = x * arga; |
| 66 | if (y != 0.0) { |
| 67 | r = r * exp (-y * arga); |
| 68 | theta = theta + y * log (absa); |
| 69 | } |
Elliott Hughes | 2d1a2aa | 2019-02-04 13:43:48 -0800 | [diff] [blame] | 70 | w = CMPLX(r * cos (theta), r * sin (theta)); |
Elliott Hughes | ab52807 | 2018-07-24 00:01:52 +0000 | [diff] [blame] | 71 | return (w); |
| 72 | } |