diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-06-05 14:48:14 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-06-05 14:48:14 +0200 |
| commit | f5ddd91d290a0c508e04cce2cb19c4c8bae32835 (patch) | |
| tree | 3d3082c3d6fa977b12290f4cd2ed6c7ada4e9606 /src/Expr.hs | |
| parent | 45cada8eade7242eb7f29af7b92858e9a1a7f68b (diff) | |
| download | computorv2-f5ddd91d290a0c508e04cce2cb19c4c8bae32835.tar.gz computorv2-f5ddd91d290a0c508e04cce2cb19c4c8bae32835.tar.bz2 computorv2-f5ddd91d290a0c508e04cce2cb19c4c8bae32835.zip | |
Removed Imaginary type, Added variable name check (/= i), Added Matrix check if rectangular
Diffstat (limited to 'src/Expr.hs')
| -rw-r--r-- | src/Expr.hs | 45 |
1 files changed, 20 insertions, 25 deletions
diff --git a/src/Expr.hs b/src/Expr.hs index 852ee47..93828d9 100644 --- a/src/Expr.hs +++ b/src/Expr.hs @@ -4,10 +4,27 @@ import Control.Monad import Data.List import qualified Data.Map as M +-- data Operand +-- = Rational Float +-- | Complex Float Float +-- | Matrix [[Operand]] +-- +-- +-- data (Operable a, Operable b) => Operator a b +-- = Add a b +-- | Sub a b +-- | Mul a b +-- | Div a b +-- | Mod a b +-- | Exp a b +-- | Dot a b +-- +-- data Label +-- = Variable String +-- | Function String Expr data Expr = Rational Float -- values - | Imaginary Float | Complex Float Float | Matrix [[Expr]] @@ -25,8 +42,7 @@ data Expr instance Show Expr where show (Rational a) = show a - show (Imaginary b) = show b ++ "i" - show (Complex a b) = show a ++ " + " ++ show (Imaginary b) + show (Complex a b) = (if a /= 0 then show a ++ " + " else "") ++ show b ++ "i" show (Add e1 e2) = show e1 ++ " + " ++ show e2 show (Sub e1 e2) = show e1 ++ " - " ++ show e2 show (Mul e1 e2) = show e1 ++ " * " ++ show e2 @@ -42,7 +58,6 @@ instance Show Expr where isLitteral :: Expr -> Bool isLitteral (Rational _) = True -isLitteral (Imaginary _) = True isLitteral (Complex _ _) = True isLitteral _ = False @@ -53,16 +68,10 @@ isLitteral _ = False add :: Expr -> Expr -> Maybe Expr add (Rational a) (Rational b) = Just $ Rational (a + b) -add (Rational a) (Imaginary b) = Just $ Complex a b add (Rational a) (Complex br bi) = Just $ Complex (br + a) bi -add (Imaginary a) (Imaginary b) = Just $ Imaginary (a + b) -add (Imaginary a) (Rational b) = Just $ Complex b a -add (Imaginary a) (Complex br bi) = Just $ Complex br (a + bi) - add (Complex ar ai) (Complex br bi) = Just $ Complex (ar + br) (ai + bi) add (Complex ar ai) (Rational b) = Just $ Complex (ar + b) ai -add (Complex ar ai) (Imaginary b) = Just $ Complex ar (ai + b) add _ _ = Nothing @@ -73,19 +82,14 @@ sub a b = add a =<< Rational (-1) `mul` b mul :: Expr -> Expr -> Maybe Expr mul (Rational a) (Rational b) = Just $ Rational (a * b) -mul (Rational a) (Imaginary b) = Just $ Imaginary (a * b) mul (Rational a) (Complex br bi) = Just $ Complex (a * br) (a * bi) -mul (Imaginary a) (Imaginary b) = Just $ Imaginary (a * b) -mul (Imaginary a) (Rational b) = Just $ Complex b a -mul (Imaginary a) (Complex br bi) = Just $ Complex (a * br) (a * bi) mul _ _ = Nothing div :: Expr -> Expr -> Maybe Expr div _ (Rational 0) = Nothing -div _ (Imaginary 0) = Nothing div _ (Complex 0 0) = Nothing div a b = mul a =<< b `Expr.exp` Rational (-1) @@ -96,21 +100,12 @@ mod _ _ = Nothing exp :: Expr -> Expr -> Maybe Expr exp (Rational a) (Rational b) = Just $ Rational (a ** b) - -exp (Imaginary a) (Rational b) - | b < 0 = Expr.div (Rational 1) =<< Imaginary a `Expr.exp` Rational b - | b == 0 = Just $ Rational a - | b == 1 = Just $ Imaginary a - | b == 2 = Just $ Rational (-a) - | b == 3 = Just $ Imaginary (-a) - | otherwise = Imaginary a `Expr.exp` Rational (b - 4) - exp _ _ = Nothing dot :: Expr -> Expr -> Maybe Expr dot (Matrix a) (Matrix b) - | shape a == shape bT = Matrix <$> mapM (\ai -> mapM (dotProd ai) bT) a + | shape a == shape bT = Matrix <$> mapM (\aRow -> mapM (dotProd aRow) bT) a | otherwise = Nothing where bT = transpose b shape m = [length m, length (head m)] |
