Simplify checks in UE3 A.3 and A.4

This commit is contained in:
Ivaylo Ivanov 2021-11-04 19:21:05 +01:00
parent 2a36ed1de7
commit 57b468af75
1 changed files with 15 additions and 9 deletions

View File

@ -64,13 +64,15 @@ Solution:
Aufgabe A.3
Solution:
* if one of the parameters is an empty matrix or an incorrect matrix, return undefined
* otherwise, check the per-element equality in both martices
> instance Eq Matrix where
> (==) (M m1) (M m2)
> | m1 Prelude.== [] || m2 Prelude.== [] = error "Gleichheit undefiniert"
> | matrixtyp (M m1) == KeineMatrix || matrixtyp (M m2) == KeineMatrix = error "Gleichheit undefiniert"
> | otherwise = (m1 Prelude.== m2)
> (/=) (M m1) (M m2)
> | m1 Prelude.== [] || m2 Prelude.== [] = error "Ungleichheit undefiniert"
> | matrixtyp (M m1) == KeineMatrix || matrixtyp (M m2) == KeineMatrix = error "Ungleichheit undefiniert"
> | otherwise = (m1 Prelude./= m2)
@ -92,38 +94,42 @@ Aufgabe A.4
> add :: Matrix -> Matrix -> Matrix
> add (M m1) (M m2)
> | isEmpty (M m1) || isEmpty (M m2) = fehlerwert
> | matrixtyp (M m1) == KeineMatrix || matrixtyp (M m2) == KeineMatrix = fehlerwert
> | matrixtyp (M m1) /= matrixtyp (M m2) = fehlerwert
> | otherwise = M (zipWith (zipWith (+)) m1 m2)
> diff :: Matrix -> Matrix -> Matrix
> diff (M m1) (M m2)
> | isEmpty (M m1) || isEmpty (M m2) = fehlerwert
> | matrixtyp (M m1) == KeineMatrix || matrixtyp (M m2) == KeineMatrix = fehlerwert
> | matrixtyp (M m1) /= matrixtyp (M m2) = fehlerwert
> | otherwise = M (zipWith (zipWith (-)) m1 m2)
> mult :: Matrix -> Matrix -> Matrix
> mult (M m1) (M m2)
> | isEmpty (M m1) || isEmpty (M m2) = fehlerwert
> | matrixtyp (M m1) == KeineMatrix || matrixtyp (M m2) == KeineMatrix = fehlerwert
> | otherwise = M ([[0]])
> negation :: Matrix -> Matrix
> negation (M m1)
> | isEmpty (M m1)= fehlerwert
> | matrixtyp (M m1) == KeineMatrix = fehlerwert
> | otherwise = M (map (map ((-1)*)) m1)
> absolute :: Matrix -> Matrix
> absolute (M m1)
> | isEmpty (M m1)= fehlerwert
> | matrixtyp (M m1) == KeineMatrix = fehlerwert
> | otherwise = M (map (map (abs)) m1)
> sign :: Matrix -> Matrix
> sign (M m1)
> | isEmpty (M m1)= error "Vorzeichenfunktion undefiniert"
> | matrixtyp (M m1) == KeineMatrix = error "Vorzeichenfunktion undefiniert"
> | otherwise = M ([[0]])
> | [True] `elem` negative && [False] `elem` negative = error "Vorzeichenfunktion undefiniert"
> | [True] `elem` positive && [False] `elem` positive = error "Vorzeichenfunktion undefiniert"
> | [True] `elem` nulls && [False] `elem` nulls = error "Vorzeichenfunktion undefiniert"
> | [True] `elem` nulls = M([[0]])
> | [True] `elem` positive = M([[1]])
> | [True] `elem` negative = M([[-1]])
> | otherwise = error "Vorzeichenfunktion undefiniert"
> where
> negative = map (map (<0)) m1
> nulls = map (map (==0)) m1
> positive = map (map (>0)) m1