diff options
Diffstat (limited to 'packages/base/src/Numeric/LinearAlgebra')
-rw-r--r-- | packages/base/src/Numeric/LinearAlgebra/Util.hs | 60 |
1 files changed, 36 insertions, 24 deletions
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 |