X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FLinear%2FMatrix.hs;h=d166e44367e626b9d87979a4b0e9ab7b2b24e7d9;hb=3cb0f69fa38e5f46a2374d48168acf69bfc9e910;hp=dbd321fefc6e076ade9344495ddfd1c6bd26858c;hpb=c22f72ffaa4edec587b28a7c22aa07330349fd73;p=numerical-analysis.git diff --git a/src/Linear/Matrix.hs b/src/Linear/Matrix.hs index dbd321f..d166e44 100644 --- a/src/Linear/Matrix.hs +++ b/src/Linear/Matrix.hs @@ -17,6 +17,7 @@ where import Data.List (intercalate) import Data.Vector.Fixed ( + (!), N1, N2, N3, @@ -42,7 +43,7 @@ import qualified Data.Vector.Fixed as V ( zipWith ) import Data.Vector.Fixed.Boxed (Vec) -import Data.Vector.Fixed.Internal (Arity, arity) +import Data.Vector.Fixed.Cont (Arity, arity) import Linear.Vector import Normed @@ -416,9 +417,8 @@ instance (Ring.C a, Arity m, Arity n) => Module.C a (Mat m n a) where instance (Algebraic.C a, ToRational.C a, - Arity m, - Arity n) - => Normed (Mat (S m) (S n) a) where + 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. -- @@ -431,7 +431,7 @@ instance (Algebraic.C a, -- 5.0 -- norm_p p (Mat rows) = - (root p') $ sum [(fromRational' $ toRational x)^p' | x <- xs] + (root p') $ sum [fromRational' (toRational x)^p' | x <- xs] where p' = toInteger p xs = concat $ V.toList $ V.map V.toList rows @@ -520,3 +520,93 @@ angle v1 v2 = where theta = (recip norms) NP.* (v1 `dot` v2) norms = (norm v1) NP.* (norm v2) + + + +-- | Given a square @matrix@, return a new matrix of the same size +-- containing only the on-diagonal entries of @matrix@. The +-- off-diagonal entries are set to zero. +-- +-- Examples: +-- +-- >>> let m = fromList [[1,2,3],[4,5,6],[7,8,9]] :: Mat3 Int +-- >>> diagonal_part m +-- ((1,0,0),(0,5,0),(0,0,9)) +-- +diagonal_part :: (Arity m, Ring.C a) + => Mat m m a + -> Mat m m a +diagonal_part matrix = + construct lambda + where + lambda i j = if i == j then matrix !!! (i,j) else 0 + + +-- | Given a square @matrix@, return a new matrix of the same size +-- containing only the on-diagonal and below-diagonal entries of +-- @matrix@. The above-diagonal entries are set to zero. +-- +-- Examples: +-- +-- >>> let m = fromList [[1,2,3],[4,5,6],[7,8,9]] :: Mat3 Int +-- >>> lt_part m +-- ((1,0,0),(4,5,0),(7,8,9)) +-- +lt_part :: (Arity m, Ring.C a) + => Mat m m a + -> Mat m m a +lt_part matrix = + construct lambda + where + lambda i j = if i >= j then matrix !!! (i,j) else 0 + + +-- | Given a square @matrix@, return a new matrix of the same size +-- containing only the below-diagonal entries of @matrix@. The on- +-- and above-diagonal entries are set to zero. +-- +-- Examples: +-- +-- >>> let m = fromList [[1,2,3],[4,5,6],[7,8,9]] :: Mat3 Int +-- >>> lt_part_strict m +-- ((0,0,0),(4,0,0),(7,8,0)) +-- +lt_part_strict :: (Arity m, Ring.C a) + => Mat m m a + -> Mat m m a +lt_part_strict matrix = + construct lambda + where + lambda i j = if i > j then matrix !!! (i,j) else 0 + + +-- | Given a square @matrix@, return a new matrix of the same size +-- containing only the on-diagonal and above-diagonal entries of +-- @matrix@. The below-diagonal entries are set to zero. +-- +-- Examples: +-- +-- >>> let m = fromList [[1,2,3],[4,5,6],[7,8,9]] :: Mat3 Int +-- >>> ut_part m +-- ((1,2,3),(0,5,6),(0,0,9)) +-- +ut_part :: (Arity m, Ring.C a) + => Mat m m a + -> Mat m m a +ut_part = transpose . lt_part . transpose + + +-- | Given a square @matrix@, return a new matrix of the same size +-- containing only the above-diagonal entries of @matrix@. The on- +-- and below-diagonal entries are set to zero. +-- +-- Examples: +-- +-- >>> let m = fromList [[1,2,3],[4,5,6],[7,8,9]] :: Mat3 Int +-- >>> ut_part_strict m +-- ((0,2,3),(0,0,6),(0,0,0)) +-- +ut_part_strict :: (Arity m, Ring.C a) + => Mat m m a + -> Mat m m a +ut_part_strict = transpose . lt_part_strict . transpose