一些复数运算的C语言实现
很久不写博客了。第一次写博客是在04年,最近的一次还是在大学时,在学校时,甚至还有过自己去买虚拟主机搭WordPress写博客的经历。现在工作时间越长,越发现积累的重要性。那么就从这里开始吧,重新开始写博客。
最近打算写小算法,里面需要用到一些复数运算。贴一点复数运算的C语言实现代码。都是些很简单的东西。
包括以下运算:
复数加法、复数减法、复数乘法、复数除法、复数取模、复指数运算、复数取相角、模与相角合成复位。本人专业本职做硬件的,写程序没受过专业训练,勿吐槽。
/*file ComplexCalculation.h
*author Vincent Cui
*e-mail whcui1987@163.com
*version 0.1
*data 20-Oct-2014
*brief 用于复数运算的一些函数头和定义
*/ #ifndef _COMPLEXCALCULATION_H_
#define _COMPLEXCALCULATION_H_ #define ASSERT_ENABLE 1 #define IS_COMPLEX_DIVISOR_CORRENT(DIVISOR_REAL, DIVISOR_IMAG) ((DIVISOR_REAL != 0) || (DIVISOR_IMAG != 0)) typedef double mathDouble;
typedef unsigned char mathUint_8;
typedef unsigned short int mathUint_16;
typedef unsigned int mathUint_32; typedef struct _ReDefcomplex
{
mathDouble Real;
mathDouble Imag;
}complexType; complexType complexAdd(complexType a, complexType b);
complexType complexSubtract(complexType minuend, complexType subtrahend);
complexType complexMultiply(complexType a, complexType b);
complexType complexDivision(complexType dividend, complexType divisor);
mathDouble complexAbs(complexType a);
mathDouble complexAngle(complexType a);
complexType complexByAbsAngle(mathDouble r, mathDouble theta);
complexType complexExp(complexType a); #if ASSERT_ENABLE
#define assert_param(expr) ((expr) ? (void)0 : assert_failed((mathUint_8 *)__FILE__, __LINE__))
void assert_failed(mathUint_8* file, mathUint_32 line);
#else
#define assert_param(expr) ((void)0)
#endif #endif
ComplexCalculation.h
/*file ComplexCalculation.c
*author Vincent Cui
*e-mail whcui1987@163.com
*version 0.1
*data 20-Oct-2014
*brief 用于复数运算的一些函数
*/ #include "ComplexCalculation.h"
#include "math.h"
#include "stdio.h" /*函数名:complexAdd
*说明:复数加法
*输入:a,b两个复数
*输出:
*返回:a + b
*调用:
*其它:
*/
complexType complexAdd(complexType a, complexType b)
{
complexType result; result.Real = a.Real + b.Real;
result.Imag = a.Imag + b.Imag; return result;
} /*函数名:complexSubtract
*说明:复数减法
*输入:minuend被减数,subtrahend减数
*输出:
*返回:a - b
*调用:
*其它:
*/
complexType complexSubtract(complexType minuend, complexType subtrahend)
{
complexType result; result.Real = minuend.Real - subtrahend.Real;
result.Imag = minuend.Imag - subtrahend.Imag; return result;
} /*函数名:complexMultiply
*说明:复数乘法
*输入:a,b两个复数
*输出:
*返回:a * b
*调用:
*其它:
*/
complexType complexMultiply(complexType a, complexType b)
{
complexType result; result.Real = a.Real * b.Real - a.Imag * b.Imag;
result.Imag = a.Imag * b.Real + a.Real * b.Imag; return result;
} /*函数名:complexDivision
*说明:复数除法
*输入:dividend被除数,divisor除数
*输出:
*返回:a / b
*调用:
*其它:divisor的实部和虚部不能同时为0
*/
complexType complexDivision(complexType dividend, complexType divisor)
{
complexType result; /*断言,被除数的实部和虚部不能同时为零*/
assert_param(IS_COMPLEX_DIVISOR_CORRENT(divisor.Real, divisor.Imag)); result.Real = (mathDouble)(dividend.Real * divisor.Real + dividend.Imag * divisor.Imag) / \
(divisor.Real * divisor.Real + divisor.Imag * divisor.Imag);
result.Imag = (mathDouble)(dividend.Imag * divisor.Real - dividend.Real * divisor.Imag) / \
(divisor.Real * divisor.Real + divisor.Imag * divisor.Imag);
return result;
} /*函数名:complexAbs
*说明:复数取模
*输入:a复数
*输出:
*返回:复数的模
*调用:
*其它:
*/
mathDouble complexAbs(complexType a)
{
return (sqrt( pow(a.Real,) + pow(a.Imag,) ));
} /*函数名:complexAngle
*说明:复数取相角
*输入:a复数
*输出:
*返回:复数的相角
*调用:
*其它:
*/
mathDouble complexAngle(complexType a)
{
/*是atan2而非atan,(-PI,PI] */
return (atan2(a.Imag, a.Real));
} /*函数名:complexByAbsAngle
*说明:通过模和相角合成复数
*输入:r 模, theta 相角
*输出:
*返回:复数
*调用:
*其它:
*/
complexType complexByAbsAngle(mathDouble r, mathDouble theta)
{
complexType tmp_1,tmp_2; tmp_1.Real = ;
tmp_1.Imag = theta;
tmp_2 = complexExp(tmp_1);
tmp_2.Real *= r;
tmp_2.Imag *= r; return tmp_2;
} /*函数名:complexExp
*说明:复指数运算
*输入:a 复指数
*输出:
*返回:e的a次方
*调用:
*其它:使用欧拉公式 e^(jw) = cos(w) + j * sin(w)
*/
complexType complexExp(complexType a)
{
complexType result; result.Real = exp(a.Real) * cos(a.Imag);
result.Imag = exp(a.Real) * sin(a.Imag); return result;
} #if ASSERT_ENABLE
/*函数名:assert_failed
*说明:断言函数
*输入:
*输出:打印出错的位置
*返回:
*调用:
*其它:
*/
void assert_failed(mathUint_8* file, mathUint_32 line)
{
printf("Assert Error in File: %s \r\nLine: %d \r\n",file,line);
} #endif
ComplexCalculation.c
#include "ComplexCalculation.h"
#include "stdio.h" int main(void)
{
complexType a,b,c;
a.Imag = 0.5;
a.Real = 2.5;
b.Real = ;
b.Imag = -; c = complexAdd(a,b);
printf("complexAdd: c.Real %f, c.Imag %f \r\n",c.Real,c.Imag);
c = complexSubtract(a,b);
printf("complexSubtract: c.Real %f, c.Imag %f \r\n",c.Real,c.Imag);
c = complexMultiply(a,b);
printf("complexMultiply: c.Real %f, c.Imag %f \r\n",c.Real,c.Imag);
c = complexDivision(a,b);
printf("complexDivision: c.Real %f, c.Imag %f \r\n",c.Real,c.Imag);
printf("Abs(c): %f\r\n",complexAbs(a));
printf("Angle(c): %f\r\n",complexAngle(a));
c = complexByAbsAngle(complexAbs(a),complexAngle(a));
printf("complexByAbsAngle: a.Real %f, a.Imag %f \r\n",c.Real,c.Imag); while();
}
main.c
下面是运行结果,在VS2012上运行的。

欢迎一起交流!
后面博客中我会写一些数字信号处理运算的C语言实现。
一些复数运算的C语言实现的更多相关文章
- C语言中复数运算及调用blas,lapack中复数函数进行科学计算
C语言中常用的数据类型主要int, float ,double ,char 等,但在科学运算中复数扮演着重要角色.这里讲下C语言中的复数运算以及如何调用blas,lapack库中的复数函数来进行科学计 ...
- c++复习一:复数运算的简单实现。
复数运算的简单实现. 程序很简单了.基本忘光了复数,重新了解了基本概念.如何在平面表示一个复数,复数的长度|x|=开根 a^2+b^2.和四则运算. 程序基本点: 封装和抽象: 1)封装成员数据,私有 ...
- 大整数加减运算的C语言实现
目录 大整数加减运算的C语言实现 一. 问题提出 二. 代码实现 三. 效果验证 大整数加减运算的C语言实现 标签: 大整数加减 C 一. 问题提出 培训老师给出一个题目:用C语言实现一个大整数计算器 ...
- 算法笔记_047:复数运算(Java)
目录 1 问题描述 2 解决方案 1 问题描述 编程实现两个复数的运算.设有两个复数 和 ,则他们的运算公式为: 要求:(1)定义一个结构体类型来描述复数. (2)复数之间的加法.减法.乘法和除法 ...
- Java编写能完成复数运算的程序
Java编写能完成复数运算的程序 题目简介: 整体分析: 界面分析: 实验代码: package complex; import java.awt.EventQueue; import javax.s ...
- Java练习 SDUT-4303_简单的复数运算(类和对象)
简单的复数运算(类和对象) Time Limit: 2000 ms Memory Limit: 65536 KiB Problem Description 设计一个类Complex,用于封装对复数的下 ...
- 2017.11.21 基于JSP+Servlet+JavaBean实现复数运算(二)
代码的实现 最基本的MVC模式 //input.jsp 输入界面 <%@ page language="java" import="java.util.*" ...
- Java实现复数运算
1 问题描述 编程实现两个复数的运算.设有两个复数 和 ,则他们的运算公式为: 要求:(1)定义一个结构体类型来描述复数. (2)复数之间的加法.减法.乘法和除法分别用不用的函数来实现. (3)必须使 ...
- C++复数运算 重载
近期整理下很久前写的程序,这里就把它放在博文中了,有些比较简单,但是很有学习价值. 下面就是自己很久前实现的复数重载代码,这里没有考虑特殊情况,像除法中,分母不为零情况. #include <i ...
随机推荐
- u Calculate e
问题陈述: 杭州电子科技大学 HANGZHOU DIANZI UNIVERSITY Online Judge Problem - 1012 问题解析: 简单题,注意输出格式.引入<iomaini ...
- 重新关联bat文件的打开方式为系统默认方式
为什么“BAT”扩展名的默认打开方式:显示出来的居然是“%1”这么一个怪异的东东,具体在什么位置的? c:\windowssystem32\command.com修复bat关联,打开command.c ...
- 同步Flex Chart的数据提示
原文 http://www.riafan.com/sync-datatips-for-flex-chart/ 图表数据提示的同步不仅包含单个图表内多个系列的数据提示的同步,也包含多个图表的数据提示的同 ...
- xrdp的rdp前端无法连接Windows 2008的问题
xrdp使用rdp前端,无法连接2008,但连接2003是可以的.连接2008的时候,会在客户端发送Client Info PDU后主动RST掉连接.如下图 开始以为是客户端发送Client Info ...
- 每天一道题:LeetCode
本人是研二程旭猿一枚,还有半年多就要找工作了,想想上一年度面试阿里的算法工程师挂了,心有不甘啊,主要还是准备不足,对一些常见的算法问题没有去组织准备,为了明年找一份好的实习,就从现在开始,好好准备吧, ...
- ESMOD北京高级时装艺术学校_百度百科
ESMOD北京高级时装艺术学校_百度百科 ESMOD北京高级时装艺术学校
- linux中ctrl+z、ctrl+d和ctrl+c的区别
ctrl+c和ctrl+z都是中断命令,但是他们的作用却不一样.ctrl+c是强制中断程序的执行,而ctrl+z的是将任务中断,但是此任务并没有结束,他仍然在进程中他只是维持挂起的状态,用户可以使用f ...
- BFS+状态压缩 HDU1429
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- JavaScript 的DOM操作
HTML DOM (文档对象模型) 当网页被加载时,浏览器会创建页面的文档对象模型(Document Object Model). HTML DOM 模型被构造为对象的树. Windows 对象操作 ...
- pagination分页插件
最近做了个项目,有用到分页, 这类插件应该是很常用的, 虽然网上很多现成的分页插件, 但是还是想着自己写一个, 给自己积累点东西, 顺便练练手, 写了差不多3个小时左右, 代码如下: 代码: < ...