{-# LANGUAGE TypeFamilies #-}
module Numeric.LAPACK.Singular.Plain (
   values,
   valuesTall,
   valuesWide,
   decompose,
   decomposeTall,
   decomposeWide,
   determinantAbsolute,
   leastSquaresMinimumNormRCond,
   pseudoInverseRCond,
   RealOf,
   ) where

import qualified Numeric.LAPACK.Matrix.Layout.Private as Layout
import qualified Numeric.LAPACK.Matrix.Square.Basic as Square
import qualified Numeric.LAPACK.Matrix.Extent.Private as Extent
import qualified Numeric.LAPACK.Matrix.Private as Matrix
import qualified Numeric.LAPACK.Matrix.Basic as Basic
import qualified Numeric.LAPACK.Vector as Vector
import qualified Numeric.LAPACK.Private as Private
import Numeric.LAPACK.Matrix.Hermitian.Private
         (TakeDiagonal(..), Determinant(..))
import Numeric.LAPACK.Matrix.Extent.Private (Extent)
import Numeric.LAPACK.Matrix.Square.Basic (Square)
import Numeric.LAPACK.Matrix.Layout.Private (Order(ColumnMajor), swapOnRowMajor)
import Numeric.LAPACK.Matrix.Private (Full, General)
import Numeric.LAPACK.Vector (Vector)
import Numeric.LAPACK.Scalar (RealOf, zero)
import Numeric.LAPACK.Private
         (withAutoWorkspace, peekCInt, createHigherArray,
          copyToTemp, copyToColumnMajorTemp, copyToSubColumnMajor)

import qualified Numeric.LAPACK.FFI.Complex as LapackComplex
import qualified Numeric.LAPACK.FFI.Real as LapackReal
import qualified Numeric.Netlib.Utility as Call
import qualified Numeric.Netlib.Class as Class

import qualified Data.Array.Comfort.Storable.Unchecked.Monadic as ArrayIO
import qualified Data.Array.Comfort.Storable.Unchecked as Array
import qualified Data.Array.Comfort.Shape as Shape
import Data.Array.Comfort.Storable.Unchecked (Array(Array))

import System.IO.Unsafe (unsafePerformIO)

import qualified Foreign.Marshal.Array.Guarded as ForeignArray
import qualified Foreign.Marshal.Utils as Marshal
import Foreign.C.Types (CInt, CChar)
import Foreign.ForeignPtr (ForeignPtr, withForeignPtr)
import Foreign.Ptr (Ptr, nullPtr)
import Foreign.Storable (Storable)

import Control.Monad.Trans.Cont (evalContT)
import Control.Monad.IO.Class (liftIO)

import Data.Complex (Complex)
import Data.Tuple.HT (mapSnd)
import Data.Bool.HT (if')


type RealVector sh a = Vector sh (RealOf a)
type RectangularRealDiagonal meas vert horiz height width a =
      RealVector (Layout.RectangularDiagonal meas vert horiz height width) a

values ::
   (Extent.Measure meas, Extent.C vert, Extent.C horiz) =>
   (Shape.C height, Shape.C width, Class.Floating a) =>
   Full meas vert horiz height width a ->
   RectangularRealDiagonal meas vert horiz height width a
values :: Full meas vert horiz height width a
-> RectangularRealDiagonal meas vert horiz height width a
values = (Extent meas vert horiz height width
 -> RectangularDiagonal meas vert horiz height width)
-> Full meas vert horiz height width a
-> RectangularRealDiagonal meas vert horiz height width a
forall meas vert horiz height width shape a.
(Measure meas, C vert, C horiz, C height, C width, C shape,
 Floating a) =>
(Extent meas vert horiz height width -> shape)
-> Full meas vert horiz height width a -> RealVector shape a
valuesGen ((Extent meas vert horiz height width
  -> RectangularDiagonal meas vert horiz height width)
 -> Full meas vert horiz height width a
 -> RectangularRealDiagonal meas vert horiz height width a)
-> (Extent meas vert horiz height width
    -> RectangularDiagonal meas vert horiz height width)
-> Full meas vert horiz height width a
-> RectangularRealDiagonal meas vert horiz height width a
forall a b. (a -> b) -> a -> b
$ (Int, RectangularDiagonal meas vert horiz height width)
-> RectangularDiagonal meas vert horiz height width
forall a b. (a, b) -> b
snd ((Int, RectangularDiagonal meas vert horiz height width)
 -> RectangularDiagonal meas vert horiz height width)
-> (Extent meas vert horiz height width
    -> (Int, RectangularDiagonal meas vert horiz height width))
-> Extent meas vert horiz height width
-> RectangularDiagonal meas vert horiz height width
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Extent meas vert horiz height width
-> (Int, RectangularDiagonal meas vert horiz height width)
forall meas vert horiz height width.
(Measure meas, C vert, C horiz, C height, C width) =>
Extent meas vert horiz height width
-> (Int, RectangularDiagonal meas vert horiz height width)
Layout.rectangularDiagonal

valuesTall ::
   (Extent.Measure meas, Extent.C vert,
    Shape.C height, Shape.C width, Class.Floating a) =>
   Full meas vert Extent.Small height width a -> RealVector width a
valuesTall :: Full meas vert Small height width a -> RealVector width a
valuesTall = (Extent meas vert Small height width -> width)
-> Full meas vert Small height width a -> RealVector width a
forall meas vert horiz height width shape a.
(Measure meas, C vert, C horiz, C height, C width, C shape,
 Floating a) =>
(Extent meas vert horiz height width -> shape)
-> Full meas vert horiz height width a -> RealVector shape a
valuesGen Extent meas vert Small height width -> width
forall meas vert horiz height width.
(Measure meas, C vert, C horiz) =>
Extent meas vert horiz height width -> width
Extent.width

valuesWide ::
   (Extent.Measure meas, Extent.C horiz,
    Shape.C height, Shape.C width, Class.Floating a) =>
   Full meas Extent.Small horiz height width a -> RealVector height a
valuesWide :: Full meas Small horiz height width a -> RealVector height a
valuesWide = Full meas horiz Small width height a -> RealVector height a
forall meas vert height width a.
(Measure meas, C vert, C height, C width, Floating a) =>
Full meas vert Small height width a -> RealVector width a
valuesTall (Full meas horiz Small width height a -> RealVector height a)
-> (Full meas Small horiz height width a
    -> Full meas horiz Small width height a)
-> Full meas Small horiz height width a
-> RealVector height a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Full meas Small horiz height width a
-> Full meas horiz Small width height a
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz) =>
Full meas vert horiz height width a
-> Full meas horiz vert width height a
Basic.transpose

valuesGen ::
   (Extent.Measure meas, Extent.C vert, Extent.C horiz,
    Shape.C height, Shape.C width,
    Shape.C shape, Class.Floating a) =>
   (Extent meas vert horiz height width -> shape) ->
   Full meas vert horiz height width a -> RealVector shape a
valuesGen :: (Extent meas vert horiz height width -> shape)
-> Full meas vert horiz height width a -> RealVector shape a
valuesGen Extent meas vert horiz height width -> shape
resultShape =
   TakeDiagonal
  (Array (Full meas vert horiz height width)) (Array shape) a
-> Full meas vert horiz height width a -> RealVector shape a
forall (f :: * -> *) (g :: * -> *) a.
TakeDiagonal f g a -> f a -> g (RealOf a)
runTakeDiagonal (TakeDiagonal
   (Array (Full meas vert horiz height width)) (Array shape) a
 -> Full meas vert horiz height width a -> RealVector shape a)
-> TakeDiagonal
     (Array (Full meas vert horiz height width)) (Array shape) a
-> Full meas vert horiz height width a
-> RealVector shape a
forall a b. (a -> b) -> a -> b
$
   TakeDiagonal
  (Array (Full meas vert horiz height width)) (Array shape) Float
-> TakeDiagonal
     (Array (Full meas vert horiz height width)) (Array shape) Double
-> TakeDiagonal
     (Array (Full meas vert horiz height width))
     (Array shape)
     (Complex Float)
-> TakeDiagonal
     (Array (Full meas vert horiz height width))
     (Array shape)
     (Complex Double)
-> TakeDiagonal
     (Array (Full meas vert horiz height width)) (Array shape) a
forall a (f :: * -> *).
Floating a =>
f Float
-> f Double -> f (Complex Float) -> f (Complex Double) -> f a
Class.switchFloating
      ((Array (Full meas vert horiz height width) Float
 -> Array shape (RealOf Float))
-> TakeDiagonal
     (Array (Full meas vert horiz height width)) (Array shape) Float
forall (f :: * -> *) (g :: * -> *) a.
(f a -> g (RealOf a)) -> TakeDiagonal f g a
TakeDiagonal ((Array (Full meas vert horiz height width) Float
  -> Array shape (RealOf Float))
 -> TakeDiagonal
      (Array (Full meas vert horiz height width)) (Array shape) Float)
-> (Array (Full meas vert horiz height width) Float
    -> Array shape (RealOf Float))
-> TakeDiagonal
     (Array (Full meas vert horiz height width)) (Array shape) Float
forall a b. (a -> b) -> a -> b
$ (Extent meas vert horiz height width -> shape)
-> Array (Full meas vert horiz height width) Float
-> Vector shape Float
forall meas vert horiz height width shape a ar.
(Measure meas, C vert, C horiz, C height, C width, C shape,
 Floating a, RealOf a ~ ar, Storable ar) =>
(Extent meas vert horiz height width -> shape)
-> Full meas vert horiz height width a -> Vector shape ar
valuesAux Extent meas vert horiz height width -> shape
resultShape)
      ((Array (Full meas vert horiz height width) Double
 -> Array shape (RealOf Double))
-> TakeDiagonal
     (Array (Full meas vert horiz height width)) (Array shape) Double
forall (f :: * -> *) (g :: * -> *) a.
(f a -> g (RealOf a)) -> TakeDiagonal f g a
TakeDiagonal ((Array (Full meas vert horiz height width) Double
  -> Array shape (RealOf Double))
 -> TakeDiagonal
      (Array (Full meas vert horiz height width)) (Array shape) Double)
-> (Array (Full meas vert horiz height width) Double
    -> Array shape (RealOf Double))
-> TakeDiagonal
     (Array (Full meas vert horiz height width)) (Array shape) Double
forall a b. (a -> b) -> a -> b
$ (Extent meas vert horiz height width -> shape)
-> Array (Full meas vert horiz height width) Double
-> Vector shape Double
forall meas vert horiz height width shape a ar.
(Measure meas, C vert, C horiz, C height, C width, C shape,
 Floating a, RealOf a ~ ar, Storable ar) =>
(Extent meas vert horiz height width -> shape)
-> Full meas vert horiz height width a -> Vector shape ar
valuesAux Extent meas vert horiz height width -> shape
resultShape)
      ((Array (Full meas vert horiz height width) (Complex Float)
 -> Array shape (RealOf (Complex Float)))
-> TakeDiagonal
     (Array (Full meas vert horiz height width))
     (Array shape)
     (Complex Float)
forall (f :: * -> *) (g :: * -> *) a.
(f a -> g (RealOf a)) -> TakeDiagonal f g a
TakeDiagonal ((Array (Full meas vert horiz height width) (Complex Float)
  -> Array shape (RealOf (Complex Float)))
 -> TakeDiagonal
      (Array (Full meas vert horiz height width))
      (Array shape)
      (Complex Float))
-> (Array (Full meas vert horiz height width) (Complex Float)
    -> Array shape (RealOf (Complex Float)))
-> TakeDiagonal
     (Array (Full meas vert horiz height width))
     (Array shape)
     (Complex Float)
forall a b. (a -> b) -> a -> b
$ (Extent meas vert horiz height width -> shape)
-> Array (Full meas vert horiz height width) (Complex Float)
-> Vector shape Float
forall meas vert horiz height width shape a ar.
(Measure meas, C vert, C horiz, C height, C width, C shape,
 Floating a, RealOf a ~ ar, Storable ar) =>
(Extent meas vert horiz height width -> shape)
-> Full meas vert horiz height width a -> Vector shape ar
valuesAux Extent meas vert horiz height width -> shape
resultShape)
      ((Array (Full meas vert horiz height width) (Complex Double)
 -> Array shape (RealOf (Complex Double)))
-> TakeDiagonal
     (Array (Full meas vert horiz height width))
     (Array shape)
     (Complex Double)
forall (f :: * -> *) (g :: * -> *) a.
(f a -> g (RealOf a)) -> TakeDiagonal f g a
TakeDiagonal ((Array (Full meas vert horiz height width) (Complex Double)
  -> Array shape (RealOf (Complex Double)))
 -> TakeDiagonal
      (Array (Full meas vert horiz height width))
      (Array shape)
      (Complex Double))
-> (Array (Full meas vert horiz height width) (Complex Double)
    -> Array shape (RealOf (Complex Double)))
-> TakeDiagonal
     (Array (Full meas vert horiz height width))
     (Array shape)
     (Complex Double)
forall a b. (a -> b) -> a -> b
$ (Extent meas vert horiz height width -> shape)
-> Array (Full meas vert horiz height width) (Complex Double)
-> Vector shape Double
forall meas vert horiz height width shape a ar.
(Measure meas, C vert, C horiz, C height, C width, C shape,
 Floating a, RealOf a ~ ar, Storable ar) =>
(Extent meas vert horiz height width -> shape)
-> Full meas vert horiz height width a -> Vector shape ar
valuesAux Extent meas vert horiz height width -> shape
resultShape)

valuesAux ::
   (Extent.Measure meas, Extent.C vert, Extent.C horiz,
    Shape.C height, Shape.C width,
    Shape.C shape, Class.Floating a, RealOf a ~ ar, Storable ar) =>
   (Extent meas vert horiz height width -> shape) ->
   Full meas vert horiz height width a -> Vector shape ar
valuesAux :: (Extent meas vert horiz height width -> shape)
-> Full meas vert horiz height width a -> Vector shape ar
valuesAux Extent meas vert horiz height width -> shape
resultShape (Array shape :: Full meas vert horiz height width
shape@(Layout.Full Order
_order Extent meas vert horiz height width
extent) ForeignPtr a
a) =
   shape -> (Int -> Ptr ar -> IO ()) -> Vector shape ar
forall sh a.
(C sh, Storable a) =>
sh -> (Int -> Ptr a -> IO ()) -> Array sh a
Array.unsafeCreateWithSize (Extent meas vert horiz height width -> shape
resultShape Extent meas vert horiz height width
extent) ((Int -> Ptr ar -> IO ()) -> Vector shape ar)
-> (Int -> Ptr ar -> IO ()) -> Vector shape ar
forall a b. (a -> b) -> a -> b
$ \Int
mn Ptr ar
sPtr -> do
   let (Int
m,Int
n) = Full meas vert horiz height width -> (Int, Int)
forall meas vert horiz height width.
(Measure meas, C vert, C horiz, C height, C width) =>
Full meas vert horiz height width -> (Int, Int)
Layout.dimensions Full meas vert horiz height width
shape
   let lda :: Int
lda = Int
m
   ContT () IO () -> IO ()
forall (m :: * -> *) r. Monad m => ContT r m r -> m r
evalContT (ContT () IO () -> IO ()) -> ContT () IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
      Ptr CChar
jobuPtr <- Char -> FortranIO () (Ptr CChar)
forall r. Char -> FortranIO r (Ptr CChar)
Call.char Char
'N'
      Ptr CChar
jobvtPtr <- Char -> FortranIO () (Ptr CChar)
forall r. Char -> FortranIO r (Ptr CChar)
Call.char Char
'N'
      Ptr CInt
mPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint Int
m
      Ptr CInt
nPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint Int
n
      Ptr a
aPtr <- Int -> ForeignPtr a -> ContT () IO (Ptr a)
forall a r. Storable a => Int -> ForeignPtr a -> ContT r IO (Ptr a)
copyToTemp (Int
mInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
n) ForeignPtr a
a
      Ptr CInt
ldaPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.leadingDim Int
lda
      let uPtr :: Ptr a
uPtr = Ptr a
forall a. Ptr a
nullPtr
      let vtPtr :: Ptr a
vtPtr = Ptr a
forall a. Ptr a
nullPtr
      Ptr CInt
lduPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.leadingDim Int
m
      Ptr CInt
ldvtPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.leadingDim Int
n
      IO () -> ContT () IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ContT () IO ()) -> IO () -> ContT () IO ()
forall a b. (a -> b) -> a -> b
$
         String -> (Ptr CInt -> IO ()) -> IO ()
withInfo String
"gesvd" ((Ptr CInt -> IO ()) -> IO ()) -> (Ptr CInt -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr CInt
infoPtr ->
         GESVD_ (RealOf a) a
forall a. Floating a => GESVD_ (RealOf a) a
gesvd Ptr CChar
jobuPtr Ptr CChar
jobvtPtr Ptr CInt
mPtr Ptr CInt
nPtr
            Ptr a
aPtr Ptr CInt
ldaPtr Ptr ar
Ptr (RealOf a)
sPtr Ptr a
forall a. Ptr a
uPtr Ptr CInt
lduPtr Ptr a
forall a. Ptr a
vtPtr Ptr CInt
ldvtPtr Int
mn Ptr CInt
infoPtr


determinantAbsolute ::
   (Shape.C height, Shape.C width, Class.Floating a) =>
   General height width a -> RealOf a
determinantAbsolute :: General height width a -> RealOf a
determinantAbsolute =
   Determinant (Array (General height width)) a
-> General height width a -> RealOf a
forall (f :: * -> *) a. Determinant f a -> f a -> RealOf a
getDeterminant (Determinant (Array (General height width)) a
 -> General height width a -> RealOf a)
-> Determinant (Array (General height width)) a
-> General height width a
-> RealOf a
forall a b. (a -> b) -> a -> b
$
   Determinant (Array (General height width)) Float
-> Determinant (Array (General height width)) Double
-> Determinant (Array (General height width)) (Complex Float)
-> Determinant (Array (General height width)) (Complex Double)
-> Determinant (Array (General height width)) a
forall a (f :: * -> *).
Floating a =>
f Float
-> f Double -> f (Complex Float) -> f (Complex Double) -> f a
Class.switchFloating
      ((Array (General height width) Float -> RealOf Float)
-> Determinant (Array (General height width)) Float
forall (f :: * -> *) a. (f a -> RealOf a) -> Determinant f a
Determinant Array (General height width) Float -> RealOf Float
forall height width a ar.
(C height, C width, Floating a, RealOf a ~ ar, Real ar) =>
General height width a -> ar
determinantAbsoluteAux)
      ((Array (General height width) Double -> RealOf Double)
-> Determinant (Array (General height width)) Double
forall (f :: * -> *) a. (f a -> RealOf a) -> Determinant f a
Determinant Array (General height width) Double -> RealOf Double
forall height width a ar.
(C height, C width, Floating a, RealOf a ~ ar, Real ar) =>
General height width a -> ar
determinantAbsoluteAux)
      ((Array (General height width) (Complex Float)
 -> RealOf (Complex Float))
-> Determinant (Array (General height width)) (Complex Float)
forall (f :: * -> *) a. (f a -> RealOf a) -> Determinant f a
Determinant Array (General height width) (Complex Float)
-> RealOf (Complex Float)
forall height width a ar.
(C height, C width, Floating a, RealOf a ~ ar, Real ar) =>
General height width a -> ar
determinantAbsoluteAux)
      ((Array (General height width) (Complex Double)
 -> RealOf (Complex Double))
-> Determinant (Array (General height width)) (Complex Double)
forall (f :: * -> *) a. (f a -> RealOf a) -> Determinant f a
Determinant Array (General height width) (Complex Double)
-> RealOf (Complex Double)
forall height width a ar.
(C height, C width, Floating a, RealOf a ~ ar, Real ar) =>
General height width a -> ar
determinantAbsoluteAux)

determinantAbsoluteAux ::
   (Shape.C height, Shape.C width,
    Class.Floating a, RealOf a ~ ar, Class.Real ar) =>
   General height width a -> ar
determinantAbsoluteAux :: General height width a -> ar
determinantAbsoluteAux =
   (Full Size Big Small height width a -> ar)
-> (Wide height width a -> ar)
-> Either
     (Full Size Big Small height width a) (Wide height width a)
-> ar
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Vector width ar -> ar
forall sh a. (C sh, Floating a) => Vector sh a -> a
Vector.product (Vector width ar -> ar)
-> (Full Size Big Small height width a -> Vector width ar)
-> Full Size Big Small height width a
-> ar
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Full Size Big Small height width a -> Vector width ar
forall meas vert height width a.
(Measure meas, C vert, C height, C width, Floating a) =>
Full meas vert Small height width a -> RealVector width a
valuesTall) (ar -> Wide height width a -> ar
forall a b. a -> b -> a
const ar
forall a. Floating a => a
zero)
   (Either (Full Size Big Small height width a) (Wide height width a)
 -> ar)
-> (General height width a
    -> Either
         (Full Size Big Small height width a) (Wide height width a))
-> General height width a
-> ar
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
   General height width a
-> Either
     (Full Size Big Small height width a) (Wide height width a)
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz, C height, C width) =>
Full meas vert horiz height width a
-> Either (Tall height width a) (Wide height width a)
Basic.caseTallWide


decompose ::
   (Extent.Measure meas, Extent.C vert, Extent.C horiz) =>
   (Shape.C height, Shape.C width, Class.Floating a) =>
   Full meas vert horiz height width a ->
   (Square height a,
    RectangularRealDiagonal meas vert horiz height width a,
    Square width a)
decompose :: Full meas vert horiz height width a
-> (Square height a,
    RectangularRealDiagonal meas vert horiz height width a,
    Square width a)
decompose =
   Decompose
  (Array (Full meas vert horiz height width))
  (Array (Square height))
  (Array (RectangularDiagonal meas vert horiz height width))
  (Array (Square width))
  a
-> Full meas vert horiz height width a
-> (Square height a,
    RectangularRealDiagonal meas vert horiz height width a,
    Square width a)
forall (m :: * -> *) (f :: * -> *) (v :: * -> *) (g :: * -> *) a.
Decompose m f v g a -> m a -> (f a, v (RealOf a), g a)
getDecompose (Decompose
   (Array (Full meas vert horiz height width))
   (Array (Square height))
   (Array (RectangularDiagonal meas vert horiz height width))
   (Array (Square width))
   a
 -> Full meas vert horiz height width a
 -> (Square height a,
     RectangularRealDiagonal meas vert horiz height width a,
     Square width a))
-> Decompose
     (Array (Full meas vert horiz height width))
     (Array (Square height))
     (Array (RectangularDiagonal meas vert horiz height width))
     (Array (Square width))
     a
-> Full meas vert horiz height width a
-> (Square height a,
    RectangularRealDiagonal meas vert horiz height width a,
    Square width a)
forall a b. (a -> b) -> a -> b
$
   Decompose
  (Array (Full meas vert horiz height width))
  (Array (Square height))
  (Array (RectangularDiagonal meas vert horiz height width))
  (Array (Square width))
  Float
-> Decompose
     (Array (Full meas vert horiz height width))
     (Array (Square height))
     (Array (RectangularDiagonal meas vert horiz height width))
     (Array (Square width))
     Double
-> Decompose
     (Array (Full meas vert horiz height width))
     (Array (Square height))
     (Array (RectangularDiagonal meas vert horiz height width))
     (Array (Square width))
     (Complex Float)
-> Decompose
     (Array (Full meas vert horiz height width))
     (Array (Square height))
     (Array (RectangularDiagonal meas vert horiz height width))
     (Array (Square width))
     (Complex Double)
-> Decompose
     (Array (Full meas vert horiz height width))
     (Array (Square height))
     (Array (RectangularDiagonal meas vert horiz height width))
     (Array (Square width))
     a
forall a (f :: * -> *).
Floating a =>
f Float
-> f Double -> f (Complex Float) -> f (Complex Double) -> f a
Class.switchFloating
      ((Array (Full meas vert horiz height width) Float
 -> (Array (Square height) Float,
     Array
       (RectangularDiagonal meas vert horiz height width) (RealOf Float),
     Array (Square width) Float))
-> Decompose
     (Array (Full meas vert horiz height width))
     (Array (Square height))
     (Array (RectangularDiagonal meas vert horiz height width))
     (Array (Square width))
     Float
forall (m :: * -> *) (f :: * -> *) (v :: * -> *) (g :: * -> *) a.
(m a -> (f a, v (RealOf a), g a)) -> Decompose m f v g a
Decompose Array (Full meas vert horiz height width) Float
-> (Array (Square height) Float,
    Array
      (RectangularDiagonal meas vert horiz height width) (RealOf Float),
    Array (Square width) Float)
forall meas vert horiz height width a ar.
(Measure meas, C vert, C horiz, C height, C width, Floating a,
 RealOf a ~ ar, Storable ar) =>
Full meas vert horiz height width a
-> (Square height a,
    RectangularRealDiagonal meas vert horiz height width a,
    Square width a)
decomposeAux)
      ((Array (Full meas vert horiz height width) Double
 -> (Array (Square height) Double,
     Array
       (RectangularDiagonal meas vert horiz height width) (RealOf Double),
     Array (Square width) Double))
-> Decompose
     (Array (Full meas vert horiz height width))
     (Array (Square height))
     (Array (RectangularDiagonal meas vert horiz height width))
     (Array (Square width))
     Double
forall (m :: * -> *) (f :: * -> *) (v :: * -> *) (g :: * -> *) a.
(m a -> (f a, v (RealOf a), g a)) -> Decompose m f v g a
Decompose Array (Full meas vert horiz height width) Double
-> (Array (Square height) Double,
    Array
      (RectangularDiagonal meas vert horiz height width) (RealOf Double),
    Array (Square width) Double)
forall meas vert horiz height width a ar.
(Measure meas, C vert, C horiz, C height, C width, Floating a,
 RealOf a ~ ar, Storable ar) =>
Full meas vert horiz height width a
-> (Square height a,
    RectangularRealDiagonal meas vert horiz height width a,
    Square width a)
decomposeAux)
      ((Array (Full meas vert horiz height width) (Complex Float)
 -> (Array (Square height) (Complex Float),
     Array
       (RectangularDiagonal meas vert horiz height width)
       (RealOf (Complex Float)),
     Array (Square width) (Complex Float)))
-> Decompose
     (Array (Full meas vert horiz height width))
     (Array (Square height))
     (Array (RectangularDiagonal meas vert horiz height width))
     (Array (Square width))
     (Complex Float)
forall (m :: * -> *) (f :: * -> *) (v :: * -> *) (g :: * -> *) a.
(m a -> (f a, v (RealOf a), g a)) -> Decompose m f v g a
Decompose Array (Full meas vert horiz height width) (Complex Float)
-> (Array (Square height) (Complex Float),
    Array
      (RectangularDiagonal meas vert horiz height width)
      (RealOf (Complex Float)),
    Array (Square width) (Complex Float))
forall meas vert horiz height width a ar.
(Measure meas, C vert, C horiz, C height, C width, Floating a,
 RealOf a ~ ar, Storable ar) =>
Full meas vert horiz height width a
-> (Square height a,
    RectangularRealDiagonal meas vert horiz height width a,
    Square width a)
decomposeAux)
      ((Array (Full meas vert horiz height width) (Complex Double)
 -> (Array (Square height) (Complex Double),
     Array
       (RectangularDiagonal meas vert horiz height width)
       (RealOf (Complex Double)),
     Array (Square width) (Complex Double)))
-> Decompose
     (Array (Full meas vert horiz height width))
     (Array (Square height))
     (Array (RectangularDiagonal meas vert horiz height width))
     (Array (Square width))
     (Complex Double)
forall (m :: * -> *) (f :: * -> *) (v :: * -> *) (g :: * -> *) a.
(m a -> (f a, v (RealOf a), g a)) -> Decompose m f v g a
Decompose Array (Full meas vert horiz height width) (Complex Double)
-> (Array (Square height) (Complex Double),
    Array
      (RectangularDiagonal meas vert horiz height width)
      (RealOf (Complex Double)),
    Array (Square width) (Complex Double))
forall meas vert horiz height width a ar.
(Measure meas, C vert, C horiz, C height, C width, Floating a,
 RealOf a ~ ar, Storable ar) =>
Full meas vert horiz height width a
-> (Square height a,
    RectangularRealDiagonal meas vert horiz height width a,
    Square width a)
decomposeAux)

newtype Decompose m f v g a =
   Decompose {Decompose m f v g a -> m a -> (f a, v (RealOf a), g a)
getDecompose :: m a -> (f a, v (RealOf a), g a)}

decomposeAux ::
   (Extent.Measure meas, Extent.C vert, Extent.C horiz) =>
   (Shape.C height, Shape.C width,
    Class.Floating a, RealOf a ~ ar, Storable ar) =>
   Full meas vert horiz height width a ->
   (Square height a,
    RectangularRealDiagonal meas vert horiz height width a,
    Square width a)
decomposeAux :: Full meas vert horiz height width a
-> (Square height a,
    RectangularRealDiagonal meas vert horiz height width a,
    Square width a)
decomposeAux arr :: Full meas vert horiz height width a
arr@(Array shape :: Full meas vert horiz height width
shape@(Layout.Full Order
order Extent meas vert horiz height width
extent) ForeignPtr a
a) =

   let (height
height,width
width) = Extent meas vert horiz height width -> (height, width)
forall meas vert horiz height width.
(Measure meas, C vert, C horiz) =>
Extent meas vert horiz height width -> (height, width)
Extent.dimensions Extent meas vert horiz height width
extent
       (Int
mn,RectangularDiagonal meas vert horiz height width
minShape) = Extent meas vert horiz height width
-> (Int, RectangularDiagonal meas vert horiz height width)
forall meas vert horiz height width.
(Measure meas, C vert, C horiz, C height, C width) =>
Extent meas vert horiz height width
-> (Int, RectangularDiagonal meas vert horiz height width)
Layout.rectangularDiagonal Extent meas vert horiz height width
extent

   in (Bool
-> (Square height a,
    Vector (RectangularDiagonal meas vert horiz height width) ar,
    Square width a)
-> (Square height a,
    Vector (RectangularDiagonal meas vert horiz height width) ar,
    Square width a)
-> (Square height a,
    Vector (RectangularDiagonal meas vert horiz height width) ar,
    Square width a)
forall a. Bool -> a -> a -> a
if' (Int
mnInt -> Int -> Bool
forall a. Eq a => a -> a -> Bool
==Int
0)
         (General height width a -> Square height a
forall height width a.
(C height, C width, Floating a) =>
General height width a -> Square height a
Square.identityFromHeight (General height width a -> Square height a)
-> General height width a -> Square height a
forall a b. (a -> b) -> a -> b
$ Full meas vert horiz height width a -> General height width a
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz) =>
Full meas vert horiz height width a -> General height width a
Matrix.fromFull Full meas vert horiz height width a
arr,
          RectangularDiagonal meas vert horiz height width
-> [ar]
-> Vector (RectangularDiagonal meas vert horiz height width) ar
forall sh a. (C sh, Storable a) => sh -> [a] -> Vector sh a
Vector.fromList RectangularDiagonal meas vert horiz height width
minShape [],
          General height width a -> Square width a
forall height width a.
(C height, C width, Floating a) =>
General height width a -> Square width a
Square.identityFromWidth (General height width a -> Square width a)
-> General height width a -> Square width a
forall a b. (a -> b) -> a -> b
$ Full meas vert horiz height width a -> General height width a
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz) =>
Full meas vert horiz height width a -> General height width a
Matrix.fromFull Full meas vert horiz height width a
arr)) ((Square height a,
  Vector (RectangularDiagonal meas vert horiz height width) ar,
  Square width a)
 -> (Square height a,
     Vector (RectangularDiagonal meas vert horiz height width) ar,
     Square width a))
-> (Square height a,
    Vector (RectangularDiagonal meas vert horiz height width) ar,
    Square width a)
-> (Square height a,
    Vector (RectangularDiagonal meas vert horiz height width) ar,
    Square width a)
forall a b. (a -> b) -> a -> b
$
      (\(Square height a
u,(Vector (RectangularDiagonal meas vert horiz height width) ar
s,Square width a
vt)) -> (Square height a
u,Vector (RectangularDiagonal meas vert horiz height width) ar
s,Square width a
vt)) ((Square height a,
  (Vector (RectangularDiagonal meas vert horiz height width) ar,
   Square width a))
 -> (Square height a,
     Vector (RectangularDiagonal meas vert horiz height width) ar,
     Square width a))
-> (Square height a,
    (Vector (RectangularDiagonal meas vert horiz height width) ar,
     Square width a))
-> (Square height a,
    Vector (RectangularDiagonal meas vert horiz height width) ar,
    Square width a)
forall a b. (a -> b) -> a -> b
$
      Square height
-> (Int
    -> Ptr a
    -> IO
         (Vector (RectangularDiagonal meas vert horiz height width) ar,
          Square width a))
-> (Square height a,
    (Vector (RectangularDiagonal meas vert horiz height width) ar,
     Square width a))
forall sh a b.
(C sh, Storable a) =>
sh -> (Int -> Ptr a -> IO b) -> (Array sh a, b)
Array.unsafeCreateWithSizeAndResult (Order -> height -> Square height
forall sh. Order -> sh -> Square sh
Layout.square Order
order height
height) ((Int
  -> Ptr a
  -> IO
       (Vector (RectangularDiagonal meas vert horiz height width) ar,
        Square width a))
 -> (Square height a,
     (Vector (RectangularDiagonal meas vert horiz height width) ar,
      Square width a)))
-> (Int
    -> Ptr a
    -> IO
         (Vector (RectangularDiagonal meas vert horiz height width) ar,
          Square width a))
-> (Square height a,
    (Vector (RectangularDiagonal meas vert horiz height width) ar,
     Square width a))
forall a b. (a -> b) -> a -> b
$
         \ Int
_ Ptr a
uPtr0 ->
      RectangularDiagonal meas vert horiz height width
-> (Int -> Ptr ar -> IO (Square width a))
-> IO
     (Vector (RectangularDiagonal meas vert horiz height width) ar,
      Square width a)
forall (m :: * -> *) sh a b.
(PrimMonad m, C sh, Storable a) =>
sh -> (Int -> Ptr a -> IO b) -> m (Array sh a, b)
ArrayIO.unsafeCreateWithSizeAndResult RectangularDiagonal meas vert horiz height width
minShape ((Int -> Ptr ar -> IO (Square width a))
 -> IO
      (Vector (RectangularDiagonal meas vert horiz height width) ar,
       Square width a))
-> (Int -> Ptr ar -> IO (Square width a))
-> IO
     (Vector (RectangularDiagonal meas vert horiz height width) ar,
      Square width a)
forall a b. (a -> b) -> a -> b
$ \ Int
_ Ptr ar
sPtr ->
      Square width -> (Ptr a -> IO ()) -> IO (Square width a)
forall (m :: * -> *) sh a.
(PrimMonad m, C sh, Storable a) =>
sh -> (Ptr a -> IO ()) -> m (Array sh a)
ArrayIO.unsafeCreate (Order -> width -> Square width
forall sh. Order -> sh -> Square sh
Layout.square Order
order width
width) ((Ptr a -> IO ()) -> IO (Square width a))
-> (Ptr a -> IO ()) -> IO (Square width a)
forall a b. (a -> b) -> a -> b
$ \Ptr a
vtPtr0 ->

   ContT () IO () -> IO ()
forall (m :: * -> *) r. Monad m => ContT r m r -> m r
evalContT (ContT () IO () -> IO ()) -> ContT () IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
      let (Int
m,Int
n) = Full meas vert horiz height width -> (Int, Int)
forall meas vert horiz height width.
(Measure meas, C vert, C horiz, C height, C width) =>
Full meas vert horiz height width -> (Int, Int)
Layout.dimensions Full meas vert horiz height width
shape
      let (Ptr a
uPtr,Ptr a
vtPtr) = Order -> (Ptr a, Ptr a) -> (Ptr a, Ptr a)
forall a. Order -> (a, a) -> (a, a)
swapOnRowMajor Order
order (Ptr a
uPtr0,Ptr a
vtPtr0)
      let lda :: Int
lda = Int
m
      Ptr CChar
jobuPtr <- Char -> FortranIO () (Ptr CChar)
forall r. Char -> FortranIO r (Ptr CChar)
Call.char Char
'A'
      Ptr CChar
jobvtPtr <- Char -> FortranIO () (Ptr CChar)
forall r. Char -> FortranIO r (Ptr CChar)
Call.char Char
'A'
      Ptr CInt
mPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint Int
m
      Ptr CInt
nPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint Int
n
      Ptr a
aPtr <- Int -> ForeignPtr a -> ContT () IO (Ptr a)
forall a r. Storable a => Int -> ForeignPtr a -> ContT r IO (Ptr a)
copyToTemp (Int
mInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
n) ForeignPtr a
a
      Ptr CInt
ldaPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.leadingDim Int
lda
      Ptr CInt
lduPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.leadingDim Int
m
      Ptr CInt
ldvtPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.leadingDim Int
n
      IO () -> ContT () IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ContT () IO ()) -> IO () -> ContT () IO ()
forall a b. (a -> b) -> a -> b
$
         String -> (Ptr CInt -> IO ()) -> IO ()
withInfo String
"gesvd" ((Ptr CInt -> IO ()) -> IO ()) -> (Ptr CInt -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr CInt
infoPtr ->
         GESVD_ (RealOf a) a
forall a. Floating a => GESVD_ (RealOf a) a
gesvd Ptr CChar
jobuPtr Ptr CChar
jobvtPtr Ptr CInt
mPtr Ptr CInt
nPtr
            Ptr a
aPtr Ptr CInt
ldaPtr Ptr ar
Ptr (RealOf a)
sPtr Ptr a
uPtr Ptr CInt
lduPtr Ptr a
vtPtr Ptr CInt
ldvtPtr Int
mn Ptr CInt
infoPtr


decomposeWide ::
   (Extent.Measure meas, Extent.C horiz,
    Shape.C height, Shape.C width, Class.Floating a) =>
   Full meas Extent.Small horiz height width a ->
   (Square height a, RealVector height a,
      Full meas Extent.Small horiz height width a)
decomposeWide :: Full meas Small horiz height width a
-> (Square height a, RealVector height a,
    Full meas Small horiz height width a)
decomposeWide Full meas Small horiz height width a
a =
   let (Full meas horiz Small width height a
u,RealVector height a
s,Square height a
vt) = Full meas horiz Small width height a
-> (Full meas horiz Small width height a, RealVector height a,
    Square height a)
forall meas vert height width a.
(Measure meas, C vert, C height, C width, Floating a) =>
Full meas vert Small height width a
-> (Full meas vert Small height width a, RealVector width a,
    Square width a)
decomposeTall (Full meas horiz Small width height a
 -> (Full meas horiz Small width height a, RealVector height a,
     Square height a))
-> Full meas horiz Small width height a
-> (Full meas horiz Small width height a, RealVector height a,
    Square height a)
forall a b. (a -> b) -> a -> b
$ Full meas Small horiz height width a
-> Full meas horiz Small width height a
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz) =>
Full meas vert horiz height width a
-> Full meas horiz vert width height a
Basic.transpose Full meas Small horiz height width a
a
   in  (Square height a -> Square height a
forall sh a. Square sh a -> Square sh a
Square.transpose Square height a
vt, RealVector height a
s, Full meas horiz Small width height a
-> Full meas Small horiz height width a
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz) =>
Full meas vert horiz height width a
-> Full meas horiz vert width height a
Basic.transpose Full meas horiz Small width height a
u)

decomposeTall ::
   (Extent.Measure meas, Extent.C vert,
    Shape.C height, Shape.C width, Class.Floating a) =>
   Full meas vert Extent.Small height width a ->
   (Full meas vert Extent.Small height width a,
      RealVector width a, Square width a)
decomposeTall :: Full meas vert Small height width a
-> (Full meas vert Small height width a, RealVector width a,
    Square width a)
decomposeTall =
   Decompose
  (Array (Full meas vert Small height width))
  (Array (Full meas vert Small height width))
  (Array width)
  (Array (Square width))
  a
-> Full meas vert Small height width a
-> (Full meas vert Small height width a, RealVector width a,
    Square width a)
forall (m :: * -> *) (f :: * -> *) (v :: * -> *) (g :: * -> *) a.
Decompose m f v g a -> m a -> (f a, v (RealOf a), g a)
getDecompose (Decompose
   (Array (Full meas vert Small height width))
   (Array (Full meas vert Small height width))
   (Array width)
   (Array (Square width))
   a
 -> Full meas vert Small height width a
 -> (Full meas vert Small height width a, RealVector width a,
     Square width a))
-> Decompose
     (Array (Full meas vert Small height width))
     (Array (Full meas vert Small height width))
     (Array width)
     (Array (Square width))
     a
-> Full meas vert Small height width a
-> (Full meas vert Small height width a, RealVector width a,
    Square width a)
forall a b. (a -> b) -> a -> b
$
   Decompose
  (Array (Full meas vert Small height width))
  (Array (Full meas vert Small height width))
  (Array width)
  (Array (Square width))
  Float
-> Decompose
     (Array (Full meas vert Small height width))
     (Array (Full meas vert Small height width))
     (Array width)
     (Array (Square width))
     Double
-> Decompose
     (Array (Full meas vert Small height width))
     (Array (Full meas vert Small height width))
     (Array width)
     (Array (Square width))
     (Complex Float)
-> Decompose
     (Array (Full meas vert Small height width))
     (Array (Full meas vert Small height width))
     (Array width)
     (Array (Square width))
     (Complex Double)
-> Decompose
     (Array (Full meas vert Small height width))
     (Array (Full meas vert Small height width))
     (Array width)
     (Array (Square width))
     a
forall a (f :: * -> *).
Floating a =>
f Float
-> f Double -> f (Complex Float) -> f (Complex Double) -> f a
Class.switchFloating
      ((Array (Full meas vert Small height width) Float
 -> (Array (Full meas vert Small height width) Float,
     Array width (RealOf Float), Array (Square width) Float))
-> Decompose
     (Array (Full meas vert Small height width))
     (Array (Full meas vert Small height width))
     (Array width)
     (Array (Square width))
     Float
forall (m :: * -> *) (f :: * -> *) (v :: * -> *) (g :: * -> *) a.
(m a -> (f a, v (RealOf a), g a)) -> Decompose m f v g a
Decompose Array (Full meas vert Small height width) Float
-> (Array (Full meas vert Small height width) Float,
    Array width (RealOf Float), Array (Square width) Float)
forall meas vert height width a ar.
(Measure meas, C vert, C height, C width, Floating a,
 RealOf a ~ ar, Storable ar) =>
Full meas vert Small height width a
-> (Full meas vert Small height width a, Vector width ar,
    Square width a)
decomposeThin)
      ((Array (Full meas vert Small height width) Double
 -> (Array (Full meas vert Small height width) Double,
     Array width (RealOf Double), Array (Square width) Double))
-> Decompose
     (Array (Full meas vert Small height width))
     (Array (Full meas vert Small height width))
     (Array width)
     (Array (Square width))
     Double
forall (m :: * -> *) (f :: * -> *) (v :: * -> *) (g :: * -> *) a.
(m a -> (f a, v (RealOf a), g a)) -> Decompose m f v g a
Decompose Array (Full meas vert Small height width) Double
-> (Array (Full meas vert Small height width) Double,
    Array width (RealOf Double), Array (Square width) Double)
forall meas vert height width a ar.
(Measure meas, C vert, C height, C width, Floating a,
 RealOf a ~ ar, Storable ar) =>
Full meas vert Small height width a
-> (Full meas vert Small height width a, Vector width ar,
    Square width a)
decomposeThin)
      ((Array (Full meas vert Small height width) (Complex Float)
 -> (Array (Full meas vert Small height width) (Complex Float),
     Array width (RealOf (Complex Float)),
     Array (Square width) (Complex Float)))
-> Decompose
     (Array (Full meas vert Small height width))
     (Array (Full meas vert Small height width))
     (Array width)
     (Array (Square width))
     (Complex Float)
forall (m :: * -> *) (f :: * -> *) (v :: * -> *) (g :: * -> *) a.
(m a -> (f a, v (RealOf a), g a)) -> Decompose m f v g a
Decompose Array (Full meas vert Small height width) (Complex Float)
-> (Array (Full meas vert Small height width) (Complex Float),
    Array width (RealOf (Complex Float)),
    Array (Square width) (Complex Float))
forall meas vert height width a ar.
(Measure meas, C vert, C height, C width, Floating a,
 RealOf a ~ ar, Storable ar) =>
Full meas vert Small height width a
-> (Full meas vert Small height width a, Vector width ar,
    Square width a)
decomposeThin)
      ((Array (Full meas vert Small height width) (Complex Double)
 -> (Array (Full meas vert Small height width) (Complex Double),
     Array width (RealOf (Complex Double)),
     Array (Square width) (Complex Double)))
-> Decompose
     (Array (Full meas vert Small height width))
     (Array (Full meas vert Small height width))
     (Array width)
     (Array (Square width))
     (Complex Double)
forall (m :: * -> *) (f :: * -> *) (v :: * -> *) (g :: * -> *) a.
(m a -> (f a, v (RealOf a), g a)) -> Decompose m f v g a
Decompose Array (Full meas vert Small height width) (Complex Double)
-> (Array (Full meas vert Small height width) (Complex Double),
    Array width (RealOf (Complex Double)),
    Array (Square width) (Complex Double))
forall meas vert height width a ar.
(Measure meas, C vert, C height, C width, Floating a,
 RealOf a ~ ar, Storable ar) =>
Full meas vert Small height width a
-> (Full meas vert Small height width a, Vector width ar,
    Square width a)
decomposeThin)

decomposeThin ::
   (Extent.Measure meas, Extent.C vert, Shape.C height, Shape.C width,
    Class.Floating a, RealOf a ~ ar, Storable ar) =>
   Full meas vert Extent.Small height width a ->
   (Full meas vert Extent.Small height width a, Vector width ar, Square width a)
decomposeThin :: Full meas vert Small height width a
-> (Full meas vert Small height width a, Vector width ar,
    Square width a)
decomposeThin (Array (Layout.Full Order
order Extent meas vert Small height width
extent) ForeignPtr a
a) =
   let (height
height,width
width) = Extent meas vert Small height width -> (height, width)
forall meas vert horiz height width.
(Measure meas, C vert, C horiz) =>
Extent meas vert horiz height width -> (height, width)
Extent.dimensions Extent meas vert Small height width
extent
   in (\(Full meas vert Small height width a
u,(Vector width ar
s,Square width a
vt)) -> (Full meas vert Small height width a
u,Vector width ar
s,Square width a
vt)) ((Full meas vert Small height width a,
  (Vector width ar, Square width a))
 -> (Full meas vert Small height width a, Vector width ar,
     Square width a))
-> (Full meas vert Small height width a,
    (Vector width ar, Square width a))
-> (Full meas vert Small height width a, Vector width ar,
    Square width a)
forall a b. (a -> b) -> a -> b
$
      Full meas vert Small height width
-> (Int -> Ptr a -> IO (Vector width ar, Square width a))
-> (Full meas vert Small height width a,
    (Vector width ar, Square width a))
forall sh a b.
(C sh, Storable a) =>
sh -> (Int -> Ptr a -> IO b) -> (Array sh a, b)
Array.unsafeCreateWithSizeAndResult (Order
-> Extent meas vert Small height width
-> Full meas vert Small height width
forall meas vert horiz height width.
Order
-> Extent meas vert horiz height width
-> Full meas vert horiz height width
Layout.Full Order
order Extent meas vert Small height width
extent) ((Int -> Ptr a -> IO (Vector width ar, Square width a))
 -> (Full meas vert Small height width a,
     (Vector width ar, Square width a)))
-> (Int -> Ptr a -> IO (Vector width ar, Square width a))
-> (Full meas vert Small height width a,
    (Vector width ar, Square width a))
forall a b. (a -> b) -> a -> b
$
         \ Int
_ Ptr a
uPtr0 ->
      width
-> (Int -> Ptr ar -> IO (Square width a))
-> IO (Vector width ar, Square width a)
forall (m :: * -> *) sh a b.
(PrimMonad m, C sh, Storable a) =>
sh -> (Int -> Ptr a -> IO b) -> m (Array sh a, b)
ArrayIO.unsafeCreateWithSizeAndResult width
width ((Int -> Ptr ar -> IO (Square width a))
 -> IO (Vector width ar, Square width a))
-> (Int -> Ptr ar -> IO (Square width a))
-> IO (Vector width ar, Square width a)
forall a b. (a -> b) -> a -> b
$ \ Int
_ Ptr ar
sPtr ->
      Square width -> (Ptr a -> IO ()) -> IO (Square width a)
forall (m :: * -> *) sh a.
(PrimMonad m, C sh, Storable a) =>
sh -> (Ptr a -> IO ()) -> m (Array sh a)
ArrayIO.unsafeCreate (Order -> width -> Square width
forall sh. Order -> sh -> Square sh
Layout.square Order
order width
width) ((Ptr a -> IO ()) -> IO (Square width a))
-> (Ptr a -> IO ()) -> IO (Square width a)
forall a b. (a -> b) -> a -> b
$ \Ptr a
vtPtr0 ->

   ContT () IO () -> IO ()
forall (m :: * -> *) r. Monad m => ContT r m r -> m r
evalContT (ContT () IO () -> IO ()) -> ContT () IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
      let ((Int
m,Ptr a
uPtr),(Int
n,Ptr a
vtPtr)) =
            Order
-> ((Int, Ptr a), (Int, Ptr a)) -> ((Int, Ptr a), (Int, Ptr a))
forall a. Order -> (a, a) -> (a, a)
swapOnRowMajor Order
order
               ((height -> Int
forall sh. C sh => sh -> Int
Shape.size height
height, Ptr a
uPtr0), (width -> Int
forall sh. C sh => sh -> Int
Shape.size width
width, Ptr a
vtPtr0))
      let mn :: Int
mn = Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
m Int
n
      let lda :: Int
lda = Int
m
      Ptr CChar
jobuPtr <- Char -> FortranIO () (Ptr CChar)
forall r. Char -> FortranIO r (Ptr CChar)
Call.char Char
'S'
      Ptr CChar
jobvtPtr <- Char -> FortranIO () (Ptr CChar)
forall r. Char -> FortranIO r (Ptr CChar)
Call.char Char
'S'
      Ptr CInt
mPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint Int
m
      Ptr CInt
nPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint Int
n
      Ptr a
aPtr <- Int -> ForeignPtr a -> ContT () IO (Ptr a)
forall a r. Storable a => Int -> ForeignPtr a -> ContT r IO (Ptr a)
copyToTemp (Int
mInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
n) ForeignPtr a
a
      Ptr CInt
ldaPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.leadingDim Int
lda
      Ptr CInt
lduPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.leadingDim Int
m
      Ptr CInt
ldvtPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.leadingDim Int
mn
      IO () -> ContT () IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ContT () IO ()) -> IO () -> ContT () IO ()
forall a b. (a -> b) -> a -> b
$
         String -> (Ptr CInt -> IO ()) -> IO ()
withInfo String
"gesvd" ((Ptr CInt -> IO ()) -> IO ()) -> (Ptr CInt -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr CInt
infoPtr ->
         GESVD_ (RealOf a) a
forall a. Floating a => GESVD_ (RealOf a) a
gesvd Ptr CChar
jobuPtr Ptr CChar
jobvtPtr Ptr CInt
mPtr Ptr CInt
nPtr
            Ptr a
aPtr Ptr CInt
ldaPtr Ptr ar
Ptr (RealOf a)
sPtr Ptr a
uPtr Ptr CInt
lduPtr Ptr a
vtPtr Ptr CInt
ldvtPtr Int
mn Ptr CInt
infoPtr


type GESVD_ ar a =
   Ptr CChar -> Ptr CChar -> Ptr CInt -> Ptr CInt ->
   Ptr a -> Ptr CInt -> Ptr ar ->
   Ptr a -> Ptr CInt -> Ptr a -> Ptr CInt -> Int -> Ptr CInt -> IO ()

newtype GESVD a = GESVD {GESVD a -> GESVD_ (RealOf a) a
getGESVD :: GESVD_ (RealOf a) a}

gesvd :: Class.Floating a => GESVD_ (RealOf a) a
gesvd :: GESVD_ (RealOf a) a
gesvd =
   GESVD a -> GESVD_ (RealOf a) a
forall a. GESVD a -> GESVD_ (RealOf a) a
getGESVD (GESVD a -> GESVD_ (RealOf a) a) -> GESVD a -> GESVD_ (RealOf a) a
forall a b. (a -> b) -> a -> b
$
   GESVD Float
-> GESVD Double
-> GESVD (Complex Float)
-> GESVD (Complex Double)
-> GESVD a
forall a (f :: * -> *).
Floating a =>
f Float
-> f Double -> f (Complex Float) -> f (Complex Double) -> f a
Class.switchFloating
      (GESVD_ (RealOf Float) Float -> GESVD Float
forall a. GESVD_ (RealOf a) a -> GESVD a
GESVD GESVD_ (RealOf Float) Float
forall a. Real a => GESVD_ a a
gesvdReal)
      (GESVD_ (RealOf Double) Double -> GESVD Double
forall a. GESVD_ (RealOf a) a -> GESVD a
GESVD GESVD_ (RealOf Double) Double
forall a. Real a => GESVD_ a a
gesvdReal)
      (GESVD_ (RealOf (Complex Float)) (Complex Float)
-> GESVD (Complex Float)
forall a. GESVD_ (RealOf a) a -> GESVD a
GESVD GESVD_ (RealOf (Complex Float)) (Complex Float)
forall a. Real a => GESVD_ a (Complex a)
gesvdComplex)
      (GESVD_ (RealOf (Complex Double)) (Complex Double)
-> GESVD (Complex Double)
forall a. GESVD_ (RealOf a) a -> GESVD a
GESVD GESVD_ (RealOf (Complex Double)) (Complex Double)
forall a. Real a => GESVD_ a (Complex a)
gesvdComplex)

gesvdReal :: (Class.Real a) => GESVD_ a a
gesvdReal :: GESVD_ a a
gesvdReal Ptr CChar
jobuPtr Ptr CChar
jobvtPtr Ptr CInt
mPtr Ptr CInt
nPtr
      Ptr a
aPtr Ptr CInt
ldaPtr Ptr a
sPtr Ptr a
uPtr Ptr CInt
lduPtr Ptr a
vtPtr Ptr CInt
ldvtPtr Int
_mn Ptr CInt
infoPtr =
   (Ptr a -> Ptr CInt -> IO ()) -> IO ()
forall a. Floating a => (Ptr a -> Ptr CInt -> IO ()) -> IO ()
withAutoWorkspace ((Ptr a -> Ptr CInt -> IO ()) -> IO ())
-> (Ptr a -> Ptr CInt -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr a
workPtr Ptr CInt
lworkPtr ->
   Ptr CChar
-> Ptr CChar
-> Ptr CInt
-> Ptr CInt
-> Ptr a
-> Ptr CInt
-> Ptr a
-> Ptr a
-> Ptr CInt
-> Ptr a
-> Ptr CInt
-> Ptr a
-> Ptr CInt
-> Ptr CInt
-> IO ()
forall a.
Real a =>
Ptr CChar
-> Ptr CChar
-> Ptr CInt
-> Ptr CInt
-> Ptr a
-> Ptr CInt
-> Ptr a
-> Ptr a
-> Ptr CInt
-> Ptr a
-> Ptr CInt
-> Ptr a
-> Ptr CInt
-> Ptr CInt
-> IO ()
LapackReal.gesvd Ptr CChar
jobuPtr Ptr CChar
jobvtPtr
      Ptr CInt
mPtr Ptr CInt
nPtr Ptr a
aPtr Ptr CInt
ldaPtr Ptr a
sPtr Ptr a
uPtr Ptr CInt
lduPtr Ptr a
vtPtr Ptr CInt
ldvtPtr
      Ptr a
workPtr Ptr CInt
lworkPtr Ptr CInt
infoPtr

gesvdComplex :: (Class.Real a) => GESVD_ a (Complex a)
gesvdComplex :: GESVD_ a (Complex a)
gesvdComplex Ptr CChar
jobuPtr Ptr CChar
jobvtPtr
      Ptr CInt
mPtr Ptr CInt
nPtr Ptr (Complex a)
aPtr Ptr CInt
ldaPtr Ptr a
sPtr Ptr (Complex a)
uPtr Ptr CInt
lduPtr Ptr (Complex a)
vtPtr Ptr CInt
ldvtPtr Int
mn Ptr CInt
infoPtr =
   Int -> (Ptr a -> IO ()) -> IO ()
forall a b. Storable a => Int -> (Ptr a -> IO b) -> IO b
ForeignArray.alloca (Int
5Int -> Int -> Int
forall a. Num a => a -> a -> a
*Int
mn) ((Ptr a -> IO ()) -> IO ()) -> (Ptr a -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr a
rworkPtr ->
   (Ptr (Complex a) -> Ptr CInt -> IO ()) -> IO ()
forall a. Floating a => (Ptr a -> Ptr CInt -> IO ()) -> IO ()
withAutoWorkspace ((Ptr (Complex a) -> Ptr CInt -> IO ()) -> IO ())
-> (Ptr (Complex a) -> Ptr CInt -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr (Complex a)
workPtr Ptr CInt
lworkPtr ->
   Ptr CChar
-> Ptr CChar
-> Ptr CInt
-> Ptr CInt
-> Ptr (Complex a)
-> Ptr CInt
-> Ptr a
-> Ptr (Complex a)
-> Ptr CInt
-> Ptr (Complex a)
-> Ptr CInt
-> Ptr (Complex a)
-> Ptr CInt
-> Ptr a
-> Ptr CInt
-> IO ()
forall a.
Real a =>
Ptr CChar
-> Ptr CChar
-> Ptr CInt
-> Ptr CInt
-> Ptr (Complex a)
-> Ptr CInt
-> Ptr a
-> Ptr (Complex a)
-> Ptr CInt
-> Ptr (Complex a)
-> Ptr CInt
-> Ptr (Complex a)
-> Ptr CInt
-> Ptr a
-> Ptr CInt
-> IO ()
LapackComplex.gesvd Ptr CChar
jobuPtr Ptr CChar
jobvtPtr
      Ptr CInt
mPtr Ptr CInt
nPtr Ptr (Complex a)
aPtr Ptr CInt
ldaPtr Ptr a
sPtr Ptr (Complex a)
uPtr Ptr CInt
lduPtr Ptr (Complex a)
vtPtr Ptr CInt
ldvtPtr
      Ptr (Complex a)
workPtr Ptr CInt
lworkPtr Ptr a
rworkPtr Ptr CInt
infoPtr


leastSquaresMinimumNormRCond ::
   (Extent.Measure meas, Extent.C vert, Extent.C horiz,
    Shape.C height, Eq height, Shape.C width, Shape.C nrhs, Class.Floating a) =>
   RealOf a ->
   Full meas horiz vert height width a ->
   Full meas vert horiz height nrhs a ->
   (Int, Full meas vert horiz width nrhs a)
leastSquaresMinimumNormRCond :: RealOf a
-> Full meas horiz vert height width a
-> Full meas vert horiz height nrhs a
-> (Int, Full meas vert horiz width nrhs a)
leastSquaresMinimumNormRCond RealOf a
rcond
      (Array (Layout.Full Order
orderA Extent meas horiz vert height width
extentA) ForeignPtr a
a)
      (Array (Layout.Full Order
orderB Extent meas vert horiz height nrhs
extentB) ForeignPtr a
b) =
   case Extent meas vert horiz width height
-> Extent meas vert horiz height nrhs
-> Maybe (Extent meas vert horiz width nrhs)
forall meas vert horiz fuse height width.
(Measure meas, C vert, C horiz, Eq fuse) =>
Extent meas vert horiz height fuse
-> Extent meas vert horiz fuse width
-> Maybe (Extent meas vert horiz height width)
Extent.fuse (Extent meas horiz vert height width
-> Extent meas vert horiz width height
forall meas vert horiz height width.
(Measure meas, C vert, C horiz) =>
Extent meas vert horiz height width
-> Extent meas horiz vert width height
Extent.transpose Extent meas horiz vert height width
extentA) Extent meas vert horiz height nrhs
extentB of
      Maybe (Extent meas vert horiz width nrhs)
Nothing -> String -> (Int, Full meas vert horiz width nrhs a)
forall a. HasCallStack => String -> a
error String
"leastSquaresMinimumNorm: height shapes mismatch"
      Just Extent meas vert horiz width nrhs
extent ->
         let widthA :: width
widthA = Extent meas horiz vert height width -> width
forall meas vert horiz height width.
(Measure meas, C vert, C horiz) =>
Extent meas vert horiz height width -> width
Extent.width Extent meas horiz vert height width
extentA
             (height
height,nrhs
widthB) = Extent meas vert horiz height nrhs -> (height, nrhs)
forall meas vert horiz height width.
(Measure meas, C vert, C horiz) =>
Extent meas vert horiz height width -> (height, width)
Extent.dimensions Extent meas vert horiz height nrhs
extentB
             shapeX :: Full meas vert horiz width nrhs
shapeX = Order
-> Extent meas vert horiz width nrhs
-> Full meas vert horiz width nrhs
forall meas vert horiz height width.
Order
-> Extent meas vert horiz height width
-> Full meas vert horiz height width
Layout.Full Order
ColumnMajor Extent meas vert horiz width nrhs
extent
             m :: Int
m = height -> Int
forall sh. C sh => sh -> Int
Shape.size height
height
             n :: Int
n = width -> Int
forall sh. C sh => sh -> Int
Shape.size width
widthA
             nrhs :: Int
nrhs = nrhs -> Int
forall sh. C sh => sh -> Int
Shape.size nrhs
widthB
         in if Int
m Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
               then (Int
0, Full meas vert horiz width nrhs
-> Full meas vert horiz width nrhs a
forall sh a. (C sh, Floating a) => sh -> Vector sh a
Vector.zero Full meas vert horiz width nrhs
shapeX)
               else
                  if Int
nrhs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
                     then
                        ((Int, Array (General width ()) a) -> Int
forall a b. (a, b) -> a
fst ((Int, Array (General width ()) a) -> Int)
-> (Int, Array (General width ()) a) -> Int
forall a b. (a -> b) -> a -> b
$ IO (Int, Array (General width ()) a)
-> (Int, Array (General width ()) a)
forall a. IO a -> a
unsafePerformIO (IO (Int, Array (General width ()) a)
 -> (Int, Array (General width ()) a))
-> IO (Int, Array (General width ()) a)
-> (Int, Array (General width ()) a)
forall a b. (a -> b) -> a -> b
$
                         case height -> Vector height a
forall sh a. (C sh, Floating a) => sh -> Vector sh a
Vector.zero height
height of
                           Array height
_ ForeignPtr a
b1 ->
                              RealOf a
-> General width ()
-> Order
-> ForeignPtr a
-> Order
-> ForeignPtr a
-> Int
-> Int
-> Int
-> IO (Int, Array (General width ()) a)
forall sh a.
(C sh, Floating a) =>
RealOf a
-> sh
-> Order
-> ForeignPtr a
-> Order
-> ForeignPtr a
-> Int
-> Int
-> Int
-> IO (Int, Array sh a)
leastSquaresMinimumNormIO RealOf a
rcond
                                 (Order -> width -> () -> General width ()
forall height width.
Order -> height -> width -> General height width
Layout.general Order
ColumnMajor width
widthA ())
                                 Order
orderA ForeignPtr a
a Order
orderB ForeignPtr a
b1 Int
m Int
n Int
1,
                         Full meas vert horiz width nrhs
-> Full meas vert horiz width nrhs a
forall sh a. (C sh, Floating a) => sh -> Vector sh a
Vector.zero Full meas vert horiz width nrhs
shapeX)
                     else
                        IO (Int, Full meas vert horiz width nrhs a)
-> (Int, Full meas vert horiz width nrhs a)
forall a. IO a -> a
unsafePerformIO (IO (Int, Full meas vert horiz width nrhs a)
 -> (Int, Full meas vert horiz width nrhs a))
-> IO (Int, Full meas vert horiz width nrhs a)
-> (Int, Full meas vert horiz width nrhs a)
forall a b. (a -> b) -> a -> b
$
                        RealOf a
-> Full meas vert horiz width nrhs
-> Order
-> ForeignPtr a
-> Order
-> ForeignPtr a
-> Int
-> Int
-> Int
-> IO (Int, Full meas vert horiz width nrhs a)
forall sh a.
(C sh, Floating a) =>
RealOf a
-> sh
-> Order
-> ForeignPtr a
-> Order
-> ForeignPtr a
-> Int
-> Int
-> Int
-> IO (Int, Array sh a)
leastSquaresMinimumNormIO RealOf a
rcond Full meas vert horiz width nrhs
shapeX
                           Order
orderA ForeignPtr a
a Order
orderB ForeignPtr a
b Int
m Int
n Int
nrhs

leastSquaresMinimumNormIO ::
   (Shape.C sh, Class.Floating a) =>
   RealOf a -> sh ->
   Order -> ForeignPtr a ->
   Order -> ForeignPtr a ->
   Int -> Int -> Int -> IO (Int, Array sh a)
leastSquaresMinimumNormIO :: RealOf a
-> sh
-> Order
-> ForeignPtr a
-> Order
-> ForeignPtr a
-> Int
-> Int
-> Int
-> IO (Int, Array sh a)
leastSquaresMinimumNormIO RealOf a
rcond sh
shapeX Order
orderA ForeignPtr a
a Order
orderB ForeignPtr a
b Int
m Int
n Int
nrhs =
   sh
-> Int
-> Int
-> Int
-> ((Ptr a, Int) -> IO Int)
-> IO (Int, Array sh a)
forall sh a rank.
(C sh, Floating a) =>
sh
-> Int
-> Int
-> Int
-> ((Ptr a, Int) -> IO rank)
-> IO (rank, Array sh a)
createHigherArray sh
shapeX Int
m Int
n Int
nrhs (((Ptr a, Int) -> IO Int) -> IO (Int, Array sh a))
-> ((Ptr a, Int) -> IO Int) -> IO (Int, Array sh a)
forall a b. (a -> b) -> a -> b
$ \(Ptr a
tmpPtr,Int
ldtmp) -> do

   let mn :: Int
mn = Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
m Int
n
   let lda :: Int
lda = Int
m
   ContT Int IO Int -> IO Int
forall (m :: * -> *) r. Monad m => ContT r m r -> m r
evalContT (ContT Int IO Int -> IO Int) -> ContT Int IO Int -> IO Int
forall a b. (a -> b) -> a -> b
$ do
      Ptr CInt
mPtr <- Int -> FortranIO Int (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint Int
m
      Ptr CInt
nPtr <- Int -> FortranIO Int (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint Int
n
      Ptr CInt
nrhsPtr <- Int -> FortranIO Int (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint Int
nrhs
      Ptr a
aPtr <- Order -> Int -> Int -> ForeignPtr a -> ContT Int IO (Ptr a)
forall a r.
Floating a =>
Order -> Int -> Int -> ForeignPtr a -> ContT r IO (Ptr a)
copyToColumnMajorTemp Order
orderA Int
m Int
n ForeignPtr a
a
      Ptr CInt
ldaPtr <- Int -> FortranIO Int (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.leadingDim Int
lda
      Ptr CInt
ldtmpPtr <- Int -> FortranIO Int (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.leadingDim Int
ldtmp
      IO () -> ContT Int IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ContT Int IO ()) -> IO () -> ContT Int IO ()
forall a b. (a -> b) -> a -> b
$ ForeignPtr a -> (Ptr a -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr a
b ((Ptr a -> IO ()) -> IO ()) -> (Ptr a -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr a
bPtr ->
         Order -> Int -> Int -> Ptr a -> Int -> Ptr a -> IO ()
forall a.
Floating a =>
Order -> Int -> Int -> Ptr a -> Int -> Ptr a -> IO ()
copyToSubColumnMajor Order
orderB Int
m Int
nrhs Ptr a
bPtr Int
ldtmp Ptr a
tmpPtr

      Ptr CInt
rankPtr <- FortranIO Int (Ptr CInt)
forall a r. Storable a => FortranIO r (Ptr a)
Call.alloca
      IO () -> ContT Int IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ContT Int IO ()) -> IO () -> ContT Int IO ()
forall a b. (a -> b) -> a -> b
$
         String -> (Ptr CInt -> IO ()) -> IO ()
withInfo String
"gelss" ((Ptr CInt -> IO ()) -> IO ()) -> (Ptr CInt -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr CInt
infoPtr ->
         GELSS_ (RealOf a) a
forall a. Floating a => GELSS_ (RealOf a) a
gelss Ptr CInt
mPtr Ptr CInt
nPtr Ptr CInt
nrhsPtr Ptr a
aPtr Ptr CInt
ldaPtr Ptr a
tmpPtr Ptr CInt
ldtmpPtr RealOf a
rcond
            Ptr CInt
rankPtr Int
mn Ptr CInt
infoPtr

      IO Int -> ContT Int IO Int
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Int -> ContT Int IO Int) -> IO Int -> ContT Int IO Int
forall a b. (a -> b) -> a -> b
$ Ptr CInt -> IO Int
peekCInt Ptr CInt
rankPtr


type GELSS_ ar a =
   Ptr CInt -> Ptr CInt -> Ptr CInt ->
   Ptr a -> Ptr CInt -> Ptr a -> Ptr CInt ->
   ar -> Ptr CInt -> Int -> Ptr CInt -> IO ()

newtype GELSS a = GELSS {GELSS a -> GELSS_ (RealOf a) a
getGELSS :: GELSS_ (RealOf a) a}

gelss :: Class.Floating a => GELSS_ (RealOf a) a
gelss :: GELSS_ (RealOf a) a
gelss =
   GELSS a -> GELSS_ (RealOf a) a
forall a. GELSS a -> GELSS_ (RealOf a) a
getGELSS (GELSS a -> GELSS_ (RealOf a) a) -> GELSS a -> GELSS_ (RealOf a) a
forall a b. (a -> b) -> a -> b
$
   GELSS Float
-> GELSS Double
-> GELSS (Complex Float)
-> GELSS (Complex Double)
-> GELSS a
forall a (f :: * -> *).
Floating a =>
f Float
-> f Double -> f (Complex Float) -> f (Complex Double) -> f a
Class.switchFloating
      (GELSS_ (RealOf Float) Float -> GELSS Float
forall a. GELSS_ (RealOf a) a -> GELSS a
GELSS GELSS_ (RealOf Float) Float
forall a. Real a => GELSS_ a a
gelssReal)
      (GELSS_ (RealOf Double) Double -> GELSS Double
forall a. GELSS_ (RealOf a) a -> GELSS a
GELSS GELSS_ (RealOf Double) Double
forall a. Real a => GELSS_ a a
gelssReal)
      (GELSS_ (RealOf (Complex Float)) (Complex Float)
-> GELSS (Complex Float)
forall a. GELSS_ (RealOf a) a -> GELSS a
GELSS GELSS_ (RealOf (Complex Float)) (Complex Float)
forall a. Real a => GELSS_ a (Complex a)
gelssComplex)
      (GELSS_ (RealOf (Complex Double)) (Complex Double)
-> GELSS (Complex Double)
forall a. GELSS_ (RealOf a) a -> GELSS a
GELSS GELSS_ (RealOf (Complex Double)) (Complex Double)
forall a. Real a => GELSS_ a (Complex a)
gelssComplex)

gelssReal :: (Class.Real a) => GELSS_ a a
gelssReal :: GELSS_ a a
gelssReal Ptr CInt
mPtr Ptr CInt
nPtr Ptr CInt
nrhsPtr Ptr a
aPtr Ptr CInt
ldaPtr Ptr a
bPtr Ptr CInt
ldbPtr a
rcond
      Ptr CInt
rankPtr Int
mn Ptr CInt
infoPtr =
   a -> (Ptr a -> IO ()) -> IO ()
forall a b. Storable a => a -> (Ptr a -> IO b) -> IO b
Marshal.with a
rcond ((Ptr a -> IO ()) -> IO ()) -> (Ptr a -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr a
rcondPtr ->
   Int -> (Ptr a -> IO ()) -> IO ()
forall a b. Storable a => Int -> (Ptr a -> IO b) -> IO b
ForeignArray.alloca Int
mn ((Ptr a -> IO ()) -> IO ()) -> (Ptr a -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr a
sPtr ->
   (Ptr a -> Ptr CInt -> IO ()) -> IO ()
forall a. Floating a => (Ptr a -> Ptr CInt -> IO ()) -> IO ()
withAutoWorkspace ((Ptr a -> Ptr CInt -> IO ()) -> IO ())
-> (Ptr a -> Ptr CInt -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr a
workPtr Ptr CInt
lworkPtr ->
   Ptr CInt
-> Ptr CInt
-> Ptr CInt
-> Ptr a
-> Ptr CInt
-> Ptr a
-> Ptr CInt
-> Ptr a
-> Ptr a
-> Ptr CInt
-> Ptr a
-> Ptr CInt
-> Ptr CInt
-> IO ()
forall a.
Real a =>
Ptr CInt
-> Ptr CInt
-> Ptr CInt
-> Ptr a
-> Ptr CInt
-> Ptr a
-> Ptr CInt
-> Ptr a
-> Ptr a
-> Ptr CInt
-> Ptr a
-> Ptr CInt
-> Ptr CInt
-> IO ()
LapackReal.gelss
      Ptr CInt
mPtr Ptr CInt
nPtr Ptr CInt
nrhsPtr Ptr a
aPtr Ptr CInt
ldaPtr Ptr a
bPtr Ptr CInt
ldbPtr Ptr a
sPtr Ptr a
rcondPtr
      Ptr CInt
rankPtr Ptr a
workPtr Ptr CInt
lworkPtr Ptr CInt
infoPtr

gelssComplex :: (Class.Real a) => GELSS_ a (Complex a)
gelssComplex :: GELSS_ a (Complex a)
gelssComplex Ptr CInt
mPtr Ptr CInt
nPtr Ptr CInt
nrhsPtr Ptr (Complex a)
aPtr Ptr CInt
ldaPtr Ptr (Complex a)
bPtr Ptr CInt
ldbPtr a
rcond
      Ptr CInt
rankPtr Int
mn Ptr CInt
infoPtr =
   a -> (Ptr a -> IO ()) -> IO ()
forall a b. Storable a => a -> (Ptr a -> IO b) -> IO b
Marshal.with a
rcond ((Ptr a -> IO ()) -> IO ()) -> (Ptr a -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr a
rcondPtr ->
   Int -> (Ptr a -> IO ()) -> IO ()
forall a b. Storable a => Int -> (Ptr a -> IO b) -> IO b
ForeignArray.alloca Int
mn ((Ptr a -> IO ()) -> IO ()) -> (Ptr a -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr a
sPtr ->
   Int -> (Ptr a -> IO ()) -> IO ()
forall a b. Storable a => Int -> (Ptr a -> IO b) -> IO b
ForeignArray.alloca (Int
5Int -> Int -> Int
forall a. Num a => a -> a -> a
*Int
mn) ((Ptr a -> IO ()) -> IO ()) -> (Ptr a -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr a
rworkPtr ->
   (Ptr (Complex a) -> Ptr CInt -> IO ()) -> IO ()
forall a. Floating a => (Ptr a -> Ptr CInt -> IO ()) -> IO ()
withAutoWorkspace ((Ptr (Complex a) -> Ptr CInt -> IO ()) -> IO ())
-> (Ptr (Complex a) -> Ptr CInt -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr (Complex a)
workPtr Ptr CInt
lworkPtr ->
   Ptr CInt
-> Ptr CInt
-> Ptr CInt
-> Ptr (Complex a)
-> Ptr CInt
-> Ptr (Complex a)
-> Ptr CInt
-> Ptr a
-> Ptr a
-> Ptr CInt
-> Ptr (Complex a)
-> Ptr CInt
-> Ptr a
-> Ptr CInt
-> IO ()
forall a.
Real a =>
Ptr CInt
-> Ptr CInt
-> Ptr CInt
-> Ptr (Complex a)
-> Ptr CInt
-> Ptr (Complex a)
-> Ptr CInt
-> Ptr a
-> Ptr a
-> Ptr CInt
-> Ptr (Complex a)
-> Ptr CInt
-> Ptr a
-> Ptr CInt
-> IO ()
LapackComplex.gelss
      Ptr CInt
mPtr Ptr CInt
nPtr Ptr CInt
nrhsPtr Ptr (Complex a)
aPtr Ptr CInt
ldaPtr Ptr (Complex a)
bPtr Ptr CInt
ldbPtr Ptr a
sPtr Ptr a
rcondPtr
      Ptr CInt
rankPtr Ptr (Complex a)
workPtr Ptr CInt
lworkPtr Ptr a
rworkPtr Ptr CInt
infoPtr


pseudoInverseRCond ::
   (Extent.Measure meas, Extent.C vert, Extent.C horiz,
    Shape.C height, Eq height, Shape.C width, Eq width, Class.Floating a) =>
   RealOf a ->
   Full meas vert horiz height width a ->
   (Int, Full meas horiz vert width height a)
pseudoInverseRCond :: RealOf a
-> Full meas vert horiz height width a
-> (Int, Full meas horiz vert width height a)
pseudoInverseRCond =
   PseudoInverseRCond
  (Array (Full meas vert horiz height width))
  (Array (Full meas horiz vert width height))
  a
-> RealOf a
-> Full meas vert horiz height width a
-> (Int, Full meas horiz vert width height a)
forall (f :: * -> *) (g :: * -> *) a.
PseudoInverseRCond f g a -> RealOf a -> f a -> (Int, g a)
getPseudoInverseRCond (PseudoInverseRCond
   (Array (Full meas vert horiz height width))
   (Array (Full meas horiz vert width height))
   a
 -> RealOf a
 -> Full meas vert horiz height width a
 -> (Int, Full meas horiz vert width height a))
-> PseudoInverseRCond
     (Array (Full meas vert horiz height width))
     (Array (Full meas horiz vert width height))
     a
-> RealOf a
-> Full meas vert horiz height width a
-> (Int, Full meas horiz vert width height a)
forall a b. (a -> b) -> a -> b
$
   PseudoInverseRCond
  (Array (Full meas vert horiz height width))
  (Array (Full meas horiz vert width height))
  Float
-> PseudoInverseRCond
     (Array (Full meas vert horiz height width))
     (Array (Full meas horiz vert width height))
     Double
-> PseudoInverseRCond
     (Array (Full meas vert horiz height width))
     (Array (Full meas horiz vert width height))
     (Complex Float)
-> PseudoInverseRCond
     (Array (Full meas vert horiz height width))
     (Array (Full meas horiz vert width height))
     (Complex Double)
-> PseudoInverseRCond
     (Array (Full meas vert horiz height width))
     (Array (Full meas horiz vert width height))
     a
forall a (f :: * -> *).
Floating a =>
f Float
-> f Double -> f (Complex Float) -> f (Complex Double) -> f a
Class.switchFloating
      ((RealOf Float
 -> Array (Full meas vert horiz height width) Float
 -> (Int, Array (Full meas horiz vert width height) Float))
-> PseudoInverseRCond
     (Array (Full meas vert horiz height width))
     (Array (Full meas horiz vert width height))
     Float
forall (f :: * -> *) (g :: * -> *) a.
(RealOf a -> f a -> (Int, g a)) -> PseudoInverseRCond f g a
PseudoInverseRCond RealOf Float
-> Array (Full meas vert horiz height width) Float
-> (Int, Array (Full meas horiz vert width height) Float)
forall meas vert horiz height width a ar.
(Measure meas, C vert, C horiz, C height, Eq height, C width,
 Eq width, Floating a, RealOf a ~ ar, Real ar) =>
ar
-> Full meas vert horiz height width a
-> (Int, Full meas horiz vert width height a)
pseudoInverseRCondAux)
      ((RealOf Double
 -> Array (Full meas vert horiz height width) Double
 -> (Int, Array (Full meas horiz vert width height) Double))
-> PseudoInverseRCond
     (Array (Full meas vert horiz height width))
     (Array (Full meas horiz vert width height))
     Double
forall (f :: * -> *) (g :: * -> *) a.
(RealOf a -> f a -> (Int, g a)) -> PseudoInverseRCond f g a
PseudoInverseRCond RealOf Double
-> Array (Full meas vert horiz height width) Double
-> (Int, Array (Full meas horiz vert width height) Double)
forall meas vert horiz height width a ar.
(Measure meas, C vert, C horiz, C height, Eq height, C width,
 Eq width, Floating a, RealOf a ~ ar, Real ar) =>
ar
-> Full meas vert horiz height width a
-> (Int, Full meas horiz vert width height a)
pseudoInverseRCondAux)
      ((RealOf (Complex Float)
 -> Array (Full meas vert horiz height width) (Complex Float)
 -> (Int,
     Array (Full meas horiz vert width height) (Complex Float)))
-> PseudoInverseRCond
     (Array (Full meas vert horiz height width))
     (Array (Full meas horiz vert width height))
     (Complex Float)
forall (f :: * -> *) (g :: * -> *) a.
(RealOf a -> f a -> (Int, g a)) -> PseudoInverseRCond f g a
PseudoInverseRCond RealOf (Complex Float)
-> Array (Full meas vert horiz height width) (Complex Float)
-> (Int, Array (Full meas horiz vert width height) (Complex Float))
forall meas vert horiz height width a ar.
(Measure meas, C vert, C horiz, C height, Eq height, C width,
 Eq width, Floating a, RealOf a ~ ar, Real ar) =>
ar
-> Full meas vert horiz height width a
-> (Int, Full meas horiz vert width height a)
pseudoInverseRCondAux)
      ((RealOf (Complex Double)
 -> Array (Full meas vert horiz height width) (Complex Double)
 -> (Int,
     Array (Full meas horiz vert width height) (Complex Double)))
-> PseudoInverseRCond
     (Array (Full meas vert horiz height width))
     (Array (Full meas horiz vert width height))
     (Complex Double)
forall (f :: * -> *) (g :: * -> *) a.
(RealOf a -> f a -> (Int, g a)) -> PseudoInverseRCond f g a
PseudoInverseRCond RealOf (Complex Double)
-> Array (Full meas vert horiz height width) (Complex Double)
-> (Int,
    Array (Full meas horiz vert width height) (Complex Double))
forall meas vert horiz height width a ar.
(Measure meas, C vert, C horiz, C height, Eq height, C width,
 Eq width, Floating a, RealOf a ~ ar, Real ar) =>
ar
-> Full meas vert horiz height width a
-> (Int, Full meas horiz vert width height a)
pseudoInverseRCondAux)

newtype PseudoInverseRCond f g a =
   PseudoInverseRCond {
      PseudoInverseRCond f g a -> RealOf a -> f a -> (Int, g a)
getPseudoInverseRCond :: RealOf a -> f a -> (Int, g a)
   }

pseudoInverseRCondAux ::
   (Extent.Measure meas, Extent.C vert, Extent.C horiz,
    Shape.C height, Eq height, Shape.C width, Eq width,
    Class.Floating a, RealOf a ~ ar, Class.Real ar) =>
   ar ->
   Full meas vert horiz height width a ->
   (Int, Full meas horiz vert width height a)
pseudoInverseRCondAux :: ar
-> Full meas vert horiz height width a
-> (Int, Full meas horiz vert width height a)
pseudoInverseRCondAux ar
rcond =
   PseudoInverseExtent height width a meas vert horiz
-> Full meas vert horiz height width a
-> (Int, Full meas horiz vert width height a)
forall height width a meas vert horiz.
PseudoInverseExtent height width a meas vert horiz
-> Full meas vert horiz height width a
-> (Int, Full meas horiz vert width height a)
getPseudoInverseExtent (PseudoInverseExtent height width a meas vert horiz
 -> Full meas vert horiz height width a
 -> (Int, Full meas horiz vert width height a))
-> PseudoInverseExtent height width a meas vert horiz
-> Full meas vert horiz height width a
-> (Int, Full meas horiz vert width height a)
forall a b. (a -> b) -> a -> b
$
   PseudoInverseExtent height width a Shape Small Small
-> PseudoInverseExtent height width a Size Small Small
-> PseudoInverseExtent height width a Size Small Big
-> PseudoInverseExtent height width a Size Big Small
-> PseudoInverseExtent height width a Size Big Big
-> PseudoInverseExtent height width a meas vert horiz
forall meas vert horiz (f :: * -> * -> * -> *).
(Measure meas, C vert, C horiz) =>
f Shape Small Small
-> f Size Small Small
-> f Size Small Big
-> f Size Big Small
-> f Size Big Big
-> f meas vert horiz
Extent.switchTagTriple
      ((Full Shape Small Small height width a
 -> (Int, Full Shape Small Small width height a))
-> PseudoInverseExtent height width a Shape Small Small
forall height width a meas vert horiz.
(Full meas vert horiz height width a
 -> (Int, Full meas horiz vert width height a))
-> PseudoInverseExtent height width a meas vert horiz
PseudoInverseExtent ((Full Shape Small Small height width a
  -> (Int, Full Shape Small Small width height a))
 -> PseudoInverseExtent height width a Shape Small Small)
-> (Full Shape Small Small height width a
    -> (Int, Full Shape Small Small width height a))
-> PseudoInverseExtent height width a Shape Small Small
forall a b. (a -> b) -> a -> b
$ RealOf a
-> Full Shape Small Small height width a
-> (Int, Full Shape Small Small width height a)
forall meas horiz height width a ar.
(Measure meas, C horiz, C height, Eq height, C width, Eq width,
 Floating a, RealOf a ~ ar, Real ar) =>
RealOf a
-> Full meas Small horiz height width a
-> (Int, Full meas horiz Small width height a)
pseudoInverseRCondWide ar
RealOf a
rcond)
      ((Full Size Small Small height width a
 -> (Int, Full Size Small Small width height a))
-> PseudoInverseExtent height width a Size Small Small
forall height width a meas vert horiz.
(Full meas vert horiz height width a
 -> (Int, Full meas horiz vert width height a))
-> PseudoInverseExtent height width a meas vert horiz
PseudoInverseExtent ((Full Size Small Small height width a
  -> (Int, Full Size Small Small width height a))
 -> PseudoInverseExtent height width a Size Small Small)
-> (Full Size Small Small height width a
    -> (Int, Full Size Small Small width height a))
-> PseudoInverseExtent height width a Size Small Small
forall a b. (a -> b) -> a -> b
$ RealOf a
-> Full Size Small Small height width a
-> (Int, Full Size Small Small width height a)
forall meas horiz height width a ar.
(Measure meas, C horiz, C height, Eq height, C width, Eq width,
 Floating a, RealOf a ~ ar, Real ar) =>
RealOf a
-> Full meas Small horiz height width a
-> (Int, Full meas horiz Small width height a)
pseudoInverseRCondWide ar
RealOf a
rcond)
      ((Full Size Small Big height width a
 -> (Int, Full Size Big Small width height a))
-> PseudoInverseExtent height width a Size Small Big
forall height width a meas vert horiz.
(Full meas vert horiz height width a
 -> (Int, Full meas horiz vert width height a))
-> PseudoInverseExtent height width a meas vert horiz
PseudoInverseExtent ((Full Size Small Big height width a
  -> (Int, Full Size Big Small width height a))
 -> PseudoInverseExtent height width a Size Small Big)
-> (Full Size Small Big height width a
    -> (Int, Full Size Big Small width height a))
-> PseudoInverseExtent height width a Size Small Big
forall a b. (a -> b) -> a -> b
$ RealOf a
-> Full Size Small Big height width a
-> (Int, Full Size Big Small width height a)
forall meas horiz height width a ar.
(Measure meas, C horiz, C height, Eq height, C width, Eq width,
 Floating a, RealOf a ~ ar, Real ar) =>
RealOf a
-> Full meas Small horiz height width a
-> (Int, Full meas horiz Small width height a)
pseudoInverseRCondWide ar
RealOf a
rcond)
      ((Full Size Big Small height width a
 -> (Int, Full Size Small Big width height a))
-> PseudoInverseExtent height width a Size Big Small
forall height width a meas vert horiz.
(Full meas vert horiz height width a
 -> (Int, Full meas horiz vert width height a))
-> PseudoInverseExtent height width a meas vert horiz
PseudoInverseExtent ((Full Size Big Small height width a
  -> (Int, Full Size Small Big width height a))
 -> PseudoInverseExtent height width a Size Big Small)
-> (Full Size Big Small height width a
    -> (Int, Full Size Small Big width height a))
-> PseudoInverseExtent height width a Size Big Small
forall a b. (a -> b) -> a -> b
$ RealOf a
-> Full Size Big Small height width a
-> (Int, Full Size Small Big width height a)
forall meas vert height width a ar.
(Measure meas, C vert, C height, Eq height, C width, Eq width,
 Floating a, RealOf a ~ ar, Real ar) =>
RealOf a
-> Full meas vert Small height width a
-> (Int, Full meas Small vert width height a)
pseudoInverseRCondTall ar
RealOf a
rcond)
      ((Full Size Big Big height width a
 -> (Int, Full Size Big Big width height a))
-> PseudoInverseExtent height width a Size Big Big
forall height width a meas vert horiz.
(Full meas vert horiz height width a
 -> (Int, Full meas horiz vert width height a))
-> PseudoInverseExtent height width a meas vert horiz
PseudoInverseExtent ((Full Size Big Big height width a
  -> (Int, Full Size Big Big width height a))
 -> PseudoInverseExtent height width a Size Big Big)
-> (Full Size Big Big height width a
    -> (Int, Full Size Big Big width height a))
-> PseudoInverseExtent height width a Size Big Big
forall a b. (a -> b) -> a -> b
$
         (Full Size Big Small height width a
 -> (Int, Full Size Big Big width height a))
-> (Full Size Small Big height width a
    -> (Int, Full Size Big Big width height a))
-> Either
     (Full Size Big Small height width a)
     (Full Size Small Big height width a)
-> (Int, Full Size Big Big width height a)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either
            ((Full Size Small Big width height a
 -> Full Size Big Big width height a)
-> (Int, Full Size Small Big width height a)
-> (Int, Full Size Big Big width height a)
forall b c a. (b -> c) -> (a, b) -> (a, c)
mapSnd Full Size Small Big width height a
-> Full Size Big Big width height a
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz) =>
Full meas vert horiz height width a -> General height width a
Matrix.fromFull ((Int, Full Size Small Big width height a)
 -> (Int, Full Size Big Big width height a))
-> (Full Size Big Small height width a
    -> (Int, Full Size Small Big width height a))
-> Full Size Big Small height width a
-> (Int, Full Size Big Big width height a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RealOf a
-> Full Size Big Small height width a
-> (Int, Full Size Small Big width height a)
forall meas vert height width a ar.
(Measure meas, C vert, C height, Eq height, C width, Eq width,
 Floating a, RealOf a ~ ar, Real ar) =>
RealOf a
-> Full meas vert Small height width a
-> (Int, Full meas Small vert width height a)
pseudoInverseRCondTall ar
RealOf a
rcond)
            ((Full Size Big Small width height a
 -> Full Size Big Big width height a)
-> (Int, Full Size Big Small width height a)
-> (Int, Full Size Big Big width height a)
forall b c a. (b -> c) -> (a, b) -> (a, c)
mapSnd Full Size Big Small width height a
-> Full Size Big Big width height a
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz) =>
Full meas vert horiz height width a -> General height width a
Matrix.fromFull ((Int, Full Size Big Small width height a)
 -> (Int, Full Size Big Big width height a))
-> (Full Size Small Big height width a
    -> (Int, Full Size Big Small width height a))
-> Full Size Small Big height width a
-> (Int, Full Size Big Big width height a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RealOf a
-> Full Size Small Big height width a
-> (Int, Full Size Big Small width height a)
forall meas horiz height width a ar.
(Measure meas, C horiz, C height, Eq height, C width, Eq width,
 Floating a, RealOf a ~ ar, Real ar) =>
RealOf a
-> Full meas Small horiz height width a
-> (Int, Full meas horiz Small width height a)
pseudoInverseRCondWide ar
RealOf a
rcond)
         (Either
   (Full Size Big Small height width a)
   (Full Size Small Big height width a)
 -> (Int, Full Size Big Big width height a))
-> (Full Size Big Big height width a
    -> Either
         (Full Size Big Small height width a)
         (Full Size Small Big height width a))
-> Full Size Big Big height width a
-> (Int, Full Size Big Big width height a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
         Full Size Big Big height width a
-> Either
     (Full Size Big Small height width a)
     (Full Size Small Big height width a)
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz, C height, C width) =>
Full meas vert horiz height width a
-> Either (Tall height width a) (Wide height width a)
Basic.caseTallWide)

newtype PseudoInverseExtent height width a meas vert horiz =
   PseudoInverseExtent {
      PseudoInverseExtent height width a meas vert horiz
-> Full meas vert horiz height width a
-> (Int, Full meas horiz vert width height a)
getPseudoInverseExtent ::
         Full meas vert horiz height width a ->
         (Int, Full meas horiz vert width height a)
   }

pseudoInverseRCondWide ::
   (Extent.Measure meas, Extent.C horiz,
    Shape.C height, Eq height, Shape.C width, Eq width,
    Class.Floating a, RealOf a ~ ar, Class.Real ar) =>
   RealOf a ->
   Full meas Extent.Small horiz height width a ->
   (Int, Full meas horiz Extent.Small width height a)
pseudoInverseRCondWide :: RealOf a
-> Full meas Small horiz height width a
-> (Int, Full meas horiz Small width height a)
pseudoInverseRCondWide RealOf a
rcond Full meas Small horiz height width a
a =
   let (Square height a
u,Array height ar
s,Full meas Small horiz height width a
vt) = Full meas Small horiz height width a
-> (Square height a, RealVector height a,
    Full meas Small horiz height width a)
forall meas horiz height width a.
(Measure meas, C horiz, C height, C width, Floating a) =>
Full meas Small horiz height width a
-> (Square height a, RealVector height a,
    Full meas Small horiz height width a)
decomposeWide Full meas Small horiz height width a
a
       (Int
rank,Array height ar
recipS) = ar -> Array height ar -> (Int, Array height ar)
forall sh a. (C sh, Real a) => a -> Array sh a -> (Int, Array sh a)
recipSigma ar
RealOf a
rcond Array height ar
s
   in  (Int
rank,
        Full meas horiz Small width height a
-> Full meas horiz Small height height a
-> Full meas horiz Small width height a
forall meas vert horiz height fuse width a.
(Measure meas, C vert, C horiz, C height, C fuse, Eq fuse, C width,
 Floating a) =>
Full meas vert horiz height fuse a
-> Full meas vert horiz fuse width a
-> Full meas vert horiz height width a
Basic.multiply (Full meas Small horiz height width a
-> Full meas horiz Small width height a
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz, C height, C width, Floating a) =>
Full meas vert horiz height width a
-> Full meas horiz vert width height a
Basic.adjoint Full meas Small horiz height width a
vt) (Full meas horiz Small height height a
 -> Full meas horiz Small width height a)
-> Full meas horiz Small height height a
-> Full meas horiz Small width height a
forall a b. (a -> b) -> a -> b
$
        RealVector height a
-> Full meas horiz Small height height a
-> Full meas horiz Small height height a
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz, C height, Eq height, C width,
 Floating a) =>
Vector height (RealOf a)
-> Full meas vert horiz height width a
-> Full meas vert horiz height width a
Basic.scaleRowsReal Array height ar
RealVector height a
recipS (Full meas horiz Small height height a
 -> Full meas horiz Small height height a)
-> Full meas horiz Small height height a
-> Full meas horiz Small height height a
forall a b. (a -> b) -> a -> b
$ Square height a -> Full meas horiz Small height height a
forall meas vert horiz sh a.
(Measure meas, C vert, C horiz) =>
Square sh a -> Full meas vert horiz sh sh a
Square.toFull (Square height a -> Full meas horiz Small height height a)
-> Square height a -> Full meas horiz Small height height a
forall a b. (a -> b) -> a -> b
$ Square height a -> Square height a
forall sh a. (C sh, Floating a) => Square sh a -> Square sh a
Square.adjoint Square height a
u)

pseudoInverseRCondTall ::
   (Extent.Measure meas, Extent.C vert,
    Shape.C height, Eq height, Shape.C width, Eq width,
    Class.Floating a, RealOf a ~ ar, Class.Real ar) =>
   RealOf a ->
   Full meas vert Extent.Small height width a ->
   (Int, Full meas Extent.Small vert width height a)
pseudoInverseRCondTall :: RealOf a
-> Full meas vert Small height width a
-> (Int, Full meas Small vert width height a)
pseudoInverseRCondTall RealOf a
rcond Full meas vert Small height width a
a =
   let (Full meas vert Small height width a
u,Array width ar
s,Square width a
vt) = Full meas vert Small height width a
-> (Full meas vert Small height width a, RealVector width a,
    Square width a)
forall meas vert height width a.
(Measure meas, C vert, C height, C width, Floating a) =>
Full meas vert Small height width a
-> (Full meas vert Small height width a, RealVector width a,
    Square width a)
decomposeTall Full meas vert Small height width a
a
       (Int
rank,Array width ar
recipS) = ar -> Array width ar -> (Int, Array width ar)
forall sh a. (C sh, Real a) => a -> Array sh a -> (Int, Array sh a)
recipSigma ar
RealOf a
rcond Array width ar
s
   in  (Int
rank,
        Full meas Small vert width width a
-> Full meas Small vert width height a
-> Full meas Small vert width height a
forall meas vert horiz height fuse width a.
(Measure meas, C vert, C horiz, C height, C fuse, Eq fuse, C width,
 Floating a) =>
Full meas vert horiz height fuse a
-> Full meas vert horiz fuse width a
-> Full meas vert horiz height width a
Basic.multiply (Square width a -> Full meas Small vert width width a
forall meas vert horiz sh a.
(Measure meas, C vert, C horiz) =>
Square sh a -> Full meas vert horiz sh sh a
Square.toFull (Square width a -> Full meas Small vert width width a)
-> Square width a -> Full meas Small vert width width a
forall a b. (a -> b) -> a -> b
$ Square width a -> Square width a
forall sh a. (C sh, Floating a) => Square sh a -> Square sh a
Square.adjoint Square width a
vt) (Full meas Small vert width height a
 -> Full meas Small vert width height a)
-> Full meas Small vert width height a
-> Full meas Small vert width height a
forall a b. (a -> b) -> a -> b
$
        RealVector width a
-> Full meas Small vert width height a
-> Full meas Small vert width height a
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz, C height, Eq height, C width,
 Floating a) =>
Vector height (RealOf a)
-> Full meas vert horiz height width a
-> Full meas vert horiz height width a
Basic.scaleRowsReal Array width ar
RealVector width a
recipS (Full meas Small vert width height a
 -> Full meas Small vert width height a)
-> Full meas Small vert width height a
-> Full meas Small vert width height a
forall a b. (a -> b) -> a -> b
$ Full meas vert Small height width a
-> Full meas Small vert width height a
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz, C height, C width, Floating a) =>
Full meas vert horiz height width a
-> Full meas horiz vert width height a
Basic.adjoint Full meas vert Small height width a
u)


recipSigma ::
   (Shape.C sh, Class.Real a) => a -> Array sh a -> (Int, Array sh a)
recipSigma :: a -> Array sh a -> (Int, Array sh a)
recipSigma a
rcond Array sh a
sigmas =
   case Array sh a -> [a]
forall sh a. (C sh, Storable a) => Array sh a -> [a]
Array.toList Array sh a
sigmas of
      [] -> (Int
0, Array sh a
sigmas)
      a
0:[a]
_ -> (Int
0, Array sh a
sigmas)
      xs :: [a]
xs@(a
x:[a]
_) ->
         let smin :: a
smin = a
x a -> a -> a
forall a. Num a => a -> a -> a
* a
rcond
         in ([a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ((a -> Bool) -> [a] -> [a]
forall a. (a -> Bool) -> [a] -> [a]
takeWhile (a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>=a
smin) [a]
xs),
             (a -> a) -> Array sh a -> Array sh a
forall sh a b.
(C sh, Storable a, Storable b) =>
(a -> b) -> Array sh a -> Array sh b
Array.map (\a
s -> if a
sa -> a -> Bool
forall a. Ord a => a -> a -> Bool
>=a
smin then a -> a
forall a. Fractional a => a -> a
recip a
s else a
0) Array sh a
sigmas)


withInfo :: String -> (Ptr CInt -> IO ()) -> IO ()
withInfo :: String -> (Ptr CInt -> IO ()) -> IO ()
withInfo = String -> String -> (Ptr CInt -> IO ()) -> IO ()
Private.withInfo String
"%d superdiagonals did not converge"