setSymDifference.js 1.5 KB

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