Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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
    public static function BESSELY(mixed $x, mixed $ord): array|string|float
35
    {
36
        if (is_array($x) || is_array($ord)) {
37
            return self::evaluateArrayArguments([self::class, __FUNCTION__], $x, $ord);
38
        }
39
 
40
        try {
41
            $x = EngineeringValidations::validateFloat($x);
42
            $ord = EngineeringValidations::validateInt($ord);
43
        } catch (Exception $e) {
44
            return $e->getMessage();
45
        }
46
 
47
        if (($ord < 0) || ($x <= 0.0)) {
48
            return ExcelError::NAN();
49
        }
50
 
51
        $fBy = self::calculate($x, $ord);
52
 
53
        return (is_nan($fBy)) ? ExcelError::NAN() : $fBy;
54
    }
55
 
56
    private static function calculate(float $x, int $ord): float
57
    {
58
        return match ($ord) {
59
 
60
            1 => self::besselY1($x),
61
            default => self::besselY2($x, $ord),
62
        };
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
    private static function besselY0(float $x): float
81
    {
82
        if ($x < 8.0) {
83
            $y = ($x * $x);
84
            $ans1 = -2957821389.0 + $y * (7062834065.0 + $y * (-512359803.6 + $y * (10879881.29 + $y
85
                            * (-86327.92757 + $y * 228.4622733))));
86
            $ans2 = 40076544269.0 + $y * (745249964.8 + $y * (7189466.438 + $y
87
                        * (47447.26470 + $y * (226.1030244 + $y))));
88
 
89
            return $ans1 / $ans2 + 0.636619772 * self::callBesselJ($x, 0) * log($x);
90
        }
91
 
92
        $z = 8.0 / $x;
93
        $y = ($z * $z);
94
        $xx = $x - 0.785398164;
95
        $ans1 = 1 + $y * (-0.1098628627e-2 + $y * (0.2734510407e-4 + $y * (-0.2073370639e-5 + $y * 0.2093887211e-6)));
96
        $ans2 = -0.1562499995e-1 + $y * (0.1430488765e-3 + $y * (-0.6911147651e-5 + $y * (0.7621095161e-6 + $y
97
                        * (-0.934945152e-7))));
98
 
99
        return sqrt(0.636619772 / $x) * (sin($xx) * $ans1 + $z * cos($xx) * $ans2);
100
    }
101
 
102
    private static function besselY1(float $x): float
103
    {
104
        if ($x < 8.0) {
105
            $y = ($x * $x);
106
            $ans1 = $x * (-0.4900604943e13 + $y * (0.1275274390e13 + $y * (-0.5153438139e11 + $y
107
                            * (0.7349264551e9 + $y * (-0.4237922726e7 + $y * 0.8511937935e4)))));
108
            $ans2 = 0.2499580570e14 + $y * (0.4244419664e12 + $y * (0.3733650367e10 + $y * (0.2245904002e8 + $y
109
                            * (0.1020426050e6 + $y * (0.3549632885e3 + $y)))));
110
 
111
            return ($ans1 / $ans2) + 0.636619772 * (self::callBesselJ($x, 1) * log($x) - 1 / $x);
112
        }
113
 
114
        $z = 8.0 / $x;
115
        $y = $z * $z;
116
        $xx = $x - 2.356194491;
117
        $ans1 = 1.0 + $y * (0.183105e-2 + $y * (-0.3516396496e-4 + $y * (0.2457520174e-5 + $y * (-0.240337019e-6))));
118
        $ans2 = 0.04687499995 + $y * (-0.2002690873e-3 + $y * (0.8449199096e-5 + $y
119
                    * (-0.88228987e-6 + $y * 0.105787412e-6)));
120
 
121
        return sqrt(0.636619772 / $x) * (sin($xx) * $ans1 + $z * cos($xx) * $ans2);
122
    }
123
 
124
    private static function besselY2(float $x, int $ord): float
125
    {
126
        $fTox = 2.0 / $x;
127
        $fBym = self::besselY0($x);
128
        $fBy = self::besselY1($x);
129
        for ($n = 1; $n < $ord; ++$n) {
130
            $fByp = $n * $fTox * $fBy - $fBym;
131
            $fBym = $fBy;
132
            $fBy = $fByp;
133
        }
134
 
135
        return $fBy;
136
    }
137
}