Jump to content
A-man

using complex numbers

Recommended Posts

Is it possible to do math with complex numbers in SCAR? I want to make a mandlebrot visualizer, but I'm unsure how to do the math. About half way down the page here is an example with C/C++ of what I'm looking to achieve.

 

http://software.intel.com/en-us/articles/introduction-to-intel-advanced-vector-extensions/

 

You could make a mandelbrot in SCAR, but it will probably be fairly slow due to the script engine. As Bixby noted, for complex numbers, you'll have to define a record type and some functions to perform operations.

Link to comment
Share on other sites

As far as I know, SCAR isn't really meant for calculations like these.

C++ would be a far better language to write it in.

 

SCAR has some functions dealing with polar points and cartesian points, normal sine functions and other basic math stuff.

PASCAL has a way to deal with complex numbers, I've found a demo about that below:

 

     program ComplexOperationsDemo;

    var
      z1, z2: Complex;
      Len, Angle: Real;

    begin
      z1 := Cmplx (2, 1);
      WriteLn;
      WriteLn ('Complex number z1 is: (', Re (z1) : 1, ',', Im (z1) : 1, ')');
      WriteLn;
      z2 := Conjugate(z1); { GPC extension }
      WriteLn ('Conjugate of z1 is: (', Re (z2) : 1, ',', Im (z2) : 1, ')');
      WriteLn;
      Len   := Abs (z1);
      Angle := Arg (z1);
      WriteLn ('The polar representation of z1 is: Length=', Len : 1,
               ', Angle=', Angle : 1);
      WriteLn;
      z2 := Polar (Len, Angle);
      WriteLn ('Converting (Length, Angle) back to (x, y) gives: (',
               Re (z2) : 1, ',', Im (z2) : 1, ')');
      WriteLn;
      WriteLn ('The following operations operate on the complex number z1');
      WriteLn;
      z2 := ArcTan (z1);
      WriteLn ('ArcTan (z1) = (', Re (z2), ', ', Im (z2), ')');
      WriteLn;
      z2 := z1 ** 3.141;
      WriteLn ('z1 ** 3.141 =', Re (z2), ', ', Im (z2), ')');
      WriteLn;
      z2 := Sin (z1);
      WriteLn ('Sin (z1) = (', Re (z2), ', ', Im (z2), ')');
      WriteLn ('(Cos, Ln, Exp, SqRt and Sqr exist also.)');
      WriteLn;
      z2 := z1 pow 8;
      WriteLn ('z1 pow 8 = (', Re (z2), ', ', Im (z2), ')');
      WriteLn;
      z2 := z1 pow (-8);
      WriteLn ('z1 pow (-8) = (', Re (z2), ', ', Im (z2), ')');
    end.

 

However, z1 and z2 are of type 'Complex', which doesn't exist in SCAR. I'm not sure why it doesn't, but you could make it work by creating the type.

Link to comment
Share on other sites

As far as I know, SCAR isn't really meant for calculations like these.

C++ would be a far better language to write it in.

 

SCAR has some functions dealing with polar points and cartesian points, normal sine functions and other basic math stuff.

PASCAL has a way to deal with complex numbers, I've found a demo about that below:

 

     program ComplexOperationsDemo;

    var
      z1, z2: Complex;
      Len, Angle: Real;

    begin
      z1 := Cmplx (2, 1);
      WriteLn;
      WriteLn ('Complex number z1 is: (', Re (z1) : 1, ',', Im (z1) : 1, ')');
      WriteLn;
      z2 := Conjugate(z1); { GPC extension }
      WriteLn ('Conjugate of z1 is: (', Re (z2) : 1, ',', Im (z2) : 1, ')');
      WriteLn;
      Len   := Abs (z1);
      Angle := Arg (z1);
      WriteLn ('The polar representation of z1 is: Length=', Len : 1,
               ', Angle=', Angle : 1);
      WriteLn;
      z2 := Polar (Len, Angle);
      WriteLn ('Converting (Length, Angle) back to (x, y) gives: (',
               Re (z2) : 1, ',', Im (z2) : 1, ')');
      WriteLn;
      WriteLn ('The following operations operate on the complex number z1');
      WriteLn;
      z2 := ArcTan (z1);
      WriteLn ('ArcTan (z1) = (', Re (z2), ', ', Im (z2), ')');
      WriteLn;
      z2 := z1 ** 3.141;
      WriteLn ('z1 ** 3.141 =', Re (z2), ', ', Im (z2), ')');
      WriteLn;
      z2 := Sin (z1);
      WriteLn ('Sin (z1) = (', Re (z2), ', ', Im (z2), ')');
      WriteLn ('(Cos, Ln, Exp, SqRt and Sqr exist also.)');
      WriteLn;
      z2 := z1 pow 8;
      WriteLn ('z1 pow 8 = (', Re (z2), ', ', Im (z2), ')');
      WriteLn;
      z2 := z1 pow (-8);
      WriteLn ('z1 pow (-8) = (', Re (z2), ', ', Im (z2), ')');
    end.

 

However, z1 and z2 are of type 'Complex', which doesn't exist in SCAR. I'm not sure why it doesn't, but you could make it work by creating the type.

 

Actually, I'm fairly sure that in "pascal", these are just record record types with operator overloading, so it's nothing you can't implement in SCAR with some tinkering.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
  • Create New...