一些复数运算的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 ...
随机推荐
- Android _优雅实现元素间的分割线 (支持3.0以下)
转:http://blog.csdn.net/lmj623565791/article/details/42407923 1.概述 话说,随着Android SDK版本的升级,很多控件增加了新的属性方 ...
- 使用Python多线程犯的错误总结
在使用Python多线程的时候,在使用多线程编程的时候,由于对于变量作用域和多线程不是很熟悉,导致在使用多线程的时候,犯了低级的错误. 第一个错误: 在多线程中使用全局变量,导致多个线程修改全局变量. ...
- RenderPartial: No overload for method 'Write' takes 0 arguments
如下方法调用RenderPartial: 报“No overload for method 'Write' takes 0 arguments”的错误: @if (@Model != null &am ...
- Type Correlation
Types of correlation: Logical correlation: Using pre-defined and customized correlation rules. Inven ...
- 鼠标拖放div 实现
Javascript的mousemove事件类型是一个实时响应的事件,当鼠标指针的位置发生变化时(至少移动1个像素),就会触发mousemove事件.该事件响应的灵敏度主要参考鼠标指针移动速度的快慢, ...
- VB.NET让webbrowser控件中JS脚本错误最新方法(2013-09-16)
最近也是在项目中遇到了webbrowser控件中想关闭JS脚本错误窗口的问题,所以经过多次测试,终于用一段高效实用的代码完美解决webbrowser控件中JS脚本错误窗口关闭的问题. 通过创建一个子线 ...
- 第52周二Restful
今天去spring官网发现一个关键词:Restful,以前只在与一个系统对接时用到过这种形式的接口,但印象不深,百度搜索后才感觉自己太out了,这个概念2000年提出,2009年时国内就有很多人推荐使 ...
- 仍需"敬请期待"的微信沃卡
从2013年7月30日广东联通联合腾讯公布将合作推出联通沃卡,到8月5日在易迅网上进行预订,8月8日正式发售,再到本人最近几日拿到预订的实卡,已经过去20多天了.于是乎,我怀着无比期待的 ...
- aix Mysql-Rpm puppet puppetAgent
http://www.bullfreeware.com/toolbox.php (Large Open Source Software Archieve for AIX 提供MySQL5.1 fo ...
- Hibernate 、多表关联映射 - 多对多关系映射(many-to-many)
hibernate.cfg.xml: <hibernate-configuration> <session-factory name="sessionFactory&quo ...