+ 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,
+ Arity n)
+ => Normed (Mat (S m) (S n) a) where
+ -- | Generic p-norms. The usual norm in R^n is (norm_p 2). We treat
+ -- all matrices as big vectors.
+ --
+ -- 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
+
+
+
+
+
+-- 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) -> Mat N1 N1 a
+vec1d (x) = Mat (mk1 (mk1 x))
+
+vec2d :: (a,a) -> Mat N2 N1 a
+vec2d (x,y) = Mat (mk2 (mk1 x) (mk1 y))
+
+vec3d :: (a,a,a) -> Mat N3 N1 a
+vec3d (x,y,z) = Mat (mk3 (mk1 x) (mk1 y) (mk1 z))
+
+vec4d :: (a,a,a,a) -> Mat N4 N1 a
+vec4d (w,x,y,z) = Mat (mk4 (mk1 w) (mk1 x) (mk1 y) (mk1 z))
+
+vec5d :: (a,a,a,a,a) -> Mat N5 N1 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 -> Mat N1 N1 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)