HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulebase-compat-batteries-0.14.1Haskell2010

Data.Complex.Compat

  • 1 type
  • 8 values
valuecis :: Floating a => a -> Complex a
#

cis t is a complex value with magnitude 1 and phase t (modulo 2*pi).

cis = mkPolar 1
Examples
Example1 expression
cis 01.0 :+ 0.0

The following examples are not perfectly zero due to IEEE 754

Example1 expression
cis pi(-1.0) :+ 1.2246467991473532e-16
Example1 expression
cis (4 * pi) - cis (2 * pi)0.0 :+ (-2.4492935982947064e-16)
valueconjugate :: Num a => Complex a -> Complex a
#

The conjugate of a complex number.

Property
conjugate (conjugate x) = x
Examples
Example1 expression
conjugate (3.0 :+ 3.0)3.0 :+ (-3.0)
Example1 expression
conjugate ((3.0 :+ 3.0) * (2.0 :+ 2.0))0.0 :+ (-12.0)
valueimagPart :: Complex a -> a
#

Extracts the imaginary part of a complex number.

Examples
Example1 expression
imagPart (5.0 :+ 3.0)3.0
Example1 expression
imagPart ((5.0 :+ 3.0) * (2.0 :+ 3.0))21.0
valuemagnitude :: RealFloat a => Complex a -> a
#

The non-negative magnitude of a complex number.

Examples
Example1 expression
magnitude (1.0 :+ 1.0)1.4142135623730951
Example1 expression
magnitude (1.0 + 0.0)1.0
Example1 expression
magnitude (0.0 :+ (-5.0))5.0
valuemkPolar :: Floating a => a -> a -> Complex a
#

Form a complex number from polar components of magnitude and phase.

Examples
Example1 expression
mkPolar 1 (pi / 4)0.7071067811865476 :+ 0.7071067811865475
Example1 expression
mkPolar 1 01.0 :+ 0.0
valuephase :: RealFloat a => Complex a -> a
#

The phase of a complex number, in the range (-pi, pi]. If the magnitude is zero, then so is the phase.

Examples
Example1 expression
phase (0.5 :+ 0.5) / pi0.25
Example1 expression
phase (0 :+ 4) / pi0.5
valuepolar :: RealFloat a => Complex a -> (a, a)
#

The function polar takes a complex number and returns a (magnitude, phase) pair in canonical form: the magnitude is non-negative, and the phase in the range (-pi, pi]; if the magnitude is zero, then so is the phase.

polar z = (magnitude z, phase z)
Examples
Example1 expression
polar (1.0 :+ 1.0)(1.4142135623730951,0.7853981633974483)
Example1 expression
polar ((-1.0) :+ 0.0)(1.0,3.141592653589793)
Example1 expression
polar (0.0 :+ 0.0)(0.0,0.0)
valuerealPart :: Complex a -> a
#

Extracts the real part of a complex number.

Examples
Example1 expression
realPart (5.0 :+ 3.0)5.0
Example1 expression
realPart ((5.0 :+ 3.0) * (2.0 :+ 3.0))1.0
datadata Complex a
#

A data type representing complex numbers.

You can read about complex numbers on wikipedia.

In haskell, complex numbers are represented as a :+ b which can be thought of as representing a + bi. For a complex number z, abs z is a number with the magnitude of z, but oriented in the positive real direction, whereas signum z has the phase of z, but unit magnitude. Apart from the loss of precision due to IEEE754 floating point numbers, it holds that z == abs z * signum z.

Note that Complex's instances inherit the deficiencies from the type parameter's. For example, Complex Float's Ord instance has similar problems to Float's.

As can be seen in the examples, the Foldable and Traversable instances traverse the real part first.

Examples
Example1 expression
(5.0 :+ 2.5) + 6.511.5 :+ 2.5
Example1 expression
abs (1.0 :+ 1.0) - sqrt 2.00.0 :+ 0.0
Example1 expression
abs (signum (4.0 :+ 3.0))1.0 :+ 0.0
Example1 expression
foldr (:) [] (1 :+ 2)[1,2]
Example1 expression
mapM print (1 :+ 2)12

Constructors

  • a :+ ainfix 6

    forms a complex number from its real and imaginary rectangular components.

Instances24Monad, Functor, MonadFix, Applicative, Foldable, Traversable, …