X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FLinear%2FMatrix.hs;h=119d77089461dfae968320c79bd1375c31d46b41;hb=342fb80c0a34bd362a51fd62989eeef462b58721;hp=c33e96691b00f9cb2c87c3332ab572a32286d10b;hpb=f9a18f9f5685f86697475529033096e8c6f6e627;p=numerical-analysis.git diff --git a/src/Linear/Matrix.hs b/src/Linear/Matrix.hs index c33e966..119d770 100644 --- a/src/Linear/Matrix.hs +++ b/src/Linear/Matrix.hs @@ -37,6 +37,7 @@ import qualified Data.Vector.Fixed as V ( fromList, head, ifoldl, + ifoldr, imap, map, maximum, @@ -662,11 +663,16 @@ vec5d (v,w,x,y,z) = Mat (mk5 (mk1 v) (mk1 w) (mk1 x) (mk1 y) (mk1 z)) scalar :: a -> Mat1 a scalar x = Mat (mk1 (mk1 x)) -dot :: (RealRing.C a, m ~ S t, Arity t) - => Col m a - -> Col m a +-- Get the scalar value out of a 1x1 matrix. +unscalar :: Mat1 a -> a +unscalar (Mat rows) = V.head $ V.head rows + + +dot :: (Ring.C a, Arity m) + => Col (S m) a + -> Col (S m) a -> a -v1 `dot` v2 = ((transpose v1) * v2) !!! (0, 0) +v1 `dot` v2 = unscalar $ ((transpose v1) * v2) -- | The angle between @v1@ and @v2@ in Euclidean space. @@ -818,37 +824,73 @@ trace matrix = element_sum $ V.map V.head rows --- | Zip together two column matrices. +-- | Zip together two matrices. +-- +-- TODO: don't cheat with construct (map V.zips instead). -- -- Examples: -- -- >>> let m1 = fromList [[1],[1],[1]] :: Col3 Int -- >>> let m2 = fromList [[1],[2],[3]] :: Col3 Int --- >>> colzip m1 m2 +-- >>> zip2 m1 m2 -- (((1,1)),((1,2)),((1,3))) -- -colzip :: Arity m => Col m a -> Col m a -> Col m (a,a) -colzip c1 c2 = +-- >>> let m1 = fromList [[1,2],[3,4]] :: Mat2 Int +-- >>> let m2 = fromList [[1,1],[1,1]] :: Mat2 Int +-- >>> zip2 m1 m2 +-- (((1,1),(2,1)),((3,1),(4,1))) +-- +zip2 :: (Arity m, Arity n) => Mat m n a -> Mat m n a -> Mat m n (a,a) +zip2 m1 m2 = + construct lambda + where + lambda i j = (m1 !!! (i,j), m2 !!! (i,j)) + + +-- | Zip together three matrices. +-- +-- TODO: don't cheat with construct (map V.zips instead). +-- +-- Examples: +-- +-- >>> let m1 = fromList [[1],[1],[1]] :: Col3 Int +-- >>> let m2 = fromList [[1],[2],[3]] :: Col3 Int +-- >>> let m3 = fromList [[4],[5],[6]] :: Col3 Int +-- >>> zip2three m1 m2 m3 +-- (((1,1,4)),((1,2,5)),((1,3,6))) +-- +-- >>> let m1 = fromList [[1,2],[3,4]] :: Mat2 Int +-- >>> let m2 = fromList [[1,1],[1,1]] :: Mat2 Int +-- >>> let m3 = fromList [[8,2],[6,3]] :: Mat2 Int +-- >>> zip2three m1 m2 m3 +-- (((1,1,8),(2,1,2)),((3,1,6),(4,1,3))) +-- +zip2three :: (Arity m, Arity n) + => Mat m n a + -> Mat m n a + -> Mat m n a + -> Mat m n (a,a,a) +zip2three m1 m2 m3 = construct lambda where - lambda i j = (c1 !!! (i,j), c2 !!! (i,j)) + lambda i j = (m1 !!! (i,j), m2 !!! (i,j), m3 !!! (i,j)) --- | Zip together two column matrices using the supplied function. +-- | Zip together two matrices using the supplied function. -- -- Examples: -- -- >>> let c1 = fromList [[1],[2],[3]] :: Col3 Integer -- >>> let c2 = fromList [[4],[5],[6]] :: Col3 Integer --- >>> colzipwith (^) c1 c2 +-- >>> zipwith2 (^) c1 c2 -- ((1),(32),(729)) -- -colzipwith :: Arity m +zipwith2 :: Arity m => (a -> a -> b) -> Col m a -> Col m a -> Col m b -colzipwith f c1 c2 = +zipwith2 f c1 c2 = construct lambda where lambda i j = f (c1 !!! (i,j)) (c2 !!! (i,j)) @@ -870,7 +912,8 @@ map2 f (Mat rows) = -- | Fold over the entire matrix passing the coordinates @i@ and @j@ --- (of the row/column) to the accumulation function. +-- (of the row/column) to the accumulation function. The fold occurs +-- from top-left to bottom-right. -- -- Examples: -- @@ -897,6 +940,36 @@ ifoldl2 f initial (Mat rows) = row_function rowinit idx r = V.ifoldl (g idx) rowinit r +-- | Fold over the entire matrix passing the coordinates @i@ and @j@ +-- (of the row/column) to the accumulation function. The fold occurs +-- from bottom-right to top-left. +-- +-- The order of the arguments in the supplied function are different +-- from those in V.ifoldr; we keep them similar to ifoldl2. +-- +-- Examples: +-- +-- >>> let m = fromList [[1,2,3],[4,5,6],[7,8,9]] :: Mat3 Int +-- >>> ifoldr2 (\i j cur _ -> cur + i + j) 0 m +-- 18 +-- +ifoldr2 :: forall a b m n. + (Int -> Int -> b -> a -> b) + -> b + -> Mat m n a + -> b +ifoldr2 f initial (Mat rows) = + V.ifoldr row_function initial rows + where + -- | Swap the order of arguments in @f@ so that it agrees with the + -- @f@ passed to ifoldl2. + g :: Int -> Int -> a -> b -> b + g w x y z = f w x z y + + row_function :: Int -> Vec n a -> b -> b + row_function idx r rowinit = V.ifoldr (g idx) rowinit r + + -- | Map a function over a matrix of any dimensions, passing the -- coordinates @i@ and @j@ to the function @f@. -- @@ -931,3 +1004,27 @@ imap2 f (Mat rows) = -- reverse2 :: (Arity m, Arity n) => Mat m n a -> Mat m n a reverse2 (Mat rows) = Mat $ V.reverse $ V.map V.reverse rows + + +-- | Unsafely set the (i,j) element of the given matrix. +-- +-- Examples: +-- +-- >>> let m = fromList [[1,2,3],[4,5,6],[7,8,9]] :: Mat3 Int +-- >>> set_idx m (1,1) 17 +-- ((1,2,3),(4,17,6),(7,8,9)) +-- +set_idx :: forall m n a. + (Arity m, Arity n) + => Mat m n a + -> (Int, Int) + -> a + -> Mat m n a +set_idx matrix (i,j) newval = + imap2 updater matrix + where + updater :: Int -> Int -> a -> a + updater k l existing = + if k == i && l == j + then newval + else existing