ajthenewguy/php7-matrix

利用 PHP7 数据结构的 Matrix 类

v2.0.0 2020-06-01 01:58 UTC

This package is auto-updated.

Last update: 2024-09-29 05:29:10 UTC


README

利用 PHP7 数据结构的 PHP Matrix。

使用

可以使用数组数组的实例化实例,第一个数组应该是行,每个成员是列/单元格值。

实例化

use Matrix\Matrix;

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

// Create a 3x3 matrix filled with 1's.

$Matrix = Matrix::create($width = 3, $height = 3, 1);

// Create a 5x5 identity matrix.

$Matrix = Matrix::identity(5);

非可变实例方法

public added($input): Matrix

返回一个新的矩阵,其中添加了此实例的矩阵或数值。

public determinant(): numeric

可以从正方形矩阵的元素计算出的标量值。

public divided($input): Matrix

返回一个新的矩阵,除以矩阵或数值。

public equals(Matrix $m): bool

检查矩阵是否等于提供的矩阵。

public exponentiated($input): Matrix

返回一个新的矩阵,其指数为数值。

public get(int $x_column, int $y_row)

获取给定坐标/偏移量的值;基于0的索引。

public getAdjugate(): Matrix

获取矩阵的伴随/伴随矩阵的转置。

public getAntidiagonal(): Vector

获取从左下角到右上角的对角线向量。

public getCofactors(): Matrix

返回所有组件的余因子的新矩阵。

public getColumn(int $offset): Vector

返回给定偏移量处的列向量。

public getData(): Vector

获取包含矩阵数据的内部向量。

public getDiagonal(): Vector

获取对角线组件的向量。

public getInverse(): Matrix

返回此矩阵的逆矩阵的新矩阵。

public getMinors(int $column_x, int $row_y = 0): Matrix

获取给定列/行的此矩阵的余子矩阵。

public getNegative(): Matrix

获取此矩阵的负矩阵。

public getRow(int $y): Vector

返回给定偏移量处的行向量。

public getTranspose(): Matrix

获取沿对角线翻转此矩阵的组件的矩阵。

public isDiagonal(): bool

检查矩阵是否是对角矩阵,即主对角线以外的条目都为零的矩阵。

public isTriangular(): bool

检查矩阵是否是三角矩阵,即上三角或下三角矩阵。

public isLowerTriangular(): bool

检查主对角线以上的条目是否为零。

public isUpperTriangular(): bool

检查主对角线以下的条目是否为零。

public isInvertible(): bool

检查此矩阵是否可逆,即行列式非零的矩阵。

public isSquare(): bool

检查矩阵的行数和列数是否相同。

public isSymmetric(): bool

检查矩阵是否等于其转置。

public isSkewSymmetric(): bool

检查矩阵的转置是否等于其负数。

public map(callable $callback): Matrix

返回应用于此矩阵每个组件的回调的矩阵。

public mapMatrix(Matrix $matrix, callable $callback, $validation = self::SAME): Matrix

返回应用于此矩阵每个组件的回调的矩阵,并传递从提供的矩阵中提供的相应值。

public multiplied($input): Matrix

返回此实例乘以矩阵或值的新矩阵。

public subtracted($input): Matrix

返回从此实例减去矩阵或值的新矩阵。

public toArray(): array

返回矩阵数据的数组表示。

public trace()

获取对角线的和。

可变实例方法

public add($input): Matrix

将矩阵或数值添加到矩阵中。

public apply(callable $callback): Matrix

对矩阵的每个组件应用回调。

public applyMatrix(Matrix $matrix, callable $callback, $validation = self::SAME): Matrix

对矩阵的每个组件应用回调,传递从提供的矩阵中提供的相应值。

public divide($input): Matrix

将此矩阵除以矩阵或数值。

public exponential($input): Matrix

用数值对矩阵进行指数运算。

public inverse(): Matrix

更新此矩阵到其逆矩阵。

public multiply($input): Matrix

将矩阵或值乘以此实例。

public set(int $x_column, int $y_row, $value): void

设置给定坐标/偏移量的值;基于0的索引。

public setData(iterable $table = []): Matrix

设置内部数据向量。

public subtract($input): Matrix

从此实例减去矩阵或值。

public transpose(): Matrix

沿对角线翻转矩阵。

矩阵映射算法

当将一个矩阵映射到另一个矩阵时,它们的维度必须完全匹配或必须反映(转置维度)。列数和行数必须匹配,或者一个的行数必须匹配另一个的列数。

示例:Matrix->multiply(Matrix)

矩阵 A

[1, 2, 3]
[4, 5, 6]

矩阵 B

[7, 8]
[9, 10]
[11, 12]

预期

[ 58,  64]
[139, 154]

步骤 1 - 1×7

A:             B:
[1, -, -] x [7, -]
[-, -, -]   [-, -]
            [-, -]

步骤 2 - 2×9

A:             B:
[-, 2, -] x [-, -]
[-, -, -]   [9, -]
            [-, -]

步骤 3 - 3×11

A:             B:
[-, -, 3] x [ -, -]
[-, -, -]   [ -, -]
            [11, -]

步骤 4 - 计算乘积之和

C:
[58, -]
[ -, -]

步骤 5 - 1×8

A:             B:
[1, -, -] x [-, 8]
[-, -, -]   [-, -]
            [-, -]

步骤 6 - 2×10

A:             B:
[-, 2, -] x [-,  -]
[-, -, -]   [-, 10]
            [-,  -]

步骤 7 - 3×12

A:             B:
[-, -, 3] x [-,  -]
[-, -, -]   [-,  -]
            [-, 12]

步骤 8 - 计算乘积之和

C:
[58, 64]
[ -, -]

步骤 9 - 4×7

A:             B:
[-, -, -] x [7, -]
[4, -, -]   [-, -]
            [-, -]

步骤 10 - 5×9

A:             B:
[-, -, -] x [-, -]
[-, 5, -]   [9, -]
            [-, -]

步骤 11 - 6×11

A:             B:
[-, -, -] x [-,  -]
[-, -, 6]   [-,  -]
            [11, -]

步骤 12 - 计算乘积之和

C:
[ 58, 64]
[139,  -]

步骤 13 - 4×8

A:             B:
[-, -, -] x [-, 8]
[4, -, -]   [-, -]
            [-, -]

步骤 14 - 5×10

A:             B:
[-, -, -] x [-,  -]
[-, 5, -]   [-, 10]
            [-,  -]

步骤 15 - 6×12

A:             B:
[-, -, -] x [-,  -]
[-, -, 6]   [-,  -]
            [-, 12]

步骤 16 - 计算乘积之和

C:
[ 58,  64]
[139, 154]