[comp.sys.amiga] Complex Numbers in C

usenet@orstcs.CS.ORST.EDU (Usenet programs owner) (01/10/89)

Distribution: usa
Organization: OR St U, Comp Sci 
From: nelsonr@romana.cs.orst.edu (Rolf Christopher Nelson)
Path: romana!nelsonr

Are there any library functions out there for dealing with complex math,
or will I end up writing them all myself?  Ugh!  (That is the one thing
FORTRAN has going for it.)  I'm looking for C libraries....
Thnx,
 Rolf,  With no fancy sigs yet...

daveh@cbmvax.UUCP (Dave Haynie) (01/11/89)

in article <8166@orstcs.CS.ORST.EDU>, usenet@orstcs.CS.ORST.EDU (Usenet programs owner) says:
> Sender:R C Nelson

> Are there any library functions out there for dealing with complex math,
> or will I end up writing them all myself?  Ugh!  (That is the one thing
> FORTRAN has going for it.)  I'm looking for C libraries....
> Thnx,
>  Rolf,  With no fancy sigs yet...

If you're doing lots of stuff with complex numbers, I'd recommend C++ from
Lattice.  It comes with a complex number library, including the following
functions: cos(), cosh(), exp(), log(), pow(), sin(), sinh(), sqrt(), abs(),
real(), imag(), polar(), conj(); and operators: +, -, *, /, ==, !=, +=,
-=, *=, /=, >>, <<.  Since C++ is extensible, you work it pretty much like
you might in fortran:

	complex a, b(1,0), c(0,1), d(5.3,23.7);

	a = (b + d) * c;

And so on.

I 
-- 
Dave Haynie  "The 32 Bit Guy"     Commodore-Amiga  "The Crew That Never Rests"
   {uunet|pyramid|rutgers}!cbmvax!daveh      PLINK: D-DAVE H     BIX: hazy
              Amiga -- It's not just a job, it's an obsession

cg@myrias.UUCP (Chris Gray) (01/11/89)

In article <8166@orstcs.CS.ORST.EDU> usenet@orstcs.CS.ORST.EDU (Usenet programs owner) writes:
>
>Are there any library functions out there for dealing with complex math,
>or will I end up writing them all myself?  Ugh!  (That is the one thing
>FORTRAN has going for it.)  I'm looking for C libraries....
>Thnx,
> Rolf,  With no fancy sigs yet...

In order to test out the "operator type" facility in Draco V1.2, I implemented
a complex number type. A friend here, Don Reble, did some of the complex
trancendental functions for me. With it, you can do things like

    complex a, b;
    
    readln(a, b);
    a := a * b;
    writeln("a = ", a, "b * 3 = ", b * complex(3.0, 0.0));
    etc.

This new version should be out this month (probably on Compu$erve as well
as sending it to Fred Fish) - there is only one thing left on my list of
"things to do before release". The full source for the complex number
package will be included.

-- 
Chris Gray		Myrias Research, Edmonton	+1 403 428 1616
	{uunet!mnetor,ubc-vision,watmath,vax135}!alberta!myrias!cg

thad@cup.portal.com (Thad P Floryan) (01/12/89)

Re: the request for complex math functions for C ...

Lest we forget, Fred Fish is the author of the Portable Math Library written
while he was a Goodyear Aerospace.  The PML (distributed in source form and
freely redistributable (my copy accompanied the KCC compiler distribution
from SRI-International)) includes all the transcendental functions and the
arithmetic operations (and other stuff).

Surprisingly, a quick perusal of the index to the "Fish Disks" didn't elicit
any matches to "pml", "math", "libr", etc. 

The code compiles and runs fine on the following systems I've personally
tested: DEC-20, VAX/VMS, and the Amiga (using Manx' C).

I'm willing to post it ONCE (and send a copy to Fred in the event his copy
was "misplaced").

To whom should it be sent?  To Bob (for inclusion with the comp.sources.amiga
stuff)?  If so, please send email instructions; the docs and possibly also
the sources have form-feeds, so it's not clear whether a "simple" shar
packaging is "safe."

Thad Floryan [thad@cup.portal.com (OR) ..!sun!portal!cup.portal.com!thad]

daveh@cbmvax.UUCP (Dave Haynie) (01/13/89)

in article <759@myrias.UUCP>, cg@myrias.UUCP (Chris Gray) says:
> Summary: Draco has them

> In order to test out the "operator type" facility in Draco V1.2, I implemented
> a complex number type. A friend here, Don Reble, did some of the complex
> trancendental functions for me. With it, you can do things like

>     complex a, b;

>     readln(a, b);
>     a := a * b;
>     writeln("a = ", a, "b * 3 = ", b * complex(3.0, 0.0));
>     etc.

> Chris Gray		Myrias Research, Edmonton	+1 403 428 1616

Hey, pretty cool.  Are you fully supporting object definitons, or just an
expanded type definition.  In either case, that's certainly one over what
you can do in C or Modula2 (one of the reasons my language of choice for
any high level programming is currently C++).  Do you allow overloading of
functions as well as operators.  One of the more foolish parts of C, if
you're using standard libraries, is all the different function names you
need to do the same basic operation on different data types.  You could
end up have abs(int), fabs(float), dabs(double float), etc. for what's
basically a very simple function.  Add in complex numbers, and you'd have
now cabs(complex), or whatever.  C++ would let you build a function called
"abs()", or actually several functions, one for each data type.  The compiler
could tell you if you try calling abs() on a type without such a function
(really, an object without such a function, but it looks pretty similar).
After several months of C++, I can't imagine using a language full time
that doesn't support such features.  Sounds to me that Draco is at least
a significant part of the way there.

					-Dave Haynie

-- 
Dave Haynie  "The 32 Bit Guy"     Commodore-Amiga  "The Crew That Never Rests"
   {uunet|pyramid|rutgers}!cbmvax!daveh      PLINK: D-DAVE H     BIX: hazy
              Amiga -- It's not just a job, it's an obsession

cs161agc@sdcc10.ucsd.EDU (John Schultz) (01/14/89)

In article <5685@cbmvax.UUCP> daveh@cbmvax.UUCP (Dave Haynie) writes:
>Hey, pretty cool.  Are you fully supporting object definitons, or just an
>expanded type definition.  In either case, that's certainly one over what
>you can do in C or Modula2 (one of the reasons my language of choice for
>any high level programming is currently C++).  Do you allow overloading of
>functions as well as operators.  One of the more foolish parts of C, if
>					-Dave Haynie

  I've been using C++ on Unix at school (on a UNISYS 7000); very
nice, very nice. Virtual functions and function overloading is
really bitchen. I heard a rumor somewhere that Wirth has a new
compiler coming out that's object oriented and very fast. Sounds
exciting.
  I've also got Lattice 5.0 on my 16 bit Amiga.  I
couldn't possibly imagine doing any serious work with C++ on the
Amiga considering how incredibly slow Lattice compiles (it's a fine
product, just too slow for me).  But, seeing as how you're the "32
bit guy", it's probably quite nice on a 32 bit Amiga.


  John Schultz

fnf@estinc.UUCP (Fred Fish) (01/15/89)

In article <13459@cup.portal.com> thad@cup.portal.com (Thad P Floryan) writes:
>Re: the request for complex math functions for C ...
>
>Lest we forget, Fred Fish is the author of the Portable Math Library written
>while he was a Goodyear Aerospace.  The PML (distributed in source form and
>freely redistributable (my copy accompanied the KCC compiler distribution
>from SRI-International)) includes all the transcendental functions and the
>arithmetic operations (and other stuff).
>
>Surprisingly, a quick perusal of the index to the "Fish Disks" didn't elicit
>any matches to "pml", "math", "libr", etc. 

Just for those that are interested, here is some history about PML.  It
was written as my first C project, and was actually partially an execise
in writing C.  Thus it is not exactly my best C code.  At the time, the
only C compilers I had access to were the Decus C compilers on a PDP-11
and another primitive C compiler on the DECSYSTEM-20.  Neither of these
compilers had structure passing, so the code does lots of passing of
pointers to structures of type "complex" (real and imag parts).  Also,
it used one of the very first versions of my macro based C debugging 
package, which had quite a few warts.  Nevertheless, the code worked
quite well, and seemed to be only about 50% slower than the native
math libraries on both of my test machines, which presumably were
written in hand optimized assembly language (remember, these were
NOT UNIX machines).

One of the nice features of the library is that it assumes only the
existance of the basic floating point operations supported by the
compiler (+,-,*,/).  Everything else is built up from scratch using
either machine (floating point format) specific C code, or portable
C code.  Thus once the primitive machine specific C code is done,
everything else falls into place.

Several years after releasing the first version (which is probably what
Thad has), I went back and made several aborted attempts to overhaul the
code.  I wanted to eliminate the structure pointer passing and pass the
actual "complex" structures, since most modern C compilers now support this
feature.  This eliminates LOTS of kludgery.  I also wanted to retrofit
my latest C debugging package macros, reorganize some of the low level
functions to use standard UNIX style floating point primitives (ldexp,
frexp, modf, etc), use standard UNIX style error handling (matherr),
and use UNIX libm compatible calling conventions for the functions
of the same name.  This would make porting easier to UNIX systems
because one would not have to rewrite the primitives (ldexp, frexp,
modf, etc).  Some of these partially converted versions may have
been released before I finally gave up on the project due to lack
of time and released the code into the public domain, hoping someone
would "adopt" it, clean it up, and rerelease it.  This version was
posted to the net about a year or two ago, and is probably the only
version that I currently have on line somewhere.  So far I haven't
seen any sign that an improved/cleaned-up version was ever produced.

-Fred
-- 
# Fred Fish, 1835 E. Belmont Drive, Tempe, AZ 85284,  USA
# asuvax!nud!estinc!fnf

daveh@cbmvax.UUCP (Dave Haynie) (01/24/89)

in article <53@sdcc10.ucsd.EDU>, cs161agc@sdcc10.ucsd.EDU (John Schultz) says:

>   I've also got Lattice 5.0 on my 16 bit Amiga.  I
> couldn't possibly imagine doing any serious work with C++ on the
> Amiga considering how incredibly slow Lattice compiles (it's a fine
> product, just too slow for me).  But, seeing as how you're the "32
> bit guy", it's probably quite nice on a 32 bit Amiga.

I'll have to admit that, yeah, I do use it exclusively on A2500 type
machines.  With fast hard drive, too.  I mentioned on bix once that I
thought Lattice C++ was palatable on a 32 bit machine thus equipped, but
I wouldn't want to try it on a vanilla Amiga.  Tim Holloway, who wrote
the thing, replied back that he developed it on a floppy-based A1000 with
extra memory.  So I guess it can be used on such a machine, but it's
certainly got to be over 4x slower per file compiled than Manx C.  I still
which it were faster, and perhaps if it's successful in the cfront version,
Lattice will produce a two phase compiler for it that works more like their
current C compiler.

>   John Schultz
-- 
Dave Haynie  "The 32 Bit Guy"     Commodore-Amiga  "The Crew That Never Rests"
   {uunet|pyramid|rutgers}!cbmvax!daveh      PLINK: D-DAVE H     BIX: hazy
              Amiga -- It's not just a job, it's an obsession