core/vnl/vnl_complex_ops.txx
Go to the documentation of this file.
00001 // This is core/vnl/vnl_complex_ops.txx
00002 #ifndef vnl_complex_ops_txx_
00003 #define vnl_complex_ops_txx_
00004 //:
00005 // \file
00006 // \author fsm
00007 // This is the implementation file for the following three header files:
00008 // vnl_complexify.h vnl_real.h vnl_imag.h
00009 
00010 #include "vnl_complexify.h"
00011 #include "vnl_real.h"
00012 #include "vnl_imag.h"
00013 
00014 #include <vcl_cassert.h>
00015 
00016 //-----------------------------------------------------------------------
00017 
00018 template <class T>
00019 void vnl_complexify(T const *src, vcl_complex<T> *dst, unsigned n)
00020 {
00021   for (unsigned i=0; i<n; ++i)
00022     dst[i] = src[i];
00023 }
00024 
00025 template <class T>
00026 void vnl_complexify(T const *re, T const *im, vcl_complex<T> *dst, unsigned n)
00027 {
00028   for (unsigned i=0; i<n; ++i)
00029     dst[i] = vcl_complex<T>(re[i], im[i]);
00030 }
00031 
00032 template <class T>
00033 vnl_vector<vcl_complex<T> > vnl_complexify(vnl_vector<T> const &v)
00034 {
00035   vnl_vector<vcl_complex<T> > vc(v.size());
00036   vnl_complexify(v.begin(), vc.begin(), v.size());
00037   return vc;
00038 }
00039 
00040 template <class T>
00041 vnl_vector<vcl_complex<T> > vnl_complexify(vnl_vector<T> const &re, vnl_vector<T> const &im)
00042 {
00043   assert(re.size() == im.size());
00044   vnl_vector<vcl_complex<T> > vc(re.size());
00045   vnl_complexify(re.begin(), im.begin(), vc.begin(), re.size());
00046   return vc;
00047 }
00048 
00049 template <class T>
00050 vnl_matrix<vcl_complex<T> > vnl_complexify(vnl_matrix<T> const &M)
00051 {
00052   vnl_matrix<vcl_complex<T> > Mc(M.rows(), M.cols());
00053   vnl_complexify(M.begin(), Mc.begin(), M.size());
00054   return Mc;
00055 }
00056 
00057 template <class T>
00058 vnl_matrix<vcl_complex<T> > vnl_complexify(vnl_matrix<T> const &re, vnl_matrix<T> const &im)
00059 {
00060   assert(re.rows() == im.rows());
00061   assert(re.cols() == im.cols());
00062   vnl_matrix<vcl_complex<T> > Mc(re.rows(), re.cols());
00063   vnl_complexify(re.begin(), im.begin(), Mc.begin(), re.size());
00064   return Mc;
00065 }
00066 
00067 //----------------------------------------------------------------------
00068 
00069 //: Return array of real parts of complex array.
00070 template <class T>
00071 void vnl_real(vcl_complex<T> const* C, T* R, unsigned int n)
00072 {
00073   for (unsigned int i=0; i<n; ++i)
00074     R[i] = vcl_real(C[i]);
00075 }
00076 
00077 //: Vector of real parts of vnl_vector<vcl_complex<T> >.
00078 template <class T>
00079 vnl_vector<T> vnl_real(vnl_vector<vcl_complex<T> > const & C)
00080 {
00081   vnl_vector<T> ret(C.size());
00082   for (unsigned i = 0; i < C.size(); ++i)
00083     ret[i] = vcl_real(C[i]);
00084   return ret;
00085 }
00086 
00087 //: Matrix of real parts of vnl_matrix<vcl_complex<T> >.
00088 template <class T>
00089 vnl_matrix<T> vnl_real(vnl_matrix<vcl_complex<T> > const& C)
00090 {
00091   vnl_matrix<T> ret(C.rows(), C.columns());
00092   for (unsigned i = 0; i < C.rows(); ++i)
00093     for (unsigned j = 0; j < C.columns(); ++j)
00094       ret(i,j) = vcl_real(C(i,j));
00095   return ret;
00096 }
00097 
00098 //----------------------------------------------------------------------
00099 
00100 //: Return array of imaginary parts of complex array.
00101 template <class T>
00102 void vnl_imag(vcl_complex<T> const* C, T* I, unsigned int n)
00103 {
00104   for (unsigned int i=0; i<n; ++i)
00105     I[i] = vcl_imag(C[i]);
00106 }
00107 
00108 //: Vector of imaginary parts of vnl_vector<vcl_complex<T> >.
00109 template <class T>
00110 vnl_vector<T> vnl_imag(vnl_vector<vcl_complex<T> > const & C)
00111 {
00112   vnl_vector<T> ret(C.size());
00113   for (unsigned i = 0; i < C.size(); ++i)
00114     ret[i] = vcl_imag(C[i]);
00115   return ret;
00116 }
00117 
00118 //: Matrix of imaginary parts of vnl_matrix<vcl_complex<T> >.
00119 template <class T>
00120 vnl_matrix<T> vnl_imag(vnl_matrix<vcl_complex<T> > const& C)
00121 {
00122   vnl_matrix<T> ret(C.rows(), C.columns());
00123   for (unsigned i = 0; i < C.rows(); ++i)
00124     for (unsigned j = 0; j < C.columns(); ++j)
00125       ret(i,j) = vcl_imag(C(i,j));
00126   return ret;
00127 }
00128 
00129 //-------------------------------------------------------------------------
00130 
00131 #define VNL_COMPLEX_OPS_INSTANTIATE(T) \
00132 template void vnl_complexify(T const *, vcl_complex<T > *, unsigned); \
00133 template void vnl_complexify(T const *, T const *, vcl_complex<T > *, unsigned); \
00134 \
00135 template vnl_vector<vcl_complex<T > > vnl_complexify(vnl_vector<T > const &); \
00136 template vnl_vector<vcl_complex<T > > vnl_complexify(vnl_vector<T > const &, vnl_vector<T > const &); \
00137 template vnl_matrix<vcl_complex<T > > vnl_complexify(vnl_matrix<T > const &); \
00138 template vnl_matrix<vcl_complex<T > > vnl_complexify(vnl_matrix<T > const &, vnl_matrix<T > const &); \
00139 \
00140 template void vnl_real(vcl_complex<T > const*, T*, unsigned int); \
00141 template void vnl_imag(vcl_complex<T > const*, T*, unsigned int); \
00142 \
00143 template vnl_vector<T > vnl_real(vnl_vector<vcl_complex<T > > const&); \
00144 template vnl_vector<T > vnl_imag(vnl_vector<vcl_complex<T > > const&); \
00145 \
00146 template vnl_matrix<T > vnl_real(vnl_matrix<vcl_complex<T > > const&); \
00147 template vnl_matrix<T > vnl_imag(vnl_matrix<vcl_complex<T > > const&)
00148 
00149 #endif // vnl_complex_ops_txx_