Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

PHP extension wrapping the LAPACK linear algebra library via the LAPACKE C bindings

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 tests
Octocat-spinner-32 CREDITS
Octocat-spinner-32 LICENSE
Octocat-spinner-32 README.md
Octocat-spinner-32 config.m4
Octocat-spinner-32 lapack.c
Octocat-spinner-32 package.xml
Octocat-spinner-32 php_lapack.h
Octocat-spinner-32 php_lapack_internal.h
README.md

PHP Lapack Extension

This PHP extension wraps the LAPACK Linear Algebra Fortran library, using the LAPACKE C wrapper to allow an easy(ish) way in PHP to get access to some decent linear algebra processing. The extension is currently early days, and wraps just a few of the "driver" functions - functions which solve specific types of problems. Specifically the current extension exposes:

  • Two methods of solving linear least squares problems
  • A method to return the singular values of a matrix
  • A method to return the eigenvalues of a matrix

All matrices are passed in as nested PHP arrays, and returned the same way. This is certainly not the worlds most memory efficient format, so don't expect to do very large sized matrix transforms. That said, other than the memory usage, the processing should be extremely quick in the library as the wrapping is fairly minimal.

Usage

The API is currently presented as static calls on the Lapack class. For example for linear least squares:

<?php
$a = array(
    array( 1.44,  -7.84,  -4.39,   4.53),
    array(-9.96,  -0.28,  -3.24,   3.83),
    array(-7.55,   3.24,   6.27,  -6.64),
    array( 8.34,   8.09,   5.28,   2.06),
    array( 7.08,   2.52,   0.74,  -2.47),
    array(-5.45,  -5.70,  -1.19,   4.70),
);

$b = array(
    array( 8.58,   9.35),
    array( 8.26,  -4.43),
    array( 8.48,  -0.70),
    array(-5.28,  -0.26),
    array( 5.72,  -7.36),
    array( 8.93,  -2.52),           
);

$result = Lapack::leastSquaresByFactorisation($a, $b);
var_dump($result);
?>

This should print (with some rounding to allow for float differences):

array(4) {
  [0]=>
  array(2) {
    [0]=>
    float(-0.45)
    [1]=>
    float(0.25)
  }
  [1]=>
  array(2) {
    [0]=>
    float(-0.85)
    [1]=>
    float(-0.9)
  }
  [2]=>
  array(2) {
    [0]=>
    float(0.71)
    [1]=>
    float(0.63)
  }
  [3]=>
  array(2) {
    [0]=>
    float(0.13)
    [1]=>
    float(0.14)
  }
}

Using the same setup we can do:

  • $result = Lapack::leastSquaresBySVD($a, $b);
  • $result = Lapack::solveLinearEquation($a, $b);
  • $result = Lapack::singularValues($a);
  • $result = Lapack::eigenValues($a);
  • $result = Lapack::pseudoInverse($a);
  • $result = Lapack::identity(3); // return an identity matrix size n

If there is a shortage of memory or the matrices are invalid, a Lapackexception will be thrown. On other errors, the returned matrix will be an empty array.

The eigenvalues function can optionally return the left and right eigenvectors if arrays are passed as the second and third arguments to the function.

Installation

From a limited survey, most distribution LAPACK packages do not include lapacke, so the easiest method is to build from source:

svn co https://icl.cs.utk.edu/svn/lapack-dev/lapack/trunk lapack
cd lapack
mkdir build
cd build
cmake -D BUILD_SHARED_LIBS=ON -D LAPACKE=ON ../
make 
sudo make install

That should build LAPACK and the C wrapper (lapacke). Installing the extension is as simple as installing from PECL:

pecl install lapack-beta

Or if building from source, downloading the package, then:

phpize
configure
make 
sudo make install

Windows support is currently not included - once the API is stabilised though this will be added relatively shortly.

Something went wrong with that request. Please try again.