setSymDifference.js 1.8 KB

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