Search by

phpmath / vector

waltertamboer

A PHP library to work with immutable vectors.

Package info

github.com/phpmath/vector

pkg:composer/phpmath/vector

Statistics

Installs: 56

Dependents: 1

Suggesters: 0

Stars: 3

Open Issues: 1

2.0.0 2026-11-23 11:30 UTC

This package is auto-updated.

Last update: 2026-11-24 12:59:52 UTC


README

Latest Version on Packagist Software License CI Total Downloads

A PHP library to work with mathematical vectors.

Documentation

The full documentation is at https://phpmath.github.io/vector/. Every change is listed in the changelog and every release on the releases page.

Install

composer require phpmath/vector

Requirements

  • PHP 8.4 or newer

Usage

Every vector is an immutable value object. Operations never change the instance they are called on, they return a new instance.

use PHPMath\Vector\Vector;
use PHPMath\Vector\Vector3;

$a = new Vector3(1, 0, 0);
$b = new Vector3(0, 1, 0);

$cross = $a->crossProduct($b); // Vector3 [0, 0, 1]
$sum = $a->add($b);            // Vector3 [1, 1, 0]
$angle = $a->angleBetween($b); // pi / 2

$vector = new Vector([3, 4]);
echo $vector->getLength();     // 5

Features

  • Vector (any number of components), Vector2, Vector3 and Vector4.
  • Adding, subtracting, scaling and negating .
  • Dot product, cross product (Vector3) and the angle between two vectors.
  • Length, squared length and normalization.
  • Vector4 for homogeneous coordinates, with fromVector3(), toVector3() and project().
  • Replacing a component with withElement() (withX(), withY() and withZ()), which returns a copy.

Development

composer test
composer test-coverage
composer analyse
composer check-style
composer fix-style

Contributing

All contributions are welcome. Open an issue or a pull request on GitHub. Please follow the coding standard and add tests.

Security

If you discover a security issue, please create an issue in the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.