xComplex.h

 

/*******************************************************************/
/*                                                                 */
/*     filename : xComplex.h                                       */
/*     author   : edison.shih/edisonx                              */
/*     compiler : Visual C++ 2008                                  */
/*     date     : 2011.03.07                                       */
/*                                                                 */
/*         A.L.L.     R.I.G.H.T.S.     R.E.S.E.R.V.E.              */
/*                                                                 */
/*******************************************************************/

#ifndef X_COMPLEX_H_
#define X_COMPLEX_H_

typedef struct tagxcompolex{
     double real;
     double img;
}xComplex;

// ---------------------------------
//
加法運算
int comp_add(xComplex *rst,
                const xComplex z1,
                const xComplex z2);
// ---------------------------------
// rst+=z
int comp_adds(xComplex *rst,
                const xComplex z);
// ---------------------------------
//
減法運算
int comp_sub(xComplex *rst,
                const xComplex z1,
                const xComplex z2);
// ---------------------------------
// rst-=z
int comp_subs(xComplex *rst,
                const xComplex z);
// ---------------------------------
//
乘法運算
int comp_mul(xComplex *rst,
                const xComplex z1,
                const xComplex z2);
// ---------------------------------
// rst*=z
int comp_muls(xComplex *rst,
                const xComplex z);
// ---------------------------------
//
除法運算
int comp_div(xComplex *rst,
                const xComplex z1,
                const xComplex z2,
                const double eps);
// ---------------------------------
// rst/=z
int comp_divs(xComplex *rst,
                const xComplex z,
                const double eps);
// ---------------------------------
//
倒數運算
int comp_inv(xComplex *rst,
                const xComplex z,
                const double eps);
// ---------------------------------
// rst = 1/ rst
int comp_invs(xComplex *rst,
                const double eps);
// ---------------------------------
//
負數運算
int comp_neg(xComplex *rst,
                const xComplex z);
// ---------------------------------
// rst = -rst
int comp_negs(xComplex *rst);
// ---------------------------------
//
共軛運算
int comp_conj(xComplex *rst,
                const xComplex z);
// ---------------------------------
//
複數顯示
void comp_dis(const xComplex z);
// ---------------------------------
//
複數比較
int comp_cmp(const xComplex z1,
                const xComplex z2,
                const double eps);
// ---------------------------------
//
模數
double comp_norm(const xComplex z);
// ---------------------------------
//
角度
double comp_sita(const xComplex z);
// ---------------------------------
// n
次根號
int comp_nroot(xComplex* rst,
                   const xComplex z,
                   const int n,
                   const double eps);
// ---------------------------------
// exp(z)
int comp_exp(xComplex *rst,
                const xComplex z);
// ---------------------------------
//
整數次方, pow(z, n)
int comp_pown(xComplex *rst,
                const xComplex z,
                const int n);
// ---------------------------------
// log(z)
int comp_log(xComplex *rst,
                const xComplex z);
// ---------------------------------
//
複數次方, z=pow(z1, z2)
int comp_pow(xComplex *rst,
                const xComplex z1,
                const xComplex z2);
// ---------------------------------
// sin(z)
int comp_sin(xComplex *rst,
                const xComplex z);
// ---------------------------------
// cos(z)
int comp_cos(xComplex *rst,
                const xComplex z);
// ---------------------------------
// sinh(z)
int comp_sinh(xComplex *rst,
                const xComplex z);
// ---------------------------------
// cosh(z)
int comp_cosh(xComplex *rst,
                const xComplex z);

#endif

 

xComplex.c

 

/*******************************************************************/
/*                                                                 */
/*     filename : xComplex.c                                       */
/*     author   : edison.shih/edisonx                              */
/*     compiler : Visual C++ 2008                                  */
/*     date     : 2011.03.07                                       */
/*                                                                 */
/*         A.L.L.     R.I.G.H.T.S.     R.E.S.E.R.V.E.              */
/*                                                                 */
/*******************************************************************/
#include <stdio.h>
#include <math.h>
#include "xComplex.h"

#define PI 3.1415926535897932
// ---------------------------------
//
加法運算
int comp_add(xComplex *rst,
                const xComplex z1,
                const xComplex z2)
{
     if(rst==NULL) return 0;
     rst->real = z1.real + z2.real;
     rst->img = z1.img + z2.img;
     return 1;
}
// ---------------------------------
// rst+=z
int comp_adds(xComplex *rst,
                const xComplex z)
{
     if(rst==NULL) return 0;
     rst->real+=z.real;
     rst->img +=z.img;
     return 1;
}
// ---------------------------------
//
減法運算
int comp_sub(xComplex *rst,
                const xComplex z1,
                const xComplex z2)
{
     if(rst==NULL) return 0;
     rst->real = z1.real - z2.real;
     rst->img = z1.img - z2.img;
     return 1;
}
// ---------------------------------
// rst-=z
int comp_subs(xComplex *rst,
                const xComplex z)
{
     if(rst==NULL) return 0;
     rst->real-=z.real;
     rst->img -=z.img;
     return 1;
}
// ---------------------------------
//
乘法運算
int comp_mul(xComplex *rst,
                const xComplex z1,
                const xComplex z2)
{
     if(rst==NULL) return 0;
     rst->real = z1.real*z2.real - z1.img * z2.img;
     rst->img = z1.img*z2.real + z1.real * z2.img;
     return 1;
}
// ---------------------------------
// rst*=z
int comp_muls(xComplex *rst,
                const xComplex z)
{
     double r, i;
     if(rst==NULL) return 0;
     r = rst->real, i=rst->img;
     rst->real = r*z.real - i*z.img;
     rst->img = i*z.real + r*z.img;
     return 1;
}
// ---------------------------------
//
除法運算
int comp_div(xComplex *rst,
                const xComplex z1,
                const xComplex z2,
                const double eps)
{
     const double R = z2.real*z2.real + z2.img * z2.img;
     if(rst==NULL || R <= eps) return 0;
     rst->real = (z1.real*z2.real - z1.img * z2.img)/R;
     rst->img = (z1.img*z2.real + z1.real * z2.img)/R;
     return 1;
}
// ---------------------------------
// rst/=z
int comp_divs(xComplex *rst,
                const xComplex z,
                const double eps)
{
     double r, i;
     double R = z.real*z.real + z.img*z.img;
     if(rst==NULL|| R <= eps) return 0;
     r = rst->real, i=rst->img;
     rst->real = (r*z.real - i*z.img)/R;
     rst->img = (i*z.real + r*z.img)/R;
     return 1;
}
// ---------------------------------
//
倒數運算
int comp_inv(xComplex *rst,
                const xComplex z,
                const double eps)
{
     const double R = z.real * z.real + z.img * z.img;
     if(rst==NULL || R <= eps) return 0;
     rst->real = z.real / R;
     rst->img = -z.img / R;
     return 1;
}
// ---------------------------------
// rst = 1/ rst
int comp_invs(xComplex *rst,
                const double eps)
{
     double R;
     if(rst==NULL) return 0;
     R = rst->real*rst->real + rst->img * rst->img;
     if(R <= eps) return 0;
     rst->real/=R, rst->img/=-R;
     return 1;
}
// ---------------------------------
//
負數運算
int comp_neg(xComplex *rst,
                const xComplex z)
{
     if(rst==NULL) return 0;
     rst->real = -z.real;
     rst->img = -z.img;
     return 1;
}
// ---------------------------------
// rst = -rst
int comp_negs(xComplex *rst)
{
     if(rst==NULL) return 0;
     rst->real = -rst->real;
     rst->img = -rst->img;
     return 1;
}
// ---------------------------------
//
共軛運算
int comp_conj(xComplex *rst,
                const xComplex z)
{
     if(rst==NULL) return 0;
     rst->real = z.real;
     rst->img =-z.img;
     return 1;
}
// ---------------------------------
//
複數顯示
void comp_dis(const xComplex z)
{
     printf("(% 7.2lf,% 7.2lf i) ", z.real , z.img);
}
// ---------------------------------
//
複數比較
int comp_cmp(const xComplex z1,
                const xComplex z2,
                const double eps)
{
     const double r1 = z1.real * z1.real + z1.img * z1.img;
     const double r2 = z2.real * z2.real + z2.img * z2.img;
     const double delta = r1-r2;
     if( fabs(delta) <= eps) return 0;
     else if(delta > 0.0) return 1;
     else return -1;
}
// ---------------------------------
//
模數
double comp_norm(const xComplex z)
{
     return sqrt(z.real * z.real + z.img * z.img);
}
// ---------------------------------
//
角度
double comp_sita(const xComplex z)
{
     return atan2(z.img, z.real);
}
// ---------------------------------
// n
次根號
int comp_nroot(xComplex* rst,
                   const xComplex z,
                   const int n,
                   const double eps)
{
     int k, ret;
     double sita, norm, R;
     if(rst==NULL) return 0;
     sita = atan2(z.img, z.real);
     norm = sqrt(z.real * z.real + z.img * z.img);
     R   = pow(norm, 1.0/n);
     for(k=0; k<n; ++k){
           rst[k].real = R * cos( (sita + 2.0 * PI * k)/n);
           rst[k].img = R * sin( (sita + 2.0 * PI * k)/n);
     }
     return 1;
}
// ---------------------------------
// exp(z)
int comp_exp(xComplex *rst,
                const xComplex z)
{
     double ex;
     if(rst==NULL) return 0;
     ex = exp(z.real);
     rst->real = ex * cos(z.img);
     rst->img = ex * sin(z.img);
     return 1;
}

// ---------------------------------
//
整數次方, pown(z, n)
int comp_pown(xComplex *rst,
                const xComplex z,
                const int n)
{
     double R, sita;
     if(rst==NULL ) return 0;
     sita = atan2(z.img, z.real);
     R = sqrt(z.real * z.real + z.img * z.img);
     rst->real = pow(R, n) * cos(n*sita);
     rst->img = pow(R, n) * sin(n*sita);
     return 1;
}
// ---------------------------------
// log(z)
int comp_log(xComplex *rst,
                const xComplex z)
{
     double sita, R;
     if(rst==NULL ==0) return 0;
     sita = atan2(z.img, z.real);
     R = sqrt(z.real * z.real + z.img * z.img);
     rst->real = log(R);
     rst->img = sita;
     return 1;
}
// ---------------------------------
//
複數次方, z=pow(z1, z2)
int comp_pow(xComplex *rst,
                const xComplex z1,
                const xComplex z2)
{
     double r, lnr, sita, ex, sita2;
     if(rst==NULL) return 0;

     sita = atan2(z1.img, z1.real);
     r = sqrt(z1.real*z1.real + z1.img * z1.img);

     lnr = log(r);
     ex = exp(z2.real*lnr - z2.img*sita);
     sita2 = z2.img * lnr + z2.real*sita;

     rst->real = ex * cos(sita2);
     rst->img = ex * sin(sita2);
     return 1;
}

// ---------------------------------
// sin(z)
int comp_sin(xComplex *rst,
                const xComplex z)
{
     double ex1, ex2;
     if(rst==NULL) return 0;
     ex1 = exp(z.img), ex2 = 1.0 / ex1;
     rst->real = 0.5 * sin(z.real) * (ex1 + ex2);
     rst->img = 0.5 * cos(z.real) * (ex1 - ex2);
     return 1;
}
// ---------------------------------
// cos(z)
int comp_cos(xComplex *rst,
                const xComplex z)
{
     double ex1, ex2;
     if(rst==NULL) return 0;
     ex1 = exp(z.img), ex2 = 1.0 / ex1;
     rst->real = 0.5 * cos(z.real) * (ex1 + ex2);
     rst->img = 0.5 * sin(z.real) * (ex1 - ex2);
     return 1;
}
// ---------------------------------
// sinh(z)
int comp_sinh(xComplex *rst,
                const xComplex z)
{
     double ex1, ex2;
     if(rst==NULL) return 0;
     ex1 = exp(z.real), ex2=1.0/ex1;
     rst->real = 0.5 * cos(z.img) * (ex1 - ex2);
     rst->img = 0.5 * sin(z.img) * (ex1 + ex2);
     return 1;
}
// ---------------------------------
// cosh(z)
int comp_cosh(xComplex *rst,
                const xComplex z)
{
     double ex1, ex2;
     if(rst==NULL) return 0;
     ex1 = exp(z.real), ex2=1.0/ex1;
     rst->real = 0.5 * cos(z.img) * (ex1 + ex2);
     rst->img = 0.5 * sin(z.img) * (ex1 - ex2);
     return 1;
}

 

 

xComplexMain.c

 

 

測試碼懶得寫,挑幾個簡單的出來,有興趣的可再加其他測試。


/*******************************************************************/
/*                                                                 */
/*     filename : xComplexMain.c                                   */
/*     author   : edison.shih/edisonx                              */
/*     compiler : Visual C++ 2008                                  */
/*     date     : 2011.03.07                                       */
/*                                                                 */
/*         A.L.L.     R.I.G.H.T.S.     R.E.S.E.R.V.E.              */
/*                                                                 */
/*******************************************************************/
#include <stdio.h>
#include "xComplex.h"

int main()
{
     const double eps = 1E-9;
     xComplex a={3.0, 4.0};
     xComplex b={6.0, 8.0};
     xComplex c;

     if(comp_add(&c, a, b)) {
           printf("\n > "),comp_dis(a), printf(" + ");
           comp_dis(b), printf(" = "),     comp_dis(c);
     }
     if(comp_sub(&c, a, b)) {
           printf("\n > "),comp_dis(a), printf(" - ");
           comp_dis(b), printf(" = "),     comp_dis(c);
     }
     if(comp_mul(&c, a, b)) {
           printf("\n > "),comp_dis(a), printf(" * ");
           comp_dis(b), printf(" = "),     comp_dis(c);
     }
     if(comp_div(&c, a, b,eps)) {
           printf("\n > "),comp_dis(a), printf(" * ");
           comp_dis(b), printf(" = "),     comp_dis(c);
     }
     if(comp_inv(&c, a, eps)) {
           printf("\n > "),comp_dis(a), printf(" inv ");
           printf(" = "),  comp_dis(c);
     }
     if(comp_conj(&c, a)) {
           printf("\n > "),comp_dis(a), printf("conj ");
           printf(" = "),  comp_dis(c);
     }
     if(comp_sin(&c, a)) {
           printf("\n > "),comp_dis(a), printf(" sin ");
           printf(" = "),  comp_dis(c);
     }
     return 0;
}

 

edisonx 發表在 痞客邦 PIXNET 留言(0) 人氣()