ones.js 3.8 KB

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