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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
|
/*!
* \file
* \brief Matrix Class Implementation
* \author Tony Ottosson, Tobias Ringstrom, Adam Piatyszek and Conrad Sanderson
*
* -------------------------------------------------------------------------
*
* Copyright (C) 1995-2010 (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/base/mat.h>
#ifndef _MSC_VER
# include <itpp/config.h>
#else
# include <itpp/config_msvc.h>
#endif
#if defined (HAVE_BLAS)
# include <itpp/base/blas.h>
#endif
//! \cond
namespace itpp
{
template<>
cmat cmat::hermitian_transpose() const
{
cmat temp(no_cols, no_rows);
for (int i = 0; i < no_rows; i++)
for (int j = 0; j < no_cols; j++)
temp(j, i) = std::conj(operator()(i,j));
return temp;
}
// -------- Multiplication operator -------------
#if defined(HAVE_BLAS)
template<>
mat& mat::operator*=(const mat &m)
{
it_assert_debug(no_cols == m.no_rows, "mat::operator*=(): Wrong sizes");
mat r(no_rows, m.no_cols); // unnecessary memory??
double alpha = 1.0;
double beta = 0.0;
char trans = 'n';
blas::dgemm_(&trans, &trans, &no_rows, &m.no_cols, &no_cols, &alpha, data,
&no_rows, m.data, &m.no_rows, &beta, r.data, &r.no_rows);
operator=(r); // time consuming
return *this;
}
template<>
cmat& cmat::operator*=(const cmat &m)
{
it_assert_debug(no_cols == m.no_rows, "cmat::operator*=(): Wrong sizes");
cmat r(no_rows, m.no_cols); // unnecessary memory??
std::complex<double> alpha = std::complex<double>(1.0);
std::complex<double> beta = std::complex<double>(0.0);
char trans = 'n';
blas::zgemm_(&trans, &trans, &no_rows, &m.no_cols, &no_cols, &alpha, data,
&no_rows, m.data, &m.no_rows, &beta, r.data, &r.no_rows);
operator=(r); // time consuming
return *this;
}
#else
template<>
mat& mat::operator*=(const mat &m)
{
it_assert_debug(no_cols == m.no_rows, "Mat<>::operator*=(): Wrong sizes");
mat r(no_rows, m.no_cols);
int r_pos = 0, pos = 0, m_pos = 0;
for (int i = 0; i < r.no_cols; i++) {
for (int j = 0; j < r.no_rows; j++) {
double tmp = 0.0;
pos = 0;
for (int k = 0; k < no_cols; k++) {
tmp += data[pos+j] * m.data[m_pos+k];
pos += no_rows;
}
r.data[r_pos+j] = tmp;
}
r_pos += r.no_rows;
m_pos += m.no_rows;
}
operator=(r); // time consuming
return *this;
}
template<>
cmat& cmat::operator*=(const cmat &m)
{
it_assert_debug(no_cols == m.no_rows, "Mat<>::operator*=(): Wrong sizes");
cmat r(no_rows, m.no_cols);
int r_pos = 0, pos = 0, m_pos = 0;
for (int i = 0; i < r.no_cols; i++) {
for (int j = 0; j < r.no_rows; j++) {
std::complex<double> tmp(0.0);
pos = 0;
for (int k = 0; k < no_cols; k++) {
tmp += data[pos+j] * m.data[m_pos+k];
pos += no_rows;
}
r.data[r_pos+j] = tmp;
}
r_pos += r.no_rows;
m_pos += m.no_rows;
}
operator=(r); // time consuming
return *this;
}
#endif // HAVE_BLAS
#if defined(HAVE_BLAS)
template<>
mat operator*(const mat &m1, const mat &m2)
{
it_assert_debug(m1.cols() == m2.rows(), "mat::operator*(): Wrong sizes");
int m1_r = m1.rows(); int m1_c = m1.cols();
int m2_r = m2.rows(); int m2_c = m2.cols();
mat r(m1_r, m2_c);
double alpha = 1.0;
double beta = 0.0;
char trans = 'n';
blas::dgemm_(&trans, &trans, &m1_r, &m2_c, &m1_c, &alpha,
m1._data(), &m1_r, m2._data(), &m2_r, &beta, r._data(),
&m1_r);
return r;
}
template<>
cmat operator*(const cmat &m1, const cmat &m2)
{
it_assert_debug(m1.cols() == m2.rows(), "cmat::operator*(): Wrong sizes");
int m1_r = m1.rows(); int m1_c = m1.cols();
int m2_r = m2.rows(); int m2_c = m2.cols();
cmat r(m1_r, m2_c);
std::complex<double> alpha = std::complex<double>(1.0);
std::complex<double> beta = std::complex<double>(0.0);
char trans = 'n';
blas::zgemm_(&trans, &trans, &m1_r, &m2_c, &m1_c, &alpha,
m1._data(), &m1_r, m2._data(), &m2_r, &beta, r._data(),
&m1_r);
return r;
}
#else
template<>
mat operator*(const mat &m1, const mat &m2)
{
it_assert_debug(m1.rows() == m2.cols(),
"Mat<>::operator*(): Wrong sizes");
mat r(m1.rows(), m2.cols());
double *tr = r._data();
const double *t1;
const double *t2 = m2._data();
for (int i = 0; i < r.cols(); i++) {
for (int j = 0; j < r.rows(); j++) {
double tmp = 0.0;
t1 = m1._data() + j;
for (int k = m1.cols(); k > 0; k--) {
tmp += *(t1) * *(t2++);
t1 += m1.rows();
}
*(tr++) = tmp;
t2 -= m2.rows();
}
t2 += m2.rows();
}
return r;
}
template<>
cmat operator*(const cmat &m1, const cmat &m2)
{
it_assert_debug(m1.cols() == m2.rows(),
"Mat<>::operator*(): Wrong sizes");
cmat r(m1.rows(), m2.cols());
std::complex<double> *tr = r._data();
const std::complex<double> *t1;
const std::complex<double> *t2 = m2._data();
for (int i = 0; i < r.cols(); i++) {
for (int j = 0; j < r.rows(); j++) {
std::complex<double> tmp(0.0);
t1 = m1._data() + j;
for (int k = m1.cols(); k > 0; k--) {
tmp += *(t1) * *(t2++);
t1 += m1.rows();
}
*(tr++) = tmp;
t2 -= m2.rows();
}
t2 += m2.rows();
}
return r;
}
#endif // HAVE_BLAS
#if defined(HAVE_BLAS)
template<>
vec operator*(const mat &m, const vec &v)
{
it_assert_debug(m.cols() == v.size(), "mat::operator*(): Wrong sizes");
int m_r = m.rows(); int m_c = m.cols();
vec r(m_r);
double alpha = 1.0;
double beta = 0.0;
char trans = 'n';
int incr = 1;
blas::dgemv_(&trans, &m_r, &m_c, &alpha, m._data(), &m_r,
v._data(), &incr, &beta, r._data(), &incr);
return r;
}
template<>
cvec operator*(const cmat &m, const cvec &v)
{
it_assert_debug(m.cols() == v.size(), "cmat::operator*(): Wrong sizes");
int m_r = m.rows(); int m_c = m.cols();
cvec r(m_r);
std::complex<double> alpha = std::complex<double>(1.0);
std::complex<double> beta = std::complex<double>(0.0);
char trans = 'n';
int incr = 1;
blas::zgemv_(&trans, &m_r, &m_c, &alpha, m._data(), &m_r,
v._data(), &incr, &beta, r._data(), &incr);
return r;
}
#else
template<>
vec operator*(const mat &m, const vec &v)
{
it_assert_debug(m.cols() == v.size(),
"Mat<>::operator*(): Wrong sizes");
vec r(m.rows());
for (int i = 0; i < m.rows(); i++) {
r(i) = 0.0;
int m_pos = 0;
for (int k = 0; k < m.cols(); k++) {
r(i) += m._data()[m_pos+i] * v(k);
m_pos += m.rows();
}
}
return r;
}
template<>
cvec operator*(const cmat &m, const cvec &v)
{
it_assert_debug(m.cols() == v.size(),
"Mat<>::operator*(): Wrong sizes");
cvec r(m.rows());
for (int i = 0; i < m.rows(); i++) {
r(i) = std::complex<double>(0.0);
int m_pos = 0;
for (int k = 0; k < m.cols(); k++) {
r(i) += m._data()[m_pos+i] * v(k);
m_pos += m.rows();
}
}
return r;
}
#endif // HAVE_BLAS
//---------------------------------------------------------------------
// Instantiations
//---------------------------------------------------------------------
// class instantiations
template class ITPP_EXPORT Mat<double>;
template class ITPP_EXPORT Mat<std::complex<double> >;
template class ITPP_EXPORT Mat<int>;
template class ITPP_EXPORT Mat<short int>;
template class ITPP_EXPORT Mat<bin>;
// addition operators
template ITPP_EXPORT mat operator+(const mat &m1, const mat &m2);
template ITPP_EXPORT cmat operator+(const cmat &m1, const cmat &m2);
template ITPP_EXPORT imat operator+(const imat &m1, const imat &m2);
template ITPP_EXPORT smat operator+(const smat &m1, const smat &m2);
template ITPP_EXPORT bmat operator+(const bmat &m1, const bmat &m2);
template ITPP_EXPORT mat operator+(const mat &m, double t);
template ITPP_EXPORT cmat operator+(const cmat &m, std::complex<double> t);
template ITPP_EXPORT imat operator+(const imat &m, int t);
template ITPP_EXPORT smat operator+(const smat &m, short t);
template ITPP_EXPORT bmat operator+(const bmat &m, bin t);
template ITPP_EXPORT mat operator+(double t, const mat &m);
template ITPP_EXPORT cmat operator+(std::complex<double> t, const cmat &m);
template ITPP_EXPORT imat operator+(int t, const imat &m);
template ITPP_EXPORT smat operator+(short t, const smat &m);
template ITPP_EXPORT bmat operator+(bin t, const bmat &m);
// subraction operators
template ITPP_EXPORT mat operator-(const mat &m1, const mat &m2);
template ITPP_EXPORT cmat operator-(const cmat &m1, const cmat &m2);
template ITPP_EXPORT imat operator-(const imat &m1, const imat &m2);
template ITPP_EXPORT smat operator-(const smat &m1, const smat &m2);
template ITPP_EXPORT bmat operator-(const bmat &m1, const bmat &m2);
template ITPP_EXPORT mat operator-(const mat &m, double t);
template ITPP_EXPORT cmat operator-(const cmat &m, std::complex<double> t);
template ITPP_EXPORT imat operator-(const imat &m, int t);
template ITPP_EXPORT smat operator-(const smat &m, short t);
template ITPP_EXPORT bmat operator-(const bmat &m, bin t);
template ITPP_EXPORT mat operator-(double t, const mat &m);
template ITPP_EXPORT cmat operator-(std::complex<double> t, const cmat &m);
template ITPP_EXPORT imat operator-(int t, const imat &m);
template ITPP_EXPORT smat operator-(short t, const smat &m);
template ITPP_EXPORT bmat operator-(bin t, const bmat &m);
// unary minus
template ITPP_EXPORT mat operator-(const mat &m);
template ITPP_EXPORT cmat operator-(const cmat &m);
template ITPP_EXPORT imat operator-(const imat &m);
template ITPP_EXPORT smat operator-(const smat &m);
template ITPP_EXPORT bmat operator-(const bmat &m);
// multiplication operators
template ITPP_EXPORT imat operator*(const imat &m1, const imat &m2);
template ITPP_EXPORT smat operator*(const smat &m1, const smat &m2);
template ITPP_EXPORT bmat operator*(const bmat &m1, const bmat &m2);
template ITPP_EXPORT ivec operator*(const imat &m, const ivec &v);
template ITPP_EXPORT svec operator*(const smat &m, const svec &v);
template ITPP_EXPORT bvec operator*(const bmat &m, const bvec &v);
template ITPP_EXPORT mat operator*(const mat &m, double t);
template ITPP_EXPORT cmat operator*(const cmat &m, std::complex<double> t);
template ITPP_EXPORT imat operator*(const imat &m, int t);
template ITPP_EXPORT smat operator*(const smat &m, short t);
template ITPP_EXPORT bmat operator*(const bmat &m, bin t);
template ITPP_EXPORT mat operator*(double t, const mat &m);
template ITPP_EXPORT cmat operator*(std::complex<double> t, const cmat &m);
template ITPP_EXPORT imat operator*(int t, const imat &m);
template ITPP_EXPORT smat operator*(short t, const smat &m);
template ITPP_EXPORT bmat operator*(bin t, const bmat &m);
// elementwise multiplication
template ITPP_EXPORT mat elem_mult(const mat &m1, const mat &m2);
template ITPP_EXPORT cmat elem_mult(const cmat &m1, const cmat &m2);
template ITPP_EXPORT imat elem_mult(const imat &m1, const imat &m2);
template ITPP_EXPORT smat elem_mult(const smat &m1, const smat &m2);
template ITPP_EXPORT bmat elem_mult(const bmat &m1, const bmat &m2);
template ITPP_EXPORT void elem_mult_out(const mat &m1, const mat &m2, mat &out);
template ITPP_EXPORT void elem_mult_out(const cmat &m1, const cmat &m2, cmat &out);
template ITPP_EXPORT void elem_mult_out(const imat &m1, const imat &m2, imat &out);
template ITPP_EXPORT void elem_mult_out(const smat &m1, const smat &m2, smat &out);
template ITPP_EXPORT void elem_mult_out(const bmat &m1, const bmat &m2, bmat &out);
template ITPP_EXPORT void elem_mult_out(const mat &m1, const mat &m2,
const mat &m3, mat &out);
template ITPP_EXPORT void elem_mult_out(const cmat &m1, const cmat &m2,
const cmat &m3, cmat &out);
template ITPP_EXPORT void elem_mult_out(const imat &m1, const imat &m2,
const imat &m3, imat &out);
template ITPP_EXPORT void elem_mult_out(const smat &m1, const smat &m2,
const smat &m3, smat &out);
template ITPP_EXPORT void elem_mult_out(const bmat &m1, const bmat &m2,
const bmat &m3, bmat &out);
template ITPP_EXPORT void elem_mult_out(const mat &m1, const mat &m2, const mat &m3,
const mat &m4, mat &out);
template ITPP_EXPORT void elem_mult_out(const cmat &m1, const cmat &m2,
const cmat &m3, const cmat &m4, cmat &out);
template ITPP_EXPORT void elem_mult_out(const imat &m1, const imat &m2,
const imat &m3, const imat &m4, imat &out);
template ITPP_EXPORT void elem_mult_out(const smat &m1, const smat &m2,
const smat &m3, const smat &m4, smat &out);
template ITPP_EXPORT void elem_mult_out(const bmat &m1, const bmat &m2,
const bmat &m3, const bmat &m4, bmat &out);
template ITPP_EXPORT void elem_mult_inplace(const mat &m1, mat &m2);
template ITPP_EXPORT void elem_mult_inplace(const cmat &m1, cmat &m2);
template ITPP_EXPORT void elem_mult_inplace(const imat &m1, imat &m2);
template ITPP_EXPORT void elem_mult_inplace(const smat &m1, smat &m2);
template ITPP_EXPORT void elem_mult_inplace(const bmat &m1, bmat &m2);
template ITPP_EXPORT double elem_mult_sum(const mat &m1, const mat &m2);
template ITPP_EXPORT std::complex<double> elem_mult_sum(const cmat &m1, const cmat &m2);
template ITPP_EXPORT int elem_mult_sum(const imat &m1, const imat &m2);
template ITPP_EXPORT short elem_mult_sum(const smat &m1, const smat &m2);
template ITPP_EXPORT bin elem_mult_sum(const bmat &m1, const bmat &m2);
// division operator
template ITPP_EXPORT mat operator/(double t, const mat &m);
template ITPP_EXPORT cmat operator/(std::complex<double> t, const cmat &m);
template ITPP_EXPORT imat operator/(int t, const imat &m);
template ITPP_EXPORT smat operator/(short t, const smat &m);
template ITPP_EXPORT bmat operator/(bin t, const bmat &m);
template ITPP_EXPORT mat operator/(const mat &m, double t);
template ITPP_EXPORT cmat operator/(const cmat &m, std::complex<double> t);
template ITPP_EXPORT imat operator/(const imat &m, int t);
template ITPP_EXPORT smat operator/(const smat &m, short t);
template ITPP_EXPORT bmat operator/(const bmat &m, bin t);
// elementwise division
template ITPP_EXPORT mat elem_div(const mat &m1, const mat &m2);
template ITPP_EXPORT cmat elem_div(const cmat &m1, const cmat &m2);
template ITPP_EXPORT imat elem_div(const imat &m1, const imat &m2);
template ITPP_EXPORT smat elem_div(const smat &m1, const smat &m2);
template ITPP_EXPORT bmat elem_div(const bmat &m1, const bmat &m2);
template ITPP_EXPORT void elem_div_out(const mat &m1, const mat &m2, mat &out);
template ITPP_EXPORT void elem_div_out(const cmat &m1, const cmat &m2, cmat &out);
template ITPP_EXPORT void elem_div_out(const imat &m1, const imat &m2, imat &out);
template ITPP_EXPORT void elem_div_out(const smat &m1, const smat &m2, smat &out);
template ITPP_EXPORT void elem_div_out(const bmat &m1, const bmat &m2, bmat &out);
template ITPP_EXPORT double elem_div_sum(const mat &m1, const mat &m2);
template ITPP_EXPORT std::complex<double> elem_div_sum(const cmat &m1,
const cmat &m2);
template ITPP_EXPORT int elem_div_sum(const imat &m1, const imat &m2);
template ITPP_EXPORT short elem_div_sum(const smat &m1, const smat &m2);
template ITPP_EXPORT bin elem_div_sum(const bmat &m1, const bmat &m2);
// concatenation
template ITPP_EXPORT mat concat_horizontal(const mat &m1, const mat &m2);
template ITPP_EXPORT cmat concat_horizontal(const cmat &m1, const cmat &m2);
template ITPP_EXPORT imat concat_horizontal(const imat &m1, const imat &m2);
template ITPP_EXPORT smat concat_horizontal(const smat &m1, const smat &m2);
template ITPP_EXPORT bmat concat_horizontal(const bmat &m1, const bmat &m2);
template ITPP_EXPORT mat concat_vertical(const mat &m1, const mat &m2);
template ITPP_EXPORT cmat concat_vertical(const cmat &m1, const cmat &m2);
template ITPP_EXPORT imat concat_vertical(const imat &m1, const imat &m2);
template ITPP_EXPORT smat concat_vertical(const smat &m1, const smat &m2);
template ITPP_EXPORT bmat concat_vertical(const bmat &m1, const bmat &m2);
// I/O streams
template ITPP_EXPORT std::ostream &operator<<(std::ostream &os, const mat &m);
template ITPP_EXPORT std::ostream &operator<<(std::ostream &os, const cmat &m);
template ITPP_EXPORT std::ostream &operator<<(std::ostream &os, const imat &m);
template ITPP_EXPORT std::ostream &operator<<(std::ostream &os, const smat &m);
template ITPP_EXPORT std::ostream &operator<<(std::ostream &os, const bmat &m);
template ITPP_EXPORT std::istream &operator>>(std::istream &is, mat &m);
template ITPP_EXPORT std::istream &operator>>(std::istream &is, cmat &m);
template ITPP_EXPORT std::istream &operator>>(std::istream &is, imat &m);
template ITPP_EXPORT std::istream &operator>>(std::istream &is, smat &m);
template ITPP_EXPORT std::istream &operator>>(std::istream &is, bmat &m);
} // namespace itpp
//! \endcond
|