PHPackages                             markbaker/complex - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. markbaker/complex

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

markbaker/complex
=================

PHP Class for working with complex numbers

3.0.2(3y ago)1.7k283.1M—5.7%12[1 issues](https://github.com/MarkBaker/PHPComplex/issues)[3 PRs](https://github.com/MarkBaker/PHPComplex/pulls)20MITPHPPHP ^7.2 || ^8.0CI failing

Since Mar 18Pushed 3y ago6 watchersCompare

[ Source](https://github.com/MarkBaker/PHPComplex)[ Packagist](https://packagist.org/packages/markbaker/complex)[ Docs](https://github.com/MarkBaker/PHPComplex)[ RSS](/packages/markbaker-complex/feed)WikiDiscussions 3.0 Synced 1mo ago

READMEChangelog (4)Dependencies (4)Versions (28)Used By (20)

PHPComplex
==========

[](#phpcomplex)

---

PHP Class Library for working with Complex numbers

[![Build Status](https://github.com/MarkBaker/PHPComplex/workflows/main/badge.svg)](https://github.com/MarkBaker/PHPComplex/actions)[![Total Downloads](https://camo.githubusercontent.com/f01d0c06c2af1b90f15bcb0b900f99ac09fab15dd8f553a59e10ed611e5a4306/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d61726b62616b65722f636f6d706c6578)](https://packagist.org/packages/markbaker/complex)[![Latest Stable Version](https://camo.githubusercontent.com/8559ab75320cf22d47abee39e7dac9c329ca3121de1ae3c39041ddf140a781ca/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f4d61726b42616b65722f504850436f6d706c6578)](https://packagist.org/packages/markbaker/complex)[![License](https://camo.githubusercontent.com/b321485d7c997e6227c934bae684bf85bf906e74a7277250593d6b46eb249353/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f4d61726b42616b65722f504850436f6d706c6578)](https://packagist.org/packages/markbaker/complex)

[![Complex Numbers](https://camo.githubusercontent.com/2bbee9afe0d3ef53e31f62819febb5c3fdce2aef5d8cfa4a0c1b038ec2ab38fc/68747470733a2f2f696d67732e786b63642e636f6d2f636f6d6963732f636f6d706c65785f6e756d626572735f32782e706e67)](https://xkcd.com/2028/)

---

The library currently provides the following operations:

- addition
- subtraction
- multiplication
- division
    - division by
    - division into

together with functions for

- theta (polar theta angle)
- rho (polar distance/radius)
- conjugate

- negative

- inverse (1 / complex)
- cos (cosine)
- acos (inverse cosine)
- cosh (hyperbolic cosine)
- acosh (inverse hyperbolic cosine)
- sin (sine)
- asin (inverse sine)
- sinh (hyperbolic sine)
- asinh (inverse hyperbolic sine)
- sec (secant)
- asec (inverse secant)
- sech (hyperbolic secant)
- asech (inverse hyperbolic secant)
- csc (cosecant)
- acsc (inverse cosecant)
- csch (hyperbolic secant)
- acsch (inverse hyperbolic secant)
- tan (tangent)
- atan (inverse tangent)
- tanh (hyperbolic tangent)
- atanh (inverse hyperbolic tangent)
- cot (cotangent)
- acot (inverse cotangent)
- coth (hyperbolic cotangent)
- acoth (inverse hyperbolic cotangent)
- sqrt (square root)
- exp (exponential)
- ln (natural log)
- log10 (base-10 log)
- log2 (base-2 log)
- pow (raised to the power of a real number)

---

Installation
============

[](#installation)

```
composer require markbaker/complex:^1.0
```

Important BC Note
=================

[](#important-bc-note)

If you've previously been using procedural calls to functions and operations using this library, then from version 3.0 you should use [MarkBaker/PHPComplexFunctions](https://github.com/MarkBaker/PHPComplexFunctions) instead (available on packagist as [markbaker/complex-functions](https://packagist.org/packages/markbaker/complex-functions)).

You'll need to replace `markbaker/complex`in your `composer.json` file with the new library, but otherwise there should be no difference in the namespacing, or in the way that you have called the Complex functions in the past, so no actual code changes are required.

```
composer require markbaker/complex-functions:^3.0
```

You should not reference this library (`markbaker/complex`) in your `composer.json`, composer wil take care of that for you.

Usage
=====

[](#usage)

To create a new complex object, you can provide either the real, imaginary and suffix parts as individual values, or as an array of values passed passed to the constructor; or a string representing the value. e.g

```
$real = 1.23;
$imaginary = -4.56;
$suffix = 'i';

$complexObject = new Complex\Complex($real, $imaginary, $suffix);
```

or as an array

```
$real = 1.23;
$imaginary = -4.56;
$suffix = 'i';

$arguments = [$real, $imaginary, $suffix];

$complexObject = new Complex\Complex($arguments);
```

or as a string

```
$complexString = '1.23-4.56i';

$complexObject = new Complex\Complex($complexString);
```

Complex objects are immutable: whenever you call a method or pass a complex value to a function that returns a complex value, a new Complex object will be returned, and the original will remain unchanged. This also allows you to chain multiple methods as you would for a fluent interface (as long as they are methods that will return a Complex result).

Performing Mathematical Operations
----------------------------------

[](#performing-mathematical-operations)

To perform mathematical operations with Complex values, you can call the appropriate method against a complex value, passing other values as arguments

```
$complexString1 = '1.23-4.56i';
$complexString2 = '2.34+5.67i';

$complexObject = new Complex\Complex($complexString1);
echo $complexObject->add($complexString2);
```

or use the static Operation methods

```
$complexString1 = '1.23-4.56i';
$complexString2 = '2.34+5.67i';

echo Complex\Operations::add($complexString1, $complexString2);
```

If you want to perform the same operation against multiple values (e.g. to add three or more complex numbers), then you can pass multiple arguments to any of the operations.

You can pass these arguments as Complex objects, or as an array, or string that will parse to a complex object.

Using functions
---------------

[](#using-functions)

When calling any of the available functions for a complex value, you can either call the relevant method for the Complex object

```
$complexString = '1.23-4.56i';

$complexObject = new Complex\Complex($complexString);
echo $complexObject->sinh();
```

or use the static Functions methods

```
$complexString = '1.23-4.56i';

echo Complex\Functions::sinh($complexString);
```

As with operations, you can pass these arguments as Complex objects, or as an array or string that will parse to a complex object.

In the case of the `pow()` function (the only implemented function that requires an additional argument) you need to pass both arguments when calling the function

```
$complexString = '1.23-4.56i';

$complexObject = new Complex\Complex($complexString);
echo Complex\Functions::pow($complexObject, 2);
```

or pass the additional argument when calling the method

```
$complexString = '1.23-4.56i';

$complexObject = new Complex\Complex($complexString);
echo $complexObject->pow(2);
```

###  Health Score

56

—

FairBetter than 98% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity78

Solid adoption and visibility

Community29

Small or concentrated contributor base

Maturity78

Established project with proven stability

 Bus Factor1

Top contributor holds 70.6% 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 ~68 days

Recently: every ~131 days

Total

26

Last Release

1259d ago

Major Versions

1.4.8 → 2.0.02020-08-26

1.5.x-dev → 2.0.12021-05-23

2.0.3 → 3.0.02021-06-27

2.0.x-dev → 3.0.22022-12-06

PHP version history (3 changes)1.0.0PHP ^5.6.0|^7.0.0

2.0.0PHP ^7.2 || ^8.0

1.5.0PHP ^5.6.0|^7.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/2b8457fa3227a7e8e38f0121f1fe254ec965133df93ae5ea8352c757adb98283?d=identicon)[PHPOffice](/maintainers/PHPOffice)

---

Top Contributors

[![jrfnl](https://avatars.githubusercontent.com/u/663378?v=4)](https://github.com/jrfnl "jrfnl (24 commits)")[![MarkBaker](https://avatars.githubusercontent.com/u/770298?v=4)](https://github.com/MarkBaker "MarkBaker (9 commits)")[![oleibman](https://avatars.githubusercontent.com/u/10341515?v=4)](https://github.com/oleibman "oleibman (1 commits)")

---

Tags

mathematicscomplex

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/markbaker-complex/health.svg)

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

###  Alternatives

[brick/math

Arbitrary-precision arithmetic library

2.1k504.0M277](/packages/brick-math)[markbaker/matrix

PHP Class for working with matrices

1.5k279.7M38](/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.4k7.1M40](/packages/markrogoyski-math-php)[aza/math

AzaMath - Anizoptera CMF mathematic component. Arbitrary precision arithmetic (for huge integers; BCMath wrapper) and universal convertor between positional numeral systems (supported bases from 2 to 62 inclusive, and systems with custom alphabet; pure PHP realisation, can use GMP and core PHP functions for speed optimization).

1921.9k1](/packages/aza-math)

PHPackages © 2026

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