Специальные функции:
arccos x (x - real)
» Кликните сюда для просмотра оффтоп текста.. «
Код
//////////////////////////////////////////////////////////////////////////////
//
// arccos x function calculating.
// (c) Johna Smith, 1996
//
// Method description:
// Calculating arccos x using the following formula:
// 2n+1
// Pi N 1*3*5..(2n-1)x
// arccos x = - - x - SUM ------------------ , |x|<1
// 2 n=1 2*4*6..(2n)(2n+1)
//////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <math.h>
#define Pi 3.1415926536
#define N 30
double Arccos(double x)
{
double arccos=x;
double a=x;
for (int i=1;i<N;i++)
{
a*=x*x*(2*i-1)*(2*i-1)/((2*i+1)*2*i);
arccos+=a;
}
return Pi/2-arccos;
}
void main(void)
{
printf("C++ arccos(0.5)=%g, this arccos(0.5)=%g.",acos(0.5),Arccos(0.5));
}
arccos z (z - complex)
» Кликните сюда для просмотра оффтоп текста.. «
Код
//////////////////////////////////////////////////////////////////////////////
//
// Calculactin function arccos z, where z is a complex value
// (c) Johna Smith, 1996
//
// Method description:
// 1 2 2 1 2 2
// A= - sqrt( (x+1) + y ) + - sqrt( (x-1) + y )
// 2 2
//
// 1 2 2 1 2 2
// B= - sqrt( (x+1) + y ) - - sqrt( (x-1) + y )
// 2 2
// 2
// arccos z = acrcos B - i*ln(a+sqrt(a -1))
//
//////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <math.h>
struct complex
{
float re;
float im;
};
void show_complex(complex c) // this functions displays complex value
{
printf("%f%+fi",c.re,c.im);
}
complex Arccos(complex z)
{
complex c;
float A,B;
A=sqrt((z.re+1)*(z.re+1)+z.im*z.im)/2 +
sqrt((z.re-1)*(z.re-1)+z.im*z.im)/2;
B=sqrt((z.re+1)*(z.re+1)+z.im*z.im)/2 -
sqrt((z.re-1)*(z.re-1)+z.im*z.im)/2;
c.re=acos(B);
c.im=-log(A+sqrt(A*A-1));
return c;
}
complex z={3,2};
void main(void)
{
printf("Arccos(");
show_complex(z);
printf(") = ");
show_complex(Arccos(z));
}
arcsin x
» Кликните сюда для просмотра оффтоп текста.. «
Код
//////////////////////////////////////////////////////////////////////////////
//
// arcsin x function calculating.
// (c) Johna Smith, 1996
//
// Method description:
// Calculating arcsin x using the following formula:
// 2n+1
// N 1*3*5..(2n-1)x
// arcsin x = x + SUM ------------------, |x|<1
// n=1 2*4*6..(2n)(2n+1)
//////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <math.h>
#define N 30
double Arcsin(double x)
{
double arcsin=x;
double a=x;
for (int i=1;i<N;i++)
{
a *= x*x*(2*i-1)*(2*i-1)/((2*i+1)*2*i);
arcsin += a;
}
return arcsin;
}
void main(void)
{
printf("C++ arcsin(0.5)=%g, this arcsin(0.5)=%g.",asin(0.5),Arcsin(0.5));
}
arcsin z
» Кликните сюда для просмотра оффтоп текста.. «
Код
//////////////////////////////////////////////////////////////////////////////
//
// Calculactin function arcsin z, where z is a complex value
// (c) Johna Smith, 1996
//
// Method description:
// 1 2 2 1 2 2
// A= - sqrt( (x+1) + y ) + - sqrt( (x-1) + y )
// 2 2
//
// 1 2 2 1 2 2
// B= - sqrt( (x+1) + y ) - - sqrt( (x-1) + y )
// 2 2
// 2
// arcsin z = acrsin B + i*ln(a+sqrt(a -1))
//
//////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <math.h>
struct complex
{
float re;
float im;
};
void show_complex(complex c) // this functions displays complex value
{
printf("%f%+fi",c.re,c.im);
}
complex Arcsin(complex z)
{
complex c;
float A,B;
A=sqrt((z.re+1)*(z.re+1)+z.im*z.im)/2 +
sqrt((z.re-1)*(z.re-1)+z.im*z.im)/2;
B=sqrt((z.re+1)*(z.re+1)+z.im*z.im)/2 -
sqrt((z.re-1)*(z.re-1)+z.im*z.im)/2;
c.re=asin(B);
c.im=log(A+sqrt(A*A-1));
return c;
}
complex z={3,2};
void main(void)
{
printf("Arcsin(");
show_complex(z);
printf(") = ");
show_complex(Arcsin(z));
}
arctg x
» Кликните сюда для просмотра оффтоп текста.. «
Код
//////////////////////////////////////////////////////////////////////////////
//
// arctg x function calculating.
// (c) Johna Smith, 1996
//
// Method description:
// Calculating arctg x using the following formulas:
// 2n+1
// N n x
// arctg x = SUM (-1) --------, |x|<1
// n=0 2n+1
//
//
// N n+1 1
// arctg x = SUM (-1) --------------, |x|>1
// n=0 (2n+1)x^(2n+1)
//
//////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <math.h>
#define N 100
#define Pi 3.1415926536
double Arctg(double x)
{
double arctg;
double a;
if (fabs(x)<1)
{
a=x;
arctg=x;
for (int i=1;i<N;i++)
{
a *= -x*x;
arctg += a/(2*i+1);
}
}
else
{
a=-1/x;
arctg=-1/x;
for (int i=1;i<N;i++)
{
a *= -1/(x*x);
arctg += a/(2*i+1);
}
arctg += (x>0?Pi/2.0:-Pi/2.0);
}
return arctg;
}
void main(void)
{
printf("C++ arctg(0.5)=%g, this arctg(0.5)=%g.",atan(0.5),Arctg(0.5));
}
arctg z
» Кликните сюда для просмотра оффтоп текста.. «
Код
//////////////////////////////////////////////////////////////////////////////
//
// arctg z function calculating, where z is a complex value
// (c) Johna Smith, 1996
//
// Method description:
//
// 1 2x 1 x^2+(y+1)^2
// arctg z = - arctg(---------) + i - ln(-----------)
// 2 1-x^2-y^2 4 x^2+(y-1)^2
//
//////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <math.h>
struct complex
{
float re;
float im;
};
void show_complex(complex c) // this functions displays complex value
{
printf("%f%+fi",c.re,c.im);
}
complex Arctg(complex z)
{
complex c;
c.re=atan(2*z.re/(1-z.re*z.re-z.im*z.im))/2;
c.im=log((z.re*z.re+(z.im+1)*(z.im+1))/(z.re*z.re+(z.im-1)*(z.im-1)))/4;
return c;
}
complex z={0.3,0.2};
void main(void)
{
printf("Arctg(");
show_complex(z);
printf(") = ");
show_complex(Arctg(z));
}
Функция Бесселя
» Кликните сюда для просмотра оффтоп текста.. «
Код
//////////////////////////////////////////////////////////////////////////////
//
// Bessel functions calculating.
// (c) Johna Smith, 1996
//
// Method description:
// Bessel functions are solutions of the followind eqation:
// 2 2
// d w 1 dw v
// ---- + - -- + ( 1 - --- )w = 0
// 2 x dx 2
// dx x
//
// This program calculates Bessel functions for integer v as infinite sum
//
//////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <math.h>
// this function returns value of Jn(x)
double Jn(int n,double x)
{
float nfact=1,ifact=1,nifact=1,sum=0,x2,element;
int i;
if (n>1) for (i=0;i<n;i++) nfact*=(i+1); // n factorial
x2=x*x/4;
i=0;
do
{
i++;
ifact*=i;
nifact*=(n+i);
element=(i%2==0?1:-1)*pow(x2,i)/(ifact*nifact);
sum+=element;
} while (fabs(element)>1e-9);
return pow(x/2,n)*(sum+1)/nfact;
}
// this function returns value of In(x)
double In(int n, double x)
{
float nfact=1,ifact=1,nifact=1,sum=0,x2,element;
int i;
if (n>1) for (i=0;i<n;i++) nfact*=(i+1); // n factorial
x2=x*x/4;
i=0;
do
{
i++;
ifact*=i;
nifact*=(n+i);
element=pow(x2,i)/(ifact*nifact);
sum+=element;
} while (fabs(element)>1e-9);
return pow(x/2,n)*(sum+1)/nfact;
}
void main(void)
{
// Examples
printf("J0(0.5)=%f\n",Jn(0,0.5));
printf("J30(20)=%f\n",Jn(30,20));
printf("I0(2)=%f\n",In(0,2));
printf("I1(2)=%f\n",In(1,2));
}
Гамма-функция
» Кликните сюда для просмотра оффтоп текста.. «
Код
//////////////////////////////////////////////////////////////////////////////
//
// Gamma function calculating.
// (c) Johna Smith, 1996
//
// Method description:
// infinity -t x-1
// Gamma(x)= Integral e t dt
// 0
//
// This integral is approximated and calculated by Stirling's formula:
// A
// (A) 1/12A
// Gamma= (-) e D sqrt(2 Pi A)
// (e)
//
// This algorithm is oriented for -10<x<=70
//
//////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <math.h>
#define E 2.7182818285
#define Pi 3.1415926536
// this function returns value of Gamma(x)
double Gamma(double x)
{
double A,z,g,c,D;
int b;
A=x-1;
z=A-64;
if (z<0)
{
b=abs((int)(z-1));
c=A;
A+=b;
D=1;
for(int i=b;i>0;i--)
{
c++;
D/=c;
}
} else D=1;
g=pow(A/E,A)*exp(1/(12*A))*D*sqrt(2*Pi*A);
return g;
}
void main(void)
{
// Examples
printf("Gamma(-1.5)=%f, exact value is 2.363271801\n",Gamma(-1.5));
printf("Gamma(-0.5)=%f, exact value is -3.544907702\n",Gamma(-0.5));
printf("Gamma(1.5)=%f, exact value is 0.886226926\n",Gamma(1.5));
printf("Gamma(40)=%f, exact value is 2.039788208E+046\n",Gamma(40));
}
Бета-функция
» Кликните сюда для просмотра оффтоп текста.. «
Код
//////////////////////////////////////////////////////////////////////////////
//
// Beta function calculating.
// (c) Johna Smith, 1996
//
// Method description:
// 1 x-1 w-1
// Beta(x,w)= Integral t (1-t) dt
// 0
// Gamma(x) Gamma(w)
// Beta(x,w)= -----------------
// Gamma(x+w)
//
// This algorithm is oriented for -10 < x,w,x+w <= 70
//
//////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <math.h>
#define E 2.7182818285
#define Pi 3.1415926536
// this function returns value of Gamma(x)
double Gamma(double x)
{
double A,z,g,c,D;
int b;
A=x-1;
z=A-64;
if (z<0)
{
b=abs((int)(z-1));
c=A;
A+=b;
D=1;
for(int i=b;i>0;i--)
{
c++;
D/=c;
}
} else D=1;
g=pow(A/E,A)*exp(1/(12*A))*D*sqrt(2*Pi*A);
return g;
}
double Beta(double x, double w)
{
return(Gamma(x)*Gamma(w)/Gamma(x+w));
}
void main(void)
{
// Examples
printf("Beta(1,2)=%f, exact value is 0.5\n",Beta(1,2));
}