op_reshape_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 
00018 //! \addtogroup op_reshape
00019 //! @{
00020 
00021 
00022 
00023 template<typename T1>
00024 inline
00025 void
00026 op_reshape::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_reshape>& in)
00027   {
00028   arma_extra_debug_sigprint();
00029   
00030   typedef typename T1::elem_type eT;
00031   
00032   const unwrap<T1>   tmp(in.m);
00033   const Mat<eT>& A = tmp.M;
00034   
00035   const u32 in_n_rows = in.aux_u32_a;
00036   const u32 in_n_cols = in.aux_u32_b;
00037   
00038   const u32 in_n_elem = in_n_rows * in_n_cols;
00039   
00040   arma_debug_check( (A.n_elem != in_n_elem), "reshape(): incompatible dimensions");
00041   
00042   if(in.aux == eT(0))
00043     {
00044     if(&out != &A)
00045       {
00046       out.set_size(in_n_rows, in_n_cols);
00047       syslib::copy_elem( out.memptr(), A.memptr(), out.n_elem );
00048       }
00049     else
00050       {
00051       access::rw(out.n_rows) = in_n_rows;
00052       access::rw(out.n_cols) = in_n_cols;
00053       }
00054     }
00055   else
00056     {
00057     unwrap_check< Mat<eT> > tmp(A, out);
00058     const Mat<eT>& B      = tmp.M;
00059 
00060     out.set_size(in_n_rows, in_n_cols);
00061     
00062     eT* out_mem = out.memptr();
00063     u32 i = 0;
00064     
00065     for(u32 row=0; row<B.n_rows; ++row)
00066       {
00067       for(u32 col=0; col<B.n_cols; ++col)
00068         {
00069         out_mem[i] = B.at(row,col);
00070         ++i;
00071         }
00072       }
00073       
00074     }
00075   
00076   }
00077 
00078 
00079 
00080 template<typename T1>
00081 inline
00082 void
00083 op_reshape::apply(Cube<typename T1::elem_type>& out, const OpCube<T1,op_reshape>& in)
00084   {
00085   arma_extra_debug_sigprint();
00086   
00087   typedef typename T1::elem_type eT;
00088   
00089   const unwrap_cube<T1> tmp(in.m);
00090   const Cube<eT>& A   = tmp.M;
00091   
00092   const u32 in_n_rows   = in.aux_u32_a;
00093   const u32 in_n_cols   = in.aux_u32_b;
00094   const u32 in_n_slices = in.aux_u32_c;
00095   
00096   const u32 in_n_elem = in_n_rows * in_n_cols * in_n_slices;
00097   
00098   arma_debug_check( (A.n_elem != in_n_elem), "reshape(): incompatible dimensions");
00099   
00100   if(in.aux == eT(0))
00101     {
00102     if(&out != &A)
00103       {
00104       out.set_size(in_n_rows, in_n_cols, in_n_slices);
00105       syslib::copy_elem( out.memptr(), A.memptr(), out.n_elem );
00106       }
00107     else
00108       {
00109       access::rw(out.n_rows)   = in_n_rows;
00110       access::rw(out.n_cols)   = in_n_cols;
00111       access::rw(out.n_slices) = in_n_slices;
00112       }
00113     }
00114   else
00115     {
00116     unwrap_cube_check< Cube<eT> > tmp(A, out);
00117     const Cube<eT>& B           = tmp.M;
00118     
00119     out.set_size(in_n_rows, in_n_cols, in_n_slices);
00120     
00121     eT* out_mem = out.memptr();
00122     u32 i = 0;
00123     
00124     for(u32 slice=0; slice<B.n_slices; ++slice)
00125       {
00126       for(u32 row=0; row<B.n_rows; ++row)
00127         {
00128         for(u32 col=0; col<B.n_cols; ++col)
00129           {
00130           out_mem[i] = B.at(row,col,slice);
00131           ++i;
00132           }
00133         }
00134       }
00135       
00136     }
00137   
00138   }
00139 
00140 
00141 
00142 //! @}