fix_operators.cpp
Go to the documentation of this file.
#include <itpp/fixed/fix_operators.h>
namespace itpp
{
// Operators for Fix and Fixed //
Fix operator+(const Fix &x, const Fix &y)
{
return Fix(x.get_re() + y.get_re(),
assert_shifts(x, y),
0, 0);
}
Fix operator-(const Fix &x, const Fix &y)
{
return Fix(x.get_re() - y.get_re(),
assert_shifts(x, y),
0, 0);
}
Fix operator*(const Fix &x, const Fix &y)
{
return Fix(x.get_re() * y.get_re(),
x.get_shift() + y.get_shift(),
0, 0);
}
Fix operator/(const Fix &x, const Fix &y)
{
return Fix(x.get_re() / y.get_re(),
x.get_shift() - y.get_shift(),
0, 0);
}
Fix operator+(const Fix &x, const int y)
{
return Fix(x.get_re() + y,
assert_shifts(x, y),
0, 0);
}
Fix operator-(const Fix &x, const int y)
{
return Fix(x.get_re() - y,
assert_shifts(x, y),
0, 0);
}
Fix operator*(const Fix &x, const int y)
{
return Fix(x.get_re() * y,
x.get_shift(),
0, 0);
}
Fix operator/(const Fix &x, const int y)
{
return Fix(x.get_re() / y,
x.get_shift(),
0, 0);
}
Fix operator+(const int x, const Fix &y)
{
return Fix(x + y.get_re(),
assert_shifts(y, x),
0, 0);
}
Fix operator-(const int x, const Fix &y)
{
return Fix(x - y.get_re(),
assert_shifts(y, x),
0, 0);
}
Fix operator*(const int x, const Fix &y)
{
return Fix(x * y.get_re(),
y.get_shift(),
0, 0);
}
Fix operator/(const int x, const Fix &y)
{
return Fix(x / y.get_re(),
-y.get_shift(),
0, 0);
}
fixvec operator+(const fixvec &a, const ivec &b)
{
it_assert_debug(a.size() == b.size(), "operator+(): sizes do not match");
fixvec temp(a);
for (int i = 0; i < a.size(); i++) {
temp(i) += b(i);
}
return temp;
}
Fix operator*(const fixvec &a, const ivec &b)
{
it_assert_debug(a.size() == b.size(), "operator+(): sizes do not match");
Fix temp(0);
for (int i = 0; i < a.size(); i++) {
temp += a(i) * b(i);
}
return temp;
}
fixmat operator+(const fixmat &a, const imat &b)
{
it_assert_debug(a.cols() == b.cols() && a.rows() == b.rows(), "operator+(): sizes do not match");
fixmat temp(a);
for (int i = 0; i < a.rows(); i++) {
for (int j = 0; j < a.cols(); j++) {
temp(i, j) += b(i, j);
}
}
return temp;
}
fixmat operator*(const fixmat &a, const imat &b)
{
it_assert_debug(a.cols() == b.rows(), "operator*: wrong sizes");
fixmat r(a.rows(), b.cols());
Fix tmp;
int i, j, k;
Fix *tr = r._data();
const Fix *t1;
const int *t2 = b._data();
for (i = 0; i < r.cols(); i++) {
for (j = 0; j < r.rows(); j++) {
tmp = Fix(0);
t1 = a._data() + j;
for (k = a.cols(); k > 0; k--) {
tmp += *(t1) * *(t2++);
t1 += a.rows();
}
*(tr++) = tmp;
t2 -= b.rows();
}
t2 += b.rows();
}
return r;
}
// Operators for CFix and CFixed //
CFix operator+(const CFix &x, const CFix &y)
{
return CFix(x.get_re() + y.get_re(),
x.get_im() + y.get_im(),
assert_shifts(x, y),
0, 0);
}
CFix operator-(const CFix &x, const CFix &y)
{
return CFix(x.get_re() - y.get_re(),
x.get_im() - y.get_im(),
assert_shifts(x, y),
0, 0);
}
CFix operator*(const CFix &x, const CFix &y)
{
return CFix(x.get_re()*y.get_re() - x.get_im()*y.get_im(),
x.get_re()*y.get_im() + x.get_im()*y.get_re(),
x.get_shift() + y.get_shift(),
0, 0);
}
CFix operator/(const CFix &x, const CFix &y)
{
fixrep denominator = y.get_re() * y.get_re() + y.get_im() * y.get_im();
return CFix((x.get_re()*y.get_re() + x.get_im()*y.get_im()) / denominator,
(x.get_im()*y.get_re() - x.get_re()*y.get_im()) / denominator,
x.get_shift() - y.get_shift(),
0, 0);
}
CFix operator+(const CFix &x, const Fix &y)
{
return CFix(x.get_re() + y.get_re(),
x.get_im(),
assert_shifts(x, y),
0, 0);
}
CFix operator-(const CFix &x, const Fix &y)
{
return CFix(x.get_re() - y.get_re(),
x.get_im(),
assert_shifts(x, y),
0, 0);
}
CFix operator*(const CFix &x, const Fix &y)
{
return CFix(x.get_re() * y.get_re(),
x.get_im() * y.get_re(),
x.get_shift() + y.get_shift(),
0, 0);
}
CFix operator/(const CFix &x, const Fix &y)
{
return CFix(x.get_re() / y.get_re(),
x.get_im() / y.get_re(),
x.get_shift() - y.get_shift(),
0, 0);
}
CFix operator+(const Fix &x, const CFix &y)
{
return CFix(x.get_re() + y.get_re(),
y.get_im(),
assert_shifts(y, x),
0, 0);
}
CFix operator-(const Fix &x, const CFix &y)
{
return CFix(x.get_re() - y.get_re(),
-y.get_im(),
assert_shifts(y, x),
0, 0);
}
CFix operator*(const Fix &x, const CFix &y)
{
return CFix(x.get_re() * y.get_re(),
x.get_re() * y.get_im(),
x.get_shift() + y.get_shift(),
0, 0);
}
CFix operator/(const Fix &x, const CFix &y)
{
fixrep denominator = y.get_re() * y.get_re() + y.get_im() * y.get_im();
return CFix(x.get_re() * y.get_re() / denominator,
-x.get_re() * y.get_im() / denominator,
x.get_shift() - y.get_shift(),
0, 0);
}
CFix operator+(const CFix &x, const int y)
{
return CFix(x.get_re() + y,
x.get_im(),
assert_shifts(x, y),
0, 0);
}
CFix operator-(const CFix &x, const int y)
{
return CFix(x.get_re() - y,
x.get_im(),
assert_shifts(x, y),
0, 0);
}
CFix operator*(const CFix &x, const int y)
{
return CFix(x.get_re() * y,
x.get_im() * y,
x.get_shift(),
0, 0);
}
CFix operator/(const CFix &x, const int y)
{
return CFix(x.get_re() / y,
x.get_im() / y,
x.get_shift(),
0, 0);
}
CFix operator+(const int x, const CFix &y)
{
return CFix(x + y.get_re(),
y.get_im(),
assert_shifts(y, x),
0, 0);
}
CFix operator-(const int x, const CFix &y)
{
return CFix(x - y.get_re(),
-y.get_im(),
assert_shifts(y, x),
0, 0);
}
CFix operator*(const int x, const CFix &y)
{
return CFix(x * y.get_re(),
x * y.get_im(),
y.get_shift(),
0, 0);
}
CFix operator/(const int x, const CFix &y)
{
fixrep denominator = y.get_re() * y.get_re() + y.get_im() * y.get_im();
return CFix(x * y.get_re() / denominator,
-x * y.get_im() / denominator,
-y.get_shift(),
0, 0);
}
cfixvec operator+(const cfixvec &a, const fixvec &b)
{
it_assert_debug(a.size() == b.size(), "operator+(): sizes do not match");
cfixvec temp(a);
for (int i = 0; i < a.size(); i++) {
temp(i) += b(i);
}
return temp;
}
CFix operator*(const cfixvec &a, const fixvec &b)
{
it_assert_debug(a.size() == b.size(), "operator+(): sizes do not match");
CFix temp(0);
for (int i = 0; i < a.size(); i++) {
temp += a(i) * b(i);
}
return temp;
}
cfixmat operator+(const cfixmat &a, const fixmat &b)
{
it_assert_debug(a.cols() == b.cols() && a.rows() == b.rows(), "operator+(): sizes do not match");
cfixmat temp(a);
for (int i = 0; i < a.rows(); i++) {
for (int j = 0; j < a.cols(); j++) {
temp(i, j) += b(i, j);
}
}
return temp;
}
cfixmat operator*(const cfixmat &a, const fixmat &b)
{
it_assert_debug(a.cols() == b.rows(), "operator*: wrong sizes");
cfixmat r(a.rows(), b.cols());
CFix tmp;
int i, j, k;
CFix *tr = r._data();
const CFix *t1;
const Fix *t2 = b._data();
for (i = 0; i < r.cols(); i++) {
for (j = 0; j < r.rows(); j++) {
tmp = CFix(0);
t1 = a._data() + j;
for (k = a.cols(); k > 0; k--) {
tmp += *(t1) * *(t2++);
t1 += a.rows();
}
*(tr++) = tmp;
t2 -= b.rows();
}
t2 += b.rows();
}
return r;
}
cfixvec operator+(const cfixvec &a, const ivec &b)
{
it_assert_debug(a.size() == b.size(), "operator+(): sizes do not match");
cfixvec temp(a);
for (int i = 0; i < a.size(); i++) {
temp(i) += b(i);
}
return temp;
}
CFix operator*(const cfixvec &a, const ivec &b)
{
it_assert_debug(a.size() == b.size(), "operator+(): sizes do not match");
CFix temp(0);
for (int i = 0; i < a.size(); i++) {
temp += a(i) * b(i);
}
return temp;
}
cfixmat operator+(const cfixmat &a, const imat &b)
{
it_assert_debug(a.cols() == b.cols() && a.rows() == b.rows(), "operator+(): sizes do not match");
cfixmat temp(a);
for (int i = 0; i < a.rows(); i++) {
for (int j = 0; j < a.cols(); j++) {
temp(i, j) += b(i, j);
}
}
return temp;
}
cfixmat operator*(const cfixmat &a, const imat &b)
{
it_assert_debug(a.cols() == b.rows(), "operator*: wrong sizes");
cfixmat r(a.rows(), b.cols());
CFix tmp;
int i, j, k;
CFix *tr = r._data();
const CFix *t1;
const int *t2 = b._data();
for (i = 0; i < r.cols(); i++) {
for (j = 0; j < r.rows(); j++) {
tmp = CFix(0);
t1 = a._data() + j;
for (k = a.cols(); k > 0; k--) {
tmp += *(t1) * *(t2++);
t1 += a.rows();
}
*(tr++) = tmp;
t2 -= b.rows();
}
t2 += b.rows();
}
return r;
}
} // namespace itpp
itpp::CFix
Complex fixed-point data type.
Definition: cfix.h:52
itpp::CFix::get_im
fixrep get_im() const
Get data representation for imaginary part (mainly for internal use since it reveals the representati...
Definition: cfix.h:140
itpp::CFix::get_re
fixrep get_re() const
Get data representation for real part (mainly for internal use since it reveals the representation ty...
Definition: cfix.h:138
itpp::Fix_Base::get_shift
int get_shift() const
Get shift.
Definition: fix_base.h:1001
itpp::Fix
Fixed-point data type.
Definition: fix.h:52
itpp::Fix::get_re
fixrep get_re() const
Get data representation (mainly for internal use since it reveals the representation type)
Definition: fix.h:116
itpp::Mat
Matrix Class (Templated)
Definition: mat.h:202
itpp::Mat::rows
int rows() const
The number of rows.
Definition: mat.h:237
itpp::Mat::cols
int cols() const
The number of columns.
Definition: mat.h:235
itpp::Mat::_data
Num_T * _data()
Access of the internal data structure (not recommended to use)
Definition: mat.h:440
itpp::Vec
Vector Class (Templated)
Definition: vec.h:245
itpp::Vec::size
int size() const
The size of the vector.
Definition: vec.h:271
fix_operators.h
Definitions of a set of operators for Fix, Fixed, CFix and CFixed classes.
it_assert_debug
#define it_assert_debug(t, s)
Abort if t is not true and NDEBUG is not defined.
Definition: itassert.h:107
itpp::assert_shifts
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
itpp::fixrep
int64_t fixrep
Representation for fixed-point data types.
Definition: fix_base.h:884
itpp
itpp namespace
Definition: itmex.h:37
itpp::operator-
Mat< Num_T > operator-(const Mat< Num_T > &m1, const Mat< Num_T > &m2)
Subtraction of two matrices.
Definition: mat.h:1382
itpp::operator*
GF2mat operator*(const GF2mat &X, const GF2mat &Y)
GF(2) matrix multiplication.
Definition: gf2mat.cpp:847
itpp::operator/
Mat< Num_T > operator/(const Mat< Num_T > &m, Num_T t)
Element-wise division by a scalar.
Definition: mat.h:1670
itpp::operator+
GF2mat operator+(const GF2mat &X, const GF2mat &Y)
GF(2) matrix addition.
Definition: gf2mat.cpp:948

Generated on Tue Aug 17 2021 10:59:15 for IT++ by Doxygen 1.9.1