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

📄 hyper1f1.cpp

📁 基于Visual C++环境的的Bessel函数程序
💻 CPP
字号:
/*************************************************************************
Cephes Math Library Release 2.8:  June, 2000
Copyright by Stephen L. Moshier

Contributors:
    * Sergey Bochkanov (ALGLIB project). Translation from C to
      pseudocode.

See subroutines comments for additional copyrights.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

- Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer.

- Redistributions in binary form must reproduce the above copyright
  notice, this list of conditions and the following disclaimer listed
  in this license in the documentation and/or other materials
  provided with the distribution.

- Neither the name of the copyright holders nor the names of its
  contributors may be used to endorse or promote products derived from
  this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*************************************************************************/

#include <stdafx.h>
#include "hyper1f1.h"

double hyper1f1p(double a, double b, double x, double& err);
double hyper1f1a(double a, double b, double x, double& err);
double hyper2f0(double a, double b, double x, int tp, double& err);

/*************************************************************************
Confluent hypergeometric function

Computes the confluent hypergeometric function

                         1           2
                      a x    a(a+1) x
  F ( a,b;x )  =  1 + ---- + --------- + ...
 1 1                  b 1!   b(b+1) 2!

Many higher transcendental functions are special cases of
this power series.

As is evident from the formula, b must not be a negative
integer or zero unless a is an integer with 0 >= a > b.

The routine attempts both a direct summation of the series
and an asymptotic expansion.  In each case error due to
roundoff, cancellation, and nonconvergence is estimated.
The result with smaller estimated error is returned.



ACCURACY:

Tested at random points (a, b, x), all three variables
ranging from 0 to 30.
                     Relative error:
arithmetic   domain     # trials      peak         rms
   DEC       0,30         2000       1.2e-15     1.3e-16
 qtst1:
 21800   max =  1.4200E-14   rms =  1.0841E-15  ave = -5.3640E-17
 ltstd:
 25500   max = 1.2759e-14   rms = 3.7155e-16  ave = 1.5384e-18
   IEEE      0,30        30000       1.8e-14     1.1e-15

Larger errors can be observed when b is near a negative
integer or zero.  Certain combinations of arguments yield
serious cancellation error in the power series summation
and also are not in the region of near convergence of the
asymptotic series.  An error message is printed if the
self-estimated relative error is greater than 1.0e-12.

Cephes Math Library Release 2.8:  June, 2000
Copyright 1984, 1987, 1988, 2000 by Stephen L. Moshier
*************************************************************************/
double hypergeometric1f1(double a, double b, double x)
{
    double result;
    double asum;
    double psum;
    double acanc;
    double pcanc;
    double temp;

    temp = b-a;
    if( fabs(temp)<0.001*fabs(a) )
    {
        result = exp(x)*hypergeometric1f1(temp, b, -x);
        return result;
    }
    psum = hyper1f1p(a, b, x, pcanc);
    if( pcanc>=ap::machineepsilon )
    {
        asum = hyper1f1a(a, b, x, acanc);
        if( acanc<pcanc )
        {
            pcanc = acanc;
            psum = asum;
        }
    }
    ap::ap_error::make_assertion(pcanc<1.0e-12);
    result = psum;
    return result;
}


/*************************************************************************
Power series summation for confluent hypergeometric function

Cephes Math Library Release 2.8:  June, 2000
Copyright 1984, 1987, 1988, 2000 by Stephen L. Moshier
*************************************************************************/
double hyper1f1p(double a, double b, double x, double& err)
{
    double result;
    double n;
    double a0;
    double sum;
    double t;
    double u;
    double temp;
    double an;
    double bn;
    double maxt;
//  double pcanc;

    an = a;
    bn = b;
    a0 = 1.0;
    sum = 1.0;
    n = 1.0;
    t = 1.0;
    maxt = 0.0;
    while(t>ap::machineepsilon)
    {
        ap::ap_error::make_assertion(bn!=0);
        if( an==0 )
        {
            result = sum;
            return result;
        }
        if( n>200 )
        {
            break;
        }
        u = x*(an/(bn*n));
        temp = fabs(u);
        if( temp>1.0&&maxt>ap::maxrealnumber/temp )
        {
            err = 1.0;
            result = sum;
            return result;
        }
        a0 = a0*u;
        sum = sum+a0;
        t = fabs(a0);
        if( t>maxt )
        {
            maxt = t;
        }
        an = an+1.0;
        bn = bn+1.0;
        n = n+1.0;
    }
    if( sum!=0.0 )
    {
        maxt = maxt/fabs(sum);
    }
    maxt = maxt*ap::machineepsilon;
    err = fabs(ap::machineepsilon*n+maxt);
    result = sum;
    return result;
}


/*************************************************************************
asymptotic formula for hypergeometric function

Cephes Math Library Release 2.8:  June, 2000
Copyright 1984, 1987, 1988, 2000 by Stephen L. Moshier
*************************************************************************/
double hyper1f1a(double a, double b, double x, double& err)
{
    double result;
    double h1;
    double h2;
    double t;
    double u;
    double temp;
    double acanc;
    double asum;
    double err1;
    double err2;
    double s;

    if( x==0 )
    {
        err = 1.0;
        result = ap::maxrealnumber;
        return result;
    }
    temp = log(fabs(x));
    t = x+temp*(a-b);
    u = -temp*a;
    if( b>0 )
    {
        temp = lngamma(b, s);
        t = t+temp;
        u = u+temp;
    }
    h1 = hyper2f0(a, a-b+1, -1.0/x, 1, err1);
    temp = exp(u)/gamma(b-a);
    h1 = h1*temp;
    err1 = err1*temp;
    h2 = hyper2f0(b-a, 1.0-a, 1.0/x, 2, err2);
    if( a<0 )
    {
        temp = exp(t)/gamma(a);
    }
    else
    {
        temp = exp(t-lngamma(a, s));
    }
    h2 = h2*temp;
    err2 = err2*temp;
    if( x<0.0 )
    {
        asum = h1;
    }
    else
    {
        asum = h2;
    }
    acanc = fabs(err1)+fabs(err2);
    if( b<0 )
    {
        temp = gamma(b);
        asum = asum*temp;
        acanc = acanc*fabs(temp);
    }
    if( asum!=0.0 )
    {
        acanc = acanc/fabs(asum);
    }
    acanc = acanc*30.0;
    err = acanc;
    result = asum;
    return result;
}


/*************************************************************************
hyp2f0()

Cephes Math Library Release 2.8:  June, 2000
Copyright 1984, 1987, 1988, 2000 by Stephen L. Moshier
*************************************************************************/
double hyper2f0(double a, double b, double x, int tp, double& err)
{
    double result;
    double a0;
    double alast;
    double t;
    double tlast;
    double maxt;
    double n;
    double an;
    double bn;
    double u;
    double sum;
    double temp;
    int doneflag;

    an = a;
    bn = b;
    a0 = 1.0e0;
    alast = 1.0e0;
    sum = 0.0;
    n = 1.0e0;
    t = 1.0e0;
    tlast = 1.0e9;
    maxt = 0.0;
    doneflag = 0;
    do
    {
        if( an==0 )
        {
            break;
        }
        if( bn==0 )
        {
            break;
        }
        u = an*(bn*x/n);
        temp = fabs(u);
        if( temp>1.0&&maxt>ap::maxrealnumber/temp )
        {
            err = ap::maxrealnumber;
            result = sum;
            return result;
        }
        a0 = a0*u;
        t = fabs(a0);
        if( t>tlast )
        {
            doneflag = 1;
            break;
        }
        tlast = t;
        sum = sum+alast;
        alast = a0;
        if( n>200 )
        {
            doneflag = 1;
            break;
        }
        an = an+1.0e0;
        bn = bn+1.0e0;
        n = n+1.0e0;
        if( t>maxt )
        {
            maxt = t;
        }
    }
    while(t>ap::machineepsilon);
    if( doneflag==0 )
    {
        err = fabs(ap::machineepsilon*(n+maxt));
        alast = a0;
    }
    else
    {
        n = n-1.0;
        x = 1.0/x;
        if( tp==1 )
        {
            alast = alast*(0.5+(0.125+0.25*b-0.5*a+0.25*x-0.25*n)/x);
        }
        if( tp==1 )
        {
            alast = alast*(2.0/3.0-b+2.0*a+x-n);
        }
        err = ap::machineepsilon*(n+maxt)+fabs(a0);
    }
    result = sum+alast;
    return result;
    return result;
}



⌨️ 快捷键说明

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