1.1.3 • Published 4 years ago

mathset.js v1.1.3

Weekly downloads
-
License
ISC
Repository
gitlab
Last release
4 years ago

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

new MathSet(iterable, depth)

Like constructor of Set.

ParamTypeDefaultDescription
iterableiterableall of its elements will be added to the new MathSet
depthboolean | numberfalseif 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

ParamType
yMathSet | Set | Array

mathSet.equals(y, ...z) ⇒ boolean

See static MathSet.equal for explanation.

Kind: instance method of MathSet

ParamType
yMathSet | Set | Array
...zMathSet | 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

ParamTypeDescription
testtestFunctionfunction 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

ParamType
setMathSet | 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

ParamTypeDescription
testtestFunctionfunction for testing, see testFunction

mathSet.intersection(y, ...z) ⇒ MathSet

See static MathSet.intersection for explanation.

Kind: instance method of MathSet

ParamType
yMathSet | Set | Array
...zMathSet | 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

ParamTypeDescription
mappermapFunctionfuction 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

ParamTypeDescription
reducerreducerFunctionsee reducerFunction
initialValuemixedvalue 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

ParamTypeDescription
testtestFunctionfunction for testing, see testFunction

mathSet.strictSubsetOf(sup) ⇒ boolean

See static MathSet.strictSubset for explanation.

Kind: instance method of MathSet

ParamType
supMathSet | Set | Array

mathSet.strictSupersetOf(sub) ⇒ boolean

See static MathSet.strictSuperset for explanation.

Kind: instance method of MathSet

ParamType
subMathSet | Set | Array

mathSet.subsetOf(sup) ⇒ boolean

See static MathSet.subset for explanation.

Kind: instance method of MathSet

ParamType
supMathSet | 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

ParamType
subMathSet | Set | Array

mathSet.symDiff(y) ⇒ MathSet

See static MathSet.symDiff for explanation.

Kind: instance method of MathSet

ParamType
yMathSet | 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

ParamTypeDefaultDescription
depthboolean | numberfalseif true, try cast elements to arrays recursively; if number, try casting to this depth
sortbooleanfalseif true, try sorting elements

mathSet.union(y, ...z) ⇒ MathSet

See static MathSet.union for explanation.

Kind: instance method of MathSet

ParamType
yMathSet | Set | Array
...zMathSet | 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)

ParamTypeDefaultDescription
namestringname of array method
argsArrayparameters for method
noCastbooleanfalseif 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

ParamTypeDescription
supMathSet | Set | Arraythe superset
setMathSet | Set | Arraya 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

ParamTypeDescription
xMathSet | Set | Arraya MathSet
yMathSet | Set | Arrayanother 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

ParamTypeDescription
xMathSet | Set | Arraya MathSet
yMathSet | Set | Arrayanother MathSet
...zMathSet | Set | Arrayany 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

ParamTypeDescription
xMathSet | Set | Arraya MathSet
yMathSet | Set | Arrayanother MathSet
...zMathSet | Set | Arrayany 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

ParamTypeDescription
xMathSet | Set | Arraya 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

ParamTypeDefaultDescription
minnumbersmallest element
maxnumbermaximal value for elements
stepnumber1difference 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

ParamTypeDescription
subMathSet | Set | Arrayset to check
supMathSet | Set | Arraysuperset 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

ParamTypeDescription
supMathSet | Set | Arrayset to check
subMathSet | Set | Arraysubset 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

ParamTypeDescription
subMathSet | Set | Arrayset to check
supMathSet | Set | Arraysuperset 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

ParamTypeDescription
supMathSet | Set | Arrayset to check
subMathSet | Set | Arraysubset 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

ParamTypeDescription
xMathSet | Set | Arraya MathSet
yMathSet | Set | Arrayanother 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

ParamTypeDefaultDescription
xMathSet | Set | ArraySet or Array types
depthboolean | numberfalseif true, try cast elements to arrays recursively; if number, try casting to this depth
sortbooleanfalseif 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

ParamTypeDescription
xMathSet | Set | ArrayMathSet 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

ParamTypeDefaultDescription
xMathSet | Set | ArrayMathSet or castable object
depthboolean | numberfalseif 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

ParamTypeDescription
xMathSet | Set | Arraya MathSet
yMathSet | Set | Arrayanother MathSet
...zMathSet | Set | Arrayany 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

ParamTypeDescription
elementmixedthe current element being processed
indexnumberthe index of the current element being processed
arrayArraythe 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

ParamTypeDescription
elementmixedthe current element being processed
indexnumberthe index of the current element being processed
arrayArraythe 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

ParamTypeDescription
accumulatormixedthe accumulated value previously returned in the last invocation of the reducerFunction
elementmixedthe current element being processed
indexnumberthe index of the current element being processed
arrayArraythe array the reducer was called upon
1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.0.2

4 years ago

1.1.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago