帮助我处理复数吗? [英] Help me in dealing with complex numbers ?

查看:95
本文介绍了帮助我处理复数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用"C"编写了一个与图论有关的程序.而且我已经使用了很多数组(2

尺寸).所有数组都包含浮点数.(意味着我所有的操作

已完成黑白浮动)现在,我必须修改程序以查看它也可以用于复杂的

为此,我想将每个数组都分成两个数组,一个是实数,另一个是

虚构的.

有没有简单的方法可以将复数作为输入并对其进行黑白运算?

用哪种方式会很简单??

用哪种语言会很简单??


我知道python,C语言.......

提前Thnx .....

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.....

推荐答案

您将每个数组拆分为实部和虚部的方法是绝对合法的.或者,您可以为复数创建自己的结构,例如:

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

/* addressing elements, example */
float real5_7 = myArray[5][7].r;
float imag5_7 = myArray[5][7].i;



在C ++中,事情变得容易一些,在C ++中,您可以定义一个成熟的Complex数据类型,包括所有标准运算符.

正如Kenneth所指出的那样,C#免费提供了内置数据类型Complex.



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.


C ++中的一类致力于处理复数,这是其中的一部分它的标准库的名称,在库群中定义.使用此功能将使您的工作更加轻松,因为除了I/O之外,该类还提供了一组函数来对这组数字执行算术运算.例如:

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>&);



如果您不知道如何使用模板,建议您阅读以下书籍:
97 Addison Wesley-C + +编程语言-Stroustrup(第三版)



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)


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

}


这篇关于帮助我处理复数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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