用C语言声明复数 [英] declaring complex numbers in C language

查看:260
本文介绍了用C语言声明复数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个matlab程序,其中必须计算包含复数的表达式的值.

我必须将同一程序转换为C语言,以便使用其他软件来计算能源估算.

现在对我而言,争论的焦点是如何将MATLAB中的复数表达式更改为C中的等价表达式.

请帮助,以防万一我也可以将表达式放在这里.

谢谢,
Chandra.

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.

推荐答案

很糟糕,复数只是遵循一定规则的有序对实数.因此,您可以通过以下方式对其进行编码:
Poorly speaking, complex numbers are just an ordered pair of real ones, following certain rules. Hence you may code them this way:
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写道:
setu.dixit19 wrote:

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

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



看来您需要复杂的exp函数



It looks you need the complex exp function

void complex_exp(Complex * pOperand, Complex * pResult)
{
  double er = exp(pOperand->re);
  pResult->re = er * cos( pOperand->im);
  pResult->im = er * sin( pOperand->im);
}


最基本的方法是创建一个定义数字每个组成部分的结构.假设您仅在1维上工作,那么您的复数将采用ai+b的形式,因此您的结构看起来像
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;



您能否提供方程式并指出天气,它需要用于其他类似方程式或仅适用于此精确方程式以及您要执行的操作.
如果它是一个像sqrt(-1)这样的相对简单的方程,则可以简单地回答而不必实现复杂的数字系统.



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;
}


感谢安德鲁的回答.
但是,我所参考的方程式如下所示:

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

该表达式是我需要计算的表达式,关心的是如何从等式中删除"j"部分.

期待您的进一步答复..

欢呼
塞图
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


这篇关于用C语言声明复数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆