ones.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { isBigNumber } from '../../utils/is.js';
  2. import { isInteger } from '../../utils/number.js';
  3. import { resize } from '../../utils/array.js';
  4. import { factory } from '../../utils/factory.js';
  5. var name = 'ones';
  6. var dependencies = ['typed', 'config', 'matrix', 'BigNumber'];
  7. export var createOnes = /* #__PURE__ */factory(name, dependencies, _ref => {
  8. var {
  9. typed,
  10. config,
  11. matrix,
  12. BigNumber
  13. } = _ref;
  14. /**
  15. * Create a matrix filled with ones. The created matrix can have one or
  16. * multiple dimensions.
  17. *
  18. * Syntax:
  19. *
  20. * math.ones(m)
  21. * math.ones(m, format)
  22. * math.ones(m, n)
  23. * math.ones(m, n, format)
  24. * math.ones([m, n])
  25. * math.ones([m, n], format)
  26. * math.ones([m, n, p, ...])
  27. * math.ones([m, n, p, ...], format)
  28. *
  29. * Examples:
  30. *
  31. * math.ones() // returns []
  32. * math.ones(3) // returns [1, 1, 1]
  33. * math.ones(3, 2) // returns [[1, 1], [1, 1], [1, 1]]
  34. * math.ones(3, 2, 'dense') // returns Dense Matrix [[1, 1], [1, 1], [1, 1]]
  35. *
  36. * const A = [[1, 2, 3], [4, 5, 6]]
  37. * math.ones(math.size(A)) // returns [[1, 1, 1], [1, 1, 1]]
  38. *
  39. * See also:
  40. *
  41. * zeros, identity, size, range
  42. *
  43. * @param {...(number|BigNumber) | Array} size The size of each dimension of the matrix
  44. * @param {string} [format] The Matrix storage format
  45. *
  46. * @return {Array | Matrix | number} A matrix filled with ones
  47. */
  48. return typed('ones', {
  49. '': function _() {
  50. return config.matrix === 'Array' ? _ones([]) : _ones([], 'default');
  51. },
  52. // math.ones(m, n, p, ..., format)
  53. // TODO: more accurate signature '...number | BigNumber, string' as soon as typed-function supports this
  54. '...number | BigNumber | string': function numberBigNumberString(size) {
  55. var last = size[size.length - 1];
  56. if (typeof last === 'string') {
  57. var format = size.pop();
  58. return _ones(size, format);
  59. } else if (config.matrix === 'Array') {
  60. return _ones(size);
  61. } else {
  62. return _ones(size, 'default');
  63. }
  64. },
  65. Array: _ones,
  66. Matrix: function Matrix(size) {
  67. var format = size.storage();
  68. return _ones(size.valueOf(), format);
  69. },
  70. 'Array | Matrix, string': function ArrayMatrixString(size, format) {
  71. return _ones(size.valueOf(), format);
  72. }
  73. });
  74. /**
  75. * Create an Array or Matrix with ones
  76. * @param {Array} size
  77. * @param {string} [format='default']
  78. * @return {Array | Matrix}
  79. * @private
  80. */
  81. function _ones(size, format) {
  82. var hasBigNumbers = _normalize(size);
  83. var defaultValue = hasBigNumbers ? new BigNumber(1) : 1;
  84. _validate(size);
  85. if (format) {
  86. // return a matrix
  87. var m = matrix(format);
  88. if (size.length > 0) {
  89. return m.resize(size, defaultValue);
  90. }
  91. return m;
  92. } else {
  93. // return an Array
  94. var arr = [];
  95. if (size.length > 0) {
  96. return resize(arr, size, defaultValue);
  97. }
  98. return arr;
  99. }
  100. }
  101. // replace BigNumbers with numbers, returns true if size contained BigNumbers
  102. function _normalize(size) {
  103. var hasBigNumbers = false;
  104. size.forEach(function (value, index, arr) {
  105. if (isBigNumber(value)) {
  106. hasBigNumbers = true;
  107. arr[index] = value.toNumber();
  108. }
  109. });
  110. return hasBigNumbers;
  111. }
  112. // validate arguments
  113. function _validate(size) {
  114. size.forEach(function (value) {
  115. if (typeof value !== 'number' || !isInteger(value) || value < 0) {
  116. throw new Error('Parameters in function ones must be positive integers');
  117. }
  118. });
  119. }
  120. });