identity.js 4.5 KB

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