12,896,108 members (51,877 online)
Rate this:
See more:
I have written a program related to Graph theory in 'C'. And I have used lot of arrays(2

dimensional) in that program. All arrays are of containing floats.(Meant that all my operations

are done b/w floats) Now I have to modify the program to see that it should also work for complex

numbers.For that I want to split each and every array into two arrays one is real and another is

imaginary.

Is there any simple way to take complex numbers as inputs and to do operations b/w them ?

In which way it will be simple ??

In which language it will be simple ??

I knew python,C languages.......

Posted 17-Oct-12 5:17am
N Shiva531
Updated 17-Oct-12 5:31am
v2
Kenneth Haugland 17-Oct-12 11:20am

In .NET we have Complex class in System.Numerics....
and I have used the class here:
http://www.codeproject.com/Articles/199800/Evaluate-complex-and-real-math-calculator
N Shiva 17-Oct-12 11:30am

Is there any other way in 'C', except what I mentioned above.
lewax00 17-Oct-12 11:32am

Python supports complex numbers as a basic type, if you aren't stuck with C (wasn't sure from your post): http://docs.python.org/library/stdtypes.html#numeric-types-int-float-long-complex
lewax00 17-Oct-12 11:33am

Make a struct with real and imaginary fields instead of doing multiple arrays, it will probably be easier.
N Shiva 18-Oct-12 8:27am

ok...
YvesDaoust 18-May-13 13:19pm

It will be simple in any language (that supports floating-point arithmetic and 2D arrays), as you essentially need to encode the formulas for the 4 basic operations on complex numbers where you did it for floats.

Rate this:

## Solution 1

Your approach of splitting every array into a real and imaginary part is absolutely legitimate. As an alternative you can create you own structure for a complex number, for example:

```typedef struct {
float r;
float i;
} COMPLEX;

COMPLEX myArray [10][20];

float real5_7 = myArray[5][7].r;
float imag5_7 = myArray[5][7].i;```

Things get a bit easier in C++, in which you can define a full-blown Complex data type, including all the standard operators.

And as Kenneth has pointed out already, C# offers a built-in data type Complex, just for free.
N Shiva 17-Oct-12 11:55am

Assume if i have defined a structure named complex, which has two fields i mean 2 floats one is imaginary and another is real.... How addition & multiplication of these no.s will be simple ?
nv3 17-Oct-12 12:05pm

Unfortunately, in C you have to code addition and multiplication of complex numbers yourself. Typically you would create some helper functions that take as input two COMPLEX arguments and deliver the result. These will be just a couple of trivial lines.
N Shiva 17-Oct-12 12:11pm

ok..thnx.. I will create a structure and i will write functions to add complex no.s (2 real numbers and 2 imaginary no.s) But how to get product ?
nv3 17-Oct-12 15:11pm

Just as simple as addition. In case you have forgotten how to multiply two complex numbers, let me refresh your memory. If (r1,i1) and (r2,i2) are the two complex numbers, their product is (r1*r2 - i1*i2, r1*i2 + r2*i1). Perhaps you should take a quick refresher at <en.wikipedia.org/wiki/Complex_number#Multiplication_and_division> :-)
N Shiva 18-Oct-12 10:50am

I meant, I knew how to get product mathematically, If i wrote a function, How to return two no.s (imaginary & real) at a time ?
nv3 18-Oct-12 11:21am

COMPLEX CmplxMul (COMPLEX c1, COMPLEX c2)
{
COMPLEX res;
res.r = ...
res.i = ...
return res;
}
Rate this:

## Solution 2

In C + + is a class dedicated to work with complex numbers, which is part of its standard library, defined in the library complex. Using this will make you work easier because the class provides a set of functions to perform arithmetic operations on this set of numbers, in addition to the I/O. For example:

```template< class T> complex<T> operator+(const complex<T>&,const complex<T>&);
template< class T> complex<T> operator+(const complex<T>&,const T&;
template< class T> complex<T> operator+(const T&,const complex<T>&);
//similarly: -,*,/,==,and,!=
template< class T> complex<T> operator+(const complex<T>&);
template< class T> complex<T> operator-(const complex<T>&);
template< class T> T real(const complex<T>&);
template< class T> T imag(const complex<T>&);
template< class T> complex<T> conj(const complex<T>&);

template< class T> complex<T> sin(const complex<T>&);
//similarly:sinh,sqrt,tan,tanh,con,cosh,exp,log,and log10

template< class T> complex<T> pow(const complex<T>&,int);
template< class T> complex<T> pow(const complex<T>&,const T&);
template< class T> complex<T> pow(const complex<T>&,const complex<T>&);```

If you don´t know how to work with templates, I recommend you the following book:
97 Addison Wesley - The C + + Programming Language - Stroustrup (Third Edition)
Rate this:

## Solution 3

```#include <stdio.h>      /* Standard Library of Input and Output */
#include <complex.h>    /* Standart Library of Complex Numbers */

int main() {

double complex z1 = 1.0 + 3.0 * I;
double complex z2 = 1.0 - 4.0 * I;

printf("Working with complex numbers:\n\v");

printf("Starting values: Z1 = %.2f + %.2fi\tZ2 = %.2f %+.2fi\n", creal(z1), cimag(z1), creal(z2), cimag(z2));

double complex sum = z1 + z2;
printf("The sum: Z1 + Z2 = %.2f %+.2fi\n", creal(sum), cimag(sum));

double complex difference = z1 - z2;
printf("The difference: Z1 - Z2 = %.2f %+.2fi\n", creal(difference), cimag(difference));

double complex product = z1 * z2;
printf("The product: Z1 x Z2 = %.2f %+.2fi\n", creal(product), cimag(product));

double complex quotient = z1 / z2;
printf("The quotient: Z1 / Z2 = %.2f %+.2fi\n", creal(quotient), cimag(quotient));

double complex conjugate = conj(z1);
printf("The conjugate of Z1 = %.2f %+.2fi\n", creal(conjugate), cimag(conjugate));

return 0;

}```
v2

Top Experts
Last 24hrsThis month
 OriginalGriff 160 CHill60 120 ppolymorphe 110 Richard MacCutchan 85 Vinod Jangle 60
 OriginalGriff 5,222 CHill60 3,548 Jochen Arndt 2,958 Karthik Bangalore 2,456 ppolymorphe 2,365