diagview_meat.hpp

Go to the documentation of this file.
00001 // Copyright (C) 2010 NICTA and the authors listed below
00002 // http://nicta.com.au
00003 // 
00004 // Authors:
00005 // - Conrad Sanderson (conradsand at ieee dot org)
00006 // 
00007 // This file is part of the Armadillo C++ library.
00008 // It is provided without any warranty of fitness
00009 // for any purpose. You can redistribute this file
00010 // and/or modify it under the terms of the GNU
00011 // Lesser General Public License (LGPL) as published
00012 // by the Free Software Foundation, either version 3
00013 // of the License or (at your option) any later version.
00014 // (see http://www.opensource.org/licenses for more info)
00015 
00016 
00017 //! \addtogroup diagview
00018 //! @{
00019 
00020 
00021 template<typename eT>
00022 inline
00023 diagview<eT>::~diagview()
00024   {
00025   arma_extra_debug_sigprint();
00026   }
00027 
00028 
00029 template<typename eT>
00030 arma_inline
00031 diagview<eT>::diagview(const Mat<eT>& in_m, const u32 in_row_offset, const u32 in_col_offset, const u32 in_len)
00032   : m(in_m)
00033   , m_ptr(0)
00034   , row_offset(in_row_offset)
00035   , col_offset(in_col_offset)
00036   , n_rows(in_len)
00037   , n_cols( (in_len > 0) ? 1 : 0 )
00038   , n_elem(in_len)
00039   {
00040   arma_extra_debug_sigprint();
00041   }
00042 
00043 
00044 
00045 template<typename eT>
00046 arma_inline
00047 diagview<eT>::diagview(Mat<eT>& in_m, const u32 in_row_offset, const u32 in_col_offset, const u32 in_len)
00048   : m(in_m)
00049   , m_ptr(&in_m)
00050   , row_offset(in_row_offset)
00051   , col_offset(in_col_offset)
00052   , n_rows(in_len)
00053   , n_cols( (in_len > 0) ? 1 : 0 )
00054   , n_elem(in_len)
00055   {
00056   arma_extra_debug_sigprint();
00057   }
00058 
00059 
00060 
00061 //! set a diagonal of our matrix using data from a foreign object
00062 template<typename eT>
00063 template<typename T1>
00064 inline
00065 void
00066 diagview<eT>::operator= (const Base<eT,T1>& o)
00067   {
00068   arma_extra_debug_sigprint();
00069   arma_check( (m_ptr == 0), "diagview::operator=(): matrix is read only");
00070   
00071   const unwrap<T1> tmp(o.get_ref());
00072   const Mat<eT>& x = tmp.M;
00073   
00074   diagview& t = *this;
00075   
00076   arma_debug_check( !x.is_vec(), "diagview::operator=(): need a vector");
00077   arma_debug_check( (t.n_elem != x.n_elem), "diagview::operator=(): diagonal and given vector have incompatible lengths");
00078   
00079   Mat<eT>& t_m = *(t.m_ptr);
00080   
00081   for(u32 i=0; i<n_elem; ++i)
00082     {
00083     t_m.at(i+row_offset, i+col_offset) = x.mem[i];
00084     }
00085   
00086   }
00087 
00088 
00089 
00090 //! set a diagonal of our matrix using a diagonal from a foreign matrix
00091 template<typename eT>
00092 inline
00093 void
00094 diagview<eT>::operator= (const diagview<eT>& x)
00095   {
00096   arma_extra_debug_sigprint();
00097   arma_check( (m_ptr == 0), "diagview::operator=(): matrix is read only");
00098   
00099   diagview<eT>& t = *this;
00100   
00101   arma_debug_check( (t.n_elem != x.n_elem), "diagview::operator=(): diagonals have incompatible lengths");
00102   
00103         Mat<eT>& t_m = *(t.m_ptr);
00104   const Mat<eT>& x_m = x.m;
00105   
00106   for(u32 i=0; i<n_elem; ++i)
00107     {
00108     t_m.at(i+t.row_offset, i+t.col_offset) = x_m.at(i+x.row_offset, i+x.col_offset);
00109     }
00110     
00111   }
00112 
00113 
00114 
00115 //! \brief
00116 //! extract a diagonal and store it as a column vector
00117 template<typename eT>
00118 inline
00119 void
00120 diagview<eT>::extract(Mat<eT>& actual_out, const diagview<eT>& in)
00121   {
00122   arma_extra_debug_sigprint();
00123   
00124   const Mat<eT>& in_m = in.m;
00125   const bool alias = (&actual_out == &in_m);
00126   
00127   Mat<eT>* tmp = (alias) ? new Mat<eT> : 0;
00128   Mat<eT>& out = (alias) ? (*tmp)      : actual_out;
00129   
00130   const u32 in_n_elem     = in.n_elem;
00131   const u32 in_row_offset = in.row_offset;
00132   const u32 in_col_offset = in.col_offset;
00133   
00134   out.set_size(in_n_elem,1);
00135   eT* out_mem = out.memptr();
00136   
00137   for(u32 i=0; i<in_n_elem; ++i)
00138     {
00139     out_mem[i] = in_m.at(i+in_row_offset, i+in_col_offset);
00140     }
00141   
00142   
00143   if(alias)
00144     {
00145     actual_out = out;
00146     delete tmp;
00147     }
00148   
00149   }
00150 
00151 
00152 
00153 //! X += Y.diagview(...)
00154 template<typename eT>
00155 inline
00156 void
00157 diagview<eT>::plus_inplace(Mat<eT>& out, const diagview<eT>& in)
00158   {
00159   arma_extra_debug_sigprint();
00160   
00161   arma_debug_assert_same_size(out.n_rows, out.n_cols, in.n_rows, 1, "matrix addition");
00162   
00163   const Mat<eT>& in_m = in.m;
00164   
00165   const u32 in_n_elem     = in.n_elem;
00166   const u32 in_row_offset = in.row_offset;
00167   const u32 in_col_offset = in.col_offset;
00168   
00169   eT* out_mem = out.memptr();
00170   
00171   for(u32 i=0; i<in_n_elem; ++i)
00172     {
00173     out_mem[i] += in_m.at(i+in_row_offset, i+in_col_offset);
00174     }
00175   }
00176 
00177 
00178 
00179 //! X -= Y.diagview(...)
00180 template<typename eT>
00181 inline
00182 void
00183 diagview<eT>::minus_inplace(Mat<eT>& out, const diagview<eT>& in)
00184   {
00185   arma_extra_debug_sigprint();
00186   
00187   arma_debug_assert_same_size(out.n_rows, out.n_cols, in.n_rows, 1, "matrix subtraction");
00188   
00189   const Mat<eT>& in_m = in.m;
00190   
00191   const u32 in_n_elem     = in.n_elem;
00192   const u32 in_row_offset = in.row_offset;
00193   const u32 in_col_offset = in.col_offset;
00194   
00195   eT* out_mem = out.memptr();
00196   
00197   for(u32 i=0; i<in_n_elem; ++i)
00198     {
00199     out_mem[i] -= in_m.at(i+in_row_offset, i+in_col_offset);
00200     }
00201   }
00202 
00203 
00204 
00205 //! X %= Y.submat(...)
00206 template<typename eT>
00207 inline
00208 void
00209 diagview<eT>::schur_inplace(Mat<eT>& out, const diagview<eT>& in)
00210   {
00211   arma_extra_debug_sigprint();
00212   
00213   arma_debug_assert_same_size(out.n_rows, out.n_cols, in.n_rows, 1, "element-wise matrix multiplication");
00214   
00215   const Mat<eT>& in_m = in.m;
00216   
00217   const u32 in_n_elem     = in.n_elem;
00218   const u32 in_row_offset = in.row_offset;
00219   const u32 in_col_offset = in.col_offset;
00220   
00221   eT* out_mem = out.memptr();
00222   
00223   for(u32 i=0; i<in_n_elem; ++i)
00224     {
00225     out_mem[i] *= in_m.at(i+in_row_offset, i+in_col_offset);
00226     }
00227   }
00228 
00229 
00230 
00231 //! X /= Y.diagview(...)
00232 template<typename eT>
00233 inline
00234 void
00235 diagview<eT>::div_inplace(Mat<eT>& out, const diagview<eT>& in)
00236   {
00237   arma_extra_debug_sigprint();
00238   
00239   arma_debug_assert_same_size(out.n_rows, out.n_cols, in.n_rows, 1, "element-wise matrix division");
00240   
00241   const Mat<eT>& in_m = in.m;
00242   
00243   const u32 in_n_elem     = in.n_elem;
00244   const u32 in_row_offset = in.row_offset;
00245   const u32 in_col_offset = in.col_offset;
00246   
00247   eT* out_mem = out.memptr();
00248   
00249   for(u32 i=0; i<in_n_elem; ++i)
00250     {
00251     out_mem[i] /= in_m.at(i+in_row_offset, i+in_col_offset);
00252     }
00253   }
00254 
00255 
00256 
00257 template<typename eT>
00258 arma_inline
00259 eT&
00260 diagview<eT>::operator[](const u32 i)
00261   {
00262   return (*m_ptr).at(i+row_offset, i+col_offset);
00263   }
00264 
00265 
00266 
00267 template<typename eT>
00268 arma_inline
00269 eT
00270 diagview<eT>::operator[](const u32 i) const
00271   {
00272   return m.at(i+row_offset, i+col_offset);
00273   }
00274 
00275 
00276 
00277 template<typename eT>
00278 arma_inline
00279 eT&
00280 diagview<eT>::at(const u32 row, const u32 col)
00281   {
00282   return (*m_ptr).at(row+row_offset, row+col_offset);
00283   }
00284 
00285 
00286 
00287 template<typename eT>
00288 arma_inline
00289 eT
00290 diagview<eT>::at(const u32 row, const u32 col) const
00291   {
00292   return m.at(row+row_offset, row+col_offset);
00293   }
00294 
00295 
00296 
00297 template<typename eT>
00298 arma_inline
00299 eT&
00300 diagview<eT>::operator()(const u32 i)
00301   {
00302   arma_check( (m_ptr == 0), "diagview::operator(): matrix is read only");
00303   arma_debug_check( (i >= n_elem), "diagview::operator(): out of bounds" );
00304   
00305   return (*m_ptr).at(i+row_offset, i+col_offset);
00306   }
00307 
00308 
00309 
00310 template<typename eT>
00311 arma_inline
00312 eT
00313 diagview<eT>::operator()(const u32 i) const
00314   {
00315   arma_debug_check( (i >= n_elem), "diagview::operator(): out of bounds" );
00316   
00317   return m.at(i+row_offset, i+col_offset);
00318   }
00319 
00320 
00321 
00322 template<typename eT>
00323 arma_inline
00324 eT&
00325 diagview<eT>::operator()(const u32 row, const u32 col)
00326   {
00327   arma_check( (m_ptr == 0), "diagview::operator(): matrix is read only");
00328   arma_debug_check( ((row >= n_elem) || (col > 0)), "diagview::operator(): out of bounds" );
00329   
00330   return (*m_ptr).at(row+row_offset, row+col_offset);
00331   }
00332 
00333 
00334 
00335 template<typename eT>
00336 arma_inline
00337 eT
00338 diagview<eT>::operator()(const u32 row, const u32 col) const
00339   {
00340   arma_debug_check( ((row >= n_elem) || (col > 0)), "diagview::operator(): out of bounds" );
00341   
00342   return m.at(row+row_offset, row+col_offset);
00343   }
00344 
00345 
00346 
00347 template<typename eT>
00348 inline
00349 void
00350 diagview<eT>::fill(const eT val)
00351   {
00352   arma_extra_debug_sigprint();
00353   arma_check( (m_ptr == 0), "diagview::fill(): matrix is read only");
00354   
00355   Mat<eT>& x = (*m_ptr);
00356   
00357   for(u32 i=0; i<n_elem; ++i)
00358     {
00359     x.at(i+row_offset, i+col_offset) = val;
00360     }
00361   }
00362 
00363 
00364 
00365 template<typename eT>
00366 inline
00367 void
00368 diagview<eT>::zeros()
00369   {
00370   arma_extra_debug_sigprint();
00371   arma_check( (m_ptr == 0), "diagview::zeros(): matrix is read only");
00372   
00373   Mat<eT>& x = (*m_ptr);
00374   
00375   for(u32 i=0; i<n_elem; ++i)
00376     {
00377     x.at(i+row_offset, i+col_offset) = eT(0);
00378     }
00379   }
00380 
00381 
00382 
00383 template<typename eT>
00384 inline
00385 void
00386 diagview<eT>::ones()
00387   {
00388   arma_extra_debug_sigprint();
00389   arma_check( (m_ptr == 0), "diagview::ones(): matrix is read only");
00390   
00391   Mat<eT>& x = (*m_ptr);
00392   
00393   for(u32 i=0; i<n_elem; ++i)
00394     {
00395     x.at(i+row_offset, i+col_offset) = eT(1);
00396     }
00397   }
00398 
00399 
00400 
00401 //! @}