vnl_complex_ops.hxx
Go to the documentation of this file.
1 // This is core/vnl/vnl_complex_ops.hxx
2 #ifndef vnl_complex_ops_hxx_
3 #define vnl_complex_ops_hxx_
4 //:
5 // \file
6 // \author fsm
7 // This is the implementation file for the following three header files:
8 // vnl_complexify.h vnl_real.h vnl_imag.h
9 
10 #include "vnl_complexify.h"
11 #include "vnl_real.h"
12 #include "vnl_imag.h"
13 
14 #include <cassert>
15 #ifdef _MSC_VER
16 # include <vcl_msvc_warnings.h>
17 #endif
18 
19 //-----------------------------------------------------------------------
20 
21 template <class T>
22 void
23 vnl_complexify(T const *src, std::complex<T> *dst, unsigned n)
24 {
25  for (unsigned i=0; i<n; ++i)
26  dst[i] = src[i];
27 }
28 
29 template <class T>
30 void
31 vnl_complexify(T const *re, T const *im, std::complex<T> *dst, unsigned n)
32 {
33  for (unsigned i=0; i<n; ++i)
34  dst[i] = std::complex<T>(re[i], im[i]);
35 }
36 
37 // Real Alone:
38 // - vnl_vector
39 // - vnl_vector_fixed -- in header
40 // - vnl_matrix
41 // - vnl_matrix_fixed -- in header
42 // - vnl_diag_matrix
43 // - vnl_diag_matrix_fixed -- in header
44 // - vnl_sym_matrix
45 
46 template <class T>
49 {
51  vnl_complexify(R.begin(), C.begin(), R.size());
52  return C;
53 }
54 
55 template <class T>
58 {
59  vnl_matrix<std::complex<T> > C(R.rows(), R.cols());
60  vnl_complexify(R.begin(), C.begin(), R.size());
61  return C;
62 }
63 
64 template <class T>
67 {
69  vnl_complexify(R.begin(), C.begin(), R.size());
70  return C;
71 }
72 
73 template <class T>
76 {
78  vnl_complexify(R.begin(), C.begin(), R.size());
79  return C;
80 }
81 
82 //----------------------------------------------------------------------
83 
84 // Real + Imaginary:
85 // - vnl_vector
86 // - vnl_vector_fixed -- in header
87 // - vnl_matrix
88 // - vnl_matrix_fixed -- in header
89 // - vnl_diag_matrix
90 // - vnl_diag_matrix_fixed -- in header
91 // - vnl_sym_matrix
92 
93 template <class T>
96 {
97  assert(R.size() == I.size());
99  vnl_complexify(R.begin(), I.begin(), C.begin(), R.size());
100  return C;
101 }
102 
103 template <class T>
106 {
107  assert(R.rows() == I.rows());
108  assert(R.cols() == I.cols());
109  vnl_matrix<std::complex<T> > C(R.rows(), R.cols());
110  vnl_complexify(R.begin(), I.begin(), C.begin(), R.size());
111  return C;
112 }
113 
114 template <class T>
117 {
118  assert(R.rows() == I.rows());
120  vnl_complexify(R.begin(), I.begin(), C.begin(), R.size());
121  return C;
122 }
123 
124 template <class T>
127 {
128  assert(R.rows() == I.rows());
130  vnl_complexify(R.begin(), I.begin(), C.begin(), R.size());
131  return C;
132 }
133 
134 //----------------------------------------------------------------------
135 // vnl_real()
136 // - vnl_vector
137 // - vnl_vector_fixed -- in header
138 // - vnl_matrix
139 // - vnl_matrix_fixed -- in header
140 // - vnl_diag_matrix
141 // - vnl_diag_matrix_fixed -- in header
142 // - vnl_sym_matrix
143 
144 //: Return array of real parts of complex array.
145 template <class T>
146 void
147  vnl_real(std::complex<T> const* C, T* R, unsigned int n)
148 {
149  for (unsigned int i=0; i<n; ++i)
150  R[i] = std::real(C[i]);
151 }
152 
153 //: Vector of real parts of vnl_vector<std::complex<T> >.
154 template <class T>
156  vnl_real(vnl_vector<std::complex<T> > const & C)
157 {
158  vnl_vector<T> R(C.size());
159  typename vnl_vector<std::complex<T> >::const_iterator cIt = C.begin();
160  typename vnl_vector<T>::iterator rIt = R.begin();
161  for (; cIt != C.end(); ++cIt, ++rIt)
162  *rIt = std::real(*cIt);
163  return R;
164 }
165 
166 //: Matrix of real parts of vnl_matrix<std::complex<T> >.
167 template <class T>
169  vnl_real(vnl_matrix<std::complex<T> > const& C)
170 {
171  vnl_matrix<T> R(C.rows(), C.columns());
172  typename vnl_matrix<std::complex<T> >::const_iterator cIt = C.begin();
173  typename vnl_matrix<T>::iterator rIt = R.begin();
174  for (; cIt != C.end(); ++cIt, ++rIt)
175  *rIt = std::real(*cIt);
176  return R;
177 }
178 
179 //: Matrix of real parts of vnl_diag_matrix<std::complex<T> >.
180 template <class T>
182  vnl_real(vnl_diag_matrix<std::complex<T> > const& C)
183 {
184  vnl_diag_matrix<T> R(C.rows());
185  typename vnl_diag_matrix<std::complex<T> >::const_iterator cIt = C.begin();
186  typename vnl_diag_matrix<T>::iterator rIt = R.begin();
187  for (; cIt != C.end(); ++cIt, ++rIt)
188  *rIt = std::real(*cIt);
189  return R;
190 }
191 
192 //: Matrix of real parts of vnl_sym_matrix<std::complex<T> >.
193 template <class T>
195  vnl_real(vnl_sym_matrix<std::complex<T> > const& C)
196 {
197  vnl_sym_matrix<T> R(C.rows());
198  typename vnl_sym_matrix<std::complex<T> >::const_iterator cIt = C.begin();
199  typename vnl_sym_matrix<T>::iterator rIt = R.begin();
200  for (; cIt != C.end(); ++cIt, ++rIt)
201  *rIt = std::real(*cIt);
202  return R;
203 }
204 
205 //----------------------------------------------------------------------
206 // vnl_imag()
207 // - vnl_vector
208 // - vnl_vector_fixed -- in header
209 // - vnl_matrix
210 // - vnl_matrix_fixed -- in header
211 // - vnl_diag_matrix
212 // - vnl_diag_matrix_fixed -- in header
213 // - vnl_sym_matrix
214 
215 //: Return array of imaginary parts of complex array.
216 template <class T>
217 void
218  vnl_imag(std::complex<T> const* C, T* I, unsigned int n)
219 {
220  for (unsigned int i=0; i<n; ++i)
221  I[i] = std::imag(C[i]);
222 }
223 
224 //: Vector of imaginary parts of vnl_vector<std::complex<T> >.
225 template <class T>
227  vnl_imag(vnl_vector<std::complex<T> > const & C)
228 {
229  vnl_vector<T> R(C.size());
230  typename vnl_vector<std::complex<T> >::const_iterator cIt = C.begin();
231  typename vnl_vector<T>::iterator rIt = R.begin();
232  for (; cIt != C.end(); ++cIt, ++rIt)
233  *rIt = std::imag(*cIt);
234  return R;
235 }
236 
237 //: Matrix of imaginary parts of vnl_matrix<std::complex<T> >.
238 template <class T>
240  vnl_imag(vnl_matrix<std::complex<T> > const& C)
241 {
242  vnl_matrix<T> R(C.rows(), C.columns());
243  typename vnl_matrix<std::complex<T> >::const_iterator cIt = C.begin();
244  typename vnl_matrix<T>::iterator rIt = R.begin();
245  for (; cIt != C.end(); ++cIt, ++rIt)
246  *rIt = std::imag(*cIt);
247  return R;
248 }
249 
250 //: Matrix of real parts of vnl_diag_matrix<std::complex<T> >.
251 template <class T>
253  vnl_imag(vnl_diag_matrix<std::complex<T> > const& C)
254 {
255  vnl_diag_matrix<T> R(C.rows());
256  typename vnl_diag_matrix<std::complex<T> >::const_iterator cIt = C.begin();
257  typename vnl_diag_matrix<T>::iterator rIt = R.begin();
258  for (; cIt != C.end(); ++cIt, ++rIt)
259  *rIt = std::imag(*cIt);
260  return R;
261 }
262 
263 //: Matrix of real parts of vnl_sym_matrix<std::complex<T> >.
264 template <class T>
266  vnl_imag(vnl_sym_matrix<std::complex<T> > const& C)
267 {
268  vnl_sym_matrix<T> R(C.rows());
269  typename vnl_sym_matrix<std::complex<T> >::const_iterator cIt = C.begin();
270  typename vnl_sym_matrix<T>::iterator rIt = R.begin();
271  for (; cIt != C.end(); ++cIt, ++rIt)
272  *rIt = std::imag(*cIt);
273  return R;
274 }
275 
276 //-------------------------------------------------------------------------
277 
278 #define VNL_COMPLEX_OPS_INSTANTIATE(T) \
279 template VNL_EXPORT void vnl_complexify(T const *, std::complex<T > *, unsigned); \
280 template VNL_EXPORT void vnl_complexify(T const *, T const *, std::complex<T > *, unsigned); \
281 \
282 template VNL_EXPORT vnl_vector<std::complex<T > > vnl_complexify(vnl_vector<T > const &); \
283 template VNL_EXPORT vnl_vector<std::complex<T > > vnl_complexify(vnl_vector<T > const &, vnl_vector<T > const &); \
284 template VNL_EXPORT vnl_matrix<std::complex<T > > vnl_complexify(vnl_matrix<T > const &); \
285 template VNL_EXPORT vnl_matrix<std::complex<T > > vnl_complexify(vnl_matrix<T > const &, vnl_matrix<T > const &); \
286 template VNL_EXPORT vnl_diag_matrix<std::complex<T > > vnl_complexify(vnl_diag_matrix<T > const &); \
287 template VNL_EXPORT vnl_diag_matrix<std::complex<T > > vnl_complexify(vnl_diag_matrix<T > const &,vnl_diag_matrix<T > const&); \
288 template VNL_EXPORT vnl_sym_matrix<std::complex<T > > vnl_complexify(vnl_sym_matrix<T > const &); \
289 template VNL_EXPORT vnl_sym_matrix<std::complex<T > > vnl_complexify(vnl_sym_matrix<T > const &,vnl_sym_matrix<T > const&); \
290 \
291 template VNL_EXPORT void vnl_real(std::complex<T > const*, T*, unsigned int); \
292 template VNL_EXPORT void vnl_imag(std::complex<T > const*, T*, unsigned int); \
293 \
294 template VNL_EXPORT vnl_vector<T > vnl_real(vnl_vector<std::complex<T > > const&); \
295 template VNL_EXPORT vnl_vector<T > vnl_imag(vnl_vector<std::complex<T > > const&); \
296 \
297 template VNL_EXPORT vnl_matrix<T > vnl_real(vnl_matrix<std::complex<T > > const&); \
298 template VNL_EXPORT vnl_matrix<T > vnl_imag(vnl_matrix<std::complex<T > > const&); \
299 \
300 template VNL_EXPORT vnl_diag_matrix<T > vnl_real(vnl_diag_matrix<std::complex<T > > const&); \
301 template VNL_EXPORT vnl_diag_matrix<T > vnl_imag(vnl_diag_matrix<std::complex<T > > const&); \
302 \
303 template VNL_EXPORT vnl_sym_matrix<T > vnl_real(vnl_sym_matrix<std::complex<T > > const&); \
304 template VNL_EXPORT vnl_sym_matrix<T > vnl_imag(vnl_sym_matrix<std::complex<T > > const&)
305 
306 #endif // vnl_complex_ops_hxx_
unsigned int size() const
Return the total number of elements stored by the matrix.
unsigned int cols() const
Return the number of columns.
Definition: vnl_matrix.h:183
iterator end()
Iterator pointing to element beyond end of data.
Definition: vnl_matrix.h:622
Functions to create complex vectors and matrices from real ones.
unsigned int rows() const
Return the number of rows.
unsigned int cols() const
Return the number of columns.
size_t size() const
Return the length, number of elements, dimension of this vector.
Definition: vnl_vector.h:126
VNL_EXPORT vnl_vector< T > vnl_real(vnl_vector< std::complex< T > > const &C)
Vector of real parts of vnl_vector<std::complex<T> >.
iterator begin()
Iterator pointing to start of data.
Definition: vnl_matrix.h:620
iterator end()
iterator end()
Iterator pointing to element beyond end of data.
Definition: vnl_vector.h:246
Functions to return the imaginary parts of complex arrays, vectors, matrices.
unsigned int rows() const
Return the number of rows.
iterator begin()
iterator begin()
Iterator pointing to start of data.
Definition: vnl_vector.h:243
unsigned int size() const
Return the total number of elements stored by the matrix.
An ordinary mathematical matrix.
Definition: vnl_adjugate.h:22
VNL_EXPORT vnl_vector< T > vnl_imag(vnl_vector< std::complex< T > > const &C)
Vector of imaginary parts of vnl_vector<std::complex<T> >.
T * iterator
Iterators.
Definition: vnl_matrix.h:618
Mathematical vector class, templated by type of element.
Definition: vnl_fwd.h:16
VNL_EXPORT vnl_vector< std::complex< T > > vnl_complexify(vnl_vector< T > const &R)
Return complexified version of real vector R.
Functions to return the real parts of complex arrays, vectors, matrices.
T * iterator
Type defs for iterators.
Definition: vnl_vector.h:241
unsigned int size() const
Return the total number of elements stored by the matrix.
Definition: vnl_matrix.h:176
stores a diagonal matrix as a single vector.
unsigned int rows() const
Return the number of rows.
Definition: vnl_matrix.h:179
vnl_vector< T >::iterator iterator
stores a symmetric matrix as just the diagonal and lower triangular part.