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-2023 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/** @addtogroup bn_complex
23 * @brief
24 * Complex numbers
25 */
26/** @{ */
27/** @file complex.h */
28
29#ifndef BN_COMPLEX_H
30#define BN_COMPLEX_H
31
32#include "common.h"
33#include "bn/defines.h"
34
35__BEGIN_DECLS
36
37/** "complex number" data type */
38typedef struct bn_complex {
39 double re; /**< @brief real part */
40 double im; /**< @brief imaginary part */
42
43/* functions that are efficiently done as macros: */
44
45#define bn_cx_copy(ap, bp) {*(ap) = *(bp);}
46#define bn_cx_neg(cp) { (cp)->re = -((cp)->re);(cp)->im = -((cp)->im);}
47#define bn_cx_real(cp) (cp)->re
48#define bn_cx_imag(cp) (cp)->im
49
50#define bn_cx_add(ap, bp) { (ap)->re += (bp)->re; (ap)->im += (bp)->im;}
51#define bn_cx_ampl(cp) hypot((cp)->re, (cp)->im)
52#define bn_cx_amplsq(cp) ((cp)->re * (cp)->re + (cp)->im * (cp)->im)
53#define bn_cx_conj(cp) { (cp)->im = -(cp)->im; }
54#define bn_cx_cons(cp, r, i) { (cp)->re = r; (cp)->im = i; }
55#define bn_cx_phas(cp) atan2((cp)->im, (cp)->re)
56#define bn_cx_scal(cp, s) { (cp)->re *= (s); (cp)->im *= (s); }
57#define bn_cx_sub(ap, bp) { (ap)->re -= (bp)->re; (ap)->im -= (bp)->im;}
58
59#define bn_cx_mul(ap, bp) \
60 { register fastf_t a__re, b__re; \
61 (ap)->re = ((a__re=(ap)->re)*(b__re=(bp)->re)) - (ap)->im*(bp)->im; \
62 (ap)->im = a__re*(bp)->im + (ap)->im*b__re; }
63
64/* Output variable "ap" is different from input variables "bp" or "cp" */
65#define bn_cx_mul2(ap, bp, cp) { \
66 (ap)->re = (cp)->re * (bp)->re - (cp)->im * (bp)->im; \
67 (ap)->im = (cp)->re * (bp)->im + (cp)->im * (bp)->re; }
68
69/**
70 *@brief
71 * Divide one complex by another
72 *
73 * bn_cx_div(&a, &b). divides a by b. Zero divisor fails. a and b
74 * may coincide. Result stored in a.
75 */
76BN_EXPORT extern void bn_cx_div(bn_complex_t *ap,
77 const bn_complex_t *bp);
78
79/**
80 *@brief
81 * Compute square root of complex number
82 *
83 * bn_cx_sqrt(&out, &c) replaces out by sqrt(c)
84 *
85 * Note: This is a double-valued function; the result of bn_cx_sqrt()
86 * always has nonnegative imaginary part.
87 */
88BN_EXPORT extern void bn_cx_sqrt(bn_complex_t *op,
89 const bn_complex_t *ip);
90
91__END_DECLS
92
93#endif /* BN_COMPLEX_H */
94/** @} */
95/*
96 * Local Variables:
97 * mode: C
98 * tab-width: 8
99 * indent-tabs-mode: t
100 * c-file-style: "stroustrup"
101 * End:
102 * ex: shiftwidth=4 tabstop=8
103 */
Header file for the BRL-CAD common definitions.
void bn_cx_sqrt(bn_complex_t *op, const bn_complex_t *ip)
Compute square root of complex number.
void bn_cx_div(bn_complex_t *ap, const bn_complex_t *bp)
Divide one complex by another.
struct bn_complex bn_complex_t
double re
real part
Definition: complex.h:39
double im
imaginary part
Definition: complex.h:40