Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from

Conversation

Smoren
Copy link
Contributor

@Smoren Smoren commented Jan 26, 2023

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.

@markrogoyski markrogoyski changed the base branch from master to develop January 26, 2023 17:15
@coveralls
Copy link

coveralls commented Jan 26, 2023

Coverage Status

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

@markrogoyski
Copy link
Owner

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
Copy link
Contributor Author

Smoren commented Jan 27, 2023

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:

  • complete intersection (N-partial intersection of N sets for any N);
  • union (1-partial intersection of N sets for any N).

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
Copy link
Owner

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
Copy link
Contributor Author

Smoren commented Jan 27, 2023

@markrogoyski, OK, got it.

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

Thank you!

@Smoren
Copy link
Contributor Author

Smoren commented Feb 2, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants