MCPcopy
hub / github.com/josdejong/mathjs / parseMatrix

Function parseMatrix

src/expression/parse.js:1596–1660  ·  view source on GitHub ↗

* parse the matrix * @return {Node} node * @private

(state)

Source from the content-addressed store, hash-verified

1594 * @private
1595 */
1596 function parseMatrix (state) {
1597 let array, params, rows, cols
1598
1599 if (state.token === '[') {
1600 // matrix [...]
1601 openParams(state)
1602 getToken(state)
1603
1604 if (state.token !== ']') {
1605 // this is a non-empty matrix
1606 const row = parseRow(state)
1607
1608 if (state.token === ';') {
1609 // 2 dimensional array
1610 rows = 1
1611 params = [row]
1612
1613 // the rows of the matrix are separated by dot-comma's
1614 while (state.token === ';') { // eslint-disable-line no-unmodified-loop-condition
1615 getToken(state)
1616
1617 if (state.token !== ']') {
1618 params[rows] = parseRow(state)
1619 rows++
1620 }
1621 }
1622
1623 if (state.token !== ']') {
1624 throw createSyntaxError(state, 'End of matrix ] expected')
1625 }
1626 closeParams(state)
1627 getToken(state)
1628
1629 // check if the number of columns matches in all rows
1630 cols = params[0].items.length
1631 for (let r = 1; r < rows; r++) {
1632 if (params[r].items.length !== cols) {
1633 throw createError(state, 'Column dimensions mismatch ' +
1634 '(' + params[r].items.length + ' !== ' + cols + ')')
1635 }
1636 }
1637
1638 array = new ArrayNode(params)
1639 } else {
1640 // 1 dimensional vector
1641 if (state.token !== ']') {
1642 throw createSyntaxError(state, 'End of matrix ] expected')
1643 }
1644 closeParams(state)
1645 getToken(state)
1646
1647 array = row
1648 }
1649 } else {
1650 // this is an empty matrix "[ ]"
1651 closeParams(state)
1652 getToken(state)
1653 array = new ArrayNode([])

Callers 1

parseStringFunction · 0.85

Calls 8

openParamsFunction · 0.85
getTokenFunction · 0.85
parseRowFunction · 0.85
createSyntaxErrorFunction · 0.85
closeParamsFunction · 0.85
createErrorFunction · 0.85
parseAccessorsFunction · 0.85
parseObjectFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…