1 | <?php |
||
13 | class Vector |
||
14 | { |
||
15 | /** @type array<int|float> The components of the vector. */ |
||
16 | protected $_components; |
||
17 | |||
18 | /** |
||
19 | * Initialize the vector with its components. |
||
20 | * |
||
21 | * @api |
||
22 | * @param array<int|float> $components The components of the vector. |
||
23 | */ |
||
24 | public function __construct(array $components) |
||
25 | { |
||
26 | $this->_components = $components; |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * Creates a null/zero-length vector of the given dimension. |
||
31 | * |
||
32 | * @api |
||
33 | * @param int $dimension The dimension of the vector to create. Must be at least 0. |
||
34 | * @return self The zero-length vector for the given dimension. |
||
35 | * @throws Exception if the dimension is less than zero. |
||
36 | */ |
||
37 | public static function nullVector($dimension) |
||
49 | |||
50 | /** |
||
51 | * Get the components of the vector. |
||
52 | * |
||
53 | * @api |
||
54 | * @return array<int|float> The components of the vector. |
||
55 | */ |
||
56 | public function components() |
||
60 | |||
61 | /** |
||
62 | * Get the dimension/cardinality of the vector. |
||
63 | * |
||
64 | * @api |
||
65 | * @return int The dimension/cardinality of the vector. |
||
66 | */ |
||
67 | public function dimension() |
||
71 | |||
72 | /** |
||
73 | * Returns the length of the vector. |
||
74 | * |
||
75 | * @api |
||
76 | * @return float The length/magnitude of the vector. |
||
77 | */ |
||
78 | public function length() |
||
87 | |||
88 | /** |
||
89 | * Check whether the given vector is the same as this vector. |
||
90 | * |
||
91 | * @api |
||
92 | * @param self $b The vector to check for equality. |
||
93 | * @return bool True if the vectors are equal and false otherwise. |
||
94 | */ |
||
95 | public function isEqual(self $b) |
||
99 | |||
100 | /** |
||
101 | * Checks whether the two vectors are of the same dimension. |
||
102 | * |
||
103 | * @api |
||
104 | * @param self $b The vector to check against. |
||
105 | * @return bool True if the vectors are of the same dimension, false otherwise. |
||
106 | */ |
||
107 | public function isSameDimension(self $b) |
||
111 | |||
112 | /** |
||
113 | * Checks whether the two vectors are of the same vector space. |
||
114 | * |
||
115 | * @api |
||
116 | * @param self $b The vector to check against. |
||
117 | * @return bool True if the vectors are the same vector space, false otherwise. |
||
118 | */ |
||
119 | public function isSameVectorSpace(self $b) |
||
123 | |||
124 | /** |
||
125 | * Adds two vectors together. |
||
126 | * |
||
127 | * @api |
||
128 | * @param self $b The vector to add. |
||
129 | * @return self The sum of the two vectors. |
||
130 | * @throws Exception if the vectors are not in the same vector space. |
||
131 | * @see self::_checkVectorSpace() For exception information. |
||
132 | */ |
||
133 | public function add(self $b) |
||
145 | |||
146 | /** |
||
147 | * Subtracts the given vector from this vector. |
||
148 | * |
||
149 | * @api |
||
150 | * @param self $b The vector to subtract from this vector. |
||
151 | * @return self The difference of the two vectors. |
||
152 | * @throws Exception if the vectors are not in the same vector space. |
||
153 | * @see self::_checkVectorSpace() For exception information. |
||
154 | */ |
||
155 | public function subtract(self $b) |
||
159 | |||
160 | /** |
||
161 | * Computes the dot product, or scalar product, of two vectors. |
||
162 | * |
||
163 | * @api |
||
164 | * @param self $b The vector to multiply with. |
||
165 | * @return int|float The dot product of the two vectors. |
||
166 | * @throws Exception if the vectors are not in the same vector space. |
||
167 | * @see self::_checkVectorSpace() For exception information. |
||
168 | */ |
||
169 | public function dotProduct(self $b) |
||
181 | |||
182 | /** |
||
183 | * Computes the cross product, or vector product, of two vectors. |
||
184 | * |
||
185 | * @api |
||
186 | * @param self $b The vector to multiply with. |
||
187 | * @return self The cross product of the two vectors. |
||
188 | * @throws Exception if the vectors are not 3-dimensional. |
||
189 | * @throws Exception if the vectors are not in the same vector space. |
||
190 | * @see self::_checkVectorSpace() For exception information. |
||
191 | */ |
||
192 | public function crossProduct(self $b) |
||
210 | |||
211 | /** |
||
212 | * Computes the scalar triple product of three vectors. |
||
213 | * |
||
214 | * @api |
||
215 | * @param self $b The second vector of the triple product. |
||
216 | * @param self $c The third vector of the triple product. |
||
217 | * @return int|float The scalar triple product of the three vectors. |
||
218 | * @throws Exception if the vectors are not 3-dimensional. |
||
219 | * @throws Exception if the vectors are not in the same vector space. |
||
220 | * @see self::_checkVectorSpace() For exception information. |
||
221 | */ |
||
222 | public function scalarTripleProduct(self $b, self $c) |
||
226 | |||
227 | /** |
||
228 | * Computes the vector triple product of three vectors. |
||
229 | * |
||
230 | * @api |
||
231 | * @param self $b The second vector of the triple product. |
||
232 | * @param self $c The third vector of the triple product. |
||
233 | * @return self The vector triple product of the three vectors. |
||
234 | * @throws Exception if the vectors are not 3-dimensional. |
||
235 | * @throws Exception if the vectors are not in the same vector space. |
||
236 | * @see self::_checkVectorSpace() For exception information. |
||
237 | */ |
||
238 | public function vectorTripleProduct(self $b, self $c) |
||
242 | |||
243 | /** |
||
244 | * Multiplies the vector by the given scalar. |
||
245 | * |
||
246 | * @api |
||
247 | * @param int|float $scalar The real number to multiply by. |
||
248 | * @return self The result of the multiplication. |
||
249 | */ |
||
250 | public function multiplyByScalar($scalar) |
||
259 | |||
260 | /** |
||
261 | * Divides the vector by the given scalar. |
||
262 | * |
||
263 | * @api |
||
264 | * @param int|float $scalar The real number to divide by. |
||
265 | * @return self The result of the division. |
||
266 | * @throws Exception if the $scalar is 0. |
||
267 | */ |
||
268 | public function divideByScalar($scalar) |
||
276 | |||
277 | /** |
||
278 | * Return the normalized vector. |
||
279 | * |
||
280 | * The normalized vector (or unit vector) is the vector with the same |
||
281 | * direction as this vector, but with a length/magnitude of 1. |
||
282 | * |
||
283 | * @api |
||
284 | * @return self The normalized vector. |
||
285 | * @throws Exception if the vector length is zero. |
||
286 | */ |
||
287 | public function normalize() |
||
291 | |||
292 | /** |
||
293 | * Project the vector onto another vector. |
||
294 | * |
||
295 | * @api |
||
296 | * @param self $b The vector to project this vector onto. |
||
297 | * @return self The vector projection of this vector onto $b. |
||
298 | * @throws Exception if the vector length of $b is zero. |
||
299 | * @throws Exception if the vectors are not in the same vector space. |
||
300 | * @see self::_checkVectorSpace() For exception information. |
||
301 | */ |
||
302 | public function projectOnto(self $b) |
||
307 | |||
308 | /** |
||
309 | * Returns the angle between the two vectors. |
||
310 | * |
||
311 | * @api |
||
312 | * @param self $b The vector to compute the angle between. |
||
313 | * @return float The angle between the two vectors in radians. |
||
314 | * @throws Exception if either of the vectors are zero-length. |
||
315 | * @throws Exception if the vectors are not in the same vector space. |
||
316 | * @see self::_checkVectorSpace() For exception information. |
||
317 | */ |
||
318 | public function angleBetween(self $b) |
||
327 | |||
328 | /** |
||
329 | * Checks that the vector spaces of the two vectors are the same. |
||
330 | * |
||
331 | * The vectors must be of the same dimension and have the same keys in their |
||
332 | * components. |
||
333 | * |
||
334 | * @internal |
||
335 | * @param self $b The vector to check against. |
||
336 | * @return void |
||
337 | * @throws Exception if the vectors are not of the same dimension. |
||
338 | * @throws Exception if the vectors' components down have the same keys. |
||
339 | */ |
||
340 | protected function _checkVectorSpace(self $b) |
||
350 | } |
||
351 |