{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}

-- |
-- Module      : Data.Massiv.Array.Delayed.Pull
-- Copyright   : (c) Alexey Kuleshevich 2018-2022
-- License     : BSD3
-- Maintainer  : Alexey Kuleshevich <lehins@yandex.ru>
-- Stability   : experimental
-- Portability : non-portable
module Data.Massiv.Array.Delayed.Pull (
  D (..),
  Array (..),
  delay,
  eqArrays,
  compareArrays,
  imap,
  liftArray2',
  liftArray2M,
  unsafeExtract,
  unsafeSlice,
  unsafeInnerSlice,
  zipWithInternal,
) where

import Control.Applicative
import qualified Data.Foldable as F
import Data.Massiv.Array.Ops.Fold.Internal as A
import Data.Massiv.Core.Common as A
import Data.Massiv.Core.List (L, showArrayList, showsArrayPrec)
import Data.Massiv.Core.Operations
import qualified Data.Massiv.Vector.Stream as S
import GHC.Base (build)
import Prelude hiding (zipWith)

#include "massiv.h"

-- | Delayed representation.
data D
  = D
  deriving (Int -> D -> ShowS
[D] -> ShowS
D -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [D] -> ShowS
$cshowList :: [D] -> ShowS
show :: D -> String
$cshow :: D -> String
showsPrec :: Int -> D -> ShowS
$cshowsPrec :: Int -> D -> ShowS
Show)

data instance Array D ix e = DArray
  { forall ix e. Array D ix e -> Comp
dComp :: !Comp
  , forall ix e. Array D ix e -> Sz ix
dSize :: !(Sz ix)
  , forall ix e. Array D ix e -> PrefIndex ix e
dPrefIndex :: !(PrefIndex ix e)
  }

instance (Ragged L ix e, Show e) => Show (Array D ix e) where
  showsPrec :: Int -> Array D ix e -> ShowS
showsPrec = forall r r' ix e.
(Ragged L ix e, Load r ix e, Load r' ix e, Source r' e, Show e) =>
(Array r ix e -> Array r' ix e) -> Int -> Array r ix e -> ShowS
showsArrayPrec forall a. a -> a
id
  showList :: [Array D ix e] -> ShowS
showList = forall arr. Show arr => [arr] -> ShowS
showArrayList

instance Index ix => Shape D ix where
  maxLinearSize :: forall e. Array D ix e -> Maybe Sz1
maxLinearSize = forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix. ix -> Sz ix
SafeSz forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix r e. (Index ix, Size r) => Array r ix e -> Int
elemsCount
  {-# INLINE maxLinearSize #-}

instance Size D where
  size :: forall ix e. Array D ix e -> Sz ix
size = forall ix e. Array D ix e -> Sz ix
dSize
  {-# INLINE size #-}
  unsafeResize :: forall ix ix' e.
(Index ix, Index ix') =>
Sz ix' -> Array D ix e -> Array D ix' e
unsafeResize !Sz ix'
sz !Array D ix e
arr =
    forall r ix e.
Load r ix e =>
Comp -> Sz ix -> (Int -> e) -> Array r ix e
makeArrayLinear (forall ix e. Array D ix e -> Comp
dComp Array D ix e
arr) Sz ix'
sz (forall r e ix. (Source r e, Index ix) => Array r ix e -> ix -> e
unsafeIndex Array D ix e
arr forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix. Index ix => Sz ix -> Int -> ix
fromLinearIndex (forall r ix e. Size r => Array r ix e -> Sz ix
size Array D ix e
arr))
  {-# INLINE unsafeResize #-}

instance Strategy D where
  setComp :: forall ix e. Comp -> Array D ix e -> Array D ix e
setComp Comp
c Array D ix e
arr = Array D ix e
arr{dComp :: Comp
dComp = Comp
c}
  {-# INLINE setComp #-}
  getComp :: forall ix e. Array D ix e -> Comp
getComp = forall ix e. Array D ix e -> Comp
dComp
  {-# INLINE getComp #-}
  repr :: D
repr = D
D

instance Source D e where
  unsafeIndex :: forall ix. Index ix => Array D ix e -> ix -> e
unsafeIndex Array D ix e
arr =
    case forall ix e. Array D ix e -> PrefIndex ix e
dPrefIndex Array D ix e
arr of
      PrefIndex ix -> e
f -> ix -> e
f
      PrefIndexLinear Int -> e
f -> Int -> e
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix. Index ix => Sz ix -> ix -> Int
toLinearIndex (forall r ix e. Size r => Array r ix e -> Sz ix
size Array D ix e
arr)
  {-# INLINE unsafeIndex #-}
  unsafeLinearIndex :: forall ix. Index ix => Array D ix e -> Int -> e
unsafeLinearIndex Array D ix e
arr =
    case forall ix e. Array D ix e -> PrefIndex ix e
dPrefIndex Array D ix e
arr of
      PrefIndex ix -> e
f -> ix -> e
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix. Index ix => Sz ix -> Int -> ix
fromLinearIndex (forall r ix e. Size r => Array r ix e -> Sz ix
size Array D ix e
arr)
      PrefIndexLinear Int -> e
f -> Int -> e
f
  {-# INLINE unsafeLinearIndex #-}
  unsafePrefIndex :: forall ix. Index ix => Array D ix e -> PrefIndex ix e
unsafePrefIndex = forall ix e. Array D ix e -> PrefIndex ix e
dPrefIndex
  {-# INLINE unsafePrefIndex #-}
  unsafeOuterSlice :: forall ix.
(Index ix, Index (Lower ix)) =>
Array D ix e -> Sz (Lower ix) -> Int -> Array D (Lower ix) e
unsafeOuterSlice !Array D ix e
arr !Sz (Lower ix)
szL !Int
i =
    forall r ix e.
Load r ix e =>
Comp -> Sz ix -> (ix -> e) -> Array r ix e
makeArray (forall ix e. Array D ix e -> Comp
dComp Array D ix e
arr) Sz (Lower ix)
szL (forall r e ix. (Source r e, Index ix) => Array r ix e -> ix -> e
unsafeIndex Array D ix e
arr forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix. Index ix => Int -> Lower ix -> ix
consDim Int
i)
  {-# INLINE unsafeOuterSlice #-}
  unsafeLinearSlice :: forall ix. Index ix => Int -> Sz1 -> Array D ix e -> Array D Int e
unsafeLinearSlice !Int
o !Sz1
sz Array D ix e
arr =
    forall r ix e.
Load r ix e =>
Comp -> Sz ix -> (Int -> e) -> Array r ix e
makeArrayLinear (forall ix e. Array D ix e -> Comp
dComp Array D ix e
arr) Sz1
sz forall a b. (a -> b) -> a -> b
$ \ !Int
i -> forall r e ix. (Source r e, Index ix) => Array r ix e -> Int -> e
unsafeLinearIndex Array D ix e
arr (Int
i forall a. Num a => a -> a -> a
+ Int
o)
  {-# INLINE unsafeLinearSlice #-}

-- | /O(1)/ - Extract a portion of an array. Staring index and new size are
-- not validated.
unsafeExtract :: (Source r e, Index ix) => ix -> Sz ix -> Array r ix e -> Array D ix e
unsafeExtract :: forall r e ix.
(Source r e, Index ix) =>
ix -> Sz ix -> Array r ix e -> Array D ix e
unsafeExtract !ix
sIx !Sz ix
newSz !Array r ix e
arr =
  forall r ix e.
Load r ix e =>
Comp -> Sz ix -> (ix -> e) -> Array r ix e
makeArray (forall r ix e. Strategy r => Array r ix e -> Comp
getComp Array r ix e
arr) Sz ix
newSz (forall r e ix. (Source r e, Index ix) => Array r ix e -> ix -> e
unsafeIndex Array r ix e
arr forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix. Index ix => (Int -> Int -> Int) -> ix -> ix -> ix
liftIndex2 forall a. Num a => a -> a -> a
(+) ix
sIx)
{-# INLINE unsafeExtract #-}

-- | /O(1)/ - Take a slice out of an array from within
unsafeSlice
  :: (Source r e, Index ix, Index (Lower ix), MonadThrow m)
  => Array r ix e
  -> ix
  -> Sz ix
  -> Dim
  -> m (Array D (Lower ix) e)
unsafeSlice :: forall r e ix (m :: * -> *).
(Source r e, Index ix, Index (Lower ix), MonadThrow m) =>
Array r ix e -> ix -> Sz ix -> Dim -> m (Array D (Lower ix) e)
unsafeSlice Array r ix e
arr ix
start cut :: Sz ix
cut@(SafeSz ix
cutSz) Dim
dim = do
  Lower ix
newSz <- forall (m :: * -> *) ix.
(MonadThrow m, Index ix) =>
ix -> Dim -> m (Lower ix)
dropDimM ix
cutSz Dim
dim
  forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall r ix ix' e.
(Size r, Index ix, Index ix') =>
Sz ix' -> Array r ix e -> Array r ix' e
unsafeResize (forall ix. ix -> Sz ix
SafeSz Lower ix
newSz) (forall r e ix.
(Source r e, Index ix) =>
ix -> Sz ix -> Array r ix e -> Array D ix e
unsafeExtract ix
start Sz ix
cut Array r ix e
arr)
{-# INLINE unsafeSlice #-}

-- | /O(1)/ - Take a slice out of an array from the inside
unsafeInnerSlice
  :: (Source r e, Index ix) => Array r ix e -> Sz (Lower ix) -> Int -> Array D (Lower ix) e
unsafeInnerSlice :: forall r e ix.
(Source r e, Index ix) =>
Array r ix e -> Sz (Lower ix) -> Int -> Array D (Lower ix) e
unsafeInnerSlice !Array r ix e
arr Sz (Lower ix)
szL !Int
i =
  forall ix e. Comp -> Sz ix -> PrefIndex ix e -> Array D ix e
DArray (forall r ix e. Strategy r => Array r ix e -> Comp
getComp Array r ix e
arr) Sz (Lower ix)
szL forall a b. (a -> b) -> a -> b
$ forall ix e. (ix -> e) -> PrefIndex ix e
PrefIndex (forall r e ix. (Source r e, Index ix) => Array r ix e -> ix -> e
unsafeIndex Array r ix e
arr forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall ix. Index ix => Lower ix -> Int -> ix
`snocDim` Int
i))
{-# INLINE unsafeInnerSlice #-}

instance (Eq e, Index ix) => Eq (Array D ix e) where
  == :: Array D ix e -> Array D ix e -> Bool
(==) = forall ix r1 e1 r2 e2.
(Index ix, Source r1 e1, Source r2 e2) =>
(e1 -> e2 -> Bool) -> Array r1 ix e1 -> Array r2 ix e2 -> Bool
eqArrays forall a. Eq a => a -> a -> Bool
(==)
  {-# INLINE (==) #-}

instance (Ord e, Index ix) => Ord (Array D ix e) where
  compare :: Array D ix e -> Array D ix e -> Ordering
compare = forall ix r1 e1 r2 e2.
(Index ix, Source r1 e1, Source r2 e2) =>
(e1 -> e2 -> Ordering)
-> Array r1 ix e1 -> Array r2 ix e2 -> Ordering
compareArrays forall a. Ord a => a -> a -> Ordering
compare
  {-# INLINE compare #-}

instance Functor (Array D ix) where
  fmap :: forall a b. (a -> b) -> Array D ix a -> Array D ix b
fmap a -> b
f (DArray Comp
c Sz ix
sz PrefIndex ix a
g) = forall ix e. Comp -> Sz ix -> PrefIndex ix e -> Array D ix e
DArray Comp
c Sz ix
sz (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f PrefIndex ix a
g)
  {-# INLINE fmap #-}
  <$ :: forall a b. a -> Array D ix b -> Array D ix a
(<$) a
e (DArray Comp
c Sz ix
sz PrefIndex ix b
g) = forall ix e. Comp -> Sz ix -> PrefIndex ix e -> Array D ix e
DArray Comp
c Sz ix
sz (a
e forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ PrefIndex ix b
g)
  {-# INLINE (<$) #-}

instance Index ix => Applicative (Array D ix) where
  pure :: forall a. a -> Array D ix a
pure = forall r ix e. Load r ix e => e -> Array r ix e
singleton
  {-# INLINE pure #-}
  <*> :: forall a b. Array D ix (a -> b) -> Array D ix a -> Array D ix b
(<*>) = forall ix r1 a r2 b e.
(HasCallStack, Index ix, Source r1 a, Source r2 b) =>
(a -> b -> e) -> Array r1 ix a -> Array r2 ix b -> Array D ix e
liftArray2' forall a. a -> a
id
  {-# INLINE (<*>) #-}
#if MIN_VERSION_base(4,10,0)
  liftA2 :: forall a b c.
(a -> b -> c) -> Array D ix a -> Array D ix b -> Array D ix c
liftA2 = forall ix r1 a r2 b e.
(HasCallStack, Index ix, Source r1 a, Source r2 b) =>
(a -> b -> e) -> Array r1 ix a -> Array r2 ix b -> Array D ix e
liftArray2'
  {-# INLINE liftA2 #-}
#endif

-- | Row-major sequential folding over a Delayed array.
instance Index ix => Foldable (Array D ix) where
  fold :: forall m. Monoid m => Array D ix m -> m
fold = forall e ix r.
(Monoid e, Index ix, Source r e) =>
Array r ix e -> e
A.fold
  {-# INLINE fold #-}
  foldMap :: forall m a. Monoid m => (a -> m) -> Array D ix a -> m
foldMap = forall ix r e m.
(Index ix, Source r e, Monoid m) =>
(e -> m) -> Array r ix e -> m
A.foldMono
  {-# INLINE foldMap #-}
  foldl :: forall b a. (b -> a -> b) -> b -> Array D ix a -> b
foldl = forall ix r e a.
(Index ix, Source r e) =>
(a -> e -> a) -> a -> Array r ix e -> a
lazyFoldlS
  {-# INLINE foldl #-}
  foldl' :: forall b a. (b -> a -> b) -> b -> Array D ix a -> b
foldl' = forall ix r e a.
(Index ix, Source r e) =>
(a -> e -> a) -> a -> Array r ix e -> a
foldlS
  {-# INLINE foldl' #-}
  foldr :: forall a b. (a -> b -> b) -> b -> Array D ix a -> b
foldr = forall ix r e b.
(Index ix, Source r e) =>
(e -> b -> b) -> b -> Array r ix e -> b
foldrFB
  {-# INLINE foldr #-}
  foldr' :: forall a b. (a -> b -> b) -> b -> Array D ix a -> b
foldr' = forall ix r e b.
(Index ix, Source r e) =>
(e -> b -> b) -> b -> Array r ix e -> b
foldrS
  {-# INLINE foldr' #-}
  null :: forall a. Array D ix a -> Bool
null (DArray Comp
_ Sz ix
sz PrefIndex ix a
_) = forall ix. Index ix => Sz ix -> Int
totalElem Sz ix
sz forall a. Eq a => a -> a -> Bool
== Int
0
  {-# INLINE null #-}
  length :: forall a. Array D ix a -> Int
length = forall ix. Index ix => Sz ix -> Int
totalElem forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r ix e. Size r => Array r ix e -> Sz ix
size
  {-# INLINE length #-}
  elem :: forall a. Eq a => a -> Array D ix a -> Bool
elem a
e = forall ix r e.
(Index ix, Source r e) =>
(e -> Bool) -> Array r ix e -> Bool
A.any (a
e forall a. Eq a => a -> a -> Bool
==)
  {-# INLINE elem #-}
  toList :: forall a. Array D ix a -> [a]
toList Array D ix a
arr = forall a. (forall b. (a -> b -> b) -> b -> b) -> [a]
build (\a -> b -> b
c b
n -> forall ix r e b.
(Index ix, Source r e) =>
(e -> b -> b) -> b -> Array r ix e -> b
foldrFB a -> b -> b
c b
n Array D ix a
arr)
  {-# INLINE toList #-}

instance Index ix => Load D ix e where
  makeArray :: Comp -> Sz ix -> (ix -> e) -> Array D ix e
makeArray Comp
comp Sz ix
sz = forall ix e. Comp -> Sz ix -> PrefIndex ix e -> Array D ix e
DArray Comp
comp Sz ix
sz forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix e. (ix -> e) -> PrefIndex ix e
PrefIndex
  {-# INLINE makeArray #-}
  makeArrayLinear :: Comp -> Sz ix -> (Int -> e) -> Array D ix e
makeArrayLinear Comp
comp Sz ix
sz = forall ix e. Comp -> Sz ix -> PrefIndex ix e -> Array D ix e
DArray Comp
comp Sz ix
sz forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix e. (Int -> e) -> PrefIndex ix e
PrefIndexLinear
  {-# INLINE makeArrayLinear #-}
  iterArrayLinearST_ :: forall s.
Scheduler s () -> Array D ix e -> (Int -> e -> ST s ()) -> ST s ()
iterArrayLinearST_ !Scheduler s ()
scheduler DArray{Comp
Sz ix
PrefIndex ix e
dPrefIndex :: PrefIndex ix e
dSize :: Sz ix
dComp :: Comp
dPrefIndex :: forall ix e. Array D ix e -> PrefIndex ix e
dSize :: forall ix e. Array D ix e -> Sz ix
dComp :: forall ix e. Array D ix e -> Comp
..} Int -> e -> ST s ()
uWrite =
    case PrefIndex ix e
dPrefIndex of
      PrefIndex ix -> e
f ->
        forall it ix s.
(Iterator it, Index ix) =>
it
-> Scheduler s ()
-> Int
-> Sz ix
-> (Int -> ix -> ST s ())
-> ST s ()
iterTargetFullST_ RowMajor
defRowMajor Scheduler s ()
scheduler Int
0 Sz ix
dSize forall a b. (a -> b) -> a -> b
$ \ !Int
i -> Int -> e -> ST s ()
uWrite Int
i forall b c a. (b -> c) -> (a -> b) -> a -> c
. ix -> e
f
      PrefIndexLinear Int -> e
f ->
        forall it ix s.
(Iterator it, Index ix) =>
it
-> Scheduler s ()
-> Int
-> Sz ix
-> (Int -> ix -> ST s ())
-> ST s ()
iterTargetFullST_ RowMajorLinear
defRowMajorLinear Scheduler s ()
scheduler Int
0 Sz ix
dSize forall a b. (a -> b) -> a -> b
$ \ !Int
i ix
_ -> Int -> e -> ST s ()
uWrite Int
i (Int -> e
f Int
i)
  {-# INLINE iterArrayLinearST_ #-}

instance Index ix => StrideLoad D ix e where
  iterArrayLinearWithStrideST_ :: forall s.
Scheduler s ()
-> Stride ix
-> Sz ix
-> Array D ix e
-> (Int -> e -> ST s ())
-> ST s ()
iterArrayLinearWithStrideST_ !Scheduler s ()
scheduler !Stride ix
stride Sz ix
sz DArray{Comp
Sz ix
PrefIndex ix e
dPrefIndex :: PrefIndex ix e
dSize :: Sz ix
dComp :: Comp
dPrefIndex :: forall ix e. Array D ix e -> PrefIndex ix e
dSize :: forall ix e. Array D ix e -> Sz ix
dComp :: forall ix e. Array D ix e -> Comp
..} Int -> e -> ST s ()
uWrite =
    case PrefIndex ix e
dPrefIndex of
      PrefIndex ix -> e
f ->
        forall it ix s.
(Iterator it, Index ix) =>
it
-> Scheduler s ()
-> Int
-> Sz ix
-> Stride ix
-> (Int -> ix -> ST s ())
-> ST s ()
iterTargetFullWithStrideST_ RowMajor
defRowMajor Scheduler s ()
scheduler Int
0 Sz ix
sz Stride ix
stride forall a b. (a -> b) -> a -> b
$ \Int
i ->
          Int -> e -> ST s ()
uWrite Int
i forall b c a. (b -> c) -> (a -> b) -> a -> c
. ix -> e
f
      PrefIndexLinear Int -> e
f -> do
        forall it ix s.
(Iterator it, Index ix) =>
it
-> Scheduler s ()
-> Int
-> Sz ix
-> Stride ix
-> (Int -> ix -> ST s ())
-> ST s ()
iterTargetFullWithStrideST_ RowMajor
defRowMajor Scheduler s ()
scheduler Int
0 Sz ix
sz Stride ix
stride forall a b. (a -> b) -> a -> b
$ \Int
i ->
          Int -> e -> ST s ()
uWrite Int
i forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> e
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix. Index ix => Sz ix -> ix -> Int
toLinearIndex Sz ix
dSize
  {-# INLINE iterArrayLinearWithStrideST_ #-}

instance Index ix => Stream D ix e where
  toStream :: Array D ix e -> Steps Id e
toStream = forall r ix e (m :: * -> *).
(Monad m, Index ix, Source r e) =>
Array r ix e -> Steps m e
S.steps
  {-# INLINE toStream #-}
  toStreamIx :: Array D ix e -> Steps Id (ix, e)
toStreamIx = forall r ix e (m :: * -> *).
(Monad m, Index ix, Source r e) =>
Array r ix e -> Steps m e
S.steps forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r ix e a.
(Index ix, Source r e) =>
(ix -> e -> a) -> Array r ix e -> Array D ix a
imap (,)
  {-# INLINE toStreamIx #-}

-- | Map an index aware function over an array
--
-- @since 0.1.0
imap
  :: forall r ix e a
   . (Index ix, Source r e)
  => (ix -> e -> a)
  -> Array r ix e
  -> Array D ix a
imap :: forall r ix e a.
(Index ix, Source r e) =>
(ix -> e -> a) -> Array r ix e -> Array D ix a
imap ix -> e -> a
f !Array r ix e
arr =
  case forall r e ix.
(Source r e, Index ix) =>
Array r ix e -> PrefIndex ix e
unsafePrefIndex Array r ix e
arr of
    PrefIndex ix -> e
gix -> forall ix e. Comp -> Sz ix -> PrefIndex ix e -> Array D ix e
DArray (forall r ix e. Strategy r => Array r ix e -> Comp
getComp Array r ix e
arr) Sz ix
sz forall a b. (a -> b) -> a -> b
$ forall ix e. (ix -> e) -> PrefIndex ix e
PrefIndex (\ !ix
ix -> ix -> e -> a
f ix
ix (ix -> e
gix ix
ix))
    PrefIndexLinear Int -> e
gi ->
      forall ix e. Comp -> Sz ix -> PrefIndex ix e -> Array D ix e
DArray (forall r ix e. Strategy r => Array r ix e -> Comp
getComp Array r ix e
arr) Sz ix
sz forall a b. (a -> b) -> a -> b
$ forall ix e. (ix -> e) -> PrefIndex ix e
PrefIndex (\ !ix
ix -> ix -> e -> a
f ix
ix (Int -> e
gi (forall ix. Index ix => Sz ix -> ix -> Int
toLinearIndex Sz ix
sz ix
ix)))
  where
    !sz :: Sz ix
sz = forall r ix e. Size r => Array r ix e -> Sz ix
size Array r ix e
arr
{-# INLINE imap #-}

instance Num e => FoldNumeric D e where
  unsafeDotProduct :: forall ix. Index ix => Array D ix e -> Array D ix e -> e
unsafeDotProduct = forall e ix r.
(Num e, Index ix, Source r e) =>
Array r ix e -> Array r ix e -> e
defaultUnsafeDotProduct
  {-# INLINE unsafeDotProduct #-}
  powerSumArray :: forall ix. Index ix => Array D ix e -> Int -> e
powerSumArray = forall ix r e.
(Index ix, Source r e, Num e) =>
Array r ix e -> Int -> e
defaultPowerSumArray
  {-# INLINE powerSumArray #-}
  foldArray :: forall ix. Index ix => (e -> e -> e) -> e -> Array D ix e -> e
foldArray = forall ix r e.
(Index ix, Source r e) =>
(e -> e -> e) -> e -> Array r ix e -> e
defaultFoldArray
  {-# INLINE foldArray #-}

instance Num e => Numeric D e where
  unsafeLiftArray :: forall ix. Index ix => (e -> e) -> Array D ix e -> Array D ix e
unsafeLiftArray e -> e
f Array D ix e
arr = Array D ix e
arr{dPrefIndex :: PrefIndex ix e
dPrefIndex = e -> e
f forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall ix e. Array D ix e -> PrefIndex ix e
dPrefIndex Array D ix e
arr}
  {-# INLINE unsafeLiftArray #-}
  unsafeLiftArray2 :: forall ix.
Index ix =>
(e -> e -> e) -> Array D ix e -> Array D ix e -> Array D ix e
unsafeLiftArray2 e -> e -> e
f Array D ix e
a1 Array D ix e
a2 = forall ix r1 e1 r2 e2 e3.
(Index ix, Source r1 e1, Source r2 e2) =>
Sz ix
-> (e1 -> e2 -> e3)
-> Array r1 ix e1
-> Array r2 ix e2
-> Array D ix e3
zipWithInternal (forall r ix e. Size r => Array r ix e -> Sz ix
size Array D ix e
a1) e -> e -> e
f Array D ix e
a1 Array D ix e
a2
  {-# INLINE unsafeLiftArray2 #-}

instance Floating e => NumericFloat D e

-- | /O(1)/ Conversion from a source array to `D` representation.
delay :: (Index ix, Source r e) => Array r ix e -> Array D ix e
delay :: forall ix r e.
(Index ix, Source r e) =>
Array r ix e -> Array D ix e
delay Array r ix e
arr =
  case forall r e ix.
(Source r e, Index ix) =>
Array r ix e -> PrefIndex ix e
unsafePrefIndex Array r ix e
arr of
    PrefIndex ix -> e
gix -> forall r ix e.
Load r ix e =>
Comp -> Sz ix -> (ix -> e) -> Array r ix e
makeArray (forall r ix e. Strategy r => Array r ix e -> Comp
getComp Array r ix e
arr) (forall r ix e. Size r => Array r ix e -> Sz ix
size Array r ix e
arr) ix -> e
gix
    PrefIndexLinear Int -> e
gi -> forall r ix e.
Load r ix e =>
Comp -> Sz ix -> (Int -> e) -> Array r ix e
makeArrayLinear (forall r ix e. Strategy r => Array r ix e -> Comp
getComp Array r ix e
arr) (forall r ix e. Size r => Array r ix e -> Sz ix
size Array r ix e
arr) Int -> e
gi
{-# INLINE [1] delay #-}

{-# RULES
"delay" [~1] forall (arr :: Array D ix e). delay arr = arr
  #-}

-- | Compute array equality by applying a comparing function to each
-- element. Empty arrays are always equal, regardless of their size.
--
-- @since 0.5.7
eqArrays
  :: (Index ix, Source r1 e1, Source r2 e2)
  => (e1 -> e2 -> Bool)
  -> Array r1 ix e1
  -> Array r2 ix e2
  -> Bool
eqArrays :: forall ix r1 e1 r2 e2.
(Index ix, Source r1 e1, Source r2 e2) =>
(e1 -> e2 -> Bool) -> Array r1 ix e1 -> Array r2 ix e2 -> Bool
eqArrays e1 -> e2 -> Bool
f Array r1 ix e1
arr1 Array r2 ix e2
arr2 =
  let sz1 :: Sz ix
sz1 = forall r ix e. Size r => Array r ix e -> Sz ix
size Array r1 ix e1
arr1
      sz2 :: Sz ix
sz2 = forall r ix e. Size r => Array r ix e -> Sz ix
size Array r2 ix e2
arr2
   in ( Sz ix
sz1 forall a. Eq a => a -> a -> Bool
== Sz ix
sz2
          Bool -> Bool -> Bool
&& Bool -> Bool
not
            ( forall ix r e.
(Index ix, Source r e) =>
(e -> Bool) -> Array r ix e -> Bool
A.any
                Bool -> Bool
not
                ( forall r ix e.
Load r ix e =>
Comp -> Sz ix -> (ix -> e) -> Array r ix e
makeArray @D (forall r ix e. Strategy r => Array r ix e -> Comp
getComp Array r1 ix e1
arr1 forall a. Semigroup a => a -> a -> a
<> forall r ix e. Strategy r => Array r ix e -> Comp
getComp Array r2 ix e2
arr2) (forall r ix e. Size r => Array r ix e -> Sz ix
size Array r1 ix e1
arr1) forall a b. (a -> b) -> a -> b
$ \ix
ix ->
                    e1 -> e2 -> Bool
f (forall r e ix. (Source r e, Index ix) => Array r ix e -> ix -> e
unsafeIndex Array r1 ix e1
arr1 ix
ix) (forall r e ix. (Source r e, Index ix) => Array r ix e -> ix -> e
unsafeIndex Array r2 ix e2
arr2 ix
ix)
                )
            )
      )
        Bool -> Bool -> Bool
|| (forall ix. Index ix => Sz ix -> Bool
isZeroSz Sz ix
sz1 Bool -> Bool -> Bool
&& forall ix. Index ix => Sz ix -> Bool
isZeroSz Sz ix
sz2)
{-# INLINE eqArrays #-}

-- | Compute array ordering by applying a comparing function to each element.
-- The exact ordering is unspecified so this is only intended for use in maps and the like where
-- you need an ordering but do not care about which one is used.
--
-- @since 0.5.7
compareArrays
  :: (Index ix, Source r1 e1, Source r2 e2)
  => (e1 -> e2 -> Ordering)
  -> Array r1 ix e1
  -> Array r2 ix e2
  -> Ordering
compareArrays :: forall ix r1 e1 r2 e2.
(Index ix, Source r1 e1, Source r2 e2) =>
(e1 -> e2 -> Ordering)
-> Array r1 ix e1 -> Array r2 ix e2 -> Ordering
compareArrays e1 -> e2 -> Ordering
f Array r1 ix e1
arr1 Array r2 ix e2
arr2 =
  forall a. Ord a => a -> a -> Ordering
compare (forall r ix e. Size r => Array r ix e -> Sz ix
size Array r1 ix e1
arr1) (forall r ix e. Size r => Array r ix e -> Sz ix
size Array r2 ix e2
arr2)
    forall a. Semigroup a => a -> a -> a
<> forall e ix r.
(Monoid e, Index ix, Source r e) =>
Array r ix e -> e
A.fold
      ( forall r ix e.
Load r ix e =>
Comp -> Sz ix -> (ix -> e) -> Array r ix e
makeArray @D (forall r ix e. Strategy r => Array r ix e -> Comp
getComp Array r1 ix e1
arr1 forall a. Semigroup a => a -> a -> a
<> forall r ix e. Strategy r => Array r ix e -> Comp
getComp Array r2 ix e2
arr2) (forall r ix e. Size r => Array r ix e -> Sz ix
size Array r1 ix e1
arr1) forall a b. (a -> b) -> a -> b
$ \ix
ix ->
          e1 -> e2 -> Ordering
f (forall r e ix. (Source r e, Index ix) => Array r ix e -> ix -> e
unsafeIndex Array r1 ix e1
arr1 ix
ix) (forall r e ix. (Source r e, Index ix) => Array r ix e -> ix -> e
unsafeIndex Array r2 ix e2
arr2 ix
ix)
      )
{-# INLINE compareArrays #-}

-- | Same as `liftArray2M`, but throws an imprecise exception on mismatched
-- sizes.
--
-- @since 1.0.0
liftArray2'
  :: (HasCallStack, Index ix, Source r1 a, Source r2 b)
  => (a -> b -> e)
  -> Array r1 ix a
  -> Array r2 ix b
  -> Array D ix e
liftArray2' :: forall ix r1 a r2 b e.
(HasCallStack, Index ix, Source r1 a, Source r2 b) =>
(a -> b -> e) -> Array r1 ix a -> Array r2 ix b -> Array D ix e
liftArray2' a -> b -> e
f Array r1 ix a
arr1 Array r2 ix b
arr2 = forall a. HasCallStack => Either SomeException a -> a
throwEither forall a b. (a -> b) -> a -> b
$ forall ix r1 a r2 b (m :: * -> *) e.
(Index ix, Source r1 a, Source r2 b, MonadThrow m) =>
(a -> b -> e) -> Array r1 ix a -> Array r2 ix b -> m (Array D ix e)
liftArray2M a -> b -> e
f Array r1 ix a
arr1 Array r2 ix b
arr2
{-# INLINE liftArray2' #-}

-- | Similar to `Data.Massiv.Array.zipWith`, except dimensions of both arrays
-- have to be the same, otherwise it throws `SizeMismatchException`.
--
-- @since 1.0.0
liftArray2M
  :: (Index ix, Source r1 a, Source r2 b, MonadThrow m)
  => (a -> b -> e)
  -> Array r1 ix a
  -> Array r2 ix b
  -> m (Array D ix e)
liftArray2M :: forall ix r1 a r2 b (m :: * -> *) e.
(Index ix, Source r1 a, Source r2 b, MonadThrow m) =>
(a -> b -> e) -> Array r1 ix a -> Array r2 ix b -> m (Array D ix e)
liftArray2M a -> b -> e
f !Array r1 ix a
arr1 !Array r2 ix b
arr2
  | Sz ix
sz1 forall a. Eq a => a -> a -> Bool
== Sz ix
sz2 = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall ix r1 e1 r2 e2 e3.
(Index ix, Source r1 e1, Source r2 e2) =>
Sz ix
-> (e1 -> e2 -> e3)
-> Array r1 ix e1
-> Array r2 ix e2
-> Array D ix e3
zipWithInternal Sz ix
sz1 a -> b -> e
f Array r1 ix a
arr1 Array r2 ix b
arr2
  | forall ix. Index ix => Sz ix -> Bool
isZeroSz Sz ix
sz1 Bool -> Bool -> Bool
&& forall ix. Index ix => Sz ix -> Bool
isZeroSz Sz ix
sz2 = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall r ix e. Load r ix e => Array r ix e
A.empty
  | Bool
otherwise = forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM forall a b. (a -> b) -> a -> b
$ forall ix. Index ix => Sz ix -> Sz ix -> SizeException
SizeMismatchException (forall r ix e. Size r => Array r ix e -> Sz ix
size Array r1 ix a
arr1) (forall r ix e. Size r => Array r ix e -> Sz ix
size Array r2 ix b
arr2)
  where
    sz1 :: Sz ix
sz1 = forall r ix e. Size r => Array r ix e -> Sz ix
size Array r1 ix a
arr1
    sz2 :: Sz ix
sz2 = forall r ix e. Size r => Array r ix e -> Sz ix
size Array r2 ix b
arr2
{-# INLINE liftArray2M #-}

zipWithInternal
  :: (Index ix, Source r1 e1, Source r2 e2)
  => Sz ix
  -> (e1 -> e2 -> e3)
  -> Array r1 ix e1
  -> Array r2 ix e2
  -> Array D ix e3
zipWithInternal :: forall ix r1 e1 r2 e2 e3.
(Index ix, Source r1 e1, Source r2 e2) =>
Sz ix
-> (e1 -> e2 -> e3)
-> Array r1 ix e1
-> Array r2 ix e2
-> Array D ix e3
zipWithInternal Sz ix
sz e1 -> e2 -> e3
f Array r1 ix e1
arr1 Array r2 ix e2
arr2 =
  case forall r e ix.
(Source r e, Index ix) =>
Array r ix e -> PrefIndex ix e
unsafePrefIndex Array r1 ix e1
arr1 of
    PrefIndexLinear Int -> e1
gi1
      | PrefIndexLinear Int -> e2
gi2 <- forall r e ix.
(Source r e, Index ix) =>
Array r ix e -> PrefIndex ix e
unsafePrefIndex Array r2 ix e2
arr2 ->
          forall r ix e.
Load r ix e =>
Comp -> Sz ix -> (Int -> e) -> Array r ix e
makeArrayLinear Comp
comp Sz ix
sz (\ !Int
i -> e1 -> e2 -> e3
f (Int -> e1
gi1 Int
i) (Int -> e2
gi2 Int
i))
    PrefIndex ix e1
_ -> forall r ix e.
Load r ix e =>
Comp -> Sz ix -> (ix -> e) -> Array r ix e
makeArray Comp
comp Sz ix
sz (\ !ix
ix -> e1 -> e2 -> e3
f (forall r e ix. (Source r e, Index ix) => Array r ix e -> ix -> e
unsafeIndex Array r1 ix e1
arr1 ix
ix) (forall r e ix. (Source r e, Index ix) => Array r ix e -> ix -> e
unsafeIndex Array r2 ix e2
arr2 ix
ix))
  where
    comp :: Comp
comp = forall r ix e. Strategy r => Array r ix e -> Comp
getComp Array r1 ix e1
arr1 forall a. Semigroup a => a -> a -> a
<> forall r ix e. Strategy r => Array r ix e -> Comp
getComp Array r2 ix e2
arr2
{-# INLINE zipWithInternal #-}