diagview_meat.hpp

Go to the documentation of this file.
00001 // Copyright (C) 2009 NICTA
00002 // 
00003 // Authors:
00004 // - Conrad Sanderson (conradsand at ieee dot org)
00005 // 
00006 // This file is part of the Armadillo C++ library.
00007 // It is provided without any warranty of fitness
00008 // for any purpose. You can redistribute this file
00009 // and/or modify it under the terms of the GNU
00010 // Lesser General Public License (LGPL) as published
00011 // by the Free Software Foundation, either version 3
00012 // of the License or (at your option) any later version.
00013 // (see http://www.opensource.org/licenses for more info)
00014 
00015 
00016 //! \addtogroup diagview
00017 //! @{
00018 
00019 
00020 template<typename eT>
00021 inline
00022 diagview<eT>::~diagview()
00023   {
00024   arma_extra_debug_sigprint();
00025   }
00026 
00027 
00028 template<typename eT>
00029 arma_inline
00030 diagview<eT>::diagview(const Mat<eT>& in_m, const u32 in_row_offset, const u32 in_col_offset, const u32 in_len)
00031   : m(in_m)
00032   , m_ptr(0)
00033   , row_offset(in_row_offset)
00034   , col_offset(in_col_offset)
00035   , n_elem(in_len)
00036   {
00037   arma_extra_debug_sigprint();
00038   }
00039 
00040 
00041 
00042 template<typename eT>
00043 arma_inline
00044 diagview<eT>::diagview(Mat<eT>& in_m, const u32 in_row_offset, const u32 in_col_offset, const u32 in_len)
00045   : m(in_m)
00046   , m_ptr(&in_m)
00047   , row_offset(in_row_offset)
00048   , col_offset(in_col_offset)
00049   , n_elem(in_len)
00050   {
00051   arma_extra_debug_sigprint();
00052   }
00053 
00054 
00055 
00056 //! set a diagonal of our matrix using data from a foreign object
00057 template<typename eT>
00058 template<typename T1>
00059 inline
00060 void
00061 diagview<eT>::operator= (const Base<eT,T1>& o)
00062   {
00063   arma_extra_debug_sigprint();
00064   arma_check( (m_ptr == 0), "diagview::operator=(): matrix is read only");
00065   
00066   const unwrap<T1> tmp(o.get_ref());
00067   const Mat<eT>& x = tmp.M;
00068   
00069   diagview& t = *this;
00070   
00071   arma_debug_check( !x.is_vec(), "diagview::operator=(): need a vector");
00072   arma_debug_check( (t.n_elem != x.n_elem), "diagview::operator=(): diagonal and given vector have incompatible lengths");
00073   
00074   Mat<eT>& t_m = *(t.m_ptr);
00075   
00076   for(u32 i=0; i<n_elem; ++i)
00077     {
00078     t_m.at(i+row_offset, i+col_offset) = x.mem[i];
00079     }
00080   
00081   }
00082 
00083 
00084 
00085 //! set a diagonal of our matrix using a diagonal from a foreign matrix
00086 template<typename eT>
00087 inline
00088 void
00089 diagview<eT>::operator= (const diagview<eT>& x)
00090   {
00091   arma_extra_debug_sigprint();
00092   arma_check( (m_ptr == 0), "diagview::operator=(): matrix is read only");
00093   
00094   diagview<eT>& t = *this;
00095   
00096   arma_debug_check( (t.n_elem != x.n_elem), "diagview::operator=(): diagonals have incompatible lengths");
00097   
00098         Mat<eT>& t_m = *(t.m_ptr);
00099   const Mat<eT>& x_m = x.m;
00100   
00101   for(u32 i=0; i<n_elem; ++i)
00102     {
00103     t_m.at(i+t.row_offset, i+t.col_offset) = x_m.at(i+x.row_offset, i+x.col_offset);
00104     }
00105     
00106   }
00107 
00108 
00109 
00110 //! \brief
00111 //! extract a diagonal and store it as a column vector
00112 template<typename eT>
00113 inline
00114 void
00115 diagview<eT>::extract(Mat<eT>& actual_out, const diagview<eT>& in)
00116   {
00117   arma_extra_debug_sigprint();
00118   
00119   const Mat<eT>& in_m = in.m;
00120   const bool alias = (&actual_out == &in_m);
00121   
00122   Mat<eT>* tmp = (alias) ? new Mat<eT> : 0;
00123   Mat<eT>& out = (alias) ? (*tmp)            : actual_out;
00124   
00125   const u32 in_n_elem     = in.n_elem;
00126   const u32 in_row_offset = in.row_offset;
00127   const u32 in_col_offset = in.col_offset;
00128   
00129   out.set_size(in_n_elem,1);
00130   eT* out_mem = out.memptr();
00131   
00132   for(u32 i=0; i<in_n_elem; ++i)
00133     {
00134     out_mem[i] = in_m.at(i+in_row_offset, i+in_col_offset);
00135     }
00136   
00137   
00138   if(alias)
00139     {
00140     actual_out = out;
00141     delete tmp;
00142     }
00143   
00144   }
00145 
00146 
00147 
00148 template<typename eT>
00149 arma_inline
00150 eT&
00151 diagview<eT>::operator[](const u32 i)
00152   {
00153   return (*m_ptr).at(i+row_offset, i+col_offset);
00154   }
00155 
00156 
00157 
00158 template<typename eT>
00159 arma_inline
00160 eT
00161 diagview<eT>::operator[](const u32 i) const
00162   {
00163   return m.at(i+row_offset, i+col_offset);
00164   }
00165 
00166 
00167 
00168 template<typename eT>
00169 arma_inline
00170 eT&
00171 diagview<eT>::operator()(const u32 i)
00172   {
00173   arma_check( (m_ptr == 0), "diagview::operator(): matrix is read only");
00174   arma_debug_check( (i > n_elem), "diagview::operator(): index out of bounds" );
00175   
00176   return (*m_ptr).at(i+row_offset, i+col_offset);
00177   }
00178 
00179 
00180 
00181 template<typename eT>
00182 arma_inline
00183 eT
00184 diagview<eT>::operator()(const u32 i) const
00185   {
00186   arma_debug_check( (i > n_elem), "diagview::operator(): index out of bounds" );
00187   
00188   return m.at(i+row_offset, i+col_offset);
00189   }
00190 
00191 
00192 
00193 template<typename eT>
00194 inline
00195 void
00196 diagview<eT>::fill(const eT val)
00197   {
00198   arma_extra_debug_sigprint();
00199   arma_check( (m_ptr == 0), "diagview::operator(): matrix is read only");
00200   
00201   Mat<eT>& x = (*m_ptr);
00202   
00203   for(u32 i=0; i<n_elem; ++i)
00204     {
00205     x.at(i+row_offset, i+col_offset) = val;
00206     }
00207   }
00208 
00209 
00210 
00211 template<typename eT>
00212 inline
00213 void
00214 diagview<eT>::zeros()
00215   {
00216   arma_extra_debug_sigprint();
00217   arma_check( (m_ptr == 0), "diagview::operator(): matrix is read only");
00218   
00219   Mat<eT>& x = (*m_ptr);
00220   
00221   for(u32 i=0; i<n_elem; ++i)
00222     {
00223     x.at(i+row_offset, i+col_offset) = eT(0);
00224     }
00225   }
00226 
00227 
00228 
00229 //! @}