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

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.


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

$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);

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

array(4) {
  array(2) {
  array(2) {
  array(2) {
  array(2) {

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.


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

svn co lapack
cd lapack
mkdir build
cd build
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:

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.