views:

349

answers:

4

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}

A: 

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.

mjv
@mjv, interesting idea. It certainly makes for a very flexible notation. However I fear it becomes too easy to make syntactic mistakes this way. It will confuse people.
David Rutten
@David R: see my 2nd approach... Just a suggestion, of course, you know your users more than I...
mjv
@mjv, thanks again. This is exactly what I was looking for.
David Rutten
+1  A: 

Do your users have a specific notation they use? Could you pick one as close to theirs as possible? In my case I'd use a + bi so I'd say {31 * 5} + {6 + -5}i however, if they used to the functional form then I suggest complex(31 * 5, 6 + -5).

Since you're using .net you might want to use the DLR to give your scripting python or ruby syntax?

Preet Sangha
Complex numbers weren't available until now, so there is no syntax yet. Points and Vectors are written as {x, y, z}. Complex numbers could be {a, b}, which is nice and short, but unfortunately not very self-documenting. "complex(,)" is good though.I'm writing in DotNET, but I wrote the interpreter for the scripting language myself. I needed a whole lot of special operators (factorial, square, cross-product, dot-product, remap-plane etc. etc.) Also, since most of my users come from a VB background I wanted something which looks like regular VBScript until you use the special features.
David Rutten
Actually I was thinking about how the users currently use complex numbers generally (not in your app).
Preet Sangha
+2  A: 

If your desire is to simply differentiate the real from the imaginary component for a complex number, I'd do one of the following.

  • Enclose the number with some form of parentheses to ensure it's treated as a unit. For example, 6+5i becomes {6,5} or {6,5i} for extra readability.
  • Introduce a marker to separate the parts, similar to the exponent in 6.022e23. For example, 6+5i becomes 6i5. You'd still need parentheses to specify formulae for this one, such as 6 + (b/2)i becoming 6i(b/2), not the confusing 6ib/2 (which could also be read as (6ib)/2) and unfortunately you lose the use of i as a variable.

Either of those would be simple to process even with a simple parser and they're close enough to the mathematical notation so as not to confuse. I prefer the first since it seems more explicit to me and uses symbols that are unlikely to confuse.

paxdiablo
@Pax, thanks. I think the bracket approach will work best. I won't use the "i" suffix, since it would be too confusing in some cases.
David Rutten
+1  A: 

Most of the languages I can find that have a complex number builtin type (such as Python and Lisp) use something like:

c{r, i}
ICR