mathset.js v1.1.3
MathSet
MathSet
is a subclass of ECMAScripts native Set. It adds methods from the algebra of sets and integrates methods from ECMAScripts native Array. Like javascripts native sets, it's also limited to a finite amount of elements.
Installation
If you are using node, you can install MathSet with npm:
npm i mathset.js
Then you can include it in your code:
const MathSet = require('mathset.js');
If you are using a browser supporting ECMAScript 6, you can download MathSet.min.js from GitLab and host it:
<script src="/js/MathSet.min.js" type="text/javascript"></script>
Examples
// create a MathSet {0, 1, 2, 3} using the contructor
var a = new MathSet([0, 1, 2, 3]);
// create a MathSet {0, 2, 4, 6} using range(min, max, step)
var b = MathSet.range(0, 6, 2);
// using native javascript Set methods and attributes
a.size == b.size;
// true, because they have 4 elements (/members)
var c;
// set c to the intersection of a and b (Math: a ∩ b) using statics
c = Math.intersection(a, b);
// or using the instance method of a
c = a.intersection(b);
// c is now {0, 2} because only these numbers are in a and also in b
// using the array method "map" (Math: c = 2·a)
c = a.map(e => e*2);
// c is now {0, 2, 4, 6}
// check for equality:
c.equals(b); // or: Math.equal(c, b);
// true, because now c and b are both containing the same elements
c == b;
// false, because they aren't the same instance (/object) reference
// generate the powerset of a, when cast it to an sorted array, containing sorted arrays itself (2D-array)
a.powerset().toArray(true, true);
// this will return an array with 16 values:
// [ [],
// [0], ... [3],
// [0, 1], ... [2, 3],
// [0, 1, 2], ... [2, 3, 4],
// [0, 1, 2, 3] ]
Documentation
- MathSet ⇐ Set
- new MathSet([iterable], [depth])
- instance
- .disjoint(y) ⇒ boolean
- .equals(y, [...z]) ⇒ boolean
- .every(test) ⇒ boolean
- .exclude(set) ⇒ MathSet
- .filter(test) ⇒ MathSet
- .intersection(y, [...z]) ⇒ MathSet
- .map(mapper) ⇒ MathSet
- .maximal() ⇒ mixed | false
- .minimal() ⇒ mixed | false
- .powerset() ⇒ MathSet
- .product() ⇒ Number
- .reduce(reducer, [initialValue]) ⇒ mixed
- .some(test) ⇒ boolean
- .strictSubsetOf(sup) ⇒ boolean
- .strictSupersetOf(sub) ⇒ boolean
- .subsetOf(sup) ⇒ boolean
- .sum() ⇒ Number
- .supersetOf(sub) ⇒ boolean
- .symDiff(y) ⇒ MathSet
- .toArray([depth], [sort]) ⇒ Array
- .union(y, [...z]) ⇒ MathSet
- .useArrayMethod(name, args, [noCast]) ⇒ MathSet | mixed
- static
- .complement(sup, set) ⇒ MathSet
- .disjoint(x, y) ⇒ boolean
- .equal(x, y, [...z]) ⇒ boolean
- .intersection(x, y, [...z]) ⇒ MathSet
- .powerset(x) ⇒ MathSet
- .range(min, max, [step]) ⇒ MathSet
- .strictSubset(sub, sup) ⇒ boolean
- .strictSuperset(sup, sub) ⇒ boolean
- .subset(sub, sup) ⇒ boolean
- .superset(sup, sub) ⇒ boolean
- .symDiff(x, y) ⇒ MathSet
- .toArray(x, [depth], [sort]) ⇒ Array
- .toMathSet(x) ⇒ MathSet
- .toNewMathSet(x, [depth]) ⇒ MathSet
- .union(x, y, [...z]) ⇒ MathSet
new MathSet(iterable, depth)
Like constructor of Set.
Param | Type | Default | Description |
---|---|---|---|
iterable | iterable | all of its elements will be added to the new MathSet | |
depth | boolean | number | false | if true, try cast elements to MathSets recursively; if number, try casting to this depth |
mathSet.disjoint(y) ⇒ boolean
See static MathSet.disjoint for explanation.
Kind: instance method of MathSet
Param | Type |
---|---|
y | MathSet | Set | Array |
mathSet.equals(y, ...z) ⇒ boolean
See static MathSet.equal for explanation.
Kind: instance method of MathSet
Param | Type |
---|---|
y | MathSet | Set | Array |
...z | MathSet | Set | Array |
mathSet.every(test) ⇒ boolean
Like Array: Tests if all elements in the set are true for a given function.
See also mathSet.useArrayMethod
and array.every.
Math: ∀(x ∈ THIS) test(x)
Kind: instance method of MathSet
Returns: boolean - return value of array method
Param | Type | Description |
---|---|---|
test | testFunction | function for testing, see testFunction |
mathSet.exclude(set) ⇒ MathSet
Returns MathSet with all elements that are not in the given MathSet.
Math: THIS\SET
See static MathSet.complement for futher
explanation.
Kind: instance method of MathSet
Param | Type |
---|---|
set | MathSet | Set | Array |
mathSet.filter(test) ⇒ MathSet
Like Array: Returns all elements, that are true for a given function.
See also mathSet.useArrayMethod
and array.filter.
Math: { x ∈ THIS | test(x) }
Kind: instance method of MathSet
Returns: MathSet - return value of array method
Param | Type | Description |
---|---|---|
test | testFunction | function for testing, see testFunction |
mathSet.intersection(y, ...z) ⇒ MathSet
See static MathSet.intersection for explanation.
Kind: instance method of MathSet
Param | Type |
---|---|
y | MathSet | Set | Array |
...z | MathSet | Set | Array |
mathSet.map(mapper) ⇒ MathSet
Like Array: New MathSet with the results of calling a provided function on
every element in a given MathSet.
See also mathSet.useArrayMethod
and array.map.
Math: (y ∈ THIS) { mapper(y) }
Kind: instance method of MathSet
Returns: MathSet - return value of array method
Param | Type | Description |
---|---|---|
mapper | mapFunction | fuction for mapping |
mathSet.maximal() ⇒ mixed | false
Greatest element of set.
Kind: instance method of MathSet
Returns: mixed | false - greatest element or false, if this element couldn't
be found
mathSet.minimal() ⇒ mixed | false
Smallest element of set.
Kind: instance method of MathSet
Returns: mixed | false - smallest element or false, if this element couldn't
be found
mathSet.powerset() ⇒ MathSet
See static MathSet.powerset for explanation.
Kind: instance method of MathSet
mathSet.product() ⇒ Number
Multiplies all elements.
Math: (x ∈ THIS) Πx
Kind: instance method of MathSet
Returns: Number - product of all elements
mathSet.reduce(reducer, initialValue) ⇒ mixed
Like Array: Executes a reducer function.
See also mathSet.useArrayMethod
and array.reduce.
Math: (yᵢ ∈ THIS) reducer( reducer( reducer(initialValue, y₁), y₂), ...)
Kind: instance method of MathSet
Returns: mixed - return value of array method
Param | Type | Description |
---|---|---|
reducer | reducerFunction | see reducerFunction |
initialValue | mixed | value to use as the first argument to the first call of the reducer; if no initialValue is supplied, the first element in the set will be used and skipped; calling reduce() on an empty array without an initialValue will throw a TypeError |
mathSet.some(test) ⇒ boolean
Like Array: Tests if at least one element in the set is true for a
given function.
See also mathSet.useArrayMethod
and array.some.
Math: ∃(x ∈ THIS) test(x)
Kind: instance method of MathSet
Returns: boolean - return value of array method
Param | Type | Description |
---|---|---|
test | testFunction | function for testing, see testFunction |
mathSet.strictSubsetOf(sup) ⇒ boolean
See static MathSet.strictSubset for explanation.
Kind: instance method of MathSet
Param | Type |
---|---|
sup | MathSet | Set | Array |
mathSet.strictSupersetOf(sub) ⇒ boolean
See static MathSet.strictSuperset for explanation.
Kind: instance method of MathSet
Param | Type |
---|---|
sub | MathSet | Set | Array |
mathSet.subsetOf(sup) ⇒ boolean
See static MathSet.subset for explanation.
Kind: instance method of MathSet
Param | Type |
---|---|
sup | MathSet | Set | Array |
mathSet.sum() ⇒ Number
Adds all elements.
Math: (x ∈ THIS) Σx
Kind: instance method of MathSet
Returns: Number - sum of all elements
mathSet.supersetOf(sub) ⇒ boolean
See static MathSet.superset for explanation.
Kind: instance method of MathSet
Param | Type |
---|---|
sub | MathSet | Set | Array |
mathSet.symDiff(y) ⇒ MathSet
See static MathSet.symDiff for explanation.
Kind: instance method of MathSet
Param | Type |
---|---|
y | MathSet | Set | Array |
mathSet.toArray(depth, sort) ⇒ Array
Casts to Array. See also MathSet.toArray.
Note: Maybe your problem can be solved better with
mathSet.useArrayMethod.
Kind: instance method of MathSet
Returns: Array - new array
Param | Type | Default | Description |
---|---|---|---|
depth | boolean | number | false | if true, try cast elements to arrays recursively; if number, try casting to this depth |
sort | boolean | false | if true, try sorting elements |
mathSet.union(y, ...z) ⇒ MathSet
See static MathSet.union for explanation.
Kind: instance method of MathSet
Param | Type |
---|---|
y | MathSet | Set | Array |
...z | MathSet | Set | Array |
mathSet.useArrayMethod(name, args, noCast) ⇒ MathSet | mixed
Applies a method of the Array class on the MathSet as if this set is an
sorted array.
See methods in Array Reference.
Note: Use MathSet or native Set properties if
you can:
- array.length ⥲ set.size
- array.incudes ⥲ set.has
- array.push or array.unshift ⥲ set.add
- array.shift or array.pop ⥲ set.delete
- array.concat ⥲ mathSet.union
Kind: instance method of MathSet
Returns: MathSet | mixed - return of array method (but maybe casted to MathSet)
Param | Type | Default | Description |
---|---|---|---|
name | string | name of array method | |
args | Array | parameters for method | |
noCast | boolean | false | if true, don't try to cast the return value to MathSet |
MathSet.complement(sup, set) ⇒ MathSet
The (relative) complement of a MathSet with respect to a (super)set. or:
The MathSet of all elements in (super)set without the elements of a given
MathSet.
Math: SUP \ SET
Kind: static method of MathSet
Returns: MathSet - superset without set
Param | Type | Description |
---|---|---|
sup | MathSet | Set | Array | the superset |
set | MathSet | Set | Array | a set |
MathSet.disjoint(x, y) ⇒ boolean
Checks, if two MathSets are disjoint.
Math: X ⋂ Y = ∅
Kind: static method of MathSet
Returns: boolean - true, if MathSets disjoint
Param | Type | Description |
---|---|---|
x | MathSet | Set | Array | a MathSet |
y | MathSet | Set | Array | another MathSet |
MathSet.equal(x, y, ...z) ⇒ boolean
Checks, if MathSets are equal.
Math: X = Y ( = Z₁ ( = Z₂ ( = ... ) ) )
Kind: static method of MathSet
Returns: boolean - true, if all MathSets are equal
Param | Type | Description |
---|---|---|
x | MathSet | Set | Array | a MathSet |
y | MathSet | Set | Array | another MathSet |
...z | MathSet | Set | Array | any number of additional MathSets |
MathSet.intersection(x, y, ...z) ⇒ MathSet
MathSet of all elements that are common in all given MathSets resp. the
intersection of all given MathSet.
Math: X ∩ Y ( ∩ Z₁ ( ∩ Z₂ ( ∩ ... ) ) )
Kind: static method of MathSet
Returns: MathSet - the intersection of all given MathSets
Param | Type | Description |
---|---|---|
x | MathSet | Set | Array | a MathSet |
y | MathSet | Set | Array | another MathSet |
...z | MathSet | Set | Array | any number of additional MathSets |
MathSet.powerset(x) ⇒ MathSet
MathSet of all subsets of a given MathSet, including the empty set and x
itself.
Math: P(X)
Kind: static method of MathSet
Returns: MathSet - powerset
Param | Type | Description |
---|---|---|
x | MathSet | Set | Array | a MathSet |
MathSet.range(min, max, step) ⇒ MathSet
Generates a MathSet of numbers starting with min value for the "first" element and adding one element with the value of the latest element plus the step value as long as they are smaller or equals than max.
Kind: static method of MathSet
Returns: MathSet - generated set with these properties
Param | Type | Default | Description |
---|---|---|---|
min | number | smallest element | |
max | number | maximal value for elements | |
step | number | 1 | difference between the elements |
MathSet.strictSubset(sub, sup) ⇒ boolean
Checks, if sub is a strict (or proper) subset of a sup.
See also MathSet.subset and
MathSet.strictSuperset.
Math: SUB ⊂ SUP
Kind: static method of MathSet
Returns: boolean - true, if sub is strict subset
Param | Type | Description |
---|---|---|
sub | MathSet | Set | Array | set to check |
sup | MathSet | Set | Array | superset as dependency |
MathSet.strictSuperset(sup, sub) ⇒ boolean
Checks, if sup is a strict (or proper) superset of a sub.
See also MathSet.superset and
MathSet.strictSubset.
Math: SUP ⊃ SUB
Kind: static method of MathSet
Returns: boolean - true, if sub is strict subset
Param | Type | Description |
---|---|---|
sup | MathSet | Set | Array | set to check |
sub | MathSet | Set | Array | subset as dependency |
MathSet.subset(sub, sup) ⇒ boolean
Checks, if sub is a subset of a sup.
See also MathSet.superset and
MathSet.strictSubset.
Math: SUB ⊆ SUP
Kind: static method of MathSet
Returns: boolean - true, if sub is subset
Param | Type | Description |
---|---|---|
sub | MathSet | Set | Array | set to check |
sup | MathSet | Set | Array | superset as dependency |
MathSet.superset(sup, sub) ⇒ boolean
Checks, if sup is a superset of a sub.
See also MathSet.subset and
MathSet.strictSuperset.
Math: SUP ⊇ SUB
Kind: static method of MathSet
Returns: boolean - true, if sup is superset
Param | Type | Description |
---|---|---|
sup | MathSet | Set | Array | set to check |
sub | MathSet | Set | Array | subset as dependency |
MathSet.symDiff(x, y) ⇒ MathSet
MathSet of elements which are in either of the given MathSets and not in
their intersection. or: Every element is only in one of the given MathSets.
Note: Symmetric difference is also knowen als disjunctive union.
Math: X ∆ Y
or X ⊕ Y
or ((X∪Y)\(X∩Y))
or ((X\Y)∪(X\Y))
Kind: static method of MathSet
Returns: MathSet - symmetric difference resp. disjunctive union
Param | Type | Description |
---|---|---|
x | MathSet | Set | Array | a MathSet |
y | MathSet | Set | Array | another MathSet |
MathSet.toArray(x, depth, sort) ⇒ Array
Casts to Array.
Kind: static method of MathSet
Returns: Array - new casted array OR returns given x, if it was already an
array
Param | Type | Default | Description |
---|---|---|---|
x | MathSet | Set | Array | Set or Array types | |
depth | boolean | number | false | if true, try cast elements to arrays recursively; if number, try casting to this depth |
sort | boolean | false | if true, try sorting elements |
MathSet.toMathSet(x) ⇒ MathSet
Casts from every type to MathSet, if necessary.
See also MathSet.toNewMathSet.
Kind: static method of MathSet
Returns: MathSet - NEW casted MathSet OR GIVEN x if it was already a MathSet
Param | Type | Description |
---|---|---|
x | MathSet | Set | Array | MathSet or castable object |
MathSet.toNewMathSet(x, depth) ⇒ MathSet
Casts from every type to a new MathSet.
See also MathSet.toMathSet.
Kind: static method of MathSet
Returns: MathSet - new MathSet of casted x EVEN IF its already was a MathSet
Param | Type | Default | Description |
---|---|---|---|
x | MathSet | Set | Array | MathSet or castable object | |
depth | boolean | number | false | if true, try cast elements to MathSets recursively; if number, try casting to this depth |
MathSet.union(x, y, ...z) ⇒ MathSet
MathSet of all given sets "added" together resp. the union of all given
MathSets.
Math: X ∪ Y ( ∪ Z₁ ( ∪ Z₂ ( ∪ ... ) ) )
Kind: static method of MathSet
Returns: MathSet - the union of all given MathSets
Param | Type | Description |
---|---|---|
x | MathSet | Set | Array | a MathSet |
y | MathSet | Set | Array | another MathSet |
...z | MathSet | Set | Array | any number of additional MathSets |
testFunction ⇒ boolean
A function to test for each element, taking three arguments.
Kind: global typedef
Returns: boolean - true, if test successful
Param | Type | Description |
---|---|---|
element | mixed | the current element being processed |
index | number | the index of the current element being processed |
array | Array | the array the tester was called upon |
mapFunction ⇒ mixed
A function that produces an element by receiving an element, taking three arguments.
Kind: global typedef
Returns: mixed - new modified element
Param | Type | Description |
---|---|---|
element | mixed | the current element being processed |
index | number | the index of the current element being processed |
array | Array | the array the tester was called upon |
reducerFunction ⇒ mixed
A function to execute on each element in the array (except for the first, if no initialValue is supplied), taking four arguments.
Kind: global typedef
Returns: mixed - the new accumulated value
Param | Type | Description |
---|---|---|
accumulator | mixed | the accumulated value previously returned in the last invocation of the reducerFunction |
element | mixed | the current element being processed |
index | number | the index of the current element being processed |
array | Array | the array the reducer was called upon |