+ sum [(m1 !!! (i,k)) NP.* (m2 !!! (k,j)) | k <- [0..(ncols m1)-1] ]
+
+
+
+instance (Ring.C a, Arity m, Arity n) => Additive.C (Mat m n a) where
+
+ (Mat rows1) + (Mat rows2) =
+ Mat $ V.zipWith (V.zipWith (+)) rows1 rows2
+
+ (Mat rows1) - (Mat rows2) =
+ Mat $ V.zipWith (V.zipWith (-)) rows1 rows2
+
+ zero = Mat (V.replicate $ V.replicate (fromInteger 0))
+
+
+instance (Ring.C a, Arity m, Arity n, m ~ n) => Ring.C (Mat m n a) where
+ -- The first * is ring multiplication, the second is matrix
+ -- multiplication.
+ m1 * m2 = m1 * m2
+
+
+instance (Ring.C a, Arity m, Arity n) => Module.C a (Mat m n a) where
+ -- We can multiply a matrix by a scalar of the same type as its
+ -- elements.
+ x *> (Mat rows) = Mat $ V.map (V.map (NP.* x)) rows
+
+
+instance (Algebraic.C a,
+ ToRational.C a,
+ Arity m)
+ => Normed (Mat (S m) N1 a) where
+ -- | Generic p-norms for vectors in R^n that are represented as nx1
+ -- matrices.
+ --
+ -- Examples:
+ --
+ -- >>> let v1 = vec2d (3,4)
+ -- >>> norm_p 1 v1
+ -- 7.0
+ -- >>> norm_p 2 v1
+ -- 5.0
+ --
+ norm_p p (Mat rows) =
+ (root p') $ sum [fromRational' (toRational x)^p' | x <- xs]
+ where
+ p' = toInteger p
+ xs = concat $ V.toList $ V.map V.toList rows
+
+ -- | The infinity norm.
+ --
+ -- Examples:
+ --
+ -- >>> let v1 = vec3d (1,5,2)
+ -- >>> norm_infty v1
+ -- 5
+ --
+ norm_infty (Mat rows) =
+ 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
+-- column vectors, which are nx1 matrices.
+
+-- | Convenient constructor for 2D vectors.
+--
+-- Examples:
+--
+-- >>> import Roots.Simple
+-- >>> let fst m = m !!! (0,0)
+-- >>> let snd m = m !!! (1,0)
+-- >>> let h = 0.5 :: Double
+-- >>> let g1 m = 1.0 + h NP.* exp(-((fst m)^2))/(1.0 + (snd m)^2)
+-- >>> let g2 m = 0.5 + h NP.* atan((fst m)^2 + (snd m)^2)
+-- >>> let g u = vec2d ((g1 u), (g2 u))
+-- >>> let u0 = vec2d (1.0, 1.0)
+-- >>> let eps = 1/(10^9)
+-- >>> fixed_point g eps u0
+-- ((1.0728549599342185),(1.0820591495686167))
+--
+vec1d :: (a) -> Col1 a
+vec1d (x) = Mat (mk1 (mk1 x))
+
+vec2d :: (a,a) -> Col2 a
+vec2d (x,y) = Mat (mk2 (mk1 x) (mk1 y))
+
+vec3d :: (a,a,a) -> Col3 a
+vec3d (x,y,z) = Mat (mk3 (mk1 x) (mk1 y) (mk1 z))
+
+vec4d :: (a,a,a,a) -> Col4 a
+vec4d (w,x,y,z) = Mat (mk4 (mk1 w) (mk1 x) (mk1 y) (mk1 z))
+
+vec5d :: (a,a,a,a,a) -> Col5 a
+vec5d (v,w,x,y,z) = Mat (mk5 (mk1 v) (mk1 w) (mk1 x) (mk1 y) (mk1 z))
+
+-- Since we commandeered multiplication, we need to create 1x1
+-- matrices in order to multiply things.
+scalar :: a -> Mat1 a
+scalar x = Mat (mk1 (mk1 x))
+
+dot :: (RealRing.C a, n ~ N1, m ~ S t, Arity t)
+ => Mat m n a
+ -> Mat m n a
+ -> a
+v1 `dot` v2 = ((transpose v1) * v2) !!! (0, 0)
+
+
+-- | The angle between @v1@ and @v2@ in Euclidean space.
+--
+-- Examples:
+--
+-- >>> let v1 = vec2d (1.0, 0.0)
+-- >>> let v2 = vec2d (0.0, 1.0)
+-- >>> angle v1 v2 == pi/2.0
+-- True
+--
+angle :: (Transcendental.C a,
+ RealRing.C a,
+ n ~ N1,
+ m ~ S t,
+ Arity t,
+ ToRational.C a)
+ => Mat m n a
+ -> Mat m n a
+ -> a
+angle v1 v2 =
+ acos theta
+ where
+ theta = (recip norms) NP.* (v1 `dot` v2)
+ norms = (norm v1) NP.* (norm v2)
+
+
+-- | Retrieve the diagonal elements of the given matrix as a \"column
+-- vector,\" i.e. a m-by-1 matrix. We require the matrix to be
+-- square to avoid ambiguity in the return type which would ideally
+-- have dimension min(m,n) supposing an m-by-n matrix.
+--
+-- Examples:
+--
+-- >>> let m = fromList [[1,2,3],[4,5,6],[7,8,9]] :: Mat3 Int
+-- >>> diagonal m
+-- ((1),(5),(9))
+--
+diagonal :: (Arity m) => Mat m m a -> Col m a
+diagonal matrix =
+ construct lambda
+ where
+ lambda i _ = matrix !!! (i,i)
+
+
+-- | 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
+
+
+-- | Compute the trace of a square matrix, the sum of the elements
+-- which lie on its diagonal. We require the matrix to be
+-- square to avoid ambiguity in the return type which would ideally
+-- have dimension min(m,n) supposing an m-by-n matrix.
+--
+-- Examples:
+--
+-- >>> let m = fromList [[1,2,3],[4,5,6],[7,8,9]] :: Mat3 Int
+-- >>> trace m
+-- 15
+--
+trace :: (Arity m, Ring.C a) => Mat m m a -> a
+trace matrix =
+ let (Mat rows) = diagonal matrix
+ in
+ element_sum $ V.map V.head rows
+
+
+-- | Zip together two column matrices.
+--
+-- Examples:
+--
+-- >>> let m1 = fromList [[1],[1],[1]] :: Col3 Int
+-- >>> let m2 = fromList [[1],[2],[3]] :: Col3 Int
+-- >>> colzip m1 m2
+-- (((1,1)),((1,2)),((1,3)))
+--
+colzip :: Arity m => Col m a -> Col m a -> Col m (a,a)
+colzip c1 c2 =
+ construct lambda
+ where
+ lambda i j = (c1 !!! (i,j), c2 !!! (i,j))
+
+
+-- | Zip together two column 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
+-- ((1),(32),(729))
+--
+colzipwith :: Arity m
+ => (a -> a -> b)
+ -> Col m a
+ -> Col m a
+ -> Col m b
+colzipwith f c1 c2 =
+ construct lambda
+ where
+ lambda i j = f (c1 !!! (i,j)) (c2 !!! (i,j))
+
+
+-- | Map a function over a matrix of any dimensions.
+--
+-- Examples:
+--
+-- >>> let m = fromList [[1,2],[3,4]] :: Mat2 Int
+-- >>> matmap (^2) m
+-- ((1,4),(9,16))
+--
+matmap :: (a -> b) -> Mat m n a -> Mat m n b
+matmap f (Mat rows) =
+ Mat $ V.map g rows
+ where
+ g = V.map f