]> gitweb.michael.orlitzky.com - numerical-analysis.git/blobdiff - src/Linear/Matrix.hs
Switch Linear.Vector to the numeric prelude and add the element_sum function to it.
[numerical-analysis.git] / src / Linear / Matrix.hs
index c0f56b348f4d4d2fbdf272cbac05e7c799eddce7..69a74b50eeacf7ea944c790d3858dd0cba12c266 100644 (file)
@@ -2,6 +2,7 @@
 {-# LANGUAGE FlexibleContexts #-}
 {-# LANGUAGE FlexibleInstances #-}
 {-# LANGUAGE MultiParamTypeClasses #-}
+{-# LANGUAGE NoMonomorphismRestriction #-}
 {-# LANGUAGE ScopedTypeVariables #-}
 {-# LANGUAGE TypeFamilies #-}
 {-# LANGUAGE RebindableSyntax #-}
@@ -43,7 +44,6 @@ import qualified Data.Vector.Fixed as V (
   toList,
   zipWith
   )
-import Data.Vector.Fixed.Boxed (Vec)
 import Data.Vector.Fixed.Cont (Arity, arity)
 import Linear.Vector
 import Normed
@@ -428,8 +428,8 @@ instance (Algebraic.C a,
           ToRational.C a,
           Arity m)
          => Normed (Mat (S m) N1 a) where
-  -- | Generic p-norms. The usual norm in R^n is (norm_p 2). We treat
-  --   all matrices as big vectors.
+  -- | Generic p-norms for vectors in R^n that are represented as nx1
+  --   matrices.
   --
   --   Examples:
   --
@@ -457,7 +457,26 @@ instance (Algebraic.C a,
     fromRational' $ toRational $ V.maximum $ V.map V.maximum rows
 
 
-
+-- | Compute the Frobenius norm of a matrix. This essentially treats
+--   the matrix as one long vector containing all of its entries (in
+--   any order, it doesn't matter).
+--
+--   Examples:
+--
+--   >>> let m = fromList [[1, 2, 3],[4,5,6],[7,8,9]] :: Mat3 Double
+--   >>> frobenius_norm m == sqrt 285
+--   True
+--
+--   >>> let m = fromList [[1, -1, 1],[-1,1,-1],[1,-1,1]] :: Mat3 Double
+--   >>> frobenius_norm m == 3
+--   True
+--
+frobenius_norm :: (Algebraic.C a, Ring.C a) => Mat m n a -> a
+frobenius_norm (Mat rows) =
+  sqrt $ element_sum $ V.map row_sum rows
+  where
+    -- | Square and add up the entries of a row.
+    row_sum = element_sum . V.map (^2)
 
 
 -- Vector helpers. We want it to be easy to create low-dimension