setUnion.js 1.8 KB

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