From e34d5566b9abfd235ce210f9f9909b23d52bd138 Mon Sep 17 00:00:00 2001 From: Alberto Ruiz Date: Mon, 17 Sep 2007 10:05:05 +0000 Subject: interface --- lib/LinearAlgebra/Algorithms.hs | 2 +- lib/LinearAlgebra/Interface.hs | 99 +++++++++++++++++++++++++++++++++++++++++ lib/LinearAlgebra/Linear.hs | 20 +-------- 3 files changed, 101 insertions(+), 20 deletions(-) create mode 100644 lib/LinearAlgebra/Interface.hs (limited to 'lib/LinearAlgebra') diff --git a/lib/LinearAlgebra/Algorithms.hs b/lib/LinearAlgebra/Algorithms.hs index 192c0d6..3112ce6 100644 --- a/lib/LinearAlgebra/Algorithms.hs +++ b/lib/LinearAlgebra/Algorithms.hs @@ -14,7 +14,7 @@ Portability : uses ffi ----------------------------------------------------------------------------- module LinearAlgebra.Algorithms ( - mXv, vXm, + -- mXv, vXm, inv, pinv, pinvTol, diff --git a/lib/LinearAlgebra/Interface.hs b/lib/LinearAlgebra/Interface.hs new file mode 100644 index 0000000..7d6ff0f --- /dev/null +++ b/lib/LinearAlgebra/Interface.hs @@ -0,0 +1,99 @@ +{-# OPTIONS_GHC -fglasgow-exts #-} +----------------------------------------------------------------------------- +{- | +Module : LinearAlgebra.Interface +Copyright : (c) Alberto Ruiz 2006 +License : GPL-style + +Maintainer : Alberto Ruiz (aruiz at um dot es) +Stability : provisional +Portability : portable + +Operators for frequent operations. + +-} +----------------------------------------------------------------------------- + +module LinearAlgebra.Interface( + (<>),(<.>), + (.*),(*/), + (<|>),(<->), +) where + +import LinearAlgebra.Linear +import Data.Packed.Vector +import Data.Packed.Matrix + +class Mul a b c | a b -> c where + infixl 7 <> + -- | matrix product + (<>) :: Field t => a t -> b t -> c t + +instance Mul Matrix Matrix Matrix where + (<>) = multiply + +instance Mul Matrix Vector Vector where + (<>) m v = flatten $ m <> (asColumn v) + +instance Mul Vector Matrix Vector where + (<>) v m = flatten $ (asRow v) <> m + +--------------------------------------------------- + +-- | @u \<.\> v = dot u v@ +(<.>) :: (Field t) => Vector t -> Vector t -> t +infixl 7 <.> +(<.>) = dot + +---------------------------------------------------- + +-- | @x .* a = scale (recip x) v@ +(.*) :: (Linear c a) => a -> c a -> c a +infixl 7 .* +a .* x = scale a x + +---------------------------------------------------- + +-- | @a *\/ x = scale (recip x) a@ +(*/) :: (Linear c a) => c a -> a -> c a +infixl 7 */ +v */ x = scale (recip x) v + +------------------------------------------------ + +class Joinable a b where + joinH :: Field t => a t -> b t -> Matrix t + joinV :: Field t => a t -> b t -> Matrix t + +instance Joinable Matrix Matrix where + joinH m1 m2 = fromBlocks [[m1,m2]] + joinV m1 m2 = fromBlocks [[m1],[m2]] + +instance Joinable Matrix Vector where + joinH m v = joinH m (asColumn v) + joinV m v = joinV m (asRow v) + +instance Joinable Vector Matrix where + joinH v m = joinH (asColumn v) m + joinV v m = joinV (asRow v) m + +infixl 4 <|> +infixl 3 <-> + +{- | Horizontal concatenation of matrices and vectors: + +@> (ident 3 -&- 3 * ident 3) |&| fromList [1..6.0] +(6><4) + [ 1.0, 0.0, 0.0, 1.0 + , 0.0, 1.0, 0.0, 2.0 + , 0.0, 0.0, 1.0, 3.0 + , 3.0, 0.0, 0.0, 4.0 + , 0.0, 3.0, 0.0, 5.0 + , 0.0, 0.0, 3.0, 6.0 ]@ +-} +(<|>) :: (Field t, Joinable a b) => a t -> b t -> Matrix t +a <|> b = joinH a b + +-- | Vertical concatenation of matrices and vectors. +(<->) :: (Field t, Joinable a b) => a t -> b t -> Matrix t +a <-> b = joinV a b diff --git a/lib/LinearAlgebra/Linear.hs b/lib/LinearAlgebra/Linear.hs index 148bbd3..c12e30b 100644 --- a/lib/LinearAlgebra/Linear.hs +++ b/lib/LinearAlgebra/Linear.hs @@ -15,8 +15,7 @@ Portability : uses ffi module LinearAlgebra.Linear ( Linear(..), - dot, outer, - Mul(..) + multiply, dot, outer ) where @@ -95,7 +94,6 @@ instance Linear Matrix (Complex Double) where -------------------------------------------------- - -- | euclidean inner product dot :: (Field t) => Vector t -> Vector t -> t dot u v = dat (multiply r c) `at` 0 @@ -113,19 +111,3 @@ dot u v = dat (multiply r c) `at` 0 -} outer :: (Field t) => Vector t -> Vector t -> Matrix t outer u v = asColumn u `multiply` asRow v - - -class Mul a b c | a b -> c where - infixl 7 <> - -- | matrix product - (<>) :: Field t => a t -> b t -> c t - -instance Mul Matrix Matrix Matrix where - (<>) = multiply - -instance Mul Matrix Vector Vector where - (<>) m v = flatten $ m <> (asColumn v) - -instance Mul Vector Matrix Vector where - (<>) v m = flatten $ (asRow v) <> m - -- cgit v1.2.3