[comp.lang.c] complex arithmetic in C

ashcraft@yale.UUCP (Cleve Ashcraft) (03/27/89)

i've got to do some complex analysis in C, and this is the one time
i look back kindly to all the fortran programming which i've done.

i've seen examples of creating a complex struct and writing
procedures to operate on complex numbers, but those always struck
me as lessons on structures rather then a good implementation.

i've got a bit of algorithm development to do, for which ease of
coding is most important. later i'll optimize the computations.
the kernels are CAXPY's and CDOT's, and their sparse indexed versions.

dealing with struct's may be easy, but will it later have to be
hardcoded into two arrays for efficiency?

any suggestions/experience to pass on? thanks.

cleve ashcraft
ashcraft@cs.yale.edu

gwyn@smoke.BRL.MIL (Doug Gwyn ) (03/27/89)

In article <54901@yale-celray.yale.UUCP> ashcraft@yale.UUCP (Cleve Ashcraft) writes:
>dealing with struct's may be easy, but will it later have to be
>hardcoded into two arrays for efficiency?

Two arrays of what?

Long ago in a company far away, I developed a set of complex arithmetic
routines which I've since improved.  They are of course based on the
use of structures.  My particular implementation passes around pointers
to the structures rather than the structures themselves; depending on
your C implementation, this can be more efficient.  Maximal efficiency
calls for in-line expansion of many of the "functions" as macros rather
than actual function calls.

If you're comfortable with Polish or LISP notation, this works fine.

P.S.  Naive implementation of functions such as division and principal
square root can get one in trouble.

dik@cwi.nl (Dik T. Winter) (03/28/89)

In article <9933@smoke.BRL.MIL> gwyn@brl.arpa (Doug Gwyn (VLD/VMB) <gwyn>) writes:
 > In article <54901@yale-celray.yale.UUCP> ashcraft@yale.UUCP (Cleve Ashcraft) writes:
 > >dealing with struct's may be easy, but will it later have to be
 > >hardcoded into two arrays for efficiency?
 > 
 > Two arrays of what?
 > 
The question was: should an array of complex be stored as such or as two
arrays of reals, real and imaginary parts.  It is difficult to say which
is more efficient, it is very machine dependent, although on most machines
storing as two arrays will be slightly more efficient (it gives a slightly
better locality of reference, and hence a slightly better cache hit ratio).
However, on vector machines storing as two arrays is in general a big win,
especially if the vector instructions do not allow strides (CDC, ETA).
But do not worry, C does not support vector machines very well.
-- 
dik t. winter, cwi, amsterdam, nederland
INTERNET   : dik@cwi.nl
BITNET/EARN: dik@mcvax

jima@hplsla.HP.COM (Jim Adcock) (03/30/89)

For complex numbers, vectors, matrices, etc, use the C++
dialect of C, rather than the K+R or ANSI dialects.

For anybody doing serious math, the operator overloading
is a godsend.