fn_det.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 fn_det
00018 //! @{
00019 
00020 
00021 
00022 //! determinant of mat
00023 template<typename T1>
00024 inline
00025 arma_warn_unused
00026 typename T1::elem_type
00027 det(const Base<typename T1::elem_type,T1>& X, const typename arma_blas_type_only<typename T1::elem_type>::result* junk = 0)
00028   {
00029   arma_extra_debug_sigprint();
00030   
00031   typedef typename T1::elem_type eT;
00032   
00033   const unwrap<T1>   tmp(X.get_ref());
00034   const Mat<eT>& A = tmp.M;
00035   
00036   arma_debug_check( !A.is_square(), "det(): matrix must be square" );
00037   
00038   return auxlib::det(A);
00039   }
00040 
00041 
00042 
00043 //! determinant of diagmat
00044 template<typename T1>
00045 inline
00046 arma_warn_unused
00047 typename T1::elem_type
00048 det(const Op<T1, op_diagmat>& X)
00049   {
00050   arma_extra_debug_sigprint();
00051   
00052   typedef typename T1::elem_type eT;
00053   
00054   const diagmat_proxy<T1> A(X.m);
00055   
00056   arma_debug_check( (A.n_elem == 0), "det(): given object has no elements" );
00057   
00058   eT val = A[0];
00059   
00060   for(u32 i=1; i<A.n_elem; ++i)
00061     {
00062     val *= A[i];
00063     }
00064   
00065   return val;
00066   }
00067 
00068 
00069 
00070 //! determinant of inv(A), without doing the inverse operation
00071 template<typename T1>
00072 inline
00073 arma_warn_unused
00074 typename T1::elem_type
00075 det(const Op<T1,op_inv>& in, const typename arma_blas_type_only<typename T1::elem_type>::result* junk = 0)
00076   {
00077   arma_extra_debug_sigprint();
00078   
00079   typedef typename T1::elem_type eT;
00080   isnt_fltpt<eT>::check();
00081   
00082   eT tmp = det(in.m);
00083   arma_warn( (tmp == eT(0)), "det(): warning: denominator is zero" );
00084   
00085   return eT(1) / tmp;
00086   }
00087 
00088 
00089 
00090 //! determinant of trans(A)
00091 template<typename T1>
00092 inline
00093 arma_warn_unused
00094 typename T1::elem_type
00095 det(const Op<T1,op_trans>& in, const typename arma_blas_type_only<typename T1::elem_type>::result* junk = 0)
00096   {
00097   arma_extra_debug_sigprint();
00098   
00099   typedef typename T1::elem_type eT;
00100   
00101   const unwrap<T1>   tmp(in.m);
00102   const Mat<eT>& X = tmp.M;
00103 
00104   return det(X);
00105   }
00106 
00107 
00108 
00109 //! @}