{-# OPTIONS_GHC -Wall #-}
{-# LANGUAGE Safe #-}
module Data.Number.RealCyclotomic
( RealCyclotomic
, sqrtRat
, sinDeg
, cosDeg
, sinRev
, cosRev
, isRat
, toRat
, toReal
, magSq
, goldenRatio
, heron
)
where
import qualified Data.Complex.Cyclotomic as Cyc
import Data.Complex
( realPart
)
newtype RealCyclotomic = RealCyclotomic Cyc.Cyclotomic
deriving (RealCyclotomic -> RealCyclotomic -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: RealCyclotomic -> RealCyclotomic -> Bool
$c/= :: RealCyclotomic -> RealCyclotomic -> Bool
== :: RealCyclotomic -> RealCyclotomic -> Bool
$c== :: RealCyclotomic -> RealCyclotomic -> Bool
Eq)
instance Num RealCyclotomic where
RealCyclotomic Cyclotomic
x + :: RealCyclotomic -> RealCyclotomic -> RealCyclotomic
+ RealCyclotomic Cyclotomic
y = Cyclotomic -> RealCyclotomic
RealCyclotomic (Cyclotomic
x forall a. Num a => a -> a -> a
+ Cyclotomic
y)
RealCyclotomic Cyclotomic
x - :: RealCyclotomic -> RealCyclotomic -> RealCyclotomic
- RealCyclotomic Cyclotomic
y = Cyclotomic -> RealCyclotomic
RealCyclotomic (Cyclotomic
x forall a. Num a => a -> a -> a
- Cyclotomic
y)
RealCyclotomic Cyclotomic
x * :: RealCyclotomic -> RealCyclotomic -> RealCyclotomic
* RealCyclotomic Cyclotomic
y = Cyclotomic -> RealCyclotomic
RealCyclotomic (Cyclotomic
x forall a. Num a => a -> a -> a
* Cyclotomic
y)
negate :: RealCyclotomic -> RealCyclotomic
negate (RealCyclotomic Cyclotomic
x) = Cyclotomic -> RealCyclotomic
RealCyclotomic (forall a. Num a => a -> a
negate Cyclotomic
x)
fromInteger :: Integer -> RealCyclotomic
fromInteger Integer
n = Cyclotomic -> RealCyclotomic
RealCyclotomic (forall a. Num a => Integer -> a
fromInteger Integer
n)
abs :: RealCyclotomic -> RealCyclotomic
abs = forall a. HasCallStack => a
undefined
signum :: RealCyclotomic -> RealCyclotomic
signum = forall a. HasCallStack => a
undefined
instance Fractional RealCyclotomic where
recip :: RealCyclotomic -> RealCyclotomic
recip (RealCyclotomic Cyclotomic
x) = Cyclotomic -> RealCyclotomic
RealCyclotomic (forall a. Fractional a => a -> a
recip Cyclotomic
x)
fromRational :: Rational -> RealCyclotomic
fromRational Rational
r = Cyclotomic -> RealCyclotomic
RealCyclotomic (forall a. Fractional a => Rational -> a
fromRational Rational
r)
instance Show RealCyclotomic where
show :: RealCyclotomic -> String
show (RealCyclotomic Cyclotomic
x) = forall a. Show a => a -> String
show Cyclotomic
x
sqrtRat :: Rational -> RealCyclotomic
sqrtRat :: Rational -> RealCyclotomic
sqrtRat Rational
r
| Rational
r forall a. Ord a => a -> a -> Bool
>= Rational
0 = Cyclotomic -> RealCyclotomic
RealCyclotomic (Rational -> Cyclotomic
Cyc.sqrtRat Rational
r)
| Bool
otherwise = forall a. HasCallStack => String -> a
error String
"sqrtRational needs a nonnegative argument"
sinDeg :: Rational -> RealCyclotomic
sinDeg :: Rational -> RealCyclotomic
sinDeg Rational
r = Cyclotomic -> RealCyclotomic
RealCyclotomic (Rational -> Cyclotomic
Cyc.sinDeg Rational
r)
cosDeg :: Rational -> RealCyclotomic
cosDeg :: Rational -> RealCyclotomic
cosDeg Rational
r = Cyclotomic -> RealCyclotomic
RealCyclotomic (Rational -> Cyclotomic
Cyc.cosDeg Rational
r)
sinRev :: Rational -> RealCyclotomic
sinRev :: Rational -> RealCyclotomic
sinRev Rational
r = Cyclotomic -> RealCyclotomic
RealCyclotomic (Rational -> Cyclotomic
Cyc.sinRev Rational
r)
cosRev :: Rational -> RealCyclotomic
cosRev :: Rational -> RealCyclotomic
cosRev Rational
r = Cyclotomic -> RealCyclotomic
RealCyclotomic (Rational -> Cyclotomic
Cyc.cosRev Rational
r)
isRat :: RealCyclotomic -> Bool
isRat :: RealCyclotomic -> Bool
isRat (RealCyclotomic Cyclotomic
r) = Cyclotomic -> Bool
Cyc.isRat Cyclotomic
r
toRat :: RealCyclotomic -> Maybe Rational
toRat :: RealCyclotomic -> Maybe Rational
toRat (RealCyclotomic Cyclotomic
r) = Cyclotomic -> Maybe Rational
Cyc.toRat Cyclotomic
r
toReal :: RealFloat a => RealCyclotomic -> a
toReal :: forall a. RealFloat a => RealCyclotomic -> a
toReal (RealCyclotomic Cyclotomic
r) = forall a. Complex a -> a
realPart (forall a. RealFloat a => Cyclotomic -> Complex a
Cyc.toComplex Cyclotomic
r)
goldenRatio :: RealCyclotomic
goldenRatio :: RealCyclotomic
goldenRatio = (RealCyclotomic
1 forall a. Num a => a -> a -> a
+ Rational -> RealCyclotomic
sqrtRat Rational
5) forall a. Fractional a => a -> a -> a
/ RealCyclotomic
2
heron :: Rational
-> Rational
-> Rational
-> RealCyclotomic
heron :: Rational -> Rational -> Rational -> RealCyclotomic
heron Rational
a Rational
b Rational
c
= Rational -> RealCyclotomic
sqrtRat (Rational
s forall a. Num a => a -> a -> a
* (Rational
sforall a. Num a => a -> a -> a
-Rational
a) forall a. Num a => a -> a -> a
* (Rational
sforall a. Num a => a -> a -> a
-Rational
b) forall a. Num a => a -> a -> a
* (Rational
sforall a. Num a => a -> a -> a
-Rational
c))
where
s :: Rational
s = (Rational
a forall a. Num a => a -> a -> a
+ Rational
b forall a. Num a => a -> a -> a
+ Rational
c) forall a. Fractional a => a -> a -> a
/ Rational
2
magSq :: RealCyclotomic -> RealCyclotomic
magSq :: RealCyclotomic -> RealCyclotomic
magSq (RealCyclotomic Cyclotomic
c) = Cyclotomic -> RealCyclotomic
RealCyclotomic (Cyclotomic
c forall a. Num a => a -> a -> a
* Cyclotomic -> Cyclotomic
Cyc.conj Cyclotomic
c)