package and depencies

This commit is contained in:
RafficMohammed
2023-01-08 02:57:24 +05:30
parent d5332eb421
commit 1d54b8bc7f
4309 changed files with 193331 additions and 172289 deletions

View File

@@ -5,7 +5,11 @@ PHPMatrix
PHP Class for handling Matrices
[![Build Status](https://travis-ci.org/MarkBaker/PHPMatrix.png?branch=2.0)](http://travis-ci.org/MarkBaker/PHPMatrix)
[![Build Status](https://github.com/MarkBaker/PHPMatrix/workflows/main/badge.svg)](https://github.com/MarkBaker/PHPMatrix/actions)
[![Total Downloads](https://img.shields.io/packagist/dt/markbaker/matrix)](https://packagist.org/packages/markbaker/matrix)
[![Latest Stable Version](https://img.shields.io/github/v/release/MarkBaker/PHPMatrix)](https://packagist.org/packages/markbaker/matrix)
[![License](https://img.shields.io/github/license/MarkBaker/PHPMatrix)](https://packagist.org/packages/markbaker/matrix)
[![Matrix Transform](https://imgs.xkcd.com/comics/matrix_transform.png)](https://xkcd.com/184/)
@@ -60,6 +64,24 @@ and classes for
---
# Installation
```shell
composer require markbaker/matrix:^3.0
```
# Important BC Note
If you've previously been using procedural calls to functions and operations using this library, then from version 3.0 you should use [MarkBaker/PHPMatrixFunctions](https://github.com/MarkBaker/PHPMatrixFunctions) instead (available on packagist as [markbaker/matrix-functions](https://packagist.org/packages/markbaker/matrix-functions)).
You'll need to replace `markbaker/matrix`in your `composer.json` file with the new library, but otherwise there should be no difference in the namespacing, or in the way that you have called the Matrix functions in the past, so no actual code changes are required.
```shell
composer require markbaker/matrix-functions:^1.0
```
You should not reference this library (`markbaker/matrix`) in your `composer.json`, composer wil take care of that for you.
# Usage
To create a new Matrix object, provide an array as the constructor argument
@@ -105,7 +127,7 @@ $matrix2 = new Matrix\Matrix([
var_dump($matrix1->multiply($matrix2)->toArray());
```
or pass all values to the appropriate function
or pass all values to the appropriate static method
```php
$matrix1 = new Matrix\Matrix([
[2, 7, 6],
@@ -118,7 +140,7 @@ $matrix2 = new Matrix\Matrix([
[7, 8, 9],
]);
var_dump(Matrix\multiply($matrix1, $matrix2)->toArray());
var_dump(Matrix\Operations::multiply($matrix1, $matrix2)->toArray());
```
You can pass in the arguments as Matrix objects, or as arrays.
@@ -139,30 +161,7 @@ $matrix = new Matrix\Matrix($grid);
echo $matrix->trace();
```
or you can call the function as you would in procedural code, passing the Matrix object as an argument
```php
$grid = [
[16, 3, 2, 13],
[ 5, 10, 11, 8],
[ 9, 6, 7, 12],
[ 4, 15, 14, 1],
];
$matrix = new Matrix\Matrix($grid);
echo Matrix\trace($matrix);
```
When called procedurally using the function, you can pass in the argument as a Matrix object, or as an array.
```php
$grid = [
[16, 3, 2, 13],
[ 5, 10, 11, 8],
[ 9, 6, 7, 12],
[ 4, 15, 14, 1],
];
echo Matrix\trace($grid);
```
As an alternative, it is also possible to call the method directly from the `Functions` class.
or you can call the static method, passing the Matrix object or array as an argument
```php
$grid = [
[16, 3, 2, 13],
@@ -174,7 +173,16 @@ $grid = [
$matrix = new Matrix\Matrix($grid);
echo Matrix\Functions::trace($matrix);
```
Used this way, methods must be called statically, and the argument must be the Matrix object, and cannot be an array.
```php
$grid = [
[16, 3, 2, 13],
[ 5, 10, 11, 8],
[ 9, 6, 7, 12],
[ 4, 15, 14, 1],
];
echo Matrix\Functions::trace($grid);
```
## Decomposition

View File

@@ -4,6 +4,25 @@ namespace Matrix;
class Functions
{
/**
* Validates an array of matrix, converting an array to a matrix if required.
*
* @param Matrix|array $matrix Matrix or an array to treat as a matrix.
* @return Matrix The new matrix
* @throws Exception If argument isn't a valid matrix or array.
*/
private static function validateMatrix($matrix)
{
if (is_array($matrix)) {
$matrix = new Matrix($matrix);
}
if (!$matrix instanceof Matrix) {
throw new Exception('Must be Matrix or array');
}
return $matrix;
}
/**
* Calculate the adjoint of the matrix
*
@@ -25,12 +44,14 @@ class Functions
* The adjugate has sometimes been called the "adjoint", but today the "adjoint" of a matrix normally refers
* to its corresponding adjoint operator, which is its conjugate transpose.
*
* @param Matrix $matrix The matrix whose adjoint we wish to calculate
* @param Matrix|array $matrix The matrix whose adjoint we wish to calculate
* @return Matrix
* @throws Exception
**/
public static function adjoint(Matrix $matrix)
public static function adjoint($matrix)
{
$matrix = self::validateMatrix($matrix);
if (!$matrix->isSquare()) {
throw new Exception('Adjoint can only be calculated for a square matrix');
}
@@ -67,13 +88,15 @@ class Functions
/**
* Return the cofactors of this matrix
*
* @param Matrix $matrix The matrix whose cofactors we wish to calculate
* @param Matrix|array $matrix The matrix whose cofactors we wish to calculate
* @return Matrix
*
* @throws Exception
*/
public static function cofactors(Matrix $matrix)
public static function cofactors($matrix)
{
$matrix = self::validateMatrix($matrix);
if (!$matrix->isSquare()) {
throw new Exception('Cofactors can only be calculated for a square matrix');
}
@@ -141,12 +164,14 @@ class Functions
/**
* Return the determinant of this matrix
*
* @param Matrix $matrix The matrix whose determinant we wish to calculate
* @param Matrix|array $matrix The matrix whose determinant we wish to calculate
* @return float
* @throws Exception
**/
public static function determinant(Matrix $matrix)
public static function determinant($matrix)
{
$matrix = self::validateMatrix($matrix);
if (!$matrix->isSquare()) {
throw new Exception('Determinant can only be calculated for a square matrix');
}
@@ -157,12 +182,14 @@ class Functions
/**
* Return the diagonal of this matrix
*
* @param Matrix $matrix The matrix whose diagonal we wish to calculate
* @param Matrix|array $matrix The matrix whose diagonal we wish to calculate
* @return Matrix
* @throws Exception
**/
public static function diagonal(Matrix $matrix)
public static function diagonal($matrix)
{
$matrix = self::validateMatrix($matrix);
if (!$matrix->isSquare()) {
throw new Exception('Diagonal can only be extracted from a square matrix');
}
@@ -181,12 +208,14 @@ class Functions
/**
* Return the antidiagonal of this matrix
*
* @param Matrix $matrix The matrix whose antidiagonal we wish to calculate
* @param Matrix|array $matrix The matrix whose antidiagonal we wish to calculate
* @return Matrix
* @throws Exception
**/
public static function antidiagonal(Matrix $matrix)
public static function antidiagonal($matrix)
{
$matrix = self::validateMatrix($matrix);
if (!$matrix->isSquare()) {
throw new Exception('Anti-Diagonal can only be extracted from a square matrix');
}
@@ -207,12 +236,14 @@ class Functions
* The identity matrix, or sometimes ambiguously called a unit matrix, of size n is the n × n square matrix
* with ones on the main diagonal and zeros elsewhere
*
* @param Matrix $matrix The matrix whose identity we wish to calculate
* @param Matrix|array $matrix The matrix whose identity we wish to calculate
* @return Matrix
* @throws Exception
**/
public static function identity(Matrix $matrix)
public static function identity($matrix)
{
$matrix = self::validateMatrix($matrix);
if (!$matrix->isSquare()) {
throw new Exception('Identity can only be created for a square matrix');
}
@@ -225,12 +256,14 @@ class Functions
/**
* Return the inverse of this matrix
*
* @param Matrix $matrix The matrix whose inverse we wish to calculate
* @param Matrix|array $matrix The matrix whose inverse we wish to calculate
* @return Matrix
* @throws Exception
**/
public static function inverse(Matrix $matrix, string $type = 'inverse')
public static function inverse($matrix, string $type = 'inverse')
{
$matrix = self::validateMatrix($matrix);
if (!$matrix->isSquare()) {
throw new Exception(ucfirst($type) . ' can only be calculated for a square matrix');
}
@@ -281,12 +314,14 @@ class Functions
* calculating matrix cofactors, which in turn are useful for computing both the determinant and inverse of
* square matrices.
*
* @param Matrix $matrix The matrix whose minors we wish to calculate
* @param Matrix|array $matrix The matrix whose minors we wish to calculate
* @return Matrix
* @throws Exception
**/
public static function minors(Matrix $matrix)
public static function minors($matrix)
{
$matrix = self::validateMatrix($matrix);
if (!$matrix->isSquare()) {
throw new Exception('Minors can only be calculated for a square matrix');
}
@@ -299,12 +334,14 @@ class Functions
* The trace is defined as the sum of the elements on the main diagonal (the diagonal from the upper left to the lower right)
* of the matrix
*
* @param Matrix $matrix The matrix whose trace we wish to calculate
* @param Matrix|array $matrix The matrix whose trace we wish to calculate
* @return float
* @throws Exception
**/
public static function trace(Matrix $matrix)
public static function trace($matrix)
{
$matrix = self::validateMatrix($matrix);
if (!$matrix->isSquare()) {
throw new Exception('Trace can only be extracted from a square matrix');
}
@@ -321,11 +358,13 @@ class Functions
/**
* Return the transpose of this matrix
*
* @param Matrix $matrix The matrix whose transpose we wish to calculate
* @param Matrix|\a $matrix The matrix whose transpose we wish to calculate
* @return Matrix
**/
public static function transpose(Matrix $matrix)
public static function transpose($matrix)
{
$matrix = self::validateMatrix($matrix);
$array = array_values(array_merge([null], $matrix->toArray()));
$grid = call_user_func_array(
'array_map',

View File

@@ -1,32 +0,0 @@
<?php
/**
*
* Function code for the matrix adjoint() function
*
* @copyright Copyright (c) 2018 Mark Baker (https://github.com/MarkBaker/PHPMatrix)
* @license https://opensource.org/licenses/MIT MIT
*/
namespace Matrix;
/**
* Returns the adjoint of a matrix or an array.
*
* @param Matrix|array $matrix Matrix or an array to treat as a matrix.
* @return Matrix The new matrix
* @throws Exception If argument isn't a valid matrix or array.
*/
if (!function_exists(__NAMESPACE__ . '\\adjoint')) {
function adjoint($matrix): Matrix
{
if (is_array($matrix)) {
$matrix = new Matrix($matrix);
}
if (!$matrix instanceof Matrix) {
throw new Exception('Must be Matrix or array');
}
return Functions::adjoint($matrix);
}
}

View File

@@ -1,31 +0,0 @@
<?php
/**
*
* Function code for the matrix antidiagonal() function
*
* @copyright Copyright (c) 2018 Mark Baker (https://github.com/MarkBaker/PHPMatrix)
* @license https://opensource.org/licenses/MIT MIT
*/
namespace Matrix;
/**
* Returns the antidiagonal of a matrix or an array.
*
* @param Matrix|array $matrix Matrix or an array to treat as a matrix.
* @return Matrix The new matrix
* @throws Exception If argument isn't a valid matrix or array.
*/
if (!function_exists(__NAMESPACE__ . '\\antidiagonal')) {
function antidiagonal($matrix): Matrix
{
if (is_array($matrix)) {
$matrix = new Matrix($matrix);
}
if (!$matrix instanceof Matrix) {
throw new Exception('Must be Matrix or array');
}
return Functions::antidiagonal($matrix);
}
}

View File

@@ -1,32 +0,0 @@
<?php
/**
*
* Function code for the matrix cofactors() function
*
* @copyright Copyright (c) 2018 Mark Baker (https://github.com/MarkBaker/PHPMatrix)
* @license https://opensource.org/licenses/MIT MIT
*/
namespace Matrix;
/**
* Returns the cofactors of a matrix or an array.
*
* @param Matrix|array $matrix Matrix or an array to treat as a matrix.
* @return Matrix The new matrix
* @throws Exception If argument isn't a valid matrix or array.
*/
if (!function_exists(__NAMESPACE__ . '\\cofactors')) {
function cofactors($matrix): Matrix
{
if (is_array($matrix)) {
$matrix = new Matrix($matrix);
}
if (!$matrix instanceof Matrix) {
throw new Exception('Must be Matrix or array');
}
return Functions::cofactors($matrix);
}
}

View File

@@ -1,32 +0,0 @@
<?php
/**
*
* Function code for the matrix determinant() function
*
* @copyright Copyright (c) 2018 Mark Baker (https://github.com/MarkBaker/PHPMatrix)
* @license https://opensource.org/licenses/MIT MIT
*/
namespace Matrix;
/**
* Returns the determinant of a matrix or an array.
*
* @param Matrix|array $matrix Matrix or an array to treat as a matrix.
* @return float Matrix determinant
* @throws Exception If argument isn't a valid matrix or array.
*/
if (!function_exists(__NAMESPACE__ . '\\determinant')) {
function determinant($matrix): float
{
if (is_array($matrix)) {
$matrix = new Matrix($matrix);
}
if (!$matrix instanceof Matrix) {
throw new Exception('Must be Matrix or array');
}
return Functions::determinant($matrix);
}
}

View File

@@ -1,32 +0,0 @@
<?php
/**
*
* Function code for the matrix diagonal() function
*
* @copyright Copyright (c) 2018 Mark Baker (https://github.com/MarkBaker/PHPMatrix)
* @license https://opensource.org/licenses/MIT MIT
*/
namespace Matrix;
/**
* Returns the diagonal of a matrix or an array.
*
* @param Matrix|array $matrix Matrix or an array to treat as a matrix.
* @return Matrix The new matrix
* @throws Exception If argument isn't a valid matrix or array.
*/
if (!function_exists(__NAMESPACE__ . '\\diagonal')) {
function diagonal($matrix): Matrix
{
if (is_array($matrix)) {
$matrix = new Matrix($matrix);
}
if (!$matrix instanceof Matrix) {
throw new Exception('Must be Matrix or array');
}
return Functions::diagonal($matrix);
}
}

View File

@@ -1,32 +0,0 @@
<?php
/**
*
* Function code for the matrix identity() function
*
* @copyright Copyright (c) 2018 Mark Baker (https://github.com/MarkBaker/PHPMatrix)
* @license https://opensource.org/licenses/MIT MIT
*/
namespace Matrix;
/**
* Returns the identity of a matrix or an array.
*
* @param Matrix|array $matrix Matrix or an array to treat as a matrix.
* @return Matrix The identity matrix
* @throws Exception If argument isn't a valid matrix or array.
*/
if (!function_exists(__NAMESPACE__ . '\\identity')) {
function identity($matrix): Matrix
{
if (is_array($matrix)) {
$matrix = new Matrix($matrix);
}
if (!$matrix instanceof Matrix) {
throw new Exception('Must be Matrix or array');
}
return Functions::identity($matrix);
}
}

View File

@@ -1,32 +0,0 @@
<?php
/**
*
* Function code for the matrix inverse() function
*
* @copyright Copyright (c) 2018 Mark Baker (https://github.com/MarkBaker/PHPMatrix)
* @license https://opensource.org/licenses/MIT MIT
*/
namespace Matrix;
/**
* Returns the inverse of a matrix or an array.
*
* @param Matrix|array $matrix Matrix or an array to treat as a matrix.
* @return Matrix The new matrix
* @throws Exception If argument isn't a valid matrix or array.
*/
if (!function_exists(__NAMESPACE__ . '\\inverse')) {
function inverse($matrix): Matrix
{
if (is_array($matrix)) {
$matrix = new Matrix($matrix);
}
if (!$matrix instanceof Matrix) {
throw new Exception('Must be Matrix or array');
}
return Functions::inverse($matrix);
}
}

View File

@@ -1,32 +0,0 @@
<?php
/**
*
* Function code for the matrix minors() function
*
* @copyright Copyright (c) 2018 Mark Baker (https://github.com/MarkBaker/PHPMatrix)
* @license https://opensource.org/licenses/MIT MIT
*/
namespace Matrix;
/**
* Returns the minors of a matrix or an array.
*
* @param Matrix|array $matrix Matrix or an array to treat as a matrix.
* @return Matrix The new matrix
* @throws Exception If argument isn't a valid matrix or array.
*/
if (!function_exists(__NAMESPACE__ . '\\minors')) {
function minors($matrix): Matrix
{
if (is_array($matrix)) {
$matrix = new Matrix($matrix);
}
if (!$matrix instanceof Matrix) {
throw new Exception('Must be Matrix or array');
}
return Functions::minors($matrix);
}
}

View File

@@ -1,32 +0,0 @@
<?php
/**
*
* Function code for the matrix trace() function
*
* @copyright Copyright (c) 2018 Mark Baker (https://github.com/MarkBaker/PHPMatrix)
* @license https://opensource.org/licenses/MIT MIT
*/
namespace Matrix;
/**
* Returns the trace of a matrix or an array.
*
* @param Matrix|array $matrix Matrix or an array to treat as a matrix.
* @return float The trace of the matrix
* @throws Exception If argument isn't a valid matrix or array.
*/
if (!function_exists(__NAMESPACE__ . '\\trace')) {
function trace($matrix): float
{
if (is_array($matrix)) {
$matrix = new Matrix($matrix);
}
if (!$matrix instanceof Matrix) {
throw new Exception('Must be Matrix or array');
}
return Functions::trace($matrix);
}
}

View File

@@ -1,32 +0,0 @@
<?php
/**
*
* Function code for the matrix transpose() function
*
* @copyright Copyright (c) 2018 Mark Baker (https://github.com/MarkBaker/PHPMatrix)
* @license https://opensource.org/licenses/MIT MIT
*/
namespace Matrix;
/**
* Returns the transpose of a matrix or an array.
*
* @param Matrix|array $matrix Matrix or an array to treat as a matrix.
* @return Matrix The transposed matrix
* @throws Exception If argument isn't a valid matrix or array.
*/
if (!function_exists(__NAMESPACE__ . '\\transpose')) {
function transpose($matrix): Matrix
{
if (is_array($matrix)) {
$matrix = new Matrix($matrix);
}
if (!$matrix instanceof Matrix) {
throw new Exception('Must be Matrix or array');
}
return Functions::transpose($matrix);
}
}

View File

@@ -28,7 +28,6 @@ use Matrix\Decomposition\QR;
* @method Matrix diagonal()
* @method Matrix identity()
* @method Matrix inverse()
* @method Matrix pseudoInverse()
* @method Matrix minors()
* @method float trace()
* @method Matrix transpose()
@@ -411,12 +410,13 @@ class Matrix
{
$functionName = strtolower(str_replace('_', '', $functionName));
if (in_array($functionName, self::$functions, true) || in_array($functionName, self::$operations, true)) {
$functionName = "\\" . __NAMESPACE__ . "\\{$functionName}";
if (is_callable($functionName)) {
$arguments = array_values(array_merge([$this], $arguments));
return call_user_func_array($functionName, $arguments);
}
// Test for function calls
if (in_array($functionName, self::$functions, true)) {
return Functions::$functionName($this, ...$arguments);
}
// Test for operation calls
if (in_array($functionName, self::$operations, true)) {
return Operations::$functionName($this, ...$arguments);
}
throw new Exception('Function or Operation does not exist');
}

View File

@@ -0,0 +1,157 @@
<?php
namespace Matrix;
use Matrix\Operators\Addition;
use Matrix\Operators\DirectSum;
use Matrix\Operators\Division;
use Matrix\Operators\Multiplication;
use Matrix\Operators\Subtraction;
class Operations
{
public static function add(...$matrixValues): Matrix
{
if (count($matrixValues) < 2) {
throw new Exception('Addition operation requires at least 2 arguments');
}
$matrix = array_shift($matrixValues);
if (is_array($matrix)) {
$matrix = new Matrix($matrix);
}
if (!$matrix instanceof Matrix) {
throw new Exception('Addition arguments must be Matrix or array');
}
$result = new Addition($matrix);
foreach ($matrixValues as $matrix) {
$result->execute($matrix);
}
return $result->result();
}
public static function directsum(...$matrixValues): Matrix
{
if (count($matrixValues) < 2) {
throw new Exception('DirectSum operation requires at least 2 arguments');
}
$matrix = array_shift($matrixValues);
if (is_array($matrix)) {
$matrix = new Matrix($matrix);
}
if (!$matrix instanceof Matrix) {
throw new Exception('DirectSum arguments must be Matrix or array');
}
$result = new DirectSum($matrix);
foreach ($matrixValues as $matrix) {
$result->execute($matrix);
}
return $result->result();
}
public static function divideby(...$matrixValues): Matrix
{
if (count($matrixValues) < 2) {
throw new Exception('Division operation requires at least 2 arguments');
}
$matrix = array_shift($matrixValues);
if (is_array($matrix)) {
$matrix = new Matrix($matrix);
}
if (!$matrix instanceof Matrix) {
throw new Exception('Division arguments must be Matrix or array');
}
$result = new Division($matrix);
foreach ($matrixValues as $matrix) {
$result->execute($matrix);
}
return $result->result();
}
public static function divideinto(...$matrixValues): Matrix
{
if (count($matrixValues) < 2) {
throw new Exception('Division operation requires at least 2 arguments');
}
$matrix = array_pop($matrixValues);
$matrixValues = array_reverse($matrixValues);
if (is_array($matrix)) {
$matrix = new Matrix($matrix);
}
if (!$matrix instanceof Matrix) {
throw new Exception('Division arguments must be Matrix or array');
}
$result = new Division($matrix);
foreach ($matrixValues as $matrix) {
$result->execute($matrix);
}
return $result->result();
}
public static function multiply(...$matrixValues): Matrix
{
if (count($matrixValues) < 2) {
throw new Exception('Multiplication operation requires at least 2 arguments');
}
$matrix = array_shift($matrixValues);
if (is_array($matrix)) {
$matrix = new Matrix($matrix);
}
if (!$matrix instanceof Matrix) {
throw new Exception('Multiplication arguments must be Matrix or array');
}
$result = new Multiplication($matrix);
foreach ($matrixValues as $matrix) {
$result->execute($matrix);
}
return $result->result();
}
public static function subtract(...$matrixValues): Matrix
{
if (count($matrixValues) < 2) {
throw new Exception('Subtraction operation requires at least 2 arguments');
}
$matrix = array_shift($matrixValues);
if (is_array($matrix)) {
$matrix = new Matrix($matrix);
}
if (!$matrix instanceof Matrix) {
throw new Exception('Subtraction arguments must be Matrix or array');
}
$result = new Subtraction($matrix);
foreach ($matrixValues as $matrix) {
$result->execute($matrix);
}
return $result->result();
}
}

View File

@@ -1,46 +0,0 @@
<?php
/**
*
* Function code for the matrix addition operation
*
* @copyright Copyright (c) 2018 Mark Baker (https://github.com/MarkBaker/PHPMatrix)
* @license https://opensource.org/licenses/MIT MIT
*/
namespace Matrix;
use Matrix\Operators\Addition;
/**
* Adds two or more matrices
*
* @param array<int, mixed> $matrixValues The matrices to add
* @return Matrix
* @throws Exception
*/
if (!function_exists(__NAMESPACE__ . '\\add')) {
function add(...$matrixValues): Matrix
{
if (count($matrixValues) < 2) {
throw new Exception('Addition operation requires at least 2 arguments');
}
$matrix = array_shift($matrixValues);
if (is_array($matrix)) {
$matrix = new Matrix($matrix);
}
if (!$matrix instanceof Matrix) {
throw new Exception('Addition arguments must be Matrix or array');
}
$result = new Addition($matrix);
foreach ($matrixValues as $matrix) {
$result->execute($matrix);
}
return $result->result();
}
}

View File

@@ -1,46 +0,0 @@
<?php
/**
*
* Function code for the matrix direct sum operation
*
* @copyright Copyright (c) 2018 Mark Baker (https://github.com/MarkBaker/PHPMatrix)
* @license https://opensource.org/licenses/MIT MIT
*/
namespace Matrix;
use Matrix\Operators\DirectSum;
/**
* Adds two or more matrices
*
* @param array<int, mixed> $matrixValues The matrices to add
* @return Matrix
* @throws Exception
*/
if (!function_exists(__NAMESPACE__ . '\\directsum')) {
function directsum(...$matrixValues): Matrix
{
if (count($matrixValues) < 2) {
throw new Exception('DirectSum operation requires at least 2 arguments');
}
$matrix = array_shift($matrixValues);
if (is_array($matrix)) {
$matrix = new Matrix($matrix);
}
if (!$matrix instanceof Matrix) {
throw new Exception('DirectSum arguments must be Matrix or array');
}
$result = new DirectSum($matrix);
foreach ($matrixValues as $matrix) {
$result->execute($matrix);
}
return $result->result();
}
}

View File

@@ -1,46 +0,0 @@
<?php
/**
*
* Function code for the matrix division operation
*
* @copyright Copyright (c) 2013-2018 Mark Baker (https://github.com/MarkBaker/PHPComplex)
* @license https://opensource.org/licenses/MIT MIT
*/
namespace Matrix;
use Matrix\Operators\Division;
/**
* Divides two or more matrix numbers
*
* @param array<int, mixed> $matrixValues The matrices to divide
* @return Matrix
* @throws Exception
*/
if (!function_exists(__NAMESPACE__ . '\\divideby')) {
function divideby(...$matrixValues): Matrix
{
if (count($matrixValues) < 2) {
throw new Exception('Division operation requires at least 2 arguments');
}
$matrix = array_shift($matrixValues);
if (is_array($matrix)) {
$matrix = new Matrix($matrix);
}
if (!$matrix instanceof Matrix) {
throw new Exception('Division arguments must be Matrix or array');
}
$result = new Division($matrix);
foreach ($matrixValues as $matrix) {
$result->execute($matrix);
}
return $result->result();
}
}

View File

@@ -1,47 +0,0 @@
<?php
/**
*
* Function code for the matrix division operation
*
* @copyright Copyright (c) 2013-2018 Mark Baker (https://github.com/MarkBaker/PHPMatrix)
* @license https://opensource.org/licenses/MIT MIT
*/
namespace Matrix;
use Matrix\Operators\Division;
/**
* Divides two or more matrix numbers
*
* @param array<int, mixed> $matrixValues The numbers to divide
* @return Matrix
* @throws Exception
*/
if (!function_exists(__NAMESPACE__ . '\\divideinto')) {
function divideinto(...$matrixValues): Matrix
{
if (count($matrixValues) < 2) {
throw new Exception('Division operation requires at least 2 arguments');
}
$matrix = array_pop($matrixValues);
$matrixValues = array_reverse($matrixValues);
if (is_array($matrix)) {
$matrix = new Matrix($matrix);
}
if (!$matrix instanceof Matrix) {
throw new Exception('Division arguments must be Matrix or array');
}
$result = new Division($matrix);
foreach ($matrixValues as $matrix) {
$result->execute($matrix);
}
return $result->result();
}
}

View File

@@ -1,46 +0,0 @@
<?php
/**
*
* Function code for the matrix multiplication operation
*
* @copyright Copyright (c) 2018 Mark Baker (https://github.com/MarkBaker/PHPMatrix)
* @license https://opensource.org/licenses/MIT MIT
*/
namespace Matrix;
use Matrix\Operators\Multiplication;
/**
* Multiplies two or more matrices
*
* @param array<int, mixed> $matrixValues The matrices to multiply
* @return Matrix
* @throws Exception
*/
if (!function_exists(__NAMESPACE__ . '\\multiply')) {
function multiply(...$matrixValues): Matrix
{
if (count($matrixValues) < 2) {
throw new Exception('Multiplication operation requires at least 2 arguments');
}
$matrix = array_shift($matrixValues);
if (is_array($matrix)) {
$matrix = new Matrix($matrix);
}
if (!$matrix instanceof Matrix) {
throw new Exception('Multiplication arguments must be Matrix or array');
}
$result = new Multiplication($matrix);
foreach ($matrixValues as $matrix) {
$result->execute($matrix);
}
return $result->result();
}
}

View File

@@ -1,46 +0,0 @@
<?php
/**
*
* Function code for the matrix subtraction operation
*
* @copyright Copyright (c) 2018 Mark Baker (https://github.com/MarkBaker/PHPMatrix)
* @license https://opensource.org/licenses/MIT MIT
*/
namespace Matrix;
use Matrix\Operators\Subtraction;
/**
* Subtracts two or more matrices
*
* @param array<int, mixed> $matrixValues The matrices to subtract
* @return Matrix
* @throws Exception
*/
if (!function_exists(__NAMESPACE__ . '\\subtract')) {
function subtract(...$matrixValues): Matrix
{
if (count($matrixValues) < 2) {
throw new Exception('Subtraction operation requires at least 2 arguments');
}
$matrix = array_shift($matrixValues);
if (is_array($matrix)) {
$matrix = new Matrix($matrix);
}
if (!$matrix instanceof Matrix) {
throw new Exception('Subtraction arguments must be Matrix or array');
}
$result = new Subtraction($matrix);
foreach ($matrixValues as $matrix) {
$result->execute($matrix);
}
return $result->result();
}
}

View File

@@ -11,12 +11,6 @@
"email": "mark@demon-angel.eu"
}
],
"config": {
"sort-packages": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
},
"require": {
"php": "^7.1 || ^8.0"
},
@@ -33,48 +27,12 @@
"autoload": {
"psr-4": {
"Matrix\\": "classes/src/"
},
"files": [
"classes/src/Functions/adjoint.php",
"classes/src/Functions/antidiagonal.php",
"classes/src/Functions/cofactors.php",
"classes/src/Functions/determinant.php",
"classes/src/Functions/diagonal.php",
"classes/src/Functions/identity.php",
"classes/src/Functions/inverse.php",
"classes/src/Functions/minors.php",
"classes/src/Functions/trace.php",
"classes/src/Functions/transpose.php",
"classes/src/Operations/add.php",
"classes/src/Operations/directsum.php",
"classes/src/Operations/subtract.php",
"classes/src/Operations/multiply.php",
"classes/src/Operations/divideby.php",
"classes/src/Operations/divideinto.php"
]
}
},
"autoload-dev": {
"psr-4": {
"MatrixTest\\": "unitTests/classes/src/"
},
"files": [
"classes/src/Functions/adjoint.php",
"classes/src/Functions/antidiagonal.php",
"classes/src/Functions/cofactors.php",
"classes/src/Functions/determinant.php",
"classes/src/Functions/diagonal.php",
"classes/src/Functions/identity.php",
"classes/src/Functions/inverse.php",
"classes/src/Functions/minors.php",
"classes/src/Functions/trace.php",
"classes/src/Functions/transpose.php",
"classes/src/Operations/add.php",
"classes/src/Operations/directsum.php",
"classes/src/Operations/subtract.php",
"classes/src/Operations/multiply.php",
"classes/src/Operations/divideby.php",
"classes/src/Operations/divideinto.php"
]
}
},
"scripts": {
"style": "phpcs --report-width=200 --standard=PSR2 --report=summary,full classes/src/ unitTests/classes/src -n",
@@ -85,5 +43,10 @@
"versions": "phpcs --report-width=200 --standard=PHPCompatibility --report=summary,full classes/src/ --runtime-set testVersion 7.2- -n",
"coverage": "phpunit -c phpunit.xml.dist --coverage-text --coverage-html ./build/coverage"
},
"minimum-stability": "dev"
}
"minimum-stability": "dev",
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}

View File

@@ -6,13 +6,13 @@ use Matrix\Decomposition\QR;
include __DIR__ . '/../vendor/autoload.php';
$grid = [
[1, 2],
[3, 4],
[0, 1],
[-1, 0],
];
$targetGrid = [
[-1],
[-2],
[2],
];
$matrix = new Matrix($grid);