-
Notifications
You must be signed in to change notification settings - Fork 62
/
complexfloat32.lua
222 lines (197 loc) · 5.46 KB
/
complexfloat32.lua
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
---
-- ComplexFloat32 data type, a C structure defined as:
--
-- ``` c
-- typedef struct complex_float32 {
-- float real;
-- float imag;
-- } complex_float32_t;
-- ```
--
-- @datatype ComplexFloat32
-- @tparam[opt=0.0] float real Initial real part
-- @tparam[opt=0.0] float imag Initial imaginary part
local ffi = require('ffi')
local CStructType = require('radio.types.cstruct')
ffi.cdef[[
typedef struct complex_float32 {
float real;
float imag;
} complex_float32_t;
]]
ffi.cdef[[
float atan2f(float y, float x);
float sqrtf(float x);
]]
local mt = {}
---
-- Construct a zero-initialized ComplexFloat32 vector.
--
-- @function ComplexFloat32.vector
-- @tparam int num Number of elements in the vector
-- @treturn Vector ComplexFloat32 vector
--
-- @usage
-- local vec = radio.types.ComplexFloat32.vector(100)
---
-- Construct a ComplexFloat32 vector initialized from an array.
--
-- @function ComplexFloat32.vector_from_array
-- @tparam array arr Array with element initializers
-- @treturn Vector ComplexFloat32 vector
--
-- @usage
-- local vec = radio.types.ComplexFloat32.vector_from_array({{1.0, 2.0}, {2.0, 3.0}, {3.0, 4.0}})
---
-- Add two ComplexFloat32s.
--
-- @function ComplexFloat32:__add
-- @tparam ComplexFloat32 other Operand
-- @treturn ComplexFloat32 Result
function mt:__add(other)
return self.new(self.real + other.real, self.imag + other.imag)
end
---
-- Subtract two ComplexFloat32s.
--
-- @function ComplexFloat32:__sub
-- @tparam ComplexFloat32 other Operand
-- @treturn ComplexFloat32 Result
function mt:__sub(other)
return self.new(self.real - other.real, self.imag - other.imag)
end
---
-- Multiply two ComplexFloat32s.
--
-- @function ComplexFloat32:__mul
-- @tparam ComplexFloat32 other Operand
-- @treturn ComplexFloat32 Result
function mt:__mul(other)
return self.new(self.real * other.real - self.imag * other.imag, self.real * other.imag + self.imag * other.real)
end
---
-- Divide two ComplexFloat32s.
--
-- @function ComplexFloat32:__div
-- @tparam ComplexFloat32 other Operand
-- @treturn ComplexFloat32 Result
function mt:__div(other)
local real = (self.real * other.real + self.imag * other.imag) / (other.real * other.real + other.imag * other.imag)
local imag = (self.imag * other.real - self.real * other.imag) / (other.real * other.real + other.imag * other.imag)
return self.new(real, imag)
end
---
-- Compare two ComplexFloat32s for equality.
--
-- @function ComplexFloat32:__eq
-- @tparam ComplexFloat32 other Other ComplexFloat32
-- @treturn bool Result
function mt:__eq(other)
return self.real == other.real and self.imag == other.imag
end
---
-- Compare two ComplexFloat32s for less than.
--
-- @function ComplexFloat32:__lt
-- @tparam ComplexFloat32 other Other ComplexFloat32
-- @treturn bool Result
function mt:__lt(other)
return (self.real < other.real) and (self.imag < other.imag)
end
---
-- Compare two ComplexFloat32s for less than or equal.
--
-- @function ComplexFloat32:__le
-- @tparam ComplexFloat32 other Other ComplexFloat32
-- @treturn bool Result
function mt:__le(other)
return (self.real <= other.real) and (self.imag <= other.imag)
end
---
-- Multiply a ComplexFloat32 by a scalar.
--
-- @function ComplexFloat32:scalar_mul
-- @tparam number other Scalar
-- @treturn ComplexFloat32 Result
function mt:scalar_mul(other)
return self.new(self.real * other, self.imag * other)
end
---
-- Divide a ComplexFloat32 by a scalar.
--
-- @function ComplexFloat32:scalar_div
-- @tparam number other Scalar
-- @treturn ComplexFloat32 Result
function mt:scalar_div(other)
return self.new(self.real / other, self.imag / other)
end
---
-- Compute the complex argument, in interval $$ (-\pi, \pi] $$.
--
-- $$ \angle z = \text{atan2}(\text{Im}(z), \text{Re}(z)) $$
--
-- @function ComplexFloat32:arg
-- @treturn number Result
function mt:arg()
return ffi.C.atan2f(self.imag, self.real)
end
---
-- Compute the complex magnitude.
--
-- $$ |z| = \sqrt{\text{Re}(z)^2 + \text{Im}(z)^2} $$
--
-- @function ComplexFloat32:abs
-- @treturn number Result
function mt:abs()
return ffi.C.sqrtf(self.real*self.real + self.imag*self.imag)
end
---
-- Compute the complex magnitude squared.
--
-- $$ |z|^2 = \text{Re}(z)^2 + \text{Im}(z)^2 $$
--
-- @function ComplexFloat32:abs_squared
-- @treturn number Result
function mt:abs_squared()
return self.real*self.real + self.imag*self.imag
end
---
-- Get the complex conjugate.
--
-- @function ComplexFloat32:conj
-- @treturn ComplexFloat32 Result
function mt:conj()
return self.new(self.real, -self.imag)
end
---
-- Compare two ComplexFloat32s for approximate equality within the specified epsilon.
--
-- @function ComplexFloat32.approx_equal
-- @tparam ComplexFloat32 x First ComplexFloat32
-- @tparam ComplexFloat32 y Second ComplexFloat32
-- @tparam number epsilon Epsilon
-- @treturn bool Result
function mt.approx_equal(x, y, epsilon)
return (x - y):abs() < epsilon
end
---
-- Get a string representation.
--
-- @function ComplexFloat32:__tostring
-- @treturn string String representation
-- @usage
-- local x = radio.types.ComplexFloat32()
-- print(x)) --> ComplexFloat32<real=0, imag=0>
function mt:__tostring()
return "ComplexFloat32<real=" .. self.real .. ", imag=" .. self.imag .. ">"
end
---
-- Type name of ComplexFloat32.
--
-- @property ComplexFloat32.type_name
-- @treturn string Type name
-- @usage
-- print(radio.types.ComplexFloat32.type_name) --> ComplexFloat32
mt.type_name = "ComplexFloat32"
local ComplexFloat32 = CStructType.factory("complex_float32_t", mt)
return ComplexFloat32