From d46df392d07779fd83b01b0e9e71e5e17a36567a Mon Sep 17 00:00:00 2001 From: Alberto Ruiz Date: Wed, 14 May 2014 14:18:30 +0200 Subject: undeprecate dot, fix <.> --- packages/hmatrix/src/Numeric/Container.hs | 96 +++++++++++---------------- packages/hmatrix/src/Numeric/ContainerBoot.hs | 2 +- packages/hmatrix/src/Numeric/HMatrix.hs | 7 +- 3 files changed, 42 insertions(+), 63 deletions(-) (limited to 'packages/hmatrix/src') diff --git a/packages/hmatrix/src/Numeric/Container.hs b/packages/hmatrix/src/Numeric/Container.hs index 3a8dd94..be2347b 100644 --- a/packages/hmatrix/src/Numeric/Container.hs +++ b/packages/hmatrix/src/Numeric/Container.hs @@ -36,11 +36,11 @@ module Numeric.Container ( -- * Generic operations Container(..), -- * Matrix product - Product(..), udot, + Product(..), udot, dot, (◇), Mul(..), Contraction(..), optimiseMult, - mXm,mXv,vXm,LSDiv(..), cdot, (·), dot, (<.>), + mXm,mXv,vXm,LSDiv(..), outer, kronecker, -- * Random numbers RandDist(..), @@ -91,18 +91,12 @@ linspace 0 (a,b) = fromList[(a+b)/2] linspace n (a,b) = addConstant a $ scale s $ fromList $ map fromIntegral [0 .. n-1] where s = (b-a)/fromIntegral (n-1) --- | 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 - -------------------------------------------------------- -class Contraction a b c | a b -> c, c -> a b +class Contraction a b c | a b -> c where - infixr 7 × - {- | Matrix-matrix product, matrix-vector product, and unconjugated dot product - -(unicode 0x00d7, multiplication sign) + infixl 7 <.> + {- | Matrix product, matrix vector product, and dot product Examples: @@ -118,7 +112,7 @@ Examples: matrix × matrix: ->>> disp 2 (a × trans a) +>>> disp 2 (a <.> trans a) 3x3 30 70 110 70 174 278 @@ -126,44 +120,38 @@ matrix × matrix: matrix × vector: ->>> a × v +>>> a <.> v fromList [3.0,11.0,19.0] -unconjugated dot product: +dot product: ->>> fromList [1,i] × fromList[2*i+1,3] -1.0 :+ 5.0 +>>> u <.> fromList[3,2,1::Double] +10 -(×) is right associative, so we can write: +For complex vectors the first argument is conjugated: ->>> u × a × v -82.0 :: Double - --} - (×) :: a -> b -> c +>>> fromList [1,i] <.> fromList[2*i+1,3] +1.0 :+ (-1.0) -instance Product t => Contraction (Matrix t) (Vector t) (Vector t) where - (×) = mXv +>>> fromList [1,i,1-i] <.> complex a +fromList [10.0 :+ 4.0,12.0 :+ 4.0,14.0 :+ 4.0,16.0 :+ 4.0] -instance Product t => Contraction (Matrix t) (Matrix t) (Matrix t) where - (×) = mXm +-} + (<.>) :: a -> b -> c -instance Contraction (Vector Double) (Vector Double) Double where - (×) = udot -instance Contraction (Vector Float) (Vector Float) Float where - (×) = udot +instance (Product t, Container Vector t) => Contraction (Vector t) (Vector t) t where + u <.> v = conj u `udot` v -instance Contraction (Vector (Complex Double)) (Vector (Complex Double)) (Complex Double) where - (×) = udot +instance Product t => Contraction (Matrix t) (Vector t) (Vector t) where + (<.>) = mXv -instance Contraction (Vector (Complex Float)) (Vector (Complex Float)) (Complex Float) where - (×) = udot +instance (Container Vector t, Product t) => Contraction (Vector t) (Matrix t) (Vector t) where + (<.>) v m = (conj v) `vXm` m +instance Product t => Contraction (Matrix t) (Matrix t) (Matrix t) where + (<.>) = mXm --- | alternative function for the matrix product (×) -mmul :: Contraction a b c => a -> b -> c -mmul = (×) -------------------------------------------------------------------------------- @@ -194,23 +182,8 @@ instance LSDiv Vector where instance LSDiv Matrix where (<\>) = linearSolveSVD --------------------------------------------------------- - -{- | Dot product : @u · v = 'cdot' u v@ - - (unicode 0x00b7, middle dot, Alt-Gr .) - ->>> fromList [1,i] · fromList[2*i+1,3] -1.0 :+ (-1.0) - --} -(·) :: (Container Vector t, Product t) => Vector t -> Vector t -> t -infixl 7 · -u · v = cdot u v - -------------------------------------------------------------------------------- --- bidirectional type inference class Konst e d c | d -> c, c -> d where -- | @@ -282,12 +255,17 @@ meanCov x = (med,cov) where -------------------------------------------------------------------------------- -{-# DEPRECATED dot "use udot" #-} -dot :: Product e => Vector e -> Vector e -> e -dot = udot +{- | alternative operator for '(\<.\>)' + +x25c7, white diamond + +-} +(◇) :: Contraction a b c => a -> b -> c +infixl 7 ◇ +(◇) = (<.>) + +-- | dot product: @cdot u v = 'udot' ('conj' u) v@ +dot :: (Container Vector t, Product t) => Vector t -> Vector t -> t +dot u v = udot (conj u) v --- | contraction operator, equivalent to (x) -infixr 7 <.> -(<.>) :: Contraction a b c => a -> b -> c -(<.>) = (×) diff --git a/packages/hmatrix/src/Numeric/ContainerBoot.hs b/packages/hmatrix/src/Numeric/ContainerBoot.hs index ef21763..bb65166 100644 --- a/packages/hmatrix/src/Numeric/ContainerBoot.hs +++ b/packages/hmatrix/src/Numeric/ContainerBoot.hs @@ -403,7 +403,7 @@ emptyVal f v = else 0 -- FIXME remove unused C wrappers --- | (unconjugated) dot product +-- | unconjugated dot product udot :: Product e => Vector e -> Vector e -> e udot u v | dim u == dim v = val (asRow u `multiply` asColumn v) diff --git a/packages/hmatrix/src/Numeric/HMatrix.hs b/packages/hmatrix/src/Numeric/HMatrix.hs index 2e01454..c8742c4 100644 --- a/packages/hmatrix/src/Numeric/HMatrix.hs +++ b/packages/hmatrix/src/Numeric/HMatrix.hs @@ -39,7 +39,7 @@ module Numeric.HMatrix ( -- -- * Products - (×), + (<.>), -- | The matrix product is also implemented in the "Data.Monoid" instance for Matrix, where -- single-element matrices (created from numeric literals or using 'scalar') @@ -53,7 +53,8 @@ module Numeric.HMatrix ( -- -- mconcat uses 'optimiseMult' to get the optimal association order. - (·), outer, kronecker, cross, + (◇), + outer, kronecker, cross, scale, sumElements, prodElements, absSum, @@ -123,7 +124,7 @@ module Numeric.HMatrix ( rand, randn, RandDist(..), randomVector, gaussianSample, uniformSample, -- * Misc - meanCov, peps, relativeError, haussholder, optimiseMult, udot, cdot, (<.>) + meanCov, peps, relativeError, haussholder, optimiseMult, udot ) where import Numeric.HMatrix.Data -- cgit v1.2.3