From 2bcc462368bce221a754fcbf6a90fd5cada5fa1e Mon Sep 17 00:00:00 2001 From: Francis Russell Date: Tue, 13 Nov 2012 19:27:05 +0000 Subject: [PATCH] Add function to determine theoretical bandwidth of expressions. --- OFC/SecondLevel.hs | 76 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) diff --git a/OFC/SecondLevel.hs b/OFC/SecondLevel.hs index 97cc475..cde2b8f 100644 --- a/OFC/SecondLevel.hs +++ b/OFC/SecondLevel.hs @@ -1,4 +1,4 @@ -{-# LANGUAGE GADTs, EmptyDataDecls, StandaloneDeriving #-} +{-# LANGUAGE FlexibleInstances, GADTs, EmptyDataDecls, StandaloneDeriving #-} module OFC.SecondLevel ( SymbolTable @@ -374,3 +374,77 @@ buildPositionOperator ofl ofl2 (TopLevel.Power a b) = (buildPositionOperator ofl buildPositionOperator _ _ (TopLevel.IndexedIdentifier name indices) = OpIndexedScalarIdentifier name indices buildPositionOperator _ _ (TopLevel.PositionComponent index) = OpTerminal $ PositionComponent index buildPositionOperator _ _ e = error $ "Unhandled expression: " ++ prettyPrint e + +-- Frequency fixup + +data Bandwidth = + GMaxMultiple Integer | + Infinite + deriving (Eq, Show) + +instance Ord Bandwidth where + compare (GMaxMultiple a) (GMaxMultiple b) = compare a b + compare (GMaxMultiple _) Infinite = LT + compare Infinite (GMaxMultiple _) = GT + compare Infinite Infinite = EQ + +instance Num Bandwidth where + (+) Infinite _ = Infinite + (+) _ Infinite = Infinite + (+) (GMaxMultiple a) (GMaxMultiple b) = GMaxMultiple $ a + b + (-) _ _ = error "Unimplemented: Bandwidth subtraction" + (*) _ _ = error "Unimplemented: Bandwidth multiplication" + abs _ = error "Unimplemnted: Bandwidth abs" + signum _ = error "Unimplemnted: Bandwidth signum" + fromInteger _ = error "Unimplemented: Bandwidth fromInteger" + +theoreticalFrequency :: OFL2 -> Expression e -> Bandwidth +theoreticalFrequency ofl2 (IndexedPsincIdentifier ident _) = case getValueType ofl2 ident of + PositionT (Psinc i) -> GMaxMultiple i + _ -> error $ "Expected identifier " ++ ident ++ " to be a Psinc field." +theoreticalFrequency _ (IndexedScalarIdentifier _ _) = GMaxMultiple 0 +theoreticalFrequency ofl2 (ToMomentum e) = theoreticalFrequency ofl2 e +theoreticalFrequency ofl2 (ToPosition e) = theoreticalFrequency ofl2 e +theoreticalFrequency ofl2 (Upsample e) = theoreticalFrequency ofl2 e +theoreticalFrequency ofl2 (Downsample e) = theoreticalFrequency ofl2 e +theoreticalFrequency _ (Integrate _) = GMaxMultiple 0 +theoreticalFrequency _ (AnalyticPosition _) = Infinite +theoreticalFrequency _ (AnalyticMomentum _) = Infinite +theoreticalFrequency ofl2 (AnalyticToPsinc e _) = theoreticalFrequency ofl2 e +theoreticalFrequency ofl2 (Sum e _) = theoreticalFrequency ofl2 e +theoreticalFrequency ofl2 (Add e1 e2) = max (theoreticalFrequency ofl2 e1) (theoreticalFrequency ofl2 e2) +theoreticalFrequency ofl2 (Sub e1 e2) = max (theoreticalFrequency ofl2 e1) (theoreticalFrequency ofl2 e2) +theoreticalFrequency ofl2 (Neg e) = theoreticalFrequency ofl2 e +theoreticalFrequency ofl2 (MulScalar e _) = theoreticalFrequency ofl2 e +theoreticalFrequency ofl2 (DivScalar e _) = theoreticalFrequency ofl2 e +theoreticalFrequency ofl2 (PsincProduct a b) = (theoreticalFrequency ofl2 a) + (theoreticalFrequency ofl2 b) +theoreticalFrequency ofl2 (PsincReciprocalProduct a b) = min (theoreticalFrequency ofl2 a) (theoreticalFrequency ofl2 b) +theoreticalFrequency _ (ConstInteger _) = GMaxMultiple 0 +theoreticalFrequency _ (ConstReal _) = GMaxMultiple 0 +theoreticalFrequency _ (ConstComplex _) = GMaxMultiple 0 +theoreticalFrequency ofl2 (Power a b) = case (theoreticalFrequency ofl2 a, theoreticalFrequency ofl2 b) of + (GMaxMultiple 0, GMaxMultiple 0) -> GMaxMultiple 0 + _ -> error "Expected power operator to be between scalars." + +-- fixScalarExpression :: OFL2 -> Expression ScalarE -> Expression ScalarE +-- fixScalarExpression _ e@(IndexedScalarIdentifier _ _) = e +-- fixScalarExpression ofl2 (Integrate f) = Integrate $ fixIntegrand ofl2 f +-- fixScalarExpression ofl2 (Power a b) = Power (fixScalarExpression ofl2 a) (fixScalarExpression ofl2 b) +-- fixScalarExpression ofl2 (Sum e index) = Sum (fixScalarExpression ofl2 e) index +-- fixScalarExpression ofl2 (Add a b) = Add (fixScalarExpression ofl2 a) (fixScalarExpression ofl2 b) +-- fixScalarExpression ofl2 (Sub a b) = Add (fixScalarExpression ofl2 a) (fixScalarExpression ofl2 b) +-- fixScalarExpression ofl2 (Neg e) = Neg (fixScalarExpression ofl2 e) +-- fixScalarExpression ofl2 (MulScalar a b) = Add (fixScalarExpression ofl2 a) (fixScalarExpression ofl2 b) +-- fixScalarExpression ofl2 (DivScalar a b) = Add (fixScalarExpression ofl2 a) (fixScalarExpression ofl2 b) +-- fixScalarExpression _ e@(ConstInteger _) = e +-- fixScalarExpression _ e@(ConstReal _) = e +-- fixScalarExpression _ e@(ConstComplex _) = e +-- +-- fixIntegrand :: OFL2 -> Expression PsincE -> Expression PsincE +-- fixIntegrand _ _ = error "Unimplemented" +-- +-- fixPositionExpression :: OFL2 -> Expression PsincE -> Expression PsincE +-- fixPositionExpression _ _ = error "Unimplemented" +-- +-- fixMomentumExpression :: OFL2 -> Expression PsincReciprocalE -> Expression PsincReciprocalE +-- fixMomentumExpression _ _ = error "Unimplemented" -- 2.47.3