IT++ Logo
operators.cpp
Go to the documentation of this file.
1
30#include <itpp/base/operators.h>
31
32
33namespace itpp
34{
35
36//----------- Scalar and a ivec -----------------
37
38vec operator+(const double &s, const ivec &v)
39{
40 it_assert_debug(v.size() > 0, "operator+(): Vector of zero length");
41
42 vec temp(v.size());
43 for (int i = 0;i < v.size();i++) {
44 temp(i) = s + double(v(i));
45 }
46 return temp;
47}
48
49vec operator-(const double &s, const ivec &v)
50{
51 it_assert_debug(v.size() > 0, "operator-(): Vector of zero length");
52
53 vec temp(v.size());
54 for (int i = 0;i < v.size();i++) {
55 temp(i) = s - double(v(i));
56 }
57 return temp;
58}
59
60vec operator*(const double &s, const ivec &v)
61{
62 it_assert_debug(v.size() > 0, "operator*(): Vector of zero length");
63
64 vec temp(v.size());
65 for (int i = 0;i < v.size();i++) {
66 temp(i) = s * double(v(i));
67 }
68 return temp;
69}
70
71vec operator/(const double &s, const ivec &v)
72{
73 it_assert_debug(v.size() > 0, "operator/(): Vector of zero length");
74
75 vec temp(v.size());
76 for (int i = 0;i < v.size();i++) {
77 temp(i) = s / double(v(i));
78 }
79 return temp;
80}
81
82vec operator/(const ivec &v, const double &s)
83{
84 it_assert_debug(v.size() > 0, "operator/(): Vector of zero length");
85
86 vec temp(v.size());
87 for (int i = 0;i < v.size();i++) {
88 temp(i) = double(v(i)) / s;
89 }
90 return temp;
91}
92
93cvec operator+(const std::complex<double> &s, const ivec &v)
94{
95 it_assert_debug(v.size() > 0, "operator+(): Vector of zero length");
96
97 cvec temp(v.size());
98 for (int i = 0;i < v.size();i++) {
99 temp(i) = s + std::complex<double>(v(i));
100 }
101 return temp;
102}
103
104cvec operator-(const std::complex<double> &s, const ivec &v)
105{
106 it_assert_debug(v.size() > 0, "operator-(): Vector of zero length");
107
108 cvec temp(v.size());
109 for (int i = 0;i < v.size();i++) {
110 temp(i) = s - std::complex<double>(v(i));
111 }
112 return temp;
113}
114
115cvec operator*(const std::complex<double> &s, const ivec &v)
116{
117 it_assert_debug(v.size() > 0, "operator*(): Vector of zero length");
118
119 cvec temp(v.size());
120 for (int i = 0;i < v.size();i++) {
121 temp(i) = s * std::complex<double>(v(i));
122 }
123 return temp;
124}
125
126cvec operator/(const std::complex<double> &s, const ivec &v)
127{
128 it_assert_debug(v.size() > 0, "operator/(): Vector of zero length");
129
130 cvec temp(v.size());
131 for (int i = 0;i < v.size();i++) {
132 temp(i) = s / std::complex<double>(v(i));
133 }
134 return temp;
135}
136
137cvec operator/(const ivec &v, const std::complex<double> &s)
138{
139 it_assert_debug(v.size() > 0, "operator/(): Vector of zero length");
140
141 cvec temp(v.size());
142 for (int i = 0;i < v.size();i++) {
143 temp(i) = std::complex<double>(v(i)) / s;
144 }
145 return temp;
146}
147
148//----------- Scalar and a cvec -----------------
149
150cvec operator+(const double &s, const cvec &v)
151{
152 it_assert_debug(v.size() > 0, "operator+(): Vector of zero length");
153
154 cvec temp = v;
155 for (int i = 0;i < v.size();i++) {
156 temp(i) += s;
157 }
158 return temp;
159}
160
161cvec operator-(const double &s, const cvec &v)
162{
163 it_assert_debug(v.size() > 0, "operator-(): Vector of zero length");
164
165 cvec temp(v.size());
166 for (int i = 0;i < v.size();i++) {
167 temp(i) = std::complex<double>((double)s - v(i).real(), -v(i).imag());
168 }
169 return temp;
170}
172cvec operator*(const double &s, const cvec &v)
173{
174 it_assert_debug(v.size() > 0, "operator*(): Vector of zero length");
175
176 cvec temp = v;
177 for (int i = 0;i < v.size();i++) {
178 temp(i) *= (double)s;
179 }
180 return temp;
181}
182
183cvec operator/(const cvec &v, const double &s)
184{
185 it_assert_debug(v.size() > 0, "operator/(): Vector of zero length");
186
187 cvec temp = v;
188 for (int i = 0;i < v.size();i++) {
189 temp(i) /= (double)s;
190 }
191 return temp;
192}
193
194cvec operator/(const double &s, const cvec &v)
196 it_assert_debug(v.size() > 0, "operator/(): Vector of zero length");
197
198 cvec temp(v.length());
199 for (int i = 0;i < v.size();i++) {
200 temp(i) = s / v(i);
201 }
202 return temp;
203}
204
205//----------- Scalar and a cmat -----------------
206
207cmat operator+(const double &s, const cmat &m)
208{
209 it_assert_debug(m.rows() > 0 && m.cols() > 0, "operator+(): Matrix of zero length");
210
211 cmat temp = m;
212 for (int i = 0;i < m._datasize();i++) {
213 temp._data()[i] += s;
214 }
215 return temp;
216}
217
218cmat operator-(const double &s, const cmat &m)
219{
220 it_assert_debug(m.rows() > 0 && m.cols() > 0, "operator-(): Matrix of zero length");
221
222 cmat temp(m.rows(), m.cols());
223 for (int i = 0;i < m._datasize();i++) {
224 temp._data()[i] = std::complex<double>((double)s - m(i).real(), -m(i).imag());
225 }
226 return temp;
227}
228
229cmat operator*(const double &s, const cmat &m)
230{
231 it_assert_debug(m.rows() > 0 && m.cols() > 0, "operator*(): Matrix of zero length");
232
233 cmat temp = m;
234 for (int i = 0;i < m._datasize();i++) {
235 temp._data()[i] *= (double)s;
236 }
237 return temp;
238}
239
240cmat operator*(const std::complex<double> &s, const mat &m)
241{
242 it_assert_debug(m.rows() > 0 && m.cols() > 0, "operator*(): Matrix of zero length");
243
244 cmat temp(m.rows(), m.cols());
245
246 for (int i = 0;i < m._datasize();i++) {
247 temp._data()[i] = s * m._data()[i];
248 }
249 return temp;
250}
251
252cmat operator/(const cmat &m, const double &s)
253{
254 it_assert_debug(m.rows() > 0 && m.cols() > 0, "operator/(): Matrix of zero length");
255
256 cmat temp = m;
257 for (int i = 0;i < m._datasize();i++) {
258 temp._data()[i] /= (double)s;
259 }
260 return temp;
261}
262
263//---------------------- between matrix and scalar --------------------
265//----------- Multiplication of a scalar and a vec -----------------
266
267cvec operator*(const std::complex<double> &s, const vec &v)
268{
269 cvec temp(v.size());
270 for (int i = 0;i < v.size();i++) {
271 temp(i) = s * std::complex<double>(v(i), 0.0);
272 }
273 return temp;
274}
275
276cvec operator*(const vec &v, const std::complex<double> &s)
277{
278 cvec temp(v.size());
279 for (int i = 0;i < v.size();i++) {
280 temp(i) = s * std::complex<double>(v(i), 0.0);
281 }
282 return temp;
283}
284
285// ===============================================================================================
286
287// ---------------- Addition of vectors ---------------
288
289
290vec operator+(const bvec &a, const vec &b)
291{
292 it_assert_debug(a.size() == b.size(), "operator+(): sizes does not match");
293 vec temp(a.size());
294 for (int i = 0;i < a.size();i++) {temp(i) = (double)a(i) + b(i);}
295 return temp;
296}
297
298vec operator+(const svec &a, const vec &b)
299{
300 it_assert_debug(a.size() == b.size(), "operator+(): sizes does not match");
301 vec temp(a.size());
302 for (int i = 0;i < a.size();i++) {temp(i) = (double)a(i) + b(i);}
303 return temp;
304}
305
306vec operator+(const ivec &a, const vec &b)
307{
308 it_assert_debug(a.size() == b.size(), "operator+(): sizes does not match");
309 vec temp(a.size());
310 for (int i = 0;i < a.size();i++) {temp(i) = (double)a(i) + b(i);}
311 return temp;
313
314cvec operator+(const bvec &a, const cvec &b)
315{
316 it_assert_debug(a.size() == b.size(), "operator+(): sizes does not match");
317 cvec temp = b;
318 for (int i = 0;i < a.size();i++) {temp(i) += (double)a(i);}
319 return temp;
321
322cvec operator+(const svec &a, const cvec &b)
323{
324 it_assert_debug(a.size() == b.size(), "operator+(): sizes does not match");
325 cvec temp = b;
326 for (int i = 0;i < a.size();i++) {temp(i) += (double)a(i);}
327 return temp;
328}
329
330cvec operator+(const ivec &a, const cvec &b)
331{
332 it_assert_debug(a.size() == b.size(), "operator+(): sizes does not match");
333 cvec temp = b;
334 for (int i = 0;i < a.size();i++) {temp(i) += (double)a(i);}
335 return temp;
336}
337
338// ---------------- Multiplication of vectors ---------------
339
340double operator*(const bvec &a, const vec &b)
341{
342 it_assert_debug(a.size() == b.size(), "operator*(): sizes does not match");
343 double temp = 0;
344 for (int i = 0;i < a.size();i++) {temp += (double)a(i) * b(i);}
345 return temp;
346}
347
348double operator*(const svec &a, const vec &b)
349{
350 it_assert_debug(a.size() == b.size(), "operator*(): sizes does not match");
351 double temp = 0;
352 for (int i = 0;i < a.size();i++) {temp += (double)a(i) * b(i);}
353 return temp;
354}
355
356double operator*(const ivec &a, const vec &b)
357{
358 it_assert_debug(a.size() == b.size(), "operator*(): sizes does not match");
359 double temp = 0;
360 for (int i = 0;i < a.size();i++) {temp += (double)a(i) * b(i);}
361 return temp;
362}
363
364std::complex<double> operator*(const bvec &a, const cvec &b)
365{
366 it_assert_debug(a.size() == b.size(), "operator*(): sizes does not match");
367 std::complex<double> temp = 0;
368 for (int i = 0;i < a.size();i++) {temp += (double)a(i) * b(i);}
369 return temp;
370}
371
372std::complex<double> operator*(const svec &a, const cvec &b)
373{
374 it_assert_debug(a.size() == b.size(), "operator*(): sizes does not match");
375 std::complex<double> temp = 0;
376 for (int i = 0;i < a.size();i++) {temp += (double)a(i) * b(i);}
377 return temp;
378}
379
380std::complex<double> operator*(const ivec &a, const cvec &b)
381{
382 it_assert_debug(a.size() == b.size(), "operator*(): sizes does not match");
383 std::complex<double> temp = 0;
384 for (int i = 0;i < a.size();i++) {temp += (double)a(i) * b(i);}
385 return temp;
386}
387
388// ---------------- Addition of matricies ---------------
389
390mat operator+(const bmat &a, const mat &b)
391{
392 it_assert_debug(a.cols() == b.cols() && a.rows() == b.rows(), "operator+(): sizes does not match");
393 mat temp(b);
394
395 for (int i = 0;i < a.rows();i++) {
396 for (int j = 0;j < a.cols();j++) {
397 temp(i, j) += (double)a(i, j);
398 }
399 }
400 return temp;
401}
402
403mat operator+(const smat &a, const mat &b)
404{
405 it_assert_debug(a.cols() == b.cols() && a.rows() == b.rows(), "operator+(): sizes does not match");
406 mat temp(b);
407
408 for (int i = 0;i < a.rows();i++) {
409 for (int j = 0;j < a.cols();j++) {
410 temp(i, j) += (double)a(i, j);
411 }
412 }
413 return temp;
414}
415
416mat operator+(const imat &a, const mat &b)
417{
418 it_assert_debug(a.cols() == b.cols() && a.rows() == b.rows(), "operator+(): sizes does not match");
419 mat temp(b);
420
421 for (int i = 0;i < a.rows();i++) {
422 for (int j = 0;j < a.cols();j++) {
423 temp(i, j) += (double)a(i, j);
424 }
425 }
426 return temp;
427}
428
429// ---------------- Addition of cmat and matrices ---------------
430
431cmat operator+(const bmat &a, const cmat &b)
432{
433 it_assert_debug(a.cols() == b.cols() && a.rows() == b.rows(), "operator+(): sizes does not match");
434 cmat temp(b);
435
436 for (int i = 0;i < a.rows();i++) {
437 for (int j = 0;j < a.cols();j++) {
438 temp(i, j) += std::complex<double>(static_cast<double>(a(i, j)), 0.0);
439 }
440 }
441 return temp;
442}
443
444cmat operator+(const smat &a, const cmat &b)
445{
446 it_assert_debug(a.cols() == b.cols() && a.rows() == b.rows(), "operator+(): sizes does not match");
447 cmat temp(b);
448
449 for (int i = 0;i < a.rows();i++) {
450 for (int j = 0;j < a.cols();j++) {
451 temp(i, j) += (double)a(i, j);
452 }
453 }
454 return temp;
455}
456
457cmat operator+(const imat &a, const cmat &b)
458{
459 it_assert_debug(a.cols() == b.cols() && a.rows() == b.rows(), "operator+(): sizes does not match");
460 cmat temp(b);
461
462 for (int i = 0;i < a.rows();i++) {
463 for (int j = 0;j < a.cols();j++) {
464 temp(i, j) += std::complex<double>(static_cast<double>(a(i, j)), 0.0);
465 }
466 }
467 return temp;
468}
469
470cmat operator+(const mat &a, const cmat &b)
471{
472 it_assert_debug(a.cols() == b.cols() && a.rows() == b.rows(), "operator+(): sizes does not match");
473 cmat temp(b);
474
475 for (int i = 0;i < a.rows();i++) {
476 for (int j = 0;j < a.cols();j++) {
477 temp(i, j) += std::complex<double>(static_cast<double>(a(i, j)), 0.0);
478 }
479 }
480 return temp;
481}
482
483} // namespace itpp
General array class.
Definition array.h:105
int size() const
Returns the number of data elements in the array object.
Definition array.h:155
int length() const
Returns the number of data elements in the array object.
Definition array.h:157
Matrix Class (Templated)
Definition mat.h:202
int rows() const
The number of rows.
Definition mat.h:237
int cols() const
The number of columns.
Definition mat.h:235
int _datasize() const
Access to the internal data structure (not recommended to use)
Definition mat.h:444
Num_T * _data()
Access of the internal data structure (not recommended to use)
Definition mat.h:440
int size() const
The size of the vector.
Definition vec.h:271
#define it_assert_debug(t, s)
Abort if t is not true and NDEBUG is not defined.
Definition itassert.h:107
vec imag(const cvec &data)
Imaginary part of complex values.
vec real(const cvec &data)
Real part of complex values.
Mat< bin > bmat
bin matrix
Definition mat.h:508
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
Definitions of operators for vectors and matricies of different types.

Generated on Tue Mar 26 2024 19:08:31 for IT++ by Doxygen 1.9.8