X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FLinear%2FMatrix.hs;h=a5fbb8403b0832becf04b0d48a68be4f0725345c;hb=9ef91ad4ec3a5c0966f0850d40310722b6c38b68;hp=660330245b330b840bcb848af512b7d6561e217f;hpb=4b5f216ca9e1890c40bbf8053cfed94e3961849f;p=numerical-analysis.git diff --git a/src/Linear/Matrix.hs b/src/Linear/Matrix.hs index 6603302..a5fbb84 100644 --- a/src/Linear/Matrix.hs +++ b/src/Linear/Matrix.hs @@ -152,60 +152,47 @@ fromList vs = Mat (V.fromList $ map V.fromList vs) -- | Unsafe indexing. (!!!) :: (Arity m, Arity n) => Mat m n a -> (Int, Int) -> a -(!!!) m (i, j) = (row m i) ! j +(!!!) (Mat rows) (i, j) = (rows ! i) ! j + -- | Safe indexing. -(!!?) :: Mat m n a -> (Int, Int) -> Maybe a -(!!?) m@(Mat rows) (i, j) +(!!?) :: (Arity m, Arity n) => Mat m n a -> (Int, Int) -> Maybe a +(!!?) matrix (i, j) | i < 0 || j < 0 = Nothing - | i > V.length rows = Nothing - | otherwise = if j > V.length (row m j) - then Nothing - else Just $ (row m j) ! j + | i > (nrows matrix) - 1 = Nothing + | j > (ncols matrix) - 1 = Nothing + | otherwise = Just $ matrix !!! (i,j) -- | The number of rows in the matrix. nrows :: forall m n a. (Arity m) => Mat m n a -> Int nrows _ = arity (undefined :: m) + -- | The number of columns in the first row of the -- matrix. Implementation stolen from Data.Vector.Fixed.length. ncols :: forall m n a. (Arity n) => Mat m n a -> Int ncols _ = arity (undefined :: n) --- | Return the @i@th row of @m@. Unsafe. -row :: Mat m n a -> Int -> (Vec n a) -row (Mat rows) i = rows ! i - - -- | Return the @i@th row of @m@ as a matrix. Unsafe. -row' :: (Arity m, Arity n) => Mat m n a -> Int -> Row n a -row' m i = +row :: (Arity m, Arity n) => Mat m n a -> Int -> Row n a +row m i = construct lambda where lambda _ j = m !!! (i, j) --- | Return the @j@th column of @m@. Unsafe. -column :: Mat m n a -> Int -> (Vec m a) -column (Mat rows) j = - V.map (element j) rows - where - element = flip (!) - - -- | Return the @j@th column of @m@ as a matrix. Unsafe. -column' :: (Arity m, Arity n) => Mat m n a -> Int -> Col m a -column' m j = +column :: (Arity m, Arity n) => Mat m n a -> Int -> Col m a +column m j = construct lambda where lambda i _ = m !!! (i, j) -- | Transpose @m@; switch it's columns and its rows. This is a dirty --- implementation.. it would be a little cleaner to use imap, but it --- doesn't seem to work. +-- implementation, but I don't see a better way. -- -- TODO: Don't cheat with fromList. -- @@ -216,9 +203,10 @@ column' m j = -- ((1,3),(2,4)) -- transpose :: (Arity m, Arity n) => Mat m n a -> Mat n m a -transpose m = Mat $ V.fromList column_list +transpose matrix = + construct lambda where - column_list = [ column m i | i <- [0..(ncols m)-1] ] + lambda i j = matrix !!! (j,i) -- | Is @m@ symmetric? @@ -270,6 +258,7 @@ identity_matrix :: (Arity m, Ring.C a) => Mat m m a identity_matrix = construct (\i j -> if i == j then (fromInteger 1) else (fromInteger 0)) + -- | Given a positive-definite matrix @m@, computes the -- upper-triangular matrix @r@ with (transpose r)*r == m and all -- values on the diagonal of @r@ positive. @@ -816,11 +805,11 @@ colzipwith f c1 c2 = -- Examples: -- -- >>> let m = fromList [[1,2],[3,4]] :: Mat2 Int --- >>> matmap (^2) m +-- >>> map2 (^2) m -- ((1,4),(9,16)) -- -matmap :: (a -> b) -> Mat m n a -> Mat m n b -matmap f (Mat rows) = +map2 :: (a -> b) -> Mat m n a -> Mat m n b +map2 f (Mat rows) = Mat $ V.map g rows where g = V.map f