{-# 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.Shape.Private as MatrixShape
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 Numeric.LAPACK.Matrix.Shape.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
MatrixShape.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 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 Big Big height width -> Tall height width)
 -> General height width a -> Tall height width a)
-> (Full Big Big height width -> Tall height width)
-> General height width a
-> Tall height width a
forall a b. (a -> b) -> a -> b
$ \(MatrixShape.Full Order
order Extent 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
MatrixShape.tall Order
order) (Extent Big Big height width -> (height, width)
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> (height, width)
Extent.dimensions Extent 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 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 Big Big height width -> Wide height width)
 -> General height width a -> Wide height width a)
-> (Full Big Big height width -> Wide height width)
-> General height width a
-> Wide height width a
forall a b. (a -> b) -> a -> b
$ \(MatrixShape.Full Order
order Extent 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
MatrixShape.wide Order
order) (Extent Big Big height width -> (height, width)
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> (height, width)
Extent.dimensions Extent Big Big height width
extent)


identity ::
   (Shape.C sh, Class.Floating a) =>
   sh -> General sh sh a
identity :: sh -> General sh sh a
identity = Square sh a -> General sh sh a
forall vert horiz sh a.
(C vert, C horiz) =>
Square sh a -> Full vert horiz sh sh a
Square.toFull (Square sh a -> General sh sh a)
-> (sh -> Square sh a) -> sh -> General 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 ::
   (Shape.C sh, Class.Floating a) =>
   Vector sh a -> General sh sh a
diagonal :: Vector sh a -> General sh sh a
diagonal = Square sh a -> General sh sh a
forall vert horiz sh a.
(C vert, C horiz) =>
Square sh a -> Full vert horiz sh sh a
Square.toFull (Square sh a -> General sh sh a)
-> (Vector sh a -> Square sh a) -> Vector sh a -> General 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.GeneralTallWide vert horiz,
    Extent.GeneralTallWide horiz vert) =>
   (heightA -> heightB) ->
   Full vert horiz heightA width a ->
   Full vert horiz heightB width a
mapHeight :: (heightA -> heightB)
-> Full vert horiz heightA width a
-> Full vert horiz heightB width a
mapHeight heightA -> heightB
f = (heightA -> heightB)
-> Full vert horiz heightA width a
-> Full vert horiz heightB width a
forall vert horiz heightA heightB width a.
(GeneralTallWide vert horiz, GeneralTallWide horiz vert) =>
(heightA -> heightB)
-> Full vert horiz heightA width a
-> Full vert horiz heightB width a
Basic.mapHeight ((heightA -> heightB)
 -> Full vert horiz heightA width a
 -> Full vert horiz heightB width a)
-> (heightA -> heightB)
-> Full vert horiz heightA width a
-> Full 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
MatrixShape.mapChecked String
"mapHeight" heightA -> heightB
f

mapWidth ::
   (Shape.C widthA, Shape.C widthB,
    Extent.GeneralTallWide vert horiz,
    Extent.GeneralTallWide horiz vert) =>
   (widthA -> widthB) ->
   Full vert horiz height widthA a ->
   Full vert horiz height widthB a
mapWidth :: (widthA -> widthB)
-> Full vert horiz height widthA a
-> Full vert horiz height widthB a
mapWidth widthA -> widthB
f = (widthA -> widthB)
-> Full vert horiz height widthA a
-> Full vert horiz height widthB a
forall vert horiz widthA widthB height a.
(GeneralTallWide vert horiz, GeneralTallWide horiz vert) =>
(widthA -> widthB)
-> Full vert horiz height widthA a
-> Full vert horiz height widthB a
Basic.mapWidth ((widthA -> widthB)
 -> Full vert horiz height widthA a
 -> Full vert horiz height widthB a)
-> (widthA -> widthB)
-> Full vert horiz height widthA a
-> Full 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
MatrixShape.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 Big Big ShapeInt width a -> General height width a
forall vert horiz heightA heightB width a.
(GeneralTallWide vert horiz, GeneralTallWide horiz vert) =>
(heightA -> heightB)
-> Full vert horiz heightA width a
-> Full 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 Big Big ShapeInt width a -> General height width a)
-> Full Big Big ShapeInt width a -> General height width a
forall a b. (a -> b) -> a -> b
$
   width -> [Vector width a] -> Full 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 Big Big ShapeInt width a)
-> [Vector width a] -> Full 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 Big Big ShapeInt width a -> General (Shape f) width a
forall vert horiz heightA heightB width a.
(GeneralTallWide vert horiz, GeneralTallWide horiz vert) =>
(heightA -> heightB)
-> Full vert horiz heightA width a
-> Full 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 Big Big ShapeInt width a -> General (Shape f) width a)
-> Full Big Big ShapeInt width a -> General (Shape f) width a
forall a b. (a -> b) -> a -> b
$
   width -> [Vector width a] -> Full 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 Big Big ShapeInt width a)
-> [Vector width a] -> Full 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 Big Big height ShapeInt a -> General height width a
forall vert horiz widthA widthB height a.
(GeneralTallWide vert horiz, GeneralTallWide horiz vert) =>
(widthA -> widthB)
-> Full vert horiz height widthA a
-> Full 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 Big Big height ShapeInt a -> General height width a)
-> Full Big Big height ShapeInt a -> General height width a
forall a b. (a -> b) -> a -> b
$
   height -> [Vector height a] -> Full 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 Big Big height ShapeInt a)
-> [Vector height a] -> Full 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 Big Big height ShapeInt a -> General height (Shape f) a
forall vert horiz widthA widthB height a.
(GeneralTallWide vert horiz, GeneralTallWide horiz vert) =>
(widthA -> widthB)
-> Full vert horiz height widthA a
-> Full 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 Big Big height ShapeInt a -> General height (Shape f) a)
-> Full Big Big height ShapeInt a -> General height (Shape f) a
forall a b. (a -> b) -> a -> b
$
   height -> [Vector height a] -> Full 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 Big Big height ShapeInt a)
-> [Vector height a] -> Full 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 Big Big ShapeInt height a -> General height ShapeInt a
forall vert horiz height width a.
(C vert, C horiz) =>
Full vert horiz height width a -> Full horiz vert width height a
transpose (Full Big Big ShapeInt height a -> General height ShapeInt a)
-> ([Vector height a] -> Full 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 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 Big Big ShapeInt height a)
-> ([Vector height a] -> Array (ShapeInt, height) a)
-> [Vector height a]
-> Full 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.C vert, Extent.C horiz,
    Shape.C height, Shape.C width, Class.Floating a) =>
   Full vert horiz height width a -> [Vector width a]
toRows :: Full vert horiz height width a -> [Vector width a]
toRows Full vert horiz height width a
a =
   let ad :: Full Big Big (Deferred height) width a
ad = (height -> Deferred height)
-> Full Big Big height width a
-> Full Big Big (Deferred height) width a
forall vert horiz heightA heightB width a.
(GeneralTallWide vert horiz, GeneralTallWide horiz vert) =>
(heightA -> heightB)
-> Full vert horiz heightA width a
-> Full vert horiz heightB width a
Basic.mapHeight height -> Deferred height
forall sh. sh -> Deferred sh
Shape.Deferred (Full Big Big height width a
 -> Full Big Big (Deferred height) width a)
-> Full Big Big height width a
-> Full Big Big (Deferred height) width a
forall a b. (a -> b) -> a -> b
$ Full vert horiz height width a -> Full Big Big height width a
forall vert horiz height width a.
(C vert, C horiz) =>
Full vert horiz height width a -> General height width a
fromFull Full 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 Big Big (Deferred height) width a
-> DeferredIndex height -> Vector width a
forall vert horiz height width ix a.
(C vert, C horiz, Indexed height, C width, Index height ~ ix,
 Floating a) =>
Full vert horiz height width a -> ix -> Vector width a
takeRow Full 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 Big Big (Deferred height) width a
-> HeightOf (Full Big Big (Deferred height) width)
forall shape a. Box shape => Array shape a -> HeightOf shape
Matrix.height Full Big Big (Deferred height) width a
ad

toColumns ::
   (Extent.C vert, Extent.C horiz,
    Shape.C height, Shape.C width, Class.Floating a) =>
   Full vert horiz height width a -> [Vector height a]
toColumns :: Full vert horiz height width a -> [Vector height a]
toColumns Full vert horiz height width a
a =
   let ad :: Full Big Big height (Deferred width) a
ad = (width -> Deferred width)
-> Full Big Big height width a
-> Full Big Big height (Deferred width) a
forall vert horiz widthA widthB height a.
(GeneralTallWide vert horiz, GeneralTallWide horiz vert) =>
(widthA -> widthB)
-> Full vert horiz height widthA a
-> Full vert horiz height widthB a
Basic.mapWidth width -> Deferred width
forall sh. sh -> Deferred sh
Shape.Deferred (Full Big Big height width a
 -> Full Big Big height (Deferred width) a)
-> Full Big Big height width a
-> Full Big Big height (Deferred width) a
forall a b. (a -> b) -> a -> b
$ Full vert horiz height width a -> Full Big Big height width a
forall vert horiz height width a.
(C vert, C horiz) =>
Full vert horiz height width a -> General height width a
fromFull Full 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 Big Big height (Deferred width) a
-> DeferredIndex width -> Vector height a
forall vert horiz height width ix a.
(C vert, C horiz, C height, Indexed width, Index width ~ ix,
 Floating a) =>
Full vert horiz height width a -> ix -> Vector height a
takeColumn Full 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 Big Big height (Deferred width) a
-> WidthOf (Full Big Big height (Deferred width))
forall shape a. Box shape => Array shape a -> WidthOf shape
Matrix.width Full Big Big height (Deferred width) a
ad

toRowArray ::
   (Extent.C vert, Extent.C horiz,
    Shape.C height, Shape.C width, Class.Floating a) =>
   Full vert horiz height width a -> BoxedArray.Array height (Vector width a)
toRowArray :: Full vert horiz height width a -> Array height (Vector width a)
toRowArray Full 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 vert horiz height width a
-> HeightOf (Full vert horiz height width)
forall shape a. Box shape => Array shape a -> HeightOf shape
Matrix.height Full vert horiz height width a
a) (Full vert horiz height width a -> [Vector width a]
forall vert horiz height width a.
(C vert, C horiz, C height, C width, Floating a) =>
Full vert horiz height width a -> [Vector width a]
toRows Full vert horiz height width a
a)

toColumnArray ::
   (Extent.C vert, Extent.C horiz,
    Shape.C height, Shape.C width, Class.Floating a) =>
   Full vert horiz height width a -> BoxedArray.Array width (Vector height a)
toColumnArray :: Full vert horiz height width a -> Array width (Vector height a)
toColumnArray Full 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 vert horiz height width a
-> WidthOf (Full vert horiz height width)
forall shape a. Box shape => Array shape a -> WidthOf shape
Matrix.width Full vert horiz height width a
a) (Full vert horiz height width a -> [Vector height a]
forall vert horiz height width a.
(C vert, C horiz, C height, C width, Floating a) =>
Full vert horiz height width a -> [Vector height a]
toColumns Full vert horiz height width a
a)

toRowContainer ::
   (Extent.C vert, Extent.C horiz,
    Container.C f, Shape.C width, Class.Floating a) =>
   Full vert horiz (Container.Shape f) width a -> f (Vector width a)
toRowContainer :: Full vert horiz (Shape f) width a -> f (Vector width a)
toRowContainer Full 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 vert horiz (Shape f) width a
-> HeightOf (Full vert horiz (Shape f) width)
forall shape a. Box shape => Array shape a -> HeightOf shape
Matrix.height Full vert horiz (Shape f) width a
a) (Full vert horiz (Shape f) width a -> [Vector width a]
forall vert horiz height width a.
(C vert, C horiz, C height, C width, Floating a) =>
Full vert horiz height width a -> [Vector width a]
toRows Full vert horiz (Shape f) width a
a)

toColumnContainer ::
   (Extent.C vert, Extent.C horiz,
    Shape.C height, Container.C f, Class.Floating a) =>
   Full vert horiz height (Container.Shape f) a -> f (Vector height a)
toColumnContainer :: Full vert horiz height (Shape f) a -> f (Vector height a)
toColumnContainer Full 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 vert horiz height (Shape f) a
-> WidthOf (Full vert horiz height (Shape f))
forall shape a. Box shape => Array shape a -> WidthOf shape
Matrix.width Full vert horiz height (Shape f) a
a) (Full vert horiz height (Shape f) a -> [Vector height a]
forall vert horiz height width a.
(C vert, C horiz, C height, C width, Floating a) =>
Full vert horiz height width a -> [Vector height a]
toColumns Full vert horiz height (Shape f) a
a)


takeRow ::
   (Extent.C vert, Extent.C horiz,
    Shape.Indexed height, Shape.C width, Shape.Index height ~ ix,
    Class.Floating a) =>
   Full vert horiz height width a -> ix -> Vector width a
takeRow :: Full vert horiz height width a -> ix -> Vector width a
takeRow Full 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 vert horiz height width a
-> Either (Matrix height width a) (Matrix width height a)
forall vert horiz height width a.
(C vert, C horiz) =>
Full vert horiz height width a
-> Either (Array (height, width) a) (Array (width, height) a)
Matrix.revealOrder Full vert horiz height width a
x

takeColumn ::
   (Extent.C vert, Extent.C horiz,
    Shape.C height, Shape.Indexed width, Shape.Index width ~ ix,
    Class.Floating a) =>
   Full vert horiz height width a -> ix -> Vector height a
takeColumn :: Full vert horiz height width a -> ix -> Vector height a
takeColumn Full 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 vert horiz height width a
-> Either (Matrix height width a) (Matrix width height a)
forall vert horiz height width a.
(C vert, C horiz) =>
Full vert horiz height width a
-> Either (Array (height, width) a) (Array (width, height) a)
Matrix.revealOrder Full vert horiz height width a
x


takeEqually ::
   (Extent.C vert, Extent.C horiz, Class.Floating a) =>
   Int ->
   Full vert horiz ShapeInt ShapeInt a ->
   Full vert horiz ShapeInt ShapeInt a
takeEqually :: Int
-> Full vert horiz ShapeInt ShapeInt a
-> Full vert horiz ShapeInt ShapeInt a
takeEqually Int
k (Array (MatrixShape.Full Order
order Extent vert horiz ShapeInt ShapeInt
extentA) ForeignPtr a
a) =
   let (Shape.ZeroBased Int
heightA, Shape.ZeroBased Int
widthA) =
         Extent vert horiz ShapeInt ShapeInt -> (ShapeInt, ShapeInt)
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> (height, width)
Extent.dimensions Extent 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 vert horiz ShapeInt ShapeInt
extentB =
         ShapeInt
-> ShapeInt
-> Extent vert horiz ShapeInt ShapeInt
-> Extent vert horiz ShapeInt ShapeInt
forall vert horiz height width.
(C vert, C horiz) =>
height
-> width
-> Extent vert horiz height width
-> Extent 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 vert horiz ShapeInt ShapeInt
extentA
   in Bool
-> Full vert horiz ShapeInt ShapeInt a
-> Full vert horiz ShapeInt ShapeInt a
-> Full 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 vert horiz ShapeInt ShapeInt a
forall a. HasCallStack => String -> a
error String
"take: negative number") (Full vert horiz ShapeInt ShapeInt a
 -> Full vert horiz ShapeInt ShapeInt a)
-> Full vert horiz ShapeInt ShapeInt a
-> Full vert horiz ShapeInt ShapeInt a
forall a b. (a -> b) -> a -> b
$
      Full vert horiz ShapeInt ShapeInt
-> (Ptr a -> IO ()) -> Full vert horiz ShapeInt ShapeInt a
forall sh a.
(C sh, Storable a) =>
sh -> (Ptr a -> IO ()) -> Array sh a
Array.unsafeCreate (Order
-> Extent vert horiz ShapeInt ShapeInt
-> Full vert horiz ShapeInt ShapeInt
forall vert horiz height width.
Order
-> Extent vert horiz height width -> Full vert horiz height width
MatrixShape.Full Order
order Extent vert horiz ShapeInt ShapeInt
extentB) ((Ptr a -> IO ()) -> Full vert horiz ShapeInt ShapeInt a)
-> (Ptr a -> IO ()) -> Full 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.C vert, Extent.C horiz, Class.Floating a) =>
   Int ->
   Full vert horiz ShapeInt ShapeInt a ->
   Full vert horiz ShapeInt ShapeInt a
dropEqually :: Int
-> Full vert horiz ShapeInt ShapeInt a
-> Full vert horiz ShapeInt ShapeInt a
dropEqually Int
k (Array (MatrixShape.Full Order
order Extent vert horiz ShapeInt ShapeInt
extentA) ForeignPtr a
a) =
   let (Shape.ZeroBased Int
heightA, Shape.ZeroBased Int
widthA) =
         Extent vert horiz ShapeInt ShapeInt -> (ShapeInt, ShapeInt)
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> (height, width)
Extent.dimensions Extent 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 vert horiz ShapeInt ShapeInt
extentB =
         ShapeInt
-> ShapeInt
-> Extent vert horiz ShapeInt ShapeInt
-> Extent vert horiz ShapeInt ShapeInt
forall vert horiz height width.
(C vert, C horiz) =>
height
-> width
-> Extent vert horiz height width
-> Extent 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 vert horiz ShapeInt ShapeInt
extentA
   in Bool
-> Full vert horiz ShapeInt ShapeInt a
-> Full vert horiz ShapeInt ShapeInt a
-> Full 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 vert horiz ShapeInt ShapeInt a
forall a. HasCallStack => String -> a
error String
"drop: negative number") (Full vert horiz ShapeInt ShapeInt a
 -> Full vert horiz ShapeInt ShapeInt a)
-> Full vert horiz ShapeInt ShapeInt a
-> Full vert horiz ShapeInt ShapeInt a
forall a b. (a -> b) -> a -> b
$
      Full vert horiz ShapeInt ShapeInt
-> (Ptr a -> IO ()) -> Full vert horiz ShapeInt ShapeInt a
forall sh a.
(C sh, Storable a) =>
sh -> (Ptr a -> IO ()) -> Array sh a
Array.unsafeCreate (Order
-> Extent vert horiz ShapeInt ShapeInt
-> Full vert horiz ShapeInt ShapeInt
forall vert horiz height width.
Order
-> Extent vert horiz height width -> Full vert horiz height width
MatrixShape.Full Order
order Extent vert horiz ShapeInt ShapeInt
extentB) ((Ptr a -> IO ()) -> Full vert horiz ShapeInt ShapeInt a)
-> (Ptr a -> IO ()) -> Full 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.C vert, Extent.C horiz,
    Shape.Indexed height, Shape.C width, Class.Floating a) =>
   Shape.Index height -> Shape.Index height ->
   Full vert horiz height width a -> Full vert horiz height width a
swapRows :: Index height
-> Index height
-> Full vert horiz height width a
-> Full vert horiz height width a
swapRows Index height
i Index height
j (Array shape :: Full vert horiz height width
shape@(MatrixShape.Full Order
order Extent vert horiz height width
extent) ForeignPtr a
a) =
   Full vert horiz height width
-> (Int -> Ptr a -> IO ()) -> Full vert horiz height width a
forall sh a.
(C sh, Storable a) =>
sh -> (Int -> Ptr a -> IO ()) -> Array sh a
Array.unsafeCreateWithSize Full vert horiz height width
shape ((Int -> Ptr a -> IO ()) -> Full vert horiz height width a)
-> (Int -> Ptr a -> IO ()) -> Full 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 vert horiz height width -> (height, width)
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> (height, width)
Extent.dimensions Extent vert horiz height width
extent
      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.C vert, Extent.C horiz,
    Shape.C height, Shape.Indexed width, Class.Floating a) =>
   Shape.Index width -> Shape.Index width ->
   Full vert horiz height width a -> Full vert horiz height width a
swapColumns :: Index width
-> Index width
-> Full vert horiz height width a
-> Full vert horiz height width a
swapColumns Index width
i Index width
j = Full horiz vert width height a -> Full vert horiz height width a
forall vert horiz height width a.
(C vert, C horiz) =>
Full vert horiz height width a -> Full horiz vert width height a
transpose (Full horiz vert width height a -> Full vert horiz height width a)
-> (Full vert horiz height width a
    -> Full horiz vert width height a)
-> Full vert horiz height width a
-> Full vert horiz height width a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index width
-> Index width
-> Full horiz vert width height a
-> Full horiz vert width height a
forall vert horiz height width a.
(C vert, C horiz, Indexed height, C width, Floating a) =>
Index height
-> Index height
-> Full vert horiz height width a
-> Full vert horiz height width a
swapRows Index width
i Index width
j (Full horiz vert width height a -> Full horiz vert width height a)
-> (Full vert horiz height width a
    -> Full horiz vert width height a)
-> Full vert horiz height width a
-> Full horiz vert width height a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Full vert horiz height width a -> Full horiz vert width height a
forall vert horiz height width a.
(C vert, C horiz) =>
Full vert horiz height width a -> Full horiz vert width height a
transpose


-- alternative: laswp
reverseRows ::
   (Extent.C vert, Extent.C horiz, Shape.C width, Class.Floating a) =>
   Full vert horiz ShapeInt width a -> Full vert horiz ShapeInt width a
reverseRows :: Full vert horiz ShapeInt width a
-> Full vert horiz ShapeInt width a
reverseRows (Array shape :: Full vert horiz ShapeInt width
shape@(MatrixShape.Full Order
order Extent vert horiz ShapeInt width
extent) ForeignPtr a
a) =
   Full vert horiz ShapeInt width
-> (Int -> Ptr a -> IO ()) -> Full vert horiz ShapeInt width a
forall sh a.
(C sh, Storable a) =>
sh -> (Int -> Ptr a -> IO ()) -> Array sh a
Array.unsafeCreateWithSize Full vert horiz ShapeInt width
shape ((Int -> Ptr a -> IO ()) -> Full vert horiz ShapeInt width a)
-> (Int -> Ptr a -> IO ()) -> Full vert horiz ShapeInt 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 (ShapeInt
height,width
width) = Extent vert horiz ShapeInt width -> (ShapeInt, width)
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> (height, width)
Extent.dimensions Extent vert horiz ShapeInt width
extent
      let n :: Int
n = ShapeInt -> Int
forall sh. C sh => sh -> Int
Shape.size ShapeInt
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.C vert, Extent.C horiz, Shape.C height, Class.Floating a) =>
   Full vert horiz height ShapeInt a -> Full vert horiz height ShapeInt a
reverseColumns :: Full vert horiz height ShapeInt a
-> Full vert horiz height ShapeInt a
reverseColumns = Full horiz vert ShapeInt height a
-> Full vert horiz height ShapeInt a
forall vert horiz height width a.
(C vert, C horiz) =>
Full vert horiz height width a -> Full horiz vert width height a
transpose (Full horiz vert ShapeInt height a
 -> Full vert horiz height ShapeInt a)
-> (Full vert horiz height ShapeInt a
    -> Full horiz vert ShapeInt height a)
-> Full vert horiz height ShapeInt a
-> Full vert horiz height ShapeInt a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Full horiz vert ShapeInt height a
-> Full horiz vert ShapeInt height a
forall vert horiz width a.
(C vert, C horiz, C width, Floating a) =>
Full vert horiz ShapeInt width a
-> Full vert horiz ShapeInt width a
reverseRows (Full horiz vert ShapeInt height a
 -> Full horiz vert ShapeInt height a)
-> (Full vert horiz height ShapeInt a
    -> Full horiz vert ShapeInt height a)
-> Full vert horiz height ShapeInt a
-> Full horiz vert ShapeInt height a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Full vert horiz height ShapeInt a
-> Full horiz vert ShapeInt height a
forall vert horiz height width a.
(C vert, C horiz) =>
Full vert horiz height width a -> Full 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 (MatrixShape.Full Order
order Extent Big Big height width
extent) ForeignPtr a
a) =
   let (height
heightA, width
width) = Extent Big Big height width -> (height, width)
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> (height, width)
Extent.dimensions Extent 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
MatrixShape.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 Big Big sh height a -> General height sh a
forall vert horiz height width a.
(C vert, C horiz) =>
Full vert horiz height width a -> Full horiz vert width height a
transpose (Full Big Big sh height a -> General height sh a)
-> (General height width a -> Full 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 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 Big Big sh height a)
-> (General height width a -> General width height a)
-> General height width a
-> Full Big Big sh height a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. General height width a -> General width height a
forall vert horiz height width a.
(C vert, C horiz) =>
Full vert horiz height width a -> Full 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
MatrixShape.general Order
RowMajor)

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

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


add, sub ::
   (Extent.C vert, Extent.C horiz,
    Shape.C height, Shape.C width, Eq height, Eq width,
    Class.Floating a) =>
   Full vert horiz height width a ->
   Full vert horiz height width a ->
   Full vert horiz height width a
add :: Full vert horiz height width a
-> Full vert horiz height width a -> Full vert horiz height width a
add Full vert horiz height width a
x Full vert horiz height width a
y = Full vert horiz height width a
-> Full vert horiz height width a -> Full 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 vert horiz height width a
-> Full vert horiz height width a -> Full vert horiz height width a
forall vert horiz height width a.
(C vert, C horiz, C height, C width, Floating a) =>
Full vert horiz height width a
-> Full vert horiz height width a -> Full vert horiz height width a
adaptOrder Full vert horiz height width a
y Full vert horiz height width a
x) Full vert horiz height width a
y
sub :: Full vert horiz height width a
-> Full vert horiz height width a -> Full vert horiz height width a
sub Full vert horiz height width a
x Full vert horiz height width a
y = Full vert horiz height width a
-> Full vert horiz height width a -> Full 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 vert horiz height width a
-> Full vert horiz height width a -> Full vert horiz height width a
forall vert horiz height width a.
(C vert, C horiz, C height, C width, Floating a) =>
Full vert horiz height width a
-> Full vert horiz height width a -> Full vert horiz height width a
adaptOrder Full vert horiz height width a
y Full vert horiz height width a
x) Full vert horiz height width a
y


rowSums ::
   (Extent.C vert, Extent.C horiz,
    Shape.C height, Shape.C width, Class.Floating a) =>
   Full vert horiz height width a -> Vector height a
rowSums :: Full vert horiz height width a -> Vector height a
rowSums Full vert horiz height width a
m = Full vert horiz height width a -> Vector width a -> Vector height a
forall vert horiz height width a.
(C vert, C horiz, C height, C width, Floating a) =>
Full vert horiz height width a -> Vector width a -> Vector height a
Basic.multiplyVectorUnchecked Full 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 vert horiz height width a
-> WidthOf (Full vert horiz height width)
forall shape a. Box shape => Array shape a -> WidthOf shape
Matrix.width Full vert horiz height width a
m

-- Does not work because incx must not be zero
_rowSums ::
   (Extent.C vert, Extent.C horiz,
    Shape.C height, Shape.C width, Class.Floating a) =>
   Full vert horiz height width a -> Vector height a
_rowSums :: Full vert horiz height width a -> Vector height a
_rowSums (Array shape :: Full vert horiz height width
shape@(MatrixShape.Full Order
order Extent 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 vert horiz height width -> height
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> height
Extent.height Extent vert horiz height width
extent) ((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 vert horiz height width -> (Int, Int)
forall vert horiz height width.
(C vert, C horiz, C height, C width) =>
Full vert horiz height width -> (Int, Int)
MatrixShape.dimensions Full 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.C vert, Extent.C horiz,
    Shape.C height, Shape.C width, Class.Floating a) =>
   Full vert horiz height width a -> Vector width a
columnSums :: Full vert horiz height width a -> Vector width a
columnSums Full vert horiz height width a
m =
   Full horiz vert width height a -> Vector height a -> Vector width a
forall vert horiz height width a.
(C vert, C horiz, C height, C width, Floating a) =>
Full vert horiz height width a -> Vector width a -> Vector height a
Basic.multiplyVectorUnchecked (Full vert horiz height width a -> Full horiz vert width height a
forall vert horiz height width a.
(C vert, C horiz) =>
Full vert horiz height width a -> Full horiz vert width height a
transpose Full 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 vert horiz height width a
-> HeightOf (Full vert horiz height width)
forall shape a. Box shape => Array shape a -> HeightOf shape
Matrix.height Full vert horiz height width a
m


rowArgAbsMaximums ::
   (Extent.C vert, Extent.C horiz,
    Shape.C height, Shape.InvIndexed width, Shape.Index width ~ ix, Storable ix,
    Class.Floating a) =>
   Full vert horiz height width a -> (Vector height ix, Vector height a)
rowArgAbsMaximums :: Full vert horiz height width a
-> (Vector height ix, Vector height a)
rowArgAbsMaximums (Array (MatrixShape.Full Order
order Extent vert horiz height width
extent) ForeignPtr a
a) =
   let (height
height,width
width) = Extent vert horiz height width -> (height, width)
forall vert horiz height width.
(C vert, C horiz) =>
Extent vert horiz height width -> (height, width)
Extent.dimensions Extent vert horiz height width
extent
   in 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.C vert, Extent.C horiz,
    Shape.InvIndexed height, Shape.C width,
    Shape.Index height ~ ix, Storable ix,
    Class.Floating a) =>
   Full vert horiz height width a -> (Vector width ix, Vector width a)
columnArgAbsMaximums :: Full vert horiz height width a -> (Vector width ix, Vector width a)
columnArgAbsMaximums = Full horiz vert width height a -> (Vector width ix, Vector width a)
forall vert horiz height width ix a.
(C vert, C horiz, C height, InvIndexed width, Index width ~ ix,
 Storable ix, Floating a) =>
Full vert horiz height width a
-> (Vector height ix, Vector height a)
rowArgAbsMaximums (Full horiz vert width height a
 -> (Vector width ix, Vector width a))
-> (Full vert horiz height width a
    -> Full horiz vert width height a)
-> Full vert horiz height width a
-> (Vector width ix, Vector width a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Full vert horiz height width a -> Full horiz vert width height a
forall vert horiz height width a.
(C vert, C horiz) =>
Full vert horiz height width a -> Full 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 Big Big width height a -> General height width a
forall vert horiz height width a.
(C vert, C horiz) =>
Full vert horiz height width a -> Full horiz vert width height a
transpose (Full Big Big width height a -> General height width a)
-> Full Big Big width height a -> General height width a
forall a b. (a -> b) -> a -> b
$
         Array (width, height) a -> Full 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 Big Big width height a)
-> Array (width, height) a -> Full 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.C vert, Extent.C horiz,
    Shape.C heightA, Shape.C widthA, Shape.C heightB, Shape.C widthB,
    Class.Floating a) =>
   Full vert horiz heightA widthA a ->
   Full vert horiz heightB widthB a ->
   Full vert horiz (heightA,heightB) (widthA,widthB) a
kronecker :: Full vert horiz heightA widthA a
-> Full vert horiz heightB widthB a
-> Full vert horiz (heightA, heightB) (widthA, widthB) a
kronecker Full vert horiz heightA widthA a
a Full vert horiz heightB widthB a
b =
   let MatrixShape.Full Order
orderB Extent vert horiz heightB widthB
extentB = Full vert horiz heightB widthB a -> Full vert horiz heightB widthB
forall sh a. Array sh a -> sh
Array.shape Full vert horiz heightB widthB a
b
       shc :: Full vert horiz (heightA, heightB) (widthA, widthB)
shc =
         Order
-> Extent vert horiz (heightA, heightB) (widthA, widthB)
-> Full vert horiz (heightA, heightB) (widthA, widthB)
forall vert horiz height width.
Order
-> Extent vert horiz height width -> Full vert horiz height width
MatrixShape.Full Order
orderB (Extent vert horiz (heightA, heightB) (widthA, widthB)
 -> Full vert horiz (heightA, heightB) (widthA, widthB))
-> Extent vert horiz (heightA, heightB) (widthA, widthB)
-> Full vert horiz (heightA, heightB) (widthA, widthB)
forall a b. (a -> b) -> a -> b
$
         Extent vert horiz heightA widthA
-> Extent vert horiz heightB widthB
-> Extent vert horiz (heightA, heightB) (widthA, widthB)
forall vert horiz heightA widthA heightB widthB.
(C vert, C horiz) =>
Extent vert horiz heightA widthA
-> Extent vert horiz heightB widthB
-> Extent vert horiz (heightA, heightB) (widthA, widthB)
Extent.kronecker (Full vert horiz heightA widthA -> Extent vert horiz heightA widthA
forall vert horiz height width.
Full vert horiz height width -> Extent vert horiz height width
MatrixShape.fullExtent (Full vert horiz heightA widthA
 -> Extent vert horiz heightA widthA)
-> Full vert horiz heightA widthA
-> Extent vert horiz heightA widthA
forall a b. (a -> b) -> a -> b
$ Full vert horiz heightA widthA a -> Full vert horiz heightA widthA
forall sh a. Array sh a -> sh
Array.shape Full vert horiz heightA widthA a
a) Extent vert horiz heightB widthB
extentB
   in (Matrix heightB widthB a
 -> Full vert horiz (heightA, heightB) (widthA, widthB) a)
-> (Matrix widthB heightB a
    -> Full vert horiz (heightA, heightB) (widthA, widthB) a)
-> Either (Matrix heightB widthB a) (Matrix widthB heightB a)
-> Full vert horiz (heightA, heightB) (widthA, widthB) a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either
         (Full vert horiz (heightA, heightB) (widthA, widthB)
-> Array ((heightA, heightB), (widthA, widthB)) a
-> Full vert horiz (heightA, heightB) (widthA, widthB) a
forall sh1 sh0 a. sh1 -> Array sh0 a -> Array sh1 a
Array.reshape Full vert horiz (heightA, heightB) (widthA, widthB)
shc (Array ((heightA, heightB), (widthA, widthB)) a
 -> Full vert horiz (heightA, heightB) (widthA, widthB) a)
-> (Matrix heightB widthB a
    -> Array ((heightA, heightB), (widthA, widthB)) a)
-> Matrix heightB widthB a
-> Full vert horiz (heightA, heightB) (widthA, widthB) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Full vert horiz heightA widthA a
-> Matrix heightB widthB a
-> Array ((heightA, heightB), (widthA, widthB)) a
forall vert horiz heightA widthA heightB widthB a.
(C vert, C horiz, C heightA, C widthA, C heightB, C widthB,
 Floating a) =>
Full vert horiz heightA widthA a
-> Matrix heightB widthB a
-> Matrix (heightA, heightB) (widthA, widthB) a
RowMajor.kronecker Full vert horiz heightA widthA a
a)
         (Full vert horiz (heightA, heightB) (widthA, widthB)
-> Array ((widthA, widthB), (heightA, heightB)) a
-> Full vert horiz (heightA, heightB) (widthA, widthB) a
forall sh1 sh0 a. sh1 -> Array sh0 a -> Array sh1 a
Array.reshape Full vert horiz (heightA, heightB) (widthA, widthB)
shc (Array ((widthA, widthB), (heightA, heightB)) a
 -> Full vert horiz (heightA, heightB) (widthA, widthB) a)
-> (Matrix widthB heightB a
    -> Array ((widthA, widthB), (heightA, heightB)) a)
-> Matrix widthB heightB a
-> Full vert horiz (heightA, heightB) (widthA, widthB) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Full horiz vert widthA heightA a
-> Matrix widthB heightB a
-> Array ((widthA, widthB), (heightA, heightB)) a
forall vert horiz heightA widthA heightB widthB a.
(C vert, C horiz, C heightA, C widthA, C heightB, C widthB,
 Floating a) =>
Full vert horiz heightA widthA a
-> Matrix heightB widthB a
-> Matrix (heightA, heightB) (widthA, widthB) a
RowMajor.kronecker (Full vert horiz heightA widthA a
-> Full horiz vert widthA heightA a
forall vert horiz height width a.
(C vert, C horiz) =>
Full vert horiz height width a -> Full horiz vert width height a
transpose Full vert horiz heightA widthA a
a)) (Either (Matrix heightB widthB a) (Matrix widthB heightB a)
 -> Full vert horiz (heightA, heightB) (widthA, widthB) a)
-> Either (Matrix heightB widthB a) (Matrix widthB heightB a)
-> Full vert horiz (heightA, heightB) (widthA, widthB) a
forall a b. (a -> b) -> a -> b
$
      Full vert horiz heightB widthB a
-> Either (Matrix heightB widthB a) (Matrix widthB heightB a)
forall vert horiz height width a.
(C vert, C horiz) =>
Full vert horiz height width a
-> Either (Array (height, width) a) (Array (width, height) a)
Matrix.revealOrder Full 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
MatrixShape.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