@@ -94,17 +94,20 @@ size = product . shapeL
9494-- In the linearization of the array the outermost (i.e. first list element)
9595-- varies most slowly.
9696-- O(1) time.
97+ {-# INLINE shapeL #-}
9798shapeL :: Array n a -> ShapeL
9899shapeL = G. shapeL . unA
99100
100101-- | The rank of an array, i.e., the number of dimensions it has,
101102-- which is the @n@ in @Array n a@.
102103-- O(1) time.
104+ {-# INLINE rank #-}
103105rank :: (KnownNat n ) => Array n a -> Int
104106rank = G. rank . unA
105107
106108-- | Index into an array. Fails if the index is out of bounds.
107109-- O(1) time.
110+ {-# INLINABLE index #-}
108111index :: (Unbox a ) => Array (1 + n ) a -> Int -> Array n a
109112index a = A . G. index (unA a)
110113
@@ -138,6 +141,7 @@ fromVector ss = A . G.fromVector ss
138141-- This is semantically an identity function, but can have big performance
139142-- implications.
140143-- O(n) or O(1) time.
144+ {-# INLINABLE normalize #-}
141145normalize :: (Unbox a , KnownNat n ) => Array n a -> Array n a
142146normalize = A . G. normalize . unA
143147
@@ -150,20 +154,24 @@ reshape s = A . G.reshape s . unA
150154-- | Change the size of dimensions with size 1. These dimension can be changed to any size.
151155-- All other dimensions must remain the same.
152156-- O(1) time.
157+ {-# INLINABLE stretch #-}
153158stretch :: ShapeL -> Array n a -> Array n a
154159stretch s = A . G. stretch s . unA
155160
156161-- | Change the size of the outermost dimension by replication.
162+ {-# INLINABLE stretchOuter #-}
157163stretchOuter :: (HasCallStack , 1 <= n ) => Int -> Array n a -> Array n a
158164stretchOuter s = A . G. stretchOuter s . unA
159165
160166-- | Convert a value to a scalar (rank 0) array.
161167-- O(1) time.
168+ {-# INLINE scalar #-}
162169scalar :: (Unbox a ) => a -> Array 0 a
163170scalar = A . G. scalar
164171
165172-- | Convert a scalar (rank 0) array to a value.
166173-- O(1) time.
174+ {-# INLINE unScalar #-}
167175unScalar :: (Unbox a ) => Array 0 a -> a
168176unScalar = G. unScalar . unA
169177
@@ -182,18 +190,21 @@ mapA f = A . G.mapA f . unA
182190
183191-- | Map over the array elements.
184192-- O(n) time.
193+ {-# INLINABLE zipWithA #-}
185194zipWithA :: (Unbox a , Unbox b , Unbox c ) =>
186195 (a -> b -> c ) -> Array n a -> Array n b -> Array n c
187196zipWithA f a b = A $ G. zipWithA f (unA a) (unA b)
188197
189198-- | Map over the array elements.
190199-- O(n) time.
200+ {-# INLINABLE zipWith3A #-}
191201zipWith3A :: (Unbox a , Unbox b , Unbox c , Unbox d ) =>
192202 (a -> b -> c -> d ) -> Array n a -> Array n b -> Array n c -> Array n d
193203zipWith3A f a b c = A $ G. zipWith3A f (unA a) (unA b) (unA c)
194204
195205-- | Pad each dimension on the low and high side with the given value.
196206-- O(n) time.
207+ {-# INLINABLE pad #-}
197208pad :: (Unbox a , KnownNat n ) => [(Int , Int )] -> a -> Array n a -> Array n a
198209pad ps v = A . G. pad ps v . unA
199210
@@ -215,6 +226,7 @@ append x y = A $ G.append (unA x) (unA y)
215226-- | Concatenate a number of arrays into a single array.
216227-- Fails if any, but the outer, dimensions differ.
217228-- O(n) time.
229+ {-# INLINABLE concatOuter #-}
218230concatOuter :: (Unbox a , KnownNat n ) => [Array n a ] -> Array n a
219231concatOuter = A . G. concatOuter . coerce
220232
@@ -244,19 +256,22 @@ unravel = R.A . G.mapA A . G.unravel . unA
244256--
245257-- If the window parameter @ws = [w1,...,wk]@ and @wa = window ws a@ then
246258-- @wa `index` i1 ... `index` ik == slice [(i1,w1),...,(ik,wk)] a@.
259+ {-# INLINABLE window #-}
247260window :: (KnownNat n , KnownNat n' ) => [Int ] -> Array n a -> Array n' a
248261window ws = A . G. window ws . unA
249262
250263-- | Stride the outermost dimensions.
251264-- E.g., if the array shape is @[10,12,8]@ and the strides are
252265-- @[2,2]@ then the resulting shape will be @[5,6,8]@.
253266-- O(1) time.
267+ {-# INLINABLE stride #-}
254268stride :: [Int ] -> Array n a -> Array n a
255269stride ws = A . G. stride ws . unA
256270
257271-- | Rotate the array k times along the d'th dimension.
258272-- E.g., if the array shape is @[2, 3, 2]@, d is 1, and k is 4,
259273-- the resulting shape will be @[2, 4, 3, 2]@.
274+ {-# INLINABLE rotate #-}
260275rotate :: forall d p a .
261276 (KnownNat p , KnownNat d , Unbox a ,
262277 -- Nonsense
@@ -276,13 +291,15 @@ rotate k = A . G.rotate @d @p k . unA
276291-- The extracted slice must fall within the array dimensions.
277292-- E.g. @slice [1,2] (fromList [4] [1,2,3,4]) == [2,3]@.
278293-- O(1) time.
294+ {-# INLINABLE slice #-}
279295slice :: [(Int , Int )] -> Array n a -> Array n a
280296slice ss = A . G. slice ss . unA
281297
282298-- | Apply a function to the subarrays /n/ levels down and make
283299-- the results into an array with the same /n/ outermost dimensions.
284300-- The /n/ must not exceed the rank of the array.
285301-- O(1) time.
302+ {-# INLINABLE rerank #-}
286303rerank :: forall n i o a b .
287304 (Unbox a , Unbox b , KnownNat n , KnownNat o , KnownNat (n + o ), KnownNat (1 + o )) =>
288305 (Array i a -> Array o b ) -> Array (n + i ) a -> Array (n + o ) b
@@ -292,37 +309,44 @@ rerank f = A . G.rerank (unA . f . A) . unA
292309-- the results into an array with the same /n/ outermost dimensions.
293310-- The /n/ must not exceed the rank of the array.
294311-- O(n) time.
312+ {-# INLINABLE rerank2 #-}
295313rerank2 :: forall n i o a b c .
296314 (Unbox a , Unbox b , Unbox c , KnownNat n , KnownNat o , KnownNat (n + o ), KnownNat (1 + o )) =>
297315 (Array i a -> Array i b -> Array o c ) -> Array (n + i ) a -> Array (n + i ) b -> Array (n + o ) c
298316rerank2 f ta tb = A $ G. rerank2 @ n (\ a b -> unA $ f (A a) (A b)) (unA ta) (unA tb)
299317
300318-- | Reverse the given dimensions, with the outermost being dimension 0.
301319-- O(1) time.
320+ {-# INLINABLE rev #-}
302321rev :: [Int ] -> Array n a -> Array n a
303322rev rs = A . G. rev rs . unA
304323
305324-- | Reduce all elements of an array into a rank 0 array.
306325-- To reduce parts use 'rerank' and 'transpose' together with 'reduce'.
307326-- O(n) time.
327+ {-# INLINABLE reduce #-}
308328reduce :: (Unbox a ) => (a -> a -> a ) -> a -> Array n a -> Array 0 a
309329reduce f z = A . G. reduce f z . unA
310330
311331-- | Constrained version of 'foldr' for Arrays.
332+ {-# INLINABLE foldrA #-}
312333foldrA :: (Unbox a , Unbox b ) => (a -> b -> b ) -> b -> Array n a -> b
313334foldrA f z = G. foldrA f z . unA
314335
315336-- | Constrained version of 'traverse' for Arrays.
337+ {-# INLINABLE traverseA #-}
316338traverseA
317339 :: (Unbox a , Unbox b , Applicative f )
318340 => (a -> f b ) -> Array n a -> f (Array n b )
319341traverseA f = fmap A . G. traverseA f . unA
320342
321343-- | Check if all elements of the array are equal.
344+ {-# INLINABLE allSameA #-}
322345allSameA :: (Unbox a , Eq a ) => Array n a -> Bool
323346allSameA = G. allSameA . unA
324347
325- instance (KnownNat r , Arbitrary a , Unbox a ) => Arbitrary (Array r a ) where arbitrary = A <$> arbitrary
348+ instance (KnownNat r , Arbitrary a , Unbox a ) => Arbitrary (Array r a ) where
349+ arbitrary = A <$> arbitrary
326350
327351-- | Sum of all elements.
328352{-# INLINE sumA #-}
@@ -358,6 +382,7 @@ allA p = G.allA p . unA
358382-- and just replicate the data along all other dimensions.
359383-- The list of dimensions indicies must have the same rank as the argument array
360384-- and it must be strictly ascending.
385+ {-# INLINABLE broadcast #-}
361386broadcast :: forall r' r a .
362387 (HasCallStack , Unbox a , KnownNat r , KnownNat r' ) =>
363388 [Int ] -> ShapeL -> Array r a -> Array r' a
0 commit comments