N5,
S,
Z,
+ generate,
mk1,
mk2,
mk3,
zipWith
)
import Data.Vector.Fixed.Boxed (Vec)
-import Data.Vector.Fixed.Internal.Arity (Arity, arity)
+import Data.Vector.Fixed.Cont (Arity, arity)
import Linear.Vector
import Normed
-- entries in the matrix. The i,j entry of the resulting matrix will
-- have the value returned by lambda i j.
--
--- TODO: Don't cheat with fromList.
---
-- Examples:
--
-- >>> let lambda i j = i + j
--
construct :: forall m n a. (Arity m, Arity n)
=> (Int -> Int -> a) -> Mat m n a
-construct lambda = Mat rows
+construct lambda = Mat $ generate make_row
where
- -- The arity trick is used in Data.Vector.Fixed.length.
- imax = (arity (undefined :: m)) - 1
- jmax = (arity (undefined :: n)) - 1
- row' i = V.fromList [ lambda i j | j <- [0..jmax] ]
- rows = V.fromList [ row' i | i <- [0..imax] ]
+ make_row :: Int -> Vec n a
+ make_row i = generate (lambda i)
+-- | Create an identity matrix with the right dimensions.
+--
+-- Examples:
+--
+-- >>> identity_matrix :: Mat3 Int
+-- ((1,0,0),(0,1,0),(0,0,1))
+-- >>> identity_matrix :: Mat3 Double
+-- ((1.0,0.0,0.0),(0.0,1.0,0.0),(0.0,0.0,1.0))
+--
+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.