op_reshape_meat.hpp
Go to the documentation of this file.
00001 // Copyright (C) 2008-2011 NICTA (www.nicta.com.au)
00002 // Copyright (C) 2008-2011 Conrad Sanderson
00003 // 
00004 // This file is part of the Armadillo C++ library.
00005 // It is provided without any warranty of fitness
00006 // for any purpose. You can redistribute this file
00007 // and/or modify it under the terms of the GNU
00008 // Lesser General Public License (LGPL) as published
00009 // by the Free Software Foundation, either version 3
00010 // of the License or (at your option) any later version.
00011 // (see http://www.opensource.org/licenses for more info)
00012 
00013 
00014 
00017 
00018 
00019 
00020 template<typename T1>
00021 inline
00022 void
00023 op_reshape::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_reshape>& in)
00024   {
00025   arma_extra_debug_sigprint();
00026   
00027   typedef typename T1::elem_type eT;
00028   
00029   const unwrap<T1>   tmp(in.m);
00030   const Mat<eT>& A = tmp.M;
00031   
00032   const uword in_n_rows = in.aux_uword_a;
00033   const uword in_n_cols = in.aux_uword_b;
00034   const uword in_dim    = in.aux_uword_c;
00035   
00036   const uword in_n_elem = in_n_rows * in_n_cols;
00037   
00038   if(A.n_elem == in_n_elem)
00039     {
00040     if(in_dim == 0)
00041       {
00042       if(&out != &A)
00043         {
00044         out.set_size(in_n_rows, in_n_cols);
00045         arrayops::copy( out.memptr(), A.memptr(), out.n_elem );
00046         }
00047       else  // &out == &A, i.e. inplace resize
00048         {
00049         const bool same_size = ( (out.n_rows == in_n_rows) && (out.n_cols == in_n_cols) );
00050         
00051         if(same_size == false)
00052           {
00053           arma_debug_check
00054             (
00055             (out.mem_state == 3),
00056             "reshape(): size can't be changed as template based size specification is in use"
00057             );
00058           
00059           access::rw(out.n_rows) = in_n_rows;
00060           access::rw(out.n_cols) = in_n_cols;
00061           }
00062         }
00063       }
00064     else
00065       {
00066       unwrap_check< Mat<eT> > tmp(A, out);
00067       const Mat<eT>& B      = tmp.M;
00068       
00069       out.set_size(in_n_rows, in_n_cols);
00070       
00071       eT* out_mem = out.memptr();
00072       uword i = 0;
00073       
00074       const uword B_n_rows = B.n_rows;
00075       const uword B_n_cols = B.n_cols;
00076       
00077       for(uword row=0; row<B_n_rows; ++row)
00078         {
00079         for(uword col=0; col<B_n_cols; ++col)
00080           {
00081           out_mem[i] = B.at(row,col);
00082           ++i;
00083           }
00084         }
00085         
00086       }
00087     }
00088   else
00089     {
00090     const unwrap_check< Mat<eT> > tmp(A, out);
00091     const Mat<eT>& B            = tmp.M;
00092     
00093     const uword n_elem_to_copy = (std::min)(B.n_elem, in_n_elem);
00094     
00095     out.set_size(in_n_rows, in_n_cols);
00096     
00097     eT* out_mem = out.memptr();
00098     
00099     if(in_dim == 0)
00100       {
00101       arrayops::copy( out_mem, B.memptr(), n_elem_to_copy );
00102       }
00103     else
00104       {
00105       uword row = 0;
00106       uword col = 0;
00107       
00108       const uword B_n_cols = B.n_cols;
00109       
00110       for(uword i=0; i<n_elem_to_copy; ++i)
00111         {
00112         out_mem[i] = B.at(row,col);
00113         
00114         ++col;
00115         
00116         if(col >= B_n_cols)
00117           {
00118           col = 0;
00119           ++row;
00120           }
00121         }
00122       }
00123     
00124     for(uword i=n_elem_to_copy; i<in_n_elem; ++i)
00125       {
00126       out_mem[i] = eT(0);
00127       }
00128     
00129     }
00130   }
00131 
00132 
00133 
00134 template<typename T1>
00135 inline
00136 void
00137 op_reshape::apply(Cube<typename T1::elem_type>& out, const OpCube<T1,op_reshape>& in)
00138   {
00139   arma_extra_debug_sigprint();
00140   
00141   typedef typename T1::elem_type eT;
00142   
00143   const unwrap_cube<T1> tmp(in.m);
00144   const Cube<eT>& A   = tmp.M;
00145   
00146   const uword in_n_rows   = in.aux_uword_a;
00147   const uword in_n_cols   = in.aux_uword_b;
00148   const uword in_n_slices = in.aux_uword_c;
00149   const uword in_dim      = in.aux_uword_d;
00150   
00151   const uword in_n_elem = in_n_rows * in_n_cols * in_n_slices;
00152   
00153   if(A.n_elem == in_n_elem)
00154     {
00155     if(in_dim == 0)
00156       {
00157       if(&out != &A)
00158         {
00159         out.set_size(in_n_rows, in_n_cols, in_n_slices);
00160         arrayops::copy( out.memptr(), A.memptr(), out.n_elem );
00161         }
00162       else  // &out == &A, i.e. inplace resize
00163         {
00164         const bool same_size = ( (out.n_rows == in_n_rows) && (out.n_cols == in_n_cols) && (out.n_slices == in_n_slices) );
00165         
00166         if(same_size == false)
00167           {
00168           arma_debug_check
00169             (
00170             (out.mem_state == 3),
00171             "reshape(): size can't be changed as template based size specification is in use"
00172             );
00173           
00174           out.delete_mat();
00175           
00176           access::rw(out.n_rows)       = in_n_rows;
00177           access::rw(out.n_cols)       = in_n_cols;
00178           access::rw(out.n_elem_slice) = in_n_rows * in_n_cols;
00179           access::rw(out.n_slices)     = in_n_slices;
00180           
00181           out.create_mat();
00182           }
00183         }
00184       }
00185     else
00186       {
00187       unwrap_cube_check< Cube<eT> > tmp(A, out);
00188       const Cube<eT>& B           = tmp.M;
00189       
00190       out.set_size(in_n_rows, in_n_cols, in_n_slices);
00191       
00192       eT* out_mem = out.memptr();
00193       uword i = 0;
00194       
00195       const uword B_n_rows   = B.n_rows;
00196       const uword B_n_cols   = B.n_cols;
00197       const uword B_n_slices = B.n_slices;
00198       
00199       for(uword slice=0; slice<B_n_slices; ++slice)
00200         {
00201         for(uword row=0; row<B_n_rows; ++row)
00202           {
00203           for(uword col=0; col<B_n_cols; ++col)
00204             {
00205             out_mem[i] = B.at(row,col,slice);
00206             ++i;
00207             }
00208           }
00209         }
00210         
00211       }
00212     }
00213   else
00214     {
00215     const unwrap_cube_check< Cube<eT> > tmp(A, out);
00216     const Cube<eT>& B                 = tmp.M;
00217     
00218     const uword n_elem_to_copy = (std::min)(B.n_elem, in_n_elem);
00219     
00220     out.set_size(in_n_rows, in_n_cols, in_n_slices);
00221     
00222     eT* out_mem = out.memptr();
00223     
00224     if(in_dim == 0)
00225       {
00226       arrayops::copy( out_mem, B.memptr(), n_elem_to_copy );
00227       }
00228     else
00229       {
00230       uword row   = 0;
00231       uword col   = 0;
00232       uword slice = 0;
00233       
00234       const uword B_n_rows = B.n_rows;
00235       const uword B_n_cols = B.n_cols;
00236       
00237       for(uword i=0; i<n_elem_to_copy; ++i)
00238         {
00239         out_mem[i] = B.at(row,col,slice);
00240         
00241         ++col;
00242         
00243         if(col >= B_n_cols)
00244           {
00245           col = 0;
00246           ++row;
00247           
00248           if(row >= B_n_rows)
00249             {
00250             row = 0;
00251             ++slice;
00252             }
00253           }
00254         }
00255       }
00256     
00257     for(uword i=n_elem_to_copy; i<in_n_elem; ++i)
00258       {
00259       out_mem[i] = eT(0);
00260       }
00261     
00262     }
00263   }
00264 
00265 
00266 


armadillo_matrix
Author(s): Conrad Sanderson - NICTA (www.nicta.com.au), (Wrapper by Sjoerd van den Dries)
autogenerated on Tue Jan 7 2014 11:42:05