⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 complex.c

📁 seismic software,very useful
💻 C
字号:
/* Copyright (c) Colorado School of Mines, 1990./* All rights reserved.                       *//*****************************************************************************Functions to manipulate complex numbers:cadd	add two complex numberscsub	subtract two complex numberscmul	multiply two complex numberscdiv	divide two complex numberscmplx	make a complex number from two real numbersconjg	complex conjugate of a complex number cneg	negate a complex numbercinv	invert a complex numbercsqrt	complex square root of a complex numbercexp	complex exponential of a complex numbercrmul	multiply a complex number by a real number fcabs	real magnitude of a complex number******************************************************************************Notes:Adapted from Press et al, 1988, Numerical Recipes in C (Appendix E).******************************************************************************Author:    	Dave Hale, Colorado School of Mines, 06/02/89Modified:	Dave Hale, Colorado School of Mines, 04/26/90		Added function cinv().*****************************************************************************/#include "cwp.h"complex cadd(complex a, complex b){	complex c;	c.r = a.r+b.r;	c.i = a.i+b.i;	return c;}complex csub(complex a, complex b){	complex c;	c.r = a.r-b.r;	c.i = a.i-b.i;	return c;}complex cmul(complex a, complex b){	complex c;	c.r = a.r*b.r-a.i*b.i;	c.i = a.i*b.r+a.r*b.i;	return c;}complex cdiv(complex a, complex b){	complex c;	float r,den;	if (fabs(b.r)>=fabs(b.i)) {		r = b.i/b.r;		den = b.r+r*b.i;		c.r = (a.r+r*a.i)/den;		c.i = (a.i-r*a.r)/den;	} else {		r = b.r/b.i;		den = b.i+r*b.r;		c.r = (a.r*r+a.i)/den;		c.i = (a.i*r-a.r)/den;	}	return c;}complex cmplx(float re, float im){	complex c;	c.r = re;	c.i = im;	return c;}complex conjg(complex z){	complex c;	c.r = z.r;	c.i = -z.i;	return c;}complex cneg(complex z){	complex c;	c.r = -z.r;	c.i = -z.i;	return c;}complex cinv(complex z){	complex c;	float s;	s = 1.0/(z.r*z.r+z.i*z.i);	c.r = z.r*s;	c.i = -z.i*s;	return c;}complex csqrt(complex z){	complex c;	float x,y,w,r;	if (z.r==0.0 && z.i==0.0) {		c.r = c.i = 0.0;		return c;	} else {		x = fabs(z.r);		y = fabs(z.i);		if (x>=y) {			r = y/x;			w = sqrt(x)*sqrt(0.5*(1.0+sqrt(1.0+r*r)));		} else {			r = x/y;			w = sqrt(y)*sqrt(0.5*(r+sqrt(1.0+r*r)));		}		if (z.r>=0.0) {			c.r = w;			c.i = z.i/(2.0*w);		} else {			c.i = (z.i>=0.0) ? w : -w;			c.r = z.i/(2.0*c.i);		}		return c;	}}complex cexp(complex z){	float a;	complex c;	a = exp(z.r);	c.r = a*cos(z.i);	c.i = a*sin(z.i);	return c;}complex crmul(complex a, float x){	complex c;	c.r = x*a.r;	c.i = x*a.i;	return c;}float fcabs(complex z){	float x,y,ans,temp;	x = fabs(z.r);	y = fabs(z.i);	if (x==0.0)		ans = y;	else if (y==0.0)		ans = x;	else if (x>y) {		temp = y/x;		ans = x*sqrt(1.0+temp*temp);	} else {		temp =x/y;		ans = y*sqrt(1.0+temp*temp);	}	return ans;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -