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