Click here to Skip to main content
15,892,797 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have a matlab program in which i have to calculate values of an expression containing complex numbers.

I have to convert the same program to C language so as to calculate the enegry estimate using another software.

Now the bone of contention here for me is how can i change the complex number expression in MATLAB to that of the equivalent in C .

Please help, in case one needs i can put the expression here as well.

Thanks,
Chandra.
Posted

Poorly speaking, complex numbers are just an ordered pair of real ones, following certain rules. Hence you may code them this way:
C
typedef struct tagComplex
{
  double re; // real part
  double im; // imaginary part
} Complex;


Then you may define the functions acting on them (and enabling you to solve expressions).
For instance,

setu.dixit19 wrote:
Qen[n1]=exp(j*(n1-1)*(vv0[k]-vv0[l]))


It looks you need the complex exp function

C
void complex_exp(Complex * pOperand, Complex * pResult)
{
  double er = exp(pOperand->re);
  pResult->re = er * cos( pOperand->im);
  pResult->im = er * sin( pOperand->im);
}
 
Share this answer
 
The most basic way would be to create a struct defining each component of the number. Assuming you are only working in 1 dimension then your complex number will be of the form ai+b, and so your struct would look like
typedef struct {
    float a; //The "imaginary" part. Might be a int instead
    float b; //The real part. Might be a int instead
} ComplexNumber;


Could you please provide the equation and indicate weather it needs to work on other similar equations or only this exact one and the operations that you are wanting to perform.
If it is a relatively simple equation like sqrt(-1) then there can be a simple answer without having to implement a complex numbers system.
ComplexNumber ComplexSqrt(float nNumber) {
    ComplexNumber cnRes = { 0, 0 };
    if (nNumber < 0) { //nNumber is negative. Result will be complex
        cnRes.a = sqrt(-nNumber);
    } else { //nNumber is positive. Result will be real
        cnRes.b = sqrt(nNumber);
    }
    return cnRes;
}
 
Share this answer
 
Thanks Andrew for your response.
However the equation that i am reffering to as given below:

"Qen[n1]=exp(j*(n1-1)*(vv0[k]-vv0[l]))"

This expression is the one that i need to calculate and the concern is how to remove the 'j' part from the equation.

Looking forward to your further replies ..

cheers
Setu
 
Share this answer
 
If you consider the complex number in the form of a+bi and like to look a code in C# then here it is. You can use operator overloading to perform arithmatic operations with complex numbers

C#
class ComplexNumber
{
    public double Number1{ get; set;}
    public double Number2{ get; set;}
    public double ImaginaryUnit { get; set; }
    public ComplexNumber()
    {

    }
    public ComplexNumber(double number1, double number2, double imaginaryUnit)
    {
        this.Number1 = number1;
        this.Number2 = number2;
        this.ImaginaryUnit = imaginaryUnit;
    }

    public static ComplexNumber operator +(ComplexNumber Number1, ComplexNumber Number2)
    {
        ComplexNumber number = new ComplexNumber();
        number.Number1 = Number1.Number1 + Number2.Number1 ;
        number.Number2 = Number1.Number2 + Number2.Number2;
        number.ImaginaryUnit = Number1.ImaginaryUnit;
        return number;
    }

    public static ComplexNumber operator -(ComplexNumber Number1, ComplexNumber Number2)
    {
        ComplexNumber number = new ComplexNumber();
        number.Number1 = Number1.Number1 - Number2.Number1;
        number.Number2 = Number1.Number2 - Number2.Number2;
        number.ImaginaryUnit = Number1.ImaginaryUnit;
        return number;
    }
    public static ComplexNumber operator *(ComplexNumber Number1, ComplexNumber Number2)
    {
        double ac = Number1.Number1 * Number2.Number1;
        double bd = Number1.Number2 * Number2.Number2;
        double bc = Number1.Number2 * Number2.Number1;
        double ad = Number1.Number1 * Number2.Number2;
        double result = (ac - bd) + ((bc + ad) * Number1.ImaginaryUnit);
        ComplexNumber number = new ComplexNumber();
        number.Number1 = ac-bd;
        number.Number2 = bc-ad;
        number.ImaginaryUnit = Number1.ImaginaryUnit;
        return number;
    }

    public static ComplexNumber operator /(ComplexNumber Number1, ComplexNumber Number2)
    {
        double square = 2;
        double ac=Number1.Number1 * Number2.Number1;
        double bd=Number1.Number2 * Number2.Number2;
        double bc=Number1.Number2 * Number2.Number1;
        double ad=Number1.Number1 * Number2.Number2;

        double c2=System.Math.Pow(Number2.Number1, square);
        double d2=System.Math.Pow(Number2.Number2, square);
        ComplexNumber number = new ComplexNumber();
        number.Number1 = (ac + bd) / (c2 + d2);
        number.Number2 = (bc - ad) / (c2 + d2);
        number.ImaginaryUnit = Number1.ImaginaryUnit;
        return number;
    }
}
 
Share this answer
 
Comments
CPallini 15-Feb-11 8:19am    
Did you miss the 'C' tag?

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