Failed Conditions
Pull Request — master (#3876)
by Abdul Malik
17:26 queued 02:15
created

BesselY   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
eloc 57
c 0
b 0
f 0
dl 0
loc 127
ccs 56
cts 56
cp 1
rs 10
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Calculation\Engineering;
4
5
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
6
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
7
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
8
9
class BesselY
10
{
11
    use ArrayEnabled;
12
13
    /**
14
     * BESSELY.
15
     *
16
     * Returns the Bessel function, which is also called the Weber function or the Neumann function.
17
     *
18
     *    Excel Function:
19
     *        BESSELY(x,ord)
20
     *
21
     * @param mixed $x A float value at which to evaluate the function.
22
     *                   If x is nonnumeric, BESSELY returns the #VALUE! error value.
23
     *                      Or can be an array of values
24
     * @param mixed $ord The integer order of the Bessel function.
25
     *                       If ord is not an integer, it is truncated.
26
     *                       If $ord is nonnumeric, BESSELY returns the #VALUE! error value.
27
     *                       If $ord < 0, BESSELY returns the #NUM! error value.
28
     *                      Or can be an array of values
29
     *
30
     * @return array|float|string Result, or a string containing an error
31
     *         If an array of numbers is passed as an argument, then the returned result will also be an array
32
     *            with the same dimensions
33
     */
34 137
    public static function BESSELY(mixed $x, mixed $ord): array|string|float
35
    {
36 137
        if (is_array($x) || is_array($ord)) {
37 46
            return self::evaluateArrayArguments([self::class, __FUNCTION__], $x, $ord);
38
        }
39
40
        try {
41 137
            $x = EngineeringValidations::validateFloat($x);
42 128
            $ord = EngineeringValidations::validateInt($ord);
43 18
        } catch (Exception $e) {
44 18
            return $e->getMessage();
45
        }
46
47 119
        if (($ord < 0) || ($x <= 0.0)) {
48 10
            return ExcelError::NAN();
49
        }
50
51 110
        $fBy = self::calculate($x, $ord);
52
53 110
        return (is_nan($fBy)) ? ExcelError::NAN() : $fBy;
54
    }
55
56 110
    private static function calculate(float $x, int $ord): float
57
    {
58 110
        return match ($ord) {
59 110
            0 => self::besselY0($x),
60 110
            1 => self::besselY1($x),
61 110
            default => self::besselY2($x, $ord),
62 110
        };
63
    }
64
65
    /**
66
     * Mollify Phpstan.
67
     *
68
     * @codeCoverageIgnore
69
     */
70
    private static function callBesselJ(float $x, int $ord): float
71
    {
72
        $rslt = BesselJ::BESSELJ($x, $ord);
73
        if (!is_float($rslt)) {
74
            throw new Exception('Unexpected array or string');
75
        }
76
77
        return $rslt;
78
    }
79
80 83
    private static function besselY0(float $x): float
81
    {
82 83
        if ($x < 8.0) {
83 44
            $y = ($x * $x);
84 44
            $ans1 = -2_957_821_389.0 + $y * (7_062_834_065.0 + $y * (-512_359_803.6 + $y * (10_879_881.29 + $y
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING on line 84 at column 22
Loading history...
85 44
                            * (-86327.92757 + $y * 228.4622733))));
86 44
            $ans2 = 40_076_544_269.0 + $y * (745_249_964.8 + $y * (7_189_466.438 + $y
87 44
                        * (47447.26470 + $y * (226.1030244 + $y))));
88
89 44
            return $ans1 / $ans2 + 0.636619772 * self::callBesselJ($x, 0) * log($x);
90
        }
91
92 39
        $z = 8.0 / $x;
93 39
        $y = ($z * $z);
94 39
        $xx = $x - 0.785398164;
95 39
        $ans1 = 1 + $y * (-0.1098628627e-2 + $y * (0.2734510407e-4 + $y * (-0.2073370639e-5 + $y * 0.2093887211e-6)));
96 39
        $ans2 = -0.1562499995e-1 + $y * (0.1430488765e-3 + $y * (-0.6911147651e-5 + $y * (0.7621095161e-6 + $y
97 39
                        * (-0.934945152e-7))));
98
99 39
        return sqrt(0.636619772 / $x) * (sin($xx) * $ans1 + $z * cos($xx) * $ans2);
100
    }
101
102 83
    private static function besselY1(float $x): float
103
    {
104 83
        if ($x < 8.0) {
105 44
            $y = ($x * $x);
106 44
            $ans1 = $x * (-0.4900604943e13 + $y * (0.1275274390e13 + $y * (-0.5153438139e11 + $y
107 44
                            * (0.7349264551e9 + $y * (-0.4237922726e7 + $y * 0.8511937935e4)))));
108 44
            $ans2 = 0.2499580570e14 + $y * (0.4244419664e12 + $y * (0.3733650367e10 + $y * (0.2245904002e8 + $y
109 44
                            * (0.1020426050e6 + $y * (0.3549632885e3 + $y)))));
110
111 44
            return ($ans1 / $ans2) + 0.636619772 * (self::callBesselJ($x, 1) * log($x) - 1 / $x);
112
        }
113
114 39
        $z = 8.0 / $x;
115 39
        $y = $z * $z;
116 39
        $xx = $x - 2.356194491;
117 39
        $ans1 = 1.0 + $y * (0.183105e-2 + $y * (-0.3516396496e-4 + $y * (0.2457520174e-5 + $y * (-0.240337019e-6))));
118 39
        $ans2 = 0.04687499995 + $y * (-0.2002690873e-3 + $y * (0.8449199096e-5 + $y
119 39
                    * (-0.88228987e-6 + $y * 0.105787412e-6)));
120
121 39
        return sqrt(0.636619772 / $x) * (sin($xx) * $ans1 + $z * cos($xx) * $ans2);
122
    }
123
124 56
    private static function besselY2(float $x, int $ord): float
125
    {
126 56
        $fTox = 2.0 / $x;
127 56
        $fBym = self::besselY0($x);
128 56
        $fBy = self::besselY1($x);
129 56
        for ($n = 1; $n < $ord; ++$n) {
130 56
            $fByp = $n * $fTox * $fBy - $fBym;
131 56
            $fBym = $fBy;
132 56
            $fBy = $fByp;
133
        }
134
135 56
        return $fBy;
136
    }
137
}
138