harrysarson / elm-complex / Complex

A library for complex calculations.

Type


type alias Complex =
Internal.Complex

A complex number. See Internal Representation for more details.

Build

complex : Basics.Float -> Basics.Float -> Complex

Construct a complex number from real and imaginary parts.

polar : Basics.Float -> Basics.Float -> Complex

Construct a complex number from magnitude and angle.

real : Basics.Float -> Complex

Construct a real number.

imaginary : Basics.Float -> Complex

Construct an imaginary number.

Special Values

nan : Complex

A complex "not a number" value.

nan == divide (complex 0 0) (complex 0 0)

infinity : Complex

Complex infinity. Infinity is 'very near' large complex numbers defined on the Riemann sphere.

infinity == divide (complex 1 0) (complex 0 0)

zero : Complex

Zero as a complex number.

zero == complex 0 0

unity : Complex

One as a complex number.

unity == complex 1 0

Checks

isInfinite : Complex -> Basics.Bool

Test if a complex number is infinite.

isInfinite infinity == True

isInfinite nan == False

isInfinite (complex 1 6) == False

Note: you can also use c == infinity to test for infinity values.

isNan : Complex -> Basics.Bool

Test if a complex number is not a number.

isNan nan == True

isNan infinity == False

isNan (complex 1 6) == False

Note: 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).

Operations

add : Complex -> Complex -> Complex

Add two complex numbers.

add (complex 4 1) (complex 1 -7) == complex 5 -6

subtract : Complex -> Complex -> Complex

Subtract the second argument (rhs) from the first (lhs).

subtract (complex 8 2) (complex 2 3) == complex 6 -1

multiply : Complex -> Complex -> Complex

Multiply two complex numbers.

multiply (complex 0 4) (complex 1 2) == complex -8 4

divide : Complex -> Complex -> Complex

Divide the first argument (lhs) by the (rhs).

multiply (complex -12 4.5) (complex 3 0) == complex -4 1.5

pow : Complex -> Complex -> Complex

Raise the first argument (lhs) to the power of the second (rhs).

pow (imaginary 2) (real 4) == real 16

exp : Complex -> Complex

Raise a complex number to the power of e.

exp (imaginary pi) == real -1 -- allowing for floating point rounding errors .

log : Complex -> Complex

Compute natural logarithm of a complex number.

log (real -10) == complex 2.302585 pi -- allowing for floating point rounding errors.

conjugate : Complex -> Complex

Compute the conjugate of a complex number

conjugate (complex 6 8) == complex 6 -8

Conversions

toCartesian : Complex -> { re : Basics.Float, im : Basics.Float }

Get 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 }

toPolar : Complex -> { abs : Basics.Float, arg : Basics.Float }

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 }

toString : Complex -> String

Convert to string.

toString (complex 5 1) == "5+i"

fromString : String -> Maybe Complex

Create a complex number from a string.

Strings must consist of

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)

Riemann Sphere

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.

Internal Representation

Complex numbers are represented as a Union Type:

type Complex
    = Complex
        { re : Float
        , im : Float
        }
    | ComplexNan
    | ComplexInfinity

A 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.