Complex numbers are catered for in the Haskell 98 and Haskell 2010 Language Reports. The former specifies library Complex exposing module Complex and the latter (which introduced hierarchical modules names) specifies module Data.Complex. Both modules define:
|
1 2 3 4 5 6 7 |
data RealFloat a => Complex a = !a :+ !a instance RealFloat a => Eq (Complex a) instance RealFloat a => Show (Complex a) instance RealFloat a => Num (Complex a) instance RealFloat a => Fractional (Complex a) instance RealFloat a => Floating (Complex a) |
The RealFloat a context also implies that the type a is an instance of typeclasses Eq, Floating, Fractional, Num, Ord, Real, RealFrac and Show. The typeclass RealFloat promises atan2 (among other functions). The typeclass Floating promises exp, log, sqrt, sin, cos, tan, asin, sinh, and asinh (among other functions).
The types Float and Double are instances of typeclass RealFloat.
base
The package base provides an implementation of module Data.Complex. For base >= 4.4.0.0 (GHC 7.2.1 came with base-4.4.0.0), the module defines (without the context RealFloat a):
|
1 |
data Complex a = a! :+ a! |
GHC 7.0.1 had introduced language extension DatatypeContexts which was “on by default“. GHC 7.2.1 changed that to “off by default“, although it was enabled by languages Haskell98 and Haskell2010 and GHC defaulted to Haskell98 and, from GHC 7.0.1, Haskell2010. (However, GHC did not document Haskell98 and Haskell2020 as language extensions until GHC 8.10.1.)
GHC 7.2.1 documented that data Complex had lost its previous context because DatatypeContexts had been ‘removed’ “from the language“.
However, the context is present in the instance of the Num typeclass:
|
1 |
instance (RealFloat a) => Num (Complex a) where |
abs :: Complex a -> Complex a is purposed to equate to magnitude (\left| z \right|) and signum :: Complex a -> Complex a is (except for 0 :+ 0) purposed to equate to z / \left| z \right|.
Multivalued functions
\sqrt z, \ln z, \arcsin z (etc) and \operatorname{arcsinh} z (etc) are multivalued functions.
For:
z = r e^{i\theta}, \quad r \gt 0, \quad -\pi \lt \theta \le \pithe principal value of \sqrt z is defined as:
\sqrt r e^{i\theta / 2}the principal value of \ln z is defined as:
\ln r + i\thetathe principal value of \arcsin z is defined as:
-i \ln \left( iz + \sqrt {1 - z^2} \right)and the principal value of \operatorname{arcsinh} z is defined as:
\ln \left( z + \sqrt {1 + z^2} \right)where the logarithm and square root refer to the principal values.
sqrt, log, asin and asinh are implemented accordingly.