Add vector & matrix modules

This commit is contained in:
Kiana Sheibani 2022-05-21 16:38:21 -04:00
parent 1e7660b1f2
commit 861f1e29f2
Signed by: toki
GPG key ID: 6CB106C25E86A9F7
9 changed files with 236 additions and 34 deletions

View file

@ -78,15 +78,10 @@ rank : Array s a -> Nat
rank = length . shape
--------------------------------------------------------------------------------
-- Array constructors
--------------------------------------------------------------------------------
shapeEq : (arr : Array s a) -> s = shape arr
shapeEq (MkArray _ _ _ _) = Refl
-- Get a list of all coordinates
getAllCoords' : Vect rk Nat -> List (Vect rk Nat)
getAllCoords' = traverse (\case Z => []; S n => [0..n])
@ -97,11 +92,35 @@ getAllCoords (Z :: s) = []
getAllCoords (S d :: s) = [| forget (allFins d) :: getAllCoords s |]
constant' : (s : Vect rk Nat) -> (ord : Order) -> a -> Array s a
constant' s ord x = MkArray ord (calcStrides ord s) s (create (product s) (const x))
--------------------------------------------------------------------------------
-- Array constructors
--------------------------------------------------------------------------------
constant : (s : Vect rk Nat) -> a -> Array s a
constant s = constant' s COrder
||| Create an array by repeating a single value.
|||
||| @ s The shape of the constructed array
||| @ ord The order of the constructed array
export
repeat' : (s : Vect rk Nat) -> (ord : Order) -> a -> Array s a
repeat' s ord x = MkArray ord (calcStrides ord s) s (constant (product s) x)
||| Create an array by repeating a single value.
||| To specify the order of the array, use `repeat'`.
|||
||| @ s The shape of the constructed array
export
repeat : (s : Vect rk Nat) -> a -> Array s a
repeat s = repeat' s COrder
export
zeros : Num a => (s : Vect rk Nat) -> Array s a
zeros s = repeat s 0
export
ones : Num a => (s : Vect rk Nat) -> Array s a
ones s = repeat s 1
||| Create an array given a vector of its elements. The elements of the vector
||| are arranged into the provided shape using the provided order.
@ -113,15 +132,31 @@ fromVect' : (s : Vect rk Nat) -> (ord : Order) -> Vect (product s) a -> Array s
fromVect' s ord v = MkArray ord (calcStrides ord s) s (fromList $ toList v)
||| Create an array given a vector of its elements. The elements of the vector
||| are assembled into the provided shape using row-major order (the last axis is the
||| are arranged into the provided shape using row-major order (the last axis is the
||| least significant).
||| To specify the order of the array, see `fromVect'`.
||| To specify the order of the array, use `fromVect'`.
|||
||| @ s The shape of the constructed array
export
fromVect : (s : Vect rk Nat) -> Vect (product s) a -> Array s a
fromVect s = fromVect' s COrder
||| Create an array by taking values from a stream.
|||
||| @ s The shape of the constructed array
||| @ ord The order to interpret the elements
export
fromStream' : (s : Vect rk Nat) -> (ord : Order) -> Stream a -> Array s a
fromStream' s ord st = MkArray ord (calcStrides ord s) s (fromList $ take (product s) st)
||| Create an array by taking values from a stream.
||| To specify the order of the array, use `fromStream'`.
|||
||| @ s The shape of the constructed array
export
fromStream : (s : Vect rk Nat) -> Stream a -> Array s a
fromStream s = fromStream' s COrder
||| Create an array given a function to generate its elements.
|||
||| @ s The shape of the constructed array
@ -292,7 +327,7 @@ Functor (Array s) where
export
{s : _} -> Applicative (Array s) where
pure = constant s
pure = repeat s
(<*>) = zipWith apply
export
@ -328,7 +363,7 @@ Semigroup a => Semigroup (Array s a) where
export
{s : _} -> Monoid a => Monoid (Array s a) where
neutral = constant s neutral
neutral = repeat s neutral
-- the shape must be known at runtime due to `fromInteger`. If `fromInteger`
-- were moved into its own interface, this constraint could be removed.
@ -338,7 +373,7 @@ export
(+) = zipWith (+)
(*) = zipWith (*)
fromInteger = constant s . fromInteger
fromInteger = repeat s . fromInteger
export

View file

@ -1,5 +1,6 @@
module Data.NumIdr.Array.Coords
import Data.Either
import Data.Vect
%default total
@ -42,11 +43,6 @@ namespace Coords
mapWithIndex' f (x::xs) = f Z x :: mapWithIndex' (f . S) xs
index : Coords s -> Vects s a -> a
index [] x = x
index (i::is) v = index is $ index i v
export
getLocation' : (sts : Vect rk Nat) -> (is : Vect rk Nat) -> Nat
getLocation' = sum .: zipWith (*)
@ -88,6 +84,10 @@ namespace CoordsRange
Left _ => newRank rs
Right _ => S (newRank rs)
export
newDim : {d : _} -> (r : CRange d) -> Maybe Nat
newDim = map (uncurry $ flip minus) . eitherToMaybe . cRangeToBounds
||| Calculate the new shape given by a coordinate range.
export
newShape : {s : _} -> (rs : CoordsRange s) -> Vect (newRank rs) Nat

View file

@ -1,12 +0,0 @@
module Data.NumIdr.Array.Utils
import Data.NumIdr.Array.Array
export
zeros : Num a => (s : Vect rk Nat) -> Array s a
zeros = constant 0
export
ones : Num a => (s : Vect rk Nat) -> Array s a
ones = constant 1