<commit_before>{-
  (c) 2012 Copyright <NAME>
-}


module Data.Complex

%access public export

------------------------------ Rectangular form

infix 6 :+
data Complex a = (:+) a a

realPart : Complex a -> a
realPart (r:+i) = r

imagPart : Complex a -> a
imagPart (r:+i) = i

implementation Eq a => Eq (Complex a) where
    (==) a b = realPart a == realPart b && imagPart a == imagPart b

implementation Show a => Show (Complex a) where
    showPrec d (r :+ i) = showParens (d >= plus_i) $ showPrec plus_i r ++ " :+ " ++ showPrec plus_i i
      where plus_i : Prec
            plus_i = User 6


-- when we have an interface 'Fractional' (which contains Double),
-- we can do:
{-
implementation Fractional a => Fractional (Complex a) where
    (/) (a:+b) (c:+d) = let
                          real = (a*c+b*d)/(c*c+d*d)
                          imag = (b*c-a*d)/(c*c+d*d)
                        in
                          (real:+imag)
-}



------------------------------ Polarform

mkPolar : Double -> Double -> Complex Double
mkPolar radius angle = radius * cos angle :+ radius * sin angle

cis : Double -> Complex Double
cis angle = cos angle :+ sin angle

magnitude : Complex Double -> Double
magnitude (r:+i) = sqrt (r*r+i*i)

phase : Complex Double -> Double
phase (x:+y) = atan2 y x


------------------------------ Conjugate

conjugate : Neg a => Complex a -> Complex a
conjugate (r:+i) = (r :+ (0-i))

implementation Functor Complex where
    map f (r :+ i) = f r :+ f i

-- We can do "implementation Neg a => Num (Complex a)" because
-- we don't need "abs", only (-)
implementation Neg a => Num (Complex a) where
    (+) (a:+b) (c:+d) = ((a+c):+(b+d))
    (*) (a:+b) (c:+d) = ((a*c-b*d):+(b*c+a*d))
    fromInteger x = (fromInteger x:+0)

implementation Neg a => Neg (Complex a) where
    negate = map negate
    (-) (a:+b) (c:+d) = ((a-c):+(b-d))

-- Specific to Double because `abs` needs `magnitude` which needs `sqrt` which
-- operates on Double.
implementation Abs (Complex Double) where
    abs (a:+b) = (magnitude (a:+b):+0)<commit_msg>Enabled Fractional instance for Complex a<commit_after>{-
  (c) 2012 Copyright <NAME>
-}


module Data.Complex

%access public export

------------------------------ Rectangular form

infix 6 :+
data Complex a = (:+) a a

realPart : Complex a -> a
realPart (r:+i) = r

imagPart : Complex a -> a
imagPart (r:+i) = i

implementation Eq a => Eq (Complex a) where
    (==) a b = realPart a == realPart b && imagPart a == imagPart b

implementation Show a => Show (Complex a) where
    showPrec d (r :+ i) = showParens (d >= plus_i) $ showPrec plus_i r ++ " :+ " ++ showPrec plus_i i
      where plus_i : Prec
            plus_i = User 6



------------------------------ Polarform

mkPolar : Double -> Double -> Complex Double
mkPolar radius angle = radius * cos angle :+ radius * sin angle

cis : Double -> Complex Double
cis angle = cos angle :+ sin angle

magnitude : Complex Double -> Double
magnitude (r:+i) = sqrt (r*r+i*i)

phase : Complex Double -> Double
phase (x:+y) = atan2 y x


------------------------------ Conjugate

conjugate : Neg a => Complex a -> Complex a
conjugate (r:+i) = (r :+ (0-i))

implementation Functor Complex where
    map f (r :+ i) = f r :+ f i

-- We can do "implementation Neg a => Num (Complex a)" because
-- we don't need "abs", only (-)
implementation Neg a => Num (Complex a) where
    (+) (a:+b) (c:+d) = ((a+c):+(b+d))
    (*) (a:+b) (c:+d) = ((a*c-b*d):+(b*c+a*d))
    fromInteger x = (fromInteger x:+0)

implementation Neg a => Neg (Complex a) where
    negate = map negate
    (-) (a:+b) (c:+d) = ((a-c):+(b-d))

-- Specific to Double because `abs` needs `magnitude` which needs `sqrt` which
-- operates on Double.
implementation Abs (Complex Double) where
    abs (a:+b) = (magnitude (a:+b):+0)

implementation (Neg a, Fractional a) => Fractional (Complex a) where
    (/) (a:+b) (c:+d) = let
                          real = (a*c+b*d)/(c*c+d*d)
                          imag = (b*c-a*d)/(c*c+d*d)
                        in
                          (real:+imag)
{'fraction_non_alphanumeric': 0.1996062023135614, 'fraction_numerical': 0.004922471080482402, 'mean_word_length': 2.253803042433947, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5739161', 'n_tokens_mistral': 1474, 'n_tokens_neox': 1389, 'n_words': 470}