00001 // This file is part of Eigen, a lightweight C++ template library 00002 // for linear algebra. Eigen itself is part of the KDE project. 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_ARRAY_NORMS_H 00026 #define EIGEN_ARRAY_NORMS_H 00027 00028 template<typename Derived, int p> 00029 struct ei_lpNorm_selector 00030 { 00031 typedef typename NumTraits<typename ei_traits<Derived>::Scalar>::Real RealScalar; 00032 inline static RealScalar run(const MatrixBase<Derived>& m) 00033 { 00034 return ei_pow(m.cwise().abs().cwise().pow(p).sum(), RealScalar(1)/p); 00035 } 00036 }; 00037 00038 template<typename Derived> 00039 struct ei_lpNorm_selector<Derived, 1> 00040 { 00041 inline static typename NumTraits<typename ei_traits<Derived>::Scalar>::Real run(const MatrixBase<Derived>& m) 00042 { 00043 return m.cwise().abs().sum(); 00044 } 00045 }; 00046 00047 template<typename Derived> 00048 struct ei_lpNorm_selector<Derived, 2> 00049 { 00050 inline static typename NumTraits<typename ei_traits<Derived>::Scalar>::Real run(const MatrixBase<Derived>& m) 00051 { 00052 return m.norm(); 00053 } 00054 }; 00055 00056 template<typename Derived> 00057 struct ei_lpNorm_selector<Derived, Infinity> 00058 { 00059 inline static typename NumTraits<typename ei_traits<Derived>::Scalar>::Real run(const MatrixBase<Derived>& m) 00060 { 00061 return m.cwise().abs().maxCoeff(); 00062 } 00063 }; 00064 00073 template<typename Derived> 00074 template<int p> 00075 inline typename NumTraits<typename ei_traits<Derived>::Scalar>::Real MatrixBase<Derived>::lpNorm() const 00076 { 00077 return ei_lpNorm_selector<Derived, p>::run(*this); 00078 } 00079 00080 #endif // EIGEN_ARRAY_NORMS_H