复数数据类型算法分析

复数的引入的一个动力之一:得到方程的解。
复平面:。
共轭复数对:。
C语言定义复数类型:
 typedef struct {
  long double rmz;
  long double imz;
 }complex;
 void complex_add(complex z1, complex z2, complex *z);//加法z=z1+z2
 void complex_dec(complex z1, complex z2, complex *z);//减法z=z1-z2
 void complex_product(complex z1, complex z2, complex z);//乘法z=z1*z2
 void complex_divide(complex z1, complex z2, complex *z);//除法z=z1/z2
 void complex_power(complex z1, complex *z, int n);//乘幂z=z1^n
 void complex_root(complex z1, complex *z, int n );//n次方根z=z1_n
 void complex_exp(complex z1, complex *z)//求指数函数
 void complex_log(complex z1, complex *z)//求对数函数z=ln(z1)
 void complex_sin(complex z1, complex *z)//求正弦函数z=sin(z1)
 void complex_sin(complex z1, complex *z)//求余弦函数z=cos(z1)


它的算法的C语言实现代码:
=======================Complex.h=============================
#include<math.h> 
#define PI 3.1415926
typedef struct {
 double rmz;
 double imz;
}complex;

 void complex_add(complex z1, complex z2, complex *z);//加法z=z1+z2
 void complex_dec(complex z1, complex z2, complex *z);//减法z=z1-z2
 void complex_product(complex z1, complex z2, complex *z);//乘法z=z1*z2
 void complex_divide(complex z1, complex z2, complex *z);//除法z=z1/z2
 void complex_power(complex z1, complex *z, int n);//乘幂z=z1^n
 void complex_root(complex z1, complex *z, int n );//n次方根z=z1_n
 void complex_exp(complex z1, complex *z);
 void complex_log(complex z1, complex *z);//求对数函数z=ln(z1)
 void complex_sin(complex z1, complex *z);//求正弦函数z=sin(z1)
 void complex_cos(complex z1, complex *z);//求余弦函数z=cos(z1)


void complex_add(complex z1, complex z2, complex *z){
 z->rmz = z1.rmz + z2.rmz;
 z->imz = z1.imz + z2.imz;
}
void complex_dec(complex z1, complex z2, complex *z){
 z->rmz = z1.rmz - z2.rmz;
 z->imz = z1.imz - z2.imz;
}

void complex_product(complex z1, complex z2, complex *z){
 z->rmz = z1.rmz * z2.rmz - z1.imz * z2.imz;
 z->imz = z1.rmz * z2.imz + z2.rmz * z1.imz;
}

void complex_divide(complex z1, complex z2, complex *z){
 z->rmz = (z1.rmz*z2.rmz + z1.imz*z2.imz)/(z2.rmz*z2.rmz + z2.imz*z2.imz);
 z->imz = (z2.rmz*z1.imz - z1.rmz*z2.imz)/(z2.rmz*z2.rmz + z2.imz*z2.imz);
}

void complex_power(complex z1, complex *z, int n){
 z->rmz = pow(sqrt(z1.rmz*z1.rmz+z1.imz*z1.imz), n)*cos(n*atan2(z1.imz, z1.rmz));
 z->imz = pow(sqrt(z1.rmz*z1.rmz+z1.imz*z1.imz), n)*sin(n*atan2(z1.imz, z1.rmz));
}

void complex_root(complex z1, complex *z, int n){
 int k;
 for(k = 0; k < n; k++){
  z[k].rmz = pow(sqrt(z1.rmz*z1.rmz+z1.imz*z1.imz), 1.0/n)*cos((atan2(z1.imz, z1.rmz)+2*k*PI)/n);
  z[k].imz = pow(sqrt(z1.rmz*z1.rmz+z1.imz*z1.imz), 1.0/n)*sin((atan2(z1.imz, z1.rmz)+2*k*PI)/n);
 }
}

void complex_exp(complex z1, complex *z){
 z->rmz = exp(z1.rmz)*cos(z1.imz);
 z->imz = exp(z1.rmz)*sin(z1.imz);
}

void complex_log(complex z1, complex *z){
 z->rmz = log(sqrt(z1.rmz*z1.rmz+z1.imz*z1.imz));
 z->imz = atan2(z1.imz, z1.rmz);
}

void complex_sin(complex z1, complex *z){
 z->rmz = sin(z1.rmz)*((exp(z1.imz)+exp(-z1.imz))/2);
 z->imz = cos(z1.rmz)*((exp(z1.imz)-exp(-z1.imz))/2);
}

void complex_cos(complex z1, complex *z){
 z->rmz = cos(z1.rmz)*((exp(z1.imz)+exp(-z1.imz))/2);
 z->imz = sin(z1.rmz)*((exp(z1.imz)-exp(-z1.imz))/2);
}

 

=====================main()===============================
#include<malloc.h>
#include<stdio.h>
#include"Complex.h"
int main(){
 complex z1, z2, *z, zp[100];
 z1.rmz = 5.5; z1.imz = 3.7;
 z2.rmz = 1.5; z2.imz = 3.9;
 z = malloc(sizeof(complex));
 complex_add(z1, z2, z);
 printf("和:z = %f + %fi/n", z->rmz, z->imz);
 complex_dec(z1, z2, z);
 printf("差:z = %f + %fi/n", z->rmz, z->imz);
 complex_product(z1, z2, z);
 printf("积:z = %f + %fi/n", z->rmz, z->imz);
 complex_divide(z1, z2, z);
 printf("商:z = %f + %fi/n", z->rmz, z->imz);
 complex_power(z1, z, 5);
 printf("z1的5次幂:z = %f + %fi/n", z->rmz, z->imz);
 complex_root(z1, zp, 5);
 printf("z1的5次方根(5个):/n");
 int i;
 for(i = 0; i < 5; i++){
  printf("zp[%d] = %f + %fi/n", i, zp[i].rmz, zp[i].imz);
 }
 complex_exp(z1, z);
 printf("指数:z = %f + %fi/n", z->rmz, z->imz);
 complex_log(z1, z);
 printf("对数:z = %f + %fi/n", z->rmz, z->imz);
 complex_sin(z1, z);
 printf("正弦:z = %f + %fi/n", z->rmz, z->imz);
 complex_cos(z1, z);
 printf("余弦:z = %f + %fi/n", z->rmz, z->imz);
}


==========================================================
编译运行:
explore@ubuntu:~/Desktop$ gcc test.c -lm
explore@ubuntu:~/Desktop$ ./a.out
和:z = 7.000000 + 7.600000i
差:z = 4.000000 + -0.200000i
积:z = -6.180000 + 27.000000i
商:z = 1.298969 + -0.910653i
z1的5次幂:z = -12589.951000 + 2299.563320i
z1的5次方根(5个):
zp[0] = 1.449549 + 0.172488i
zp[1] = 0.283889 + 1.431905i
zp[2] = -1.274095 + 0.712478i
zp[3] = -1.071324 + -0.991569i
zp[4] = 0.611981 + -1.325301i
指数:z = -207.523236 + -129.646629i
对数:z = 1.891413 + 0.592187i
正弦:z = -14.277324 + 14.323131i
余弦:z = 14.340651 + -14.259880i
explore@ubuntu:~/Desktop$

 

 

你可能感兴趣的:(复数数据类型算法分析)