{-# LANGUAGE CPP                   #-}
{-# LANGUAGE DataKinds             #-}
{-# LANGUAGE FlexibleContexts      #-}
{-# LANGUAGE FlexibleInstances     #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PolyKinds             #-}
{-# LANGUAGE RecordWildCards       #-}
{-# LANGUAGE ScopedTypeVariables   #-}
{-# LANGUAGE StandaloneDeriving    #-}
{-# LANGUAGE TypeApplications      #-}
{-# LANGUAGE TypeFamilies          #-}
{-# LANGUAGE TypeOperators         #-}
{-# LANGUAGE UndecidableInstances  #-}
module Numeric.Matrix.QR
( QR (..), LQ (..), MatrixQR (..)
, detViaQR, inverseViaQR
, qrSolveR, qrSolveL
) where

import Data.Kind
import Numeric.Basics
import Numeric.DataFrame.ST
import Numeric.DataFrame.SubSpace
import Numeric.DataFrame.Type
import Numeric.Dimensions
import Numeric.Matrix.Internal
import Numeric.Scalar.Internal
import Numeric.Subroutine.Householder
import Numeric.Subroutine.SolveTriangular

import Unsafe.Coerce (unsafeCoerce)

-- | Result of QR factorization
--   $$A = QR$$.
data QR (t :: Type) (n :: Nat) (m :: Nat)
= QR
{ QR t n m -> Matrix t n n
qrQ    :: Matrix t n n
-- ^ Orthogonal matrix $$Q$$
, QR t n m -> Scalar t
qrQDet :: Scalar t
-- ^ A shortcut for evaluating a determinant of $$|Q| = \pm 1$$
, QR t n m -> Matrix t n m
qrR    :: Matrix t n m
-- ^ Upper-triangular matrix $$R$$
}

-- | Result of LQ factorization
--   $$A = LQ$$.
data LQ (t :: Type) (n :: Nat) (m :: Nat)
= LQ
{ LQ t n m -> Matrix t n m
lqL    :: Matrix t n m
-- ^ Lower-triangular matrix $$L$$
, LQ t n m -> Matrix t m m
lqQ    :: Matrix t m m
-- ^ Orthogonal matrix $$Q$$
, LQ t n m -> Scalar t
lqQDet :: Scalar t
-- ^ A shortcut for evaluating a determinant of $$|Q| = \pm 1$$
}

deriving instance ( Eq t, PrimBytes t
, KnownDim n, KnownDim m) => Eq (QR t n m)
deriving instance ( Show t, PrimBytes t
, KnownDim n, KnownDim m) => Show (QR t n m)
deriving instance ( Eq t, PrimBytes t
, KnownDim n, KnownDim m) => Eq (LQ t n m)
deriving instance ( Show t, PrimBytes t
, KnownDim n, KnownDim m) => Show (LQ t n m)

class (PrimBytes t, Ord t, Epsilon t, KnownDim n, KnownDim m)
=> MatrixQR t (n :: Nat) (m :: Nat) where
-- | Compute QR factorization
qr :: Matrix t n m -> QR t n m
-- | Compute LQ factorization
lq :: Matrix t n m -> LQ t n m

instance (PrimBytes t, Ord t, Epsilon t, KnownDim n, KnownDim m)
=> MatrixQR t (n :: Nat) (m :: Nat) where
qr :: Matrix t n m -> QR t n m
qr Matrix t n m
a
| Word
lim Word -> Word -> Bool
forall a. Eq a => a -> a -> Bool
== Word
0 = Matrix t n n -> Scalar t -> Matrix t n m -> QR t n m
forall t (n :: Nat) (m :: Nat).
Matrix t n n -> Scalar t -> Matrix t n m -> QR t n m
QR Matrix t n n
forall a. HasCallStack => a
undefined Scalar t
forall a. HasCallStack => a
undefined Matrix t n m
1
| Bool
otherwise = (forall s. ST s (QR t n m)) -> QR t n m
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (QR t n m)) -> QR t n m)
-> (forall s. ST s (QR t n m)) -> QR t n m
forall a b. (a -> b) -> a -> b
$do STDataFrame s t '[n] uPtr <- ST s (STDataFrame s t '[n]) forall k t (ns :: [k]) s. (PrimBytes t, Dimensions ns) => ST s (STDataFrame s t ns) newDataFrame STDataFrame s t '[n, n] pPtr <- Matrix t n n -> ST s (STDataFrame s t '[n, n]) forall k t (ns :: [k]) s. (Dimensions ns, PrimArray t (DataFrame t ns)) => DataFrame t ns -> ST s (STDataFrame s t ns) unsafeThawDataFrame Matrix t n n forall t (n :: Nat). SquareMatrix t n => Matrix t n n eye STDataFrame s t '[n, m] rPtr <- Matrix t n m -> ST s (STDataFrame s t '[n, m]) forall k t (ns :: [k]) s. (Dimensions ns, PrimArray t (DataFrame t ns)) => DataFrame t ns -> ST s (STDataFrame s t ns) thawDataFrame Matrix t n m a Bool detNegative <- let f :: Bool -> Word -> ST s Bool f Bool x Word i = (Bool x Bool -> Bool -> Bool forall a. Eq a => a -> a -> Bool /=) (Bool -> Bool) -> ST s Bool -> ST s Bool forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b <$> STDataFrame s t '[n]
-> STDataFrame s t '[n, n]
-> STDataFrame s t '[n, m]
-> Idxs '[n, m]
-> ST s Bool
forall s t (n :: Nat) (m :: Nat).
(PrimBytes t, Ord t, Epsilon t, KnownDim n, KnownDim m) =>
STDataFrame s t '[n]
-> STDataFrame s t '[n, n]
-> STDataFrame s t '[n, m]
-> Idxs '[n, m]
-> ST s Bool
householderReflectionInplaceL
STDataFrame s t '[n]
uPtr STDataFrame s t '[n, n]
pPtr STDataFrame s t '[n, m]
rPtr (Word -> Idx n
forall k (d :: k). BoundedDim d => Word -> Idx d
Idx Word
i Idx n -> TypedList Idx '[m] -> Idxs '[n, m]
forall k (f :: k -> *) (xs :: [k]) (y :: k) (ys :: [k]).
(xs ~ (y : ys)) =>
f y -> TypedList f ys -> TypedList f xs
:* Word -> Idx m
forall k (d :: k). BoundedDim d => Word -> Idx d
Idx Word
i Idx m -> TypedList Idx '[] -> TypedList Idx '[m]
forall k (f :: k -> *) (xs :: [k]) (y :: k) (ys :: [k]).
(xs ~ (y : ys)) =>
f y -> TypedList f ys -> TypedList f xs
:* TypedList Idx '[]
forall k (f :: k -> *) (xs :: [k]). (xs ~ '[]) => TypedList f xs
U)
in  (Bool -> Word -> ST s Bool) -> Bool -> [Word] -> ST s Bool
forall (t :: * -> *) (m :: * -> *) b a.
(b -> a -> m b) -> b -> t a -> m b
foldM Bool -> Word -> ST s Bool
f Bool
False [Word
0..Word
lim Word -> Word -> Word
forall a. Num a => a -> a -> a
- Word
1]
Matrix t n m
qrR <- STDataFrame s t '[n, m] -> ST s (Matrix t n m)
forall k t (ns :: [k]) s.
PrimArray t (DataFrame t ns) =>
STDataFrame s t ns -> ST s (DataFrame t ns)
unsafeFreezeDataFrame STDataFrame s t '[n, m]
rPtr
Matrix t n n
qrQ <- STDataFrame s t '[n, n] -> ST s (Matrix t n n)
forall k t (ns :: [k]) s.
PrimArray t (DataFrame t ns) =>
STDataFrame s t ns -> ST s (DataFrame t ns)
unsafeFreezeDataFrame STDataFrame s t '[n, n]
pPtr
let qrQDet :: Scalar t
qrQDet = if Bool
detNegative then -Scalar t
1 else Scalar t
1
QR t n m -> ST s (QR t n m)
forall (m :: * -> *) a. Monad m => a -> m a
return QR :: forall t (n :: Nat) (m :: Nat).
Matrix t n n -> Scalar t -> Matrix t n m -> QR t n m
QR {Matrix t n n
Matrix t n m
Scalar t
qrQDet :: Scalar t
qrQ :: Matrix t n n
qrR :: Matrix t n m
qrR :: Matrix t n m
qrQDet :: Scalar t
qrQ :: Matrix t n n
..}
where
n :: Word
n = KnownDim n => Word
forall k (n :: k). KnownDim n => Word
dimVal' @n
m :: Word
m = KnownDim m => Word
forall k (n :: k). KnownDim n => Word
dimVal' @m
lim :: Word
lim = Word -> Word -> Word
forall a. Ord a => a -> a -> a
min Word
n Word
m
lq :: Matrix t n m -> LQ t n m
lq Matrix t n m
a
| Word
lim Word -> Word -> Bool
forall a. Eq a => a -> a -> Bool
== Word
0 = Matrix t n m -> Matrix t m m -> Scalar t -> LQ t n m
forall t (n :: Nat) (m :: Nat).
Matrix t n m -> Matrix t m m -> Scalar t -> LQ t n m
LQ Matrix t n m
forall a. HasCallStack => a
undefined Matrix t m m
forall a. HasCallStack => a
undefined Scalar t
1
| Bool
otherwise = (forall s. ST s (LQ t n m)) -> LQ t n m
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (LQ t n m)) -> LQ t n m)
-> (forall s. ST s (LQ t n m)) -> LQ t n m
forall a b. (a -> b) -> a -> b
$do STDataFrame s t '[m] uPtr <- ST s (STDataFrame s t '[m]) forall k t (ns :: [k]) s. (PrimBytes t, Dimensions ns) => ST s (STDataFrame s t ns) newDataFrame STDataFrame s t '[m, m] pPtr <- Matrix t m m -> ST s (STDataFrame s t '[m, m]) forall k t (ns :: [k]) s. (Dimensions ns, PrimArray t (DataFrame t ns)) => DataFrame t ns -> ST s (STDataFrame s t ns) unsafeThawDataFrame Matrix t m m forall t (n :: Nat). SquareMatrix t n => Matrix t n n eye STDataFrame s t '[n, m] lPtr <- Matrix t n m -> ST s (STDataFrame s t '[n, m]) forall k t (ns :: [k]) s. (Dimensions ns, PrimArray t (DataFrame t ns)) => DataFrame t ns -> ST s (STDataFrame s t ns) thawDataFrame Matrix t n m a Bool detNegative <- let f :: Bool -> Word -> ST s Bool f Bool x Word i = (Bool x Bool -> Bool -> Bool forall a. Eq a => a -> a -> Bool /=) (Bool -> Bool) -> ST s Bool -> ST s Bool forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b <$> STDataFrame s t '[m]
-> STDataFrame s t '[m, m]
-> STDataFrame s t '[n, m]
-> Idxs '[n, m]
-> ST s Bool
forall s t (n :: Nat) (m :: Nat).
(PrimBytes t, Ord t, Epsilon t, KnownDim n, KnownDim m) =>
STDataFrame s t '[m]
-> STDataFrame s t '[m, m]
-> STDataFrame s t '[n, m]
-> Idxs '[n, m]
-> ST s Bool
householderReflectionInplaceR
STDataFrame s t '[m]
uPtr STDataFrame s t '[m, m]
pPtr STDataFrame s t '[n, m]
lPtr (Word -> Idx n
forall k (d :: k). BoundedDim d => Word -> Idx d
Idx Word
i Idx n -> TypedList Idx '[m] -> Idxs '[n, m]
forall k (f :: k -> *) (xs :: [k]) (y :: k) (ys :: [k]).
(xs ~ (y : ys)) =>
f y -> TypedList f ys -> TypedList f xs
:* Word -> Idx m
forall k (d :: k). BoundedDim d => Word -> Idx d
Idx Word
i Idx m -> TypedList Idx '[] -> TypedList Idx '[m]
forall k (f :: k -> *) (xs :: [k]) (y :: k) (ys :: [k]).
(xs ~ (y : ys)) =>
f y -> TypedList f ys -> TypedList f xs
:* TypedList Idx '[]
forall k (f :: k -> *) (xs :: [k]). (xs ~ '[]) => TypedList f xs
U)
in  (Bool -> Word -> ST s Bool) -> Bool -> [Word] -> ST s Bool
forall (t :: * -> *) (m :: * -> *) b a.
(b -> a -> m b) -> b -> t a -> m b
foldM Bool -> Word -> ST s Bool
f Bool
False [Word
0..Word
lim Word -> Word -> Word
forall a. Num a => a -> a -> a
- Word
1]
Matrix t n m
lqL <- STDataFrame s t '[n, m] -> ST s (Matrix t n m)
forall k t (ns :: [k]) s.
PrimArray t (DataFrame t ns) =>
STDataFrame s t ns -> ST s (DataFrame t ns)
unsafeFreezeDataFrame STDataFrame s t '[n, m]
lPtr
-- WARNING! todo: get rid of transpose.
Matrix t m m
lqQ <- Matrix t m m -> Matrix t m m
forall k k (t :: k) (n :: k) (m :: k).
MatrixTranspose t n m =>
Matrix t n m -> Matrix t m n
transpose (Matrix t m m -> Matrix t m m)
-> ST s (Matrix t m m) -> ST s (Matrix t m m)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> STDataFrame s t '[m, m] -> ST s (Matrix t m m) forall k t (ns :: [k]) s. PrimArray t (DataFrame t ns) => STDataFrame s t ns -> ST s (DataFrame t ns) unsafeFreezeDataFrame STDataFrame s t '[m, m] pPtr let lqQDet :: Scalar t lqQDet = if Bool detNegative then -Scalar t 1 else Scalar t 1 LQ t n m -> ST s (LQ t n m) forall (m :: * -> *) a. Monad m => a -> m a return LQ :: forall t (n :: Nat) (m :: Nat). Matrix t n m -> Matrix t m m -> Scalar t -> LQ t n m LQ {Matrix t n m Matrix t m m Scalar t lqQDet :: Scalar t lqQ :: Matrix t m m lqL :: Matrix t n m lqQDet :: Scalar t lqQ :: Matrix t m m lqL :: Matrix t n m ..} where n :: Word n = KnownDim n => Word forall k (n :: k). KnownDim n => Word dimVal' @n m :: Word m = KnownDim m => Word forall k (n :: k). KnownDim n => Word dimVal' @m lim :: Word lim = Word -> Word -> Word forall a. Ord a => a -> a -> a min Word n Word m -- | Calculate determinant of a matrix via QR decomposition detViaQR :: forall t n . MatrixQR t n n => Matrix t n n -> Scalar t detViaQR :: Matrix t n n -> Scalar t detViaQR Matrix t n n m = (Scalar t -> Int -> Scalar t) -> Scalar t -> [Int] -> Scalar t forall (t :: * -> *) b a. Foldable t => (b -> a -> b) -> b -> t a -> b foldl (\Scalar t x Int off -> t -> Scalar t forall t. t -> DataFrame t '[] scalar (Int -> Matrix t n n -> t forall t a. PrimArray t a => Int -> a -> t ixOff Int off Matrix t n n qrR) Scalar t -> Scalar t -> Scalar t forall a. Num a => a -> a -> a * Scalar t x) Scalar t qrQDet [Int 0,Int nInt -> Int -> Int forall a. Num a => a -> a -> a +Int 1..Int nInt -> Int -> Int forall a. Num a => a -> a -> a *Int n] where n :: Int n = Word -> Int forall a b. (Integral a, Num b) => a -> b fromIntegral (KnownDim n => Word forall k (n :: k). KnownDim n => Word dimVal' @n) :: Int QR {Matrix t n n Scalar t qrQ :: Matrix t n n qrQDet :: Scalar t qrR :: Matrix t n n qrR :: forall t (n :: Nat) (m :: Nat). QR t n m -> Matrix t n m qrQDet :: forall t (n :: Nat) (m :: Nat). QR t n m -> Scalar t qrQ :: forall t (n :: Nat) (m :: Nat). QR t n m -> Matrix t n n ..} = Matrix t n n -> QR t n n forall t (n :: Nat) (m :: Nat). MatrixQR t n m => Matrix t n m -> QR t n m qr Matrix t n n m -- | Calculate inverse of a matrix via QR decomposition inverseViaQR :: forall t n . MatrixQR t n n => Matrix t n n -> Matrix t n n inverseViaQR :: Matrix t n n -> Matrix t n n inverseViaQR = (Matrix t n n -> Matrix t n n -> Matrix t n n forall t (n :: Nat) (m :: Nat) (ds :: [Nat]). (MatrixQR t n m, Dimensions ds) => Matrix t n m -> DataFrame t (n :+ ds) -> DataFrame t (m :+ ds) qrSolveR Matrix t n n forall t (n :: Nat). SquareMatrix t n => Matrix t n n eye) {- | Compute a QR or LQ decomposition of matrix $$A : n \times m$$, and solve a system of linear equations $$Ax = b$$. If $$n >= m$$ QR decomposition is used; if $$n > m$$ this function solves linear least squares problem. If $$n < m$$ (underdetermined system) LQ decomposition is used to yield a minimum norm solution. -} qrSolveR :: forall t (n :: Nat) (m :: Nat) (ds :: [Nat]) . (MatrixQR t n m, Dimensions ds) => Matrix t n m -> DataFrame t (n :+ ds) -> DataFrame t (m :+ ds) qrSolveR :: Matrix t n m -> DataFrame t (n :+ ds) -> DataFrame t (m :+ ds) qrSolveR Matrix t n m a DataFrame t (n :+ ds) b = case Dim n -> Dim m -> SOrdering (CmpNat n m) forall (a :: Nat) (b :: Nat). Dim a -> Dim b -> SOrdering (CmpNat a b) compareDim Dim n dn Dim m dm of SOrdering (CmpNat n m) SEQ | Dict (m ~ n) Dict <- (Dict (m ~ m) -> Dict (m ~ n) forall a b. a -> b unsafeCoerce ((m ~ m) => Dict (m ~ m) forall (a :: Constraint). a => Dict a Dict @(m ~ m)) :: Dict (m ~ n)) -> (forall s. ST s (DataFrame t (m :+ ds))) -> DataFrame t (m :+ ds) forall a. (forall s. ST s a) -> a runST ((forall s. ST s (DataFrame t (m :+ ds))) -> DataFrame t (m :+ ds)) -> (forall s. ST s (DataFrame t (m :+ ds))) -> DataFrame t (m :+ ds) forall a b. (a -> b) -> a -> b$ do
let QR {Matrix t n n
Matrix t n m
Scalar t
qrR :: Matrix t n m
qrQDet :: Scalar t
qrQ :: Matrix t n n
qrR :: forall t (n :: Nat) (m :: Nat). QR t n m -> Matrix t n m
qrQDet :: forall t (n :: Nat) (m :: Nat). QR t n m -> Scalar t
qrQ :: forall t (n :: Nat) (m :: Nat). QR t n m -> Matrix t n n
..} = Matrix t n m -> QR t n m
forall t (n :: Nat) (m :: Nat).
MatrixQR t n m =>
Matrix t n m -> QR t n m
qr Matrix t n m
a
STDataFrame s t (m :+ ds)
xPtr <- DataFrame t (m :+ ds) -> ST s (STDataFrame s t (m :+ ds))
forall k t (ns :: [k]) s.
(Dimensions ns, PrimArray t (DataFrame t ns)) =>
DataFrame t ns -> ST s (STDataFrame s t ns)
thawDataFrame (Matrix t n n -> Matrix t n n
forall k k (t :: k) (n :: k) (m :: k).
MatrixTranspose t n m =>
Matrix t n m -> Matrix t m n
transpose Matrix t n n
qrQ DataFrame t (RunList (Snoc' '[n] n))
-> DataFrame t (n :+ ds) -> DataFrame t (m :+ ds)
forall t (as :: [Nat]) (bs :: [Nat]) (asbs :: [Nat]) (m :: Nat).
(Contraction t as bs asbs, KnownDim m,
PrimArray t (DataFrame t (as +: m)),
PrimArray t (DataFrame t (m :+ bs)),
PrimArray t (DataFrame t asbs)) =>
DataFrame t (as +: m) -> DataFrame t (m :+ bs) -> DataFrame t asbs
%* DataFrame t (n :+ ds)
b) -- NB: make a stateful product for transposed mat
Matrix t n m -> STDataFrame s t (m :+ ds) -> ST s ()
forall s t (n :: Nat) (m :: Nat) (ds :: [Nat]).
(PrimBytes t, Fractional t, Eq t, KnownDim m, m <= n) =>
DataFrame t '[n, m] -> STDataFrame s t (m :+ ds) -> ST s ()
solveUpperTriangularR Matrix t n m
qrR STDataFrame s t (m :+ ds)
xPtr
STDataFrame s t (m :+ ds) -> ST s (DataFrame t (m :+ ds))
forall k t (ns :: [k]) s.
PrimArray t (DataFrame t ns) =>
STDataFrame s t ns -> ST s (DataFrame t ns)
unsafeFreezeDataFrame STDataFrame s t (m :+ ds)
xPtr
SOrdering (CmpNat n m)
SGT | Dict (m <= n)
Dict <- Dict ('EQ ~ 'EQ) -> Dict (m <= n)
forall a b. a -> b
unsafeCoerce ((m <= m) => Dict (m <= m)
forall (a :: Constraint). a => Dict a
Dict @(m <= m)) :: Dict (m <= n)
, Dict ((((n - m) + 1) + m) ~ (n + 1))
Dict <- Dict (n ~ n) -> Dict ((((n - m) + 1) + m) ~ (n + 1))
forall a b. a -> b
unsafeCoerce ((n ~ n) => Dict (n ~ n)
forall (a :: Constraint). a => Dict a
Dict @(n ~ n)) :: Dict ((((n - m) + 1) + m) ~ (n + 1) )
, Dim ((n - m) + 1)
D <- Dim n -> Dim m -> Dim (n - m)
forall (n :: Nat) (m :: Nat).
(m <= n) =>
Dim n -> Dim m -> Dim (n - m)
minusDim Dim n
dn Dim m
dm Dim (n - m) -> Dim 1 -> Dim ((n - m) + 1)
forall (n :: Nat) (m :: Nat). Dim n -> Dim m -> Dim (n + m)
plusDim Dim 1
forall (n :: Nat). (n ~ 1) => Dim n
D1
-> (forall s. ST s (DataFrame t (m :+ ds))) -> DataFrame t (m :+ ds)
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (DataFrame t (m :+ ds))) -> DataFrame t (m :+ ds))
-> (forall s. ST s (DataFrame t (m :+ ds)))
-> DataFrame t (m :+ ds)
forall a b. (a -> b) -> a -> b
$do let QR {Matrix t n n Matrix t n m Scalar t qrR :: Matrix t n m qrQDet :: Scalar t qrQ :: Matrix t n n qrR :: forall t (n :: Nat) (m :: Nat). QR t n m -> Matrix t n m qrQDet :: forall t (n :: Nat) (m :: Nat). QR t n m -> Scalar t qrQ :: forall t (n :: Nat) (m :: Nat). QR t n m -> Matrix t n n ..} = Matrix t n m -> QR t n m forall t (n :: Nat) (m :: Nat). MatrixQR t n m => Matrix t n m -> QR t n m qr Matrix t n m a i0 :: Idxs '[n - m + 1] i0 :: Idxs '[(n - m) + 1] i0 = Word -> Idx ((n - m) + 1) forall k (d :: k). BoundedDim d => Word -> Idx d Idx Word 0 Idx ((n - m) + 1) -> TypedList Idx '[] -> Idxs '[(n - m) + 1] forall k (f :: k -> *) (xs :: [k]) (y :: k) (ys :: [k]). (xs ~ (y : ys)) => f y -> TypedList f ys -> TypedList f xs :* TypedList Idx '[] forall k (f :: k -> *) (xs :: [k]). (xs ~ '[]) => TypedList f xs U STDataFrame s t (m :+ ds) xPtr <- DataFrame t (m :+ ds) -> ST s (STDataFrame s t (m :+ ds)) forall k t (ns :: [k]) s. (Dimensions ns, PrimArray t (DataFrame t ns)) => DataFrame t ns -> ST s (STDataFrame s t ns) thawDataFrame ( Idxs ('[] +: ((n - m) + 1)) -> DataFrame t (n :+ ds) -> DataFrame t (m :+ ds) forall k t (b :: k) (bi :: k) (bd :: k) (as :: [k]) (bs :: [k]) (asbs :: [k]). (KnownDimKind k, CanSlice t asbs, SubFrameIndexCtx b bi bd, KnownDim bd, ConcatList as (b :+ bs) asbs, PrimArray t (DataFrame t (bd :+ bs))) => Idxs (as +: bi) -> DataFrame t asbs -> DataFrame t (bd :+ bs) slice Idxs '[(n - m) + 1] Idxs ('[] +: ((n - m) + 1)) i0 (DataFrame t (n :+ ds) -> DataFrame t (m :+ ds)) -> DataFrame t (n :+ ds) -> DataFrame t (m :+ ds) forall a b. (a -> b) -> a -> b$ Matrix t n n -> Matrix t n n
forall k k (t :: k) (n :: k) (m :: k).
MatrixTranspose t n m =>
Matrix t n m -> Matrix t m n
transpose Matrix t n n
qrQ DataFrame t (RunList (Snoc' '[n] n))
-> DataFrame t (n :+ ds) -> DataFrame t (n :+ ds)
forall t (as :: [Nat]) (bs :: [Nat]) (asbs :: [Nat]) (m :: Nat).
(Contraction t as bs asbs, KnownDim m,
PrimArray t (DataFrame t (as +: m)),
PrimArray t (DataFrame t (m :+ bs)),
PrimArray t (DataFrame t asbs)) =>
DataFrame t (as +: m) -> DataFrame t (m :+ bs) -> DataFrame t asbs
%* DataFrame t (n :+ ds)
b
) -- NB: make a stateful product for transposed mat
Matrix t n m -> STDataFrame s t (m :+ ds) -> ST s ()
forall s t (n :: Nat) (m :: Nat) (ds :: [Nat]).
(PrimBytes t, Fractional t, Eq t, KnownDim m, m <= n) =>
DataFrame t '[n, m] -> STDataFrame s t (m :+ ds) -> ST s ()
solveUpperTriangularR Matrix t n m
qrR STDataFrame s t (m :+ ds)
xPtr
STDataFrame s t (m :+ ds) -> ST s (DataFrame t (m :+ ds))
forall k t (ns :: [k]) s.
PrimArray t (DataFrame t ns) =>
STDataFrame s t ns -> ST s (DataFrame t ns)
unsafeFreezeDataFrame STDataFrame s t (m :+ ds)
xPtr
SOrdering (CmpNat n m)
SLT | Dict (n <= m)
Dict <- Dict ('EQ ~ 'EQ) -> Dict ('LT ~ 'LT)
forall a b. a -> b
unsafeCoerce ((m <= m) => Dict (m <= m)
forall (a :: Constraint). a => Dict a
Dict @(m <= m)) :: Dict (n <= m)
, Dict ((((m - n) + 1) + n) ~ (m + 1))
Dict <- Dict (n ~ n) -> Dict ((((m - n) + 1) + n) ~ (m + 1))
forall a b. a -> b
unsafeCoerce ((n ~ n) => Dict (n ~ n)
forall (a :: Constraint). a => Dict a
Dict @(n ~ n)) :: Dict ((((m - n) + 1) + n) ~ (m + 1))
, Dict (((n + 1) + (m - n)) ~ (m + 1))
Dict <- Dict (n ~ n) -> Dict (((n + 1) + (m - n)) ~ (m + 1))
forall a b. a -> b
unsafeCoerce ((n ~ n) => Dict (n ~ n)
forall (a :: Constraint). a => Dict a
Dict @(n ~ n)) :: Dict (((n + 1) + (m - n)) ~ (m + 1))
, dd :: Dim (m - n)
dd@Dim (m - n)
D <- Dim m -> Dim n -> Dim (m - n)
forall (n :: Nat) (m :: Nat).
(m <= n) =>
Dim n -> Dim m -> Dim (n - m)
minusDim Dim m
dm Dim n
dn
, Dim ((m - n) + 1)
D <- Dim (m - n)
dd Dim (m - n) -> Dim 1 -> Dim ((m - n) + 1)
forall (n :: Nat) (m :: Nat). Dim n -> Dim m -> Dim (n + m)
plusDim Dim 1
forall (n :: Nat). (n ~ 1) => Dim n
D1
, Dim (n + 1)
D <- Dim n
dn Dim n -> Dim 1 -> Dim (n + 1)
forall (n :: Nat) (m :: Nat). Dim n -> Dim m -> Dim (n + m)
plusDim Dim 1
forall (n :: Nat). (n ~ 1) => Dim n
D1
-> (forall s. ST s (DataFrame t (m :+ ds))) -> DataFrame t (m :+ ds)
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (DataFrame t (m :+ ds))) -> DataFrame t (m :+ ds))
-> (forall s. ST s (DataFrame t (m :+ ds)))
-> DataFrame t (m :+ ds)
forall a b. (a -> b) -> a -> b
$do let LQ {Matrix t n m Matrix t m m Scalar t lqQDet :: Scalar t lqQ :: Matrix t m m lqL :: Matrix t n m lqQDet :: forall t (n :: Nat) (m :: Nat). LQ t n m -> Scalar t lqQ :: forall t (n :: Nat) (m :: Nat). LQ t n m -> Matrix t m m lqL :: forall t (n :: Nat) (m :: Nat). LQ t n m -> Matrix t n m ..} = Matrix t n m -> LQ t n m forall t (n :: Nat) (m :: Nat). MatrixQR t n m => Matrix t n m -> LQ t n m lq Matrix t n m a i0 :: Idxs '[m - n + 1] i0 :: Idxs '[(m - n) + 1] i0 = Word -> Idx ((m - n) + 1) forall k (d :: k). BoundedDim d => Word -> Idx d Idx Word 0 Idx ((m - n) + 1) -> TypedList Idx '[] -> Idxs '[(m - n) + 1] forall k (f :: k -> *) (xs :: [k]) (y :: k) (ys :: [k]). (xs ~ (y : ys)) => f y -> TypedList f ys -> TypedList f xs :* TypedList Idx '[] forall k (f :: k -> *) (xs :: [k]). (xs ~ '[]) => TypedList f xs U iz :: Idxs '[n + 1] iz :: Idxs '[n + 1] iz = Idx (n + 1) forall a. Bounded a => a maxBound Idx (n + 1) -> TypedList Idx '[] -> Idxs '[n + 1] forall k (f :: k -> *) (xs :: [k]) (y :: k) (ys :: [k]). (xs ~ (y : ys)) => f y -> TypedList f ys -> TypedList f xs :* TypedList Idx '[] forall k (f :: k -> *) (xs :: [k]). (xs ~ '[]) => TypedList f xs U STDataFrame s t (m :+ ds) xPtr <- ST s (STDataFrame s t (m :+ ds)) forall k t (ns :: [k]) s. (PrimBytes t, Dimensions ns) => ST s (STDataFrame s t ns) newDataFrame Idxs ('[] +: ((m - n) + 1)) -> DataFrame t (n :+ ds) -> STDataFrame s t (m :+ ds) -> ST s () forall k t (b :: k) (bi :: k) (bd :: k) (as :: [k]) (bs :: [k]) (asbs :: [k]) s. (SubFrameIndexCtx b bi bd, KnownDim bd, ExactDims bs, PrimArray t (DataFrame t (bd :+ bs)), ConcatList as (b :+ bs) asbs) => Idxs (as +: bi) -> DataFrame t (bd :+ bs) -> STDataFrame s t asbs -> ST s () copyDataFrame Idxs '[(m - n) + 1] Idxs ('[] +: ((m - n) + 1)) i0 DataFrame t (n :+ ds) b STDataFrame s t (m :+ ds) xPtr Idxs ('[] +: (n + 1)) -> DataFrame t ((m - n) :+ ds) -> STDataFrame s t (m :+ ds) -> ST s () forall k t (b :: k) (bi :: k) (bd :: k) (as :: [k]) (bs :: [k]) (asbs :: [k]) s. (SubFrameIndexCtx b bi bd, KnownDim bd, ExactDims bs, PrimArray t (DataFrame t (bd :+ bs)), ConcatList as (b :+ bs) asbs) => Idxs (as +: bi) -> DataFrame t (bd :+ bs) -> STDataFrame s t asbs -> ST s () copyDataFrame Idxs '[n + 1] Idxs ('[] +: (n + 1)) iz (DataFrame t ((m - n) :+ ds) 0 :: DataFrame t ((m - n) ': ds)) STDataFrame s t (m :+ ds) xPtr Matrix t n m -> STDataFrame s t (n :+ ds) -> ST s () forall s t (n :: Nat) (m :: Nat) (ds :: [Nat]). (PrimBytes t, Fractional t, Eq t, KnownDim n, KnownDim m, n <= m) => DataFrame t '[n, m] -> STDataFrame s t (n :+ ds) -> ST s () solveLowerTriangularR Matrix t n m lqL (Idxs ('[] +: ((m - n) + 1)) -> STDataFrame s t (m :+ ds) -> STDataFrame s t (n :+ ds) forall k t (b :: k) (bi :: k) (bd :: k) (as :: [k]) (bs :: [k]) (asbs :: [k]) s. (SubFrameIndexCtx b bi bd, KnownDim bd, ConcatList as (b :+ bs) asbs) => Idxs (as +: bi) -> STDataFrame s t asbs -> STDataFrame s t (bd :+ bs) subDataFrameView Idxs '[(m - n) + 1] Idxs ('[] +: ((m - n) + 1)) i0 STDataFrame s t (m :+ ds) xPtr) (Matrix t m m -> Matrix t m m forall k k (t :: k) (n :: k) (m :: k). MatrixTranspose t n m => Matrix t n m -> Matrix t m n transpose Matrix t m m lqQ DataFrame t (RunList (Snoc' '[m] m)) -> DataFrame t (m :+ ds) -> DataFrame t (m :+ ds) forall t (as :: [Nat]) (bs :: [Nat]) (asbs :: [Nat]) (m :: Nat). (Contraction t as bs asbs, KnownDim m, PrimArray t (DataFrame t (as +: m)), PrimArray t (DataFrame t (m :+ bs)), PrimArray t (DataFrame t asbs)) => DataFrame t (as +: m) -> DataFrame t (m :+ bs) -> DataFrame t asbs %*) (DataFrame t (m :+ ds) -> DataFrame t (m :+ ds)) -> ST s (DataFrame t (m :+ ds)) -> ST s (DataFrame t (m :+ ds)) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b <$> STDataFrame s t (m :+ ds) -> ST s (DataFrame t (m :+ ds))
forall k t (ns :: [k]) s.
PrimArray t (DataFrame t ns) =>
STDataFrame s t ns -> ST s (DataFrame t ns)
unsafeFreezeDataFrame STDataFrame s t (m :+ ds)
xPtr
-- NB: make a stateful product for transposed mat
_ -> error "Numeric.Matrix.QR.qrSolveR: impossible pattern"
#endif
where
dn :: Dim n
dn = KnownDim n => Dim n
forall k (n :: k). KnownDim n => Dim n
dim @n
dm :: Dim m
dm = KnownDim m => Dim m
forall k (n :: k). KnownDim n => Dim n
dim @m

{- |
Compute a QR or LQ decomposition of matrix $$A : n \times m$$,
and solve a system of linear equations $$xA = b$$.

If $$n <= m$$ LQ decomposition is used;
if $$n < m$$ this function solves linear least squares problem.
If $$n > m$$ (underdetermined system) QR decomposition is used
to yield a minimum norm solution.
-}
qrSolveL ::
forall t (n :: Nat) (m :: Nat) (ds :: [Nat])
. (MatrixQR t n m, Dimensions ds)
=> Matrix t n m -> DataFrame t (ds +: m) -> DataFrame t (ds +: n)
qrSolveL :: Matrix t n m -> DataFrame t (ds +: m) -> DataFrame t (ds +: n)
qrSolveL Matrix t n m
a DataFrame t (ds +: m)
b
| Dims (ds +: n)
Dims <- TypedList Dim ds -> Dim n -> Dims (ds +: n)
forall k (f :: k -> *) (xs :: [k]) (sy :: [k]) (y :: k).
SnocList sy y xs =>
TypedList f sy -> f y -> TypedList f xs
Snoc (Dimensions ds => TypedList Dim ds
forall k (ds :: [k]). Dimensions ds => Dims ds
dims @ds) Dim n
dn
, Dims (ds +: m)
Dims <- TypedList Dim ds -> Dim m -> Dims (ds +: m)
forall k (f :: k -> *) (xs :: [k]) (sy :: [k]) (y :: k).
SnocList sy y xs =>
TypedList f sy -> f y -> TypedList f xs
Snoc (Dimensions ds => TypedList Dim ds
forall k (ds :: [k]). Dimensions ds => Dims ds
dims @ds) Dim m
dm
, Dict (SnocList ds n ns)
Dict <- SnocList ds n ns => Dict (SnocList ds n ns)
forall (a :: Constraint). a => Dict a
Dict @(SnocList ds n _) -- remind GHC that we have (ds +: n)
, Dict (SnocList ds m ns)
Dict <- SnocList ds m ns => Dict (SnocList ds m ns)
forall (a :: Constraint). a => Dict a
Dict @(SnocList ds m _) -- remind GHC that we have (ds +: m)
= case Dim n -> Dim m -> SOrdering (CmpNat n m)
forall (a :: Nat) (b :: Nat).
Dim a -> Dim b -> SOrdering (CmpNat a b)
compareDim Dim n
dn Dim m
dm of
SOrdering (CmpNat n m)
SEQ | Dict (m ~ n)
Dict <- (Dict (m ~ m) -> Dict (m ~ n)
forall a b. a -> b
unsafeCoerce ((m ~ m) => Dict (m ~ m)
forall (a :: Constraint). a => Dict a
Dict @(m ~ m)) :: Dict (m ~ n))
-> (forall s. ST s (DataFrame t ns)) -> DataFrame t ns
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (DataFrame t ns)) -> DataFrame t ns)
-> (forall s. ST s (DataFrame t ns)) -> DataFrame t ns
forall a b. (a -> b) -> a -> b
$do let LQ {Matrix t n m Matrix t m m Scalar t lqQDet :: Scalar t lqQ :: Matrix t m m lqL :: Matrix t n m lqQDet :: forall t (n :: Nat) (m :: Nat). LQ t n m -> Scalar t lqQ :: forall t (n :: Nat) (m :: Nat). LQ t n m -> Matrix t m m lqL :: forall t (n :: Nat) (m :: Nat). LQ t n m -> Matrix t n m ..} = Matrix t n m -> LQ t n m forall t (n :: Nat) (m :: Nat). MatrixQR t n m => Matrix t n m -> LQ t n m lq Matrix t n m a STDataFrame s t ns xPtr <- DataFrame t ns -> ST s (STDataFrame s t ns) forall k t (ns :: [k]) s. (Dimensions ns, PrimArray t (DataFrame t ns)) => DataFrame t ns -> ST s (STDataFrame s t ns) thawDataFrame (DataFrame t (ds +: m) b DataFrame t (ds +: m) -> Matrix t m m -> DataFrame t ns forall t (as :: [Nat]) (bs :: [Nat]) (asbs :: [Nat]) (m :: Nat). (Contraction t as bs asbs, KnownDim m, PrimArray t (DataFrame t (as +: m)), PrimArray t (DataFrame t (m :+ bs)), PrimArray t (DataFrame t asbs)) => DataFrame t (as +: m) -> DataFrame t (m :+ bs) -> DataFrame t asbs %* Matrix t m m -> Matrix t m m forall k k (t :: k) (n :: k) (m :: k). MatrixTranspose t n m => Matrix t n m -> Matrix t m n transpose Matrix t m m lqQ) -- NB: make a stateful product for transposed mat STDataFrame s t (ds +: m) -> Matrix t n m -> ST s () forall s t (n :: Nat) (m :: Nat) (ds :: [Nat]). (PrimBytes t, Fractional t, Eq t, KnownDim n, KnownDim m, n <= m) => STDataFrame s t (ds +: m) -> DataFrame t '[n, m] -> ST s () solveLowerTriangularL STDataFrame s t ns STDataFrame s t (ds +: m) xPtr Matrix t n m lqL STDataFrame s t ns -> ST s (DataFrame t ns) forall k t (ns :: [k]) s. PrimArray t (DataFrame t ns) => STDataFrame s t ns -> ST s (DataFrame t ns) unsafeFreezeDataFrame STDataFrame s t ns xPtr SOrdering (CmpNat n m) SGT | Dict (m <= n) Dict <- Dict ('EQ ~ 'EQ) -> Dict (m <= n) forall a b. a -> b unsafeCoerce ((m <= m) => Dict (m <= m) forall (a :: Constraint). a => Dict a Dict @(m <= m)) :: Dict (m <= n) , Dict ((m + (n - m)) ~ n) Dict <- Dict (n ~ n) -> Dict ((m + (n - m)) ~ n) forall a b. a -> b unsafeCoerce ((n ~ n) => Dict (n ~ n) forall (a :: Constraint). a => Dict a Dict @(n ~ n)) :: Dict ((m + (n - m)) ~ n) , Dim (n - m) D <- Dim n -> Dim m -> Dim (n - m) forall (n :: Nat) (m :: Nat). (m <= n) => Dim n -> Dim m -> Dim (n - m) minusDim Dim n dn Dim m dm -> (forall s. ST s (DataFrame t ns)) -> DataFrame t ns forall a. (forall s. ST s a) -> a runST ((forall s. ST s (DataFrame t ns)) -> DataFrame t ns) -> (forall s. ST s (DataFrame t ns)) -> DataFrame t ns forall a b. (a -> b) -> a -> b$ do
let QR {Matrix t n n
Matrix t n m
Scalar t
qrR :: Matrix t n m
qrQDet :: Scalar t
qrQ :: Matrix t n n
qrR :: forall t (n :: Nat) (m :: Nat). QR t n m -> Matrix t n m
qrQDet :: forall t (n :: Nat) (m :: Nat). QR t n m -> Scalar t
qrQ :: forall t (n :: Nat) (m :: Nat). QR t n m -> Matrix t n n
..} = Matrix t n m -> QR t n m
forall t (n :: Nat) (m :: Nat).
MatrixQR t n m =>
Matrix t n m -> QR t n m
qr Matrix t n m
a
STDataFrame s t ns
xPtr <- DataFrame t ns -> ST s (STDataFrame s t ns)
forall k t (ns :: [k]) s.
(Dimensions ns, PrimArray t (DataFrame t ns)) =>
DataFrame t ns -> ST s (STDataFrame s t ns)
thawDataFrame DataFrame t ns
DataFrame t (ds +: m)
b
STDataFrame s t (ds +: m) -> Matrix t n m -> ST s ()
forall s t (n :: Nat) (m :: Nat) (ds :: [Nat]).
(PrimBytes t, Fractional t, Eq t, KnownDim m, m <= n) =>
STDataFrame s t (ds +: m) -> DataFrame t '[n, m] -> ST s ()
solveUpperTriangularL STDataFrame s t ns
STDataFrame s t (ds +: m)
xPtr Matrix t n m
qrR
(DataFrame t (m :+ '[]) -> DataFrame t '[n])
-> DataFrame t ns -> DataFrame t ns
forall k t (as :: [k]) (bs :: [k]) (asbs :: [k]) s (bs' :: [k])
(asbs' :: [k]).
(SubSpace t as bs asbs, SubSpace s as bs' asbs') =>
(DataFrame s bs' -> DataFrame t bs)
-> DataFrame s asbs' -> DataFrame t asbs
ewmap @t @ds @'[n] (\DataFrame t (m :+ '[])
x -> Matrix t n n
DataFrame t (RunList (Snoc' '[n] n))
qrQ DataFrame t (RunList (Snoc' '[n] n))
-> DataFrame t '[n] -> DataFrame t '[n]
forall t (as :: [Nat]) (bs :: [Nat]) (asbs :: [Nat]) (m :: Nat).
(Contraction t as bs asbs, KnownDim m,
PrimArray t (DataFrame t (as +: m)),
PrimArray t (DataFrame t (m :+ bs)),
PrimArray t (DataFrame t asbs)) =>
DataFrame t (as +: m) -> DataFrame t (m :+ bs) -> DataFrame t asbs
%* DataFrame t (m :+ '[])
-> DataFrame t ((n - m) :+ '[])
-> DataFrame t ((m + (n - m)) :+ '[])
forall (n :: Nat) (m :: Nat) (ds :: [Nat]) t.
(PrimBytes t, Dimensions ds, KnownDim n, KnownDim m) =>
DataFrame t (n :+ ds)
-> DataFrame t (m :+ ds) -> DataFrame t ((n + m) :+ ds)
appendDF DataFrame t (m :+ '[])
x (DataFrame t ((n - m) :+ '[])
0 :: DataFrame t '[n - m]))
(DataFrame t ns -> DataFrame t ns)
-> ST s (DataFrame t ns) -> ST s (DataFrame t ns)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> STDataFrame s t ns -> ST s (DataFrame t ns) forall k t (ns :: [k]) s. PrimArray t (DataFrame t ns) => STDataFrame s t ns -> ST s (DataFrame t ns) unsafeFreezeDataFrame STDataFrame s t ns xPtr SOrdering (CmpNat n m) SLT | Dict (n <= m) Dict <- Dict ('EQ ~ 'EQ) -> Dict ('LT ~ 'LT) forall a b. a -> b unsafeCoerce ((m <= m) => Dict (m <= m) forall (a :: Constraint). a => Dict a Dict @(m <= m)) :: Dict (n <= m) , Dict ((((m - n) + 1) + n) ~ (m + 1)) Dict <- Dict (n ~ n) -> Dict ((((m - n) + 1) + n) ~ (m + 1)) forall a b. a -> b unsafeCoerce ((n ~ n) => Dict (n ~ n) forall (a :: Constraint). a => Dict a Dict @(n ~ n)) :: Dict ((((m - n) + 1) + n) ~ (m + 1)) , Dim ((m - n) + 1) D <- Dim m -> Dim n -> Dim (m - n) forall (n :: Nat) (m :: Nat). (m <= n) => Dim n -> Dim m -> Dim (n - m) minusDim Dim m dm Dim n dn Dim (m - n) -> Dim 1 -> Dim ((m - n) + 1) forall (n :: Nat) (m :: Nat). Dim n -> Dim m -> Dim (n + m) plusDim Dim 1 forall (n :: Nat). (n ~ 1) => Dim n D1 -> (forall s. ST s (DataFrame t ns)) -> DataFrame t ns forall a. (forall s. ST s a) -> a runST ((forall s. ST s (DataFrame t ns)) -> DataFrame t ns) -> (forall s. ST s (DataFrame t ns)) -> DataFrame t ns forall a b. (a -> b) -> a -> b$ do
let LQ {Matrix t n m
Matrix t m m
Scalar t
lqQDet :: Scalar t
lqQ :: Matrix t m m
lqL :: Matrix t n m
lqQDet :: forall t (n :: Nat) (m :: Nat). LQ t n m -> Scalar t
lqQ :: forall t (n :: Nat) (m :: Nat). LQ t n m -> Matrix t m m
lqL :: forall t (n :: Nat) (m :: Nat). LQ t n m -> Matrix t n m
..} = Matrix t n m -> LQ t n m
forall t (n :: Nat) (m :: Nat).
MatrixQR t n m =>
Matrix t n m -> LQ t n m
lq Matrix t n m
a
i0 :: Idx (m - n + 1)
i0 :: Idx ((m - n) + 1)
i0 = Word -> Idx ((m - n) + 1)
forall k (d :: k). BoundedDim d => Word -> Idx d
Idx Word
0
STDataFrame s t ns
xPtr <- DataFrame t ns -> ST s (STDataFrame s t ns)
forall k t (ns :: [k]) s.
(Dimensions ns, PrimArray t (DataFrame t ns)) =>
DataFrame t ns -> ST s (STDataFrame s t ns)
thawDataFrame (DataFrame t (ds +: m)
b DataFrame t (ds +: m) -> Matrix t m m -> DataFrame t ns
forall t (as :: [Nat]) (bs :: [Nat]) (asbs :: [Nat]) (m :: Nat).
(Contraction t as bs asbs, KnownDim m,
PrimArray t (DataFrame t (as +: m)),
PrimArray t (DataFrame t (m :+ bs)),
PrimArray t (DataFrame t asbs)) =>
DataFrame t (as +: m) -> DataFrame t (m :+ bs) -> DataFrame t asbs
%* Matrix t m m -> Matrix t m m
forall k k (t :: k) (n :: k) (m :: k).
MatrixTranspose t n m =>
Matrix t n m -> Matrix t m n
transpose Matrix t m m
lqQ) -- NB: make a stateful product for transposed mat
STDataFrame s t (ds +: m) -> Matrix t n m -> ST s ()
forall s t (n :: Nat) (m :: Nat) (ds :: [Nat]).
(PrimBytes t, Fractional t, Eq t, KnownDim n, KnownDim m,
n <= m) =>
STDataFrame s t (ds +: m) -> DataFrame t '[n, m] -> ST s ()
solveLowerTriangularL STDataFrame s t ns
STDataFrame s t (ds +: m)
xPtr Matrix t n m
lqL
(DataFrame t (m :+ '[]) -> DataFrame t '[n])
-> DataFrame t ns -> DataFrame t ns
forall k t (as :: [k]) (bs :: [k]) (asbs :: [k]) s (bs' :: [k])
(asbs' :: [k]).
(SubSpace t as bs asbs, SubSpace s as bs' asbs') =>
(DataFrame s bs' -> DataFrame t bs)
-> DataFrame s asbs' -> DataFrame t asbs
ewmap @t @ds @'[n] (Idx ((m - n) + 1) -> DataFrame t (m :+ '[]) -> DataFrame t '[n]
forall k t (b :: k) (bi :: k) (bd :: k) (bs :: [k]).
(KnownDimKind k, CanSlice t (b :+ bs), SubFrameIndexCtx b bi bd,
KnownDim bd, PrimArray t (DataFrame t (bd :+ bs))) =>
Idx bi -> DataFrame t (b :+ bs) -> DataFrame t (bd :+ bs)
sslice Idx ((m - n) + 1)
i0)
(DataFrame t ns -> DataFrame t ns)
-> ST s (DataFrame t ns) -> ST s (DataFrame t ns)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<\$> STDataFrame s t ns -> ST s (DataFrame t ns)
forall k t (ns :: [k]) s.
PrimArray t (DataFrame t ns) =>
STDataFrame s t ns -> ST s (DataFrame t ns)
unsafeFreezeDataFrame STDataFrame s t ns
xPtr
_ -> error "Numeric.Matrix.QR.qrSolveL/compareDim: impossible pattern"
#endif
| Bool
otherwise = String -> DataFrame t (ds +: n)
forall a. HasCallStack => String -> a
error String
"Numeric.Matrix.QR.qrSolveL: impossible pattern"
where
dn :: Dim n
dn = KnownDim n => Dim n
forall k (n :: k). KnownDim n => Dim n
dim @n
dm :: Dim m
dm = KnownDim m => Dim m
forall k (n :: k). KnownDim n => Dim n
dim @m