setMultiplicity.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.createSetMultiplicity = void 0;
  6. var _array = require("../../utils/array.js");
  7. var _factory = require("../../utils/factory.js");
  8. var name = 'setMultiplicity';
  9. var dependencies = ['typed', 'size', 'subset', 'compareNatural', 'Index'];
  10. var createSetMultiplicity = /* #__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. /**
  17. * Count the multiplicity of an element in a multiset.
  18. * A multi-dimension array will be converted to a single-dimension array before the operation.
  19. *
  20. * Syntax:
  21. *
  22. * math.setMultiplicity(element, set)
  23. *
  24. * Examples:
  25. *
  26. * math.setMultiplicity(1, [1, 2, 2, 4]) // returns 1
  27. * math.setMultiplicity(2, [1, 2, 2, 4]) // returns 2
  28. *
  29. * See also:
  30. *
  31. * setDistinct, setSize
  32. *
  33. * @param {number | BigNumber | Fraction | Complex} e An element in the multiset
  34. * @param {Array | Matrix} a A multiset
  35. * @return {number} The number of how many times the multiset contains the element
  36. */
  37. return typed(name, {
  38. 'number | BigNumber | Fraction | Complex, Array | Matrix': function numberBigNumberFractionComplexArrayMatrix(e, a) {
  39. if (subset(size(a), new Index(0)) === 0) {
  40. // if empty, return 0
  41. return 0;
  42. }
  43. var b = (0, _array.flatten)(Array.isArray(a) ? a : a.toArray());
  44. var count = 0;
  45. for (var i = 0; i < b.length; i++) {
  46. if (compareNatural(b[i], e) === 0) {
  47. count++;
  48. }
  49. }
  50. return count;
  51. }
  52. });
  53. });
  54. exports.createSetMultiplicity = createSetMultiplicity;