cyclotomic 0.4.1 → 0.4.2
raw patch · 2 files changed
+88/−11 lines, 2 files
Files
- cyclotomic.cabal +1/−1
- src/Data/Complex/Cyclotomic.hs +87/−10
cyclotomic.cabal view
@@ -1,5 +1,5 @@ Name: cyclotomic-Version: 0.4.1+Version: 0.4.2 Synopsis: A subfield of the complex numbers for exact calculation. Description: The cyclotomic numbers are a subset of the complex numbers that are represented exactly, enabling exact
src/Data/Complex/Cyclotomic.hs view
@@ -1,12 +1,15 @@ {-# OPTIONS_GHC -Wall #-}+{-# LANGUAGE Trustworthy #-} --- Module : Data.Complex.Cyclotomic--- Copyright : (c) Scott N. Walck 2012--- License : GPL-3 (see LICENSE)--- Maintainer : Scott N. Walck <walck@lvc.edu>+{- | +Module : Data.Complex.Cyclotomic+Copyright : (c) Scott N. Walck 2012-2013+License : GPL-3 (see LICENSE)+Maintainer : Scott N. Walck <walck@lvc.edu>+Stability : experimental -{- | The cyclotomic numbers are a subset of the complex numbers with- the following properties:+The cyclotomic numbers are a subset of the complex numbers with+the following properties: 1. The cyclotomic numbers are represented exactly, enabling exact computations and equality comparisons.@@ -64,8 +67,12 @@ ,sqrtRat ,sinDeg ,cosDeg+ ,sinRev+ ,cosRev ,gaussianRat ,polarRat+ ,polarRatDeg+ ,polarRatRev ,conj ,real ,imag@@ -81,11 +88,40 @@ ) where -import Data.List (nub)+import Data.List+ ( nub+ ) import Data.Ratio+ ( (%)+ , numerator+ , denominator+ ) import Data.Complex+ ( Complex(..)+ , realPart+ ) import qualified Data.Map as M-import Math.NumberTheory.Primes.Factorisation (factorise)+ ( Map+ , empty+ , singleton+ , lookup+ , keys+ , elems+ , size+ , fromList+ , toList+ , mapKeys+ , filter+ , insertWith+ , delete+ , map+ , unionWith+ , findWithDefault+ , fromListWith+ )+import Math.NumberTheory.Primes.Factorisation+ ( factorise+ ) -- | A cyclotomic number. data Cyclotomic = Cyclotomic { order :: Integer@@ -209,8 +245,10 @@ -- | A complex number in polar form, with rational magnitude @r@ and rational angle @s@ -- of the form @r * exp(2*pi*i*s)@; @polarRat r s@ is the same as @r * e q ^ p@,--- where @s = p/q@.-polarRat :: Rational -> Rational -> Cyclotomic+-- where @s = p/q@. This function is the same as 'polarRatRev'.+polarRat :: Rational -- ^ magnitude+ -> Rational -- ^ angle, in revolutions+ -> Cyclotomic -- ^ cyclotomic number polarRat r s = let p = numerator s q = denominator s@@ -218,6 +256,31 @@ True -> fromRational r * e q ^ p False -> conj $ fromRational r * e q ^ (-p) +-- | A complex number in polar form, with rational magnitude and rational angle+-- in degrees.+polarRatDeg :: Rational -- ^ magnitude+ -> Rational -- ^ angle, in degrees+ -> Cyclotomic -- ^ cyclotomic number+polarRatDeg r deg+ = let s = deg / 360+ p = numerator s+ q = denominator s+ in case p >= 0 of+ True -> fromRational r * e q ^ p+ False -> conj $ fromRational r * e q ^ (-p)++-- | A complex number in polar form, with rational magnitude and rational angle+-- in revolutions.+polarRatRev :: Rational -- ^ magnitude+ -> Rational -- ^ angle, in revolutions+ -> Cyclotomic -- ^ cyclotomic number+polarRatRev r s+ = let p = numerator s+ q = denominator s+ in case p >= 0 of+ True -> fromRational r * e q ^ p+ False -> conj $ fromRational r * e q ^ (-p)+ -- | Complex conjugate. conj :: Cyclotomic -> Cyclotomic conj (Cyclotomic n mp)@@ -439,6 +502,20 @@ cosDeg :: Rational -> Cyclotomic cosDeg d = let n = d / 360 nm = abs (numerator n)+ dn = denominator n+ a = e dn^nm+ in (a + conj a) / 2++-- | Sine function with argument in revolutions.+sinRev :: Rational -> Cyclotomic+sinRev n = let nm = abs (numerator n)+ dn = denominator n+ a = e dn^nm+ in fromRational(signum n) * (a - conj a) / (2*i)++-- | Cosine function with argument in revolutions.+cosRev :: Rational -> Cyclotomic+cosRev n = let nm = abs (numerator n) dn = denominator n a = e dn^nm in (a + conj a) / 2