Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
28.57% |
8 / 28 |
|
66.67% |
4 / 6 |
CRAP | |
0.00% |
0 / 1 |
| Vector | |
28.57% |
8 / 28 |
|
66.67% |
4 / 6 |
64.48 | |
0.00% |
0 / 1 |
| fromArray | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| setV | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getV | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setMatrixV | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| cosine | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
42 | |||
| cross3 | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jingga |
| 4 | * |
| 5 | * PHP Version 8.1 |
| 6 | * |
| 7 | * @package phpOMS\Math\Matrix |
| 8 | * @copyright Dennis Eichhorn |
| 9 | * @license OMS License 2.0 |
| 10 | * @version 1.0.0 |
| 11 | * @link https://jingga.app |
| 12 | */ |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace phpOMS\Math\Matrix; |
| 16 | |
| 17 | /** |
| 18 | * Vector class |
| 19 | * |
| 20 | * @package phpOMS\Math\Matrix |
| 21 | * @license OMS License 2.0 |
| 22 | * @link https://jingga.app |
| 23 | * @since 1.0.0 |
| 24 | */ |
| 25 | final class Vector extends Matrix |
| 26 | { |
| 27 | /** |
| 28 | * Create vector from array |
| 29 | * |
| 30 | * @param array $vector Matrix array |
| 31 | * |
| 32 | * @return self |
| 33 | * |
| 34 | * @since 1.0.0 |
| 35 | */ |
| 36 | public static function fromArray(array $vector) : self |
| 37 | { |
| 38 | $v = new self(\count($vector), 1); |
| 39 | $v->setMatrixV($vector); |
| 40 | |
| 41 | return $v; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Set vector value |
| 46 | * |
| 47 | * @param int $m Position to set |
| 48 | * @param int|float $value Value to set |
| 49 | * |
| 50 | * @return void |
| 51 | * |
| 52 | * @since 1.0.0 |
| 53 | */ |
| 54 | public function setV(int $m, int | float $value) : void |
| 55 | { |
| 56 | parent::set($m , 0, $value); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Get vector value |
| 61 | * |
| 62 | * @param int $m Position to get |
| 63 | * |
| 64 | * @return int|float |
| 65 | * |
| 66 | * @since 1.0.0 |
| 67 | */ |
| 68 | public function getV(int $m) : int | float |
| 69 | { |
| 70 | return parent::get($m, 0); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Set matrix |
| 75 | * |
| 76 | * @param array<int, int|float> $vector 1-Dimensional array |
| 77 | * |
| 78 | * @return Vector |
| 79 | * |
| 80 | * @since 1.0.0 |
| 81 | */ |
| 82 | public function setMatrixV(array $vector) : self |
| 83 | { |
| 84 | foreach ($vector as $key => $value) { |
| 85 | $this->setV($key, $value); |
| 86 | } |
| 87 | |
| 88 | return $this; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Angle between two vectors |
| 93 | * |
| 94 | * @param self $v Vector |
| 95 | * |
| 96 | * @return float |
| 97 | * |
| 98 | * @since 1.0.0 |
| 99 | */ |
| 100 | public function cosine(self $v) : float |
| 101 | { |
| 102 | $dotProduct = 0.0; |
| 103 | for ($i = 0; $i < $this->m; ++$i) { |
| 104 | $dotProduct += $this->matrix[$i][0] * $v->matrix[$i][0]; |
| 105 | } |
| 106 | |
| 107 | $sumOfSquares = 0; |
| 108 | foreach ($this->matrix as $value) { |
| 109 | $sumOfSquares += $value[0] * $value[0]; |
| 110 | } |
| 111 | $magnitude1 = \sqrt($sumOfSquares); |
| 112 | |
| 113 | $sumOfSquares = 0.0; |
| 114 | foreach ($v->matrix as $value) { |
| 115 | $sumOfSquares += $value[0] * $value[0]; |
| 116 | } |
| 117 | $magnitude2 = \sqrt($sumOfSquares); |
| 118 | |
| 119 | if ($magnitude1 === 0.0 || $magnitude2 === 0.0) { |
| 120 | return \PHP_FLOAT_MAX; |
| 121 | } |
| 122 | |
| 123 | return $dotProduct / ($magnitude1 * $magnitude2); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Calculate the cross product |
| 128 | * |
| 129 | * @param self $vector 3 Vector |
| 130 | * |
| 131 | * @return Vector |
| 132 | * |
| 133 | * @since 1.0.0 |
| 134 | */ |
| 135 | public function cross3(self $vector) : self |
| 136 | { |
| 137 | $crossArray = [ |
| 138 | $this->getV(1) * $vector->getV(2) - $this->getV(2) * $vector->getV(1), |
| 139 | $this->getV(2) * $vector->getV(0) - $this->getV(0) * $vector->getV(2), |
| 140 | $this->getV(0) * $vector->getV(1) - $this->getV(1) * $vector->getV(0), |
| 141 | ]; |
| 142 | |
| 143 | return self::fromArray($crossArray); |
| 144 | } |
| 145 | } |