PHP classes for Complex and Rational number arithmetic, plus Vector and Matrix operations.

Maintainers

Package info

github.com/mossy2100/PHP-Math

pkg:composer/oceanmoon/math

Transparency log

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 1

Open Issues: 0

v3.0.0 2026-07-08 23:05 UTC

This package is auto-updated.

Last update: 2026-08-16 03:48:46 UTC


README

Provides classes for Complex numbers, Rational numbers, Vectors, and Matrices.

License | Changelog | Documentation

PHP 8.4

Description

This package provides classes for working with complex numbers, rational numbers, vectors, and matrices in PHP.

Key Features:

  • Complex numbers - Complex arithmetic, trigonometry, transcendental functions, polar/rectangular conversions, and conversion to/from strings.
  • Rational numbers - Exact fraction arithmetic using integer ratios, automatic simplification, overflow detection, and conversion to/from floats and strings.
  • Vectors - Element-wise arithmetic, dot and cross products, array-style access, and conversion to/from arrays.
  • Matrices - Matrix arithmetic, inverse, determinant, transpose, power, and matrix-vector multiplication.

The salient features of the package include:

  • Careful attention to precision, efficiency, usefulness, clear documentation, and coding standards.
  • Seamless interoperation with PHP int, float, string, and array types.
  • A fluent API that enables expressive operations.
  • Expressive exception types and messages.
  • Comprehensive tests providing 100% code coverage.

Development and Quality Assurance

Claude Chat and Claude Code were used in the development of this package. The core classes were designed, coded, and commented primarily by the author, with Claude providing substantial assistance with code review, suggesting improvements, debugging, and generating tests and documentation. All code was thoroughly reviewed by the author, and validated using industry-standard tools including PHP_Codesniffer, PHPStan (to level 9), and PHPUnit to ensure full compliance with PSR-12 coding standards and comprehensive unit testing with 100% code coverage. This collaborative approach has produced a well-designed, production-ready package with thorough test coverage and documentation.

Code Coverage

Requirements

  • PHP ^8.4
  • oceanmoon/core

Installation

composer require oceanmoon/math

Strict Typing

Strict typing (declare(strict_types=1)) is used throughout the library, with type hints on every property, parameter, and return type - this catches type errors at the call site instead of them surfacing as subtle bugs later.

One PHP-level exception applies: an int is always accepted where a float is type-hinted, even under strict types, since PHP treats this as lossless "widening" (the reverse - passing a float where int is expected - is not allowed, and throws a TypeError). Many methods here are typed float for exactly this reason, so both forms work:

$v = Vector::fromArray([1, 2, 3]);
$v->mul(3);      // int - accepted, widens to 3.0
$v->mul(3.0);    // float - also fine

See the "Strict typing" section of the PHP manual's Type Declarations page for the full rules.

Classes

Complex

Immutable class for complex numbers (a + bi) with support for:

  • Basic arithmetic operations (add, subtract, multiply, divide)
  • Transcendental functions (exp, ln, log, pow, roots)
  • Trigonometric and hyperbolic functions (sin, cos, tan, asin, acos, atan, etc.)
  • Polar and rectangular form conversions
  • Epsilon-based equality comparison
  • String parsing and formatting

Rational

Immutable class for rational numbers (p/q) with support for:

  • Exact arithmetic using integer ratios (no floating-point errors)
  • Automatic reduction to simplest form (e.g., 6/8 → 3/4)
  • A dedicated fromFloat() method for approximate conversion from floats using continued fractions
  • Overflow-safe integer operations
  • Comparison operations with mixed types
  • String parsing and formatting

Vector

Mutable numeric vector with support for:

  • Element-wise arithmetic (add, subtract, scalar multiply, scalar divide)
  • Dot, cross, Hadamard, and outer product operations
  • Exact and approximate equality comparison
  • Conversion to arrays and matrices
  • Array-style element access via the ArrayAccess interface
  • String representation using mathematical angle brackets

Matrix

Mutable two-dimensional matrix with support for:

  • Matrix arithmetic (add, subtract, multiply, divide)
  • Matrix-vector multiplication using column vector convention
  • Transpose, determinant, and inverse operations
  • Matrix power with binary exponentiation (including negative powers)
  • Row-level ArrayAccess interface (get/set rows as Vectors)
  • String representation using box-drawing characters

Globals

M_I, the imaginary unit constant (namespaced to OceanMoon\Math), which is more convenient to use a global than as a class member.

Testing

The library includes comprehensive test coverage:

# Run all tests
vendor/bin/phpunit

# Run tests for specific class
vendor/bin/phpunit tests/Complex
vendor/bin/phpunit tests/Rational
vendor/bin/phpunit tests/Vector
vendor/bin/phpunit tests/Matrix

# Run with coverage (generates HTML report and clover.xml)
composer test

License

MIT License - see LICENSE for details

Support

For questions or suggestions, please open an issue.

Changelog

See CHANGELOG.md for version history and changes.