summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--packages/base/src/Numeric/HMatrix.hs7
-rw-r--r--packages/base/src/Numeric/LinearAlgebra/Util.hs60
-rw-r--r--packages/tests/src/Numeric/LinearAlgebra/Tests.hs2
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 (
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
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(
27) where 27) where
28 28
29import Numeric.LinearAlgebra 29import Numeric.LinearAlgebra
30import Numeric.HMatrix hiding ((<>)) 30import Numeric.HMatrix hiding ((<>),linearSolve)
31import Numeric.LinearAlgebra.Real(L) 31import Numeric.LinearAlgebra.Real(L)
32import Numeric.LinearAlgebra.Util(col,row) 32import Numeric.LinearAlgebra.Util(col,row)
33import Data.Packed 33import Data.Packed