{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
module Numeric.LAPACK.Matrix.Plain (
   Full,
   General, Tall, Wide,
   ShapeInt, shapeInt,
   transpose, adjoint,
   Matrix.height, Matrix.width,
   Basic.caseTallWide,
   fromList,
   mapExtent, fromFull,
   tallFromGeneral, wideFromGeneral,
   generalizeTall, generalizeWide,
   mapHeight, mapWidth,
   identity,
   diagonal,
   fromRowsNonEmpty,    fromRowArray,    fromRows,
   fromRowsNonEmptyContainer,    fromRowContainer,
   fromColumnsNonEmpty, fromColumnArray, fromColumns,
   fromColumnsNonEmptyContainer, fromColumnContainer,
   Basic.singleRow,   Basic.singleColumn,
   Basic.flattenRow,  Basic.flattenColumn,
   Basic.liftRow,     Basic.liftColumn,
   Basic.unliftRow,   Basic.unliftColumn,
   toRows, toColumns,
   toRowArray, toColumnArray,
   toRowContainer, toColumnContainer,
   takeRow, takeColumn,
   Basic.takeRows, Basic.takeColumns, takeEqually,
   Basic.dropRows, Basic.dropColumns, dropEqually,
   Basic.takeTop, Basic.takeBottom,
   Basic.takeLeft, Basic.takeRight,
   takeRowArray, takeColumnArray,
   swapRows, swapColumns,
   reverseRows, reverseColumns,
   fromRowMajor, toRowMajor,
   forceOrder, adaptOrder,

   (|*-),
   tensorProduct,
   outer,
   kronecker,
   sumRank1,

   add, sub,
   rowSums, columnSums,
   rowArgAbsMaximums, columnArgAbsMaximums,
   Basic.scaleRows, Basic.scaleColumns,
   Basic.scaleRowsComplex, Basic.scaleColumnsComplex,
   Basic.scaleRowsReal, Basic.scaleColumnsReal,
   ) where

import qualified Numeric.LAPACK.Matrix.Layout.Private as Layout
import qualified Numeric.LAPACK.Matrix.Square.Basic as Square
import qualified Numeric.LAPACK.Matrix.Extent.Private as Extent
import qualified Numeric.LAPACK.Matrix.Basic as Basic
import qualified Numeric.LAPACK.Matrix.RowMajor as RowMajor
import qualified Numeric.LAPACK.Matrix.Private as Matrix
import qualified Numeric.LAPACK.Vector.Private as VectorPriv
import qualified Numeric.LAPACK.Vector as Vector
import qualified Numeric.LAPACK.Shape as ExtShape
import Numeric.LAPACK.Matrix.Layout.Private
         (Order(RowMajor, ColumnMajor), transposeFromOrder)
import Numeric.LAPACK.Matrix.Basic
         (transpose, adjoint, forceOrder, forceRowMajor)
import Numeric.LAPACK.Matrix.Private
         (Full, Tall, Wide, General, ShapeInt, shapeInt,
          mapExtent, fromFull, generalizeTall, generalizeWide)
import Numeric.LAPACK.Matrix.Modifier (Conjugation(NonConjugated,Conjugated))
import Numeric.LAPACK.Vector (Vector)
import Numeric.LAPACK.Scalar (zero, one)
import Numeric.LAPACK.Private (pointerSeq, fill, copySubMatrix, copyBlock)

import qualified Numeric.LAPACK.FFI.Generic as LapackGen
import qualified Numeric.BLAS.FFI.Generic as BlasGen
import qualified Numeric.Netlib.Utility as Call
import qualified Numeric.Netlib.Class as Class

import qualified Data.Array.Comfort.Boxed as BoxedArray
import qualified Data.Array.Comfort.Storable.Unchecked.Monadic as ArrayIO
import qualified Data.Array.Comfort.Storable.Unchecked as Array
import qualified Data.Array.Comfort.Storable as CheckedArray
import qualified Data.Array.Comfort.Container as Container
import qualified Data.Array.Comfort.Shape as Shape
import Data.Array.Comfort.Storable.Unchecked (Array(Array))

import Foreign.Marshal.Array (advancePtr, pokeArray)
import Foreign.ForeignPtr (withForeignPtr)
import Foreign.Storable (Storable, poke, peekElemOff)

import Control.Monad.Trans.Cont (ContT(ContT), evalContT)
import Control.Monad.IO.Class (liftIO)
import Control.Monad (when, mfilter)
import Control.Applicative ((<$>))

import qualified Data.NonEmpty.Mixed as NonEmptyM
import qualified Data.NonEmpty as NonEmpty
import qualified Data.Foldable as Fold
import Data.Foldable (forM_)
import Data.Maybe (listToMaybe)
import Data.Bool.HT (if')


fromList ::
   (Shape.C height, Shape.C width, Storable a) =>
   height -> width -> [a] -> General height width a
fromList :: height -> width -> [a] -> General height width a
fromList height
height width
width =
   General height width -> [a] -> General height width a
forall sh a. (C sh, Storable a) => sh -> [a] -> Array sh a
CheckedArray.fromList (Order -> height -> width -> General height width
forall height width.
Order -> height -> width -> General height width
Layout.general Order
RowMajor height
height width
width)

tallFromGeneral ::
   (Shape.C height, Shape.C width, Storable a) =>
   General height width a -> Tall height width a
tallFromGeneral :: General height width a -> Tall height width a
tallFromGeneral =
   (Full Size Big Big height width -> Tall height width)
-> General height width a -> Tall height width a
forall sh0 sh1 a. (sh0 -> sh1) -> Array sh0 a -> Array sh1 a
Array.mapShape ((Full Size Big Big height width -> Tall height width)
 -> General height width a -> Tall height width a)
-> (Full Size Big Big height width -> Tall height width)
-> General height width a
-> Tall height width a
forall a b. (a -> b) -> a -> b
$ \(Layout.Full Order
order Extent Size Big Big height width
extent) ->
      (height -> width -> Tall height width)
-> (height, width) -> Tall height width
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (Order -> height -> width -> Tall height width
forall height width.
(C height, C width) =>
Order -> height -> width -> Tall height width
Layout.tall Order
order) (Extent Size Big Big height width -> (height, width)
forall meas vert horiz height width.
(Measure meas, C vert, C horiz) =>
Extent meas vert horiz height width -> (height, width)
Extent.dimensions Extent Size Big Big height width
extent)

wideFromGeneral ::
   (Shape.C height, Shape.C width, Storable a) =>
   General height width a -> Wide height width a
wideFromGeneral :: General height width a -> Wide height width a
wideFromGeneral =
   (Full Size Big Big height width -> Wide height width)
-> General height width a -> Wide height width a
forall sh0 sh1 a. (sh0 -> sh1) -> Array sh0 a -> Array sh1 a
Array.mapShape ((Full Size Big Big height width -> Wide height width)
 -> General height width a -> Wide height width a)
-> (Full Size Big Big height width -> Wide height width)
-> General height width a
-> Wide height width a
forall a b. (a -> b) -> a -> b
$ \(Layout.Full Order
order Extent Size Big Big height width
extent) ->
      (height -> width -> Wide height width)
-> (height, width) -> Wide height width
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (Order -> height -> width -> Wide height width
forall height width.
(C height, C width) =>
Order -> height -> width -> Wide height width
Layout.wide Order
order) (Extent Size Big Big height width -> (height, width)
forall meas vert horiz height width.
(Measure meas, C vert, C horiz) =>
Extent meas vert horiz height width -> (height, width)
Extent.dimensions Extent Size Big Big height width
extent)


identity ::
   (Extent.Measure meas, Extent.C vert, Extent.C horiz) =>
   (Shape.C sh, Class.Floating a) =>
   sh -> Full meas vert horiz sh sh a
identity :: sh -> Full meas vert horiz sh sh a
identity = Square sh a -> Full meas vert horiz sh sh a
forall meas vert horiz sh a.
(Measure meas, C vert, C horiz) =>
Square sh a -> Full meas vert horiz sh sh a
Square.toFull (Square sh a -> Full meas vert horiz sh sh a)
-> (sh -> Square sh a) -> sh -> Full meas vert horiz sh sh a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. sh -> Square sh a
forall sh a. (C sh, Floating a) => sh -> Square sh a
Square.identity

diagonal ::
   (Extent.Measure meas, Extent.C vert, Extent.C horiz) =>
   (Shape.C sh, Class.Floating a) =>
   Vector sh a -> Full meas vert horiz sh sh a
diagonal :: Vector sh a -> Full meas vert horiz sh sh a
diagonal = Square sh a -> Full meas vert horiz sh sh a
forall meas vert horiz sh a.
(Measure meas, C vert, C horiz) =>
Square sh a -> Full meas vert horiz sh sh a
Square.toFull (Square sh a -> Full meas vert horiz sh sh a)
-> (Vector sh a -> Square sh a)
-> Vector sh a
-> Full meas vert horiz sh sh a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector sh a -> Square sh a
forall sh a. (C sh, Floating a) => Vector sh a -> Square sh a
Square.diagonal


mapHeight ::
   (Shape.C heightA, Shape.C heightB, Extent.C vert, Extent.C horiz) =>
   (heightA -> heightB) ->
   Full Extent.Size vert horiz heightA width a ->
   Full Extent.Size vert horiz heightB width a
mapHeight :: (heightA -> heightB)
-> Full Size vert horiz heightA width a
-> Full Size vert horiz heightB width a
mapHeight heightA -> heightB
f = (heightA -> heightB)
-> Full Size vert horiz heightA width a
-> Full Size vert horiz heightB width a
forall vert horiz heightA heightB width a.
(C vert, C horiz) =>
(heightA -> heightB)
-> Full Size vert horiz heightA width a
-> Full Size vert horiz heightB width a
Basic.mapHeight ((heightA -> heightB)
 -> Full Size vert horiz heightA width a
 -> Full Size vert horiz heightB width a)
-> (heightA -> heightB)
-> Full Size vert horiz heightA width a
-> Full Size vert horiz heightB width a
forall a b. (a -> b) -> a -> b
$ String -> (heightA -> heightB) -> heightA -> heightB
forall sha shb.
(C sha, C shb) =>
String -> (sha -> shb) -> sha -> shb
Layout.mapChecked String
"mapHeight" heightA -> heightB
f

mapWidth ::
   (Shape.C widthA, Shape.C widthB, Extent.C vert, Extent.C horiz) =>
   (widthA -> widthB) ->
   Full Extent.Size vert horiz height widthA a ->
   Full Extent.Size vert horiz height widthB a
mapWidth :: (widthA -> widthB)
-> Full Size vert horiz height widthA a
-> Full Size vert horiz height widthB a
mapWidth widthA -> widthB
f = (widthA -> widthB)
-> Full Size vert horiz height widthA a
-> Full Size vert horiz height widthB a
forall vert horiz widthA widthB height a.
(C vert, C horiz) =>
(widthA -> widthB)
-> Full Size vert horiz height widthA a
-> Full Size vert horiz height widthB a
Basic.mapWidth ((widthA -> widthB)
 -> Full Size vert horiz height widthA a
 -> Full Size vert horiz height widthB a)
-> (widthA -> widthB)
-> Full Size vert horiz height widthA a
-> Full Size vert horiz height widthB a
forall a b. (a -> b) -> a -> b
$ String -> (widthA -> widthB) -> widthA -> widthB
forall sha shb.
(C sha, C shb) =>
String -> (sha -> shb) -> sha -> shb
Layout.mapChecked String
"mapWidth" widthA -> widthB
f


fromRowsNonEmpty ::
   (Shape.C width, Eq width, Storable a) =>
   NonEmpty.T [] (Vector width a) -> General ShapeInt width a
fromRowsNonEmpty :: T [] (Vector width a) -> General ShapeInt width a
fromRowsNonEmpty (NonEmpty.Cons Vector width a
row [Vector width a]
rows) =
   width -> [Vector width a] -> General ShapeInt width a
forall width a.
(C width, Eq width, Storable a) =>
width -> [Vector width a] -> General ShapeInt width a
fromRows (Vector width a -> width
forall sh a. Array sh a -> sh
Array.shape Vector width a
row) (Vector width a
rowVector width a -> [Vector width a] -> [Vector width a]
forall a. a -> [a] -> [a]
:[Vector width a]
rows)

fromRowArray ::
   (Shape.C height, Shape.C width, Eq width, Storable a) =>
   width -> BoxedArray.Array height (Vector width a) -> General height width a
fromRowArray :: width -> Array height (Vector width a) -> General height width a
fromRowArray width
width Array height (Vector width a)
rows =
   (ShapeInt -> height)
-> Full Size Big Big ShapeInt width a -> General height width a
forall vert horiz heightA heightB width a.
(C vert, C horiz) =>
(heightA -> heightB)
-> Full Size vert horiz heightA width a
-> Full Size vert horiz heightB width a
Basic.mapHeight (height -> ShapeInt -> height
forall a b. a -> b -> a
const (height -> ShapeInt -> height) -> height -> ShapeInt -> height
forall a b. (a -> b) -> a -> b
$ Array height (Vector width a) -> height
forall sh a. Array sh a -> sh
BoxedArray.shape Array height (Vector width a)
rows) (Full Size Big Big ShapeInt width a -> General height width a)
-> Full Size Big Big ShapeInt width a -> General height width a
forall a b. (a -> b) -> a -> b
$
   width -> [Vector width a] -> Full Size Big Big ShapeInt width a
forall width a.
(C width, Eq width, Storable a) =>
width -> [Vector width a] -> General ShapeInt width a
fromRows width
width ([Vector width a] -> Full Size Big Big ShapeInt width a)
-> [Vector width a] -> Full Size Big Big ShapeInt width a
forall a b. (a -> b) -> a -> b
$ Array height (Vector width a) -> [Vector width a]
forall sh a. C sh => Array sh a -> [a]
BoxedArray.toList Array height (Vector width a)
rows

fromRowsNonEmptyContainer ::
   (f ~ NonEmpty.T g, Container.C g,
    Shape.C width, Eq width, Storable a) =>
   f (Vector width a) -> General (Container.Shape f) width a
fromRowsNonEmptyContainer :: f (Vector width a) -> General (Shape f) width a
fromRowsNonEmptyContainer f (Vector width a)
rows =
   width -> f (Vector width a) -> General (Shape f) width a
forall (f :: * -> *) width a.
(C f, C width, Eq width, Storable a) =>
width -> f (Vector width a) -> General (Shape f) width a
fromRowContainer (Vector width a -> width
forall sh a. Array sh a -> sh
Array.shape (Vector width a -> width) -> Vector width a -> width
forall a b. (a -> b) -> a -> b
$ T g (Vector width a) -> Vector width a
forall (f :: * -> *) a. T f a -> a
NonEmpty.head f (Vector width a)
T g (Vector width a)
rows) f (Vector width a)
rows

fromRowContainer ::
   (Container.C f, Shape.C width, Eq width, Storable a) =>
   width -> f (Vector width a) -> General (Container.Shape f) width a
fromRowContainer :: width -> f (Vector width a) -> General (Shape f) width a
fromRowContainer width
width f (Vector width a)
rows =
   (ShapeInt -> Shape f)
-> Full Size Big Big ShapeInt width a -> General (Shape f) width a
forall vert horiz heightA heightB width a.
(C vert, C horiz) =>
(heightA -> heightB)
-> Full Size vert horiz heightA width a
-> Full Size vert horiz heightB width a
Basic.mapHeight (Shape f -> ShapeInt -> Shape f
forall a b. a -> b -> a
const (Shape f -> ShapeInt -> Shape f) -> Shape f -> ShapeInt -> Shape f
forall a b. (a -> b) -> a -> b
$ f (Vector width a) -> Shape f
forall (f :: * -> *) a. C f => f a -> Shape f
Container.toShape f (Vector width a)
rows) (Full Size Big Big ShapeInt width a -> General (Shape f) width a)
-> Full Size Big Big ShapeInt width a -> General (Shape f) width a
forall a b. (a -> b) -> a -> b
$
   width -> [Vector width a] -> Full Size Big Big ShapeInt width a
forall width a.
(C width, Eq width, Storable a) =>
width -> [Vector width a] -> General ShapeInt width a
fromRows width
width ([Vector width a] -> Full Size Big Big ShapeInt width a)
-> [Vector width a] -> Full Size Big Big ShapeInt width a
forall a b. (a -> b) -> a -> b
$ f (Vector width a) -> [Vector width a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
Fold.toList f (Vector width a)
rows

fromRows ::
   (Shape.C width, Eq width, Storable a) =>
   width -> [Vector width a] -> General ShapeInt width a
fromRows :: width -> [Vector width a] -> General ShapeInt width a
fromRows width
width = Array (ShapeInt, width) a -> General ShapeInt width a
forall height width a.
(C height, C width, Storable a) =>
Array (height, width) a -> General height width a
fromRowMajor (Array (ShapeInt, width) a -> General ShapeInt width a)
-> ([Vector width a] -> Array (ShapeInt, width) a)
-> [Vector width a]
-> General ShapeInt width a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. width -> [Vector width a] -> Array (ShapeInt, width) a
forall width a.
(C width, Eq width, Storable a) =>
width -> [Vector width a] -> Matrix ShapeInt width a
RowMajor.fromRows width
width


fromColumnsNonEmpty ::
   (Shape.C height, Eq height, Storable a) =>
   NonEmpty.T [] (Vector height a) -> General height ShapeInt a
fromColumnsNonEmpty :: T [] (Vector height a) -> General height ShapeInt a
fromColumnsNonEmpty (NonEmpty.Cons Vector height a
column [Vector height a]
columns) =
   height -> [Vector height a] -> General height ShapeInt a
forall height a.
(C height, Eq height, Storable a) =>
height -> [Vector height a] -> General height ShapeInt a
fromColumns (Vector height a -> height
forall sh a. Array sh a -> sh
Array.shape Vector height a
column) (Vector height a
columnVector height a -> [Vector height a] -> [Vector height a]
forall a. a -> [a] -> [a]
:[Vector height a]
columns)

fromColumnArray ::
   (Shape.C height, Eq height, Shape.C width, Storable a) =>
   height -> BoxedArray.Array width (Vector height a) -> General height width a
fromColumnArray :: height -> Array width (Vector height a) -> General height width a
fromColumnArray height
height Array width (Vector height a)
columns =
   (ShapeInt -> width)
-> Full Size Big Big height ShapeInt a -> General height width a
forall vert horiz widthA widthB height a.
(C vert, C horiz) =>
(widthA -> widthB)
-> Full Size vert horiz height widthA a
-> Full Size vert horiz height widthB a
Basic.mapWidth (width -> ShapeInt -> width
forall a b. a -> b -> a
const (width -> ShapeInt -> width) -> width -> ShapeInt -> width
forall a b. (a -> b) -> a -> b
$ Array width (Vector height a) -> width
forall sh a. Array sh a -> sh
BoxedArray.shape Array width (Vector height a)
columns) (Full Size Big Big height ShapeInt a -> General height width a)
-> Full Size Big Big height ShapeInt a -> General height width a
forall a b. (a -> b) -> a -> b
$
   height -> [Vector height a] -> Full Size Big Big height ShapeInt a
forall height a.
(C height, Eq height, Storable a) =>
height -> [Vector height a] -> General height ShapeInt a
fromColumns height
height ([Vector height a] -> Full Size Big Big height ShapeInt a)
-> [Vector height a] -> Full Size Big Big height ShapeInt a
forall a b. (a -> b) -> a -> b
$ Array width (Vector height a) -> [Vector height a]
forall sh a. C sh => Array sh a -> [a]
BoxedArray.toList Array width (Vector height a)
columns

fromColumnsNonEmptyContainer ::
   (f ~ NonEmpty.T g, Container.C g,
    Shape.C height, Eq height, Storable a) =>
   f (Vector height a) -> General height (Container.Shape f) a
fromColumnsNonEmptyContainer :: f (Vector height a) -> General height (Shape f) a
fromColumnsNonEmptyContainer f (Vector height a)
columns =
   height -> f (Vector height a) -> General height (Shape f) a
forall height (f :: * -> *) a.
(C height, Eq height, C f, Storable a) =>
height -> f (Vector height a) -> General height (Shape f) a
fromColumnContainer (Vector height a -> height
forall sh a. Array sh a -> sh
Array.shape (Vector height a -> height) -> Vector height a -> height
forall a b. (a -> b) -> a -> b
$ T g (Vector height a) -> Vector height a
forall (f :: * -> *) a. T f a -> a
NonEmpty.head f (Vector height a)
T g (Vector height a)
columns) f (Vector height a)
columns

fromColumnContainer ::
   (Shape.C height, Eq height, Container.C f, Storable a) =>
   height -> f (Vector height a) -> General height (Container.Shape f) a
fromColumnContainer :: height -> f (Vector height a) -> General height (Shape f) a
fromColumnContainer height
height f (Vector height a)
columns =
   (ShapeInt -> Shape f)
-> Full Size Big Big height ShapeInt a
-> General height (Shape f) a
forall vert horiz widthA widthB height a.
(C vert, C horiz) =>
(widthA -> widthB)
-> Full Size vert horiz height widthA a
-> Full Size vert horiz height widthB a
Basic.mapWidth (Shape f -> ShapeInt -> Shape f
forall a b. a -> b -> a
const (Shape f -> ShapeInt -> Shape f) -> Shape f -> ShapeInt -> Shape f
forall a b. (a -> b) -> a -> b
$ f (Vector height a) -> Shape f
forall (f :: * -> *) a. C f => f a -> Shape f
Container.toShape f (Vector height a)
columns) (Full Size Big Big height ShapeInt a -> General height (Shape f) a)
-> Full Size Big Big height ShapeInt a
-> General height (Shape f) a
forall a b. (a -> b) -> a -> b
$
   height -> [Vector height a] -> Full Size Big Big height ShapeInt a
forall height a.
(C height, Eq height, Storable a) =>
height -> [Vector height a] -> General height ShapeInt a
fromColumns height
height ([Vector height a] -> Full Size Big Big height ShapeInt a)
-> [Vector height a] -> Full Size Big Big height ShapeInt a
forall a b. (a -> b) -> a -> b
$ f (Vector height a) -> [Vector height a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
Fold.toList f (Vector height a)
columns

fromColumns ::
   (Shape.C height, Eq height, Storable a) =>
   height -> [Vector height a] -> General height ShapeInt a
fromColumns :: height -> [Vector height a] -> General height ShapeInt a
fromColumns height
height = Full Size Big Big ShapeInt height a -> General height ShapeInt a
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz) =>
Full meas vert horiz height width a
-> Full meas horiz vert width height a
transpose (Full Size Big Big ShapeInt height a -> General height ShapeInt a)
-> ([Vector height a] -> Full Size Big Big ShapeInt height a)
-> [Vector height a]
-> General height ShapeInt a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array (ShapeInt, height) a -> Full Size Big Big ShapeInt height a
forall height width a.
(C height, C width, Storable a) =>
Array (height, width) a -> General height width a
fromRowMajor (Array (ShapeInt, height) a -> Full Size Big Big ShapeInt height a)
-> ([Vector height a] -> Array (ShapeInt, height) a)
-> [Vector height a]
-> Full Size Big Big ShapeInt height a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. height -> [Vector height a] -> Array (ShapeInt, height) a
forall width a.
(C width, Eq width, Storable a) =>
width -> [Vector width a] -> Matrix ShapeInt width a
RowMajor.fromRows height
height


toRows ::
   (Extent.Measure meas, Extent.C vert, Extent.C horiz,
    Shape.C height, Shape.C width, Class.Floating a) =>
   Full meas vert horiz height width a -> [Vector width a]
toRows :: Full meas vert horiz height width a -> [Vector width a]
toRows Full meas vert horiz height width a
a =
   let ad :: Full Size Big Big (Deferred height) width a
ad = (height -> Deferred height)
-> Full Size Big Big height width a
-> Full Size Big Big (Deferred height) width a
forall vert horiz heightA heightB width a.
(C vert, C horiz) =>
(heightA -> heightB)
-> Full Size vert horiz heightA width a
-> Full Size vert horiz heightB width a
Basic.mapHeight height -> Deferred height
forall sh. sh -> Deferred sh
Shape.Deferred (Full Size Big Big height width a
 -> Full Size Big Big (Deferred height) width a)
-> Full Size Big Big height width a
-> Full Size Big Big (Deferred height) width a
forall a b. (a -> b) -> a -> b
$ Full meas vert horiz height width a
-> Full Size Big Big height width a
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz) =>
Full meas vert horiz height width a -> General height width a
fromFull Full meas vert horiz height width a
a
   in (DeferredIndex height -> Vector width a)
-> [DeferredIndex height] -> [Vector width a]
forall a b. (a -> b) -> [a] -> [b]
map (Full Size Big Big (Deferred height) width a
-> DeferredIndex height -> Vector width a
forall meas vert horiz height width ix a.
(Measure meas, C vert, C horiz, Indexed height, C width,
 Index height ~ ix, Floating a) =>
Full meas vert horiz height width a -> ix -> Vector width a
takeRow Full Size Big Big (Deferred height) width a
ad) ([DeferredIndex height] -> [Vector width a])
-> [DeferredIndex height] -> [Vector width a]
forall a b. (a -> b) -> a -> b
$ Deferred height -> [Index (Deferred height)]
forall sh. Indexed sh => sh -> [Index sh]
Shape.indices (Deferred height -> [Index (Deferred height)])
-> Deferred height -> [Index (Deferred height)]
forall a b. (a -> b) -> a -> b
$ Full Size Big Big (Deferred height) width a -> Deferred height
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz) =>
Full meas vert horiz height width a -> height
Matrix.height Full Size Big Big (Deferred height) width a
ad

toColumns ::
   (Extent.Measure meas, Extent.C vert, Extent.C horiz,
    Shape.C height, Shape.C width, Class.Floating a) =>
   Full meas vert horiz height width a -> [Vector height a]
toColumns :: Full meas vert horiz height width a -> [Vector height a]
toColumns Full meas vert horiz height width a
a =
   let ad :: Full Size Big Big height (Deferred width) a
ad = (width -> Deferred width)
-> Full Size Big Big height width a
-> Full Size Big Big height (Deferred width) a
forall vert horiz widthA widthB height a.
(C vert, C horiz) =>
(widthA -> widthB)
-> Full Size vert horiz height widthA a
-> Full Size vert horiz height widthB a
Basic.mapWidth width -> Deferred width
forall sh. sh -> Deferred sh
Shape.Deferred (Full Size Big Big height width a
 -> Full Size Big Big height (Deferred width) a)
-> Full Size Big Big height width a
-> Full Size Big Big height (Deferred width) a
forall a b. (a -> b) -> a -> b
$ Full meas vert horiz height width a
-> Full Size Big Big height width a
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz) =>
Full meas vert horiz height width a -> General height width a
fromFull Full meas vert horiz height width a
a
   in (DeferredIndex width -> Vector height a)
-> [DeferredIndex width] -> [Vector height a]
forall a b. (a -> b) -> [a] -> [b]
map (Full Size Big Big height (Deferred width) a
-> DeferredIndex width -> Vector height a
forall meas vert horiz height width ix a.
(Measure meas, C vert, C horiz, C height, Indexed width,
 Index width ~ ix, Floating a) =>
Full meas vert horiz height width a -> ix -> Vector height a
takeColumn Full Size Big Big height (Deferred width) a
ad) ([DeferredIndex width] -> [Vector height a])
-> [DeferredIndex width] -> [Vector height a]
forall a b. (a -> b) -> a -> b
$ Deferred width -> [Index (Deferred width)]
forall sh. Indexed sh => sh -> [Index sh]
Shape.indices (Deferred width -> [Index (Deferred width)])
-> Deferred width -> [Index (Deferred width)]
forall a b. (a -> b) -> a -> b
$ Full Size Big Big height (Deferred width) a -> Deferred width
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz) =>
Full meas vert horiz height width a -> width
Matrix.width Full Size Big Big height (Deferred width) a
ad

toRowArray ::
   (Extent.Measure meas, Extent.C vert, Extent.C horiz,
    Shape.C height, Shape.C width, Class.Floating a) =>
   Full meas vert horiz height width a ->
   BoxedArray.Array height (Vector width a)
toRowArray :: Full meas vert horiz height width a
-> Array height (Vector width a)
toRowArray Full meas vert horiz height width a
a = height -> [Vector width a] -> Array height (Vector width a)
forall sh a. C sh => sh -> [a] -> Array sh a
BoxedArray.fromList (Full meas vert horiz height width a -> height
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz) =>
Full meas vert horiz height width a -> height
Matrix.height Full meas vert horiz height width a
a) (Full meas vert horiz height width a -> [Vector width a]
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz, C height, C width, Floating a) =>
Full meas vert horiz height width a -> [Vector width a]
toRows Full meas vert horiz height width a
a)

toColumnArray ::
   (Extent.Measure meas, Extent.C vert, Extent.C horiz,
    Shape.C height, Shape.C width, Class.Floating a) =>
   Full meas vert horiz height width a ->
   BoxedArray.Array width (Vector height a)
toColumnArray :: Full meas vert horiz height width a
-> Array width (Vector height a)
toColumnArray Full meas vert horiz height width a
a = width -> [Vector height a] -> Array width (Vector height a)
forall sh a. C sh => sh -> [a] -> Array sh a
BoxedArray.fromList (Full meas vert horiz height width a -> width
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz) =>
Full meas vert horiz height width a -> width
Matrix.width Full meas vert horiz height width a
a) (Full meas vert horiz height width a -> [Vector height a]
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz, C height, C width, Floating a) =>
Full meas vert horiz height width a -> [Vector height a]
toColumns Full meas vert horiz height width a
a)

toRowContainer ::
   (Extent.Measure meas, Extent.C vert, Extent.C horiz,
    Container.C f, Shape.C width, Class.Floating a) =>
   Full meas vert horiz (Container.Shape f) width a -> f (Vector width a)
toRowContainer :: Full meas vert horiz (Shape f) width a -> f (Vector width a)
toRowContainer Full meas vert horiz (Shape f) width a
a = Shape f -> [Vector width a] -> f (Vector width a)
forall (f :: * -> *) a. C f => Shape f -> [a] -> f a
Container.fromList (Full meas vert horiz (Shape f) width a -> Shape f
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz) =>
Full meas vert horiz height width a -> height
Matrix.height Full meas vert horiz (Shape f) width a
a) (Full meas vert horiz (Shape f) width a -> [Vector width a]
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz, C height, C width, Floating a) =>
Full meas vert horiz height width a -> [Vector width a]
toRows Full meas vert horiz (Shape f) width a
a)

toColumnContainer ::
   (Extent.Measure meas, Extent.C vert, Extent.C horiz,
    Shape.C height, Container.C f, Class.Floating a) =>
   Full meas vert horiz height (Container.Shape f) a -> f (Vector height a)
toColumnContainer :: Full meas vert horiz height (Shape f) a -> f (Vector height a)
toColumnContainer Full meas vert horiz height (Shape f) a
a = Shape f -> [Vector height a] -> f (Vector height a)
forall (f :: * -> *) a. C f => Shape f -> [a] -> f a
Container.fromList (Full meas vert horiz height (Shape f) a -> Shape f
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz) =>
Full meas vert horiz height width a -> width
Matrix.width Full meas vert horiz height (Shape f) a
a) (Full meas vert horiz height (Shape f) a -> [Vector height a]
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz, C height, C width, Floating a) =>
Full meas vert horiz height width a -> [Vector height a]
toColumns Full meas vert horiz height (Shape f) a
a)


takeRow ::
   (Extent.Measure meas, Extent.C vert, Extent.C horiz,
    Shape.Indexed height, Shape.C width, Shape.Index height ~ ix,
    Class.Floating a) =>
   Full meas vert horiz height width a -> ix -> Vector width a
takeRow :: Full meas vert horiz height width a -> ix -> Vector width a
takeRow Full meas vert horiz height width a
x ix
ix =
   (Matrix height width a -> Vector width a)
-> (Matrix width height a -> Vector width a)
-> Either (Matrix height width a) (Matrix width height a)
-> Vector width a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (ix -> Matrix height width a -> Vector width a
forall height width ix a.
(Indexed height, C width, Index height ~ ix, Storable a) =>
ix -> Matrix height width a -> Vector width a
RowMajor.takeRow ix
ix) (ix -> Matrix width height a -> Vector width a
forall height width ix a.
(C height, Indexed width, Index width ~ ix, Floating a) =>
ix -> Matrix height width a -> Vector height a
RowMajor.takeColumn ix
ix) (Either (Matrix height width a) (Matrix width height a)
 -> Vector width a)
-> Either (Matrix height width a) (Matrix width height a)
-> Vector width a
forall a b. (a -> b) -> a -> b
$
   Full meas vert horiz height width a
-> Either (Matrix height width a) (Matrix width height a)
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz) =>
Full meas vert horiz height width a
-> Either (Array (height, width) a) (Array (width, height) a)
Matrix.revealOrder Full meas vert horiz height width a
x

takeColumn ::
   (Extent.Measure meas, Extent.C vert, Extent.C horiz,
    Shape.C height, Shape.Indexed width, Shape.Index width ~ ix,
    Class.Floating a) =>
   Full meas vert horiz height width a -> ix -> Vector height a
takeColumn :: Full meas vert horiz height width a -> ix -> Vector height a
takeColumn Full meas vert horiz height width a
x ix
ix =
   (Matrix height width a -> Vector height a)
-> (Matrix width height a -> Vector height a)
-> Either (Matrix height width a) (Matrix width height a)
-> Vector height a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (ix -> Matrix height width a -> Vector height a
forall height width ix a.
(C height, Indexed width, Index width ~ ix, Floating a) =>
ix -> Matrix height width a -> Vector height a
RowMajor.takeColumn ix
ix) (ix -> Matrix width height a -> Vector height a
forall height width ix a.
(Indexed height, C width, Index height ~ ix, Storable a) =>
ix -> Matrix height width a -> Vector width a
RowMajor.takeRow ix
ix) (Either (Matrix height width a) (Matrix width height a)
 -> Vector height a)
-> Either (Matrix height width a) (Matrix width height a)
-> Vector height a
forall a b. (a -> b) -> a -> b
$
   Full meas vert horiz height width a
-> Either (Matrix height width a) (Matrix width height a)
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz) =>
Full meas vert horiz height width a
-> Either (Array (height, width) a) (Array (width, height) a)
Matrix.revealOrder Full meas vert horiz height width a
x


takeEqually ::
   (Extent.Measure meas, Extent.C vert, Extent.C horiz, Class.Floating a) =>
   Int ->
   Full meas vert horiz ShapeInt ShapeInt a ->
   Full meas vert horiz ShapeInt ShapeInt a
takeEqually :: Int
-> Full meas vert horiz ShapeInt ShapeInt a
-> Full meas vert horiz ShapeInt ShapeInt a
takeEqually Int
k (Array (Layout.Full Order
order Extent meas vert horiz ShapeInt ShapeInt
extentA) ForeignPtr a
a) =
   let (Shape.ZeroBased Int
heightA, Shape.ZeroBased Int
widthA) =
         Extent meas vert horiz ShapeInt ShapeInt -> (ShapeInt, ShapeInt)
forall meas vert horiz height width.
(Measure meas, C vert, C horiz) =>
Extent meas vert horiz height width -> (height, width)
Extent.dimensions Extent meas vert horiz ShapeInt ShapeInt
extentA
       heightB :: Int
heightB = Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
k Int
heightA
       widthB :: Int
widthB  = Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
k Int
widthA
       extentB :: Extent meas vert horiz ShapeInt ShapeInt
extentB =
         ShapeInt
-> ShapeInt
-> Extent meas vert horiz ShapeInt ShapeInt
-> Extent meas vert horiz ShapeInt ShapeInt
forall meas vert horiz height width.
(Measure meas, C vert, C horiz) =>
height
-> width
-> Extent meas vert horiz height width
-> Extent meas vert horiz height width
Extent.reduceConsistent
            (Int -> ShapeInt
forall n. n -> ZeroBased n
Shape.ZeroBased Int
heightB) (Int -> ShapeInt
forall n. n -> ZeroBased n
Shape.ZeroBased Int
widthB) Extent meas vert horiz ShapeInt ShapeInt
extentA
   in Bool
-> Full meas vert horiz ShapeInt ShapeInt a
-> Full meas vert horiz ShapeInt ShapeInt a
-> Full meas vert horiz ShapeInt ShapeInt a
forall a. Bool -> a -> a -> a
if' (Int
kInt -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<Int
0) (String -> Full meas vert horiz ShapeInt ShapeInt a
forall a. HasCallStack => String -> a
error String
"take: negative number") (Full meas vert horiz ShapeInt ShapeInt a
 -> Full meas vert horiz ShapeInt ShapeInt a)
-> Full meas vert horiz ShapeInt ShapeInt a
-> Full meas vert horiz ShapeInt ShapeInt a
forall a b. (a -> b) -> a -> b
$
      Full meas vert horiz ShapeInt ShapeInt
-> (Ptr a -> IO ()) -> Full meas vert horiz ShapeInt ShapeInt a
forall sh a.
(C sh, Storable a) =>
sh -> (Ptr a -> IO ()) -> Array sh a
Array.unsafeCreate (Order
-> Extent meas vert horiz ShapeInt ShapeInt
-> Full meas vert horiz ShapeInt ShapeInt
forall meas vert horiz height width.
Order
-> Extent meas vert horiz height width
-> Full meas vert horiz height width
Layout.Full Order
order Extent meas vert horiz ShapeInt ShapeInt
extentB) ((Ptr a -> IO ()) -> Full meas vert horiz ShapeInt ShapeInt a)
-> (Ptr a -> IO ()) -> Full meas vert horiz ShapeInt ShapeInt a
forall a b. (a -> b) -> a -> b
$ \Ptr a
bPtr ->
      ForeignPtr a -> (Ptr a -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr a
a ((Ptr a -> IO ()) -> IO ()) -> (Ptr a -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr a
aPtr ->
      case Order
order of
         Order
RowMajor -> Int -> Int -> Int -> Ptr a -> Int -> Ptr a -> IO ()
forall a.
Floating a =>
Int -> Int -> Int -> Ptr a -> Int -> Ptr a -> IO ()
copySubMatrix Int
widthB Int
heightB Int
widthA Ptr a
aPtr Int
widthB Ptr a
bPtr
         Order
ColumnMajor -> Int -> Int -> Int -> Ptr a -> Int -> Ptr a -> IO ()
forall a.
Floating a =>
Int -> Int -> Int -> Ptr a -> Int -> Ptr a -> IO ()
copySubMatrix Int
heightB Int
widthB Int
heightA Ptr a
aPtr Int
heightB Ptr a
bPtr

dropEqually ::
   (Extent.Measure meas, Extent.C vert, Extent.C horiz, Class.Floating a) =>
   Int ->
   Full meas vert horiz ShapeInt ShapeInt a ->
   Full meas vert horiz ShapeInt ShapeInt a
dropEqually :: Int
-> Full meas vert horiz ShapeInt ShapeInt a
-> Full meas vert horiz ShapeInt ShapeInt a
dropEqually Int
k (Array (Layout.Full Order
order Extent meas vert horiz ShapeInt ShapeInt
extentA) ForeignPtr a
a) =
   let (Shape.ZeroBased Int
heightA, Shape.ZeroBased Int
widthA) =
         Extent meas vert horiz ShapeInt ShapeInt -> (ShapeInt, ShapeInt)
forall meas vert horiz height width.
(Measure meas, C vert, C horiz) =>
Extent meas vert horiz height width -> (height, width)
Extent.dimensions Extent meas vert horiz ShapeInt ShapeInt
extentA
       heightB :: Int
heightB = Int
heightA Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
top; top :: Int
top  = Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
k Int
heightA
       widthB :: Int
widthB  = Int
widthA Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
left; left :: Int
left = Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
k Int
widthA
       extentB :: Extent meas vert horiz ShapeInt ShapeInt
extentB =
         ShapeInt
-> ShapeInt
-> Extent meas vert horiz ShapeInt ShapeInt
-> Extent meas vert horiz ShapeInt ShapeInt
forall meas vert horiz height width.
(Measure meas, C vert, C horiz) =>
height
-> width
-> Extent meas vert horiz height width
-> Extent meas vert horiz height width
Extent.reduceConsistent
            (Int -> ShapeInt
forall n. n -> ZeroBased n
Shape.ZeroBased Int
heightB) (Int -> ShapeInt
forall n. n -> ZeroBased n
Shape.ZeroBased Int
widthB) Extent meas vert horiz ShapeInt ShapeInt
extentA
   in Bool
-> Full meas vert horiz ShapeInt ShapeInt a
-> Full meas vert horiz ShapeInt ShapeInt a
-> Full meas vert horiz ShapeInt ShapeInt a
forall a. Bool -> a -> a -> a
if' (Int
kInt -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<Int
0) (String -> Full meas vert horiz ShapeInt ShapeInt a
forall a. HasCallStack => String -> a
error String
"drop: negative number") (Full meas vert horiz ShapeInt ShapeInt a
 -> Full meas vert horiz ShapeInt ShapeInt a)
-> Full meas vert horiz ShapeInt ShapeInt a
-> Full meas vert horiz ShapeInt ShapeInt a
forall a b. (a -> b) -> a -> b
$
      Full meas vert horiz ShapeInt ShapeInt
-> (Ptr a -> IO ()) -> Full meas vert horiz ShapeInt ShapeInt a
forall sh a.
(C sh, Storable a) =>
sh -> (Ptr a -> IO ()) -> Array sh a
Array.unsafeCreate (Order
-> Extent meas vert horiz ShapeInt ShapeInt
-> Full meas vert horiz ShapeInt ShapeInt
forall meas vert horiz height width.
Order
-> Extent meas vert horiz height width
-> Full meas vert horiz height width
Layout.Full Order
order Extent meas vert horiz ShapeInt ShapeInt
extentB) ((Ptr a -> IO ()) -> Full meas vert horiz ShapeInt ShapeInt a)
-> (Ptr a -> IO ()) -> Full meas vert horiz ShapeInt ShapeInt a
forall a b. (a -> b) -> a -> b
$ \Ptr a
bPtr ->
      ForeignPtr a -> (Ptr a -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr a
a ((Ptr a -> IO ()) -> IO ()) -> (Ptr a -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr a
aPtr ->
      case Order
order of
         Order
RowMajor ->
            Int -> Int -> Int -> Ptr a -> Int -> Ptr a -> IO ()
forall a.
Floating a =>
Int -> Int -> Int -> Ptr a -> Int -> Ptr a -> IO ()
copySubMatrix Int
widthB Int
heightB
               Int
widthA (Ptr a -> Int -> Ptr a
forall a. Storable a => Ptr a -> Int -> Ptr a
advancePtr Ptr a
aPtr (Int
topInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
widthAInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
left)) Int
widthB Ptr a
bPtr
         Order
ColumnMajor ->
            Int -> Int -> Int -> Ptr a -> Int -> Ptr a -> IO ()
forall a.
Floating a =>
Int -> Int -> Int -> Ptr a -> Int -> Ptr a -> IO ()
copySubMatrix Int
heightB Int
widthB
               Int
heightA (Ptr a -> Int -> Ptr a
forall a. Storable a => Ptr a -> Int -> Ptr a
advancePtr Ptr a
aPtr (Int
leftInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
heightAInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
top)) Int
heightB Ptr a
bPtr


swapRows ::
   (Extent.Measure meas, Extent.C vert, Extent.C horiz,
    Shape.Indexed height, Shape.C width, Class.Floating a) =>
   Shape.Index height -> Shape.Index height ->
   Full meas vert horiz height width a -> Full meas vert horiz height width a
swapRows :: Index height
-> Index height
-> Full meas vert horiz height width a
-> Full meas vert horiz height width a
swapRows Index height
i Index height
j (Array shape :: Full meas vert horiz height width
shape@(Layout.Full Order
order Extent meas vert horiz height width
extent) ForeignPtr a
a) =
   Full meas vert horiz height width
-> (Int -> Ptr a -> IO ()) -> Full meas vert horiz height width a
forall sh a.
(C sh, Storable a) =>
sh -> (Int -> Ptr a -> IO ()) -> Array sh a
Array.unsafeCreateWithSize Full meas vert horiz height width
shape ((Int -> Ptr a -> IO ()) -> Full meas vert horiz height width a)
-> (Int -> Ptr a -> IO ()) -> Full meas vert horiz height width a
forall a b. (a -> b) -> a -> b
$ \Int
blockSize Ptr a
bPtr -> ContT () IO () -> IO ()
forall (m :: * -> *) r. Monad m => ContT r m r -> m r
evalContT (ContT () IO () -> IO ()) -> ContT () IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
      let (height
height,width
width) = Extent meas vert horiz height width -> (height, width)
forall meas vert horiz height width.
(Measure meas, C vert, C horiz) =>
Extent meas vert horiz height width -> (height, width)
Extent.dimensions Extent meas vert horiz height width
extent
      let m :: Int
m = height -> Int
forall sh. C sh => sh -> Int
Shape.size height
height
      let n :: Int
n = width -> Int
forall sh. C sh => sh -> Int
Shape.size width
width
      Ptr CInt
nPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint Int
n
      Ptr a
aPtr <- ((Ptr a -> IO ()) -> IO ()) -> ContT () IO (Ptr a)
forall k (r :: k) (m :: k -> *) a.
((a -> m r) -> m r) -> ContT r m a
ContT (((Ptr a -> IO ()) -> IO ()) -> ContT () IO (Ptr a))
-> ((Ptr a -> IO ()) -> IO ()) -> ContT () IO (Ptr a)
forall a b. (a -> b) -> a -> b
$ ForeignPtr a -> (Ptr a -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr a
a
      let offsetI :: Int
offsetI = height -> Index height -> Int
forall sh. Indexed sh => sh -> Index sh -> Int
Shape.offset height
height Index height
i
      let offsetJ :: Int
offsetJ = height -> Index height -> Int
forall sh. Indexed sh => sh -> Index sh -> Int
Shape.offset height
height Index height
j
      let (Int
incVert,Int
incHoriz) =
            case Order
order of
               Order
RowMajor -> (Int
n,Int
1)
               Order
ColumnMajor -> (Int
1,Int
m)
      Ptr CInt
incPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint Int
incHoriz
      IO () -> ContT () IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ContT () IO ()) -> IO () -> ContT () IO ()
forall a b. (a -> b) -> a -> b
$ do
         Int -> Ptr a -> Ptr a -> IO ()
forall a. Floating a => Int -> Ptr a -> Ptr a -> IO ()
copyBlock Int
blockSize Ptr a
aPtr Ptr a
bPtr
         Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
offsetIInt -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/=Int
offsetJ) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
            Ptr CInt -> Ptr a -> Ptr CInt -> Ptr a -> Ptr CInt -> IO ()
forall a.
Floating a =>
Ptr CInt -> Ptr a -> Ptr CInt -> Ptr a -> Ptr CInt -> IO ()
BlasGen.swap Ptr CInt
nPtr
               (Ptr a -> Int -> Ptr a
forall a. Storable a => Ptr a -> Int -> Ptr a
advancePtr Ptr a
bPtr (Int
incVertInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
offsetI)) Ptr CInt
incPtr
               (Ptr a -> Int -> Ptr a
forall a. Storable a => Ptr a -> Int -> Ptr a
advancePtr Ptr a
bPtr (Int
incVertInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
offsetJ)) Ptr CInt
incPtr

swapColumns ::
   (Extent.Measure meas, Extent.C vert, Extent.C horiz,
    Shape.C height, Shape.Indexed width, Class.Floating a) =>
   Shape.Index width -> Shape.Index width ->
   Full meas vert horiz height width a -> Full meas vert horiz height width a
swapColumns :: Index width
-> Index width
-> Full meas vert horiz height width a
-> Full meas vert horiz height width a
swapColumns Index width
i Index width
j = Full meas horiz vert width height a
-> Full meas vert horiz height width a
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz) =>
Full meas vert horiz height width a
-> Full meas horiz vert width height a
transpose (Full meas horiz vert width height a
 -> Full meas vert horiz height width a)
-> (Full meas vert horiz height width a
    -> Full meas horiz vert width height a)
-> Full meas vert horiz height width a
-> Full meas vert horiz height width a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index width
-> Index width
-> Full meas horiz vert width height a
-> Full meas horiz vert width height a
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz, Indexed height, C width,
 Floating a) =>
Index height
-> Index height
-> Full meas vert horiz height width a
-> Full meas vert horiz height width a
swapRows Index width
i Index width
j (Full meas horiz vert width height a
 -> Full meas horiz vert width height a)
-> (Full meas vert horiz height width a
    -> Full meas horiz vert width height a)
-> Full meas vert horiz height width a
-> Full meas horiz vert width height a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Full meas vert horiz height width a
-> Full meas horiz vert width height a
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz) =>
Full meas vert horiz height width a
-> Full meas horiz vert width height a
transpose


-- alternative: laswp
reverseRows ::
   (Extent.Measure meas, Extent.C vert, Extent.C horiz,
    ExtShape.Permutable height, Shape.C width, Class.Floating a) =>
   Full meas vert horiz height width a ->
   Full meas vert horiz height width a
reverseRows :: Full meas vert horiz height width a
-> Full meas vert horiz height width a
reverseRows (Array shape :: Full meas vert horiz height width
shape@(Layout.Full Order
order Extent meas vert horiz height width
extent) ForeignPtr a
a) =
   Full meas vert horiz height width
-> (Int -> Ptr a -> IO ()) -> Full meas vert horiz height width a
forall sh a.
(C sh, Storable a) =>
sh -> (Int -> Ptr a -> IO ()) -> Array sh a
Array.unsafeCreateWithSize Full meas vert horiz height width
shape ((Int -> Ptr a -> IO ()) -> Full meas vert horiz height width a)
-> (Int -> Ptr a -> IO ()) -> Full meas vert horiz height width a
forall a b. (a -> b) -> a -> b
$ \Int
blockSize Ptr a
bPtr -> ContT () IO () -> IO ()
forall (m :: * -> *) r. Monad m => ContT r m r -> m r
evalContT (ContT () IO () -> IO ()) -> ContT () IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
      let (height
height,width
width) = Extent meas vert horiz height width -> (height, width)
forall meas vert horiz height width.
(Measure meas, C vert, C horiz) =>
Extent meas vert horiz height width -> (height, width)
Extent.dimensions Extent meas vert horiz height width
extent
      let n :: Int
n = height -> Int
forall sh. C sh => sh -> Int
Shape.size height
height
      let m :: Int
m = width -> Int
forall sh. C sh => sh -> Int
Shape.size width
width
      Ptr Bool
fwdPtr <- Bool -> FortranIO () (Ptr Bool)
forall r. Bool -> FortranIO r (Ptr Bool)
Call.bool Bool
True
      Ptr CInt
nPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint Int
n
      Ptr CInt
mPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint Int
m
      Ptr CInt
kPtr <- Int -> FortranIO () (Ptr CInt)
forall a r. Storable a => Int -> FortranIO r (Ptr a)
Call.allocaArray Int
n
      Ptr a
aPtr <- ((Ptr a -> IO ()) -> IO ()) -> ContT () IO (Ptr a)
forall k (r :: k) (m :: k -> *) a.
((a -> m r) -> m r) -> ContT r m a
ContT (((Ptr a -> IO ()) -> IO ()) -> ContT () IO (Ptr a))
-> ((Ptr a -> IO ()) -> IO ()) -> ContT () IO (Ptr a)
forall a b. (a -> b) -> a -> b
$ ForeignPtr a -> (Ptr a -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr a
a
      IO () -> ContT () IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ContT () IO ()) -> IO () -> ContT () IO ()
forall a b. (a -> b) -> a -> b
$ do
         Int -> Ptr a -> Ptr a -> IO ()
forall a. Floating a => Int -> Ptr a -> Ptr a -> IO ()
copyBlock Int
blockSize Ptr a
aPtr Ptr a
bPtr
         Ptr CInt -> [CInt] -> IO ()
forall a. Storable a => Ptr a -> [a] -> IO ()
pokeArray Ptr CInt
kPtr ([CInt] -> IO ()) -> [CInt] -> IO ()
forall a b. (a -> b) -> a -> b
$ Int -> [CInt] -> [CInt]
forall a. Int -> [a] -> [a]
take Int
n ([CInt] -> [CInt]) -> [CInt] -> [CInt]
forall a b. (a -> b) -> a -> b
$ (CInt -> CInt) -> CInt -> [CInt]
forall a. (a -> a) -> a -> [a]
iterate (CInt -> CInt -> CInt
forall a. Num a => a -> a -> a
subtract CInt
1) (CInt -> [CInt]) -> CInt -> [CInt]
forall a b. (a -> b) -> a -> b
$ Int -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n
         case Order
order of
            Order
RowMajor -> Ptr Bool
-> Ptr CInt -> Ptr CInt -> Ptr a -> Ptr CInt -> Ptr CInt -> IO ()
forall a.
Floating a =>
Ptr Bool
-> Ptr CInt -> Ptr CInt -> Ptr a -> Ptr CInt -> Ptr CInt -> IO ()
LapackGen.lapmt Ptr Bool
fwdPtr Ptr CInt
mPtr Ptr CInt
nPtr Ptr a
bPtr Ptr CInt
mPtr Ptr CInt
kPtr
            Order
ColumnMajor -> Ptr Bool
-> Ptr CInt -> Ptr CInt -> Ptr a -> Ptr CInt -> Ptr CInt -> IO ()
forall a.
Floating a =>
Ptr Bool
-> Ptr CInt -> Ptr CInt -> Ptr a -> Ptr CInt -> Ptr CInt -> IO ()
LapackGen.lapmr Ptr Bool
fwdPtr Ptr CInt
nPtr Ptr CInt
mPtr Ptr a
bPtr Ptr CInt
nPtr Ptr CInt
kPtr

reverseColumns ::
   (Extent.Measure meas, Extent.C vert, Extent.C horiz,
    Shape.C height, ExtShape.Permutable width, Class.Floating a) =>
   Full meas vert horiz height width a ->
   Full meas vert horiz height width a
reverseColumns :: Full meas vert horiz height width a
-> Full meas vert horiz height width a
reverseColumns = Full meas horiz vert width height a
-> Full meas vert horiz height width a
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz) =>
Full meas vert horiz height width a
-> Full meas horiz vert width height a
transpose (Full meas horiz vert width height a
 -> Full meas vert horiz height width a)
-> (Full meas vert horiz height width a
    -> Full meas horiz vert width height a)
-> Full meas vert horiz height width a
-> Full meas vert horiz height width a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Full meas horiz vert width height a
-> Full meas horiz vert width height a
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz, Permutable height, C width,
 Floating a) =>
Full meas vert horiz height width a
-> Full meas vert horiz height width a
reverseRows (Full meas horiz vert width height a
 -> Full meas horiz vert width height a)
-> (Full meas vert horiz height width a
    -> Full meas horiz vert width height a)
-> Full meas vert horiz height width a
-> Full meas horiz vert width height a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Full meas vert horiz height width a
-> Full meas horiz vert width height a
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz) =>
Full meas vert horiz height width a
-> Full meas horiz vert width height a
transpose


{-
The function is optimized for blocks of consecutive rows.
For scattered rows in column major order
the function has quite ugly memory access patterns.
-}
takeRowArray ::
   (Shape.Indexed height, Shape.C width, Shape.C sh, Class.Floating a) =>
   BoxedArray.Array sh (Shape.Index height) ->
   General height width a -> General sh width a
takeRowArray :: Array sh (Index height)
-> General height width a -> General sh width a
takeRowArray Array sh (Index height)
ixs (Array (Layout.Full Order
order Extent Size Big Big height width
extent) ForeignPtr a
a) =
   let (height
heightA, width
width) = Extent Size Big Big height width -> (height, width)
forall meas vert horiz height width.
(Measure meas, C vert, C horiz) =>
Extent meas vert horiz height width -> (height, width)
Extent.dimensions Extent Size Big Big height width
extent
       heightB :: sh
heightB = Array sh (Index height) -> sh
forall sh a. Array sh a -> sh
BoxedArray.shape Array sh (Index height)
ixs
       offsets :: [Int]
offsets = (Index height -> Int) -> [Index height] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map (height -> Index height -> Int
forall sh. Indexed sh => sh -> Index sh -> Int
Shape.offset height
heightA) ([Index height] -> [Int]) -> [Index height] -> [Int]
forall a b. (a -> b) -> a -> b
$ Array sh (Index height) -> [Index height]
forall sh a. C sh => Array sh a -> [a]
BoxedArray.toList Array sh (Index height)
ixs
       startBlocks :: [(a, b)] -> [(a, (a, b))]
startBlocks [(a, b)]
blocks = [a] -> [(a, b)] -> [(a, (a, b))]
forall a b. [a] -> [b] -> [(a, b)]
zip ((a -> a -> a) -> a -> [a] -> [a]
forall b a. (b -> a -> b) -> b -> [a] -> [b]
scanl a -> a -> a
forall a. Num a => a -> a -> a
(+) a
0 ([a] -> [a]) -> [a] -> [a]
forall a b. (a -> b) -> a -> b
$ ((a, b) -> a) -> [(a, b)] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map (a, b) -> a
forall a b. (a, b) -> a
fst [(a, b)]
blocks) [(a, b)]
blocks
       ma :: Int
ma = height -> Int
forall sh. C sh => sh -> Int
Shape.size height
heightA
       mb :: Int
mb = sh -> Int
forall sh. C sh => sh -> Int
Shape.size sh
heightB
       n :: Int
n = width -> Int
forall sh. C sh => sh -> Int
Shape.size width
width
   in General sh width -> (Ptr a -> IO ()) -> General sh width a
forall sh a.
(C sh, Storable a) =>
sh -> (Ptr a -> IO ()) -> Array sh a
Array.unsafeCreate (Order -> sh -> width -> General sh width
forall height width.
Order -> height -> width -> General height width
Layout.general Order
order sh
heightB width
width) ((Ptr a -> IO ()) -> General sh width a)
-> (Ptr a -> IO ()) -> General sh width a
forall a b. (a -> b) -> a -> b
$ \Ptr a
bPtr ->
      ForeignPtr a -> (Ptr a -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr a
a ((Ptr a -> IO ()) -> IO ()) -> (Ptr a -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr a
aPtr ->
      case Order
order of
         Order
RowMajor -> do
            [(Int, (Int, (Int, Int)))]
-> ((Int, (Int, (Int, Int))) -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([(Int, (Int, Int))] -> [(Int, (Int, (Int, Int)))]
forall a b. Num a => [(a, b)] -> [(a, (a, b))]
startBlocks ([(Int, (Int, Int))] -> [(Int, (Int, (Int, Int)))])
-> [(Int, (Int, Int))] -> [(Int, (Int, (Int, Int)))]
forall a b. (a -> b) -> a -> b
$ [Int] -> [(Int, (Int, Int))]
forall i. Integral i => [i] -> [(Int, (i, i))]
chopRowBlocks [Int]
offsets) (((Int, (Int, (Int, Int))) -> IO ()) -> IO ())
-> ((Int, (Int, (Int, Int))) -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$
               \(Int
dest, (Int
numRows, (Int
start,Int
step))) ->
                  Int -> Int -> Int -> Ptr a -> Int -> Ptr a -> IO ()
forall a.
Floating a =>
Int -> Int -> Int -> Ptr a -> Int -> Ptr a -> IO ()
copySubMatrix Int
n Int
numRows
                     (Int
stepInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
n) (Ptr a -> Int -> Ptr a
forall a. Storable a => Ptr a -> Int -> Ptr a
advancePtr Ptr a
aPtr (Int
startInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
n))
                     Int
n (Ptr a -> Int -> Ptr a
forall a. Storable a => Ptr a -> Int -> Ptr a
advancePtr Ptr a
bPtr (Int
destInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
n))
         Order
ColumnMajor -> do
            [(Int, (Int, Int))] -> ((Int, (Int, Int)) -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([(Int, Int)] -> [(Int, (Int, Int))]
forall a b. Num a => [(a, b)] -> [(a, (a, b))]
startBlocks ([(Int, Int)] -> [(Int, (Int, Int))])
-> [(Int, Int)] -> [(Int, (Int, Int))]
forall a b. (a -> b) -> a -> b
$ [Int] -> [(Int, Int)]
forall i. Integral i => [i] -> [(Int, i)]
chopColumnBlocks [Int]
offsets) (((Int, (Int, Int)) -> IO ()) -> IO ())
-> ((Int, (Int, Int)) -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$
               \(Int
dest, (Int
numRows, Int
start)) ->
                  Int -> Int -> Int -> Ptr a -> Int -> Ptr a -> IO ()
forall a.
Floating a =>
Int -> Int -> Int -> Ptr a -> Int -> Ptr a -> IO ()
copySubMatrix Int
numRows Int
n
                     Int
ma (Ptr a -> Int -> Ptr a
forall a. Storable a => Ptr a -> Int -> Ptr a
advancePtr Ptr a
aPtr Int
start)
                     Int
mb (Ptr a -> Int -> Ptr a
forall a. Storable a => Ptr a -> Int -> Ptr a
advancePtr Ptr a
bPtr Int
dest)

chopRowBlocks :: (Integral i) => [i] -> [(Int,(i,i))]
chopRowBlocks :: [i] -> [(Int, (i, i))]
chopRowBlocks =
   let go :: [b] -> [(Int, (b, b))]
go [] = []
       go is :: [b]
is@(b
i0:[b]
is0) =
         case (b -> Bool) -> Maybe b -> Maybe b
forall (m :: * -> *) a. MonadPlus m => (a -> Bool) -> m a -> m a
mfilter (b
i0b -> b -> Bool
forall a. Ord a => a -> a -> Bool
<) (Maybe b -> Maybe b) -> Maybe b -> Maybe b
forall a b. (a -> b) -> a -> b
$ [b] -> Maybe b
forall a. [a] -> Maybe a
listToMaybe [b]
is0 of
            Maybe b
Nothing -> (Int
1,(b
i0,b
0)) (Int, (b, b)) -> [(Int, (b, b))] -> [(Int, (b, b))]
forall a. a -> [a] -> [a]
: [b] -> [(Int, (b, b))]
go [b]
is0
            Just b
i1 ->
               let ([(b, b)]
consecutive,[(b, b)]
remainder) =
                     ((b, b) -> Bool) -> [(b, b)] -> ([(b, b)], [(b, b)])
forall a. (a -> Bool) -> [a] -> ([a], [a])
span ((b -> b -> Bool) -> (b, b) -> Bool
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry b -> b -> Bool
forall a. Eq a => a -> a -> Bool
(==)) ([(b, b)] -> ([(b, b)], [(b, b)]))
-> [(b, b)] -> ([(b, b)], [(b, b)])
forall a b. (a -> b) -> a -> b
$ [b] -> [b] -> [(b, b)]
forall a b. [a] -> [b] -> [(a, b)]
zip [b
i0,b
i1..] [b]
is
               in ([(b, b)] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(b, b)]
consecutive, (b
i0,b
i1b -> b -> b
forall a. Num a => a -> a -> a
-b
i0)) (Int, (b, b)) -> [(Int, (b, b))] -> [(Int, (b, b))]
forall a. a -> [a] -> [a]
: [b] -> [(Int, (b, b))]
go (((b, b) -> b) -> [(b, b)] -> [b]
forall a b. (a -> b) -> [a] -> [b]
map (b, b) -> b
forall a b. (a, b) -> b
snd [(b, b)]
remainder)
   in [i] -> [(Int, (i, i))]
forall b. (Ord b, Num b, Enum b) => [b] -> [(Int, (b, b))]
go

chopColumnBlocks :: (Integral i) => [i] -> [(Int,i)]
chopColumnBlocks :: [i] -> [(Int, i)]
chopColumnBlocks =
   (T [] i -> (Int, i)) -> [T [] i] -> [(Int, i)]
forall a b. (a -> b) -> [a] -> [b]
map (\T [] i
is -> ([i] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([i] -> Int) -> [i] -> Int
forall a b. (a -> b) -> a -> b
$ T [] i -> [i]
forall (f :: * -> *) a. Cons f => T f a -> f a
NonEmpty.flatten T [] i
is, T [] i -> i
forall (f :: * -> *) a. T f a -> a
NonEmpty.head T [] i
is)) ([T [] i] -> [(Int, i)]) -> ([i] -> [T [] i]) -> [i] -> [(Int, i)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
   (i -> i -> Bool) -> [i] -> [T [] i]
forall (f :: * -> *) a.
Foldable f =>
(a -> a -> Bool) -> f a -> [T [] a]
NonEmptyM.groupBy (\i
i i
j -> i
ii -> i -> i
forall a. Num a => a -> a -> a
+i
1 i -> i -> Bool
forall a. Eq a => a -> a -> Bool
== i
j)


takeColumnArray ::
   (Shape.C height, Shape.Indexed width, Shape.C sh, Class.Floating a) =>
   BoxedArray.Array sh (Shape.Index width) ->
   General height width a -> General height sh a
takeColumnArray :: Array sh (Index width)
-> General height width a -> General height sh a
takeColumnArray Array sh (Index width)
ixs = Full Size Big Big sh height a -> General height sh a
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz) =>
Full meas vert horiz height width a
-> Full meas horiz vert width height a
transpose (Full Size Big Big sh height a -> General height sh a)
-> (General height width a -> Full Size Big Big sh height a)
-> General height width a
-> General height sh a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array sh (Index width)
-> General width height a -> Full Size Big Big sh height a
forall height width sh a.
(Indexed height, C width, C sh, Floating a) =>
Array sh (Index height)
-> General height width a -> General sh width a
takeRowArray Array sh (Index width)
ixs (General width height a -> Full Size Big Big sh height a)
-> (General height width a -> General width height a)
-> General height width a
-> Full Size Big Big sh height a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. General height width a -> General width height a
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz) =>
Full meas vert horiz height width a
-> Full meas horiz vert width height a
transpose



fromRowMajor ::
   (Shape.C height, Shape.C width, Storable a) =>
   Array (height,width) a -> General height width a
fromRowMajor :: Array (height, width) a -> General height width a
fromRowMajor = ((height, width) -> General height width)
-> Array (height, width) a -> General height width a
forall sh0 sh1 a. (sh0 -> sh1) -> Array sh0 a -> Array sh1 a
Array.mapShape ((height -> width -> General height width)
-> (height, width) -> General height width
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry ((height -> width -> General height width)
 -> (height, width) -> General height width)
-> (height -> width -> General height width)
-> (height, width)
-> General height width
forall a b. (a -> b) -> a -> b
$ Order -> height -> width -> General height width
forall height width.
Order -> height -> width -> General height width
Layout.general Order
RowMajor)

toRowMajor ::
   (Extent.Measure meas, Extent.C vert, Extent.C horiz,
    Shape.C height, Shape.C width, Class.Floating a) =>
   Full meas vert horiz height width a -> Array (height,width) a
toRowMajor :: Full meas vert horiz height width a -> Array (height, width) a
toRowMajor =
   (Full meas vert horiz height width -> (height, width))
-> Full meas vert horiz height width a -> Array (height, width) a
forall sh0 sh1 a. (sh0 -> sh1) -> Array sh0 a -> Array sh1 a
Array.mapShape
      (\Full meas vert horiz height width
shape -> (Full meas vert horiz height width -> height
forall meas vert horiz height width.
(Measure meas, C vert, C horiz) =>
Full meas vert horiz height width -> height
Layout.fullHeight Full meas vert horiz height width
shape, Full meas vert horiz height width -> width
forall meas vert horiz height width.
(Measure meas, C vert, C horiz) =>
Full meas vert horiz height width -> width
Layout.fullWidth Full meas vert horiz height width
shape)) (Full meas vert horiz height width a -> Array (height, width) a)
-> (Full meas vert horiz height width a
    -> Full meas vert horiz height width a)
-> Full meas vert horiz height width a
-> Array (height, width) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
   Full meas vert horiz height width a
-> Full meas vert horiz height width a
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz, C height, C width, Floating a) =>
Full meas vert horiz height width a
-> Full meas vert horiz height width a
forceRowMajor

adaptOrder ::
   (Extent.Measure meas, Extent.C vert, Extent.C horiz, Shape.C height, Shape.C width,
    Class.Floating a) =>
   Full meas vert horiz height width a ->
   Full meas vert horiz height width a ->
   Full meas vert horiz height width a
adaptOrder :: Full meas vert horiz height width a
-> Full meas vert horiz height width a
-> Full meas vert horiz height width a
adaptOrder Full meas vert horiz height width a
x = Order
-> Full meas vert horiz height width a
-> Full meas vert horiz height width a
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz, C height, C width, Floating a) =>
Order
-> Full meas vert horiz height width a
-> Full meas vert horiz height width a
forceOrder (Full meas vert horiz height width -> Order
forall meas vert horiz height width.
Full meas vert horiz height width -> Order
Layout.fullOrder (Full meas vert horiz height width -> Order)
-> Full meas vert horiz height width -> Order
forall a b. (a -> b) -> a -> b
$ Full meas vert horiz height width a
-> Full meas vert horiz height width
forall sh a. Array sh a -> sh
Array.shape Full meas vert horiz height width a
x)


add, sub ::
   (Extent.Measure meas, Extent.C vert, Extent.C horiz,
    Shape.C height, Shape.C width, Eq height, Eq width,
    Class.Floating a) =>
   Full meas vert horiz height width a ->
   Full meas vert horiz height width a ->
   Full meas vert horiz height width a
add :: Full meas vert horiz height width a
-> Full meas vert horiz height width a
-> Full meas vert horiz height width a
add Full meas vert horiz height width a
x Full meas vert horiz height width a
y = Full meas vert horiz height width a
-> Full meas vert horiz height width a
-> Full meas vert horiz height width a
forall sh a.
(C sh, Eq sh, Floating a) =>
Vector sh a -> Vector sh a -> Vector sh a
Vector.add (Full meas vert horiz height width a
-> Full meas vert horiz height width a
-> Full meas vert horiz height width a
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz, C height, C width, Floating a) =>
Full meas vert horiz height width a
-> Full meas vert horiz height width a
-> Full meas vert horiz height width a
adaptOrder Full meas vert horiz height width a
y Full meas vert horiz height width a
x) Full meas vert horiz height width a
y
sub :: Full meas vert horiz height width a
-> Full meas vert horiz height width a
-> Full meas vert horiz height width a
sub Full meas vert horiz height width a
x Full meas vert horiz height width a
y = Full meas vert horiz height width a
-> Full meas vert horiz height width a
-> Full meas vert horiz height width a
forall sh a.
(C sh, Eq sh, Floating a) =>
Vector sh a -> Vector sh a -> Vector sh a
Vector.sub (Full meas vert horiz height width a
-> Full meas vert horiz height width a
-> Full meas vert horiz height width a
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz, C height, C width, Floating a) =>
Full meas vert horiz height width a
-> Full meas vert horiz height width a
-> Full meas vert horiz height width a
adaptOrder Full meas vert horiz height width a
y Full meas vert horiz height width a
x) Full meas vert horiz height width a
y


rowSums ::
   (Extent.Measure meas, Extent.C vert, Extent.C horiz,
    Shape.C height, Shape.C width, Class.Floating a) =>
   Full meas vert horiz height width a -> Vector height a
rowSums :: Full meas vert horiz height width a -> Vector height a
rowSums Full meas vert horiz height width a
m = Full meas vert horiz height width a
-> Vector width a -> Vector height a
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz, C height, C width, Floating a) =>
Full meas vert horiz height width a
-> Vector width a -> Vector height a
Basic.multiplyVectorUnchecked Full meas vert horiz height width a
m (Vector width a -> Vector height a)
-> Vector width a -> Vector height a
forall a b. (a -> b) -> a -> b
$ width -> Vector width a
forall sh a. (C sh, Floating a) => sh -> Vector sh a
Vector.one (width -> Vector width a) -> width -> Vector width a
forall a b. (a -> b) -> a -> b
$ Full meas vert horiz height width a -> width
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz) =>
Full meas vert horiz height width a -> width
Matrix.width Full meas vert horiz height width a
m

-- Does not work because incx must not be zero
_rowSums ::
   (Extent.Measure meas, Extent.C vert, Extent.C horiz,
    Shape.C height, Shape.C width, Class.Floating a) =>
   Full meas vert horiz height width a -> Vector height a
_rowSums :: Full meas vert horiz height width a -> Vector height a
_rowSums (Array shape :: Full meas vert horiz height width
shape@(Layout.Full Order
order Extent meas vert horiz height width
extent) ForeignPtr a
a) =
      height -> (Ptr a -> IO ()) -> Vector height a
forall sh a.
(C sh, Storable a) =>
sh -> (Ptr a -> IO ()) -> Array sh a
Array.unsafeCreate (Extent meas vert horiz height width -> height
forall meas vert horiz height width.
(Measure meas, C vert, C horiz) =>
Extent meas vert horiz height width -> height
Extent.height Extent meas vert horiz height width
extent) ((Ptr a -> IO ()) -> Vector height a)
-> (Ptr a -> IO ()) -> Vector height a
forall a b. (a -> b) -> a -> b
$ \Ptr a
yPtr -> do
   let (Int
m,Int
n) = Full meas vert horiz height width -> (Int, Int)
forall meas vert horiz height width.
(Measure meas, C vert, C horiz, C height, C width) =>
Full meas vert horiz height width -> (Int, Int)
Layout.dimensions Full meas vert horiz height width
shape
   let lda :: Int
lda = Int
m
   ContT () IO () -> IO ()
forall (m :: * -> *) r. Monad m => ContT r m r -> m r
evalContT (ContT () IO () -> IO ()) -> ContT () IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
      Ptr CChar
transPtr <- Char -> FortranIO () (Ptr CChar)
forall r. Char -> FortranIO r (Ptr CChar)
Call.char (Char -> FortranIO () (Ptr CChar))
-> Char -> FortranIO () (Ptr CChar)
forall a b. (a -> b) -> a -> b
$ Order -> Char
transposeFromOrder Order
order
      Ptr CInt
mPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint Int
m
      Ptr CInt
nPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint Int
n
      Ptr a
alphaPtr <- a -> FortranIO () (Ptr a)
forall a r. Floating a => a -> FortranIO r (Ptr a)
Call.number a
forall a. Floating a => a
one
      Ptr a
aPtr <- ((Ptr a -> IO ()) -> IO ()) -> FortranIO () (Ptr a)
forall k (r :: k) (m :: k -> *) a.
((a -> m r) -> m r) -> ContT r m a
ContT (((Ptr a -> IO ()) -> IO ()) -> FortranIO () (Ptr a))
-> ((Ptr a -> IO ()) -> IO ()) -> FortranIO () (Ptr a)
forall a b. (a -> b) -> a -> b
$ ForeignPtr a -> (Ptr a -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr a
a
      Ptr CInt
ldaPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.leadingDim Int
lda
      Ptr a
xPtr <- a -> FortranIO () (Ptr a)
forall a r. Floating a => a -> FortranIO r (Ptr a)
Call.number a
forall a. Floating a => a
one
      Ptr CInt
incxPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint Int
0
      Ptr a
betaPtr <- a -> FortranIO () (Ptr a)
forall a r. Floating a => a -> FortranIO r (Ptr a)
Call.number a
forall a. Floating a => a
zero
      Ptr CInt
incyPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint Int
1
      IO () -> ContT () IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ContT () IO ()) -> IO () -> ContT () IO ()
forall a b. (a -> b) -> a -> b
$
         Ptr CChar
-> Ptr CInt
-> Ptr CInt
-> Ptr a
-> Ptr a
-> Ptr CInt
-> Ptr a
-> Ptr CInt
-> Ptr a
-> Ptr a
-> Ptr CInt
-> IO ()
forall a.
Floating a =>
Ptr CChar
-> Ptr CInt
-> Ptr CInt
-> Ptr a
-> Ptr a
-> Ptr CInt
-> Ptr a
-> Ptr CInt
-> Ptr a
-> Ptr a
-> Ptr CInt
-> IO ()
BlasGen.gemv
            Ptr CChar
transPtr Ptr CInt
mPtr Ptr CInt
nPtr Ptr a
alphaPtr Ptr a
aPtr Ptr CInt
ldaPtr
            Ptr a
xPtr Ptr CInt
incxPtr Ptr a
betaPtr Ptr a
yPtr Ptr CInt
incyPtr

columnSums ::
   (Extent.Measure meas, Extent.C vert, Extent.C horiz,
    Shape.C height, Shape.C width, Class.Floating a) =>
   Full meas vert horiz height width a -> Vector width a
columnSums :: Full meas vert horiz height width a -> Vector width a
columnSums Full meas vert horiz height width a
m =
   Full meas horiz vert width height a
-> Vector height a -> Vector width a
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz, C height, C width, Floating a) =>
Full meas vert horiz height width a
-> Vector width a -> Vector height a
Basic.multiplyVectorUnchecked (Full meas vert horiz height width a
-> Full meas horiz vert width height a
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz) =>
Full meas vert horiz height width a
-> Full meas horiz vert width height a
transpose Full meas vert horiz height width a
m) (Vector height a -> Vector width a)
-> Vector height a -> Vector width a
forall a b. (a -> b) -> a -> b
$ height -> Vector height a
forall sh a. (C sh, Floating a) => sh -> Vector sh a
Vector.one (height -> Vector height a) -> height -> Vector height a
forall a b. (a -> b) -> a -> b
$ Full meas vert horiz height width a -> height
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz) =>
Full meas vert horiz height width a -> height
Matrix.height Full meas vert horiz height width a
m


rowArgAbsMaximums ::
   (Extent.Measure meas, Extent.C vert, Extent.C horiz,
    Shape.C height, Shape.InvIndexed width, Shape.Index width ~ ix, Storable ix,
    Class.Floating a) =>
   Full meas vert horiz height width a -> (Vector height ix, Vector height a)
rowArgAbsMaximums :: Full meas vert horiz height width a
-> (Vector height ix, Vector height a)
rowArgAbsMaximums (Array (Layout.Full Order
order Extent meas vert horiz height width
extent) ForeignPtr a
a) =
   let (height
height,width
width) = Extent meas vert horiz height width -> (height, width)
forall meas vert horiz height width.
(Measure meas, C vert, C horiz) =>
Extent meas vert horiz height width -> (height, width)
Extent.dimensions Extent meas vert horiz height width
extent
   in height
-> (Int -> Ptr ix -> IO (Vector height a))
-> (Vector height ix, Vector height a)
forall sh a b.
(C sh, Storable a) =>
sh -> (Int -> Ptr a -> IO b) -> (Array sh a, b)
Array.unsafeCreateWithSizeAndResult height
height ((Int -> Ptr ix -> IO (Vector height a))
 -> (Vector height ix, Vector height a))
-> (Int -> Ptr ix -> IO (Vector height a))
-> (Vector height ix, Vector height a)
forall a b. (a -> b) -> a -> b
$ \Int
m Ptr ix
ixPtr ->
      height -> (Ptr a -> IO ()) -> IO (Vector height a)
forall (m :: * -> *) sh a.
(PrimMonad m, C sh, Storable a) =>
sh -> (Ptr a -> IO ()) -> m (Array sh a)
ArrayIO.unsafeCreate height
height ((Ptr a -> IO ()) -> IO (Vector height a))
-> (Ptr a -> IO ()) -> IO (Vector height a)
forall a b. (a -> b) -> a -> b
$ \Ptr a
bPtr -> ContT () IO () -> IO ()
forall (m :: * -> *) r. Monad m => ContT r m r -> m r
evalContT (ContT () IO () -> IO ()) -> ContT () IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
      let n :: Int
n = width -> Int
forall sh. C sh => sh -> Int
Shape.size width
width
      let (Int
inca,Int
incx) =
            case Order
order of
               {-
               It would be more CPU friendly to compute the maximum row by row.
               Unfortunately BLAS does not support this mode.
               -}
               Order
ColumnMajor -> (Int
1,Int
m)
               Order
RowMajor -> (Int
n,Int
1)
      Ptr CInt
nPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint Int
n
      Ptr a
aPtr <- ((Ptr a -> IO ()) -> IO ()) -> ContT () IO (Ptr a)
forall k (r :: k) (m :: k -> *) a.
((a -> m r) -> m r) -> ContT r m a
ContT (((Ptr a -> IO ()) -> IO ()) -> ContT () IO (Ptr a))
-> ((Ptr a -> IO ()) -> IO ()) -> ContT () IO (Ptr a)
forall a b. (a -> b) -> a -> b
$ ForeignPtr a -> (Ptr a -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr a
a
      Ptr CInt
incxPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint Int
incx
      IO () -> ContT () IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ContT () IO ()) -> IO () -> ContT () IO ()
forall a b. (a -> b) -> a -> b
$ do
         String -> Bool -> IO ()
Call.assert String
"rowArgAbsMaximums: no columns" (Int
nInt -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>Int
0)
         [(Ptr a, (Ptr ix, Ptr a))]
-> ((Ptr a, (Ptr ix, Ptr a)) -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (Int -> [(Ptr a, (Ptr ix, Ptr a))] -> [(Ptr a, (Ptr ix, Ptr a))]
forall a. Int -> [a] -> [a]
take Int
m ([(Ptr a, (Ptr ix, Ptr a))] -> [(Ptr a, (Ptr ix, Ptr a))])
-> [(Ptr a, (Ptr ix, Ptr a))] -> [(Ptr a, (Ptr ix, Ptr a))]
forall a b. (a -> b) -> a -> b
$ [Ptr a] -> [(Ptr ix, Ptr a)] -> [(Ptr a, (Ptr ix, Ptr a))]
forall a b. [a] -> [b] -> [(a, b)]
zip (Int -> Ptr a -> [Ptr a]
forall a. Storable a => Int -> Ptr a -> [Ptr a]
pointerSeq Int
inca Ptr a
aPtr) ([(Ptr ix, Ptr a)] -> [(Ptr a, (Ptr ix, Ptr a))])
-> [(Ptr ix, Ptr a)] -> [(Ptr a, (Ptr ix, Ptr a))]
forall a b. (a -> b) -> a -> b
$
                [Ptr ix] -> [Ptr a] -> [(Ptr ix, Ptr a)]
forall a b. [a] -> [b] -> [(a, b)]
zip (Int -> Ptr ix -> [Ptr ix]
forall a. Storable a => Int -> Ptr a -> [Ptr a]
pointerSeq Int
1 Ptr ix
ixPtr) (Int -> Ptr a -> [Ptr a]
forall a. Storable a => Int -> Ptr a -> [Ptr a]
pointerSeq Int
1 Ptr a
bPtr)) (((Ptr a, (Ptr ix, Ptr a)) -> IO ()) -> IO ())
-> ((Ptr a, (Ptr ix, Ptr a)) -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$
               \(Ptr a
aiPtr,(Ptr ix
ixiPtr,Ptr a
biPtr)) -> do
            Int
ix <- Int -> Int
forall a. Enum a => a -> a
pred (Int -> Int) -> (CInt -> Int) -> CInt -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (CInt -> Int) -> IO CInt -> IO Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr CInt -> Ptr a -> Ptr CInt -> IO CInt
forall a. Floating a => Ptr CInt -> Ptr a -> Ptr CInt -> IO CInt
VectorPriv.absMax Ptr CInt
nPtr Ptr a
aiPtr Ptr CInt
incxPtr
            Ptr ix -> ix -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr ix
ixiPtr (ix -> IO ()) -> ix -> IO ()
forall a b. (a -> b) -> a -> b
$ width -> Int -> Index width
forall sh. InvIndexed sh => sh -> Int -> Index sh
Shape.indexFromOffset width
width Int
ix
            Ptr a -> a -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr a
biPtr (a -> IO ()) -> IO a -> IO ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Ptr a -> Int -> IO a
forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Ptr a
aiPtr (Int
ixInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
incx)

columnArgAbsMaximums ::
   (Extent.Measure meas, Extent.C vert, Extent.C horiz,
    Shape.InvIndexed height, Shape.C width,
    Shape.Index height ~ ix, Storable ix,
    Class.Floating a) =>
   Full meas vert horiz height width a -> (Vector width ix, Vector width a)
columnArgAbsMaximums :: Full meas vert horiz height width a
-> (Vector width ix, Vector width a)
columnArgAbsMaximums = Full meas horiz vert width height a
-> (Vector width ix, Vector width a)
forall meas vert horiz height width ix a.
(Measure meas, C vert, C horiz, C height, InvIndexed width,
 Index width ~ ix, Storable ix, Floating a) =>
Full meas vert horiz height width a
-> (Vector height ix, Vector height a)
rowArgAbsMaximums (Full meas horiz vert width height a
 -> (Vector width ix, Vector width a))
-> (Full meas vert horiz height width a
    -> Full meas horiz vert width height a)
-> Full meas vert horiz height width a
-> (Vector width ix, Vector width a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Full meas vert horiz height width a
-> Full meas horiz vert width height a
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz) =>
Full meas vert horiz height width a
-> Full meas horiz vert width height a
transpose


infixl 7 |*-

(|*-) ::
   (Shape.C height, Shape.C width, Class.Floating a) =>
   Vector height a -> Vector width a -> General height width a
|*- :: Vector height a -> Vector width a -> General height width a
(|*-) = Order
-> Vector height a -> Vector width a -> General height width a
forall height width a.
(C height, C width, Floating a) =>
Order
-> Vector height a -> Vector width a -> General height width a
tensorProduct Order
RowMajor

tensorProduct ::
   (Shape.C height, Shape.C width, Class.Floating a) =>
   Order -> Vector height a -> Vector width a -> General height width a
tensorProduct :: Order
-> Vector height a -> Vector width a -> General height width a
tensorProduct = Conjugation
-> Order
-> Vector height a
-> Vector width a
-> General height width a
forall height width a.
(C height, C width, Floating a) =>
Conjugation
-> Order
-> Vector height a
-> Vector width a
-> General height width a
tensorProductAux Conjugation
NonConjugated

outer ::
   (Shape.C height, Shape.C width, Class.Floating a) =>
   Order -> Vector height a -> Vector width a -> General height width a
outer :: Order
-> Vector height a -> Vector width a -> General height width a
outer = Conjugation
-> Order
-> Vector height a
-> Vector width a
-> General height width a
forall height width a.
(C height, C width, Floating a) =>
Conjugation
-> Order
-> Vector height a
-> Vector width a
-> General height width a
tensorProductAux Conjugation
Conjugated

tensorProductAux ::
   (Shape.C height, Shape.C width, Class.Floating a) =>
   Conjugation -> Order ->
   Vector height a -> Vector width a -> General height width a
tensorProductAux :: Conjugation
-> Order
-> Vector height a
-> Vector width a
-> General height width a
tensorProductAux Conjugation
conjugated Order
order Vector height a
x Vector width a
y =
   case Order
order of
      Order
ColumnMajor ->
         Full Size Big Big width height a -> General height width a
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz) =>
Full meas vert horiz height width a
-> Full meas horiz vert width height a
transpose (Full Size Big Big width height a -> General height width a)
-> Full Size Big Big width height a -> General height width a
forall a b. (a -> b) -> a -> b
$
         Array (width, height) a -> Full Size Big Big width height a
forall height width a.
(C height, C width, Storable a) =>
Array (height, width) a -> General height width a
fromRowMajor (Array (width, height) a -> Full Size Big Big width height a)
-> Array (width, height) a -> Full Size Big Big width height a
forall a b. (a -> b) -> a -> b
$ Either Conjugation Conjugation
-> Vector width a -> Vector height a -> Array (width, height) a
forall height width a.
(C height, C width, Floating a) =>
Either Conjugation Conjugation
-> Vector height a -> Vector width a -> Matrix height width a
RowMajor.tensorProduct (Conjugation -> Either Conjugation Conjugation
forall a b. b -> Either a b
Right Conjugation
conjugated) Vector width a
y Vector height a
x
      Order
RowMajor -> Array (height, width) a -> General height width a
forall height width a.
(C height, C width, Storable a) =>
Array (height, width) a -> General height width a
fromRowMajor (Array (height, width) a -> General height width a)
-> Array (height, width) a -> General height width a
forall a b. (a -> b) -> a -> b
$ Either Conjugation Conjugation
-> Vector height a -> Vector width a -> Array (height, width) a
forall height width a.
(C height, C width, Floating a) =>
Either Conjugation Conjugation
-> Vector height a -> Vector width a -> Matrix height width a
RowMajor.tensorProduct (Conjugation -> Either Conjugation Conjugation
forall a b. a -> Either a b
Left Conjugation
conjugated) Vector height a
x Vector width a
y


kronecker ::
   (Extent.Measure meas, Extent.C vert, Extent.C horiz,
    Shape.C heightA, Shape.C widthA, Shape.C heightB, Shape.C widthB,
    Class.Floating a) =>
   Full meas vert horiz heightA widthA a ->
   Full meas vert horiz heightB widthB a ->
   Full meas vert horiz (heightA,heightB) (widthA,widthB) a
kronecker :: Full meas vert horiz heightA widthA a
-> Full meas vert horiz heightB widthB a
-> Full meas vert horiz (heightA, heightB) (widthA, widthB) a
kronecker Full meas vert horiz heightA widthA a
a Full meas vert horiz heightB widthB a
b =
   let Layout.Full Order
orderB Extent meas vert horiz heightB widthB
extentB = Full meas vert horiz heightB widthB a
-> Full meas vert horiz heightB widthB
forall sh a. Array sh a -> sh
Array.shape Full meas vert horiz heightB widthB a
b
       shc :: Full meas vert horiz (heightA, heightB) (widthA, widthB)
shc =
         Order
-> Extent meas vert horiz (heightA, heightB) (widthA, widthB)
-> Full meas vert horiz (heightA, heightB) (widthA, widthB)
forall meas vert horiz height width.
Order
-> Extent meas vert horiz height width
-> Full meas vert horiz height width
Layout.Full Order
orderB (Extent meas vert horiz (heightA, heightB) (widthA, widthB)
 -> Full meas vert horiz (heightA, heightB) (widthA, widthB))
-> Extent meas vert horiz (heightA, heightB) (widthA, widthB)
-> Full meas vert horiz (heightA, heightB) (widthA, widthB)
forall a b. (a -> b) -> a -> b
$
         Extent meas vert horiz heightA widthA
-> Extent meas vert horiz heightB widthB
-> Extent meas vert horiz (heightA, heightB) (widthA, widthB)
forall meas vert horiz heightA widthA heightB widthB.
(Measure meas, C vert, C horiz) =>
Extent meas vert horiz heightA widthA
-> Extent meas vert horiz heightB widthB
-> Extent meas vert horiz (heightA, heightB) (widthA, widthB)
Extent.kronecker (Full meas vert horiz heightA widthA
-> Extent meas vert horiz heightA widthA
forall meas vert horiz height width.
Full meas vert horiz height width
-> Extent meas vert horiz height width
Layout.fullExtent (Full meas vert horiz heightA widthA
 -> Extent meas vert horiz heightA widthA)
-> Full meas vert horiz heightA widthA
-> Extent meas vert horiz heightA widthA
forall a b. (a -> b) -> a -> b
$ Full meas vert horiz heightA widthA a
-> Full meas vert horiz heightA widthA
forall sh a. Array sh a -> sh
Array.shape Full meas vert horiz heightA widthA a
a) Extent meas vert horiz heightB widthB
extentB
   in (Matrix heightB widthB a
 -> Full meas vert horiz (heightA, heightB) (widthA, widthB) a)
-> (Matrix widthB heightB a
    -> Full meas vert horiz (heightA, heightB) (widthA, widthB) a)
-> Either (Matrix heightB widthB a) (Matrix widthB heightB a)
-> Full meas vert horiz (heightA, heightB) (widthA, widthB) a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either
         (Full meas vert horiz (heightA, heightB) (widthA, widthB)
-> Array ((heightA, heightB), (widthA, widthB)) a
-> Full meas vert horiz (heightA, heightB) (widthA, widthB) a
forall sh1 sh0 a. sh1 -> Array sh0 a -> Array sh1 a
Array.reshape Full meas vert horiz (heightA, heightB) (widthA, widthB)
shc (Array ((heightA, heightB), (widthA, widthB)) a
 -> Full meas vert horiz (heightA, heightB) (widthA, widthB) a)
-> (Matrix heightB widthB a
    -> Array ((heightA, heightB), (widthA, widthB)) a)
-> Matrix heightB widthB a
-> Full meas vert horiz (heightA, heightB) (widthA, widthB) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Full meas vert horiz heightA widthA a
-> Matrix heightB widthB a
-> Array ((heightA, heightB), (widthA, widthB)) a
forall meas vert horiz heightA widthA heightB widthB a.
(Measure meas, C vert, C horiz, C heightA, C widthA, C heightB,
 C widthB, Floating a) =>
Full meas vert horiz heightA widthA a
-> Matrix heightB widthB a
-> Matrix (heightA, heightB) (widthA, widthB) a
RowMajor.kronecker Full meas vert horiz heightA widthA a
a)
         (Full meas vert horiz (heightA, heightB) (widthA, widthB)
-> Array ((widthA, widthB), (heightA, heightB)) a
-> Full meas vert horiz (heightA, heightB) (widthA, widthB) a
forall sh1 sh0 a. sh1 -> Array sh0 a -> Array sh1 a
Array.reshape Full meas vert horiz (heightA, heightB) (widthA, widthB)
shc (Array ((widthA, widthB), (heightA, heightB)) a
 -> Full meas vert horiz (heightA, heightB) (widthA, widthB) a)
-> (Matrix widthB heightB a
    -> Array ((widthA, widthB), (heightA, heightB)) a)
-> Matrix widthB heightB a
-> Full meas vert horiz (heightA, heightB) (widthA, widthB) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Full meas horiz vert widthA heightA a
-> Matrix widthB heightB a
-> Array ((widthA, widthB), (heightA, heightB)) a
forall meas vert horiz heightA widthA heightB widthB a.
(Measure meas, C vert, C horiz, C heightA, C widthA, C heightB,
 C widthB, Floating a) =>
Full meas vert horiz heightA widthA a
-> Matrix heightB widthB a
-> Matrix (heightA, heightB) (widthA, widthB) a
RowMajor.kronecker (Full meas vert horiz heightA widthA a
-> Full meas horiz vert widthA heightA a
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz) =>
Full meas vert horiz height width a
-> Full meas horiz vert width height a
transpose Full meas vert horiz heightA widthA a
a)) (Either (Matrix heightB widthB a) (Matrix widthB heightB a)
 -> Full meas vert horiz (heightA, heightB) (widthA, widthB) a)
-> Either (Matrix heightB widthB a) (Matrix widthB heightB a)
-> Full meas vert horiz (heightA, heightB) (widthA, widthB) a
forall a b. (a -> b) -> a -> b
$
      Full meas vert horiz heightB widthB a
-> Either (Matrix heightB widthB a) (Matrix widthB heightB a)
forall meas vert horiz height width a.
(Measure meas, C vert, C horiz) =>
Full meas vert horiz height width a
-> Either (Array (height, width) a) (Array (width, height) a)
Matrix.revealOrder Full meas vert horiz heightB widthB a
b


sumRank1 ::
   (Shape.C height, Eq height, Shape.C width, Eq width, Class.Floating a) =>
   (height,width) ->
   [(a, (Vector height a, Vector width a))] -> General height width a
sumRank1 :: (height, width)
-> [(a, (Vector height a, Vector width a))]
-> General height width a
sumRank1 (height
height,width
width) [(a, (Vector height a, Vector width a))]
xys =
   General height width
-> (Int -> Ptr a -> IO ()) -> General height width a
forall sh a.
(C sh, Storable a) =>
sh -> (Int -> Ptr a -> IO ()) -> Array sh a
Array.unsafeCreateWithSize (Order -> height -> width -> General height width
forall height width.
Order -> height -> width -> General height width
Layout.general Order
ColumnMajor height
height width
width) ((Int -> Ptr a -> IO ()) -> General height width a)
-> (Int -> Ptr a -> IO ()) -> General height width a
forall a b. (a -> b) -> a -> b
$
      \Int
size Ptr a
aPtr ->
   ContT () IO () -> IO ()
forall (m :: * -> *) r. Monad m => ContT r m r -> m r
evalContT (ContT () IO () -> IO ()) -> ContT () IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
      let m :: Int
m = height -> Int
forall sh. C sh => sh -> Int
Shape.size height
height
      let n :: Int
n = width -> Int
forall sh. C sh => sh -> Int
Shape.size width
width
      Ptr CInt
mPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint Int
m
      Ptr CInt
nPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint Int
n
      Ptr a
alphaPtr <- FortranIO () (Ptr a)
forall a r. Storable a => FortranIO r (Ptr a)
Call.alloca
      Ptr CInt
incxPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint Int
1
      Ptr CInt
incyPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint Int
1
      Ptr CInt
ldaPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.leadingDim Int
m
      IO () -> ContT () IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ContT () IO ()) -> IO () -> ContT () IO ()
forall a b. (a -> b) -> a -> b
$ do
         a -> Int -> Ptr a -> IO ()
forall a. Floating a => a -> Int -> Ptr a -> IO ()
fill a
forall a. Floating a => a
zero Int
size Ptr a
aPtr
         [(a, (Vector height a, Vector width a))]
-> ((a, (Vector height a, Vector width a)) -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(a, (Vector height a, Vector width a))]
xys (((a, (Vector height a, Vector width a)) -> IO ()) -> IO ())
-> ((a, (Vector height a, Vector width a)) -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \(a
alpha, (Array height
shX ForeignPtr a
x, Array width
shY ForeignPtr a
y)) ->
            ForeignPtr a -> (Ptr a -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr a
x ((Ptr a -> IO ()) -> IO ()) -> (Ptr a -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr a
xPtr ->
            ForeignPtr a -> (Ptr a -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr a
y ((Ptr a -> IO ()) -> IO ()) -> (Ptr a -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr a
yPtr -> do
               String -> Bool -> IO ()
Call.assert String
"Matrix.sumRank1: non-matching height" (height
heightheight -> height -> Bool
forall a. Eq a => a -> a -> Bool
==height
shX)
               String -> Bool -> IO ()
Call.assert String
"Matrix.sumRank1: non-matching width" (width
widthwidth -> width -> Bool
forall a. Eq a => a -> a -> Bool
==width
shY)
               Ptr a -> a -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr a
alphaPtr a
alpha
               Ptr CInt
-> Ptr CInt
-> Ptr a
-> Ptr a
-> Ptr CInt
-> Ptr a
-> Ptr CInt
-> Ptr a
-> Ptr CInt
-> IO ()
forall a.
Floating a =>
Ptr CInt
-> Ptr CInt
-> Ptr a
-> Ptr a
-> Ptr CInt
-> Ptr a
-> Ptr CInt
-> Ptr a
-> Ptr CInt
-> IO ()
BlasGen.gerc Ptr CInt
mPtr Ptr CInt
nPtr
                  Ptr a
alphaPtr Ptr a
xPtr Ptr CInt
incxPtr Ptr a
yPtr Ptr CInt
incyPtr Ptr a
aPtr Ptr CInt
ldaPtr