]> git.unchartedbackwaters.co.uk Git - francis/lta.git/commitdiff
Construct padded DFT.
authorFrancis Russell <francis@unchartedbackwaters.co.uk>
Wed, 17 Apr 2013 14:36:13 +0000 (15:36 +0100)
committerFrancis Russell <francis@unchartedbackwaters.co.uk>
Wed, 17 Apr 2013 14:36:13 +0000 (15:36 +0100)
LTA/Symbolic.hs
src/Main.hs

index 107d3bd9344e3b421794fecaf066dc1a3462d2d1..80939eecce99080ae5f93d185152a4f10552528a 100644 (file)
@@ -4,10 +4,12 @@ module LTA.Symbolic
   ( Expr(..)
   , Cond(..)
   , Constant(..)
+  , ContainsSymbols(..)
   , Literal(..)
   , CompareOp(..)
   , UnaryFunction(..)
   , buildConditional
+  , findUniqueSymbol
   , pow
   , simplify
   , (~==)
@@ -222,6 +224,11 @@ instance ContainsSymbols Cond where
     Or e1 e2 -> Set.union (findSymbols e1) (findSymbols e2)
     Not e -> findSymbols e
 
+findUniqueSymbol :: (ContainsSymbols e) => String -> e -> String
+findUniqueSymbol prefix expr = 
+  head [n | n <- generateNames, Set.notMember n syms ] where
+    syms = findSymbols expr
+    generateNames = [prefix ++ "_" ++ show n | n <- [0::Integer ..]]
 
 simplify :: Expr -> Expr
 simplify (Sum pairSeq) = rebuild $ normalise pairSeq
index 952e49dcb57314bb8c33b4bb494a09b54e5566f5..b39cd82ab9965bc5227ddf0912c7d149b82c437f 100644 (file)
@@ -2,8 +2,20 @@ module Main (main) where
 
 import LTA.Symbolic
 
+data Matrix 
+ = Matrix Expr Expr Expr
+ deriving(Eq, Ord, Show)
+
+multiply :: Matrix -> Matrix -> Matrix
+multiply (Matrix aRows aCols aExpr) (Matrix bRows bCols bExpr) = 
+  Matrix aRows bCols $ Summation sumVar 0 aCols (aExpr' * bExpr') where
+    sumVar = findUniqueSymbol "sum" (aExpr * bExpr)
+    aExpr' = rename "col" sumVar aExpr
+    bExpr' = rename "row" sumVar bExpr
+
 main :: IO()
-main = putStrLn . show $ dft (IntegerSymbol "N")
+main = putStrLn . show $ (pad size) `multiply` (dft size) where
+  size = IntegerSymbol "N"
 
 row :: Expr
 row = IntegerSymbol "row"
@@ -20,14 +32,15 @@ i = Constant ImaginaryUnit
 pi :: Expr
 pi = Constant Pi
 
-dft :: Expr -> Expr
-dft size = pow e ((- i * 2 * Main.pi * row * col) / size)
+dft :: Expr -> Matrix
+dft size = Matrix size size $ pow e ((- i * 2 * Main.pi * row * col) / size)
 
 isEven :: Expr -> Cond
 isEven n = (n `Mod` 2) ~== 0
 
-pad :: Expr -> Expr
-pad size = Conditional (isEven size) (evenPad size) (oddPad size)
+pad :: Expr -> Matrix
+pad size = 
+  Matrix (2*size) size $ Conditional (isEven size) (evenPad size) (oddPad size)
 
 evenPad :: Expr -> Expr
 evenPad size = buildConditional 0 conditions where