column.js (1556B)
1 import { factory } from '../../utils/factory.js'; 2 import { clone } from '../../utils/object.js'; 3 import { validateIndex } from '../../utils/array.js'; 4 var name = 'column'; 5 var dependencies = ['typed', 'Index', 'matrix', 'range']; 6 export var createColumn = /* #__PURE__ */factory(name, dependencies, _ref => { 7 var { 8 typed, 9 Index, 10 matrix, 11 range 12 } = _ref; 13 14 /** 15 * Return a column from a Matrix. 16 * 17 * Syntax: 18 * 19 * math.column(value, index) 20 * 21 * Example: 22 * 23 * // get a column 24 * const d = [[1, 2], [3, 4]] 25 * math.column(d, 1) // returns [[2], [4]] 26 * 27 * See also: 28 * 29 * row 30 * 31 * @param {Array | Matrix } value An array or matrix 32 * @param {number} column The index of the column 33 * @return {Array | Matrix} The retrieved column 34 */ 35 return typed(name, { 36 'Matrix, number': _column, 37 'Array, number': function ArrayNumber(value, column) { 38 return _column(matrix(clone(value)), column).valueOf(); 39 } 40 }); 41 /** 42 * Retrieve a column of a matrix 43 * @param {Matrix } value A matrix 44 * @param {number} column The index of the column 45 * @return {Matrix} The retrieved column 46 */ 47 48 function _column(value, column) { 49 // check dimensions 50 if (value.size().length !== 2) { 51 throw new Error('Only two dimensional matrix is supported'); 52 } 53 54 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 });