BRL-CAD
complex.h
Go to the documentation of this file.
1 /* C O M P L E X . H
2  * BRL-CAD
3  *
4  * Copyright (c) 2004-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 
21 /*----------------------------------------------------------------------*/
22 /* @file complex.h */
23 /** @addtogroup complex */
24 /** @{ */
25 
26 /**
27  * @brief Complex numbers
28  */
29 
30 #ifndef BN_COMPLEX_H
31 #define BN_COMPLEX_H
32 
33 #include "common.h"
34 #include "bn/defines.h"
35 
37 
38 /* "complex number" data type: */
39 typedef struct bn_complex {
40  double re; /**< @brief real part */
41  double im; /**< @brief imaginary part */
42 } bn_complex_t;
43 
44 /* functions that are efficiently done as macros: */
45 
46 #define bn_cx_copy(ap, bp) {*(ap) = *(bp);}
47 #define bn_cx_neg(cp) { (cp)->re = -((cp)->re);(cp)->im = -((cp)->im);}
48 #define bn_cx_real(cp) (cp)->re
49 #define bn_cx_imag(cp) (cp)->im
50 
51 #define bn_cx_add(ap, bp) { (ap)->re += (bp)->re; (ap)->im += (bp)->im;}
52 #define bn_cx_ampl(cp) hypot((cp)->re, (cp)->im)
53 #define bn_cx_amplsq(cp) ((cp)->re * (cp)->re + (cp)->im * (cp)->im)
54 #define bn_cx_conj(cp) { (cp)->im = -(cp)->im; }
55 #define bn_cx_cons(cp, r, i) { (cp)->re = r; (cp)->im = i; }
56 #define bn_cx_phas(cp) atan2((cp)->im, (cp)->re)
57 #define bn_cx_scal(cp, s) { (cp)->re *= (s); (cp)->im *= (s); }
58 #define bn_cx_sub(ap, bp) { (ap)->re -= (bp)->re; (ap)->im -= (bp)->im;}
59 
60 #define bn_cx_mul(ap, bp) \
61  { register fastf_t a__re, b__re; \
62  (ap)->re = ((a__re=(ap)->re)*(b__re=(bp)->re)) - (ap)->im*(bp)->im; \
63  (ap)->im = a__re*(bp)->im + (ap)->im*b__re; }
64 
65 /* Output variable "ap" is different from input variables "bp" or "cp" */
66 #define bn_cx_mul2(ap, bp, cp) { \
67  (ap)->re = (cp)->re * (bp)->re - (cp)->im * (bp)->im; \
68  (ap)->im = (cp)->re * (bp)->im + (cp)->im * (bp)->re; }
69 
70 /**
71  *@brief
72  * Divide one complex by another
73  *
74  * bn_cx_div(&a, &b). divides a by b. Zero divisor fails. a and b
75  * may coincide. Result stored in a.
76  */
77 BN_EXPORT extern void bn_cx_div(bn_complex_t *ap,
78  const bn_complex_t *bp);
79 
80 /**
81  *@brief
82  * Compute square root of complex number
83  *
84  * bn_cx_sqrt(&out, &c) replaces out by sqrt(c)
85  *
86  * Note: This is a double-valued function; the result of bn_cx_sqrt()
87  * always has nonnegative imaginary part.
88  */
89 BN_EXPORT extern void bn_cx_sqrt(bn_complex_t *op,
90  const bn_complex_t *ip);
91 
93 
94 #endif /* BN_COMPLEX_H */
95 /** @} */
96 /*
97  * Local Variables:
98  * mode: C
99  * tab-width: 8
100  * indent-tabs-mode: t
101  * c-file-style: "stroustrup"
102  * End:
103  * ex: shiftwidth=4 tabstop=8
104  */
struct bn_complex bn_complex_t
Complex numbers.
Header file for the BRL-CAD common definitions.
double im
imaginary part
Definition: complex.h:41
double re
real part
Definition: complex.h:40
#define __BEGIN_DECLS
Definition: common.h:73
unsigned char * bp
Definition: rot.c:56
void bn_cx_div(bn_complex_t *ap, const bn_complex_t *bp)
Divide one complex by another.
#define __END_DECLS
Definition: common.h:74
Complex numbers.
Definition: complex.h:39
void bn_cx_sqrt(bn_complex_t *op, const bn_complex_t *ip)
Compute square root of complex number.
Definition: complex.c:66