[comp.lang.c] complex data manipulation

wjbrown@evax0.eng.fsu.edu (WILLIAM J. BROWN) (05/30/91)

I'm writing a program in C (of course) which requires the inversion of
a rather large matrix.  This by itself is no problem except that some of
the elements in the array are complex.  Is there an easy way to handle
complex data in C?  I have a book titled *C TOOLS for Scientists and
Engineers* where the author uses the header file #include <complex.h>
and uses functions such as CADD(),CSUB(),CMULT() etc. for complex
arithmetic.  I have several books on C including The Waite Group's
*C Primer Plus* and Schaum's *Programming in C* neither of which make
any mention of any of these header files or functions.  Has anyone out
there written a program that would invert a complex matrix?  Any help
would be greatly appreciated.
				William Brown 

mouse@thunder.mcrcim.mcgill.edu (der Mouse) (06/02/91)

In article <1991May29.211117.29303@mailer.cc.fsu.edu>, wjbrown@evax0.eng.fsu.edu (WILLIAM J. BROWN) writes:

> I'm writing a program in C (of course) which requires the inversion
> of a rather large matrix.  This by itself is no problem except that
> some of the elements in the array are complex.  Is there an easy way
> to handle complex data in C?

Not especially.  You can define a small struct and then have functions
that operate on these things, but C provides no way to make the
"natural" way of writing things with the usual arithmetic operators
work.  For that you need a language that allows the programmer to
extend operator polymorphism, like C++.

> I have a book titled *C TOOLS for Scientists and Engineers* where the
> author uses the header file #include <complex.h> and uses functions
> such as CADD(),CSUB(),CMULT() etc. for complex arithmetic.

Not hard to do.  Something like

complex.h:
	typedef struct { double r; double i; } complex;
	complex CADD(complex,complex);
[prototypes for other functions omitted]

complex.c:
	complex CADD(complex a, complex b)
	{ complex sum; sum.r = a.r + b.r; sum.i = a.i + b.i; return(sum); }
[code for other functions omitted]

					der Mouse

			old: mcgill-vision!mouse
			new: mouse@larry.mcrcim.mcgill.edu