Determinant.h
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com>
00005 //
00006 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00024 
00025 #ifndef EIGEN_DETERMINANT_H
00026 #define EIGEN_DETERMINANT_H
00027 
00028 namespace Eigen { 
00029 
00030 namespace internal {
00031 
00032 template<typename Derived>
00033 inline const typename Derived::Scalar bruteforce_det3_helper
00034 (const MatrixBase<Derived>& matrix, int a, int b, int c)
00035 {
00036   return matrix.coeff(0,a)
00037          * (matrix.coeff(1,b) * matrix.coeff(2,c) - matrix.coeff(1,c) * matrix.coeff(2,b));
00038 }
00039 
00040 template<typename Derived>
00041 const typename Derived::Scalar bruteforce_det4_helper
00042 (const MatrixBase<Derived>& matrix, int j, int k, int m, int n)
00043 {
00044   return (matrix.coeff(j,0) * matrix.coeff(k,1) - matrix.coeff(k,0) * matrix.coeff(j,1))
00045        * (matrix.coeff(m,2) * matrix.coeff(n,3) - matrix.coeff(n,2) * matrix.coeff(m,3));
00046 }
00047 
00048 template<typename Derived,
00049          int DeterminantType = Derived::RowsAtCompileTime
00050 > struct determinant_impl
00051 {
00052   static inline typename traits<Derived>::Scalar run(const Derived& m)
00053   {
00054     if(Derived::ColsAtCompileTime==Dynamic && m.rows()==0)
00055       return typename traits<Derived>::Scalar(1);
00056     return m.partialPivLu().determinant();
00057   }
00058 };
00059 
00060 template<typename Derived> struct determinant_impl<Derived, 1>
00061 {
00062   static inline typename traits<Derived>::Scalar run(const Derived& m)
00063   {
00064     return m.coeff(0,0);
00065   }
00066 };
00067 
00068 template<typename Derived> struct determinant_impl<Derived, 2>
00069 {
00070   static inline typename traits<Derived>::Scalar run(const Derived& m)
00071   {
00072     return m.coeff(0,0) * m.coeff(1,1) - m.coeff(1,0) * m.coeff(0,1);
00073   }
00074 };
00075 
00076 template<typename Derived> struct determinant_impl<Derived, 3>
00077 {
00078   static inline typename traits<Derived>::Scalar run(const Derived& m)
00079   {
00080     return bruteforce_det3_helper(m,0,1,2)
00081           - bruteforce_det3_helper(m,1,0,2)
00082           + bruteforce_det3_helper(m,2,0,1);
00083   }
00084 };
00085 
00086 template<typename Derived> struct determinant_impl<Derived, 4>
00087 {
00088   static typename traits<Derived>::Scalar run(const Derived& m)
00089   {
00090     // trick by Martin Costabel to compute 4x4 det with only 30 muls
00091     return bruteforce_det4_helper(m,0,1,2,3)
00092           - bruteforce_det4_helper(m,0,2,1,3)
00093           + bruteforce_det4_helper(m,0,3,1,2)
00094           + bruteforce_det4_helper(m,1,2,0,3)
00095           - bruteforce_det4_helper(m,1,3,0,2)
00096           + bruteforce_det4_helper(m,2,3,0,1);
00097   }
00098 };
00099 
00100 } // end namespace internal
00101 
00106 template<typename Derived>
00107 inline typename internal::traits<Derived>::Scalar MatrixBase<Derived>::determinant() const
00108 {
00109   assert(rows() == cols());
00110   typedef typename internal::nested<Derived,Base::RowsAtCompileTime>::type Nested;
00111   return internal::determinant_impl<typename internal::remove_all<Nested>::type>::run(derived());
00112 }
00113 
00114 } // end namespace Eigen
00115 
00116 #endif // EIGEN_DETERMINANT_H