Download the PHP package mcordingley/linearalgebra without Composer

On this page you can find all versions of the php package mcordingley/linearalgebra. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package linearalgebra

Matrix

Build Status Code Climate Code Coverage

Stand-alone Linear Algebra Library for PHP

Installation

composer require mcordingley/LinearAlgebra

Alternately, include this in your composer.json and then update:

"mcordingley/linearalgebra": "^3.0.0"

If Composer isn't an option for you, clone this repository and run build-phar.php to generate a phar archive that you can include into your project. PHP will autoload classes from inside the archive as needed.

Usage

Matrix

Start with a use statement for the class:

use MCordingley\LinearAlgebra\Matrix;

Then, instantiate a new instance of the matrix class like so:

$matrix = new Matrix([
    [0, 1, 2],
    [3, 4, 5],
    [6, 7, 8]
]);

You can also generate an identity matrix with the identity factory function:

$threeByThreeIdentityMatrix = Matrix::identity(3);

With the matrix instance, you can retrieve individual elements with get using the zero-based indices of the row and column that you want:

$element = $matrix->get($row, $column);

It's also possible to find out how large the matrix is with getRowCount() and getColumnCount():

$rows = $matrix->getRowCount();
$columns = $matrix->getColumnCount();

You can also add, subtract, and multiply the matrix with scalar values and other matrices. All operations return a new Matrix and do not modify the underlying matrix:

$addedScalar = $matrix->addScalar(3);
$addedMatrix = $matrix->addMatrix($anotherMatrix);
$subtractedScalar = $matrix->subtractScalar(2);
$subtractedMatrix = $matrix->subtractMatrix($anotherMatrix);
$multipliedByScalar = $matrix->multiplyScalar(4);
$multipliedByMatrix = $matrix->multiplyMatrix($anotherMatrix);

Matrices can be compared with equals to see if they're equal:

if ($matrix1->equals($matrix2)) {
    // Equality for all!
}

In addition to these basic operations, the Matrix class offers other common matrix operations:

$matrix->inverse()
$matrix->adjugate()
$matrix->determinant()
$matrix->trace()
$matrix->transpose()

You can get the upper and lower triangular matrices by calling upper(bool) and lower(bool). The lone argument tells whether the main diagonal of the triangular matrix should be set to ones (true) or the value of the parent matrix (false).

It's also possible to run a map over the matrix:

$squaredElements = $matrix->map(function($element, $row, $column, $matrix) {
    return $element * $element
});

Submatrices may be extracted with sliceColumns($offset, $length) and sliceRows($offset, $length). The semantics of the arguments are the same as PHP's array_slice.

Similarly, spliceColumns($offset, $length, $replacement) and spliceRows($offset, $length, $replacement) can be used to create new matrices with specific rows or columns removed or replaced. Unlike the native PHP array_splice, these operations do not modify the matrix in place and return the removed elements, but instead return a new matrix with the splice applied.

If you need to combine together matrices, you can do so by calling the concatenation methods:

$m1 = new Matrix([
  [1,2,3],
  [4,5,6],
]);

$m2 = new Matrix([
  [7],
  [8],
]);

$m3 = new Matrix([[3,2,1]]);

$m4 = $m1->concatenateRight($m2);
//  [
//      [1,2,3,7],
//      [4,5,6,8],
//  ]

$m5 = $m1->concatenateBottom($m3);
// [
//     [1,2,3],
//     [4,5,6],
//     [3,2,1],
// ]

LU and LUP decomposition methods are available as separate classes and both expose lower() and upper() for the L and U portions of the decompositions, respectively. The LUP decomposition additionally exposes permutationMatrix and permutationArray to fetch the P component of the decomposition as well as parity to return the total number of pivots performed.

Vector

As with Matrix, import the class into your current namespace:

use MCordingley\LinearAlgebra\Vector;

Since a Vector is a special case of a Matrix, Vector inherits from Matrix. As such, every method available on Matrix is also available on Vector. Vector also exposes additional methods specific to working with vectors.

Creating a Vector differs from creating a Matrix only in that the constructor takes an array of scalars, rather than an array of arrays:

$vector = new Vector([1, 2, 3, 4]);

Note that Vector instances are all row vectors. If you need a column vector, transpose() the vector to get a Matrix with a single column.

If you need to cast a Matrix into a Vector, call the factory method fromMatrix():

$vector = Vector::fromMatrix($matrix);

toArray() is overridden to return an array of scalars to mirror how the constructor works. It is equivalent to calling $matrix->toArray()[0] on a Matrix instance.

getSize() is provided as an alias for getColumnCount(). sum() will return the sum of the Vector elements, while dotProduct($otherVector) will return the sum of the pair-wise products of $vector and $otherVector, and is also availabe aliased as innerProduct($otherVector). outerProduct($otherVector) will return a new Matrix representing the outer product of the two vectors. crossProduct($otherVector) is also available. Vectors may be normalized with normalize(). They may also be projected onto other vectors with project($otherVector). The Euclidean distance may also be calculated between two vectors with euclideanDistance($otherVector).

For measures of vector magnitude, l1Norm(), l2Norm(), and maxNorm() are all available, with length() as an alias for l2Norm().

Links to relevant Wikipedia articles are provided in the function documentation for additional detail.

Change-log


All versions of linearalgebra with dependencies

PHP Build Version
Package Version
Requires php Version ^8.0.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package mcordingley/linearalgebra contains the following files

Loading the files please wait ....