diff options
author | Alberto Ruiz <aruiz@um.es> | 2014-06-06 20:01:57 +0200 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2014-06-06 20:01:57 +0200 |
commit | 57bf4ffaa426358a03839a2b6e4075e7dc000d9f (patch) | |
tree | 0298a32bda9f8cfe91e312d254bac1c84c0f8fdd /packages/base | |
parent | a40ed5c42f779561151b3119df0ebeddfcec183c (diff) |
norms
Diffstat (limited to 'packages/base')
-rw-r--r-- | packages/base/src/Numeric/HMatrix.hs | 7 | ||||
-rw-r--r-- | packages/base/src/Numeric/LinearAlgebra/Util.hs | 60 |
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 | |||
152 | import Numeric.Matrix() | 151 | import Numeric.Matrix() |
153 | import Numeric.Vector() | 152 | import Numeric.Vector() |
154 | import Data.Packed.Numeric hiding ((<>)) | 153 | import Data.Packed.Numeric hiding ((<>)) |
155 | import Numeric.LinearAlgebra.Algorithms hiding (linearSolve) | 154 | import Numeric.LinearAlgebra.Algorithms hiding (linearSolve,Normed) |
156 | import qualified Numeric.LinearAlgebra.Algorithms as A | 155 | import qualified Numeric.LinearAlgebra.Algorithms as A |
157 | import Numeric.LinearAlgebra.Util | 156 | import Numeric.LinearAlgebra.Util |
158 | import Numeric.LinearAlgebra.Random | 157 | 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( | |||
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 | ||
63 | import Data.Packed.Numeric | 62 | import Data.Packed.Numeric |
64 | import Numeric.LinearAlgebra.Algorithms hiding (i) | 63 | import Numeric.LinearAlgebra.Algorithms hiding (i,Normed) |
64 | --import qualified Numeric.LinearAlgebra.Algorithms as A | ||
65 | import Numeric.Matrix() | 65 | import Numeric.Matrix() |
66 | import Numeric.Vector() | 66 | import Numeric.Vector() |
67 | import Numeric.LinearAlgebra.Random | 67 | import Numeric.LinearAlgebra.Random |
@@ -225,37 +225,49 @@ norm :: Vector Double -> Double | |||
225 | -- ^ 2-norm of real vector | 225 | -- ^ 2-norm of real vector |
226 | norm = pnorm PNorm2 | 226 | norm = pnorm PNorm2 |
227 | 227 | ||
228 | norm_2 :: Normed Vector t => Vector t -> RealOf t | 228 | class Normed a |
229 | norm_2 = pnorm PNorm2 | 229 | where |
230 | norm_0 :: a -> ℝ | ||
231 | norm_1 :: a -> ℝ | ||
232 | norm_2 :: a -> ℝ | ||
233 | norm_Inf :: a -> ℝ | ||
230 | 234 | ||
231 | norm_1 :: Normed Vector t => Vector t -> RealOf t | ||
232 | norm_1 = pnorm PNorm1 | ||
233 | 235 | ||
234 | norm_Inf :: Normed Vector t => Vector t -> RealOf t | 236 | instance Normed (Vector ℝ) |
235 | norm_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 | ||
237 | norm_0 :: Vector ℝ -> ℝ | 243 | instance Normed (Vector ℂ) |
238 | norm_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 | ||
242 | norm_Frob :: Normed Matrix t => Matrix t -> RealOf t | 250 | instance Normed (Matrix ℝ) |
243 | norm_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 | ||
245 | norm_nuclear :: Field t => Matrix t -> ℝ | 257 | instance Normed (Matrix ℂ) |
246 | norm_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 | ||
248 | mnorm_2 :: Normed Matrix t => Matrix t -> RealOf t | ||
249 | mnorm_2 = pnorm PNorm2 | ||
250 | 264 | ||
251 | mnorm_1 :: Normed Matrix t => Matrix t -> RealOf t | 265 | norm_Frob :: (Normed (Vector t), Element t) => Matrix t -> ℝ |
252 | mnorm_1 = pnorm PNorm1 | 266 | norm_Frob = norm_2 . flatten |
253 | 267 | ||
254 | mnorm_Inf :: Normed Matrix t => Matrix t -> RealOf t | 268 | norm_nuclear :: Field t => Matrix t -> ℝ |
255 | mnorm_Inf = pnorm Infinity | 269 | norm_nuclear = sumElements . singularValues |
256 | 270 | ||
257 | mnorm_0 :: Matrix ℝ -> ℝ | ||
258 | mnorm_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 |
261 | unitary :: Vector Double -> Vector Double | 273 | unitary :: Vector Double -> Vector Double |