SDDS ToolKit Programs and Libraries for C and Python
Loading...
Searching...
No Matches
complex.cc
Go to the documentation of this file.
1/**
2 * @file complex.cc
3 * @brief Implementation of complex number functions.
4 *
5 * @copyright
6 * - (c) 2002 The University of Chicago, as Operator of Argonne National Laboratory.
7 * - (c) 2002 The Regents of the University of California, as Operator of Los Alamos National Laboratory.
8 *
9 * @license
10 * This file is distributed under the terms of the Software License Agreement
11 * found in the file LICENSE included with this distribution.
12 *
13 * @author M. Borland, R. Soliday
14 */
15
16#include <complex>
17#include "mdb.h"
18
19/**
20 * @brief Computes the complex error function of a given complex number.
21 *
22 * @param z The complex number input.
23 * @param overflow Pointer to a flag variable to store computation status.
24 * 1 = error, 0 = no error
25 * @return The complex error function value of z.
26 */
27std::complex<double> complexErf(std::complex<double> z, long *overflow) {
28 double xi, yi;
29 double u = 0, v = 0;
30 xi = z.real();
31 yi = z.imag();
32 wofz(&xi, &yi, &u, &v, overflow);
33 return std::complex<double>(u, v);
34}
35
36/**
37 * @brief Computes the complex exponential of an imaginary number.
38 *
39 * @param p The real number representing the imaginary part.
40 * @return The complex exponential of the input.
41 */
42std::complex<double> cexpi(double p) {
43 std::complex<double> a;
44 a = cos(p) + std::complex<double>(0, 1) * sin(p);
45 return (a);
46}
47
48/**
49 * @brief Raises a complex number to an integer power.
50 *
51 * @param a The base complex number.
52 * @param n The exponent (integer).
53 * @return The complex number a raised to the power n.
54 */
55std::complex<double> cipowr(std::complex<double> a, int n) {
56 int i;
57 std::complex<double> p(1, 0);
58
59 if (n >= 0) {
60 for (i = 0; i < n; i++)
61 p = p * a;
62 return (p);
63 }
64 a = p / a;
65 return (cipowr(a, -n));
66}
67
68/**
69 * @brief Multiplies two complex numbers.
70 *
71 * @deprecated These routines are obsolete, really, but some code uses them.
72 *
73 * @param r0 Pointer to store the real part of the result.
74 * @param i0 Pointer to store the imaginary part of the result.
75 * @param r1 Real part of the first complex number.
76 * @param i1 Imaginary part of the first complex number.
77 * @param r2 Real part of the second complex number.
78 * @param i2 Imaginary part of the second complex number.
79 */
81 double *r0, double *i0, /* result */
82 double r1, double i1,
83 double r2, double i2) {
84 double tempr;
85
86 tempr = r1 * r2 - i1 * i2;
87 *i0 = r1 * i2 + i1 * r2;
88 *r0 = tempr;
89}
90
91/**
92 * @brief Divides two complex numbers.
93 *
94 * @deprecated These routines are obsolete, really, but some code uses them.
95 *
96 * @param r0 Pointer to store the real part of the result.
97 * @param i0 Pointer to store the imaginary part of the result.
98 * @param r1 Real part of the numerator complex number.
99 * @param i1 Imaginary part of the numerator complex number.
100 * @param r2 Real part of the denominator complex number.
101 * @param i2 Imaginary part of the denominator complex number.
102 * @param threshold The threshold to prevent division by very small numbers.
103 */
105 double *r0, double *i0, /* result */
106 double r1, double i1,
107 double r2, double i2,
108 double threshold) {
109 double tempr, denom;
110
111 if ((denom = sqr(r2) + sqr(i2)) < threshold)
112 denom = threshold;
113 i2 = -i2;
114 tempr = (r1 * r2 - i1 * i2) / denom;
115 *i0 = (r1 * i2 + i1 * r2) / denom;
116 *r0 = tempr;
117}
std::complex< double > complexErf(std::complex< double > z, long *overflow)
Computes the complex error function of a given complex number.
Definition complex.cc:27
std::complex< double > cipowr(std::complex< double > a, int n)
Raises a complex number to an integer power.
Definition complex.cc:55
void complex_divide(double *r0, double *i0, double r1, double i1, double r2, double i2, double threshold)
Divides two complex numbers.
Definition complex.cc:104
void complex_multiply(double *r0, double *i0, double r1, double i1, double r2, double i2)
Multiplies two complex numbers.
Definition complex.cc:80
std::complex< double > cexpi(double p)
Computes the complex exponential of an imaginary number.
Definition complex.cc:42
int wofz(doublereal *xi, doublereal *yi, doublereal *u, doublereal *v, logical *flag__)
Computes the complex error function for a given complex number .
Definition wofz.c:79