phpmath / vector
A PHP library to work with immutable vectors.
Requires
- php: ^8.4
Requires (Dev)
- phpmath/coding-standard: ^2.0
- phpstan/phpstan: ^2.3
- phpunit/phpunit: ^13.4
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-24 12:59:52 UTC
README
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,Vector3andVector4.- Adding, subtracting, scaling and negating .
- Dot product, cross product (
Vector3) and the angle between two vectors. - Length, squared length and normalization.
Vector4for homogeneous coordinates, withfromVector3(),toVector3()andproject().- Replacing a component with
withElement()(withX(),withY()andwithZ()), 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.