Copyright | (c) The University of Glasgow 2001 |
---|---|
License | BSD-style (see the file libraries/base/LICENSE) |
Maintainer | libraries@haskell.org |
Stability | provisional |
Portability | portable |
Safe Haskell | Trustworthy |
Language | Haskell2010 |
Complex numbers.
Synopsis
- data Complex a = !a :+ !a
- realPart :: Complex a -> a
- imagPart :: Complex a -> a
- mkPolar :: Floating a => a -> a -> Complex a
- cis :: Floating a => a -> Complex a
- polar :: RealFloat a => Complex a -> (a, a)
- magnitude :: RealFloat a => Complex a -> a
- phase :: RealFloat a => Complex a -> a
- conjugate :: Num a => Complex a -> Complex a
Rectangular form
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
,
is a number with the abs
zmagnitude
of z
,
but oriented in the positive real direction, whereas
has the signum
zphase
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
>>>
(5.0 :+ 2.5) + 6.5
11.5 :+ 2.5
>>>
abs (1.0 :+ 1.0) - sqrt 2.0
0.0 :+ 0.0
>>>
abs (signum (4.0 :+ 3.0))
1.0 :+ 0.0
>>>
foldr (:) [] (1 :+ 2)
[1,2]
>>>
mapM print (1 :+ 2)
1 2
!a :+ !a infix 6 | forms a complex number from its real and imaginary rectangular components. |
Instances
realPart :: Complex a -> a Source #
Extracts the real part of a complex number.
Examples
>>>
realPart (5.0 :+ 3.0)
5.0
>>>
realPart ((5.0 :+ 3.0) * (2.0 :+ 3.0))
1.0
imagPart :: Complex a -> a Source #
Extracts the imaginary part of a complex number.
Examples
>>>
imagPart (5.0 :+ 3.0)
3.0
>>>
imagPart ((5.0 :+ 3.0) * (2.0 :+ 3.0))
21.0
Polar form
polar :: RealFloat a => Complex a -> (a, a) Source #
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 (-
;
if the pi
, pi
]magnitude
is zero, then so is the phase
.
polar
z = (magnitude
z,phase
z)
Examples
>>>
polar (1.0 :+ 1.0)
(1.4142135623730951,0.7853981633974483)
>>>
polar ((-1.0) :+ 0.0)
(1.0,3.141592653589793)
>>>
polar (0.0 :+ 0.0)
(0.0,0.0)
magnitude :: RealFloat a => Complex a -> a Source #
The non-negative magnitude
of a complex number.
Examples
>>>
magnitude (1.0 :+ 1.0)
1.4142135623730951
>>>
magnitude (1.0 + 0.0)
1.0
>>>
magnitude (0.0 :+ (-5.0))
5.0