{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE MultiParamTypeClasses #-}
module Numeric.LAPACK.Matrix.Shape.Private where

import qualified Numeric.LAPACK.Matrix.Shape.Box as Box
import qualified Numeric.LAPACK.Matrix.Extent.Private as Extent
import Numeric.LAPACK.Matrix.Extent.Private (Extent)
import Numeric.LAPACK.Wrapper (Flip(Flip, getFlip))

import qualified Type.Data.Num.Unary.Literal as TypeNum
import qualified Type.Data.Num.Unary.Proof as Proof
import qualified Type.Data.Num.Unary as Unary
import Type.Data.Num.Unary (unary, (:+:))
import Type.Data.Num (integralFromProxy)
import Type.Base.Proxy (Proxy(Proxy))

import qualified Data.Array.Comfort.Shape as Shape
import Data.Array.Comfort.Shape (triangleSize, triangleRoot)

import Control.DeepSeq (NFData, rnf)
import Control.Applicative (Const(Const, getConst))

import Data.Functor.Identity (Identity(Identity), runIdentity)
import Data.List (tails)
import Data.Tuple.HT (mapSnd, swap, double)
import Data.Bool.HT (if')


data Order = RowMajor | ColumnMajor
   deriving (Order -> Order -> Bool
(Order -> Order -> Bool) -> (Order -> Order -> Bool) -> Eq Order
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Order -> Order -> Bool
$c/= :: Order -> Order -> Bool
== :: Order -> Order -> Bool
$c== :: Order -> Order -> Bool
Eq, Int -> Order -> ShowS
[Order] -> ShowS
Order -> String
(Int -> Order -> ShowS)
-> (Order -> String) -> ([Order] -> ShowS) -> Show Order
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Order] -> ShowS
$cshowList :: [Order] -> ShowS
show :: Order -> String
$cshow :: Order -> String
showsPrec :: Int -> Order -> ShowS
$cshowsPrec :: Int -> Order -> ShowS
Show)

instance NFData Order where
   rnf :: Order -> ()
rnf Order
RowMajor = ()
   rnf Order
ColumnMajor = ()

flipOrder :: Order -> Order
flipOrder :: Order -> Order
flipOrder Order
RowMajor = Order
ColumnMajor
flipOrder Order
ColumnMajor = Order
RowMajor

transposeFromOrder :: Order -> Char
transposeFromOrder :: Order -> Char
transposeFromOrder Order
RowMajor = Char
'T'
transposeFromOrder Order
ColumnMajor = Char
'N'

swapOnRowMajor :: Order -> (a,a) -> (a,a)
swapOnRowMajor :: Order -> (a, a) -> (a, a)
swapOnRowMajor Order
order =
   case Order
order of
      Order
RowMajor -> (a, a) -> (a, a)
forall a b. (a, b) -> (b, a)
swap
      Order
ColumnMajor -> (a, a) -> (a, a)
forall a. a -> a
id

sideSwapFromOrder :: Order -> (a,a) -> (Char, (a,a))
sideSwapFromOrder :: Order -> (a, a) -> (Char, (a, a))
sideSwapFromOrder Order
order (a
m0,a
n0) =
   let ((Char
side,a
m), (Char
_,a
n)) = Order -> ((Char, a), (Char, a)) -> ((Char, a), (Char, a))
forall a. Order -> (a, a) -> (a, a)
swapOnRowMajor Order
order ((Char
'L', a
m0), (Char
'R', a
n0))
   in (Char
side,(a
m,a
n))


mapChecked ::
   (Shape.C sha, Shape.C shb) =>
   String -> (sha -> shb) -> sha -> shb
mapChecked :: String -> (sha -> shb) -> sha -> shb
mapChecked String
name sha -> shb
f sha
sizeA =
   let sizeB :: shb
sizeB = sha -> shb
f sha
sizeA
   in if sha -> Int
forall sh. C sh => sh -> Int
Shape.size sha
sizeA Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== shb -> Int
forall sh. C sh => sh -> Int
Shape.size shb
sizeB
         then shb
sizeB
         else String -> shb
forall a. HasCallStack => String -> a
error (String -> shb) -> String -> shb
forall a b. (a -> b) -> a -> b
$ String
name String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
": sizes mismatch"


data Full vert horiz height width =
   Full {
      Full vert horiz height width -> Order
fullOrder :: Order,
      Full vert horiz height width -> Extent vert horiz height width
fullExtent :: Extent vert horiz height width
   } deriving (Full vert horiz height width
-> Full vert horiz height width -> Bool
(Full vert horiz height width
 -> Full vert horiz height width -> Bool)
-> (Full vert horiz height width
    -> Full vert horiz height width -> Bool)
-> Eq (Full vert horiz height width)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall vert horiz height width.
(C vert, C horiz, Eq height, Eq width) =>
Full vert horiz height width
-> Full vert horiz height width -> Bool
/= :: Full vert horiz height width
-> Full vert horiz height width -> Bool
$c/= :: forall vert horiz height width.
(C vert, C horiz, Eq height, Eq width) =>
Full vert horiz height width
-> Full vert horiz height width -> Bool
== :: Full vert horiz height width
-> Full vert horiz height width -> Bool
$c== :: forall vert horiz height width.
(C vert, C horiz, Eq height, Eq width) =>
Full vert horiz height width
-> Full vert horiz height width -> Bool
Eq, Int -> Full vert horiz height width -> ShowS
[Full vert horiz height width] -> ShowS
Full vert horiz height width -> String
(Int -> Full vert horiz height width -> ShowS)
-> (Full vert horiz height width -> String)
-> ([Full vert horiz height width] -> ShowS)
-> Show (Full vert horiz height width)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall vert horiz height width.
(C vert, C horiz, Show height, Show width) =>
Int -> Full vert horiz height width -> ShowS
forall vert horiz height width.
(C vert, C horiz, Show height, Show width) =>
[Full vert horiz height width] -> ShowS
forall vert horiz height width.
(C vert, C horiz, Show height, Show width) =>
Full vert horiz height width -> String
showList :: [Full vert horiz height width] -> ShowS
$cshowList :: forall vert horiz height width.
(C vert, C horiz, Show height, Show width) =>
[Full vert horiz height width] -> ShowS
show :: Full vert horiz height width -> String
$cshow :: forall vert horiz height width.
(C vert, C horiz, Show height, Show width) =>
Full vert horiz height width -> String
showsPrec :: Int -> Full vert horiz height width -> ShowS
$cshowsPrec :: forall vert horiz height width.
(C vert, C horiz, Show height, Show width) =>
Int -> Full vert horiz height width -> ShowS
Show)

instance
   (Extent.C vert, Extent.C horiz, Shape.C height, Shape.C width) =>
      Box.Box (Full vert horiz height width) where
   type HeightOf (Full vert horiz height width) = height
   type WidthOf (Full vert horiz height width) = width
   height :: Full vert horiz height width
-> HeightOf (Full vert horiz height width)
height = Full vert horiz height width
-> HeightOf (Full vert horiz height width)
forall vert horiz height width.
(C vert, C horiz) =>
Full vert horiz height width -> height
fullHeight
   width :: Full vert horiz height width
-> WidthOf (Full vert horiz height width)
width = Full vert horiz height width
-> WidthOf (Full vert horiz height width)
forall vert horiz height width.
(C vert, C horiz) =>
Full vert horiz height width -> width
fullWidth

instance
   (Extent.C vert, Extent.C horiz, NFData height, NFData width) =>
      NFData (Full vert horiz height width) where
   rnf :: Full vert horiz height width -> ()
rnf (Full Order
order Extent vert horiz height width
extent) = (Order, Extent vert horiz height width) -> ()
forall a. NFData a => a -> ()
rnf (Order
order, Extent vert horiz height width
extent)

instance
   (Extent.C vert, Extent.C horiz, Shape.C height, Shape.C width) =>
      Shape.C (Full vert horiz height width) where

   size :: Full vert horiz height width -> Int
size (Full Order
_ Extent vert horiz height width
extent) = (height, width) -> Int
forall sh. C sh => sh -> Int
Shape.size (Extent vert horiz height width -> (height, width)
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> (height, width)
Extent.dimensions Extent vert horiz height width
extent)
   uncheckedSize :: Full vert horiz height width -> Int
uncheckedSize (Full Order
_ Extent vert horiz height width
extent) =
      (height, width) -> Int
forall sh. C sh => sh -> Int
Shape.uncheckedSize (Extent vert horiz height width -> (height, width)
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> (height, width)
Extent.dimensions Extent vert horiz height width
extent)

instance
   (Extent.C vert, Extent.C horiz, Shape.Indexed height, Shape.Indexed width) =>
      Shape.Indexed (Full vert horiz height width) where

   type Index (Full vert horiz height width) =
            (Shape.Index height, Shape.Index width)
   indices :: Full vert horiz height width
-> [Index (Full vert horiz height width)]
indices (Full Order
order Extent vert horiz height width
extent) = Order
-> Extent vert horiz height width -> [(Index height, Index width)]
forall vert horiz a b.
(C vert, C horiz, Indexed a, Indexed b) =>
Order -> Extent vert horiz a b -> [(Index a, Index b)]
fullIndices Order
order Extent vert horiz height width
extent

   offset :: Full vert horiz height width
-> Index (Full vert horiz height width) -> Int
offset (Full Order
RowMajor Extent vert horiz height width
extent) =
      (height, width) -> Index (height, width) -> Int
forall sh. Indexed sh => sh -> Index sh -> Int
Shape.offset (Extent vert horiz height width -> (height, width)
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> (height, width)
Extent.dimensions Extent vert horiz height width
extent)
   offset (Full Order
ColumnMajor Extent vert horiz height width
extent) =
      (width, height) -> Index (width, height) -> Int
forall sh. Indexed sh => sh -> Index sh -> Int
Shape.offset ((height, width) -> (width, height)
forall a b. (a, b) -> (b, a)
swap ((height, width) -> (width, height))
-> (height, width) -> (width, height)
forall a b. (a -> b) -> a -> b
$ Extent vert horiz height width -> (height, width)
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> (height, width)
Extent.dimensions Extent vert horiz height width
extent) ((Index width, Index height) -> Int)
-> ((Index height, Index width) -> (Index width, Index height))
-> (Index height, Index width)
-> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Index height, Index width) -> (Index width, Index height)
forall a b. (a, b) -> (b, a)
swap
   uncheckedOffset :: Full vert horiz height width
-> Index (Full vert horiz height width) -> Int
uncheckedOffset (Full Order
RowMajor Extent vert horiz height width
extent) =
      (height, width) -> Index (height, width) -> Int
forall sh. Indexed sh => sh -> Index sh -> Int
Shape.uncheckedOffset (Extent vert horiz height width -> (height, width)
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> (height, width)
Extent.dimensions Extent vert horiz height width
extent)
   uncheckedOffset (Full Order
ColumnMajor Extent vert horiz height width
extent) =
      (width, height) -> Index (width, height) -> Int
forall sh. Indexed sh => sh -> Index sh -> Int
Shape.uncheckedOffset ((height, width) -> (width, height)
forall a b. (a, b) -> (b, a)
swap ((height, width) -> (width, height))
-> (height, width) -> (width, height)
forall a b. (a -> b) -> a -> b
$ Extent vert horiz height width -> (height, width)
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> (height, width)
Extent.dimensions Extent vert horiz height width
extent) ((Index width, Index height) -> Int)
-> ((Index height, Index width) -> (Index width, Index height))
-> (Index height, Index width)
-> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Index height, Index width) -> (Index width, Index height)
forall a b. (a, b) -> (b, a)
swap

   sizeOffset :: Full vert horiz height width
-> (Int, Index (Full vert horiz height width) -> Int)
sizeOffset (Full Order
RowMajor Extent vert horiz height width
extent) =
      (height, width) -> (Int, Index (height, width) -> Int)
forall sh. Indexed sh => sh -> (Int, Index sh -> Int)
Shape.sizeOffset (Extent vert horiz height width -> (height, width)
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> (height, width)
Extent.dimensions Extent vert horiz height width
extent)
   sizeOffset (Full Order
ColumnMajor Extent vert horiz height width
extent) =
      (((Index width, Index height) -> Int)
 -> (Index height, Index width) -> Int)
-> (Int, (Index width, Index height) -> Int)
-> (Int, (Index height, Index width) -> Int)
forall b c a. (b -> c) -> (a, b) -> (a, c)
mapSnd (((Index width, Index height) -> Int)
-> ((Index height, Index width) -> (Index width, Index height))
-> (Index height, Index width)
-> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Index height, Index width) -> (Index width, Index height)
forall a b. (a, b) -> (b, a)
swap) ((Int, (Index width, Index height) -> Int)
 -> (Int, (Index height, Index width) -> Int))
-> (Int, (Index width, Index height) -> Int)
-> (Int, (Index height, Index width) -> Int)
forall a b. (a -> b) -> a -> b
$ (width, height) -> (Int, Index (width, height) -> Int)
forall sh. Indexed sh => sh -> (Int, Index sh -> Int)
Shape.sizeOffset ((height, width) -> (width, height)
forall a b. (a, b) -> (b, a)
swap ((height, width) -> (width, height))
-> (height, width) -> (width, height)
forall a b. (a -> b) -> a -> b
$ Extent vert horiz height width -> (height, width)
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> (height, width)
Extent.dimensions Extent vert horiz height width
extent)
   uncheckedSizeOffset :: Full vert horiz height width
-> (Int, Index (Full vert horiz height width) -> Int)
uncheckedSizeOffset (Full Order
RowMajor Extent vert horiz height width
extent) =
      (height, width) -> (Int, Index (height, width) -> Int)
forall sh. Indexed sh => sh -> (Int, Index sh -> Int)
Shape.uncheckedSizeOffset (Extent vert horiz height width -> (height, width)
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> (height, width)
Extent.dimensions Extent vert horiz height width
extent)
   uncheckedSizeOffset (Full Order
ColumnMajor Extent vert horiz height width
extent) =
      (((Index width, Index height) -> Int)
 -> (Index height, Index width) -> Int)
-> (Int, (Index width, Index height) -> Int)
-> (Int, (Index height, Index width) -> Int)
forall b c a. (b -> c) -> (a, b) -> (a, c)
mapSnd (((Index width, Index height) -> Int)
-> ((Index height, Index width) -> (Index width, Index height))
-> (Index height, Index width)
-> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Index height, Index width) -> (Index width, Index height)
forall a b. (a, b) -> (b, a)
swap) ((Int, (Index width, Index height) -> Int)
 -> (Int, (Index height, Index width) -> Int))
-> (Int, (Index width, Index height) -> Int)
-> (Int, (Index height, Index width) -> Int)
forall a b. (a -> b) -> a -> b
$
      (width, height) -> (Int, Index (width, height) -> Int)
forall sh. Indexed sh => sh -> (Int, Index sh -> Int)
Shape.uncheckedSizeOffset ((height, width) -> (width, height)
forall a b. (a, b) -> (b, a)
swap ((height, width) -> (width, height))
-> (height, width) -> (width, height)
forall a b. (a -> b) -> a -> b
$ Extent vert horiz height width -> (height, width)
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> (height, width)
Extent.dimensions Extent vert horiz height width
extent)

   inBounds :: Full vert horiz height width
-> Index (Full vert horiz height width) -> Bool
inBounds (Full Order
_ Extent vert horiz height width
extent) = (height, width) -> Index (height, width) -> Bool
forall sh. Indexed sh => sh -> Index sh -> Bool
Shape.inBounds (Extent vert horiz height width -> (height, width)
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> (height, width)
Extent.dimensions Extent vert horiz height width
extent)

instance
   (Extent.C vert, Extent.C horiz,
    Shape.InvIndexed height, Shape.InvIndexed width) =>
      Shape.InvIndexed (Full vert horiz height width) where

   indexFromOffset :: Full vert horiz height width
-> Int -> Index (Full vert horiz height width)
indexFromOffset (Full Order
order Extent vert horiz height width
extent) = Order
-> Extent vert horiz height width
-> Int
-> (Index height, Index width)
forall vert horiz a b.
(C vert, C horiz, InvIndexed a, InvIndexed b) =>
Order -> Extent vert horiz a b -> Int -> (Index a, Index b)
fullIndexFromOffset Order
order Extent vert horiz height width
extent


transpose ::
   (Extent.C vert, Extent.C horiz) =>
   Full vert horiz height width -> Full horiz vert width height
transpose :: Full vert horiz height width -> Full horiz vert width height
transpose (Full Order
order Extent vert horiz height width
extent) = Order
-> Extent horiz vert width height -> Full horiz vert width height
forall vert horiz height width.
Order
-> Extent vert horiz height width -> Full vert horiz height width
Full (Order -> Order
flipOrder Order
order) (Extent vert horiz height width -> Extent horiz vert width height
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> Extent horiz vert width height
Extent.transpose Extent vert horiz height width
extent)

dimensions ::
   (Extent.C vert, Extent.C horiz, Shape.C height, Shape.C width) =>
   Full vert horiz height width -> (Int, Int)
dimensions :: Full vert horiz height width -> (Int, Int)
dimensions (Full Order
order Extent vert horiz height width
extent) =
   Order -> (Int, Int) -> (Int, Int)
forall a. Order -> (a, a) -> (a, a)
swapOnRowMajor Order
order
      (height -> Int
forall sh. C sh => sh -> Int
Shape.size (height -> Int) -> height -> Int
forall a b. (a -> b) -> a -> b
$ Extent vert horiz height width -> height
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> height
Extent.height Extent vert horiz height width
extent,
       width -> Int
forall sh. C sh => sh -> Int
Shape.size (width -> Int) -> width -> Int
forall a b. (a -> b) -> a -> b
$ Extent vert horiz height width -> width
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> width
Extent.width Extent vert horiz height width
extent)

fullHeight ::
   (Extent.C vert, Extent.C horiz) => Full vert horiz height width -> height
fullHeight :: Full vert horiz height width -> height
fullHeight = Extent vert horiz height width -> height
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> height
Extent.height (Extent vert horiz height width -> height)
-> (Full vert horiz height width -> Extent vert horiz height width)
-> Full vert horiz height width
-> height
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Full vert horiz height width -> Extent vert horiz height width
forall vert horiz height width.
Full vert horiz height width -> Extent vert horiz height width
fullExtent

fullWidth ::
   (Extent.C vert, Extent.C horiz) => Full vert horiz height width -> width
fullWidth :: Full vert horiz height width -> width
fullWidth = Extent vert horiz height width -> width
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> width
Extent.width (Extent vert horiz height width -> width)
-> (Full vert horiz height width -> Extent vert horiz height width)
-> Full vert horiz height width
-> width
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Full vert horiz height width -> Extent vert horiz height width
forall vert horiz height width.
Full vert horiz height width -> Extent vert horiz height width
fullExtent


fullIndices ::
   (Extent.C vert, Extent.C horiz, Shape.Indexed a, Shape.Indexed b) =>
   Order -> Extent vert horiz a b -> [(Shape.Index a, Shape.Index b)]
fullIndices :: Order -> Extent vert horiz a b -> [(Index a, Index b)]
fullIndices Order
order Extent vert horiz a b
extent =
   case Order
order of
      Order
RowMajor -> (a, b) -> [Index (a, b)]
forall sh. Indexed sh => sh -> [Index sh]
Shape.indices ((a, b) -> [Index (a, b)]) -> (a, b) -> [Index (a, b)]
forall a b. (a -> b) -> a -> b
$ Extent vert horiz a b -> (a, b)
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> (height, width)
Extent.dimensions Extent vert horiz a b
extent
      Order
ColumnMajor -> ((Index b, Index a) -> (Index a, Index b))
-> [(Index b, Index a)] -> [(Index a, Index b)]
forall a b. (a -> b) -> [a] -> [b]
map (Index b, Index a) -> (Index a, Index b)
forall a b. (a, b) -> (b, a)
swap ([(Index b, Index a)] -> [(Index a, Index b)])
-> [(Index b, Index a)] -> [(Index a, Index b)]
forall a b. (a -> b) -> a -> b
$ (b, a) -> [Index (b, a)]
forall sh. Indexed sh => sh -> [Index sh]
Shape.indices ((b, a) -> [Index (b, a)]) -> (b, a) -> [Index (b, a)]
forall a b. (a -> b) -> a -> b
$ (a, b) -> (b, a)
forall a b. (a, b) -> (b, a)
swap ((a, b) -> (b, a)) -> (a, b) -> (b, a)
forall a b. (a -> b) -> a -> b
$ Extent vert horiz a b -> (a, b)
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> (height, width)
Extent.dimensions Extent vert horiz a b
extent

fullIndexFromOffset ::
   (Extent.C vert, Extent.C horiz, Shape.InvIndexed a, Shape.InvIndexed b) =>
   Order -> Extent vert horiz a b -> Int ->
   (Shape.Index a, Shape.Index b)
fullIndexFromOffset :: Order -> Extent vert horiz a b -> Int -> (Index a, Index b)
fullIndexFromOffset Order
order Extent vert horiz a b
extent =
   case Order
order of
      Order
RowMajor ->
         (a, b) -> Int -> Index (a, b)
forall sh. InvIndexed sh => sh -> Int -> Index sh
Shape.indexFromOffset (Extent vert horiz a b -> (a, b)
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> (height, width)
Extent.dimensions Extent vert horiz a b
extent)
      Order
ColumnMajor ->
         (Index b, Index a) -> (Index a, Index b)
forall a b. (a, b) -> (b, a)
swap ((Index b, Index a) -> (Index a, Index b))
-> (Int -> (Index b, Index a)) -> Int -> (Index a, Index b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (b, a) -> Int -> Index (b, a)
forall sh. InvIndexed sh => sh -> Int -> Index sh
Shape.indexFromOffset ((a, b) -> (b, a)
forall a b. (a, b) -> (b, a)
swap ((a, b) -> (b, a)) -> (a, b) -> (b, a)
forall a b. (a -> b) -> a -> b
$ Extent vert horiz a b -> (a, b)
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> (height, width)
Extent.dimensions Extent vert horiz a b
extent)


type General height width = Full Extent.Big Extent.Big height width
type Tall height width = Full Extent.Big Extent.Small height width
type Wide height width = Full Extent.Small Extent.Big height width
type Square size = Full Extent.Small Extent.Small size size


fullMapExtent ::
   Extent.Map vertA horizA vertB horizB height width ->
   Full vertA horizA height width ->
   Full vertB horizB height width
fullMapExtent :: Map vertA horizA vertB horizB height width
-> Full vertA horizA height width -> Full vertB horizB height width
fullMapExtent Map vertA horizA vertB horizB height width
f (Full Order
order Extent vertA horizA height width
extent) = Order
-> Extent vertB horizB height width
-> Full vertB horizB height width
forall vert horiz height width.
Order
-> Extent vert horiz height width -> Full vert horiz height width
Full Order
order (Extent vertB horizB height width
 -> Full vertB horizB height width)
-> Extent vertB horizB height width
-> Full vertB horizB height width
forall a b. (a -> b) -> a -> b
$ Map vertA horizA vertB horizB height width
-> Extent vertA horizA height width
-> Extent vertB horizB height width
forall vertA horizA vertB horizB height width.
Map vertA horizA vertB horizB height width
-> Extent vertA horizA height width
-> Extent vertB horizB height width
Extent.apply Map vertA horizA vertB horizB height width
f Extent vertA horizA height width
extent

general :: Order -> height -> width -> General height width
general :: Order -> height -> width -> General height width
general Order
order height
height width
width = Order -> Extent Big Big height width -> General height width
forall vert horiz height width.
Order
-> Extent vert horiz height width -> Full vert horiz height width
Full Order
order (Extent Big Big height width -> General height width)
-> Extent Big Big height width -> General height width
forall a b. (a -> b) -> a -> b
$ height -> width -> Extent Big Big height width
forall height width. height -> width -> General height width
Extent.general height
height width
width

tall ::
   (Shape.C height, Shape.C width) =>
   Order -> height -> width -> Tall height width
tall :: Order -> height -> width -> Tall height width
tall Order
order height
height width
width =
   if height -> Int
forall sh. C sh => sh -> Int
Shape.size height
height Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= width -> Int
forall sh. C sh => sh -> Int
Shape.size width
width
      then Order -> Extent Big Small height width -> Tall height width
forall vert horiz height width.
Order
-> Extent vert horiz height width -> Full vert horiz height width
Full Order
order (Extent Big Small height width -> Tall height width)
-> Extent Big Small height width -> Tall height width
forall a b. (a -> b) -> a -> b
$ height -> width -> Extent Big Small height width
forall height width. height -> width -> Tall height width
Extent.tall height
height width
width
      else String -> Tall height width
forall a. HasCallStack => String -> a
error String
"MatrixShape.tall: height smaller than width"

wide ::
   (Shape.C height, Shape.C width) =>
   Order -> height -> width -> Wide height width
wide :: Order -> height -> width -> Wide height width
wide Order
order height
height width
width =
   if height -> Int
forall sh. C sh => sh -> Int
Shape.size height
height Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= width -> Int
forall sh. C sh => sh -> Int
Shape.size width
width
      then Order -> Extent Small Big height width -> Wide height width
forall vert horiz height width.
Order
-> Extent vert horiz height width -> Full vert horiz height width
Full Order
order (Extent Small Big height width -> Wide height width)
-> Extent Small Big height width -> Wide height width
forall a b. (a -> b) -> a -> b
$ height -> width -> Extent Small Big height width
forall height width. height -> width -> Wide height width
Extent.wide height
height width
width
      else String -> Wide height width
forall a. HasCallStack => String -> a
error String
"MatrixShape.wide: width smaller than height"

square :: Order -> sh -> Square sh
square :: Order -> sh -> Square sh
square Order
order sh
sh = Order -> Extent Small Small sh sh -> Square sh
forall vert horiz height width.
Order
-> Extent vert horiz height width -> Full vert horiz height width
Full Order
order (Extent Small Small sh sh -> Square sh)
-> Extent Small Small sh sh -> Square sh
forall a b. (a -> b) -> a -> b
$ sh -> Extent Small Small sh sh
forall sh. sh -> Square sh
Extent.square sh
sh


caseTallWide ::
   (Extent.C vert, Extent.C horiz, Shape.C height, Shape.C width) =>
   Full vert horiz height width ->
   Either (Tall height width) (Wide height width)
caseTallWide :: Full vert horiz height width
-> Either (Tall height width) (Wide height width)
caseTallWide (Full Order
order Extent vert horiz height width
extent) =
   (Extent Big Small height width
 -> Either (Tall height width) (Wide height width))
-> (Extent Small Big height width
    -> Either (Tall height width) (Wide height width))
-> Either
     (Extent Big Small height width) (Extent Small Big height width)
-> Either (Tall height width) (Wide height width)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Tall height width -> Either (Tall height width) (Wide height width)
forall a b. a -> Either a b
Left (Tall height width
 -> Either (Tall height width) (Wide height width))
-> (Extent Big Small height width -> Tall height width)
-> Extent Big Small height width
-> Either (Tall height width) (Wide height width)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Order -> Extent Big Small height width -> Tall height width
forall vert horiz height width.
Order
-> Extent vert horiz height width -> Full vert horiz height width
Full Order
order) (Wide height width -> Either (Tall height width) (Wide height width)
forall a b. b -> Either a b
Right (Wide height width
 -> Either (Tall height width) (Wide height width))
-> (Extent Small Big height width -> Wide height width)
-> Extent Small Big height width
-> Either (Tall height width) (Wide height width)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Order -> Extent Small Big height width -> Wide height width
forall vert horiz height width.
Order
-> Extent vert horiz height width -> Full vert horiz height width
Full Order
order) (Either
   (Extent Big Small height width) (Extent Small Big height width)
 -> Either (Tall height width) (Wide height width))
-> Either
     (Extent Big Small height width) (Extent Small Big height width)
-> Either (Tall height width) (Wide height width)
forall a b. (a -> b) -> a -> b
$
   (height -> width -> Bool)
-> Extent vert horiz height width
-> Either
     (Extent Big Small height width) (Extent Small Big height width)
forall vert horiz height width.
(C vert, C horiz) =>
(height -> width -> Bool)
-> Extent vert horiz height width
-> Either (Tall height width) (Wide height width)
Extent.caseTallWide (\height
h width
w -> height -> Int
forall sh. C sh => sh -> Int
Shape.size height
h Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= width -> Int
forall sh. C sh => sh -> Int
Shape.size width
w) Extent vert horiz height width
extent


data Split lower vert horiz height width =
   Split {
      Split lower vert horiz height width -> lower
splitLower :: lower,
      Split lower vert horiz height width -> Order
splitOrder :: Order,
      Split lower vert horiz height width
-> Extent vert horiz height width
splitExtent :: Extent vert horiz height width
   } deriving (Split lower vert horiz height width
-> Split lower vert horiz height width -> Bool
(Split lower vert horiz height width
 -> Split lower vert horiz height width -> Bool)
-> (Split lower vert horiz height width
    -> Split lower vert horiz height width -> Bool)
-> Eq (Split lower vert horiz height width)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall lower vert horiz height width.
(C vert, C horiz, Eq lower, Eq height, Eq width) =>
Split lower vert horiz height width
-> Split lower vert horiz height width -> Bool
/= :: Split lower vert horiz height width
-> Split lower vert horiz height width -> Bool
$c/= :: forall lower vert horiz height width.
(C vert, C horiz, Eq lower, Eq height, Eq width) =>
Split lower vert horiz height width
-> Split lower vert horiz height width -> Bool
== :: Split lower vert horiz height width
-> Split lower vert horiz height width -> Bool
$c== :: forall lower vert horiz height width.
(C vert, C horiz, Eq lower, Eq height, Eq width) =>
Split lower vert horiz height width
-> Split lower vert horiz height width -> Bool
Eq, Int -> Split lower vert horiz height width -> ShowS
[Split lower vert horiz height width] -> ShowS
Split lower vert horiz height width -> String
(Int -> Split lower vert horiz height width -> ShowS)
-> (Split lower vert horiz height width -> String)
-> ([Split lower vert horiz height width] -> ShowS)
-> Show (Split lower vert horiz height width)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall lower vert horiz height width.
(C vert, C horiz, Show lower, Show height, Show width) =>
Int -> Split lower vert horiz height width -> ShowS
forall lower vert horiz height width.
(C vert, C horiz, Show lower, Show height, Show width) =>
[Split lower vert horiz height width] -> ShowS
forall lower vert horiz height width.
(C vert, C horiz, Show lower, Show height, Show width) =>
Split lower vert horiz height width -> String
showList :: [Split lower vert horiz height width] -> ShowS
$cshowList :: forall lower vert horiz height width.
(C vert, C horiz, Show lower, Show height, Show width) =>
[Split lower vert horiz height width] -> ShowS
show :: Split lower vert horiz height width -> String
$cshow :: forall lower vert horiz height width.
(C vert, C horiz, Show lower, Show height, Show width) =>
Split lower vert horiz height width -> String
showsPrec :: Int -> Split lower vert horiz height width -> ShowS
$cshowsPrec :: forall lower vert horiz height width.
(C vert, C horiz, Show lower, Show height, Show width) =>
Int -> Split lower vert horiz height width -> ShowS
Show)

splitHeight ::
   (Extent.C vert, Extent.C horiz) =>
   Split lower vert horiz height width -> height
splitHeight :: Split lower vert horiz height width -> height
splitHeight = Extent vert horiz height width -> height
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> height
Extent.height (Extent vert horiz height width -> height)
-> (Split lower vert horiz height width
    -> Extent vert horiz height width)
-> Split lower vert horiz height width
-> height
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Split lower vert horiz height width
-> Extent vert horiz height width
forall lower vert horiz height width.
Split lower vert horiz height width
-> Extent vert horiz height width
splitExtent

splitWidth ::
   (Extent.C vert, Extent.C horiz) =>
   Split lower vert horiz height width -> width
splitWidth :: Split lower vert horiz height width -> width
splitWidth = Extent vert horiz height width -> width
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> width
Extent.width (Extent vert horiz height width -> width)
-> (Split lower vert horiz height width
    -> Extent vert horiz height width)
-> Split lower vert horiz height width
-> width
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Split lower vert horiz height width
-> Extent vert horiz height width
forall lower vert horiz height width.
Split lower vert horiz height width
-> Extent vert horiz height width
splitExtent

splitMapExtent ::
   Extent.Map vertA horizA vertB horizB height width ->
   Split lower vertA horizA height width ->
   Split lower vertB horizB height width
splitMapExtent :: Map vertA horizA vertB horizB height width
-> Split lower vertA horizA height width
-> Split lower vertB horizB height width
splitMapExtent Map vertA horizA vertB horizB height width
f (Split lower
lowerPart Order
order Extent vertA horizA height width
extent) =
   lower
-> Order
-> Extent vertB horizB height width
-> Split lower vertB horizB height width
forall lower vert horiz height width.
lower
-> Order
-> Extent vert horiz height width
-> Split lower vert horiz height width
Split lower
lowerPart Order
order (Extent vertB horizB height width
 -> Split lower vertB horizB height width)
-> Extent vertB horizB height width
-> Split lower vertB horizB height width
forall a b. (a -> b) -> a -> b
$ Map vertA horizA vertB horizB height width
-> Extent vertA horizA height width
-> Extent vertB horizB height width
forall vertA horizA vertB horizB height width.
Map vertA horizA vertB horizB height width
-> Extent vertA horizA height width
-> Extent vertB horizB height width
Extent.apply Map vertA horizA vertB horizB height width
f Extent vertA horizA height width
extent


caseTallWideSplit ::
   (Extent.C vert, Extent.C horiz, Shape.C height, Shape.C width) =>
   Split lower vert horiz height width ->
   Either
      (Split lower Extent.Big Extent.Small height width)
      (Split lower Extent.Small Extent.Big height width)
caseTallWideSplit :: Split lower vert horiz height width
-> Either
     (Split lower Big Small height width)
     (Split lower Small Big height width)
caseTallWideSplit (Split lower
lowerPart Order
order Extent vert horiz height width
extent) =
   (Extent Big Small height width
 -> Either
      (Split lower Big Small height width)
      (Split lower Small Big height width))
-> (Extent Small Big height width
    -> Either
         (Split lower Big Small height width)
         (Split lower Small Big height width))
-> Either
     (Extent Big Small height width) (Extent Small Big height width)
-> Either
     (Split lower Big Small height width)
     (Split lower Small Big height width)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Split lower Big Small height width
-> Either
     (Split lower Big Small height width)
     (Split lower Small Big height width)
forall a b. a -> Either a b
Left (Split lower Big Small height width
 -> Either
      (Split lower Big Small height width)
      (Split lower Small Big height width))
-> (Extent Big Small height width
    -> Split lower Big Small height width)
-> Extent Big Small height width
-> Either
     (Split lower Big Small height width)
     (Split lower Small Big height width)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. lower
-> Order
-> Extent Big Small height width
-> Split lower Big Small height width
forall lower vert horiz height width.
lower
-> Order
-> Extent vert horiz height width
-> Split lower vert horiz height width
Split lower
lowerPart Order
order) (Split lower Small Big height width
-> Either
     (Split lower Big Small height width)
     (Split lower Small Big height width)
forall a b. b -> Either a b
Right (Split lower Small Big height width
 -> Either
      (Split lower Big Small height width)
      (Split lower Small Big height width))
-> (Extent Small Big height width
    -> Split lower Small Big height width)
-> Extent Small Big height width
-> Either
     (Split lower Big Small height width)
     (Split lower Small Big height width)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. lower
-> Order
-> Extent Small Big height width
-> Split lower Small Big height width
forall lower vert horiz height width.
lower
-> Order
-> Extent vert horiz height width
-> Split lower vert horiz height width
Split lower
lowerPart Order
order) (Either
   (Extent Big Small height width) (Extent Small Big height width)
 -> Either
      (Split lower Big Small height width)
      (Split lower Small Big height width))
-> Either
     (Extent Big Small height width) (Extent Small Big height width)
-> Either
     (Split lower Big Small height width)
     (Split lower Small Big height width)
forall a b. (a -> b) -> a -> b
$
   (height -> width -> Bool)
-> Extent vert horiz height width
-> Either
     (Extent Big Small height width) (Extent Small Big height width)
forall vert horiz height width.
(C vert, C horiz) =>
(height -> width -> Bool)
-> Extent vert horiz height width
-> Either (Tall height width) (Wide height width)
Extent.caseTallWide (\height
h width
w -> height -> Int
forall sh. C sh => sh -> Int
Shape.size height
h Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= width -> Int
forall sh. C sh => sh -> Int
Shape.size width
w) Extent vert horiz height width
extent

instance
   (Eq lower, Extent.C vert, Extent.C horiz, Shape.C height, Shape.C width) =>
      Box.Box (Split lower vert horiz height width) where
   type HeightOf (Split lower vert horiz height width) = height
   type WidthOf (Split lower vert horiz height width) = width
   height :: Split lower vert horiz height width
-> HeightOf (Split lower vert horiz height width)
height = Split lower vert horiz height width
-> HeightOf (Split lower vert horiz height width)
forall vert horiz lower height width.
(C vert, C horiz) =>
Split lower vert horiz height width -> height
splitHeight
   width :: Split lower vert horiz height width
-> WidthOf (Split lower vert horiz height width)
width = Split lower vert horiz height width
-> WidthOf (Split lower vert horiz height width)
forall vert horiz lower height width.
(C vert, C horiz) =>
Split lower vert horiz height width -> width
splitWidth

data Reflector = Reflector deriving (Reflector -> Reflector -> Bool
(Reflector -> Reflector -> Bool)
-> (Reflector -> Reflector -> Bool) -> Eq Reflector
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Reflector -> Reflector -> Bool
$c/= :: Reflector -> Reflector -> Bool
== :: Reflector -> Reflector -> Bool
$c== :: Reflector -> Reflector -> Bool
Eq, Int -> Reflector -> ShowS
[Reflector] -> ShowS
Reflector -> String
(Int -> Reflector -> ShowS)
-> (Reflector -> String)
-> ([Reflector] -> ShowS)
-> Show Reflector
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Reflector] -> ShowS
$cshowList :: [Reflector] -> ShowS
show :: Reflector -> String
$cshow :: Reflector -> String
showsPrec :: Int -> Reflector -> ShowS
$cshowsPrec :: Int -> Reflector -> ShowS
Show)
data Triangle = Triangle deriving (Triangle -> Triangle -> Bool
(Triangle -> Triangle -> Bool)
-> (Triangle -> Triangle -> Bool) -> Eq Triangle
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Triangle -> Triangle -> Bool
$c/= :: Triangle -> Triangle -> Bool
== :: Triangle -> Triangle -> Bool
$c== :: Triangle -> Triangle -> Bool
Eq, Int -> Triangle -> ShowS
[Triangle] -> ShowS
Triangle -> String
(Int -> Triangle -> ShowS)
-> (Triangle -> String) -> ([Triangle] -> ShowS) -> Show Triangle
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Triangle] -> ShowS
$cshowList :: [Triangle] -> ShowS
show :: Triangle -> String
$cshow :: Triangle -> String
showsPrec :: Int -> Triangle -> ShowS
$cshowsPrec :: Int -> Triangle -> ShowS
Show)

instance NFData Reflector where rnf :: Reflector -> ()
rnf Reflector
Reflector = ()
instance NFData Triangle where rnf :: Triangle -> ()
rnf Triangle
Triangle = ()

splitPart ::
   (Extent.C vert, Extent.C horiz,
    Shape.Indexed height, Shape.Indexed width) =>
   Split lower vert horiz height width ->
   (Shape.Index height, Shape.Index width) -> Either lower Triangle
splitPart :: Split lower vert horiz height width
-> (Index height, Index width) -> Either lower Triangle
splitPart (Split lower
lowerPart Order
_ Extent vert horiz height width
extent) (Index height
r,Index width
c) =
   if height -> Index height -> Int
forall sh. Indexed sh => sh -> Index sh -> Int
Shape.offset (Extent vert horiz height width -> height
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> height
Extent.height Extent vert horiz height width
extent) Index height
r Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>
         width -> Index width -> Int
forall sh. Indexed sh => sh -> Index sh -> Int
Shape.offset (Extent vert horiz height width -> width
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> width
Extent.width Extent vert horiz height width
extent) Index width
c
     then lower -> Either lower Triangle
forall a b. a -> Either a b
Left lower
lowerPart
     else Triangle -> Either lower Triangle
forall a b. b -> Either a b
Right Triangle
Triangle

instance
   (NFData lower, Extent.C vert, Extent.C horiz, NFData height, NFData width) =>
      NFData (Split lower vert horiz height width) where
   rnf :: Split lower vert horiz height width -> ()
rnf (Split lower
lowerPart Order
order Extent vert horiz height width
extent) = (lower, Order, Extent vert horiz height width) -> ()
forall a. NFData a => a -> ()
rnf (lower
lowerPart, Order
order, Extent vert horiz height width
extent)

instance
   (Eq lower, Extent.C vert, Extent.C horiz, Shape.C height, Shape.C width) =>
      Shape.C (Split lower vert horiz height width) where

   size :: Split lower vert horiz height width -> Int
size (Split lower
_ Order
_ Extent vert horiz height width
extent) = (height, width) -> Int
forall sh. C sh => sh -> Int
Shape.size (Extent vert horiz height width -> (height, width)
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> (height, width)
Extent.dimensions Extent vert horiz height width
extent)
   uncheckedSize :: Split lower vert horiz height width -> Int
uncheckedSize (Split lower
_ Order
_ Extent vert horiz height width
extent) =
      (height, width) -> Int
forall sh. C sh => sh -> Int
Shape.uncheckedSize (Extent vert horiz height width -> (height, width)
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> (height, width)
Extent.dimensions Extent vert horiz height width
extent)

instance
   (Eq lower, Extent.C vert, Extent.C horiz,
    Shape.Indexed height, Shape.Indexed width) =>
      Shape.Indexed (Split lower vert horiz height width) where

   type Index (Split lower vert horiz height width) =
            (Either lower Triangle,
             (Shape.Index height, Shape.Index width))

   indices :: Split lower vert horiz height width
-> [Index (Split lower vert horiz height width)]
indices sh :: Split lower vert horiz height width
sh@(Split lower
_ Order
order Extent vert horiz height width
extent) =
      ((Index height, Index width)
 -> (Either lower Triangle, (Index height, Index width)))
-> [(Index height, Index width)]
-> [(Either lower Triangle, (Index height, Index width))]
forall a b. (a -> b) -> [a] -> [b]
map (\(Index height, Index width)
ix -> (Split lower vert horiz height width
-> (Index height, Index width) -> Either lower Triangle
forall vert horiz height width lower.
(C vert, C horiz, Indexed height, Indexed width) =>
Split lower vert horiz height width
-> (Index height, Index width) -> Either lower Triangle
splitPart Split lower vert horiz height width
sh (Index height, Index width)
ix, (Index height, Index width)
ix)) ([(Index height, Index width)]
 -> [(Either lower Triangle, (Index height, Index width))])
-> [(Index height, Index width)]
-> [(Either lower Triangle, (Index height, Index width))]
forall a b. (a -> b) -> a -> b
$ Order
-> Extent vert horiz height width -> [(Index height, Index width)]
forall vert horiz a b.
(C vert, C horiz, Indexed a, Indexed b) =>
Order -> Extent vert horiz a b -> [(Index a, Index b)]
fullIndices Order
order Extent vert horiz height width
extent

   offset :: Split lower vert horiz height width
-> Index (Split lower vert horiz height width) -> Int
offset sh :: Split lower vert horiz height width
sh@(Split lower
_ Order
order Extent vert horiz height width
extent) (part,ix) =
      if Either lower Triangle
part Either lower Triangle -> Either lower Triangle -> Bool
forall a. Eq a => a -> a -> Bool
== Split lower vert horiz height width
-> (Index height, Index width) -> Either lower Triangle
forall vert horiz height width lower.
(C vert, C horiz, Indexed height, Indexed width) =>
Split lower vert horiz height width
-> (Index height, Index width) -> Either lower Triangle
splitPart Split lower vert horiz height width
sh (Index height, Index width)
ix
        then
            case Order
order of
               Order
RowMajor -> (height, width) -> Index (height, width) -> Int
forall sh. Indexed sh => sh -> Index sh -> Int
Shape.offset (Extent vert horiz height width -> (height, width)
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> (height, width)
Extent.dimensions Extent vert horiz height width
extent) (Index height, Index width)
Index (height, width)
ix
               Order
ColumnMajor ->
                  (width, height) -> Index (width, height) -> Int
forall sh. Indexed sh => sh -> Index sh -> Int
Shape.offset ((height, width) -> (width, height)
forall a b. (a, b) -> (b, a)
swap ((height, width) -> (width, height))
-> (height, width) -> (width, height)
forall a b. (a -> b) -> a -> b
$ Extent vert horiz height width -> (height, width)
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> (height, width)
Extent.dimensions Extent vert horiz height width
extent) ((Index height, Index width) -> (Index width, Index height)
forall a b. (a, b) -> (b, a)
swap (Index height, Index width)
ix)
        else String -> Int
forall a. HasCallStack => String -> a
error String
"Shape.Split.offset: wrong matrix part"
   uncheckedOffset :: Split lower vert horiz height width
-> Index (Split lower vert horiz height width) -> Int
uncheckedOffset (Split lower
_ Order
RowMajor Extent vert horiz height width
extent) =
      (height, width) -> Index (height, width) -> Int
forall sh. Indexed sh => sh -> Index sh -> Int
Shape.uncheckedOffset (Extent vert horiz height width -> (height, width)
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> (height, width)
Extent.dimensions Extent vert horiz height width
extent) ((Index height, Index width) -> Int)
-> ((Either lower Triangle, (Index height, Index width))
    -> (Index height, Index width))
-> (Either lower Triangle, (Index height, Index width))
-> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Either lower Triangle, (Index height, Index width))
-> (Index height, Index width)
forall a b. (a, b) -> b
snd
   uncheckedOffset (Split lower
_ Order
ColumnMajor Extent vert horiz height width
extent) =
      (width, height) -> Index (width, height) -> Int
forall sh. Indexed sh => sh -> Index sh -> Int
Shape.uncheckedOffset ((height, width) -> (width, height)
forall a b. (a, b) -> (b, a)
swap ((height, width) -> (width, height))
-> (height, width) -> (width, height)
forall a b. (a -> b) -> a -> b
$ Extent vert horiz height width -> (height, width)
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> (height, width)
Extent.dimensions Extent vert horiz height width
extent) ((Index width, Index height) -> Int)
-> ((Either lower Triangle, (Index height, Index width))
    -> (Index width, Index height))
-> (Either lower Triangle, (Index height, Index width))
-> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Index height, Index width) -> (Index width, Index height)
forall a b. (a, b) -> (b, a)
swap ((Index height, Index width) -> (Index width, Index height))
-> ((Either lower Triangle, (Index height, Index width))
    -> (Index height, Index width))
-> (Either lower Triangle, (Index height, Index width))
-> (Index width, Index height)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Either lower Triangle, (Index height, Index width))
-> (Index height, Index width)
forall a b. (a, b) -> b
snd

   sizeOffset :: Split lower vert horiz height width
-> (Int, Index (Split lower vert horiz height width) -> Int)
sizeOffset sh :: Split lower vert horiz height width
sh@(Split lower
_ Order
order Extent vert horiz height width
extent) =
      let check :: (Either lower Triangle, (Index height, Index width)) -> Int -> Int
check (Either lower Triangle
part,(Index height, Index width)
ix) Int
a =
            if Either lower Triangle
part Either lower Triangle -> Either lower Triangle -> Bool
forall a. Eq a => a -> a -> Bool
== Split lower vert horiz height width
-> (Index height, Index width) -> Either lower Triangle
forall vert horiz height width lower.
(C vert, C horiz, Indexed height, Indexed width) =>
Split lower vert horiz height width
-> (Index height, Index width) -> Either lower Triangle
splitPart Split lower vert horiz height width
sh (Index height, Index width)
ix
              then Int
a
              else String -> Int
forall a. HasCallStack => String -> a
error String
"Shape.Split.sizeOffset: wrong matrix part"
      in case Order
order of
            Order
RowMajor ->
               (((Index height, Index width) -> Int)
 -> (Either lower Triangle, (Index height, Index width)) -> Int)
-> (Int, (Index height, Index width) -> Int)
-> (Int,
    (Either lower Triangle, (Index height, Index width)) -> Int)
forall b c a. (b -> c) -> (a, b) -> (a, c)
mapSnd (\(Index height, Index width) -> Int
getOffset (Either lower Triangle
part,(Index height, Index width)
ix) -> (Either lower Triangle, (Index height, Index width)) -> Int -> Int
check (Either lower Triangle
part,(Index height, Index width)
ix) (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ (Index height, Index width) -> Int
getOffset (Index height, Index width)
ix) ((Int, (Index height, Index width) -> Int)
 -> (Int,
     (Either lower Triangle, (Index height, Index width)) -> Int))
-> (Int, (Index height, Index width) -> Int)
-> (Int,
    (Either lower Triangle, (Index height, Index width)) -> Int)
forall a b. (a -> b) -> a -> b
$
               (height, width) -> (Int, Index (height, width) -> Int)
forall sh. Indexed sh => sh -> (Int, Index sh -> Int)
Shape.sizeOffset (Extent vert horiz height width -> (height, width)
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> (height, width)
Extent.dimensions Extent vert horiz height width
extent)
            Order
ColumnMajor ->
               (((Index width, Index height) -> Int)
 -> (Either lower Triangle, (Index height, Index width)) -> Int)
-> (Int, (Index width, Index height) -> Int)
-> (Int,
    (Either lower Triangle, (Index height, Index width)) -> Int)
forall b c a. (b -> c) -> (a, b) -> (a, c)
mapSnd
                  (\(Index width, Index height) -> Int
getOffset (Either lower Triangle
part,(Index height, Index width)
ix) ->
                     (Either lower Triangle, (Index height, Index width)) -> Int -> Int
check (Either lower Triangle
part,(Index height, Index width)
ix) (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ (Index width, Index height) -> Int
getOffset ((Index width, Index height) -> Int)
-> (Index width, Index height) -> Int
forall a b. (a -> b) -> a -> b
$ (Index height, Index width) -> (Index width, Index height)
forall a b. (a, b) -> (b, a)
swap (Index height, Index width)
ix) ((Int, (Index width, Index height) -> Int)
 -> (Int,
     (Either lower Triangle, (Index height, Index width)) -> Int))
-> (Int, (Index width, Index height) -> Int)
-> (Int,
    (Either lower Triangle, (Index height, Index width)) -> Int)
forall a b. (a -> b) -> a -> b
$
               (width, height) -> (Int, Index (width, height) -> Int)
forall sh. Indexed sh => sh -> (Int, Index sh -> Int)
Shape.sizeOffset ((height, width) -> (width, height)
forall a b. (a, b) -> (b, a)
swap ((height, width) -> (width, height))
-> (height, width) -> (width, height)
forall a b. (a -> b) -> a -> b
$ Extent vert horiz height width -> (height, width)
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> (height, width)
Extent.dimensions Extent vert horiz height width
extent)
   uncheckedSizeOffset :: Split lower vert horiz height width
-> (Int, Index (Split lower vert horiz height width) -> Int)
uncheckedSizeOffset (Split lower
_ Order
RowMajor Extent vert horiz height width
extent) =
      (((Index height, Index width) -> Int)
 -> (Either lower Triangle, (Index height, Index width)) -> Int)
-> (Int, (Index height, Index width) -> Int)
-> (Int,
    (Either lower Triangle, (Index height, Index width)) -> Int)
forall b c a. (b -> c) -> (a, b) -> (a, c)
mapSnd (((Index height, Index width) -> Int)
-> ((Either lower Triangle, (Index height, Index width))
    -> (Index height, Index width))
-> (Either lower Triangle, (Index height, Index width))
-> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Either lower Triangle, (Index height, Index width))
-> (Index height, Index width)
forall a b. (a, b) -> b
snd) ((Int, (Index height, Index width) -> Int)
 -> (Int,
     (Either lower Triangle, (Index height, Index width)) -> Int))
-> (Int, (Index height, Index width) -> Int)
-> (Int,
    (Either lower Triangle, (Index height, Index width)) -> Int)
forall a b. (a -> b) -> a -> b
$ (height, width) -> (Int, Index (height, width) -> Int)
forall sh. Indexed sh => sh -> (Int, Index sh -> Int)
Shape.uncheckedSizeOffset (Extent vert horiz height width -> (height, width)
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> (height, width)
Extent.dimensions Extent vert horiz height width
extent)
   uncheckedSizeOffset (Split lower
_ Order
ColumnMajor Extent vert horiz height width
extent) =
      (((Index width, Index height) -> Int)
 -> (Either lower Triangle, (Index height, Index width)) -> Int)
-> (Int, (Index width, Index height) -> Int)
-> (Int,
    (Either lower Triangle, (Index height, Index width)) -> Int)
forall b c a. (b -> c) -> (a, b) -> (a, c)
mapSnd (((Index width, Index height) -> Int)
-> ((Either lower Triangle, (Index height, Index width))
    -> (Index width, Index height))
-> (Either lower Triangle, (Index height, Index width))
-> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Index height, Index width) -> (Index width, Index height)
forall a b. (a, b) -> (b, a)
swap((Index height, Index width) -> (Index width, Index height))
-> ((Either lower Triangle, (Index height, Index width))
    -> (Index height, Index width))
-> (Either lower Triangle, (Index height, Index width))
-> (Index width, Index height)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Either lower Triangle, (Index height, Index width))
-> (Index height, Index width)
forall a b. (a, b) -> b
snd) ((Int, (Index width, Index height) -> Int)
 -> (Int,
     (Either lower Triangle, (Index height, Index width)) -> Int))
-> (Int, (Index width, Index height) -> Int)
-> (Int,
    (Either lower Triangle, (Index height, Index width)) -> Int)
forall a b. (a -> b) -> a -> b
$
      (width, height) -> (Int, Index (width, height) -> Int)
forall sh. Indexed sh => sh -> (Int, Index sh -> Int)
Shape.uncheckedSizeOffset ((height, width) -> (width, height)
forall a b. (a, b) -> (b, a)
swap ((height, width) -> (width, height))
-> (height, width) -> (width, height)
forall a b. (a -> b) -> a -> b
$ Extent vert horiz height width -> (height, width)
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> (height, width)
Extent.dimensions Extent vert horiz height width
extent)

   inBounds :: Split lower vert horiz height width
-> Index (Split lower vert horiz height width) -> Bool
inBounds sh :: Split lower vert horiz height width
sh@(Split lower
_ Order
_ Extent vert horiz height width
extent) (part,ix) =
      (height, width) -> Index (height, width) -> Bool
forall sh. Indexed sh => sh -> Index sh -> Bool
Shape.inBounds (Extent vert horiz height width -> (height, width)
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> (height, width)
Extent.dimensions Extent vert horiz height width
extent) (Index height, Index width)
Index (height, width)
ix
      Bool -> Bool -> Bool
&&
      Either lower Triangle
part Either lower Triangle -> Either lower Triangle -> Bool
forall a. Eq a => a -> a -> Bool
== Split lower vert horiz height width
-> (Index height, Index width) -> Either lower Triangle
forall vert horiz height width lower.
(C vert, C horiz, Indexed height, Indexed width) =>
Split lower vert horiz height width
-> (Index height, Index width) -> Either lower Triangle
splitPart Split lower vert horiz height width
sh (Index height, Index width)
ix

instance
   (Eq lower, Extent.C vert, Extent.C horiz,
    Shape.InvIndexed height, Shape.InvIndexed width) =>
      Shape.InvIndexed (Split lower vert horiz height width) where

   indexFromOffset :: Split lower vert horiz height width
-> Int -> Index (Split lower vert horiz height width)
indexFromOffset sh :: Split lower vert horiz height width
sh@(Split lower
_ Order
order Extent vert horiz height width
extent) Int
k =
      let ix :: (Index height, Index width)
ix = Order
-> Extent vert horiz height width
-> Int
-> (Index height, Index width)
forall vert horiz a b.
(C vert, C horiz, InvIndexed a, InvIndexed b) =>
Order -> Extent vert horiz a b -> Int -> (Index a, Index b)
fullIndexFromOffset Order
order Extent vert horiz height width
extent Int
k
      in (Split lower vert horiz height width
-> (Index height, Index width) -> Either lower Triangle
forall vert horiz height width lower.
(C vert, C horiz, Indexed height, Indexed width) =>
Split lower vert horiz height width
-> (Index height, Index width) -> Either lower Triangle
splitPart Split lower vert horiz height width
sh (Index height, Index width)
ix, (Index height, Index width)
ix)


{- |
Store the upper triangular half of a real symmetric or complex Hermitian matrix.
-}
data Hermitian size =
   Hermitian {
      Hermitian size -> Order
hermitianOrder :: Order,
      Hermitian size -> size
hermitianSize :: size
   } deriving (Hermitian size -> Hermitian size -> Bool
(Hermitian size -> Hermitian size -> Bool)
-> (Hermitian size -> Hermitian size -> Bool)
-> Eq (Hermitian size)
forall size. Eq size => Hermitian size -> Hermitian size -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Hermitian size -> Hermitian size -> Bool
$c/= :: forall size. Eq size => Hermitian size -> Hermitian size -> Bool
== :: Hermitian size -> Hermitian size -> Bool
$c== :: forall size. Eq size => Hermitian size -> Hermitian size -> Bool
Eq, Int -> Hermitian size -> ShowS
[Hermitian size] -> ShowS
Hermitian size -> String
(Int -> Hermitian size -> ShowS)
-> (Hermitian size -> String)
-> ([Hermitian size] -> ShowS)
-> Show (Hermitian size)
forall size. Show size => Int -> Hermitian size -> ShowS
forall size. Show size => [Hermitian size] -> ShowS
forall size. Show size => Hermitian size -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Hermitian size] -> ShowS
$cshowList :: forall size. Show size => [Hermitian size] -> ShowS
show :: Hermitian size -> String
$cshow :: forall size. Show size => Hermitian size -> String
showsPrec :: Int -> Hermitian size -> ShowS
$cshowsPrec :: forall size. Show size => Int -> Hermitian size -> ShowS
Show)

instance (Shape.C size) => Box.Box (Hermitian size) where
   type HeightOf (Hermitian size) = size
   type WidthOf (Hermitian size) = size
   height :: Hermitian size -> HeightOf (Hermitian size)
height = Hermitian size -> HeightOf (Hermitian size)
forall size. Hermitian size -> size
hermitianSize
   width :: Hermitian size -> WidthOf (Hermitian size)
width = Hermitian size -> WidthOf (Hermitian size)
forall size. Hermitian size -> size
hermitianSize

hermitianFromSymmetric :: Symmetric size -> Hermitian size
hermitianFromSymmetric :: Symmetric size -> Hermitian size
hermitianFromSymmetric (Triangular NonUnit
_diag (Filled, Filled)
_uplo Order
order size
size) =
   Order -> size -> Hermitian size
forall size. Order -> size -> Hermitian size
Hermitian Order
order size
size

uploFromOrder :: Order -> Char
uploFromOrder :: Order -> Char
uploFromOrder Order
RowMajor = Char
'L'
uploFromOrder Order
ColumnMajor = Char
'U'

instance (NFData size) => NFData (Hermitian size) where
   rnf :: Hermitian size -> ()
rnf (Hermitian Order
order size
size) = (Order, size) -> ()
forall a. NFData a => a -> ()
rnf (Order
order, size
size)

instance (Shape.C size) => Shape.C (Hermitian size) where
   size :: Hermitian size -> Int
size (Hermitian Order
_ size
size) = Int -> Int
triangleSize (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ size -> Int
forall sh. C sh => sh -> Int
Shape.size size
size
   uncheckedSize :: Hermitian size -> Int
uncheckedSize (Hermitian Order
_ size
size) = Int -> Int
triangleSize (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ size -> Int
forall sh. C sh => sh -> Int
Shape.uncheckedSize size
size

instance (Shape.Indexed size) => Shape.Indexed (Hermitian size) where
   type Index (Hermitian size) = (Shape.Index size, Shape.Index size)

   indices :: Hermitian size -> [Index (Hermitian size)]
indices (Hermitian Order
order size
size) = Order -> size -> [(Index size, Index size)]
forall sh. Indexed sh => Order -> sh -> [(Index sh, Index sh)]
triangleIndices Order
order size
size

   offset :: Hermitian size -> Index (Hermitian size) -> Int
offset (Hermitian Order
order size
size) = Order -> size -> (Index size, Index size) -> Int
forall sh. Indexed sh => Order -> sh -> (Index sh, Index sh) -> Int
triangleOffset Order
order size
size
   uncheckedOffset :: Hermitian size -> Index (Hermitian size) -> Int
uncheckedOffset (Hermitian Order
order size
size) =
      Order -> size -> (Index size, Index size) -> Int
forall sh. Indexed sh => Order -> sh -> (Index sh, Index sh) -> Int
triangleUncheckedOffset Order
order size
size

   sizeOffset :: Hermitian size -> (Int, Index (Hermitian size) -> Int)
sizeOffset (Hermitian Order
order size
size) = Order -> size -> (Int, (Index size, Index size) -> Int)
forall sh.
Indexed sh =>
Order -> sh -> (Int, (Index sh, Index sh) -> Int)
triangleSizeOffset Order
order size
size
   uncheckedSizeOffset :: Hermitian size -> (Int, Index (Hermitian size) -> Int)
uncheckedSizeOffset (Hermitian Order
order size
size) =
      Order -> size -> (Int, (Index size, Index size) -> Int)
forall sh.
Indexed sh =>
Order -> sh -> (Int, (Index sh, Index sh) -> Int)
triangleUncheckedSizeOffset Order
order size
size

   inBounds :: Hermitian size -> Index (Hermitian size) -> Bool
inBounds (Hermitian Order
_ size
size) ix :: Index (Hermitian size)
ix@(r,c) =
      (size, size) -> Index (size, size) -> Bool
forall sh. Indexed sh => sh -> Index sh -> Bool
Shape.inBounds (size
size,size
size) Index (size, size)
Index (Hermitian size)
ix
      Bool -> Bool -> Bool
&&
      size -> Index size -> Int
forall sh. Indexed sh => sh -> Index sh -> Int
Shape.offset size
size Index size
r Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= size -> Index size -> Int
forall sh. Indexed sh => sh -> Index sh -> Int
Shape.offset size
size Index size
c

instance (Shape.InvIndexed size) => Shape.InvIndexed (Hermitian size) where
   indexFromOffset :: Hermitian size -> Int -> Index (Hermitian size)
indexFromOffset (Hermitian Order
order size
size) Int
k =
      Order -> size -> Int -> (Index size, Index size)
forall sh.
InvIndexed sh =>
Order -> sh -> Int -> (Index sh, Index sh)
triangleIndexFromOffset Order
order size
size Int
k



data Triangular lo diag up size =
   Triangular {
      Triangular lo diag up size -> diag
triangularDiag :: diag,
      Triangular lo diag up size -> (lo, up)
triangularUplo :: (lo,up),
      Triangular lo diag up size -> Order
triangularOrder :: Order,
      Triangular lo diag up size -> size
triangularSize :: size
   } deriving (Triangular lo diag up size -> Triangular lo diag up size -> Bool
(Triangular lo diag up size -> Triangular lo diag up size -> Bool)
-> (Triangular lo diag up size
    -> Triangular lo diag up size -> Bool)
-> Eq (Triangular lo diag up size)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall lo diag up size.
(Eq diag, Eq lo, Eq up, Eq size) =>
Triangular lo diag up size -> Triangular lo diag up size -> Bool
/= :: Triangular lo diag up size -> Triangular lo diag up size -> Bool
$c/= :: forall lo diag up size.
(Eq diag, Eq lo, Eq up, Eq size) =>
Triangular lo diag up size -> Triangular lo diag up size -> Bool
== :: Triangular lo diag up size -> Triangular lo diag up size -> Bool
$c== :: forall lo diag up size.
(Eq diag, Eq lo, Eq up, Eq size) =>
Triangular lo diag up size -> Triangular lo diag up size -> Bool
Eq, Int -> Triangular lo diag up size -> ShowS
[Triangular lo diag up size] -> ShowS
Triangular lo diag up size -> String
(Int -> Triangular lo diag up size -> ShowS)
-> (Triangular lo diag up size -> String)
-> ([Triangular lo diag up size] -> ShowS)
-> Show (Triangular lo diag up size)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall lo diag up size.
(Show diag, Show lo, Show up, Show size) =>
Int -> Triangular lo diag up size -> ShowS
forall lo diag up size.
(Show diag, Show lo, Show up, Show size) =>
[Triangular lo diag up size] -> ShowS
forall lo diag up size.
(Show diag, Show lo, Show up, Show size) =>
Triangular lo diag up size -> String
showList :: [Triangular lo diag up size] -> ShowS
$cshowList :: forall lo diag up size.
(Show diag, Show lo, Show up, Show size) =>
[Triangular lo diag up size] -> ShowS
show :: Triangular lo diag up size -> String
$cshow :: forall lo diag up size.
(Show diag, Show lo, Show up, Show size) =>
Triangular lo diag up size -> String
showsPrec :: Int -> Triangular lo diag up size -> ShowS
$cshowsPrec :: forall lo diag up size.
(Show diag, Show lo, Show up, Show size) =>
Int -> Triangular lo diag up size -> ShowS
Show)

instance
   (Content lo, TriDiag diag, Content up, Shape.C size) =>
      Box.Box (Triangular lo diag up size) where
   type HeightOf (Triangular lo diag up size) = size
   type WidthOf (Triangular lo diag up size) = size
   height :: Triangular lo diag up size -> HeightOf (Triangular lo diag up size)
height = Triangular lo diag up size -> HeightOf (Triangular lo diag up size)
forall lo diag up size. Triangular lo diag up size -> size
triangularSize
   width :: Triangular lo diag up size -> WidthOf (Triangular lo diag up size)
width = Triangular lo diag up size -> WidthOf (Triangular lo diag up size)
forall lo diag up size. Triangular lo diag up size -> size
triangularSize

symmetricFromHermitian :: Hermitian size -> Symmetric size
symmetricFromHermitian :: Hermitian size -> Symmetric size
symmetricFromHermitian (Hermitian Order
order size
size) =
   NonUnit -> (Filled, Filled) -> Order -> size -> Symmetric size
forall lo diag up size.
diag -> (lo, up) -> Order -> size -> Triangular lo diag up size
Triangular NonUnit
NonUnit (Filled, Filled)
forall lo up. (Content lo, Content up) => (lo, up)
autoUplo Order
order size
size


data Unit = Unit deriving (Unit -> Unit -> Bool
(Unit -> Unit -> Bool) -> (Unit -> Unit -> Bool) -> Eq Unit
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Unit -> Unit -> Bool
$c/= :: Unit -> Unit -> Bool
== :: Unit -> Unit -> Bool
$c== :: Unit -> Unit -> Bool
Eq, Int -> Unit -> ShowS
[Unit] -> ShowS
Unit -> String
(Int -> Unit -> ShowS)
-> (Unit -> String) -> ([Unit] -> ShowS) -> Show Unit
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Unit] -> ShowS
$cshowList :: [Unit] -> ShowS
show :: Unit -> String
$cshow :: Unit -> String
showsPrec :: Int -> Unit -> ShowS
$cshowsPrec :: Int -> Unit -> ShowS
Show)
data NonUnit = NonUnit deriving (NonUnit -> NonUnit -> Bool
(NonUnit -> NonUnit -> Bool)
-> (NonUnit -> NonUnit -> Bool) -> Eq NonUnit
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: NonUnit -> NonUnit -> Bool
$c/= :: NonUnit -> NonUnit -> Bool
== :: NonUnit -> NonUnit -> Bool
$c== :: NonUnit -> NonUnit -> Bool
Eq, Int -> NonUnit -> ShowS
[NonUnit] -> ShowS
NonUnit -> String
(Int -> NonUnit -> ShowS)
-> (NonUnit -> String) -> ([NonUnit] -> ShowS) -> Show NonUnit
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [NonUnit] -> ShowS
$cshowList :: [NonUnit] -> ShowS
show :: NonUnit -> String
$cshow :: NonUnit -> String
showsPrec :: Int -> NonUnit -> ShowS
$cshowsPrec :: Int -> NonUnit -> ShowS
Show)

class TriDiag diag where switchTriDiag :: f Unit -> f NonUnit -> f diag
instance TriDiag Unit where switchTriDiag :: f Unit -> f NonUnit -> f Unit
switchTriDiag f Unit
f f NonUnit
_ = f Unit
f
instance TriDiag NonUnit where switchTriDiag :: f Unit -> f NonUnit -> f NonUnit
switchTriDiag f Unit
_ f NonUnit
f = f NonUnit
f

autoDiag :: TriDiag diag => diag
autoDiag :: diag
autoDiag = Identity diag -> diag
forall a. Identity a -> a
runIdentity (Identity diag -> diag) -> Identity diag -> diag
forall a b. (a -> b) -> a -> b
$ Identity Unit -> Identity NonUnit -> Identity diag
forall diag (f :: * -> *).
TriDiag diag =>
f Unit -> f NonUnit -> f diag
switchTriDiag (Unit -> Identity Unit
forall a. a -> Identity a
Identity Unit
Unit) (NonUnit -> Identity NonUnit
forall a. a -> Identity a
Identity NonUnit
NonUnit)

caseTriDiag :: TriDiag diag => diag -> a -> a -> a
caseTriDiag :: diag -> a -> a -> a
caseTriDiag diag
diag a
unit a
nonUnit =
   diag -> Const a diag -> a
forall c a. c -> Const a c -> a
getConstAs diag
diag (Const a diag -> a) -> Const a diag -> a
forall a b. (a -> b) -> a -> b
$ Const a Unit -> Const a NonUnit -> Const a diag
forall diag (f :: * -> *).
TriDiag diag =>
f Unit -> f NonUnit -> f diag
switchTriDiag (a -> Const a Unit
forall k a (b :: k). a -> Const a b
Const a
unit) (a -> Const a NonUnit
forall k a (b :: k). a -> Const a b
Const a
nonUnit)

charFromTriDiag :: TriDiag diag => diag -> Char
charFromTriDiag :: diag -> Char
charFromTriDiag diag
diag = diag -> Char -> Char -> Char
forall diag a. TriDiag diag => diag -> a -> a -> a
caseTriDiag diag
diag Char
'U' Char
'N'


relaxUnitDiagonal ::
   (TriDiag diag) => Triangular lo Unit up sh -> Triangular lo diag up sh
relaxUnitDiagonal :: Triangular lo Unit up sh -> Triangular lo diag up sh
relaxUnitDiagonal Triangular lo Unit up sh
shape = Triangular lo Unit up sh
shape{triangularDiag :: diag
triangularDiag = diag
forall diag. TriDiag diag => diag
autoDiag}

strictNonUnitDiagonal ::
   (TriDiag diag) => Triangular lo diag up sh -> Triangular lo NonUnit up sh
strictNonUnitDiagonal :: Triangular lo diag up sh -> Triangular lo NonUnit up sh
strictNonUnitDiagonal Triangular lo diag up sh
shape = Triangular lo diag up sh
shape{triangularDiag :: NonUnit
triangularDiag = NonUnit
NonUnit}


data Empty = Empty deriving (Empty -> Empty -> Bool
(Empty -> Empty -> Bool) -> (Empty -> Empty -> Bool) -> Eq Empty
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Empty -> Empty -> Bool
$c/= :: Empty -> Empty -> Bool
== :: Empty -> Empty -> Bool
$c== :: Empty -> Empty -> Bool
Eq, Int -> Empty -> ShowS
[Empty] -> ShowS
Empty -> String
(Int -> Empty -> ShowS)
-> (Empty -> String) -> ([Empty] -> ShowS) -> Show Empty
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Empty] -> ShowS
$cshowList :: [Empty] -> ShowS
show :: Empty -> String
$cshow :: Empty -> String
showsPrec :: Int -> Empty -> ShowS
$cshowsPrec :: Int -> Empty -> ShowS
Show)
data Filled = Filled deriving (Filled -> Filled -> Bool
(Filled -> Filled -> Bool)
-> (Filled -> Filled -> Bool) -> Eq Filled
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Filled -> Filled -> Bool
$c/= :: Filled -> Filled -> Bool
== :: Filled -> Filled -> Bool
$c== :: Filled -> Filled -> Bool
Eq, Int -> Filled -> ShowS
[Filled] -> ShowS
Filled -> String
(Int -> Filled -> ShowS)
-> (Filled -> String) -> ([Filled] -> ShowS) -> Show Filled
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Filled] -> ShowS
$cshowList :: [Filled] -> ShowS
show :: Filled -> String
$cshow :: Filled -> String
showsPrec :: Int -> Filled -> ShowS
$cshowsPrec :: Int -> Filled -> ShowS
Show)

lower :: (Filled,Empty)
lower :: (Filled, Empty)
lower = (Filled
Filled,Empty
Empty)

upper :: (Empty,Filled)
upper :: (Empty, Filled)
upper = (Empty
Empty,Filled
Filled)

type Identity = Triangular Empty Unit Empty
type Diagonal = Triangular Empty NonUnit Empty
type LowerTriangular diag = Triangular Filled diag Empty
type UpperTriangular diag = Triangular Empty diag Filled
type FlexSymmetric diag = Triangular Filled diag Filled
type Symmetric = FlexSymmetric NonUnit

triangularTranspose ::
   (Content lo, Content up) =>
   Triangular lo diag up sh -> Triangular up diag lo sh
triangularTranspose :: Triangular lo diag up sh -> Triangular up diag lo sh
triangularTranspose (Triangular diag
diag (lo, up)
uplo Order
order sh
size) =
   diag -> (up, lo) -> Order -> sh -> Triangular up diag lo sh
forall lo diag up size.
diag -> (lo, up) -> Order -> size -> Triangular lo diag up size
Triangular diag
diag
      ((lo, up) -> (up, lo)
forall a b. (a, b) -> (b, a)
swap (lo, up)
uplo)
      ((lo, up)
-> (Order -> Order)
-> (Order -> Order)
-> (Order -> Order)
-> (Order -> Order)
-> Order
-> Order
forall lo up a.
(Content lo, Content up) =>
(lo, up) -> a -> a -> a -> a -> a
caseDiagUpLoSym (lo, up)
uplo Order -> Order
flipOrder Order -> Order
flipOrder Order -> Order
flipOrder Order -> Order
forall a. a -> a
id Order
order)
      sh
size


class Content c where switchContent :: f Empty -> f Filled -> f c
instance Content Empty where switchContent :: f Empty -> f Filled -> f Empty
switchContent f Empty
f f Filled
_ = f Empty
f
instance Content Filled where switchContent :: f Empty -> f Filled -> f Filled
switchContent f Empty
_ f Filled
f = f Filled
f


type UpLo lo up = (UpLoC lo up, UpLoC up lo)

class (DiagUpLoC lo up, UpLoSymC lo up) => UpLoC lo up where
   switchUpLo :: f Empty Filled -> f Filled Empty -> f lo up

instance UpLoC Empty  Filled where switchUpLo :: f Empty Filled -> f Filled Empty -> f Empty Filled
switchUpLo f Empty Filled
f f Filled Empty
_ = f Empty Filled
f
instance UpLoC Filled Empty  where switchUpLo :: f Empty Filled -> f Filled Empty -> f Filled Empty
switchUpLo f Empty Filled
_ f Filled Empty
f = f Filled Empty
f


type DiagUpLo lo up = (DiagUpLoC lo up, DiagUpLoC up lo)

class (Content lo, Content up) => DiagUpLoC lo up where
   switchDiagUpLo ::
      f Empty Empty -> f Empty Filled -> f Filled Empty -> f lo up

instance DiagUpLoC Empty  Empty  where switchDiagUpLo :: f Empty Empty -> f Empty Filled -> f Filled Empty -> f Empty Empty
switchDiagUpLo f Empty Empty
f f Empty Filled
_ f Filled Empty
_ = f Empty Empty
f
instance DiagUpLoC Empty  Filled where switchDiagUpLo :: f Empty Empty -> f Empty Filled -> f Filled Empty -> f Empty Filled
switchDiagUpLo f Empty Empty
_ f Empty Filled
f f Filled Empty
_ = f Empty Filled
f
instance DiagUpLoC Filled Empty  where switchDiagUpLo :: f Empty Empty -> f Empty Filled -> f Filled Empty -> f Filled Empty
switchDiagUpLo f Empty Empty
_ f Empty Filled
_ f Filled Empty
f = f Filled Empty
f


type UpLoSym lo up = (UpLoSymC lo up, UpLoSymC up lo)

class (Content lo, Content up) => UpLoSymC lo up where
   switchUpLoSym ::
      f Empty Filled -> f Filled Empty -> f Filled Filled -> f lo up

instance UpLoSymC Empty  Filled where switchUpLoSym :: f Empty Filled
-> f Filled Empty -> f Filled Filled -> f Empty Filled
switchUpLoSym f Empty Filled
f f Filled Empty
_ f Filled Filled
_ = f Empty Filled
f
instance UpLoSymC Filled Empty  where switchUpLoSym :: f Empty Filled
-> f Filled Empty -> f Filled Filled -> f Filled Empty
switchUpLoSym f Empty Filled
_ f Filled Empty
f f Filled Filled
_ = f Filled Empty
f
instance UpLoSymC Filled Filled where switchUpLoSym :: f Empty Filled
-> f Filled Empty -> f Filled Filled -> f Filled Filled
switchUpLoSym f Empty Filled
_ f Filled Empty
_ f Filled Filled
f = f Filled Filled
f


switchDiagUpLoSym ::
   (Content lo, Content up) =>
   f Empty Empty -> f Empty Filled -> f Filled Empty -> f Filled Filled ->
   f lo up
switchDiagUpLoSym :: f Empty Empty
-> f Empty Filled -> f Filled Empty -> f Filled Filled -> f lo up
switchDiagUpLoSym f Empty Empty
fDiag f Empty Filled
fUpper f Filled Empty
fLower f Filled Filled
fSymm =
   Flip f up lo -> f lo up
forall (f :: * -> * -> *) b a. Flip f b a -> f a b
getFlip (Flip f up lo -> f lo up) -> Flip f up lo -> f lo up
forall a b. (a -> b) -> a -> b
$
   Flip f up Empty -> Flip f up Filled -> Flip f up lo
forall c (f :: * -> *). Content c => f Empty -> f Filled -> f c
switchContent
      (f Empty up -> Flip f up Empty
forall (f :: * -> * -> *) b a. f a b -> Flip f b a
Flip (f Empty up -> Flip f up Empty) -> f Empty up -> Flip f up Empty
forall a b. (a -> b) -> a -> b
$ f Empty Empty -> f Empty Filled -> f Empty up
forall c (f :: * -> *). Content c => f Empty -> f Filled -> f c
switchContent f Empty Empty
fDiag f Empty Filled
fUpper)
      (f Filled up -> Flip f up Filled
forall (f :: * -> * -> *) b a. f a b -> Flip f b a
Flip (f Filled up -> Flip f up Filled)
-> f Filled up -> Flip f up Filled
forall a b. (a -> b) -> a -> b
$ f Filled Empty -> f Filled Filled -> f Filled up
forall c (f :: * -> *). Content c => f Empty -> f Filled -> f c
switchContent f Filled Empty
fLower f Filled Filled
fSymm)

autoContent :: Content c => c
autoContent :: c
autoContent = Identity c -> c
forall a. Identity a -> a
runIdentity (Identity c -> c) -> Identity c -> c
forall a b. (a -> b) -> a -> b
$ Identity Empty -> Identity Filled -> Identity c
forall c (f :: * -> *). Content c => f Empty -> f Filled -> f c
switchContent (Empty -> Identity Empty
forall a. a -> Identity a
Identity Empty
Empty) (Filled -> Identity Filled
forall a. a -> Identity a
Identity Filled
Filled)

autoUplo :: (Content lo, Content up) => (lo,up)
autoUplo :: (lo, up)
autoUplo = (lo
forall c. Content c => c
autoContent,up
forall c. Content c => c
autoContent)

uploOrder :: (Content lo, Content up) => (lo,up) -> Order -> Order
uploOrder :: (lo, up) -> Order -> Order
uploOrder (lo
_loc,up
upc) = up -> (Order -> Order) -> (Order -> Order) -> Order -> Order
forall c a. Content c => c -> a -> a -> a
caseContent up
upc Order -> Order
flipOrder Order -> Order
forall a. a -> a
id

getConstAs :: c -> Const a c -> a
getConstAs :: c -> Const a c -> a
getConstAs c
_ = Const a c -> a
forall a k (b :: k). Const a b -> a
getConst

caseContent :: Content c => c -> a -> a -> a
caseContent :: c -> a -> a -> a
caseContent c
c a
lo a
up =
   c -> Const a c -> a
forall c a. c -> Const a c -> a
getConstAs c
c (Const a c -> a) -> Const a c -> a
forall a b. (a -> b) -> a -> b
$ Const a Empty -> Const a Filled -> Const a c
forall c (f :: * -> *). Content c => f Empty -> f Filled -> f c
switchContent (a -> Const a Empty
forall k a (b :: k). a -> Const a b
Const a
lo) (a -> Const a Filled
forall k a (b :: k). a -> Const a b
Const a
up)

caseLoUp :: UpLo lo up => (lo,up) -> a -> a -> a
caseLoUp :: (lo, up) -> a -> a -> a
caseLoUp (lo
_loc,up
upc) = up -> a -> a -> a
forall c a. Content c => c -> a -> a -> a
caseContent up
upc

caseDiagUpLoSym :: (Content lo, Content up) => (lo,up) -> a -> a -> a -> a -> a
caseDiagUpLoSym :: (lo, up) -> a -> a -> a -> a -> a
caseDiagUpLoSym (lo
loc,up
upc) a
diag a
up a
lo a
symm =
   lo -> a -> a -> a
forall c a. Content c => c -> a -> a -> a
caseContent lo
loc
      (up -> a -> a -> a
forall c a. Content c => c -> a -> a -> a
caseContent up
upc a
diag a
up)
      (up -> a -> a -> a
forall c a. Content c => c -> a -> a -> a
caseContent up
upc a
lo a
symm)


newtype Const2 a lo up = Const2 {Const2 a lo up -> a
getConst2 :: a}

getContentConst2 :: (lo,up) -> Const2 a lo up -> a
getContentConst2 :: (lo, up) -> Const2 a lo up -> a
getContentConst2 (lo, up)
_ = Const2 a lo up -> a
forall a lo up. Const2 a lo up -> a
getConst2

caseUpLoSym :: (UpLoSym lo up) => (lo,up) -> a -> a -> a -> a
caseUpLoSym :: (lo, up) -> a -> a -> a -> a
caseUpLoSym (lo, up)
c a
lo a
up a
sym =
   (lo, up) -> Const2 a lo up -> a
forall lo up a. (lo, up) -> Const2 a lo up -> a
getContentConst2 (lo, up)
c (Const2 a lo up -> a) -> Const2 a lo up -> a
forall a b. (a -> b) -> a -> b
$ Const2 a Empty Filled
-> Const2 a Filled Empty
-> Const2 a Filled Filled
-> Const2 a lo up
forall lo up (f :: * -> * -> *).
UpLoSymC lo up =>
f Empty Filled -> f Filled Empty -> f Filled Filled -> f lo up
switchUpLoSym (a -> Const2 a Empty Filled
forall a lo up. a -> Const2 a lo up
Const2 a
lo) (a -> Const2 a Filled Empty
forall a lo up. a -> Const2 a lo up
Const2 a
up) (a -> Const2 a Filled Filled
forall a lo up. a -> Const2 a lo up
Const2 a
sym)


instance
   (Content lo, TriDiag diag, Content up, NFData size) =>
      NFData (Triangular lo diag up size) where
   rnf :: Triangular lo diag up size -> ()
rnf (Triangular diag
diag (lo
loc,up
upc) Order
order size
size) =
      ((), ((), ()), Order, size) -> ()
forall a. NFData a => a -> ()
rnf
         ((Flip (->) () diag -> diag -> ())
-> diag -> Flip (->) () diag -> ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip Flip (->) () diag -> diag -> ()
forall (f :: * -> * -> *) b a. Flip f b a -> f a b
getFlip diag
diag (Flip (->) () diag -> ()) -> Flip (->) () diag -> ()
forall a b. (a -> b) -> a -> b
$
            Flip (->) () Unit -> Flip (->) () NonUnit -> Flip (->) () diag
forall diag (f :: * -> *).
TriDiag diag =>
f Unit -> f NonUnit -> f diag
switchTriDiag ((Unit -> ()) -> Flip (->) () Unit
forall (f :: * -> * -> *) b a. f a b -> Flip f b a
Flip ((Unit -> ()) -> Flip (->) () Unit)
-> (Unit -> ()) -> Flip (->) () Unit
forall a b. (a -> b) -> a -> b
$ \Unit
Unit -> ()) ((NonUnit -> ()) -> Flip (->) () NonUnit
forall (f :: * -> * -> *) b a. f a b -> Flip f b a
Flip ((NonUnit -> ()) -> Flip (->) () NonUnit)
-> (NonUnit -> ()) -> Flip (->) () NonUnit
forall a b. (a -> b) -> a -> b
$ \NonUnit
NonUnit -> ()),
          let rnfContent :: b -> ()
rnfContent b
c =
               (Flip (->) () b -> b -> ()) -> b -> Flip (->) () b -> ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip Flip (->) () b -> b -> ()
forall (f :: * -> * -> *) b a. Flip f b a -> f a b
getFlip b
c (Flip (->) () b -> ()) -> Flip (->) () b -> ()
forall a b. (a -> b) -> a -> b
$
               Flip (->) () Empty -> Flip (->) () Filled -> Flip (->) () b
forall c (f :: * -> *). Content c => f Empty -> f Filled -> f c
switchContent
                  ((Empty -> ()) -> Flip (->) () Empty
forall (f :: * -> * -> *) b a. f a b -> Flip f b a
Flip ((Empty -> ()) -> Flip (->) () Empty)
-> (Empty -> ()) -> Flip (->) () Empty
forall a b. (a -> b) -> a -> b
$ \Empty
Empty -> ())
                  ((Filled -> ()) -> Flip (->) () Filled
forall (f :: * -> * -> *) b a. f a b -> Flip f b a
Flip ((Filled -> ()) -> Flip (->) () Filled)
-> (Filled -> ()) -> Flip (->) () Filled
forall a b. (a -> b) -> a -> b
$ \Filled
Filled -> ())
          in (lo -> ()
forall b. Content b => b -> ()
rnfContent lo
loc, up -> ()
forall b. Content b => b -> ()
rnfContent up
upc),
          Order
order, size
size)

instance
   (Content lo, TriDiag diag, Content up, Shape.C size) =>
      Shape.C (Triangular lo diag up size) where

   size :: Triangular lo diag up size -> Int
size (Triangular diag
_diag (lo, up)
uplo Order
_ size
size) =
      let n :: Int
n = size -> Int
forall sh. C sh => sh -> Int
Shape.size size
size
      in (lo, up) -> Int -> Int -> Int -> Int -> Int
forall lo up a.
(Content lo, Content up) =>
(lo, up) -> a -> a -> a -> a -> a
caseDiagUpLoSym (lo, up)
uplo Int
n
            (Int -> Int
triangleSize Int
n)
            (Int -> Int
triangleSize Int
n)
            (Int -> Int
triangleSize Int
n)
   uncheckedSize :: Triangular lo diag up size -> Int
uncheckedSize (Triangular diag
_diag (lo, up)
uplo Order
_ size
size) =
      let n :: Int
n = size -> Int
forall sh. C sh => sh -> Int
Shape.uncheckedSize size
size
      in (lo, up) -> Int -> Int -> Int -> Int -> Int
forall lo up a.
(Content lo, Content up) =>
(lo, up) -> a -> a -> a -> a -> a
caseDiagUpLoSym (lo, up)
uplo Int
n
            (Int -> Int
triangleSize Int
n)
            (Int -> Int
triangleSize Int
n)
            (Int -> Int
triangleSize Int
n)

instance
   (Content lo, TriDiag diag, Content up, Shape.Indexed size) =>
      Shape.Indexed (Triangular lo diag up size) where
   type Index (Triangular lo diag up size) =
         (Shape.Index size, Shape.Index size)

   indices :: Triangular lo diag up size -> [Index (Triangular lo diag up size)]
indices (Triangular diag
_diag (lo, up)
uplo Order
order size
size) =
      (lo, up)
-> [(Index size, Index size)]
-> [(Index size, Index size)]
-> [(Index size, Index size)]
-> [(Index size, Index size)]
-> [(Index size, Index size)]
forall lo up a.
(Content lo, Content up) =>
(lo, up) -> a -> a -> a -> a -> a
caseDiagUpLoSym (lo, up)
uplo
         ((Index size -> (Index size, Index size))
-> [Index size] -> [(Index size, Index size)]
forall a b. (a -> b) -> [a] -> [b]
map Index size -> (Index size, Index size)
forall a. a -> (a, a)
double ([Index size] -> [(Index size, Index size)])
-> [Index size] -> [(Index size, Index size)]
forall a b. (a -> b) -> a -> b
$ size -> [Index size]
forall sh. Indexed sh => sh -> [Index sh]
Shape.indices size
size)
         (Order -> size -> [(Index size, Index size)]
forall sh. Indexed sh => Order -> sh -> [(Index sh, Index sh)]
triangleIndices Order
order size
size)
         (((Index size, Index size) -> (Index size, Index size))
-> [(Index size, Index size)] -> [(Index size, Index size)]
forall a b. (a -> b) -> [a] -> [b]
map (Index size, Index size) -> (Index size, Index size)
forall a b. (a, b) -> (b, a)
swap ([(Index size, Index size)] -> [(Index size, Index size)])
-> [(Index size, Index size)] -> [(Index size, Index size)]
forall a b. (a -> b) -> a -> b
$ Order -> size -> [(Index size, Index size)]
forall sh. Indexed sh => Order -> sh -> [(Index sh, Index sh)]
triangleIndices (Order -> Order
flipOrder Order
order) size
size)
         (Order -> size -> [(Index size, Index size)]
forall sh. Indexed sh => Order -> sh -> [(Index sh, Index sh)]
triangleIndices Order
order size
size)

   offset :: Triangular lo diag up size
-> Index (Triangular lo diag up size) -> Int
offset (Triangular diag
_diag (lo, up)
uplo Order
order size
size) =
      (lo, up)
-> ((Index size, Index size) -> Int)
-> ((Index size, Index size) -> Int)
-> ((Index size, Index size) -> Int)
-> ((Index size, Index size) -> Int)
-> (Index size, Index size)
-> Int
forall lo up a.
(Content lo, Content up) =>
(lo, up) -> a -> a -> a -> a -> a
caseDiagUpLoSym (lo, up)
uplo
         (size -> Index size -> Int
forall sh. Indexed sh => sh -> Index sh -> Int
Shape.offset size
size (Index size -> Int)
-> ((Index size, Index size) -> Index size)
-> (Index size, Index size)
-> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Index size, Index size) -> Index size
forall a b. (a, b) -> b
snd)
         (Order -> size -> (Index size, Index size) -> Int
forall sh. Indexed sh => Order -> sh -> (Index sh, Index sh) -> Int
triangleOffset Order
order size
size)
         (Order -> size -> (Index size, Index size) -> Int
forall sh. Indexed sh => Order -> sh -> (Index sh, Index sh) -> Int
triangleOffset (Order -> Order
flipOrder Order
order) size
size ((Index size, Index size) -> Int)
-> ((Index size, Index size) -> (Index size, Index size))
-> (Index size, Index size)
-> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Index size, Index size) -> (Index size, Index size)
forall a b. (a, b) -> (b, a)
swap)
         (Order -> size -> (Index size, Index size) -> Int
forall sh. Indexed sh => Order -> sh -> (Index sh, Index sh) -> Int
triangleOffset Order
order size
size)
   uncheckedOffset :: Triangular lo diag up size
-> Index (Triangular lo diag up size) -> Int
uncheckedOffset (Triangular diag
_diag (lo, up)
uplo Order
order size
size) =
      (lo, up)
-> ((Index size, Index size) -> Int)
-> ((Index size, Index size) -> Int)
-> ((Index size, Index size) -> Int)
-> ((Index size, Index size) -> Int)
-> (Index size, Index size)
-> Int
forall lo up a.
(Content lo, Content up) =>
(lo, up) -> a -> a -> a -> a -> a
caseDiagUpLoSym (lo, up)
uplo
         (size -> Index size -> Int
forall sh. Indexed sh => sh -> Index sh -> Int
Shape.offset size
size (Index size -> Int)
-> ((Index size, Index size) -> Index size)
-> (Index size, Index size)
-> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Index size, Index size) -> Index size
forall a b. (a, b) -> b
snd)
         (Order -> size -> (Index size, Index size) -> Int
forall sh. Indexed sh => Order -> sh -> (Index sh, Index sh) -> Int
triangleUncheckedOffset Order
order size
size)
         (Order -> size -> (Index size, Index size) -> Int
forall sh. Indexed sh => Order -> sh -> (Index sh, Index sh) -> Int
triangleUncheckedOffset (Order -> Order
flipOrder Order
order) size
size ((Index size, Index size) -> Int)
-> ((Index size, Index size) -> (Index size, Index size))
-> (Index size, Index size)
-> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Index size, Index size) -> (Index size, Index size)
forall a b. (a, b) -> (b, a)
swap)
         (Order -> size -> (Index size, Index size) -> Int
forall sh. Indexed sh => Order -> sh -> (Index sh, Index sh) -> Int
triangleUncheckedOffset Order
order size
size)

   sizeOffset :: Triangular lo diag up size
-> (Int, Index (Triangular lo diag up size) -> Int)
sizeOffset (Triangular diag
_diag (lo, up)
uplo Order
order size
size) =
      (lo, up)
-> (Int, (Index size, Index size) -> Int)
-> (Int, (Index size, Index size) -> Int)
-> (Int, (Index size, Index size) -> Int)
-> (Int, (Index size, Index size) -> Int)
-> (Int, (Index size, Index size) -> Int)
forall lo up a.
(Content lo, Content up) =>
(lo, up) -> a -> a -> a -> a -> a
caseDiagUpLoSym (lo, up)
uplo
         (((Index size -> Int) -> (Index size, Index size) -> Int)
-> (Int, Index size -> Int)
-> (Int, (Index size, Index size) -> Int)
forall b c a. (b -> c) -> (a, b) -> (a, c)
mapSnd ((Index size -> Int)
-> ((Index size, Index size) -> Index size)
-> (Index size, Index size)
-> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Index size, Index size) -> Index size
forall a b. (a, b) -> b
snd) ((Int, Index size -> Int)
 -> (Int, (Index size, Index size) -> Int))
-> (Int, Index size -> Int)
-> (Int, (Index size, Index size) -> Int)
forall a b. (a -> b) -> a -> b
$ size -> (Int, Index size -> Int)
forall sh. Indexed sh => sh -> (Int, Index sh -> Int)
Shape.sizeOffset size
size)
         (Order -> size -> (Int, (Index size, Index size) -> Int)
forall sh.
Indexed sh =>
Order -> sh -> (Int, (Index sh, Index sh) -> Int)
triangleSizeOffset Order
order size
size)
         ((((Index size, Index size) -> Int)
 -> (Index size, Index size) -> Int)
-> (Int, (Index size, Index size) -> Int)
-> (Int, (Index size, Index size) -> Int)
forall b c a. (b -> c) -> (a, b) -> (a, c)
mapSnd (((Index size, Index size) -> Int)
-> ((Index size, Index size) -> (Index size, Index size))
-> (Index size, Index size)
-> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Index size, Index size) -> (Index size, Index size)
forall a b. (a, b) -> (b, a)
swap) ((Int, (Index size, Index size) -> Int)
 -> (Int, (Index size, Index size) -> Int))
-> (Int, (Index size, Index size) -> Int)
-> (Int, (Index size, Index size) -> Int)
forall a b. (a -> b) -> a -> b
$ Order -> size -> (Int, (Index size, Index size) -> Int)
forall sh.
Indexed sh =>
Order -> sh -> (Int, (Index sh, Index sh) -> Int)
triangleSizeOffset (Order -> Order
flipOrder Order
order) size
size)
         (Order -> size -> (Int, (Index size, Index size) -> Int)
forall sh.
Indexed sh =>
Order -> sh -> (Int, (Index sh, Index sh) -> Int)
triangleSizeOffset Order
order size
size)
   uncheckedSizeOffset :: Triangular lo diag up size
-> (Int, Index (Triangular lo diag up size) -> Int)
uncheckedSizeOffset (Triangular diag
_diag (lo, up)
uplo Order
order size
size) =
      (lo, up)
-> (Int, (Index size, Index size) -> Int)
-> (Int, (Index size, Index size) -> Int)
-> (Int, (Index size, Index size) -> Int)
-> (Int, (Index size, Index size) -> Int)
-> (Int, (Index size, Index size) -> Int)
forall lo up a.
(Content lo, Content up) =>
(lo, up) -> a -> a -> a -> a -> a
caseDiagUpLoSym (lo, up)
uplo
         (((Index size -> Int) -> (Index size, Index size) -> Int)
-> (Int, Index size -> Int)
-> (Int, (Index size, Index size) -> Int)
forall b c a. (b -> c) -> (a, b) -> (a, c)
mapSnd ((Index size -> Int)
-> ((Index size, Index size) -> Index size)
-> (Index size, Index size)
-> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Index size, Index size) -> Index size
forall a b. (a, b) -> b
snd) ((Int, Index size -> Int)
 -> (Int, (Index size, Index size) -> Int))
-> (Int, Index size -> Int)
-> (Int, (Index size, Index size) -> Int)
forall a b. (a -> b) -> a -> b
$ size -> (Int, Index size -> Int)
forall sh. Indexed sh => sh -> (Int, Index sh -> Int)
Shape.uncheckedSizeOffset size
size)
         (Order -> size -> (Int, (Index size, Index size) -> Int)
forall sh.
Indexed sh =>
Order -> sh -> (Int, (Index sh, Index sh) -> Int)
triangleUncheckedSizeOffset Order
order size
size)
         ((((Index size, Index size) -> Int)
 -> (Index size, Index size) -> Int)
-> (Int, (Index size, Index size) -> Int)
-> (Int, (Index size, Index size) -> Int)
forall b c a. (b -> c) -> (a, b) -> (a, c)
mapSnd (((Index size, Index size) -> Int)
-> ((Index size, Index size) -> (Index size, Index size))
-> (Index size, Index size)
-> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Index size, Index size) -> (Index size, Index size)
forall a b. (a, b) -> (b, a)
swap) ((Int, (Index size, Index size) -> Int)
 -> (Int, (Index size, Index size) -> Int))
-> (Int, (Index size, Index size) -> Int)
-> (Int, (Index size, Index size) -> Int)
forall a b. (a -> b) -> a -> b
$ Order -> size -> (Int, (Index size, Index size) -> Int)
forall sh.
Indexed sh =>
Order -> sh -> (Int, (Index sh, Index sh) -> Int)
triangleUncheckedSizeOffset (Order -> Order
flipOrder Order
order) size
size)
         (Order -> size -> (Int, (Index size, Index size) -> Int)
forall sh.
Indexed sh =>
Order -> sh -> (Int, (Index sh, Index sh) -> Int)
triangleUncheckedSizeOffset Order
order size
size)

   inBounds :: Triangular lo diag up size
-> Index (Triangular lo diag up size) -> Bool
inBounds (Triangular diag
_diag (lo, up)
uplo Order
_ size
size) ix :: Index (Triangular lo diag up size)
ix@(r,c) =
      (size, size) -> Index (size, size) -> Bool
forall sh. Indexed sh => sh -> Index sh -> Bool
Shape.inBounds (size
size,size
size) Index (size, size)
Index (Triangular lo diag up size)
ix
      Bool -> Bool -> Bool
&&
      (lo, up) -> Bool -> Bool -> Bool -> Bool -> Bool
forall lo up a.
(Content lo, Content up) =>
(lo, up) -> a -> a -> a -> a -> a
caseDiagUpLoSym (lo, up)
uplo
         (size -> Index size -> Int
forall sh. Indexed sh => sh -> Index sh -> Int
Shape.offset size
size Index size
r Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== size -> Index size -> Int
forall sh. Indexed sh => sh -> Index sh -> Int
Shape.offset size
size Index size
c)
         (size -> Index size -> Int
forall sh. Indexed sh => sh -> Index sh -> Int
Shape.offset size
size Index size
r Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= size -> Index size -> Int
forall sh. Indexed sh => sh -> Index sh -> Int
Shape.offset size
size Index size
c)
         (size -> Index size -> Int
forall sh. Indexed sh => sh -> Index sh -> Int
Shape.offset size
size Index size
r Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= size -> Index size -> Int
forall sh. Indexed sh => sh -> Index sh -> Int
Shape.offset size
size Index size
c)
         (size -> Index size -> Int
forall sh. Indexed sh => sh -> Index sh -> Int
Shape.offset size
size Index size
r Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= size -> Index size -> Int
forall sh. Indexed sh => sh -> Index sh -> Int
Shape.offset size
size Index size
c)

instance
   (Content lo, TriDiag diag, Content up, Shape.InvIndexed size) =>
      Shape.InvIndexed (Triangular lo diag up size) where

   indexFromOffset :: Triangular lo diag up size
-> Int -> Index (Triangular lo diag up size)
indexFromOffset (Triangular diag
_diag (lo, up)
uplo Order
order size
size) Int
k =
      (lo, up)
-> (Index size, Index size)
-> (Index size, Index size)
-> (Index size, Index size)
-> (Index size, Index size)
-> (Index size, Index size)
forall lo up a.
(Content lo, Content up) =>
(lo, up) -> a -> a -> a -> a -> a
caseDiagUpLoSym (lo, up)
uplo
         (Index size -> (Index size, Index size)
forall a. a -> (a, a)
double (Index size -> (Index size, Index size))
-> Index size -> (Index size, Index size)
forall a b. (a -> b) -> a -> b
$ size -> Int -> Index size
forall sh. InvIndexed sh => sh -> Int -> Index sh
Shape.indexFromOffset size
size Int
k)
         (Order -> size -> Int -> (Index size, Index size)
forall sh.
InvIndexed sh =>
Order -> sh -> Int -> (Index sh, Index sh)
triangleIndexFromOffset Order
order size
size Int
k)
         ((Index size, Index size) -> (Index size, Index size)
forall a b. (a, b) -> (b, a)
swap ((Index size, Index size) -> (Index size, Index size))
-> (Index size, Index size) -> (Index size, Index size)
forall a b. (a -> b) -> a -> b
$ Order -> size -> Int -> (Index size, Index size)
forall sh.
InvIndexed sh =>
Order -> sh -> Int -> (Index sh, Index sh)
triangleIndexFromOffset (Order -> Order
flipOrder Order
order) size
size Int
k)
         (Order -> size -> Int -> (Index size, Index size)
forall sh.
InvIndexed sh =>
Order -> sh -> Int -> (Index sh, Index sh)
triangleIndexFromOffset Order
order size
size Int
k)


triangleRootDouble :: Int -> Double
triangleRootDouble :: Int -> Double
triangleRootDouble = Double -> Double
forall a. Floating a => a -> a
triangleRoot (Double -> Double) -> (Int -> Double) -> Int -> Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral

triangleExtent :: String -> Int -> Int
triangleExtent :: String -> Int -> Int
triangleExtent String
name Int
size =
   let n :: Int
n = Double -> Int
forall a b. (RealFrac a, Integral b) => a -> b
round (Double -> Int) -> Double -> Int
forall a b. (a -> b) -> a -> b
$ Int -> Double
triangleRootDouble Int
size
   in if Int
size Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int -> Int
triangleSize Int
n
        then Int
n
        else String -> Int
forall a. HasCallStack => String -> a
error (String
name String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
": no triangular number of elements")

triangleIndices ::
   (Shape.Indexed sh) => Order -> sh -> [(Shape.Index sh, Shape.Index sh)]
triangleIndices :: Order -> sh -> [(Index sh, Index sh)]
triangleIndices Order
RowMajor = UpperTriangular sh -> [(Index sh, Index sh)]
forall sh. Indexed sh => sh -> [Index sh]
Shape.indices (UpperTriangular sh -> [(Index sh, Index sh)])
-> (sh -> UpperTriangular sh) -> sh -> [(Index sh, Index sh)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. sh -> UpperTriangular sh
forall size. size -> UpperTriangular size
Shape.upperTriangular
triangleIndices Order
ColumnMajor = ((Index sh, Index sh) -> (Index sh, Index sh))
-> [(Index sh, Index sh)] -> [(Index sh, Index sh)]
forall a b. (a -> b) -> [a] -> [b]
map (Index sh, Index sh) -> (Index sh, Index sh)
forall a b. (a, b) -> (b, a)
swap ([(Index sh, Index sh)] -> [(Index sh, Index sh)])
-> (sh -> [(Index sh, Index sh)]) -> sh -> [(Index sh, Index sh)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LowerTriangular sh -> [(Index sh, Index sh)]
forall sh. Indexed sh => sh -> [Index sh]
Shape.indices (LowerTriangular sh -> [(Index sh, Index sh)])
-> (sh -> LowerTriangular sh) -> sh -> [(Index sh, Index sh)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. sh -> LowerTriangular sh
forall size. size -> LowerTriangular size
Shape.lowerTriangular

triangleOffset ::
   (Shape.Indexed sh) => Order -> sh -> (Shape.Index sh, Shape.Index sh) -> Int
triangleOffset :: Order -> sh -> (Index sh, Index sh) -> Int
triangleOffset Order
order sh
size =
   case Order
order of
      Order
RowMajor -> UpperTriangular sh -> Index (UpperTriangular sh) -> Int
forall sh. Indexed sh => sh -> Index sh -> Int
Shape.offset (sh -> UpperTriangular sh
forall size. size -> UpperTriangular size
Shape.upperTriangular sh
size)
      Order
ColumnMajor -> LowerTriangular sh -> Index (LowerTriangular sh) -> Int
forall sh. Indexed sh => sh -> Index sh -> Int
Shape.offset (sh -> LowerTriangular sh
forall size. size -> LowerTriangular size
Shape.lowerTriangular sh
size) ((Index sh, Index sh) -> Int)
-> ((Index sh, Index sh) -> (Index sh, Index sh))
-> (Index sh, Index sh)
-> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Index sh, Index sh) -> (Index sh, Index sh)
forall a b. (a, b) -> (b, a)
swap

triangleUncheckedOffset ::
   (Shape.Indexed sh) => Order -> sh -> (Shape.Index sh, Shape.Index sh) -> Int
triangleUncheckedOffset :: Order -> sh -> (Index sh, Index sh) -> Int
triangleUncheckedOffset Order
order sh
size =
   case Order
order of
      Order
RowMajor -> UpperTriangular sh -> Index (UpperTriangular sh) -> Int
forall sh. Indexed sh => sh -> Index sh -> Int
Shape.uncheckedOffset (sh -> UpperTriangular sh
forall size. size -> UpperTriangular size
Shape.upperTriangular sh
size)
      Order
ColumnMajor -> LowerTriangular sh -> Index (LowerTriangular sh) -> Int
forall sh. Indexed sh => sh -> Index sh -> Int
Shape.uncheckedOffset (sh -> LowerTriangular sh
forall size. size -> LowerTriangular size
Shape.lowerTriangular sh
size) ((Index sh, Index sh) -> Int)
-> ((Index sh, Index sh) -> (Index sh, Index sh))
-> (Index sh, Index sh)
-> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Index sh, Index sh) -> (Index sh, Index sh)
forall a b. (a, b) -> (b, a)
swap

triangleSizeOffset ::
   (Shape.Indexed sh) =>
   Order -> sh -> (Int, (Shape.Index sh, Shape.Index sh) -> Int)
triangleSizeOffset :: Order -> sh -> (Int, (Index sh, Index sh) -> Int)
triangleSizeOffset Order
order sh
size =
   case Order
order of
      Order
RowMajor -> UpperTriangular sh -> (Int, Index (UpperTriangular sh) -> Int)
forall sh. Indexed sh => sh -> (Int, Index sh -> Int)
Shape.sizeOffset (sh -> UpperTriangular sh
forall size. size -> UpperTriangular size
Shape.upperTriangular sh
size)
      Order
ColumnMajor ->
         (((Index sh, Index sh) -> Int) -> (Index sh, Index sh) -> Int)
-> (Int, (Index sh, Index sh) -> Int)
-> (Int, (Index sh, Index sh) -> Int)
forall b c a. (b -> c) -> (a, b) -> (a, c)
mapSnd (((Index sh, Index sh) -> Int)
-> ((Index sh, Index sh) -> (Index sh, Index sh))
-> (Index sh, Index sh)
-> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Index sh, Index sh) -> (Index sh, Index sh)
forall a b. (a, b) -> (b, a)
swap) ((Int, (Index sh, Index sh) -> Int)
 -> (Int, (Index sh, Index sh) -> Int))
-> (Int, (Index sh, Index sh) -> Int)
-> (Int, (Index sh, Index sh) -> Int)
forall a b. (a -> b) -> a -> b
$ LowerTriangular sh -> (Int, Index (LowerTriangular sh) -> Int)
forall sh. Indexed sh => sh -> (Int, Index sh -> Int)
Shape.sizeOffset (sh -> LowerTriangular sh
forall size. size -> LowerTriangular size
Shape.lowerTriangular sh
size)

triangleUncheckedSizeOffset ::
   (Shape.Indexed sh) =>
   Order -> sh -> (Int, (Shape.Index sh, Shape.Index sh) -> Int)
triangleUncheckedSizeOffset :: Order -> sh -> (Int, (Index sh, Index sh) -> Int)
triangleUncheckedSizeOffset Order
order sh
size =
   case Order
order of
      Order
RowMajor -> UpperTriangular sh -> (Int, Index (UpperTriangular sh) -> Int)
forall sh. Indexed sh => sh -> (Int, Index sh -> Int)
Shape.uncheckedSizeOffset (sh -> UpperTriangular sh
forall size. size -> UpperTriangular size
Shape.upperTriangular sh
size)
      Order
ColumnMajor ->
         (((Index sh, Index sh) -> Int) -> (Index sh, Index sh) -> Int)
-> (Int, (Index sh, Index sh) -> Int)
-> (Int, (Index sh, Index sh) -> Int)
forall b c a. (b -> c) -> (a, b) -> (a, c)
mapSnd (((Index sh, Index sh) -> Int)
-> ((Index sh, Index sh) -> (Index sh, Index sh))
-> (Index sh, Index sh)
-> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Index sh, Index sh) -> (Index sh, Index sh)
forall a b. (a, b) -> (b, a)
swap) ((Int, (Index sh, Index sh) -> Int)
 -> (Int, (Index sh, Index sh) -> Int))
-> (Int, (Index sh, Index sh) -> Int)
-> (Int, (Index sh, Index sh) -> Int)
forall a b. (a -> b) -> a -> b
$ LowerTriangular sh -> (Int, Index (LowerTriangular sh) -> Int)
forall sh. Indexed sh => sh -> (Int, Index sh -> Int)
Shape.uncheckedSizeOffset (sh -> LowerTriangular sh
forall size. size -> LowerTriangular size
Shape.lowerTriangular sh
size)

triangleIndexFromOffset ::
   (Shape.InvIndexed sh) =>
   Order -> sh -> Int -> (Shape.Index sh, Shape.Index sh)
triangleIndexFromOffset :: Order -> sh -> Int -> (Index sh, Index sh)
triangleIndexFromOffset Order
order sh
size =
   case Order
order of
      Order
RowMajor -> UpperTriangular sh -> Int -> Index (UpperTriangular sh)
forall sh. InvIndexed sh => sh -> Int -> Index sh
Shape.indexFromOffset (sh -> UpperTriangular sh
forall size. size -> UpperTriangular size
Shape.upperTriangular sh
size)
      Order
ColumnMajor -> (Index sh, Index sh) -> (Index sh, Index sh)
forall a b. (a, b) -> (b, a)
swap ((Index sh, Index sh) -> (Index sh, Index sh))
-> (Int -> (Index sh, Index sh)) -> Int -> (Index sh, Index sh)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LowerTriangular sh -> Int -> Index (LowerTriangular sh)
forall sh. InvIndexed sh => sh -> Int -> Index sh
Shape.indexFromOffset (sh -> LowerTriangular sh
forall size. size -> LowerTriangular size
Shape.lowerTriangular sh
size)


type UnaryProxy a = Proxy (Unary.Un a)

data Banded sub super vert horiz height width =
   Banded {
      Banded sub super vert horiz height width
-> (UnaryProxy sub, UnaryProxy super)
bandedOffDiagonals :: (UnaryProxy sub, UnaryProxy super),
      Banded sub super vert horiz height width -> Order
bandedOrder :: Order,
      Banded sub super vert horiz height width
-> Extent vert horiz height width
bandedExtent :: Extent vert horiz height width
   } deriving (Banded sub super vert horiz height width
-> Banded sub super vert horiz height width -> Bool
(Banded sub super vert horiz height width
 -> Banded sub super vert horiz height width -> Bool)
-> (Banded sub super vert horiz height width
    -> Banded sub super vert horiz height width -> Bool)
-> Eq (Banded sub super vert horiz height width)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall sub super vert horiz height width.
(C vert, C horiz, Eq height, Eq width) =>
Banded sub super vert horiz height width
-> Banded sub super vert horiz height width -> Bool
/= :: Banded sub super vert horiz height width
-> Banded sub super vert horiz height width -> Bool
$c/= :: forall sub super vert horiz height width.
(C vert, C horiz, Eq height, Eq width) =>
Banded sub super vert horiz height width
-> Banded sub super vert horiz height width -> Bool
== :: Banded sub super vert horiz height width
-> Banded sub super vert horiz height width -> Bool
$c== :: forall sub super vert horiz height width.
(C vert, C horiz, Eq height, Eq width) =>
Banded sub super vert horiz height width
-> Banded sub super vert horiz height width -> Bool
Eq, Int -> Banded sub super vert horiz height width -> ShowS
[Banded sub super vert horiz height width] -> ShowS
Banded sub super vert horiz height width -> String
(Int -> Banded sub super vert horiz height width -> ShowS)
-> (Banded sub super vert horiz height width -> String)
-> ([Banded sub super vert horiz height width] -> ShowS)
-> Show (Banded sub super vert horiz height width)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall sub super vert horiz height width.
(Natural sub, Natural super, C vert, C horiz, Show height,
 Show width) =>
Int -> Banded sub super vert horiz height width -> ShowS
forall sub super vert horiz height width.
(Natural sub, Natural super, C vert, C horiz, Show height,
 Show width) =>
[Banded sub super vert horiz height width] -> ShowS
forall sub super vert horiz height width.
(Natural sub, Natural super, C vert, C horiz, Show height,
 Show width) =>
Banded sub super vert horiz height width -> String
showList :: [Banded sub super vert horiz height width] -> ShowS
$cshowList :: forall sub super vert horiz height width.
(Natural sub, Natural super, C vert, C horiz, Show height,
 Show width) =>
[Banded sub super vert horiz height width] -> ShowS
show :: Banded sub super vert horiz height width -> String
$cshow :: forall sub super vert horiz height width.
(Natural sub, Natural super, C vert, C horiz, Show height,
 Show width) =>
Banded sub super vert horiz height width -> String
showsPrec :: Int -> Banded sub super vert horiz height width -> ShowS
$cshowsPrec :: forall sub super vert horiz height width.
(Natural sub, Natural super, C vert, C horiz, Show height,
 Show width) =>
Int -> Banded sub super vert horiz height width -> ShowS
Show)

type BandedGeneral sub super = Banded sub super Extent.Big Extent.Big
type BandedSquare sub super size =
      Banded sub super Extent.Small Extent.Small size size

type BandedLowerTriangular sub size = BandedSquare sub TypeNum.U0 size
type BandedUpperTriangular super size = BandedSquare TypeNum.U0 super size

type BandedDiagonal size = BandedSquare TypeNum.U0 TypeNum.U0 size


bandedHeight ::
   (Extent.C vert, Extent.C horiz) =>
   Banded sub super vert horiz height width -> height
bandedHeight :: Banded sub super vert horiz height width -> height
bandedHeight = Extent vert horiz height width -> height
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> height
Extent.height (Extent vert horiz height width -> height)
-> (Banded sub super vert horiz height width
    -> Extent vert horiz height width)
-> Banded sub super vert horiz height width
-> height
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Banded sub super vert horiz height width
-> Extent vert horiz height width
forall sub super vert horiz height width.
Banded sub super vert horiz height width
-> Extent vert horiz height width
bandedExtent

bandedWidth ::
   (Extent.C vert, Extent.C horiz) =>
   Banded sub super vert horiz height width -> width
bandedWidth :: Banded sub super vert horiz height width -> width
bandedWidth = Extent vert horiz height width -> width
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> width
Extent.width (Extent vert horiz height width -> width)
-> (Banded sub super vert horiz height width
    -> Extent vert horiz height width)
-> Banded sub super vert horiz height width
-> width
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Banded sub super vert horiz height width
-> Extent vert horiz height width
forall sub super vert horiz height width.
Banded sub super vert horiz height width
-> Extent vert horiz height width
bandedExtent

bandedMapExtent ::
   Extent.Map vertA horizA vertB horizB height width ->
   Banded sub super vertA horizA height width ->
   Banded sub super vertB horizB height width
bandedMapExtent :: Map vertA horizA vertB horizB height width
-> Banded sub super vertA horizA height width
-> Banded sub super vertB horizB height width
bandedMapExtent Map vertA horizA vertB horizB height width
f (Banded (UnaryProxy sub, UnaryProxy super)
numDiag Order
order Extent vertA horizA height width
extent) =
   (UnaryProxy sub, UnaryProxy super)
-> Order
-> Extent vertB horizB height width
-> Banded sub super vertB horizB height width
forall sub super vert horiz height width.
(UnaryProxy sub, UnaryProxy super)
-> Order
-> Extent vert horiz height width
-> Banded sub super vert horiz height width
Banded (UnaryProxy sub, UnaryProxy super)
numDiag Order
order (Extent vertB horizB height width
 -> Banded sub super vertB horizB height width)
-> Extent vertB horizB height width
-> Banded sub super vertB horizB height width
forall a b. (a -> b) -> a -> b
$ Map vertA horizA vertB horizB height width
-> Extent vertA horizA height width
-> Extent vertB horizB height width
forall vertA horizA vertB horizB height width.
Map vertA horizA vertB horizB height width
-> Extent vertA horizA height width
-> Extent vertB horizB height width
Extent.apply Map vertA horizA vertB horizB height width
f Extent vertA horizA height width
extent

instance
   (Unary.Natural sub, Unary.Natural super, Extent.C vert, Extent.C horiz,
    Shape.C height, Shape.C width) =>
      Box.Box (Banded sub super vert horiz height width) where
   type HeightOf (Banded sub super vert horiz height width) = height
   type WidthOf (Banded sub super vert horiz height width) = width
   height :: Banded sub super vert horiz height width
-> HeightOf (Banded sub super vert horiz height width)
height = Banded sub super vert horiz height width
-> HeightOf (Banded sub super vert horiz height width)
forall vert horiz sub super height width.
(C vert, C horiz) =>
Banded sub super vert horiz height width -> height
bandedHeight
   width :: Banded sub super vert horiz height width
-> WidthOf (Banded sub super vert horiz height width)
width = Banded sub super vert horiz height width
-> WidthOf (Banded sub super vert horiz height width)
forall vert horiz sub super height width.
(C vert, C horiz) =>
Banded sub super vert horiz height width -> width
bandedWidth

bandedBreadth ::
   (Unary.Natural sub, Unary.Natural super) =>
   (UnaryProxy sub, UnaryProxy super) -> Int
bandedBreadth :: (UnaryProxy sub, UnaryProxy super) -> Int
bandedBreadth (UnaryProxy sub
sub,UnaryProxy super
super) =
   UnaryProxy sub -> Int
forall x y. (Integer x, Num y) => Proxy x -> y
integralFromProxy UnaryProxy sub
sub Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ UnaryProxy super -> Int
forall x y. (Integer x, Num y) => Proxy x -> y
integralFromProxy UnaryProxy super
super

numOffDiagonals ::
   (Unary.Natural sub, Unary.Natural super) =>
   Order -> (UnaryProxy sub, UnaryProxy super) -> (Int,Int)
numOffDiagonals :: Order -> (UnaryProxy sub, UnaryProxy super) -> (Int, Int)
numOffDiagonals Order
order (UnaryProxy sub
sub,UnaryProxy super
super) =
   Order -> (Int, Int) -> (Int, Int)
forall a. Order -> (a, a) -> (a, a)
swapOnRowMajor Order
order (UnaryProxy sub -> Int
forall x y. (Integer x, Num y) => Proxy x -> y
integralFromProxy UnaryProxy sub
sub, UnaryProxy super -> Int
forall x y. (Integer x, Num y) => Proxy x -> y
integralFromProxy UnaryProxy super
super)

natFromProxy :: (Unary.Natural n) => UnaryProxy n -> Proof.Nat n
natFromProxy :: UnaryProxy n -> Nat n
natFromProxy UnaryProxy n
Proxy = Nat n
forall x. Natural x => Nat x
Proof.Nat

addOffDiagonals ::
   (Unary.Natural subA, Unary.Natural superA,
    Unary.Natural subB, Unary.Natural superB,
    (subA :+: subB) ~ subC,
    (superA :+: superB) ~ superC) =>
   (UnaryProxy subA, UnaryProxy superA) ->
   (UnaryProxy subB, UnaryProxy superB) ->
   ((Proof.Nat subC, Proof.Nat superC),
    (UnaryProxy subC, UnaryProxy superC))
addOffDiagonals :: (UnaryProxy subA, UnaryProxy superA)
-> (UnaryProxy subB, UnaryProxy superB)
-> ((Nat subC, Nat superC), (UnaryProxy subC, UnaryProxy superC))
addOffDiagonals (UnaryProxy subA
subA,UnaryProxy superA
superA) (UnaryProxy subB
subB,UnaryProxy superB
superB) =
   ((Nat subA -> Nat subB -> Nat (subA :+: subB)
forall x y. Nat x -> Nat y -> Nat (x :+: y)
Proof.addNat (UnaryProxy subA -> Nat subA
forall n. Natural n => UnaryProxy n -> Nat n
natFromProxy UnaryProxy subA
subA) (UnaryProxy subB -> Nat subB
forall n. Natural n => UnaryProxy n -> Nat n
natFromProxy UnaryProxy subB
subB),
     Nat superA -> Nat superB -> Nat (superA :+: superB)
forall x y. Nat x -> Nat y -> Nat (x :+: y)
Proof.addNat (UnaryProxy superA -> Nat superA
forall n. Natural n => UnaryProxy n -> Nat n
natFromProxy UnaryProxy superA
superA) (UnaryProxy superB -> Nat superB
forall n. Natural n => UnaryProxy n -> Nat n
natFromProxy UnaryProxy superB
superB)),
    (UnaryProxy subC
forall a. Proxy a
Proxy,UnaryProxy superC
forall a. Proxy a
Proxy))

bandedTranspose ::
   (Extent.C vert, Extent.C horiz) =>
   Banded sub super vert horiz height width ->
   Banded super sub horiz vert width height
bandedTranspose :: Banded sub super vert horiz height width
-> Banded super sub horiz vert width height
bandedTranspose (Banded (UnaryProxy sub
sub,UnaryProxy super
super) Order
order Extent vert horiz height width
extent) =
   (UnaryProxy super, UnaryProxy sub)
-> Order
-> Extent horiz vert width height
-> Banded super sub horiz vert width height
forall sub super vert horiz height width.
(UnaryProxy sub, UnaryProxy super)
-> Order
-> Extent vert horiz height width
-> Banded sub super vert horiz height width
Banded (UnaryProxy super
super,UnaryProxy sub
sub) (Order -> Order
flipOrder Order
order) (Extent vert horiz height width -> Extent horiz vert width height
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> Extent horiz vert width height
Extent.transpose Extent vert horiz height width
extent)


bandedGeneral ::
   (UnaryProxy sub, UnaryProxy super) -> Order -> height -> width ->
   Banded sub super Extent.Big Extent.Big height width
bandedGeneral :: (UnaryProxy sub, UnaryProxy super)
-> Order
-> height
-> width
-> Banded sub super Big Big height width
bandedGeneral (UnaryProxy sub, UnaryProxy super)
offDiag Order
order height
height width
width =
   (UnaryProxy sub, UnaryProxy super)
-> Order
-> Extent Big Big height width
-> Banded sub super Big Big height width
forall sub super vert horiz height width.
(UnaryProxy sub, UnaryProxy super)
-> Order
-> Extent vert horiz height width
-> Banded sub super vert horiz height width
Banded (UnaryProxy sub, UnaryProxy super)
offDiag Order
order (height -> width -> Extent Big Big height width
forall height width. height -> width -> General height width
Extent.general height
height width
width)

bandedSquare ::
   (UnaryProxy sub, UnaryProxy super) -> Order -> size ->
   Banded sub super Extent.Small Extent.Small size size
bandedSquare :: (UnaryProxy sub, UnaryProxy super)
-> Order -> size -> Banded sub super Small Small size size
bandedSquare (UnaryProxy sub, UnaryProxy super)
offDiag Order
order = (UnaryProxy sub, UnaryProxy super)
-> Order
-> Extent Small Small size size
-> Banded sub super Small Small size size
forall sub super vert horiz height width.
(UnaryProxy sub, UnaryProxy super)
-> Order
-> Extent vert horiz height width
-> Banded sub super vert horiz height width
Banded (UnaryProxy sub, UnaryProxy super)
offDiag Order
order (Extent Small Small size size
 -> Banded sub super Small Small size size)
-> (size -> Extent Small Small size size)
-> size
-> Banded sub super Small Small size size
forall b c a. (b -> c) -> (a -> b) -> a -> c
. size -> Extent Small Small size size
forall sh. sh -> Square sh
Extent.square


data BandedIndex row column =
     InsideBox row column
   | VertOutsideBox Int column
   | HorizOutsideBox row Int
   deriving (BandedIndex row column -> BandedIndex row column -> Bool
(BandedIndex row column -> BandedIndex row column -> Bool)
-> (BandedIndex row column -> BandedIndex row column -> Bool)
-> Eq (BandedIndex row column)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall row column.
(Eq row, Eq column) =>
BandedIndex row column -> BandedIndex row column -> Bool
/= :: BandedIndex row column -> BandedIndex row column -> Bool
$c/= :: forall row column.
(Eq row, Eq column) =>
BandedIndex row column -> BandedIndex row column -> Bool
== :: BandedIndex row column -> BandedIndex row column -> Bool
$c== :: forall row column.
(Eq row, Eq column) =>
BandedIndex row column -> BandedIndex row column -> Bool
Eq, Int -> BandedIndex row column -> ShowS
[BandedIndex row column] -> ShowS
BandedIndex row column -> String
(Int -> BandedIndex row column -> ShowS)
-> (BandedIndex row column -> String)
-> ([BandedIndex row column] -> ShowS)
-> Show (BandedIndex row column)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall row column.
(Show row, Show column) =>
Int -> BandedIndex row column -> ShowS
forall row column.
(Show row, Show column) =>
[BandedIndex row column] -> ShowS
forall row column.
(Show row, Show column) =>
BandedIndex row column -> String
showList :: [BandedIndex row column] -> ShowS
$cshowList :: forall row column.
(Show row, Show column) =>
[BandedIndex row column] -> ShowS
show :: BandedIndex row column -> String
$cshow :: forall row column.
(Show row, Show column) =>
BandedIndex row column -> String
showsPrec :: Int -> BandedIndex row column -> ShowS
$cshowsPrec :: forall row column.
(Show row, Show column) =>
Int -> BandedIndex row column -> ShowS
Show)

instance
   (Unary.Natural sub, Unary.Natural super,
    Extent.C vert, Extent.C horiz, NFData height, NFData width) =>
      NFData (Banded sub super vert horiz height width) where
   rnf :: Banded sub super vert horiz height width -> ()
rnf (Banded (UnaryProxy sub
Proxy,UnaryProxy super
Proxy) Order
order Extent vert horiz height width
extent) = (Order, Extent vert horiz height width) -> ()
forall a. NFData a => a -> ()
rnf (Order
order, Extent vert horiz height width
extent)

instance
   (Unary.Natural sub, Unary.Natural super,
    Extent.C vert, Extent.C horiz, Shape.C height, Shape.C width) =>
      Shape.C (Banded sub super vert horiz height width) where

   size :: Banded sub super vert horiz height width -> Int
size (Banded (UnaryProxy sub, UnaryProxy super)
offDiag Order
order Extent vert horiz height width
extent) =
      (UnaryProxy sub, UnaryProxy super) -> Int
forall sub super.
(Natural sub, Natural super) =>
(UnaryProxy sub, UnaryProxy super) -> Int
bandedBreadth (UnaryProxy sub, UnaryProxy super)
offDiag Int -> Int -> Int
forall a. Num a => a -> a -> a
*
      case Order
order of
         Order
RowMajor -> height -> Int
forall sh. C sh => sh -> Int
Shape.size (Extent vert horiz height width -> height
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> height
Extent.height Extent vert horiz height width
extent)
         Order
ColumnMajor -> width -> Int
forall sh. C sh => sh -> Int
Shape.size (Extent vert horiz height width -> width
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> width
Extent.width Extent vert horiz height width
extent)
   uncheckedSize :: Banded sub super vert horiz height width -> Int
uncheckedSize (Banded (UnaryProxy sub, UnaryProxy super)
offDiag Order
order Extent vert horiz height width
extent) =
      (UnaryProxy sub, UnaryProxy super) -> Int
forall sub super.
(Natural sub, Natural super) =>
(UnaryProxy sub, UnaryProxy super) -> Int
bandedBreadth (UnaryProxy sub, UnaryProxy super)
offDiag Int -> Int -> Int
forall a. Num a => a -> a -> a
*
      case Order
order of
         Order
RowMajor -> height -> Int
forall sh. C sh => sh -> Int
Shape.uncheckedSize (Extent vert horiz height width -> height
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> height
Extent.height Extent vert horiz height width
extent)
         Order
ColumnMajor -> width -> Int
forall sh. C sh => sh -> Int
Shape.uncheckedSize (Extent vert horiz height width -> width
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> width
Extent.width Extent vert horiz height width
extent)

instance
   (Unary.Natural sub, Unary.Natural super,
    Extent.C vert, Extent.C horiz, Shape.Indexed height, Shape.Indexed width) =>
      Shape.Indexed (Banded sub super vert horiz height width) where

   type Index (Banded sub super vert horiz height width) =
            BandedIndex (Shape.Index height) (Shape.Index width)
   indices :: Banded sub super vert horiz height width
-> [Index (Banded sub super vert horiz height width)]
indices (Banded (UnaryProxy sub
sub,UnaryProxy super
super) Order
order Extent vert horiz height width
extent) =
      let (height
height,width
width) = Extent vert horiz height width -> (height, width)
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> (height, width)
Extent.dimensions Extent vert horiz height width
extent
      in case Order
order of
            Order
RowMajor ->
               ((Index height, Either Int (Index width))
 -> BandedIndex (Index height) (Index width))
-> [(Index height, Either Int (Index width))]
-> [BandedIndex (Index height) (Index width)]
forall a b. (a -> b) -> [a] -> [b]
map (\(Index height
r,Either Int (Index width)
c) -> (Int -> BandedIndex (Index height) (Index width))
-> (Index width -> BandedIndex (Index height) (Index width))
-> Either Int (Index width)
-> BandedIndex (Index height) (Index width)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Index height -> Int -> BandedIndex (Index height) (Index width)
forall row column. row -> Int -> BandedIndex row column
HorizOutsideBox Index height
r) (Index height
-> Index width -> BandedIndex (Index height) (Index width)
forall row column. row -> column -> BandedIndex row column
InsideBox Index height
r) Either Int (Index width)
c) ([(Index height, Either Int (Index width))]
 -> [BandedIndex (Index height) (Index width)])
-> [(Index height, Either Int (Index width))]
-> [BandedIndex (Index height) (Index width)]
forall a b. (a -> b) -> a -> b
$
               (UnaryProxy sub, UnaryProxy super)
-> (height, width) -> [(Index height, Either Int (Index width))]
forall sub super height width.
(Natural sub, Natural super, Indexed height, Indexed width) =>
(UnaryProxy sub, UnaryProxy super)
-> (height, width) -> [(Index height, Either Int (Index width))]
bandedIndicesRowMajor (UnaryProxy sub
sub,UnaryProxy super
super) (height
height,width
width)
            Order
ColumnMajor ->
               ((Index width, Either Int (Index height))
 -> BandedIndex (Index height) (Index width))
-> [(Index width, Either Int (Index height))]
-> [BandedIndex (Index height) (Index width)]
forall a b. (a -> b) -> [a] -> [b]
map (\(Index width
c,Either Int (Index height)
r) ->
                     (Int -> BandedIndex (Index height) (Index width))
-> (Index height -> BandedIndex (Index height) (Index width))
-> Either Int (Index height)
-> BandedIndex (Index height) (Index width)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either ((Int -> Index width -> BandedIndex (Index height) (Index width))
-> Index width -> Int -> BandedIndex (Index height) (Index width)
forall a b c. (a -> b -> c) -> b -> a -> c
flip Int -> Index width -> BandedIndex (Index height) (Index width)
forall row column. Int -> column -> BandedIndex row column
VertOutsideBox Index width
c) ((Index height
 -> Index width -> BandedIndex (Index height) (Index width))
-> Index width
-> Index height
-> BandedIndex (Index height) (Index width)
forall a b c. (a -> b -> c) -> b -> a -> c
flip Index height
-> Index width -> BandedIndex (Index height) (Index width)
forall row column. row -> column -> BandedIndex row column
InsideBox Index width
c) Either Int (Index height)
r) ([(Index width, Either Int (Index height))]
 -> [BandedIndex (Index height) (Index width)])
-> [(Index width, Either Int (Index height))]
-> [BandedIndex (Index height) (Index width)]
forall a b. (a -> b) -> a -> b
$
               (UnaryProxy super, UnaryProxy sub)
-> (width, height) -> [(Index width, Either Int (Index height))]
forall sub super height width.
(Natural sub, Natural super, Indexed height, Indexed width) =>
(UnaryProxy sub, UnaryProxy super)
-> (height, width) -> [(Index height, Either Int (Index width))]
bandedIndicesRowMajor (UnaryProxy super
super,UnaryProxy sub
sub) (width
width,height
height)

   offset :: Banded sub super vert horiz height width
-> Index (Banded sub super vert horiz height width) -> Int
offset Banded sub super vert horiz height width
shape Index (Banded sub super vert horiz height width)
ix =
      if Banded sub super vert horiz height width
-> Index (Banded sub super vert horiz height width) -> Bool
forall sh. Indexed sh => sh -> Index sh -> Bool
Shape.inBounds Banded sub super vert horiz height width
shape Index (Banded sub super vert horiz height width)
ix
         then Banded sub super vert horiz height width
-> Index (Banded sub super vert horiz height width) -> Int
forall sh. Indexed sh => sh -> Index sh -> Int
Shape.uncheckedOffset Banded sub super vert horiz height width
shape Index (Banded sub super vert horiz height width)
ix
         else String -> Int
forall a. HasCallStack => String -> a
error String
"Banded.offset: index outside band"

   uncheckedOffset :: Banded sub super vert horiz height width
-> Index (Banded sub super vert horiz height width) -> Int
uncheckedOffset (Banded (UnaryProxy sub
sub,UnaryProxy super
super) Order
order Extent vert horiz height width
extent) Index (Banded sub super vert horiz height width)
ix =
      let (height
height,width
width) = Extent vert horiz height width -> (height, width)
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> (height, width)
Extent.dimensions Extent vert horiz height width
extent
          kl :: Int
kl = UnaryProxy sub -> Int
forall x y. (Integer x, Num y) => Proxy x -> y
integralFromProxy UnaryProxy sub
sub
          ku :: Int
ku = UnaryProxy super -> Int
forall x y. (Integer x, Num y) => Proxy x -> y
integralFromProxy UnaryProxy super
super
      in (Int, Int)
-> Order
-> (height, width)
-> BandedIndex (Index height) (Index width)
-> Int
forall height width.
(Indexed height, Indexed width) =>
(Int, Int)
-> Order
-> (height, width)
-> BandedIndex (Index height) (Index width)
-> Int
bandedOffset (Int
kl,Int
ku) Order
order (height
height,width
width) Index (Banded sub super vert horiz height width)
BandedIndex (Index height) (Index width)
ix

   inBounds :: Banded sub super vert horiz height width
-> Index (Banded sub super vert horiz height width) -> Bool
inBounds (Banded (UnaryProxy sub
sub,UnaryProxy super
super) Order
order Extent vert horiz height width
extent) Index (Banded sub super vert horiz height width)
ix =
      let (height
height,width
width) = Extent vert horiz height width -> (height, width)
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> (height, width)
Extent.dimensions Extent vert horiz height width
extent
          kl :: Int
kl = UnaryProxy sub -> Int
forall x y. (Integer x, Num y) => Proxy x -> y
integralFromProxy UnaryProxy sub
sub
          ku :: Int
ku = UnaryProxy super -> Int
forall x y. (Integer x, Num y) => Proxy x -> y
integralFromProxy UnaryProxy super
super
          insideBand :: Int -> Int -> Bool
insideBand Int
r Int
c = Range Int -> Index (Range Int) -> Bool
forall sh. Indexed sh => sh -> Index sh -> Bool
Shape.inBounds (Int -> Int -> Range Int
forall n. n -> n -> Range n
Shape.Range (-Int
kl) Int
ku) (Int
cInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
r)
      in case (Order
order,Index (Banded sub super vert horiz height width)
BandedIndex (Index height) (Index width)
ix) of
            (Order
_, InsideBox Index height
r Index width
c) ->
               (height, width) -> Index (height, width) -> Bool
forall sh. Indexed sh => sh -> Index sh -> Bool
Shape.inBounds (height
height,width
width) (Index height
r,Index width
c)
               Bool -> Bool -> Bool
&&
               Int -> Int -> Bool
insideBand (height -> Index height -> Int
forall sh. Indexed sh => sh -> Index sh -> Int
Shape.offset height
height Index height
r) (width -> Index width -> Int
forall sh. Indexed sh => sh -> Index sh -> Int
Shape.offset width
width Index width
c)
            (Order
RowMajor, HorizOutsideBox Index height
r Int
c) ->
               height -> Index height -> Bool
forall sh. Indexed sh => sh -> Index sh -> Bool
Shape.inBounds height
height Index height
r
               Bool -> Bool -> Bool
&&
               Int -> Int -> Bool
insideBand (height -> Index height -> Int
forall sh. Indexed sh => sh -> Index sh -> Int
Shape.offset height
height Index height
r) (width -> Int -> Int
forall sh. C sh => sh -> Int -> Int
outsideOffset width
width Int
c)
            (Order
ColumnMajor, VertOutsideBox Int
r Index width
c) ->
               width -> Index width -> Bool
forall sh. Indexed sh => sh -> Index sh -> Bool
Shape.inBounds width
width Index width
c
               Bool -> Bool -> Bool
&&
               Int -> Int -> Bool
insideBand (height -> Int -> Int
forall sh. C sh => sh -> Int -> Int
outsideOffset height
height Int
r) (width -> Index width -> Int
forall sh. Indexed sh => sh -> Index sh -> Int
Shape.offset width
width Index width
c)
            (Order, BandedIndex (Index height) (Index width))
_ -> Bool
False

instance
   (Unary.Natural sub, Unary.Natural super, Extent.C vert, Extent.C horiz,
    Shape.InvIndexed height, Shape.InvIndexed width) =>
      Shape.InvIndexed (Banded sub super vert horiz height width) where

   indexFromOffset :: Banded sub super vert horiz height width
-> Int -> Index (Banded sub super vert horiz height width)
indexFromOffset (Banded (UnaryProxy sub
sub,UnaryProxy super
super) Order
order Extent vert horiz height width
extent) Int
j =
      (height -> Int -> Index height)
-> (width -> Int -> Index width)
-> (Int, Int)
-> Order
-> (height, width)
-> Int
-> BandedIndex (Index height) (Index width)
forall height width row column.
(C height, C width) =>
(height -> Int -> row)
-> (width -> Int -> column)
-> (Int, Int)
-> Order
-> (height, width)
-> Int
-> BandedIndex row column
bandedIndexFromOffset
         height -> Int -> Index height
forall sh. InvIndexed sh => sh -> Int -> Index sh
Shape.indexFromOffset width -> Int -> Index width
forall sh. InvIndexed sh => sh -> Int -> Index sh
Shape.indexFromOffset
         (UnaryProxy sub -> Int
forall x y. (Integer x, Num y) => Proxy x -> y
integralFromProxy UnaryProxy sub
sub, UnaryProxy super -> Int
forall x y. (Integer x, Num y) => Proxy x -> y
integralFromProxy UnaryProxy super
super) Order
order
         (Extent vert horiz height width -> (height, width)
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> (height, width)
Extent.dimensions Extent vert horiz height width
extent) Int
j

   uncheckedIndexFromOffset :: Banded sub super vert horiz height width
-> Int -> Index (Banded sub super vert horiz height width)
uncheckedIndexFromOffset (Banded (UnaryProxy sub
sub,UnaryProxy super
super) Order
order Extent vert horiz height width
extent) Int
j =
      (height -> Int -> Index height)
-> (width -> Int -> Index width)
-> (Int, Int)
-> Order
-> (height, width)
-> Int
-> BandedIndex (Index height) (Index width)
forall height width row column.
(C height, C width) =>
(height -> Int -> row)
-> (width -> Int -> column)
-> (Int, Int)
-> Order
-> (height, width)
-> Int
-> BandedIndex row column
bandedIndexFromOffset
         height -> Int -> Index height
forall sh. InvIndexed sh => sh -> Int -> Index sh
Shape.uncheckedIndexFromOffset width -> Int -> Index width
forall sh. InvIndexed sh => sh -> Int -> Index sh
Shape.uncheckedIndexFromOffset
         (UnaryProxy sub -> Int
forall x y. (Integer x, Num y) => Proxy x -> y
integralFromProxy UnaryProxy sub
sub, UnaryProxy super -> Int
forall x y. (Integer x, Num y) => Proxy x -> y
integralFromProxy UnaryProxy super
super) Order
order
         (Extent vert horiz height width -> (height, width)
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> (height, width)
Extent.dimensions Extent vert horiz height width
extent) Int
j

outsideOffset :: Shape.C sh => sh -> Int -> Int
outsideOffset :: sh -> Int -> Int
outsideOffset sh
size Int
k = if Int
kInt -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<Int
0 then Int
k else sh -> Int
forall sh. C sh => sh -> Int
Shape.size sh
size Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
k

bandedOffset ::
   (Shape.Indexed height, Shape.Indexed width) =>
   (Int, Int) -> Order -> (height, width) ->
   BandedIndex (Shape.Index height) (Shape.Index width) -> Int
bandedOffset :: (Int, Int)
-> Order
-> (height, width)
-> BandedIndex (Index height) (Index width)
-> Int
bandedOffset (Int
kl,Int
ku) Order
order (height
height,width
width) BandedIndex (Index height) (Index width)
ix =
   let k :: Int
k = Int
klInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
ku
   in case BandedIndex (Index height) (Index width)
ix of
         InsideBox Index height
r Index width
c ->
            let i :: Int
i = height -> Index height -> Int
forall sh. Indexed sh => sh -> Index sh -> Int
Shape.uncheckedOffset height
height Index height
r
                j :: Int
j = width -> Index width -> Int
forall sh. Indexed sh => sh -> Index sh -> Int
Shape.uncheckedOffset width
width Index width
c
            in case Order
order of
                  Order
RowMajor -> Int
kInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
klInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
j
                  Order
ColumnMajor -> Int
kInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
j Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
kuInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
i
         VertOutsideBox Int
r Index width
c ->
            let i :: Int
i = height -> Int -> Int
forall sh. C sh => sh -> Int -> Int
outsideOffset height
height Int
r
                j :: Int
j = width -> Index width -> Int
forall sh. Indexed sh => sh -> Index sh -> Int
Shape.uncheckedOffset width
width Index width
c
            in  Int
kInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
j Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
kuInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
i
         HorizOutsideBox Index height
r Int
c ->
            let i :: Int
i = height -> Index height -> Int
forall sh. Indexed sh => sh -> Index sh -> Int
Shape.uncheckedOffset height
height Index height
r
                j :: Int
j = width -> Int -> Int
forall sh. C sh => sh -> Int -> Int
outsideOffset width
width Int
c
            in  Int
kInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
klInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
j

bandedIndicesRowMajor ::
   (Unary.Natural sub, Unary.Natural super,
    Shape.Indexed height, Shape.Indexed width) =>
   (UnaryProxy sub, UnaryProxy super) ->
   (height, width) ->
   [(Shape.Index height, Either Int (Shape.Index width))]
bandedIndicesRowMajor :: (UnaryProxy sub, UnaryProxy super)
-> (height, width) -> [(Index height, Either Int (Index width))]
bandedIndicesRowMajor (UnaryProxy sub
sub,UnaryProxy super
super) (height
height,width
width) =
   let kl :: Int
kl = UnaryProxy sub -> Int
forall x y. (Integer x, Num y) => Proxy x -> y
integralFromProxy UnaryProxy sub
sub
       ku :: Int
ku = UnaryProxy super -> Int
forall x y. (Integer x, Num y) => Proxy x -> y
integralFromProxy UnaryProxy super
super
   in [[(Index height, Either Int (Index width))]]
-> [(Index height, Either Int (Index width))]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[(Index height, Either Int (Index width))]]
 -> [(Index height, Either Int (Index width))])
-> [[(Index height, Either Int (Index width))]]
-> [(Index height, Either Int (Index width))]
forall a b. (a -> b) -> a -> b
$
      (Index height
 -> [Either Int (Index width)]
 -> [(Index height, Either Int (Index width))])
-> [Index height]
-> [[Either Int (Index width)]]
-> [[(Index height, Either Int (Index width))]]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (\Index height
r -> (Either Int (Index width)
 -> (Index height, Either Int (Index width)))
-> [Either Int (Index width)]
-> [(Index height, Either Int (Index width))]
forall a b. (a -> b) -> [a] -> [b]
map ((,) Index height
r)) (height -> [Index height]
forall sh. Indexed sh => sh -> [Index sh]
Shape.indices height
height) ([[Either Int (Index width)]]
 -> [[(Index height, Either Int (Index width))]])
-> [[Either Int (Index width)]]
-> [[(Index height, Either Int (Index width))]]
forall a b. (a -> b) -> a -> b
$
      ([Either Int (Index width)] -> [Either Int (Index width)])
-> [[Either Int (Index width)]] -> [[Either Int (Index width)]]
forall a b. (a -> b) -> [a] -> [b]
map (Int -> [Either Int (Index width)] -> [Either Int (Index width)]
forall a. Int -> [a] -> [a]
take (Int
klInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
ku)) ([[Either Int (Index width)]] -> [[Either Int (Index width)]])
-> [[Either Int (Index width)]] -> [[Either Int (Index width)]]
forall a b. (a -> b) -> a -> b
$ [Either Int (Index width)] -> [[Either Int (Index width)]]
forall a. [a] -> [[a]]
tails ([Either Int (Index width)] -> [[Either Int (Index width)]])
-> [Either Int (Index width)] -> [[Either Int (Index width)]]
forall a b. (a -> b) -> a -> b
$
         ((Int -> Either Int (Index width))
-> [Int] -> [Either Int (Index width)]
forall a b. (a -> b) -> [a] -> [b]
map Int -> Either Int (Index width)
forall a b. a -> Either a b
Left ([Int] -> [Either Int (Index width)])
-> [Int] -> [Either Int (Index width)]
forall a b. (a -> b) -> a -> b
$ Int -> [Int] -> [Int]
forall a. Int -> [a] -> [a]
take Int
kl ([Int] -> [Int]) -> [Int] -> [Int]
forall a b. (a -> b) -> a -> b
$ (Int -> Int) -> Int -> [Int]
forall a. (a -> a) -> a -> [a]
iterate (Int
1Int -> Int -> Int
forall a. Num a => a -> a -> a
+) (-Int
kl)) [Either Int (Index width)]
-> [Either Int (Index width)] -> [Either Int (Index width)]
forall a. [a] -> [a] -> [a]
++
         ((Index width -> Either Int (Index width))
-> [Index width] -> [Either Int (Index width)]
forall a b. (a -> b) -> [a] -> [b]
map Index width -> Either Int (Index width)
forall a b. b -> Either a b
Right ([Index width] -> [Either Int (Index width)])
-> [Index width] -> [Either Int (Index width)]
forall a b. (a -> b) -> a -> b
$ width -> [Index width]
forall sh. Indexed sh => sh -> [Index sh]
Shape.indices width
width) [Either Int (Index width)]
-> [Either Int (Index width)] -> [Either Int (Index width)]
forall a. [a] -> [a] -> [a]
++
         ((Int -> Either Int (Index width))
-> [Int] -> [Either Int (Index width)]
forall a b. (a -> b) -> [a] -> [b]
map Int -> Either Int (Index width)
forall a b. a -> Either a b
Left ([Int] -> [Either Int (Index width)])
-> [Int] -> [Either Int (Index width)]
forall a b. (a -> b) -> a -> b
$ (Int -> Int) -> Int -> [Int]
forall a. (a -> a) -> a -> [a]
iterate (Int
1Int -> Int -> Int
forall a. Num a => a -> a -> a
+) Int
0)

bandedIndexFromOffset ::
   (Shape.C height, Shape.C width) =>
   (height -> Int -> row) ->
   (width -> Int -> column) ->
   (Int,Int) -> Order -> (height,width) -> Int -> BandedIndex row column
bandedIndexFromOffset :: (height -> Int -> row)
-> (width -> Int -> column)
-> (Int, Int)
-> Order
-> (height, width)
-> Int
-> BandedIndex row column
bandedIndexFromOffset
      height -> Int -> row
rowFromOffset width -> Int -> column
columnFromOffset (Int
kl,Int
ku) Order
order (height
height,width
width) Int
j =
   case Order
order of
      Order
RowMajor ->
         let n :: Int
n = width -> Int
forall sh. C sh => sh -> Int
Shape.size width
width
             (Int
rb,Int
cb) = Int -> Int -> (Int, Int)
forall a. Integral a => a -> a -> (a, a)
divMod Int
j (Int
klInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
ku)
             r :: row
r = height -> Int -> row
rowFromOffset height
height Int
rb
             ci :: Int
ci = Int
rbInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
cbInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
kl
         in Bool
-> BandedIndex row column
-> BandedIndex row column
-> BandedIndex row column
forall a. Bool -> a -> a -> a
if' (Int
ciInt -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<Int
0) (row -> Int -> BandedIndex row column
forall row column. row -> Int -> BandedIndex row column
HorizOutsideBox row
r Int
ci) (BandedIndex row column -> BandedIndex row column)
-> BandedIndex row column -> BandedIndex row column
forall a b. (a -> b) -> a -> b
$
            Bool
-> BandedIndex row column
-> BandedIndex row column
-> BandedIndex row column
forall a. Bool -> a -> a -> a
if' (Int
ciInt -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>=Int
n) (row -> Int -> BandedIndex row column
forall row column. row -> Int -> BandedIndex row column
HorizOutsideBox row
r (Int
ciInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
n)) (BandedIndex row column -> BandedIndex row column)
-> BandedIndex row column -> BandedIndex row column
forall a b. (a -> b) -> a -> b
$
            row -> column -> BandedIndex row column
forall row column. row -> column -> BandedIndex row column
InsideBox row
r (width -> Int -> column
columnFromOffset width
width Int
ci)
      Order
ColumnMajor ->
         let m :: Int
m = height -> Int
forall sh. C sh => sh -> Int
Shape.size height
height
             (Int
cb,Int
rb) = Int -> Int -> (Int, Int)
forall a. Integral a => a -> a -> (a, a)
divMod Int
j (Int
klInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
ku)
             c :: column
c = width -> Int -> column
columnFromOffset width
width Int
cb
             ri :: Int
ri = Int
rbInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
cbInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
ku
         in Bool
-> BandedIndex row column
-> BandedIndex row column
-> BandedIndex row column
forall a. Bool -> a -> a -> a
if' (Int
riInt -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<Int
0) (Int -> column -> BandedIndex row column
forall row column. Int -> column -> BandedIndex row column
VertOutsideBox Int
ri column
c) (BandedIndex row column -> BandedIndex row column)
-> BandedIndex row column -> BandedIndex row column
forall a b. (a -> b) -> a -> b
$
            Bool
-> BandedIndex row column
-> BandedIndex row column
-> BandedIndex row column
forall a. Bool -> a -> a -> a
if' (Int
riInt -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>=Int
m) (Int -> column -> BandedIndex row column
forall row column. Int -> column -> BandedIndex row column
VertOutsideBox (Int
riInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
m) column
c) (BandedIndex row column -> BandedIndex row column)
-> BandedIndex row column -> BandedIndex row column
forall a b. (a -> b) -> a -> b
$
            row -> column -> BandedIndex row column
forall row column. row -> column -> BandedIndex row column
InsideBox (height -> Int -> row
rowFromOffset height
height Int
ri) column
c


data BandedHermitian off size =
   BandedHermitian {
      BandedHermitian off size -> UnaryProxy off
bandedHermitianOffDiagonals :: UnaryProxy off,
      BandedHermitian off size -> Order
bandedHermitianOrder :: Order,
      BandedHermitian off size -> size
bandedHermitianSize :: size
   } deriving (BandedHermitian off size -> BandedHermitian off size -> Bool
(BandedHermitian off size -> BandedHermitian off size -> Bool)
-> (BandedHermitian off size -> BandedHermitian off size -> Bool)
-> Eq (BandedHermitian off size)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall off size.
Eq size =>
BandedHermitian off size -> BandedHermitian off size -> Bool
/= :: BandedHermitian off size -> BandedHermitian off size -> Bool
$c/= :: forall off size.
Eq size =>
BandedHermitian off size -> BandedHermitian off size -> Bool
== :: BandedHermitian off size -> BandedHermitian off size -> Bool
$c== :: forall off size.
Eq size =>
BandedHermitian off size -> BandedHermitian off size -> Bool
Eq, Int -> BandedHermitian off size -> ShowS
[BandedHermitian off size] -> ShowS
BandedHermitian off size -> String
(Int -> BandedHermitian off size -> ShowS)
-> (BandedHermitian off size -> String)
-> ([BandedHermitian off size] -> ShowS)
-> Show (BandedHermitian off size)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall off size.
(Natural off, Show size) =>
Int -> BandedHermitian off size -> ShowS
forall off size.
(Natural off, Show size) =>
[BandedHermitian off size] -> ShowS
forall off size.
(Natural off, Show size) =>
BandedHermitian off size -> String
showList :: [BandedHermitian off size] -> ShowS
$cshowList :: forall off size.
(Natural off, Show size) =>
[BandedHermitian off size] -> ShowS
show :: BandedHermitian off size -> String
$cshow :: forall off size.
(Natural off, Show size) =>
BandedHermitian off size -> String
showsPrec :: Int -> BandedHermitian off size -> ShowS
$cshowsPrec :: forall off size.
(Natural off, Show size) =>
Int -> BandedHermitian off size -> ShowS
Show)

instance
   (Unary.Natural off, Shape.C size) =>
      Box.Box (BandedHermitian off size) where
   type HeightOf (BandedHermitian off size) = size
   type WidthOf (BandedHermitian off size) = size
   height :: BandedHermitian off size -> HeightOf (BandedHermitian off size)
height = BandedHermitian off size -> HeightOf (BandedHermitian off size)
forall off size. BandedHermitian off size -> size
bandedHermitianSize
   width :: BandedHermitian off size -> WidthOf (BandedHermitian off size)
width = BandedHermitian off size -> WidthOf (BandedHermitian off size)
forall off size. BandedHermitian off size -> size
bandedHermitianSize

instance (Unary.Natural off, NFData size) =>
      NFData (BandedHermitian off size) where
   rnf :: BandedHermitian off size -> ()
rnf (BandedHermitian Proxy (Un off)
Proxy Order
order size
size) = (Order, size) -> ()
forall a. NFData a => a -> ()
rnf (Order
order, size
size)

instance (Unary.Natural off, Shape.C size) =>
      Shape.C (BandedHermitian off size) where
   size :: BandedHermitian off size -> Int
size (BandedHermitian UnaryProxy off
offDiag Order
_order size
size) =
      (Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ UnaryProxy off -> Int
forall x y. (Integer x, Num y) => Proxy x -> y
integralFromProxy UnaryProxy off
offDiag) Int -> Int -> Int
forall a. Num a => a -> a -> a
* size -> Int
forall sh. C sh => sh -> Int
Shape.size size
size
   uncheckedSize :: BandedHermitian off size -> Int
uncheckedSize (BandedHermitian UnaryProxy off
offDiag Order
_order size
size) =
      (Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ UnaryProxy off -> Int
forall x y. (Integer x, Num y) => Proxy x -> y
integralFromProxy UnaryProxy off
offDiag) Int -> Int -> Int
forall a. Num a => a -> a -> a
* size -> Int
forall sh. C sh => sh -> Int
Shape.uncheckedSize size
size

instance (Unary.Natural off, Shape.Indexed size) =>
      Shape.Indexed (BandedHermitian off size) where
   type Index (BandedHermitian off size) =
            BandedIndex (Shape.Index size) (Shape.Index size)
   indices :: BandedHermitian off size -> [Index (BandedHermitian off size)]
indices (BandedHermitian UnaryProxy off
offDiag Order
order size
size) =
      case Order
order of
         Order
RowMajor ->
            ((Index size, Either Int (Index size))
 -> BandedIndex (Index size) (Index size))
-> [(Index size, Either Int (Index size))]
-> [BandedIndex (Index size) (Index size)]
forall a b. (a -> b) -> [a] -> [b]
map (\(Index size
r,Either Int (Index size)
c) -> (Int -> BandedIndex (Index size) (Index size))
-> (Index size -> BandedIndex (Index size) (Index size))
-> Either Int (Index size)
-> BandedIndex (Index size) (Index size)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Index size -> Int -> BandedIndex (Index size) (Index size)
forall row column. row -> Int -> BandedIndex row column
HorizOutsideBox Index size
r) (Index size -> Index size -> BandedIndex (Index size) (Index size)
forall row column. row -> column -> BandedIndex row column
InsideBox Index size
r) Either Int (Index size)
c) ([(Index size, Either Int (Index size))]
 -> [BandedIndex (Index size) (Index size)])
-> [(Index size, Either Int (Index size))]
-> [BandedIndex (Index size) (Index size)]
forall a b. (a -> b) -> a -> b
$
            (UnaryProxy U0, UnaryProxy off)
-> (size, size) -> [(Index size, Either Int (Index size))]
forall sub super height width.
(Natural sub, Natural super, Indexed height, Indexed width) =>
(UnaryProxy sub, UnaryProxy super)
-> (height, width) -> [(Index height, Either Int (Index width))]
bandedIndicesRowMajor (Proxy U0 -> UnaryProxy U0
forall n. Proxy n -> Proxy (Un n)
unary Proxy U0
TypeNum.u0, UnaryProxy off
offDiag) (size
size,size
size)
         Order
ColumnMajor ->
            ((Index size, Either Int (Index size))
 -> BandedIndex (Index size) (Index size))
-> [(Index size, Either Int (Index size))]
-> [BandedIndex (Index size) (Index size)]
forall a b. (a -> b) -> [a] -> [b]
map (\(Index size
c,Either Int (Index size)
r) ->
                  (Int -> BandedIndex (Index size) (Index size))
-> (Index size -> BandedIndex (Index size) (Index size))
-> Either Int (Index size)
-> BandedIndex (Index size) (Index size)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either ((Int -> Index size -> BandedIndex (Index size) (Index size))
-> Index size -> Int -> BandedIndex (Index size) (Index size)
forall a b c. (a -> b -> c) -> b -> a -> c
flip Int -> Index size -> BandedIndex (Index size) (Index size)
forall row column. Int -> column -> BandedIndex row column
VertOutsideBox Index size
c) ((Index size -> Index size -> BandedIndex (Index size) (Index size))
-> Index size
-> Index size
-> BandedIndex (Index size) (Index size)
forall a b c. (a -> b -> c) -> b -> a -> c
flip Index size -> Index size -> BandedIndex (Index size) (Index size)
forall row column. row -> column -> BandedIndex row column
InsideBox Index size
c) Either Int (Index size)
r) ([(Index size, Either Int (Index size))]
 -> [BandedIndex (Index size) (Index size)])
-> [(Index size, Either Int (Index size))]
-> [BandedIndex (Index size) (Index size)]
forall a b. (a -> b) -> a -> b
$
            (UnaryProxy off, UnaryProxy U0)
-> (size, size) -> [(Index size, Either Int (Index size))]
forall sub super height width.
(Natural sub, Natural super, Indexed height, Indexed width) =>
(UnaryProxy sub, UnaryProxy super)
-> (height, width) -> [(Index height, Either Int (Index width))]
bandedIndicesRowMajor (UnaryProxy off
offDiag, Proxy U0 -> UnaryProxy U0
forall n. Proxy n -> Proxy (Un n)
unary Proxy U0
TypeNum.u0) (size
size,size
size)

   offset :: BandedHermitian off size -> Index (BandedHermitian off size) -> Int
offset BandedHermitian off size
shape Index (BandedHermitian off size)
ix =
      if BandedHermitian off size
-> Index (BandedHermitian off size) -> Bool
forall sh. Indexed sh => sh -> Index sh -> Bool
Shape.inBounds BandedHermitian off size
shape Index (BandedHermitian off size)
ix
         then BandedHermitian off size -> Index (BandedHermitian off size) -> Int
forall sh. Indexed sh => sh -> Index sh -> Int
Shape.uncheckedOffset BandedHermitian off size
shape Index (BandedHermitian off size)
ix
         else String -> Int
forall a. HasCallStack => String -> a
error String
"BandedHermitian.offset: index outside band"

   uncheckedOffset :: BandedHermitian off size -> Index (BandedHermitian off size) -> Int
uncheckedOffset (BandedHermitian UnaryProxy off
offDiag Order
order size
size) Index (BandedHermitian off size)
ix =
      let k :: Int
k = UnaryProxy off -> Int
forall x y. (Integer x, Num y) => Proxy x -> y
integralFromProxy UnaryProxy off
offDiag
      in (Int, Int)
-> Order
-> (size, size)
-> BandedIndex (Index size) (Index size)
-> Int
forall height width.
(Indexed height, Indexed width) =>
(Int, Int)
-> Order
-> (height, width)
-> BandedIndex (Index height) (Index width)
-> Int
bandedOffset (Int
0,Int
k) Order
order (size
size,size
size) Index (BandedHermitian off size)
BandedIndex (Index size) (Index size)
ix

   inBounds :: BandedHermitian off size
-> Index (BandedHermitian off size) -> Bool
inBounds (BandedHermitian UnaryProxy off
offDiag Order
order size
size) Index (BandedHermitian off size)
ix =
      let ku :: Int
ku = UnaryProxy off -> Int
forall x y. (Integer x, Num y) => Proxy x -> y
integralFromProxy UnaryProxy off
offDiag
          insideBand :: Int -> Int -> Bool
insideBand Int
r Int
c = Range Int -> Index (Range Int) -> Bool
forall sh. Indexed sh => sh -> Index sh -> Bool
Shape.inBounds (Int -> Int -> Range Int
forall n. n -> n -> Range n
Shape.Range Int
0 Int
ku) (Int
cInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
r)
      in case (Order
order,Index (BandedHermitian off size)
BandedIndex (Index size) (Index size)
ix) of
            (Order
_, InsideBox Index size
r Index size
c) ->
               (size, size) -> Index (size, size) -> Bool
forall sh. Indexed sh => sh -> Index sh -> Bool
Shape.inBounds (size
size,size
size) (Index size
r,Index size
c)
               Bool -> Bool -> Bool
&&
               Int -> Int -> Bool
insideBand (size -> Index size -> Int
forall sh. Indexed sh => sh -> Index sh -> Int
Shape.offset size
size Index size
r) (size -> Index size -> Int
forall sh. Indexed sh => sh -> Index sh -> Int
Shape.offset size
size Index size
c)
            (Order
RowMajor, HorizOutsideBox Index size
r Int
c) ->
               size -> Index size -> Bool
forall sh. Indexed sh => sh -> Index sh -> Bool
Shape.inBounds size
size Index size
r
               Bool -> Bool -> Bool
&&
               Int -> Int -> Bool
insideBand (size -> Index size -> Int
forall sh. Indexed sh => sh -> Index sh -> Int
Shape.offset size
size Index size
r) (size -> Int -> Int
forall sh. C sh => sh -> Int -> Int
outsideOffset size
size Int
c)
            (Order
ColumnMajor, VertOutsideBox Int
r Index size
c) ->
               size -> Index size -> Bool
forall sh. Indexed sh => sh -> Index sh -> Bool
Shape.inBounds size
size Index size
c
               Bool -> Bool -> Bool
&&
               Int -> Int -> Bool
insideBand (size -> Int -> Int
forall sh. C sh => sh -> Int -> Int
outsideOffset size
size Int
r) (size -> Index size -> Int
forall sh. Indexed sh => sh -> Index sh -> Int
Shape.offset size
size Index size
c)
            (Order, BandedIndex (Index size) (Index size))
_ -> Bool
False

instance (Unary.Natural off, Shape.InvIndexed size) =>
      Shape.InvIndexed (BandedHermitian off size) where

   indexFromOffset :: BandedHermitian off size -> Int -> Index (BandedHermitian off size)
indexFromOffset (BandedHermitian UnaryProxy off
offDiag Order
order size
size) Int
j =
      (size -> Int -> Index size)
-> (size -> Int -> Index size)
-> Int
-> Order
-> size
-> Int
-> BandedIndex (Index size) (Index size)
forall sh row column.
C sh =>
(sh -> Int -> row)
-> (sh -> Int -> column)
-> Int
-> Order
-> sh
-> Int
-> BandedIndex row column
bandedHermitianIndexFromOffset
         size -> Int -> Index size
forall sh. InvIndexed sh => sh -> Int -> Index sh
Shape.indexFromOffset size -> Int -> Index size
forall sh. InvIndexed sh => sh -> Int -> Index sh
Shape.indexFromOffset
         (UnaryProxy off -> Int
forall x y. (Integer x, Num y) => Proxy x -> y
integralFromProxy UnaryProxy off
offDiag) Order
order size
size Int
j

   uncheckedIndexFromOffset :: BandedHermitian off size -> Int -> Index (BandedHermitian off size)
uncheckedIndexFromOffset (BandedHermitian UnaryProxy off
offDiag Order
order size
size) Int
j =
      (size -> Int -> Index size)
-> (size -> Int -> Index size)
-> Int
-> Order
-> size
-> Int
-> BandedIndex (Index size) (Index size)
forall sh row column.
C sh =>
(sh -> Int -> row)
-> (sh -> Int -> column)
-> Int
-> Order
-> sh
-> Int
-> BandedIndex row column
bandedHermitianIndexFromOffset
         size -> Int -> Index size
forall sh. InvIndexed sh => sh -> Int -> Index sh
Shape.uncheckedIndexFromOffset size -> Int -> Index size
forall sh. InvIndexed sh => sh -> Int -> Index sh
Shape.uncheckedIndexFromOffset
         (UnaryProxy off -> Int
forall x y. (Integer x, Num y) => Proxy x -> y
integralFromProxy UnaryProxy off
offDiag) Order
order size
size Int
j

bandedHermitianIndexFromOffset ::
   (Shape.C sh) =>
   (sh -> Int -> row) ->
   (sh -> Int -> column) ->
   Int -> Order -> sh -> Int -> BandedIndex row column
bandedHermitianIndexFromOffset :: (sh -> Int -> row)
-> (sh -> Int -> column)
-> Int
-> Order
-> sh
-> Int
-> BandedIndex row column
bandedHermitianIndexFromOffset sh -> Int -> row
rowFromOffset sh -> Int -> column
columnFromOffset Int
k Order
order sh
size Int
j =
   case Order
order of
      Order
RowMajor ->
         let n :: Int
n = sh -> Int
forall sh. C sh => sh -> Int
Shape.size sh
size
             (Int
rb,Int
cb) = Int -> Int -> (Int, Int)
forall a. Integral a => a -> a -> (a, a)
divMod Int
j (Int
kInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
             r :: row
r = sh -> Int -> row
rowFromOffset sh
size Int
rb
             ci :: Int
ci = Int
rbInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
cb
         in if Int
ciInt -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<Int
n
               then row -> column -> BandedIndex row column
forall row column. row -> column -> BandedIndex row column
InsideBox row
r (sh -> Int -> column
columnFromOffset sh
size Int
ci)
               else row -> Int -> BandedIndex row column
forall row column. row -> Int -> BandedIndex row column
HorizOutsideBox row
r (Int
ciInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
n)
      Order
ColumnMajor ->
         let (Int
cb,Int
rb) = Int -> Int -> (Int, Int)
forall a. Integral a => a -> a -> (a, a)
divMod Int
j (Int
kInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
             c :: column
c = sh -> Int -> column
columnFromOffset sh
size Int
cb
             ri :: Int
ri = Int
rbInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
cbInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
k
         in if Int
riInt -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>=Int
0
               then row -> column -> BandedIndex row column
forall row column. row -> column -> BandedIndex row column
InsideBox (sh -> Int -> row
rowFromOffset sh
size Int
ri) column
c
               else Int -> column -> BandedIndex row column
forall row column. Int -> column -> BandedIndex row column
VertOutsideBox Int
ri column
c