op_inv_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_inv
00018 //! @{
00019 
00020 
00021 //! immediate inverse of a matrix, storing the result in a dense matrix
00022 template<typename eT>
00023 inline
00024 void
00025 op_inv::apply(Mat<eT>& out, const Mat<eT>& A)
00026   {
00027   arma_extra_debug_sigprint();
00028   
00029   // no need to check for aliasing, due to:
00030   // - auxlib::inv() copies A to out before inversion
00031   // - for 2x2 and 3x3 matrices the code is alias safe
00032   
00033   arma_debug_check( !A.is_square(), "op_inv::apply(): matrix must be square" );
00034   
00035   const bool status = (&out != &A) ? auxlib::inv_noalias(out, A) : auxlib::inv_inplace(out);
00036   
00037   if(status == false)
00038     {
00039     arma_warn( true, "inv(): matrix appears to be singular" );
00040     out.set_size(0,0);
00041     }
00042   }
00043 
00044 
00045 
00046 //! immediate inverse of T1, storing the result in a dense matrix
00047 template<typename T1>
00048 inline
00049 void
00050 op_inv::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_inv>& X)
00051   {
00052   arma_extra_debug_sigprint();
00053   
00054   typedef typename T1::elem_type eT;
00055   
00056   const strip_diagmat<T1> strip(X.m);
00057   
00058   if(strip.do_diagmat == true)
00059     {
00060     op_inv::apply_diag(out, strip.M);
00061     }
00062   else
00063     {
00064     const unwrap<T1>   tmp(X.m);
00065     const Mat<eT>& A = tmp.M;
00066   
00067     op_inv::apply(out, A);
00068     }
00069   }
00070 
00071 
00072 
00073 template<typename T1>
00074 inline
00075 void
00076 op_inv::apply_diag(Mat<typename T1::elem_type>& out, const Base<typename T1::elem_type, T1>& X)
00077   {
00078   arma_extra_debug_sigprint();
00079   
00080   typedef typename T1::elem_type eT;
00081   
00082   const diagmat_proxy_check<T1> A(X.get_ref(), out);
00083   
00084   const u32 N = A.n_elem;
00085   
00086   out.set_size(N,N);
00087   
00088   for(u32 col=0; col<N; ++col)
00089     {
00090     for(u32 row=0; row<col; ++row)   { out.at(row,col) = eT(0); }
00091     
00092     out.at(col,col) = eT(1) / A[col];
00093     
00094     for(u32 row=col+1; row<N; ++row) { out.at(row,col) = eT(0); }
00095     }
00096   
00097   }
00098 
00099 
00100 
00101 //! @}