File: complex.h

package info (click to toggle)
phast 1.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 12,412 kB
  • sloc: ansic: 54,180; makefile: 354; sh: 337; perl: 321
file content (151 lines) | stat: -rw-r--r-- 3,696 bytes parent folder | download | duplicates (2)
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
/***************************************************************************
 * PHAST: PHylogenetic Analysis with Space/Time models
 * Copyright (c) 2002-2005 University of California, 2006-2010 Cornell 
 * University.  All rights reserved.
 *
 * This source code is distributed under a BSD-style license.  See the
 * file LICENSE.txt for details.
 ***************************************************************************/

/** @file complex.h
    Functions and structures to hold, manipulate, and test complex numbers.
    All functions are inline; there is no complex.c file

    @ingroup base
*/

#ifndef COMPLEX_H
#define COMPLEX_H

#include <math.h>
#include <external_libs.h>

/** Structure representing complex number */
typedef struct {
  double x;			/**< real component */
  double y;			/**< imaginary component */
} Complex;

/** \name Initialize complex number function 
 \{ */

/** Create a complex number from Real and Imaginary components
    @param x Real component
    @param y Imaginary component
*/

static PHAST_INLINE
Complex z_set(double x, double y) {
  Complex z;
  z.x = x;
  z.y = y;
  return z;
}

/** \name Basic arithmetic for complex numbers functions
 \{ */

/** Add two complex numbers and return the result.
    @param z1 First complex number to add
    @param z2 Second complex number to add
    @result = z1 + z2
 */
static PHAST_INLINE
Complex z_add(Complex z1, Complex z2) {
  Complex sum;
  sum.x = z1.x + z2.x;
  sum.y = z1.y + z2.y;
  return sum;
}

/** Subtract one complex number from another and return the result.
   @param z1 Complex number to subtract from
   @param z2 Complex number to subtract
   @result  = z1 - z2
*/
static PHAST_INLINE
Complex z_sub(Complex z1, Complex z2) {
  Complex diff;
  diff.x = z1.x - z2.x;
  diff.y = z1.y - z2.y;
  return diff;
}

/** Multiply two complex numbers and return the result.
  @param z1 Complex number to multiply
  @param z2 Complex number to multiply by
  @result = z1 * z2
 */
static PHAST_INLINE
Complex z_mul(Complex z1, Complex z2) {
  Complex prod;
  prod.x = z1.x * z2.x - z1.y * z2.y;
  prod.y = z1.x * z2.y + z1.y * z2.x;
  return prod;
}

/** Divide one complex number by another and return the result.
  @param z1 Complex number to divide
  @param z2 Complex number to divide by
  @result = z1 / z2
*/
static PHAST_INLINE
Complex z_div(Complex z1, Complex z2) {
  Complex quot;
  double denom = z2.x * z2.x + z2.y * z2.y;
  quot.x = (z1.x * z2.x + z1.y * z2.y) / denom;
  quot.y = (z1.y * z2.x - z1.x * z2.y) / denom;
  return quot;
}

/** Multiply a complex number by a real number.
  @param z1 Complex number to multiply
  @param z2 Real number to multiply by
  @result = z1 * z2
 */
static PHAST_INLINE
Complex z_mul_real(Complex z, double x) {
  Complex prod;
  prod.x = z.x * x;
  prod.y = z.y * x;
  return prod;
}

/** Compute exponential function of complex number.
  @param z Power to raise to
  @return Exponential value of z
*/
static PHAST_INLINE
Complex z_exp(Complex z) {
  Complex retval;
  double exp_zx = exp(z.x);
  retval.x = exp_zx * cos(z.y);
  retval.y = exp_zx * sin(z.y);
  return retval;
}

/** Get absolute value of complex number.
  @param z Positive or negative complex number
  @result positive version of z
*/
static PHAST_INLINE
double z_abs(Complex z) {
  return sqrt(z.x * z.x + z.y * z.y);
}

/** \name Test equality complex number function
 \{ */


/** Check if two complex numbers are equal.
  @param z1 First complex number to compare
  @param z2 Second complex number to compare
  @result If (Z1 == Z2) { return 1; } else { return 0 }
*/
static PHAST_INLINE
int z_eq(Complex z1, Complex z2) {
  return(z1.x == z2.x && z1.y == z2.y);
}
/** \} */

#endif