A library for complex calculations.
Construct a complex number from real and imaginary parts.
Construct a complex number from magnitude and angle.
Construct a real number.
Construct an imaginary number.
A complex "not a number" value.
nan == divide (complex 0 0) (complex 0 0)Complex infinity. Infinity is 'very near' large complex numbers defined on the Riemann sphere.
infinity == divide (complex 1 0) (complex 0 0)Zero as a complex number.
zero == complex 0 0One as a complex number.
unity == complex 1 0Test if a complex number is infinite.
isInfinite infinity == True
isInfinite nan == False
isInfinite (complex 1 6) == FalseNote: you can also use c == infinity to test for infinity values.
Test if a complex number is not a number.
isNan nan == True
isNan infinity == False
isNan (complex 1 6) == FalseNote: you could use c == nan to check for NaN values but it is
not recommended as this behavour is different to that of floats (as `0/0 /= 0/0).
Add two complex numbers.
add (complex 4 1) (complex 1 -7) == complex 5 -6Subtract the second argument (rhs) from the first (lhs).
subtract (complex 8 2) (complex 2 3) == complex 6 -1Multiply two complex numbers.
multiply (complex 0 4) (complex 1 2) == complex -8 4Divide the first argument (lhs) by the (rhs).
multiply (complex -12 4.5) (complex 3 0) == complex -4 1.5Raise the first argument (lhs) to the power of the second (rhs).
pow (imaginary 2) (real 4) == real 16Raise a complex number to the power of e.
exp (imaginary pi) == real -1 -- allowing for floating point rounding errors .Compute natural logarithm of a complex number.
log (real -10) == complex 2.302585 pi -- allowing for floating point rounding errors.Compute the conjugate of a complex number
conjugate (complex 6 8) == complex 6 -8Get real and imaginary parts of a complex number.
toCartesian (complex 4 7) == { re = 4, im = 7 }
toCartesian (divide (real 1) (real 0)) == { re = 1 / 0, im = 1 / 0 }Get absolute value and argument of a complex number.
toPolar (complex 4 7) == { abs = 8.0622577, arg = 0.5191461 } -- allowing for floating point rounding errors.
toPolar (divide (real 1) (real 0)) == { abs = 1 / 0, arg = 0 / 0 }Convert to string.
toString (complex 5 1) == "5+i"Create a complex number from a string.
Strings must consist of
'i'.'i''i'.Whitespace is allowed anywhere between the real part and imaginary parts and between the imaginary
part and the character 'i'.
fromString "4 + 6i" == Just (complex 4 6)The Riemann sphere is a way of extending the
complex plane to allow infinite values. All functions in this libary handle infinite values consistantly
with Wolfram Alpha's ComplexInfinity.
Complex numbers are represented as a Union Type:
type Complex
= Complex
{ re : Float
, im : Float
}
| ComplexNan
| ComplexInfinityA complex number will either be finite (Complex), NaN or Infinite.
It should be impossible to create a complex number which is finite but has NaN or Infinite components.
If you manage to create one please file an issue.
A complex number. See Internal Representation for more details.