column.js 1.7 KB

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