PHPackages                             nubs/vectorix - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. nubs/vectorix

ActiveLibrary

nubs/vectorix
=============

A vector library.

v1.1.0(11y ago)162.4k11MITPHPPHP &gt;=5.3.2

Since May 3Pushed 9y ago3 watchersCompare

[ Source](https://github.com/nubs/vectorix)[ Packagist](https://packagist.org/packages/nubs/vectorix)[ RSS](/packages/nubs-vectorix/feed)WikiDiscussions master Synced today

READMEChangelog (2)Dependencies (1)Versions (3)Used By (1)

vectorix
========

[](#vectorix)

A PHP vector library.

[![Build Status](https://camo.githubusercontent.com/a0f0f617163de073d9ad27b390e0207c273d865b0474291fb7de8e8309e13a15/687474703a2f2f696d672e736869656c64732e696f2f7472617669732f6e7562732f766563746f7269782e7376673f7374796c653d666c6174)](https://travis-ci.org/nubs/vectorix)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/0402f92780097f45bf9955f26ef692b43652261a316a15031ac2b5796b4ffbaa/687474703a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6e7562732f766563746f7269782e7376673f7374796c653d666c6174)](https://scrutinizer-ci.com/g/nubs/vectorix/)[![Code Coverage](https://camo.githubusercontent.com/14d4292033b28f04b6a00e62c7db7841ba23eaf9bb560b2a0f1af8d2951546e1/687474703a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f6e7562732f766563746f7269782e7376673f7374796c653d666c6174)](https://coveralls.io/r/nubs/vectorix)

[![Latest Stable Version](https://camo.githubusercontent.com/8dda3df542daacad36269040a4f595c2320f61509ec30bec7108873ad1526173/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6e7562732f766563746f7269782e7376673f7374796c653d666c6174)](https://packagist.org/packages/nubs/vectorix)[![Total Downloads](https://camo.githubusercontent.com/9c4d5b645077003fa27e36adba410d6b7639b39bff1ed738180f2e2c1ca66a9d/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6e7562732f766563746f7269782e7376673f7374796c653d666c6174)](https://packagist.org/packages/nubs/vectorix)[![License](https://camo.githubusercontent.com/b9251bfcf7d16a67179b88ba51cd9e7f588ada7900e29169d4e1ec0ee26c503b/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6e7562732f766563746f7269782e7376673f7374796c653d666c6174)](https://packagist.org/packages/nubs/vectorix)

[![Dependency Status](https://camo.githubusercontent.com/1062d84ec2c4027dc715eb6c946bc7198387e56f7f4a53e204d8d0b4cb95a985/68747470733a2f2f7777772e76657273696f6e6579652e636f6d2f757365722f70726f6a656374732f3533346562333462666530643037316330353030303761632f62616467652e7376673f7374796c653d666c6174)](https://www.versioneye.com/user/projects/534eb34bfe0d071c050007ac)

Requirements
------------

[](#requirements)

This library requires PHP 5.6, or newer.

Installation
------------

[](#installation)

This package uses [composer](https://getcomposer.org) so you can just add `nubs/vectorix` as a dependency to your `composer.json` file or execute the following command:

```
composer require nubs/vectorix
```

Vector
------

[](#vector)

The [`Vector`](src/Vector.php) class represents an immutable [Euclidean vector](http://en.wikipedia.org/wiki/Euclidean_vector) and its associated operations.

All operations on the vector will return a new vector with the results. For example,

```
$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]);
```

The keys of a vector's components are preserved through operations. Because of this, vectors MUST have the same keys in order to use them together. For example,

```
$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'
```

### Creating a Vector

[](#creating-a-vector)

#### Constructor

[](#constructor)

```
/**
 * @param array $components The components of the vector.
 */
public function __construct(array $components)
```

The primary method for creating a vector is, of course, the constructor. It takes an array of the components of the vector (e.g., *x*, *y*, and *z*components). Components can be integers, floats, or a combination thereof.

```
// 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]);
```

#### Null Vectors

[](#null-vectors)

```
/**
 * @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)
```

When needing a [null vector](http://en.wikipedia.org/wiki/Null_vector) (a vector with zero magnitude), this static method makes creating one easy. All of its components will be initialized to the integer `0`.

```
$a = \Nubs\Vectorix\Vector::nullVector(3);
var_dump($a->components());
// array(3) {
//   [0] =>
//   int(0)
//   [1] =>
//   int(0)
//   [2] =>
//   int(0)
// }
```

### Properties of a Vector

[](#properties-of-a-vector)

#### Components

[](#components)

```
/**
 * @return array The components of the vector.
 */
public function components()
```

The `components` method returns the components of the vector with keys kept intact.

```
$a = new \Nubs\Vectorix\Vector([7, 4]);
var_dump($a->components());
// array(2) {
//   [0] =>
//   int(7)
//   [1] =>
//   int(4)
// }
```

#### Dimension

[](#dimension)

```
/**
 * @return int The dimension/cardinality of the vector.
 */
public function dimension()
```

The `dimension` of a vector is the number of components in it. This is also referred to as "cardinality".

```
$a = new \Nubs\Vectorix\Vector([5.2, 1.4]);
var_dump($a->dimension());
// int(2)
```

#### Length

[](#length)

```
/**
 * @return float The length/magnitude of the vector.
 */
public function length()
```

The `length`, or [magnitude](http://en.wikipedia.org/wiki/Magnitude_%28mathematics%29) of a vector is the distance from the origin to the point described by the vector.

It is always returned as a floating point number.

```
$a = new \Nubs\Vectorix\Vector([3, 4]);
var_dump($a->length());
// double(5)
```

### Tests

[](#tests)

#### Equality

[](#equality)

```
/**
 * @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)
```

The `isEqual` method tests to see if the two vectors are equal. They are only equal if their components are identical (including same keys).

```
$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)
```

#### Same Dimension

[](#same-dimension)

```
/**
 * @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)
```

The `isSameDimension` method tests to see if the two vectors both have the same dimension.

```
$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)
```

#### Same Vector Space

[](#same-vector-space)

```
/**
 * @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)
```

The `isSameVectorSpace` method tests to see if the two vectors both belong to the same vector space.

The vector space is defined in this library by the dimension of the vectors, and the keys of the vectors' components.

```
$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)
```

### Basic Operations

[](#basic-operations)

#### Addition

[](#addition)

```
/**
 * @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)
```

The `add` method performs vector [addition](http://en.wikipedia.org/wiki/Euclidean_vector#Addition_and_subtraction). The two vectors must belong to the same vector space.

The result is a new vector where each component is the sum of the corresponding components in the two vectors.

```
$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)
// }
```

#### Subtraction

[](#subtraction)

```
/**
 * @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)
```

The `subtract` method performs vector [subtraction](http://en.wikipedia.org/wiki/Euclidean_vector#Addition_and_subtraction). The two vectors must belong to the same vector space.

The result is a new vector where each component is the difference of the corresponding components in the two vectors

```
$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)
// }
```

#### Scalar Multiplication

[](#scalar-multiplication)

```
/**
 * @param int|float $scalar The real number to multiply by.
 * @return self The result of the multiplication.
 */
public function multiplyByScalar($scalar)
```

The `multiplyByScalar` function performs [scalar multiplication](http://en.wikipedia.org/wiki/Euclidean_vector#Scalar_multiplication)of a vector with a scalar value.

The result is a new vector where each component is the multiplication of that component with the scalar value.

```
$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)
// }
```

#### Scalar Division

[](#scalar-division)

```
/**
 * @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)
```

The `divideByScalar` function performs scalar division of a vector with a scalar value. This is the same as multiplying the vector by `1 / scalarValue`.

Trying to divide by zero will throw an exception.

The result is a new vector where each component is the division of that component with the scalar value.

```
$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)
// }
```

#### Dot Product

[](#dot-product)

```
/**
 * @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)
```

The `dotProduct` method performs a [dot product](http://en.wikipedia.org/wiki/Dot_product) between two vectors. The two vectors must belong to the same vector space.

```
$a = new \Nubs\Vectorix\Vector([1, 3, -5]);
$b = new \Nubs\Vectorix\Vector([4, -2, -1]);
var_dump($a->dotProduct($b));
// int(3)
```

#### Cross Product

[](#cross-product)

```
/**
 * @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)
```

The `crossProduct` method computes the [cross product](http://en.wikipedia.org/wiki/Cross_product) between two three-dimensional vectors. The resulting vector is perpendicular to the plane containing the two vectors.

```
$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)
// }
```

### Other Operations

[](#other-operations)

#### Normalization

[](#normalization)

```
/**
 * @return self The normalized vector.
 * @throws Exception if the vector length is zero.
 */
public function normalize()
```

The `normalize` method returns the [unit vector](http://en.wikipedia.org/wiki/Unit_vector) with the same direction as the original vector.

```
$a = new \Nubs\Vectorix\Vector([3, 3]);
$b = $a->normalize();
var_dump($b->components());
// array(2) {
//   [0] =>
//   double(0.70710678118655)
//   [1] =>
//   double(0.70710678118655)
// }
```

#### Projection

[](#projection)

```
/**
 * @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)
/*
```

The `projectOnto` method computes the [vector projection](http://en.wikipedia.org/wiki/Vector_projection) of one vector onto another. The resulting vector will be colinear with `$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)
// }
```

#### Scalar Triple Product

[](#scalar-triple-product)

```
/**
 * @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)
```

The `scalarTripleProduct` method computes the [scalar triple product](http://en.wikipedia.org/wiki/Triple_product#Scalar_triple_product). This value represents the volume of the parallelepiped defined by the three vectors.

```
$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)
```

#### Vector Triple Product

[](#vector-triple-product)

```
/**
 * @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)
```

The `vectorTripleProduct` method computes the [vector triple product](http://en.wikipedia.org/wiki/Triple_product#Vector_triple_product).

```
$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)
// }
```

#### Angle Between Vectors

[](#angle-between-vectors)

```
/**
 * @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)
```

The `angleBetween` method computes the angle between two vectors in radians.

```
$a = new \Nubs\Vectorix\Vector(array(0, 5));
$b = new \Nubs\Vectorix\Vector(array(3, 3));
var_dump($a->angleBetween($b));
// double(0.78539816339745)
```

License
-------

[](#license)

vectorix is licensed under the MIT license. See [LICENSE](LICENSE) for the full license text.

###  Health Score

32

—

LowBetter than 74% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~2 days

Total

2

Last Release

4338d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/43e7568db49cb140fe4deef1e585de0874e2a962a1140662003a5c070f536879?d=identicon)[nubs](/maintainers/nubs)

---

Top Contributors

[![nubs](https://avatars.githubusercontent.com/u/57673?v=4)](https://github.com/nubs "nubs (55 commits)")

---

Tags

vectormath

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/nubs-vectorix/health.svg)

```
[![Health](https://phpackages.com/badges/nubs-vectorix/health.svg)](https://phpackages.com/packages/nubs-vectorix)
```

###  Alternatives

[brick/math

Arbitrary-precision arithmetic library

2.1k490.5M217](/packages/brick-math)[markbaker/matrix

PHP Class for working with matrices

1.5k272.5M34](/packages/markbaker-matrix)[markrogoyski/math-php

Math Library for PHP. Features descriptive statistics and regressions; Continuous and discrete probability distributions; Linear algebra with matrices and vectors, Numerical analysis; special mathematical functions; Algebra

2.4k6.9M36](/packages/markrogoyski-math-php)[rubix/tensor

A library and extension that provides objects for scientific computing in PHP.

2691.4M5](/packages/rubix-tensor)[phpseclib/bcmath_compat

PHP 5.x-8.x polyfill for bcmath extension

16620.4M11](/packages/phpseclib-bcmath-compat)[nxp/math-executor

Simple math expressions calculator

2271.7M4](/packages/nxp-math-executor)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
