Vector class provides constructor to instantiate arbitrary vector and incapsulates common linear algebra methods to interact with it.
It implements irrevion\science\Math\Entities\Entity
, \Iterator
, \ArrayAccess
, \Countable
interfaces as it can be seen by its definition:
<?php
namespace irrevion\science\Math\Entities;
use irrevion\science\Math\Math;
use irrevion\science\Helpers\Delegator;
class Vector extends Scalar implements Entity, \Iterator, \ArrayAccess, \Countable {}
Yep, it extends Scalar, because thats what the single-dimensional vector looks like, just good old arrow on the real number line and any n-dimensional vector behave just like number for any other collinear vector.
use irrevion\science\Math\Entities\{Scalar, Vector};
$x = new Vector([2, 3, 5]);
Here is Vector class constructor’s signature:
public function __construct($array=[], $type=self::T_SCALAR, $pad_to_length=0) {}
So, you can pass in array of values and values will be mapped as objects of type denoted by second parameter. You can also pad it with zero values using third parameter.
If instead of array an Entity is passed, constructor will try to transform it to array using $array->toArray()
method. Otherwise if Entity have no such method, it will be treated as only element of array.
There are methods for addition and subtraction of vectors:
$v1 = new Vector([1, 2, 3]);
$v2 = new Vector([4, 5, 6]);
$sum = $v1->add($v2); // result is [5, 7, 9]
$diff = $v1->subtract($v2); // result is [-3, -3, -3]
Vector multiplication and division methods are also available:
$prod = $v1->multiply($v2); // element-wise multiplication, result is [4, 10, 18]
$quotient = $v1->divide($v2); // element-wise division, result is [0.25, 0.4, 0.5]
You can calculate the dot product of two vectors:
$dotProduct = $v1->dot($v2); // result is 32
You can calculate the cross product of two vectors (only for 3-dimensional vectors):
$v1 = new Vector([1, 2, 3]);
$v2 = new Vector([4, 5, 6]);
$crossProduct = $v1->cross($v2); // result is [-3, 6, -3]
You can calculate the magnitude (length) of a vector:
$magnitude = $v1->abs(); // result is 3.7416573867739
You can normalize a vector (make it a unit vector):
$normalized = $v1->normalize(); // result is [0.26726124191242, 0.53452248382485, 0.80178372573727]