setUnion.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { flatten } from '../../utils/array.js';
  2. import { factory } from '../../utils/factory.js';
  3. var name = 'setUnion';
  4. var dependencies = ['typed', 'size', 'concat', 'subset', 'setIntersect', 'setSymDifference', 'Index'];
  5. export var createSetUnion = /* #__PURE__ */factory(name, dependencies, _ref => {
  6. var {
  7. typed,
  8. size,
  9. concat,
  10. subset,
  11. setIntersect,
  12. setSymDifference,
  13. Index
  14. } = _ref;
  15. /**
  16. * Create the union of two (multi)sets.
  17. * Multi-dimension arrays will be converted to single-dimension arrays before the operation.
  18. *
  19. * Syntax:
  20. *
  21. * math.setUnion(set1, set2)
  22. *
  23. * Examples:
  24. *
  25. * math.setUnion([1, 2, 3, 4], [3, 4, 5, 6]) // returns [1, 2, 3, 4, 5, 6]
  26. * math.setUnion([[1, 2], [3, 4]], [[3, 4], [5, 6]]) // returns [1, 2, 3, 4, 5, 6]
  27. *
  28. * See also:
  29. *
  30. * setIntersect, setDifference
  31. *
  32. * @param {Array | Matrix} a1 A (multi)set
  33. * @param {Array | Matrix} a2 A (multi)set
  34. * @return {Array | Matrix} The union 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. var b1 = flatten(a1);
  45. var b2 = flatten(a2);
  46. return concat(setSymDifference(b1, b2), setIntersect(b1, b2));
  47. }
  48. });
  49. });