Sha256: 7fb2ca9e345b5be837c2bc92267b29ee014b680057e5f0ede7a735a5f4a7de8c

Contents?: true

Size: 1.42 KB

Versions: 133

Compression:

Stored size: 1.42 KB

Contents

module ComplexNumbers
(Complex,
 conjugate,
 abs,
 mul,
 add,
 sub,
 div,
 real,
 imaginary,
 complex) where

import Prelude hiding (div, abs)

-- Data definition -------------------------------------------------------------
data Complex a = Complex a a deriving (Eq, Show)

--------------------------------------------------------------------------------
complex :: (a, a) -> Complex a
complex = uncurry Complex

exp2 :: Num a => a -> a
exp2 x = x^(2 :: Integer)

-- unary operators -------------------------------------------------------------
real :: Num a => Complex a -> a
real (Complex a _) = a

imaginary :: Num a => Complex a -> a
imaginary (Complex _ b) = b

conjugate :: Num a => Complex a -> Complex a
conjugate (Complex a b) = Complex a (-b)

abs :: Floating a => Complex a -> a
abs (Complex a b) = sqrt (exp2 a + exp2 b)

-- binary operators ------------------------------------------------------------
mul :: Num a => Complex a -> Complex a -> Complex a
mul (Complex a b) (Complex c d) = Complex (a*c - b*d) (a*d + b*c)

add :: Num a => Complex a -> Complex a -> Complex a
add (Complex a b) (Complex c d) = Complex (a+c) (b+d)

sub :: Num a => Complex a -> Complex a -> Complex a
sub (Complex a b) (Complex c d) = Complex (a-c) (b-d)

div :: Fractional a => Complex a -> Complex a -> Complex a
div (Complex a b) (Complex c d) = Complex ((a*c + b*d)/(exp2 c + exp2 d))
                                          ((b*c - a*d)/(exp2 c + exp2 d))

Version data entries

133 entries across 133 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/haskell/exercises/complex-numbers/examples/success-standard/src/ComplexNumbers.hs
trackler-2.2.1.179 tracks/haskell/exercises/complex-numbers/examples/success-standard/src/ComplexNumbers.hs
trackler-2.2.1.178 tracks/haskell/exercises/complex-numbers/examples/success-standard/src/ComplexNumbers.hs
trackler-2.2.1.177 tracks/haskell/exercises/complex-numbers/examples/success-standard/src/ComplexNumbers.hs
trackler-2.2.1.176 tracks/haskell/exercises/complex-numbers/examples/success-standard/src/ComplexNumbers.hs
trackler-2.2.1.175 tracks/haskell/exercises/complex-numbers/examples/success-standard/src/ComplexNumbers.hs
trackler-2.2.1.174 tracks/haskell/exercises/complex-numbers/examples/success-standard/src/ComplexNumbers.hs
trackler-2.2.1.173 tracks/haskell/exercises/complex-numbers/examples/success-standard/src/ComplexNumbers.hs
trackler-2.2.1.172 tracks/haskell/exercises/complex-numbers/examples/success-standard/src/ComplexNumbers.hs
trackler-2.2.1.171 tracks/haskell/exercises/complex-numbers/examples/success-standard/src/ComplexNumbers.hs
trackler-2.2.1.170 tracks/haskell/exercises/complex-numbers/examples/success-standard/src/ComplexNumbers.hs
trackler-2.2.1.169 tracks/haskell/exercises/complex-numbers/examples/success-standard/src/ComplexNumbers.hs
trackler-2.2.1.167 tracks/haskell/exercises/complex-numbers/examples/success-standard/src/ComplexNumbers.hs
trackler-2.2.1.166 tracks/haskell/exercises/complex-numbers/examples/success-standard/src/ComplexNumbers.hs
trackler-2.2.1.165 tracks/haskell/exercises/complex-numbers/examples/success-standard/src/ComplexNumbers.hs
trackler-2.2.1.164 tracks/haskell/exercises/complex-numbers/examples/success-standard/src/ComplexNumbers.hs
trackler-2.2.1.163 tracks/haskell/exercises/complex-numbers/examples/success-standard/src/ComplexNumbers.hs
trackler-2.2.1.162 tracks/haskell/exercises/complex-numbers/examples/success-standard/src/ComplexNumbers.hs
trackler-2.2.1.161 tracks/haskell/exercises/complex-numbers/examples/success-standard/src/ComplexNumbers.hs
trackler-2.2.1.160 tracks/haskell/exercises/complex-numbers/examples/success-standard/src/ComplexNumbers.hs