PHPackages                             iagapie/matrix-php - 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. iagapie/matrix-php

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

iagapie/matrix-php
==================

Matrix library

v1.0.2(6y ago)011MITPHPPHP ^7.2

Since Oct 14Pushed 6y ago1 watchersCompare

[ Source](https://github.com/iagapie/matrix-php)[ Packagist](https://packagist.org/packages/iagapie/matrix-php)[ RSS](/packages/iagapie-matrix-php/feed)WikiDiscussions master Synced 4d ago

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

Matrix PHP
==========

[](#matrix-php)

[![Minimum PHP Version](https://camo.githubusercontent.com/4c62148864d567c4ee794ffab09c1dd4a3f45e41064bbb016440beddaae71a4d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344253230372e322d3838393242462e737667)](https://php.net/)[![Latest Version](https://camo.githubusercontent.com/a98258f2867dad6e824c107236fbd2ebeb1a9c946690a4cbb18ab050db16d5e2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f696167617069652f6d61747269782d7068702e737667)](https://packagist.org/packages/iagapie/matrix-php)[![Build Status](https://camo.githubusercontent.com/2515521e0e2e207f3fe16c544f9a4fffc66c3671eee4b4ca94410276970df488/68747470733a2f2f7472617669732d63692e6f72672f696167617069652f6d61747269782d7068702e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/iagapie/matrix-php)[![License](https://camo.githubusercontent.com/fd9a1b5e3ba99c51864f870c857c519e36867d7efa9623df9a7a0781187f8125/68747470733a2f2f706f7365722e707567782e6f72672f696167617069652f6d61747269782d7068702f6c6963656e7365)](https://packagist.org/packages/iagapie/matrix-php)[![Coverage Status](https://camo.githubusercontent.com/8f5c34ddd311f156646f7dfa8a11076fd3281da1453cfac4a53e8d58645d7b5b/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f696167617069652f6d61747269782d7068702f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/iagapie/matrix-php?branch=master)

Matrix PHP requires PHP &gt;= 7.2.

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

[](#installation)

```
composer require iagapie/matrix-php

```

Simple example
--------------

[](#simple-example)

```
require_once __DIR__ . '/vendor/autoload.php';

use IA\Matrix\ImmutableMatrix as M;
use IA\Matrix\MatrixInterface as Mi;

class NeuralNetwork
{
    private $w1;
    private $w2;
    private $z2;
    private $z3;

    public function __construct($inputSize = 2, $hiddenSize = 3, $outputSize = 1)
    {
        $this->w1 = M::randn([$inputSize, $hiddenSize]);
        $this->w2 = M::randn([$hiddenSize, $outputSize]);
    }

    public function predict(Mi $x): Mi
    {
        return $this->forward($x);
    }

    public function train(Mi $x, Mi $y, int $epochs = 15000, bool $verbose = true): void
    {
        if ($verbose) {
            printf("Training Input (scaled): \n%s\n", $x);
            printf("Training Output: \n%s\n", $y);
        }

        for ($i = 0; $i < $epochs; ++$i) {
            $o = $this->forward($x);
            $this->backward($x, $y, $o);

            if ($verbose) {
                printf("\n# %s\n", $i);
                printf("Predicted Output: \n%s\n", $o->__toString());
                $loss = $y->sub($o)->apply(function ($value) { return $value * $value; })->mean();
                printf("Loss: \n%s\n", $loss);
            }
        }
    }

    private function forward(Mi $x): Mi
    {
        $z = $x->dot($this->w1);
        $this->z2 = $this->sigmoid($z);
        $this->z3 = $this->z2->dot($this->w2);
        $o = $this->sigmoid($this->z3);
        return $o;
    }

    private function backward(Mi $x, Mi $y, Mi $o): void
    {
        $error = $y->sub($o);
        $delta = $error->mul($this->sigmoidPrime($o));
        $z2Error = $delta->dot($this->w2->transpose());
        $z2Delta = $z2Error->mul($this->sigmoidPrime($this->z2));
        $this->w1 = $this->w1->add($x->transpose()->dot($z2Delta));
        $this->w2 = $this->w2->add($this->z2->transpose()->dot($delta));
    }

    private function sigmoid(Mi $s): Mi
    {
        return $s->apply(function ($value) {
            return 1 / (1 + exp(-$value));
        });
    }

    private function sigmoidPrime(Mi $s): Mi
    {
        return $s->apply(function ($value) {
            return $value * (1 -$value);
        });
    }
}

$x = M::from([[0.4, 0.9], [0.2, 0.5], [0.6, 0.6]]);
$y = M::from([[92], [86], [89]])->div(100);

$nn = new NeuralNetwork();
$nn->train($x, $y);

$xp = M::from([[1., 1.]]);
$predicted = $nn->predict($xp);

printf("\nPredicted data based on trained weights:\n");
printf("Input (scaled):\n%s\n", $xp->__toString());
printf("Output:\n%s\n", $predicted->__toString());
```

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity54

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

Every ~1 days

Total

2

Last Release

2404d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/426384?v=4)[Igor Agapie](/maintainers/iagapie)[@iagapie](https://github.com/iagapie)

---

Top Contributors

[![iagapie](https://avatars.githubusercontent.com/u/426384?v=4)](https://github.com/iagapie "iagapie (17 commits)")

---

Tags

matrixphpmatrixmath

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/iagapie-matrix-php/health.svg)

```
[![Health](https://phpackages.com/badges/iagapie-matrix-php/health.svg)](https://phpackages.com/packages/iagapie-matrix-php)
```

###  Alternatives

[brick/math

Arbitrary-precision arithmetic library

2.1k504.0M277](/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.1M40](/packages/markrogoyski-math-php)[markbaker/matrix

PHP Class for working with matrices

1.5k279.7M38](/packages/markbaker-matrix)[rubix/tensor

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

2751.4M5](/packages/rubix-tensor)[phpseclib/bcmath_compat

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

16720.7M17](/packages/phpseclib-bcmath-compat)[rindow/rindow-math-matrix

The fundamental package for scientific matrix operation

13281.5k7](/packages/rindow-rindow-math-matrix)

PHPackages © 2026

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