ningshuxia
2022-12-12 4f1314cb69a47c22e52f1efdc4b4be6c1d55143d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using System;
using Complex = System.Numerics.Complex;
 
namespace IStation.Numerics
{
    /// <summary>
    /// This partial implementation of the SpecialFunctions class contains all methods related to the spherical Bessel functions.
    /// </summary>
    public static partial class SpecialFunctions
    {
        /// <summary>
        /// Returns the spherical Bessel function of the first kind.
        /// <para>SphericalBesselJ(n, z) is given by Sqrt(pi/2) / Sqrt(z) * BesselJ(n + 1/2, z).</para>
        /// </summary>
        /// <param name="n">The order of the spherical Bessel function.</param>
        /// <param name="z">The value to compute the spherical Bessel function of.</param>
        /// <returns>The spherical Bessel function of the first kind.</returns>
        public static Complex SphericalBesselJ(double n, Complex z)
        {
            if (double.IsNaN(n) || double.IsNaN(z.Real) || double.IsNaN(z.Imaginary))
            {
                return new Complex(double.NaN, double.NaN);
            }
            
            if (double.IsInfinity(z.Real))
            {
                return (z.Imaginary == 0) ? Complex.Zero : new Complex(double.PositiveInfinity, double.PositiveInfinity);
            }
 
            if (z.Real == 0 && z.Imaginary == 0)
            {
                return (n == 0) ? 1 : 0;
            }
 
            return Constants.SqrtPiOver2 * BesselJ(n + 0.5, z) / Complex.Sqrt(z);
        }
 
        /// <summary>
        /// Returns the spherical Bessel function of the first kind.
        /// <para>SphericalBesselJ(n, z) is given by Sqrt(pi/2) / Sqrt(z) * BesselJ(n + 1/2, z).</para>
        /// </summary>
        /// <param name="n">The order of the spherical Bessel function.</param>
        /// <param name="z">The value to compute the spherical Bessel function of.</param>
        /// <returns>The spherical Bessel function of the first kind.</returns>
        public static double SphericalBesselJ(double n, double z)
        {
            if (double.IsNaN(n) || double.IsNaN(z))
            {
                return double.NaN;
            }
 
            if (n < 0)
            {
                return double.NaN;
            }
 
            if (double.IsInfinity(z))
            {
                return 0;
            }
 
            if (z == 0)
            {
                return (n == 0) ? 1 : 0;
            }
 
            return Constants.SqrtPiOver2 * BesselJ(n + 0.5, z) / Math.Sqrt(z);
        }
 
        /// <summary>
        /// Returns the spherical Bessel function of the second kind.
        /// <para>SphericalBesselY(n, z) is given by Sqrt(pi/2) / Sqrt(z) * BesselY(n + 1/2, z).</para>
        /// </summary>
        /// <param name="n">The order of the spherical Bessel function.</param>
        /// <param name="z">The value to compute the spherical Bessel function of.</param>
        /// <returns>The spherical Bessel function of the second kind.</returns>
        public static Complex SphericalBesselY(double n, Complex z)
        {
            if (double.IsNaN(n) || double.IsNaN(z.Real) || double.IsNaN(z.Imaginary))
            {
                return new Complex(double.NaN, double.NaN);
            }
 
            if (double.IsInfinity(z.Real))
            {
                return (z.Imaginary == 0) ? Complex.Zero : new Complex(double.PositiveInfinity, double.PositiveInfinity);
            }
 
            if (z.Real == 0 && z.Imaginary == 0)
            {
                return new Complex(double.NaN, double.NaN);
            }
 
            return Constants.SqrtPiOver2 * BesselY(n + 0.5, z) / Complex.Sqrt(z);
        }
 
        /// <summary>
        /// Returns the spherical Bessel function of the second kind.
        /// <para>SphericalBesselY(n, z) is given by Sqrt(pi/2) / Sqrt(z) * BesselY(n + 1/2, z).</para>
        /// </summary>
        /// <param name="n">The order of the spherical Bessel function.</param>
        /// <param name="z">The value to compute the spherical Bessel function of.</param>
        /// <returns>The spherical Bessel function of the second kind.</returns>
        public static double SphericalBesselY(double n, double z)
        {
            if (double.IsNaN(n) || double.IsNaN(z))
            {
                return double.NaN;
            }
 
            if (n < 0)
            {
                return double.NaN;
            }
 
            if (double.IsInfinity(z))
            {
                return 0;
            }
 
            if (z == 0)
            {
                return double.NegativeInfinity;
            }
 
            return Constants.SqrtPiOver2 * BesselY(n + 0.5, z) / Math.Sqrt(z);
        }
    }
}