row.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.createRow = void 0;
  6. var _factory = require("../../utils/factory.js");
  7. var _object = require("../../utils/object.js");
  8. var _array = require("../../utils/array.js");
  9. var name = 'row';
  10. var dependencies = ['typed', 'Index', 'matrix', 'range'];
  11. var createRow = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
  12. var typed = _ref.typed,
  13. Index = _ref.Index,
  14. matrix = _ref.matrix,
  15. range = _ref.range;
  16. /**
  17. * Return a row from a Matrix.
  18. *
  19. * Syntax:
  20. *
  21. * math.row(value, index)
  22. *
  23. * Example:
  24. *
  25. * // get a row
  26. * const d = [[1, 2], [3, 4]]
  27. * math.row(d, 1) // returns [[3, 4]]
  28. *
  29. * See also:
  30. *
  31. * column
  32. *
  33. * @param {Array | Matrix } value An array or matrix
  34. * @param {number} row The index of the row
  35. * @return {Array | Matrix} The retrieved row
  36. */
  37. return typed(name, {
  38. 'Matrix, number': _row,
  39. 'Array, number': function ArrayNumber(value, row) {
  40. return _row(matrix((0, _object.clone)(value)), row).valueOf();
  41. }
  42. });
  43. /**
  44. * Retrieve a row of a matrix
  45. * @param {Matrix } value A matrix
  46. * @param {number} row The index of the row
  47. * @return {Matrix} The retrieved row
  48. */
  49. function _row(value, row) {
  50. // check dimensions
  51. if (value.size().length !== 2) {
  52. throw new Error('Only two dimensional matrix is supported');
  53. }
  54. (0, _array.validateIndex)(row, value.size()[0]);
  55. var columnRange = range(0, value.size()[1]);
  56. var index = new Index(row, columnRange);
  57. return value.subset(index);
  58. }
  59. });
  60. exports.createRow = createRow;