UNPKG

1.66 kBJavaScriptView Raw
1'use strict'
2
3const flatten = require('../../utils/array').flatten
4
5function factory (type, config, load, typed) {
6 const MatrixIndex = load(require('../../type/matrix/MatrixIndex'))
7 const concat = load(require('../matrix/concat'))
8 const size = load(require('../matrix/size'))
9 const subset = load(require('../matrix/subset'))
10 const setDifference = load(require('../set/setDifference'))
11
12 /**
13 * Create the symmetric difference of two (multi)sets.
14 * Multi-dimension arrays will be converted to single-dimension arrays before the operation.
15 *
16 * Syntax:
17 *
18 * math.setSymDifference(set1, set2)
19 *
20 * Examples:
21 *
22 * math.setSymDifference([1, 2, 3, 4], [3, 4, 5, 6]) // returns [1, 2, 5, 6]
23 * math.setSymDifference([[1, 2], [3, 4]], [[3, 4], [5, 6]]) // returns [1, 2, 5, 6]
24 *
25 * See also:
26 *
27 * setUnion, setIntersect, setDifference
28 *
29 * @param {Array | Matrix} a1 A (multi)set
30 * @param {Array | Matrix} a2 A (multi)set
31 * @return {Array | Matrix} The symmetric difference of two (multi)sets
32 */
33 const setSymDifference = typed('setSymDifference', {
34 'Array | Matrix, Array | Matrix': function (a1, a2) {
35 if (subset(size(a1), new MatrixIndex(0)) === 0) { // if any of them is empty, return the other one
36 return flatten(a2)
37 } else if (subset(size(a2), new MatrixIndex(0)) === 0) {
38 return flatten(a1)
39 }
40 const b1 = flatten(a1)
41 const b2 = flatten(a2)
42 return concat(setDifference(b1, b2), setDifference(b2, b1))
43 }
44 })
45
46 return setSymDifference
47}
48
49exports.name = 'setSymDifference'
50exports.factory = factory