qlinear 0.1.0.1 → 0.1.2.0
raw patch · 3 files changed
+41/−28 lines, 3 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
+ QLinear.Operations: infixl 6 ~-~
+ QLinear.Operations: infixl 7 ~*~
- QLinear.Operations: length :: (Real a, Floating b) => Vector n a -> b
+ QLinear.Operations: length :: forall a b n. (Real a, Floating b) => Vector n a -> b
Files
- qlinear.cabal +1/−1
- src/QLinear/Operations.hs +18/−5
- test/Main.hs +22/−22
qlinear.cabal view
@@ -1,6 +1,6 @@ cabal-version: 3.0 name: qlinear-version: 0.1.0.1+version: 0.1.2.0 synopsis: Typesafe library for linear algebra description: Please see the README on GitHub at <https://github.com/JuniorGarbageCollector/QLinear> category: Math
src/QLinear/Operations.hs view
@@ -1,5 +1,5 @@ {-# LANGUAGE RankNTypes #-}-+{-# LANGUAGE ScopedTypeVariables #-} module QLinear.Operations ( length, mulMatricesWith,@@ -35,6 +35,8 @@ (~+~) :: Num a => Matrix m n a -> Matrix m n a -> Matrix m n a (~+~) = zipMatricesWith (+) +infixl 6 ~+~+ -- | Multuplies all elements of matrix __m__ by __k__ -- -- >>> 5 *~ [matrix| 1 2 3; 4 5 6 |]@@ -49,6 +51,8 @@ Matrix m n a (*~) n = fmap (n *) +infixl 7 *~+ -- | Adds __a__ to all elements of matrix __m__ -- -- >>> [matrix| 1 2 3 |] ~+ 8@@ -60,10 +64,14 @@ Matrix m n a (~+) m n = (+ n) <$> m +infixl 6 ~++ -- | Flipped __~+__ :) (+~) :: Num a => a -> Matrix m n a -> Matrix m n a (+~) = flip (~+) +infixl 6 +~+ -- | Substracts second matrix from first one -- -- >>> [matrix| 1 2 3 |] ~-~ [matrix| 3 2 1 |]@@ -71,6 +79,9 @@ (~-~) :: Num a => Matrix m n a -> Matrix m n a -> Matrix m n a (~-~) = zipMatricesWith (-) +infixl 6 ~-~++ -- | Multiplies two matrix -- -- >>> [matrix| 1 2; 3 4 |] ~*~ [matrix| 1; 2 |]@@ -79,6 +90,8 @@ (~*~) :: Num a => Matrix m n a -> Matrix n k a -> Matrix m k a (~*~) = mulMatricesWith (*) (+) +infixl 7 ~*~+ -- | Generalized matrices multiplication mulMatricesWith :: -- | operation "__*__"@@ -124,10 +137,10 @@ -- 5.0 -- >>> length [vector| 1 1 |] -- 1.4142135623730951-length :: (Real a, Floating b) => Vector n a -> b+length :: forall a b n. (Real a, Floating b) => Vector n a -> b length (Matrix _ matrix) = sqrt $ sum $ squares where- toFloating = realToFrac :: (Real a, Floating b) => a -> b+ toFloating = realToFrac :: a -> b squares = map ((** 2) . toFloating) $ concat matrix -- | Inverted matrix@@ -140,10 +153,10 @@ inverted :: forall a b n. (Fractional b, Eq a, Real a) => Matrix n n a -> Maybe (Matrix n n b) inverted (Matrix size@(1, 1) [[a]]) = if a /= 0 then Just (Matrix size [[1.0 / toFloating a]]) else Nothing where- toFloating = realToFrac :: (Real a, Fractional b) => a -> b+ toFloating = realToFrac :: a -> b inverted matrix = if determinant /= 0 then Just $ ((invertedDet *) . toFloating) <$> adj else Nothing where- toFloating = realToFrac :: (Real a, Fractional b) => a -> b+ toFloating = realToFrac :: a -> b determinant = det matrix invertedDet = 1.0 / toFloating determinant adj = adjugate matrix
test/Main.hs view
@@ -65,60 +65,60 @@ it "two matrices" do {- will not be compiled -} -- [matrix| 1 2 |] ~+~ [matrix| 1 2 3 |] `matrixEq` undefined- [matrix| 1 2 |] ~+~ [matrix| 3 4 |] `matrixEq` [matrix| 4 6 |]- [matrix| 1 2; 3 4 |] ~+~ [matrix| 3 4; 5 6 |] `matrixEq` [matrix| 4 6; 8 10 |]+ ([matrix| 1 2 |] ~+~ [matrix| 3 4 |]) `matrixEq` [matrix| 4 6 |]+ ([matrix| 1 2; 3 4 |] ~+~ [matrix| 3 4; 5 6 |]) `matrixEq` [matrix| 4 6; 8 10 |] it "matrix and identity matrix" do {- will not be compiled -} -- [matrix| 1 2 |] ~+~ e `matrixEq` undefined- [matrix| 1 2; 3 4 |] ~+~ e `matrixEq` [matrix| 2 2; 3 5 |]- e ~+~ [matrix| 1 2; 3 4 |] `matrixEq` [matrix| 2 2; 3 5 |]+ ([matrix| 1 2; 3 4 |] ~+~ e) `matrixEq` [matrix| 2 2; 3 5 |]+ (e ~+~ [matrix| 1 2; 3 4 |]) `matrixEq` [matrix| 2 2; 3 5 |] it "two identity matrices" do {- will not be compiled -} -- (e :: Matrix 3 3 Int) ~+~ (e :: Matrix 4 4 Int) `matrixEq` undefined- (e :: Matrix 3 3 Int) ~+~ e `matrixEq` [matrix| 2 0 0; 0 2 0; 0 0 2 |]+ ((e :: Matrix 3 3 Int) ~+~ e) `matrixEq` [matrix| 2 0 0; 0 2 0; 0 0 2 |] it "two vectors" do {- will not be compiled -} -- [vector| 1 2 |] ~+~ [vector| 1 2 3 |] `matrixEq` undefined- [vector| 1 2 |] ~+~ [vector| 3 4 |] `matrixEq` [vector| 4 6 |]+ ([vector| 1 2 |] ~+~ [vector| 3 4 |]) `matrixEq` [vector| 4 6 |] describe "Substaction" do it "two matrices" do {- will not be compiled -} -- [matrix| 1 2 |] ~-~ [matrix| 1 2 3 |] `matrixEq` undefined- [matrix| 1 2 |] ~-~ [matrix| 3 4 |] `matrixEq` [matrix| -2 -2 |]- [matrix| 1 2; 3 4 |] ~-~ [matrix| 3 4; 5 6 |] `matrixEq` [matrix| -2 -2; -2 -2 |]+ ([matrix| 1 2 |] ~-~ [matrix| 3 4 |]) `matrixEq` [matrix| -2 -2 |]+ ([matrix| 1 2; 3 4 |] ~-~ [matrix| 3 4; 5 6 |]) `matrixEq` [matrix| -2 -2; -2 -2 |] it "matrix and identity matrix" do {- will not be compiled -} -- [matrix| 1 2 |] ~-~ e `matrixEq` undefined- [matrix| 1 2; 3 4 |] ~-~ e `matrixEq` [matrix| 0 2; 3 3 |]- e ~-~ [matrix| 1 2; 3 4 |] `matrixEq` [matrix| 0 -2; -3 -3 |]+ ([matrix| 1 2; 3 4 |] ~-~ e) `matrixEq` [matrix| 0 2; 3 3 |]+ (e ~-~ [matrix| 1 2; 3 4 |]) `matrixEq` [matrix| 0 -2; -3 -3 |] it "two identity matrices" do {- will not be compiled -} -- (e :: Matrix 3 3 Int) ~-~ (e :: Matrix 4 4 Int) `matrixEq` undefined- (e :: Matrix 3 3 Int) ~-~ e `matrixEq` [matrix| 0 0 0; 0 0 0; 0 0 0 |]+ ((e :: Matrix 3 3 Int) ~-~ e) `matrixEq` [matrix| 0 0 0; 0 0 0; 0 0 0 |] it "two vectors" do {- will not be compiled -} -- [vector| 1 2 |] ~-~ [vector| 1 2 3 |] `matrixEq` undefined- [vector| 1 2 |] ~-~ [vector| 3 4 |] `matrixEq` [vector| -2 -2 |]+ ([vector| 1 2 |] ~-~ [vector| 3 4 |]) `matrixEq` [vector| -2 -2 |] describe "Multiplication" do it "two matrices" do {- will not be compiled -} -- [matrix| 1 2 |] ~*~ [matrix| 1 2 |] `matrixEq` undefined- [matrix| 2 |] ~*~ [matrix| 3 |] `matrixEq` [matrix| 6 |]- [matrix| 1 2 |] ~*~ [matrix| 1; 2 |] `matrixEq` [matrix| 5 |]- [matrix| 1 2 |] ~*~ [matrix| 1 2; 3 4 |] `matrixEq` [matrix| 7 10 |]- [matrix| 1 2; 3 4 |] ~*~ [matrix| 2 3; 4 5 |] `matrixEq` [matrix| 10 13; 22 29 |]+ ([matrix| 2 |] ~*~ [matrix| 3 |]) `matrixEq` [matrix| 6 |]+ ([matrix| 1 2 |] ~*~ [matrix| 1; 2 |]) `matrixEq` [matrix| 5 |]+ ([matrix| 1 2 |] ~*~ [matrix| 1 2; 3 4 |]) `matrixEq` [matrix| 7 10 |]+ ([matrix| 1 2; 3 4 |] ~*~ [matrix| 2 3; 4 5 |]) `matrixEq` [matrix| 10 13; 22 29 |] it "matrix and identity matrix" do {- will not be compiled -} -- [matrix| 1 2 |] ~*~ (e :: Matrix 3 3 Int) `matrixEq` undefined- [matrix| 1 2 |] ~*~ e `matrixEq` [matrix| 1 2 |]- [matrix| 1 2; 3 4 |] ~*~ e `matrixEq` [matrix| 1 2; 3 4 |]- e ~*~ [matrix| 1 2; 3 4 |] `matrixEq` [matrix| 1 2; 3 4 |]+ ([matrix| 1 2 |] ~*~ e) `matrixEq` [matrix| 1 2 |]+ ([matrix| 1 2; 3 4 |] ~*~ e) `matrixEq` [matrix| 1 2; 3 4 |]+ (e ~*~ [matrix| 1 2; 3 4 |]) `matrixEq` [matrix| 1 2; 3 4 |] it "two identity matrices" do {- will not be compiled -} -- (e :: Matrix 3 3 Int) ~*~ (e :: Matrix 4 4 Int) `matrixEq` undefined- (e :: Matrix 3 3 Int) ~*~ e `matrixEq` e+ ((e :: Matrix 3 3 Int) ~*~ e) `matrixEq` e describe "Determinant" do it "not identity matrix" do@@ -179,8 +179,8 @@ res it "A^(-1) * A = E" do let Just inv = inverted a- a ~*~ invA `matrixEqDouble` e- invA ~*~ a `matrixEqDouble` e+ (a ~*~ invA) `matrixEqDouble` e+ (invA ~*~ a) `matrixEqDouble` e it "(AB)^(-1) = B^(-1)A^(-1)" do let Just invAB = inverted $ a ~*~ b invAB `matrixEqDouble` (invB ~*~ invA)