PHP Classes

How to Use a PHP Matrix Library in C++ to Perform Matrix Operations Faster using the Package PHP Matrix Extension Class: Perform matrix calculations using a PHP extension

Recommend this page to a friend!
  Info   View files Example   View files View files (13)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-05-23 (2 days ago) RSS 2.0 feedNot yet rated by the usersTotal: 18 This week: 18All time: 11,237 This week: 5Up
Version License PHP version Categories
php-matrix 1.0MIT/X Consortium ...5PHP 5, Data types, Math
Description 

Author

This package can perform matrix calculations using a PHP extension.

It provides a C++ extension that implements a matrix class to perform several matrix operations.


Currently, it implements the matrix operations:

- Addition

- Division

- Log

- Exponential

- Inversion

- Determinant

- Eigen

- Sum

- Transpose

- Subtract

- Etc..

Picture of Cuthbert Martin Lwinga
  Performance   Level  
Name: Cuthbert Martin Lwinga <contact>
Classes: 4 packages by
Country: Canada Canada
Innovation award
Innovation award
Nominee: 1x

Example

<?php
ini_set
('memory_limit', '200G'); // Increase to 2GB or more as needed

// Helper function to create a random matrix
function create_random_matrix($rows, $cols) {
   
$matrix = array_fill(0, $rows, array_fill(0, $cols, 0));
    for (
$i = 0; $i < $rows; ++$i) {
        for (
$j = 0; $j < $cols; ++$j) {
           
$matrix[$i][$j] = mt_rand() / mt_getrandmax();
        }
    }
    return
$matrix;
}

// Function to measure execution time of a given operation
function measure_time($callback, $description) {
   
$start_time = microtime(true);
   
$callback();
   
$end_time = microtime(true);
   
$execution_time = $end_time - $start_time;
    echo
$description . ": " . $execution_time . " seconds\n";
}

// Generate random matrices
$rows = 10000; // Adjust size as needed
$cols = 10000;
$min_val = 0.2;
$max_val = 0.8;

echo
" MATRIX($rows,$cols)\n\n";

$m1 = create_random_matrix($rows, $cols);
$m2 = create_random_matrix($rows, $cols);

$matrix1 = new MatrixWrapper($m1);
$matrix2 = new MatrixWrapper($m2);

measure_time(function() use ($m1) {
   
$matrix1 = new MatrixWrapper($m1);
},
"Init Time");


measure_time(function() use ($matrix1) {
   
$matrix1->shape();
},
"Finding Shape Time");

// Perform subtraction first and then addition
measure_time(function() use ($matrix1, $matrix2) {
   
$matrix1->sub($matrix2);
},
"Subtraction Time");

measure_time(function() use ($matrix1, $matrix2) {
   
$matrix1->add($matrix2);
},
"Addition Time after Subtraction");

// Perform addition first and then subtraction
measure_time(function() use ($matrix1, $matrix2) {
   
$matrix1->add($matrix2);
},
"Addition Time");

measure_time(function() use ($matrix1, $matrix2) {
   
$matrix1->sub($matrix2);
},
"Subtraction Time after Addition");

// Measure constant addition time
measure_time(function() use ($matrix1) {
   
$matrix1->add(2);
},
"Addition Constant Time");

// Measure dot product time
measure_time(function() use ($matrix1, $matrix2) {
   
$matrix1->dot($matrix2);
},
"Dot Product Time");

measure_time(function() use ($matrix1, $matrix2) {
   
$matrix1->div($matrix2);
},
"Matrix-to-Matrix Division Time");

// Measure matrix-to-scalar division time
measure_time(function() use ($matrix1) {
   
$matrix1->div(2);
},
"Matrix-to-Scalar Division Time");

// Measure transpose time
measure_time(function() use ($matrix1) {
   
$matrix1->transpose();
},
"Transpose Time");

// Measure Multiplication time
measure_time(function() use ($matrix1) {
   
$matrix1->mul(2);
},
"Multiplication Time");

// Measure argmax time
measure_time(function() use ($matrix1) {
   
$matrix1->argmax(1);
},
"Arg max Time");

// Measure clip time
measure_time(function() use ($matrix1, $min_val, $max_val) {
   
$matrix1->clip($min_val, $max_val);
},
"Clip Time");

// Measure log
measure_time(function() use ($matrix1) {
   
$matrix1->log();
},
"Log Time");

// Measure exp
measure_time(function() use ($matrix1) {
   
$matrix1->exp();
},
"Exp Time");

// Measure sum
measure_time(function() use ($matrix1) {
   
$matrix1->sum();
},
"Sum Time");

measure_time(function() use ($matrix1) {
   
$matrix1->sum(0);
},
"Sum along columns Time");

measure_time(function() use ($matrix1) {
   
$matrix1->sum(1);
},
"Sum along rows Time");

// Measure inverse
measure_time(function() use ($matrix1) {
   
$matrix1->inverse();
},
"Inverse Time");

// Measure determinant
measure_time(function() use ($matrix1) {
   
$matrix1->determinant();
},
"Determinant Time");

// Measure eigen
measure_time(function() use ($matrix1) {
   
$matrix1->eigen();
},
"Eigenvalues and Eigenvectors Time");
?>


  Files folder image Files  
File Role Description
Files folder image.libs (1 file, 1 directory)
Accessible without login Plain text file CMakeLists.txt Doc. Documentation
Accessible without login Plain text file extension.cpp Data Auxiliary data
Accessible without login Plain text file Makefile Data Auxiliary data
Accessible without login Plain text file matrix.cpp Data Auxiliary data
Accessible without login Plain text file matrix.h Data Auxiliary data
Accessible without login Plain text file matrixwrapper.cpp Data Auxiliary data
Accessible without login Plain text file matrixwrapper.h Data Auxiliary data
Accessible without login Plain text file test.php Example Example script
Accessible without login Plain text file TODO Data Auxiliary data
Accessible without login Plain text file warm-up-test.php Example Example script

  Files folder image Files  /  .libs  
File Role Description
Files folder imagematrix.so.dSYM (1 directory)
  Accessible without login Plain text file matrix.lai Data Auxiliary data

  Files folder image Files  /  .libs  /  matrix.so.dSYM  
File Role Description
Files folder imageContents (1 file, 1 directory)

  Files folder image Files  /  .libs  /  matrix.so.dSYM  /  Contents  
File Role Description
Files folder imageResources (1 directory)
  Accessible without login Plain text file Info.plist Data Auxiliary data

  Files folder image Files  /  .libs  /  matrix.so.dSYM  /  Contents  /  Resources  
File Role Description
Files folder imageRelocations (1 directory)

  Files folder image Files  /  .libs  /  matrix.so.dSYM  /  Contents  /  Resources  /  Relocations  
File Role Description
Files folder imageaarch64 (1 file)

  Files folder image Files  /  .libs  /  matrix.so.dSYM  /  Contents  /  Resources  /  Relocations  /  aarch64  
File Role Description
  Accessible without login Plain text file matrix.so.yml Data Auxiliary data

 Version Control Unique User Downloads Download Rankings  
 100%
Total:18
This week:18
All time:11,237
This week:5Up