size.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { arraySize } from '../../utils/array.js';
  2. import { factory } from '../../utils/factory.js';
  3. import { noMatrix } from '../../utils/noop.js';
  4. var name = 'size';
  5. var dependencies = ['typed', 'config', '?matrix'];
  6. export var createSize = /* #__PURE__ */factory(name, dependencies, _ref => {
  7. var {
  8. typed,
  9. config,
  10. matrix
  11. } = _ref;
  12. /**
  13. * Calculate the size of a matrix or scalar.
  14. *
  15. * Syntax:
  16. *
  17. * math.size(x)
  18. *
  19. * Examples:
  20. *
  21. * math.size(2.3) // returns []
  22. * math.size('hello world') // returns [11]
  23. *
  24. * const A = [[1, 2, 3], [4, 5, 6]]
  25. * math.size(A) // returns [2, 3]
  26. * math.size(math.range(1,6)) // returns [5]
  27. *
  28. * See also:
  29. *
  30. * count, resize, squeeze, subset
  31. *
  32. * @param {boolean | number | Complex | Unit | string | Array | Matrix} x A matrix
  33. * @return {Array | Matrix} A vector with size of `x`.
  34. */
  35. return typed(name, {
  36. Matrix: function Matrix(x) {
  37. return x.create(x.size());
  38. },
  39. Array: arraySize,
  40. string: function string(x) {
  41. return config.matrix === 'Array' ? [x.length] : matrix([x.length]);
  42. },
  43. 'number | Complex | BigNumber | Unit | boolean | null': function numberComplexBigNumberUnitBooleanNull(x) {
  44. // scalar
  45. return config.matrix === 'Array' ? [] : matrix ? matrix([]) : noMatrix();
  46. }
  47. });
  48. });