毛刺 / 向量ix
一个向量库。
Requires
- php: >=5.3.2
Requires (Dev)
- phpunit/phpunit: ~4.0
This package is auto-updated.
Last update: 2024-09-12 21:21:38 UTC
README
一个PHP向量库。
需求
此库需要PHP 5.6或更高版本。
安装
此包使用composer,因此您可以将nubs/vectorix
添加到您的composer.json
文件中作为依赖项,或者执行以下命令
composer require nubs/vectorix
向量
对向量的所有操作都将返回一个包含结果的新向量。例如,
$a = new \Nubs\Vectorix\Vector([1, 5]); $b = $a->multiplyByScalar(2); // $a is not changed. Once a vector is created, it is immutable. assert($a->components() === [1, 5]); // Results of operations (like multiplyByScalar) are returned where they can be // used. assert($b->components() === [2, 10]);
向量的组件键在操作过程中被保留。因此,向量必须具有相同的键才能一起使用。例如,
$a = new \Nubs\Vectorix\Vector(['i' => 5, 'j' => 9]); $b = new \Nubs\Vectorix\Vector(['i' => 1, 'j' => 2]); $c = $a->add($b); var_dump($c->components()); // array(2) { // 'i' => // int(6) // 'j' => // int(11) // } $d = new \Nubs\Vectorix\Vector([5, 9]); $e = $c->subtract($d); // PHP Fatal error: Uncaught exception 'Exception' with message 'The vectors' // components must have the same keys'
创建向量
构造函数
/** * @param array<int|float> $components The components of the vector. */ public function __construct(array $components)
创建向量的主要方法是构造函数。它需要一个包含向量组件(例如,x、y和z组件)的数组。组件可以是整数、浮点数或它们的组合。
// Create a 3-dimensional vector. $a = new \Nubs\Vectorix\Vector([2, 3, -1]); // Create a 2-dimension vector with named components. $b = new \Nubs\Vectorix\Vector(['x' => 1.7, 'y' => -5.3]);
零向量
/** * @param int $dimension The dimension of the vector to create. Must be at least 0. * @return self The zero-length vector for the given dimension. * @throws Exception if the dimension is less than zero. */ public static function nullVector($dimension)
当需要零向量(一个具有零模量的向量)时,这个静态方法使得创建一个变得容易。它的所有组件都将初始化为整数0
。
$a = \Nubs\Vectorix\Vector::nullVector(3); var_dump($a->components()); // array(3) { // [0] => // int(0) // [1] => // int(0) // [2] => // int(0) // }
向量的属性
组件
/** * @return array<int|float> The components of the vector. */ public function components()
components
方法返回向量的组件,键保持不变。
$a = new \Nubs\Vectorix\Vector([7, 4]); var_dump($a->components()); // array(2) { // [0] => // int(7) // [1] => // int(4) // }
维度
/** * @return int The dimension/cardinality of the vector. */ public function dimension()
向量的维度是其中的组件数量。这通常也称为“基数”。
$a = new \Nubs\Vectorix\Vector([5.2, 1.4]); var_dump($a->dimension()); // int(2)
长度
/** * @return float The length/magnitude of the vector. */ public function length()
向量的长度或大小是从原点到由向量描述的点的距离。
它总是以浮点数返回。
$a = new \Nubs\Vectorix\Vector([3, 4]); var_dump($a->length()); // double(5)
测试
相等性
/** * @param self $b The vector to check for equality. * @return bool True if the vectors are equal and false otherwise. */ public function isEqual(self $b)
isEqual
方法检查两个向量是否相等。只有当它们的组件完全相同(包括相同的键)时,它们才相等。
$a = new \Nubs\Vectorix\Vector([1, 2]); $b = new \Nubs\Vectorix\Vector([1, 2]); $c = new \Nubs\Vectorix\Vector([5, 7]); var_dump($a->isEqual($b)); // bool(true) var_dump($a->isEqual($c)); // bool(false)
相同维度
/** * @param self $b The vector to check against. * @return bool True if the vectors are of the same dimension, false otherwise. */ public function isSameDimension(self $b)
isSameDimension
方法检查两个向量是否都具有相同的维度。
$a = new \Nubs\Vectorix\Vector([1, 2]); $b = new \Nubs\Vectorix\Vector([5, 1]); $c = new \Nubs\Vectorix\Vector([5, 8, 2]); var_dump($a->isSameDimension($b)); // bool(true) var_dump($a->isSameDimension($c)); // bool(false)
相同向量空间
/** * @param self $b The vector to check against. * @return bool True if the vectors are the same vector space, false otherwise. */ public function isSameVectorSpace(self $b)
isSameVectorSpace
方法检查两个向量是否都属于同一个向量空间。
向量空间在此库中由向量的维度和向量组件的键定义。
$a = new \Nubs\Vectorix\Vector([1, 2]); $b = new \Nubs\Vectorix\Vector([5, 1]); $c = new \Nubs\Vectorix\Vector([2, 1, 7]); $d = new \Nubs\Vectorix\Vector(['x' => 3, 'y' => 2]); var_dump($a->isSameVectorSpace($b)); // bool(true) var_dump($a->isSameVectorSpace($c)); // bool(false) var_dump($a->isSameVectorSpace($d)); // bool(false)
基本操作
加法
/** * @param self $b The vector to add. * @return self The sum of the two vectors. * @throws Exception if the vectors are not in the same vector space. */ public function add(self $b)
add
方法执行向量加法。两个向量必须属于同一个向量空间。
结果是每个组件都是两个向量中相应组件之和的新向量。
$a = new \Nubs\Vectorix\Vector([7, -2]); $b = new \Nubs\Vectorix\Vector([-1, 5]); $c = $a->add($b); var_dump($c->components()); // array(2) { // [0] => // int(6) // [1] => // int(3) // }
减法
/** * @param self $b The vector to subtract from this vector. * @return self The difference of the two vectors. * @throws Exception if the vectors are not in the same vector space. */ public function subtract(self $b)
subtract
方法执行向量减法。两个向量必须属于同一个向量空间。
结果是每个组件都是两个向量中相应组件之差的新向量。
$a = new \Nubs\Vectorix\Vector([5, 7]); $b = new \Nubs\Vectorix\Vector([-1, 6]); $c = $a->subtract($b); var_dump($c->components()); // array(2) { // [0] => // int(6) // [1] => // int(1) // }
标量乘法
/** * @param int|float $scalar The real number to multiply by. * @return self The result of the multiplication. */ public function multiplyByScalar($scalar)
multiplyByScalar
函数对向量执行数乘。
结果是新的向量,其中每个分量是该分量与标量值的乘积。
$a = new \Nubs\Vectorix\Vector([2, 8, -1]); $b = 5; $c = $a->multiplyByScalar($b); var_dump($c->components()); // array(3) { // [0] => // int(10) // [1] => // int(40) // [2] => // int(-5) // }
标量除法
/** * @param int|float $scalar The real number to divide by. * @return self The result of the division. * @throws Exception if the $scalar is 0. */ public function divideByScalar($scalar)
divideByScalar
函数对向量执行标量除法。这与将向量乘以1 / scalarValue
相同。
尝试除以零将抛出异常。
结果是新的向量,其中每个分量是该分量与标量值的除法。
$a = new \Nubs\Vectorix\Vector([4, 12, -8]); $b = 2; $c = $a->divideByScalar($b); var_dump($c->components()); // array(3) { // [0] => // double(2) // [1] => // double(6) // [2] => // double(-4) // }
点积
/** * @param self $b The vector to multiply with. * @return int|float The dot product of the two vectors. * @throws Exception if the vectors are not in the same vector space. */ public function dotProduct(self $b)
dotProduct
方法执行两个向量的点积。这两个向量必须属于同一个向量空间。
$a = new \Nubs\Vectorix\Vector([1, 3, -5]); $b = new \Nubs\Vectorix\Vector([4, -2, -1]); var_dump($a->dotProduct($b)); // int(3)
叉积
/** * @param self $b The vector to multiply with. * @return self The cross product of the two vectors. * @throws Exception if the vectors are not 3-dimensional. * @throws Exception if the vectors are not in the same vector space. */ public function crossProduct(self $b)
crossProduct
方法计算两个三维向量的叉积。结果向量垂直于包含两个向量的平面。
$a = new \Nubs\Vectorix\Vector([2, 3, 4]); $b = new \Nubs\Vectorix\Vector([5, 6, 7]); $c = $a->crossProduct($b); var_dump($c->components()); // array(3) { // [0] => // int(-3) // [1] => // int(6) // [2] => // int(-3) // }
其他操作
归一化
/** * @return self The normalized vector. * @throws Exception if the vector length is zero. */ public function normalize()
normalize
方法返回与原始向量具有相同方向的单位向量。
$a = new \Nubs\Vectorix\Vector([3, 3]); $b = $a->normalize(); var_dump($b->components()); // array(2) { // [0] => // double(0.70710678118655) // [1] => // double(0.70710678118655) // }
投影
/** * @param self $b The vector to project this vector onto. * @return self The vector projection of this vector onto $b. * @throws Exception if the vector length of $b is zero. * @throws Exception if the vectors are not in the same vector space. */ public function projectOnto(self $b) /*
projectOnto
方法计算一个向量在另一个向量上的向量投影。结果向量将与$b
共线。
$a = new \Nubs\Vectorix\Vector([4, 0]); $b = new \Nubs\Vectorix\Vector([3, 3]); $c = $a->projectOnto($b); var_dump($c->components()); // array(2) { // [0] => // double(2) // [1] => // double(2) // }
标量三重积
/** * @param self $b The second vector of the triple product. * @param self $c The third vector of the triple product. * @return int|float The scalar triple product of the three vectors. * @throws Exception if the vectors are not 3-dimensional. * @throws Exception if the vectors are not in the same vector space. */ public function scalarTripleProduct(self $b, self $c)
scalarTripleProduct
方法计算标量三重积。此值表示由三个向量定义的平行六面体的体积。
$a = new \Nubs\Vectorix\Vector([-2, 3, 1]); $b = new \Nubs\Vectorix\Vector([0, 4, 0]); $c = new \Nubs\Vectorix\Vector([-1, 3, 3]); var_dump($a->scalarTripleProduct($b, $c)); // int(-20)
向量三重积
/** * @param self $b The second vector of the triple product. * @param self $c The third vector of the triple product. * @return self The vector triple product of the three vectors. * @throws Exception if the vectors are not 3-dimensional. * @throws Exception if the vectors are not in the same vector space. */ public function vectorTripleProduct(self $b, self $c)
vectorTripleProduct
方法计算向量三重积。
$a = new \Nubs\Vectorix\Vector([-2, 3, 1]); $b = new \Nubs\Vectorix\Vector([0, 4, 0]); $c = new \Nubs\Vectorix\Vector([-1, 3, 3]); $d = $a->vectorTripleProduct($b, $c); var_dump($d->components()); // array(3) { // [0] => // int(12) // [1] => // int(20) // [2] => // int(-36) // }
向量间夹角
/** * @param self $b The vector to compute the angle between. * @return float The angle between the two vectors in radians. * @throws Exception if either of the vectors are zero-length. * @throws Exception if the vectors are not in the same vector space. */ public function angleBetween(self $b)
angleBetween
方法计算两个向量之间的角度(以弧度为单位)。
$a = new \Nubs\Vectorix\Vector(array(0, 5)); $b = new \Nubs\Vectorix\Vector(array(3, 3)); var_dump($a->angleBetween($b)); // double(0.78539816339745)
许可
vectorix遵循MIT许可协议。有关完整许可文本,请参阅LICENSE。