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.119 tracks/haskell/exercises/complex-numbers/examples/success-standard/src/ComplexNumbers.hs
trackler-2.2.1.118 tracks/haskell/exercises/complex-numbers/examples/success-standard/src/ComplexNumbers.hs
trackler-2.2.1.117 tracks/haskell/exercises/complex-numbers/examples/success-standard/src/ComplexNumbers.hs
trackler-2.2.1.116 tracks/haskell/exercises/complex-numbers/examples/success-standard/src/ComplexNumbers.hs
trackler-2.2.1.115 tracks/haskell/exercises/complex-numbers/examples/success-standard/src/ComplexNumbers.hs
trackler-2.2.1.114 tracks/haskell/exercises/complex-numbers/examples/success-standard/src/ComplexNumbers.hs
trackler-2.2.1.113 tracks/haskell/exercises/complex-numbers/examples/success-standard/src/ComplexNumbers.hs
trackler-2.2.1.111 tracks/haskell/exercises/complex-numbers/examples/success-standard/src/ComplexNumbers.hs
trackler-2.2.1.110 tracks/haskell/exercises/complex-numbers/examples/success-standard/src/ComplexNumbers.hs
trackler-2.2.1.109 tracks/haskell/exercises/complex-numbers/examples/success-standard/src/ComplexNumbers.hs
trackler-2.2.1.108 tracks/haskell/exercises/complex-numbers/examples/success-standard/src/ComplexNumbers.hs
trackler-2.2.1.107 tracks/haskell/exercises/complex-numbers/examples/success-standard/src/ComplexNumbers.hs
trackler-2.2.1.106 tracks/haskell/exercises/complex-numbers/examples/success-standard/src/ComplexNumbers.hs
trackler-2.2.1.105 tracks/haskell/exercises/complex-numbers/examples/success-standard/src/ComplexNumbers.hs
trackler-2.2.1.104 tracks/haskell/exercises/complex-numbers/examples/success-standard/src/ComplexNumbers.hs
trackler-2.2.1.103 tracks/haskell/exercises/complex-numbers/examples/success-standard/src/ComplexNumbers.hs
trackler-2.2.1.102 tracks/haskell/exercises/complex-numbers/examples/success-standard/src/ComplexNumbers.hs
trackler-2.2.1.101 tracks/haskell/exercises/complex-numbers/examples/success-standard/src/ComplexNumbers.hs
trackler-2.2.1.100 tracks/haskell/exercises/complex-numbers/examples/success-standard/src/ComplexNumbers.hs
trackler-2.2.1.99 tracks/haskell/exercises/complex-numbers/examples/success-standard/src/ComplexNumbers.hs