count.js 939 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { factory } from '../../utils/factory.js';
  2. var name = 'count';
  3. var dependencies = ['typed', 'size', 'prod'];
  4. export var createCount = /* #__PURE__ */factory(name, dependencies, _ref => {
  5. var {
  6. typed,
  7. size,
  8. prod
  9. } = _ref;
  10. /**
  11. * Count the number of elements of a matrix, array or string.
  12. *
  13. * Syntax:
  14. *
  15. * math.count(x)
  16. *
  17. * Examples:
  18. *
  19. * math.count('hello world') // returns 11
  20. * const A = [[1, 2, 3], [4, 5, 6]]
  21. * math.count(A) // returns 6
  22. * math.count(math.range(1,6)) // returns 5
  23. *
  24. * See also:
  25. *
  26. * size
  27. *
  28. * @param {string | Array | Matrix} x A matrix or string
  29. * @return {number} An integer with the elements in `x`.
  30. */
  31. return typed(name, {
  32. string: function string(x) {
  33. return x.length;
  34. },
  35. 'Matrix | Array': function MatrixArray(x) {
  36. return prod(size(x));
  37. }
  38. });
  39. });