ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
SumSquares.php
Go to the documentation of this file.
1 <?php
2 
4 
7 
8 class SumSquares
9 {
22  public static function sumSquare(...$args)
23  {
24  try {
25  $returnValue = 0;
26 
27  // Loop through arguments
28  foreach (Functions::flattenArray($args) as $arg) {
30  $returnValue += ($arg1 * $arg1);
31  }
32  } catch (Exception $e) {
33  return $e->getMessage();
34  }
35 
36  return $returnValue;
37  }
38 
39  private static function getCount(array $array1, array $array2): int
40  {
41  $count = count($array1);
42  if ($count !== count($array2)) {
43  throw new Exception(Functions::NA());
44  }
45 
46  return $count;
47  }
48 
54  private static function numericNotString($item): bool
55  {
56  return is_numeric($item) && !is_string($item);
57  }
58 
67  public static function sumXSquaredMinusYSquared($matrixData1, $matrixData2)
68  {
69  try {
70  $array1 = Functions::flattenArray($matrixData1);
71  $array2 = Functions::flattenArray($matrixData2);
72  $count = self::getCount($array1, $array2);
73 
74  $result = 0;
75  for ($i = 0; $i < $count; ++$i) {
76  if (self::numericNotString($array1[$i]) && self::numericNotString($array2[$i])) {
77  $result += ($array1[$i] * $array1[$i]) - ($array2[$i] * $array2[$i]);
78  }
79  }
80  } catch (Exception $e) {
81  return $e->getMessage();
82  }
83 
84  return $result;
85  }
86 
95  public static function sumXSquaredPlusYSquared($matrixData1, $matrixData2)
96  {
97  try {
98  $array1 = Functions::flattenArray($matrixData1);
99  $array2 = Functions::flattenArray($matrixData2);
100  $count = self::getCount($array1, $array2);
101 
102  $result = 0;
103  for ($i = 0; $i < $count; ++$i) {
104  if (self::numericNotString($array1[$i]) && self::numericNotString($array2[$i])) {
105  $result += ($array1[$i] * $array1[$i]) + ($array2[$i] * $array2[$i]);
106  }
107  }
108  } catch (Exception $e) {
109  return $e->getMessage();
110  }
111 
112  return $result;
113  }
114 
123  public static function sumXMinusYSquared($matrixData1, $matrixData2)
124  {
125  try {
126  $array1 = Functions::flattenArray($matrixData1);
127  $array2 = Functions::flattenArray($matrixData2);
128  $count = self::getCount($array1, $array2);
129 
130  $result = 0;
131  for ($i = 0; $i < $count; ++$i) {
132  if (self::numericNotString($array1[$i]) && self::numericNotString($array2[$i])) {
133  $result += ($array1[$i] - $array2[$i]) * ($array1[$i] - $array2[$i]);
134  }
135  }
136  } catch (Exception $e) {
137  return $e->getMessage();
138  }
139 
140  return $result;
141  }
142 }
$result
static validateNumericNullSubstitution($number, $substitute)
Validate numeric, but allow substitute for null.
Definition: Helpers.php:51
static flattenArray($array)
Convert a multi-dimensional array to a simple 1-dimensional array.
Definition: Functions.php:583
static sumXSquaredPlusYSquared($matrixData1, $matrixData2)
SUMX2PY2.
Definition: SumSquares.php:95
static sumXSquaredMinusYSquared($matrixData1, $matrixData2)
SUMX2MY2.
Definition: SumSquares.php:67
static getCount(array $array1, array $array2)
Definition: SumSquares.php:39
static numericNotString($item)
These functions accept only numeric arguments, not even strings which are numeric.
Definition: SumSquares.php:54
$i
Definition: disco.tpl.php:19
static sumXMinusYSquared($matrixData1, $matrixData2)
SUMXMY2.
Definition: SumSquares.php:123