| 1441 |
ariadna |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
|
|
|
4 |
|
|
|
5 |
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
|
|
|
6 |
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
|
|
7 |
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
|
|
|
8 |
|
|
|
9 |
class SumSquares
|
|
|
10 |
{
|
|
|
11 |
/**
|
|
|
12 |
* SUMSQ.
|
|
|
13 |
*
|
|
|
14 |
* SUMSQ returns the sum of the squares of the arguments
|
|
|
15 |
*
|
|
|
16 |
* Excel Function:
|
|
|
17 |
* SUMSQ(value1[,value2[, ...]])
|
|
|
18 |
*
|
|
|
19 |
* @param mixed ...$args Data values
|
|
|
20 |
*/
|
|
|
21 |
public static function sumSquare(mixed ...$args): string|int|float
|
|
|
22 |
{
|
|
|
23 |
try {
|
|
|
24 |
$returnValue = 0;
|
|
|
25 |
|
|
|
26 |
// Loop through arguments
|
|
|
27 |
foreach (Functions::flattenArray($args) as $arg) {
|
|
|
28 |
$arg1 = Helpers::validateNumericNullSubstitution($arg, 0);
|
|
|
29 |
$returnValue += ($arg1 * $arg1);
|
|
|
30 |
}
|
|
|
31 |
} catch (Exception $e) {
|
|
|
32 |
return $e->getMessage();
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
return $returnValue;
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
private static function getCount(array $array1, array $array2): int
|
|
|
39 |
{
|
|
|
40 |
$count = count($array1);
|
|
|
41 |
if ($count !== count($array2)) {
|
|
|
42 |
throw new Exception(ExcelError::NA());
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
return $count;
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* These functions accept only numeric arguments, not even strings which are numeric.
|
|
|
50 |
*/
|
|
|
51 |
private static function numericNotString(mixed $item): bool
|
|
|
52 |
{
|
|
|
53 |
return is_numeric($item) && !is_string($item);
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* SUMX2MY2.
|
|
|
58 |
*
|
|
|
59 |
* @param mixed[] $matrixData1 Matrix #1
|
|
|
60 |
* @param mixed[] $matrixData2 Matrix #2
|
|
|
61 |
*/
|
|
|
62 |
public static function sumXSquaredMinusYSquared(array $matrixData1, array $matrixData2): string|int|float
|
|
|
63 |
{
|
|
|
64 |
try {
|
|
|
65 |
$array1 = Functions::flattenArray($matrixData1);
|
|
|
66 |
$array2 = Functions::flattenArray($matrixData2);
|
|
|
67 |
$count = self::getCount($array1, $array2);
|
|
|
68 |
|
|
|
69 |
$result = 0;
|
|
|
70 |
for ($i = 0; $i < $count; ++$i) {
|
|
|
71 |
if (self::numericNotString($array1[$i]) && self::numericNotString($array2[$i])) {
|
|
|
72 |
$result += ($array1[$i] * $array1[$i]) - ($array2[$i] * $array2[$i]);
|
|
|
73 |
}
|
|
|
74 |
}
|
|
|
75 |
} catch (Exception $e) {
|
|
|
76 |
return $e->getMessage();
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
return $result;
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* SUMX2PY2.
|
|
|
84 |
*
|
|
|
85 |
* @param mixed[] $matrixData1 Matrix #1
|
|
|
86 |
* @param mixed[] $matrixData2 Matrix #2
|
|
|
87 |
*/
|
|
|
88 |
public static function sumXSquaredPlusYSquared(array $matrixData1, array $matrixData2): string|int|float
|
|
|
89 |
{
|
|
|
90 |
try {
|
|
|
91 |
$array1 = Functions::flattenArray($matrixData1);
|
|
|
92 |
$array2 = Functions::flattenArray($matrixData2);
|
|
|
93 |
$count = self::getCount($array1, $array2);
|
|
|
94 |
|
|
|
95 |
$result = 0;
|
|
|
96 |
for ($i = 0; $i < $count; ++$i) {
|
|
|
97 |
if (self::numericNotString($array1[$i]) && self::numericNotString($array2[$i])) {
|
|
|
98 |
$result += ($array1[$i] * $array1[$i]) + ($array2[$i] * $array2[$i]);
|
|
|
99 |
}
|
|
|
100 |
}
|
|
|
101 |
} catch (Exception $e) {
|
|
|
102 |
return $e->getMessage();
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
return $result;
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
/**
|
|
|
109 |
* SUMXMY2.
|
|
|
110 |
*
|
|
|
111 |
* @param mixed[] $matrixData1 Matrix #1
|
|
|
112 |
* @param mixed[] $matrixData2 Matrix #2
|
|
|
113 |
*/
|
|
|
114 |
public static function sumXMinusYSquared(array $matrixData1, array $matrixData2): string|int|float
|
|
|
115 |
{
|
|
|
116 |
try {
|
|
|
117 |
$array1 = Functions::flattenArray($matrixData1);
|
|
|
118 |
$array2 = Functions::flattenArray($matrixData2);
|
|
|
119 |
$count = self::getCount($array1, $array2);
|
|
|
120 |
|
|
|
121 |
$result = 0;
|
|
|
122 |
for ($i = 0; $i < $count; ++$i) {
|
|
|
123 |
if (self::numericNotString($array1[$i]) && self::numericNotString($array2[$i])) {
|
|
|
124 |
$result += ($array1[$i] - $array2[$i]) * ($array1[$i] - $array2[$i]);
|
|
|
125 |
}
|
|
|
126 |
}
|
|
|
127 |
} catch (Exception $e) {
|
|
|
128 |
return $e->getMessage();
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
return $result;
|
|
|
132 |
}
|
|
|
133 |
}
|