Skip to content
Browse files

rename module to Util, some changes, add cross product and disp

  • Loading branch information...
1 parent 1a6d316 commit b0424cacf0453d8294fb37ab1325be256fd7be3e @albertoruiz committed Mar 19, 2012
Showing with 99 additions and 118 deletions.
  1. +1 −1 hmatrix.cabal
  2. +0 −117 lib/Numeric/LinearAlgebra/Real.hs
  3. +98 −0 lib/Numeric/LinearAlgebra/Util.hs
View
2 hmatrix.cabal
@@ -110,7 +110,7 @@ library
Numeric.LinearAlgebra,
Numeric.LinearAlgebra.LAPACK,
Numeric.LinearAlgebra.Algorithms,
- Numeric.LinearAlgebra.Real,
+ Numeric.LinearAlgebra.Util,
Graphics.Plot,
Data.Packed.ST,
Data.Packed.Development
View
117 lib/Numeric/LinearAlgebra/Real.hs
@@ -1,117 +0,0 @@
------------------------------------------------------------------------------
-{- |
-Module : Numeric.LinearAlgebra.Real
-Copyright : (c) Alberto Ruiz 2012
-License : GPL
-
-Maintainer : Alberto Ruiz (aruiz at um dot es)
-Stability : provisional
-
-Additional functions for real arrays.
-
--}
------------------------------------------------------------------------------
-
-module Numeric.LinearAlgebra.Real(
- (<>), (<\>),
- vector,
- linspace,
- eye,
- zeros, ones,
- diagl,
- row,
- col,
- (#),(&), (//), blocks,
- rand, randn,
- module Numeric.LinearAlgebra
-) where
-
-import Numeric.LinearAlgebra hiding ((<>), (<\>), linspace)
-import qualified Numeric.LinearAlgebra as LA
-import System.Random(randomIO)
-
-linspace :: Int -> (Double,Double) -> Vector Double
-linspace = LA.linspace
-
-
-infixl 7 <>
--- | Matrix product
-(<>) ::Mul a b c => a Double -> b Double -> c Double
-(<>) = (LA.<>)
-
-
-infixl 7 <\>
--- | Least squares solution of a linear system
-(<\>) ::LSDiv b c => Matrix Double -> b Double -> c Double
-(<\>) = (LA.<\>)
-
-
--- | Pseudorandom matrix with uniform elements between 0 and 1.
-randm :: RandDist
- -> Int -- ^ rows
- -> Int -- ^ columns
- -> IO (Matrix Double)
-randm d r c = do
- seed <- randomIO
- return (reshape c $ randomVector seed d (r*c))
-
--- | Pseudorandom matrix with uniform elements between 0 and 1.
-rand :: Int -> Int -> IO (Matrix Double)
-rand = randm Uniform
-
--- | Pseudorandom matrix with normal elements
-randn :: Int -> Int -> IO (Matrix Double)
-randn = randm Gaussian
-
--- | Real identity matrix.
-eye :: Int -> Matrix Double
-eye = ident
-
--- | Create a real vector from a list.
-vector :: [Double] -> Vector Double
-vector = fromList
-
--- | Create a real diagonal matrix from a list.
-diagl :: [Double] -> Matrix Double
-diagl = diag . vector
-
--- | Create a matrix or zeros.
-zeros :: Int -- ^ rows
- -> Int -- ^ columns
- -> Matrix Double
-zeros r c = konst 0 (r,c)
-
--- | Create a matrix or ones.
-ones :: Int -- ^ rows
- -> Int -- ^ columns
- -> Matrix Double
-ones r c = konst 1 (r,c)
-
--- | Concatenation of real vectors.
-infixl 3 #
-(#) :: Vector Double -> Vector Double -> Vector Double
-a # b = join [a,b]
-
--- | Horizontal concatenation of real matrices.
-infixl 3 !
-(!) :: Matrix Double -> Matrix Double -> Matrix Double
-a ! b = fromBlocks [[a,b]]
-
--- | Vertical concatenation of real matrices.
-(#) :: Matrix Double -> Matrix Double -> Matrix Double
-infixl 2 #
-a # b = fromBlocks [[a],[b]]
-
-
--- | Real block matrix from a rectangular list of lists.
-blocks :: [[Matrix Double]] -> Matrix Double
-blocks = fromBlocks
-
--- | A real matrix with a single row, created from a list of elements.
-row :: [Double] -> Matrix Double
-row = asRow . vector
-
--- | A real matrix with a single column, created from a list of elements.
-col :: [Double] -> Matrix Double
-col = asColumn . vector
-
View
98 lib/Numeric/LinearAlgebra/Util.hs
@@ -0,0 +1,98 @@
+-----------------------------------------------------------------------------
+{- |
+Module : Numeric.LinearAlgebra.Util
+Copyright : (c) Alberto Ruiz 2012
+License : GPL
+
+Maintainer : Alberto Ruiz (aruiz at um dot es)
+Stability : provisional
+
+-}
+-----------------------------------------------------------------------------
+
+module Numeric.LinearAlgebra.Util(
+ disp,
+ zeros, ones,
+ diagl,
+ row,
+ col,
+ (&),(!), (#),
+ rand, randn,
+ cross
+) where
+
+import Numeric.LinearAlgebra
+import System.Random(randomIO)
+
+
+disp :: Int -> Matrix Double -> IO ()
+-- ^ show a matrix with given number of digits after the decimal point
+disp n = putStrLn . dispf n
+
+-- | pseudorandom matrix with uniform elements between 0 and 1
+randm :: RandDist
+ -> Int -- ^ rows
+ -> Int -- ^ columns
+ -> IO (Matrix Double)
+randm d r c = do
+ seed <- randomIO
+ return (reshape c $ randomVector seed d (r*c))
+
+-- | pseudorandom matrix with uniform elements between 0 and 1
+rand :: Int -> Int -> IO (Matrix Double)
+rand = randm Uniform
+
+-- | pseudorandom matrix with normal elements
+randn :: Int -> Int -> IO (Matrix Double)
+randn = randm Gaussian
+
+-- | create a real diagonal matrix from a list
+diagl :: [Double] -> Matrix Double
+diagl = diag . fromList
+
+-- | a real matrix of zeros
+zeros :: Int -- ^ rows
+ -> Int -- ^ columns
+ -> Matrix Double
+zeros r c = konst 0 (r,c)
+
+-- | a real matrix of ones
+ones :: Int -- ^ rows
+ -> Int -- ^ columns
+ -> Matrix Double
+ones r c = konst 1 (r,c)
+
+-- | concatenation of real vectors
+infixl 3 &
+(&) :: Vector Double -> Vector Double -> Vector Double
+a & b = join [a,b]
+
+-- | horizontal concatenation of real matrices
+infixl 3 !
+(!) :: Matrix Double -> Matrix Double -> Matrix Double
+a ! b = fromBlocks [[a,b]]
+
+-- | vertical concatenation of real matrices
+(#) :: Matrix Double -> Matrix Double -> Matrix Double
+infixl 2 #
+a # b = fromBlocks [[a],[b]]
+
+-- | create a single row real matrix from a list
+row :: [Double] -> Matrix Double
+row = asRow . fromList
+
+-- | create a single column real matrix from a list
+col :: [Double] -> Matrix Double
+col = asColumn . fromList
+
+cross :: Vector Double -> Vector Double -> Vector Double
+-- ^ cross product of dimension 3 real vectors
+cross x y | dim x == 3 && dim y == 3 = fromList [z1,z2,z3]
+ | otherwise = error $ "cross ("++show x++") ("++show y++")"
+ where
+ [x1,x2,x3] = toList x
+ [y1,y2,y3] = toList y
+ z1 = x2*y3-x3*y2
+ z2 = x3*y1-x1*y3
+ z3 = x1*y2-x2*y1
+

0 comments on commit b0424ca

Please sign in to comment.
Something went wrong with that request. Please try again.