summaryrefslogtreecommitdiff
path: root/packages/base
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2014-06-06 20:01:57 +0200
committerAlberto Ruiz <aruiz@um.es>2014-06-06 20:01:57 +0200
commit57bf4ffaa426358a03839a2b6e4075e7dc000d9f (patch)
tree0298a32bda9f8cfe91e312d254bac1c84c0f8fdd /packages/base
parenta40ed5c42f779561151b3119df0ebeddfcec183c (diff)
norms
Diffstat (limited to 'packages/base')
-rw-r--r--packages/base/src/Numeric/HMatrix.hs7
-rw-r--r--packages/base/src/Numeric/LinearAlgebra/Util.hs60
2 files changed, 39 insertions, 28 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 (
121 orth, 121 orth,
122 122
123 -- * Norms 123 -- * Norms
124 norm_0, norm_1, norm_2, norm_Inf, 124 Normed(..),
125 mnorm_0, mnorm_1, mnorm_2, mnorm_Inf,
126 norm_Frob, norm_nuclear, 125 norm_Frob, norm_nuclear,
127 126
128 -- * Correlation and convolution 127 -- * Correlation and convolution
@@ -140,7 +139,7 @@ module Numeric.HMatrix (
140 RealOf, ComplexOf, SingleOf, DoubleOf, 139 RealOf, ComplexOf, SingleOf, DoubleOf,
141 IndexOf, 140 IndexOf,
142 Field, 141 Field,
143 Normed, 142-- Normed,
144 Transposable, 143 Transposable,
145 CGState(..), 144 CGState(..),
146 Testable(..), 145 Testable(..),
@@ -152,7 +151,7 @@ import Numeric.LinearAlgebra.Data
152import Numeric.Matrix() 151import Numeric.Matrix()
153import Numeric.Vector() 152import Numeric.Vector()
154import Data.Packed.Numeric hiding ((<>)) 153import Data.Packed.Numeric hiding ((<>))
155import Numeric.LinearAlgebra.Algorithms hiding (linearSolve) 154import Numeric.LinearAlgebra.Algorithms hiding (linearSolve,Normed)
156import qualified Numeric.LinearAlgebra.Algorithms as A 155import qualified Numeric.LinearAlgebra.Algorithms as A
157import Numeric.LinearAlgebra.Util 156import Numeric.LinearAlgebra.Util
158import Numeric.LinearAlgebra.Random 157import 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(
33 cross, 33 cross,
34 norm, 34 norm,
35 ℕ,ℤ,ℝ,ℂ,𝑖,i_C, --ℍ 35 ℕ,ℤ,ℝ,ℂ,𝑖,i_C, --ℍ
36 norm_1, norm_2, norm_0, norm_Inf, norm_Frob, norm_nuclear, 36 Normed(..), norm_Frob, norm_nuclear,
37 mnorm_1, mnorm_2, mnorm_0, mnorm_Inf,
38 unitary, 37 unitary,
39 mt, 38 mt,
40 (~!~), 39 (~!~),
@@ -61,7 +60,8 @@ module Numeric.LinearAlgebra.Util(
61) where 60) where
62 61
63import Data.Packed.Numeric 62import Data.Packed.Numeric
64import Numeric.LinearAlgebra.Algorithms hiding (i) 63import Numeric.LinearAlgebra.Algorithms hiding (i,Normed)
64--import qualified Numeric.LinearAlgebra.Algorithms as A
65import Numeric.Matrix() 65import Numeric.Matrix()
66import Numeric.Vector() 66import Numeric.Vector()
67import Numeric.LinearAlgebra.Random 67import Numeric.LinearAlgebra.Random
@@ -225,37 +225,49 @@ norm :: Vector Double -> Double
225-- ^ 2-norm of real vector 225-- ^ 2-norm of real vector
226norm = pnorm PNorm2 226norm = pnorm PNorm2
227 227
228norm_2 :: Normed Vector t => Vector t -> RealOf t 228class Normed a
229norm_2 = pnorm PNorm2 229 where
230 norm_0 :: a -> ℝ
231 norm_1 :: a -> ℝ
232 norm_2 :: a -> ℝ
233 norm_Inf :: a -> ℝ
230 234
231norm_1 :: Normed Vector t => Vector t -> RealOf t
232norm_1 = pnorm PNorm1
233 235
234norm_Inf :: Normed Vector t => Vector t -> RealOf t 236instance Normed (Vector ℝ)
235norm_Inf = pnorm Infinity 237 where
238 norm_0 v = sumElements (step (abs v - scalar (eps*normInf v)))
239 norm_1 = pnorm PNorm1
240 norm_2 = pnorm PNorm2
241 norm_Inf = pnorm Infinity
236 242
237norm_0 :: Vector ℝ -> ℝ 243instance Normed (Vector ℂ)
238norm_0 v = sumElements (step (abs v - scalar (eps*mx)))
239 where 244 where
240 mx = norm_Inf v 245 norm_0 v = sumElements (step (fst (fromComplex (abs v)) - scalar (eps*normInf v)))
246 norm_1 = pnorm PNorm1
247 norm_2 = pnorm PNorm2
248 norm_Inf = pnorm Infinity
241 249
242norm_Frob :: Normed Matrix t => Matrix t -> RealOf t 250instance Normed (Matrix ℝ)
243norm_Frob = pnorm Frobenius 251 where
252 norm_0 = norm_0 . flatten
253 norm_1 = pnorm PNorm1
254 norm_2 = pnorm PNorm2
255 norm_Inf = pnorm Infinity
244 256
245norm_nuclear :: Field t => Matrix t -> ℝ 257instance Normed (Matrix ℂ)
246norm_nuclear = sumElements . singularValues 258 where
259 norm_0 = norm_0 . flatten
260 norm_1 = pnorm PNorm1
261 norm_2 = pnorm PNorm2
262 norm_Inf = pnorm Infinity
247 263
248mnorm_2 :: Normed Matrix t => Matrix t -> RealOf t
249mnorm_2 = pnorm PNorm2
250 264
251mnorm_1 :: Normed Matrix t => Matrix t -> RealOf t 265norm_Frob :: (Normed (Vector t), Element t) => Matrix t ->
252mnorm_1 = pnorm PNorm1 266norm_Frob = norm_2 . flatten
253 267
254mnorm_Inf :: Normed Matrix t => Matrix t -> RealOf t 268norm_nuclear :: Field t => Matrix t ->
255mnorm_Inf = pnorm Infinity 269norm_nuclear = sumElements . singularValues
256 270
257mnorm_0 :: Matrix ℝ -> ℝ
258mnorm_0 = norm_0 . flatten
259 271
260-- | Obtains a vector in the same direction with 2-norm=1 272-- | Obtains a vector in the same direction with 2-norm=1
261unitary :: Vector Double -> Vector Double 273unitary :: Vector Double -> Vector Double