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