setDistinct.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.createSetDistinct = void 0;
  6. var _array = require("../../utils/array.js");
  7. var _factory = require("../../utils/factory.js");
  8. var name = 'setDistinct';
  9. var dependencies = ['typed', 'size', 'subset', 'compareNatural', 'Index', 'DenseMatrix'];
  10. var createSetDistinct = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
  11. var typed = _ref.typed,
  12. size = _ref.size,
  13. subset = _ref.subset,
  14. compareNatural = _ref.compareNatural,
  15. Index = _ref.Index,
  16. DenseMatrix = _ref.DenseMatrix;
  17. /**
  18. * Collect the distinct elements of a multiset.
  19. * A multi-dimension array will be converted to a single-dimension array before the operation.
  20. *
  21. * Syntax:
  22. *
  23. * math.setDistinct(set)
  24. *
  25. * Examples:
  26. *
  27. * math.setDistinct([1, 1, 1, 2, 2, 3]) // returns [1, 2, 3]
  28. *
  29. * See also:
  30. *
  31. * setMultiplicity
  32. *
  33. * @param {Array | Matrix} a A multiset
  34. * @return {Array | Matrix} A set containing the distinc elements of the multiset
  35. */
  36. return typed(name, {
  37. 'Array | Matrix': function ArrayMatrix(a) {
  38. var result;
  39. if (subset(size(a), new Index(0)) === 0) {
  40. // if empty, return empty
  41. result = [];
  42. } else {
  43. var b = (0, _array.flatten)(Array.isArray(a) ? a : a.toArray()).sort(compareNatural);
  44. result = [];
  45. result.push(b[0]);
  46. for (var i = 1; i < b.length; i++) {
  47. if (compareNatural(b[i], b[i - 1]) !== 0) {
  48. result.push(b[i]);
  49. }
  50. }
  51. }
  52. // return an array, if the input was an array
  53. if (Array.isArray(a)) {
  54. return result;
  55. }
  56. // return a matrix otherwise
  57. return new DenseMatrix(result);
  58. }
  59. });
  60. });
  61. exports.createSetDistinct = createSetDistinct;