From 6fbed842525491e280448a00a4b5426e6830ccaa Mon Sep 17 00:00:00 2001 From: Alberto Ruiz Date: Thu, 24 Apr 2014 10:30:01 +0200 Subject: cdot and (×) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit added cdot dot renamed to udot <.> changed to cdot and moved to Numeric.LinearAlgebra.Util new general contraction operator (×) Plot functions moved to Numeric.LinearAlgebra.Util --- lib/Numeric/Container.hs | 49 ++++++++++++++++++++++----- lib/Numeric/ContainerBoot.hs | 12 +++---- lib/Numeric/LinearAlgebra/Util.hs | 24 ++++++++----- lib/Numeric/LinearAlgebra/Util/Convolution.hs | 2 +- 4 files changed, 63 insertions(+), 24 deletions(-) (limited to 'lib/Numeric') diff --git a/lib/Numeric/Container.hs b/lib/Numeric/Container.hs index 345c1f1..ed6714f 100644 --- a/lib/Numeric/Container.hs +++ b/lib/Numeric/Container.hs @@ -8,8 +8,8 @@ ----------------------------------------------------------------------------- -- | -- Module : Numeric.Container --- Copyright : (c) Alberto Ruiz 2010 --- License : GPL-style +-- Copyright : (c) Alberto Ruiz 2010-14 +-- License : GPL -- -- Maintainer : Alberto Ruiz -- Stability : provisional @@ -35,8 +35,9 @@ module Numeric.Container ( Container(..), -- * Matrix product Product(..), + Contraction(..), optimiseMult, - mXm,mXv,vXm,(<.>),Mul(..),LSDiv(..), + mXm,mXv,vXm,Mul(..),LSDiv(..), cdot, outer, kronecker, -- * Random numbers RandDist(..), @@ -95,12 +96,9 @@ linspace :: (Enum e, Container Vector e) => Int -> (e, e) -> Vector e linspace n (a,b) = addConstant a $ scale s $ fromList [0 .. fromIntegral n-1] where s = (b-a)/fromIntegral (n-1) --- | Dot product: @u \<.\> v = dot u v@ -(<.>) :: Product t => Vector t -> Vector t -> t -infixl 7 <.> -(<.>) = dot - - +-- | dot product: @cdot u v = 'udot' ('conj' u) v@ +cdot :: (Container Vector t, Product t) => Vector t -> Vector t -> t +cdot u v = udot (conj u) v -------------------------------------------------------- @@ -143,3 +141,36 @@ meanCov x = (med,cov) where xc = x `sub` meds cov = scale (recip (fromIntegral (r-1))) (trans xc `mXm` xc) +-------------------------------------------------------------------------------- + +-- | matrix-matrix product, matrix-vector product, unconjugated dot product, and scaling +class Contraction a b c | a b -> c + where + -- ^ 0x00d7 multiplication sign + infixl 7 × + (×) :: a -> b -> c + +instance Product t => Contraction (Vector t) (Vector t) t where + (×) = udot + +instance Product t => Contraction (Matrix t) (Vector t) (Vector t) where + (×) = mXv + +instance Product t => Contraction (Vector t) (Matrix t) (Vector t) where + (×) = vXm + +instance Product t => Contraction (Matrix t) (Matrix t) (Matrix t) where + (×) = mXm + +instance Container Vector t => Contraction t (Vector t) (Vector t) where + (×) = scale + +instance Container Vector t => Contraction (Vector t) t (Vector t) where + (×) = flip scale + +instance Container Matrix t => Contraction t (Matrix t) (Matrix t) where + (×) = scale + +instance Container Matrix t => Contraction (Matrix t) t (Matrix t) where + (×) = flip scale + diff --git a/lib/Numeric/ContainerBoot.hs b/lib/Numeric/ContainerBoot.hs index dcb326c..4c5bbd0 100644 --- a/lib/Numeric/ContainerBoot.hs +++ b/lib/Numeric/ContainerBoot.hs @@ -327,8 +327,8 @@ instance (Container Vector a) => Container Matrix a where class Element e => Product e where -- | matrix product multiply :: Matrix e -> Matrix e -> Matrix e - -- | dot (inner) product - dot :: Vector e -> Vector e -> e + -- | (unconjugated) dot product + udot :: Vector e -> Vector e -> e -- | sum of absolute value of elements (differs in complex case from @norm1@) absSum :: Vector e -> RealOf e -- | sum of absolute value of elements @@ -341,7 +341,7 @@ class Element e => Product e where instance Product Float where norm2 = toScalarF Norm2 absSum = toScalarF AbsSum - dot = dotF + udot = dotF norm1 = toScalarF AbsSum normInf = maxElement . vectorMapF Abs multiply = multiplyF @@ -349,7 +349,7 @@ instance Product Float where instance Product Double where norm2 = toScalarR Norm2 absSum = toScalarR AbsSum - dot = dotR + udot = dotR norm1 = toScalarR AbsSum normInf = maxElement . vectorMapR Abs multiply = multiplyR @@ -357,7 +357,7 @@ instance Product Double where instance Product (Complex Float) where norm2 = toScalarQ Norm2 absSum = toScalarQ AbsSum - dot = dotQ + udot = dotQ norm1 = sumElements . fst . fromComplex . vectorMapQ Abs normInf = maxElement . fst . fromComplex . vectorMapQ Abs multiply = multiplyQ @@ -365,7 +365,7 @@ instance Product (Complex Float) where instance Product (Complex Double) where norm2 = toScalarC Norm2 absSum = toScalarC AbsSum - dot = dotC + udot = dotC norm1 = sumElements . fst . fromComplex . vectorMapC Abs normInf = maxElement . fst . fromComplex . vectorMapC Abs multiply = multiplyC diff --git a/lib/Numeric/LinearAlgebra/Util.hs b/lib/Numeric/LinearAlgebra/Util.hs index f7c40d7..f6aa7da 100644 --- a/lib/Numeric/LinearAlgebra/Util.hs +++ b/lib/Numeric/LinearAlgebra/Util.hs @@ -19,10 +19,11 @@ module Numeric.LinearAlgebra.Util( diagl, row, col, - (&),(!), (¦), (#), - (?),(¿), + (&), (¦), (#), + (?), (¿), rand, randn, cross, + (<.>), norm, unitary, mt, @@ -45,7 +46,13 @@ module Numeric.LinearAlgebra.Util( vec, vech, dup, - vtrans + vtrans, + -- * Plot + mplot, + plot, parametricPlot, + splot, mesh, meshdom, + matrixToPGM, imshow, + gnuplotX, gnuplotpdf, gnuplotWin ) where import Numeric.Container @@ -55,6 +62,7 @@ import Numeric.Vector() import System.Random(randomIO) import Numeric.LinearAlgebra.Util.Convolution +import Graphics.Plot disp :: Int -> Matrix Double -> IO () @@ -99,11 +107,6 @@ infixl 3 & (&) :: Vector Double -> Vector Double -> Vector Double a & b = vjoin [a,b] --- | horizontal concatenation of real matrices -infixl 3 ! -(!) :: Matrix Double -> Matrix Double -> Matrix Double -a ! b = fromBlocks [[a,b]] - -- | (00A6) horizontal concatenation of real matrices infixl 3 ¦ (¦) :: Matrix Double -> Matrix Double -> Matrix Double @@ -161,6 +164,11 @@ size m = (rows m, cols m) mt :: Matrix Double -> Matrix Double mt = trans . inv +-- | dot product: @u \<.\> v = 'cdot' u v@ +(<.>) :: (Container Vector t, Product t) => Vector t -> Vector t -> t +infixl 7 <.> +u <.> v = cdot u v + ---------------------------------------------------------------------- -- | Matrix of pairwise squared distances of row vectors diff --git a/lib/Numeric/LinearAlgebra/Util/Convolution.hs b/lib/Numeric/LinearAlgebra/Util/Convolution.hs index be9b1eb..1043614 100644 --- a/lib/Numeric/LinearAlgebra/Util/Convolution.hs +++ b/lib/Numeric/LinearAlgebra/Util/Convolution.hs @@ -75,7 +75,7 @@ matSS dr m = map (reshape c) [ subVector (k*c) n v | k <- [0 .. r - dr] ] corr2 :: Product a => Matrix a -> Matrix a -> Matrix a -- ^ 2D correlation corr2 ker mat = dims - . concatMap (map ((<.> ker') . flatten) . matSS c . trans) + . concatMap (map (udot ker' . flatten) . matSS c . trans) . matSS r $ mat where r = rows ker -- cgit v1.2.3