• 一些复数运算的C语言实现


      很久不写博客了。第一次写博客是在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_
    13 
    14 #define ASSERT_ENABLE 1
    15 
    16 #define IS_COMPLEX_DIVISOR_CORRENT(DIVISOR_REAL, DIVISOR_IMAG)        ((DIVISOR_REAL != 0) || (DIVISOR_IMAG != 0)) 
    17 
    18 typedef double                    mathDouble;
    19 typedef unsigned char            mathUint_8;
    20 typedef unsigned short int        mathUint_16;
    21 typedef unsigned int            mathUint_32;
    22 
    23 
    24 typedef struct _ReDefcomplex
    25 {
    26     mathDouble    Real;
    27     mathDouble    Imag;
    28 }complexType;
    29 
    30 
    31 complexType    complexAdd(complexType a, complexType b);
    32 complexType    complexSubtract(complexType minuend, complexType subtrahend);
    33 complexType complexMultiply(complexType a, complexType b);
    34 complexType complexDivision(complexType dividend, complexType divisor);
    35 mathDouble    complexAbs(complexType a);
    36 mathDouble    complexAngle(complexType a);
    37 complexType complexByAbsAngle(mathDouble r, mathDouble theta);
    38 complexType complexExp(complexType a);
    39 
    40 #if ASSERT_ENABLE
    41   #define assert_param(expr) ((expr) ? (void)0 : assert_failed((mathUint_8 *)__FILE__, __LINE__))
    42   void assert_failed(mathUint_8* file, mathUint_32 line);
    43 #else
    44   #define assert_param(expr) ((void)0)
    45 #endif
    46 
    47 
    48 
    49 #endif
    ComplexCalculation.h
      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  */
     23 complexType    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 
     30     return result;
     31 }
     32 
     33 /*函数名:complexSubtract
     34  *说明:复数减法
     35  *输入:minuend被减数,subtrahend减数
     36  *输出:
     37  *返回:a - b 
     38  *调用:
     39  *其它:
     40  */
     41 complexType    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 
     48     return result;
     49 }
     50 
     51 /*函数名:complexMultiply
     52  *说明:复数乘法
     53  *输入:a,b两个复数
     54  *输出:
     55  *返回:a * b 
     56  *调用:
     57  *其它:
     58  */
     59 complexType 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 
     66     return result;
     67 }
     68 
     69 
     70 /*函数名:complexDivision
     71  *说明:复数除法
     72  *输入:dividend被除数,divisor除数
     73  *输出:
     74  *返回:a / b 
     75  *调用:
     76  *其它:divisor的实部和虚部不能同时为0
     77  */
     78 complexType 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);
     89     return result;
     90 }
     91 
     92 /*函数名:complexAbs
     93  *说明:复数取模
     94  *输入:a复数
     95  *输出:
     96  *返回:复数的模
     97  *调用:
     98  *其它:
     99  */
    100 mathDouble    complexAbs(complexType a)
    101 {
    102     return (sqrt( pow(a.Real,2) + pow(a.Imag,2) ));
    103 }
    104 
    105 
    106 /*函数名:complexAngle
    107  *说明:复数取相角
    108  *输入:a复数
    109  *输出:
    110  *返回:复数的相角
    111  *调用:
    112  *其它:
    113  */
    114 mathDouble    complexAngle(complexType a)
    115 {
    116     /*是atan2而非atan,(-PI,PI] */
    117     return (atan2(a.Imag, a.Real));
    118 }
    119 
    120 /*函数名:complexByAbsAngle
    121  *说明:通过模和相角合成复数
    122  *输入:r 模, theta 相角
    123  *输出:
    124  *返回:复数
    125  *调用:
    126  *其它:
    127  */
    128 complexType complexByAbsAngle(mathDouble r, mathDouble theta)
    129 {
    130     complexType tmp_1,tmp_2;
    131 
    132     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;
    137 
    138     return tmp_2;
    139 }
    140 
    141 /*函数名:complexExp
    142  *说明:复指数运算
    143  *输入:a 复指数
    144  *输出:
    145  *返回:e的a次方
    146  *调用:
    147  *其它:使用欧拉公式 e^(jw) = cos(w) + j * sin(w)
    148  */
    149 complexType complexExp(complexType a)
    150 {
    151     complexType result;
    152 
    153     result.Real = exp(a.Real) * cos(a.Imag);
    154     result.Imag = exp(a.Real) * sin(a.Imag);
    155 
    156     return result;
    157 }
    158 
    159 
    160 #if ASSERT_ENABLE
    161 /*函数名:assert_failed
    162  *说明:断言函数
    163  *输入:
    164  *输出:打印出错的位置
    165  *返回:
    166  *调用:
    167  *其它:
    168  */
    169 void assert_failed(mathUint_8* file, mathUint_32 line)
    170 {
    171     printf("Assert Error in File: %s 
    Line: %d 
    ",file,line);
    172 }
    173 
    174 #endif
    ComplexCalculation.c
     1 #include "ComplexCalculation.h"
     2 #include "stdio.h"
     3 
     4 int 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;
    11 
    12     c = complexAdd(a,b);
    13     printf("complexAdd: c.Real %f, c.Imag %f 
    ",c.Real,c.Imag);
    14     c = complexSubtract(a,b);
    15     printf("complexSubtract: c.Real %f, c.Imag %f 
    ",c.Real,c.Imag);
    16     c = complexMultiply(a,b);
    17     printf("complexMultiply: c.Real %f, c.Imag %f 
    ",c.Real,c.Imag);
    18     c = complexDivision(a,b);
    19     printf("complexDivision: c.Real %f, c.Imag %f 
    ",c.Real,c.Imag);
    20     printf("Abs(c): %f
    ",complexAbs(a));
    21     printf("Angle(c): %f
    ",complexAngle(a));
    22     c = complexByAbsAngle(complexAbs(a),complexAngle(a));
    23     printf("complexByAbsAngle: a.Real %f, a.Imag %f 
    ",c.Real,c.Imag);
    24 
    25     while(1);
    26 }
    main.c

    下面是运行结果,在VS2012上运行的。

    欢迎一起交流!

    后面博客中我会写一些数字信号处理运算的C语言实现。

  • 相关阅读:
    Web前端可以转行做游戏吗?
    SublimeText3常用快捷键和优秀插件(亲测)
    jQuery编程规范与最佳实践(附带一些个人的笔记)
    outline详解
    几种创建XMLHttpRequest对象的方法
    8条规则图解JavaScript原型链继承原理
    技术跟产品杂谈
    谈一谈URL
    React + Reflux 渲染性能优化原理
    Interesting JavaScript
  • 原文地址:https://www.cnblogs.com/mildsim/p/4052072.html
Copyright © 2020-2023  润新知