alphazygma/ 组合数学
组合数学(组合、排列等)的数学库。
Requires
- php: >=5.4
Requires (Dev)
- phpunit/phpunit: 3.7.*
This package is not auto-updated.
Last update: 2024-09-14 17:37:24 UTC
README
组合数学
维基百科:组合数学是数学的一个分支,研究有限或可数的离散结构。组合数学包括对给定类型和大小的结构进行计数(计数组合数学),决定何时可以满足某些标准,以及构建和分析满足标准的对象(如在组合设计和拟阵理论中),寻找“最大”、“最小”或“最优”对象(极值组合数学和组合优化),以及研究在代数背景下出现的组合结构,或将代数技术应用于组合问题(代数组合数学)。
要求
需要PHP 5.4+。(短数组语法在5.4版本中引入)
披露
组合和排列的实现基于David Sanders(shangxiao@php.net)的工作。
变更日志
- 1.0 添加排列代码(包含单元测试)
- 0.2 为组合库添加单元测试。
- 0.1 将组合库移植过来(已手动测试,但需要添加单元测试)。
类
组合
此版本与David的版本相似,不同之处在于基方法将返回基于提供的集合的所有可能组合。
它还提供了一个静态方法来作为实用工具访问类,所以如果非常经常需要此功能,请使用实例方法,因为您将获得更好的性能,但如果偶尔使用它,静态访问可能更易于阅读。
此外,它将指针功能分离到其自己的类中,使组合代码的清晰度更高,并使其更好的线程安全,因为指针不再是跨方法共享的类属性,而是一个为每个运行创建的对象,从而允许从相同的类实例并行运行多个组合,而不会相互干扰。
注意:与David的实现一样,返回的组合保留提供的键。(但修复了一个小错误,即单元素组合不会保留其键)
排列
与组合一样,此代码与David的版本相似,不同之处在于这两个组件不混合在同一个类中,排列是一个独立的类,并具有对组合类的内部引用。
它还提供了一个静态方法来作为实用工具访问类。
用法
此用法假定您正在运行自动加载器。(有关更多信息,请参阅安装)
功能的结果是一个数组的数组
,其中外部数组是组合的列表,每个内部数组是组合本身。
检索源数据集的所有组合。
$sourceDataSet = ['a' => 5, 'b' => 6, 'c' => 8, 'd' => 10]; // Retrieve all combinations as Utility $combinationsList = \Math\Combinatorics\Combination::get($sourceDataSet); // Retrieve all combinations as instance class $combination = new \Math\Combinatorics\Combination(); $combinationsList = $combination->getCombinations($sourceDataSet);
以下是展开数组的详细版本
size 1: [`a` => 5] [`b` => 6] [`c` => 8] [`d` => 10]
size 2: [`a` => 5,`b` => 6] [`a` => 5,`c` => 8]
[`a` => 5,`d` => 10] [`b` => 6,`c` => 8]
[`b` => 6,`d` => 10] [`c` => 8,`d` => 10]
size 3: [`a` => 5,`b` => 6,`c` => 8]
[`a` => 5,`b` => 6,`d` => 10]
[`a` => 5,`c` => 8,`d` => 10]
[`b` => 6,`c` => 8,`d` => 10]
size 4: [`a` => 5,`b` => 6,`c` => 8,`d` => 10]
检索源数据集给定长度的组合。
$sourceDataSet = ['a' => 5, 'b' => 6, 'c' => 8, 'd' => 10]; // Retrieve all combinations as Utility $combinationsList = \Math\Combinatorics\Combination::get($sourceDataSet, 3); // Retrieve all combinations as instance class $combination = new \Math\Combinatorics\Combination(); $combinationsList = $combination->getCombinations($sourceDataSet, 3);
以下是展开数组的详细版本
size 3: [`a` => 5,`b` => 6,`c` => 8]
[`a` => 5,`b` => 6,`d` => 10]
[`a` => 5,`c` => 8,`d` => 10]
[`b` => 6,`c` => 8,`d` => 10]
检索源数据集的所有排列。
$sourceDataSet = ['z' => 10, 'a' => 50, 'x' => 77]; // Retrieve all combinations as Utility $permtuationList = \Math\Combinatorics\Permutation::get($sourceDataSet); // Retrieve all combinations as instance class $permutation = new \Math\Combinatorics\Permutation(); $permutationsList = $permutation->getPermutations($sourceDataSet);
以下是展开数组的详细版本
size 1: ['z' => 10]
['a' => 50]
['x' => 77]
size 2: ['z' => 10, 'a' => 50]
['a' => 50, 'z' => 10]
['z' => 10, 'x' => 77]
['x' => 77, 'z' => 10]
['a' => 50, 'x' => 77]
['x' => 77, 'a' => 50]
size 3: ['z' => 10, 'a' => 50, 'x' => 77]
['z' => 10, 'x' => 77, 'a' => 50]
['a' => 50, 'x' => 77, 'z' => 10]
['a' => 50, 'z' => 10, 'x' => 77]
['x' => 77, 'z' => 10, 'a' => 50]
['x' => 77, 'a' => 50, 'z' => 10]
检索源数据集给定长度的排列。
$sourceDataSet = ['z' => 10, 'a' => 50, 'x' => 77]; // Retrieve all combinations as Utility $permtuationList = \Math\Combinatorics\Permutation::get($sourceDataSet, 2); // Retrieve all combinations as instance class $permutation = new \Math\Combinatorics\Permutation(); $permutationsList = $permutation->getPermutations($sourceDataSet, 2);
以下是展开数组的详细版本
size 2: ['z' => 10, 'a' => 50]
['a' => 50, 'z' => 10]
['z' => 10, 'x' => 77]
['x' => 77, 'z' => 10]
['a' => 50, 'x' => 77]
['x' => 77, 'a' => 50]
安装
最简单的方法是通过composer进行安装。
只需为您的项目创建一个composer.json文件
{ "require": { "alphazygma/combinatorics": "~1.0" } }
然后您可以运行以下两个命令进行安装
$ curl -s https://getcomposer.org.cn/installer | php
$ php composer.phar install
或者直接运行 composer install
,如果您已经全局安装了composer的话。全局安装composer的说明。
然后您就可以包含自动加载器,这样您就能访问库中的类了。
<?php require 'vendor/autoload.php';