op_trans_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 op_trans
00018 //! @{
00019 
00020 
00021 
00022 //! Immediate transpose of a dense matrix
00023 template<typename eT>
00024 inline
00025 void
00026 op_trans::apply_noalias(Mat<eT>& out, const Mat<eT>& A)
00027   {
00028   arma_extra_debug_sigprint();
00029   
00030   const u32 A_n_cols = A.n_cols;
00031   const u32 A_n_rows = A.n_rows;
00032   
00033   out.set_size(A_n_cols, A_n_rows);
00034   
00035   if( (A_n_cols == 1) || (A_n_rows == 1) )
00036     {
00037     syslib::copy_elem( out.memptr(), A.mem, A.n_elem );
00038     }
00039   else
00040     {
00041     for(u32 in_row = 0; in_row<A_n_rows; ++in_row)
00042       {
00043       const u32 out_col = in_row;
00044     
00045       for(u32 in_col = 0; in_col<A_n_cols; ++in_col)
00046         {
00047         const u32 out_row = in_col;
00048         out.at(out_row, out_col) = A.at(in_row, in_col);
00049         }
00050       }
00051     }
00052   
00053   }
00054 
00055 
00056 
00057 template<typename eT>
00058 inline
00059 void
00060 op_trans::apply(Mat<eT>& out, const Mat<eT>& A)
00061   {
00062   arma_extra_debug_sigprint();
00063   
00064   if(&out != &A)
00065     {
00066     op_trans::apply_noalias(out, A);
00067     }
00068   else
00069     {
00070     if(out.n_rows == out.n_cols)
00071       {
00072       arma_extra_debug_print("op_trans::apply(): doing in-place transpose of a square matrix");
00073       
00074       const u32 n_rows = out.n_rows;
00075       const u32 n_cols = out.n_cols;
00076       
00077       for(u32 col=0; col<n_cols; ++col)
00078         {
00079         eT* coldata = out.colptr(col);
00080         
00081         for(u32 row=(col+1); row<n_rows; ++row)
00082           {
00083           std::swap( out.at(col,row), coldata[row] );
00084           }
00085         }
00086       }
00087     else
00088       {
00089       const Mat<eT> A_copy = A;
00090       op_trans::apply_noalias(out, A_copy);
00091       }
00092     }
00093   }
00094 
00095 
00096 
00097 template<typename T1>
00098 inline
00099 void
00100 op_trans::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_trans>& in)
00101   {
00102   arma_extra_debug_sigprint();
00103   
00104   typedef typename T1::elem_type eT;
00105   
00106   const unwrap<T1> tmp(in.m);
00107   const Mat<eT>& A = tmp.M;
00108   
00109   op_trans::apply(out, A);
00110   }
00111 
00112 
00113 
00114 // inline void op_trans::apply_inplace(mat &X)
00115 //   {
00116 //   arma_extra_debug_sigprint();
00117 //   
00118 //   if((X.n_rows == 1) || (X.n_cols == 1))
00119 //     {
00120 //     const u32 old_n_rows = X.n_rows;
00121 //     access::rw(X.n_rows) = X.n_cols;
00122 //     access::rw(X.n_cols) = old_n_rows;
00123 //     }
00124 //   else
00125 //   if(X.n_rows == X.n_cols)
00126 //     {
00127 //     for(u32 col=0; col < X.n_cols; ++col)
00128 //       {
00129 //       double* X_coldata = X.colptr(col);
00130 //       
00131 //       for(u32 row=(col+1); row < X.n_rows; ++row)
00132 //         {
00133 //         std::swap( A.at(col,row), A_coldata[row] );
00134 //         }
00135 //       }
00136 //     }
00137 //   else
00138 //     {
00139 //     mat tmp = trans(X);
00140 //     
00141 //     if(X.mem != X.mem_local)
00142 //       {
00143 //       double* old_mem = X.memptr();
00144 //       access::rw(X.mem) = tmp.memptr();
00145 //       access::rw(tmp.mem) = old_mem;
00146 //       }
00147 //     else
00148 //       {
00149 //       X = tmp;
00150 //       }
00151 //     }
00152 //   
00153 //   }
00154 
00155 
00156 
00157 
00158 template<typename T1>
00159 inline
00160 void
00161 op_trans2::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_trans2>& in)
00162   {
00163   arma_extra_debug_sigprint();
00164   
00165   typedef typename T1::elem_type eT;
00166   
00167   const unwrap<T1> tmp(in.m);
00168   
00169   op_trans::apply(out, tmp.M);
00170   
00171         eT* out_mem = out.memptr();
00172   const u32 n_elem  = out.n_elem;
00173   const eT  k       = in.aux;
00174   
00175   for(u32 i=0; i<n_elem; ++i)
00176     {
00177     out_mem[i] *= k;
00178     }
00179   
00180   }
00181 
00182 
00183 
00184 //! @}