定制 markbaker/matrix 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

markbaker/matrix

Composer 安装命令:

composer require markbaker/matrix

包简介

PHP Class for working with matrices

README 文档

README

PHP Class for handling Matrices

Build Status Total Downloads Latest Stable Version License

Matrix Transform

Matrix Transform

This library currently provides the following operations:

  • addition
  • direct sum
  • subtraction
  • multiplication
  • division (using [A].[B]-1)
    • division by
    • division into

together with functions for

  • adjoint

  • antidiagonal

  • cofactors

  • determinant

  • diagonal

  • identity

  • inverse

  • minors

  • trace

  • transpose

  • solve

    Given Matrices A and B, calculate X for A.X = B

and classes for

  • Decomposition
    • LU Decomposition with partial row pivoting,

      such that [P].[A] = [L].[U] and [A] = [P]|.[L].[U]

    • QR Decomposition

      such that [A] = [Q].[R]

TO DO

  • power() function
  • Decomposition
    • Cholesky Decomposition
    • EigenValue Decomposition
      • EigenValues
      • EigenVectors

Installation

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 instead (available on packagist as markbaker/matrix-functions).

You'll need to replace markbaker/matrixin 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.

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

$grid = [
    [16,  3,  2, 13],
    [ 5, 10, 11,  8],
    [ 9,  6,  7, 12],
    [ 4, 15, 14,  1],
];

$matrix = new Matrix\Matrix($grid);

The Builder class provides helper methods for creating specific matrices, specifically an identity matrix of a specified size; or a matrix of a specified dimensions, with every cell containing a set value.

$matrix = Matrix\Builder::createFilledMatrix(1, 5, 3);

Will create a matrix of 5 rows and 3 columns, filled with a 1 in every cell; while

$matrix = Matrix\Builder::createIdentityMatrix(3);

will create a 3x3 identity matrix.

Matrix objects are immutable: whenever you call a method or pass a grid to a function that returns a matrix value, a new Matrix object will be returned, and the original will remain unchanged. This also allows you to chain multiple methods as you would for a fluent interface (as long as they are methods that will return a Matrix result).

Performing Mathematical Operations

To perform mathematical operations with Matrices, you can call the appropriate method against a matrix value, passing other values as arguments

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

var_dump($matrix1->multiply($matrix2)->toArray());

or pass all values to the appropriate static method

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

var_dump(Matrix\Operations::multiply($matrix1, $matrix2)->toArray());

You can pass in the arguments as Matrix objects, or as arrays.

If you want to perform the same operation against multiple values (e.g. to add three or more matrices), then you can pass multiple arguments to any of the operations.

Using functions

When calling any of the available functions for a matrix value, you can either call the relevant method for the Matrix object

$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();

or you can call the static method, passing the Matrix object or array as an argument

$grid = [
    [16,  3,  2, 13],
    [ 5, 10, 11,  8],
    [ 9,  6,  7, 12],
    [ 4, 15, 14,  1],
];

$matrix = new Matrix\Matrix($grid);
echo Matrix\Functions::trace($matrix);
$grid = [
    [16,  3,  2, 13],
    [ 5, 10, 11,  8],
    [ 9,  6,  7, 12],
    [ 4, 15, 14,  1],
];

echo Matrix\Functions::trace($grid);

Decomposition

The library also provides classes for matrix decomposition. You can access these using

$grid = [
    [1, 2],
    [3, 4],
];

$matrix = new Matrix\Matrix($grid);

$decomposition = new Matrix\Decomposition\QR($matrix);
$Q = $decomposition->getQ();
$R = $decomposition->getR();

or alternatively us the Decomposition factory, identifying which form of decomposition you want to use

$grid = [
    [1, 2],
    [3, 4],
];

$matrix = new Matrix\Matrix($grid);

$decomposition = Matrix\Decomposition\Decomposition::decomposition(Matrix\Decomposition\Decomposition::QR, $matrix);
$Q = $decomposition->getQ();
$R = $decomposition->getR();

markbaker/matrix 适用场景与选型建议

markbaker/matrix 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 302.57M 次下载、GitHub Stars 达 1.51k, 最近一次更新时间为 2018 年 08 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「mathematics」 「vector」 「matrix」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 markbaker/matrix 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 markbaker/matrix 我们能提供哪些服务?
定制开发 / 二次开发

基于 markbaker/matrix 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 302.57M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1516
  • 点击次数: 35
  • 依赖项目数: 40
  • 推荐数: 1

GitHub 信息

  • Stars: 1512
  • Watchers: 7
  • Forks: 14
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-08-12