setDifference.js (2751B)
1 "use strict"; 2 3 Object.defineProperty(exports, "__esModule", { 4 value: true 5 }); 6 exports.createSetDifference = void 0; 7 8 var _array = require("../../utils/array.js"); 9 10 var _factory = require("../../utils/factory.js"); 11 12 var name = 'setDifference'; 13 var dependencies = ['typed', 'size', 'subset', 'compareNatural', 'Index', 'DenseMatrix']; 14 var createSetDifference = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) { 15 var typed = _ref.typed, 16 size = _ref.size, 17 subset = _ref.subset, 18 compareNatural = _ref.compareNatural, 19 Index = _ref.Index, 20 DenseMatrix = _ref.DenseMatrix; 21 22 /** 23 * Create the difference of two (multi)sets: every element of set1, that is not the element of set2. 24 * Multi-dimension arrays will be converted to single-dimension arrays before the operation. 25 * 26 * Syntax: 27 * 28 * math.setDifference(set1, set2) 29 * 30 * Examples: 31 * 32 * math.setDifference([1, 2, 3, 4], [3, 4, 5, 6]) // returns [1, 2] 33 * math.setDifference([[1, 2], [3, 4]], [[3, 4], [5, 6]]) // returns [1, 2] 34 * 35 * See also: 36 * 37 * setUnion, setIntersect, setSymDifference 38 * 39 * @param {Array | Matrix} a1 A (multi)set 40 * @param {Array | Matrix} a2 A (multi)set 41 * @return {Array | Matrix} The difference of two (multi)sets 42 */ 43 return typed(name, { 44 'Array | Matrix, Array | Matrix': function ArrayMatrixArrayMatrix(a1, a2) { 45 var result; 46 47 if (subset(size(a1), new Index(0)) === 0) { 48 // empty-anything=empty 49 result = []; 50 } else if (subset(size(a2), new Index(0)) === 0) { 51 // anything-empty=anything 52 return (0, _array.flatten)(a1.toArray()); 53 } else { 54 var b1 = (0, _array.identify)((0, _array.flatten)(Array.isArray(a1) ? a1 : a1.toArray()).sort(compareNatural)); 55 var b2 = (0, _array.identify)((0, _array.flatten)(Array.isArray(a2) ? a2 : a2.toArray()).sort(compareNatural)); 56 result = []; 57 var inb2; 58 59 for (var i = 0; i < b1.length; i++) { 60 inb2 = false; 61 62 for (var j = 0; j < b2.length; j++) { 63 if (compareNatural(b1[i].value, b2[j].value) === 0 && b1[i].identifier === b2[j].identifier) { 64 // the identifier is always a decimal int 65 inb2 = true; 66 break; 67 } 68 } 69 70 if (!inb2) { 71 result.push(b1[i]); 72 } 73 } 74 } // return an array, if both inputs were arrays 75 76 77 if (Array.isArray(a1) && Array.isArray(a2)) { 78 return (0, _array.generalize)(result); 79 } // return a matrix otherwise 80 81 82 return new DenseMatrix((0, _array.generalize)(result)); 83 } 84 }); 85 }); 86 exports.createSetDifference = createSetDifference;