1.3.0 • Published 2 years ago

@neoncitylights/set-theory v1.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

@neoncitylights/set-theory

GitHub license npm version codecov Node.js workflow FOSSA Status

A library written in TypeScript implementing the mathematical branch known as set theory - a subset of discrete mathematics.

While there is a current proposal for adding new methods to the Set class^ecma-set-methods-proposal, it is not formally recommended yet. This library provides functions to coincide with the Set class^mdn-set, as standardized by the ECMAScript Language Specification. ^ecma-spec.

Install

npm install @neoncitylights/set-theory

Usage

const a = new Set<number>([1, 2, 3, 4]);
const b = new Set<number>([4, 5, 6, 7]);
getIntersection(a, b); // new Set<number>([4])
getUnion(a, b); // new Set<number>([1, 2, 3, 4, 5, 6, 7])

const c = new Set<string>(["hello", "world"]);
const d = new Set<string>(["hello"]);
isSupersetOf(c, d); // true
isSubsetOf(d, c); // true

Documentation

Auto-generated API documentation is available.

API Reference

Predicates

  • # predicates.areSetsDisjoint<TElement>(a, b): booleansource
  • # predicates.areSetsEquivalent<TElement>(a, b): booleansource
  • # predicates.areSetsEqual<TElement>(a, b): booleansource
  • # predicates.isSubsetOf<TElement>(a, b): booleansource
  • # predicates.isSupersetOf<TElement>(a, b): booleansource
  • # predicates.isProperSubsetOf<TElement>(a, b): booleansource
  • # predicates.isProperSupersetOf<TElement>(a, b): booleansource

Operations

  • # operations.getUnion<TElement>(a, b): Set<TElement> • source
  • # operations.getIntersection<TElement>(a, b): Set<TElement> • source
  • # operations.getDifference<TElement>(a, b): Set<TElement> • source
  • # operations.getSymmetricDifference<TElement>(a, b): Set<TElement> • source

Similarity measuring algorithms

  • # similarity.getJaccardSimilarityCoefficient<TElement>(a, b): numbersource
  • # similarity.getLogDice<TElement>(a, b): numbersource
  • # similarity.getOverlapCoefficient<TElement>(a, b): numbersource
  • # similarity.getSorensenDiceCoefficient<TElement>(a, b): numbersource

Types

This library provides generic types for describing different types of set functions via:

  • SetFunction<TElement>
  • SetOperation<TElement>
  • SetPredicate<TElement>
  • SetSimilarity<TElement>

Set Theory 101

Set

A set is an unordered collection of unique elements. The type of elements can be anything, such as words, numbers, and even other sets. In math, the name of sets are usually denoted with a capital letter, like A, B, C, etc. They can be expressed in namely 2 different notations being "Roster notation" and "Set-Builder notation".

Roster notation

Sets are written with curly brackets at the start and end, and each element is separated by a comma.

A = {1, 2, 3, 4}

For expressing larger, finite sets, we can use an ellipsis in-between. They can also be at the beginning or end to indicate that the set is infinite.

B = {1, 2, 3, …, 100}
C = {1, 3, 5, 7, 9, …}

Set-builder notation

Sets are written by defining a variable, and declaring a predicate (a statement that must be true or false) for that variable. For example, to express {0, 1, 2, 3, …, 10} in set-builder notation:

{x|x ∈ ℤ & 0 ≤ x ≤ 10}

To express {1, 3, 5, 7, 9, …} in set-builder notation:

{x|x = x + 2, 1 ≤ x ≤ ∞}

Compared to roster notation, set-builder notation is a way of more rigorously^rigour-wiki defining a set. However, it is important to note that depending on the audience, it can be less accessible to readers.

Cardinality

Cardinality is the formal term that represents the number of elements within a set. It is also known as the "size" of the element. Given a set named A, the cardinality can be expressed using pipes, via |A|.

 A = {hello, world}
|A| = 2

Disjoint sets

Disjoint sets are sets that have no elements in common. Given two sets named A and B, they are formally expressed as A ∩ B = ϕ.

A = {1, 2, 3, 4}
B = {5, 6, 7, 8}
A ∩ B = ϕ

Equivalent sets

Equivalent sets are two sets that have the same cardinality.

 A = {5, 6, 7, 8, 9, 10}
 B = {11, 12, 13, 14, 15, 16}
|A| = |B|

Equal sets

Equal sets are sets that have the same cardinality and the same elements.

A = {1, 2, 3, 4, 5}
B = {1, 2, 3, 4, 5}
A = B

Subset

A subset is a set where all elements of set A are in set B.

A = {1, 2, 3}
B = {1, 2, 3, 4, 5}
A ⊆ B

Superset

A superset is a set where all elements of B are in set A.

A = {4, 5, 6}
B = {5, 6, 7, 8, 9}
B ⊇ A

Proper subset

A proper subset is when set A is a subset of B, but A ≠ B.

{1} ⊂ {1, 3}

Proper superset

A proper superset is when set A is a superset of B, but A ≠ B.

{2, 3, 9} ⊃ {3, 9}

Intersection

The intersection is the set of elements that are the members of two sets.

A = {1, 2, 3, 4, 5}
B = {3, 4, 5, 6, 7}
A ∩ B = {3, 4, 5}

Union

The union is the set of all elements in a collection of sets.

A = {1, 2, 3, 4}
B = {4, 5, 6, 7, 8}
A ∪ B = {1, 2, 3, 4, 5, 6, 7, 8}

Difference

The difference (also known as the complement) is the set of all elements that are in set A that are not in set B.

A = {1, 2, 3, 4}
B = {2, 3, 5, 6}
A \ B = {1, 4}

Symmetric difference

The symmetric difference is the set that belongs to one but not both of two sets.

A = {2, 4, 5, 8}
B = {2, 6, 7, 8}
A △ B = {4, 5, 6, 7}

Symbols

CharacterCode pointName
U+2286Subset
U+2287Superset
U+2282Proper subset
U+2283Proper superset
U+03D5Empty set
U+2208Element of
U+2209Not element of
U+2229Intersection
U+222AUnion
\ | U+005C | Complement/difference
U+25B3Symmetric difference
U+2296Symmetric difference
×U+00D7Cartesian product
U+2115Set of all natural numbers
𝕎U+1D54ESet of all whole numbers
U+2124Set of all integer numbers
U+211ASet of all rational numbers
𝔽U+1D53DSet of all irrational numbers
U+211DSet of all real numbers
U+2102Set of all complex numbers

License

This library is licensed under the MIT License.

^mdn-set: MDN Web Docs Contributors. (n.d.). Set - javascript: MDN. JavaScript | MDN. Retrieved November 1, 2021, from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set. ^ecma-spec: TC39. (2021, October 30). Draft ECMA-262 / October 30, 2021. ECMAScript® 2022 Language Specification. Retrieved November 1, 2021, from https://tc39.es/ecma262/#sec-set-objects. ^ecma-set-methods-proposal: Wadas, M., & Gunasekaran, S. (n.d.). New Set methods. New set methods. Retrieved November 1, 2021, from https://tc39.es/proposal-set-methods/. ^rigour-wiki: Wikipedia contributors. (2021, November 1). Rigour. In Wikipedia, The Free Encyclopedia. Retrieved 17:24, November 1, 2021, from https://en.wikipedia.org/w/index.php?title=Rigour&oldid=1053055627

FOSSA Status