{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
module Numeric.LAPACK.Vector (
   Vector,
   RealOf,
   ComplexOf,
   toList,
   fromList,
   autoFromList,
   CheckedArray.append, (+++),
   CheckedArray.take, CheckedArray.drop,
   CheckedArray.takeLeft, CheckedArray.takeRight,
   swap,
   CheckedArray.singleton,
   constant,
   zero,
   one,
   unit,
   dot, inner, (-*|),
   sum,
   absSum,
   norm1,
   norm2,
   norm2Squared,
   normInf,
   normInf1,
   argAbsMaximum,
   argAbs1Maximum,
   product,
   scale, scaleReal, (.*|),
   add, sub, (|+|), (|-|),
   negate, raise,
   mac,
   mul, mulConj,
   divide, recip,
   minimum, argMinimum,
   maximum, argMaximum,
   limits, argLimits,
   CheckedArray.foldl,
   CheckedArray.foldl1,
   CheckedArray.foldMap,

   conjugate,
   fromReal,
   toComplex,
   realPart,
   imaginaryPart,
   zipComplex,
   unzipComplex,

   random, RandomDistribution(..),
   ) where

import qualified Numeric.LAPACK.Matrix.RowMajor as RowMajor
import qualified Numeric.LAPACK.Vector.Private as Vector
import qualified Numeric.LAPACK.Scalar as Scalar
import qualified Numeric.LAPACK.Private as Private
import Numeric.LAPACK.Matrix.Hermitian.Private
         (Determinant(Determinant, getDeterminant))
import Numeric.LAPACK.Matrix.Modifier (Conjugation(NonConjugated, Conjugated))
import Numeric.LAPACK.Linear.Private (withInfo)
import Numeric.LAPACK.Scalar (ComplexOf, RealOf, minusOne, absolute)
import Numeric.LAPACK.Private
         (ComplexPart(RealPart, ImaginaryPart), fill, copyConjugate, realPtr)

import qualified Numeric.LAPACK.FFI.Generic as LapackGen
import qualified Numeric.LAPACK.FFI.Complex as LapackComplex
import qualified Numeric.BLAS.FFI.Generic as BlasGen
import qualified Numeric.BLAS.FFI.Complex as BlasComplex
import qualified Numeric.BLAS.FFI.Real as BlasReal
import qualified Numeric.Netlib.Utility as Call
import qualified Numeric.Netlib.Class as Class

import Foreign.Marshal.Array (advancePtr)
import Foreign.ForeignPtr (withForeignPtr, castForeignPtr)
import Foreign.Ptr (Ptr)
import Foreign.Storable (Storable, peek, peekElemOff, pokeElemOff)
import Foreign.C.Types (CInt)

import System.IO.Unsafe (unsafePerformIO)

import Control.Monad.Trans.Cont (ContT(ContT), evalContT)
import Control.Monad.IO.Class (liftIO)
import Control.Monad.ST (runST)
import Control.Monad (fmap, return, (=<<))
import Control.Applicative (liftA3, (<$>))

import qualified Data.Array.Comfort.Storable.Mutable.Unchecked as UMutArray
import qualified Data.Array.Comfort.Storable.Mutable as MutArray
import qualified Data.Array.Comfort.Storable.Unchecked as Array
import qualified Data.Array.Comfort.Storable as CheckedArray
import qualified Data.Array.Comfort.Shape as Shape
import Data.Array.Comfort.Storable.Unchecked (Array(Array), append, (!))
import Data.Array.Comfort.Shape ((::+))

import Data.Function (id, flip, ($), (.))
import Data.Complex (Complex)
import Data.Maybe (Maybe(Nothing,Just), maybe)
import Data.Tuple.HT (mapFst, uncurry3)
import Data.Tuple (fst, snd)
import Data.Word (Word64)
import Data.Bits (shiftR, (.&.))
import Data.Ord (Ord, (>=))
import Data.Eq (Eq, (==))
import Data.Bool (Bool(False,True))

import Prelude (Int, fromIntegral, (+), (-), (*), Char, Show, Enum, error, IO)


type Vector = Array


toList :: (Shape.C sh, Storable a) => Vector sh a -> [a]
toList :: Vector sh a -> [a]
toList = Vector sh a -> [a]
forall sh a. (C sh, Storable a) => Array sh a -> [a]
Array.toList

fromList :: (Shape.C sh, Storable a) => sh -> [a] -> Vector sh a
fromList :: sh -> [a] -> Vector sh a
fromList = sh -> [a] -> Vector sh a
forall sh a. (C sh, Storable a) => sh -> [a] -> Array sh a
CheckedArray.fromList

autoFromList :: (Storable a) => [a] -> Vector (Shape.ZeroBased Int) a
autoFromList :: [a] -> Vector (ZeroBased Int) a
autoFromList = [a] -> Vector (ZeroBased Int) a
forall a. Storable a => [a] -> Array (ZeroBased Int) a
Array.vectorFromList


{- |
> constant () = singleton

However, singleton does not need 'Class.Floating' constraint.
-}
constant :: (Shape.C sh, Class.Floating a) => sh -> a -> Vector sh a
constant :: sh -> a -> Vector sh a
constant sh
sh a
a = sh -> (Int -> Ptr a -> IO ()) -> Vector sh a
forall sh a.
(C sh, Storable a) =>
sh -> (Int -> Ptr a -> IO ()) -> Array sh a
Array.unsafeCreateWithSize sh
sh ((Int -> Ptr a -> IO ()) -> Vector sh a)
-> (Int -> Ptr a -> IO ()) -> Vector sh a
forall a b. (a -> b) -> a -> b
$ a -> Int -> Ptr a -> IO ()
forall a. Floating a => a -> Int -> Ptr a -> IO ()
fill a
a

zero :: (Shape.C sh, Class.Floating a) => sh -> Vector sh a
zero :: sh -> Vector sh a
zero = (sh -> a -> Vector sh a) -> a -> sh -> Vector sh a
forall a b c. (a -> b -> c) -> b -> a -> c
flip sh -> a -> Vector sh a
forall sh a. (C sh, Floating a) => sh -> a -> Vector sh a
constant a
forall a. Floating a => a
Scalar.zero

one :: (Shape.C sh, Class.Floating a) => sh -> Vector sh a
one :: sh -> Vector sh a
one = (sh -> a -> Vector sh a) -> a -> sh -> Vector sh a
forall a b c. (a -> b -> c) -> b -> a -> c
flip sh -> a -> Vector sh a
forall sh a. (C sh, Floating a) => sh -> a -> Vector sh a
constant a
forall a. Floating a => a
Scalar.one

unit ::
   (Shape.Indexed sh, Class.Floating a) =>
   sh -> Shape.Index sh -> Vector sh a
unit :: sh -> Index sh -> Vector sh a
unit sh
sh Index sh
ix = sh -> (Int -> Ptr a -> IO ()) -> Vector sh a
forall sh a.
(C sh, Storable a) =>
sh -> (Int -> Ptr a -> IO ()) -> Array sh a
Array.unsafeCreateWithSize sh
sh ((Int -> Ptr a -> IO ()) -> Vector sh a)
-> (Int -> Ptr a -> IO ()) -> Vector sh a
forall a b. (a -> b) -> a -> b
$ \Int
n Ptr a
xPtr -> do
   a -> Int -> Ptr a -> IO ()
forall a. Floating a => a -> Int -> Ptr a -> IO ()
fill a
forall a. Floating a => a
Scalar.zero Int
n Ptr a
xPtr
   Ptr a -> Int -> a -> IO ()
forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff Ptr a
xPtr (sh -> Index sh -> Int
forall sh. Indexed sh => sh -> Index sh -> Int
Shape.offset sh
sh Index sh
ix) a
forall a. Floating a => a
Scalar.one


{- |
Precedence and associativity (right) of (List.++).
This also matches '(::+)'.
-}
infixr 5 +++

(+++) ::
   (Shape.C shx, Shape.C shy, Storable a) =>
   Vector shx a -> Vector shy a -> Vector (shx::+shy) a
+++ :: Vector shx a -> Vector shy a -> Vector (shx ::+ shy) a
(+++) = Vector shx a -> Vector shy a -> Vector (shx ::+ shy) a
forall shx shy a.
(C shx, C shy, Storable a) =>
Array shx a -> Array shy a -> Array (shx ::+ shy) a
append


swap ::
   (Shape.Indexed sh, Storable a) =>
   Shape.Index sh -> Shape.Index sh -> Vector sh a -> Vector sh a
swap :: Index sh -> Index sh -> Vector sh a -> Vector sh a
swap Index sh
i Index sh
j Vector sh a
x =
   (forall s. ST s (Vector sh a)) -> Vector sh a
forall a. (forall s. ST s a) -> a
runST (do
      Array (ST s) sh a
y <- Vector sh a -> ST s (Array (ST s) sh a)
forall (m :: * -> *) sh a.
(PrimMonad m, C sh, Storable a) =>
Array sh a -> m (Array m sh a)
MutArray.thaw Vector sh a
x
      a
xi <- Array (ST s) sh a -> Index sh -> ST s a
forall (m :: * -> *) sh a.
(PrimMonad m, Indexed sh, Storable a) =>
Array m sh a -> Index sh -> m a
MutArray.read Array (ST s) sh a
y Index sh
i
      a
xj <- Array (ST s) sh a -> Index sh -> ST s a
forall (m :: * -> *) sh a.
(PrimMonad m, Indexed sh, Storable a) =>
Array m sh a -> Index sh -> m a
MutArray.read Array (ST s) sh a
y Index sh
j
      Array (ST s) sh a -> Index sh -> a -> ST s ()
forall (m :: * -> *) sh a.
(PrimMonad m, Indexed sh, Storable a) =>
Array m sh a -> Index sh -> a -> m ()
MutArray.write Array (ST s) sh a
y Index sh
i a
xj
      Array (ST s) sh a -> Index sh -> a -> ST s ()
forall (m :: * -> *) sh a.
(PrimMonad m, Indexed sh, Storable a) =>
Array m sh a -> Index sh -> a -> m ()
MutArray.write Array (ST s) sh a
y Index sh
j a
xi
      Array (ST s) sh a -> ST s (Vector sh a)
forall (m :: * -> *) sh a.
(PrimMonad m, C sh, Storable a) =>
Array m sh a -> m (Array sh a)
UMutArray.unsafeFreeze Array (ST s) sh a
y)


infixl 7 -*|, .*|

newtype Dot f a = Dot {Dot f a -> f a -> f a -> a
runDot :: f a -> f a -> a}

{- |
> dot x y = Matrix.toScalar (singleRow x <#> singleColumn y)
-}
dot, (-*|) ::
   (Shape.C sh, Eq sh, Class.Floating a) =>
   Vector sh a -> Vector sh a -> a
-*| :: Vector sh a -> Vector sh a -> a
(-*|) = Vector sh a -> Vector sh a -> a
forall sh a.
(C sh, Eq sh, Floating a) =>
Vector sh a -> Vector sh a -> a
dot
dot :: Vector sh a -> Vector sh a -> a
dot =
   Dot (Array sh) a -> Vector sh a -> Vector sh a -> a
forall (f :: * -> *) a. Dot f a -> f a -> f a -> a
runDot (Dot (Array sh) a -> Vector sh a -> Vector sh a -> a)
-> Dot (Array sh) a -> Vector sh a -> Vector sh a -> a
forall a b. (a -> b) -> a -> b
$
   Dot (Array sh) Float
-> Dot (Array sh) Double
-> Dot (Array sh) (Complex Float)
-> Dot (Array sh) (Complex Double)
-> Dot (Array sh) a
forall a (f :: * -> *).
Floating a =>
f Float
-> f Double -> f (Complex Float) -> f (Complex Double) -> f a
Class.switchFloating
      ((Array sh Float -> Array sh Float -> Float) -> Dot (Array sh) Float
forall (f :: * -> *) a. (f a -> f a -> a) -> Dot f a
Dot Array sh Float -> Array sh Float -> Float
forall sh a.
(C sh, Eq sh, Real a) =>
Vector sh a -> Vector sh a -> a
dotReal)
      ((Array sh Double -> Array sh Double -> Double)
-> Dot (Array sh) Double
forall (f :: * -> *) a. (f a -> f a -> a) -> Dot f a
Dot Array sh Double -> Array sh Double -> Double
forall sh a.
(C sh, Eq sh, Real a) =>
Vector sh a -> Vector sh a -> a
dotReal)
      ((Array sh (Complex Float)
 -> Array sh (Complex Float) -> Complex Float)
-> Dot (Array sh) (Complex Float)
forall (f :: * -> *) a. (f a -> f a -> a) -> Dot f a
Dot ((Array sh (Complex Float)
  -> Array sh (Complex Float) -> Complex Float)
 -> Dot (Array sh) (Complex Float))
-> (Array sh (Complex Float)
    -> Array sh (Complex Float) -> Complex Float)
-> Dot (Array sh) (Complex Float)
forall a b. (a -> b) -> a -> b
$ Char
-> Array sh (Complex Float)
-> Array sh (Complex Float)
-> Complex Float
forall sh a.
(C sh, Eq sh, Real a) =>
Char -> Vector sh (Complex a) -> Vector sh (Complex a) -> Complex a
dotComplex Char
'T')
      ((Array sh (Complex Double)
 -> Array sh (Complex Double) -> Complex Double)
-> Dot (Array sh) (Complex Double)
forall (f :: * -> *) a. (f a -> f a -> a) -> Dot f a
Dot ((Array sh (Complex Double)
  -> Array sh (Complex Double) -> Complex Double)
 -> Dot (Array sh) (Complex Double))
-> (Array sh (Complex Double)
    -> Array sh (Complex Double) -> Complex Double)
-> Dot (Array sh) (Complex Double)
forall a b. (a -> b) -> a -> b
$ Char
-> Array sh (Complex Double)
-> Array sh (Complex Double)
-> Complex Double
forall sh a.
(C sh, Eq sh, Real a) =>
Char -> Vector sh (Complex a) -> Vector sh (Complex a) -> Complex a
dotComplex Char
'T')

{- |
> inner x y = dot (conjugate x) y
-}
inner ::
   (Shape.C sh, Eq sh, Class.Floating a) =>
   Vector sh a -> Vector sh a -> a
inner :: Vector sh a -> Vector sh a -> a
inner =
   Dot (Array sh) a -> Vector sh a -> Vector sh a -> a
forall (f :: * -> *) a. Dot f a -> f a -> f a -> a
runDot (Dot (Array sh) a -> Vector sh a -> Vector sh a -> a)
-> Dot (Array sh) a -> Vector sh a -> Vector sh a -> a
forall a b. (a -> b) -> a -> b
$
   Dot (Array sh) Float
-> Dot (Array sh) Double
-> Dot (Array sh) (Complex Float)
-> Dot (Array sh) (Complex Double)
-> Dot (Array sh) a
forall a (f :: * -> *).
Floating a =>
f Float
-> f Double -> f (Complex Float) -> f (Complex Double) -> f a
Class.switchFloating
      ((Array sh Float -> Array sh Float -> Float) -> Dot (Array sh) Float
forall (f :: * -> *) a. (f a -> f a -> a) -> Dot f a
Dot Array sh Float -> Array sh Float -> Float
forall sh a.
(C sh, Eq sh, Real a) =>
Vector sh a -> Vector sh a -> a
dotReal)
      ((Array sh Double -> Array sh Double -> Double)
-> Dot (Array sh) Double
forall (f :: * -> *) a. (f a -> f a -> a) -> Dot f a
Dot Array sh Double -> Array sh Double -> Double
forall sh a.
(C sh, Eq sh, Real a) =>
Vector sh a -> Vector sh a -> a
dotReal)
      ((Array sh (Complex Float)
 -> Array sh (Complex Float) -> Complex Float)
-> Dot (Array sh) (Complex Float)
forall (f :: * -> *) a. (f a -> f a -> a) -> Dot f a
Dot ((Array sh (Complex Float)
  -> Array sh (Complex Float) -> Complex Float)
 -> Dot (Array sh) (Complex Float))
-> (Array sh (Complex Float)
    -> Array sh (Complex Float) -> Complex Float)
-> Dot (Array sh) (Complex Float)
forall a b. (a -> b) -> a -> b
$ Char
-> Array sh (Complex Float)
-> Array sh (Complex Float)
-> Complex Float
forall sh a.
(C sh, Eq sh, Real a) =>
Char -> Vector sh (Complex a) -> Vector sh (Complex a) -> Complex a
dotComplex Char
'C')
      ((Array sh (Complex Double)
 -> Array sh (Complex Double) -> Complex Double)
-> Dot (Array sh) (Complex Double)
forall (f :: * -> *) a. (f a -> f a -> a) -> Dot f a
Dot ((Array sh (Complex Double)
  -> Array sh (Complex Double) -> Complex Double)
 -> Dot (Array sh) (Complex Double))
-> (Array sh (Complex Double)
    -> Array sh (Complex Double) -> Complex Double)
-> Dot (Array sh) (Complex Double)
forall a b. (a -> b) -> a -> b
$ Char
-> Array sh (Complex Double)
-> Array sh (Complex Double)
-> Complex Double
forall sh a.
(C sh, Eq sh, Real a) =>
Char -> Vector sh (Complex a) -> Vector sh (Complex a) -> Complex a
dotComplex Char
'C')

dotReal ::
   (Shape.C sh, Eq sh, Class.Real a) =>
   Vector sh a -> Vector sh a -> a
dotReal :: Vector sh a -> Vector sh a -> a
dotReal arrX :: Vector sh a
arrX@(Array sh
shX ForeignPtr a
_x) (Array sh
shY ForeignPtr a
y) = IO a -> a
forall a. IO a -> a
unsafePerformIO (IO a -> a) -> IO a -> a
forall a b. (a -> b) -> a -> b
$ do
   String -> Bool -> IO ()
Call.assert String
"dot: shapes mismatch" (sh
shX sh -> sh -> Bool
forall a. Eq a => a -> a -> Bool
== sh
shY)
   ContT a IO a -> IO a
forall (m :: * -> *) r. Monad m => ContT r m r -> m r
evalContT (ContT a IO a -> IO a) -> ContT a IO a -> IO a
forall a b. (a -> b) -> a -> b
$ do
      (Ptr CInt
nPtr, Ptr a
sxPtr, Ptr CInt
incxPtr) <- Vector sh a -> ContT a IO (Ptr CInt, Ptr a, Ptr CInt)
forall sh a r.
C sh =>
Array sh a -> ContT r IO (Ptr CInt, Ptr a, Ptr CInt)
vectorArgs Vector sh a
arrX
      Ptr a
syPtr <- ((Ptr a -> IO a) -> IO a) -> ContT a IO (Ptr a)
forall k (r :: k) (m :: k -> *) a.
((a -> m r) -> m r) -> ContT r m a
ContT (((Ptr a -> IO a) -> IO a) -> ContT a IO (Ptr a))
-> ((Ptr a -> IO a) -> IO a) -> ContT a IO (Ptr a)
forall a b. (a -> b) -> a -> b
$ ForeignPtr a -> (Ptr a -> IO a) -> IO a
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr a
y
      Ptr CInt
incyPtr <- Int -> FortranIO a (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint Int
1
      IO a -> ContT a IO a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO a -> ContT a IO a) -> IO a -> ContT a IO a
forall a b. (a -> b) -> a -> b
$ Ptr CInt -> Ptr a -> Ptr CInt -> Ptr a -> Ptr CInt -> IO a
forall a.
Real a =>
Ptr CInt -> Ptr a -> Ptr CInt -> Ptr a -> Ptr CInt -> IO a
BlasReal.dot Ptr CInt
nPtr Ptr a
sxPtr Ptr CInt
incxPtr Ptr a
syPtr Ptr CInt
incyPtr

{-
We cannot use 'cdot' because Haskell's FFI
does not support Complex numbers as return values.
-}
dotComplex ::
   (Shape.C sh, Eq sh, Class.Real a) =>
   Char -> Vector sh (Complex a) -> Vector sh (Complex a) -> Complex a
dotComplex :: Char -> Vector sh (Complex a) -> Vector sh (Complex a) -> Complex a
dotComplex Char
trans (Array sh
shX ForeignPtr (Complex a)
x) (Array sh
shY ForeignPtr (Complex a)
y) = IO (Complex a) -> Complex a
forall a. IO a -> a
unsafePerformIO (IO (Complex a) -> Complex a) -> IO (Complex a) -> Complex a
forall a b. (a -> b) -> a -> b
$ do
   String -> Bool -> IO ()
Call.assert String
"dot: shapes mismatch" (sh
shX sh -> sh -> Bool
forall a. Eq a => a -> a -> Bool
== sh
shY)
   ContT (Complex a) IO (Complex a) -> IO (Complex a)
forall (m :: * -> *) r. Monad m => ContT r m r -> m r
evalContT (ContT (Complex a) IO (Complex a) -> IO (Complex a))
-> ContT (Complex a) IO (Complex a) -> IO (Complex a)
forall a b. (a -> b) -> a -> b
$ do
      let m :: Int
m = sh -> Int
forall sh. C sh => sh -> Int
Shape.size sh
shX
      Ptr CChar
transPtr <- Char -> FortranIO (Complex a) (Ptr CChar)
forall r. Char -> FortranIO r (Ptr CChar)
Call.char Char
trans
      Ptr CInt
mPtr <- Int -> FortranIO (Complex a) (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint Int
m
      Ptr CInt
nPtr <- Int -> FortranIO (Complex a) (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint Int
1
      Ptr (Complex a)
alphaPtr <- Complex a -> FortranIO (Complex a) (Ptr (Complex a))
forall a r. Floating a => a -> FortranIO r (Ptr a)
Call.number Complex a
forall a. Floating a => a
Scalar.one
      Ptr (Complex a)
xPtr <- ((Ptr (Complex a) -> IO (Complex a)) -> IO (Complex a))
-> FortranIO (Complex a) (Ptr (Complex a))
forall k (r :: k) (m :: k -> *) a.
((a -> m r) -> m r) -> ContT r m a
ContT (((Ptr (Complex a) -> IO (Complex a)) -> IO (Complex a))
 -> FortranIO (Complex a) (Ptr (Complex a)))
-> ((Ptr (Complex a) -> IO (Complex a)) -> IO (Complex a))
-> FortranIO (Complex a) (Ptr (Complex a))
forall a b. (a -> b) -> a -> b
$ ForeignPtr (Complex a)
-> (Ptr (Complex a) -> IO (Complex a)) -> IO (Complex a)
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr (Complex a)
x
      Ptr CInt
ldxPtr <- Int -> FortranIO (Complex a) (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.leadingDim Int
m
      Ptr (Complex a)
yPtr <- ((Ptr (Complex a) -> IO (Complex a)) -> IO (Complex a))
-> FortranIO (Complex a) (Ptr (Complex a))
forall k (r :: k) (m :: k -> *) a.
((a -> m r) -> m r) -> ContT r m a
ContT (((Ptr (Complex a) -> IO (Complex a)) -> IO (Complex a))
 -> FortranIO (Complex a) (Ptr (Complex a)))
-> ((Ptr (Complex a) -> IO (Complex a)) -> IO (Complex a))
-> FortranIO (Complex a) (Ptr (Complex a))
forall a b. (a -> b) -> a -> b
$ ForeignPtr (Complex a)
-> (Ptr (Complex a) -> IO (Complex a)) -> IO (Complex a)
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr (Complex a)
y
      Ptr CInt
incyPtr <- Int -> FortranIO (Complex a) (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint Int
1
      Ptr (Complex a)
betaPtr <- Complex a -> FortranIO (Complex a) (Ptr (Complex a))
forall a r. Floating a => a -> FortranIO r (Ptr a)
Call.number Complex a
forall a. Floating a => a
Scalar.zero
      Ptr (Complex a)
zPtr <- FortranIO (Complex a) (Ptr (Complex a))
forall a r. Storable a => FortranIO r (Ptr a)
Call.alloca
      Ptr CInt
inczPtr <- Int -> FortranIO (Complex a) (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint Int
1
      IO () -> ContT (Complex a) IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ContT (Complex a) IO ())
-> IO () -> ContT (Complex a) IO ()
forall a b. (a -> b) -> a -> b
$
         Ptr CChar
-> Ptr CInt
-> Ptr CInt
-> Ptr (Complex a)
-> Ptr (Complex a)
-> Ptr CInt
-> Ptr (Complex a)
-> Ptr CInt
-> Ptr (Complex a)
-> Ptr (Complex 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 ()
Private.gemv
            Ptr CChar
transPtr Ptr CInt
mPtr Ptr CInt
nPtr Ptr (Complex a)
alphaPtr Ptr (Complex a)
xPtr Ptr CInt
ldxPtr
            Ptr (Complex a)
yPtr Ptr CInt
incyPtr Ptr (Complex a)
betaPtr Ptr (Complex a)
zPtr Ptr CInt
inczPtr
      IO (Complex a) -> ContT (Complex a) IO (Complex a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Complex a) -> ContT (Complex a) IO (Complex a))
-> IO (Complex a) -> ContT (Complex a) IO (Complex a)
forall a b. (a -> b) -> a -> b
$ Ptr (Complex a) -> IO (Complex a)
forall a. Storable a => Ptr a -> IO a
peek Ptr (Complex a)
zPtr

sum :: (Shape.C sh, Class.Floating a) => Vector sh a -> a
sum :: Vector sh a -> a
sum (Array sh
sh ForeignPtr a
x) = IO a -> a
forall a. IO a -> a
unsafePerformIO (IO a -> a) -> IO a -> a
forall a b. (a -> b) -> a -> b
$
   ForeignPtr a -> (Ptr a -> IO a) -> IO a
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr a
x ((Ptr a -> IO a) -> IO a) -> (Ptr a -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$ \Ptr a
xPtr -> Int -> Ptr a -> Int -> IO a
forall a. Floating a => Int -> Ptr a -> Int -> IO a
Private.sum (sh -> Int
forall sh. C sh => sh -> Int
Shape.size sh
sh) Ptr a
xPtr Int
1

norm1 :: (Shape.C sh, Class.Floating a) => Vector sh a -> RealOf a
norm1 :: Vector sh a -> RealOf a
norm1 Vector sh a
arr = IO (RealOf a) -> RealOf a
forall a. IO a -> a
unsafePerformIO (IO (RealOf a) -> RealOf a) -> IO (RealOf a) -> RealOf a
forall a b. (a -> b) -> a -> b
$
   ContT (RealOf a) IO (RealOf a) -> IO (RealOf a)
forall (m :: * -> *) r. Monad m => ContT r m r -> m r
evalContT (ContT (RealOf a) IO (RealOf a) -> IO (RealOf a))
-> ContT (RealOf a) IO (RealOf a) -> IO (RealOf a)
forall a b. (a -> b) -> a -> b
$ IO (RealOf a) -> ContT (RealOf a) IO (RealOf a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (RealOf a) -> ContT (RealOf a) IO (RealOf a))
-> ((Ptr CInt, Ptr a, Ptr CInt) -> IO (RealOf a))
-> (Ptr CInt, Ptr a, Ptr CInt)
-> ContT (RealOf a) IO (RealOf a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Ptr CInt -> Ptr a -> Ptr CInt -> IO (RealOf a))
-> (Ptr CInt, Ptr a, Ptr CInt) -> IO (RealOf a)
forall a b c d. (a -> b -> c -> d) -> (a, b, c) -> d
uncurry3 Ptr CInt -> Ptr a -> Ptr CInt -> IO (RealOf a)
forall a.
Floating a =>
Ptr CInt -> Ptr a -> Ptr CInt -> IO (RealOf a)
csum1 ((Ptr CInt, Ptr a, Ptr CInt) -> ContT (RealOf a) IO (RealOf a))
-> ContT (RealOf a) IO (Ptr CInt, Ptr a, Ptr CInt)
-> ContT (RealOf a) IO (RealOf a)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Vector sh a -> ContT (RealOf a) IO (Ptr CInt, Ptr a, Ptr CInt)
forall sh a r.
C sh =>
Array sh a -> ContT r IO (Ptr CInt, Ptr a, Ptr CInt)
vectorArgs Vector sh a
arr

csum1 :: Class.Floating a => Ptr CInt -> Ptr a -> Ptr CInt -> IO (RealOf a)
csum1 :: Ptr CInt -> Ptr a -> Ptr CInt -> IO (RealOf a)
csum1 =
   Norm a -> Ptr CInt -> Ptr a -> Ptr CInt -> IO (RealOf a)
forall a. Norm a -> Ptr CInt -> Ptr a -> Ptr CInt -> IO (RealOf a)
getNorm (Norm a -> Ptr CInt -> Ptr a -> Ptr CInt -> IO (RealOf a))
-> Norm a -> Ptr CInt -> Ptr a -> Ptr CInt -> IO (RealOf a)
forall a b. (a -> b) -> a -> b
$
   Norm Float
-> Norm Double
-> Norm (Complex Float)
-> Norm (Complex Double)
-> Norm a
forall a (f :: * -> *).
Floating a =>
f Float
-> f Double -> f (Complex Float) -> f (Complex Double) -> f a
Class.switchFloating
      ((Ptr CInt -> Ptr Float -> Ptr CInt -> IO (RealOf Float))
-> Norm Float
forall a.
(Ptr CInt -> Ptr a -> Ptr CInt -> IO (RealOf a)) -> Norm a
Norm Ptr CInt -> Ptr Float -> Ptr CInt -> IO (RealOf Float)
forall a. Real a => Ptr CInt -> Ptr a -> Ptr CInt -> IO a
BlasReal.asum)
      ((Ptr CInt -> Ptr Double -> Ptr CInt -> IO (RealOf Double))
-> Norm Double
forall a.
(Ptr CInt -> Ptr a -> Ptr CInt -> IO (RealOf a)) -> Norm a
Norm Ptr CInt -> Ptr Double -> Ptr CInt -> IO (RealOf Double)
forall a. Real a => Ptr CInt -> Ptr a -> Ptr CInt -> IO a
BlasReal.asum)
      ((Ptr CInt
 -> Ptr (Complex Float) -> Ptr CInt -> IO (RealOf (Complex Float)))
-> Norm (Complex Float)
forall a.
(Ptr CInt -> Ptr a -> Ptr CInt -> IO (RealOf a)) -> Norm a
Norm Ptr CInt
-> Ptr (Complex Float) -> Ptr CInt -> IO (RealOf (Complex Float))
forall a. Real a => Ptr CInt -> Ptr (Complex a) -> Ptr CInt -> IO a
LapackComplex.sum1)
      ((Ptr CInt
 -> Ptr (Complex Double)
 -> Ptr CInt
 -> IO (RealOf (Complex Double)))
-> Norm (Complex Double)
forall a.
(Ptr CInt -> Ptr a -> Ptr CInt -> IO (RealOf a)) -> Norm a
Norm Ptr CInt
-> Ptr (Complex Double) -> Ptr CInt -> IO (RealOf (Complex Double))
forall a. Real a => Ptr CInt -> Ptr (Complex a) -> Ptr CInt -> IO a
LapackComplex.sum1)


{- |
Sum of the absolute values of real numbers or components of complex numbers.
For real numbers it is equivalent to 'norm1'.
-}
absSum :: (Shape.C sh, Class.Floating a) => Vector sh a -> RealOf a
absSum :: Vector sh a -> RealOf a
absSum Vector sh a
arr = IO (RealOf a) -> RealOf a
forall a. IO a -> a
unsafePerformIO (IO (RealOf a) -> RealOf a) -> IO (RealOf a) -> RealOf a
forall a b. (a -> b) -> a -> b
$
   ContT (RealOf a) IO (RealOf a) -> IO (RealOf a)
forall (m :: * -> *) r. Monad m => ContT r m r -> m r
evalContT (ContT (RealOf a) IO (RealOf a) -> IO (RealOf a))
-> ContT (RealOf a) IO (RealOf a) -> IO (RealOf a)
forall a b. (a -> b) -> a -> b
$ IO (RealOf a) -> ContT (RealOf a) IO (RealOf a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (RealOf a) -> ContT (RealOf a) IO (RealOf a))
-> ((Ptr CInt, Ptr a, Ptr CInt) -> IO (RealOf a))
-> (Ptr CInt, Ptr a, Ptr CInt)
-> ContT (RealOf a) IO (RealOf a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Ptr CInt -> Ptr a -> Ptr CInt -> IO (RealOf a))
-> (Ptr CInt, Ptr a, Ptr CInt) -> IO (RealOf a)
forall a b c d. (a -> b -> c -> d) -> (a, b, c) -> d
uncurry3 Ptr CInt -> Ptr a -> Ptr CInt -> IO (RealOf a)
forall a.
Floating a =>
Ptr CInt -> Ptr a -> Ptr CInt -> IO (RealOf a)
asum ((Ptr CInt, Ptr a, Ptr CInt) -> ContT (RealOf a) IO (RealOf a))
-> ContT (RealOf a) IO (Ptr CInt, Ptr a, Ptr CInt)
-> ContT (RealOf a) IO (RealOf a)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Vector sh a -> ContT (RealOf a) IO (Ptr CInt, Ptr a, Ptr CInt)
forall sh a r.
C sh =>
Array sh a -> ContT r IO (Ptr CInt, Ptr a, Ptr CInt)
vectorArgs Vector sh a
arr

asum :: Class.Floating a => Ptr CInt -> Ptr a -> Ptr CInt -> IO (RealOf a)
asum :: Ptr CInt -> Ptr a -> Ptr CInt -> IO (RealOf a)
asum =
   Norm a -> Ptr CInt -> Ptr a -> Ptr CInt -> IO (RealOf a)
forall a. Norm a -> Ptr CInt -> Ptr a -> Ptr CInt -> IO (RealOf a)
getNorm (Norm a -> Ptr CInt -> Ptr a -> Ptr CInt -> IO (RealOf a))
-> Norm a -> Ptr CInt -> Ptr a -> Ptr CInt -> IO (RealOf a)
forall a b. (a -> b) -> a -> b
$
   Norm Float
-> Norm Double
-> Norm (Complex Float)
-> Norm (Complex Double)
-> Norm a
forall a (f :: * -> *).
Floating a =>
f Float
-> f Double -> f (Complex Float) -> f (Complex Double) -> f a
Class.switchFloating
      ((Ptr CInt -> Ptr Float -> Ptr CInt -> IO (RealOf Float))
-> Norm Float
forall a.
(Ptr CInt -> Ptr a -> Ptr CInt -> IO (RealOf a)) -> Norm a
Norm Ptr CInt -> Ptr Float -> Ptr CInt -> IO (RealOf Float)
forall a. Real a => Ptr CInt -> Ptr a -> Ptr CInt -> IO a
BlasReal.asum) ((Ptr CInt -> Ptr Double -> Ptr CInt -> IO (RealOf Double))
-> Norm Double
forall a.
(Ptr CInt -> Ptr a -> Ptr CInt -> IO (RealOf a)) -> Norm a
Norm Ptr CInt -> Ptr Double -> Ptr CInt -> IO (RealOf Double)
forall a. Real a => Ptr CInt -> Ptr a -> Ptr CInt -> IO a
BlasReal.asum)
      ((Ptr CInt
 -> Ptr (Complex Float) -> Ptr CInt -> IO (RealOf (Complex Float)))
-> Norm (Complex Float)
forall a.
(Ptr CInt -> Ptr a -> Ptr CInt -> IO (RealOf a)) -> Norm a
Norm Ptr CInt
-> Ptr (Complex Float) -> Ptr CInt -> IO (RealOf (Complex Float))
forall a. Real a => Ptr CInt -> Ptr (Complex a) -> Ptr CInt -> IO a
BlasComplex.casum) ((Ptr CInt
 -> Ptr (Complex Double)
 -> Ptr CInt
 -> IO (RealOf (Complex Double)))
-> Norm (Complex Double)
forall a.
(Ptr CInt -> Ptr a -> Ptr CInt -> IO (RealOf a)) -> Norm a
Norm Ptr CInt
-> Ptr (Complex Double) -> Ptr CInt -> IO (RealOf (Complex Double))
forall a. Real a => Ptr CInt -> Ptr (Complex a) -> Ptr CInt -> IO a
BlasComplex.casum)


{- |
Euclidean norm of a vector or Frobenius norm of a matrix.
-}
norm2 :: (Shape.C sh, Class.Floating a) => Vector sh a -> RealOf a
norm2 :: Vector sh a -> RealOf a
norm2 Vector sh a
arr = IO (RealOf a) -> RealOf a
forall a. IO a -> a
unsafePerformIO (IO (RealOf a) -> RealOf a) -> IO (RealOf a) -> RealOf a
forall a b. (a -> b) -> a -> b
$
   ContT (RealOf a) IO (RealOf a) -> IO (RealOf a)
forall (m :: * -> *) r. Monad m => ContT r m r -> m r
evalContT (ContT (RealOf a) IO (RealOf a) -> IO (RealOf a))
-> ContT (RealOf a) IO (RealOf a) -> IO (RealOf a)
forall a b. (a -> b) -> a -> b
$ IO (RealOf a) -> ContT (RealOf a) IO (RealOf a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (RealOf a) -> ContT (RealOf a) IO (RealOf a))
-> ((Ptr CInt, Ptr a, Ptr CInt) -> IO (RealOf a))
-> (Ptr CInt, Ptr a, Ptr CInt)
-> ContT (RealOf a) IO (RealOf a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Ptr CInt -> Ptr a -> Ptr CInt -> IO (RealOf a))
-> (Ptr CInt, Ptr a, Ptr CInt) -> IO (RealOf a)
forall a b c d. (a -> b -> c -> d) -> (a, b, c) -> d
uncurry3 Ptr CInt -> Ptr a -> Ptr CInt -> IO (RealOf a)
forall a.
Floating a =>
Ptr CInt -> Ptr a -> Ptr CInt -> IO (RealOf a)
nrm2 ((Ptr CInt, Ptr a, Ptr CInt) -> ContT (RealOf a) IO (RealOf a))
-> ContT (RealOf a) IO (Ptr CInt, Ptr a, Ptr CInt)
-> ContT (RealOf a) IO (RealOf a)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Vector sh a -> ContT (RealOf a) IO (Ptr CInt, Ptr a, Ptr CInt)
forall sh a r.
C sh =>
Array sh a -> ContT r IO (Ptr CInt, Ptr a, Ptr CInt)
vectorArgs Vector sh a
arr

nrm2 :: Class.Floating a => Ptr CInt -> Ptr a -> Ptr CInt -> IO (RealOf a)
nrm2 :: Ptr CInt -> Ptr a -> Ptr CInt -> IO (RealOf a)
nrm2 =
   Norm a -> Ptr CInt -> Ptr a -> Ptr CInt -> IO (RealOf a)
forall a. Norm a -> Ptr CInt -> Ptr a -> Ptr CInt -> IO (RealOf a)
getNorm (Norm a -> Ptr CInt -> Ptr a -> Ptr CInt -> IO (RealOf a))
-> Norm a -> Ptr CInt -> Ptr a -> Ptr CInt -> IO (RealOf a)
forall a b. (a -> b) -> a -> b
$
   Norm Float
-> Norm Double
-> Norm (Complex Float)
-> Norm (Complex Double)
-> Norm a
forall a (f :: * -> *).
Floating a =>
f Float
-> f Double -> f (Complex Float) -> f (Complex Double) -> f a
Class.switchFloating
      ((Ptr CInt -> Ptr Float -> Ptr CInt -> IO (RealOf Float))
-> Norm Float
forall a.
(Ptr CInt -> Ptr a -> Ptr CInt -> IO (RealOf a)) -> Norm a
Norm Ptr CInt -> Ptr Float -> Ptr CInt -> IO (RealOf Float)
forall a. Real a => Ptr CInt -> Ptr a -> Ptr CInt -> IO a
BlasReal.nrm2) ((Ptr CInt -> Ptr Double -> Ptr CInt -> IO (RealOf Double))
-> Norm Double
forall a.
(Ptr CInt -> Ptr a -> Ptr CInt -> IO (RealOf a)) -> Norm a
Norm Ptr CInt -> Ptr Double -> Ptr CInt -> IO (RealOf Double)
forall a. Real a => Ptr CInt -> Ptr a -> Ptr CInt -> IO a
BlasReal.nrm2)
      ((Ptr CInt
 -> Ptr (Complex Float) -> Ptr CInt -> IO (RealOf (Complex Float)))
-> Norm (Complex Float)
forall a.
(Ptr CInt -> Ptr a -> Ptr CInt -> IO (RealOf a)) -> Norm a
Norm Ptr CInt
-> Ptr (Complex Float) -> Ptr CInt -> IO (RealOf (Complex Float))
forall a. Real a => Ptr CInt -> Ptr (Complex a) -> Ptr CInt -> IO a
BlasComplex.cnrm2) ((Ptr CInt
 -> Ptr (Complex Double)
 -> Ptr CInt
 -> IO (RealOf (Complex Double)))
-> Norm (Complex Double)
forall a.
(Ptr CInt -> Ptr a -> Ptr CInt -> IO (RealOf a)) -> Norm a
Norm Ptr CInt
-> Ptr (Complex Double) -> Ptr CInt -> IO (RealOf (Complex Double))
forall a. Real a => Ptr CInt -> Ptr (Complex a) -> Ptr CInt -> IO a
BlasComplex.cnrm2)

newtype Norm a =
   Norm {Norm a -> Ptr CInt -> Ptr a -> Ptr CInt -> IO (RealOf a)
getNorm :: Ptr CInt -> Ptr a -> Ptr CInt -> IO (RealOf a)}


norm2Squared :: (Shape.C sh, Class.Floating a) => Vector sh a -> RealOf a
norm2Squared :: Vector sh a -> RealOf a
norm2Squared =
   Determinant (Array sh) a -> Vector sh a -> RealOf a
forall (f :: * -> *) a. Determinant f a -> f a -> RealOf a
getDeterminant (Determinant (Array sh) a -> Vector sh a -> RealOf a)
-> Determinant (Array sh) a -> Vector sh a -> RealOf a
forall a b. (a -> b) -> a -> b
$
   Determinant (Array sh) Float
-> Determinant (Array sh) Double
-> Determinant (Array sh) (Complex Float)
-> Determinant (Array sh) (Complex Double)
-> Determinant (Array sh) a
forall a (f :: * -> *).
Floating a =>
f Float
-> f Double -> f (Complex Float) -> f (Complex Double) -> f a
Class.switchFloating
      ((Array sh Float -> RealOf Float) -> Determinant (Array sh) Float
forall (f :: * -> *) a. (f a -> RealOf a) -> Determinant f a
Determinant Array sh Float -> RealOf Float
forall sh a. (C sh, Real a) => Vector sh a -> a
norm2SquaredReal)
      ((Array sh Double -> RealOf Double) -> Determinant (Array sh) Double
forall (f :: * -> *) a. (f a -> RealOf a) -> Determinant f a
Determinant Array sh Double -> RealOf Double
forall sh a. (C sh, Real a) => Vector sh a -> a
norm2SquaredReal)
      ((Array sh (Complex Float) -> RealOf (Complex Float))
-> Determinant (Array sh) (Complex Float)
forall (f :: * -> *) a. (f a -> RealOf a) -> Determinant f a
Determinant ((Array sh (Complex Float) -> RealOf (Complex Float))
 -> Determinant (Array sh) (Complex Float))
-> (Array sh (Complex Float) -> RealOf (Complex Float))
-> Determinant (Array sh) (Complex Float)
forall a b. (a -> b) -> a -> b
$ Vector (sh, Enumeration ComplexPart) Float -> Float
forall sh a. (C sh, Real a) => Vector sh a -> a
norm2SquaredReal (Vector (sh, Enumeration ComplexPart) Float -> Float)
-> (Array sh (Complex Float)
    -> Vector (sh, Enumeration ComplexPart) Float)
-> Array sh (Complex Float)
-> Float
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array sh (Complex Float)
-> Vector (sh, Enumeration ComplexPart) Float
forall a sh.
Real a =>
Vector sh (Complex a) -> Vector (sh, Enumeration ComplexPart) a
decomplex)
      ((Array sh (Complex Double) -> RealOf (Complex Double))
-> Determinant (Array sh) (Complex Double)
forall (f :: * -> *) a. (f a -> RealOf a) -> Determinant f a
Determinant ((Array sh (Complex Double) -> RealOf (Complex Double))
 -> Determinant (Array sh) (Complex Double))
-> (Array sh (Complex Double) -> RealOf (Complex Double))
-> Determinant (Array sh) (Complex Double)
forall a b. (a -> b) -> a -> b
$ Vector (sh, Enumeration ComplexPart) Double -> Double
forall sh a. (C sh, Real a) => Vector sh a -> a
norm2SquaredReal (Vector (sh, Enumeration ComplexPart) Double -> Double)
-> (Array sh (Complex Double)
    -> Vector (sh, Enumeration ComplexPart) Double)
-> Array sh (Complex Double)
-> Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array sh (Complex Double)
-> Vector (sh, Enumeration ComplexPart) Double
forall a sh.
Real a =>
Vector sh (Complex a) -> Vector (sh, Enumeration ComplexPart) a
decomplex)

norm2SquaredReal :: (Shape.C sh, Class.Real a) => Vector sh a -> a
norm2SquaredReal :: Vector sh a -> a
norm2SquaredReal Vector sh a
arr =
   IO a -> a
forall a. IO a -> a
unsafePerformIO (IO a -> a) -> IO a -> a
forall a b. (a -> b) -> a -> b
$ ContT a IO a -> IO a
forall (m :: * -> *) r. Monad m => ContT r m r -> m r
evalContT (ContT a IO a -> IO a) -> ContT a IO a -> IO a
forall a b. (a -> b) -> a -> b
$ do
      (Ptr CInt
nPtr, Ptr a
sxPtr, Ptr CInt
incxPtr) <- Vector sh a -> ContT a IO (Ptr CInt, Ptr a, Ptr CInt)
forall sh a r.
C sh =>
Array sh a -> ContT r IO (Ptr CInt, Ptr a, Ptr CInt)
vectorArgs Vector sh a
arr
      IO a -> ContT a IO a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO a -> ContT a IO a) -> IO a -> ContT a IO a
forall a b. (a -> b) -> a -> b
$ Ptr CInt -> Ptr a -> Ptr CInt -> Ptr a -> Ptr CInt -> IO a
forall a.
Real a =>
Ptr CInt -> Ptr a -> Ptr CInt -> Ptr a -> Ptr CInt -> IO a
BlasReal.dot Ptr CInt
nPtr Ptr a
sxPtr Ptr CInt
incxPtr Ptr a
sxPtr Ptr CInt
incxPtr


normInf :: (Shape.C sh, Class.Floating a) => Vector sh a -> RealOf a
normInf :: Vector sh a -> RealOf a
normInf Vector sh a
arr = IO (RealOf a) -> RealOf a
forall a. IO a -> a
unsafePerformIO (IO (RealOf a) -> RealOf a) -> IO (RealOf a) -> RealOf a
forall a b. (a -> b) -> a -> b
$
   ContT (RealOf a) IO (RealOf a) -> IO (RealOf a)
forall (m :: * -> *) r. Monad m => ContT r m r -> m r
evalContT (ContT (RealOf a) IO (RealOf a) -> IO (RealOf a))
-> ContT (RealOf a) IO (RealOf a) -> IO (RealOf a)
forall a b. (a -> b) -> a -> b
$ do
      (Ptr CInt
nPtr, Ptr a
sxPtr, Ptr CInt
incxPtr) <- Vector sh a -> ContT (RealOf a) IO (Ptr CInt, Ptr a, Ptr CInt)
forall sh a r.
C sh =>
Array sh a -> ContT r IO (Ptr CInt, Ptr a, Ptr CInt)
vectorArgs Vector sh a
arr
      IO (RealOf a) -> ContT (RealOf a) IO (RealOf a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (RealOf a) -> ContT (RealOf a) IO (RealOf a))
-> IO (RealOf a) -> ContT (RealOf a) IO (RealOf a)
forall a b. (a -> b) -> a -> b
$
         (Maybe (Int, a) -> RealOf a)
-> IO (Maybe (Int, a)) -> IO (RealOf a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a -> RealOf a
forall a. Floating a => a -> RealOf a
absolute (a -> RealOf a)
-> (Maybe (Int, a) -> a) -> Maybe (Int, a) -> RealOf a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ((Int, a) -> a) -> Maybe (Int, a) -> a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe a
forall a. Floating a => a
Scalar.zero (Int, a) -> a
forall a b. (a, b) -> b
snd) (IO (Maybe (Int, a)) -> IO (RealOf a))
-> IO (Maybe (Int, a)) -> IO (RealOf a)
forall a b. (a -> b) -> a -> b
$
         Ptr a -> CInt -> IO (Maybe (Int, a))
forall a. Storable a => Ptr a -> CInt -> IO (Maybe (Int, a))
peekElemOff1 Ptr a
sxPtr (CInt -> IO (Maybe (Int, a))) -> IO CInt -> IO (Maybe (Int, a))
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Ptr CInt -> Ptr a -> Ptr CInt -> IO CInt
forall a. Floating a => Ptr CInt -> Ptr a -> Ptr CInt -> IO CInt
Vector.absMax Ptr CInt
nPtr Ptr a
sxPtr Ptr CInt
incxPtr

{- |
Computes (almost) the infinity norm of the vector.
For complex numbers every element is replaced
by the sum of the absolute component values first.
-}
normInf1 :: (Shape.C sh, Class.Floating a) => Vector sh a -> RealOf a
normInf1 :: Vector sh a -> RealOf a
normInf1 Vector sh a
arr = IO (RealOf a) -> RealOf a
forall a. IO a -> a
unsafePerformIO (IO (RealOf a) -> RealOf a) -> IO (RealOf a) -> RealOf a
forall a b. (a -> b) -> a -> b
$
   ContT (RealOf a) IO (RealOf a) -> IO (RealOf a)
forall (m :: * -> *) r. Monad m => ContT r m r -> m r
evalContT (ContT (RealOf a) IO (RealOf a) -> IO (RealOf a))
-> ContT (RealOf a) IO (RealOf a) -> IO (RealOf a)
forall a b. (a -> b) -> a -> b
$ do
      (Ptr CInt
nPtr, Ptr a
sxPtr, Ptr CInt
incxPtr) <- Vector sh a -> ContT (RealOf a) IO (Ptr CInt, Ptr a, Ptr CInt)
forall sh a r.
C sh =>
Array sh a -> ContT r IO (Ptr CInt, Ptr a, Ptr CInt)
vectorArgs Vector sh a
arr
      IO (RealOf a) -> ContT (RealOf a) IO (RealOf a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (RealOf a) -> ContT (RealOf a) IO (RealOf a))
-> IO (RealOf a) -> ContT (RealOf a) IO (RealOf a)
forall a b. (a -> b) -> a -> b
$
         (Maybe (Int, a) -> RealOf a)
-> IO (Maybe (Int, a)) -> IO (RealOf a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a -> RealOf a
forall a. Floating a => a -> RealOf a
Scalar.norm1 (a -> RealOf a)
-> (Maybe (Int, a) -> a) -> Maybe (Int, a) -> RealOf a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ((Int, a) -> a) -> Maybe (Int, a) -> a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe a
forall a. Floating a => a
Scalar.zero (Int, a) -> a
forall a b. (a, b) -> b
snd) (IO (Maybe (Int, a)) -> IO (RealOf a))
-> IO (Maybe (Int, a)) -> IO (RealOf a)
forall a b. (a -> b) -> a -> b
$
         Ptr a -> CInt -> IO (Maybe (Int, a))
forall a. Storable a => Ptr a -> CInt -> IO (Maybe (Int, a))
peekElemOff1 Ptr a
sxPtr (CInt -> IO (Maybe (Int, a))) -> IO CInt -> IO (Maybe (Int, a))
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Ptr CInt -> Ptr a -> Ptr CInt -> IO CInt
forall a. Floating a => Ptr CInt -> Ptr a -> Ptr CInt -> IO CInt
BlasGen.iamax Ptr CInt
nPtr Ptr a
sxPtr Ptr CInt
incxPtr


{- |
Returns the index and value of the element with the maximal absolute value.
Caution: It actually returns the value of the element, not its absolute value!
-}
argAbsMaximum ::
   (Shape.InvIndexed sh, Class.Floating a) =>
   Vector sh a -> (Shape.Index sh, a)
argAbsMaximum :: Vector sh a -> (Index sh, a)
argAbsMaximum Vector sh a
arr = IO (Index sh, a) -> (Index sh, a)
forall a. IO a -> a
unsafePerformIO (IO (Index sh, a) -> (Index sh, a))
-> IO (Index sh, a) -> (Index sh, a)
forall a b. (a -> b) -> a -> b
$
   ContT (Index sh, a) IO (Index sh, a) -> IO (Index sh, a)
forall (m :: * -> *) r. Monad m => ContT r m r -> m r
evalContT (ContT (Index sh, a) IO (Index sh, a) -> IO (Index sh, a))
-> ContT (Index sh, a) IO (Index sh, a) -> IO (Index sh, a)
forall a b. (a -> b) -> a -> b
$ do
      (Ptr CInt
nPtr, Ptr a
sxPtr, Ptr CInt
incxPtr) <- Vector sh a -> ContT (Index sh, a) IO (Ptr CInt, Ptr a, Ptr CInt)
forall sh a r.
C sh =>
Array sh a -> ContT r IO (Ptr CInt, Ptr a, Ptr CInt)
vectorArgs Vector sh a
arr
      IO (Index sh, a) -> ContT (Index sh, a) IO (Index sh, a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Index sh, a) -> ContT (Index sh, a) IO (Index sh, a))
-> IO (Index sh, a) -> ContT (Index sh, a) IO (Index sh, a)
forall a b. (a -> b) -> a -> b
$
         (Maybe (Int, a) -> (Index sh, a))
-> IO (Maybe (Int, a)) -> IO (Index sh, a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap
            ((Index sh, a)
-> ((Int, a) -> (Index sh, a)) -> Maybe (Int, a) -> (Index sh, a)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
               (String -> (Index sh, a)
forall a. HasCallStack => String -> a
error String
"Vector.argAbsMaximum: empty vector")
               ((Int -> Index sh) -> (Int, a) -> (Index sh, a)
forall a c b. (a -> c) -> (a, b) -> (c, b)
mapFst (sh -> Int -> Index sh
forall sh. InvIndexed sh => sh -> Int -> Index sh
Shape.uncheckedIndexFromOffset (sh -> Int -> Index sh) -> sh -> Int -> Index sh
forall a b. (a -> b) -> a -> b
$ Vector sh a -> sh
forall sh a. Array sh a -> sh
Array.shape Vector sh a
arr))) (IO (Maybe (Int, a)) -> IO (Index sh, a))
-> IO (Maybe (Int, a)) -> IO (Index sh, a)
forall a b. (a -> b) -> a -> b
$
         Ptr a -> CInt -> IO (Maybe (Int, a))
forall a. Storable a => Ptr a -> CInt -> IO (Maybe (Int, a))
peekElemOff1 Ptr a
sxPtr (CInt -> IO (Maybe (Int, a))) -> IO CInt -> IO (Maybe (Int, a))
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Ptr CInt -> Ptr a -> Ptr CInt -> IO CInt
forall a. Floating a => Ptr CInt -> Ptr a -> Ptr CInt -> IO CInt
Vector.absMax Ptr CInt
nPtr Ptr a
sxPtr Ptr CInt
incxPtr


{- |
Returns the index and value of the element with the maximal absolute value.
The function does not strictly compare the absolute value of a complex number
but the sum of the absolute complex components.
Caution: It actually returns the value of the element, not its absolute value!
-}
argAbs1Maximum ::
   (Shape.InvIndexed sh, Class.Floating a) =>
   Vector sh a -> (Shape.Index sh, a)
argAbs1Maximum :: Vector sh a -> (Index sh, a)
argAbs1Maximum Vector sh a
arr = IO (Index sh, a) -> (Index sh, a)
forall a. IO a -> a
unsafePerformIO (IO (Index sh, a) -> (Index sh, a))
-> IO (Index sh, a) -> (Index sh, a)
forall a b. (a -> b) -> a -> b
$
   ContT (Index sh, a) IO (Index sh, a) -> IO (Index sh, a)
forall (m :: * -> *) r. Monad m => ContT r m r -> m r
evalContT (ContT (Index sh, a) IO (Index sh, a) -> IO (Index sh, a))
-> ContT (Index sh, a) IO (Index sh, a) -> IO (Index sh, a)
forall a b. (a -> b) -> a -> b
$ do
      (Ptr CInt
nPtr, Ptr a
sxPtr, Ptr CInt
incxPtr) <- Vector sh a -> ContT (Index sh, a) IO (Ptr CInt, Ptr a, Ptr CInt)
forall sh a r.
C sh =>
Array sh a -> ContT r IO (Ptr CInt, Ptr a, Ptr CInt)
vectorArgs Vector sh a
arr
      IO (Index sh, a) -> ContT (Index sh, a) IO (Index sh, a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Index sh, a) -> ContT (Index sh, a) IO (Index sh, a))
-> IO (Index sh, a) -> ContT (Index sh, a) IO (Index sh, a)
forall a b. (a -> b) -> a -> b
$
         (Maybe (Int, a) -> (Index sh, a))
-> IO (Maybe (Int, a)) -> IO (Index sh, a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap
            ((Index sh, a)
-> ((Int, a) -> (Index sh, a)) -> Maybe (Int, a) -> (Index sh, a)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
               (String -> (Index sh, a)
forall a. HasCallStack => String -> a
error String
"Vector.argAbs1Maximum: empty vector")
               ((Int -> Index sh) -> (Int, a) -> (Index sh, a)
forall a c b. (a -> c) -> (a, b) -> (c, b)
mapFst (sh -> Int -> Index sh
forall sh. InvIndexed sh => sh -> Int -> Index sh
Shape.uncheckedIndexFromOffset (sh -> Int -> Index sh) -> sh -> Int -> Index sh
forall a b. (a -> b) -> a -> b
$ Vector sh a -> sh
forall sh a. Array sh a -> sh
Array.shape Vector sh a
arr))) (IO (Maybe (Int, a)) -> IO (Index sh, a))
-> IO (Maybe (Int, a)) -> IO (Index sh, a)
forall a b. (a -> b) -> a -> b
$
         Ptr a -> CInt -> IO (Maybe (Int, a))
forall a. Storable a => Ptr a -> CInt -> IO (Maybe (Int, a))
peekElemOff1 Ptr a
sxPtr (CInt -> IO (Maybe (Int, a))) -> IO CInt -> IO (Maybe (Int, a))
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Ptr CInt -> Ptr a -> Ptr CInt -> IO CInt
forall a. Floating a => Ptr CInt -> Ptr a -> Ptr CInt -> IO CInt
BlasGen.iamax Ptr CInt
nPtr Ptr a
sxPtr Ptr CInt
incxPtr

vectorArgs ::
   (Shape.C sh) => Array sh a -> ContT r IO (Ptr CInt, Ptr a, Ptr CInt)
vectorArgs :: Array sh a -> ContT r IO (Ptr CInt, Ptr a, Ptr CInt)
vectorArgs (Array sh
sh ForeignPtr a
x) =
   (Ptr CInt -> Ptr a -> Ptr CInt -> (Ptr CInt, Ptr a, Ptr CInt))
-> ContT r IO (Ptr CInt)
-> ContT r IO (Ptr a)
-> ContT r IO (Ptr CInt)
-> ContT r IO (Ptr CInt, Ptr a, Ptr CInt)
forall (f :: * -> *) a b c d.
Applicative f =>
(a -> b -> c -> d) -> f a -> f b -> f c -> f d
liftA3 (,,)
      (Int -> ContT r IO (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint (Int -> ContT r IO (Ptr CInt)) -> Int -> ContT r IO (Ptr CInt)
forall a b. (a -> b) -> a -> b
$ sh -> Int
forall sh. C sh => sh -> Int
Shape.size sh
sh)
      (((Ptr a -> IO r) -> IO r) -> ContT r IO (Ptr a)
forall k (r :: k) (m :: k -> *) a.
((a -> m r) -> m r) -> ContT r m a
ContT (((Ptr a -> IO r) -> IO r) -> ContT r IO (Ptr a))
-> ((Ptr a -> IO r) -> IO r) -> ContT r IO (Ptr a)
forall a b. (a -> b) -> a -> b
$ ForeignPtr a -> (Ptr a -> IO r) -> IO r
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr a
x)
      (Int -> ContT r IO (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint Int
1)

peekElemOff1 :: (Storable a) => Ptr a -> CInt -> IO (Maybe (Int, a))
peekElemOff1 :: Ptr a -> CInt -> IO (Maybe (Int, a))
peekElemOff1 Ptr a
ptr CInt
k1 =
   let k1i :: Int
k1i = CInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CInt
k1
       ki :: Int
ki = Int
k1iInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1
   in if Int
k1i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
         then Maybe (Int, a) -> IO (Maybe (Int, a))
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (Int, a)
forall a. Maybe a
Nothing
         else (Int, a) -> Maybe (Int, a)
forall a. a -> Maybe a
Just ((Int, a) -> Maybe (Int, a))
-> (a -> (Int, a)) -> a -> Maybe (Int, a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (,) Int
ki (a -> Maybe (Int, a)) -> IO a -> IO (Maybe (Int, a))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr a -> Int -> IO a
forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Ptr a
ptr Int
ki


product :: (Shape.C sh, Class.Floating a) => Vector sh a -> a
product :: Vector sh a -> a
product (Array sh
sh ForeignPtr a
x) = IO a -> a
forall a. IO a -> a
unsafePerformIO (IO a -> a) -> IO a -> a
forall a b. (a -> b) -> a -> b
$
   ForeignPtr a -> (Ptr a -> IO a) -> IO a
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr a
x ((Ptr a -> IO a) -> IO a) -> (Ptr a -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$ \Ptr a
xPtr -> Int -> Ptr a -> Int -> IO a
forall a. Floating a => Int -> Ptr a -> Int -> IO a
Private.product (sh -> Int
forall sh. C sh => sh -> Int
Shape.size sh
sh) Ptr a
xPtr Int
1


{- |
For restrictions see 'limits'.
-}
minimum, maximum :: (Shape.C sh, Class.Real a) => Vector sh a -> a
minimum :: Vector sh a -> a
minimum = (a, a) -> a
forall a b. (a, b) -> a
fst ((a, a) -> a) -> (Vector sh a -> (a, a)) -> Vector sh a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector sh a -> (a, a)
forall sh a. (C sh, Real a) => Vector sh a -> (a, a)
limits
maximum :: Vector sh a -> a
maximum = (a, a) -> a
forall a b. (a, b) -> b
snd ((a, a) -> a) -> (Vector sh a -> (a, a)) -> Vector sh a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector sh a -> (a, a)
forall sh a. (C sh, Real a) => Vector sh a -> (a, a)
limits

{- |
For restrictions see 'limits'.
-}
argMinimum, argMaximum ::
   (Shape.InvIndexed sh, Shape.Index sh ~ ix, Class.Real a) =>
   Vector sh a -> (ix,a)
argMinimum :: Vector sh a -> (ix, a)
argMinimum = ((ix, a), (ix, a)) -> (ix, a)
forall a b. (a, b) -> a
fst (((ix, a), (ix, a)) -> (ix, a))
-> (Vector sh a -> ((ix, a), (ix, a))) -> Vector sh a -> (ix, a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector sh a -> ((ix, a), (ix, a))
forall sh ix a.
(InvIndexed sh, Index sh ~ ix, Real a) =>
Vector sh a -> ((ix, a), (ix, a))
argLimits
argMaximum :: Vector sh a -> (ix, a)
argMaximum = ((ix, a), (ix, a)) -> (ix, a)
forall a b. (a, b) -> b
snd (((ix, a), (ix, a)) -> (ix, a))
-> (Vector sh a -> ((ix, a), (ix, a))) -> Vector sh a -> (ix, a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector sh a -> ((ix, a), (ix, a))
forall sh ix a.
(InvIndexed sh, Index sh ~ ix, Real a) =>
Vector sh a -> ((ix, a), (ix, a))
argLimits

{- |
It should hold @limits x = Array.limits x@.
The function is based on fast BLAS functions.
It should be faster than @Array.minimum@ and @Array.maximum@
although it is certainly not as fast as possible.
It is less precise if minimum and maximum differ considerably in magnitude
and there are several minimum or maximum candidates of similar value.
E.g. you cannot rely on the property
that @raise (- minimum x) x@ has only non-negative elements.
-}
limits :: (Shape.C sh, Class.Real a) => Vector sh a -> (a,a)
limits :: Vector sh a -> (a, a)
limits Vector sh a
xs0 =
   let xs :: Array (Deferred sh) a
xs = (sh -> Deferred sh) -> Vector sh a -> Array (Deferred sh) a
forall sh0 sh1 a. (sh0 -> sh1) -> Array sh0 a -> Array sh1 a
Array.mapShape sh -> Deferred sh
forall sh. sh -> Deferred sh
Shape.Deferred Vector sh a
xs0
       x0 :: a
x0 = (DeferredIndex sh, a) -> a
forall a b. (a, b) -> b
snd ((DeferredIndex sh, a) -> a) -> (DeferredIndex sh, a) -> a
forall a b. (a -> b) -> a -> b
$ Array (Deferred sh) a -> (Index (Deferred sh), a)
forall sh a.
(InvIndexed sh, Floating a) =>
Vector sh a -> (Index sh, a)
argAbsMaximum Array (Deferred sh) a
xs
       x1 :: a
x1 = Array (Deferred sh) a
xs Array (Deferred sh) a -> Index (Deferred sh) -> a
forall sh a.
(Indexed sh, Storable a) =>
Array sh a -> Index sh -> a
! (DeferredIndex sh, a) -> DeferredIndex sh
forall a b. (a, b) -> a
fst (Array (Deferred sh) a -> (Index (Deferred sh), a)
forall sh a.
(InvIndexed sh, Floating a) =>
Vector sh a -> (Index sh, a)
argAbsMaximum (a -> Array (Deferred sh) a -> Array (Deferred sh) a
forall sh a. (C sh, Floating a) => a -> Array sh a -> Array sh a
raise (-a
x0) Array (Deferred sh) a
xs))
   in if a
x0a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>=a
0 then (a
x1,a
x0) else (a
x0,a
x1)

argLimits ::
   (Shape.InvIndexed sh, Shape.Index sh ~ ix, Class.Real a) =>
   Vector sh a -> ((ix,a),(ix,a))
argLimits :: Vector sh a -> ((ix, a), (ix, a))
argLimits Vector sh a
xs =
   let p0 :: (ix, a)
p0@(ix
_i0,a
x0) = Vector sh a -> (Index sh, a)
forall sh a.
(InvIndexed sh, Floating a) =>
Vector sh a -> (Index sh, a)
argAbsMaximum Vector sh a
xs
       p1 :: (ix, a)
p1 = (ix
i1,Vector sh a
xsVector sh a -> Index sh -> a
forall sh a.
(Indexed sh, Storable a) =>
Array sh a -> Index sh -> a
!ix
Index sh
i1); i1 :: ix
i1 = (ix, a) -> ix
forall a b. (a, b) -> a
fst ((ix, a) -> ix) -> (ix, a) -> ix
forall a b. (a -> b) -> a -> b
$ Vector sh a -> (Index sh, a)
forall sh a.
(InvIndexed sh, Floating a) =>
Vector sh a -> (Index sh, a)
argAbsMaximum (Vector sh a -> (Index sh, a)) -> Vector sh a -> (Index sh, a)
forall a b. (a -> b) -> a -> b
$ a -> Vector sh a -> Vector sh a
forall sh a. (C sh, Floating a) => a -> Array sh a -> Array sh a
raise (-a
x0) Vector sh a
xs
   in if a
x0a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>=a
0 then ((ix, a)
p1,(ix, a)
p0) else ((ix, a)
p0,(ix, a)
p1)


scale, _scale, (.*|) ::
   (Shape.C sh, Class.Floating a) =>
   a -> Vector sh a -> Vector sh a
.*| :: a -> Vector sh a -> Vector sh a
(.*|) = a -> Vector sh a -> Vector sh a
forall sh a. (C sh, Floating a) => a -> Array sh a -> Array sh a
scale

scale :: a -> Vector sh a -> Vector sh a
scale a
alpha (Array sh
sh ForeignPtr a
x) = sh -> (Int -> Ptr a -> IO ()) -> Vector sh a
forall sh a.
(C sh, Storable a) =>
sh -> (Int -> Ptr a -> IO ()) -> Array sh a
Array.unsafeCreateWithSize sh
sh ((Int -> Ptr a -> IO ()) -> Vector sh a)
-> (Int -> Ptr a -> IO ()) -> Vector sh a
forall a b. (a -> b) -> a -> b
$ \Int
n Ptr a
syPtr -> do
   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 a
alphaPtr <- a -> FortranIO () (Ptr a)
forall a r. Floating a => a -> FortranIO r (Ptr a)
Call.number a
alpha
      Ptr CInt
nPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint Int
n
      Ptr a
sxPtr <- ((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
x
      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
      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 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.copy Ptr CInt
nPtr Ptr a
sxPtr Ptr CInt
incxPtr Ptr a
syPtr Ptr CInt
incyPtr
      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 CInt -> Ptr a -> Ptr a -> Ptr CInt -> IO ()
forall a.
Floating a =>
Ptr CInt -> Ptr a -> Ptr a -> Ptr CInt -> IO ()
BlasGen.scal Ptr CInt
nPtr Ptr a
alphaPtr Ptr a
syPtr Ptr CInt
incyPtr

_scale :: a -> Vector sh a -> Vector sh a
_scale a
a (Array sh
sh ForeignPtr a
b) = sh -> (Int -> Ptr a -> IO ()) -> Vector sh a
forall sh a.
(C sh, Storable a) =>
sh -> (Int -> Ptr a -> IO ()) -> Array sh a
Array.unsafeCreateWithSize sh
sh ((Int -> Ptr a -> IO ()) -> Vector sh a)
-> (Int -> Ptr a -> IO ()) -> Vector sh a
forall a b. (a -> b) -> a -> b
$ \Int
n Ptr a
cPtr -> do
   let m :: Int
m = Int
1
   let k :: Int
k = Int
1
   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
transaPtr <- Char -> FortranIO () (Ptr CChar)
forall r. Char -> FortranIO r (Ptr CChar)
Call.char Char
'N'
      Ptr CChar
transbPtr <- Char -> FortranIO () (Ptr CChar)
forall r. Char -> FortranIO r (Ptr CChar)
Call.char Char
'N'
      Ptr CInt
mPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint Int
m
      Ptr CInt
kPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint Int
k
      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
Scalar.one
      Ptr a
aPtr <- a -> FortranIO () (Ptr a)
forall a r. Floating a => a -> FortranIO r (Ptr a)
Call.number a
a
      Ptr CInt
ldaPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.leadingDim Int
m
      Ptr a
bPtr <- ((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
b
      Ptr CInt
ldbPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.leadingDim Int
k
      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
Scalar.zero
      Ptr CInt
ldcPtr <- 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
$
         Ptr CChar
-> Ptr CChar
-> Ptr CInt
-> 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 CChar
-> Ptr CInt
-> Ptr CInt
-> Ptr CInt
-> Ptr a
-> Ptr a
-> Ptr CInt
-> Ptr a
-> Ptr CInt
-> Ptr a
-> Ptr a
-> Ptr CInt
-> IO ()
BlasGen.gemm
            Ptr CChar
transaPtr Ptr CChar
transbPtr Ptr CInt
mPtr Ptr CInt
nPtr Ptr CInt
kPtr Ptr a
alphaPtr
            Ptr a
aPtr Ptr CInt
ldaPtr Ptr a
bPtr Ptr CInt
ldbPtr Ptr a
betaPtr Ptr a
cPtr Ptr CInt
ldcPtr


scaleReal ::
   (Shape.C sh, Class.Floating a) =>
   RealOf a -> Vector sh a -> Vector sh a
scaleReal :: RealOf a -> Vector sh a -> Vector sh a
scaleReal =
   ScaleReal (Array sh) a -> RealOf a -> Vector sh a -> Vector sh a
forall (f :: * -> *) a. ScaleReal f a -> RealOf a -> f a -> f a
getScaleReal (ScaleReal (Array sh) a -> RealOf a -> Vector sh a -> Vector sh a)
-> ScaleReal (Array sh) a -> RealOf a -> Vector sh a -> Vector sh a
forall a b. (a -> b) -> a -> b
$
   ScaleReal (Array sh) Float
-> ScaleReal (Array sh) Double
-> ScaleReal (Array sh) (Complex Float)
-> ScaleReal (Array sh) (Complex Double)
-> ScaleReal (Array sh) a
forall a (f :: * -> *).
Floating a =>
f Float
-> f Double -> f (Complex Float) -> f (Complex Double) -> f a
Class.switchFloating
      ((RealOf Float -> Array sh Float -> Array sh Float)
-> ScaleReal (Array sh) Float
forall (f :: * -> *) a. (RealOf a -> f a -> f a) -> ScaleReal f a
ScaleReal RealOf Float -> Array sh Float -> Array sh Float
forall sh a. (C sh, Floating a) => a -> Array sh a -> Array sh a
scale)
      ((RealOf Double -> Array sh Double -> Array sh Double)
-> ScaleReal (Array sh) Double
forall (f :: * -> *) a. (RealOf a -> f a -> f a) -> ScaleReal f a
ScaleReal RealOf Double -> Array sh Double -> Array sh Double
forall sh a. (C sh, Floating a) => a -> Array sh a -> Array sh a
scale)
      ((RealOf (Complex Float)
 -> Array sh (Complex Float) -> Array sh (Complex Float))
-> ScaleReal (Array sh) (Complex Float)
forall (f :: * -> *) a. (RealOf a -> f a -> f a) -> ScaleReal f a
ScaleReal ((RealOf (Complex Float)
  -> Array sh (Complex Float) -> Array sh (Complex Float))
 -> ScaleReal (Array sh) (Complex Float))
-> (RealOf (Complex Float)
    -> Array sh (Complex Float) -> Array sh (Complex Float))
-> ScaleReal (Array sh) (Complex Float)
forall a b. (a -> b) -> a -> b
$ \RealOf (Complex Float)
x -> Vector (sh, Enumeration ComplexPart) Float
-> Array sh (Complex Float)
forall a sh.
Real a =>
Vector (sh, Enumeration ComplexPart) a -> Vector sh (Complex a)
recomplex (Vector (sh, Enumeration ComplexPart) Float
 -> Array sh (Complex Float))
-> (Array sh (Complex Float)
    -> Vector (sh, Enumeration ComplexPart) Float)
-> Array sh (Complex Float)
-> Array sh (Complex Float)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Float
-> Vector (sh, Enumeration ComplexPart) Float
-> Vector (sh, Enumeration ComplexPart) Float
forall sh a. (C sh, Floating a) => a -> Array sh a -> Array sh a
scale Float
RealOf (Complex Float)
x (Vector (sh, Enumeration ComplexPart) Float
 -> Vector (sh, Enumeration ComplexPart) Float)
-> (Array sh (Complex Float)
    -> Vector (sh, Enumeration ComplexPart) Float)
-> Array sh (Complex Float)
-> Vector (sh, Enumeration ComplexPart) Float
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array sh (Complex Float)
-> Vector (sh, Enumeration ComplexPart) Float
forall a sh.
Real a =>
Vector sh (Complex a) -> Vector (sh, Enumeration ComplexPart) a
decomplex)
      ((RealOf (Complex Double)
 -> Array sh (Complex Double) -> Array sh (Complex Double))
-> ScaleReal (Array sh) (Complex Double)
forall (f :: * -> *) a. (RealOf a -> f a -> f a) -> ScaleReal f a
ScaleReal ((RealOf (Complex Double)
  -> Array sh (Complex Double) -> Array sh (Complex Double))
 -> ScaleReal (Array sh) (Complex Double))
-> (RealOf (Complex Double)
    -> Array sh (Complex Double) -> Array sh (Complex Double))
-> ScaleReal (Array sh) (Complex Double)
forall a b. (a -> b) -> a -> b
$ \RealOf (Complex Double)
x -> Vector (sh, Enumeration ComplexPart) Double
-> Array sh (Complex Double)
forall a sh.
Real a =>
Vector (sh, Enumeration ComplexPart) a -> Vector sh (Complex a)
recomplex (Vector (sh, Enumeration ComplexPart) Double
 -> Array sh (Complex Double))
-> (Array sh (Complex Double)
    -> Vector (sh, Enumeration ComplexPart) Double)
-> Array sh (Complex Double)
-> Array sh (Complex Double)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double
-> Vector (sh, Enumeration ComplexPart) Double
-> Vector (sh, Enumeration ComplexPart) Double
forall sh a. (C sh, Floating a) => a -> Array sh a -> Array sh a
scale Double
RealOf (Complex Double)
x (Vector (sh, Enumeration ComplexPart) Double
 -> Vector (sh, Enumeration ComplexPart) Double)
-> (Array sh (Complex Double)
    -> Vector (sh, Enumeration ComplexPart) Double)
-> Array sh (Complex Double)
-> Vector (sh, Enumeration ComplexPart) Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array sh (Complex Double)
-> Vector (sh, Enumeration ComplexPart) Double
forall a sh.
Real a =>
Vector sh (Complex a) -> Vector (sh, Enumeration ComplexPart) a
decomplex)

newtype ScaleReal f a = ScaleReal {ScaleReal f a -> RealOf a -> f a -> f a
getScaleReal :: RealOf a -> f a -> f a}


decomplex ::
   (Class.Real a) =>
   Vector sh (Complex a) -> Vector (sh, Shape.Enumeration ComplexPart) a
decomplex :: Vector sh (Complex a) -> Vector (sh, Enumeration ComplexPart) a
decomplex (Array sh
sh ForeignPtr (Complex a)
a) = (sh, Enumeration ComplexPart)
-> ForeignPtr a -> Vector (sh, Enumeration ComplexPart) a
forall sh a. sh -> ForeignPtr a -> Array sh a
Array (sh
sh, Enumeration ComplexPart
forall n. Enumeration n
Shape.Enumeration) (ForeignPtr (Complex a) -> ForeignPtr a
forall a b. ForeignPtr a -> ForeignPtr b
castForeignPtr ForeignPtr (Complex a)
a)

recomplex ::
   (Class.Real a) =>
   Vector (sh, Shape.Enumeration ComplexPart) a -> Vector sh (Complex a)
recomplex :: Vector (sh, Enumeration ComplexPart) a -> Vector sh (Complex a)
recomplex (Array (sh
sh, Enumeration ComplexPart
Shape.Enumeration) ForeignPtr a
a) = sh -> ForeignPtr (Complex a) -> Vector sh (Complex a)
forall sh a. sh -> ForeignPtr a -> Array sh a
Array sh
sh (ForeignPtr a -> ForeignPtr (Complex a)
forall a b. ForeignPtr a -> ForeignPtr b
castForeignPtr ForeignPtr a
a)



infixl 6 |+|, |-|, `add`, `sub`


add, sub, (|+|), (|-|) ::
   (Shape.C sh, Eq sh, Class.Floating a) =>
   Vector sh a -> Vector sh a -> Vector sh a
add :: Vector sh a -> Vector sh a -> Vector sh a
add = a -> Vector sh a -> Vector sh a -> Vector sh a
forall sh a.
(C sh, Eq sh, Floating a) =>
a -> Vector sh a -> Vector sh a -> Vector sh a
mac a
forall a. Floating a => a
Scalar.one
sub :: Vector sh a -> Vector sh a -> Vector sh a
sub Vector sh a
x Vector sh a
y = a -> Vector sh a -> Vector sh a -> Vector sh a
forall sh a.
(C sh, Eq sh, Floating a) =>
a -> Vector sh a -> Vector sh a -> Vector sh a
mac a
forall a. Floating a => a
minusOne Vector sh a
y Vector sh a
x

|+| :: Vector sh a -> Vector sh a -> Vector sh a
(|+|) = Vector sh a -> Vector sh a -> Vector sh a
forall sh a.
(C sh, Eq sh, Floating a) =>
Vector sh a -> Vector sh a -> Vector sh a
add
|-| :: Vector sh a -> Vector sh a -> Vector sh a
(|-|) = Vector sh a -> Vector sh a -> Vector sh a
forall sh a.
(C sh, Eq sh, Floating a) =>
Vector sh a -> Vector sh a -> Vector sh a
sub

mac ::
   (Shape.C sh, Eq sh, Class.Floating a) =>
   a -> Vector sh a -> Vector sh a -> Vector sh a
mac :: a -> Vector sh a -> Vector sh a -> Vector sh a
mac a
alpha Vector sh a
x Vector sh a
y =
   if Vector sh a -> sh
forall sh a. Array sh a -> sh
Array.shape Vector sh a
x sh -> sh -> Bool
forall a. Eq a => a -> a -> Bool
== Vector sh a -> sh
forall sh a. Array sh a -> sh
Array.shape Vector sh a
y
      then a -> Vector sh a -> Vector sh a -> Vector sh a
forall sh a.
(C sh, Floating a) =>
a -> Array sh a -> Array sh a -> Array sh a
Vector.mac a
alpha Vector sh a
x Vector sh a
y
      else String -> Vector sh a
forall a. HasCallStack => String -> a
error String
"mac: shapes mismatch"

negate :: (Shape.C sh, Class.Floating a) => Vector sh a -> Vector sh a
negate :: Vector sh a -> Vector sh a
negate =
   Conjugate (Array sh) a -> Vector sh a -> Vector sh a
forall (f :: * -> *) a. Conjugate f a -> f a -> f a
getConjugate (Conjugate (Array sh) a -> Vector sh a -> Vector sh a)
-> Conjugate (Array sh) a -> Vector sh a -> Vector sh a
forall a b. (a -> b) -> a -> b
$
   Conjugate (Array sh) Float
-> Conjugate (Array sh) Double
-> Conjugate (Array sh) (Complex Float)
-> Conjugate (Array sh) (Complex Double)
-> Conjugate (Array sh) a
forall a (f :: * -> *).
Floating a =>
f Float
-> f Double -> f (Complex Float) -> f (Complex Double) -> f a
Class.switchFloating
      ((Array sh Float -> Array sh Float) -> Conjugate (Array sh) Float
forall (f :: * -> *) a. (f a -> f a) -> Conjugate f a
Conjugate ((Array sh Float -> Array sh Float) -> Conjugate (Array sh) Float)
-> (Array sh Float -> Array sh Float) -> Conjugate (Array sh) Float
forall a b. (a -> b) -> a -> b
$ RealOf Float -> Array sh Float -> Array sh Float
forall sh a.
(C sh, Floating a) =>
RealOf a -> Vector sh a -> Vector sh a
scaleReal RealOf Float
forall a. Floating a => a
Scalar.minusOne)
      ((Array sh Double -> Array sh Double) -> Conjugate (Array sh) Double
forall (f :: * -> *) a. (f a -> f a) -> Conjugate f a
Conjugate ((Array sh Double -> Array sh Double)
 -> Conjugate (Array sh) Double)
-> (Array sh Double -> Array sh Double)
-> Conjugate (Array sh) Double
forall a b. (a -> b) -> a -> b
$ RealOf Double -> Array sh Double -> Array sh Double
forall sh a.
(C sh, Floating a) =>
RealOf a -> Vector sh a -> Vector sh a
scaleReal RealOf Double
forall a. Floating a => a
Scalar.minusOne)
      ((Array sh (Complex Float) -> Array sh (Complex Float))
-> Conjugate (Array sh) (Complex Float)
forall (f :: * -> *) a. (f a -> f a) -> Conjugate f a
Conjugate ((Array sh (Complex Float) -> Array sh (Complex Float))
 -> Conjugate (Array sh) (Complex Float))
-> (Array sh (Complex Float) -> Array sh (Complex Float))
-> Conjugate (Array sh) (Complex Float)
forall a b. (a -> b) -> a -> b
$ RealOf (Complex Float)
-> Array sh (Complex Float) -> Array sh (Complex Float)
forall sh a.
(C sh, Floating a) =>
RealOf a -> Vector sh a -> Vector sh a
scaleReal RealOf (Complex Float)
forall a. Floating a => a
Scalar.minusOne)
      ((Array sh (Complex Double) -> Array sh (Complex Double))
-> Conjugate (Array sh) (Complex Double)
forall (f :: * -> *) a. (f a -> f a) -> Conjugate f a
Conjugate ((Array sh (Complex Double) -> Array sh (Complex Double))
 -> Conjugate (Array sh) (Complex Double))
-> (Array sh (Complex Double) -> Array sh (Complex Double))
-> Conjugate (Array sh) (Complex Double)
forall a b. (a -> b) -> a -> b
$ RealOf (Complex Double)
-> Array sh (Complex Double) -> Array sh (Complex Double)
forall sh a.
(C sh, Floating a) =>
RealOf a -> Vector sh a -> Vector sh a
scaleReal RealOf (Complex Double)
forall a. Floating a => a
Scalar.minusOne)


raise :: (Shape.C sh, Class.Floating a) => a -> Array sh a -> Array sh a
raise :: a -> Array sh a -> Array sh a
raise a
c (Array sh
shX ForeignPtr a
x) =
   sh -> (Int -> Ptr a -> IO ()) -> Array sh a
forall sh a.
(C sh, Storable a) =>
sh -> (Int -> Ptr a -> IO ()) -> Array sh a
Array.unsafeCreateWithSize sh
shX ((Int -> Ptr a -> IO ()) -> Array sh a)
-> (Int -> Ptr a -> IO ()) -> Array sh a
forall a b. (a -> b) -> a -> b
$ \Int
n Ptr a
yPtr -> 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 CInt
nPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint Int
n
      Ptr a
cPtr <- a -> FortranIO () (Ptr a)
forall a r. Floating a => a -> FortranIO r (Ptr a)
Call.number a
c
      Ptr a
onePtr <- a -> FortranIO () (Ptr a)
forall a r. Floating a => a -> FortranIO r (Ptr a)
Call.number a
forall a. Floating a => a
Scalar.one
      Ptr CInt
inccPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint Int
0
      Ptr a
xPtr <- ((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
x
      Ptr CInt
inc1Ptr <- 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
$ do
         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.copy Ptr CInt
nPtr Ptr a
xPtr Ptr CInt
inc1Ptr Ptr a
yPtr Ptr CInt
inc1Ptr
         Ptr CInt
-> Ptr a -> Ptr a -> Ptr CInt -> Ptr a -> Ptr CInt -> IO ()
forall a.
Floating a =>
Ptr CInt
-> Ptr a -> Ptr a -> Ptr CInt -> Ptr a -> Ptr CInt -> IO ()
BlasGen.axpy Ptr CInt
nPtr Ptr a
onePtr Ptr a
cPtr Ptr CInt
inccPtr Ptr a
yPtr Ptr CInt
inc1Ptr


mul ::
   (Shape.C sh, Eq sh, Class.Floating a) =>
   Vector sh a -> Vector sh a -> Vector sh a
mul :: Vector sh a -> Vector sh a -> Vector sh a
mul = Conjugation -> Vector sh a -> Vector sh a -> Vector sh a
forall sh a.
(C sh, Eq sh, Floating a) =>
Conjugation -> Vector sh a -> Vector sh a -> Vector sh a
mulConjugation Conjugation
NonConjugated

{- |
prop> \xs ys -> mulConj xs ys == mul (conjugate xs) ys
-}
mulConj ::
   (Shape.C sh, Eq sh, Class.Floating a) =>
   Vector sh a -> Vector sh a -> Vector sh a
mulConj :: Vector sh a -> Vector sh a -> Vector sh a
mulConj = Conjugation -> Vector sh a -> Vector sh a -> Vector sh a
forall sh a.
(C sh, Eq sh, Floating a) =>
Conjugation -> Vector sh a -> Vector sh a -> Vector sh a
mulConjugation Conjugation
Conjugated

mulConjugation ::
   (Shape.C sh, Eq sh, Class.Floating a) =>
   Conjugation -> Vector sh a -> Vector sh a -> Vector sh a
mulConjugation :: Conjugation -> Vector sh a -> Vector sh a -> Vector sh a
mulConjugation Conjugation
conj (Array sh
shA ForeignPtr a
a) (Array sh
shX ForeignPtr a
x) =
      sh -> (Int -> Ptr a -> IO ()) -> Vector sh a
forall sh a.
(C sh, Storable a) =>
sh -> (Int -> Ptr a -> IO ()) -> Array sh a
Array.unsafeCreateWithSize sh
shX ((Int -> Ptr a -> IO ()) -> Vector sh a)
-> (Int -> Ptr a -> IO ()) -> Vector sh a
forall a b. (a -> b) -> a -> b
$ \Int
n Ptr a
yPtr -> do
   String -> Bool -> IO ()
Call.assert String
"mul: shapes mismatch" (sh
shA sh -> sh -> Bool
forall a. Eq a => a -> a -> Bool
== sh
shX)
   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 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 a
xPtr <- ((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
x
      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
$ Conjugation
-> Int -> Ptr a -> Int -> Ptr a -> Int -> Ptr a -> Int -> IO ()
forall a.
Floating a =>
Conjugation
-> Int -> Ptr a -> Int -> Ptr a -> Int -> Ptr a -> Int -> IO ()
Private.mul Conjugation
conj Int
n Ptr a
aPtr Int
1 Ptr a
xPtr Int
1 Ptr a
yPtr Int
1

divide ::
   (Shape.C sh, Eq sh, Class.Floating a) =>
   Vector sh a -> Vector sh a -> Vector sh a
divide :: Vector sh a -> Vector sh a -> Vector sh a
divide (Array sh
shB ForeignPtr a
b) (Array sh
shA ForeignPtr a
a) =
      sh -> (Int -> Ptr a -> IO ()) -> Vector sh a
forall sh a.
(C sh, Storable a) =>
sh -> (Int -> Ptr a -> IO ()) -> Array sh a
Array.unsafeCreateWithSize sh
shB ((Int -> Ptr a -> IO ()) -> Vector sh a)
-> (Int -> Ptr a -> IO ()) -> Vector sh a
forall a b. (a -> b) -> a -> b
$ \Int
n Ptr a
xPtr -> do
   String -> Bool -> IO ()
Call.assert String
"divide: shapes mismatch" (sh
shA sh -> sh -> Bool
forall a. Eq a => a -> a -> Bool
== sh
shB)
   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 CInt
nPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint Int
n
      Ptr CInt
klPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint Int
0
      Ptr CInt
kuPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint Int
0
      Ptr CInt
nrhsPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint Int
1
      Ptr a
abPtr <- Int -> ForeignPtr a -> ContT () IO (Ptr a)
forall a r. Storable a => Int -> ForeignPtr a -> ContT r IO (Ptr a)
Private.copyToTemp Int
n ForeignPtr a
a
      Ptr CInt
ldabPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.leadingDim Int
1
      Ptr CInt
ipivPtr <- Int -> FortranIO () (Ptr CInt)
forall a r. Storable a => Int -> FortranIO r (Ptr a)
Call.allocaArray Int
n
      Ptr a
bPtr <- ((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
b
      Ptr CInt
ldxPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.leadingDim Int
n
      IO () -> ContT () IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ContT () IO ()) -> IO () -> ContT () IO ()
forall a b. (a -> b) -> a -> b
$ do
         Int -> Ptr a -> Ptr a -> IO ()
forall a. Floating a => Int -> Ptr a -> Ptr a -> IO ()
Private.copyBlock Int
n Ptr a
bPtr Ptr a
xPtr
         String -> (Ptr CInt -> IO ()) -> IO ()
withInfo String
"gbsv" ((Ptr CInt -> IO ()) -> IO ()) -> (Ptr CInt -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$
            Ptr CInt
-> Ptr CInt
-> Ptr CInt
-> Ptr CInt
-> Ptr a
-> Ptr CInt
-> Ptr CInt
-> Ptr a
-> Ptr CInt
-> Ptr CInt
-> IO ()
forall a.
Floating a =>
Ptr CInt
-> Ptr CInt
-> Ptr CInt
-> Ptr CInt
-> Ptr a
-> Ptr CInt
-> Ptr CInt
-> Ptr a
-> Ptr CInt
-> Ptr CInt
-> IO ()
LapackGen.gbsv Ptr CInt
nPtr Ptr CInt
klPtr Ptr CInt
kuPtr Ptr CInt
nrhsPtr
               Ptr a
abPtr Ptr CInt
ldabPtr Ptr CInt
ipivPtr Ptr a
xPtr Ptr CInt
ldxPtr

recip :: (Shape.C sh, Class.Floating a) => Vector sh a -> Vector sh a
recip :: Vector sh a -> Vector sh a
recip Vector sh a
x =
   Array (Unchecked sh) a -> Vector sh a
forall sh a. Array (Unchecked sh) a -> Array sh a
Vector.recheck (Array (Unchecked sh) a -> Vector sh a)
-> Array (Unchecked sh) a -> Vector sh a
forall a b. (a -> b) -> a -> b
$
   Array (Unchecked sh) a
-> Array (Unchecked sh) a -> Array (Unchecked sh) a
forall sh a.
(C sh, Eq sh, Floating a) =>
Vector sh a -> Vector sh a -> Vector sh a
divide (Vector sh a -> Array (Unchecked sh) a
forall sh a. Array sh a -> Array (Unchecked sh) a
Vector.uncheck (Vector sh a -> Array (Unchecked sh) a)
-> Vector sh a -> Array (Unchecked sh) a
forall a b. (a -> b) -> a -> b
$ sh -> Vector sh a
forall sh a. (C sh, Floating a) => sh -> Vector sh a
one (sh -> Vector sh a) -> sh -> Vector sh a
forall a b. (a -> b) -> a -> b
$ Vector sh a -> sh
forall sh a. Array sh a -> sh
Array.shape Vector sh a
x) (Vector sh a -> Array (Unchecked sh) a
forall sh a. Array sh a -> Array (Unchecked sh) a
Vector.uncheck Vector sh a
x)


newtype Conjugate f a = Conjugate {Conjugate f a -> f a -> f a
getConjugate :: f a -> f a}

conjugate ::
   (Shape.C sh, Class.Floating a) =>
   Vector sh a -> Vector sh a
conjugate :: Vector sh a -> Vector sh a
conjugate =
   Conjugate (Array sh) a -> Vector sh a -> Vector sh a
forall (f :: * -> *) a. Conjugate f a -> f a -> f a
getConjugate (Conjugate (Array sh) a -> Vector sh a -> Vector sh a)
-> Conjugate (Array sh) a -> Vector sh a -> Vector sh a
forall a b. (a -> b) -> a -> b
$
   Conjugate (Array sh) Float
-> Conjugate (Array sh) Double
-> Conjugate (Array sh) (Complex Float)
-> Conjugate (Array sh) (Complex Double)
-> Conjugate (Array sh) a
forall a (f :: * -> *).
Floating a =>
f Float
-> f Double -> f (Complex Float) -> f (Complex Double) -> f a
Class.switchFloating
      ((Array sh Float -> Array sh Float) -> Conjugate (Array sh) Float
forall (f :: * -> *) a. (f a -> f a) -> Conjugate f a
Conjugate Array sh Float -> Array sh Float
forall a. a -> a
id)
      ((Array sh Double -> Array sh Double) -> Conjugate (Array sh) Double
forall (f :: * -> *) a. (f a -> f a) -> Conjugate f a
Conjugate Array sh Double -> Array sh Double
forall a. a -> a
id)
      ((Array sh (Complex Float) -> Array sh (Complex Float))
-> Conjugate (Array sh) (Complex Float)
forall (f :: * -> *) a. (f a -> f a) -> Conjugate f a
Conjugate Array sh (Complex Float) -> Array sh (Complex Float)
forall sh a.
(C sh, Real a) =>
Vector sh (Complex a) -> Vector sh (Complex a)
complexConjugate)
      ((Array sh (Complex Double) -> Array sh (Complex Double))
-> Conjugate (Array sh) (Complex Double)
forall (f :: * -> *) a. (f a -> f a) -> Conjugate f a
Conjugate Array sh (Complex Double) -> Array sh (Complex Double)
forall sh a.
(C sh, Real a) =>
Vector sh (Complex a) -> Vector sh (Complex a)
complexConjugate)

complexConjugate ::
   (Shape.C sh, Class.Real a) =>
   Vector sh (Complex a) -> Vector sh (Complex a)
complexConjugate :: Vector sh (Complex a) -> Vector sh (Complex a)
complexConjugate (Array sh
sh ForeignPtr (Complex a)
x) = sh -> (Int -> Ptr (Complex a) -> IO ()) -> Vector sh (Complex a)
forall sh a.
(C sh, Storable a) =>
sh -> (Int -> Ptr a -> IO ()) -> Array sh a
Array.unsafeCreateWithSize sh
sh ((Int -> Ptr (Complex a) -> IO ()) -> Vector sh (Complex a))
-> (Int -> Ptr (Complex a) -> IO ()) -> Vector sh (Complex a)
forall a b. (a -> b) -> a -> b
$ \Int
n Ptr (Complex a)
syPtr ->
   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 CInt
nPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint Int
n
      Ptr (Complex a)
sxPtr <- ((Ptr (Complex a) -> IO ()) -> IO ())
-> ContT () IO (Ptr (Complex a))
forall k (r :: k) (m :: k -> *) a.
((a -> m r) -> m r) -> ContT r m a
ContT (((Ptr (Complex a) -> IO ()) -> IO ())
 -> ContT () IO (Ptr (Complex a)))
-> ((Ptr (Complex a) -> IO ()) -> IO ())
-> ContT () IO (Ptr (Complex a))
forall a b. (a -> b) -> a -> b
$ ForeignPtr (Complex a) -> (Ptr (Complex a) -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr (Complex a)
x
      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
      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 CInt
-> Ptr (Complex a)
-> Ptr CInt
-> Ptr (Complex a)
-> Ptr CInt
-> IO ()
forall a.
Floating a =>
Ptr CInt -> Ptr a -> Ptr CInt -> Ptr a -> Ptr CInt -> IO ()
copyConjugate Ptr CInt
nPtr Ptr (Complex a)
sxPtr Ptr CInt
incxPtr Ptr (Complex a)
syPtr Ptr CInt
incyPtr


fromReal ::
   (Shape.C sh, Class.Floating a) => Vector sh (RealOf a) -> Vector sh a
fromReal :: Vector sh (RealOf a) -> Vector sh a
fromReal =
   FromReal (Array sh) a -> Vector sh (RealOf a) -> Vector sh a
forall (f :: * -> *) a. FromReal f a -> f (RealOf a) -> f a
getFromReal (FromReal (Array sh) a -> Vector sh (RealOf a) -> Vector sh a)
-> FromReal (Array sh) a -> Vector sh (RealOf a) -> Vector sh a
forall a b. (a -> b) -> a -> b
$
   FromReal (Array sh) Float
-> FromReal (Array sh) Double
-> FromReal (Array sh) (Complex Float)
-> FromReal (Array sh) (Complex Double)
-> FromReal (Array sh) a
forall a (f :: * -> *).
Floating a =>
f Float
-> f Double -> f (Complex Float) -> f (Complex Double) -> f a
Class.switchFloating
      ((Array sh (RealOf Float) -> Array sh Float)
-> FromReal (Array sh) Float
forall (f :: * -> *) a. (f (RealOf a) -> f a) -> FromReal f a
FromReal Array sh (RealOf Float) -> Array sh Float
forall a. a -> a
id)
      ((Array sh (RealOf Double) -> Array sh Double)
-> FromReal (Array sh) Double
forall (f :: * -> *) a. (f (RealOf a) -> f a) -> FromReal f a
FromReal Array sh (RealOf Double) -> Array sh Double
forall a. a -> a
id)
      ((Array sh (RealOf (Complex Float)) -> Array sh (Complex Float))
-> FromReal (Array sh) (Complex Float)
forall (f :: * -> *) a. (f (RealOf a) -> f a) -> FromReal f a
FromReal Array sh (RealOf (Complex Float)) -> Array sh (Complex Float)
forall sh a. (C sh, Real a) => Vector sh a -> Vector sh (Complex a)
complexFromReal)
      ((Array sh (RealOf (Complex Double)) -> Array sh (Complex Double))
-> FromReal (Array sh) (Complex Double)
forall (f :: * -> *) a. (f (RealOf a) -> f a) -> FromReal f a
FromReal Array sh (RealOf (Complex Double)) -> Array sh (Complex Double)
forall sh a. (C sh, Real a) => Vector sh a -> Vector sh (Complex a)
complexFromReal)

newtype FromReal f a = FromReal {FromReal f a -> f (RealOf a) -> f a
getFromReal :: f (RealOf a) -> f a}

toComplex ::
   (Shape.C sh, Class.Floating a) => Vector sh a -> Vector sh (ComplexOf a)
toComplex :: Vector sh a -> Vector sh (ComplexOf a)
toComplex =
   ToComplex (Array sh) a -> Vector sh a -> Vector sh (ComplexOf a)
forall (f :: * -> *) a. ToComplex f a -> f a -> f (ComplexOf a)
getToComplex (ToComplex (Array sh) a -> Vector sh a -> Vector sh (ComplexOf a))
-> ToComplex (Array sh) a -> Vector sh a -> Vector sh (ComplexOf a)
forall a b. (a -> b) -> a -> b
$
   ToComplex (Array sh) Float
-> ToComplex (Array sh) Double
-> ToComplex (Array sh) (Complex Float)
-> ToComplex (Array sh) (Complex Double)
-> ToComplex (Array sh) a
forall a (f :: * -> *).
Floating a =>
f Float
-> f Double -> f (Complex Float) -> f (Complex Double) -> f a
Class.switchFloating
      ((Array sh Float -> Array sh (ComplexOf Float))
-> ToComplex (Array sh) Float
forall (f :: * -> *) a. (f a -> f (ComplexOf a)) -> ToComplex f a
ToComplex Array sh Float -> Array sh (ComplexOf Float)
forall sh a. (C sh, Real a) => Vector sh a -> Vector sh (Complex a)
complexFromReal)
      ((Array sh Double -> Array sh (ComplexOf Double))
-> ToComplex (Array sh) Double
forall (f :: * -> *) a. (f a -> f (ComplexOf a)) -> ToComplex f a
ToComplex Array sh Double -> Array sh (ComplexOf Double)
forall sh a. (C sh, Real a) => Vector sh a -> Vector sh (Complex a)
complexFromReal)
      ((Array sh (Complex Float) -> Array sh (ComplexOf (Complex Float)))
-> ToComplex (Array sh) (Complex Float)
forall (f :: * -> *) a. (f a -> f (ComplexOf a)) -> ToComplex f a
ToComplex Array sh (Complex Float) -> Array sh (ComplexOf (Complex Float))
forall a. a -> a
id)
      ((Array sh (Complex Double)
 -> Array sh (ComplexOf (Complex Double)))
-> ToComplex (Array sh) (Complex Double)
forall (f :: * -> *) a. (f a -> f (ComplexOf a)) -> ToComplex f a
ToComplex Array sh (Complex Double) -> Array sh (ComplexOf (Complex Double))
forall a. a -> a
id)

newtype ToComplex f a = ToComplex {ToComplex f a -> f a -> f (ComplexOf a)
getToComplex :: f a -> f (ComplexOf a)}

complexFromReal ::
   (Shape.C sh, Class.Real a) => Vector sh a -> Vector sh (Complex a)
complexFromReal :: Vector sh a -> Vector sh (Complex a)
complexFromReal (Array sh
sh ForeignPtr a
x) =
   sh -> (Int -> Ptr (Complex a) -> IO ()) -> Vector sh (Complex a)
forall sh a.
(C sh, Storable a) =>
sh -> (Int -> Ptr a -> IO ()) -> Array sh a
Array.unsafeCreateWithSize sh
sh ((Int -> Ptr (Complex a) -> IO ()) -> Vector sh (Complex a))
-> (Int -> Ptr (Complex a) -> IO ()) -> Vector sh (Complex a)
forall a b. (a -> b) -> a -> b
$ \Int
n Ptr (Complex a)
yPtr ->
   case Ptr (Complex a) -> Ptr (RealOf (Complex a))
forall a. Ptr a -> Ptr (RealOf a)
realPtr Ptr (Complex a)
yPtr of
      Ptr (RealOf (Complex a))
yrPtr -> 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 CInt
nPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint Int
n
         Ptr a
xPtr <- ((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
x
         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
2
         Ptr CInt
inczPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint Int
0
         Ptr a
zPtr <- a -> ContT () IO (Ptr a)
forall a r. Floating a => a -> FortranIO r (Ptr a)
Call.number a
forall a. Floating a => a
Scalar.zero
         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
            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.copy Ptr CInt
nPtr Ptr a
xPtr Ptr CInt
incxPtr Ptr a
Ptr (RealOf (Complex a))
yrPtr Ptr CInt
incyPtr
            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.copy Ptr CInt
nPtr Ptr a
zPtr Ptr CInt
inczPtr (Ptr a -> Int -> Ptr a
forall a. Storable a => Ptr a -> Int -> Ptr a
advancePtr Ptr a
Ptr (RealOf (Complex a))
yrPtr Int
1) Ptr CInt
incyPtr


realPart ::
   (Shape.C sh, Class.Floating a) => Vector sh a -> Vector sh (RealOf a)
realPart :: Vector sh a -> Vector sh (RealOf a)
realPart =
   ToReal (Array sh) a -> Vector sh a -> Vector sh (RealOf a)
forall (f :: * -> *) a. ToReal f a -> f a -> f (RealOf a)
getToReal (ToReal (Array sh) a -> Vector sh a -> Vector sh (RealOf a))
-> ToReal (Array sh) a -> Vector sh a -> Vector sh (RealOf a)
forall a b. (a -> b) -> a -> b
$
   ToReal (Array sh) Float
-> ToReal (Array sh) Double
-> ToReal (Array sh) (Complex Float)
-> ToReal (Array sh) (Complex Double)
-> ToReal (Array sh) a
forall a (f :: * -> *).
Floating a =>
f Float
-> f Double -> f (Complex Float) -> f (Complex Double) -> f a
Class.switchFloating
      ((Array sh Float -> Array sh (RealOf Float))
-> ToReal (Array sh) Float
forall (f :: * -> *) a. (f a -> f (RealOf a)) -> ToReal f a
ToReal Array sh Float -> Array sh (RealOf Float)
forall a. a -> a
id)
      ((Array sh Double -> Array sh (RealOf Double))
-> ToReal (Array sh) Double
forall (f :: * -> *) a. (f a -> f (RealOf a)) -> ToReal f a
ToReal Array sh Double -> Array sh (RealOf Double)
forall a. a -> a
id)
      ((Array sh (Complex Float) -> Array sh (RealOf (Complex Float)))
-> ToReal (Array sh) (Complex Float)
forall (f :: * -> *) a. (f a -> f (RealOf a)) -> ToReal f a
ToReal ((Array sh (Complex Float) -> Array sh (RealOf (Complex Float)))
 -> ToReal (Array sh) (Complex Float))
-> (Array sh (Complex Float) -> Array sh (RealOf (Complex Float)))
-> ToReal (Array sh) (Complex Float)
forall a b. (a -> b) -> a -> b
$ ComplexPart
-> Matrix sh (Enumeration ComplexPart) Float -> Array sh Float
forall height width ix a.
(C height, Indexed width, Index width ~ ix, Floating a) =>
ix -> Matrix height width a -> Vector height a
RowMajor.takeColumn ComplexPart
RealPart (Matrix sh (Enumeration ComplexPart) Float -> Array sh Float)
-> (Array sh (Complex Float)
    -> Matrix sh (Enumeration ComplexPart) Float)
-> Array sh (Complex Float)
-> Array sh Float
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array sh (Complex Float)
-> Matrix sh (Enumeration ComplexPart) Float
forall a sh.
Real a =>
Vector sh (Complex a) -> Vector (sh, Enumeration ComplexPart) a
decomplex)
      ((Array sh (Complex Double) -> Array sh (RealOf (Complex Double)))
-> ToReal (Array sh) (Complex Double)
forall (f :: * -> *) a. (f a -> f (RealOf a)) -> ToReal f a
ToReal ((Array sh (Complex Double) -> Array sh (RealOf (Complex Double)))
 -> ToReal (Array sh) (Complex Double))
-> (Array sh (Complex Double)
    -> Array sh (RealOf (Complex Double)))
-> ToReal (Array sh) (Complex Double)
forall a b. (a -> b) -> a -> b
$ ComplexPart
-> Matrix sh (Enumeration ComplexPart) Double -> Array sh Double
forall height width ix a.
(C height, Indexed width, Index width ~ ix, Floating a) =>
ix -> Matrix height width a -> Vector height a
RowMajor.takeColumn ComplexPart
RealPart (Matrix sh (Enumeration ComplexPart) Double -> Array sh Double)
-> (Array sh (Complex Double)
    -> Matrix sh (Enumeration ComplexPart) Double)
-> Array sh (Complex Double)
-> Array sh Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array sh (Complex Double)
-> Matrix sh (Enumeration ComplexPart) Double
forall a sh.
Real a =>
Vector sh (Complex a) -> Vector (sh, Enumeration ComplexPart) a
decomplex)

newtype ToReal f a = ToReal {ToReal f a -> f a -> f (RealOf a)
getToReal :: f a -> f (RealOf a)}

imaginaryPart ::
   (Shape.C sh, Class.Real a) => Vector sh (Complex a) -> Vector sh a
imaginaryPart :: Vector sh (Complex a) -> Vector sh a
imaginaryPart = ComplexPart -> Matrix sh (Enumeration ComplexPart) a -> Vector sh 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 ComplexPart
ImaginaryPart (Matrix sh (Enumeration ComplexPart) a -> Vector sh a)
-> (Vector sh (Complex a) -> Matrix sh (Enumeration ComplexPart) a)
-> Vector sh (Complex a)
-> Vector sh a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector sh (Complex a) -> Matrix sh (Enumeration ComplexPart) a
forall a sh.
Real a =>
Vector sh (Complex a) -> Vector (sh, Enumeration ComplexPart) a
decomplex


zipComplex ::
   (Shape.C sh, Eq sh, Class.Real a) =>
   Vector sh a -> Vector sh a -> Vector sh (Complex a)
zipComplex :: Vector sh a -> Vector sh a -> Vector sh (Complex a)
zipComplex (Array sh
shr ForeignPtr a
xr) (Array sh
shi ForeignPtr a
xi) =
   sh -> (Int -> Ptr (Complex a) -> IO ()) -> Vector sh (Complex a)
forall sh a.
(C sh, Storable a) =>
sh -> (Int -> Ptr a -> IO ()) -> Array sh a
Array.unsafeCreateWithSize sh
shr ((Int -> Ptr (Complex a) -> IO ()) -> Vector sh (Complex a))
-> (Int -> Ptr (Complex a) -> IO ()) -> Vector sh (Complex a)
forall a b. (a -> b) -> a -> b
$ \Int
n Ptr (Complex a)
yPtr -> 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
      IO () -> ContT () IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ContT () IO ()) -> IO () -> ContT () IO ()
forall a b. (a -> b) -> a -> b
$ String -> Bool -> IO ()
Call.assert String
"zipComplex: shapes mismatch" (sh
shrsh -> sh -> Bool
forall a. Eq a => a -> a -> Bool
==sh
shi)
      Ptr CInt
nPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint Int
n
      Ptr a
xrPtr <- ((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
xr
      Ptr a
xiPtr <- ((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
xi
      let yrPtr :: Ptr (RealOf (Complex a))
yrPtr = Ptr (Complex a) -> Ptr (RealOf (Complex a))
forall a. Ptr a -> Ptr (RealOf a)
realPtr Ptr (Complex a)
yPtr
      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
2
      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
         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.copy Ptr CInt
nPtr Ptr a
xrPtr Ptr CInt
incxPtr Ptr a
Ptr (RealOf (Complex a))
yrPtr Ptr CInt
incyPtr
         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.copy Ptr CInt
nPtr Ptr a
xiPtr Ptr CInt
incxPtr (Ptr a -> Int -> Ptr a
forall a. Storable a => Ptr a -> Int -> Ptr a
advancePtr Ptr a
Ptr (RealOf (Complex a))
yrPtr Int
1) Ptr CInt
incyPtr

unzipComplex ::
   (Shape.C sh, Class.Real a) =>
   Vector sh (Complex a) -> (Vector sh a, Vector sh a)
unzipComplex :: Vector sh (Complex a) -> (Vector sh a, Vector sh a)
unzipComplex Vector sh (Complex a)
x = (Vector sh (Complex a) -> Vector sh (RealOf (Complex a))
forall sh a.
(C sh, Floating a) =>
Vector sh a -> Vector sh (RealOf a)
realPart Vector sh (Complex a)
x, Vector sh (Complex a) -> Vector sh a
forall sh a. (C sh, Real a) => Vector sh (Complex a) -> Vector sh a
imaginaryPart Vector sh (Complex a)
x)


data RandomDistribution =
     UniformBox01
   | UniformBoxPM1
   | Normal
   | UniformDisc
   | UniformCircle
   deriving (RandomDistribution -> RandomDistribution -> Bool
(RandomDistribution -> RandomDistribution -> Bool)
-> (RandomDistribution -> RandomDistribution -> Bool)
-> Eq RandomDistribution
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: RandomDistribution -> RandomDistribution -> Bool
$c/= :: RandomDistribution -> RandomDistribution -> Bool
== :: RandomDistribution -> RandomDistribution -> Bool
$c== :: RandomDistribution -> RandomDistribution -> Bool
Eq, Eq RandomDistribution
Eq RandomDistribution
-> (RandomDistribution -> RandomDistribution -> Ordering)
-> (RandomDistribution -> RandomDistribution -> Bool)
-> (RandomDistribution -> RandomDistribution -> Bool)
-> (RandomDistribution -> RandomDistribution -> Bool)
-> (RandomDistribution -> RandomDistribution -> Bool)
-> (RandomDistribution -> RandomDistribution -> RandomDistribution)
-> (RandomDistribution -> RandomDistribution -> RandomDistribution)
-> Ord RandomDistribution
RandomDistribution -> RandomDistribution -> Bool
RandomDistribution -> RandomDistribution -> Ordering
RandomDistribution -> RandomDistribution -> RandomDistribution
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: RandomDistribution -> RandomDistribution -> RandomDistribution
$cmin :: RandomDistribution -> RandomDistribution -> RandomDistribution
max :: RandomDistribution -> RandomDistribution -> RandomDistribution
$cmax :: RandomDistribution -> RandomDistribution -> RandomDistribution
>= :: RandomDistribution -> RandomDistribution -> Bool
$c>= :: RandomDistribution -> RandomDistribution -> Bool
> :: RandomDistribution -> RandomDistribution -> Bool
$c> :: RandomDistribution -> RandomDistribution -> Bool
<= :: RandomDistribution -> RandomDistribution -> Bool
$c<= :: RandomDistribution -> RandomDistribution -> Bool
< :: RandomDistribution -> RandomDistribution -> Bool
$c< :: RandomDistribution -> RandomDistribution -> Bool
compare :: RandomDistribution -> RandomDistribution -> Ordering
$ccompare :: RandomDistribution -> RandomDistribution -> Ordering
$cp1Ord :: Eq RandomDistribution
Ord, Int -> RandomDistribution -> ShowS
[RandomDistribution] -> ShowS
RandomDistribution -> String
(Int -> RandomDistribution -> ShowS)
-> (RandomDistribution -> String)
-> ([RandomDistribution] -> ShowS)
-> Show RandomDistribution
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [RandomDistribution] -> ShowS
$cshowList :: [RandomDistribution] -> ShowS
show :: RandomDistribution -> String
$cshow :: RandomDistribution -> String
showsPrec :: Int -> RandomDistribution -> ShowS
$cshowsPrec :: Int -> RandomDistribution -> ShowS
Show, Int -> RandomDistribution
RandomDistribution -> Int
RandomDistribution -> [RandomDistribution]
RandomDistribution -> RandomDistribution
RandomDistribution -> RandomDistribution -> [RandomDistribution]
RandomDistribution
-> RandomDistribution -> RandomDistribution -> [RandomDistribution]
(RandomDistribution -> RandomDistribution)
-> (RandomDistribution -> RandomDistribution)
-> (Int -> RandomDistribution)
-> (RandomDistribution -> Int)
-> (RandomDistribution -> [RandomDistribution])
-> (RandomDistribution
    -> RandomDistribution -> [RandomDistribution])
-> (RandomDistribution
    -> RandomDistribution -> [RandomDistribution])
-> (RandomDistribution
    -> RandomDistribution
    -> RandomDistribution
    -> [RandomDistribution])
-> Enum RandomDistribution
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: RandomDistribution
-> RandomDistribution -> RandomDistribution -> [RandomDistribution]
$cenumFromThenTo :: RandomDistribution
-> RandomDistribution -> RandomDistribution -> [RandomDistribution]
enumFromTo :: RandomDistribution -> RandomDistribution -> [RandomDistribution]
$cenumFromTo :: RandomDistribution -> RandomDistribution -> [RandomDistribution]
enumFromThen :: RandomDistribution -> RandomDistribution -> [RandomDistribution]
$cenumFromThen :: RandomDistribution -> RandomDistribution -> [RandomDistribution]
enumFrom :: RandomDistribution -> [RandomDistribution]
$cenumFrom :: RandomDistribution -> [RandomDistribution]
fromEnum :: RandomDistribution -> Int
$cfromEnum :: RandomDistribution -> Int
toEnum :: Int -> RandomDistribution
$ctoEnum :: Int -> RandomDistribution
pred :: RandomDistribution -> RandomDistribution
$cpred :: RandomDistribution -> RandomDistribution
succ :: RandomDistribution -> RandomDistribution
$csucc :: RandomDistribution -> RandomDistribution
Enum)

{-
@random distribution shape seed@

Only the least significant 47 bits of @seed@ are used.
-}
random ::
   (Shape.C sh, Class.Floating a) =>
   RandomDistribution -> sh -> Word64 -> Vector sh a
random :: RandomDistribution -> sh -> Word64 -> Vector sh a
random RandomDistribution
dist sh
sh Word64
seed = sh -> (Int -> Ptr a -> IO ()) -> Vector sh a
forall sh a.
(C sh, Storable a) =>
sh -> (Int -> Ptr a -> IO ()) -> Array sh a
Array.unsafeCreateWithSize sh
sh ((Int -> Ptr a -> IO ()) -> Vector sh a)
-> (Int -> Ptr a -> IO ()) -> Vector sh a
forall a b. (a -> b) -> a -> b
$ \Int
n Ptr a
xPtr ->
   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 CInt
nPtr <- Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint Int
n
      Ptr CInt
distPtr <-
         Int -> FortranIO () (Ptr CInt)
forall r. Int -> FortranIO r (Ptr CInt)
Call.cint (Int -> FortranIO () (Ptr CInt)) -> Int -> FortranIO () (Ptr CInt)
forall a b. (a -> b) -> a -> b
$
         case (Ptr a -> Bool -> Bool -> Bool
forall a (f :: * -> *) b. Floating a => f a -> b -> b -> b
Private.caseRealComplexFunc Ptr a
xPtr Bool
False Bool
True, RandomDistribution
dist) of
            (Bool
_, RandomDistribution
UniformBox01) -> Int
1
            (Bool
_, RandomDistribution
UniformBoxPM1) -> Int
2
            (Bool
_, RandomDistribution
Normal) -> Int
3
            (Bool
True, RandomDistribution
UniformDisc) -> Int
4
            (Bool
True, RandomDistribution
UniformCircle) -> Int
5
            (Bool
False, RandomDistribution
UniformDisc) -> Int
2
            (Bool
False, RandomDistribution
UniformCircle) ->
               String -> Int
forall a. HasCallStack => String -> a
error
                  String
"Vector.random: UniformCircle not supported for real numbers"
      Ptr CInt
iseedPtr <- Int -> FortranIO () (Ptr CInt)
forall a r. Storable a => Int -> FortranIO r (Ptr a)
Call.allocaArray Int
4
      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
         Ptr CInt -> Int -> CInt -> IO ()
forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff Ptr CInt
iseedPtr Int
0 (CInt -> IO ()) -> CInt -> IO ()
forall a b. (a -> b) -> a -> b
$ Word64 -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral ((Word64
seed Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
35) Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. Word64
0xFFF)
         Ptr CInt -> Int -> CInt -> IO ()
forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff Ptr CInt
iseedPtr Int
1 (CInt -> IO ()) -> CInt -> IO ()
forall a b. (a -> b) -> a -> b
$ Word64 -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral ((Word64
seed Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
23) Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. Word64
0xFFF)
         Ptr CInt -> Int -> CInt -> IO ()
forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff Ptr CInt
iseedPtr Int
2 (CInt -> IO ()) -> CInt -> IO ()
forall a b. (a -> b) -> a -> b
$ Word64 -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral ((Word64
seed Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
11) Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. Word64
0xFFF)
         Ptr CInt -> Int -> CInt -> IO ()
forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff Ptr CInt
iseedPtr Int
3 (CInt -> IO ()) -> CInt -> IO ()
forall a b. (a -> b) -> a -> b
$ Word64 -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral ((Word64
seedWord64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&.Word64
0x7FF)Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
*Word64
2Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
+Word64
1)
         Ptr CInt -> Ptr CInt -> Ptr CInt -> Ptr a -> IO ()
forall a.
Floating a =>
Ptr CInt -> Ptr CInt -> Ptr CInt -> Ptr a -> IO ()
LapackGen.larnv Ptr CInt
distPtr Ptr CInt
iseedPtr Ptr CInt
nPtr Ptr a
xPtr