operators.cpp
Go to the documentation of this file.
#include <itpp/base/operators.h>
namespace itpp
{
//----------- Scalar and a ivec -----------------
vec operator+(const double &s, const ivec &v)
{
it_assert_debug(v.size() > 0, "operator+(): Vector of zero length");
vec temp(v.size());
for (int i = 0;i < v.size();i++) {
temp(i) = s + double(v(i));
}
return temp;
}
vec operator-(const double &s, const ivec &v)
{
it_assert_debug(v.size() > 0, "operator-(): Vector of zero length");
vec temp(v.size());
for (int i = 0;i < v.size();i++) {
temp(i) = s - double(v(i));
}
return temp;
}
vec operator*(const double &s, const ivec &v)
{
it_assert_debug(v.size() > 0, "operator*(): Vector of zero length");
vec temp(v.size());
for (int i = 0;i < v.size();i++) {
temp(i) = s * double(v(i));
}
return temp;
}
vec operator/(const double &s, const ivec &v)
{
it_assert_debug(v.size() > 0, "operator/(): Vector of zero length");
vec temp(v.size());
for (int i = 0;i < v.size();i++) {
temp(i) = s / double(v(i));
}
return temp;
}
vec operator/(const ivec &v, const double &s)
{
it_assert_debug(v.size() > 0, "operator/(): Vector of zero length");
vec temp(v.size());
for (int i = 0;i < v.size();i++) {
temp(i) = double(v(i)) / s;
}
return temp;
}
cvec operator+(const std::complex<double> &s, const ivec &v)
{
it_assert_debug(v.size() > 0, "operator+(): Vector of zero length");
cvec temp(v.size());
for (int i = 0;i < v.size();i++) {
temp(i) = s + std::complex<double>(v(i));
}
return temp;
}
cvec operator-(const std::complex<double> &s, const ivec &v)
{
it_assert_debug(v.size() > 0, "operator-(): Vector of zero length");
cvec temp(v.size());
for (int i = 0;i < v.size();i++) {
temp(i) = s - std::complex<double>(v(i));
}
return temp;
}
cvec operator*(const std::complex<double> &s, const ivec &v)
{
it_assert_debug(v.size() > 0, "operator*(): Vector of zero length");
cvec temp(v.size());
for (int i = 0;i < v.size();i++) {
temp(i) = s * std::complex<double>(v(i));
}
return temp;
}
cvec operator/(const std::complex<double> &s, const ivec &v)
{
it_assert_debug(v.size() > 0, "operator/(): Vector of zero length");
cvec temp(v.size());
for (int i = 0;i < v.size();i++) {
temp(i) = s / std::complex<double>(v(i));
}
return temp;
}
cvec operator/(const ivec &v, const std::complex<double> &s)
{
it_assert_debug(v.size() > 0, "operator/(): Vector of zero length");
cvec temp(v.size());
for (int i = 0;i < v.size();i++) {
temp(i) = std::complex<double>(v(i)) / s;
}
return temp;
}
//----------- Scalar and a cvec -----------------
cvec operator+(const double &s, const cvec &v)
{
it_assert_debug(v.size() > 0, "operator+(): Vector of zero length");
cvec temp = v;
for (int i = 0;i < v.size();i++) {
temp(i) += s;
}
return temp;
}
cvec operator-(const double &s, const cvec &v)
{
it_assert_debug(v.size() > 0, "operator-(): Vector of zero length");
cvec temp(v.size());
for (int i = 0;i < v.size();i++) {
temp(i) = std::complex<double>((double)s - v(i).real(), -v(i).imag());
}
return temp;
}
cvec operator*(const double &s, const cvec &v)
{
it_assert_debug(v.size() > 0, "operator*(): Vector of zero length");
cvec temp = v;
for (int i = 0;i < v.size();i++) {
temp(i) *= (double)s;
}
return temp;
}
cvec operator/(const cvec &v, const double &s)
{
it_assert_debug(v.size() > 0, "operator/(): Vector of zero length");
cvec temp = v;
for (int i = 0;i < v.size();i++) {
temp(i) /= (double)s;
}
return temp;
}
cvec operator/(const double &s, const cvec &v)
{
it_assert_debug(v.size() > 0, "operator/(): Vector of zero length");
cvec temp(v.length());
for (int i = 0;i < v.size();i++) {
temp(i) = s / v(i);
}
return temp;
}
//----------- Scalar and a cmat -----------------
cmat operator+(const double &s, const cmat &m)
{
it_assert_debug(m.rows() > 0 && m.cols() > 0, "operator+(): Matrix of zero length");
cmat temp = m;
for (int i = 0;i < m._datasize();i++) {
temp._data()[i] += s;
}
return temp;
}
cmat operator-(const double &s, const cmat &m)
{
it_assert_debug(m.rows() > 0 && m.cols() > 0, "operator-(): Matrix of zero length");
cmat temp(m.rows(), m.cols());
for (int i = 0;i < m._datasize();i++) {
temp._data()[i] = std::complex<double>((double)s - m(i).real(), -m(i).imag());
}
return temp;
}
cmat operator*(const double &s, const cmat &m)
{
it_assert_debug(m.rows() > 0 && m.cols() > 0, "operator*(): Matrix of zero length");
cmat temp = m;
for (int i = 0;i < m._datasize();i++) {
temp._data()[i] *= (double)s;
}
return temp;
}
cmat operator*(const std::complex<double> &s, const mat &m)
{
it_assert_debug(m.rows() > 0 && m.cols() > 0, "operator*(): Matrix of zero length");
cmat temp(m.rows(), m.cols());
for (int i = 0;i < m._datasize();i++) {
temp._data()[i] = s * m._data()[i];
}
return temp;
}
cmat operator/(const cmat &m, const double &s)
{
it_assert_debug(m.rows() > 0 && m.cols() > 0, "operator/(): Matrix of zero length");
cmat temp = m;
for (int i = 0;i < m._datasize();i++) {
temp._data()[i] /= (double)s;
}
return temp;
}
//---------------------- between matrix and scalar --------------------
//----------- Multiplication of a scalar and a vec -----------------
cvec operator*(const std::complex<double> &s, const vec &v)
{
cvec temp(v.size());
for (int i = 0;i < v.size();i++) {
temp(i) = s * std::complex<double>(v(i), 0.0);
}
return temp;
}
cvec operator*(const vec &v, const std::complex<double> &s)
{
cvec temp(v.size());
for (int i = 0;i < v.size();i++) {
temp(i) = s * std::complex<double>(v(i), 0.0);
}
return temp;
}
// ===============================================================================================
// ---------------- Addition of vectors ---------------
vec operator+(const bvec &a, const vec &b)
{
it_assert_debug(a.size() == b.size(), "operator+(): sizes does not match");
vec temp(a.size());
for (int i = 0;i < a.size();i++) {temp(i) = (double)a(i) + b(i);}
return temp;
}
vec operator+(const svec &a, const vec &b)
{
it_assert_debug(a.size() == b.size(), "operator+(): sizes does not match");
vec temp(a.size());
for (int i = 0;i < a.size();i++) {temp(i) = (double)a(i) + b(i);}
return temp;
}
vec operator+(const ivec &a, const vec &b)
{
it_assert_debug(a.size() == b.size(), "operator+(): sizes does not match");
vec temp(a.size());
for (int i = 0;i < a.size();i++) {temp(i) = (double)a(i) + b(i);}
return temp;
}
cvec operator+(const bvec &a, const cvec &b)
{
it_assert_debug(a.size() == b.size(), "operator+(): sizes does not match");
cvec temp = b;
for (int i = 0;i < a.size();i++) {temp(i) += (double)a(i);}
return temp;
}
cvec operator+(const svec &a, const cvec &b)
{
it_assert_debug(a.size() == b.size(), "operator+(): sizes does not match");
cvec temp = b;
for (int i = 0;i < a.size();i++) {temp(i) += (double)a(i);}
return temp;
}
cvec operator+(const ivec &a, const cvec &b)
{
it_assert_debug(a.size() == b.size(), "operator+(): sizes does not match");
cvec temp = b;
for (int i = 0;i < a.size();i++) {temp(i) += (double)a(i);}
return temp;
}
// ---------------- Multiplication of vectors ---------------
double operator*(const bvec &a, const vec &b)
{
it_assert_debug(a.size() == b.size(), "operator*(): sizes does not match");
double temp = 0;
for (int i = 0;i < a.size();i++) {temp += (double)a(i) * b(i);}
return temp;
}
double operator*(const svec &a, const vec &b)
{
it_assert_debug(a.size() == b.size(), "operator*(): sizes does not match");
double temp = 0;
for (int i = 0;i < a.size();i++) {temp += (double)a(i) * b(i);}
return temp;
}
double operator*(const ivec &a, const vec &b)
{
it_assert_debug(a.size() == b.size(), "operator*(): sizes does not match");
double temp = 0;
for (int i = 0;i < a.size();i++) {temp += (double)a(i) * b(i);}
return temp;
}
std::complex<double> operator*(const bvec &a, const cvec &b)
{
it_assert_debug(a.size() == b.size(), "operator*(): sizes does not match");
std::complex<double> temp = 0;
for (int i = 0;i < a.size();i++) {temp += (double)a(i) * b(i);}
return temp;
}
std::complex<double> operator*(const svec &a, const cvec &b)
{
it_assert_debug(a.size() == b.size(), "operator*(): sizes does not match");
std::complex<double> temp = 0;
for (int i = 0;i < a.size();i++) {temp += (double)a(i) * b(i);}
return temp;
}
std::complex<double> operator*(const ivec &a, const cvec &b)
{
it_assert_debug(a.size() == b.size(), "operator*(): sizes does not match");
std::complex<double> temp = 0;
for (int i = 0;i < a.size();i++) {temp += (double)a(i) * b(i);}
return temp;
}
// ---------------- Addition of matricies ---------------
mat operator+(const bmat &a, const mat &b)
{
it_assert_debug(a.cols() == b.cols() && a.rows() == b.rows(), "operator+(): sizes does not match");
mat temp(b);
for (int i = 0;i < a.rows();i++) {
for (int j = 0;j < a.cols();j++) {
temp(i, j) += (double)a(i, j);
}
}
return temp;
}
mat operator+(const smat &a, const mat &b)
{
it_assert_debug(a.cols() == b.cols() && a.rows() == b.rows(), "operator+(): sizes does not match");
mat temp(b);
for (int i = 0;i < a.rows();i++) {
for (int j = 0;j < a.cols();j++) {
temp(i, j) += (double)a(i, j);
}
}
return temp;
}
mat operator+(const imat &a, const mat &b)
{
it_assert_debug(a.cols() == b.cols() && a.rows() == b.rows(), "operator+(): sizes does not match");
mat temp(b);
for (int i = 0;i < a.rows();i++) {
for (int j = 0;j < a.cols();j++) {
temp(i, j) += (double)a(i, j);
}
}
return temp;
}
// ---------------- Addition of cmat and matrices ---------------
cmat operator+(const bmat &a, const cmat &b)
{
it_assert_debug(a.cols() == b.cols() && a.rows() == b.rows(), "operator+(): sizes does not match");
cmat temp(b);
for (int i = 0;i < a.rows();i++) {
for (int j = 0;j < a.cols();j++) {
temp(i, j) += std::complex<double>(static_cast<double>(a(i, j)), 0.0);
}
}
return temp;
}
cmat operator+(const smat &a, const cmat &b)
{
it_assert_debug(a.cols() == b.cols() && a.rows() == b.rows(), "operator+(): sizes does not match");
cmat temp(b);
for (int i = 0;i < a.rows();i++) {
for (int j = 0;j < a.cols();j++) {
temp(i, j) += (double)a(i, j);
}
}
return temp;
}
cmat operator+(const imat &a, const cmat &b)
{
it_assert_debug(a.cols() == b.cols() && a.rows() == b.rows(), "operator+(): sizes does not match");
cmat temp(b);
for (int i = 0;i < a.rows();i++) {
for (int j = 0;j < a.cols();j++) {
temp(i, j) += std::complex<double>(static_cast<double>(a(i, j)), 0.0);
}
}
return temp;
}
cmat operator+(const mat &a, const cmat &b)
{
it_assert_debug(a.cols() == b.cols() && a.rows() == b.rows(), "operator+(): sizes does not match");
cmat temp(b);
for (int i = 0;i < a.rows();i++) {
for (int j = 0;j < a.cols();j++) {
temp(i, j) += std::complex<double>(static_cast<double>(a(i, j)), 0.0);
}
}
return temp;
}
} // namespace itpp
itpp::Array
General array class.
Definition array.h:105
itpp::Array::size
int size() const
Returns the number of data elements in the array object.
Definition array.h:155
itpp::Array::length
int length() const
Returns the number of data elements in the array object.
Definition array.h:157
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::_datasize
int _datasize() const
Access to the internal data structure (not recommended to use)
Definition mat.h:444
itpp::Mat::_data
Num_T * _data()
Access of the internal data structure (not recommended to use)
Definition mat.h:440
itpp::Vec< double >
itpp::Vec::size
int size() const
The size of the vector.
Definition vec.h:271
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::imag
vec imag(const cvec &data)
Imaginary part of complex values.
Definition elem_math.cpp:180
itpp::real
vec real(const cvec &data)
Real part of complex values.
Definition elem_math.cpp:157
bmat
Mat< bin > bmat
bin matrix
Definition mat.h:508
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
operators.h
Definitions of operators for vectors and matricies of different types.

Generated on Mon Jun 10 2024 11:49:18 for IT++ by Doxygen 1.9.8