{-# LANGUAGE FlexibleInstances     #-}
{-# LANGUAGE MultiParamTypeClasses #-}

-- | This module defines classes used to handle Matrices and Vectors in a generic way.
module FPNLA.Matrix (
    -- * Vector
    Vector(..),
    -- * Matrix
    Matrix(..),
    -- * MatrixVectror
    MatrixVector(..),
    -- * Miscellaneous
    cantRows_m,
    cantCols_m,
    size_m,
    asColumn_vm,
    diagonalBlock,
    verticalBlock
) where

import FPNLA.Utils (iif, splitSlice)

-- | This class represents a vector structure
-- For any vector data structure used in this framework it must have an instance of this class.
-- A minimal instance of this class must provide an implementation for 'generate_v', 'elem_v' and 'length_v'.
-- The index of the first element stored in the vector must be 0.
-- This is a multi-param type class because usually libraries of Matrix and Vector imposes restrictions over the type of the elements (for example, to have a "Storable" instance).
class Vector v e where
    -- *Constructors. This functions allows to create a new vector.
    
    -- | Creates a new vector from the length of the vector and a function that returns the value for each index.
    generate_v :: Int -- ^ The length of the vector
        -> (Int -> e) -- ^ A function that returns the value for each index
        -> v e
    -- | Creates a new vector from a list, th vector will contain all (and only) the elements of the list, and in the same order.
    fromList_v :: [e] -- ^ A list that contains the elements of the vector. The first element of the list will be the first element of the vector and so on.
        -> v e
    -- | Join multiple vectors in one single vector.
    concat_v :: [v e] -- ^ A list containing the vectors to be joined. If the list is @[<1>, <2,3>, <4,5,6>]@ then the vector will be @<1,2,3,4,5,6>@.
        -> v e
    
    -- * Selectors. This functions returns information from a vector.
    -- | Returns the element from a given index. The index must be between 0 and the length of the vector.
    elem_v :: Int -> v e -> e
    -- | Returns the length of the vector.
    length_v :: v e -> Int
    
    -- * High-order. This functions are the classics /fold/, /map/ and /zipWith/ but are in the class to allow to get an specific implementation from a specific data structure in order to achieve better performance.
    -- | The common /foldr/ function but specific for this instance.
    foldr_v :: (e -> b -> b) -> b -> v e -> b
    -- | The common /map/ function but specific for this instance.
    map_v :: (e -> e) -> v e -> v e
    -- | The common /zipWith/ function but specific for this instance.
    zipWith_v :: (e -> e -> e) -> v e -> v e -> v e

    -- Default implementations:
    fromList_v [e]
l = Int -> (Int -> e) -> v e
forall (v :: * -> *) e. Vector v e => Int -> (Int -> e) -> v e
generate_v ([e] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [e]
l) (\Int
i ->  [e]
l [e] -> Int -> e
forall a. HasCallStack => [a] -> Int -> a
!! Int
i)
    concat_v [] = Int -> (Int -> e) -> v e
forall (v :: * -> *) e. Vector v e => Int -> (Int -> e) -> v e
generate_v Int
0 Int -> e
forall a. HasCallStack => a
undefined
    concat_v [v e
v] = v e
v
    concat_v (v e
v1:v e
v2:[v e]
vs) = [v e] -> v e
forall (v :: * -> *) e. Vector v e => [v e] -> v e
concat_v (v e
joinv e -> [v e] -> [v e]
forall a. a -> [a] -> [a]
:[v e]
vs)
        where l1 :: Int
l1 = v e -> Int
forall (v :: * -> *) e. Vector v e => v e -> Int
length_v v e
v1
              l2 :: Int
l2 = v e -> Int
forall (v :: * -> *) e. Vector v e => v e -> Int
length_v v e
v2
              gen :: Int -> e
gen Int
i | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
l1 = Int -> v e -> e
forall (v :: * -> *) e. Vector v e => Int -> v e -> e
elem_v Int
i v e
v1
                    | Bool
otherwise = Int -> v e -> e
forall (v :: * -> *) e. Vector v e => Int -> v e -> e
elem_v (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
l1) v e
v2
              join :: v e
join = Int -> (Int -> e) -> v e
forall (v :: * -> *) e. Vector v e => Int -> (Int -> e) -> v e
generate_v (Int
l1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
l2) Int -> e
gen
    foldr_v e -> b -> b
f b
z v e
v = Int -> b
go Int
0
        where len :: Int
len = v e -> Int
forall (v :: * -> *) e. Vector v e => v e -> Int
length_v v e
v
              go :: Int -> b
go Int
i | Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
len   = b
z
                   | Bool
otherwise  = e -> b -> b
f (Int -> v e -> e
forall (v :: * -> *) e. Vector v e => Int -> v e -> e
elem_v Int
i v e
v) (Int -> b
go (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1))
    map_v e -> e
f v e
v = Int -> (Int -> e) -> v e
forall (v :: * -> *) e. Vector v e => Int -> (Int -> e) -> v e
generate_v (v e -> Int
forall (v :: * -> *) e. Vector v e => v e -> Int
length_v v e
v) (\Int
i -> e -> e
f (Int -> v e -> e
forall (v :: * -> *) e. Vector v e => Int -> v e -> e
elem_v Int
i v e
v))
    zipWith_v e -> e -> e
f v e
v1 v e
v2 = Int -> (Int -> e) -> v e
forall (v :: * -> *) e. Vector v e => Int -> (Int -> e) -> v e
generate_v (v e -> Int
forall (v :: * -> *) e. Vector v e => v e -> Int
length_v v e
v1) (\Int
i -> e -> e -> e
f (Int -> v e -> e
forall (v :: * -> *) e. Vector v e => Int -> v e -> e
elem_v Int
i v e
v1) (Int -> v e -> e
forall (v :: * -> *) e. Vector v e => Int -> v e -> e
elem_v Int
i v e
v2))


-- | This class represents any Matrix structure
-- For any matrix data structure used in this framework it must have an instance of this class.
-- A minimal instance of this class must provide an implementation for 'generate_m', 'dim_m' and 'elem_m'.
-- The index of the first element stored in the vector must be (0,0).
-- This is a multi-param type class because usually libraries of Matrix and Vector imposes restrictions over the type of the elements (for example, to have an "Storable" instance).
class Matrix m e where
    -- * Constructors. This functions allows to create a new matrix.
    -- | Creates a new matrix from the dimension (length of rows and columns) of the matrix and a function that returns the value for each index.
    generate_m :: Int -- ^ The length of the rows
        -> Int -- ^ A function that returns the value for each index
        -> (Int -> Int -> e) -- ^ 
        -> m e
    -- | Creates a new matrix from the dimension (length of rows and columns) of and a list.
    fromList_m :: Int -> Int -> [e] -> m e
    -- | Given a matrix, creates a new matrix that will be the transpose of the original.
    transpose_m :: m e -> m e

    -- * Selectors. This functions returns information from a vector.
    -- | Returns the dimension of a given matrix. The first element in the pair is the number of rows and the second the number of columns.
    dim_m :: m e -> (Int, Int)
    -- | Returns the element that is in the position @(i,j)@.
    elem_m :: Int -- ^ The row component of the position (@i@).
        -> Int -- ^ The column component of the position (@j@).
        -> m e -- ^ The matrix.
        -> e

    -- * High-order. This functions are the classics /fold/, /map/ and /zipWith/ but are in the class to allow to get an specific implementation from a specific data structure in order to achieve better performance.
    -- | The common /map/ function but specific for this instance.
    map_m :: (e -> e) -> m e -> m e
    -- | The common /zipWith/ function but specific for this instance.
    zipWith_m :: (e -> e -> e) -> m e -> m e -> m e

    -- * Blocks. This functions are constructors too, but this splits or allow to get a sub-matrix from a given matrix.
    -- | Given an initial position @(i,j)@, a dimension @(r,c)@ and a matrix @m@, creates a new matrix that is a sub matrix of @m@ containing the elements in positions @[(p1, p2) | p1 <- [i..i+r], p2 <- [j..j+c]]@
    -- For example, if @m@ is:
    -- @1 2 3
    -- 4 5 6
    -- 7 8 9@
    -- and @(i,j)@ and @(r,c)@ are @(0,1)@ and @(2,3)@ respectively, then the result will be a new matrix containing:
    -- @2 3
    -- 5 6
    -- 8 9@
    subMatrix_m :: Int -- ^ @i@
        -> Int -- ^ @j@
        -> Int -- ^ @r@
        -> Int -- ^ @c@
        -> m e -- ^ @m@
        -> m e
    -- | Constructs a new matrix from a list of list where the elements are sub-matrices.
    -- This operations is the analogue of 'concat_v' operation from the 'Vector' class.
    -- The \'borders\' of the sub-matrices will be joined.
    -- The dimensions of the sum-matrices must be compatibles.
    fromBlocks_m :: [[m e]] -> m e
    -- | Split a matrix @m@ in blocks of size @(r,c)@ (the dimension of the blocks in the right and the bottom may be smaller).
    toBlocks_m :: Int -- ^ @r@
        -> Int -- ^ @c@
        -> m e -- ^ @m@
        -> [[m e]]

    -- Default implementations:
    fromList_m Int
m Int
n [e]
l = Int -> Int -> (Int -> Int -> e) -> m e
forall (m :: * -> *) e.
Matrix m e =>
Int -> Int -> (Int -> Int -> e) -> m e
generate_m Int
m Int
n (\Int
i Int
j ->  [[e]]
ls [[e]] -> Int -> [e]
forall a. HasCallStack => [a] -> Int -> a
!! Int
i [e] -> Int -> e
forall a. HasCallStack => [a] -> Int -> a
!! Int
j)
        where ls :: [[e]]
ls = Int -> [e] -> [[e]]
forall a. Int -> [a] -> [[a]]
splitSlice Int
n [e]
l
    transpose_m m e
m = Int -> Int -> (Int -> Int -> e) -> m e
forall (m :: * -> *) e.
Matrix m e =>
Int -> Int -> (Int -> Int -> e) -> m e
generate_m (m e -> Int
forall (m :: * -> *) e. Matrix m e => m e -> Int
cantCols_m m e
m) (m e -> Int
forall (m :: * -> *) e. Matrix m e => m e -> Int
cantRows_m m e
m) (\Int
i Int
j -> Int -> Int -> m e -> e
forall (m :: * -> *) e. Matrix m e => Int -> Int -> m e -> e
elem_m Int
j Int
i m e
m)
    map_m e -> e
f m e
m = Int -> Int -> (Int -> Int -> e) -> m e
forall (m :: * -> *) e.
Matrix m e =>
Int -> Int -> (Int -> Int -> e) -> m e
generate_m (m e -> Int
forall (m :: * -> *) e. Matrix m e => m e -> Int
cantRows_m m e
m) (m e -> Int
forall (m :: * -> *) e. Matrix m e => m e -> Int
cantCols_m m e
m) (\Int
i Int
j -> e -> e
f (Int -> Int -> m e -> e
forall (m :: * -> *) e. Matrix m e => Int -> Int -> m e -> e
elem_m Int
i Int
j m e
m))
    zipWith_m e -> e -> e
f m e
m1 m e
m2 = Int -> Int -> (Int -> Int -> e) -> m e
forall (m :: * -> *) e.
Matrix m e =>
Int -> Int -> (Int -> Int -> e) -> m e
generate_m (m e -> Int
forall (m :: * -> *) e. Matrix m e => m e -> Int
cantRows_m m e
m1) (m e -> Int
forall (m :: * -> *) e. Matrix m e => m e -> Int
cantCols_m m e
m1) (\Int
i Int
j -> e -> e -> e
f (Int -> Int -> m e -> e
forall (m :: * -> *) e. Matrix m e => Int -> Int -> m e -> e
elem_m Int
i Int
j m e
m1) (Int -> Int -> m e -> e
forall (m :: * -> *) e. Matrix m e => Int -> Int -> m e -> e
elem_m Int
i Int
j m e
m2))
    subMatrix_m Int
posI Int
posJ Int
cantFilas Int
cantCols m e
m = Int -> Int -> (Int -> Int -> e) -> m e
forall (m :: * -> *) e.
Matrix m e =>
Int -> Int -> (Int -> Int -> e) -> m e
generate_m Int
cantFilas Int
cantCols (\Int
i Int
j -> Int -> Int -> m e -> e
forall (m :: * -> *) e. Matrix m e => Int -> Int -> m e -> e
elem_m (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
posI) (Int
j Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
posJ) m e
m)
    fromBlocks_m [] = Int -> Int -> [e] -> m e
forall (m :: * -> *) e. Matrix m e => Int -> Int -> [e] -> m e
fromList_m Int
0 Int
0 []
    fromBlocks_m [[]] = Int -> Int -> [e] -> m e
forall (m :: * -> *) e. Matrix m e => Int -> Int -> [e] -> m e
fromList_m Int
0 Int
0 []
    fromBlocks_m [[m e]]
bss = 
        Int -> Int -> (Int -> Int -> e) -> m e
forall (m :: * -> *) e.
Matrix m e =>
Int -> Int -> (Int -> Int -> e) -> m e
generate_m Int
cr Int
cc ((Int -> Int -> e) -> m e) -> (Int -> Int -> e) -> m e
forall a b. (a -> b) -> a -> b
$ \Int
i Int
j -> Int -> Int -> m e -> e
forall (m :: * -> *) e. Matrix m e => Int -> Int -> m e -> e
elem_m (Int -> Int
iToB Int
i) (Int -> Int
jToB Int
j) (m e -> e) -> m e -> e
forall a b. (a -> b) -> a -> b
$ [[m e]]
bss [[m e]] -> Int -> [m e]
forall a. HasCallStack => [a] -> Int -> a
!! Int -> Int
iToBSS Int
i [m e] -> Int -> m e
forall a. HasCallStack => [a] -> Int -> a
!! Int -> Int
jToBSS Int
j
        where bssm :: Int
bssm = [[m e]] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [[m e]]
bss
              bssn :: Int
bssn = [m e] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([m e] -> Int) -> [m e] -> Int
forall a b. (a -> b) -> a -> b
$ [[m e]] -> [m e]
forall a. HasCallStack => [a] -> a
head [[m e]]
bss
              (Int
br, Int
bc) = m e -> (Int, Int)
forall (m :: * -> *) e. Matrix m e => m e -> (Int, Int)
dim_m (m e -> (Int, Int)) -> ([[m e]] -> m e) -> [[m e]] -> (Int, Int)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [m e] -> m e
forall a. HasCallStack => [a] -> a
head ([m e] -> m e) -> ([[m e]] -> [m e]) -> [[m e]] -> m e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[m e]] -> [m e]
forall a. HasCallStack => [a] -> a
head ([[m e]] -> (Int, Int)) -> [[m e]] -> (Int, Int)
forall a b. (a -> b) -> a -> b
$ [[m e]]
bss
              lbr :: Int
lbr = m e -> Int
forall (m :: * -> *) e. Matrix m e => m e -> Int
cantRows_m (m e -> Int) -> ([[m e]] -> m e) -> [[m e]] -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [m e] -> m e
forall a. HasCallStack => [a] -> a
head ([m e] -> m e) -> ([[m e]] -> [m e]) -> [[m e]] -> m e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[m e]] -> [m e]
forall a. HasCallStack => [a] -> a
last ([[m e]] -> Int) -> [[m e]] -> Int
forall a b. (a -> b) -> a -> b
$ [[m e]]
bss
              lbc :: Int
lbc = m e -> Int
forall (m :: * -> *) e. Matrix m e => m e -> Int
cantCols_m (m e -> Int) -> ([[m e]] -> m e) -> [[m e]] -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [m e] -> m e
forall a. HasCallStack => [a] -> a
last ([m e] -> m e) -> ([[m e]] -> [m e]) -> [[m e]] -> m e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[m e]] -> [m e]
forall a. HasCallStack => [a] -> a
head ([[m e]] -> Int) -> [[m e]] -> Int
forall a b. (a -> b) -> a -> b
$ [[m e]]
bss
              ilimit :: Int
ilimit = (Int
bssmInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
br
              jlimit :: Int
jlimit = (Int
bssnInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
bc
              cr :: Int
cr = Int
ilimit Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
lbr
              cc :: Int
cc = Int
jlimit Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
lbc
              iToBSS :: Int -> Int
iToBSS Int
i = Bool -> Int -> Int -> Int
forall a. Bool -> a -> a -> a
iif (Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
ilimit) (Int
i Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
br) (Int
bssmInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)
              jToBSS :: Int -> Int
jToBSS Int
j = Bool -> Int -> Int -> Int
forall a. Bool -> a -> a -> a
iif (Int
j Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
jlimit) (Int
j Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
bc) (Int
bssnInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)
              iToB :: Int -> Int
iToB Int
i = Bool -> Int -> Int -> Int
forall a. Bool -> a -> a -> a
iif (Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
ilimit) (Int
i Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` Int
br) ((Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
ilimit) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` Int
lbr)
              jToB :: Int -> Int
jToB Int
j = Bool -> Int -> Int -> Int
forall a. Bool -> a -> a -> a
iif (Int
j Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
jlimit) (Int
j Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` Int
bc) ((Int
jInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
jlimit) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` Int
lbc)
    toBlocks_m Int
brs Int
bcs m e
m
        | Int
brs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 Bool -> Bool -> Bool
|| Int
bcs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = []
        | Bool
otherwise = (Int -> [m e]) -> [Int] -> [[m e]]
forall a b. (a -> b) -> [a] -> [b]
map (Int -> Int -> [m e]
rowBlocks Int
0) [Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
brs | Int
i <- [Int
0 .. (Int
rs Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
brs Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Bool -> Int -> Int -> Int
forall a. Bool -> a -> a -> a
iif (Int
rs Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` Int
brs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0) Int
1 Int
0) Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1]]
        where
            (Int
rs, Int
cs) = m e -> (Int, Int)
forall (m :: * -> *) e. Matrix m e => m e -> (Int, Int)
dim_m m e
m
            getBound :: a -> a -> a -> a
getBound a
s a
b a
c = Bool -> a -> a -> a
forall a. Bool -> a -> a -> a
iif (a
s a -> a -> a
forall a. Num a => a -> a -> a
+ a
b a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
c) (a
c a -> a -> a
forall a. Num a => a -> a -> a
- a
s) a
b
            rowBlocks :: Int -> Int -> [m e]
rowBlocks Int
sJ Int
sI
                    | Int
sJ Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
cs = []
                    | Bool
otherwise = Int -> Int -> Int -> Int -> m e -> m e
forall (m :: * -> *) e.
Matrix m e =>
Int -> Int -> Int -> Int -> m e -> m e
subMatrix_m Int
sI Int
sJ (Int -> Int -> Int -> Int
forall {a}. (Ord a, Num a) => a -> a -> a -> a
getBound Int
sI Int
brs Int
rs) (Int -> Int -> Int -> Int
forall {a}. (Ord a, Num a) => a -> a -> a -> a
getBound Int
sJ Int
bcs Int
cs) m e
m m e -> [m e] -> [m e]
forall a. a -> [a] -> [a]
: Int -> Int -> [m e]
rowBlocks (Int
sJ Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
bcs) Int
sI


-- | This class allows us to leave separated the two concepts of 'Vector' and 'Matrix'.
-- This class establishes a link between a vector and a matrix structure.
-- We provide a default instance for any 'Vector' and 'Matrix' instances so any vector could be used with any matrix whenever the type of the elements are the same.
class (Vector v e, Matrix m e) => MatrixVector m v e where
    -- | Extracts the @i@-th row of the matrix @m@ and converts it in the vector structure.
    row_vm :: Int -> m e -> v e
    -- | Extracts the @j@-th column of the matrix @m@ and converts it in the vector structure.
    col_vm :: Int -> m e -> v e
    -- | Joins a list of column vectors in a matrix structure.
    -- The length of every vector in the list must be the same.
    fromCols_vm :: [v e] -> m e
    -- | Returns a list containing every column of the matrix converted in the vector structure.
    toCols_vm :: m e -> [v e]

    -- Default implementations:
    row_vm Int
i m e
m = Int -> (Int -> e) -> v e
forall (v :: * -> *) e. Vector v e => Int -> (Int -> e) -> v e
generate_v (m e -> Int
forall (m :: * -> *) e. Matrix m e => m e -> Int
cantCols_m m e
m) (\Int
j -> Int -> Int -> m e -> e
forall (m :: * -> *) e. Matrix m e => Int -> Int -> m e -> e
elem_m Int
i Int
j m e
m)
    col_vm Int
j m e
m = Int -> (Int -> e) -> v e
forall (v :: * -> *) e. Vector v e => Int -> (Int -> e) -> v e
generate_v (m e -> Int
forall (m :: * -> *) e. Matrix m e => m e -> Int
cantRows_m m e
m) (\Int
i -> Int -> Int -> m e -> e
forall (m :: * -> *) e. Matrix m e => Int -> Int -> m e -> e
elem_m Int
i Int
j m e
m)
    fromCols_vm [v e]
css | Int
cc Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = Int -> Int -> [e] -> m e
forall (m :: * -> *) e. Matrix m e => Int -> Int -> [e] -> m e
fromList_m Int
0 Int
0 []
                  | Bool
otherwise = Int -> Int -> (Int -> Int -> e) -> m e
forall (m :: * -> *) e.
Matrix m e =>
Int -> Int -> (Int -> Int -> e) -> m e
generate_m Int
cr Int
cc (\Int
i Int
j -> Int -> v e -> e
forall (v :: * -> *) e. Vector v e => Int -> v e -> e
elem_v Int
i (v e -> e) -> v e -> e
forall a b. (a -> b) -> a -> b
$ [v e]
css [v e] -> Int -> v e
forall a. HasCallStack => [a] -> Int -> a
!! Int
j)
        where cc :: Int
cc = [v e] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [v e]
css
              cr :: Int
cr = v e -> Int
forall (v :: * -> *) e. Vector v e => v e -> Int
length_v (v e -> Int) -> v e -> Int
forall a b. (a -> b) -> a -> b
$ [v e] -> v e
forall a. HasCallStack => [a] -> a
head [v e]
css
    toCols_vm m e
m = (Int -> v e) -> [Int] -> [v e]
forall a b. (a -> b) -> [a] -> [b]
map (Int -> m e -> v e
forall (m :: * -> *) (v :: * -> *) e.
MatrixVector m v e =>
Int -> m e -> v e
`col_vm` m e
m) [Int
0 .. m e -> Int
forall (m :: * -> *) e. Matrix m e => m e -> Int
cantCols_m m e
m Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1]


instance (Vector v e, Matrix m e) => MatrixVector m v e

-- | Returns the number of rows of the matrix.
cantRows_m :: (Matrix m e) => m e -> Int
cantRows_m :: forall (m :: * -> *) e. Matrix m e => m e -> Int
cantRows_m = (Int, Int) -> Int
forall a b. (a, b) -> a
fst ((Int, Int) -> Int) -> (m e -> (Int, Int)) -> m e -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m e -> (Int, Int)
forall (m :: * -> *) e. Matrix m e => m e -> (Int, Int)
dim_m

-- | Returns the number of columns of the matrix.
cantCols_m :: (Matrix m e) => m e -> Int
cantCols_m :: forall (m :: * -> *) e. Matrix m e => m e -> Int
cantCols_m = (Int, Int) -> Int
forall a b. (a, b) -> b
snd ((Int, Int) -> Int) -> (m e -> (Int, Int)) -> m e -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m e -> (Int, Int)
forall (m :: * -> *) e. Matrix m e => m e -> (Int, Int)
dim_m

-- | Returns the size of the matrix (number of rows x number of columns).
size_m :: (Matrix m e) => m e -> Int
size_m :: forall (m :: * -> *) e. Matrix m e => m e -> Int
size_m = (Int -> Int -> Int) -> (Int, Int) -> Int
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Int -> Int -> Int
forall a. Num a => a -> a -> a
(*) ((Int, Int) -> Int) -> (m e -> (Int, Int)) -> m e -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m e -> (Int, Int)
forall (m :: * -> *) e. Matrix m e => m e -> (Int, Int)
dim_m

-- | Converts a vector in a matrix of one column.
asColumn_vm :: MatrixVector m v e => v e -> m e
asColumn_vm :: forall (m :: * -> *) (v :: * -> *) e.
MatrixVector m v e =>
v e -> m e
asColumn_vm = [v e] -> m e
forall (m :: * -> *) (v :: * -> *) e.
MatrixVector m v e =>
[v e] -> m e
fromCols_vm ([v e] -> m e) -> (v e -> [v e]) -> v e -> m e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (v e -> [v e] -> [v e]
forall a. a -> [a] -> [a]
:[])

-- | Splits a matrix @m@ into blocks of size @(r,c)@ and returns the block on the position @(i,i)@.
diagonalBlock :: (Matrix m e) =>  (Int, Int) -- ^ @(r,c)@
    -> Int -- ^ @i@
    -> m e -- ^ @m@
    -> m e
diagonalBlock :: forall (m :: * -> *) e.
Matrix m e =>
(Int, Int) -> Int -> m e -> m e
diagonalBlock (Int
blockDimM, Int
blockDimN) Int
i m e
m =
    Int -> Int -> Int -> Int -> m e -> m e
forall (m :: * -> *) e.
Matrix m e =>
Int -> Int -> Int -> Int -> m e -> m e
subMatrix_m (Int
blockDimMInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
i) (Int
blockDimNInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
i) (Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
blockDimM (m e -> Int
forall (m :: * -> *) e. Matrix m e => m e -> Int
cantRows_m m e
m Int -> Int -> Int
forall a. Num a => a -> a -> a
- (Int
blockDimM Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
i))) (Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
blockDimN (m e -> Int
forall (m :: * -> *) e. Matrix m e => m e -> Int
cantCols_m m e
m Int -> Int -> Int
forall a. Num a => a -> a -> a
- (Int
blockDimN Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
i))) m e
m



-- | Splits a matrix @m@ into blocks of size @(s, cantCols_m m)@ and returns the block on the position @i@.
verticalBlock :: (Matrix m e) =>  Int -- ^ @s@
    -> Int -- ^ @i@
    -> m e -- ^ @m@
    -> m e
verticalBlock :: forall (m :: * -> *) e. Matrix m e => Int -> Int -> m e -> m e
verticalBlock Int
blockDim Int
i m e
m =
    Int -> Int -> Int -> Int -> m e -> m e
forall (m :: * -> *) e.
Matrix m e =>
Int -> Int -> Int -> Int -> m e -> m e
subMatrix_m (Int
blockDimInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
i) Int
0 (Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
blockDim (m e -> Int
forall (m :: * -> *) e. Matrix m e => m e -> Int
cantRows_m m e
m Int -> Int -> Int
forall a. Num a => a -> a -> a
- (Int
blockDim Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
i))) (m e -> Int
forall (m :: * -> *) e. Matrix m e => m e -> Int
cantCols_m m e
m) m e
m