IT++ Logo
fix_operators.cpp
Go to the documentation of this file.
1
31
32
33namespace itpp
34{
35
37// Operators for Fix and Fixed //
39
40Fix operator+(const Fix &x, const Fix &y)
41{
42 return Fix(x.get_re() + y.get_re(),
43 assert_shifts(x, y),
44 0, 0);
45}
46
47Fix operator-(const Fix &x, const Fix &y)
48{
49 return Fix(x.get_re() - y.get_re(),
50 assert_shifts(x, y),
51 0, 0);
52}
53
54Fix operator*(const Fix &x, const Fix &y)
55{
56 return Fix(x.get_re() * y.get_re(),
57 x.get_shift() + y.get_shift(),
58 0, 0);
59}
60
61Fix operator/(const Fix &x, const Fix &y)
62{
63 return Fix(x.get_re() / y.get_re(),
64 x.get_shift() - y.get_shift(),
65 0, 0);
66}
67
68Fix operator+(const Fix &x, const int y)
69{
70 return Fix(x.get_re() + y,
71 assert_shifts(x, y),
72 0, 0);
73}
74
75Fix operator-(const Fix &x, const int y)
76{
77 return Fix(x.get_re() - y,
78 assert_shifts(x, y),
79 0, 0);
80}
81
82Fix operator*(const Fix &x, const int y)
83{
84 return Fix(x.get_re() * y,
85 x.get_shift(),
86 0, 0);
87}
88
89Fix operator/(const Fix &x, const int y)
90{
91 return Fix(x.get_re() / y,
92 x.get_shift(),
93 0, 0);
94}
95
96Fix operator+(const int x, const Fix &y)
97{
98 return Fix(x + y.get_re(),
99 assert_shifts(y, x),
100 0, 0);
101}
102
103Fix operator-(const int x, const Fix &y)
104{
105 return Fix(x - y.get_re(),
106 assert_shifts(y, x),
107 0, 0);
108}
109
110Fix operator*(const int x, const Fix &y)
111{
112 return Fix(x * y.get_re(),
113 y.get_shift(),
114 0, 0);
115}
116
117Fix operator/(const int x, const Fix &y)
118{
119 return Fix(x / y.get_re(),
120 -y.get_shift(),
121 0, 0);
122}
123
124
125fixvec operator+(const fixvec &a, const ivec &b)
126{
127 it_assert_debug(a.size() == b.size(), "operator+(): sizes do not match");
128 fixvec temp(a);
129 for (int i = 0; i < a.size(); i++) {
130 temp(i) += b(i);
131 }
132 return temp;
133}
134
135Fix operator*(const fixvec &a, const ivec &b)
136{
137 it_assert_debug(a.size() == b.size(), "operator+(): sizes do not match");
138 Fix temp(0);
139 for (int i = 0; i < a.size(); i++) {
140 temp += a(i) * b(i);
141 }
142 return temp;
143}
144
145fixmat operator+(const fixmat &a, const imat &b)
146{
147 it_assert_debug(a.cols() == b.cols() && a.rows() == b.rows(), "operator+(): sizes do not match");
148 fixmat temp(a);
149
150 for (int i = 0; i < a.rows(); i++) {
151 for (int j = 0; j < a.cols(); j++) {
152 temp(i, j) += b(i, j);
153 }
154 }
155 return temp;
156}
157
158fixmat operator*(const fixmat &a, const imat &b)
159{
160 it_assert_debug(a.cols() == b.rows(), "operator*: wrong sizes");
161 fixmat r(a.rows(), b.cols());
162
163 Fix tmp;
164 int i, j, k;
165 Fix *tr = r._data();
166 const Fix *t1;
167 const int *t2 = b._data();
168
169 for (i = 0; i < r.cols(); i++) {
170 for (j = 0; j < r.rows(); j++) {
171 tmp = Fix(0);
172 t1 = a._data() + j;
173 for (k = a.cols(); k > 0; k--) {
174 tmp += *(t1) * *(t2++);
175 t1 += a.rows();
176 }
177 *(tr++) = tmp;
178 t2 -= b.rows();
179 }
180 t2 += b.rows();
181 }
182 return r;
183}
184
186// Operators for CFix and CFixed //
188
189CFix operator+(const CFix &x, const CFix &y)
190{
191 return CFix(x.get_re() + y.get_re(),
192 x.get_im() + y.get_im(),
193 assert_shifts(x, y),
194 0, 0);
195}
196
197CFix operator-(const CFix &x, const CFix &y)
198{
199 return CFix(x.get_re() - y.get_re(),
200 x.get_im() - y.get_im(),
201 assert_shifts(x, y),
202 0, 0);
203}
204
205CFix operator*(const CFix &x, const CFix &y)
206{
207 return CFix(x.get_re()*y.get_re() - x.get_im()*y.get_im(),
208 x.get_re()*y.get_im() + x.get_im()*y.get_re(),
209 x.get_shift() + y.get_shift(),
210 0, 0);
211}
212
213CFix operator/(const CFix &x, const CFix &y)
214{
215 fixrep denominator = y.get_re() * y.get_re() + y.get_im() * y.get_im();
216 return CFix((x.get_re()*y.get_re() + x.get_im()*y.get_im()) / denominator,
217 (x.get_im()*y.get_re() - x.get_re()*y.get_im()) / denominator,
218 x.get_shift() - y.get_shift(),
219 0, 0);
220}
221
222CFix operator+(const CFix &x, const Fix &y)
223{
224 return CFix(x.get_re() + y.get_re(),
225 x.get_im(),
226 assert_shifts(x, y),
227 0, 0);
228}
229
230CFix operator-(const CFix &x, const Fix &y)
231{
232 return CFix(x.get_re() - y.get_re(),
233 x.get_im(),
234 assert_shifts(x, y),
235 0, 0);
236}
237
238CFix operator*(const CFix &x, const Fix &y)
239{
240 return CFix(x.get_re() * y.get_re(),
241 x.get_im() * y.get_re(),
242 x.get_shift() + y.get_shift(),
243 0, 0);
244}
245
246CFix operator/(const CFix &x, const Fix &y)
247{
248 return CFix(x.get_re() / y.get_re(),
249 x.get_im() / y.get_re(),
250 x.get_shift() - y.get_shift(),
251 0, 0);
252}
253
254CFix operator+(const Fix &x, const CFix &y)
255{
256 return CFix(x.get_re() + y.get_re(),
257 y.get_im(),
258 assert_shifts(y, x),
259 0, 0);
260}
261
262CFix operator-(const Fix &x, const CFix &y)
263{
264 return CFix(x.get_re() - y.get_re(),
265 -y.get_im(),
266 assert_shifts(y, x),
267 0, 0);
268}
269
270CFix operator*(const Fix &x, const CFix &y)
271{
272 return CFix(x.get_re() * y.get_re(),
273 x.get_re() * y.get_im(),
274 x.get_shift() + y.get_shift(),
275 0, 0);
276}
277
278CFix operator/(const Fix &x, const CFix &y)
279{
280 fixrep denominator = y.get_re() * y.get_re() + y.get_im() * y.get_im();
281 return CFix(x.get_re() * y.get_re() / denominator,
282 -x.get_re() * y.get_im() / denominator,
283 x.get_shift() - y.get_shift(),
284 0, 0);
285}
286
287CFix operator+(const CFix &x, const int y)
288{
289 return CFix(x.get_re() + y,
290 x.get_im(),
291 assert_shifts(x, y),
292 0, 0);
293}
294
295CFix operator-(const CFix &x, const int y)
296{
297 return CFix(x.get_re() - y,
298 x.get_im(),
299 assert_shifts(x, y),
300 0, 0);
301}
302
303CFix operator*(const CFix &x, const int y)
304{
305 return CFix(x.get_re() * y,
306 x.get_im() * y,
307 x.get_shift(),
308 0, 0);
309}
310
311CFix operator/(const CFix &x, const int y)
312{
313 return CFix(x.get_re() / y,
314 x.get_im() / y,
315 x.get_shift(),
316 0, 0);
317}
318
319CFix operator+(const int x, const CFix &y)
320{
321 return CFix(x + y.get_re(),
322 y.get_im(),
323 assert_shifts(y, x),
324 0, 0);
325}
326
327CFix operator-(const int x, const CFix &y)
328{
329 return CFix(x - y.get_re(),
330 -y.get_im(),
331 assert_shifts(y, x),
332 0, 0);
333}
334
335CFix operator*(const int x, const CFix &y)
336{
337 return CFix(x * y.get_re(),
338 x * y.get_im(),
339 y.get_shift(),
340 0, 0);
341}
342
343CFix operator/(const int x, const CFix &y)
344{
345 fixrep denominator = y.get_re() * y.get_re() + y.get_im() * y.get_im();
346 return CFix(x * y.get_re() / denominator,
347 -x * y.get_im() / denominator,
348 -y.get_shift(),
349 0, 0);
350}
351
352cfixvec operator+(const cfixvec &a, const fixvec &b)
353{
354 it_assert_debug(a.size() == b.size(), "operator+(): sizes do not match");
355 cfixvec temp(a);
356 for (int i = 0; i < a.size(); i++) {
357 temp(i) += b(i);
358 }
359 return temp;
360}
361
362CFix operator*(const cfixvec &a, const fixvec &b)
363{
364 it_assert_debug(a.size() == b.size(), "operator+(): sizes do not match");
365 CFix temp(0);
366 for (int i = 0; i < a.size(); i++) {
367 temp += a(i) * b(i);
368 }
369 return temp;
370}
371
372cfixmat operator+(const cfixmat &a, const fixmat &b)
373{
374 it_assert_debug(a.cols() == b.cols() && a.rows() == b.rows(), "operator+(): sizes do not match");
375 cfixmat temp(a);
376
377 for (int i = 0; i < a.rows(); i++) {
378 for (int j = 0; j < a.cols(); j++) {
379 temp(i, j) += b(i, j);
380 }
381 }
382 return temp;
383}
384
385cfixmat operator*(const cfixmat &a, const fixmat &b)
386{
387 it_assert_debug(a.cols() == b.rows(), "operator*: wrong sizes");
388 cfixmat r(a.rows(), b.cols());
389
390 CFix tmp;
391 int i, j, k;
392 CFix *tr = r._data();
393 const CFix *t1;
394 const Fix *t2 = b._data();
395
396 for (i = 0; i < r.cols(); i++) {
397 for (j = 0; j < r.rows(); j++) {
398 tmp = CFix(0);
399 t1 = a._data() + j;
400 for (k = a.cols(); k > 0; k--) {
401 tmp += *(t1) * *(t2++);
402 t1 += a.rows();
403 }
404 *(tr++) = tmp;
405 t2 -= b.rows();
406 }
407 t2 += b.rows();
408 }
409 return r;
410}
411
412cfixvec operator+(const cfixvec &a, const ivec &b)
413{
414 it_assert_debug(a.size() == b.size(), "operator+(): sizes do not match");
415 cfixvec temp(a);
416 for (int i = 0; i < a.size(); i++) {
417 temp(i) += b(i);
418 }
419 return temp;
420}
421
422CFix operator*(const cfixvec &a, const ivec &b)
423{
424 it_assert_debug(a.size() == b.size(), "operator+(): sizes do not match");
425 CFix temp(0);
426 for (int i = 0; i < a.size(); i++) {
427 temp += a(i) * b(i);
428 }
429 return temp;
430}
431
432cfixmat operator+(const cfixmat &a, const imat &b)
433{
434 it_assert_debug(a.cols() == b.cols() && a.rows() == b.rows(), "operator+(): sizes do not match");
435 cfixmat temp(a);
436
437 for (int i = 0; i < a.rows(); i++) {
438 for (int j = 0; j < a.cols(); j++) {
439 temp(i, j) += b(i, j);
440 }
441 }
442 return temp;
443}
444
445cfixmat operator*(const cfixmat &a, const imat &b)
446{
447 it_assert_debug(a.cols() == b.rows(), "operator*: wrong sizes");
448 cfixmat r(a.rows(), b.cols());
449
450 CFix tmp;
451 int i, j, k;
452 CFix *tr = r._data();
453 const CFix *t1;
454 const int *t2 = b._data();
455
456 for (i = 0; i < r.cols(); i++) {
457 for (j = 0; j < r.rows(); j++) {
458 tmp = CFix(0);
459 t1 = a._data() + j;
460 for (k = a.cols(); k > 0; k--) {
461 tmp += *(t1) * *(t2++);
462 t1 += a.rows();
463 }
464 *(tr++) = tmp;
465 t2 -= b.rows();
466 }
467 t2 += b.rows();
468 }
469 return r;
470}
471
472} // namespace itpp
Complex fixed-point data type.
Definition cfix.h:52
fixrep get_im() const
Get data representation for imaginary part (mainly for internal use since it reveals the representati...
Definition cfix.h:140
fixrep get_re() const
Get data representation for real part (mainly for internal use since it reveals the representation ty...
Definition cfix.h:138
int get_shift() const
Get shift.
Definition fix_base.h:1001
Fixed-point data type.
Definition fix.h:52
fixrep get_re() const
Get data representation (mainly for internal use since it reveals the representation type)
Definition fix.h:116
Matrix Class (Templated)
Definition fix.h:41
int rows() const
The number of rows.
Definition mat.h:237
int cols() const
The number of columns.
Definition mat.h:235
Num_T * _data()
Access of the internal data structure (not recommended to use)
Definition mat.h:440
Vector Class (Templated)
Definition fix.h:40
int size() const
The size of the vector.
Definition vec.h:271
Definitions of a set of operators for Fix, Fixed, CFix and CFixed classes.
#define it_assert_debug(t, s)
Abort if t is not true and NDEBUG is not defined.
Definition itassert.h:107
int assert_shifts(const CFix &x, const CFix &y)
Check that x.shift==y.shift OR x==0 OR y==0 and return the shift (for the non-zero argument)
Definition cfix.cpp:249
int64_t fixrep
Representation for fixed-point data types.
Definition fix_base.h:884
itpp namespace
Definition itmex.h:37
Mat< Num_T > operator-(const Mat< Num_T > &m1, const Mat< Num_T > &m2)
Subtraction of two matrices.
Definition mat.h:1382
GF2mat operator*(const GF2mat &X, const GF2mat &Y)
GF(2) matrix multiplication.
Definition gf2mat.cpp:847
Mat< Num_T > operator/(const Mat< Num_T > &m, Num_T t)
Element-wise division by a scalar.
Definition mat.h:1670
GF2mat operator+(const GF2mat &X, const GF2mat &Y)
GF(2) matrix addition.
Definition gf2mat.cpp:948
SourceForge Logo

Generated on Tue Dec 10 2024 04:49:37 for IT++ by Doxygen 1.12.0