root/win32/itpp-4.0.1/itpp/base/specmat.h @ 47

Revision 35, 10.1 kB (checked in by mido, 17 years ago)

zasadni zmeny ve /win32

Line 
1/*!
2 * \file
3 * \brief Definitions of special vectors and matrices
4 * \author Tony Ottosson, Tobias Ringstrom, Pal Frenger, Adam Piatyszek and Erik G. Larsson
5 *
6 * -------------------------------------------------------------------------
7 *
8 * IT++ - C++ library of mathematical, signal processing, speech processing,
9 *        and communications classes and functions
10 *
11 * Copyright (C) 1995-2007  (see AUTHORS file for a list of contributors)
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 *
27 * -------------------------------------------------------------------------
28 */
29
30#ifndef SPECMAT_H
31#define SPECMAT_H
32
33#include <itpp/base/vec.h>
34#include <itpp/base/mat.h>
35
36
37namespace itpp {
38
39  /*!
40    \brief Return a integer vector with indicies where bvec == 1
41    \ingroup miscfunc
42  */
43  ivec find(const bvec &invector);
44
45  /*!
46    \addtogroup specmat
47  */
48
49  //!\addtogroup specmat
50  //!@{
51
52  //! A float vector of ones
53  vec ones(int size);
54  //! A Binary vector of ones
55  bvec ones_b(int size);
56  //! A Int vector of ones
57  ivec ones_i(int size);
58  //! A float Complex vector of ones
59  cvec ones_c(int size);
60
61  //! A float (rows,cols)-matrix of ones
62  mat ones(int rows, int cols);
63  //! A Binary (rows,cols)-matrix of ones
64  bmat ones_b(int rows, int cols);
65  //! A Int (rows,cols)-matrix of ones
66  imat ones_i(int rows, int cols);
67  //! A Double Complex (rows,cols)-matrix of ones
68  cmat ones_c(int rows, int cols);
69
70  //! A Double vector of zeros
71  vec zeros(int size);
72  //! A Binary vector of zeros
73  bvec zeros_b(int size);
74  //! A Int vector of zeros
75  ivec zeros_i(int size);
76  //! A Double Complex vector of zeros
77  cvec zeros_c(int size);
78
79  //! A Double (rows,cols)-matrix of zeros
80  mat zeros(int rows, int cols);
81  //! A Binary (rows,cols)-matrix of zeros
82  bmat zeros_b(int rows, int cols);
83  //! A Int (rows,cols)-matrix of zeros
84  imat zeros_i(int rows, int cols);
85  //! A Double Complex (rows,cols)-matrix of zeros
86  cmat zeros_c(int rows, int cols);
87
88  //! A Double (size,size) unit matrix
89  mat eye(int size);
90  //! A Binary (size,size) unit matrix
91  bmat eye_b(int size);
92  //! A Int (size,size) unit matrix
93  imat eye_i(int size);
94  //! A Double Complex (size,size) unit matrix
95  cmat eye_c(int size);
96  //! A non-copying version of the eye function.
97  template <class T>
98    void eye(int size, Mat<T> &m);
99
100  //! Impulse vector
101  vec impulse(int size);
102  //! Linspace (works in the same way as the matlab version)
103  vec linspace(double from, double to, int length = 100);
104  /*! \brief Zig-zag space function (variation on linspace)
105
106  This function is a variation on linspace().  It traverses the points
107  in different order. For example
108  \code
109  zigzag_space(-5,5,3)
110  \endcode
111  gives the vector
112  \code
113  [-5 5 0 -2.5 2.5 -3.75 -1.25 1.25 3.75]
114  \endcode
115  and
116  \code
117  zigzag_space(-5,5,4)
118  \endcode
119  gives
120  the vector
121  \code
122  [-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]
123  \endcode
124  and so on.
125
126  I.e. the function samples the interval [t0,t1] with finer and finer
127  density and with points uniformly distributed over the interval,
128  rather than from left to right (as does linspace).
129
130  The result is a vector of length 1+2^K.
131  */
132  vec zigzag_space(double t0, double t1, int K=5);
133
134  /*!
135   * \brief Hadamard matrix
136   *
137   * This function constructs a \a size by \a size Hadammard matrix, where
138   * \a size is a power of 2.
139   */
140  imat hadamard(int size);
141
142  /*!
143    \brief Jacobsthal matrix.
144
145    Constructs an p by p matrix Q where p is a prime (not checked).
146    The elements in Q {qij} is given by qij=X(j-i), where X(x) is the
147    Legendre symbol given as:
148
149    <ul>
150    <li> X(x)=0 if x is a multiple of p, </li>
151    <li> X(x)=1 if x is a quadratic residue modulo p, </li>
152    <li> X(x)=-1 if x is a quadratic nonresidue modulo p. </li>
153    </ul>
154
155    See Wicker "Error Control Systems for digital communication and storage", p. 134
156    for more information on these topics. Do not check that p is a prime.
157  */
158  imat jacobsthal(int p);
159
160  /*!
161    \brief Conference matrix.
162
163    Constructs an n by n matrix C, where n=p^m+1=2 (mod 4) and p is a odd prime (not checked).
164    This code only work with m=1, that is n=p+1 and p odd prime. The valid sizes
165    of n is then n=6, 14, 18, 30, 38, ... (and not 10, 26, ...).
166    C has the property that C*C'=(n-1)I, that is it has orthogonal rows and columns
167    in the same way as Hadamard matricies. However, one element in each row (on the
168    diagonal) is zeros. The others are {-1,+1}.
169
170    For more details see pp. 55-58 in MacWilliams & Sloane "The theory of error correcting codes",
171    North-Holland, 1977.
172  */
173  imat conference(int n);
174
175  /*!
176    \brief Computes the Hermitian Toeplitz matrix.
177
178    Return the Toeplitz matrix constructed given the first column C,
179    and (optionally) the first row R. If the first element of C is not
180    the same as the first element of R, the first element of C is
181    used.  If the second argument is omitted, the first row is taken
182    to be the same as the first column.
183
184    A square Toeplitz matrix has the form:
185    \verbatim
186          c(0)    r(1)     r(2)   ...   r(n)
187          c(1)*   c(0)     r(1)        r(n-1)
188          c(2)*   c(1)*    c(0)        r(n-2)
189           .                             .
190           .                             .
191           .                             .
192
193          c(n)*  c(n-1)*  c(n-2)* ...   c(0)
194    \endverbatim
195  */
196  cmat toeplitz(const cvec &c, const cvec &r);
197  //! Computes the Hermitian Toeplitz matrix.
198  cmat toeplitz(const cvec &c);
199  //! Computes the Hermitian Toeplitz matrix.
200  mat toeplitz(const vec &c, const vec &r);
201  //! Computes the Hermitian Toeplitz matrix.
202  mat toeplitz(const vec &c);
203
204  //!@}
205
206
207  /*!
208    \brief Create a rotation matrix that rotates the given plane \c angle radians. Note that the order of the planes are important!
209    \ingroup miscfunc
210  */
211  mat rotation_matrix(int dim, int plane1, int plane2, double angle);
212
213  /*!
214    \brief Calcualte the Householder vector
215    \ingroup miscfunc
216  */
217  void house(const vec &x, vec &v, double &beta);
218
219  /*!
220    \brief Calculate the Givens rotation values
221    \ingroup miscfunc
222  */
223  void givens(double a, double b, double &c, double &s);
224
225  /*!
226    \brief Calculate the Givens rotation matrix
227    \ingroup miscfunc
228  */
229  void givens(double a, double b, mat &m);
230
231  /*!
232    \brief Calculate the Givens rotation matrix
233    \ingroup miscfunc
234  */
235  mat givens(double a, double b);
236
237  /*!
238    \brief Calculate the transposed Givens rotation matrix
239    \ingroup miscfunc
240  */
241  void givens_t(double a, double b, mat &m);
242
243  /*!
244    \brief Calculate the transposed Givens rotation matrix
245    \ingroup miscfunc
246  */
247  mat givens_t(double a, double b);
248
249  /*!
250    \relates Vec
251    \brief Vector of length 1
252  */
253  template <class T>
254    Vec<T> vec_1(T v0)
255    {
256      Vec<T> v(1);
257      v(0) = v0;
258      return v;
259    }
260
261  /*!
262    \relates Vec
263    \brief Vector of length 2
264  */
265  template <class T>
266    Vec<T> vec_2(T v0, T v1)
267    {
268      Vec<T> v(2);
269      v(0) = v0;
270      v(1) = v1;
271      return v;
272    }
273
274  /*!
275    \relates Vec
276    \brief Vector of length 3
277  */
278  template <class T>
279    Vec<T> vec_3(T v0, T v1, T v2)
280    {
281      Vec<T> v(3);
282      v(0) = v0;
283      v(1) = v1;
284      v(2) = v2;
285      return v;
286    }
287
288  /*!
289    \relates Mat
290    \brief Matrix of size 1 by 1
291  */
292  template <class T>
293    Mat<T> mat_1x1(T m00)
294    {
295      Mat<T> m(1,1);
296      m(0,0) = m00;
297      return m;
298    }
299
300  /*!
301    \relates Mat
302    \brief Matrix of size 1 by 2
303  */
304  template <class T>
305    Mat<T> mat_1x2(T m00, T m01)
306    {
307      Mat<T> m(1,2);
308      m(0,0) = m00; m(0,1) = m01;
309      return m;
310    }
311
312  /*!
313    \relates Mat
314    \brief Matrix of size 2 by 1
315  */
316  template <class T>
317    Mat<T> mat_2x1(T m00,
318                   T m10)
319    {
320      Mat<T> m(2,1);
321      m(0,0) = m00;
322      m(1,0) = m10;
323      return m;
324    }
325
326  /*!
327    \relates Mat
328    \brief Matrix of size 2 by 2
329  */
330  template <class T>
331    Mat<T> mat_2x2(T m00, T m01,
332                   T m10, T m11)
333    {
334      Mat<T> m(2,2);
335      m(0,0) = m00; m(0,1) = m01;
336      m(1,0) = m10; m(1,1) = m11;
337      return m;
338    }
339
340  /*!
341    \relates Mat
342    \brief Matrix of size 1 by 3
343  */
344  template <class T>
345    Mat<T> mat_1x3(T m00, T m01, T m02)
346    {
347      Mat<T> m(1,3);
348      m(0,0) = m00; m(0,1) = m01; m(0,2) = m02;
349      return m;
350    }
351
352  /*!
353    \relates Mat
354    \brief Matrix of size 3 by 1
355  */
356  template <class T>
357    Mat<T> mat_3x1(T m00,
358                   T m10,
359                   T m20)
360    {
361      Mat<T> m(3,1);
362      m(0,0) = m00;
363      m(1,0) = m10;
364      m(2,0) = m20;
365      return m;
366    }
367
368  /*!
369    \relates Mat
370    \brief Matrix of size 2 by 3
371  */
372  template <class T>
373    Mat<T> mat_2x3(T m00, T m01, T m02,
374                   T m10, T m11, T m12)
375    {
376      Mat<T> m(2,3);
377      m(0,0) = m00; m(0,1) = m01; m(0,2) = m02;
378      m(1,0) = m10; m(1,1) = m11; m(1,2) = m12;
379      return m;
380    }
381
382  /*!
383    \relates Mat
384    \brief Matrix of size 3 by 2
385  */
386  template <class T>
387    Mat<T> mat_3x2(T m00, T m01,
388                   T m10, T m11,
389                   T m20, T m21)
390    {
391      Mat<T> m(3,2);
392      m(0,0) = m00; m(0,1) = m01;
393      m(1,0) = m10; m(1,1) = m11;
394      m(2,0) = m20; m(2,1) = m21;
395      return m;
396    }
397
398  /*!
399    \relates Mat
400    \brief Matrix of size 3 by 3
401  */
402  template <class T>
403    Mat<T> mat_3x3(T m00, T m01, T m02,
404                   T m10, T m11, T m12,
405                   T m20, T m21, T m22)
406    {
407      Mat<T> m(3,3);
408      m(0,0) = m00; m(0,1) = m01; m(0,2) = m02;
409      m(1,0) = m10; m(1,1) = m11; m(1,2) = m12;
410      m(2,0) = m20; m(2,1) = m21; m(2,2) = m22;
411      return m;
412    }
413
414} //namespace itpp
415
416#endif // #ifndef SPECMAT_H
Note: See TracBrowser for help on using the browser.