[comp.lang.pascal] complex numbers

heff@ohstpy.mps.ohio-state.edu (07/13/89)

Is there an easy way to do complex numbers in pascal?  In particular is
there a way to write equations in source code such as
                     a =: b + c;
or
                     a := (b+c)/(d+e);
as can be done in fortran.  For that matter, are there any languages beside
fortran which allow easy computation of complex numbers?
                                        

conan@vax1.acs.udel.EDU (Robert B Carroll) (07/14/89)

In article <2751@ohstpy.mps.ohio-state.edu> heff@ohstpy.mps.ohio-state.edu writes:
>.  For that matter, are there any languages beside
>fortran which allow easy computation of complex numbers?
>                                        
Try ADA. You can overload +,-,/,* opereators.





-- 
conan@vax1.acs.udel.edu OR conan@192.5.57.1
*******************************************
****I only play DISC GOLF and drink tea****
*******************************************

ken@cs.rochester.edu (Ken Yap) (07/14/89)

|Is there an easy way to do complex numbers in pascal?  In particular is
|there a way to write equations in source code such as
|                     a =: b + c;
|or
|                     a := (b+c)/(d+e);
|as can be done in fortran.  For that matter, are there any languages beside
|fortran which allow easy computation of complex numbers?

Not in Pascal, but in Pascal-SC, a version designed for scientific
computation. So far all I know of it is from the book "Pascal-SC:  A
Computer Language for Scientific Computation" (Academic Press 1987).
Are implementations distributed?

Then there is C++. Please no language wars here ok?

ts@chyde.uwasa.fi (Timo Salmi LASK) (07/14/89)

In article <2751@ohstpy.mps.ohio-state.edu> heff@ohstpy.mps.ohio-state.edu writes:
>Is there an easy way to do complex numbers in pascal?  In particular is
>there a way to write equations in source code such as
>                     a =: b + c;
>or
>                     a := (b+c)/(d+e);
>as can be done in fortran.  For that matter, are there any languages beside

Forgive me if this reply is too trite, but one could define
  type complex = record
                    RlPart : real;
                    ImPart : real;
                 end;
and take it up from there by defining the basic operations for
complex numbers as functions.

...................................................................
Prof. Timo Salmi                                (Site 128.214.12.3)
School of Business Studies, University of Vaasa, SF-65101, Finland
Internet: ts@chyde.uwasa.fi Funet: vakk::salmi Bitnet: salmi@finfun

hal@pur-phy (Hal Chambers) (07/14/89)

In article <2751@ohstpy.mps.ohio-state.edu> heff@ohstpy.mps.ohio-state.edu writes:
>. . . to write equations in source code such as
>                     a =: b + c;
>                     a := (b+c)/(d+e);
>as can be done in fortran.  For that matter, are there any languages beside
>fortran which allow easy computation of complex numbers?

This is allowed in Ada.  Besides defining the type Complex, Ada allows
programmers to define functions named "+", "/", etc. (and if I remember
correctly, these functions can be defined as "INLINE" so that the code
is generated in place).  The compiler determines which "+" function to
use from the types of the operands.  To be complete, be sure to define
the operators for mixed-mode expressions (complex + real, real + complex,
etc.).

Although Pascal (and Modula-2, etc.) allows the definition of a Complex
type,  there is no way for the programmer to define the operators
other than as explicit procedure calls (functions can't return a
structured type).

So the examples above would appear as something like:
    >   a =: b + c;
		cadd(a, b,c);

    >   a := (b+c)/(d+e);
		cadd(c1, b,c);
		cadd(c2, d,e);
		cdiv(a, c1,c2);
Ugly!!!!

-- 
Hal Chambers
hal@newton.physics.purdue.edu
hal@physics-newton.arpa

d88-eli@nada.kth.se (Erik Liljencrantz) (07/14/89)

Complex arithmetic can't be done the nice way (i.e. a:=b+c; and so on) in
standard pascal. Suggestions of Pascal-SC, C++ and Ada has been posted, but
what about Turbo Pascal 5.5. It's objectoriented (like C++) but is it
possible to define addition (and the other operations as well) for objects?
Or is it just possible to define objectspecific procedures and functions?

If anybody missunderstood, things like this would be nice:

VAR
  A,B,C:COMPLEX;		{ Our complex objecttype }
...
  A:=B+C;			{ Not + but complex + }
...
  A:=(B+C)/(B-C);		{ Very complex... }

Procedures with VAR-parameters aren't that nice...:
VAR
  A,B,C,Tmp1,Tmp2:COMPLEX;
...
  ComplexAdd(A,B,C);		{ Not that ugly, but wait... }
...
  ComplexAdd(Tmp1,B,C);		{ ...this is ugly! }
  ComplexSub(Tmp2,B,C);
  ComplexDiv(A,Tmp1,Tmp2);

If functions could return our COMPLEX type then it could look like this:
VAR
  A,B,C:COMPLEX;
...
  A:=ComplexAdd(B,C);
...
  A:=ComplexDiv(ComplexAdd(B,C),ComplexSub(B,C));

Soon my update to Turbo Pascal 5.5 is here and then I'll try to add my
objects, but until then someone else might have done that...

Erik Liljencrantz		| "No silly quotes!"
d88-eli@nada.kth.se		|	Embraquel Tuta

dat0@rimfaxe.diku.dk (Dat-0 undervisningsassistent) (07/14/89)

d88-eli@nada.kth.se (Erik Liljencrantz) writes:

>Complex arithmetic can't be done the nice way (i.e. a:=b+c; and so on) in
>standard pascal. 

Rigth, sadly enough, but the new extended Pascal will be able to handle
complex numbers, if I'm not very much mistaken.


Kristian Damm Jensen 
(dat0@diku.dk)
Institute of datalogi, University of Copenhagen (DIKU)

rogerson@PEDEV.Columbia.NCR.COM (Dale Rogerson) (07/15/89)

In article <1301@draken.nada.kth.se> d88-eli@nada.kth.se (Erik Liljencrantz) writes:
>Complex arithmetic can't be done the nice way (i.e. a:=b+c; and so on) in
>standard pascal. Suggestions of Pascal-SC, C++ and Ada has been posted, but
>what about Turbo Pascal 5.5. It's objectoriented (like C++) but is it
>possible to define addition (and the other operations as well) for objects?

It is not possible to overload the standard operations.  Also no provisions
are made for making any prefix or postfix operators.  The language is
also a little weak in any kind of overloading.  I will check and see if an
object can have two methods with the same name, but different number of
parameters.

>Or is it just possible to define objectspecific procedures and functions?
>  A,B,C:COMPLEX;		{ Our complex objecttype }
>  A:=B+C;			{ Not + but complex + }
>  A:=(B+C)/(B-C);		{ Very complex... }

The above is not possible with turbo pascal 5.5.
This is possible with C++ and the new version of Eiffel may do this.

>Procedures with VAR-parameters aren't that nice...:
>  ComplexAdd(Tmp1,B,C);		{ ...this is ugly! }
>  ComplexSub(Tmp2,B,C);
>  ComplexDiv(A,Tmp1,Tmp2);


And the way you must do it is the ugly way.
The new OOPs features of TP are really not hurt that much by not having this
feature.

>If functions could return our COMPLEX type then it could look like this:
>  A,B,C:COMPLEX;
>  A:=ComplexAdd(B,C);
>  A:=ComplexDiv(ComplexAdd(B,C),ComplexSub(B,C));

I don't think that this will work either.  My refernece manual for TP 5.5
does not say that functions have been extended to return objects, so I would
bet that this is not possible.

I will also check this out and see if it will work.
Even, if these features do not work in TP 5.5 it is still a major improvment
over the standard pascal.




-----Dale
	Rogerson----

murphy@pur-phy (William J. Murphy) (07/15/89)

In article <2360@pur-phy> hal@newton.physics.purdue.edu.UUCP (Hal Chambers) writes:
>In article <2751@ohstpy.mps.ohio-state.edu> heff@ohstpy.mps.ohio-state.edu writes:
>>. . . to write equations in source code such as
>>                     a =: b + c;
>>                     a := (b+c)/(d+e);
>>as can be done in fortran.  For that matter, are there any languages beside
>>fortran which allow easy computation of complex numbers?
>
>Although Pascal (and Modula-2, etc.) allows the definition of a Complex
>type,  there is no way for the programmer to define the operators
>other than as explicit procedure calls (functions can't return a
>structured type).
>
>So the examples above would appear as something like:
>    >   a =: b + c;
>		cadd(a, b,c);
>
>    >   a := (b+c)/(d+e);
>		cadd(c1, b,c);
>		cadd(c2, d,e);
>		cdiv(a, c1,c2);
>Ugly!!!!
This is one approach, another which works and is not quite so ugly is
to create a memory pool for temporary calculation and use pointers
into that pool.  This way you can write
    TempPtr := cdiv( cadd(^b, ^c), cadd(^d, ^e) );
    a.x     := TempPtr^.x;
    a.y     := TempPtr^.y;

Don't flame the syntax, I haven't done this for a month at least.
The point is that you can use pointers to the variable rather
than passing a bunch of values and then you must be certain to 
reassign the values in the TempPtr into the original variable.

The pool is treated as a circular buffer which has an arbitrary
size (read big enough to fit your longest expression) I use a buffer
of 150 double precision complex numbers. 

If you are interested in a copy of the Turbo-Pascal source that
I have, send e-mail to me. I feel that it is too long to post on the
net.

Bill Murphy
murphy@newton.physics.purdue.edu

binni@raunvis.UUCP (Brynjolfur Thorsson ) (07/15/89)

In article <2751@ohstpy.mps.ohio-state.edu>, heff@ohstpy.mps.ohio-state.edu writes:
> Is there an easy way to do complex numbers in pascal?  In particular is
> there a way to write equations in source code such as
>                      a =: b + c;
> or
>                      a := (b+c)/(d+e);
> as can be done in fortran.  For that matter, are there any languages beside
> fortran which allow easy computation of complex numbers?
>                                         


You should consider C++.  C++ allows overflow of operators, so you can
use a = b + c, if a, b, and c are of class complex.
Hope you got sence out of this

Brynjolfur

jkmedcal@uokmax.UUCP (Jeff K Medcalf) (07/16/89)

In article <2751@ohstpy.mps.ohio-state.edu> heff@ohstpy.mps.ohio-state.edu writes:
>Is there an easy way to do complex numbers in pascal?  In particular is
>there a way to write equations in source code such as
>                     a =: b + c;
>or
>                     a := (b+c)/(d+e);

Well, one way (not quite this easy) would by to declare a

type
  complex_number = record
		     re : real;
		     im : real
		   end; {complex_number}

var
  a, b, c, d, e : complex_number;

and then

		a.re := b.re + c.re; a.im := b.im + c.im;
or
		a.re := (b.re + c.re)/(d.re + e.re);
		a.im := (b.im + c.im)/(d.im + e.im);

Perhaps not as simple as you want, but it does work well.


-- 
I dream I'm safe				jkmedcal@uokmax.UUCP
In my hotel womb 				Jeff Medcalf
Soft and so nice
It's a wonderful womb				<-The Church, "Hotel Womb"

damm@rimfaxe.diku.dk (Kristian Damm Jensen) (07/16/89)

hal@pur-phy (Hal Chambers) writes:

>In article <2751@ohstpy.mps.ohio-state.edu> heff@ohstpy.mps.ohio-state.edu writes:
>>. . . to write equations in source code such as
>>                     a =: b + c;
>>                     a := (b+c)/(d+e);
>>as can be done in fortran.  For that matter, are there any languages beside
>>fortran which allow easy computation of complex numbers?

>Although Pascal (and Modula-2, etc.) allows the definition of a Complex
>type,  there is no way for the programmer to define the operators
>other than as explicit procedure calls (functions can't return a
>structured type).

No, but you can return a pointer to a structured type. 
In fact I once received a whole packages of about 10K to do 
complex-arithmetic programmed that way.

>So the examples above would appear as something like:
>    >   a =: b + c;
>		cadd(a, b,c);
or if you use functions as described

	a := cadd (b, c);
>    >   a := (b+c)/(d+e);
>		cadd(c1, b,c);
>		cadd(c2, d,e);
>		cdiv(a, c1,c2);
>Ugly!!!!
	a := cdiv (cadd (b, c), cadd (d, e))

with a, b, c, d and e being pointers to the records containing the complex 
numbers. Not to bad if yoy don't dislike a lisp-like programming style.

-------------------------------------------------------------------------
Kristian Damm Jensen (damm@freja.diku.dk)

ejablow@dasys1.UUCP (Eric Robert Jablow) (07/26/89)

I have recently written a TP5.5 unit for using compolex numbers as
objects.  I wanted to do it this way because I intend to extend the
complex numbers to the "Riemann sphere" and to the quaternions; also, I
wanted to learn TP5.5.  I can't post it here; it's extremely long,
as I added everything but the kitchen sink; also, my error handling
routine uses two units from TurboPower's Turbo Professional 5.0, and
if you wanted to recompile my unit, you would need that product or you
would have to change the routine.  Here's a typical example of how it
might be used.

Program Example;	{Print the 3 third roots of 5.0 + 3.0i}

uses Complex;

var
  z     : ComplexNumber;
  root : ComplexNumber;
  j     : Integer;

begin
  z.Init(5.0, 3.0);	{z = 5 + 3i}
  w.Copy(CZero);        {root = 0.0 + 0.0i}

  For j := 0 To 2 Do
    root := z;         {root = 5 + 3i}
    root.BrComplexPower((1.0/3.0), 2.0*Pi*j);
			{replace root by its third root, where}
			{the argument of the original root has its}
			{argument assumed to be between 2*pi*j}
			{ and 2*pi*(j+1).}
    root.Display;       {write it on the standard output.}
    WriteLn;
  End {of For}
End;
-- 
Eric Jablow                      {allegra,philabs,cmcl2}!hombre\
Big Electric Cat Public Unix           {bellcore,cmcl2}!cucard!dasys1!ejablow
New York, NY, USA	 	 
New address:	eric%sbmath@sbee.sunysb.edu.