https://github.com/smoren/partial-intersection-php
M-partial intersection of sets and multisets explanation and examples
https://github.com/smoren/partial-intersection-php
intersection multiset multisets partial-intersection set set-theory sets
Last synced: 3 months ago
JSON representation
M-partial intersection of sets and multisets explanation and examples
- Host: GitHub
- URL: https://github.com/smoren/partial-intersection-php
- Owner: Smoren
- License: mit
- Created: 2023-01-25T07:26:34.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2023-01-28T13:21:53.000Z (over 2 years ago)
- Last Synced: 2025-04-02T09:51:11.993Z (6 months ago)
- Topics: intersection, multiset, multisets, partial-intersection, set, set-theory, sets
- Language: PHP
- Homepage:
- Size: 713 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# M-partial intersection of sets and multisets explanation
[](https://scrutinizer-ci.com/g/Smoren/partial-intersection-php/?branch=master)
[](https://coveralls.io/github/Smoren/partial-intersection-php?branch=master)

[](https://opensource.org/licenses/MIT)## Theory
### Definition
> An **M**-partial intersection (for **M > 0**) of **N** sets is a set elements
> in which are contained in at least **M** initial sets.### Properties
For any **N** sets:
1. **1**-partial intersection is equivalent to the
[union](https://en.wikipedia.org/wiki/Union_(set_theory)) of these sets.
2. **N**-partial intersection is equivalent to the
[common (complete) intersection](https://en.wikipedia.org/wiki/Intersection_(set_theory)) of these sets.
3. For any **M > N** **M**-partial intersection always equals to the
[empty set](https://en.wikipedia.org/wiki/Empty_set).## Examples
* [Simple integer sets example](#Simple-integer-sets-example)
* [Iterable integer sets example](#Iterable-integer-sets-example)
* [Mixed iterable sets example](#Mixed-iterable-sets-example)
* [Multisets example](#Multisets-example)### Simple integer sets example
Given: sets **A**, **B**, **C**, **D** (**N = 4**).
```php
$a = [1, 2, 3, 4, 5];
$b = [1, 2, 10, 11];
$c = [1, 2, 3, 12];
$d = [1, 4, 13, 14];
```#### M = 1
It is equivalent to `A ∪ B ∪ C ∪ D`.
```php
use Smoren\PartialIntersection\IntegerSetArrayImplementation;$r = IntegerSetArrayImplementation::partialIntersection(1, $a, $b, $c, $d);
// [1, 2, 3, 4, 5, 10, 11, 12, 13, 14]
```#### M = 2
```php
use Smoren\PartialIntersection\IntegerSetArrayImplementation;$r = IntegerSetArrayImplementation::partialIntersection(2, $a, $b, $c, $d);
// [1, 2, 3, 4]
```#### M = 3

```php
use Smoren\PartialIntersection\IntegerSetArrayImplementation;$r = IntegerSetArrayImplementation::partialIntersection(3, $a, $b, $c, $d);
// [1, 2]
```#### M = 4 (M = N)
It is equivalent to `A ∩ B ∩ C ∩ D`.
```php
use Smoren\PartialIntersection\IntegerSetArrayImplementation;$r = IntegerSetArrayImplementation::partialIntersection(4, $a, $b, $c, $d);
// [1]
```#### M = 5 (M > N)
Equals to an empty set.
```php
use Smoren\PartialIntersection\IntegerSetArrayImplementation;$r = IntegerSetArrayImplementation::partialIntersection(5, $a, $b, $c, $d);
// []
```### Iterable integer sets example
```php
$a = [1, 2, 3, 4, 5];
$b = [1, 2, 10, 11];
$c = [1, 2, 3, 12];
$d = [1, 4, 13, 14];use Smoren\PartialIntersection\IntegerSetIterableImplementation;
$r = IntegerSetArrayImplementation::partialIntersection(1, $a, $b, $c, $d);
print_r(iterator_to_array($r));
// [1, 2, 3, 4, 5, 10, 11, 12, 13, 14]$r = IntegerSetArrayImplementation::partialIntersection(2, $a, $b, $c, $d);
print_r(iterator_to_array($r));
// [1, 2, 3, 4]$r = IntegerSetArrayImplementation::partialIntersection(3, $a, $b, $c, $d);
print_r(iterator_to_array($r));
// [1, 2]$r = IntegerSetArrayImplementation::partialIntersection(4, $a, $b, $c, $d);
print_r(iterator_to_array($r));
// [1]$r = IntegerSetArrayImplementation::partialIntersection(5, $a, $b, $c, $d);
print_r(iterator_to_array($r));
// []
```### Mixed iterable sets example
```php
$a = ['1', 2, 3, 4, 5];
$b = ['1', 2, 10, 11];
$c = ['1', 2, 3, 12];
$d = ['1', 4, 13, 14];use Smoren\PartialIntersection\MixedSetIterableImplementation;
$r = MixedSetIterableImplementation::partialIntersection(true, 1, $a, $b, $c, $d);
print_r(iterator_to_array($r));
// ['1', 2, 3, 4, 5, 10, 11, 12, 13, 14]$r = MixedSetIterableImplementation::partialIntersection(true, 2, $a, $b, $c, $d);
print_r(iterator_to_array($r));
// ['1', 2, 3, 4]$r = MixedSetIterableImplementation::partialIntersection(true, 3, $a, $b, $c, $d);
print_r(iterator_to_array($r));
// ['1', 2]$r = MixedSetIterableImplementation::partialIntersection(true, 4, $a, $b, $c, $d);
print_r(iterator_to_array($r));
// ['1']$r = IntegerSetArrayImplementation::partialIntersection(true, 5, $a, $b, $c, $d);
print_r(iterator_to_array($r));
// []
```### Multisets example
*Note: If input collections contains duplicate items, then
[multiset](https://en.wikipedia.org/wiki/Multiset) intersection rules apply.*```php
$a = [1, 1, 1, 1, 1];
$b = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5];
$c = [5, 5, 5, 5, 5, 1, 5, 5, 1];use Smoren\PartialIntersection\MultisetIterableImplementation;
$r = MultisetIterableImplementation::partialIntersection(true, 1, $a, $b, $c);
print_r(iterator_to_array($r));
// [1, 1, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 5, 5, 5, 5, 5]$r = MultisetIterableImplementation::partialIntersection(true, 2, $a, $b, $c);
print_r(iterator_to_array($r));
// [1, 1, 5, 5]$r = MultisetIterableImplementation::partialIntersection(true, 3, $a, $b, $c);
print_r(iterator_to_array($r));
// [1, 1]$r = MultisetIterableImplementation::partialIntersection(true, 4, $a, $b, $c);
print_r(iterator_to_array($r));
// []
```## Unit testing
```
composer install
composer test-init
composer test
```