identity.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.createIdentity = void 0;
  6. var _is = require("../../utils/is.js");
  7. var _array = require("../../utils/array.js");
  8. var _number = require("../../utils/number.js");
  9. var _factory = require("../../utils/factory.js");
  10. var name = 'identity';
  11. var dependencies = ['typed', 'config', 'matrix', 'BigNumber', 'DenseMatrix', 'SparseMatrix'];
  12. var createIdentity = /* #__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. DenseMatrix = _ref.DenseMatrix,
  18. SparseMatrix = _ref.SparseMatrix;
  19. /**
  20. * Create a 2-dimensional identity matrix with size m x n or n x n.
  21. * The matrix has ones on the diagonal and zeros elsewhere.
  22. *
  23. * Syntax:
  24. *
  25. * math.identity(n)
  26. * math.identity(n, format)
  27. * math.identity(m, n)
  28. * math.identity(m, n, format)
  29. * math.identity([m, n])
  30. * math.identity([m, n], format)
  31. *
  32. * Examples:
  33. *
  34. * math.identity(3) // returns [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
  35. * math.identity(3, 2) // returns [[1, 0], [0, 1], [0, 0]]
  36. *
  37. * const A = [[1, 2, 3], [4, 5, 6]]
  38. * math.identity(math.size(A)) // returns [[1, 0, 0], [0, 1, 0]]
  39. *
  40. * See also:
  41. *
  42. * diag, ones, zeros, size, range
  43. *
  44. * @param {...number | Matrix | Array} size The size for the matrix
  45. * @param {string} [format] The Matrix storage format
  46. *
  47. * @return {Matrix | Array | number} A matrix with ones on the diagonal.
  48. */
  49. return typed(name, {
  50. '': function _() {
  51. return config.matrix === 'Matrix' ? matrix([]) : [];
  52. },
  53. string: function string(format) {
  54. return matrix(format);
  55. },
  56. 'number | BigNumber': function numberBigNumber(rows) {
  57. return _identity(rows, rows, config.matrix === 'Matrix' ? 'dense' : undefined);
  58. },
  59. 'number | BigNumber, string': function numberBigNumberString(rows, format) {
  60. return _identity(rows, rows, format);
  61. },
  62. 'number | BigNumber, number | BigNumber': function numberBigNumberNumberBigNumber(rows, cols) {
  63. return _identity(rows, cols, config.matrix === 'Matrix' ? 'dense' : undefined);
  64. },
  65. 'number | BigNumber, number | BigNumber, string': function numberBigNumberNumberBigNumberString(rows, cols, format) {
  66. return _identity(rows, cols, format);
  67. },
  68. Array: function Array(size) {
  69. return _identityVector(size);
  70. },
  71. 'Array, string': function ArrayString(size, format) {
  72. return _identityVector(size, format);
  73. },
  74. Matrix: function Matrix(size) {
  75. return _identityVector(size.valueOf(), size.storage());
  76. },
  77. 'Matrix, string': function MatrixString(size, format) {
  78. return _identityVector(size.valueOf(), format);
  79. }
  80. });
  81. function _identityVector(size, format) {
  82. switch (size.length) {
  83. case 0:
  84. return format ? matrix(format) : [];
  85. case 1:
  86. return _identity(size[0], size[0], format);
  87. case 2:
  88. return _identity(size[0], size[1], format);
  89. default:
  90. throw new Error('Vector containing two values expected');
  91. }
  92. }
  93. /**
  94. * Create an identity matrix
  95. * @param {number | BigNumber} rows
  96. * @param {number | BigNumber} cols
  97. * @param {string} [format]
  98. * @returns {Matrix}
  99. * @private
  100. */
  101. function _identity(rows, cols, format) {
  102. // BigNumber constructor with the right precision
  103. var Big = (0, _is.isBigNumber)(rows) || (0, _is.isBigNumber)(cols) ? BigNumber : null;
  104. if ((0, _is.isBigNumber)(rows)) rows = rows.toNumber();
  105. if ((0, _is.isBigNumber)(cols)) cols = cols.toNumber();
  106. if (!(0, _number.isInteger)(rows) || rows < 1) {
  107. throw new Error('Parameters in function identity must be positive integers');
  108. }
  109. if (!(0, _number.isInteger)(cols) || cols < 1) {
  110. throw new Error('Parameters in function identity must be positive integers');
  111. }
  112. var one = Big ? new BigNumber(1) : 1;
  113. var defaultValue = Big ? new Big(0) : 0;
  114. var size = [rows, cols];
  115. // check we need to return a matrix
  116. if (format) {
  117. // create diagonal matrix (use optimized implementation for storage format)
  118. if (format === 'sparse') {
  119. return SparseMatrix.diagonal(size, one, 0, defaultValue);
  120. }
  121. if (format === 'dense') {
  122. return DenseMatrix.diagonal(size, one, 0, defaultValue);
  123. }
  124. throw new TypeError("Unknown matrix type \"".concat(format, "\""));
  125. }
  126. // create and resize array
  127. var res = (0, _array.resize)([], size, defaultValue);
  128. // fill in ones on the diagonal
  129. var minimum = rows < cols ? rows : cols;
  130. // fill diagonal
  131. for (var d = 0; d < minimum; d++) {
  132. res[d][d] = one;
  133. }
  134. return res;
  135. }
  136. });
  137. exports.createIdentity = createIdentity;