elgigi/matrix

dev-main 2023-01-04 15:18 UTC

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:获取向量值的标准差