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

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

kachnitel/math-utils
====================

PHP library for mathematical operations including median, average, sum, and formula processing

1.0.0(5mo ago)0191MITPHPPHP &gt;=8.2

Since Jan 29Pushed 5mo agoCompare

[ Source](https://github.com/kachnitel/frd-math-utils)[ Packagist](https://packagist.org/packages/kachnitel/math-utils)[ RSS](/packages/kachnitel-math-utils/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (2)Versions (2)Used By (0)

Math Utils
==========

[](#math-utils)

PHP library for mathematical operations including median, average, sum, and formula processing.

Features
--------

[](#features)

- **Basic Statistical Functions**: median, average, sum
- **Formula Processor**: Process complex mathematical formulas with nested functions and parameters
- **Secure Evaluation**: Protected against code injection attacks
- **Pure PHP**: No dependencies (except PHPUnit for development)

Installation
------------

[](#installation)

```
composer require kachnitel/math-utils
```

Usage
-----

[](#usage)

### Basic Operations

[](#basic-operations)

```
use Kachnitel\MathUtils\Math;

// Calculate median
$median = Math::median([1, 2, 3, 4, 5]); // 3

// Calculate average
$average = Math::average([1, 2, 3, 4, 5]); // 3

// Calculate sum
$sum = Math::sum([1, 2, 3, 4, 5]); // 15
```

### Formula Processing

[](#formula-processing)

The formula processor supports nested functions, parameters, and basic arithmetic operations (+, -, \*, /).

```
use Kachnitel\MathUtils\Math;

// Simple formula
$result = Math::process('1 + 1', []); // 2

// Formula with parameters
$result = Math::process('$0 * $1', [2, 3]); // 6

// Complex nested formula
$result = Math::process(
    'average([average([sum($0), sum($1)]),median($2)]) * 2',
    [
        [1, 2, 3, 4],    // $0
        [5, 6, 7, 8],    // $1
        [10, 11, 12]     // $2
    ]
); // Calculated result

// Track calculation steps
$steps = [];
$result = Math::process('average([sum($0), sum($1)])', [[1, 2], [3, 4]], $steps);
// $steps will contain each step of the calculation
```

### Supported Functions in Formulas

[](#supported-functions-in-formulas)

- `sum(array)` - Sum of array elements
- `average(array)` - Average of array elements
- `median(array)` - Median of array elements

Security
--------

[](#security)

The formula processor is protected against code injection. All formulas are validated before evaluation:

```
// This will throw MathException
Math::process('1 + 1; echo "injection";', []);
```

Requirements
------------

[](#requirements)

- PHP 8.2 or higher

License
-------

[](#license)

MIT License - see LICENSE file for details

###  Health Score

37

—

LowBetter than 81% of packages

Maintenance72

Regular maintenance activity

Popularity13

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 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

Unknown

Total

1

Last Release

157d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/4067705?v=4)[Ondrej Vana](/maintainers/kachnitel)[@kachnitel](https://github.com/kachnitel)

---

Top Contributors

[![kachnitel](https://avatars.githubusercontent.com/u/4067705?v=4)](https://github.com/kachnitel "kachnitel (7 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[whichbrowser/parser

Useragent sniffing library for PHP

1.8k12.7M59](/packages/whichbrowser-parser)[outl1ne/nova-sortable

This Laravel Nova package allows you to reorder models in a Nova resource's index view using drag &amp; drop.

2862.1M9](/packages/outl1ne-nova-sortable)[spiral/tokenizer

Static Analysis: Class and Invocation locators

188.6M73](/packages/spiral-tokenizer)[chatter-laravel/core

A simple forum package for your laravel app

1591.4k](/packages/chatter-laravel-core)

PHPackages © 2026

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