elgigi / matrix
dev-main
2023-01-04 15:18 UTC
Requires
- php: ^8.0
Requires (Dev)
- phpunit/phpunit: ^9.5
This package is not auto-updated.
Last update: 2024-09-12 21:36:23 UTC
README
该库实现了PHP中的矩阵和向量的概念。该库的动机首先是为了优化大型矩阵的大尺寸。
安装
Composer
您可以使用Composer安装ElGigi Matrix,这是推荐安装方法。
composer require elgigi/matrix
依赖关系
- PHP ^8.0
用法
Matrix对象的实例化
use ElGigi\Matrix\Matrix; $matrix = new Matrix([ [1, 2, 3], [2, 4, 1], [3, 2, 4], ]);
使用构建器创建Matrix对象
use ElGigi\Matrix\MatrixBuilder; $builder = new MatrixBuilder(); $builder->addRow([1, 2, 3]); $builder->addRow([2, 4, 1]); $builder->addRow([3, 2, 4]); $matrix = $builder->getMatrix();
API
矩阵和向量实现默认接口
\Countable:对象可用于count()函数\JsonSearialize:对象可用于json_encode()函数\IteratorAggregate:对象可用于foreach语句\ArrayAccess:对象可用于像数组一样获取值
矩阵
Matrix::isSquare(): bool:返回矩阵是否为方阵Matrix::count(): int:获取元素数量Matrix::countRows(): int:获取行数Matrix::countColumns(): int:获取列数Matrix::rows(): Generator<VectorInterface>:获取行作为生成器Matrix::columns(): Generator<VectorInterface>:获取列作为生成器Matrix::getRow(int $offset): VectorInterface:获取指定索引的行作为向量Matrix::getColumn(int $offset): VectorInterface:获取指定索引的列作为向量Matrix::getValue(int $y, int $x): mixed:获取指定坐标的值Matrix::map(callable $callback, mixed ...$arg): static:对每个值应用回调并返回新的矩阵对象Matrix::max(): int|float:获取矩阵的最大值Matrix::min(): int|float:获取矩阵的最小值Matrix::sum(): int|float:获取矩阵值的总和Matrix::avg(): int|float:获取矩阵值的平均值Matrix::median(): int|float:获取矩阵值的中位数Matrix::variance(): int|float:获取矩阵值的方差Matrix::deviation(): int|float:获取矩阵值的标准差
向量
Vector::values(): array:获取向量值作为数组Vector::extend(int $length, mixed $value = null): static:使用给定值扩展指定长度的向量Vector::reduce(int $length): static:缩短指定长度的向量Vector::map(callable $callback, mixed ...$arg): static:对每个值应用回调并返回新的向量对象Vector::max(): int|float:获取向量的最大值Vector::min(): int|float:获取向量的最小值Vector::sum(): int|float:获取向量值的总和Vector::avg(): int|float:获取向量值的平均值Vector::median(): int|float:获取向量值的中位数Vector::variance(): int|float:获取向量值的方差Vector::deviation(): int|float:获取向量值的标准差