module ELynx.Tools.LinearAlgebra
(
matrixSeparateSymSkew,
matrixSetDiagToZero,
dispv,
dispm,
dispmi,
)
where
import Data.List
import Numeric.LinearAlgebra
matrixSeparateSymSkew :: Matrix R -> (Matrix R, Matrix R)
matrixSeparateSymSkew :: Matrix R -> (Matrix R, Matrix R)
matrixSeparateSymSkew Matrix R
m = (Matrix R
mSym, Matrix R
mSkew)
where
trM :: Matrix R
trM = Matrix R -> Matrix R
forall m mt. Transposable m mt => m -> mt
tr Matrix R
m
mSym :: Matrix R
mSym = R -> Matrix R -> Matrix R
forall t (c :: * -> *). Linear t c => t -> c t -> c t
scale R
0.5 (Matrix R -> Matrix R) -> Matrix R -> Matrix R
forall a b. (a -> b) -> a -> b
$ Matrix R
m Matrix R -> Matrix R -> Matrix R
forall a. Num a => a -> a -> a
+ Matrix R
trM
mSkew :: Matrix R
mSkew = R -> Matrix R -> Matrix R
forall t (c :: * -> *). Linear t c => t -> c t -> c t
scale R
0.5 (Matrix R -> Matrix R) -> Matrix R -> Matrix R
forall a b. (a -> b) -> a -> b
$ Matrix R
m Matrix R -> Matrix R -> Matrix R
forall a. Num a => a -> a -> a
- Matrix R
trM
matrixSetDiagToZero :: Matrix R -> Matrix R
matrixSetDiagToZero :: Matrix R -> Matrix R
matrixSetDiagToZero Matrix R
m = Matrix R
m Matrix R -> Matrix R -> Matrix R
forall a. Num a => a -> a -> a
- Vector R -> Matrix R
forall a. (Num a, Element a) => Vector a -> Matrix a
diag (Matrix R -> Vector R
forall t. Element t => Matrix t -> Vector t
takeDiag Matrix R
m)
{-# INLINE matrixSetDiagToZero #-}
dispv :: Int -> Vector R -> String
dispv :: Int -> Vector R -> String
dispv Int
p Vector R
v = [String] -> String
forall a. [a] -> a
head ([String] -> String) -> [String] -> String
forall a b. (a -> b) -> a -> b
$ [String] -> [String]
forall a. [a] -> [a]
tail ([String] -> [String]) -> [String] -> [String]
forall a b. (a -> b) -> a -> b
$ String -> [String]
lines (String -> [String]) -> String -> [String]
forall a b. (a -> b) -> a -> b
$ Int -> Matrix R -> String
dispf Int
p (Vector R -> Matrix R
forall a. Storable a => Vector a -> Matrix a
asRow Vector R
v)
dispm :: Int -> Matrix R -> String
dispm :: Int -> Matrix R -> String
dispm Int
p Matrix R
m = String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"\n" ([String] -> String) -> [String] -> String
forall a b. (a -> b) -> a -> b
$ [String] -> [String]
forall a. [a] -> [a]
init ([String] -> [String]) -> [String] -> [String]
forall a b. (a -> b) -> a -> b
$ String -> [String]
lines (String -> [String]) -> String -> [String]
forall a b. (a -> b) -> a -> b
$ Int -> Matrix R -> String
dispf Int
p Matrix R
m
dispmi :: Int -> Int -> Matrix R -> String
dispmi :: Int -> Int -> Matrix R -> String
dispmi Int
p Int
i Matrix R
m =
String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"\n" ([String] -> String) -> [String] -> String
forall a b. (a -> b) -> a -> b
$ (String -> String) -> [String] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (Int -> Char -> String
forall a. Int -> a -> [a]
replicate Int
i Char
' ' String -> String -> String
forall a. [a] -> [a] -> [a]
++) ([String] -> [String]) -> [String] -> [String]
forall a b. (a -> b) -> a -> b
$ [String] -> [String]
forall a. [a] -> [a]
tail ([String] -> [String]) -> [String] -> [String]
forall a b. (a -> b) -> a -> b
$ String -> [String]
lines (String -> [String]) -> String -> [String]
forall a b. (a -> b) -> a -> b
$ Int -> Matrix R -> String
dispf Int
p Matrix R
m