简体   繁体   English

以极坐标形式表示的复数之和

[英]Sum of complex numbers expressed in polar form

I need to sum two complex numbers (c1,c2) and then express the result in its polar form.我需要将两个复数 (c1,c2) 相加,然后以极坐标形式表示结果。

I don't really know how to access the result for c1+c2, I mean I store them in the variable "result" but when I try to access them I find myself in the ComplexPolar structure and so I can't access the result.real and result.img to calculate magnitude and angle:我真的不知道如何访问 c1+c2 的结果,我的意思是我将它们存储在变量“result”中,但是当我尝试访问它们时,我发现自己处于 ComplexPolar 结构中,所以我无法访问结果.real 和 result.img 来计算大小和角度:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

struct ComplexCartesian
{
    float real;
    float img;
};

struct ComplexPolar
{
    float magnitude;
    float angle;
};

struct ComplexPolar add_two_complex(struct ComplexCartesian c1, struct ComplexCartesian c2, struct ComplexPolar result)
{
    result.real= c1.real+c2.real;
    result.img=c1.img+c2.img;

    result.magnitude= sqrt((result.real)^2 + (result.img)^2);
    result.angle= atan2(result.img, result.real);
}

^2 is not how you square in C, you have to either multiply the number by itself or use libc pow function. ^2不是您在 C 中的平方,您必须将数字乘以自身或使用 libc pow function。

^2 is a XOR operation where you aim to toggle the second bit, but in your case you are using it on a float which violates the strict aliasing rule and cause undefined behavior (on top of not being what you seek). ^2是一个 XOR 操作,您的目标是切换第二位,但在您的情况下,您在一个浮点上使用它,这违反了严格的别名规则并导致未定义的行为(除了不是您所寻求的之外)。

See the code below with some comments:请参阅下面的代码和一些注释:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

struct ComplexCartesian
{
    float real;
    float img;
};

struct ComplexPolar
{
    float magnitude;
    float angle;
};

struct ComplexPolar polar_from_cartesian_sum(struct ComplexCartesian c1, struct ComplexCartesian c2)
{
    struct ComplexPolar complexPolar; // here you declare the variable of your ComplexPolar struct

    c1.real += c2.real; // you don't need to have a result var, you can just reuse c1.
    c1.img += c2.img;
    complexPolar.magnitude = sqrt(c1.real * c1.real + c1.img * c1.img);
    complexPolar.angle = atan2(c1.img, c1.real);

    return complexPolar; // you return the value;
}


int main(void) {
    struct ComplexCartesian c1 = {0.12f, 0.15f};
    struct ComplexCartesian c2 = {0.42f, 1.15f};
    struct ComplexPolar complexPolar = polar_from_cartesian_sum(c1, c2);

    printf("%f %f\n", complexPolar.magnitude, complexPolar.angle);

    return 0;
}

Compile with gcc complex.c -lm &&./a.out使用 gcc 复合体编译gcc complex.c -lm &&./a.out

Output: Output:

1.407693 1.177098

NB: Perhaps you should explicitly tell that your angle is expressed in radians, and also rename your function as polar_from_cartesian_sum注意:也许您应该明确说明您的角度以弧度表示,并将您的 function 重命名为polar_from_cartesian_sum

Radius = 1.41半径 = 1.41
θ = 67.44o = 1.18 radians θ = 67.44o = 1.18 弧度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM