PHPackages                             innmind/math - 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. innmind/math

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

innmind/math
============

Library holding a set of math functions

7.0.0(4mo ago)320.3k↓75.6%1[1 issues](https://github.com/Innmind/Math/issues)3MITPHPPHP ~8.4CI passing

Since Oct 20Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/Innmind/Math)[ Packagist](https://packagist.org/packages/innmind/math)[ Docs](http://github.com/Innmind/Math)[ RSS](/packages/innmind-math/feed)WikiDiscussions develop Synced 2d ago

READMEChangelog (2)Dependencies (4)Versions (25)Used By (3)

Math
====

[](#math)

[![Build Status](https://github.com/innmind/math/workflows/CI/badge.svg?branch=master)](https://github.com/innmind/math/actions?query=workflow%3ACI)[![codecov](https://camo.githubusercontent.com/846dd06b4a7ffb7dd14ecd36c35427a3c5a0d346abafd643a5c98b513a3f3f3a/68747470733a2f2f636f6465636f762e696f2f67682f696e6e6d696e642f6d6174682f6272616e63682f646576656c6f702f67726170682f62616467652e737667)](https://codecov.io/gh/innmind/math)[![Type Coverage](https://camo.githubusercontent.com/3bfee5ee5c00cc8bcb3afad26a7f7e22386fdf3a031148fea57407e76c8d7e7f/68747470733a2f2f73686570686572642e6465762f6769746875622f696e6e6d696e642f6d6174682f636f7665726167652e737667)](https://shepherd.dev/github/innmind/math)

Expose some math concepts as objects.

**Note**: all classes are immutable.

Algebra
-------

[](#algebra)

```
use Innmind\Math\Algebra\{
    Value,
    Integer,
};

$perimeter = Value::two->multiplyBy(Value::pi, $r = Integer::of(42)); // value still not calculated
echo $perimeter->toString(); // 2 x π x 42 (value still not calculated)
echo $perimeter->value(); // 263.89378290154
```

By doing math like this you calculate the data that's really needed, so if you pass around a variable but never check it's content then it will never be calculated. The other advantage is that by casting to a string an operation you can see what the operation steps are (might be helpful for debugging a function operation).

**Note**: by calling `collapse` on a `Number` it will try to optimize some calculations such as `squareRoot(square(x))` will directly return `x` thus avoiding rounding errors.

Definition sets
---------------

[](#definition-sets)

```
use Innmind\Math\{
    DefinitionSet\Range,
    Algebra\Integer,
    Algebra\Value,
};

$set = Range::exlusive(Value::zero, Value::infinite);
echo $set->toString(); // ]0;+∞[
$set->contains(Integer::of(42)); // true
$set->contains(Integer::of(-42)); // false

$set = $set->union(
    Range::exclusive(Value::negativeInfinite, Value::zero),
);
echo $set; // ]-∞;0[∪]0;+∞[
$set->contains(Integer::of(-42)); // true
$set->contains(Integer::of(0)); // false
```

Polynom
-------

[](#polynom)

```
use Innmind\Math\Polynom\Polynom;

$p = Polynom::interceptAt($intercept = Integer::of(1))
    ->withDegree(Integer::of(1), new Number(0.5))
    ->withDegree(Integer::of(2), new Number(0.1));
$p(Integer::of(4))->value(); // 4.6
echo $p->toString(); // 0.1x^2 + 0.5x + 1
```

You also can call the `derived` number for any point `x` (as well as the `tangent`). You can have access to the `primitive` and `derivative` of the polynom, the last one is notably used to calculate an `Integral`.

```
use Innmind\Math\Polynom\Integral;

$integral = Integral::of($somePolynom);
$area = $integral(Integer::of(0), new Integral(42)); // find the area beneath the curve between point 0 and 42
echo $integral->toString(); // ∫(-1x^2 + 4x)dx = [(-1 ÷ (2 + 1))x^3 + (4 ÷ (1 + 1))x^2] (if the polynom is -1x^2 + 4x)
```

Regression
----------

[](#regression)

### Polynomial Regression

[](#polynomial-regression)

```
use Innmind\Math\{
    Regression\PolynomialRegression,
    Regression\Dataset,
    Algebra\Integer,
};

$regression = PolynomialRegression::of(
    Dataset::of([
        [-8, 64],
        [-4, 16],
        [0, 0],
        [2, 4],
        [4, 16],
        [8, 64],
    ]),
);
// so in essence it found x^2
$regression(Integer::of(9))->value(); // 81.0
```

### Linear regression

[](#linear-regression)

```
use Innmind\Math\{
    Regression\LinearRegression,
    Regression\Dataset,
    Algebra\Integer;
};

$r = LinearRegression::of(Dataset::of([
    [0, 0],
    [1, 1],
    [2, 0],
    [3, 2],
]));
$r->intercept()->value(); // 0.0
$r->slope()->value(); // 0.5
$r(Integer::of(4))->value(); // 2.0
```

Probabilities
-------------

[](#probabilities)

```
use Innmind\Math\{
    Regression\Dataset,
    Probabilities\Expectation,
    Probabilities\StandardDeviation,
    Probabilities\Variance,
};

$dataset = Dataset::of([
    [-1, 4/6], // 4 6th of a chance to obtain a -1
    [2, 1/6],
    [3, 1/6],
]);
echo Expectation::of($dataset)()->value(); //0,1666666667 (1 6th)
echo StandardDeviation::of($dataset)()->value(); //√(101/36)
echo Variance::of($dataset)()->value(); //101/36
```

Quantile
--------

[](#quantile)

```
use Innmind\Math\Quantile\Quantile;
use Innmind\Immutable\Sequence;

$q = Quantile::of(Sequence::of(...\range(1,12)));
$q->min()->value(); // 1
$q->max()->value(); // 12
$q->mean(); // 6.5
$q->median()->value(); // 6.5
$q->quartile(1)->value(); // 3.5 because 25% of values from the set are lower than 3.5
$q->quartile(3)->value(); // 9.5 because 75% of values from the set are lower than 9.5
```

###  Health Score

56

—

FairBetter than 97% of packages

Maintenance73

Regular maintenance activity

Popularity29

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity90

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 100% 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 ~235 days

Recently: every ~552 days

Total

17

Last Release

140d ago

Major Versions

2.1.1 → 3.0.02017-02-12

3.0.0 → 4.0.02017-05-22

4.3.0 → 5.0.02020-01-26

5.1.0 → 6.0.02022-10-01

6.1.0 → 7.0.02026-02-14

PHP version history (8 changes)1.0.0PHP &gt;=5.5

2.0.0PHP ~7.0

4.0.0PHP ~7.1

5.0.0PHP ~7.4

5.1.0PHP ~7.4|~8.0

6.0.0PHP ~8.1

6.1.0PHP ~8.2

7.0.0PHP ~8.4

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/851425?v=4)[Baptiste Langlade](/maintainers/Baptouuuu)[@Baptouuuu](https://github.com/Baptouuuu)

---

Top Contributors

[![Baptouuuu](https://avatars.githubusercontent.com/u/851425?v=4)](https://github.com/Baptouuuu "Baptouuuu (415 commits)")

---

Tags

math

### Embed Badge

![Health badge](/badges/innmind-math/health.svg)

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

###  Alternatives

[brick/math

Arbitrary-precision arithmetic library

2.2k548.2M431](/packages/brick-math)[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.6M53](/packages/markrogoyski-math-php)[phpseclib/bcmath_compat

PHP 5.x-8.x polyfill for bcmath extension

16821.5M26](/packages/phpseclib-bcmath-compat)[rubix/tensor

A library and extension that provides objects for scientific computing in PHP.

2801.6M5](/packages/rubix-tensor)[jlawrence/eos

Parse and solve math equations without using 'eval()'.

1071.2M11](/packages/jlawrence-eos)[krowinski/bcmath-extended

Extends php BCMath lib for missing functions like floor, ceil, round, abs, min, max, rand for big numbers. Also wraps existing BCMath functions. (more http://php.net/manual/en/book.bc.php) Supports scientific notations

801.1M24](/packages/krowinski-bcmath-extended)

PHPackages © 2026

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