Giter Site home page Giter Site logo

smoren / partial-intersection-php Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 1.0 730 KB

M-partial intersection of sets and multisets explanation and examples

License: MIT License

PHP 100.00%
multiset multisets set set-theory sets intersection partial-intersection

partial-intersection-php's Introduction

M-partial intersection of sets and multisets explanation

Scrutinizer Code Quality Coverage Status Build and test License: 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 of these sets.
  2. N-partial intersection is equivalent to the common (complete) intersection of these sets.
  3. For any M > N M-partial intersection always equals to the empty set.

Examples

Simple integer sets example

Given: sets A, B, C, D (N = 4).

$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.

image

use Smoren\PartialIntersection\IntegerSetArrayImplementation;

$r = IntegerSetArrayImplementation::partialIntersection(1, $a, $b, $c, $d);
// [1, 2, 3, 4, 5, 10, 11, 12, 13, 14]

M = 2

image

use Smoren\PartialIntersection\IntegerSetArrayImplementation;

$r = IntegerSetArrayImplementation::partialIntersection(2, $a, $b, $c, $d);
// [1, 2, 3, 4]

M = 3

image

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.

image

use Smoren\PartialIntersection\IntegerSetArrayImplementation;

$r = IntegerSetArrayImplementation::partialIntersection(4, $a, $b, $c, $d);
// [1]

M = 5 (M > N)

Equals to an empty set.

image

use Smoren\PartialIntersection\IntegerSetArrayImplementation;

$r = IntegerSetArrayImplementation::partialIntersection(5, $a, $b, $c, $d);
// []

Iterable integer sets example

$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

$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 intersection rules apply.

$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

partial-intersection-php's People

Contributors

smoren avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

pis0sion

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.