Refactor Mult and add MultNeutral

This commit is contained in:
Kiana Sheibani 2022-06-23 19:09:10 -04:00
parent 015b7f8cb1
commit a0d9c766c0
Signed by: toki
GPG key ID: 6CB106C25E86A9F7
4 changed files with 37 additions and 13 deletions

View file

@ -11,10 +11,6 @@ import Data.NumIdr.Array.Coords
%default total
infix 2 !!
infix 2 !?
infixl 3 !!..
infix 3 !?..
||| Arrays are the central data structure of NumIdr. They are an `n`-dimensional
||| grid of values, where `n` is a value known as the *rank* of the array. Arrays
@ -223,6 +219,10 @@ array v = MkArray COrder (calcStrides COrder s) s (fromList $ collapse v)
-- Indexing
--------------------------------------------------------------------------------
infix 10 !!
infix 10 !?
infixl 11 !!..
infix 11 !?..
||| Index the array using the given `Coords` object.
export
@ -462,13 +462,11 @@ export
export
Num a => Mult a (Array {rk} s a) where
Result = Array {rk} s a
Num a => Mult a (Array {rk} s a) (Array s a) where
(*.) x = map (*x)
export
Num a => Mult (Array {rk} s a) a where
Result = Array {rk} s a
Num a => Mult (Array {rk} s a) a (Array s a) where
(*.) = flip (*.)

View file

@ -54,12 +54,12 @@ fromDiag ds o = fromFunction [m,n] (\[i,j] => maybe o (`index` ds) $ i `eq` j)
export
identity : Num a => {n : _} -> Matrix n n a
identity : Num a => {n : _} -> Matrix' n a
identity = repeatDiag 1 0
export
scaling : Num a => {n : _} -> a -> Matrix n n a
scaling : Num a => {n : _} -> a -> Matrix' n a
scaling x = repeatDiag x 0
export

View file

@ -4,10 +4,34 @@ module Data.NumIdr.Multiply
infixr 9 *.
infixr 10 ^
||| A generalized multiplication/transformation operator. This interface is
||| necessary since the standard multiplication operator is homogenous.
public export
interface Mult a b where
0 Result : Type
(*.) : a -> b -> Result
interface Mult a b c | a,b where
(*.) : a -> b -> c
public export
interface (Mult a a a) => MultNeutral a where
neutral : a
public export
[MultSemigroup] Mult a a a => Semigroup a where
(<+>) = (*.)
public export
[MultMonoid] MultNeutral a => Monoid a using MultSemigroup where
neutral = Multiply.neutral
public export
power : MultNeutral a => Nat -> a -> a
power 0 _ = neutral
power 1 x = x
power (S n@(S _)) x = x *. power n x
public export
(^) : MultNeutral a => a -> Nat -> a
(^) = flip power

View file

@ -56,6 +56,8 @@ unit3D pol az = vector [cos az * sin pol, sin az * sin pol, cos pol]
-- Indexing
--------------------------------------------------------------------------------
infix 10 !!
infix 10 !?
export
index : Fin n -> Vector n a -> a