mathjs
Version:
Math.js is an extensive math library for JavaScript and Node.js. It features a flexible expression parser with support for symbolic computation, comes with a large set of built-in functions and constants, and offers an integrated solution to work with dif
39 lines (35 loc) • 1.01 kB
JavaScript
import { clone } from '../../utils/object'
import { flatten as flattenArray } from '../../utils/array'
import { factory } from '../../utils/factory'
const name = 'flatten'
const dependencies = ['typed', 'matrix']
export const createFlatten = /* #__PURE__ */ factory(name, dependencies, ({ typed, matrix }) => {
/**
* Flatten a multi dimensional matrix into a single dimensional matrix.
*
* Syntax:
*
* math.flatten(x)
*
* Examples:
*
* math.flatten([[1,2], [3,4]]) // returns [1, 2, 3, 4]
*
* See also:
*
* concat, resize, size, squeeze
*
* @param {Matrix | Array} x Matrix to be flattened
* @return {Matrix | Array} Returns the flattened matrix
*/
return typed(name, {
Array: function (x) {
return flattenArray(clone(x))
},
Matrix: function (x) {
const flat = flattenArray(clone(x.toArray()))
// TODO: return the same matrix type as x
return matrix(flat)
}
})
})