File: mat.cpp

package info (click to toggle)
libitpp 4.0.4-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 7,520 kB
  • ctags: 6,341
  • sloc: cpp: 51,608; sh: 9,248; makefile: 636; fortran: 8
file content (347 lines) | stat: -rw-r--r-- 13,553 bytes parent folder | download
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
/*!
 * \file
 * \brief Matrix Class Implementation
 * \author Tony Ottosson, Tobias Ringstrom, Adam Piatyszek and Conrad Sanderson
 *
 * -------------------------------------------------------------------------
 *
 * IT++ - C++ library of mathematical, signal processing, speech processing,
 *        and communications classes and functions
 *
 * Copyright (C) 1995-2008  (see AUTHORS file for a list of contributors)
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * -------------------------------------------------------------------------
 */

#include <itpp/base/mat.h>

#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;
  }

  template<>
  mat operator*(const mat &m1, const mat &m2)
  {
    it_assert_debug(m1.no_cols == m2.no_rows,"mat::operator*(): Wrong sizes");
    mat r(m1.no_rows, m2.no_cols);
    double alpha = 1.0;
    double beta = 0.0;
    char trans = 'n';
    blas::dgemm_(&trans, &trans, &m1.no_rows, &m2.no_cols, &m1.no_cols, &alpha,
		 m1.data, &m1.no_rows, m2.data, &m2.no_rows, &beta, r.data,
		 &r.no_rows);
    return r;
  }

  template<>
  cmat operator*(const cmat &m1, const cmat &m2)
  {
    it_assert_debug(m1.no_cols == m2.no_rows,"cmat::operator*(): Wrong sizes");
    cmat r(m1.no_rows, m2.no_cols);
    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.no_rows, &m2.no_cols, &m1.no_cols, &alpha,
		 m1.data, &m1.no_rows, m2.data, &m2.no_rows, &beta, r.data,
		 &r.no_rows);
    return r;
  }

  template<>
  vec operator*(const mat &m, const vec &v)
  {
    it_assert_debug(m.no_cols == v.size(), "mat::operator*(): Wrong sizes");
    vec r(m.no_rows);
    double alpha = 1.0;
    double beta = 0.0;
    char trans = 'n';
    int incr = 1;
    blas::dgemv_(&trans, &m.no_rows, &m.no_cols, &alpha, m.data, &m.no_rows,
		 v._data(), &incr, &beta, r._data(), &incr);
    return r;
  }

  template<>
  cvec operator*(const cmat &m, const cvec &v)
  {
    it_assert_debug(m.no_cols == v.size(), "cmat::operator*(): Wrong sizes");
    cvec r(m.no_rows);
    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.no_rows, &m.no_cols, &alpha, m.data, &m.no_rows,
		 v._data(), &incr, &beta, r._data(), &incr);
    return r;
  }

#endif // HAVE_BLAS


  //---------------------------------------------------------------------
  // Instantiations
  //---------------------------------------------------------------------

  // class instantiations

  template class Mat<double>;
  template class Mat<std::complex<double> >;
  template class Mat<int>;
  template class Mat<short int>;
  template class Mat<bin>;

  // addition operators

  template mat operator+(const mat &m1, const mat &m2);
  template cmat operator+(const cmat &m1, const cmat &m2);
  template imat operator+(const imat &m1, const imat &m2);
  template smat operator+(const smat &m1, const smat &m2);
  template bmat operator+(const bmat &m1, const bmat &m2);

  template mat operator+(const mat &m, double t);
  template cmat operator+(const cmat &m, std::complex<double> t);
  template imat operator+(const imat &m, int t);
  template smat operator+(const smat &m, short t);
  template bmat operator+(const bmat &m, bin t);

  template mat operator+(double t, const mat &m);
  template cmat operator+(std::complex<double> t, const cmat &m);
  template imat operator+(int t, const imat &m);
  template smat operator+(short t, const smat &m);
  template bmat operator+(bin t, const bmat &m);

  // subraction operators

  template mat operator-(const mat &m1, const mat &m2);
  template cmat operator-(const cmat &m1, const cmat &m2);
  template imat operator-(const imat &m1, const imat &m2);
  template smat operator-(const smat &m1, const smat &m2);
  template bmat operator-(const bmat &m1, const bmat &m2);

  template mat operator-(const mat &m, double t);
  template cmat operator-(const cmat &m, std::complex<double> t);
  template imat operator-(const imat &m, int t);
  template smat operator-(const smat &m, short t);
  template bmat operator-(const bmat &m, bin t);

  template mat operator-(double t, const mat &m);
  template cmat operator-(std::complex<double> t, const cmat &m);
  template imat operator-(int t, const imat &m);
  template smat operator-(short t, const smat &m);
  template bmat operator-(bin t, const bmat &m);

  // unary minus

  template mat operator-(const mat &m);
  template cmat operator-(const cmat &m);
  template imat operator-(const imat &m);
  template smat operator-(const smat &m);
  template bmat operator-(const bmat &m);

  // multiplication operators

#if !defined(HAVE_BLAS)
  template mat operator*(const mat &m1, const mat &m2);
  template cmat operator*(const cmat &m1, const cmat &m2);
#endif
  template imat operator*(const imat &m1, const imat &m2);
  template smat operator*(const smat &m1, const smat &m2);
  template bmat operator*(const bmat &m1, const bmat &m2);

#if !defined(HAVE_BLAS)
  template vec operator*(const mat &m, const vec &v);
  template cvec operator*(const cmat &m, const cvec &v);
#endif

  template ivec operator*(const imat &m, const ivec &v);
  template svec operator*(const smat &m, const svec &v);
  template bvec operator*(const bmat &m, const bvec &v);

  template mat operator*(const vec &v, const mat &m);
  template cmat operator*(const cvec &v, const cmat &m);
  template imat operator*(const ivec &v, const imat &m);
  template smat operator*(const svec &v, const smat &m);
  template bmat operator*(const bvec &v, const bmat &m);

  template mat operator*(const mat &m, double t);
  template cmat operator*(const cmat &m, std::complex<double> t);
  template imat operator*(const imat &m, int t);
  template smat operator*(const smat &m, short t);
  template bmat operator*(const bmat &m, bin t);

  template mat operator*(double t, const mat &m);
  template cmat operator*(std::complex<double> t, const cmat &m);
  template imat operator*(int t, const imat &m);
  template smat operator*(short t, const smat &m);
  template bmat operator*(bin t, const bmat &m);

  // elementwise multiplication

  template mat elem_mult(const mat &m1, const mat &m2);
  template cmat elem_mult(const cmat &m1, const cmat &m2);
  template imat elem_mult(const imat &m1, const imat &m2);
  template smat elem_mult(const smat &m1, const smat &m2);
  template bmat elem_mult(const bmat &m1, const bmat &m2);

  template void elem_mult_out(const mat &m1, const mat &m2, mat &out);
  template void elem_mult_out(const cmat &m1, const cmat &m2, cmat &out);
  template void elem_mult_out(const imat &m1, const imat &m2, imat &out);
  template void elem_mult_out(const smat &m1, const smat &m2, smat &out);
  template void elem_mult_out(const bmat &m1, const bmat &m2, bmat &out);

  template void elem_mult_out(const mat &m1, const mat &m2,
                              const mat &m3, mat &out);
  template void elem_mult_out(const cmat &m1, const cmat &m2,
                              const cmat &m3, cmat &out);
  template void elem_mult_out(const imat &m1, const imat &m2,
                              const imat &m3, imat &out);
  template void elem_mult_out(const smat &m1, const smat &m2,
                              const smat &m3, smat &out);
  template void elem_mult_out(const bmat &m1, const bmat &m2,
                              const bmat &m3, bmat &out);

  template void elem_mult_out(const mat &m1, const mat &m2, const mat &m3,
                              const mat &m4, mat &out);
  template void elem_mult_out(const cmat &m1, const cmat &m2,
                              const cmat &m3, const cmat &m4, cmat &out);
  template void elem_mult_out(const imat &m1, const imat &m2,
                              const imat &m3, const imat &m4, imat &out);
  template void elem_mult_out(const smat &m1, const smat &m2,
                              const smat &m3, const smat &m4, smat &out);
  template void elem_mult_out(const bmat &m1, const bmat &m2,
                              const bmat &m3, const bmat &m4, bmat &out);

  template void elem_mult_inplace(const mat &m1, mat &m2);
  template void elem_mult_inplace(const cmat &m1, cmat &m2);
  template void elem_mult_inplace(const imat &m1, imat &m2);
  template void elem_mult_inplace(const smat &m1, smat &m2);
  template void elem_mult_inplace(const bmat &m1, bmat &m2);

  template double elem_mult_sum(const mat &m1, const mat &m2);
  template std::complex<double> elem_mult_sum(const cmat &m1, const cmat &m2);
  template int elem_mult_sum(const imat &m1, const imat &m2);
  template short elem_mult_sum(const smat &m1, const smat &m2);
  template bin elem_mult_sum(const bmat &m1, const bmat &m2);

  // division operator

  template mat operator/(const mat &m, double t);
  template cmat operator/(const cmat &m, std::complex<double> t);
  template imat operator/(const imat &m, int t);
  template smat operator/(const smat &m, short t);
  template bmat operator/(const bmat &m, bin t);

  // elementwise division

  template mat elem_div(const mat &m1, const mat &m2);
  template cmat elem_div(const cmat &m1, const cmat &m2);
  template imat elem_div(const imat &m1, const imat &m2);
  template smat elem_div(const smat &m1, const smat &m2);
  template bmat elem_div(const bmat &m1, const bmat &m2);

  template void elem_div_out(const mat &m1, const mat &m2, mat &out);
  template void elem_div_out(const cmat &m1, const cmat &m2, cmat &out);
  template void elem_div_out(const imat &m1, const imat &m2, imat &out);
  template void elem_div_out(const smat &m1, const smat &m2, smat &out);
  template void elem_div_out(const bmat &m1, const bmat &m2, bmat &out);

  template double elem_div_sum(const mat &m1, const mat &m2);
  template std::complex<double> elem_div_sum(const cmat &m1,
                                                    const cmat &m2);
  template int elem_div_sum(const imat &m1, const imat &m2);
  template short elem_div_sum(const smat &m1, const smat &m2);
  template bin elem_div_sum(const bmat &m1, const bmat &m2);

  // concatenation

  template mat concat_horizontal(const mat &m1, const mat &m2);
  template cmat concat_horizontal(const cmat &m1, const cmat &m2);
  template imat concat_horizontal(const imat &m1, const imat &m2);
  template smat concat_horizontal(const smat &m1, const smat &m2);
  template bmat concat_horizontal(const bmat &m1, const bmat &m2);

  template mat concat_vertical(const mat &m1, const mat &m2);
  template cmat concat_vertical(const cmat &m1, const cmat &m2);
  template imat concat_vertical(const imat &m1, const imat &m2);
  template smat concat_vertical(const smat &m1, const smat &m2);
  template bmat concat_vertical(const bmat &m1, const bmat &m2);

  // I/O streams

  template std::ostream &operator<<(std::ostream &os, const mat  &m);
  template std::ostream &operator<<(std::ostream &os, const cmat &m);
  template std::ostream &operator<<(std::ostream &os, const imat  &m);
  template std::ostream &operator<<(std::ostream &os, const smat  &m);
  template std::ostream &operator<<(std::ostream &os, const bmat  &m);

  template std::istream &operator>>(std::istream &is, mat  &m);
  template std::istream &operator>>(std::istream &is, cmat &m);
  template std::istream &operator>>(std::istream &is, imat  &m);
  template std::istream &operator>>(std::istream &is, smat  &m);
  template std::istream &operator>>(std::istream &is, bmat  &m);

} // namespace itpp

//! \endcond