From 5158a1717f1d4caee25669a0781602fe64787302 Mon Sep 17 00:00:00 2001 From: Alberto Ruiz Date: Thu, 22 May 2014 11:49:23 +0200 Subject: initial support for sparse matrix --- packages/base/src/Numeric/Container.hs | 37 +++++++++++++++---------- packages/base/src/Numeric/LinearAlgebra.hs | 14 +++++----- packages/base/src/Numeric/LinearAlgebra/Data.hs | 7 ++++- 3 files changed, 35 insertions(+), 23 deletions(-) (limited to 'packages/base/src/Numeric') diff --git a/packages/base/src/Numeric/Container.hs b/packages/base/src/Numeric/Container.hs index 264a619..0633640 100644 --- a/packages/base/src/Numeric/Container.hs +++ b/packages/base/src/Numeric/Container.hs @@ -32,11 +32,11 @@ module Numeric.Container ( diag, ident, ctrans, -- * Generic operations - Container(..), + Container(..), Transposable(..), Linear(..), -- * Matrix product Product(..), udot, dot, (◇), Mul(..), - Contraction(..), + Contraction(..),(<.>), optimiseMult, mXm,mXv,vXm,LSDiv(..), outer, kronecker, @@ -55,7 +55,9 @@ module Numeric.Container ( IndexOf, module Data.Complex, -- * IO - module Data.Packed.IO + module Data.Packed.IO, + -- * Misc + Testable(..) ) where import Data.Packed hiding (stepD, stepF, condD, condF, conjugateC, conjugateQ) @@ -87,10 +89,9 @@ linspace n (a,b) = addConstant a $ scale s $ fromList $ map fromIntegral [0 .. n -------------------------------------------------------- -class Contraction a b c | a b -> c - where - infixl 7 <.> - {- | Matrix product, matrix - vector product, and dot product +{- | Matrix product, matrix - vector product, and dot product (equivalent to 'contraction') + +(This operator can also be written using the unicode symbol ◇ (25c7).) Examples: @@ -129,22 +130,28 @@ For complex vectors the first argument is conjugated: >>> fromList [1,i,1-i] <.> complex a fromList [10.0 :+ 4.0,12.0 :+ 4.0,14.0 :+ 4.0,16.0 :+ 4.0] - -} - (<.>) :: a -> b -> c +infixl 7 <.> +(<.>) :: Contraction a b c => a -> b -> c +(<.>) = contraction +class Contraction a b c | a b -> c + where + -- | Matrix product, matrix - vector product, and dot product + contraction :: a -> b -> c + instance (Product t, Container Vector t) => Contraction (Vector t) (Vector t) t where - u <.> v = conj u `udot` v + u `contraction` v = conj u `udot` v instance Product t => Contraction (Matrix t) (Vector t) (Vector t) where - (<.>) = mXv + contraction = mXv instance (Container Vector t, Product t) => Contraction (Vector t) (Matrix t) (Vector t) where - (<.>) v m = (conj v) `vXm` m + contraction v m = (conj v) `vXm` m instance Product t => Contraction (Matrix t) (Matrix t) (Matrix t) where - (<.>) = mXm + contraction = mXm -------------------------------------------------------------------------------- @@ -229,10 +236,10 @@ instance Container Matrix e => Build (Int,Int) (e -> e -> e) Matrix e -------------------------------------------------------------------------------- --- | alternative unicode symbol (25c7) for the contraction operator '(\<.\>)' +-- | alternative unicode symbol (25c7) for 'contraction' (◇) :: Contraction a b c => a -> b -> c infixl 7 ◇ -(◇) = (<.>) +(◇) = contraction -- | dot product: @cdot u v = 'udot' ('conj' u) v@ dot :: (Container Vector t, Product t) => Vector t -> Vector t -> t diff --git a/packages/base/src/Numeric/LinearAlgebra.hs b/packages/base/src/Numeric/LinearAlgebra.hs index 96bf29f..549ebd0 100644 --- a/packages/base/src/Numeric/LinearAlgebra.hs +++ b/packages/base/src/Numeric/LinearAlgebra.hs @@ -37,11 +37,11 @@ module Numeric.LinearAlgebra ( -- * Matrix product (<.>), - - -- | This operator can also be written using the unicode symbol ◇ (25c7). + + -- | The overloaded multiplication operator may need type annotations to remove + -- ambiguity. In those cases we can also use the specific functions 'mXm', 'mXv', and 'dot'. -- - - -- | The matrix x matrix product is also implemented in the "Data.Monoid" instance, where + -- The matrix x matrix product is also implemented in the "Data.Monoid" instance, where -- single-element matrices (created from numeric literals or using 'scalar') -- are used for scaling. -- @@ -52,6 +52,7 @@ module Numeric.LinearAlgebra ( -- , 4.0, 10.0, 0.0 ] -- -- 'mconcat' uses 'optimiseMult' to get the optimal association order. + -- * Other products outer, kronecker, cross, @@ -125,7 +126,7 @@ module Numeric.LinearAlgebra ( RandDist(..), randomVector, rand, randn, gaussianSample, uniformSample, -- * Misc - meanCov, peps, relativeError, haussholder, optimiseMult, udot, Seed, (◇) + meanCov, peps, relativeError, haussholder, optimiseMult, dot, udot, mXm, mXv, smXv, (<>), (◇), Seed, checkT ) where import Numeric.LinearAlgebra.Data @@ -136,6 +137,5 @@ import Numeric.Container import Numeric.LinearAlgebra.Algorithms import Numeric.LinearAlgebra.Util import Numeric.LinearAlgebra.Random - - +import Data.Packed.Internal.Sparse(smXv) diff --git a/packages/base/src/Numeric/LinearAlgebra/Data.hs b/packages/base/src/Numeric/LinearAlgebra/Data.hs index 7e8af03..49bc1c0 100644 --- a/packages/base/src/Numeric/LinearAlgebra/Data.hs +++ b/packages/base/src/Numeric/LinearAlgebra/Data.hs @@ -48,6 +48,10 @@ module Numeric.LinearAlgebra.Data( latexFormat, dispf, disps, dispcf, format, + -- * Sparse + SMatrix, AssocMatrix, mkCSR, toDense, + mkDiag, + -- * Conversion Convert(..), @@ -56,7 +60,7 @@ module Numeric.LinearAlgebra.Data( rows, cols, separable, - module Data.Complex + module Data.Complex, ) where @@ -65,4 +69,5 @@ import Data.Packed.Matrix import Numeric.Container import Numeric.LinearAlgebra.Util import Data.Complex +import Data.Packed.Internal.Sparse -- cgit v1.2.3