-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculateDotProduct.php
93 lines (76 loc) · 2.65 KB
/
calculateDotProduct.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
// my shared server doesn't support PHP statistic functions (so, no stats_stat_innerproduct)
// check if each element in array is numeric (very improvable)
function arrayIsNumeric($array) {
foreach($array as $value) {
if (!(is_numeric($value))) {
return false;
}
}
return true;
}
/**
*
* Calculates the dot product of two or more vectors.
* The dot product is the result of multiplying the individual numerical values in two or more vectors
*
* @param array $vector1 The first vector
* @param array $vector2 The second vector
* @param array ...$moreVectors Other eventual vectors
*
* @throws ArgumentCountError if user passes less than two vectors
* @throws TypeError if user doesn't pass an array
* @throws LengthException if the length is not the same for every array OR if array contains less than 1 element
* @throws UnexpectedValueException if element in array is not numeric
*
* @author https://github.com/n-gram-hub
*
* @return float
*
*/
function calculateDotProduct(array $vector1, array $vector2, array ...$moreVectors)
{
// array comprising the function's argument list
$v = func_get_args();
// arguments count (if valid, vectors count)
$vCount = count($v);
// tests
for ($i = 0; $i < $vCount; $i++) {
// check if vectors have equal length
if (count($v[0]) != count($v[$i])) {
throw new LengthException("The array length is not the same");
}
// if there is at least one element for each array
if (empty($v[$i])) {
throw new LengthException("Each array must contain at least 1 element");
}
// any element in any array must be numeric
if (!(arrayIsNumeric($v[$i]))) {
throw new UnexpectedValueException("Each array element must be numeric");
}
}
// length of a vector
$singleVectorLength = count($v[0]);
// initialize scalar
$scalar = 0;
// loop "horizontally"
for ($i = 0; $i < $singleVectorLength; $i++) {
// add, multiply "vertically" :-)
$scalar += array_product(array_column($v,$i));
}
// return scalar
return floatval($scalar);
}
try {
echo calculateDotProduct([1.01, 2, 3], [1.0101, 2, 3], [1.1212, 2, 3]);
} catch (ArgumentCountError $e) {
echo "ArgumentCountError: " . $e->getMessage();
} catch (TypeError $e) {
echo "TypeError: " . $e->getMessage();
} catch (LengthException $e) {
echo "LengthException: " . $e->getMessage();
} catch (UnexpectedValueException $e) {
echo "UnexpectedValueException: " . $e->getMessage();
} catch (Exception $e) {
echo "Exception: " . $e->getMessage();
}