Search code examples
c#mathnotationcomplex-numbers

Complex number notation


My DotNET application has a limited scripting language build in (modelled loosely on VBScript) mainly for post-processing numbers, points and vectors. I've just added support for complex numbers, but I'm struggling with the notation.

I don't want to use the A + Bi notation, since it is not a clear division if A or B are defined as equations already:

31 * 5 + 6 + -5i

This could be interpreted as:

A = 31 * 5 + 6 B = -5i

and:

A = 31 * 5 B = 6 + -5i

None of the programming languages I know have native support for complex numbers. I'm thinking something like the following might work, but I'd appreciate any input on this:

{31 * 5} + {6 + -5}i

complex(31 * 5, 6 + -5)

r{31 * 5} i{6 + -5}


Solution

  • It seems you are using explicit multiplication in your examples (i.e. you require A * B, rather than A B ).

    In that case why not simply use the i suffix directly following the value as in

     myComplex = 12 + 6i
       or
     myOtherComplex = 12/7 + (6 * pi)i
    

    Then you may need to decide about i or j, I've seen both...

    This i-suffix trick is not unlike the scientific notication and is e (3.1415e7 for example)

    Edit (following David's comments)

    The format above can become confusing, depending on the audience, one way to clarify this may be to only allow for imaginary literals, and to include these into a complex notation derived from your existing vector notation. When imaginary numbers or complex number require an expression to designate them, the syntax would require the explicit "function-looking" syntax such as Imaginary(i) and Complex(r, i).

    Parsing rules:

    • Any number (signed or unsigned, integer or decimal, or even exp. notation number) directly followed by the suffix i is a imaginary number: -7i or 1.23i or 5.76e4i but not 12 i (no space allowed between number and suffix)
    • a two values vector with the first one real and the 2nd imaginary is a complex: (1, 7i) or even (7, 0i)
    • Imaginary(i) format is used when "i" value is an expression. i is expressed without the i suffix which is implied by the method call syntax.
    • Complex(r,i) format is used when either "r" or "i" params is/are an expression, and also whenever we wish to avoid ambiguity.

    In short:

    • (7, 1i) , (0, -3.1415i), (13, 0i), Complex(13, 0) or Complex(7x+3, sin(y)+2) are all complex numbers
    • 6i, -1.234e5i, Imaginary(1.234) or Imaginary(sqrt(19x+5y)) are all imaginary numbers
    • (12, 23, 34) is a vector in R^3
    • (12i, -2i) in a vector in I^2 (not a complex number, since the first element is not real)
    • ((0,0i), (1,-9.2i), (12, 0i)) or ((0, 21i), Complex(12,3), (44, -55i)) are vectors in C^3

    That's seems consistent and simple enough, but then again, only the true end-users can tell.