From 57bf4ffaa426358a03839a2b6e4075e7dc000d9f Mon Sep 17 00:00:00 2001 From: Alberto Ruiz Date: Fri, 6 Jun 2014 20:01:57 +0200 Subject: norms --- packages/base/src/Numeric/HMatrix.hs | 7 ++- packages/base/src/Numeric/LinearAlgebra/Util.hs | 60 ++++++++++++++--------- packages/tests/src/Numeric/LinearAlgebra/Tests.hs | 2 +- 3 files changed, 40 insertions(+), 29 deletions(-) diff --git a/packages/base/src/Numeric/HMatrix.hs b/packages/base/src/Numeric/HMatrix.hs index 024e462..9d34658 100644 --- a/packages/base/src/Numeric/HMatrix.hs +++ b/packages/base/src/Numeric/HMatrix.hs @@ -121,8 +121,7 @@ module Numeric.HMatrix ( orth, -- * Norms - norm_0, norm_1, norm_2, norm_Inf, - mnorm_0, mnorm_1, mnorm_2, mnorm_Inf, + Normed(..), norm_Frob, norm_nuclear, -- * Correlation and convolution @@ -140,7 +139,7 @@ module Numeric.HMatrix ( RealOf, ComplexOf, SingleOf, DoubleOf, IndexOf, Field, - Normed, +-- Normed, Transposable, CGState(..), Testable(..), @@ -152,7 +151,7 @@ import Numeric.LinearAlgebra.Data import Numeric.Matrix() import Numeric.Vector() import Data.Packed.Numeric hiding ((<>)) -import Numeric.LinearAlgebra.Algorithms hiding (linearSolve) +import Numeric.LinearAlgebra.Algorithms hiding (linearSolve,Normed) import qualified Numeric.LinearAlgebra.Algorithms as A import Numeric.LinearAlgebra.Util import Numeric.LinearAlgebra.Random diff --git a/packages/base/src/Numeric/LinearAlgebra/Util.hs b/packages/base/src/Numeric/LinearAlgebra/Util.hs index aee21b8..324fb44 100644 --- a/packages/base/src/Numeric/LinearAlgebra/Util.hs +++ b/packages/base/src/Numeric/LinearAlgebra/Util.hs @@ -33,8 +33,7 @@ module Numeric.LinearAlgebra.Util( cross, norm, ℕ,ℤ,ℝ,ℂ,𝑖,i_C, --ℍ - norm_1, norm_2, norm_0, norm_Inf, norm_Frob, norm_nuclear, - mnorm_1, mnorm_2, mnorm_0, mnorm_Inf, + Normed(..), norm_Frob, norm_nuclear, unitary, mt, (~!~), @@ -61,7 +60,8 @@ module Numeric.LinearAlgebra.Util( ) where import Data.Packed.Numeric -import Numeric.LinearAlgebra.Algorithms hiding (i) +import Numeric.LinearAlgebra.Algorithms hiding (i,Normed) +--import qualified Numeric.LinearAlgebra.Algorithms as A import Numeric.Matrix() import Numeric.Vector() import Numeric.LinearAlgebra.Random @@ -225,37 +225,49 @@ norm :: Vector Double -> Double -- ^ 2-norm of real vector norm = pnorm PNorm2 -norm_2 :: Normed Vector t => Vector t -> RealOf t -norm_2 = pnorm PNorm2 +class Normed a + where + norm_0 :: a -> ℝ + norm_1 :: a -> ℝ + norm_2 :: a -> ℝ + norm_Inf :: a -> ℝ -norm_1 :: Normed Vector t => Vector t -> RealOf t -norm_1 = pnorm PNorm1 -norm_Inf :: Normed Vector t => Vector t -> RealOf t -norm_Inf = pnorm Infinity +instance Normed (Vector ℝ) + where + norm_0 v = sumElements (step (abs v - scalar (eps*normInf v))) + norm_1 = pnorm PNorm1 + norm_2 = pnorm PNorm2 + norm_Inf = pnorm Infinity -norm_0 :: Vector ℝ -> ℝ -norm_0 v = sumElements (step (abs v - scalar (eps*mx))) +instance Normed (Vector ℂ) where - mx = norm_Inf v + norm_0 v = sumElements (step (fst (fromComplex (abs v)) - scalar (eps*normInf v))) + norm_1 = pnorm PNorm1 + norm_2 = pnorm PNorm2 + norm_Inf = pnorm Infinity -norm_Frob :: Normed Matrix t => Matrix t -> RealOf t -norm_Frob = pnorm Frobenius +instance Normed (Matrix ℝ) + where + norm_0 = norm_0 . flatten + norm_1 = pnorm PNorm1 + norm_2 = pnorm PNorm2 + norm_Inf = pnorm Infinity -norm_nuclear :: Field t => Matrix t -> ℝ -norm_nuclear = sumElements . singularValues +instance Normed (Matrix ℂ) + where + norm_0 = norm_0 . flatten + norm_1 = pnorm PNorm1 + norm_2 = pnorm PNorm2 + norm_Inf = pnorm Infinity -mnorm_2 :: Normed Matrix t => Matrix t -> RealOf t -mnorm_2 = pnorm PNorm2 -mnorm_1 :: Normed Matrix t => Matrix t -> RealOf t -mnorm_1 = pnorm PNorm1 +norm_Frob :: (Normed (Vector t), Element t) => Matrix t -> ℝ +norm_Frob = norm_2 . flatten -mnorm_Inf :: Normed Matrix t => Matrix t -> RealOf t -mnorm_Inf = pnorm Infinity +norm_nuclear :: Field t => Matrix t -> ℝ +norm_nuclear = sumElements . singularValues -mnorm_0 :: Matrix ℝ -> ℝ -mnorm_0 = norm_0 . flatten -- | Obtains a vector in the same direction with 2-norm=1 unitary :: Vector Double -> Vector Double diff --git a/packages/tests/src/Numeric/LinearAlgebra/Tests.hs b/packages/tests/src/Numeric/LinearAlgebra/Tests.hs index 02beb21..cd88a24 100644 --- a/packages/tests/src/Numeric/LinearAlgebra/Tests.hs +++ b/packages/tests/src/Numeric/LinearAlgebra/Tests.hs @@ -27,7 +27,7 @@ module Numeric.LinearAlgebra.Tests( ) where import Numeric.LinearAlgebra -import Numeric.HMatrix hiding ((<>)) +import Numeric.HMatrix hiding ((<>),linearSolve) import Numeric.LinearAlgebra.Real(L) import Numeric.LinearAlgebra.Util(col,row) import Data.Packed -- cgit v1.2.3