+ sum [(m1 !!! (i,k)) NP.* (m2 !!! (k,j)) | k <- [0..(ncols m1)-1] ]
+
+
+
+instance (Ring.C a,
+ Vector v (w a),
+ Vector w a)
+ => Additive.C (Mat v w 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,
+ Vector v (w a),
+ Vector w a,
+ v ~ w)
+ => Ring.C (Mat v w a) where
+ -- The first * is ring multiplication, the second is matrix
+ -- multiplication.
+ m1 * m2 = m1 * m2
+
+
+instance (Ring.C a,
+ Vector v (w a),
+ Vector w a)
+ => Module.C a (Mat v w 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,
+ Vector v (w a),
+ Vector w a,
+ Vector v a,
+ Vector v [a])
+ => Normed (Mat v w 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. We don't use V.maximum here because it
+ -- relies on a type constraint that the vector be non-empty and I
+ -- don't know how to pattern match it away.
+ --
+ -- Examples:
+ --
+ -- >>> let v1 = vec3d (1,5,2)
+ -- >>> norm_infty v1
+ -- 5
+ --
+ norm_infty m@(Mat rows)
+ | nrows m == 0 || ncols m == 0 = 0
+ | otherwise =
+ fromRational' $ toRational $
+ P.maximum $ V.toList $ V.map (P.maximum . V.toList) 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 h = 0.5 :: Double
+-- >>> let g1 (Mat (D2 (D1 x) (D1 y))) = 1.0 + h NP.* exp(-(x^2))/(1.0 + y^2)
+-- >>> let g2 (Mat (D2 (D1 x) (D1 y))) = 0.5 + h NP.* atan(x^2 + y^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 D1 D1 a
+vec1d (x) = Mat (D1 (D1 x))
+
+vec2d :: (a,a) -> Mat D2 D1 a
+vec2d (x,y) = Mat (D2 (D1 x) (D1 y))
+
+vec3d :: (a,a,a) -> Mat D3 D1 a
+vec3d (x,y,z) = Mat (D3 (D1 x) (D1 y) (D1 z))
+
+vec4d :: (a,a,a,a) -> Mat D4 D1 a
+vec4d (w,x,y,z) = Mat (D4 (D1 w) (D1 x) (D1 y) (D1 z))
+
+-- Since we commandeered multiplication, we need to create 1x1
+-- matrices in order to multiply things.
+scalar :: a -> Mat D1 D1 a
+scalar x = Mat (D1 (D1 x))
+
+dot :: (RealRing.C a,
+ Dim w ~ N1,
+ Dim v ~ S n,
+ Vector v a,
+ Vector w a,
+ Vector w (v a),
+ Vector w (w a))
+ => Mat v w a
+ -> Mat v w 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,
+ Dim w ~ N1,
+ Dim v ~ S n,
+ Vector w (w a),
+ Vector v [a],
+ Vector v a,
+ Vector w a,
+ Vector v (w a),
+ Vector w (v a),
+ ToRational.C a)
+ => Mat v w a
+ -> Mat v w a
+ -> a
+angle v1 v2 =
+ acos theta
+ where
+ theta = (recip norms) NP.* (v1 `dot` v2)
+ norms = (norm v1) NP.* (norm v2)