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
|
/*
* A handy, tested, small, stand-alone, double precision floating point
* complex number arithmetic library for C.
* Just include "complex.h" if you use this.
*
* Copyright (C) 1987-2012 George Gesslein II.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
The chief copyright holder can be contacted at gesslein@mathomatic.org, or
George Gesslein II, P.O. Box 224, Lansing, NY 14882-0224 USA.
*/
#include "complex.h"
#include <math.h>
#define true 1
#define false 0
#define epsilon 0.00000000000005 /* a good value for doubles */
/*
* Zero out relatively very small real or imaginary parts of a complex number,
* because they probably are a result of accumulated floating point inaccuracies.
*
* Return true if something was zeroed out.
*/
int
complex_fixup(ap)
complexs *ap; /* complex number pointer */
{
if (fabs(ap->re * epsilon) > fabs(ap->im)) {
ap->im = 0.0;
return true;
}
if (fabs(ap->im * epsilon) > fabs(ap->re)) {
ap->re = 0.0;
return true;
}
return false;
}
/*
* Add two complex numbers (a + b)
* and return the complex number result.
*
* Complex number subtraction (a - b) is done by
* complex_add(a, complex_negate(b)).
*/
complexs
complex_add(a, b)
complexs a, b;
{
a.re += b.re;
a.im += b.im;
return(a);
}
/*
* Negate a complex number (-a)
* and return the complex number result.
*/
complexs
complex_negate(a)
complexs a;
{
a.re = -a.re;
a.im = -a.im;
return(a);
}
/*
* Multiply two complex numbers (a * b)
* and return the complex number result.
*/
complexs
complex_mult(a, b)
complexs a, b;
{
complexs r;
r.re = a.re * b.re - a.im * b.im;
r.im = a.re * b.im + a.im * b.re;
return(r);
}
/*
* Divide two complex numbers (a / b)
* and return the complex number result.
*/
complexs
complex_div(a, b)
complexs a; /* dividend */
complexs b; /* divisor */
{
complexs r, num;
double denom;
b.im = -b.im;
num = complex_mult(a, b);
denom = b.re * b.re + b.im * b.im;
r.re = num.re / denom;
r.im = num.im / denom;
return r;
}
/*
* Take the natural logarithm of a complex number
* and return the complex number result.
*/
complexs
complex_log(a)
complexs a;
{
complexs r;
r.re = log(a.re * a.re + a.im * a.im) / 2.0;
r.im = atan2(a.im, a.re);
return(r);
}
/*
* Raise the natural number (e) to the power of a complex number (e^a)
* and return the complex number result.
*/
complexs
complex_exp(a)
complexs a;
{
complexs r;
double m;
m = exp(a.re);
r.re = m * cos(a.im);
r.im = m * sin(a.im);
return(r);
}
/*
* Raise complex number "a" to the power of complex number "b" (a^b)
* and return the complex number result.
*/
complexs
complex_pow(a, b)
complexs a, b;
{
complexs r;
r = complex_log(a);
r = complex_mult(r, b);
r = complex_exp(r);
complex_fixup(&r);
return(r);
}
|