天天看點

C/C++ 内使用貝塞爾函數

今天在搗鼓MMSE,倒在了貝塞爾函數上,翻找好久,原來C++的cmath裡有。

官方文檔在這裡:

  1. https://docs.microsoft.com/en-us/cpp/standard-library/cmath?view=vs-2019#regular-modified-cylindrical-bessel-functions
  2. https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/bessel-functions-j0-j1-jn-y0-y1-yn?view=vs-2019

    第一個連結中是C++,引入cmath,可以調用四種貝塞爾函數。使用以下函數必須支援C++ 17标準,請注意在項目屬性中進行設定。

    (項目-屬性-正常-C++标準,下拉選擇C++ 17)

    首先給我這條九漏魚提個醒,

Bessel functions for integer α are also known as cylinder functions or the cylindrical harmonics because they appear in the solution to Laplace’s equation in cylindrical coordinates。

第一類貝塞爾函數:

double cyl_bessel_j(double nu, double x);
	float cyl_bessel_jf(float nu, float x);
	long double cyl_bessel_jl(long double nu, long double x);
           

第一類修正的貝塞爾函數,官方文檔的 Regular modified cylindrical Bessel functions。我思考了很久它到底是不是 Modified Bessel functions of the first kind…如果不是在stackoverflow裡看到一位朋友說:

By regular and irregular, I presume you mean regular or modified Bessel functions.

我可能真的以為這是什麼很高深的函數…看來這個說法也不怎麼普遍。官方文檔裡的命名也是讓我很不解…我剛剛一拍腦袋,我明明可以看中文版啊,過去切換語言一看——害,是機翻。

double cyl_bessel_i(double nu, double x);
float cyl_bessel_if(float nu, float x);
long double cyl_bessel_il(long double nu, long double x);
           

第二類貝塞爾函數,AKA Weber functions 、Neumann functions。

double cyl_neumann(double nu, double x);
float cyl_neumannf(float nu, float x);
long double cyl_neumannl(long double nu, long double x);
           

第二類修正的貝塞爾函數,官方文檔裡的 Irregular modified cylindrical Bessel functions。

double cyl_bessel_k(double nu, double x);
float cyl_bessel_kf(float nu, float x);
long double cyl_bessel_kl(long double nu, long double x);
           

第二個連結中是math.h中關于貝塞爾函數的說明。

如果在C中,引入math.h以後的函數調用與C++不同。math.h裡的貝塞爾函數隻能調用整數階的,感覺沒有C++那麼自由。

double _j0(double x);
double _j1(double x);
double _jn(int n, double x);
double _y0(double x);
double _y1(double x);
double _yn(int n, double x);
           

以下是官方示例。

// crt_bessel1.c
#include <math.h>
#include <stdio.h>

int main( void )
{
   double x = 2.387;
   int n = 3, c;

   printf( "Bessel functions for x = %f:\n", x );
   printf( "   Kind   Order  Function     Result\n\n" );
   printf( "   First  0      _j0( x )     %f\n", _j0( x ) );
   printf( "   First  1      _j1( x )     %f\n", _j1( x ) );
   for( c = 2; c < 5; c++ )
      printf( "   First  %d      _jn( %d, x )  %f\n", c, c, _jn( c, x ) );
   printf( "   Second 0      _y0( x )     %f\n", _y0( x ) );
   printf( "   Second 1      _y1( x )     %f\n", _y1( x ) );
   for( c = 2; c < 5; c++ )
      printf( "   Second %d      _yn( %d, x )  %f\n", c, c, _yn( c, x ) );
}
           

總結,還是要多學習唉。

[1] https://encyclopedia.thefreedictionary.com/Bessel+function
c++

繼續閱讀