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
|
/*!
* \file
* \brief Operators test program
* \author Bogdan Cristea
*
* -------------------------------------------------------------------------
*
* Copyright (C) 1995-2011 (see AUTHORS file for a list of contributors)
*
* This file is part of IT++ - a C++ library of mathematical, signal
* processing, speech processing, and communications classes and functions.
*
* IT++ is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* IT++ is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along
* with IT++. If not, see <http://www.gnu.org/licenses/>.
*
* -------------------------------------------------------------------------
*/
#include "itpp/itbase.h"
using namespace itpp;
using std::cout;
using std::endl;
using std::complex;
int main()
{
//complex plus/minus scalar
{
//complex scalar
complex<double> x(1.0, 1.0);
int y = 2;
complex<double> z = x+y;
if (complex<double>(x.real()+y, x.imag()) != z)
{
cout << "complex scalar plus int scalar operator is wrong" << endl;
return EXIT_FAILURE;
}
z = x-y;
if (complex<double>(x.real()-y, x.imag()) != z)
{
cout << "complex scalar minus int scalar operator is wrong" << endl;
return EXIT_FAILURE;
}
//complex vector
vec x1_re("1 3");
vec x1_im("2 4");
cvec x1 = to_cvec(x1_re, x1_im);
cvec z1 = x1+y;
if (to_cvec(x1_re+y, x1_im) != z1)
{
cout << "complex vector plus int scalar operator is wrong" << endl;
return EXIT_FAILURE;
}
z1 = x1-y;
if (to_cvec(x1_re-y, x1_im) != z1)
{
cout << "complex vector minus int scalar operator is wrong" << endl;
return EXIT_FAILURE;
}
//complex scalar
float y1 = 2.7;
z = x+y1;
if (complex<double>(x.real()+y1, x.imag()) != z)
{
cout << "complex plus float scalar operator is wrong" << endl;
return EXIT_FAILURE;
}
z = x-y1;
if (complex<double>(x.real()-y1, x.imag()) != z)
{
cout << "complex minus float scalar operator is wrong" << endl;
return EXIT_FAILURE;
}
//complex vector
z1 = x1+y1;
if (to_cvec(x1_re+y1, x1_im) != z1)
{
cout << "complex vector plus float scalar operator is wrong" << endl;
return EXIT_FAILURE;
}
z1 = x1-y1;
if (to_cvec(x1_re-y1, x1_im) != z1)
{
cout << "complex vector minus float scalar operator is wrong" << endl;
return EXIT_FAILURE;
}
//complex matrix
mat x2_re("1 3; 5 7");
mat x2_im("2 4; 6 8");
cmat x2 = to_cmat(x2_re, x2_im);
cmat z2 = x2+complex<double>(y1, 0);
if (to_cmat(x2_re+double(y1), x2_im) != z2)
{
cout << "complex matrix plus complex scalar operator is wrong" << endl;
return EXIT_FAILURE;
}
z2 = x2-complex<double>(y1, 0);
if (to_cmat(x2_re-double(y1), x2_im) != z2)
{
cout << "complex matrix minus complex scalar operator is wrong" << endl;
return EXIT_FAILURE;
}
}
//scalar plus/minus complex
{
//complex scalar
int x = 2;
complex<double> y(1.0, 1.0);
complex<double> z = x+y;
if (complex<double>(x+y.real(), y.imag()) != z)
{
cout << "int scalar plus complex scalar operator is wrong" << endl;
return EXIT_FAILURE;
}
z = x-y;
if (complex<double>(x-y.real(), -y.imag()) != z)
{
cout << "int scalar minus complex scalar operator is wrong" << endl;
return EXIT_FAILURE;
}
//complex vector
vec y1_re("1 3");
vec y1_im("2 4");
cvec y1 = to_cvec(y1_re, y1_im);
cvec z1 = x+y1;
if (to_cvec(x+y1_re, y1_im) != z1)
{
cout << "int scalar plus complex vector operator is wrong" << endl;
return EXIT_FAILURE;
}
z1 = x-y1;
if (to_cvec(x-y1_re, -y1_im) != z1)
{
cout << "int scalar minus complex vector operator is wrong" << endl;
return EXIT_FAILURE;
}
//complex scalar
float x1 = 2.7;
z = x1+y;
if (complex<double>(x1+y.real(), y.imag()) != z)
{
cout << "float scalar plus complex operator is wrong" << endl;
return EXIT_FAILURE;
}
z = x1-y;
if (complex<double>(x1-y.real(), -y.imag()) != z)
{
cout << "float scalar minus complex operator is wrong" << endl;
return EXIT_FAILURE;
}
//complex vector
z1 = x1+y1;
if (to_cvec(x1+y1_re, y1_im) != z1)
{
cout << "float scalar plus complex vector operator is wrong" << endl;
return EXIT_FAILURE;
}
z1 = x1-y1;
if (to_cvec(x1-y1_re, -y1_im) != z1)
{
cout << "float scalar minus complex vector operator is wrong" << endl;
return EXIT_FAILURE;
}
//complex matrix
mat y2_re("1 3; 5 7");
mat y2_im("2 4; 6 8");
cmat y2 = to_cmat(y2_re, y2_im);
cmat z2 = complex<double>(x1, 0)+y2;
if (to_cmat(x1+y2_re, y2_im) != z2)
{
cout << "complex scalar plus complex matrix operator is wrong" << endl;
return EXIT_FAILURE;
}
z2 = complex<double>(x1, 0)-y2;
if (to_cmat(x1-y2_re, -y2_im) != z2)
{
cout << "complex scalar minus complex matrix operator is wrong" << endl;
return EXIT_FAILURE;
}
}
cout << "operators test successful" << endl;
return EXIT_SUCCESS;
}
|