{-# LANGUAGE GADTs #-}
module Numeric.LAPACK.Matrix.Plain.Class (
   check,
   -- for testing
   fromList,
   ) where

import qualified Numeric.LAPACK.Matrix.Shape as MatrixShape
import qualified Numeric.LAPACK.Matrix.Shape.Omni as Omni
import qualified Numeric.LAPACK.Matrix.Layout.Private as Layout
import qualified Numeric.LAPACK.Matrix.Extent.Private as ExtentPriv
import qualified Numeric.LAPACK.Matrix.Extent as Extent
import qualified Numeric.LAPACK.Matrix.BandedHermitian.Basic as BandedHermitian
import qualified Numeric.LAPACK.Matrix.Banded.Basic as Banded
import qualified Numeric.LAPACK.Matrix.Triangular.Basic as Triangular
import qualified Numeric.LAPACK.Matrix.Mosaic.Basic as Mosaic
import qualified Numeric.LAPACK.Matrix.Basic as Basic
import qualified Numeric.LAPACK.Matrix.Private as Matrix
import qualified Numeric.LAPACK.Vector as Vector
import qualified Numeric.LAPACK.Scalar as Scalar
import Numeric.LAPACK.Matrix.Shape.Omni (Omni)
import Numeric.LAPACK.Vector (Vector)
import Numeric.LAPACK.Wrapper (Flip(Flip, getFlip))

import qualified Numeric.Netlib.Class as Class

import qualified Type.Data.Num.Unary as Unary
import Type.Base.Proxy (Proxy(Proxy))

import Control.Applicative ((<|>))

import qualified Data.Array.Comfort.Storable.Unchecked as Array
import qualified Data.Array.Comfort.Storable as CheckedArray
import qualified Data.Array.Comfort.Shape as Shape
import Data.Array.Comfort.Storable.Unchecked (Array, (!))

import qualified Data.Complex as Complex
import Data.Functor.Compose (Compose(Compose, getCompose))
import Data.Tuple.HT (mapPair)
import Data.Maybe.HT (toMaybe)
import Data.Bool.HT (implies)



check ::
   (Omni.Plain pack property lower upper meas vert horiz height width ~ shape,
    Extent.Measure meas, Extent.C vert, Extent.C horiz) =>
   (Shape.C height, Shape.C width, Class.Floating a) =>
   Omni pack property lower upper meas vert horiz height width ->
   Array shape a -> Maybe String
check :: Omni pack property lower upper meas vert horiz height width
-> Array shape a -> Maybe String
check Omni pack property lower upper meas vert horiz height width
omni =
   case Omni pack property lower upper meas vert horiz height width
omni of
      Omni.Full Full meas vert horiz height width
shape -> \Array shape a
arr0 ->
         let arr :: Full Size Big Big (Deferred height) (Deferred width) a
arr =
               (height -> Deferred height)
-> Full Size Big Big height (Deferred width) a
-> Full Size Big Big (Deferred height) (Deferred width) a
forall vert horiz heightA heightB width a.
(C vert, C horiz) =>
(heightA -> heightB)
-> Full Size vert horiz heightA width a
-> Full Size vert horiz heightB width a
Basic.mapHeight height -> Deferred height
forall sh. sh -> Deferred sh
Shape.Deferred (Full Size Big Big height (Deferred width) a
 -> Full Size Big Big (Deferred height) (Deferred width) a)
-> Full Size Big Big height (Deferred width) a
-> Full Size Big Big (Deferred height) (Deferred width) a
forall a b. (a -> b) -> a -> b
$
               (width -> Deferred width)
-> Full Size Big Big height width a
-> Full Size Big Big height (Deferred width) a
forall vert horiz widthA widthB height a.
(C vert, C horiz) =>
(widthA -> widthB)
-> Full Size vert horiz height widthA a
-> Full Size vert horiz height widthB a
Basic.mapWidth width -> Deferred width
forall sh. sh -> Deferred sh
Shape.Deferred (Full Size Big Big height width a
 -> Full Size Big Big height (Deferred width) a)
-> Full Size Big Big height width a
-> Full Size Big Big height (Deferred width) a
forall a b. (a -> b) -> a -> b
$
               Full meas vert horiz height width a
-> Full Size Big Big 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
 -> Full Size Big Big height width a)
-> Full meas vert horiz height width a
-> Full Size Big Big height width a
forall a b. (a -> b) -> a -> b
$ Full meas vert horiz height width
-> Array shape a -> Full meas vert horiz height width a
forall sh1 sh0 a. sh1 -> Array sh0 a -> Array sh1 a
Array.reshape Full meas vert horiz height width
shape Array shape a
arr0
         in Omni pack property lower upper meas vert horiz height width
-> Full Size Big Big (Deferred height) (Deferred width) a
-> Maybe String
forall lower upper meas vert horiz height width a pack property.
(Strip lower, Strip upper, Measure meas, C vert, C horiz, C height,
 C width, Floating a) =>
Omni pack property lower upper meas vert horiz height width
-> General (Deferred height) (Deferred width) a -> Maybe String
checkStrips Omni pack property lower upper meas vert horiz height width
omni Full Size Big Big (Deferred height) (Deferred width) a
arr Maybe String -> Maybe String -> Maybe String
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Omni pack property lower upper meas vert horiz height width
-> Full Size Big Big (Deferred height) (Deferred width) a
-> Maybe String
forall property meas vert horiz height width a pack lower upper.
(Property property, Measure meas, C vert, C horiz, C height,
 C width, Floating a) =>
Omni pack property lower upper meas vert horiz height width
-> General (Deferred height) (Deferred width) a -> Maybe String
checkProperty Omni pack property lower upper meas vert horiz height width
omni Full Size Big Big (Deferred height) (Deferred width) a
arr
      Omni.LowerTriangular LowerTriangular height
_ -> DiagSingleton property -> Triangular Lower height a -> Maybe String
forall uplo diag size a.
(UpLo uplo, TriDiag diag, C size, Floating a) =>
DiagSingleton diag -> Triangular uplo size a -> Maybe String
checkTriangular (Triangular lower property upper height -> DiagSingleton property
forall diag lo up sh.
TriDiag diag =>
Triangular lo diag up sh -> DiagSingleton diag
diagTag Omni pack property lower upper meas vert horiz height width
Triangular lower property upper height
omni)
      Omni.UpperTriangular UpperTriangular height
_ -> DiagSingleton property -> Triangular Upper height a -> Maybe String
forall uplo diag size a.
(UpLo uplo, TriDiag diag, C size, Floating a) =>
DiagSingleton diag -> Triangular uplo size a -> Maybe String
checkTriangular (Triangular lower property upper height -> DiagSingleton property
forall diag lo up sh.
TriDiag diag =>
Triangular lo diag up sh -> DiagSingleton diag
diagTag Omni pack property lower upper meas vert horiz height width
Triangular lower property upper height
omni)
      Omni.Hermitian Hermitian height
_ -> String -> Vector height a -> Maybe String
forall sh a.
(C sh, Floating a) =>
String -> Vector sh a -> Maybe String
assertReal String
"Hermitian" (Vector height a -> Maybe String)
-> (Mosaic Packed ConjugateMirror Upper height a
    -> Vector height a)
-> Mosaic Packed ConjugateMirror Upper height a
-> Maybe String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Mosaic Packed ConjugateMirror Upper height a -> Vector height a
forall uplo sh a pack mirror.
(UpLo uplo, C sh, Floating a) =>
Mosaic pack mirror uplo sh a -> Vector sh a
Mosaic.takeDiagonal
      Omni.Banded Banded sub super meas vert horiz height width
_ -> Array shape a -> Maybe String
forall sub super meas vert horiz height width a.
(Natural sub, Natural super, Measure meas, C vert, C horiz,
 C height, C width, Floating a) =>
Banded sub super meas vert horiz height width a -> Maybe String
checkBanded
      Omni.UnitBandedTriangular BandedSquare sub super height
_ -> \Array shape a
arr ->
         Banded sub super Shape Small Small height height a -> Maybe String
forall sub super meas vert horiz height width a.
(Natural sub, Natural super, Measure meas, C vert, C horiz,
 C height, C width, Floating a) =>
Banded sub super meas vert horiz height width a -> Maybe String
checkBanded Array shape a
Banded sub super Shape Small Small height height a
arr Maybe String -> Maybe String -> Maybe String
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
         String -> Vector height a -> Maybe String
forall sh a.
(C sh, Floating a) =>
String -> Vector sh a -> Maybe String
assertOnes String
"BandedTriangular" (Banded sub super Shape Small Small height height a
-> Vector height a
forall sub super sh a.
(Natural sub, Natural super, C sh, Floating a) =>
Square sub super sh a -> Vector sh a
Banded.takeDiagonal Array shape a
Banded sub super Shape Small Small height height a
arr)
      Omni.BandedHermitian BandedHermitian offDiag height
_ -> \Array shape a
arr ->
         let u :: Square U0 offDiag height a
u = BandedHermitian offDiag height a -> Square U0 offDiag height a
forall offDiag size a.
(Natural offDiag, C size, Floating a) =>
BandedHermitian offDiag size a -> Square U0 offDiag size a
BandedHermitian.takeUpper Array shape a
BandedHermitian offDiag height a
arr
         in Square U0 offDiag height a -> Maybe String
forall sub super meas vert horiz height width a.
(Natural sub, Natural super, Measure meas, C vert, C horiz,
 C height, C width, Floating a) =>
Banded sub super meas vert horiz height width a -> Maybe String
checkBanded Square U0 offDiag height a
u Maybe String -> Maybe String -> Maybe String
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
            String -> Vector height a -> Maybe String
forall sh a.
(C sh, Floating a) =>
String -> Vector sh a -> Maybe String
assertReal String
"BandedHermitian" (Square U0 offDiag height a -> Vector height a
forall sub super sh a.
(Natural sub, Natural super, C sh, Floating a) =>
Square sub super sh a -> Vector sh a
Banded.takeDiagonal Square U0 offDiag height a
u)
      Omni pack property lower upper meas vert horiz height width
_ -> Maybe String -> Array shape a -> Maybe String
forall a b. a -> b -> a
const Maybe String
forall a. Maybe a
Nothing

fromList ::
   (Omni.ToPlain pack property lower upper meas vert horiz height width,
    Omni pack property lower upper meas vert horiz height width ~ shape,
    Extent.Measure meas, Extent.C vert, Extent.C horiz) =>
   (Shape.C height, Shape.C width, Class.Floating a) =>
   shape -> [a] -> Array shape a
fromList :: shape -> [a] -> Array shape a
fromList shape
omni =
   (\Array
  (Omni pack property lower upper meas vert horiz height width) a
arr -> Array
  (Omni pack property lower upper meas vert horiz height width) a
-> (String
    -> Array
         (Omni pack property lower upper meas vert horiz height width) a)
-> Maybe String
-> Array
     (Omni pack property lower upper meas vert horiz height width) a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Array
  (Omni pack property lower upper meas vert horiz height width) a
arr String
-> Array
     (Omni pack property lower upper meas vert horiz height width) a
forall a. HasCallStack => String -> a
error (Maybe String
 -> Array
      (Omni pack property lower upper meas vert horiz height width) a)
-> Maybe String
-> Array
     (Omni pack property lower upper meas vert horiz height width) a
forall a b. (a -> b) -> a -> b
$ Omni pack property lower upper meas vert horiz height width
-> Array
     (Plain pack property lower upper meas vert horiz height width) a
-> Maybe String
forall pack property lower upper meas vert horiz height width shape
       a.
(Plain pack property lower upper meas vert horiz height width
 ~ shape,
 Measure meas, C vert, C horiz, C height, C width, Floating a) =>
Omni pack property lower upper meas vert horiz height width
-> Array shape a -> Maybe String
check shape
Omni pack property lower upper meas vert horiz height width
omni (Array
   (Plain pack property lower upper meas vert horiz height width) a
 -> Maybe String)
-> Array
     (Plain pack property lower upper meas vert horiz height width) a
-> Maybe String
forall a b. (a -> b) -> a -> b
$ (Omni pack property lower upper meas vert horiz height width
 -> Plain pack property lower upper meas vert horiz height width)
-> Array
     (Omni pack property lower upper meas vert horiz height width) a
-> Array
     (Plain pack property lower upper meas vert horiz height width) a
forall sh0 sh1 a. (sh0 -> sh1) -> Array sh0 a -> Array sh1 a
Array.mapShape Omni pack property lower upper meas vert horiz height width
-> Plain pack property lower upper meas vert horiz height width
forall pack property lower upper meas vert horiz height width.
ToPlain pack property lower upper meas vert horiz height width =>
Omni pack property lower upper meas vert horiz height width
-> Plain pack property lower upper meas vert horiz height width
Omni.toPlain Array
  (Omni pack property lower upper meas vert horiz height width) a
arr) (Array
   (Omni pack property lower upper meas vert horiz height width) a
 -> Array
      (Omni pack property lower upper meas vert horiz height width) a)
-> ([a]
    -> Array
         (Omni pack property lower upper meas vert horiz height width) a)
-> [a]
-> Array
     (Omni pack property lower upper meas vert horiz height width) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
   shape -> [a] -> Array shape a
forall sh a. (C sh, Storable a) => sh -> [a] -> Array sh a
CheckedArray.fromList shape
omni


assert :: msg -> Bool -> Maybe msg
assert :: msg -> Bool -> Maybe msg
assert msg
msg = (Bool -> msg -> Maybe msg) -> msg -> Bool -> Maybe msg
forall a b c. (a -> b -> c) -> b -> a -> c
flip Bool -> msg -> Maybe msg
forall a. Bool -> a -> Maybe a
toMaybe msg
msg (Bool -> Maybe msg) -> (Bool -> Bool) -> Bool -> Maybe msg
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> Bool
not

proxyFromBands :: f n -> Proxy n
proxyFromBands :: f n -> Proxy n
proxyFromBands f n
_ = Proxy n
forall a. Proxy a
Proxy

outsideBands :: Omni.StripSingleton offDiag -> Int -> Bool
outsideBands :: StripSingleton offDiag -> Int -> Bool
outsideBands StripSingleton offDiag
strip =
   case StripSingleton offDiag
strip of
      StripSingleton offDiag
Omni.StripFilled -> Bool -> Int -> Bool
forall a b. a -> b -> a
const Bool
False
      Omni.StripBands HeadSingleton offDiag
k -> (Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Proxy offDiag -> Int
forall n a. (Natural n, Num a) => Proxy n -> a
Unary.integralFromProxy (HeadSingleton offDiag -> Proxy offDiag
forall (f :: * -> *) n. f n -> Proxy n
proxyFromBands HeadSingleton offDiag
k))

checkStrips ::
   (Omni.Strip lower, Omni.Strip upper) =>
   (Extent.Measure meas, Extent.C vert, Extent.C horiz) =>
   (Shape.C height, Shape.C width, Class.Floating a) =>
   Omni pack property lower upper meas vert horiz height width ->
   Matrix.General (Shape.Deferred height) (Shape.Deferred width) a ->
   Maybe String
checkStrips :: Omni pack property lower upper meas vert horiz height width
-> General (Deferred height) (Deferred width) a -> Maybe String
checkStrips Omni pack property lower upper meas vert horiz height width
omni General (Deferred height) (Deferred width) a
arr =
   case (StripSingleton lower -> Int -> Bool,
 StripSingleton upper -> Int -> Bool)
-> (StripSingleton lower, StripSingleton upper)
-> (Int -> Bool, Int -> Bool)
forall a c b d. (a -> c, b -> d) -> (a, b) -> (c, d)
mapPair (StripSingleton lower -> Int -> Bool
forall offDiag. StripSingleton offDiag -> Int -> Bool
outsideBands, StripSingleton upper -> Int -> Bool
forall offDiag. StripSingleton offDiag -> Int -> Bool
outsideBands) ((StripSingleton lower, StripSingleton upper)
 -> (Int -> Bool, Int -> Bool))
-> (StripSingleton lower, StripSingleton upper)
-> (Int -> Bool, Int -> Bool)
forall a b. (a -> b) -> a -> b
$ Omni pack property lower upper meas vert horiz height width
-> (StripSingleton lower, StripSingleton upper)
forall lower upper pack property meas vert horiz height width.
(Strip lower, Strip upper) =>
Omni pack property lower upper meas vert horiz height width
-> (StripSingleton lower, StripSingleton upper)
Omni.strips Omni pack property lower upper meas vert horiz height width
omni of
      (Int -> Bool
outsideLower,Int -> Bool
outsideUpper) ->
         String -> Bool -> Maybe String
forall msg. msg -> Bool -> Maybe msg
assert String
"non-zero elements outside of declared bands" (Bool -> Maybe String) -> Bool -> Maybe String
forall a b. (a -> b) -> a -> b
$
         ((DeferredIndex height, DeferredIndex width) -> Bool)
-> [(DeferredIndex height, DeferredIndex width)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (a -> Bool
forall a. Floating a => a -> Bool
Scalar.isZero (a -> Bool)
-> ((DeferredIndex height, DeferredIndex width) -> a)
-> (DeferredIndex height, DeferredIndex width)
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (General (Deferred height) (Deferred width) a
arrGeneral (Deferred height) (Deferred width) a
-> Index (General (Deferred height) (Deferred width)) -> a
forall sh a.
(Indexed sh, Storable a) =>
Array sh a -> Index sh -> a
!)) ([(DeferredIndex height, DeferredIndex width)] -> Bool)
-> [(DeferredIndex height, DeferredIndex width)] -> Bool
forall a b. (a -> b) -> a -> b
$
         ((DeferredIndex height, DeferredIndex width) -> Bool)
-> [(DeferredIndex height, DeferredIndex width)]
-> [(DeferredIndex height, DeferredIndex width)]
forall a. (a -> Bool) -> [a] -> [a]
filter
            (\(Shape.DeferredIndex Int
r, Shape.DeferredIndex Int
c) ->
               Int -> Bool
outsideLower (Int
rInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
c) Bool -> Bool -> Bool
|| Int -> Bool
outsideUpper (Int
cInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
r)) ([(DeferredIndex height, DeferredIndex width)]
 -> [(DeferredIndex height, DeferredIndex width)])
-> [(DeferredIndex height, DeferredIndex width)]
-> [(DeferredIndex height, DeferredIndex width)]
forall a b. (a -> b) -> a -> b
$
         General (Deferred height) (Deferred width)
-> [Index (General (Deferred height) (Deferred width))]
forall sh. Indexed sh => sh -> [Index sh]
Shape.indices (General (Deferred height) (Deferred width)
 -> [Index (General (Deferred height) (Deferred width))])
-> General (Deferred height) (Deferred width)
-> [Index (General (Deferred height) (Deferred width))]
forall a b. (a -> b) -> a -> b
$ General (Deferred height) (Deferred width) a
-> General (Deferred height) (Deferred width)
forall sh a. Array sh a -> sh
Array.shape General (Deferred height) (Deferred width) a
arr

checkProperty ::
   (Omni.Property property) =>
   (Extent.Measure meas, Extent.C vert, Extent.C horiz) =>
   (Shape.C height, Shape.C width, Class.Floating a) =>
   Omni pack property lower upper meas vert horiz height width ->
   Matrix.General (Shape.Deferred height) (Shape.Deferred width) a ->
   Maybe String
checkProperty :: Omni pack property lower upper meas vert horiz height width
-> General (Deferred height) (Deferred width) a -> Maybe String
checkProperty Omni pack property lower upper meas vert horiz height width
omni General (Deferred height) (Deferred width) a
arr =
   let shape :: General (Deferred height) (Deferred width)
shape = General (Deferred height) (Deferred width) a
-> General (Deferred height) (Deferred width)
forall sh a. Array sh a -> sh
Array.shape General (Deferred height) (Deferred width) a
arr
   in case Omni pack property lower upper meas vert horiz height width
-> PropertySingleton property
forall property pack lower upper meas vert horiz height width.
Property property =>
Omni pack property lower upper meas vert horiz height width
-> PropertySingleton property
Omni.property Omni pack property lower upper meas vert horiz height width
omni of
         PropertySingleton property
Omni.PropArbitrary -> Maybe String
forall a. Maybe a
Nothing
         PropertySingleton property
Omni.PropUnit ->
            String -> Bool -> Maybe String
forall msg. msg -> Bool -> Maybe msg
assert String
"non-unit diagonal in unpacked matrix" (Bool -> Maybe String) -> Bool -> Maybe String
forall a b. (a -> b) -> a -> b
$
            ((DeferredIndex height, DeferredIndex width) -> Bool)
-> [(DeferredIndex height, DeferredIndex width)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (a -> a -> Bool
forall a. Floating a => a -> a -> Bool
Scalar.equal a
forall a. Floating a => a
Scalar.one (a -> Bool)
-> ((DeferredIndex height, DeferredIndex width) -> a)
-> (DeferredIndex height, DeferredIndex width)
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (General (Deferred height) (Deferred width) a
arrGeneral (Deferred height) (Deferred width) a
-> Index (General (Deferred height) (Deferred width)) -> a
forall sh a.
(Indexed sh, Storable a) =>
Array sh a -> Index sh -> a
!)) ([(DeferredIndex height, DeferredIndex width)] -> Bool)
-> [(DeferredIndex height, DeferredIndex width)] -> Bool
forall a b. (a -> b) -> a -> b
$
            [DeferredIndex height]
-> [DeferredIndex width]
-> [(DeferredIndex height, DeferredIndex width)]
forall a b. [a] -> [b] -> [(a, b)]
zip
               (Deferred height -> [Index (Deferred height)]
forall sh. Indexed sh => sh -> [Index sh]
Shape.indices (Deferred height -> [Index (Deferred height)])
-> Deferred height -> [Index (Deferred height)]
forall a b. (a -> b) -> a -> b
$ General (Deferred height) (Deferred width) a -> Deferred height
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz) =>
Full meas vert horiz height width a -> height
Matrix.height General (Deferred height) (Deferred width) a
arr)
               (Deferred width -> [Index (Deferred width)]
forall sh. Indexed sh => sh -> [Index sh]
Shape.indices (Deferred width -> [Index (Deferred width)])
-> Deferred width -> [Index (Deferred width)]
forall a b. (a -> b) -> a -> b
$ General (Deferred height) (Deferred width) a -> Deferred width
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz) =>
Full meas vert horiz height width a -> width
Matrix.width General (Deferred height) (Deferred width) a
arr)
         PropertySingleton property
Omni.PropSymmetric ->
            String -> Bool -> Maybe String
forall msg. msg -> Bool -> Maybe msg
assert String
"symmetry violated in unpacked matrix" (Bool -> Maybe String) -> Bool -> Maybe String
forall a b. (a -> b) -> a -> b
$
            ((DeferredIndex height, DeferredIndex width) -> Bool)
-> [(DeferredIndex height, DeferredIndex width)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all
               (\ix :: (DeferredIndex height, DeferredIndex width)
ix@(Shape.DeferredIndex Int
r, Shape.DeferredIndex Int
c) ->
                  let tix :: (DeferredIndex height, DeferredIndex width)
tix = (Int -> DeferredIndex height
forall sh. Int -> DeferredIndex sh
Shape.DeferredIndex Int
c, Int -> DeferredIndex width
forall sh. Int -> DeferredIndex sh
Shape.DeferredIndex Int
r)
                  in General (Deferred height) (Deferred width)
-> Index (General (Deferred height) (Deferred width)) -> Bool
forall sh. Indexed sh => sh -> Index sh -> Bool
Shape.inBounds General (Deferred height) (Deferred width)
shape (DeferredIndex height, DeferredIndex width)
Index (General (Deferred height) (Deferred width))
tix Bool -> Bool -> Bool
`implies`
                        a -> a -> Bool
forall a. Floating a => a -> a -> Bool
Scalar.equal (General (Deferred height) (Deferred width) a
arrGeneral (Deferred height) (Deferred width) a
-> Index (General (Deferred height) (Deferred width)) -> a
forall sh a.
(Indexed sh, Storable a) =>
Array sh a -> Index sh -> a
!(DeferredIndex height, DeferredIndex width)
Index (General (Deferred height) (Deferred width))
ix) (General (Deferred height) (Deferred width) a
arrGeneral (Deferred height) (Deferred width) a
-> Index (General (Deferred height) (Deferred width)) -> a
forall sh a.
(Indexed sh, Storable a) =>
Array sh a -> Index sh -> a
!(DeferredIndex height, DeferredIndex width)
Index (General (Deferred height) (Deferred width))
tix)) ([(DeferredIndex height, DeferredIndex width)] -> Bool)
-> [(DeferredIndex height, DeferredIndex width)] -> Bool
forall a b. (a -> b) -> a -> b
$
            General (Deferred height) (Deferred width)
-> [Index (General (Deferred height) (Deferred width))]
forall sh. Indexed sh => sh -> [Index sh]
Shape.indices General (Deferred height) (Deferred width)
shape
         PropertySingleton property
Omni.PropHermitian ->
            String -> Bool -> Maybe String
forall msg. msg -> Bool -> Maybe msg
assert String
"conjugated symmetry violated in unpacked matrix" (Bool -> Maybe String) -> Bool -> Maybe String
forall a b. (a -> b) -> a -> b
$
            ((DeferredIndex height, DeferredIndex width) -> Bool)
-> [(DeferredIndex height, DeferredIndex width)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all
               (\ix :: (DeferredIndex height, DeferredIndex width)
ix@(Shape.DeferredIndex Int
r, Shape.DeferredIndex Int
c) ->
                  let tix :: (DeferredIndex height, DeferredIndex width)
tix = (Int -> DeferredIndex height
forall sh. Int -> DeferredIndex sh
Shape.DeferredIndex Int
c, Int -> DeferredIndex width
forall sh. Int -> DeferredIndex sh
Shape.DeferredIndex Int
r)
                  in General (Deferred height) (Deferred width)
-> Index (General (Deferred height) (Deferred width)) -> Bool
forall sh. Indexed sh => sh -> Index sh -> Bool
Shape.inBounds General (Deferred height) (Deferred width)
shape (DeferredIndex height, DeferredIndex width)
Index (General (Deferred height) (Deferred width))
tix Bool -> Bool -> Bool
`implies`
                        a -> a -> Bool
forall a. Floating a => a -> a -> Bool
Scalar.equal (General (Deferred height) (Deferred width) a
arrGeneral (Deferred height) (Deferred width) a
-> Index (General (Deferred height) (Deferred width)) -> a
forall sh a.
(Indexed sh, Storable a) =>
Array sh a -> Index sh -> a
!(DeferredIndex height, DeferredIndex width)
Index (General (Deferred height) (Deferred width))
ix) (a -> a
forall a. Floating a => a -> a
Scalar.conjugate (a -> a) -> a -> a
forall a b. (a -> b) -> a -> b
$ General (Deferred height) (Deferred width) a
arrGeneral (Deferred height) (Deferred width) a
-> Index (General (Deferred height) (Deferred width)) -> a
forall sh a.
(Indexed sh, Storable a) =>
Array sh a -> Index sh -> a
!(DeferredIndex height, DeferredIndex width)
Index (General (Deferred height) (Deferred width))
tix)) ([(DeferredIndex height, DeferredIndex width)] -> Bool)
-> [(DeferredIndex height, DeferredIndex width)] -> Bool
forall a b. (a -> b) -> a -> b
$
            General (Deferred height) (Deferred width)
-> [Index (General (Deferred height) (Deferred width))]
forall sh. Indexed sh => sh -> [Index sh]
Shape.indices General (Deferred height) (Deferred width)
shape


diagTag ::
   (Omni.TriDiag diag) =>
   MatrixShape.Triangular lo diag up sh -> Omni.DiagSingleton diag
diagTag :: Triangular lo diag up sh -> DiagSingleton diag
diagTag Triangular lo diag up sh
_ = DiagSingleton diag
forall diag. TriDiag diag => DiagSingleton diag
Omni.autoDiag

checkTriangular ::
   (Layout.UpLo uplo, Omni.TriDiag diag,
    Shape.C size, Class.Floating a) =>
   Omni.DiagSingleton diag ->
   Triangular.Triangular uplo size a -> Maybe String
checkTriangular :: DiagSingleton diag -> Triangular uplo size a -> Maybe String
checkTriangular DiagSingleton diag
diag =
   case DiagSingleton diag
diag of
      DiagSingleton diag
Omni.Arbitrary -> Maybe String -> Triangular uplo size a -> Maybe String
forall a b. a -> b -> a
const Maybe String
forall a. Maybe a
Nothing
      DiagSingleton diag
Omni.Unit -> String -> Vector size a -> Maybe String
forall sh a.
(C sh, Floating a) =>
String -> Vector sh a -> Maybe String
assertOnes String
"Triangular" (Vector size a -> Maybe String)
-> (Triangular uplo size a -> Vector size a)
-> Triangular uplo size a
-> Maybe String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Triangular uplo size a -> Vector size a
forall uplo sh a pack mirror.
(UpLo uplo, C sh, Floating a) =>
Mosaic pack mirror uplo sh a -> Vector sh a
Mosaic.takeDiagonal

checkBanded ::
   (Unary.Natural sub, Unary.Natural super,
    Extent.Measure meas, Extent.C vert, Extent.C horiz,
    Shape.C height, Shape.C width,  Class.Floating a) =>
   Banded.Banded sub super meas vert horiz height width a -> Maybe String
checkBanded :: Banded sub super meas vert horiz height width a -> Maybe String
checkBanded Banded sub super meas vert horiz height width a
arr0 =
   let arr :: Banded sub super Size Big Big (Deferred height) (Deferred width) a
arr =
         (height -> Deferred height)
-> Banded sub super Size Big Big height (Deferred width) a
-> Banded
     sub super Size Big Big (Deferred height) (Deferred width) a
forall vert horiz heightA heightB sub super width a.
(C vert, C horiz) =>
(heightA -> heightB)
-> Banded sub super Size vert horiz heightA width a
-> Banded sub super Size vert horiz heightB width a
Banded.mapHeight height -> Deferred height
forall sh. sh -> Deferred sh
Shape.Deferred (Banded sub super Size Big Big height (Deferred width) a
 -> Banded
      sub super Size Big Big (Deferred height) (Deferred width) a)
-> Banded sub super Size Big Big height (Deferred width) a
-> Banded
     sub super Size Big Big (Deferred height) (Deferred width) a
forall a b. (a -> b) -> a -> b
$
         (width -> Deferred width)
-> Banded sub super Size Big Big height width a
-> Banded sub super Size Big Big height (Deferred width) a
forall vert horiz widthA widthB sub super height a.
(C vert, C horiz) =>
(widthA -> widthB)
-> Banded sub super Size vert horiz height widthA a
-> Banded sub super Size vert horiz height widthB a
Banded.mapWidth width -> Deferred width
forall sh. sh -> Deferred sh
Shape.Deferred (Banded sub super Size Big Big height width a
 -> Banded sub super Size Big Big height (Deferred width) a)
-> Banded sub super Size Big Big height width a
-> Banded sub super Size Big Big height (Deferred width) a
forall a b. (a -> b) -> a -> b
$
         Map meas vert horiz Size Big Big height width
-> Banded sub super meas vert horiz height width a
-> Banded sub super Size Big Big height width a
forall vertA horizA vertB horizB measA measB height width sub super
       a.
(C vertA, C horizA, C vertB, C horizB) =>
Map measA vertA horizA measB vertB horizB height width
-> Banded sub super measA vertA horizA height width a
-> Banded sub super measB vertB horizB height width a
Banded.mapExtent Map meas vert horiz Size Big Big height width
forall meas vert horiz height width.
(Measure meas, C vert, C horiz) =>
Extent meas vert horiz height width -> General height width
ExtentPriv.toGeneral Banded sub super meas vert horiz height width a
arr0
   in String -> Bool -> Maybe String
forall msg. msg -> Bool -> Maybe msg
assert String
"Banded with non-zero unused elements" (Bool -> Maybe String) -> Bool -> Maybe String
forall a b. (a -> b) -> a -> b
$
      (BandedIndex (DeferredIndex height) (DeferredIndex width) -> Bool)
-> [BandedIndex (DeferredIndex height) (DeferredIndex width)]
-> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (a -> Bool
forall a. Floating a => a -> Bool
Scalar.isZero (a -> Bool)
-> (BandedIndex (DeferredIndex height) (DeferredIndex width) -> a)
-> BandedIndex (DeferredIndex height) (DeferredIndex width)
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Banded sub super Size Big Big (Deferred height) (Deferred width) a
arrBanded sub super Size Big Big (Deferred height) (Deferred width) a
-> Index
     (Banded sub super Size Big Big (Deferred height) (Deferred width))
-> a
forall sh a.
(Indexed sh, Storable a) =>
Array sh a -> Index sh -> a
!)) ([BandedIndex (DeferredIndex height) (DeferredIndex width)]
 -> Bool)
-> [BandedIndex (DeferredIndex height) (DeferredIndex width)]
-> Bool
forall a b. (a -> b) -> a -> b
$
      (BandedIndex (DeferredIndex height) (DeferredIndex width) -> Bool)
-> [BandedIndex (DeferredIndex height) (DeferredIndex width)]
-> [BandedIndex (DeferredIndex height) (DeferredIndex width)]
forall a. (a -> Bool) -> [a] -> [a]
filter
         (\BandedIndex (DeferredIndex height) (DeferredIndex width)
ix -> case BandedIndex (DeferredIndex height) (DeferredIndex width)
ix of Layout.InsideBox DeferredIndex height
_ DeferredIndex width
_ -> Bool
False; BandedIndex (DeferredIndex height) (DeferredIndex width)
_ -> Bool
True) ([BandedIndex (DeferredIndex height) (DeferredIndex width)]
 -> [BandedIndex (DeferredIndex height) (DeferredIndex width)])
-> [BandedIndex (DeferredIndex height) (DeferredIndex width)]
-> [BandedIndex (DeferredIndex height) (DeferredIndex width)]
forall a b. (a -> b) -> a -> b
$
      Banded sub super Size Big Big (Deferred height) (Deferred width)
-> [Index
      (Banded sub super Size Big Big (Deferred height) (Deferred width))]
forall sh. Indexed sh => sh -> [Index sh]
Shape.indices (Banded sub super Size Big Big (Deferred height) (Deferred width)
 -> [Index
       (Banded
          sub super Size Big Big (Deferred height) (Deferred width))])
-> Banded sub super Size Big Big (Deferred height) (Deferred width)
-> [Index
      (Banded sub super Size Big Big (Deferred height) (Deferred width))]
forall a b. (a -> b) -> a -> b
$ Banded sub super Size Big Big (Deferred height) (Deferred width) a
-> Banded sub super Size Big Big (Deferred height) (Deferred width)
forall sh a. Array sh a -> sh
Array.shape Banded sub super Size Big Big (Deferred height) (Deferred width) a
arr

assertReal ::
   (Shape.C sh, Class.Floating a) => String -> Vector sh a -> Maybe String
assertReal :: String -> Vector sh a -> Maybe String
assertReal String
name = String -> Bool -> Maybe String
forall msg. msg -> Bool -> Maybe msg
assert (String
name String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" with non-real diagonal") (Bool -> Maybe String)
-> (Vector sh a -> Bool) -> Vector sh a -> Maybe String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector sh a -> Bool
forall sh a. (C sh, Floating a) => Vector sh a -> Bool
isReal

isReal :: (Shape.C sh, Class.Floating a) => Vector sh a -> Bool
isReal :: Vector sh a -> Bool
isReal =
   Flip (->) Bool (Vector sh a) -> Vector sh a -> Bool
forall (f :: * -> * -> *) b a. Flip f b a -> f a b
getFlip (Flip (->) Bool (Vector sh a) -> Vector sh a -> Bool)
-> Flip (->) Bool (Vector sh a) -> Vector sh a -> Bool
forall a b. (a -> b) -> a -> b
$ Compose (Flip (->) Bool) (Array sh) a
-> Flip (->) Bool (Vector sh a)
forall k1 (f :: k1 -> *) k2 (g :: k2 -> k1) (a :: k2).
Compose f g a -> f (g a)
getCompose (Compose (Flip (->) Bool) (Array sh) a
 -> Flip (->) Bool (Vector sh a))
-> Compose (Flip (->) Bool) (Array sh) a
-> Flip (->) Bool (Vector sh a)
forall a b. (a -> b) -> a -> b
$
   Compose (Flip (->) Bool) (Array sh) Float
-> Compose (Flip (->) Bool) (Array sh) Double
-> Compose (Flip (->) Bool) (Array sh) (Complex Float)
-> Compose (Flip (->) Bool) (Array sh) (Complex Double)
-> Compose (Flip (->) Bool) (Array sh) a
forall a (f :: * -> *).
Floating a =>
f Float
-> f Double -> f (Complex Float) -> f (Complex Double) -> f a
Class.switchFloating
      (Flip (->) Bool (Array sh Float)
-> Compose (Flip (->) Bool) (Array sh) Float
forall k k1 (f :: k -> *) (g :: k1 -> k) (a :: k1).
f (g a) -> Compose f g a
Compose (Flip (->) Bool (Array sh Float)
 -> Compose (Flip (->) Bool) (Array sh) Float)
-> Flip (->) Bool (Array sh Float)
-> Compose (Flip (->) Bool) (Array sh) Float
forall a b. (a -> b) -> a -> b
$ (Array sh Float -> Bool) -> Flip (->) Bool (Array sh Float)
forall (f :: * -> * -> *) b a. f a b -> Flip f b a
Flip ((Array sh Float -> Bool) -> Flip (->) Bool (Array sh Float))
-> (Array sh Float -> Bool) -> Flip (->) Bool (Array sh Float)
forall a b. (a -> b) -> a -> b
$ Bool -> Array sh Float -> Bool
forall a b. a -> b -> a
const Bool
True)
      (Flip (->) Bool (Array sh Double)
-> Compose (Flip (->) Bool) (Array sh) Double
forall k k1 (f :: k -> *) (g :: k1 -> k) (a :: k1).
f (g a) -> Compose f g a
Compose (Flip (->) Bool (Array sh Double)
 -> Compose (Flip (->) Bool) (Array sh) Double)
-> Flip (->) Bool (Array sh Double)
-> Compose (Flip (->) Bool) (Array sh) Double
forall a b. (a -> b) -> a -> b
$ (Array sh Double -> Bool) -> Flip (->) Bool (Array sh Double)
forall (f :: * -> * -> *) b a. f a b -> Flip f b a
Flip ((Array sh Double -> Bool) -> Flip (->) Bool (Array sh Double))
-> (Array sh Double -> Bool) -> Flip (->) Bool (Array sh Double)
forall a b. (a -> b) -> a -> b
$ Bool -> Array sh Double -> Bool
forall a b. a -> b -> a
const Bool
True)
      (Flip (->) Bool (Array sh (Complex Float))
-> Compose (Flip (->) Bool) (Array sh) (Complex Float)
forall k k1 (f :: k -> *) (g :: k1 -> k) (a :: k1).
f (g a) -> Compose f g a
Compose (Flip (->) Bool (Array sh (Complex Float))
 -> Compose (Flip (->) Bool) (Array sh) (Complex Float))
-> Flip (->) Bool (Array sh (Complex Float))
-> Compose (Flip (->) Bool) (Array sh) (Complex Float)
forall a b. (a -> b) -> a -> b
$ (Array sh (Complex Float) -> Bool)
-> Flip (->) Bool (Array sh (Complex Float))
forall (f :: * -> * -> *) b a. f a b -> Flip f b a
Flip Array sh (Complex Float) -> Bool
forall sh a. (C sh, Real a) => Vector sh (Complex a) -> Bool
isComplexReal)
      (Flip (->) Bool (Array sh (Complex Double))
-> Compose (Flip (->) Bool) (Array sh) (Complex Double)
forall k k1 (f :: k -> *) (g :: k1 -> k) (a :: k1).
f (g a) -> Compose f g a
Compose (Flip (->) Bool (Array sh (Complex Double))
 -> Compose (Flip (->) Bool) (Array sh) (Complex Double))
-> Flip (->) Bool (Array sh (Complex Double))
-> Compose (Flip (->) Bool) (Array sh) (Complex Double)
forall a b. (a -> b) -> a -> b
$ (Array sh (Complex Double) -> Bool)
-> Flip (->) Bool (Array sh (Complex Double))
forall (f :: * -> * -> *) b a. f a b -> Flip f b a
Flip Array sh (Complex Double) -> Bool
forall sh a. (C sh, Real a) => Vector sh (Complex a) -> Bool
isComplexReal)

isComplexReal ::
   (Shape.C sh, Class.Real a) => Vector sh (Complex.Complex a) -> Bool
isComplexReal :: Vector sh (Complex a) -> Bool
isComplexReal = (a -> Bool) -> [a] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all a -> Bool
forall a. Floating a => a -> Bool
Scalar.isZero ([a] -> Bool)
-> (Vector sh (Complex a) -> [a]) -> Vector sh (Complex a) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector sh a -> [a]
forall sh a. (C sh, Storable a) => Vector sh a -> [a]
Vector.toList (Vector sh a -> [a])
-> (Vector sh (Complex a) -> Vector sh a)
-> Vector sh (Complex a)
-> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector sh (Complex a) -> Vector sh a
forall sh a. (C sh, Real a) => Vector sh (Complex a) -> Vector sh a
Vector.imaginaryPart

assertOnes ::
   (Shape.C sh, Class.Floating a) => String -> Vector sh a -> Maybe String
assertOnes :: String -> Vector sh a -> Maybe String
assertOnes String
name =
   String -> Bool -> Maybe String
forall msg. msg -> Bool -> Maybe msg
assert (String
name String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
".Unit with non-unit diagonal elements") (Bool -> Maybe String)
-> (Vector sh a -> Bool) -> Vector sh a -> Maybe String
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
   (a -> Bool) -> [a] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (a -> a -> Bool
forall a. Floating a => a -> a -> Bool
Scalar.equal a
forall a. Floating a => a
Scalar.one) ([a] -> Bool) -> (Vector sh a -> [a]) -> Vector sh a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector sh a -> [a]
forall sh a. (C sh, Storable a) => Vector sh a -> [a]
Vector.toList