UNPKG

1.6 kBJavaScriptView Raw
1'use strict'
2
3const clone = require('../../utils/object').clone
4const validateIndex = require('../../utils/array').validateIndex
5
6function factory (type, config, load, typed) {
7 const MatrixIndex = load(require('../../type/matrix/MatrixIndex'))
8 const matrix = load(require('../../type/matrix/function/matrix'))
9 const range = load(require('./range'))
10
11 /**
12 * Return a row from a Matrix.
13 *
14 * Syntax:
15 *
16 * math.row(value, index)
17 *
18 * Example:
19 *
20 * // get a row
21 * const d = [[1, 2], [3, 4]]
22 * math.row(d, 1) // returns [3, 4]
23 *
24 * See also:
25 *
26 * column
27 *
28 * @param {Array | Matrix } value An array or matrix
29 * @param {number} row The index of the row
30 * @return {Array | Matrix} The retrieved row
31 */
32 const row = typed('row', {
33 'Matrix, number': _row,
34
35 'Array, number': function (value, row) {
36 return _row(matrix(clone(value)), row).valueOf()
37 }
38 })
39
40 row.toTex = undefined // use default template
41
42 return row
43
44 /**
45 * Retrieve a row of a matrix
46 * @param {Matrix } value A matrix
47 * @param {number} row The index of the row
48 * @return {Matrix} The retrieved row
49 */
50 function _row (value, row) {
51 // check dimensions
52 if (value.size().length !== 2) {
53 throw new Error('Only two dimensional matrix is supported')
54 }
55
56 validateIndex(row, value.size()[0])
57
58 const columnRange = range(0, value.size()[1])
59 const index = new MatrixIndex(row, columnRange)
60 return value.subset(index)
61 }
62}
63
64exports.name = 'row'
65exports.factory = factory