1. Go to this page and download the library: Download nubs/vectorix library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
nubs / vectorix example snippets
$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)
// 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)
/**
* @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)
$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)
$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)
$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)
/**
* @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)
/**
* @param int|float $scalar The real number to multiply by.
* @return self The result of the multiplication.
*/
public function multiplyByScalar($scalar)
/**
* @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)
/**
* @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)
$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)
/**
* @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)
/*
/**
* @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)
$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)
/**
* @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)
$a = new \Nubs\Vectorix\Vector(array(0, 5));
$b = new \Nubs\Vectorix\Vector(array(3, 3));
var_dump($a->angleBetween($b));
// double(0.78539816339745)
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.