Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.......

Thnx in advance.....
Posted
Updated 17-Oct-12 5:31am
v2
Comments
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:33am    
Make a struct with real and imaginary fields instead of doing multiple arrays, it will probably be easier.
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
N Shiva 18-Oct-12 8:27am    
ok...

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:

C++
typedef struct {
    float r;
    float i;
} COMPLEX;

COMPLEX myArray [10][20];

/* addressing elements, example */
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.
 
Share this answer
 
Comments
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 ?
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:

XML
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)
 
Share this answer
 
C++
#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;

}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900