PHP Classes

Np PHP Matrix Class: Manipulate matrices with Lapack and Blas libraries

Recommend this page to a friend!
  Info   View files Example   View files View files (73)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 51 All time: 10,634 This week: 560Up
Version License PHP version Categories
np 0.0.1Custom (specified...8Math, PHP 8, FFI
Description 

Author

This package can manipulate matrices with Lapack and OpenBlas libraries.

It uses the PHP FFI extension to call the Lapack and OpenBlas and perform several types of operations with matrices. Currently, it can :

- Matrices decomposition (LU, SVD, Eigen, Cholesky)
- Matrices Signal-processing (1D, 2D Convolution)
- Poisson,Gaussian Matrices
- Calculate the product of a general matrix and another general matrix
- Calculate the product of a symmetric matrix and a general matrix
- Update rank n of a symmetric matrix
- Update rank 2k of a symmetric matrix
- Compute the product of a general matrix and a vector stored in a band format.
- Compute the product of a column vector and a row vector. (Real number)
- Calculates the index of the element with the largest absolute value in the vector.
- Calculates the index of the element with the smallest absolute value in the vector.
- Exchange the contents of the vector.
- Compute the Euclidean norm of a vector.
- Add vectors
- Calculates the sum of the absolute values of each component of the vector.
- Rotate about a given point.
- Given the point P (a, b), rotate this point to givens and calculate the parameters a, b, c, and s to make the y coordinate zero.
- Multiply a vector by a scalar.
- Etc.

Innovation Award
PHP Programming Innovation award winner
June 2021
Winner
Matrix manipulation is often used to perform complex calculations for scientific purposes.

Many of those calculations require a lot of processing. So when it is possible to perform those calculations in a lower-level programming language like C or C++, those calculations can be done much faster.

This package allows performing many types of matrix manipulation operations using libraries written in the C/C++ languages.

Then using the FFI extension, PHP applications can call those libraries to get much higher performance when doing those calculations than they could use pure PHP libraries.

Manuel Lemos
Picture of Shubham Chaudhary
  Performance   Level  
Name: Shubham Chaudhary <contact>
Classes: 3 packages by
Country: India India
Age: 33
All time rank: 3530232 in India India
Week rank: 51 Up4 in India India Up
Innovation award
Innovation award
Nominee: 3x

Winner: 2x

Recommendations

Recommendation for a PHP class to regrid 2d matrix
Regrid one 2d matrix to another of different dimensions

Example

<?php

require __DIR__ . '/../vendor/autoload.php';

use
Np\matrix;

$a = matrix::randn(1000, 1000);
$b = matrix::randn(1000, 1000);
$a->dot($b);
$a->getMemory(); // get memory use
$a->time(); // get time
/**
 * Memory-Consumed 7.7mb
 * Time-Consumed:- 0.18390893936157
 */


Details

Scrutinizer Code Quality Packagist PHP Version Support Build Status Code Intelligence Status GitHub contributors GitHub commit activity GitHub last commit Packagist Version GitHub code size in bytes Lines of code GitHub top language

Np

Description

----------- Lite, Fast &amp; Memory Efficient Mathematical PHP library for scientific computing

Np(numphp) is a library that provides objects for computing large sets of numbers in PHP.

Installation

Install Np into your project with Composer:

$ composer require ghostjat/np

Sample Code

require __DIR__ . '/../vendor/autoload.php';
use Np\matrix;

$ta = matrix::randn(1000, 1000);    
$tb = matrix::randn(1000, 1000); // to generate random 2d matrix
$ta->dot($tb);                  // do a dot operation on given matrix
$ta->getMemory();              // get memory use
$ta->time();                  // get time
/
 * 7.7mb
 * Time-Consumed:- 0.18390893936157
 */

Synopsis

WARNING: This module is in its early stages and should be considered a Work in Progress.The interface is not final and may change in the future.

Requirements

  • PHP 8+ 64bit with ffi & #libblas, #liblapacke

Make sure you have all the necessary tools installed such as FFI, libblas, liblapacke.

Performance

System Conf:- Intel(R) Core(TM) i3-2370M CPU @ 2.40GHz 64bit Memory:- 8GB php:- 8.0.5 64bit

Current Benchmarks of this library

Benckmark

Data Size :- [500x500] Revolutions:- 5 Iterations:- 5

| subject | mem_peak | best | mode | mean | worst | stdev | |----------|----------|--------|--------|--------|--------|-------| | sum | 3.606mb | 0.014s | 0.014s | 0.015s | 0.015s | 0.000s| | multiply | 8.589mb | 0.070s | 0.071s | 0.071s | 0.071s | 0.000s| | lu | 4.648mb | 0.064s | 0.065s | 0.065s | 0.068s | 0.001s| | eign | 2.801mb | 0.085s | 0.086s | 0.086s | 0.088s | 0.001s| | cholesky | 1.621mb | 0.001s | 0.001s | 0.001s | 0.001s | 0.000s| | svd | 3.706mb | 0.126s | 0.126s | 0.127s | 0.133s | 0.002s| | normL2 | 1.621mb | 0.003s | 0.003s | 0.003s | 0.003s | 0.000s| | Pinverse | 4.903mb | 0.156s | 0.156s | 0.158s | 0.163s | 0.003s| | inverse | 1.819mb | 0.016s | 0.016s | 0.016s | 0.017s | 0.000s| | normL1 | 1.621mb | 0.001s | 0.001s | 0.001s | 0.001s | 0.000s| | dotMatrix| 3.769mb | 0.006s | 0.006s | 0.006s | 0.006s | 0.000s| | det | 4.662mb | 0.066s | 0.066s | 0.067s | 0.067s | 0.000s| | rref | 1.529mb | 9.227s | 9.271s | 9.309s | 9.427s | 0.072s| | ref | 1.818mb | 0.007s | 0.008s | 0.008s | 0.008s | 0.000s| | clip | 8.516mb | 0.073s | 0.076s | 0.075s | 0.077s | 0.002s| | clipUpper| 8.516mb | 0.055s | 0.056s | 0.057s | 0.059s | 0.002s| | clipLower| 8.516mb | 0.055s | 0.058s | 0.057s | 0.059s | 0.002s| | joinBelow| 4.517mb | 0.027s | 0.027s | 0.027s | 0.028s | 0.000s| | transpose| 8.504mb | 0.057s | 0.057s | 0.058s | 0.059s | 0.001s| | joinLeft | 4.511mb | 0.025s | 0.025s | 0.026s | 0.027s | 0.001s| | poisson | 1.590mb | 0.029s | 0.029s | 0.029s | 0.030s | 0.000s| | gaussian | 20.203mb | 0.056s | 0.056s | 0.056s | 0.056s | 0.000s| | randn | 1.528mb | 0.017s | 0.017s | 0.017s | 0.017s | 0.000s| | uniform | 1.528mb | 0.021s | 0.021s | 0.021s | 0.022s | 0.000s| | multiply | 4.507mb | 0.042s | 0.042s | 0.043s | 0.045s | 0.001s|

Previous BenchMark

| benchmark | subject | set | revs | its | mem_peak | mode | rstdev | |---------------------------|-----------|-----|------|-----|----------|---------|----------| | eignBench | eign | 0 | 1 | 5 | 2.699mb | 0.309s | ±4.51% | | svdBench | svd | 0 | 1 | 5 | 3.604mb | 0.148s | ±3.60% | | poissonMatrixBench | poisson | 0 | 1 | 5 | 11.738mb | 0.105s | ±7.07% | | gaussianMatrixBench | gaussian | 0 | 1 | 5 | 11.738mb | 0.112s | ±17.12% | | randMatrixBench | randn | 0 | 1 | 5 | 1.429mb | 0.048s | ±2.37% | | uniformMatrixBench | uniform | 0 | 1 | 5 | 1.429mb | 0.063s | ±8.16% | | matrixTransposeBench | transpose | 0 | 1 | 5 | 8.431mb | 0.120s | ±1.32% | | rrefBench | rref | 0 | 1 | 5 | 1.501mb | 28.513s | ±1.90% | | refBench | ref | 0 | 1 | 5 | 1.731mb | 0.023s | ±7.24% | | sumMatrixBench | sum | 0 | 1 | 5 | 2.434mb | 0.051s | ±3.59% | | matrixPseudoInverseBench | inverse | 0 | 1 | 5 | 4.775mb | 0.222s | ±13.76% | | matrixInverseBench | inverse | 0 | 1 | 5 | 1.731mb | 0.032s | ±127.50% | | dotMatrixBench | dotMatrix | 0 | 1 | 5 | 3.656mb | 0.013s | ±27.94% | | matrixL1NormBench | normL1 | 0 | 1 | 10 | 1.525mb | 0.001s | ±0.80% | | matrixL2NormBench | normL2 | 0 | 1 | 10 | 1.525mb | 0.003s | ±1.63% |

License

The code is licensed MIT and the documentation is licensed CC BY-NC 4.0.

Author

Shubham Chaudhary <ghost.jat@gmail.com>


  Files folder image Files  
File Role Description
Files folder imagebenchmarks (2 directories)
Files folder imageexamples (2 files)
Files folder imagesrc (5 files, 4 directories)
Plain text file composer.json Data Auxiliary data
Plain text file LICENSE Lic. License text
Image file np.png Icon Icon image
Image file npbm.png Screen Auxiliary data
Plain text file php.ini Data Auxiliary data
Plain text file phpbench.json Data Auxiliary data
Plain text file phpdoc.xml Data Auxiliary data
Plain text file preload.php Aux. Auxiliary script
Plain text file README.md Doc. Documentation
Plain text file SECURITY.md Data Auxiliary data

  Files folder image Files  /  benchmarks  
File Role Description
Files folder imagematrix (10 directories)
Files folder imagevector (3 directories)

  Files folder image Files  /  benchmarks  /  matrix  
File Role Description
Files folder imagearithmetic (3 files)
Files folder imagedecomposition (4 files)
Files folder imagefunctions (6 files)
Files folder imagelineAlgb (6 files)
Files folder imagerandom (4 files)
Files folder imagereduction (2 files)
Files folder imagesignalProcessing (1 file)
Files folder imagespecial (3 files)
Files folder imagestructural (3 files)
Files folder imagetrigonometric (3 files)

  Files folder image Files  /  benchmarks  /  matrix  /  arithmetic  
File Role Description
  Plain text file matrixScalarMultiplyBench.php Class Class source
  Plain text file matrixVectorMultiplyBench.php Class Class source
  Plain text file sumMatrixBench.php Class Class source

  Files folder image Files  /  benchmarks  /  matrix  /  decomposition  
File Role Description
  Plain text file choleskyBench.php Class Class source
  Plain text file eignBench.php Class Class source
  Plain text file luBench.php Class Class source
  Plain text file svdBench.php Class Class source

  Files folder image Files  /  benchmarks  /  matrix  /  functions  
File Role Description
  Plain text file absMatrixBench.php Class Class source
  Plain text file expMatrixBench.php Class Class source
  Plain text file logMatrixBench.php Class Class source
  Plain text file sqrtMatrixBench.php Class Class source
  Plain text file squareMatrixBench.php Class Class source
  Plain text file sumMatrixBench.php Class Class source

  Files folder image Files  /  benchmarks  /  matrix  /  lineAlgb  
File Role Description
  Plain text file dotMatrixBench.php Class Class source
  Plain text file matrixDeterminantBench.php Class Class source
  Plain text file matrixInverseBench.php Class Class source
  Plain text file matrixL1NormBench.php Class Class source
  Plain text file matrixL2NormBench.php Class Class source
  Plain text file matrixPseudoInverseBench.php Class Class source

  Files folder image Files  /  benchmarks  /  matrix  /  random  
File Role Description
  Plain text file gaussianMatrixBench.php Class Class source
  Plain text file poissonMatrixBench.php Class Class source
  Plain text file randMatrixBench.php Class Class source
  Plain text file uniformMatrixBench.php Class Class source

  Files folder image Files  /  benchmarks  /  matrix  /  reduction  
File Role Description
  Plain text file refBench.php Class Class source
  Plain text file rrefBench.php Class Class source

  Files folder image Files  /  benchmarks  /  matrix  /  signalProcessing  
File Role Description
  Plain text file matrixConvolveBench.php Class Class source

  Files folder image Files  /  benchmarks  /  matrix  /  special  
File Role Description
  Plain text file matrixClippingBench.php Class Class source
  Plain text file matrixClippingLowerBench.php Class Class source
  Plain text file matrixClippingUpperBench.php Class Class source

  Files folder image Files  /  benchmarks  /  matrix  /  structural  
File Role Description
  Plain text file matrixJoinBelowBench.php Class Class source
  Plain text file matrixJoinLeftBench.php Class Class source
  Plain text file matrixTransposeBench.php Class Class source

  Files folder image Files  /  benchmarks  /  matrix  /  trigonometric  
File Role Description
  Plain text file cosMatrixBench.php Class Class source
  Plain text file sinMatrixBench.php Class Class source
  Plain text file tanMatrixBench.php Class Class source

  Files folder image Files  /  benchmarks  /  vector  
File Role Description
Files folder imagefunctions (2 files)
Files folder imagesignalProcessing (1 file)
Files folder imagetrigonometric (1 file)

  Files folder image Files  /  benchmarks  /  vector  /  functions  
File Role Description
  Plain text file productVectorBench.php Class Class source
  Plain text file sumVectorBench.php Class Class source

  Files folder image Files  /  benchmarks  /  vector  /  signalProcessing  
File Role Description
  Plain text file vectorConvolveBench.php Class Class source

  Files folder image Files  /  benchmarks  /  vector  /  trigonometric  
File Role Description
  Plain text file cosVectorBench.php Class Class source

  Files folder image Files  /  examples  
File Role Description
  Plain text file matrix.php Example Example script
  Plain text file vector.php Example Example script

  Files folder image Files  /  src  
File Role Description
Files folder imagecore (5 files)
Files folder imagedecompositions (4 files)
Files folder imageexceptions (6 files)
Files folder imagereductions (2 files)
  Plain text file convolve.php Class Class source
  Plain text file linAlg.php Class added
  Plain text file matrix.php Class Class source
  Plain text file ops.php Class added
  Plain text file vector.php Class Class source

  Files folder image Files  /  src  /  core  
File Role Description
  Plain text file blas.h Data Auxiliary data
  Plain text file blas.php Class Class source
  Plain text file lapack.h Data Auxiliary data
  Plain text file lapack.php Class Class source
  Plain text file nd.php Class Class source

  Files folder image Files  /  src  /  decompositions  
File Role Description
  Plain text file cholesky.php Class Class source
  Plain text file eigen.php Class Class source
  Plain text file lu.php Class Class source
  Plain text file svd.php Class Class source

  Files folder image Files  /  src  /  exceptions  
File Role Description
  Plain text file dimensionalityMismatch.php Class Class source
  Plain text file dtypeException.php Class Class source
  Plain text file invalidArgumentException.php Class Class source
  Plain text file message.php Class temp
  Plain text file npException.php Class Class source
  Plain text file runtimeException.php Class Class source

  Files folder image Files  /  src  /  reductions  
File Role Description
  Plain text file ref.php Class Class source
  Plain text file rref.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:51
This week:0
All time:10,634
This week:560Up