File: specmat.h

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 (416 lines) | stat: -rw-r--r-- 10,323 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
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
/*!
 * \file
 * \brief Definitions of special vectors and matrices
 * \author Tony Ottosson, Tobias Ringstrom, Pal Frenger, Adam Piatyszek and Erik G. Larsson
 *
 * -------------------------------------------------------------------------
 *
 * 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
 *
 * -------------------------------------------------------------------------
 */

#ifndef SPECMAT_H
#define SPECMAT_H

#include <itpp/base/vec.h>
#include <itpp/base/mat.h>


namespace itpp {

  /*!
    \brief Return a integer vector with indicies where bvec == 1
    \ingroup miscfunc
  */
  ivec find(const bvec &invector);

  /*!
    \addtogroup specmat
  */

  //!\addtogroup specmat
  //!@{

  //! A float vector of ones
  vec ones(int size);
  //! A Binary vector of ones
  bvec ones_b(int size);
  //! A Int vector of ones
  ivec ones_i(int size);
  //! A float Complex vector of ones
  cvec ones_c(int size);

  //! A float (rows,cols)-matrix of ones
  mat ones(int rows, int cols);
  //! A Binary (rows,cols)-matrix of ones
  bmat ones_b(int rows, int cols);
  //! A Int (rows,cols)-matrix of ones
  imat ones_i(int rows, int cols);
  //! A Double Complex (rows,cols)-matrix of ones
  cmat ones_c(int rows, int cols);

  //! A Double vector of zeros
  vec zeros(int size);
  //! A Binary vector of zeros
  bvec zeros_b(int size);
  //! A Int vector of zeros
  ivec zeros_i(int size);
  //! A Double Complex vector of zeros
  cvec zeros_c(int size);

  //! A Double (rows,cols)-matrix of zeros
  mat zeros(int rows, int cols);
  //! A Binary (rows,cols)-matrix of zeros
  bmat zeros_b(int rows, int cols);
  //! A Int (rows,cols)-matrix of zeros
  imat zeros_i(int rows, int cols);
  //! A Double Complex (rows,cols)-matrix of zeros
  cmat zeros_c(int rows, int cols);

  //! A Double (size,size) unit matrix
  mat eye(int size);
  //! A Binary (size,size) unit matrix
  bmat eye_b(int size);
  //! A Int (size,size) unit matrix
  imat eye_i(int size);
  //! A Double Complex (size,size) unit matrix
  cmat eye_c(int size);
  //! A non-copying version of the eye function.
  template <class T>
    void eye(int size, Mat<T> &m);

  //! Impulse vector
  vec impulse(int size);
  //! Linspace (works in the same way as the matlab version)
  vec linspace(double from, double to, int length = 100);
  /*! \brief Zig-zag space function (variation on linspace)

  This function is a variation on linspace().  It traverses the points
  in different order. For example
  \code
  zigzag_space(-5,5,3)
  \endcode
  gives the vector
  \code
  [-5 5 0 -2.5 2.5 -3.75 -1.25 1.25 3.75]
  \endcode
  and
  \code
  zigzag_space(-5,5,4)
  \endcode
  gives
  the vector
  \code
  [-5 5 0 -2.5 2.5 -3.75 -1.25 1.25 3.75 -4.375 -3.125 -1.875 -0.625 0.625 1.875 3.125 4.375]
  \endcode
  and so on.

  I.e. the function samples the interval [t0,t1] with finer and finer
  density and with points uniformly distributed over the interval,
  rather than from left to right (as does linspace).

  The result is a vector of length 1+2^K.
  */
  vec zigzag_space(double t0, double t1, int K=5);

  /*!
   * \brief Hadamard matrix
   *
   * This function constructs a \a size by \a size Hadammard matrix, where
   * \a size is a power of 2.
   */
  imat hadamard(int size);

  /*!
    \brief Jacobsthal matrix.

    Constructs an p by p matrix Q where p is a prime (not checked).
    The elements in Q {qij} is given by qij=X(j-i), where X(x) is the
    Legendre symbol given as:

    <ul>
    <li> X(x)=0 if x is a multiple of p, </li>
    <li> X(x)=1 if x is a quadratic residue modulo p, </li>
    <li> X(x)=-1 if x is a quadratic nonresidue modulo p. </li>
    </ul>

    See Wicker "Error Control Systems for digital communication and storage", p. 134
    for more information on these topics. Do not check that p is a prime.
  */
  imat jacobsthal(int p);

  /*!
    \brief Conference matrix.

    Constructs an n by n matrix C, where n=p^m+1=2 (mod 4) and p is a odd prime (not checked).
    This code only work with m=1, that is n=p+1 and p odd prime. The valid sizes
    of n is then n=6, 14, 18, 30, 38, ... (and not 10, 26, ...).
    C has the property that C*C'=(n-1)I, that is it has orthogonal rows and columns
    in the same way as Hadamard matricies. However, one element in each row (on the
    diagonal) is zeros. The others are {-1,+1}.

    For more details see pp. 55-58 in MacWilliams & Sloane "The theory of error correcting codes",
    North-Holland, 1977.
  */
  imat conference(int n);

  /*!
    \brief Computes the Hermitian Toeplitz matrix.

    Return the Toeplitz matrix constructed given the first column C,
    and (optionally) the first row R. If the first element of C is not
    the same as the first element of R, the first element of C is
    used.  If the second argument is omitted, the first row is taken
    to be the same as the first column.

    A square Toeplitz matrix has the form:
    \verbatim
          c(0)    r(1)     r(2)   ...   r(n)
          c(1)*   c(0)     r(1)        r(n-1)
          c(2)*   c(1)*    c(0)        r(n-2)
           .                             .
           .                             .
           .                             .

          c(n)*  c(n-1)*  c(n-2)* ...   c(0)
    \endverbatim
  */
  cmat toeplitz(const cvec &c, const cvec &r);
  //! Computes the Hermitian Toeplitz matrix.
  cmat toeplitz(const cvec &c);
  //! Computes the Hermitian Toeplitz matrix.
  mat toeplitz(const vec &c, const vec &r);
  //! Computes the Hermitian Toeplitz matrix.
  mat toeplitz(const vec &c);

  //!@}


  /*!
    \brief Create a rotation matrix that rotates the given plane \c angle radians. Note that the order of the planes are important!
    \ingroup miscfunc
  */
  mat rotation_matrix(int dim, int plane1, int plane2, double angle);

  /*!
    \brief Calcualte the Householder vector
    \ingroup miscfunc
  */
  void house(const vec &x, vec &v, double &beta);

  /*!
    \brief Calculate the Givens rotation values
    \ingroup miscfunc
  */
  void givens(double a, double b, double &c, double &s);

  /*!
    \brief Calculate the Givens rotation matrix
    \ingroup miscfunc
  */
  void givens(double a, double b, mat &m);

  /*!
    \brief Calculate the Givens rotation matrix
    \ingroup miscfunc
  */
  mat givens(double a, double b);

  /*!
    \brief Calculate the transposed Givens rotation matrix
    \ingroup miscfunc
  */
  void givens_t(double a, double b, mat &m);

  /*!
    \brief Calculate the transposed Givens rotation matrix
    \ingroup miscfunc
  */
  mat givens_t(double a, double b);

  /*!
    \relates Vec
    \brief Vector of length 1
  */
  template <class T>
    Vec<T> vec_1(T v0)
    {
      Vec<T> v(1);
      v(0) = v0;
      return v;
    }

  /*!
    \relates Vec
    \brief Vector of length 2
  */
  template <class T>
    Vec<T> vec_2(T v0, T v1)
    {
      Vec<T> v(2);
      v(0) = v0;
      v(1) = v1;
      return v;
    }

  /*!
    \relates Vec
    \brief Vector of length 3
  */
  template <class T>
    Vec<T> vec_3(T v0, T v1, T v2)
    {
      Vec<T> v(3);
      v(0) = v0;
      v(1) = v1;
      v(2) = v2;
      return v;
    }

  /*!
    \relates Mat
    \brief Matrix of size 1 by 1
  */
  template <class T>
    Mat<T> mat_1x1(T m00)
    {
      Mat<T> m(1,1);
      m(0,0) = m00;
      return m;
    }

  /*!
    \relates Mat
    \brief Matrix of size 1 by 2
  */
  template <class T>
    Mat<T> mat_1x2(T m00, T m01)
    {
      Mat<T> m(1,2);
      m(0,0) = m00; m(0,1) = m01;
      return m;
    }

  /*!
    \relates Mat
    \brief Matrix of size 2 by 1
  */
  template <class T>
    Mat<T> mat_2x1(T m00,
		   T m10)
    {
      Mat<T> m(2,1);
      m(0,0) = m00;
      m(1,0) = m10;
      return m;
    }

  /*!
    \relates Mat
    \brief Matrix of size 2 by 2
  */
  template <class T>
    Mat<T> mat_2x2(T m00, T m01,
		   T m10, T m11)
    {
      Mat<T> m(2,2);
      m(0,0) = m00; m(0,1) = m01;
      m(1,0) = m10; m(1,1) = m11;
      return m;
    }

  /*!
    \relates Mat
    \brief Matrix of size 1 by 3
  */
  template <class T>
    Mat<T> mat_1x3(T m00, T m01, T m02)
    {
      Mat<T> m(1,3);
      m(0,0) = m00; m(0,1) = m01; m(0,2) = m02;
      return m;
    }

  /*!
    \relates Mat
    \brief Matrix of size 3 by 1
  */
  template <class T>
    Mat<T> mat_3x1(T m00,
		   T m10,
		   T m20)
    {
      Mat<T> m(3,1);
      m(0,0) = m00;
      m(1,0) = m10;
      m(2,0) = m20;
      return m;
    }

  /*!
    \relates Mat
    \brief Matrix of size 2 by 3
  */
  template <class T>
    Mat<T> mat_2x3(T m00, T m01, T m02,
		   T m10, T m11, T m12)
    {
      Mat<T> m(2,3);
      m(0,0) = m00; m(0,1) = m01; m(0,2) = m02;
      m(1,0) = m10; m(1,1) = m11; m(1,2) = m12;
      return m;
    }

  /*!
    \relates Mat
    \brief Matrix of size 3 by 2
  */
  template <class T>
    Mat<T> mat_3x2(T m00, T m01,
		   T m10, T m11,
		   T m20, T m21)
    {
      Mat<T> m(3,2);
      m(0,0) = m00; m(0,1) = m01;
      m(1,0) = m10; m(1,1) = m11;
      m(2,0) = m20; m(2,1) = m21;
      return m;
    }

  /*!
    \relates Mat
    \brief Matrix of size 3 by 3
  */
  template <class T>
    Mat<T> mat_3x3(T m00, T m01, T m02,
		   T m10, T m11, T m12,
		   T m20, T m21, T m22)
    {
      Mat<T> m(3,3);
      m(0,0) = m00; m(0,1) = m01; m(0,2) = m02;
      m(1,0) = m10; m(1,1) = m11; m(1,2) = m12;
      m(2,0) = m20; m(2,1) = m21; m(2,2) = m22;
      return m;
    }

} //namespace itpp

#endif // #ifndef SPECMAT_H