SDDSlib
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 flag Pointer to a flag variable to store computation status.
24 * @return The complex error function value of z.
25 */
26std::complex<double> complexErf(std::complex<double> z, long *flag) {
27 double xi, yi;
28 double u = 0, v = 0;
29 long lflag = 0;
30 xi = z.real();
31 yi = z.imag();
32 wofz(&xi, &yi, &u, &v, &lflag);
33 *flag = lflag;
34 return std::complex<double>(u, v);
35}
36
37/**
38 * @brief Computes the complex exponential of an imaginary number.
39 *
40 * @param p The real number representing the imaginary part.
41 * @return The complex exponential of the input.
42 */
43std::complex<double> cexpi(double p) {
44 std::complex<double> a;
45 a = cos(p) + std::complex<double>(0, 1) * sin(p);
46 return (a);
47}
48
49/**
50 * @brief Raises a complex number to an integer power.
51 *
52 * @param a The base complex number.
53 * @param n The exponent (integer).
54 * @return The complex number a raised to the power n.
55 */
56std::complex<double> cipowr(std::complex<double> a, int n) {
57 int i;
58 std::complex<double> p(1, 0);
59
60 if (n >= 0) {
61 for (i = 0; i < n; i++)
62 p = p * a;
63 return (p);
64 }
65 a = p / a;
66 return (cipowr(a, -n));
67}
68
69/**
70 * @brief Multiplies two complex numbers.
71 *
72 * @deprecated These routines are obsolete, really, but some code uses them.
73 *
74 * @param r0 Pointer to store the real part of the result.
75 * @param i0 Pointer to store the imaginary part of the result.
76 * @param r1 Real part of the first complex number.
77 * @param i1 Imaginary part of the first complex number.
78 * @param r2 Real part of the second complex number.
79 * @param i2 Imaginary part of the second complex number.
80 */
82 double *r0, double *i0, /* result */
83 double r1, double i1,
84 double r2, double i2) {
85 double tempr;
86
87 tempr = r1 * r2 - i1 * i2;
88 *i0 = r1 * i2 + i1 * r2;
89 *r0 = tempr;
90}
91
92/**
93 * @brief Divides two complex numbers.
94 *
95 * @deprecated These routines are obsolete, really, but some code uses them.
96 *
97 * @param r0 Pointer to store the real part of the result.
98 * @param i0 Pointer to store the imaginary part of the result.
99 * @param r1 Real part of the numerator complex number.
100 * @param i1 Imaginary part of the numerator complex number.
101 * @param r2 Real part of the denominator complex number.
102 * @param i2 Imaginary part of the denominator complex number.
103 * @param threshold The threshold to prevent division by very small numbers.
104 */
106 double *r0, double *i0, /* result */
107 double r1, double i1,
108 double r2, double i2,
109 double threshold) {
110 double tempr, denom;
111
112 if ((denom = sqr(r2) + sqr(i2)) < threshold)
113 denom = threshold;
114 i2 = -i2;
115 tempr = (r1 * r2 - i1 * i2) / denom;
116 *i0 = (r1 * i2 + i1 * r2) / denom;
117 *r0 = tempr;
118}
std::complex< double > complexErf(std::complex< double > z, long *flag)
Computes the complex error function of a given complex number.
Definition complex.cc:26
std::complex< double > cipowr(std::complex< double > a, int n)
Raises a complex number to an integer power.
Definition complex.cc:56
void complex_divide(double *r0, double *i0, double r1, double i1, double r2, double i2, double threshold)
Divides two complex numbers.
Definition complex.cc:105
void complex_multiply(double *r0, double *i0, double r1, double i1, double r2, double i2)
Multiplies two complex numbers.
Definition complex.cc:81
std::complex< double > cexpi(double p)
Computes the complex exponential of an imaginary number.
Definition complex.cc:43
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