Introduction

Complex numbers for PHP. Every number is an immutable value object with a float real and a float imaginary part.

Requirements#

The library needs a current PHP version. It has no runtime dependencies and no extensions are required.

Installation#

Install the library with Composer.

composer require phpmath/complex

A first example#

A complex number has a real part and an imaginary part. You write it as a+bi, where i is the imaginary unit (the number whose square is -1). Create one with Complex::of(), then combine numbers with methods such as add() and multiply().

use PHPMath\Complex\Complex;

$a = Complex::of(3, 4);
$b = Complex::of(1, -2);

echo $a->add($b);      // 4+2i
echo $a->multiply($b); // 11-2i
echo $a->modulus();    // 5

Calling add() did not change $a. It returned a new Complex. This is true for every operation in the library.

The parts are PHP floats, so results follow normal float rules. They can be slightly inexact, and a very large result can become INF or NAN. See Core concepts.

Where to go next#

Start with the core concepts, or jump straight to the topic you need.