phpmath /
vector
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace PHP\Math\Vector; |
||
| 4 | |||
| 5 | use InvalidArgumentException; |
||
| 6 | use PHP\Math\BigNumber\BigNumber; |
||
| 7 | |||
| 8 | class Vector extends Tuple |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * Adds the given vector to this vector. |
||
| 12 | * |
||
| 13 | * @param Vector $vector The vector to add. |
||
| 14 | * @return Vector |
||
| 15 | * @throws InvalidArgumentException Thrown when the given vector is of a different length. |
||
| 16 | */ |
||
| 17 | 2 | public function add(Vector $vector) |
|
| 18 | { |
||
| 19 | 2 | if ($this->getSize() != $vector->getSize()) { |
|
| 20 | 1 | throw new InvalidArgumentException('Invalid vector provided, should be of the same size.'); |
|
| 21 | } |
||
| 22 | |||
| 23 | 1 | foreach ($this as $index => $value) { |
|
| 24 | 1 | $this[$index]->add($vector[$index]); |
|
| 25 | 1 | } |
|
| 26 | |||
| 27 | 1 | return $this; |
|
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Conjugates the vector. This is an alias of reverse(). |
||
| 32 | */ |
||
| 33 | 1 | public function conjugate() |
|
| 34 | { |
||
| 35 | 1 | return $this->reverse(); |
|
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Calculates the dot product between this vector and the given vector. |
||
| 40 | * |
||
| 41 | * @param Vector $vector The vector to calculate thet dot product with. |
||
| 42 | * @return float |
||
| 43 | */ |
||
| 44 | 3 | public function dotProduct(Vector $vector) |
|
| 45 | { |
||
| 46 | 3 | if ($this->getSize() !== $vector->getSize()) { |
|
| 47 | 1 | throw new InvalidArgumentException('Invalid vector provided. The size should be the same.'); |
|
| 48 | } |
||
| 49 | |||
| 50 | 2 | $result = new BigNumber(); |
|
| 51 | |||
| 52 | 2 | for ($i = 0; $i < $this->getSize(); ++$i) { |
|
| 53 | 2 | $value = new BigNumber($this[$i]); |
|
| 54 | 2 | $value->multiply($vector[$i]); |
|
| 55 | |||
| 56 | 2 | $result->add($value); |
|
| 57 | 2 | } |
|
| 58 | |||
| 59 | 2 | return $result; |
|
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Gets the length of the vector. |
||
| 64 | * |
||
| 65 | * @return float |
||
| 66 | */ |
||
| 67 | 3 | public function getLength() |
|
| 68 | { |
||
| 69 | 3 | return $this->getLengthSquared()->sqrt(); |
|
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Gets the square of the vector's length. |
||
| 74 | * |
||
| 75 | * @return float |
||
| 76 | */ |
||
| 77 | 4 | public function getLengthSquared() |
|
| 78 | { |
||
| 79 | 4 | $result = new BigNumber(); |
|
| 80 | 4 | for ($i = 0; $i < $this->getSize(); $i++) { |
|
| 81 | 4 | $pow = new BigNumber($this->getElement($i)); |
|
|
0 ignored issues
–
show
|
|||
| 82 | 4 | $pow->pow(2); |
|
| 83 | |||
| 84 | 4 | $result->add($pow); |
|
| 85 | 4 | } |
|
| 86 | 4 | return $result; |
|
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Gets the magnitude of the vector. This is an alias of getLength(). |
||
| 91 | * |
||
| 92 | * @return float |
||
| 93 | */ |
||
| 94 | 1 | public function getMagnitude() |
|
| 95 | { |
||
| 96 | 1 | return $this->getLength(); |
|
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Negates this vector. The vector has the same magnitude as before, but its direction is now opposite. |
||
| 101 | * |
||
| 102 | * @return Vector |
||
| 103 | */ |
||
| 104 | 3 | public function negate() |
|
| 105 | { |
||
| 106 | 3 | for ($i = 0; $i < $this->getSize(); $i++) { |
|
| 107 | 3 | $this->getElement($i)->multiply(-1); |
|
| 108 | 3 | } |
|
| 109 | |||
| 110 | 3 | return $this; |
|
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Normalizes the vector, making sure the length of the vector is 1 (a unit vector). |
||
| 115 | * |
||
| 116 | * @return Vector |
||
| 117 | */ |
||
| 118 | 1 | public function normalize() |
|
| 119 | { |
||
| 120 | 1 | $length = $this->getLength(); |
|
| 121 | |||
| 122 | 1 | for ($i = 0; $i < $this->getSize(); $i++) { |
|
| 123 | 1 | $this->getElement($i)->divide($length); |
|
| 124 | 1 | } |
|
| 125 | |||
| 126 | 1 | return $this; |
|
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Reverses the sign of the components. This is an alias of negate. |
||
| 131 | * |
||
| 132 | * @return Vector |
||
| 133 | */ |
||
| 134 | 2 | public function reverse() |
|
| 135 | { |
||
| 136 | 2 | return $this->negate(); |
|
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Scales the vector. |
||
| 141 | * |
||
| 142 | * @param float $scale The scale factor. |
||
| 143 | * @return Vector |
||
| 144 | */ |
||
| 145 | 1 | public function scale($scale) |
|
| 146 | { |
||
| 147 | 1 | foreach ($this as $key => $value) { |
|
| 148 | 1 | $this[$key]->multiply($scale); |
|
| 149 | 1 | } |
|
| 150 | |||
| 151 | 1 | return $this; |
|
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Subtracts the given vector from this vector. |
||
| 156 | * |
||
| 157 | * @param Vector $vector The vector to sutbract. |
||
| 158 | * @return Vector |
||
| 159 | * @throws InvalidArgumentException Thrown when the given vector is of a different length. |
||
| 160 | */ |
||
| 161 | 2 | public function subtract(Vector $vector) |
|
| 162 | { |
||
| 163 | 2 | if ($this->getSize() != $vector->getSize()) { |
|
| 164 | 1 | throw new InvalidArgumentException('Invalid vector provided, should be of the same size.'); |
|
| 165 | } |
||
| 166 | |||
| 167 | 1 | foreach ($this as $index => $value) { |
|
| 168 | 1 | $this[$index]->subtract($vector[$index]); |
|
| 169 | 1 | } |
|
| 170 | |||
| 171 | 1 | return $this; |
|
| 172 | } |
||
| 173 | } |
||
| 174 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: