UNPKG

1.64 kBJavaScriptView Raw
1import { flatten } from '../../utils/array';
2import { factory } from '../../utils/factory';
3var name = 'setUnion';
4var dependencies = ['typed', 'size', 'concat', 'subset', 'setIntersect', 'setSymDifference', 'Index'];
5export var createSetUnion =
6/* #__PURE__ */
7factory(name, dependencies, function (_ref) {
8 var typed = _ref.typed,
9 size = _ref.size,
10 concat = _ref.concat,
11 subset = _ref.subset,
12 setIntersect = _ref.setIntersect,
13 setSymDifference = _ref.setSymDifference,
14 Index = _ref.Index;
15
16 /**
17 * Create the union of two (multi)sets.
18 * Multi-dimension arrays will be converted to single-dimension arrays before the operation.
19 *
20 * Syntax:
21 *
22 * math.setUnion(set1, set2)
23 *
24 * Examples:
25 *
26 * math.setUnion([1, 2, 3, 4], [3, 4, 5, 6]) // returns [1, 2, 3, 4, 5, 6]
27 * math.setUnion([[1, 2], [3, 4]], [[3, 4], [5, 6]]) // returns [1, 2, 3, 4, 5, 6]
28 *
29 * See also:
30 *
31 * setIntersect, setDifference
32 *
33 * @param {Array | Matrix} a1 A (multi)set
34 * @param {Array | Matrix} a2 A (multi)set
35 * @return {Array | Matrix} The union of two (multi)sets
36 */
37 return typed(name, {
38 'Array | Matrix, Array | Matrix': function ArrayMatrixArrayMatrix(a1, a2) {
39 if (subset(size(a1), new Index(0)) === 0) {
40 // if any of them is empty, return the other one
41 return flatten(a2);
42 } else if (subset(size(a2), new Index(0)) === 0) {
43 return flatten(a1);
44 }
45
46 var b1 = flatten(a1);
47 var b2 = flatten(a2);
48 return concat(setSymDifference(b1, b2), setIntersect(b1, b2));
49 }
50 });
51});
\No newline at end of file