{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MonoLocalBinds #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
module Test.Massiv.Array.Numeric
  ( -- * Spec for safe Manifest instance
    prop_MatrixMatrixMultiply
  , mutableNumericSpec
  , mutableNumericFloatSpec
  ) where

import Data.Massiv.Array as A
import Test.Massiv.Utils as T
import Test.Massiv.Core.Common ()


naiveMatrixMatrixMultiply ::
     (Num e, Source r1 e, Source r2 e)
  => Array r1 Ix2 e
  -> Array r2 Ix2 e
  -> Array D Ix2 e
naiveMatrixMatrixMultiply :: Array r1 Ix2 e -> Array r2 Ix2 e -> Array D Ix2 e
naiveMatrixMatrixMultiply Array r1 Ix2 e
arr1 Array r2 Ix2 e
arr2
  | Int
n1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
m2 =
    [Char] -> Array D Ix2 e
forall a. HasCallStack => [Char] -> a
error ([Char] -> Array D Ix2 e) -> [Char] -> Array D Ix2 e
forall a b. (a -> b) -> a -> b
$
    [Char]
"(|*|): Inner array dimensions must agree, but received: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++
    Sz Ix2 -> [Char]
forall a. Show a => a -> [Char]
show (Array r1 Ix2 e -> Sz Ix2
forall r ix e. Size r => Array r ix e -> Sz ix
size Array r1 Ix2 e
arr1) [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" and " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Sz Ix2 -> [Char]
forall a. Show a => a -> [Char]
show (Array r2 Ix2 e -> Sz Ix2
forall r ix e. Size r => Array r ix e -> Sz ix
size Array r2 Ix2 e
arr2)
  | Array r1 Ix2 e -> Bool
forall ix r e. (Index ix, Size r) => Array r ix e -> Bool
isEmpty Array r1 Ix2 e
arr1 Bool -> Bool -> Bool
|| Array r2 Ix2 e -> Bool
forall ix r e. (Index ix, Size r) => Array r ix e -> Bool
isEmpty Array r2 Ix2 e
arr2 = Array D Ix2 e
forall r ix e. Load r ix e => Array r ix e
empty
  | Bool
otherwise =
    D -> Comp -> Sz Ix2 -> (Ix2 -> e) -> Array D Ix2 e
forall r ix e.
Load r ix e =>
r -> Comp -> Sz ix -> (ix -> e) -> Array r ix e
makeArrayR D
D Comp
Seq (Ix2 -> Sz Ix2
forall ix. Index ix => ix -> Sz ix
Sz (Int
m1 Int -> Int -> Ix2
:. Int
n2)) ((Ix2 -> e) -> Array D Ix2 e) -> (Ix2 -> e) -> Array D Ix2 e
forall a b. (a -> b) -> a -> b
$ \(Int
i :. Int
j) ->
      (e -> e -> e) -> e -> Array D Int e -> e
forall ix r e a.
(Index ix, Source r e) =>
(a -> e -> a) -> a -> Array r ix e -> a
A.foldlS e -> e -> e
forall a. Num a => a -> a -> a
(+) e
0 ((e -> e -> e) -> Array r1 Int e -> Array D Int e -> Array D Int e
forall ix r1 e1 r2 e2 e.
(Index ix, Source r1 e1, Source r2 e2) =>
(e1 -> e2 -> e) -> Array r1 ix e1 -> Array r2 ix e2 -> Array D ix e
A.zipWith e -> e -> e
forall a. Num a => a -> a -> a
(*) (Array r1 Ix2 e
arr1 Array r1 Ix2 e -> Int -> Array r1 (Lower Ix2) e
forall r ix e.
(HasCallStack, Index ix, Index (Lower ix), Source r e) =>
Array r ix e -> Int -> Array r (Lower ix) e
!> Int
i) (Array r2 Ix2 e
arr2 Array r2 Ix2 e -> Int -> Array D (Lower Ix2) e
forall r ix e.
(HasCallStack, Index ix, Source r e) =>
Array r ix e -> Int -> Array D (Lower ix) e
<! Int
j))
  where
    Sz2 Int
m1 Int
n1 = Array r1 Ix2 e -> Sz Ix2
forall r ix e. Size r => Array r ix e -> Sz ix
size Array r1 Ix2 e
arr1
    Sz2 Int
m2 Int
n2 = Array r2 Ix2 e -> Sz Ix2
forall r ix e. Size r => Array r ix e -> Sz ix
size Array r2 Ix2 e
arr2
{-# INLINE naiveMatrixMatrixMultiply #-}


prop_MatrixMatrixMultiply ::
     forall r e. (Numeric r e, Manifest r e, Eq (Matrix r e), Show (Matrix r e))
  => Fun e e
  -> Matrix r e
  -> Property
prop_MatrixMatrixMultiply :: Fun e e -> Matrix r e -> Property
prop_MatrixMatrixMultiply Fun e e
f Matrix r e
arr = Expectation -> Property
expectProp (Expectation -> Property) -> Expectation -> Property
forall a b. (a -> b) -> a -> b
$ do
  let arr' :: Matrix D e
arr' = Matrix D e -> Matrix D e
forall r e. Source r e => Matrix r e -> Matrix D e
A.transpose ((e -> e) -> Matrix r e -> Matrix D e
forall ix r e' e.
(Index ix, Source r e') =>
(e' -> e) -> Array r ix e' -> Array D ix e
A.map (Fun e e -> e -> e
forall a b. Fun a b -> a -> b
applyFun Fun e e
f) Matrix r e
arr)
  Matrix r e
arr Matrix r e -> Matrix r e -> Matrix r e
forall r e.
(Numeric r e, Manifest r e) =>
Matrix r e -> Matrix r e -> Matrix r e
!><! Matrix D e -> Matrix r e
forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
compute Matrix D e
arr' Matrix r e -> Matrix r e -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Matrix D e -> Matrix r e
forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
compute (Matrix D e -> Matrix D e -> Matrix D e
forall e r1 r2.
(Num e, Source r1 e, Source r2 e) =>
Array r1 Ix2 e -> Array r2 Ix2 e -> Array D Ix2 e
naiveMatrixMatrixMultiply (Matrix r e -> Matrix D e
forall ix r e.
(Index ix, Source r e) =>
Array r ix e -> Array D ix e
delay Matrix r e
arr) Matrix D e
arr')
  Matrix r e
arr Matrix r e -> Matrix r e -> Matrix r e
forall r e.
(Numeric r e, Manifest r e) =>
Matrix r e -> Matrix r e -> Matrix r e
!><! Matrix D e -> Matrix r e
forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
compute (Matrix r e -> Matrix D e
forall r e. Source r e => Matrix r e -> Matrix D e
transpose Matrix r e
arr) Matrix r e -> Matrix r e -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe`
    Matrix D e -> Matrix r e
forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
compute (Matrix D e -> Matrix D e -> Matrix D e
forall e r1 r2.
(Num e, Source r1 e, Source r2 e) =>
Array r1 Ix2 e -> Array r2 Ix2 e -> Array D Ix2 e
naiveMatrixMatrixMultiply (Matrix r e -> Matrix D e
forall ix r e.
(Index ix, Source r e) =>
Array r ix e -> Array D ix e
delay Matrix r e
arr) (Matrix r e -> Matrix D e
forall r e. Source r e => Matrix r e -> Matrix D e
transpose Matrix r e
arr))
  let Sz2 Int
m Int
n = Matrix r e -> Sz Ix2
forall r ix e. Size r => Array r ix e -> Sz ix
size Matrix r e
arr
  Bool -> Expectation -> Expectation
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
m Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
n) (Expectation -> Expectation) -> Expectation -> Expectation
forall a b. (a -> b) -> a -> b
$
    Matrix r e
arr Matrix r e -> Matrix r e -> IO (Matrix r e)
forall r e (m :: * -> *).
(Numeric r e, Manifest r e, MonadThrow m) =>
Matrix r e -> Matrix r e -> m (Matrix r e)
.><. Matrix r e
arr IO (Matrix r e) -> Selector SizeException -> Expectation
forall e a.
(HasCallStack, Exception e) =>
IO a -> Selector e -> Expectation
`shouldThrow` (SizeException -> Selector SizeException
forall a. Eq a => a -> a -> Bool
== Sz Ix2 -> Sz Ix2 -> SizeException
forall ix. Index ix => Sz ix -> Sz ix -> SizeException
SizeMismatchException (Matrix r e -> Sz Ix2
forall r ix e. Size r => Array r ix e -> Sz ix
size Matrix r e
arr) (Int -> Int -> Sz Ix2
Sz2 Int
m Int
n))

prop_MatrixVectorMultiply ::
     forall r e.
     ( Numeric r e
     , Manifest r e
     , Load r Ix1 e
     , Eq e
     , Show e
     )
  => Fun Int e
  -> Matrix r e
  -> Property
prop_MatrixVectorMultiply :: Fun Int e -> Matrix r e -> Property
prop_MatrixVectorMultiply Fun Int e
f Matrix r e
arr =
  Expectation -> Property
expectProp (Expectation -> Property) -> Expectation -> Property
forall a b. (a -> b) -> a -> b
$ do
    let Sz2 Int
_ Int
n = Matrix r e -> Sz Ix2
forall r ix e. Size r => Array r ix e -> Sz ix
size Matrix r e
arr
        v :: Array r Int e
v = Comp -> Sz Int -> (Int -> e) -> Array r Int e
forall r ix e.
Load r ix e =>
Comp -> Sz ix -> (ix -> e) -> Array r ix e
makeArray Comp
Seq (Int -> Sz Int
forall ix. Index ix => ix -> Sz ix
Sz Int
n) (Fun Int e -> Int -> e
forall a b. Fun a b -> a -> b
applyFun Fun Int e
f)
    Matrix r e
arr Matrix r e -> Array r Int e -> Vector D e
forall r e.
(Numeric r e, Source r e) =>
Matrix r e -> Vector r e -> Vector D e
!>< Array r Int e
v Vector D e -> Vector D e -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Array D Ix2 e -> Vector D e
forall r ix e. (Index ix, Size r) => Array r ix e -> Vector r e
flatten (Array D Ix2 e -> Matrix r e -> Array D Ix2 e
forall e r1 r2.
(Num e, Source r1 e, Source r2 e) =>
Array r1 Ix2 e -> Array r2 Ix2 e -> Array D Ix2 e
naiveMatrixMatrixMultiply (Matrix r e -> Array D Ix2 e
forall ix r e.
(Index ix, Source r e) =>
Array r ix e -> Array D ix e
delay Matrix r e
arr) (Sz Ix2 -> Array r Int e -> Matrix r e
forall r ix ix' e.
(HasCallStack, Index ix', Index ix, Size r) =>
Sz ix' -> Array r ix e -> Array r ix' e
resize' (Int -> Int -> Sz Ix2
Sz2 Int
n Int
1) Array r Int e
v))
    Matrix r e
arr Matrix r e -> Array r Int e -> IO (Vector D e)
forall (m :: * -> *) r e.
(MonadThrow m, FoldNumeric r e, Source r e) =>
Matrix r e -> Vector r e -> m (Vector D e)
.>< Comp -> Sz Int -> (Int -> e) -> Array r Int e
forall r ix e.
Load r ix e =>
Comp -> Sz ix -> (ix -> e) -> Array r ix e
makeArray Comp
Seq (Int -> Sz Int
forall ix. Index ix => ix -> Sz ix
Sz (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)) (Fun Int e -> Int -> e
forall a b. Fun a b -> a -> b
applyFun Fun Int e
f) IO (Vector D e) -> Selector SizeException -> Expectation
forall e a.
(HasCallStack, Exception e) =>
IO a -> Selector e -> Expectation
`shouldThrow`
      (SizeException -> Selector SizeException
forall a. Eq a => a -> a -> Bool
== Sz Ix2 -> Sz Ix2 -> SizeException
forall ix. Index ix => Sz ix -> Sz ix -> SizeException
SizeMismatchException (Matrix r e -> Sz Ix2
forall r ix e. Size r => Array r ix e -> Sz ix
size Matrix r e
arr) (Int -> Int -> Sz Ix2
Sz2 (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int
1))

prop_VectorMatrixMultiply ::
     forall r e.
     ( Numeric r e
     , Load r Ix1 e
     , Manifest r e
     , Show (Vector r e)
     , Eq (Vector r e)
     )
  => Fun Int e
  -> Matrix r e
  -> Property
prop_VectorMatrixMultiply :: Fun Int e -> Matrix r e -> Property
prop_VectorMatrixMultiply Fun Int e
f Matrix r e
arr =
  Expectation -> Property
expectProp (Expectation -> Property) -> Expectation -> Property
forall a b. (a -> b) -> a -> b
$ do
    let Sz2 Int
m Int
_ = Matrix r e -> Sz Ix2
forall r ix e. Size r => Array r ix e -> Sz ix
size Matrix r e
arr
        v :: Array r Int e
v = Comp -> Sz Int -> (Int -> e) -> Array r Int e
forall r ix e.
Load r ix e =>
Comp -> Sz ix -> (ix -> e) -> Array r ix e
makeArray Comp
Seq (Int -> Sz Int
forall ix. Index ix => ix -> Sz ix
Sz Int
m) (Fun Int e -> Int -> e
forall a b. Fun a b -> a -> b
applyFun Fun Int e
f)
    Array r Int e
v Array r Int e -> Matrix r e -> Array r Int e
forall r e.
(Numeric r e, Manifest r e) =>
Vector r e -> Matrix r e -> Vector r e
><! Matrix r e
arr Array r Int e -> Array r Int e -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe`
      Array D Int e -> Array r Int e
forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
compute (Array D Ix2 e -> Array D Int e
forall r ix e. (Index ix, Size r) => Array r ix e -> Vector r e
flatten (Matrix r e -> Array D Ix2 e -> Array D Ix2 e
forall e r1 r2.
(Num e, Source r1 e, Source r2 e) =>
Array r1 Ix2 e -> Array r2 Ix2 e -> Array D Ix2 e
naiveMatrixMatrixMultiply (Sz Ix2 -> Array r Int e -> Matrix r e
forall r ix ix' e.
(HasCallStack, Index ix', Index ix, Size r) =>
Sz ix' -> Array r ix e -> Array r ix' e
resize' (Int -> Int -> Sz Ix2
Sz2 Int
1 Int
m) Array r Int e
v) (Matrix r e -> Array D Ix2 e
forall ix r e.
(Index ix, Source r e) =>
Array r ix e -> Array D ix e
delay Matrix r e
arr)))
    Array r Int e -> Matrix r e -> IO (Array r Int e)
forall (m :: * -> *) r e.
(MonadThrow m, Numeric r e, Manifest r e) =>
Vector r e -> Matrix r e -> m (Vector r e)
multiplyVectorByMatrix Array r Int e
v Matrix r e
arr IO (Array r Int e) -> Array r Int e -> Expectation
forall a. (HasCallStack, Show a, Eq a) => IO a -> a -> Expectation
`shouldReturn` Array r Int e -> Array r Int e
forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
compute (Array r Int e
v Array r Int e -> Matrix r e -> Array r Int e
forall r e.
(Numeric r e, Manifest r e) =>
Vector r e -> Matrix r e -> Vector r e
><! Matrix r e
arr)
    Comp -> Sz Int -> (Int -> e) -> Array r Int e
forall r ix e.
Load r ix e =>
Comp -> Sz ix -> (ix -> e) -> Array r ix e
makeArray Comp
Seq (Int -> Sz Int
forall ix. Index ix => ix -> Sz ix
Sz (Int
m Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)) (Fun Int e -> Int -> e
forall a b. Fun a b -> a -> b
applyFun Fun Int e
f) Array r Int e -> Matrix r e -> IO (Array r Int e)
forall (m :: * -> *) r e.
(MonadThrow m, Numeric r e, Manifest r e) =>
Vector r e -> Matrix r e -> m (Vector r e)
><. Matrix r e
arr IO (Array r Int e) -> Selector SizeException -> Expectation
forall e a.
(HasCallStack, Exception e) =>
IO a -> Selector e -> Expectation
`shouldThrow`
      (SizeException -> Selector SizeException
forall a. Eq a => a -> a -> Bool
== Sz Ix2 -> Sz Ix2 -> SizeException
forall ix. Index ix => Sz ix -> Sz ix -> SizeException
SizeMismatchException (Int -> Int -> Sz Ix2
Sz2 Int
1 (Int
m Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)) (Matrix r e -> Sz Ix2
forall r ix e. Size r => Array r ix e -> Sz ix
size Matrix r e
arr))

prop_DotProduct ::
     forall r e. (Numeric r e, Manifest r e, Eq e, Show e, Load r Ix1 e)
  => Fun e e
  -> Vector r e
  -> Property
prop_DotProduct :: Fun e e -> Vector r e -> Property
prop_DotProduct Fun e e
f Vector r e
v =
  Expectation -> Property
expectProp (Expectation -> Property) -> Expectation -> Property
forall a b. (a -> b) -> a -> b
$ do
    let v' :: Array D Int e
v' = (e -> e) -> Vector r e -> Array D Int e
forall ix r e' e.
(Index ix, Source r e') =>
(e' -> e) -> Array r ix e' -> Array D ix e
A.map (Fun e e -> e -> e
forall a b. Fun a b -> a -> b
applyFun Fun e e
f) Vector r e
v
    Vector r e
v Vector r e -> Vector r e -> e
forall r e.
(Numeric r e, Source r e) =>
Vector r e -> Vector r e -> e
!.! Array D Int e -> Vector r e
forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
compute Array D Int e
v' e -> e -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Array D Int e -> e
forall ix r e. (Index ix, Source r e, Num e) => Array r ix e -> e
A.sum ((e -> e -> e) -> Vector r e -> Array D Int e -> Array D Int e
forall ix r1 e1 r2 e2 e.
(Index ix, Source r1 e1, Source r2 e2) =>
(e1 -> e2 -> e) -> Array r1 ix e1 -> Array r2 ix e2 -> Array D ix e
A.zipWith e -> e -> e
forall a. Num a => a -> a -> a
(*) Vector r e
v Array D Int e
v')
    Vector r e -> Vector r e -> IO e
forall r e (m :: * -> *).
(FoldNumeric r e, Source r e, MonadThrow m) =>
Vector r e -> Vector r e -> m e
dotM Vector r e
v (Comp -> Sz Int -> (Int -> e) -> Vector r e
forall r ix e.
Load r ix e =>
Comp -> Sz ix -> (ix -> e) -> Array r ix e
makeArray Comp
Seq (Vector r e -> Sz Int
forall r ix e. Size r => Array r ix e -> Sz ix
size Vector r e
v Sz Int -> Sz Int -> Sz Int
forall a. Num a => a -> a -> a
+ Sz Int
1) (e -> Int -> e
forall a b. a -> b -> a
const e
0)) IO e -> Selector SizeException -> Expectation
forall e a.
(HasCallStack, Exception e) =>
IO a -> Selector e -> Expectation
`shouldThrow`
      (SizeException -> Selector SizeException
forall a. Eq a => a -> a -> Bool
== Sz Int -> Sz Int -> SizeException
forall ix. Index ix => Sz ix -> Sz ix -> SizeException
SizeMismatchException (Vector r e -> Sz Int
forall r ix e. Size r => Array r ix e -> Sz ix
size Vector r e
v) (Vector r e -> Sz Int
forall r ix e. Size r => Array r ix e -> Sz ix
size Vector r e
v Sz Int -> Sz Int -> Sz Int
forall a. Num a => a -> a -> a
+ Sz Int
1))

prop_Norm ::
     forall r e. (NumericFloat r e, Manifest r e, RealFloat e, Show e)
  => e
  -> Vector r e
  -> Property
prop_Norm :: e -> Vector r e -> Property
prop_Norm e
eps Vector r e
v = e -> e -> e -> Property
forall a. (Show a, RealFloat a) => a -> a -> a -> Property
epsilonEq e
eps (e -> e
forall a. Floating a => a -> a
sqrt (Vector r e
v Vector r e -> Vector r e -> e
forall r e.
(Numeric r e, Source r e) =>
Vector r e -> Vector r e -> e
!.! Vector r e
v)) (Vector r e -> e
forall r e ix.
(FoldNumeric r e, Source r e, Index ix, Floating e) =>
Array r ix e -> e
normL2 Vector r e
v)



prop_Plus ::
     forall r e.
     (Numeric r e, Manifest r e, Show (Matrix r e), Eq (Matrix r e))
  => Fun e e
  -> Matrix r e
  -> e
  -> Property
prop_Plus :: Fun e e -> Matrix r e -> e -> Property
prop_Plus Fun e e
f Matrix r e
arr e
e = Expectation -> Property
expectProp (Expectation -> Property) -> Expectation -> Property
forall a b. (a -> b) -> a -> b
$ do
  Matrix r e
arr Matrix r e -> e -> Matrix r e
forall ix r e.
(Index ix, Numeric r e) =>
Array r ix e -> e -> Array r ix e
.+ e
e Matrix r e -> Matrix r e -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Array D Ix2 e -> Matrix r e
forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
compute ((e -> e) -> Matrix r e -> Array D Ix2 e
forall ix r e' e.
(Index ix, Source r e') =>
(e' -> e) -> Array r ix e' -> Array D ix e
A.map (e -> e -> e
forall a. Num a => a -> a -> a
+ e
e) Matrix r e
arr)
  e
e e -> Matrix r e -> Matrix r e
forall ix r e.
(Index ix, Numeric r e) =>
e -> Array r ix e -> Array r ix e
+. Matrix r e
arr Matrix r e -> Matrix r e -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Matrix r e
arr Matrix r e -> e -> Matrix r e
forall ix r e.
(Index ix, Numeric r e) =>
Array r ix e -> e -> Array r ix e
.+ e
e
  let arr' :: Matrix r e
arr' = Array D Ix2 e -> Matrix r e
forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
compute ((e -> e) -> Matrix r e -> Array D Ix2 e
forall ix r e' e.
(Index ix, Source r e') =>
(e' -> e) -> Array r ix e' -> Array D ix e
A.map (Fun e e -> e -> e
forall a b. Fun a b -> a -> b
applyFun Fun e e
f) Matrix r e
arr)
  Matrix r e
arr Matrix r e -> Matrix r e -> Matrix r e
forall ix r e.
(Index ix, Numeric r e) =>
Array r ix e -> Array r ix e -> Array r ix e
!+! Matrix r e
arr' Matrix r e -> Matrix r e -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Array D Ix2 e -> Matrix r e
forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
compute ((e -> e -> e) -> Matrix r e -> Matrix r e -> Array D Ix2 e
forall ix r1 e1 r2 e2 e.
(Index ix, Source r1 e1, Source r2 e2) =>
(e1 -> e2 -> e) -> Array r1 ix e1 -> Array r2 ix e2 -> Array D ix e
A.zipWith e -> e -> e
forall a. Num a => a -> a -> a
(+) Matrix r e
arr Matrix r e
arr')
  let Sz2 Int
m Int
n = Matrix r e -> Sz Ix2
forall r ix e. Size r => Array r ix e -> Sz ix
size Matrix r e
arr
  Bool -> Expectation -> Expectation
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
m Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
n Bool -> Bool -> Bool
&& Int
m Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
0) (Expectation -> Expectation) -> Expectation -> Expectation
forall a b. (a -> b) -> a -> b
$
    Matrix r e
arr Matrix r e -> Matrix r e -> IO (Matrix r e)
forall ix r e (m :: * -> *).
(Index ix, Numeric r e, MonadThrow m) =>
Array r ix e -> Array r ix e -> m (Array r ix e)
.+. Array D Ix2 e -> Matrix r e
forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
compute (Matrix r e -> Array D Ix2 e
forall r e. Source r e => Matrix r e -> Matrix D e
transpose Matrix r e
arr) IO (Matrix r e) -> Selector SizeException -> Expectation
forall e a.
(HasCallStack, Exception e) =>
IO a -> Selector e -> Expectation
`shouldThrow` (SizeException -> Selector SizeException
forall a. Eq a => a -> a -> Bool
== Sz Ix2 -> Sz Ix2 -> SizeException
forall ix. Index ix => Sz ix -> Sz ix -> SizeException
SizeMismatchException (Matrix r e -> Sz Ix2
forall r ix e. Size r => Array r ix e -> Sz ix
size Matrix r e
arr) (Int -> Int -> Sz Ix2
Sz2 Int
n Int
m))

prop_Minus ::
     forall r e.
     (Numeric r e, Manifest r e, Show (Array r Ix2 e), Eq (Array r Ix2 e))
  => Fun e e
  -> Matrix r e
  -> e
  -> Property
prop_Minus :: Fun e e -> Matrix r e -> e -> Property
prop_Minus Fun e e
f Matrix r e
arr e
e = Expectation -> Property
expectProp (Expectation -> Property) -> Expectation -> Property
forall a b. (a -> b) -> a -> b
$ do
  Matrix r e
arr Matrix r e -> e -> Matrix r e
forall ix r e.
(Index ix, Numeric r e) =>
Array r ix e -> e -> Array r ix e
.- e
e Matrix r e -> Matrix r e -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Array D Ix2 e -> Matrix r e
forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
compute ((e -> e) -> Matrix r e -> Array D Ix2 e
forall ix r e' e.
(Index ix, Source r e') =>
(e' -> e) -> Array r ix e' -> Array D ix e
A.map (e -> e -> e
forall a. Num a => a -> a -> a
subtract e
e) Matrix r e
arr)
  e
e e -> Matrix r e -> Matrix r e
forall ix r e.
(Index ix, Numeric r e) =>
e -> Array r ix e -> Array r ix e
-. Matrix r e
arr Matrix r e -> Matrix r e -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Matrix r e -> Matrix r e
forall ix r e.
(Index ix, Numeric r e) =>
Array r ix e -> Array r ix e
negateA (Matrix r e
arr Matrix r e -> e -> Matrix r e
forall ix r e.
(Index ix, Numeric r e) =>
Array r ix e -> e -> Array r ix e
.- e
e)
  let arr' :: Matrix r e
arr' = Array D Ix2 e -> Matrix r e
forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
compute ((e -> e) -> Matrix r e -> Array D Ix2 e
forall ix r e' e.
(Index ix, Source r e') =>
(e' -> e) -> Array r ix e' -> Array D ix e
A.map (Fun e e -> e -> e
forall a b. Fun a b -> a -> b
applyFun Fun e e
f) Matrix r e
arr)
  Matrix r e
arr Matrix r e -> Matrix r e -> Matrix r e
forall ix r e.
(Index ix, Numeric r e) =>
Array r ix e -> Array r ix e -> Array r ix e
!-! Matrix r e
arr' Matrix r e -> Matrix r e -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Array D Ix2 e -> Matrix r e
forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
compute ((e -> e -> e) -> Matrix r e -> Matrix r e -> Array D Ix2 e
forall ix r1 e1 r2 e2 e.
(Index ix, Source r1 e1, Source r2 e2) =>
(e1 -> e2 -> e) -> Array r1 ix e1 -> Array r2 ix e2 -> Array D ix e
A.zipWith (-) Matrix r e
arr Matrix r e
arr')
  let Sz2 Int
m Int
n = Matrix r e -> Sz Ix2
forall r ix e. Size r => Array r ix e -> Sz ix
size Matrix r e
arr
  Bool -> Expectation -> Expectation
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
m Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
n Bool -> Bool -> Bool
&& Int
m Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
0) (Expectation -> Expectation) -> Expectation -> Expectation
forall a b. (a -> b) -> a -> b
$
    Matrix r e
arr Matrix r e -> Matrix r e -> IO (Matrix r e)
forall ix r e (m :: * -> *).
(Index ix, Numeric r e, MonadThrow m) =>
Array r ix e -> Array r ix e -> m (Array r ix e)
.-. Array D Ix2 e -> Matrix r e
forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
compute (Matrix r e -> Array D Ix2 e
forall r e. Source r e => Matrix r e -> Matrix D e
transpose Matrix r e
arr) IO (Matrix r e) -> Selector SizeException -> Expectation
forall e a.
(HasCallStack, Exception e) =>
IO a -> Selector e -> Expectation
`shouldThrow` (SizeException -> Selector SizeException
forall a. Eq a => a -> a -> Bool
== Sz Ix2 -> Sz Ix2 -> SizeException
forall ix. Index ix => Sz ix -> Sz ix -> SizeException
SizeMismatchException (Matrix r e -> Sz Ix2
forall r ix e. Size r => Array r ix e -> Sz ix
size Matrix r e
arr) (Int -> Int -> Sz Ix2
Sz2 Int
n Int
m))

prop_Times ::
     forall r e.
     (Numeric r e, Manifest r e, Show (Matrix r e), Eq (Matrix r e))
  => Fun e e
  -> Matrix r e
  -> e
  -> Property
prop_Times :: Fun e e -> Matrix r e -> e -> Property
prop_Times Fun e e
f Matrix r e
arr e
e = Expectation -> Property
expectProp (Expectation -> Property) -> Expectation -> Property
forall a b. (a -> b) -> a -> b
$ do
  Matrix r e
arr Matrix r e -> e -> Matrix r e
forall ix r e.
(Index ix, Numeric r e) =>
Array r ix e -> e -> Array r ix e
.* e
e Matrix r e -> Matrix r e -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Array D Ix2 e -> Matrix r e
forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
compute ((e -> e) -> Matrix r e -> Array D Ix2 e
forall ix r e' e.
(Index ix, Source r e') =>
(e' -> e) -> Array r ix e' -> Array D ix e
A.map (e -> e -> e
forall a. Num a => a -> a -> a
* e
e) Matrix r e
arr)
  e
e e -> Matrix r e -> Matrix r e
forall ix r e.
(Index ix, Numeric r e) =>
e -> Array r ix e -> Array r ix e
*. Matrix r e
arr Matrix r e -> Matrix r e -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Matrix r e
arr Matrix r e -> e -> Matrix r e
forall ix r e.
(Index ix, Numeric r e) =>
Array r ix e -> e -> Array r ix e
.* e
e
  let arr' :: Matrix r e
arr' = Array D Ix2 e -> Matrix r e
forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
compute ((e -> e) -> Matrix r e -> Array D Ix2 e
forall ix r e' e.
(Index ix, Source r e') =>
(e' -> e) -> Array r ix e' -> Array D ix e
A.map (Fun e e -> e -> e
forall a b. Fun a b -> a -> b
applyFun Fun e e
f) Matrix r e
arr)
  Matrix r e
arr Matrix r e -> Matrix r e -> Matrix r e
forall ix r e.
(Index ix, Numeric r e) =>
Array r ix e -> Array r ix e -> Array r ix e
!*! Matrix r e
arr' Matrix r e -> Matrix r e -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Array D Ix2 e -> Matrix r e
forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
compute ((e -> e -> e) -> Matrix r e -> Matrix r e -> Array D Ix2 e
forall ix r1 e1 r2 e2 e.
(Index ix, Source r1 e1, Source r2 e2) =>
(e1 -> e2 -> e) -> Array r1 ix e1 -> Array r2 ix e2 -> Array D ix e
A.zipWith e -> e -> e
forall a. Num a => a -> a -> a
(*) Matrix r e
arr Matrix r e
arr')
  let Sz2 Int
m Int
n = Matrix r e -> Sz Ix2
forall r ix e. Size r => Array r ix e -> Sz ix
size Matrix r e
arr
  Bool -> Expectation -> Expectation
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
m Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
n Bool -> Bool -> Bool
&& Int
m Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
0) (Expectation -> Expectation) -> Expectation -> Expectation
forall a b. (a -> b) -> a -> b
$
    Matrix r e
arr Matrix r e -> Matrix r e -> IO (Matrix r e)
forall ix r e (m :: * -> *).
(Index ix, Numeric r e, MonadThrow m) =>
Array r ix e -> Array r ix e -> m (Array r ix e)
.*. Array D Ix2 e -> Matrix r e
forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
compute (Matrix r e -> Array D Ix2 e
forall r e. Source r e => Matrix r e -> Matrix D e
transpose Matrix r e
arr) IO (Matrix r e) -> Selector SizeException -> Expectation
forall e a.
(HasCallStack, Exception e) =>
IO a -> Selector e -> Expectation
`shouldThrow` (SizeException -> Selector SizeException
forall a. Eq a => a -> a -> Bool
== Sz Ix2 -> Sz Ix2 -> SizeException
forall ix. Index ix => Sz ix -> Sz ix -> SizeException
SizeMismatchException (Matrix r e -> Sz Ix2
forall r ix e. Size r => Array r ix e -> Sz ix
size Matrix r e
arr) (Int -> Int -> Sz Ix2
Sz2 Int
n Int
m))

prop_Divide ::
     forall r e.
     ( NumericFloat r e
     , Manifest r e
     , Show e
     , RealFloat e
     , Show (Matrix r e)
     , Eq (Matrix r e)
     )
  => e -- ^ Epsilon
  -> Fun e e
  -> Matrix r e
  -> e
  -> Property
prop_Divide :: e -> Fun e e -> Matrix r e -> e -> Property
prop_Divide e
eps Fun e e
f Matrix r e
arr e
e = e
e e -> e -> Bool
forall a. Eq a => a -> a -> Bool
/= e
0 Bool -> Property -> Property
forall prop. Testable prop => Bool -> prop -> Property
==> Expectation -> Property
expectProp (Expectation -> Property) -> Expectation -> Property
forall a b. (a -> b) -> a -> b
$ do
  Matrix r e
arr Matrix r e -> e -> Matrix r e
forall ix r e.
(Index ix, NumericFloat r e) =>
Array r ix e -> e -> Array r ix e
./ e
e Matrix r e -> Matrix r e -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Array D Ix2 e -> Matrix r e
forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
compute ((e -> e) -> Matrix r e -> Array D Ix2 e
forall ix r e' e.
(Index ix, Source r e') =>
(e' -> e) -> Array r ix e' -> Array D ix e
A.map (e -> e -> e
forall a. Fractional a => a -> a -> a
/ e
e) Matrix r e
arr)
  e -> Array D Ix2 e -> Array D Ix2 e -> Expectation
forall (f :: * -> *) e.
(HasCallStack, Foldable f, Show (f e), Show e, RealFloat e) =>
e -> f e -> f e -> Expectation
epsilonFoldableExpect e
eps (Matrix r e -> Array D Ix2 e
forall ix r e.
(Index ix, Source r e) =>
Array r ix e -> Array D ix e
delay (e
e e -> Matrix r e -> Matrix r e
forall ix r e.
(Index ix, NumericFloat r e) =>
e -> Array r ix e -> Array r ix e
/. Matrix r e
arr)) (Matrix r e -> Array D Ix2 e
forall ix r e.
(Index ix, Source r e) =>
Array r ix e -> Array D ix e
delay (e
e e -> Matrix r e -> Matrix r e
forall ix r e.
(Index ix, Numeric r e) =>
e -> Array r ix e -> Array r ix e
*. Matrix r e -> Matrix r e
forall ix r e.
(Index ix, NumericFloat r e) =>
Array r ix e -> Array r ix e
recipA Matrix r e
arr))
  let arr' :: Matrix r e
arr' = Array D Ix2 e -> Matrix r e
forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
compute ((e -> e) -> Matrix r e -> Array D Ix2 e
forall ix r e' e.
(Index ix, Source r e') =>
(e' -> e) -> Array r ix e' -> Array D ix e
A.map (Fun e e -> e -> e
forall a b. Fun a b -> a -> b
applyFun Fun e e
f) Matrix r e
arr)
  Bool -> Expectation -> Expectation
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Array D Ix2 Bool -> Bool
forall ix r. (Index ix, Source r Bool) => Array r ix Bool -> Bool
A.or ((e -> e -> Bool) -> Matrix r e -> Matrix r e -> Array D Ix2 Bool
forall ix r1 e1 r2 e2 e.
(Index ix, Source r1 e1, Source r2 e2) =>
(e1 -> e2 -> e) -> Array r1 ix e1 -> Array r2 ix e2 -> Array D ix e
A.zipWith (\e
x e
y -> e
x e -> e -> Bool
forall a. Eq a => a -> a -> Bool
== e
0 Bool -> Bool -> Bool
&& e
y e -> e -> Bool
forall a. Eq a => a -> a -> Bool
== e
0) Matrix r e
arr Matrix r e
arr')) (Expectation -> Expectation) -> Expectation -> Expectation
forall a b. (a -> b) -> a -> b
$
    Matrix r e
arr Matrix r e -> Matrix r e -> Matrix r e
forall ix r e.
(Index ix, NumericFloat r e) =>
Array r ix e -> Array r ix e -> Array r ix e
!/! Matrix r e
arr' Matrix r e -> Matrix r e -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Array D Ix2 e -> Matrix r e
forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
compute ((e -> e -> e) -> Matrix r e -> Matrix r e -> Array D Ix2 e
forall ix r1 e1 r2 e2 e.
(Index ix, Source r1 e1, Source r2 e2) =>
(e1 -> e2 -> e) -> Array r1 ix e1 -> Array r2 ix e2 -> Array D ix e
A.zipWith e -> e -> e
forall a. Fractional a => a -> a -> a
(/) Matrix r e
arr Matrix r e
arr')
  let Sz2 Int
m Int
n = Matrix r e -> Sz Ix2
forall r ix e. Size r => Array r ix e -> Sz ix
size Matrix r e
arr
  Bool -> Expectation -> Expectation
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
m Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
n Bool -> Bool -> Bool
&& Int
m Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
0) (Expectation -> Expectation) -> Expectation -> Expectation
forall a b. (a -> b) -> a -> b
$
    Matrix r e
arr Matrix r e -> Matrix r e -> IO (Matrix r e)
forall ix r e (m :: * -> *).
(Index ix, NumericFloat r e, MonadThrow m) =>
Array r ix e -> Array r ix e -> m (Array r ix e)
./. Array D Ix2 e -> Matrix r e
forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
compute (Matrix r e -> Array D Ix2 e
forall r e. Source r e => Matrix r e -> Matrix D e
transpose Matrix r e
arr) IO (Matrix r e) -> Selector SizeException -> Expectation
forall e a.
(HasCallStack, Exception e) =>
IO a -> Selector e -> Expectation
`shouldThrow` (SizeException -> Selector SizeException
forall a. Eq a => a -> a -> Bool
== Sz Ix2 -> Sz Ix2 -> SizeException
forall ix. Index ix => Sz ix -> Sz ix -> SizeException
SizeMismatchException (Matrix r e -> Sz Ix2
forall r ix e. Size r => Array r ix e -> Sz ix
size Matrix r e
arr) (Int -> Int -> Sz Ix2
Sz2 Int
n Int
m))

prop_Floating ::
     forall r e. (RealFloat e, Source r e, NumericFloat r e, Show e)
  => e
  -> Matrix r e
  -> Property
prop_Floating :: e -> Matrix r e -> Property
prop_Floating e
eps Matrix r e
arr = Expectation -> Property
expectProp (Expectation -> Property) -> Expectation -> Property
forall a b. (a -> b) -> a -> b
$ do
  e -> Array D Ix2 e -> Array D Ix2 e -> Expectation
forall (f :: * -> *) e.
(HasCallStack, Foldable f, Show (f e), Show e, RealFloat e) =>
e -> f e -> f e -> Expectation
epsilonFoldableExpect e
eps (Matrix r e -> Array D Ix2 e
forall ix r e.
(Index ix, Source r e) =>
Array r ix e -> Array D ix e
delay (Matrix r e -> Matrix r e
forall ix r e.
(Index ix, Numeric r e) =>
Array r ix e -> Array r ix e
absA Matrix r e
arr)) ((e -> e) -> Matrix r e -> Array D Ix2 e
forall ix r e' e.
(Index ix, Source r e') =>
(e' -> e) -> Array r ix e' -> Array D ix e
A.map e -> e
forall a. Num a => a -> a
abs Matrix r e
arr)
  e -> Array D Ix2 e -> Array D Ix2 e -> Expectation
forall (f :: * -> *) e.
(HasCallStack, Foldable f, Show (f e), Show e, RealFloat e) =>
e -> f e -> f e -> Expectation
epsilonFoldableExpect e
eps (Matrix r e -> Array D Ix2 e
forall ix r e.
(Index ix, Source r e) =>
Array r ix e -> Array D ix e
delay (Matrix r e -> Matrix r e
forall ix r e.
(Index ix, Numeric r e) =>
Array r ix e -> Array r ix e
signumA Matrix r e
arr)) ((e -> e) -> Matrix r e -> Array D Ix2 e
forall ix r e' e.
(Index ix, Source r e') =>
(e' -> e) -> Array r ix e' -> Array D ix e
A.map e -> e
forall a. Num a => a -> a
signum Matrix r e
arr)
  e -> Array D Ix2 e -> Array D Ix2 e -> Expectation
forall (f :: * -> *) e.
(HasCallStack, Foldable f, Show (f e), Show e, RealFloat e) =>
e -> f e -> f e -> Expectation
epsilonFoldableExpect e
eps (Matrix r e -> Array D Ix2 e
forall ix r e.
(Index ix, Source r e) =>
Array r ix e -> Array D ix e
delay (Matrix r e -> Matrix r e
forall ix r e.
(Index ix, NumericFloat r e) =>
Array r ix e -> Array r ix e
recipA Matrix r e
arr)) ((e -> e) -> Matrix r e -> Array D Ix2 e
forall ix r e' e.
(Index ix, Source r e') =>
(e' -> e) -> Array r ix e' -> Array D ix e
A.map e -> e
forall a. Fractional a => a -> a
recip Matrix r e
arr)
  e -> Array D Ix2 e -> Array D Ix2 e -> Expectation
forall (f :: * -> *) e.
(HasCallStack, Foldable f, Show (f e), Show e, RealFloat e) =>
e -> f e -> f e -> Expectation
epsilonFoldableExpect e
eps (Matrix r e -> Array D Ix2 e
forall ix r e.
(Index ix, Source r e) =>
Array r ix e -> Array D ix e
delay (Matrix r e -> Matrix r e
forall ix r e.
(Index ix, NumericFloat r e) =>
Array r ix e -> Array r ix e
expA Matrix r e
arr)) ((e -> e) -> Matrix r e -> Array D Ix2 e
forall ix r e' e.
(Index ix, Source r e') =>
(e' -> e) -> Array r ix e' -> Array D ix e
A.map e -> e
forall a. Floating a => a -> a
exp Matrix r e
arr)
  e -> Array D Ix2 e -> Array D Ix2 e -> Expectation
forall (f :: * -> *) e.
(HasCallStack, Foldable f, Show (f e), Show e, RealFloat e) =>
e -> f e -> f e -> Expectation
epsilonFoldableExpect e
eps (Matrix r e -> Array D Ix2 e
forall ix r e.
(Index ix, Source r e) =>
Array r ix e -> Array D ix e
delay (Matrix r e -> Matrix r e
forall ix r e.
(Index ix, NumericFloat r e) =>
Array r ix e -> Array r ix e
sqrtA Matrix r e
arr)) ((e -> e) -> Matrix r e -> Array D Ix2 e
forall ix r e' e.
(Index ix, Source r e') =>
(e' -> e) -> Array r ix e' -> Array D ix e
A.map e -> e
forall a. Floating a => a -> a
sqrt Matrix r e
arr)
  e -> Array D Ix2 e -> Array D Ix2 e -> Expectation
forall (f :: * -> *) e.
(HasCallStack, Foldable f, Show (f e), Show e, RealFloat e) =>
e -> f e -> f e -> Expectation
epsilonFoldableExpect e
eps (Matrix r e -> Array D Ix2 e
forall ix r e.
(Index ix, Source r e) =>
Array r ix e -> Array D ix e
delay (Matrix r e -> Matrix r e
forall ix r e.
(Index ix, NumericFloat r e) =>
Array r ix e -> Array r ix e
logA Matrix r e
arr)) ((e -> e) -> Matrix r e -> Array D Ix2 e
forall ix r e' e.
(Index ix, Source r e') =>
(e' -> e) -> Array r ix e' -> Array D ix e
A.map e -> e
forall a. Floating a => a -> a
log Matrix r e
arr)
  e -> Array D Ix2 e -> Array D Ix2 e -> Expectation
forall (f :: * -> *) e.
(HasCallStack, Foldable f, Show (f e), Show e, RealFloat e) =>
e -> f e -> f e -> Expectation
epsilonFoldableExpect e
eps (Matrix r e -> Array D Ix2 e
forall ix r e.
(Index ix, Source r e) =>
Array r ix e -> Array D ix e
delay (Matrix r e -> Matrix r e
forall ix r e.
(Index ix, NumericFloat r e) =>
Array r ix e -> Array r ix e
sinA Matrix r e
arr)) ((e -> e) -> Matrix r e -> Array D Ix2 e
forall ix r e' e.
(Index ix, Source r e') =>
(e' -> e) -> Array r ix e' -> Array D ix e
A.map e -> e
forall a. Floating a => a -> a
sin Matrix r e
arr)
  e -> Array D Ix2 e -> Array D Ix2 e -> Expectation
forall (f :: * -> *) e.
(HasCallStack, Foldable f, Show (f e), Show e, RealFloat e) =>
e -> f e -> f e -> Expectation
epsilonFoldableExpect e
eps (Matrix r e -> Array D Ix2 e
forall ix r e.
(Index ix, Source r e) =>
Array r ix e -> Array D ix e
delay (Matrix r e -> Matrix r e
forall ix r e.
(Index ix, NumericFloat r e) =>
Array r ix e -> Array r ix e
cosA Matrix r e
arr)) ((e -> e) -> Matrix r e -> Array D Ix2 e
forall ix r e' e.
(Index ix, Source r e') =>
(e' -> e) -> Array r ix e' -> Array D ix e
A.map e -> e
forall a. Floating a => a -> a
cos Matrix r e
arr)
  e -> Array D Ix2 e -> Array D Ix2 e -> Expectation
forall (f :: * -> *) e.
(HasCallStack, Foldable f, Show (f e), Show e, RealFloat e) =>
e -> f e -> f e -> Expectation
epsilonFoldableExpect e
eps (Matrix r e -> Array D Ix2 e
forall ix r e.
(Index ix, Source r e) =>
Array r ix e -> Array D ix e
delay (Matrix r e -> Matrix r e
forall ix r e.
(Index ix, NumericFloat r e) =>
Array r ix e -> Array r ix e
tanA Matrix r e
arr)) ((e -> e) -> Matrix r e -> Array D Ix2 e
forall ix r e' e.
(Index ix, Source r e') =>
(e' -> e) -> Array r ix e' -> Array D ix e
A.map e -> e
forall a. Floating a => a -> a
tan Matrix r e
arr)
  e -> Array D Ix2 e -> Array D Ix2 e -> Expectation
forall (f :: * -> *) e.
(HasCallStack, Foldable f, Show (f e), Show e, RealFloat e) =>
e -> f e -> f e -> Expectation
epsilonFoldableExpect e
eps (Matrix r e -> Array D Ix2 e
forall ix r e.
(Index ix, Source r e) =>
Array r ix e -> Array D ix e
delay (Matrix r e -> Matrix r e
forall ix r e.
(Index ix, NumericFloat r e) =>
Array r ix e -> Array r ix e
asinA Matrix r e
arr)) ((e -> e) -> Matrix r e -> Array D Ix2 e
forall ix r e' e.
(Index ix, Source r e') =>
(e' -> e) -> Array r ix e' -> Array D ix e
A.map e -> e
forall a. Floating a => a -> a
asin Matrix r e
arr)
  e -> Array D Ix2 e -> Array D Ix2 e -> Expectation
forall (f :: * -> *) e.
(HasCallStack, Foldable f, Show (f e), Show e, RealFloat e) =>
e -> f e -> f e -> Expectation
epsilonFoldableExpect e
eps (Matrix r e -> Array D Ix2 e
forall ix r e.
(Index ix, Source r e) =>
Array r ix e -> Array D ix e
delay (Matrix r e -> Matrix r e
forall ix r e.
(Index ix, NumericFloat r e) =>
Array r ix e -> Array r ix e
acosA Matrix r e
arr)) ((e -> e) -> Matrix r e -> Array D Ix2 e
forall ix r e' e.
(Index ix, Source r e') =>
(e' -> e) -> Array r ix e' -> Array D ix e
A.map e -> e
forall a. Floating a => a -> a
acos Matrix r e
arr)
  e -> Array D Ix2 e -> Array D Ix2 e -> Expectation
forall (f :: * -> *) e.
(HasCallStack, Foldable f, Show (f e), Show e, RealFloat e) =>
e -> f e -> f e -> Expectation
epsilonFoldableExpect e
eps (Matrix r e -> Array D Ix2 e
forall ix r e.
(Index ix, Source r e) =>
Array r ix e -> Array D ix e
delay (Matrix r e -> Matrix r e
forall ix r e.
(Index ix, NumericFloat r e) =>
Array r ix e -> Array r ix e
atanA Matrix r e
arr)) ((e -> e) -> Matrix r e -> Array D Ix2 e
forall ix r e' e.
(Index ix, Source r e') =>
(e' -> e) -> Array r ix e' -> Array D ix e
A.map e -> e
forall a. Floating a => a -> a
atan Matrix r e
arr)
  e -> Array D Ix2 e -> Array D Ix2 e -> Expectation
forall (f :: * -> *) e.
(HasCallStack, Foldable f, Show (f e), Show e, RealFloat e) =>
e -> f e -> f e -> Expectation
epsilonFoldableExpect e
eps (Matrix r e -> Array D Ix2 e
forall ix r e.
(Index ix, Source r e) =>
Array r ix e -> Array D ix e
delay (Matrix r e -> Matrix r e
forall ix r e.
(Index ix, NumericFloat r e) =>
Array r ix e -> Array r ix e
sinhA Matrix r e
arr)) ((e -> e) -> Matrix r e -> Array D Ix2 e
forall ix r e' e.
(Index ix, Source r e') =>
(e' -> e) -> Array r ix e' -> Array D ix e
A.map e -> e
forall a. Floating a => a -> a
sinh Matrix r e
arr)
  e -> Array D Ix2 e -> Array D Ix2 e -> Expectation
forall (f :: * -> *) e.
(HasCallStack, Foldable f, Show (f e), Show e, RealFloat e) =>
e -> f e -> f e -> Expectation
epsilonFoldableExpect e
eps (Matrix r e -> Array D Ix2 e
forall ix r e.
(Index ix, Source r e) =>
Array r ix e -> Array D ix e
delay (Matrix r e -> Matrix r e
forall ix r e.
(Index ix, NumericFloat r e) =>
Array r ix e -> Array r ix e
coshA Matrix r e
arr)) ((e -> e) -> Matrix r e -> Array D Ix2 e
forall ix r e' e.
(Index ix, Source r e') =>
(e' -> e) -> Array r ix e' -> Array D ix e
A.map e -> e
forall a. Floating a => a -> a
cosh Matrix r e
arr)
  e -> Array D Ix2 e -> Array D Ix2 e -> Expectation
forall (f :: * -> *) e.
(HasCallStack, Foldable f, Show (f e), Show e, RealFloat e) =>
e -> f e -> f e -> Expectation
epsilonFoldableExpect e
eps (Matrix r e -> Array D Ix2 e
forall ix r e.
(Index ix, Source r e) =>
Array r ix e -> Array D ix e
delay (Matrix r e -> Matrix r e
forall ix r e.
(Index ix, NumericFloat r e) =>
Array r ix e -> Array r ix e
tanhA Matrix r e
arr)) ((e -> e) -> Matrix r e -> Array D Ix2 e
forall ix r e' e.
(Index ix, Source r e') =>
(e' -> e) -> Array r ix e' -> Array D ix e
A.map e -> e
forall a. Floating a => a -> a
tanh Matrix r e
arr)
  e -> Array D Ix2 e -> Array D Ix2 e -> Expectation
forall (f :: * -> *) e.
(HasCallStack, Foldable f, Show (f e), Show e, RealFloat e) =>
e -> f e -> f e -> Expectation
epsilonFoldableExpect e
eps (Matrix r e -> Array D Ix2 e
forall ix r e.
(Index ix, Source r e) =>
Array r ix e -> Array D ix e
delay (Matrix r e -> Matrix r e
forall ix r e.
(Index ix, NumericFloat r e) =>
Array r ix e -> Array r ix e
asinhA Matrix r e
arr)) ((e -> e) -> Matrix r e -> Array D Ix2 e
forall ix r e' e.
(Index ix, Source r e') =>
(e' -> e) -> Array r ix e' -> Array D ix e
A.map e -> e
forall a. Floating a => a -> a
asinh Matrix r e
arr)
  e -> Array D Ix2 e -> Array D Ix2 e -> Expectation
forall (f :: * -> *) e.
(HasCallStack, Foldable f, Show (f e), Show e, RealFloat e) =>
e -> f e -> f e -> Expectation
epsilonFoldableExpect e
eps (Matrix r e -> Array D Ix2 e
forall ix r e.
(Index ix, Source r e) =>
Array r ix e -> Array D ix e
delay (Matrix r e -> Matrix r e
forall ix r e.
(Index ix, NumericFloat r e) =>
Array r ix e -> Array r ix e
acoshA Matrix r e
arr)) ((e -> e) -> Matrix r e -> Array D Ix2 e
forall ix r e' e.
(Index ix, Source r e') =>
(e' -> e) -> Array r ix e' -> Array D ix e
A.map e -> e
forall a. Floating a => a -> a
acosh Matrix r e
arr)
  e -> Array D Ix2 e -> Array D Ix2 e -> Expectation
forall (f :: * -> *) e.
(HasCallStack, Foldable f, Show (f e), Show e, RealFloat e) =>
e -> f e -> f e -> Expectation
epsilonFoldableExpect e
eps (Matrix r e -> Array D Ix2 e
forall ix r e.
(Index ix, Source r e) =>
Array r ix e -> Array D ix e
delay (Matrix r e -> Matrix r e
forall ix r e.
(Index ix, NumericFloat r e) =>
Array r ix e -> Array r ix e
atanhA Matrix r e
arr)) ((e -> e) -> Matrix r e -> Array D Ix2 e
forall ix r e' e.
(Index ix, Source r e') =>
(e' -> e) -> Array r ix e' -> Array D ix e
A.map e -> e
forall a. Floating a => a -> a
atanh Matrix r e
arr)

prop_Floating2 ::
     forall r e. (RealFloat e, Manifest r e, NumericFloat r e, Show e)
  => e
  -> Matrix r e
  -> Fun e e
  -> Property
prop_Floating2 :: e -> Matrix r e -> Fun e e -> Property
prop_Floating2 e
eps Matrix r e
arr1 Fun e e
f = Expectation -> Property
expectProp (Expectation -> Property) -> Expectation -> Property
forall a b. (a -> b) -> a -> b
$ do
  let arr2 :: Matrix r e
arr2 = Array D Ix2 e -> Matrix r e
forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
compute ((e -> e) -> Matrix r e -> Array D Ix2 e
forall ix r e' e.
(Index ix, Source r e') =>
(e' -> e) -> Array r ix e' -> Array D ix e
A.map (Fun e e -> e -> e
forall a b. Fun a b -> a -> b
applyFun Fun e e
f) Matrix r e
arr1)
  e -> Array D Ix2 e -> Array D Ix2 e -> Expectation
forall (f :: * -> *) e.
(HasCallStack, Foldable f, Show (f e), Show e, RealFloat e) =>
e -> f e -> f e -> Expectation
epsilonFoldableExpect e
eps (Array D Ix2 e -> Array D Ix2 e
forall ix r e.
(Index ix, Source r e) =>
Array r ix e -> Array D ix e
delay (Matrix r e -> Matrix r e -> Array D Ix2 e
forall ix r1 e r2.
(Index ix, Source r1 e, Source r2 e, Floating e) =>
Array r1 ix e -> Array r2 ix e -> Array D ix e
logBaseA Matrix r e
arr1 Matrix r e
arr2)) ((e -> e -> e) -> Matrix r e -> Matrix r e -> Array D Ix2 e
forall ix r1 e1 r2 e2 e.
(Index ix, Source r1 e1, Source r2 e2) =>
(e1 -> e2 -> e) -> Array r1 ix e1 -> Array r2 ix e2 -> Array D ix e
A.zipWith e -> e -> e
forall a. Floating a => a -> a -> a
logBase Matrix r e
arr1 Matrix r e
arr2)
  e -> Array D Ix2 e -> Array D Ix2 e -> Expectation
forall (f :: * -> *) e.
(HasCallStack, Foldable f, Show (f e), Show e, RealFloat e) =>
e -> f e -> f e -> Expectation
epsilonFoldableExpect e
eps (Array D Ix2 e -> Array D Ix2 e
forall ix r e.
(Index ix, Source r e) =>
Array r ix e -> Array D ix e
delay (Matrix r e
arr1 Matrix r e -> Matrix r e -> Array D Ix2 e
forall ix r1 e r2.
(Index ix, Source r1 e, Source r2 e, Floating e) =>
Array r1 ix e -> Array r2 ix e -> Array D ix e
.** Matrix r e
arr2)) ((e -> e -> e) -> Matrix r e -> Matrix r e -> Array D Ix2 e
forall ix r1 e1 r2 e2 e.
(Index ix, Source r1 e1, Source r2 e2) =>
(e1 -> e2 -> e) -> Array r1 ix e1 -> Array r2 ix e2 -> Array D ix e
A.zipWith e -> e -> e
forall a. Floating a => a -> a -> a
(**) Matrix r e
arr1 Matrix r e
arr2)
  Matrix r e
res <- Matrix r e -> Matrix r e -> IO (Matrix r e)
forall ix r e (m :: * -> *).
(Index ix, Numeric r e, RealFloat e, MonadThrow m) =>
Array r ix e -> Array r ix e -> m (Array r ix e)
atan2A Matrix r e
arr1 Matrix r e
arr2
  e -> Array D Ix2 e -> Array D Ix2 e -> Expectation
forall (f :: * -> *) e.
(HasCallStack, Foldable f, Show (f e), Show e, RealFloat e) =>
e -> f e -> f e -> Expectation
epsilonFoldableExpect e
eps (Matrix r e -> Array D Ix2 e
forall ix r e.
(Index ix, Source r e) =>
Array r ix e -> Array D ix e
delay Matrix r e
res) ((e -> e -> e) -> Matrix r e -> Matrix r e -> Array D Ix2 e
forall ix r1 e1 r2 e2 e.
(Index ix, Source r1 e1, Source r2 e2) =>
(e1 -> e2 -> e) -> Array r1 ix e1 -> Array r2 ix e2 -> Array D ix e
A.zipWith e -> e -> e
forall a. RealFloat a => a -> a -> a
atan2 Matrix r e
arr1 Matrix r e
arr2)


mutableNumericSpec ::
     forall r e.
     ( Numeric r e
     , Manifest r e
     , Load r Ix1 e
     , Load r Ix2 e
     , Eq e
     , Show e
     , Function e
     , CoArbitrary e
     , Arbitrary e
     , Arbitrary (Matrix r e)
     , Arbitrary (Vector r e)
     , Show (Matrix r e)
     , Eq (Matrix r e)
     , Show (Vector r e)
     , Eq (Vector r e)
     )
  => Spec
mutableNumericSpec :: Spec
mutableNumericSpec =
  [Char] -> Spec -> Spec
forall a. HasCallStack => [Char] -> SpecWith a -> SpecWith a
describe [Char]
"Numeric Operations" (Spec -> Spec) -> Spec -> Spec
forall a b. (a -> b) -> a -> b
$ do
    [Char] -> (Fun e e -> Matrix r e -> e -> Property) -> Spec
forall prop.
(HasCallStack, Testable prop) =>
[Char] -> prop -> Spec
prop [Char]
"Plus" ((Fun e e -> Matrix r e -> e -> Property) -> Spec)
-> (Fun e e -> Matrix r e -> e -> Property) -> Spec
forall a b. (a -> b) -> a -> b
$ (Numeric r e, Manifest r e, Show (Matrix r e), Eq (Matrix r e)) =>
Fun e e -> Matrix r e -> e -> Property
forall r e.
(Numeric r e, Manifest r e, Show (Matrix r e), Eq (Matrix r e)) =>
Fun e e -> Matrix r e -> e -> Property
prop_Plus @r @e
    [Char] -> (Fun e e -> Matrix r e -> e -> Property) -> Spec
forall prop.
(HasCallStack, Testable prop) =>
[Char] -> prop -> Spec
prop [Char]
"Minus" ((Fun e e -> Matrix r e -> e -> Property) -> Spec)
-> (Fun e e -> Matrix r e -> e -> Property) -> Spec
forall a b. (a -> b) -> a -> b
$ (Numeric r e, Manifest r e, Show (Matrix r e), Eq (Matrix r e)) =>
Fun e e -> Matrix r e -> e -> Property
forall r e.
(Numeric r e, Manifest r e, Show (Matrix r e), Eq (Matrix r e)) =>
Fun e e -> Matrix r e -> e -> Property
prop_Minus @r @e
    [Char] -> (Fun e e -> Matrix r e -> e -> Property) -> Spec
forall prop.
(HasCallStack, Testable prop) =>
[Char] -> prop -> Spec
prop [Char]
"Times" ((Fun e e -> Matrix r e -> e -> Property) -> Spec)
-> (Fun e e -> Matrix r e -> e -> Property) -> Spec
forall a b. (a -> b) -> a -> b
$ (Numeric r e, Manifest r e, Show (Matrix r e), Eq (Matrix r e)) =>
Fun e e -> Matrix r e -> e -> Property
forall r e.
(Numeric r e, Manifest r e, Show (Matrix r e), Eq (Matrix r e)) =>
Fun e e -> Matrix r e -> e -> Property
prop_Times @r @e
    [Char] -> (Fun e e -> Vector r e -> Property) -> Spec
forall prop.
(HasCallStack, Testable prop) =>
[Char] -> prop -> Spec
prop [Char]
"DotProduct" ((Fun e e -> Vector r e -> Property) -> Spec)
-> (Fun e e -> Vector r e -> Property) -> Spec
forall a b. (a -> b) -> a -> b
$ (Numeric r e, Manifest r e, Eq e, Show e, Load r Int e) =>
Fun e e -> Vector r e -> Property
forall r e.
(Numeric r e, Manifest r e, Eq e, Show e, Load r Int e) =>
Fun e e -> Vector r e -> Property
prop_DotProduct @r @e
    [Char] -> (Matrix r e -> NonNegative Int -> Property) -> Spec
forall prop.
(HasCallStack, Testable prop) =>
[Char] -> prop -> Spec
prop [Char]
"Power" ((Matrix r e -> NonNegative Int -> Property) -> Spec)
-> (Matrix r e -> NonNegative Int -> Property) -> Spec
forall a b. (a -> b) -> a -> b
$ \(Matrix r e
arr :: Array r Ix2 e) (NonNegative Int
p) -> Expectation -> Property
expectProp (Expectation -> Property) -> Expectation -> Property
forall a b. (a -> b) -> a -> b
$
      Matrix r e
arr Matrix r e -> Int -> Matrix r e
forall ix r e.
(Index ix, Numeric r e) =>
Array r ix e -> Int -> Array r ix e
.^ Int
p Matrix r e -> Matrix r e -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Array D Ix2 e -> Matrix r e
forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
compute ((e -> e) -> Matrix r e -> Array D Ix2 e
forall ix r e' e.
(Index ix, Source r e') =>
(e' -> e) -> Array r ix e' -> Array D ix e
A.map (e -> Int -> e
forall a b. (Num a, Integral b) => a -> b -> a
^ Int
p) Matrix r e
arr)
    [Char] -> (Fun e e -> Matrix r e -> Property) -> Spec
forall prop.
(HasCallStack, Testable prop) =>
[Char] -> prop -> Spec
prop [Char]
"MatrixMatrixMultiply" ((Fun e e -> Matrix r e -> Property) -> Spec)
-> (Fun e e -> Matrix r e -> Property) -> Spec
forall a b. (a -> b) -> a -> b
$ (Numeric r e, Manifest r e, Eq (Matrix r e), Show (Matrix r e)) =>
Fun e e -> Matrix r e -> Property
forall r e.
(Numeric r e, Manifest r e, Eq (Matrix r e), Show (Matrix r e)) =>
Fun e e -> Matrix r e -> Property
prop_MatrixMatrixMultiply @r @e
    [Char] -> (Fun Int e -> Matrix r e -> Property) -> Spec
forall prop.
(HasCallStack, Testable prop) =>
[Char] -> prop -> Spec
prop [Char]
"MatrixVectorMultiply" ((Fun Int e -> Matrix r e -> Property) -> Spec)
-> (Fun Int e -> Matrix r e -> Property) -> Spec
forall a b. (a -> b) -> a -> b
$ (Numeric r e, Manifest r e, Load r Int e, Eq e, Show e) =>
Fun Int e -> Matrix r e -> Property
forall r e.
(Numeric r e, Manifest r e, Load r Int e, Eq e, Show e) =>
Fun Int e -> Matrix r e -> Property
prop_MatrixVectorMultiply @r @e
    [Char] -> (Fun Int e -> Matrix r e -> Property) -> Spec
forall prop.
(HasCallStack, Testable prop) =>
[Char] -> prop -> Spec
prop [Char]
"VectorMatrixMultiply" ((Fun Int e -> Matrix r e -> Property) -> Spec)
-> (Fun Int e -> Matrix r e -> Property) -> Spec
forall a b. (a -> b) -> a -> b
$ (Numeric r e, Load r Int e, Manifest r e, Show (Vector r e),
 Eq (Vector r e)) =>
Fun Int e -> Matrix r e -> Property
forall r e.
(Numeric r e, Load r Int e, Manifest r e, Show (Vector r e),
 Eq (Vector r e)) =>
Fun Int e -> Matrix r e -> Property
prop_VectorMatrixMultiply @r @e
    [Char] -> (Int -> Property) -> Spec
forall prop.
(HasCallStack, Testable prop) =>
[Char] -> prop -> Spec
prop [Char]
"Identity" ((Int -> Property) -> Spec) -> (Int -> Property) -> Spec
forall a b. (a -> b) -> a -> b
$ \ Int
n -> Expectation -> Property
expectProp (Expectation -> Property) -> Expectation -> Property
forall a b. (a -> b) -> a -> b
$ do
      Array DL Ix2 e -> IO (Matrix r e)
forall r ix e r' (m :: * -> *).
(Manifest r e, Load r' ix e, MonadIO m) =>
Array r' ix e -> m (Array r ix e)
computeIO (Sz Int -> Array DL Ix2 e
forall e. Num e => Sz Int -> Matrix DL e
identityMatrix (Int -> Sz Int
forall ix. Index ix => ix -> Sz ix
Sz Int
n)) IO (Matrix r e) -> Matrix r e -> Expectation
forall a. (HasCallStack, Show a, Eq a) => IO a -> a -> Expectation
`shouldReturn`
        Comp -> Sz Ix2 -> (Ix2 -> e) -> Matrix r e
forall r ix e.
Load r ix e =>
Comp -> Sz ix -> (ix -> e) -> Array r ix e
makeArray @r Comp
Seq (Int -> Int -> Sz Ix2
Sz2 Int
n Int
n) (\ (Int
i :. Int
j) -> if Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
j then e
1 else e
0 :: e)
    [Char] -> (Comp -> Int -> Fun Ix2T e -> Property) -> Spec
forall prop.
(HasCallStack, Testable prop) =>
[Char] -> prop -> Spec
prop [Char]
"LowerTriangular" ((Comp -> Int -> Fun Ix2T e -> Property) -> Spec)
-> (Comp -> Int -> Fun Ix2T e -> Property) -> Spec
forall a b. (a -> b) -> a -> b
$ \ Comp
comp Int
n Fun Ix2T e
f -> Expectation -> Property
expectProp (Expectation -> Property) -> Expectation -> Property
forall a b. (a -> b) -> a -> b
$ do
      Array DL Ix2 e -> IO (Matrix r e)
forall r ix e r' (m :: * -> *).
(Manifest r e, Load r' ix e, MonadIO m) =>
Array r' ix e -> m (Array r ix e)
computeIO (Comp -> Sz Int -> (Ix2 -> e) -> Array DL Ix2 e
forall e. Num e => Comp -> Sz Int -> (Ix2 -> e) -> Matrix DL e
lowerTriangular Comp
comp (Int -> Sz Int
forall ix. Index ix => ix -> Sz ix
Sz Int
n) (Fun Ix2T e -> Ix2T -> e
forall a b. Fun a b -> a -> b
applyFun Fun Ix2T e
f (Ix2T -> e) -> (Ix2 -> Ix2T) -> Ix2 -> e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ix2 -> Ix2T
fromIx2)) IO (Matrix r e) -> Matrix r e -> Expectation
forall a. (HasCallStack, Show a, Eq a) => IO a -> a -> Expectation
`shouldReturn`
        Comp -> Sz Ix2 -> (Ix2 -> e) -> Matrix r e
forall r ix e.
Load r ix e =>
Comp -> Sz ix -> (ix -> e) -> Array r ix e
makeArray @r Comp
Seq (Int -> Int -> Sz Ix2
Sz2 Int
n Int
n) (\ (Int
i :. Int
j) -> if Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
j then Fun Ix2T e -> Ix2T -> e
forall a b. Fun a b -> a -> b
applyFun Fun Ix2T e
f (Int
i, Int
j) else e
0 :: e)
    [Char] -> (Comp -> Int -> Fun Ix2T e -> Property) -> Spec
forall prop.
(HasCallStack, Testable prop) =>
[Char] -> prop -> Spec
prop [Char]
"UpperTriangular" ((Comp -> Int -> Fun Ix2T e -> Property) -> Spec)
-> (Comp -> Int -> Fun Ix2T e -> Property) -> Spec
forall a b. (a -> b) -> a -> b
$ \ Comp
comp Int
n Fun Ix2T e
f -> Expectation -> Property
expectProp (Expectation -> Property) -> Expectation -> Property
forall a b. (a -> b) -> a -> b
$ do
      Array DL Ix2 e -> IO (Matrix r e)
forall r ix e r' (m :: * -> *).
(Manifest r e, Load r' ix e, MonadIO m) =>
Array r' ix e -> m (Array r ix e)
computeIO (Comp -> Sz Int -> (Ix2 -> e) -> Array DL Ix2 e
forall e. Num e => Comp -> Sz Int -> (Ix2 -> e) -> Matrix DL e
upperTriangular Comp
comp (Int -> Sz Int
forall ix. Index ix => ix -> Sz ix
Sz Int
n) (Fun Ix2T e -> Ix2T -> e
forall a b. Fun a b -> a -> b
applyFun Fun Ix2T e
f (Ix2T -> e) -> (Ix2 -> Ix2T) -> Ix2 -> e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ix2 -> Ix2T
fromIx2)) IO (Matrix r e) -> Matrix r e -> Expectation
forall a. (HasCallStack, Show a, Eq a) => IO a -> a -> Expectation
`shouldReturn`
        Comp -> Sz Ix2 -> (Ix2 -> e) -> Matrix r e
forall r ix e.
Load r ix e =>
Comp -> Sz ix -> (ix -> e) -> Array r ix e
makeArray @r Comp
Seq (Int -> Int -> Sz Ix2
Sz2 Int
n Int
n) (\ (Int
i :. Int
j) -> if Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
j then Fun Ix2T e -> Ix2T -> e
forall a b. Fun a b -> a -> b
applyFun Fun Ix2T e
f (Int
i, Int
j) else e
0 :: e)

mutableNumericFloatSpec ::
     forall r.
     ( NumericFloat r Float
     , Manifest r Float
     , Arbitrary (Vector r Float)
     , Arbitrary (Matrix r Float)
     , Show (Vector r Float)
     , Show (Matrix r Float)
     , Eq (Matrix r Float)
     , NumericFloat r Double
     , Manifest r Double
     , Arbitrary (Vector r Double)
     , Arbitrary (Matrix r Double)
     , Show (Vector r Double)
     , Show (Matrix r Double)
     , Eq (Matrix r Double)
     )
  => Spec
mutableNumericFloatSpec :: Spec
mutableNumericFloatSpec = do
  let ef :: Float
ef = Float
1e-6 :: Float
      ed :: Double
ed = Double
1e-12 :: Double
  [Char] -> Spec -> Spec
forall a. HasCallStack => [Char] -> SpecWith a -> SpecWith a
describe [Char]
"NumericFloat Operations" (Spec -> Spec) -> Spec -> Spec
forall a b. (a -> b) -> a -> b
$ do
    [Char] -> Spec -> Spec
forall a. HasCallStack => [Char] -> SpecWith a -> SpecWith a
describe [Char]
"Float" (Spec -> Spec) -> Spec -> Spec
forall a b. (a -> b) -> a -> b
$ do
      [Char]
-> (Fun Float Float -> Matrix r Float -> Float -> Property) -> Spec
forall prop.
(HasCallStack, Testable prop) =>
[Char] -> prop -> Spec
prop [Char]
"Divide" ((Fun Float Float -> Matrix r Float -> Float -> Property) -> Spec)
-> (Fun Float Float -> Matrix r Float -> Float -> Property) -> Spec
forall a b. (a -> b) -> a -> b
$ Float -> Fun Float Float -> Matrix r Float -> Float -> Property
forall r e.
(NumericFloat r e, Manifest r e, Show e, RealFloat e,
 Show (Matrix r e), Eq (Matrix r e)) =>
e -> Fun e e -> Matrix r e -> e -> Property
prop_Divide @r Float
ef
      [Char] -> (Matrix r Float -> Property) -> Spec
forall prop.
(HasCallStack, Testable prop) =>
[Char] -> prop -> Spec
prop [Char]
"Floating" ((Matrix r Float -> Property) -> Spec)
-> (Matrix r Float -> Property) -> Spec
forall a b. (a -> b) -> a -> b
$ Float -> Matrix r Float -> Property
forall r e.
(RealFloat e, Source r e, NumericFloat r e, Show e) =>
e -> Matrix r e -> Property
prop_Floating @r Float
ef
      [Char] -> (Matrix r Float -> Fun Float Float -> Property) -> Spec
forall prop.
(HasCallStack, Testable prop) =>
[Char] -> prop -> Spec
prop [Char]
"Floating2" ((Matrix r Float -> Fun Float Float -> Property) -> Spec)
-> (Matrix r Float -> Fun Float Float -> Property) -> Spec
forall a b. (a -> b) -> a -> b
$ Float -> Matrix r Float -> Fun Float Float -> Property
forall r e.
(RealFloat e, Manifest r e, NumericFloat r e, Show e) =>
e -> Matrix r e -> Fun e e -> Property
prop_Floating2 @r Float
ef
      [Char] -> (Vector r Float -> Property) -> Spec
forall prop.
(HasCallStack, Testable prop) =>
[Char] -> prop -> Spec
prop [Char]
"Norm" ((Vector r Float -> Property) -> Spec)
-> (Vector r Float -> Property) -> Spec
forall a b. (a -> b) -> a -> b
$ Float -> Vector r Float -> Property
forall r e.
(NumericFloat r e, Manifest r e, RealFloat e, Show e) =>
e -> Vector r e -> Property
prop_Norm @r Float
ef
      [Char] -> (Matrix r Float -> Int -> Property) -> Spec
forall prop.
(HasCallStack, Testable prop) =>
[Char] -> prop -> Spec
prop [Char]
"Power" ((Matrix r Float -> Int -> Property) -> Spec)
-> (Matrix r Float -> Int -> Property) -> Spec
forall a b. (a -> b) -> a -> b
$ Float -> Matrix r Float -> Int -> Property
forall r e.
(Numeric r e, Source r e, RealFloat e, Show e) =>
e -> Matrix r e -> Int -> Property
prop_Power @r Float
ef
    [Char] -> Spec -> Spec
forall a. HasCallStack => [Char] -> SpecWith a -> SpecWith a
describe [Char]
"Double" (Spec -> Spec) -> Spec -> Spec
forall a b. (a -> b) -> a -> b
$ do
      [Char]
-> (Fun Double Double -> Matrix r Double -> Double -> Property)
-> Spec
forall prop.
(HasCallStack, Testable prop) =>
[Char] -> prop -> Spec
prop [Char]
"Divide" ((Fun Double Double -> Matrix r Double -> Double -> Property)
 -> Spec)
-> (Fun Double Double -> Matrix r Double -> Double -> Property)
-> Spec
forall a b. (a -> b) -> a -> b
$ Double
-> Fun Double Double -> Matrix r Double -> Double -> Property
forall r e.
(NumericFloat r e, Manifest r e, Show e, RealFloat e,
 Show (Matrix r e), Eq (Matrix r e)) =>
e -> Fun e e -> Matrix r e -> e -> Property
prop_Divide @r Double
ed
      [Char] -> (Matrix r Double -> Property) -> Spec
forall prop.
(HasCallStack, Testable prop) =>
[Char] -> prop -> Spec
prop [Char]
"Floating" ((Matrix r Double -> Property) -> Spec)
-> (Matrix r Double -> Property) -> Spec
forall a b. (a -> b) -> a -> b
$ Double -> Matrix r Double -> Property
forall r e.
(RealFloat e, Source r e, NumericFloat r e, Show e) =>
e -> Matrix r e -> Property
prop_Floating @r Double
ed
      [Char]
-> (Matrix r Double -> Fun Double Double -> Property) -> Spec
forall prop.
(HasCallStack, Testable prop) =>
[Char] -> prop -> Spec
prop [Char]
"Floating2" ((Matrix r Double -> Fun Double Double -> Property) -> Spec)
-> (Matrix r Double -> Fun Double Double -> Property) -> Spec
forall a b. (a -> b) -> a -> b
$ Double -> Matrix r Double -> Fun Double Double -> Property
forall r e.
(RealFloat e, Manifest r e, NumericFloat r e, Show e) =>
e -> Matrix r e -> Fun e e -> Property
prop_Floating2 @r Double
ed
      [Char] -> (Vector r Double -> Property) -> Spec
forall prop.
(HasCallStack, Testable prop) =>
[Char] -> prop -> Spec
prop [Char]
"Norm" ((Vector r Double -> Property) -> Spec)
-> (Vector r Double -> Property) -> Spec
forall a b. (a -> b) -> a -> b
$ Double -> Vector r Double -> Property
forall r e.
(NumericFloat r e, Manifest r e, RealFloat e, Show e) =>
e -> Vector r e -> Property
prop_Norm @r Double
ed
      [Char] -> (Matrix r Double -> Int -> Property) -> Spec
forall prop.
(HasCallStack, Testable prop) =>
[Char] -> prop -> Spec
prop [Char]
"Power" ((Matrix r Double -> Int -> Property) -> Spec)
-> (Matrix r Double -> Int -> Property) -> Spec
forall a b. (a -> b) -> a -> b
$ Double -> Matrix r Double -> Int -> Property
forall r e.
(Numeric r e, Source r e, RealFloat e, Show e) =>
e -> Matrix r e -> Int -> Property
prop_Power @r Double
ed

prop_Power ::
     (Numeric r e, Source r e, RealFloat e, Show e) => e -> Matrix r e -> Int -> Property
prop_Power :: e -> Matrix r e -> Int -> Property
prop_Power e
eps Matrix r e
arr Int
p = Expectation -> Property
expectProp (Expectation -> Property) -> Expectation -> Property
forall a b. (a -> b) -> a -> b
$
  e -> Array D Ix2 e -> Array D Ix2 e -> Expectation
forall (f :: * -> *) e.
(HasCallStack, Foldable f, Show (f e), Show e, RealFloat e) =>
e -> f e -> f e -> Expectation
epsilonFoldableExpect e
eps (Matrix r e -> Array D Ix2 e
forall ix r e.
(Index ix, Source r e) =>
Array r ix e -> Array D ix e
delay (Matrix r e
arr Matrix r e -> Int -> Matrix r e
forall ix r e b.
(Index ix, Numeric r e, Fractional e, Integral b) =>
Array r ix e -> b -> Array r ix e
.^^ Int
p)) ((e -> e) -> Matrix r e -> Array D Ix2 e
forall ix r e' e.
(Index ix, Source r e') =>
(e' -> e) -> Array r ix e' -> Array D ix e
A.map (e -> Int -> e
forall a b. (Fractional a, Integral b) => a -> b -> a
^^ Int
p) Matrix r e
arr)