首页 / C语言 / 一些复数运算的C语言实现
一些复数运算的C语言实现
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了一些复数运算的C语言实现,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5670字,纯文字阅读大概需要9分钟。
内容图文
![一些复数运算的C语言实现](/upload/InfoBanner/zyjiaocheng/1126/805ffb7e806541bca098a3f89defc97d.jpg)
很久不写博客了。第一次写博客是在04年,最近的一次还是在大学时,在学校时,甚至还有过自己去买虚拟主机搭WordPress写博客的经历。现在工作时间越长,越发现积累的重要性。那么就从这里开始吧,重新开始写博客。
最近打算写小算法,里面需要用到一些复数运算。贴一点复数运算的C语言实现代码。都是些很简单的东西。
包括以下运算:
复数加法、复数减法、复数乘法、复数除法、复数取模、复指数运算、复数取相角、模与相角合成复位。本人专业本职做硬件的,写程序没受过专业训练,勿吐槽。
1 /* file ComplexCalculation.h 2 *author Vincent Cui 3 *e-mail whcui1987@163.com 4 *version 0.1 5 *data 20-Oct-2014 6 *brief 用于复数运算的一些函数头和定义 7 */ 8 9 10 11 #ifndef _COMPLEXCALCULATION_H_ 12 #define _COMPLEXCALCULATION_H_ 1314#define ASSERT_ENABLE 1 1516#define IS_COMPLEX_DIVISOR_CORRENT(DIVISOR_REAL, DIVISOR_IMAG) ((DIVISOR_REAL != 0) || (DIVISOR_IMAG != 0)) 1718 typedef double mathDouble; 19 typedef unsigned char mathUint_8; 20 typedef unsigned shortint mathUint_16; 21 typedef unsigned int mathUint_32; 222324 typedef struct _ReDefcomplex 25{ 26 mathDouble Real; 27 mathDouble Imag; 28}complexType; 293031complexType complexAdd(complexType a, complexType b); 32complexType complexSubtract(complexType minuend, complexType subtrahend); 33complexType complexMultiply(complexType a, complexType b); 34complexType complexDivision(complexType dividend, complexType divisor); 35mathDouble complexAbs(complexType a); 36mathDouble complexAngle(complexType a); 37complexType complexByAbsAngle(mathDouble r, mathDouble theta); 38complexType complexExp(complexType a); 3940#if ASSERT_ENABLE 41#define assert_param(expr) ((expr) ? (void)0 : assert_failed((mathUint_8 *)__FILE__, __LINE__)) 42void assert_failed(mathUint_8* file, mathUint_32 line); 43#else44#define assert_param(expr) ((void)0) 45#endif46474849#endif
1 /* file ComplexCalculation.c 2 *author Vincent Cui 3 *e-mail whcui1987@163.com 4 *version 0.1 5 *data 20-Oct-2014 6 *brief 用于复数运算的一些函数 7 */ 8 9 10 #include "ComplexCalculation.h" 11 #include "math.h" 12 #include "stdio.h" 13 14 15/*函数名:complexAdd 16 *说明:复数加法 17 *输入:a,b两个复数 18 *输出: 19 *返回:a + b 20 *调用: 21 *其它: 22*/ 23complexType complexAdd(complexType a, complexType b) 24{ 25 complexType result; 26 27 result.Real = a.Real + b.Real; 28 result.Imag = a.Imag + b.Imag; 29 30return result; 31} 32 33/*函数名:complexSubtract 34 *说明:复数减法 35 *输入:minuend被减数,subtrahend减数 36 *输出: 37 *返回:a - b 38 *调用: 39 *其它: 40*/ 41complexType complexSubtract(complexType minuend, complexType subtrahend) 42{ 43 complexType result; 44 45 result.Real = minuend.Real - subtrahend.Real; 46 result.Imag = minuend.Imag - subtrahend.Imag; 47 48return result; 49} 50 51/*函数名:complexMultiply 52 *说明:复数乘法 53 *输入:a,b两个复数 54 *输出: 55 *返回:a * b 56 *调用: 57 *其它: 58*/ 59complexType complexMultiply(complexType a, complexType b) 60{ 61 complexType result; 62 63 result.Real = a.Real * b.Real - a.Imag * b.Imag; 64 result.Imag = a.Imag * b.Real + a.Real * b.Imag; 65 66return result; 67} 68 69 70/*函数名:complexDivision 71 *说明:复数除法 72 *输入:dividend被除数,divisor除数 73 *输出: 74 *返回:a / b 75 *调用: 76 *其它:divisor的实部和虚部不能同时为0 77*/ 78complexType complexDivision(complexType dividend, complexType divisor) 79{ 80 complexType result; 81 82/*断言,被除数的实部和虚部不能同时为零*/ 83 assert_param(IS_COMPLEX_DIVISOR_CORRENT(divisor.Real, divisor.Imag)); 84 85 result.Real = (mathDouble)(dividend.Real * divisor.Real + dividend.Imag * divisor.Imag) / 86 (divisor.Real * divisor.Real + divisor.Imag * divisor.Imag); 87 result.Imag = (mathDouble)(dividend.Imag * divisor.Real - dividend.Real * divisor.Imag) / 88 (divisor.Real * divisor.Real + divisor.Imag * divisor.Imag); 89return result; 90} 91 92/*函数名:complexAbs 93 *说明:复数取模 94 *输入:a复数 95 *输出: 96 *返回:复数的模 97 *调用: 98 *其它: 99*/100mathDouble complexAbs(complexType a) 101{ 102return (sqrt( pow(a.Real,2) + pow(a.Imag,2) )); 103} 104105106/*函数名:complexAngle 107 *说明:复数取相角 108 *输入:a复数 109 *输出: 110 *返回:复数的相角 111 *调用: 112 *其它: 113*/114mathDouble complexAngle(complexType a) 115{ 116/*是atan2而非atan,(-PI,PI] */117return (atan2(a.Imag, a.Real)); 118} 119120/*函数名:complexByAbsAngle 121 *说明:通过模和相角合成复数 122 *输入:r 模, theta 相角 123 *输出: 124 *返回:复数 125 *调用: 126 *其它: 127*/128complexType complexByAbsAngle(mathDouble r, mathDouble theta) 129{ 130 complexType tmp_1,tmp_2; 131132 tmp_1.Real = 0; 133 tmp_1.Imag = theta; 134 tmp_2 = complexExp(tmp_1); 135 tmp_2.Real *= r; 136 tmp_2.Imag *= r; 137138return tmp_2; 139} 140141/*函数名:complexExp 142 *说明:复指数运算 143 *输入:a 复指数 144 *输出: 145 *返回:e的a次方 146 *调用: 147 *其它:使用欧拉公式 e^(jw) = cos(w) + j * sin(w) 148*/149complexType complexExp(complexType a) 150{ 151 complexType result; 152153 result.Real = exp(a.Real) * cos(a.Imag); 154 result.Imag = exp(a.Real) * sin(a.Imag); 155156return result; 157} 158159160#if ASSERT_ENABLE 161/*函数名:assert_failed 162 *说明:断言函数 163 *输入: 164 *输出:打印出错的位置 165 *返回: 166 *调用: 167 *其它: 168*/169void assert_failed(mathUint_8* file, mathUint_32 line) 170{ 171 printf("Assert Error in File: %s \r\nLine: %d \r\n",file,line); 172} 173174#endif
1 #include "ComplexCalculation.h" 2 #include "stdio.h" 3 4int main(void) 5{ 6 complexType a,b,c; 7 a.Imag = 0.5; 8 a.Real = 2.5; 9 b.Real = 1; 10 b.Imag = -5; 1112 c = complexAdd(a,b); 13 printf("complexAdd: c.Real %f, c.Imag %f \r\n",c.Real,c.Imag); 14 c = complexSubtract(a,b); 15 printf("complexSubtract: c.Real %f, c.Imag %f \r\n",c.Real,c.Imag); 16 c = complexMultiply(a,b); 17 printf("complexMultiply: c.Real %f, c.Imag %f \r\n",c.Real,c.Imag); 18 c = complexDivision(a,b); 19 printf("complexDivision: c.Real %f, c.Imag %f \r\n",c.Real,c.Imag); 20 printf("Abs(c): %f\r\n",complexAbs(a)); 21 printf("Angle(c): %f\r\n",complexAngle(a)); 22 c = complexByAbsAngle(complexAbs(a),complexAngle(a)); 23 printf("complexByAbsAngle: a.Real %f, a.Imag %f \r\n",c.Real,c.Imag); 2425while(1); 26 }
下面是运行结果,在VS2012上运行的。
欢迎一起交流!
后面博客中我会写一些数字信号处理运算的C语言实现。
原文:http://www.cnblogs.com/mildsim/p/4052072.html
内容总结
以上是互联网集市为您收集整理的一些复数运算的C语言实现全部内容,希望文章能够帮你解决一些复数运算的C语言实现所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。