markrogoyski / math-php

Powerful modern math library for PHP: Features descriptive statistics and regressions; Continuous and discrete probability distributions; Linear algebra with matrices and vectors, Numerical analysis; special mathematical functions; Algebra
MIT License
2.33k stars 240 forks source link

Set::intersectPartial() method added to the SetTheory namespace #464

Open Smoren opened 1 year ago

Smoren commented 1 year ago

Hi @markrogoyski,

This is a PR about the M-partial intersection which have been previously added to your IterTools repository.

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. 2-partial intersection is equivalent to the difference of the union and the symmetric difference of these sets.
  3. N-partial intersection is equivalent to the common (complete) intersection of these sets.
  4. For any M > N M-partial intersection always equals to the empty set.

Example

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

use MathPHP\SetTheory\Set;

$a = new Set([1, 2, 3, 4, 5]);
$b = new Set([1, 2, 10, 11]);
$c = new Set([1, 2, 3, 12]);
$d = new Set([1, 4, 13, 14]);

M = 1

It is equivalent to A ∪ B ∪ C ∪ D.

image

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

M = 2

It is equivalent to (A ∪ B ∪ C ∪ D) \ ∆(A, B, C, D).

image

$r = $a->intersectPartial(2, $b, $c, $d);
// [1, 2, 3, 4]

M = 3

image

$r = $a->intersectPartial(3, $b, $c, $d);
// [1, 2]

M = 4 (M = N)

It is equivalent to A ∩ B ∩ C ∩ D.

image

$r = $a->intersectPartial(4, $b, $c, $d);
// [1]

M = 5 (M > N)

Equals to an empty set.

image

$r = $a->intersectPartial(5, $b, $c, $d);
// []

More examples you can see in this repo.

coveralls commented 1 year ago

Coverage Status

Coverage: 99.937% (+0.02%) from 99.92% when pulling f5a52dc7042094ff59a5952f1893a6cbeb031596 on Smoren:set_partial_intersection into 7795af2a20cc24af11766c56356fe003689a9ede on markrogoyski:develop.

markrogoyski commented 1 year ago

Hi @Smoren,

Thank you for your interest in MathPHP and for your pull request.

I understand the functionality from our conversations in the IterTools PRs.

I have a question about implementation. Based on your description here:

For any N sets:

1-partial intersection is equivalent to the union of these sets. 2-partial intersection is equivalent to the difference of the union and the symmetric difference of these sets. N-partial intersection is equivalent to the common (complete) intersection of these sets. For any M > N M-partial intersection always equals to the empty set.

Since this new set operation seems to be able to be defined in terms of other set operations (union, difference, symmetric difference, and intersection), and those set operations are already defined on Set, would it be possible to implement the proposed partialIntersection in terms of the other already-existing set operations?

Also, whether or not the above is possible or not, allow me to introduce how we do unit testing for MathPHP. In addition to the individual unit tests on each method (Which you added, thank you!), whenever possible, we also add axiom unit tests based on mathematical axioms that show principles that should hold regardless of how things are implemented. See the SetAxiomsTest for examples for the class you are working on. For other examples, see MatrixAxiomsTest and VectorAxiomsTest for instance. The definitions you stated above seem like good axiom tests to add for this new operation. For these kinds of tests, using a wide variety of inputs is recommended, as the axioms should hold regardless, and no pre-computed results generally need to be done as the axioms compute the rules that must hold.

Thanks, Mark

Smoren commented 1 year ago

Hi @markrogoyski,

Thanks for the feedback!

Indeed, there are cases (N=1, N=2, N=M, N>M) in which the operation is easily defined in terms of classical set operations. However, I don't see an easy way to express the partial intersection in terms of other operations for the cases 2<M<N. The algorithm that I have implemented works universally for any M>0, without considering special cases. Moreover, two classical operations can be considered as a special case of partial intersection:

As for axiomatic tests: I'll try to figure it out.

P.S. Can you unlock github workflow for me to run without manual approval please?

Thank you!

UPD: All the axiomatic unit tests are added.

markrogoyski commented 1 year ago

P.S. Can you unlock github workflow for me to run without manual approval please?

It seems like first time contributors to a repository require manual approval from what I can tell. I think this is a relatively new GitHub feature probably to prevent abuse of resources. After you have merged something it should run automatically on each PR and additional commit. I'll just have to kick it off manually for now it seems.

Smoren commented 1 year ago

@markrogoyski, OK, got it.

So I think this PR is ready to merge if you do not have new questions.

Thank you!

Smoren commented 1 year ago

Hi @markrogoyski, I've rebase this branch from develop and resolved the conflicts.