{"version":3,"file":"ml-spectra-fitting.esm.min.js","sources":["../node_modules/is-any-array/lib/index.js","../node_modules/ml-spectra-processing/lib/x/xCheck.js","../node_modules/ml-spectra-processing/lib/x/xFindClosestIndex.js","../node_modules/ml-spectra-processing/lib/x/xGetFromToIndex.js","../node_modules/ml-matrix/matrix.js","../node_modules/ml-matrix/matrix.mjs","../node_modules/ml-spectra-processing/lib/x/xMaxValue.js","../node_modules/ml-spectra-processing/lib/x/xMinValue.js","../node_modules/ml-spectra-processing/lib/x/xNorm.js","../node_modules/ml-peak-shape-generator/lib-esm/util/constants.js","../node_modules/ml-peak-shape-generator/lib-esm/shapes/1d/gaussian/Gaussian.js","../node_modules/ml-peak-shape-generator/lib-esm/util/erfinv.js","../node_modules/ml-peak-shape-generator/lib-esm/shapes/1d/lorentzian/Lorentzian.js","../node_modules/ml-peak-shape-generator/lib-esm/shapes/1d/lorentzianDispersive/LorentzianDispersive.js","../node_modules/ml-peak-shape-generator/lib-esm/shapes/1d/pseudoVoigt/PseudoVoigt.js","../node_modules/ml-peak-shape-generator/lib-esm/shapes/1d/generalizedLorentzian/GeneralizedLorentzian.js","../node_modules/ml-peak-shape-generator/lib-esm/shapes/1d/getShape1D.js","../lib/util/assert.js","../lib/util/internalPeaks/DefaultParameters.js","../lib/util/internalPeaks/getInternalPeaks.js","../node_modules/ml-levenberg-marquardt/lib/check_options.js","../node_modules/ml-levenberg-marquardt/lib/error_calculation.js","../node_modules/ml-levenberg-marquardt/lib/step.js","../node_modules/ml-levenberg-marquardt/lib/gradient_function.js","../node_modules/ml-levenberg-marquardt/lib/levenberg_marquardt.js","../node_modules/ml-direct/src/util/antiLowerConvexHull.js","../node_modules/ml-direct/src/index.js","../lib/util/wrappers/directOptimization.js","../lib/index.js","../node_modules/ml-spectra-processing/lib/x/xMaxAbsoluteValue.js","../lib/util/getGlobalParameterVectors.js","../lib/util/selectMethod.js","../lib/shapes/getSumOfShapes.js","../lib/util/getFixedParametersResult.js"],"sourcesContent":["// eslint-disable-next-line @typescript-eslint/unbound-method\nconst toString = Object.prototype.toString;\n/**\n * Checks if an object is an instance of an Array (array or typed array, except those that contain bigint values).\n * @param value - Object to check.\n * @returns True if the object is an array or a typed array.\n */\nexport function isAnyArray(value) {\n    const tag = toString.call(value);\n    return tag.endsWith('Array]') && !tag.includes('Big');\n}\n//# sourceMappingURL=index.js.map","import { isAnyArray } from 'is-any-array';\n/**\n * Checks if the input is a non-empty array of numbers.\n * Only checks the first element.\n * @param input - Array to check.\n * @param options - Additional checks.\n */\nexport function xCheck(input, options = {}) {\n    const { minLength } = options;\n    if (!isAnyArray(input)) {\n        throw new TypeError('input must be an array');\n    }\n    if (input.length === 0) {\n        throw new TypeError('input must not be empty');\n    }\n    if (typeof input[0] !== 'number') {\n        throw new TypeError('input must contain numbers');\n    }\n    if (minLength && input.length < minLength) {\n        throw new Error(`input must have a length of at least ${minLength}`);\n    }\n}\n//# sourceMappingURL=xCheck.js.map","/**\n * Returns the closest index of a `target`\n * @param array - array of numbers\n * @param target - target\n * @param options\n * @returns - closest index\n */\nexport function xFindClosestIndex(array, target, options = {}) {\n    const { sorted = true } = options;\n    if (sorted) {\n        let low = 0;\n        let high = array.length - 1;\n        let middle = 0;\n        while (high - low > 1) {\n            middle = low + ((high - low) >> 1);\n            if (array[middle] < target) {\n                low = middle;\n            }\n            else if (array[middle] > target) {\n                high = middle;\n            }\n            else {\n                return middle;\n            }\n        }\n        if (low < array.length - 1) {\n            if (Math.abs(target - array[low]) < Math.abs(array[low + 1] - target)) {\n                return low;\n            }\n            else {\n                return low + 1;\n            }\n        }\n        else {\n            return low;\n        }\n    }\n    else {\n        let index = 0;\n        let diff = Number.POSITIVE_INFINITY;\n        for (let i = 0; i < array.length; i++) {\n            const currentDiff = Math.abs(array[i] - target);\n            if (currentDiff < diff) {\n                diff = currentDiff;\n                index = i;\n            }\n        }\n        return index;\n    }\n}\n//# sourceMappingURL=xFindClosestIndex.js.map","import { xFindClosestIndex } from \"./xFindClosestIndex.js\";\n/**\n * Returns an object with {fromIndex, toIndex} for a specific from / to\n * @param x - array of numbers\n * @param options - Options\n */\nexport function xGetFromToIndex(x, options = {}) {\n    let { fromIndex, toIndex } = options;\n    const { from, to } = options;\n    if (fromIndex === undefined) {\n        if (from !== undefined) {\n            fromIndex = xFindClosestIndex(x, from);\n        }\n        else {\n            fromIndex = 0;\n        }\n    }\n    if (toIndex === undefined) {\n        if (to !== undefined) {\n            toIndex = xFindClosestIndex(x, to);\n        }\n        else {\n            toIndex = x.length - 1;\n        }\n    }\n    if (fromIndex < 0)\n        fromIndex = 0;\n    if (toIndex < 0)\n        toIndex = 0;\n    if (fromIndex >= x.length)\n        fromIndex = x.length - 1;\n    if (toIndex >= x.length)\n        toIndex = x.length - 1;\n    if (fromIndex > toIndex)\n        [fromIndex, toIndex] = [toIndex, fromIndex];\n    return { fromIndex, toIndex };\n}\n//# sourceMappingURL=xGetFromToIndex.js.map","'use strict';\n\nObject.defineProperty(exports, '__esModule', { value: true });\n\n// eslint-disable-next-line @typescript-eslint/unbound-method\nconst toString = Object.prototype.toString;\n/**\n * Checks if an object is an instance of an Array (array or typed array, except those that contain bigint values).\n * @param value - Object to check.\n * @returns True if the object is an array or a typed array.\n */\nfunction isAnyArray(value) {\n    const tag = toString.call(value);\n    return tag.endsWith('Array]') && !tag.includes('Big');\n}\n\n/**\n * Computes the maximum of the given values.\n *\n * @param input\n * @param options\n */\nfunction max(input, options = {}) {\n    if (!isAnyArray(input)) {\n        throw new TypeError('input must be an array');\n    }\n    if (input.length === 0) {\n        throw new TypeError('input must not be empty');\n    }\n    const { fromIndex = 0, toIndex = input.length } = options;\n    if (fromIndex < 0 ||\n        fromIndex >= input.length ||\n        !Number.isInteger(fromIndex)) {\n        throw new Error('fromIndex must be a positive integer smaller than length');\n    }\n    if (toIndex <= fromIndex ||\n        toIndex > input.length ||\n        !Number.isInteger(toIndex)) {\n        throw new Error('toIndex must be an integer greater than fromIndex and at most equal to length');\n    }\n    let maxValue = input[fromIndex];\n    for (let i = fromIndex + 1; i < toIndex; i++) {\n        if (input[i] > maxValue)\n            maxValue = input[i];\n    }\n    return maxValue;\n}\n\n/**\n * Computes the minimum of the given values.\n */\nfunction min(input, options = {}) {\n    if (!isAnyArray(input)) {\n        throw new TypeError('input must be an array');\n    }\n    if (input.length === 0) {\n        throw new TypeError('input must not be empty');\n    }\n    const { fromIndex = 0, toIndex = input.length } = options;\n    if (fromIndex < 0 ||\n        fromIndex >= input.length ||\n        !Number.isInteger(fromIndex)) {\n        throw new Error('fromIndex must be a positive integer smaller than length');\n    }\n    if (toIndex <= fromIndex ||\n        toIndex > input.length ||\n        !Number.isInteger(toIndex)) {\n        throw new Error('toIndex must be an integer greater than fromIndex and at most equal to length');\n    }\n    let minValue = input[fromIndex];\n    for (let i = fromIndex + 1; i < toIndex; i++) {\n        if (input[i] < minValue)\n            minValue = input[i];\n    }\n    return minValue;\n}\n\n/**\n * Rescale an array into a range.\n */\nfunction rescale(input, options = {}) {\n    if (!isAnyArray(input)) {\n        throw new TypeError('input must be an array');\n    }\n    else if (input.length === 0) {\n        throw new TypeError('input must not be empty');\n    }\n    let output;\n    if (options.output !== undefined) {\n        if (!isAnyArray(options.output)) {\n            throw new TypeError('output option must be an array if specified');\n        }\n        output = options.output;\n    }\n    else {\n        output = new Array(input.length);\n    }\n    const currentMin = min(input);\n    const currentMax = max(input);\n    if (currentMin === currentMax) {\n        throw new RangeError('minimum and maximum input values are equal. Cannot rescale a constant array');\n    }\n    const { min: minValue = options.autoMinMax ? currentMin : 0, max: maxValue = options.autoMinMax ? currentMax : 1, } = options;\n    if (minValue >= maxValue) {\n        throw new RangeError('min option must be smaller than max option');\n    }\n    const factor = (maxValue - minValue) / (currentMax - currentMin);\n    for (let i = 0; i < input.length; i++) {\n        output[i] = (input[i] - currentMin) * factor + minValue;\n    }\n    return output;\n}\n\nconst indent = ' '.repeat(2);\nconst indentData = ' '.repeat(4);\n\n/**\n * @this {Matrix}\n * @returns {string}\n */\nfunction inspectMatrix() {\n  return inspectMatrixWithOptions(this);\n}\n\nfunction inspectMatrixWithOptions(matrix, options = {}) {\n  const {\n    maxRows = 15,\n    maxColumns = 10,\n    maxNumSize = 8,\n    padMinus = 'auto',\n  } = options;\n  return `${matrix.constructor.name} {\n${indent}[\n${indentData}${inspectData(matrix, maxRows, maxColumns, maxNumSize, padMinus)}\n${indent}]\n${indent}rows: ${matrix.rows}\n${indent}columns: ${matrix.columns}\n}`;\n}\n\nfunction inspectData(matrix, maxRows, maxColumns, maxNumSize, padMinus) {\n  const { rows, columns } = matrix;\n  const maxI = Math.min(rows, maxRows);\n  const maxJ = Math.min(columns, maxColumns);\n  const result = [];\n\n  if (padMinus === 'auto') {\n    padMinus = false;\n    loop: for (let i = 0; i < maxI; i++) {\n      for (let j = 0; j < maxJ; j++) {\n        if (matrix.get(i, j) < 0) {\n          padMinus = true;\n          break loop;\n        }\n      }\n    }\n  }\n\n  for (let i = 0; i < maxI; i++) {\n    let line = [];\n    for (let j = 0; j < maxJ; j++) {\n      line.push(formatNumber(matrix.get(i, j), maxNumSize, padMinus));\n    }\n    result.push(`${line.join(' ')}`);\n  }\n  if (maxJ !== columns) {\n    result[result.length - 1] += ` ... ${columns - maxColumns} more columns`;\n  }\n  if (maxI !== rows) {\n    result.push(`... ${rows - maxRows} more rows`);\n  }\n  return result.join(`\\n${indentData}`);\n}\n\nfunction formatNumber(num, maxNumSize, padMinus) {\n  return (\n    num >= 0 && padMinus\n      ? ` ${formatNumber2(num, maxNumSize - 1)}`\n      : formatNumber2(num, maxNumSize)\n  ).padEnd(maxNumSize);\n}\n\nfunction formatNumber2(num, len) {\n  // small.length numbers should be as is\n  let str = num.toString();\n  if (str.length <= len) return str;\n\n  // (7)'0.00123' is better then (7)'1.23e-2'\n  // (8)'0.000123' is worse then (7)'1.23e-3',\n  let fix = num.toFixed(len);\n  if (fix.length > len) {\n    fix = num.toFixed(Math.max(0, len - (fix.length - len)));\n  }\n  if (\n    fix.length <= len &&\n    !fix.startsWith('0.000') &&\n    !fix.startsWith('-0.000')\n  ) {\n    return fix;\n  }\n\n  // well, if it's still too long the user should've used longer numbers\n  let exp = num.toExponential(len);\n  if (exp.length > len) {\n    exp = num.toExponential(Math.max(0, len - (exp.length - len)));\n  }\n  return exp.slice(0);\n}\n\nfunction installMathOperations(AbstractMatrix, Matrix) {\n  AbstractMatrix.prototype.add = function add(value) {\n    if (typeof value === 'number') return this.addS(value);\n    return this.addM(value);\n  };\n\n  AbstractMatrix.prototype.addS = function addS(value) {\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, this.get(i, j) + value);\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.prototype.addM = function addM(matrix) {\n    matrix = Matrix.checkMatrix(matrix);\n    if (this.rows !== matrix.rows ||\n      this.columns !== matrix.columns) {\n      throw new RangeError('Matrices dimensions must be equal');\n    }\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, this.get(i, j) + matrix.get(i, j));\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.add = function add(matrix, value) {\n    const newMatrix = new Matrix(matrix);\n    return newMatrix.add(value);\n  };\n\n  AbstractMatrix.prototype.sub = function sub(value) {\n    if (typeof value === 'number') return this.subS(value);\n    return this.subM(value);\n  };\n\n  AbstractMatrix.prototype.subS = function subS(value) {\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, this.get(i, j) - value);\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.prototype.subM = function subM(matrix) {\n    matrix = Matrix.checkMatrix(matrix);\n    if (this.rows !== matrix.rows ||\n      this.columns !== matrix.columns) {\n      throw new RangeError('Matrices dimensions must be equal');\n    }\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, this.get(i, j) - matrix.get(i, j));\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.sub = function sub(matrix, value) {\n    const newMatrix = new Matrix(matrix);\n    return newMatrix.sub(value);\n  };\n  AbstractMatrix.prototype.subtract = AbstractMatrix.prototype.sub;\n  AbstractMatrix.prototype.subtractS = AbstractMatrix.prototype.subS;\n  AbstractMatrix.prototype.subtractM = AbstractMatrix.prototype.subM;\n  AbstractMatrix.subtract = AbstractMatrix.sub;\n\n  AbstractMatrix.prototype.mul = function mul(value) {\n    if (typeof value === 'number') return this.mulS(value);\n    return this.mulM(value);\n  };\n\n  AbstractMatrix.prototype.mulS = function mulS(value) {\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, this.get(i, j) * value);\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.prototype.mulM = function mulM(matrix) {\n    matrix = Matrix.checkMatrix(matrix);\n    if (this.rows !== matrix.rows ||\n      this.columns !== matrix.columns) {\n      throw new RangeError('Matrices dimensions must be equal');\n    }\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, this.get(i, j) * matrix.get(i, j));\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.mul = function mul(matrix, value) {\n    const newMatrix = new Matrix(matrix);\n    return newMatrix.mul(value);\n  };\n  AbstractMatrix.prototype.multiply = AbstractMatrix.prototype.mul;\n  AbstractMatrix.prototype.multiplyS = AbstractMatrix.prototype.mulS;\n  AbstractMatrix.prototype.multiplyM = AbstractMatrix.prototype.mulM;\n  AbstractMatrix.multiply = AbstractMatrix.mul;\n\n  AbstractMatrix.prototype.div = function div(value) {\n    if (typeof value === 'number') return this.divS(value);\n    return this.divM(value);\n  };\n\n  AbstractMatrix.prototype.divS = function divS(value) {\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, this.get(i, j) / value);\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.prototype.divM = function divM(matrix) {\n    matrix = Matrix.checkMatrix(matrix);\n    if (this.rows !== matrix.rows ||\n      this.columns !== matrix.columns) {\n      throw new RangeError('Matrices dimensions must be equal');\n    }\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, this.get(i, j) / matrix.get(i, j));\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.div = function div(matrix, value) {\n    const newMatrix = new Matrix(matrix);\n    return newMatrix.div(value);\n  };\n  AbstractMatrix.prototype.divide = AbstractMatrix.prototype.div;\n  AbstractMatrix.prototype.divideS = AbstractMatrix.prototype.divS;\n  AbstractMatrix.prototype.divideM = AbstractMatrix.prototype.divM;\n  AbstractMatrix.divide = AbstractMatrix.div;\n\n  AbstractMatrix.prototype.mod = function mod(value) {\n    if (typeof value === 'number') return this.modS(value);\n    return this.modM(value);\n  };\n\n  AbstractMatrix.prototype.modS = function modS(value) {\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, this.get(i, j) % value);\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.prototype.modM = function modM(matrix) {\n    matrix = Matrix.checkMatrix(matrix);\n    if (this.rows !== matrix.rows ||\n      this.columns !== matrix.columns) {\n      throw new RangeError('Matrices dimensions must be equal');\n    }\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, this.get(i, j) % matrix.get(i, j));\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.mod = function mod(matrix, value) {\n    const newMatrix = new Matrix(matrix);\n    return newMatrix.mod(value);\n  };\n  AbstractMatrix.prototype.modulus = AbstractMatrix.prototype.mod;\n  AbstractMatrix.prototype.modulusS = AbstractMatrix.prototype.modS;\n  AbstractMatrix.prototype.modulusM = AbstractMatrix.prototype.modM;\n  AbstractMatrix.modulus = AbstractMatrix.mod;\n\n  AbstractMatrix.prototype.and = function and(value) {\n    if (typeof value === 'number') return this.andS(value);\n    return this.andM(value);\n  };\n\n  AbstractMatrix.prototype.andS = function andS(value) {\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, this.get(i, j) & value);\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.prototype.andM = function andM(matrix) {\n    matrix = Matrix.checkMatrix(matrix);\n    if (this.rows !== matrix.rows ||\n      this.columns !== matrix.columns) {\n      throw new RangeError('Matrices dimensions must be equal');\n    }\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, this.get(i, j) & matrix.get(i, j));\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.and = function and(matrix, value) {\n    const newMatrix = new Matrix(matrix);\n    return newMatrix.and(value);\n  };\n\n  AbstractMatrix.prototype.or = function or(value) {\n    if (typeof value === 'number') return this.orS(value);\n    return this.orM(value);\n  };\n\n  AbstractMatrix.prototype.orS = function orS(value) {\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, this.get(i, j) | value);\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.prototype.orM = function orM(matrix) {\n    matrix = Matrix.checkMatrix(matrix);\n    if (this.rows !== matrix.rows ||\n      this.columns !== matrix.columns) {\n      throw new RangeError('Matrices dimensions must be equal');\n    }\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, this.get(i, j) | matrix.get(i, j));\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.or = function or(matrix, value) {\n    const newMatrix = new Matrix(matrix);\n    return newMatrix.or(value);\n  };\n\n  AbstractMatrix.prototype.xor = function xor(value) {\n    if (typeof value === 'number') return this.xorS(value);\n    return this.xorM(value);\n  };\n\n  AbstractMatrix.prototype.xorS = function xorS(value) {\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, this.get(i, j) ^ value);\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.prototype.xorM = function xorM(matrix) {\n    matrix = Matrix.checkMatrix(matrix);\n    if (this.rows !== matrix.rows ||\n      this.columns !== matrix.columns) {\n      throw new RangeError('Matrices dimensions must be equal');\n    }\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, this.get(i, j) ^ matrix.get(i, j));\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.xor = function xor(matrix, value) {\n    const newMatrix = new Matrix(matrix);\n    return newMatrix.xor(value);\n  };\n\n  AbstractMatrix.prototype.leftShift = function leftShift(value) {\n    if (typeof value === 'number') return this.leftShiftS(value);\n    return this.leftShiftM(value);\n  };\n\n  AbstractMatrix.prototype.leftShiftS = function leftShiftS(value) {\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, this.get(i, j) << value);\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.prototype.leftShiftM = function leftShiftM(matrix) {\n    matrix = Matrix.checkMatrix(matrix);\n    if (this.rows !== matrix.rows ||\n      this.columns !== matrix.columns) {\n      throw new RangeError('Matrices dimensions must be equal');\n    }\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, this.get(i, j) << matrix.get(i, j));\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.leftShift = function leftShift(matrix, value) {\n    const newMatrix = new Matrix(matrix);\n    return newMatrix.leftShift(value);\n  };\n\n  AbstractMatrix.prototype.signPropagatingRightShift = function signPropagatingRightShift(value) {\n    if (typeof value === 'number') return this.signPropagatingRightShiftS(value);\n    return this.signPropagatingRightShiftM(value);\n  };\n\n  AbstractMatrix.prototype.signPropagatingRightShiftS = function signPropagatingRightShiftS(value) {\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, this.get(i, j) >> value);\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.prototype.signPropagatingRightShiftM = function signPropagatingRightShiftM(matrix) {\n    matrix = Matrix.checkMatrix(matrix);\n    if (this.rows !== matrix.rows ||\n      this.columns !== matrix.columns) {\n      throw new RangeError('Matrices dimensions must be equal');\n    }\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, this.get(i, j) >> matrix.get(i, j));\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.signPropagatingRightShift = function signPropagatingRightShift(matrix, value) {\n    const newMatrix = new Matrix(matrix);\n    return newMatrix.signPropagatingRightShift(value);\n  };\n\n  AbstractMatrix.prototype.rightShift = function rightShift(value) {\n    if (typeof value === 'number') return this.rightShiftS(value);\n    return this.rightShiftM(value);\n  };\n\n  AbstractMatrix.prototype.rightShiftS = function rightShiftS(value) {\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, this.get(i, j) >>> value);\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.prototype.rightShiftM = function rightShiftM(matrix) {\n    matrix = Matrix.checkMatrix(matrix);\n    if (this.rows !== matrix.rows ||\n      this.columns !== matrix.columns) {\n      throw new RangeError('Matrices dimensions must be equal');\n    }\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, this.get(i, j) >>> matrix.get(i, j));\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.rightShift = function rightShift(matrix, value) {\n    const newMatrix = new Matrix(matrix);\n    return newMatrix.rightShift(value);\n  };\n  AbstractMatrix.prototype.zeroFillRightShift = AbstractMatrix.prototype.rightShift;\n  AbstractMatrix.prototype.zeroFillRightShiftS = AbstractMatrix.prototype.rightShiftS;\n  AbstractMatrix.prototype.zeroFillRightShiftM = AbstractMatrix.prototype.rightShiftM;\n  AbstractMatrix.zeroFillRightShift = AbstractMatrix.rightShift;\n\n  AbstractMatrix.prototype.not = function not() {\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, ~(this.get(i, j)));\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.not = function not(matrix) {\n    const newMatrix = new Matrix(matrix);\n    return newMatrix.not();\n  };\n\n  AbstractMatrix.prototype.abs = function abs() {\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, Math.abs(this.get(i, j)));\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.abs = function abs(matrix) {\n    const newMatrix = new Matrix(matrix);\n    return newMatrix.abs();\n  };\n\n  AbstractMatrix.prototype.acos = function acos() {\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, Math.acos(this.get(i, j)));\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.acos = function acos(matrix) {\n    const newMatrix = new Matrix(matrix);\n    return newMatrix.acos();\n  };\n\n  AbstractMatrix.prototype.acosh = function acosh() {\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, Math.acosh(this.get(i, j)));\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.acosh = function acosh(matrix) {\n    const newMatrix = new Matrix(matrix);\n    return newMatrix.acosh();\n  };\n\n  AbstractMatrix.prototype.asin = function asin() {\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, Math.asin(this.get(i, j)));\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.asin = function asin(matrix) {\n    const newMatrix = new Matrix(matrix);\n    return newMatrix.asin();\n  };\n\n  AbstractMatrix.prototype.asinh = function asinh() {\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, Math.asinh(this.get(i, j)));\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.asinh = function asinh(matrix) {\n    const newMatrix = new Matrix(matrix);\n    return newMatrix.asinh();\n  };\n\n  AbstractMatrix.prototype.atan = function atan() {\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, Math.atan(this.get(i, j)));\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.atan = function atan(matrix) {\n    const newMatrix = new Matrix(matrix);\n    return newMatrix.atan();\n  };\n\n  AbstractMatrix.prototype.atanh = function atanh() {\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, Math.atanh(this.get(i, j)));\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.atanh = function atanh(matrix) {\n    const newMatrix = new Matrix(matrix);\n    return newMatrix.atanh();\n  };\n\n  AbstractMatrix.prototype.cbrt = function cbrt() {\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, Math.cbrt(this.get(i, j)));\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.cbrt = function cbrt(matrix) {\n    const newMatrix = new Matrix(matrix);\n    return newMatrix.cbrt();\n  };\n\n  AbstractMatrix.prototype.ceil = function ceil() {\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, Math.ceil(this.get(i, j)));\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.ceil = function ceil(matrix) {\n    const newMatrix = new Matrix(matrix);\n    return newMatrix.ceil();\n  };\n\n  AbstractMatrix.prototype.clz32 = function clz32() {\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, Math.clz32(this.get(i, j)));\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.clz32 = function clz32(matrix) {\n    const newMatrix = new Matrix(matrix);\n    return newMatrix.clz32();\n  };\n\n  AbstractMatrix.prototype.cos = function cos() {\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, Math.cos(this.get(i, j)));\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.cos = function cos(matrix) {\n    const newMatrix = new Matrix(matrix);\n    return newMatrix.cos();\n  };\n\n  AbstractMatrix.prototype.cosh = function cosh() {\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, Math.cosh(this.get(i, j)));\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.cosh = function cosh(matrix) {\n    const newMatrix = new Matrix(matrix);\n    return newMatrix.cosh();\n  };\n\n  AbstractMatrix.prototype.exp = function exp() {\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, Math.exp(this.get(i, j)));\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.exp = function exp(matrix) {\n    const newMatrix = new Matrix(matrix);\n    return newMatrix.exp();\n  };\n\n  AbstractMatrix.prototype.expm1 = function expm1() {\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, Math.expm1(this.get(i, j)));\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.expm1 = function expm1(matrix) {\n    const newMatrix = new Matrix(matrix);\n    return newMatrix.expm1();\n  };\n\n  AbstractMatrix.prototype.floor = function floor() {\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, Math.floor(this.get(i, j)));\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.floor = function floor(matrix) {\n    const newMatrix = new Matrix(matrix);\n    return newMatrix.floor();\n  };\n\n  AbstractMatrix.prototype.fround = function fround() {\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, Math.fround(this.get(i, j)));\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.fround = function fround(matrix) {\n    const newMatrix = new Matrix(matrix);\n    return newMatrix.fround();\n  };\n\n  AbstractMatrix.prototype.log = function log() {\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, Math.log(this.get(i, j)));\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.log = function log(matrix) {\n    const newMatrix = new Matrix(matrix);\n    return newMatrix.log();\n  };\n\n  AbstractMatrix.prototype.log1p = function log1p() {\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, Math.log1p(this.get(i, j)));\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.log1p = function log1p(matrix) {\n    const newMatrix = new Matrix(matrix);\n    return newMatrix.log1p();\n  };\n\n  AbstractMatrix.prototype.log10 = function log10() {\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, Math.log10(this.get(i, j)));\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.log10 = function log10(matrix) {\n    const newMatrix = new Matrix(matrix);\n    return newMatrix.log10();\n  };\n\n  AbstractMatrix.prototype.log2 = function log2() {\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, Math.log2(this.get(i, j)));\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.log2 = function log2(matrix) {\n    const newMatrix = new Matrix(matrix);\n    return newMatrix.log2();\n  };\n\n  AbstractMatrix.prototype.round = function round() {\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, Math.round(this.get(i, j)));\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.round = function round(matrix) {\n    const newMatrix = new Matrix(matrix);\n    return newMatrix.round();\n  };\n\n  AbstractMatrix.prototype.sign = function sign() {\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, Math.sign(this.get(i, j)));\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.sign = function sign(matrix) {\n    const newMatrix = new Matrix(matrix);\n    return newMatrix.sign();\n  };\n\n  AbstractMatrix.prototype.sin = function sin() {\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, Math.sin(this.get(i, j)));\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.sin = function sin(matrix) {\n    const newMatrix = new Matrix(matrix);\n    return newMatrix.sin();\n  };\n\n  AbstractMatrix.prototype.sinh = function sinh() {\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, Math.sinh(this.get(i, j)));\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.sinh = function sinh(matrix) {\n    const newMatrix = new Matrix(matrix);\n    return newMatrix.sinh();\n  };\n\n  AbstractMatrix.prototype.sqrt = function sqrt() {\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, Math.sqrt(this.get(i, j)));\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.sqrt = function sqrt(matrix) {\n    const newMatrix = new Matrix(matrix);\n    return newMatrix.sqrt();\n  };\n\n  AbstractMatrix.prototype.tan = function tan() {\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, Math.tan(this.get(i, j)));\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.tan = function tan(matrix) {\n    const newMatrix = new Matrix(matrix);\n    return newMatrix.tan();\n  };\n\n  AbstractMatrix.prototype.tanh = function tanh() {\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, Math.tanh(this.get(i, j)));\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.tanh = function tanh(matrix) {\n    const newMatrix = new Matrix(matrix);\n    return newMatrix.tanh();\n  };\n\n  AbstractMatrix.prototype.trunc = function trunc() {\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, Math.trunc(this.get(i, j)));\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.trunc = function trunc(matrix) {\n    const newMatrix = new Matrix(matrix);\n    return newMatrix.trunc();\n  };\n\n  AbstractMatrix.pow = function pow(matrix, arg0) {\n    const newMatrix = new Matrix(matrix);\n    return newMatrix.pow(arg0);\n  };\n\n  AbstractMatrix.prototype.pow = function pow(value) {\n    if (typeof value === 'number') return this.powS(value);\n    return this.powM(value);\n  };\n\n  AbstractMatrix.prototype.powS = function powS(value) {\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, this.get(i, j) ** value);\n      }\n    }\n    return this;\n  };\n\n  AbstractMatrix.prototype.powM = function powM(matrix) {\n    matrix = Matrix.checkMatrix(matrix);\n    if (this.rows !== matrix.rows ||\n      this.columns !== matrix.columns) {\n      throw new RangeError('Matrices dimensions must be equal');\n    }\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, this.get(i, j) ** matrix.get(i, j));\n      }\n    }\n    return this;\n  };\n}\n\n/**\n * @private\n * Check that a row index is not out of bounds\n * @param {Matrix} matrix\n * @param {number} index\n * @param {boolean} [outer]\n */\nfunction checkRowIndex(matrix, index, outer) {\n  let max = outer ? matrix.rows : matrix.rows - 1;\n  if (index < 0 || index > max) {\n    throw new RangeError('Row index out of range');\n  }\n}\n\n/**\n * @private\n * Check that a column index is not out of bounds\n * @param {Matrix} matrix\n * @param {number} index\n * @param {boolean} [outer]\n */\nfunction checkColumnIndex(matrix, index, outer) {\n  let max = outer ? matrix.columns : matrix.columns - 1;\n  if (index < 0 || index > max) {\n    throw new RangeError('Column index out of range');\n  }\n}\n\n/**\n * @private\n * Check that the provided vector is an array with the right length\n * @param {Matrix} matrix\n * @param {Array|Matrix} vector\n * @return {Array}\n * @throws {RangeError}\n */\nfunction checkRowVector(matrix, vector) {\n  if (vector.to1DArray) {\n    vector = vector.to1DArray();\n  }\n  if (vector.length !== matrix.columns) {\n    throw new RangeError(\n      'vector size must be the same as the number of columns',\n    );\n  }\n  return vector;\n}\n\n/**\n * @private\n * Check that the provided vector is an array with the right length\n * @param {Matrix} matrix\n * @param {Array|Matrix} vector\n * @return {Array}\n * @throws {RangeError}\n */\nfunction checkColumnVector(matrix, vector) {\n  if (vector.to1DArray) {\n    vector = vector.to1DArray();\n  }\n  if (vector.length !== matrix.rows) {\n    throw new RangeError('vector size must be the same as the number of rows');\n  }\n  return vector;\n}\n\nfunction checkRowIndices(matrix, rowIndices) {\n  if (!isAnyArray(rowIndices)) {\n    throw new TypeError('row indices must be an array');\n  }\n\n  for (let i = 0; i < rowIndices.length; i++) {\n    if (rowIndices[i] < 0 || rowIndices[i] >= matrix.rows) {\n      throw new RangeError('row indices are out of range');\n    }\n  }\n}\n\nfunction checkColumnIndices(matrix, columnIndices) {\n  if (!isAnyArray(columnIndices)) {\n    throw new TypeError('column indices must be an array');\n  }\n\n  for (let i = 0; i < columnIndices.length; i++) {\n    if (columnIndices[i] < 0 || columnIndices[i] >= matrix.columns) {\n      throw new RangeError('column indices are out of range');\n    }\n  }\n}\n\nfunction checkRange(matrix, startRow, endRow, startColumn, endColumn) {\n  if (arguments.length !== 5) {\n    throw new RangeError('expected 4 arguments');\n  }\n  checkNumber('startRow', startRow);\n  checkNumber('endRow', endRow);\n  checkNumber('startColumn', startColumn);\n  checkNumber('endColumn', endColumn);\n  if (\n    startRow > endRow ||\n    startColumn > endColumn ||\n    startRow < 0 ||\n    startRow >= matrix.rows ||\n    endRow < 0 ||\n    endRow >= matrix.rows ||\n    startColumn < 0 ||\n    startColumn >= matrix.columns ||\n    endColumn < 0 ||\n    endColumn >= matrix.columns\n  ) {\n    throw new RangeError('Submatrix indices are out of range');\n  }\n}\n\nfunction newArray(length, value = 0) {\n  let array = [];\n  for (let i = 0; i < length; i++) {\n    array.push(value);\n  }\n  return array;\n}\n\nfunction checkNumber(name, value) {\n  if (typeof value !== 'number') {\n    throw new TypeError(`${name} must be a number`);\n  }\n}\n\nfunction checkNonEmpty(matrix) {\n  if (matrix.isEmpty()) {\n    throw new Error('Empty matrix has no elements to index');\n  }\n}\n\nfunction sumByRow(matrix) {\n  let sum = newArray(matrix.rows);\n  for (let i = 0; i < matrix.rows; ++i) {\n    for (let j = 0; j < matrix.columns; ++j) {\n      sum[i] += matrix.get(i, j);\n    }\n  }\n  return sum;\n}\n\nfunction sumByColumn(matrix) {\n  let sum = newArray(matrix.columns);\n  for (let i = 0; i < matrix.rows; ++i) {\n    for (let j = 0; j < matrix.columns; ++j) {\n      sum[j] += matrix.get(i, j);\n    }\n  }\n  return sum;\n}\n\nfunction sumAll(matrix) {\n  let v = 0;\n  for (let i = 0; i < matrix.rows; i++) {\n    for (let j = 0; j < matrix.columns; j++) {\n      v += matrix.get(i, j);\n    }\n  }\n  return v;\n}\n\nfunction productByRow(matrix) {\n  let sum = newArray(matrix.rows, 1);\n  for (let i = 0; i < matrix.rows; ++i) {\n    for (let j = 0; j < matrix.columns; ++j) {\n      sum[i] *= matrix.get(i, j);\n    }\n  }\n  return sum;\n}\n\nfunction productByColumn(matrix) {\n  let sum = newArray(matrix.columns, 1);\n  for (let i = 0; i < matrix.rows; ++i) {\n    for (let j = 0; j < matrix.columns; ++j) {\n      sum[j] *= matrix.get(i, j);\n    }\n  }\n  return sum;\n}\n\nfunction productAll(matrix) {\n  let v = 1;\n  for (let i = 0; i < matrix.rows; i++) {\n    for (let j = 0; j < matrix.columns; j++) {\n      v *= matrix.get(i, j);\n    }\n  }\n  return v;\n}\n\nfunction varianceByRow(matrix, unbiased, mean) {\n  const rows = matrix.rows;\n  const cols = matrix.columns;\n  const variance = [];\n\n  for (let i = 0; i < rows; i++) {\n    let sum1 = 0;\n    let sum2 = 0;\n    let x = 0;\n    for (let j = 0; j < cols; j++) {\n      x = matrix.get(i, j) - mean[i];\n      sum1 += x;\n      sum2 += x * x;\n    }\n    if (unbiased) {\n      variance.push((sum2 - (sum1 * sum1) / cols) / (cols - 1));\n    } else {\n      variance.push((sum2 - (sum1 * sum1) / cols) / cols);\n    }\n  }\n  return variance;\n}\n\nfunction varianceByColumn(matrix, unbiased, mean) {\n  const rows = matrix.rows;\n  const cols = matrix.columns;\n  const variance = [];\n\n  for (let j = 0; j < cols; j++) {\n    let sum1 = 0;\n    let sum2 = 0;\n    let x = 0;\n    for (let i = 0; i < rows; i++) {\n      x = matrix.get(i, j) - mean[j];\n      sum1 += x;\n      sum2 += x * x;\n    }\n    if (unbiased) {\n      variance.push((sum2 - (sum1 * sum1) / rows) / (rows - 1));\n    } else {\n      variance.push((sum2 - (sum1 * sum1) / rows) / rows);\n    }\n  }\n  return variance;\n}\n\nfunction varianceAll(matrix, unbiased, mean) {\n  const rows = matrix.rows;\n  const cols = matrix.columns;\n  const size = rows * cols;\n\n  let sum1 = 0;\n  let sum2 = 0;\n  let x = 0;\n  for (let i = 0; i < rows; i++) {\n    for (let j = 0; j < cols; j++) {\n      x = matrix.get(i, j) - mean;\n      sum1 += x;\n      sum2 += x * x;\n    }\n  }\n  if (unbiased) {\n    return (sum2 - (sum1 * sum1) / size) / (size - 1);\n  } else {\n    return (sum2 - (sum1 * sum1) / size) / size;\n  }\n}\n\nfunction centerByRow(matrix, mean) {\n  for (let i = 0; i < matrix.rows; i++) {\n    for (let j = 0; j < matrix.columns; j++) {\n      matrix.set(i, j, matrix.get(i, j) - mean[i]);\n    }\n  }\n}\n\nfunction centerByColumn(matrix, mean) {\n  for (let i = 0; i < matrix.rows; i++) {\n    for (let j = 0; j < matrix.columns; j++) {\n      matrix.set(i, j, matrix.get(i, j) - mean[j]);\n    }\n  }\n}\n\nfunction centerAll(matrix, mean) {\n  for (let i = 0; i < matrix.rows; i++) {\n    for (let j = 0; j < matrix.columns; j++) {\n      matrix.set(i, j, matrix.get(i, j) - mean);\n    }\n  }\n}\n\nfunction getScaleByRow(matrix) {\n  const scale = [];\n  for (let i = 0; i < matrix.rows; i++) {\n    let sum = 0;\n    for (let j = 0; j < matrix.columns; j++) {\n      sum += matrix.get(i, j) ** 2 / (matrix.columns - 1);\n    }\n    scale.push(Math.sqrt(sum));\n  }\n  return scale;\n}\n\nfunction scaleByRow(matrix, scale) {\n  for (let i = 0; i < matrix.rows; i++) {\n    for (let j = 0; j < matrix.columns; j++) {\n      matrix.set(i, j, matrix.get(i, j) / scale[i]);\n    }\n  }\n}\n\nfunction getScaleByColumn(matrix) {\n  const scale = [];\n  for (let j = 0; j < matrix.columns; j++) {\n    let sum = 0;\n    for (let i = 0; i < matrix.rows; i++) {\n      sum += matrix.get(i, j) ** 2 / (matrix.rows - 1);\n    }\n    scale.push(Math.sqrt(sum));\n  }\n  return scale;\n}\n\nfunction scaleByColumn(matrix, scale) {\n  for (let i = 0; i < matrix.rows; i++) {\n    for (let j = 0; j < matrix.columns; j++) {\n      matrix.set(i, j, matrix.get(i, j) / scale[j]);\n    }\n  }\n}\n\nfunction getScaleAll(matrix) {\n  const divider = matrix.size - 1;\n  let sum = 0;\n  for (let j = 0; j < matrix.columns; j++) {\n    for (let i = 0; i < matrix.rows; i++) {\n      sum += matrix.get(i, j) ** 2 / divider;\n    }\n  }\n  return Math.sqrt(sum);\n}\n\nfunction scaleAll(matrix, scale) {\n  for (let i = 0; i < matrix.rows; i++) {\n    for (let j = 0; j < matrix.columns; j++) {\n      matrix.set(i, j, matrix.get(i, j) / scale);\n    }\n  }\n}\n\nclass AbstractMatrix {\n  static from1DArray(newRows, newColumns, newData) {\n    let length = newRows * newColumns;\n    if (length !== newData.length) {\n      throw new RangeError('data length does not match given dimensions');\n    }\n    let newMatrix = new Matrix(newRows, newColumns);\n    for (let row = 0; row < newRows; row++) {\n      for (let column = 0; column < newColumns; column++) {\n        newMatrix.set(row, column, newData[row * newColumns + column]);\n      }\n    }\n    return newMatrix;\n  }\n\n  static rowVector(newData) {\n    let vector = new Matrix(1, newData.length);\n    for (let i = 0; i < newData.length; i++) {\n      vector.set(0, i, newData[i]);\n    }\n    return vector;\n  }\n\n  static columnVector(newData) {\n    let vector = new Matrix(newData.length, 1);\n    for (let i = 0; i < newData.length; i++) {\n      vector.set(i, 0, newData[i]);\n    }\n    return vector;\n  }\n\n  static zeros(rows, columns) {\n    return new Matrix(rows, columns);\n  }\n\n  static ones(rows, columns) {\n    return new Matrix(rows, columns).fill(1);\n  }\n\n  static rand(rows, columns, options = {}) {\n    if (typeof options !== 'object') {\n      throw new TypeError('options must be an object');\n    }\n    const { random = Math.random } = options;\n    let matrix = new Matrix(rows, columns);\n    for (let i = 0; i < rows; i++) {\n      for (let j = 0; j < columns; j++) {\n        matrix.set(i, j, random());\n      }\n    }\n    return matrix;\n  }\n\n  static randInt(rows, columns, options = {}) {\n    if (typeof options !== 'object') {\n      throw new TypeError('options must be an object');\n    }\n    const { min = 0, max = 1000, random = Math.random } = options;\n    if (!Number.isInteger(min)) throw new TypeError('min must be an integer');\n    if (!Number.isInteger(max)) throw new TypeError('max must be an integer');\n    if (min >= max) throw new RangeError('min must be smaller than max');\n    let interval = max - min;\n    let matrix = new Matrix(rows, columns);\n    for (let i = 0; i < rows; i++) {\n      for (let j = 0; j < columns; j++) {\n        let value = min + Math.round(random() * interval);\n        matrix.set(i, j, value);\n      }\n    }\n    return matrix;\n  }\n\n  static eye(rows, columns, value) {\n    if (columns === undefined) columns = rows;\n    if (value === undefined) value = 1;\n    let min = Math.min(rows, columns);\n    let matrix = this.zeros(rows, columns);\n    for (let i = 0; i < min; i++) {\n      matrix.set(i, i, value);\n    }\n    return matrix;\n  }\n\n  static diag(data, rows, columns) {\n    let l = data.length;\n    if (rows === undefined) rows = l;\n    if (columns === undefined) columns = rows;\n    let min = Math.min(l, rows, columns);\n    let matrix = this.zeros(rows, columns);\n    for (let i = 0; i < min; i++) {\n      matrix.set(i, i, data[i]);\n    }\n    return matrix;\n  }\n\n  static min(matrix1, matrix2) {\n    matrix1 = this.checkMatrix(matrix1);\n    matrix2 = this.checkMatrix(matrix2);\n    let rows = matrix1.rows;\n    let columns = matrix1.columns;\n    let result = new Matrix(rows, columns);\n    for (let i = 0; i < rows; i++) {\n      for (let j = 0; j < columns; j++) {\n        result.set(i, j, Math.min(matrix1.get(i, j), matrix2.get(i, j)));\n      }\n    }\n    return result;\n  }\n\n  static max(matrix1, matrix2) {\n    matrix1 = this.checkMatrix(matrix1);\n    matrix2 = this.checkMatrix(matrix2);\n    let rows = matrix1.rows;\n    let columns = matrix1.columns;\n    let result = new this(rows, columns);\n    for (let i = 0; i < rows; i++) {\n      for (let j = 0; j < columns; j++) {\n        result.set(i, j, Math.max(matrix1.get(i, j), matrix2.get(i, j)));\n      }\n    }\n    return result;\n  }\n\n  static checkMatrix(value) {\n    return AbstractMatrix.isMatrix(value) ? value : new Matrix(value);\n  }\n\n  static isMatrix(value) {\n    return value != null && value.klass === 'Matrix';\n  }\n\n  get size() {\n    return this.rows * this.columns;\n  }\n\n  apply(callback) {\n    if (typeof callback !== 'function') {\n      throw new TypeError('callback must be a function');\n    }\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        callback.call(this, i, j);\n      }\n    }\n    return this;\n  }\n\n  to1DArray() {\n    let array = [];\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        array.push(this.get(i, j));\n      }\n    }\n    return array;\n  }\n\n  to2DArray() {\n    let copy = [];\n    for (let i = 0; i < this.rows; i++) {\n      copy.push([]);\n      for (let j = 0; j < this.columns; j++) {\n        copy[i].push(this.get(i, j));\n      }\n    }\n    return copy;\n  }\n\n  toJSON() {\n    return this.to2DArray();\n  }\n\n  isRowVector() {\n    return this.rows === 1;\n  }\n\n  isColumnVector() {\n    return this.columns === 1;\n  }\n\n  isVector() {\n    return this.rows === 1 || this.columns === 1;\n  }\n\n  isSquare() {\n    return this.rows === this.columns;\n  }\n\n  isEmpty() {\n    return this.rows === 0 || this.columns === 0;\n  }\n\n  isSymmetric() {\n    if (this.isSquare()) {\n      for (let i = 0; i < this.rows; i++) {\n        for (let j = 0; j <= i; j++) {\n          if (this.get(i, j) !== this.get(j, i)) {\n            return false;\n          }\n        }\n      }\n      return true;\n    }\n    return false;\n  }\n\n  isDistance() {\n    if (!this.isSymmetric()) return false;\n\n    for (let i = 0; i < this.rows; i++) {\n      if (this.get(i, i) !== 0) return false;\n    }\n\n    return true;\n  }\n\n  isEchelonForm() {\n    let i = 0;\n    let j = 0;\n    let previousColumn = -1;\n    let isEchelonForm = true;\n    let checked = false;\n    while (i < this.rows && isEchelonForm) {\n      j = 0;\n      checked = false;\n      while (j < this.columns && checked === false) {\n        if (this.get(i, j) === 0) {\n          j++;\n        } else if (this.get(i, j) === 1 && j > previousColumn) {\n          checked = true;\n          previousColumn = j;\n        } else {\n          isEchelonForm = false;\n          checked = true;\n        }\n      }\n      i++;\n    }\n    return isEchelonForm;\n  }\n\n  isReducedEchelonForm() {\n    let i = 0;\n    let j = 0;\n    let previousColumn = -1;\n    let isReducedEchelonForm = true;\n    let checked = false;\n    while (i < this.rows && isReducedEchelonForm) {\n      j = 0;\n      checked = false;\n      while (j < this.columns && checked === false) {\n        if (this.get(i, j) === 0) {\n          j++;\n        } else if (this.get(i, j) === 1 && j > previousColumn) {\n          checked = true;\n          previousColumn = j;\n        } else {\n          isReducedEchelonForm = false;\n          checked = true;\n        }\n      }\n      for (let k = j + 1; k < this.rows; k++) {\n        if (this.get(i, k) !== 0) {\n          isReducedEchelonForm = false;\n        }\n      }\n      i++;\n    }\n    return isReducedEchelonForm;\n  }\n\n  echelonForm() {\n    let result = this.clone();\n    let h = 0;\n    let k = 0;\n    while (h < result.rows && k < result.columns) {\n      let iMax = h;\n      for (let i = h; i < result.rows; i++) {\n        if (result.get(i, k) > result.get(iMax, k)) {\n          iMax = i;\n        }\n      }\n      if (result.get(iMax, k) === 0) {\n        k++;\n      } else {\n        result.swapRows(h, iMax);\n        let tmp = result.get(h, k);\n        for (let j = k; j < result.columns; j++) {\n          result.set(h, j, result.get(h, j) / tmp);\n        }\n        for (let i = h + 1; i < result.rows; i++) {\n          let factor = result.get(i, k) / result.get(h, k);\n          result.set(i, k, 0);\n          for (let j = k + 1; j < result.columns; j++) {\n            result.set(i, j, result.get(i, j) - result.get(h, j) * factor);\n          }\n        }\n        h++;\n        k++;\n      }\n    }\n    return result;\n  }\n\n  reducedEchelonForm() {\n    let result = this.echelonForm();\n    let m = result.columns;\n    let n = result.rows;\n    let h = n - 1;\n    while (h >= 0) {\n      if (result.maxRow(h) === 0) {\n        h--;\n      } else {\n        let p = 0;\n        let pivot = false;\n        while (p < n && pivot === false) {\n          if (result.get(h, p) === 1) {\n            pivot = true;\n          } else {\n            p++;\n          }\n        }\n        for (let i = 0; i < h; i++) {\n          let factor = result.get(i, p);\n          for (let j = p; j < m; j++) {\n            let tmp = result.get(i, j) - factor * result.get(h, j);\n            result.set(i, j, tmp);\n          }\n        }\n        h--;\n      }\n    }\n    return result;\n  }\n\n  set() {\n    throw new Error('set method is unimplemented');\n  }\n\n  get() {\n    throw new Error('get method is unimplemented');\n  }\n\n  repeat(options = {}) {\n    if (typeof options !== 'object') {\n      throw new TypeError('options must be an object');\n    }\n    const { rows = 1, columns = 1 } = options;\n    if (!Number.isInteger(rows) || rows <= 0) {\n      throw new TypeError('rows must be a positive integer');\n    }\n    if (!Number.isInteger(columns) || columns <= 0) {\n      throw new TypeError('columns must be a positive integer');\n    }\n    let matrix = new Matrix(this.rows * rows, this.columns * columns);\n    for (let i = 0; i < rows; i++) {\n      for (let j = 0; j < columns; j++) {\n        matrix.setSubMatrix(this, this.rows * i, this.columns * j);\n      }\n    }\n    return matrix;\n  }\n\n  fill(value) {\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, value);\n      }\n    }\n    return this;\n  }\n\n  neg() {\n    return this.mulS(-1);\n  }\n\n  getRow(index) {\n    checkRowIndex(this, index);\n    let row = [];\n    for (let i = 0; i < this.columns; i++) {\n      row.push(this.get(index, i));\n    }\n    return row;\n  }\n\n  getRowVector(index) {\n    return Matrix.rowVector(this.getRow(index));\n  }\n\n  setRow(index, array) {\n    checkRowIndex(this, index);\n    array = checkRowVector(this, array);\n    for (let i = 0; i < this.columns; i++) {\n      this.set(index, i, array[i]);\n    }\n    return this;\n  }\n\n  swapRows(row1, row2) {\n    checkRowIndex(this, row1);\n    checkRowIndex(this, row2);\n    for (let i = 0; i < this.columns; i++) {\n      let temp = this.get(row1, i);\n      this.set(row1, i, this.get(row2, i));\n      this.set(row2, i, temp);\n    }\n    return this;\n  }\n\n  getColumn(index) {\n    checkColumnIndex(this, index);\n    let column = [];\n    for (let i = 0; i < this.rows; i++) {\n      column.push(this.get(i, index));\n    }\n    return column;\n  }\n\n  getColumnVector(index) {\n    return Matrix.columnVector(this.getColumn(index));\n  }\n\n  setColumn(index, array) {\n    checkColumnIndex(this, index);\n    array = checkColumnVector(this, array);\n    for (let i = 0; i < this.rows; i++) {\n      this.set(i, index, array[i]);\n    }\n    return this;\n  }\n\n  swapColumns(column1, column2) {\n    checkColumnIndex(this, column1);\n    checkColumnIndex(this, column2);\n    for (let i = 0; i < this.rows; i++) {\n      let temp = this.get(i, column1);\n      this.set(i, column1, this.get(i, column2));\n      this.set(i, column2, temp);\n    }\n    return this;\n  }\n\n  addRowVector(vector) {\n    vector = checkRowVector(this, vector);\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, this.get(i, j) + vector[j]);\n      }\n    }\n    return this;\n  }\n\n  subRowVector(vector) {\n    vector = checkRowVector(this, vector);\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, this.get(i, j) - vector[j]);\n      }\n    }\n    return this;\n  }\n\n  mulRowVector(vector) {\n    vector = checkRowVector(this, vector);\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, this.get(i, j) * vector[j]);\n      }\n    }\n    return this;\n  }\n\n  divRowVector(vector) {\n    vector = checkRowVector(this, vector);\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, this.get(i, j) / vector[j]);\n      }\n    }\n    return this;\n  }\n\n  addColumnVector(vector) {\n    vector = checkColumnVector(this, vector);\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, this.get(i, j) + vector[i]);\n      }\n    }\n    return this;\n  }\n\n  subColumnVector(vector) {\n    vector = checkColumnVector(this, vector);\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, this.get(i, j) - vector[i]);\n      }\n    }\n    return this;\n  }\n\n  mulColumnVector(vector) {\n    vector = checkColumnVector(this, vector);\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, this.get(i, j) * vector[i]);\n      }\n    }\n    return this;\n  }\n\n  divColumnVector(vector) {\n    vector = checkColumnVector(this, vector);\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        this.set(i, j, this.get(i, j) / vector[i]);\n      }\n    }\n    return this;\n  }\n\n  mulRow(index, value) {\n    checkRowIndex(this, index);\n    for (let i = 0; i < this.columns; i++) {\n      this.set(index, i, this.get(index, i) * value);\n    }\n    return this;\n  }\n\n  mulColumn(index, value) {\n    checkColumnIndex(this, index);\n    for (let i = 0; i < this.rows; i++) {\n      this.set(i, index, this.get(i, index) * value);\n    }\n    return this;\n  }\n\n  max(by) {\n    if (this.isEmpty()) {\n      return NaN;\n    }\n    switch (by) {\n      case 'row': {\n        const max = new Array(this.rows).fill(Number.NEGATIVE_INFINITY);\n        for (let row = 0; row < this.rows; row++) {\n          for (let column = 0; column < this.columns; column++) {\n            if (this.get(row, column) > max[row]) {\n              max[row] = this.get(row, column);\n            }\n          }\n        }\n        return max;\n      }\n      case 'column': {\n        const max = new Array(this.columns).fill(Number.NEGATIVE_INFINITY);\n        for (let row = 0; row < this.rows; row++) {\n          for (let column = 0; column < this.columns; column++) {\n            if (this.get(row, column) > max[column]) {\n              max[column] = this.get(row, column);\n            }\n          }\n        }\n        return max;\n      }\n      case undefined: {\n        let max = this.get(0, 0);\n        for (let row = 0; row < this.rows; row++) {\n          for (let column = 0; column < this.columns; column++) {\n            if (this.get(row, column) > max) {\n              max = this.get(row, column);\n            }\n          }\n        }\n        return max;\n      }\n      default:\n        throw new Error(`invalid option: ${by}`);\n    }\n  }\n\n  maxIndex() {\n    checkNonEmpty(this);\n    let v = this.get(0, 0);\n    let idx = [0, 0];\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        if (this.get(i, j) > v) {\n          v = this.get(i, j);\n          idx[0] = i;\n          idx[1] = j;\n        }\n      }\n    }\n    return idx;\n  }\n\n  min(by) {\n    if (this.isEmpty()) {\n      return NaN;\n    }\n\n    switch (by) {\n      case 'row': {\n        const min = new Array(this.rows).fill(Number.POSITIVE_INFINITY);\n        for (let row = 0; row < this.rows; row++) {\n          for (let column = 0; column < this.columns; column++) {\n            if (this.get(row, column) < min[row]) {\n              min[row] = this.get(row, column);\n            }\n          }\n        }\n        return min;\n      }\n      case 'column': {\n        const min = new Array(this.columns).fill(Number.POSITIVE_INFINITY);\n        for (let row = 0; row < this.rows; row++) {\n          for (let column = 0; column < this.columns; column++) {\n            if (this.get(row, column) < min[column]) {\n              min[column] = this.get(row, column);\n            }\n          }\n        }\n        return min;\n      }\n      case undefined: {\n        let min = this.get(0, 0);\n        for (let row = 0; row < this.rows; row++) {\n          for (let column = 0; column < this.columns; column++) {\n            if (this.get(row, column) < min) {\n              min = this.get(row, column);\n            }\n          }\n        }\n        return min;\n      }\n      default:\n        throw new Error(`invalid option: ${by}`);\n    }\n  }\n\n  minIndex() {\n    checkNonEmpty(this);\n    let v = this.get(0, 0);\n    let idx = [0, 0];\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        if (this.get(i, j) < v) {\n          v = this.get(i, j);\n          idx[0] = i;\n          idx[1] = j;\n        }\n      }\n    }\n    return idx;\n  }\n\n  maxRow(row) {\n    checkRowIndex(this, row);\n    if (this.isEmpty()) {\n      return NaN;\n    }\n    let v = this.get(row, 0);\n    for (let i = 1; i < this.columns; i++) {\n      if (this.get(row, i) > v) {\n        v = this.get(row, i);\n      }\n    }\n    return v;\n  }\n\n  maxRowIndex(row) {\n    checkRowIndex(this, row);\n    checkNonEmpty(this);\n    let v = this.get(row, 0);\n    let idx = [row, 0];\n    for (let i = 1; i < this.columns; i++) {\n      if (this.get(row, i) > v) {\n        v = this.get(row, i);\n        idx[1] = i;\n      }\n    }\n    return idx;\n  }\n\n  minRow(row) {\n    checkRowIndex(this, row);\n    if (this.isEmpty()) {\n      return NaN;\n    }\n    let v = this.get(row, 0);\n    for (let i = 1; i < this.columns; i++) {\n      if (this.get(row, i) < v) {\n        v = this.get(row, i);\n      }\n    }\n    return v;\n  }\n\n  minRowIndex(row) {\n    checkRowIndex(this, row);\n    checkNonEmpty(this);\n    let v = this.get(row, 0);\n    let idx = [row, 0];\n    for (let i = 1; i < this.columns; i++) {\n      if (this.get(row, i) < v) {\n        v = this.get(row, i);\n        idx[1] = i;\n      }\n    }\n    return idx;\n  }\n\n  maxColumn(column) {\n    checkColumnIndex(this, column);\n    if (this.isEmpty()) {\n      return NaN;\n    }\n    let v = this.get(0, column);\n    for (let i = 1; i < this.rows; i++) {\n      if (this.get(i, column) > v) {\n        v = this.get(i, column);\n      }\n    }\n    return v;\n  }\n\n  maxColumnIndex(column) {\n    checkColumnIndex(this, column);\n    checkNonEmpty(this);\n    let v = this.get(0, column);\n    let idx = [0, column];\n    for (let i = 1; i < this.rows; i++) {\n      if (this.get(i, column) > v) {\n        v = this.get(i, column);\n        idx[0] = i;\n      }\n    }\n    return idx;\n  }\n\n  minColumn(column) {\n    checkColumnIndex(this, column);\n    if (this.isEmpty()) {\n      return NaN;\n    }\n    let v = this.get(0, column);\n    for (let i = 1; i < this.rows; i++) {\n      if (this.get(i, column) < v) {\n        v = this.get(i, column);\n      }\n    }\n    return v;\n  }\n\n  minColumnIndex(column) {\n    checkColumnIndex(this, column);\n    checkNonEmpty(this);\n    let v = this.get(0, column);\n    let idx = [0, column];\n    for (let i = 1; i < this.rows; i++) {\n      if (this.get(i, column) < v) {\n        v = this.get(i, column);\n        idx[0] = i;\n      }\n    }\n    return idx;\n  }\n\n  diag() {\n    let min = Math.min(this.rows, this.columns);\n    let diag = [];\n    for (let i = 0; i < min; i++) {\n      diag.push(this.get(i, i));\n    }\n    return diag;\n  }\n\n  norm(type = 'frobenius') {\n    switch (type) {\n      case 'max':\n        return this.max();\n      case 'frobenius':\n        return Math.sqrt(this.dot(this));\n      default:\n        throw new RangeError(`unknown norm type: ${type}`);\n    }\n  }\n\n  cumulativeSum() {\n    let sum = 0;\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        sum += this.get(i, j);\n        this.set(i, j, sum);\n      }\n    }\n    return this;\n  }\n\n  dot(vector2) {\n    if (AbstractMatrix.isMatrix(vector2)) vector2 = vector2.to1DArray();\n    let vector1 = this.to1DArray();\n    if (vector1.length !== vector2.length) {\n      throw new RangeError('vectors do not have the same size');\n    }\n    let dot = 0;\n    for (let i = 0; i < vector1.length; i++) {\n      dot += vector1[i] * vector2[i];\n    }\n    return dot;\n  }\n\n  mmul(other) {\n    other = Matrix.checkMatrix(other);\n\n    let m = this.rows;\n    let n = this.columns;\n    let p = other.columns;\n\n    let result = new Matrix(m, p);\n\n    let Bcolj = new Float64Array(n);\n    for (let j = 0; j < p; j++) {\n      for (let k = 0; k < n; k++) {\n        Bcolj[k] = other.get(k, j);\n      }\n\n      for (let i = 0; i < m; i++) {\n        let s = 0;\n        for (let k = 0; k < n; k++) {\n          s += this.get(i, k) * Bcolj[k];\n        }\n\n        result.set(i, j, s);\n      }\n    }\n    return result;\n  }\n\n  mpow(scalar) {\n    if (!this.isSquare()) {\n      throw new RangeError('Matrix must be square');\n    }\n    if (!Number.isInteger(scalar) || scalar < 0) {\n      throw new RangeError('Exponent must be a non-negative integer');\n    }\n    // Russian Peasant exponentiation, i.e. exponentiation by squaring\n    let result = Matrix.eye(this.rows);\n    let bb = this;\n    // Note: Don't bit shift. In JS, that would truncate at 32 bits\n    for (let e = scalar; e >= 1; e /= 2) {\n      if ((e & 1) !== 0) {\n        result = result.mmul(bb);\n      }\n      bb = bb.mmul(bb);\n    }\n    return result;\n  }\n\n  strassen2x2(other) {\n    other = Matrix.checkMatrix(other);\n    let result = new Matrix(2, 2);\n    const a11 = this.get(0, 0);\n    const b11 = other.get(0, 0);\n    const a12 = this.get(0, 1);\n    const b12 = other.get(0, 1);\n    const a21 = this.get(1, 0);\n    const b21 = other.get(1, 0);\n    const a22 = this.get(1, 1);\n    const b22 = other.get(1, 1);\n\n    // Compute intermediate values.\n    const m1 = (a11 + a22) * (b11 + b22);\n    const m2 = (a21 + a22) * b11;\n    const m3 = a11 * (b12 - b22);\n    const m4 = a22 * (b21 - b11);\n    const m5 = (a11 + a12) * b22;\n    const m6 = (a21 - a11) * (b11 + b12);\n    const m7 = (a12 - a22) * (b21 + b22);\n\n    // Combine intermediate values into the output.\n    const c00 = m1 + m4 - m5 + m7;\n    const c01 = m3 + m5;\n    const c10 = m2 + m4;\n    const c11 = m1 - m2 + m3 + m6;\n\n    result.set(0, 0, c00);\n    result.set(0, 1, c01);\n    result.set(1, 0, c10);\n    result.set(1, 1, c11);\n    return result;\n  }\n\n  strassen3x3(other) {\n    other = Matrix.checkMatrix(other);\n    let result = new Matrix(3, 3);\n\n    const a00 = this.get(0, 0);\n    const a01 = this.get(0, 1);\n    const a02 = this.get(0, 2);\n    const a10 = this.get(1, 0);\n    const a11 = this.get(1, 1);\n    const a12 = this.get(1, 2);\n    const a20 = this.get(2, 0);\n    const a21 = this.get(2, 1);\n    const a22 = this.get(2, 2);\n\n    const b00 = other.get(0, 0);\n    const b01 = other.get(0, 1);\n    const b02 = other.get(0, 2);\n    const b10 = other.get(1, 0);\n    const b11 = other.get(1, 1);\n    const b12 = other.get(1, 2);\n    const b20 = other.get(2, 0);\n    const b21 = other.get(2, 1);\n    const b22 = other.get(2, 2);\n\n    const m1 = (a00 + a01 + a02 - a10 - a11 - a21 - a22) * b11;\n    const m2 = (a00 - a10) * (-b01 + b11);\n    const m3 = a11 * (-b00 + b01 + b10 - b11 - b12 - b20 + b22);\n    const m4 = (-a00 + a10 + a11) * (b00 - b01 + b11);\n    const m5 = (a10 + a11) * (-b00 + b01);\n    const m6 = a00 * b00;\n    const m7 = (-a00 + a20 + a21) * (b00 - b02 + b12);\n    const m8 = (-a00 + a20) * (b02 - b12);\n    const m9 = (a20 + a21) * (-b00 + b02);\n    const m10 = (a00 + a01 + a02 - a11 - a12 - a20 - a21) * b12;\n    const m11 = a21 * (-b00 + b02 + b10 - b11 - b12 - b20 + b21);\n    const m12 = (-a02 + a21 + a22) * (b11 + b20 - b21);\n    const m13 = (a02 - a22) * (b11 - b21);\n    const m14 = a02 * b20;\n    const m15 = (a21 + a22) * (-b20 + b21);\n    const m16 = (-a02 + a11 + a12) * (b12 + b20 - b22);\n    const m17 = (a02 - a12) * (b12 - b22);\n    const m18 = (a11 + a12) * (-b20 + b22);\n    const m19 = a01 * b10;\n    const m20 = a12 * b21;\n    const m21 = a10 * b02;\n    const m22 = a20 * b01;\n    const m23 = a22 * b22;\n\n    const c00 = m6 + m14 + m19;\n    const c01 = m1 + m4 + m5 + m6 + m12 + m14 + m15;\n    const c02 = m6 + m7 + m9 + m10 + m14 + m16 + m18;\n    const c10 = m2 + m3 + m4 + m6 + m14 + m16 + m17;\n    const c11 = m2 + m4 + m5 + m6 + m20;\n    const c12 = m14 + m16 + m17 + m18 + m21;\n    const c20 = m6 + m7 + m8 + m11 + m12 + m13 + m14;\n    const c21 = m12 + m13 + m14 + m15 + m22;\n    const c22 = m6 + m7 + m8 + m9 + m23;\n\n    result.set(0, 0, c00);\n    result.set(0, 1, c01);\n    result.set(0, 2, c02);\n    result.set(1, 0, c10);\n    result.set(1, 1, c11);\n    result.set(1, 2, c12);\n    result.set(2, 0, c20);\n    result.set(2, 1, c21);\n    result.set(2, 2, c22);\n    return result;\n  }\n\n  mmulStrassen(y) {\n    y = Matrix.checkMatrix(y);\n    let x = this.clone();\n    let r1 = x.rows;\n    let c1 = x.columns;\n    let r2 = y.rows;\n    let c2 = y.columns;\n    if (c1 !== r2) {\n      // eslint-disable-next-line no-console\n      console.warn(\n        `Multiplying ${r1} x ${c1} and ${r2} x ${c2} matrix: dimensions do not match.`,\n      );\n    }\n\n    // Put a matrix into the top left of a matrix of zeros.\n    // `rows` and `cols` are the dimensions of the output matrix.\n    function embed(mat, rows, cols) {\n      let r = mat.rows;\n      let c = mat.columns;\n      if (r === rows && c === cols) {\n        return mat;\n      } else {\n        let resultat = AbstractMatrix.zeros(rows, cols);\n        resultat = resultat.setSubMatrix(mat, 0, 0);\n        return resultat;\n      }\n    }\n\n    // Make sure both matrices are the same size.\n    // This is exclusively for simplicity:\n    // this algorithm can be implemented with matrices of different sizes.\n\n    let r = Math.max(r1, r2);\n    let c = Math.max(c1, c2);\n    x = embed(x, r, c);\n    y = embed(y, r, c);\n\n    // Our recursive multiplication function.\n    function blockMult(a, b, rows, cols) {\n      // For small matrices, resort to naive multiplication.\n      if (rows <= 512 || cols <= 512) {\n        return a.mmul(b); // a is equivalent to this\n      }\n\n      // Apply dynamic padding.\n      if (rows % 2 === 1 && cols % 2 === 1) {\n        a = embed(a, rows + 1, cols + 1);\n        b = embed(b, rows + 1, cols + 1);\n      } else if (rows % 2 === 1) {\n        a = embed(a, rows + 1, cols);\n        b = embed(b, rows + 1, cols);\n      } else if (cols % 2 === 1) {\n        a = embed(a, rows, cols + 1);\n        b = embed(b, rows, cols + 1);\n      }\n\n      let halfRows = parseInt(a.rows / 2, 10);\n      let halfCols = parseInt(a.columns / 2, 10);\n      // Subdivide input matrices.\n      let a11 = a.subMatrix(0, halfRows - 1, 0, halfCols - 1);\n      let b11 = b.subMatrix(0, halfRows - 1, 0, halfCols - 1);\n\n      let a12 = a.subMatrix(0, halfRows - 1, halfCols, a.columns - 1);\n      let b12 = b.subMatrix(0, halfRows - 1, halfCols, b.columns - 1);\n\n      let a21 = a.subMatrix(halfRows, a.rows - 1, 0, halfCols - 1);\n      let b21 = b.subMatrix(halfRows, b.rows - 1, 0, halfCols - 1);\n\n      let a22 = a.subMatrix(halfRows, a.rows - 1, halfCols, a.columns - 1);\n      let b22 = b.subMatrix(halfRows, b.rows - 1, halfCols, b.columns - 1);\n\n      // Compute intermediate values.\n      let m1 = blockMult(\n        AbstractMatrix.add(a11, a22),\n        AbstractMatrix.add(b11, b22),\n        halfRows,\n        halfCols,\n      );\n      let m2 = blockMult(AbstractMatrix.add(a21, a22), b11, halfRows, halfCols);\n      let m3 = blockMult(a11, AbstractMatrix.sub(b12, b22), halfRows, halfCols);\n      let m4 = blockMult(a22, AbstractMatrix.sub(b21, b11), halfRows, halfCols);\n      let m5 = blockMult(AbstractMatrix.add(a11, a12), b22, halfRows, halfCols);\n      let m6 = blockMult(\n        AbstractMatrix.sub(a21, a11),\n        AbstractMatrix.add(b11, b12),\n        halfRows,\n        halfCols,\n      );\n      let m7 = blockMult(\n        AbstractMatrix.sub(a12, a22),\n        AbstractMatrix.add(b21, b22),\n        halfRows,\n        halfCols,\n      );\n\n      // Combine intermediate values into the output.\n      let c11 = AbstractMatrix.add(m1, m4);\n      c11.sub(m5);\n      c11.add(m7);\n      let c12 = AbstractMatrix.add(m3, m5);\n      let c21 = AbstractMatrix.add(m2, m4);\n      let c22 = AbstractMatrix.sub(m1, m2);\n      c22.add(m3);\n      c22.add(m6);\n\n      // Crop output to the desired size (undo dynamic padding).\n      let result = AbstractMatrix.zeros(2 * c11.rows, 2 * c11.columns);\n      result = result.setSubMatrix(c11, 0, 0);\n      result = result.setSubMatrix(c12, c11.rows, 0);\n      result = result.setSubMatrix(c21, 0, c11.columns);\n      result = result.setSubMatrix(c22, c11.rows, c11.columns);\n      return result.subMatrix(0, rows - 1, 0, cols - 1);\n    }\n\n    return blockMult(x, y, r, c);\n  }\n\n  scaleRows(options = {}) {\n    if (typeof options !== 'object') {\n      throw new TypeError('options must be an object');\n    }\n    const { min = 0, max = 1 } = options;\n    if (!Number.isFinite(min)) throw new TypeError('min must be a number');\n    if (!Number.isFinite(max)) throw new TypeError('max must be a number');\n    if (min >= max) throw new RangeError('min must be smaller than max');\n    let newMatrix = new Matrix(this.rows, this.columns);\n    for (let i = 0; i < this.rows; i++) {\n      const row = this.getRow(i);\n      if (row.length > 0) {\n        rescale(row, { min, max, output: row });\n      }\n      newMatrix.setRow(i, row);\n    }\n    return newMatrix;\n  }\n\n  scaleColumns(options = {}) {\n    if (typeof options !== 'object') {\n      throw new TypeError('options must be an object');\n    }\n    const { min = 0, max = 1 } = options;\n    if (!Number.isFinite(min)) throw new TypeError('min must be a number');\n    if (!Number.isFinite(max)) throw new TypeError('max must be a number');\n    if (min >= max) throw new RangeError('min must be smaller than max');\n    let newMatrix = new Matrix(this.rows, this.columns);\n    for (let i = 0; i < this.columns; i++) {\n      const column = this.getColumn(i);\n      if (column.length) {\n        rescale(column, {\n          min,\n          max,\n          output: column,\n        });\n      }\n      newMatrix.setColumn(i, column);\n    }\n    return newMatrix;\n  }\n\n  flipRows() {\n    const middle = Math.ceil(this.columns / 2);\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < middle; j++) {\n        let first = this.get(i, j);\n        let last = this.get(i, this.columns - 1 - j);\n        this.set(i, j, last);\n        this.set(i, this.columns - 1 - j, first);\n      }\n    }\n    return this;\n  }\n\n  flipColumns() {\n    const middle = Math.ceil(this.rows / 2);\n    for (let j = 0; j < this.columns; j++) {\n      for (let i = 0; i < middle; i++) {\n        let first = this.get(i, j);\n        let last = this.get(this.rows - 1 - i, j);\n        this.set(i, j, last);\n        this.set(this.rows - 1 - i, j, first);\n      }\n    }\n    return this;\n  }\n\n  kroneckerProduct(other) {\n    other = Matrix.checkMatrix(other);\n\n    let m = this.rows;\n    let n = this.columns;\n    let p = other.rows;\n    let q = other.columns;\n\n    let result = new Matrix(m * p, n * q);\n    for (let i = 0; i < m; i++) {\n      for (let j = 0; j < n; j++) {\n        for (let k = 0; k < p; k++) {\n          for (let l = 0; l < q; l++) {\n            result.set(p * i + k, q * j + l, this.get(i, j) * other.get(k, l));\n          }\n        }\n      }\n    }\n    return result;\n  }\n\n  kroneckerSum(other) {\n    other = Matrix.checkMatrix(other);\n    if (!this.isSquare() || !other.isSquare()) {\n      throw new Error('Kronecker Sum needs two Square Matrices');\n    }\n    let m = this.rows;\n    let n = other.rows;\n    let AxI = this.kroneckerProduct(Matrix.eye(n, n));\n    let IxB = Matrix.eye(m, m).kroneckerProduct(other);\n    return AxI.add(IxB);\n  }\n\n  transpose() {\n    let result = new Matrix(this.columns, this.rows);\n    for (let i = 0; i < this.rows; i++) {\n      for (let j = 0; j < this.columns; j++) {\n        result.set(j, i, this.get(i, j));\n      }\n    }\n    return result;\n  }\n\n  sortRows(compareFunction = compareNumbers) {\n    for (let i = 0; i < this.rows; i++) {\n      this.setRow(i, this.getRow(i).sort(compareFunction));\n    }\n    return this;\n  }\n\n  sortColumns(compareFunction = compareNumbers) {\n    for (let i = 0; i < this.columns; i++) {\n      this.setColumn(i, this.getColumn(i).sort(compareFunction));\n    }\n    return this;\n  }\n\n  subMatrix(startRow, endRow, startColumn, endColumn) {\n    checkRange(this, startRow, endRow, startColumn, endColumn);\n    let newMatrix = new Matrix(\n      endRow - startRow + 1,\n      endColumn - startColumn + 1,\n    );\n    for (let i = startRow; i <= endRow; i++) {\n      for (let j = startColumn; j <= endColumn; j++) {\n        newMatrix.set(i - startRow, j - startColumn, this.get(i, j));\n      }\n    }\n    return newMatrix;\n  }\n\n  subMatrixRow(indices, startColumn, endColumn) {\n    if (startColumn === undefined) startColumn = 0;\n    if (endColumn === undefined) endColumn = this.columns - 1;\n    if (\n      startColumn > endColumn ||\n      startColumn < 0 ||\n      startColumn >= this.columns ||\n      endColumn < 0 ||\n      endColumn >= this.columns\n    ) {\n      throw new RangeError('Argument out of range');\n    }\n\n    let newMatrix = new Matrix(indices.length, endColumn - startColumn + 1);\n    for (let i = 0; i < indices.length; i++) {\n      for (let j = startColumn; j <= endColumn; j++) {\n        if (indices[i] < 0 || indices[i] >= this.rows) {\n          throw new RangeError(`Row index out of range: ${indices[i]}`);\n        }\n        newMatrix.set(i, j - startColumn, this.get(indices[i], j));\n      }\n    }\n    return newMatrix;\n  }\n\n  subMatrixColumn(indices, startRow, endRow) {\n    if (startRow === undefined) startRow = 0;\n    if (endRow === undefined) endRow = this.rows - 1;\n    if (\n      startRow > endRow ||\n      startRow < 0 ||\n      startRow >= this.rows ||\n      endRow < 0 ||\n      endRow >= this.rows\n    ) {\n      throw new RangeError('Argument out of range');\n    }\n\n    let newMatrix = new Matrix(endRow - startRow + 1, indices.length);\n    for (let i = 0; i < indices.length; i++) {\n      for (let j = startRow; j <= endRow; j++) {\n        if (indices[i] < 0 || indices[i] >= this.columns) {\n          throw new RangeError(`Column index out of range: ${indices[i]}`);\n        }\n        newMatrix.set(j - startRow, i, this.get(j, indices[i]));\n      }\n    }\n    return newMatrix;\n  }\n\n  setSubMatrix(matrix, startRow, startColumn) {\n    matrix = Matrix.checkMatrix(matrix);\n    if (matrix.isEmpty()) {\n      return this;\n    }\n    let endRow = startRow + matrix.rows - 1;\n    let endColumn = startColumn + matrix.columns - 1;\n    checkRange(this, startRow, endRow, startColumn, endColumn);\n    for (let i = 0; i < matrix.rows; i++) {\n      for (let j = 0; j < matrix.columns; j++) {\n        this.set(startRow + i, startColumn + j, matrix.get(i, j));\n      }\n    }\n    return this;\n  }\n\n  selection(rowIndices, columnIndices) {\n    checkRowIndices(this, rowIndices);\n    checkColumnIndices(this, columnIndices);\n    let newMatrix = new Matrix(rowIndices.length, columnIndices.length);\n    for (let i = 0; i < rowIndices.length; i++) {\n      let rowIndex = rowIndices[i];\n      for (let j = 0; j < columnIndices.length; j++) {\n        let columnIndex = columnIndices[j];\n        newMatrix.set(i, j, this.get(rowIndex, columnIndex));\n      }\n    }\n    return newMatrix;\n  }\n\n  trace() {\n    let min = Math.min(this.rows, this.columns);\n    let trace = 0;\n    for (let i = 0; i < min; i++) {\n      trace += this.get(i, i);\n    }\n    return trace;\n  }\n\n  clone() {\n    return this.constructor.copy(this, new Matrix(this.rows, this.columns));\n  }\n\n  /**\n   * @template {AbstractMatrix} M\n   * @param {AbstractMatrix} from\n   * @param {M} to\n   * @return {M}\n   */\n  static copy(from, to) {\n    for (const [row, column, value] of from.entries()) {\n      to.set(row, column, value);\n    }\n\n    return to;\n  }\n\n  sum(by) {\n    switch (by) {\n      case 'row':\n        return sumByRow(this);\n      case 'column':\n        return sumByColumn(this);\n      case undefined:\n        return sumAll(this);\n      default:\n        throw new Error(`invalid option: ${by}`);\n    }\n  }\n\n  product(by) {\n    switch (by) {\n      case 'row':\n        return productByRow(this);\n      case 'column':\n        return productByColumn(this);\n      case undefined:\n        return productAll(this);\n      default:\n        throw new Error(`invalid option: ${by}`);\n    }\n  }\n\n  mean(by) {\n    const sum = this.sum(by);\n    switch (by) {\n      case 'row': {\n        for (let i = 0; i < this.rows; i++) {\n          sum[i] /= this.columns;\n        }\n        return sum;\n      }\n      case 'column': {\n        for (let i = 0; i < this.columns; i++) {\n          sum[i] /= this.rows;\n        }\n        return sum;\n      }\n      case undefined:\n        return sum / this.size;\n      default:\n        throw new Error(`invalid option: ${by}`);\n    }\n  }\n\n  variance(by, options = {}) {\n    if (typeof by === 'object') {\n      options = by;\n      by = undefined;\n    }\n    if (typeof options !== 'object') {\n      throw new TypeError('options must be an object');\n    }\n    const { unbiased = true, mean = this.mean(by) } = options;\n    if (typeof unbiased !== 'boolean') {\n      throw new TypeError('unbiased must be a boolean');\n    }\n    switch (by) {\n      case 'row': {\n        if (!isAnyArray(mean)) {\n          throw new TypeError('mean must be an array');\n        }\n        return varianceByRow(this, unbiased, mean);\n      }\n      case 'column': {\n        if (!isAnyArray(mean)) {\n          throw new TypeError('mean must be an array');\n        }\n        return varianceByColumn(this, unbiased, mean);\n      }\n      case undefined: {\n        if (typeof mean !== 'number') {\n          throw new TypeError('mean must be a number');\n        }\n        return varianceAll(this, unbiased, mean);\n      }\n      default:\n        throw new Error(`invalid option: ${by}`);\n    }\n  }\n\n  standardDeviation(by, options) {\n    if (typeof by === 'object') {\n      options = by;\n      by = undefined;\n    }\n    const variance = this.variance(by, options);\n    if (by === undefined) {\n      return Math.sqrt(variance);\n    } else {\n      for (let i = 0; i < variance.length; i++) {\n        variance[i] = Math.sqrt(variance[i]);\n      }\n      return variance;\n    }\n  }\n\n  center(by, options = {}) {\n    if (typeof by === 'object') {\n      options = by;\n      by = undefined;\n    }\n    if (typeof options !== 'object') {\n      throw new TypeError('options must be an object');\n    }\n    const { center = this.mean(by) } = options;\n    switch (by) {\n      case 'row': {\n        if (!isAnyArray(center)) {\n          throw new TypeError('center must be an array');\n        }\n        centerByRow(this, center);\n        return this;\n      }\n      case 'column': {\n        if (!isAnyArray(center)) {\n          throw new TypeError('center must be an array');\n        }\n        centerByColumn(this, center);\n        return this;\n      }\n      case undefined: {\n        if (typeof center !== 'number') {\n          throw new TypeError('center must be a number');\n        }\n        centerAll(this, center);\n        return this;\n      }\n      default:\n        throw new Error(`invalid option: ${by}`);\n    }\n  }\n\n  scale(by, options = {}) {\n    if (typeof by === 'object') {\n      options = by;\n      by = undefined;\n    }\n    if (typeof options !== 'object') {\n      throw new TypeError('options must be an object');\n    }\n    let scale = options.scale;\n    switch (by) {\n      case 'row': {\n        if (scale === undefined) {\n          scale = getScaleByRow(this);\n        } else if (!isAnyArray(scale)) {\n          throw new TypeError('scale must be an array');\n        }\n        scaleByRow(this, scale);\n        return this;\n      }\n      case 'column': {\n        if (scale === undefined) {\n          scale = getScaleByColumn(this);\n        } else if (!isAnyArray(scale)) {\n          throw new TypeError('scale must be an array');\n        }\n        scaleByColumn(this, scale);\n        return this;\n      }\n      case undefined: {\n        if (scale === undefined) {\n          scale = getScaleAll(this);\n        } else if (typeof scale !== 'number') {\n          throw new TypeError('scale must be a number');\n        }\n        scaleAll(this, scale);\n        return this;\n      }\n      default:\n        throw new Error(`invalid option: ${by}`);\n    }\n  }\n\n  toString(options) {\n    return inspectMatrixWithOptions(this, options);\n  }\n\n  [Symbol.iterator]() {\n    return this.entries();\n  }\n\n  /**\n   * iterator from left to right, from top to bottom\n   * yield [row, column, value]\n   * @returns {Generator<[number, number, number], void, void>}\n   */\n  *entries() {\n    for (let row = 0; row < this.rows; row++) {\n      for (let col = 0; col < this.columns; col++) {\n        yield [row, col, this.get(row, col)];\n      }\n    }\n  }\n\n  /**\n   * iterator from left to right, from top to bottom\n   * yield value\n   * @returns {Generator<number, void, void>}\n   */\n  *values() {\n    for (let row = 0; row < this.rows; row++) {\n      for (let col = 0; col < this.columns; col++) {\n        yield this.get(row, col);\n      }\n    }\n  }\n}\n\nAbstractMatrix.prototype.klass = 'Matrix';\nif (typeof Symbol !== 'undefined') {\n  AbstractMatrix.prototype[Symbol.for('nodejs.util.inspect.custom')] =\n    inspectMatrix;\n}\n\nfunction compareNumbers(a, b) {\n  return a - b;\n}\n\nfunction isArrayOfNumbers(array) {\n  return array.every((element) => {\n    return typeof element === 'number';\n  });\n}\n\n// Synonyms\nAbstractMatrix.random = AbstractMatrix.rand;\nAbstractMatrix.randomInt = AbstractMatrix.randInt;\nAbstractMatrix.diagonal = AbstractMatrix.diag;\nAbstractMatrix.prototype.diagonal = AbstractMatrix.prototype.diag;\nAbstractMatrix.identity = AbstractMatrix.eye;\nAbstractMatrix.prototype.negate = AbstractMatrix.prototype.neg;\nAbstractMatrix.prototype.tensorProduct =\n  AbstractMatrix.prototype.kroneckerProduct;\n\nclass Matrix extends AbstractMatrix {\n  /**\n   * @type {Float64Array[]}\n   */\n  data;\n\n  /**\n   * Init an empty matrix\n   * @param {number} nRows\n   * @param {number} nColumns\n   */\n  #initData(nRows, nColumns) {\n    this.data = [];\n\n    if (Number.isInteger(nColumns) && nColumns >= 0) {\n      for (let i = 0; i < nRows; i++) {\n        this.data.push(new Float64Array(nColumns));\n      }\n    } else {\n      throw new TypeError('nColumns must be a positive integer');\n    }\n\n    this.rows = nRows;\n    this.columns = nColumns;\n  }\n\n  constructor(nRows, nColumns) {\n    super();\n    if (Matrix.isMatrix(nRows)) {\n      this.#initData(nRows.rows, nRows.columns);\n      Matrix.copy(nRows, this);\n    } else if (Number.isInteger(nRows) && nRows >= 0) {\n      this.#initData(nRows, nColumns);\n    } else if (isAnyArray(nRows)) {\n      // Copy the values from the 2D array\n      const arrayData = nRows;\n      nRows = arrayData.length;\n      nColumns = nRows ? arrayData[0].length : 0;\n      if (typeof nColumns !== 'number') {\n        throw new TypeError(\n          'Data must be a 2D array with at least one element',\n        );\n      }\n      this.data = [];\n\n      for (let i = 0; i < nRows; i++) {\n        if (arrayData[i].length !== nColumns) {\n          throw new RangeError('Inconsistent array dimensions');\n        }\n        if (!isArrayOfNumbers(arrayData[i])) {\n          throw new TypeError('Input data contains non-numeric values');\n        }\n        this.data.push(Float64Array.from(arrayData[i]));\n      }\n\n      this.rows = nRows;\n      this.columns = nColumns;\n    } else {\n      throw new TypeError(\n        'First argument must be a positive number or an array',\n      );\n    }\n  }\n\n  set(rowIndex, columnIndex, value) {\n    this.data[rowIndex][columnIndex] = value;\n    return this;\n  }\n\n  get(rowIndex, columnIndex) {\n    return this.data[rowIndex][columnIndex];\n  }\n\n  removeRow(index) {\n    checkRowIndex(this, index);\n    this.data.splice(index, 1);\n    this.rows -= 1;\n    return this;\n  }\n\n  addRow(index, array) {\n    if (array === undefined) {\n      array = index;\n      index = this.rows;\n    }\n    checkRowIndex(this, index, true);\n    array = Float64Array.from(checkRowVector(this, array));\n    this.data.splice(index, 0, array);\n    this.rows += 1;\n    return this;\n  }\n\n  removeColumn(index) {\n    checkColumnIndex(this, index);\n    for (let i = 0; i < this.rows; i++) {\n      const newRow = new Float64Array(this.columns - 1);\n      for (let j = 0; j < index; j++) {\n        newRow[j] = this.data[i][j];\n      }\n      for (let j = index + 1; j < this.columns; j++) {\n        newRow[j - 1] = this.data[i][j];\n      }\n      this.data[i] = newRow;\n    }\n    this.columns -= 1;\n    return this;\n  }\n\n  addColumn(index, array) {\n    if (typeof array === 'undefined') {\n      array = index;\n      index = this.columns;\n    }\n    checkColumnIndex(this, index, true);\n    array = checkColumnVector(this, array);\n    for (let i = 0; i < this.rows; i++) {\n      const newRow = new Float64Array(this.columns + 1);\n      let j = 0;\n      for (; j < index; j++) {\n        newRow[j] = this.data[i][j];\n      }\n      newRow[j++] = array[i];\n      for (; j < this.columns + 1; j++) {\n        newRow[j] = this.data[i][j - 1];\n      }\n      this.data[i] = newRow;\n    }\n    this.columns += 1;\n    return this;\n  }\n}\n\ninstallMathOperations(AbstractMatrix, Matrix);\n\n/**\n * @typedef {0 | 1 | number | boolean} Mask\n */\n\nclass SymmetricMatrix extends AbstractMatrix {\n  /** @type {Matrix} */\n  #matrix;\n\n  get size() {\n    return this.#matrix.size;\n  }\n\n  get rows() {\n    return this.#matrix.rows;\n  }\n\n  get columns() {\n    return this.#matrix.columns;\n  }\n\n  get diagonalSize() {\n    return this.rows;\n  }\n\n  /**\n   * not the same as matrix.isSymmetric()\n   * Here is to check if it's instanceof SymmetricMatrix without bundling issues\n   *\n   * @param value\n   * @returns {boolean}\n   */\n  static isSymmetricMatrix(value) {\n    return Matrix.isMatrix(value) && value.klassType === 'SymmetricMatrix';\n  }\n\n  /**\n   * @param diagonalSize\n   * @return {SymmetricMatrix}\n   */\n  static zeros(diagonalSize) {\n    return new this(diagonalSize);\n  }\n\n  /**\n   * @param diagonalSize\n   * @return {SymmetricMatrix}\n   */\n  static ones(diagonalSize) {\n    return new this(diagonalSize).fill(1);\n  }\n\n  /**\n   * @param {number | AbstractMatrix | ArrayLike<ArrayLike<number>>} diagonalSize\n   * @return {this}\n   */\n  constructor(diagonalSize) {\n    super();\n\n    if (Matrix.isMatrix(diagonalSize)) {\n      if (!diagonalSize.isSymmetric()) {\n        throw new TypeError('not symmetric data');\n      }\n\n      this.#matrix = Matrix.copy(\n        diagonalSize,\n        new Matrix(diagonalSize.rows, diagonalSize.rows),\n      );\n    } else if (Number.isInteger(diagonalSize) && diagonalSize >= 0) {\n      this.#matrix = new Matrix(diagonalSize, diagonalSize);\n    } else {\n      this.#matrix = new Matrix(diagonalSize);\n\n      if (!this.isSymmetric()) {\n        throw new TypeError('not symmetric data');\n      }\n    }\n  }\n\n  clone() {\n    const matrix = new SymmetricMatrix(this.diagonalSize);\n\n    for (const [row, col, value] of this.upperRightEntries()) {\n      matrix.set(row, col, value);\n    }\n\n    return matrix;\n  }\n\n  toMatrix() {\n    return new Matrix(this);\n  }\n\n  get(rowIndex, columnIndex) {\n    return this.#matrix.get(rowIndex, columnIndex);\n  }\n  set(rowIndex, columnIndex, value) {\n    // symmetric set\n    this.#matrix.set(rowIndex, columnIndex, value);\n    this.#matrix.set(columnIndex, rowIndex, value);\n\n    return this;\n  }\n\n  removeCross(index) {\n    // symmetric remove side\n    this.#matrix.removeRow(index);\n    this.#matrix.removeColumn(index);\n\n    return this;\n  }\n\n  addCross(index, array) {\n    if (array === undefined) {\n      array = index;\n      index = this.diagonalSize;\n    }\n\n    const row = array.slice();\n    row.splice(index, 1);\n\n    this.#matrix.addRow(index, row);\n    this.#matrix.addColumn(index, array);\n\n    return this;\n  }\n\n  /**\n   * @param {Mask[]} mask\n   */\n  applyMask(mask) {\n    if (mask.length !== this.diagonalSize) {\n      throw new RangeError('Mask size do not match with matrix size');\n    }\n\n    // prepare sides to remove from matrix from mask\n    /** @type {number[]} */\n    const sidesToRemove = [];\n    for (const [index, passthroughs] of mask.entries()) {\n      if (passthroughs) continue;\n      sidesToRemove.push(index);\n    }\n    // to remove from highest to lowest for no mutation shifting\n    sidesToRemove.reverse();\n\n    // remove sides\n    for (const sideIndex of sidesToRemove) {\n      this.removeCross(sideIndex);\n    }\n\n    return this;\n  }\n\n  /**\n   * Compact format upper-right corner of matrix\n   * iterate from left to right, from top to bottom.\n   *\n   * ```\n   *   A B C D\n   * A 1 2 3 4\n   * B 2 5 6 7\n   * C 3 6 8 9\n   * D 4 7 9 10\n   * ```\n   *\n   * will return compact 1D array `[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]`\n   *\n   * length is S(i=0, n=sideSize) => 10 for a 4 sideSized matrix\n   *\n   * @returns {number[]}\n   */\n  toCompact() {\n    const { diagonalSize } = this;\n\n    /** @type {number[]} */\n    const compact = new Array((diagonalSize * (diagonalSize + 1)) / 2);\n    for (let col = 0, row = 0, index = 0; index < compact.length; index++) {\n      compact[index] = this.get(row, col);\n\n      if (++col >= diagonalSize) col = ++row;\n    }\n\n    return compact;\n  }\n\n  /**\n   * @param {number[]} compact\n   * @return {SymmetricMatrix}\n   */\n  static fromCompact(compact) {\n    const compactSize = compact.length;\n    // compactSize = (sideSize * (sideSize + 1)) / 2\n    // https://mathsolver.microsoft.com/fr/solve-problem/y%20%3D%20%20x%20%60cdot%20%20%20%60frac%7B%20%20%60left(%20x%2B1%20%20%60right)%20%20%20%20%7D%7B%202%20%20%7D\n    // sideSize = (Sqrt(8 × compactSize + 1) - 1) / 2\n    const diagonalSize = (Math.sqrt(8 * compactSize + 1) - 1) / 2;\n\n    if (!Number.isInteger(diagonalSize)) {\n      throw new TypeError(\n        `This array is not a compact representation of a Symmetric Matrix, ${JSON.stringify(\n          compact,\n        )}`,\n      );\n    }\n\n    const matrix = new SymmetricMatrix(diagonalSize);\n    for (let col = 0, row = 0, index = 0; index < compactSize; index++) {\n      matrix.set(col, row, compact[index]);\n      if (++col >= diagonalSize) col = ++row;\n    }\n\n    return matrix;\n  }\n\n  /**\n   * half iterator upper-right-corner from left to right, from top to bottom\n   * yield [row, column, value]\n   *\n   * @returns {Generator<[number, number, number], void, void>}\n   */\n  *upperRightEntries() {\n    for (let row = 0, col = 0; row < this.diagonalSize; void 0) {\n      const value = this.get(row, col);\n\n      yield [row, col, value];\n\n      // at the end of row, move cursor to next row at diagonal position\n      if (++col >= this.diagonalSize) col = ++row;\n    }\n  }\n\n  /**\n   * half iterator upper-right-corner from left to right, from top to bottom\n   * yield value\n   *\n   * @returns {Generator<[number, number, number], void, void>}\n   */\n  *upperRightValues() {\n    for (let row = 0, col = 0; row < this.diagonalSize; void 0) {\n      const value = this.get(row, col);\n\n      yield value;\n\n      // at the end of row, move cursor to next row at diagonal position\n      if (++col >= this.diagonalSize) col = ++row;\n    }\n  }\n}\nSymmetricMatrix.prototype.klassType = 'SymmetricMatrix';\n\nclass DistanceMatrix extends SymmetricMatrix {\n  /**\n   * not the same as matrix.isSymmetric()\n   * Here is to check if it's instanceof SymmetricMatrix without bundling issues\n   *\n   * @param value\n   * @returns {boolean}\n   */\n  static isDistanceMatrix(value) {\n    return (\n      SymmetricMatrix.isSymmetricMatrix(value) &&\n      value.klassSubType === 'DistanceMatrix'\n    );\n  }\n\n  constructor(sideSize) {\n    super(sideSize);\n\n    if (!this.isDistance()) {\n      throw new TypeError('Provided arguments do no produce a distance matrix');\n    }\n  }\n\n  set(rowIndex, columnIndex, value) {\n    // distance matrix diagonal is 0\n    if (rowIndex === columnIndex) value = 0;\n\n    return super.set(rowIndex, columnIndex, value);\n  }\n\n  addCross(index, array) {\n    if (array === undefined) {\n      array = index;\n      index = this.diagonalSize;\n    }\n\n    // ensure distance\n    array = array.slice();\n    array[index] = 0;\n\n    return super.addCross(index, array);\n  }\n\n  toSymmetricMatrix() {\n    return new SymmetricMatrix(this);\n  }\n\n  clone() {\n    const matrix = new DistanceMatrix(this.diagonalSize);\n\n    for (const [row, col, value] of this.upperRightEntries()) {\n      if (row === col) continue;\n      matrix.set(row, col, value);\n    }\n\n    return matrix;\n  }\n\n  /**\n   * Compact format upper-right corner of matrix\n   * no diagonal (only zeros)\n   * iterable from left to right, from top to bottom.\n   *\n   * ```\n   *   A B C D\n   * A 0 1 2 3\n   * B 1 0 4 5\n   * C 2 4 0 6\n   * D 3 5 6 0\n   * ```\n   *\n   * will return compact 1D array `[1, 2, 3, 4, 5, 6]`\n   *\n   * length is S(i=0, n=sideSize-1) => 6 for a 4 side sized matrix\n   *\n   * @returns {number[]}\n   */\n  toCompact() {\n    const { diagonalSize } = this;\n    const compactLength = ((diagonalSize - 1) * diagonalSize) / 2;\n\n    /** @type {number[]} */\n    const compact = new Array(compactLength);\n    for (let col = 1, row = 0, index = 0; index < compact.length; index++) {\n      compact[index] = this.get(row, col);\n\n      if (++col >= diagonalSize) col = ++row + 1;\n    }\n\n    return compact;\n  }\n\n  /**\n   * @param {number[]} compact\n   */\n  static fromCompact(compact) {\n    const compactSize = compact.length;\n\n    if (compactSize === 0) {\n      return new this(0);\n    }\n\n    // compactSize in Natural integer range ]0;∞]\n    // compactSize = (sideSize * (sideSize - 1)) / 2\n    // sideSize = (Sqrt(8 × compactSize + 1) + 1) / 2\n    const diagonalSize = (Math.sqrt(8 * compactSize + 1) + 1) / 2;\n\n    if (!Number.isInteger(diagonalSize)) {\n      throw new TypeError(\n        `This array is not a compact representation of a DistanceMatrix, ${JSON.stringify(\n          compact,\n        )}`,\n      );\n    }\n\n    const matrix = new this(diagonalSize);\n    for (let col = 1, row = 0, index = 0; index < compactSize; index++) {\n      matrix.set(col, row, compact[index]);\n      if (++col >= diagonalSize) col = ++row + 1;\n    }\n\n    return matrix;\n  }\n}\nDistanceMatrix.prototype.klassSubType = 'DistanceMatrix';\n\nclass BaseView extends AbstractMatrix {\n  constructor(matrix, rows, columns) {\n    super();\n    this.matrix = matrix;\n    this.rows = rows;\n    this.columns = columns;\n  }\n}\n\nclass MatrixColumnView extends BaseView {\n  constructor(matrix, column) {\n    checkColumnIndex(matrix, column);\n    super(matrix, matrix.rows, 1);\n    this.column = column;\n  }\n\n  set(rowIndex, columnIndex, value) {\n    this.matrix.set(rowIndex, this.column, value);\n    return this;\n  }\n\n  get(rowIndex) {\n    return this.matrix.get(rowIndex, this.column);\n  }\n}\n\nclass MatrixColumnSelectionView extends BaseView {\n  constructor(matrix, columnIndices) {\n    checkColumnIndices(matrix, columnIndices);\n    super(matrix, matrix.rows, columnIndices.length);\n    this.columnIndices = columnIndices;\n  }\n\n  set(rowIndex, columnIndex, value) {\n    this.matrix.set(rowIndex, this.columnIndices[columnIndex], value);\n    return this;\n  }\n\n  get(rowIndex, columnIndex) {\n    return this.matrix.get(rowIndex, this.columnIndices[columnIndex]);\n  }\n}\n\nclass MatrixFlipColumnView extends BaseView {\n  constructor(matrix) {\n    super(matrix, matrix.rows, matrix.columns);\n  }\n\n  set(rowIndex, columnIndex, value) {\n    this.matrix.set(rowIndex, this.columns - columnIndex - 1, value);\n    return this;\n  }\n\n  get(rowIndex, columnIndex) {\n    return this.matrix.get(rowIndex, this.columns - columnIndex - 1);\n  }\n}\n\nclass MatrixFlipRowView extends BaseView {\n  constructor(matrix) {\n    super(matrix, matrix.rows, matrix.columns);\n  }\n\n  set(rowIndex, columnIndex, value) {\n    this.matrix.set(this.rows - rowIndex - 1, columnIndex, value);\n    return this;\n  }\n\n  get(rowIndex, columnIndex) {\n    return this.matrix.get(this.rows - rowIndex - 1, columnIndex);\n  }\n}\n\nclass MatrixRowView extends BaseView {\n  constructor(matrix, row) {\n    checkRowIndex(matrix, row);\n    super(matrix, 1, matrix.columns);\n    this.row = row;\n  }\n\n  set(rowIndex, columnIndex, value) {\n    this.matrix.set(this.row, columnIndex, value);\n    return this;\n  }\n\n  get(rowIndex, columnIndex) {\n    return this.matrix.get(this.row, columnIndex);\n  }\n}\n\nclass MatrixRowSelectionView extends BaseView {\n  constructor(matrix, rowIndices) {\n    checkRowIndices(matrix, rowIndices);\n    super(matrix, rowIndices.length, matrix.columns);\n    this.rowIndices = rowIndices;\n  }\n\n  set(rowIndex, columnIndex, value) {\n    this.matrix.set(this.rowIndices[rowIndex], columnIndex, value);\n    return this;\n  }\n\n  get(rowIndex, columnIndex) {\n    return this.matrix.get(this.rowIndices[rowIndex], columnIndex);\n  }\n}\n\nclass MatrixSelectionView extends BaseView {\n  constructor(matrix, rowIndices, columnIndices) {\n    checkRowIndices(matrix, rowIndices);\n    checkColumnIndices(matrix, columnIndices);\n    super(matrix, rowIndices.length, columnIndices.length);\n    this.rowIndices = rowIndices;\n    this.columnIndices = columnIndices;\n  }\n\n  set(rowIndex, columnIndex, value) {\n    this.matrix.set(\n      this.rowIndices[rowIndex],\n      this.columnIndices[columnIndex],\n      value,\n    );\n    return this;\n  }\n\n  get(rowIndex, columnIndex) {\n    return this.matrix.get(\n      this.rowIndices[rowIndex],\n      this.columnIndices[columnIndex],\n    );\n  }\n}\n\nclass MatrixSubView extends BaseView {\n  constructor(matrix, startRow, endRow, startColumn, endColumn) {\n    checkRange(matrix, startRow, endRow, startColumn, endColumn);\n    super(matrix, endRow - startRow + 1, endColumn - startColumn + 1);\n    this.startRow = startRow;\n    this.startColumn = startColumn;\n  }\n\n  set(rowIndex, columnIndex, value) {\n    this.matrix.set(\n      this.startRow + rowIndex,\n      this.startColumn + columnIndex,\n      value,\n    );\n    return this;\n  }\n\n  get(rowIndex, columnIndex) {\n    return this.matrix.get(\n      this.startRow + rowIndex,\n      this.startColumn + columnIndex,\n    );\n  }\n}\n\nclass MatrixTransposeView extends BaseView {\n  constructor(matrix) {\n    super(matrix, matrix.columns, matrix.rows);\n  }\n\n  set(rowIndex, columnIndex, value) {\n    this.matrix.set(columnIndex, rowIndex, value);\n    return this;\n  }\n\n  get(rowIndex, columnIndex) {\n    return this.matrix.get(columnIndex, rowIndex);\n  }\n}\n\nclass WrapperMatrix1D extends AbstractMatrix {\n  constructor(data, options = {}) {\n    const { rows = 1 } = options;\n\n    if (data.length % rows !== 0) {\n      throw new Error('the data length is not divisible by the number of rows');\n    }\n    super();\n    this.rows = rows;\n    this.columns = data.length / rows;\n    this.data = data;\n  }\n\n  set(rowIndex, columnIndex, value) {\n    let index = this._calculateIndex(rowIndex, columnIndex);\n    this.data[index] = value;\n    return this;\n  }\n\n  get(rowIndex, columnIndex) {\n    let index = this._calculateIndex(rowIndex, columnIndex);\n    return this.data[index];\n  }\n\n  _calculateIndex(row, column) {\n    return row * this.columns + column;\n  }\n}\n\nclass WrapperMatrix2D extends AbstractMatrix {\n  constructor(data) {\n    super();\n    this.data = data;\n    this.rows = data.length;\n    this.columns = data[0].length;\n  }\n\n  set(rowIndex, columnIndex, value) {\n    this.data[rowIndex][columnIndex] = value;\n    return this;\n  }\n\n  get(rowIndex, columnIndex) {\n    return this.data[rowIndex][columnIndex];\n  }\n}\n\nfunction wrap(array, options) {\n  if (isAnyArray(array)) {\n    if (array[0] && isAnyArray(array[0])) {\n      return new WrapperMatrix2D(array);\n    } else {\n      return new WrapperMatrix1D(array, options);\n    }\n  } else {\n    throw new Error('the argument is not an array');\n  }\n}\n\nclass LuDecomposition {\n  constructor(matrix) {\n    matrix = WrapperMatrix2D.checkMatrix(matrix);\n\n    let lu = matrix.clone();\n    let rows = lu.rows;\n    let columns = lu.columns;\n    let pivotVector = new Float64Array(rows);\n    let pivotSign = 1;\n    let i, j, k, p, s, t, v;\n    let LUcolj, kmax;\n\n    for (i = 0; i < rows; i++) {\n      pivotVector[i] = i;\n    }\n\n    LUcolj = new Float64Array(rows);\n\n    for (j = 0; j < columns; j++) {\n      for (i = 0; i < rows; i++) {\n        LUcolj[i] = lu.get(i, j);\n      }\n\n      for (i = 0; i < rows; i++) {\n        kmax = Math.min(i, j);\n        s = 0;\n        for (k = 0; k < kmax; k++) {\n          s += lu.get(i, k) * LUcolj[k];\n        }\n        LUcolj[i] -= s;\n        lu.set(i, j, LUcolj[i]);\n      }\n\n      p = j;\n      for (i = j + 1; i < rows; i++) {\n        if (Math.abs(LUcolj[i]) > Math.abs(LUcolj[p])) {\n          p = i;\n        }\n      }\n\n      if (p !== j) {\n        for (k = 0; k < columns; k++) {\n          t = lu.get(p, k);\n          lu.set(p, k, lu.get(j, k));\n          lu.set(j, k, t);\n        }\n\n        v = pivotVector[p];\n        pivotVector[p] = pivotVector[j];\n        pivotVector[j] = v;\n\n        pivotSign = -pivotSign;\n      }\n\n      if (j < rows && lu.get(j, j) !== 0) {\n        for (i = j + 1; i < rows; i++) {\n          lu.set(i, j, lu.get(i, j) / lu.get(j, j));\n        }\n      }\n    }\n\n    this.LU = lu;\n    this.pivotVector = pivotVector;\n    this.pivotSign = pivotSign;\n  }\n\n  isSingular() {\n    let data = this.LU;\n    let col = data.columns;\n    for (let j = 0; j < col; j++) {\n      if (data.get(j, j) === 0) {\n        return true;\n      }\n    }\n    return false;\n  }\n\n  solve(value) {\n    value = Matrix.checkMatrix(value);\n\n    let lu = this.LU;\n    let rows = lu.rows;\n\n    if (rows !== value.rows) {\n      throw new Error('Invalid matrix dimensions');\n    }\n    if (this.isSingular()) {\n      throw new Error('LU matrix is singular');\n    }\n\n    let count = value.columns;\n    let X = value.subMatrixRow(this.pivotVector, 0, count - 1);\n    let columns = lu.columns;\n    let i, j, k;\n\n    for (k = 0; k < columns; k++) {\n      for (i = k + 1; i < columns; i++) {\n        for (j = 0; j < count; j++) {\n          X.set(i, j, X.get(i, j) - X.get(k, j) * lu.get(i, k));\n        }\n      }\n    }\n    for (k = columns - 1; k >= 0; k--) {\n      for (j = 0; j < count; j++) {\n        X.set(k, j, X.get(k, j) / lu.get(k, k));\n      }\n      for (i = 0; i < k; i++) {\n        for (j = 0; j < count; j++) {\n          X.set(i, j, X.get(i, j) - X.get(k, j) * lu.get(i, k));\n        }\n      }\n    }\n    return X;\n  }\n\n  get determinant() {\n    let data = this.LU;\n    if (!data.isSquare()) {\n      throw new Error('Matrix must be square');\n    }\n    let determinant = this.pivotSign;\n    let col = data.columns;\n    for (let j = 0; j < col; j++) {\n      determinant *= data.get(j, j);\n    }\n    return determinant;\n  }\n\n  get lowerTriangularMatrix() {\n    let data = this.LU;\n    let rows = data.rows;\n    let columns = data.columns;\n    let X = new Matrix(rows, columns);\n    for (let i = 0; i < rows; i++) {\n      for (let j = 0; j < columns; j++) {\n        if (i > j) {\n          X.set(i, j, data.get(i, j));\n        } else if (i === j) {\n          X.set(i, j, 1);\n        } else {\n          X.set(i, j, 0);\n        }\n      }\n    }\n    return X;\n  }\n\n  get upperTriangularMatrix() {\n    let data = this.LU;\n    let rows = data.rows;\n    let columns = data.columns;\n    let X = new Matrix(rows, columns);\n    for (let i = 0; i < rows; i++) {\n      for (let j = 0; j < columns; j++) {\n        if (i <= j) {\n          X.set(i, j, data.get(i, j));\n        } else {\n          X.set(i, j, 0);\n        }\n      }\n    }\n    return X;\n  }\n\n  get pivotPermutationVector() {\n    return Array.from(this.pivotVector);\n  }\n}\n\nfunction hypotenuse(a, b) {\n  let r = 0;\n  if (Math.abs(a) > Math.abs(b)) {\n    r = b / a;\n    return Math.abs(a) * Math.sqrt(1 + r * r);\n  }\n  if (b !== 0) {\n    r = a / b;\n    return Math.abs(b) * Math.sqrt(1 + r * r);\n  }\n  return 0;\n}\n\nclass QrDecomposition {\n  constructor(value) {\n    value = WrapperMatrix2D.checkMatrix(value);\n\n    let qr = value.clone();\n    let m = value.rows;\n    let n = value.columns;\n    let rdiag = new Float64Array(n);\n    let i, j, k, s;\n\n    for (k = 0; k < n; k++) {\n      let nrm = 0;\n      for (i = k; i < m; i++) {\n        nrm = hypotenuse(nrm, qr.get(i, k));\n      }\n      if (nrm !== 0) {\n        if (qr.get(k, k) < 0) {\n          nrm = -nrm;\n        }\n        for (i = k; i < m; i++) {\n          qr.set(i, k, qr.get(i, k) / nrm);\n        }\n        qr.set(k, k, qr.get(k, k) + 1);\n        for (j = k + 1; j < n; j++) {\n          s = 0;\n          for (i = k; i < m; i++) {\n            s += qr.get(i, k) * qr.get(i, j);\n          }\n          s = -s / qr.get(k, k);\n          for (i = k; i < m; i++) {\n            qr.set(i, j, qr.get(i, j) + s * qr.get(i, k));\n          }\n        }\n      }\n      rdiag[k] = -nrm;\n    }\n\n    this.QR = qr;\n    this.Rdiag = rdiag;\n  }\n\n  solve(value) {\n    value = Matrix.checkMatrix(value);\n\n    let qr = this.QR;\n    let m = qr.rows;\n\n    if (value.rows !== m) {\n      throw new Error('Matrix row dimensions must agree');\n    }\n    if (!this.isFullRank()) {\n      throw new Error('Matrix is rank deficient');\n    }\n\n    let count = value.columns;\n    let X = value.clone();\n    let n = qr.columns;\n    let i, j, k, s;\n\n    for (k = 0; k < n; k++) {\n      for (j = 0; j < count; j++) {\n        s = 0;\n        for (i = k; i < m; i++) {\n          s += qr.get(i, k) * X.get(i, j);\n        }\n        s = -s / qr.get(k, k);\n        for (i = k; i < m; i++) {\n          X.set(i, j, X.get(i, j) + s * qr.get(i, k));\n        }\n      }\n    }\n    for (k = n - 1; k >= 0; k--) {\n      for (j = 0; j < count; j++) {\n        X.set(k, j, X.get(k, j) / this.Rdiag[k]);\n      }\n      for (i = 0; i < k; i++) {\n        for (j = 0; j < count; j++) {\n          X.set(i, j, X.get(i, j) - X.get(k, j) * qr.get(i, k));\n        }\n      }\n    }\n\n    return X.subMatrix(0, n - 1, 0, count - 1);\n  }\n\n  isFullRank() {\n    let columns = this.QR.columns;\n    for (let i = 0; i < columns; i++) {\n      if (this.Rdiag[i] === 0) {\n        return false;\n      }\n    }\n    return true;\n  }\n\n  get upperTriangularMatrix() {\n    let qr = this.QR;\n    let n = qr.columns;\n    let X = new Matrix(n, n);\n    let i, j;\n    for (i = 0; i < n; i++) {\n      for (j = 0; j < n; j++) {\n        if (i < j) {\n          X.set(i, j, qr.get(i, j));\n        } else if (i === j) {\n          X.set(i, j, this.Rdiag[i]);\n        } else {\n          X.set(i, j, 0);\n        }\n      }\n    }\n    return X;\n  }\n\n  get orthogonalMatrix() {\n    let qr = this.QR;\n    let rows = qr.rows;\n    let columns = qr.columns;\n    let X = new Matrix(rows, columns);\n    let i, j, k, s;\n\n    for (k = columns - 1; k >= 0; k--) {\n      for (i = 0; i < rows; i++) {\n        X.set(i, k, 0);\n      }\n      X.set(k, k, 1);\n      for (j = k; j < columns; j++) {\n        if (qr.get(k, k) !== 0) {\n          s = 0;\n          for (i = k; i < rows; i++) {\n            s += qr.get(i, k) * X.get(i, j);\n          }\n\n          s = -s / qr.get(k, k);\n\n          for (i = k; i < rows; i++) {\n            X.set(i, j, X.get(i, j) + s * qr.get(i, k));\n          }\n        }\n      }\n    }\n    return X;\n  }\n}\n\nclass SingularValueDecomposition {\n  constructor(value, options = {}) {\n    value = WrapperMatrix2D.checkMatrix(value);\n\n    if (value.isEmpty()) {\n      throw new Error('Matrix must be non-empty');\n    }\n\n    let m = value.rows;\n    let n = value.columns;\n\n    const {\n      computeLeftSingularVectors = true,\n      computeRightSingularVectors = true,\n      autoTranspose = false,\n    } = options;\n\n    let wantu = Boolean(computeLeftSingularVectors);\n    let wantv = Boolean(computeRightSingularVectors);\n\n    let swapped = false;\n    let a;\n    if (m < n) {\n      if (!autoTranspose) {\n        a = value.clone();\n        // eslint-disable-next-line no-console\n        console.warn(\n          'Computing SVD on a matrix with more columns than rows. Consider enabling autoTranspose',\n        );\n      } else {\n        a = value.transpose();\n        m = a.rows;\n        n = a.columns;\n        swapped = true;\n        let aux = wantu;\n        wantu = wantv;\n        wantv = aux;\n      }\n    } else {\n      a = value.clone();\n    }\n\n    let nu = Math.min(m, n);\n    let ni = Math.min(m + 1, n);\n    let s = new Float64Array(ni);\n    let U = new Matrix(m, nu);\n    let V = new Matrix(n, n);\n\n    let e = new Float64Array(n);\n    let work = new Float64Array(m);\n\n    let si = new Float64Array(ni);\n    for (let i = 0; i < ni; i++) si[i] = i;\n\n    let nct = Math.min(m - 1, n);\n    let nrt = Math.max(0, Math.min(n - 2, m));\n    let mrc = Math.max(nct, nrt);\n\n    for (let k = 0; k < mrc; k++) {\n      if (k < nct) {\n        s[k] = 0;\n        for (let i = k; i < m; i++) {\n          s[k] = hypotenuse(s[k], a.get(i, k));\n        }\n        if (s[k] !== 0) {\n          if (a.get(k, k) < 0) {\n            s[k] = -s[k];\n          }\n          for (let i = k; i < m; i++) {\n            a.set(i, k, a.get(i, k) / s[k]);\n          }\n          a.set(k, k, a.get(k, k) + 1);\n        }\n        s[k] = -s[k];\n      }\n\n      for (let j = k + 1; j < n; j++) {\n        if (k < nct && s[k] !== 0) {\n          let t = 0;\n          for (let i = k; i < m; i++) {\n            t += a.get(i, k) * a.get(i, j);\n          }\n          t = -t / a.get(k, k);\n          for (let i = k; i < m; i++) {\n            a.set(i, j, a.get(i, j) + t * a.get(i, k));\n          }\n        }\n        e[j] = a.get(k, j);\n      }\n\n      if (wantu && k < nct) {\n        for (let i = k; i < m; i++) {\n          U.set(i, k, a.get(i, k));\n        }\n      }\n\n      if (k < nrt) {\n        e[k] = 0;\n        for (let i = k + 1; i < n; i++) {\n          e[k] = hypotenuse(e[k], e[i]);\n        }\n        if (e[k] !== 0) {\n          if (e[k + 1] < 0) {\n            e[k] = 0 - e[k];\n          }\n          for (let i = k + 1; i < n; i++) {\n            e[i] /= e[k];\n          }\n          e[k + 1] += 1;\n        }\n        e[k] = -e[k];\n        if (k + 1 < m && e[k] !== 0) {\n          for (let i = k + 1; i < m; i++) {\n            work[i] = 0;\n          }\n          for (let i = k + 1; i < m; i++) {\n            for (let j = k + 1; j < n; j++) {\n              work[i] += e[j] * a.get(i, j);\n            }\n          }\n          for (let j = k + 1; j < n; j++) {\n            let t = -e[j] / e[k + 1];\n            for (let i = k + 1; i < m; i++) {\n              a.set(i, j, a.get(i, j) + t * work[i]);\n            }\n          }\n        }\n        if (wantv) {\n          for (let i = k + 1; i < n; i++) {\n            V.set(i, k, e[i]);\n          }\n        }\n      }\n    }\n\n    let p = Math.min(n, m + 1);\n    if (nct < n) {\n      s[nct] = a.get(nct, nct);\n    }\n    if (m < p) {\n      s[p - 1] = 0;\n    }\n    if (nrt + 1 < p) {\n      e[nrt] = a.get(nrt, p - 1);\n    }\n    e[p - 1] = 0;\n\n    if (wantu) {\n      for (let j = nct; j < nu; j++) {\n        for (let i = 0; i < m; i++) {\n          U.set(i, j, 0);\n        }\n        U.set(j, j, 1);\n      }\n      for (let k = nct - 1; k >= 0; k--) {\n        if (s[k] !== 0) {\n          for (let j = k + 1; j < nu; j++) {\n            let t = 0;\n            for (let i = k; i < m; i++) {\n              t += U.get(i, k) * U.get(i, j);\n            }\n            t = -t / U.get(k, k);\n            for (let i = k; i < m; i++) {\n              U.set(i, j, U.get(i, j) + t * U.get(i, k));\n            }\n          }\n          for (let i = k; i < m; i++) {\n            U.set(i, k, -U.get(i, k));\n          }\n          U.set(k, k, 1 + U.get(k, k));\n          for (let i = 0; i < k - 1; i++) {\n            U.set(i, k, 0);\n          }\n        } else {\n          for (let i = 0; i < m; i++) {\n            U.set(i, k, 0);\n          }\n          U.set(k, k, 1);\n        }\n      }\n    }\n\n    if (wantv) {\n      for (let k = n - 1; k >= 0; k--) {\n        if (k < nrt && e[k] !== 0) {\n          for (let j = k + 1; j < n; j++) {\n            let t = 0;\n            for (let i = k + 1; i < n; i++) {\n              t += V.get(i, k) * V.get(i, j);\n            }\n            t = -t / V.get(k + 1, k);\n            for (let i = k + 1; i < n; i++) {\n              V.set(i, j, V.get(i, j) + t * V.get(i, k));\n            }\n          }\n        }\n        for (let i = 0; i < n; i++) {\n          V.set(i, k, 0);\n        }\n        V.set(k, k, 1);\n      }\n    }\n\n    let pp = p - 1;\n    let eps = Number.EPSILON;\n    while (p > 0) {\n      let k, kase;\n      for (k = p - 2; k >= -1; k--) {\n        if (k === -1) {\n          break;\n        }\n        const alpha =\n          Number.MIN_VALUE + eps * Math.abs(s[k] + Math.abs(s[k + 1]));\n        if (Math.abs(e[k]) <= alpha || Number.isNaN(e[k])) {\n          e[k] = 0;\n          break;\n        }\n      }\n      if (k === p - 2) {\n        kase = 4;\n      } else {\n        let ks;\n        for (ks = p - 1; ks >= k; ks--) {\n          if (ks === k) {\n            break;\n          }\n          let t =\n            (ks !== p ? Math.abs(e[ks]) : 0) +\n            (ks !== k + 1 ? Math.abs(e[ks - 1]) : 0);\n          if (Math.abs(s[ks]) <= eps * t) {\n            s[ks] = 0;\n            break;\n          }\n        }\n        if (ks === k) {\n          kase = 3;\n        } else if (ks === p - 1) {\n          kase = 1;\n        } else {\n          kase = 2;\n          k = ks;\n        }\n      }\n\n      k++;\n\n      switch (kase) {\n        case 1: {\n          let f = e[p - 2];\n          e[p - 2] = 0;\n          for (let j = p - 2; j >= k; j--) {\n            let t = hypotenuse(s[j], f);\n            let cs = s[j] / t;\n            let sn = f / t;\n            s[j] = t;\n            if (j !== k) {\n              f = -sn * e[j - 1];\n              e[j - 1] = cs * e[j - 1];\n            }\n            if (wantv) {\n              for (let i = 0; i < n; i++) {\n                t = cs * V.get(i, j) + sn * V.get(i, p - 1);\n                V.set(i, p - 1, -sn * V.get(i, j) + cs * V.get(i, p - 1));\n                V.set(i, j, t);\n              }\n            }\n          }\n          break;\n        }\n        case 2: {\n          let f = e[k - 1];\n          e[k - 1] = 0;\n          for (let j = k; j < p; j++) {\n            let t = hypotenuse(s[j], f);\n            let cs = s[j] / t;\n            let sn = f / t;\n            s[j] = t;\n            f = -sn * e[j];\n            e[j] = cs * e[j];\n            if (wantu) {\n              for (let i = 0; i < m; i++) {\n                t = cs * U.get(i, j) + sn * U.get(i, k - 1);\n                U.set(i, k - 1, -sn * U.get(i, j) + cs * U.get(i, k - 1));\n                U.set(i, j, t);\n              }\n            }\n          }\n          break;\n        }\n        case 3: {\n          const scale = Math.max(\n            Math.abs(s[p - 1]),\n            Math.abs(s[p - 2]),\n            Math.abs(e[p - 2]),\n            Math.abs(s[k]),\n            Math.abs(e[k]),\n          );\n          const sp = s[p - 1] / scale;\n          const spm1 = s[p - 2] / scale;\n          const epm1 = e[p - 2] / scale;\n          const sk = s[k] / scale;\n          const ek = e[k] / scale;\n          const b = ((spm1 + sp) * (spm1 - sp) + epm1 * epm1) / 2;\n          const c = sp * epm1 * (sp * epm1);\n          let shift = 0;\n          if (b !== 0 || c !== 0) {\n            if (b < 0) {\n              shift = 0 - Math.sqrt(b * b + c);\n            } else {\n              shift = Math.sqrt(b * b + c);\n            }\n            shift = c / (b + shift);\n          }\n          let f = (sk + sp) * (sk - sp) + shift;\n          let g = sk * ek;\n          for (let j = k; j < p - 1; j++) {\n            let t = hypotenuse(f, g);\n            if (t === 0) t = Number.MIN_VALUE;\n            let cs = f / t;\n            let sn = g / t;\n            if (j !== k) {\n              e[j - 1] = t;\n            }\n            f = cs * s[j] + sn * e[j];\n            e[j] = cs * e[j] - sn * s[j];\n            g = sn * s[j + 1];\n            s[j + 1] = cs * s[j + 1];\n            if (wantv) {\n              for (let i = 0; i < n; i++) {\n                t = cs * V.get(i, j) + sn * V.get(i, j + 1);\n                V.set(i, j + 1, -sn * V.get(i, j) + cs * V.get(i, j + 1));\n                V.set(i, j, t);\n              }\n            }\n            t = hypotenuse(f, g);\n            if (t === 0) t = Number.MIN_VALUE;\n            cs = f / t;\n            sn = g / t;\n            s[j] = t;\n            f = cs * e[j] + sn * s[j + 1];\n            s[j + 1] = -sn * e[j] + cs * s[j + 1];\n            g = sn * e[j + 1];\n            e[j + 1] = cs * e[j + 1];\n            if (wantu && j < m - 1) {\n              for (let i = 0; i < m; i++) {\n                t = cs * U.get(i, j) + sn * U.get(i, j + 1);\n                U.set(i, j + 1, -sn * U.get(i, j) + cs * U.get(i, j + 1));\n                U.set(i, j, t);\n              }\n            }\n          }\n          e[p - 2] = f;\n          break;\n        }\n        case 4: {\n          if (s[k] <= 0) {\n            s[k] = s[k] < 0 ? -s[k] : 0;\n            if (wantv) {\n              for (let i = 0; i <= pp; i++) {\n                V.set(i, k, -V.get(i, k));\n              }\n            }\n          }\n          while (k < pp) {\n            if (s[k] >= s[k + 1]) {\n              break;\n            }\n            let t = s[k];\n            s[k] = s[k + 1];\n            s[k + 1] = t;\n            if (wantv && k < n - 1) {\n              for (let i = 0; i < n; i++) {\n                t = V.get(i, k + 1);\n                V.set(i, k + 1, V.get(i, k));\n                V.set(i, k, t);\n              }\n            }\n            if (wantu && k < m - 1) {\n              for (let i = 0; i < m; i++) {\n                t = U.get(i, k + 1);\n                U.set(i, k + 1, U.get(i, k));\n                U.set(i, k, t);\n              }\n            }\n            k++;\n          }\n          p--;\n          break;\n        }\n        // no default\n      }\n    }\n\n    if (swapped) {\n      let tmp = V;\n      V = U;\n      U = tmp;\n    }\n\n    this.m = m;\n    this.n = n;\n    this.s = s;\n    this.U = U;\n    this.V = V;\n  }\n\n  solve(value) {\n    let Y = value;\n    let e = this.threshold;\n    let scols = this.s.length;\n    let Ls = Matrix.zeros(scols, scols);\n\n    for (let i = 0; i < scols; i++) {\n      if (Math.abs(this.s[i]) <= e) {\n        Ls.set(i, i, 0);\n      } else {\n        Ls.set(i, i, 1 / this.s[i]);\n      }\n    }\n\n    let U = this.U;\n    let V = this.rightSingularVectors;\n\n    let VL = V.mmul(Ls);\n    let vrows = V.rows;\n    let urows = U.rows;\n    let VLU = Matrix.zeros(vrows, urows);\n\n    for (let i = 0; i < vrows; i++) {\n      for (let j = 0; j < urows; j++) {\n        let sum = 0;\n        for (let k = 0; k < scols; k++) {\n          sum += VL.get(i, k) * U.get(j, k);\n        }\n        VLU.set(i, j, sum);\n      }\n    }\n\n    return VLU.mmul(Y);\n  }\n\n  solveForDiagonal(value) {\n    return this.solve(Matrix.diag(value));\n  }\n\n  inverse() {\n    let V = this.V;\n    let e = this.threshold;\n    let vrows = V.rows;\n    let vcols = V.columns;\n    let X = new Matrix(vrows, this.s.length);\n\n    for (let i = 0; i < vrows; i++) {\n      for (let j = 0; j < vcols; j++) {\n        if (Math.abs(this.s[j]) > e) {\n          X.set(i, j, V.get(i, j) / this.s[j]);\n        }\n      }\n    }\n\n    let U = this.U;\n\n    let urows = U.rows;\n    let ucols = U.columns;\n    let Y = new Matrix(vrows, urows);\n\n    for (let i = 0; i < vrows; i++) {\n      for (let j = 0; j < urows; j++) {\n        let sum = 0;\n        for (let k = 0; k < ucols; k++) {\n          sum += X.get(i, k) * U.get(j, k);\n        }\n        Y.set(i, j, sum);\n      }\n    }\n\n    return Y;\n  }\n\n  get condition() {\n    return this.s[0] / this.s[Math.min(this.m, this.n) - 1];\n  }\n\n  get norm2() {\n    return this.s[0];\n  }\n\n  get rank() {\n    let tol = Math.max(this.m, this.n) * this.s[0] * Number.EPSILON;\n    let r = 0;\n    let s = this.s;\n    for (let i = 0, ii = s.length; i < ii; i++) {\n      if (s[i] > tol) {\n        r++;\n      }\n    }\n    return r;\n  }\n\n  get diagonal() {\n    return Array.from(this.s);\n  }\n\n  get threshold() {\n    return (Number.EPSILON / 2) * Math.max(this.m, this.n) * this.s[0];\n  }\n\n  get leftSingularVectors() {\n    return this.U;\n  }\n\n  get rightSingularVectors() {\n    return this.V;\n  }\n\n  get diagonalMatrix() {\n    return Matrix.diag(this.s);\n  }\n}\n\nfunction inverse(matrix, useSVD = false) {\n  matrix = WrapperMatrix2D.checkMatrix(matrix);\n  if (useSVD) {\n    return new SingularValueDecomposition(matrix).inverse();\n  } else {\n    return solve(matrix, Matrix.eye(matrix.rows));\n  }\n}\n\nfunction solve(leftHandSide, rightHandSide, useSVD = false) {\n  leftHandSide = WrapperMatrix2D.checkMatrix(leftHandSide);\n  rightHandSide = WrapperMatrix2D.checkMatrix(rightHandSide);\n  if (useSVD) {\n    return new SingularValueDecomposition(leftHandSide).solve(rightHandSide);\n  } else {\n    return leftHandSide.isSquare()\n      ? new LuDecomposition(leftHandSide).solve(rightHandSide)\n      : new QrDecomposition(leftHandSide).solve(rightHandSide);\n  }\n}\n\nfunction determinant(matrix) {\n  matrix = Matrix.checkMatrix(matrix);\n  if (matrix.isSquare()) {\n    if (matrix.columns === 0) {\n      return 1;\n    }\n\n    let a, b, c, d;\n    if (matrix.columns === 2) {\n      // 2 x 2 matrix\n      a = matrix.get(0, 0);\n      b = matrix.get(0, 1);\n      c = matrix.get(1, 0);\n      d = matrix.get(1, 1);\n\n      return a * d - b * c;\n    } else if (matrix.columns === 3) {\n      // 3 x 3 matrix\n      let subMatrix0, subMatrix1, subMatrix2;\n      subMatrix0 = new MatrixSelectionView(matrix, [1, 2], [1, 2]);\n      subMatrix1 = new MatrixSelectionView(matrix, [1, 2], [0, 2]);\n      subMatrix2 = new MatrixSelectionView(matrix, [1, 2], [0, 1]);\n      a = matrix.get(0, 0);\n      b = matrix.get(0, 1);\n      c = matrix.get(0, 2);\n\n      return (\n        a * determinant(subMatrix0) -\n        b * determinant(subMatrix1) +\n        c * determinant(subMatrix2)\n      );\n    } else {\n      // general purpose determinant using the LU decomposition\n      return new LuDecomposition(matrix).determinant;\n    }\n  } else {\n    throw Error('determinant can only be calculated for a square matrix');\n  }\n}\n\nfunction xrange(n, exception) {\n  let range = [];\n  for (let i = 0; i < n; i++) {\n    if (i !== exception) {\n      range.push(i);\n    }\n  }\n  return range;\n}\n\nfunction dependenciesOneRow(\n  error,\n  matrix,\n  index,\n  thresholdValue = 10e-10,\n  thresholdError = 10e-10,\n) {\n  if (error > thresholdError) {\n    return new Array(matrix.rows + 1).fill(0);\n  } else {\n    let returnArray = matrix.addRow(index, [0]);\n    for (let i = 0; i < returnArray.rows; i++) {\n      if (Math.abs(returnArray.get(i, 0)) < thresholdValue) {\n        returnArray.set(i, 0, 0);\n      }\n    }\n    return returnArray.to1DArray();\n  }\n}\n\nfunction linearDependencies(matrix, options = {}) {\n  const { thresholdValue = 10e-10, thresholdError = 10e-10 } = options;\n  matrix = Matrix.checkMatrix(matrix);\n\n  let n = matrix.rows;\n  let results = new Matrix(n, n);\n\n  for (let i = 0; i < n; i++) {\n    let b = Matrix.columnVector(matrix.getRow(i));\n    let Abis = matrix.subMatrixRow(xrange(n, i)).transpose();\n    let svd = new SingularValueDecomposition(Abis);\n    let x = svd.solve(b);\n    let error = Matrix.sub(b, Abis.mmul(x)).abs().max();\n    results.setRow(\n      i,\n      dependenciesOneRow(error, x, i, thresholdValue, thresholdError),\n    );\n  }\n  return results;\n}\n\nfunction pseudoInverse(matrix, threshold = Number.EPSILON) {\n  matrix = Matrix.checkMatrix(matrix);\n  if (matrix.isEmpty()) {\n    // with a zero dimension, the pseudo-inverse is the transpose, since all 0xn and nx0 matrices are singular\n    // (0xn)*(nx0)*(0xn) = 0xn\n    // (nx0)*(0xn)*(nx0) = nx0\n    return matrix.transpose();\n  }\n  let svdSolution = new SingularValueDecomposition(matrix, { autoTranspose: true });\n\n  let U = svdSolution.leftSingularVectors;\n  let V = svdSolution.rightSingularVectors;\n  let s = svdSolution.diagonal;\n\n  for (let i = 0; i < s.length; i++) {\n    if (Math.abs(s[i]) > threshold) {\n      s[i] = 1.0 / s[i];\n    } else {\n      s[i] = 0.0;\n    }\n  }\n\n  return V.mmul(Matrix.diag(s).mmul(U.transpose()));\n}\n\nfunction covariance(xMatrix, yMatrix = xMatrix, options = {}) {\n  xMatrix = new Matrix(xMatrix);\n  let yIsSame = false;\n  if (\n    typeof yMatrix === 'object' &&\n    !Matrix.isMatrix(yMatrix) &&\n    !isAnyArray(yMatrix)\n  ) {\n    options = yMatrix;\n    yMatrix = xMatrix;\n    yIsSame = true;\n  } else {\n    yMatrix = new Matrix(yMatrix);\n  }\n  if (xMatrix.rows !== yMatrix.rows) {\n    throw new TypeError('Both matrices must have the same number of rows');\n  }\n  const { center = true } = options;\n  if (center) {\n    xMatrix = xMatrix.center('column');\n    if (!yIsSame) {\n      yMatrix = yMatrix.center('column');\n    }\n  }\n  const cov = xMatrix.transpose().mmul(yMatrix);\n  for (let i = 0; i < cov.rows; i++) {\n    for (let j = 0; j < cov.columns; j++) {\n      cov.set(i, j, cov.get(i, j) * (1 / (xMatrix.rows - 1)));\n    }\n  }\n  return cov;\n}\n\nfunction correlation(xMatrix, yMatrix = xMatrix, options = {}) {\n  xMatrix = new Matrix(xMatrix);\n  let yIsSame = false;\n  if (\n    typeof yMatrix === 'object' &&\n    !Matrix.isMatrix(yMatrix) &&\n    !isAnyArray(yMatrix)\n  ) {\n    options = yMatrix;\n    yMatrix = xMatrix;\n    yIsSame = true;\n  } else {\n    yMatrix = new Matrix(yMatrix);\n  }\n  if (xMatrix.rows !== yMatrix.rows) {\n    throw new TypeError('Both matrices must have the same number of rows');\n  }\n\n  const { center = true, scale = true } = options;\n  if (center) {\n    xMatrix.center('column');\n    if (!yIsSame) {\n      yMatrix.center('column');\n    }\n  }\n  if (scale) {\n    xMatrix.scale('column');\n    if (!yIsSame) {\n      yMatrix.scale('column');\n    }\n  }\n\n  const sdx = xMatrix.standardDeviation('column', { unbiased: true });\n  const sdy = yIsSame\n    ? sdx\n    : yMatrix.standardDeviation('column', { unbiased: true });\n\n  const corr = xMatrix.transpose().mmul(yMatrix);\n  for (let i = 0; i < corr.rows; i++) {\n    for (let j = 0; j < corr.columns; j++) {\n      corr.set(\n        i,\n        j,\n        corr.get(i, j) * (1 / (sdx[i] * sdy[j])) * (1 / (xMatrix.rows - 1)),\n      );\n    }\n  }\n  return corr;\n}\n\nclass EigenvalueDecomposition {\n  constructor(matrix, options = {}) {\n    const { assumeSymmetric = false } = options;\n\n    matrix = WrapperMatrix2D.checkMatrix(matrix);\n    if (!matrix.isSquare()) {\n      throw new Error('Matrix is not a square matrix');\n    }\n\n    if (matrix.isEmpty()) {\n      throw new Error('Matrix must be non-empty');\n    }\n\n    let n = matrix.columns;\n    let V = new Matrix(n, n);\n    let d = new Float64Array(n);\n    let e = new Float64Array(n);\n    let value = matrix;\n    let i, j;\n\n    let isSymmetric = false;\n    if (assumeSymmetric) {\n      isSymmetric = true;\n    } else {\n      isSymmetric = matrix.isSymmetric();\n    }\n\n    if (isSymmetric) {\n      for (i = 0; i < n; i++) {\n        for (j = 0; j < n; j++) {\n          V.set(i, j, value.get(i, j));\n        }\n      }\n      tred2(n, e, d, V);\n      tql2(n, e, d, V);\n    } else {\n      let H = new Matrix(n, n);\n      let ort = new Float64Array(n);\n      for (j = 0; j < n; j++) {\n        for (i = 0; i < n; i++) {\n          H.set(i, j, value.get(i, j));\n        }\n      }\n      orthes(n, H, ort, V);\n      hqr2(n, e, d, V, H);\n    }\n\n    this.n = n;\n    this.e = e;\n    this.d = d;\n    this.V = V;\n  }\n\n  get realEigenvalues() {\n    return Array.from(this.d);\n  }\n\n  get imaginaryEigenvalues() {\n    return Array.from(this.e);\n  }\n\n  get eigenvectorMatrix() {\n    return this.V;\n  }\n\n  get diagonalMatrix() {\n    let n = this.n;\n    let e = this.e;\n    let d = this.d;\n    let X = new Matrix(n, n);\n    let i, j;\n    for (i = 0; i < n; i++) {\n      for (j = 0; j < n; j++) {\n        X.set(i, j, 0);\n      }\n      X.set(i, i, d[i]);\n      if (e[i] > 0) {\n        X.set(i, i + 1, e[i]);\n      } else if (e[i] < 0) {\n        X.set(i, i - 1, e[i]);\n      }\n    }\n    return X;\n  }\n}\n\nfunction tred2(n, e, d, V) {\n  let f, g, h, i, j, k, hh, scale;\n\n  for (j = 0; j < n; j++) {\n    d[j] = V.get(n - 1, j);\n  }\n\n  for (i = n - 1; i > 0; i--) {\n    scale = 0;\n    h = 0;\n    for (k = 0; k < i; k++) {\n      scale = scale + Math.abs(d[k]);\n    }\n\n    if (scale === 0) {\n      e[i] = d[i - 1];\n      for (j = 0; j < i; j++) {\n        d[j] = V.get(i - 1, j);\n        V.set(i, j, 0);\n        V.set(j, i, 0);\n      }\n    } else {\n      for (k = 0; k < i; k++) {\n        d[k] /= scale;\n        h += d[k] * d[k];\n      }\n\n      f = d[i - 1];\n      g = Math.sqrt(h);\n      if (f > 0) {\n        g = -g;\n      }\n\n      e[i] = scale * g;\n      h = h - f * g;\n      d[i - 1] = f - g;\n      for (j = 0; j < i; j++) {\n        e[j] = 0;\n      }\n\n      for (j = 0; j < i; j++) {\n        f = d[j];\n        V.set(j, i, f);\n        g = e[j] + V.get(j, j) * f;\n        for (k = j + 1; k <= i - 1; k++) {\n          g += V.get(k, j) * d[k];\n          e[k] += V.get(k, j) * f;\n        }\n        e[j] = g;\n      }\n\n      f = 0;\n      for (j = 0; j < i; j++) {\n        e[j] /= h;\n        f += e[j] * d[j];\n      }\n\n      hh = f / (h + h);\n      for (j = 0; j < i; j++) {\n        e[j] -= hh * d[j];\n      }\n\n      for (j = 0; j < i; j++) {\n        f = d[j];\n        g = e[j];\n        for (k = j; k <= i - 1; k++) {\n          V.set(k, j, V.get(k, j) - (f * e[k] + g * d[k]));\n        }\n        d[j] = V.get(i - 1, j);\n        V.set(i, j, 0);\n      }\n    }\n    d[i] = h;\n  }\n\n  for (i = 0; i < n - 1; i++) {\n    V.set(n - 1, i, V.get(i, i));\n    V.set(i, i, 1);\n    h = d[i + 1];\n    if (h !== 0) {\n      for (k = 0; k <= i; k++) {\n        d[k] = V.get(k, i + 1) / h;\n      }\n\n      for (j = 0; j <= i; j++) {\n        g = 0;\n        for (k = 0; k <= i; k++) {\n          g += V.get(k, i + 1) * V.get(k, j);\n        }\n        for (k = 0; k <= i; k++) {\n          V.set(k, j, V.get(k, j) - g * d[k]);\n        }\n      }\n    }\n\n    for (k = 0; k <= i; k++) {\n      V.set(k, i + 1, 0);\n    }\n  }\n\n  for (j = 0; j < n; j++) {\n    d[j] = V.get(n - 1, j);\n    V.set(n - 1, j, 0);\n  }\n\n  V.set(n - 1, n - 1, 1);\n  e[0] = 0;\n}\n\nfunction tql2(n, e, d, V) {\n  let g, h, i, j, k, l, m, p, r, dl1, c, c2, c3, el1, s, s2;\n\n  for (i = 1; i < n; i++) {\n    e[i - 1] = e[i];\n  }\n\n  e[n - 1] = 0;\n\n  let f = 0;\n  let tst1 = 0;\n  let eps = Number.EPSILON;\n\n  for (l = 0; l < n; l++) {\n    tst1 = Math.max(tst1, Math.abs(d[l]) + Math.abs(e[l]));\n    m = l;\n    while (m < n) {\n      if (Math.abs(e[m]) <= eps * tst1) {\n        break;\n      }\n      m++;\n    }\n\n    if (m > l) {\n      do {\n\n        g = d[l];\n        p = (d[l + 1] - g) / (2 * e[l]);\n        r = hypotenuse(p, 1);\n        if (p < 0) {\n          r = -r;\n        }\n\n        d[l] = e[l] / (p + r);\n        d[l + 1] = e[l] * (p + r);\n        dl1 = d[l + 1];\n        h = g - d[l];\n        for (i = l + 2; i < n; i++) {\n          d[i] -= h;\n        }\n\n        f = f + h;\n\n        p = d[m];\n        c = 1;\n        c2 = c;\n        c3 = c;\n        el1 = e[l + 1];\n        s = 0;\n        s2 = 0;\n        for (i = m - 1; i >= l; i--) {\n          c3 = c2;\n          c2 = c;\n          s2 = s;\n          g = c * e[i];\n          h = c * p;\n          r = hypotenuse(p, e[i]);\n          e[i + 1] = s * r;\n          s = e[i] / r;\n          c = p / r;\n          p = c * d[i] - s * g;\n          d[i + 1] = h + s * (c * g + s * d[i]);\n\n          for (k = 0; k < n; k++) {\n            h = V.get(k, i + 1);\n            V.set(k, i + 1, s * V.get(k, i) + c * h);\n            V.set(k, i, c * V.get(k, i) - s * h);\n          }\n        }\n\n        p = (-s * s2 * c3 * el1 * e[l]) / dl1;\n        e[l] = s * p;\n        d[l] = c * p;\n      } while (Math.abs(e[l]) > eps * tst1);\n    }\n    d[l] = d[l] + f;\n    e[l] = 0;\n  }\n\n  for (i = 0; i < n - 1; i++) {\n    k = i;\n    p = d[i];\n    for (j = i + 1; j < n; j++) {\n      if (d[j] < p) {\n        k = j;\n        p = d[j];\n      }\n    }\n\n    if (k !== i) {\n      d[k] = d[i];\n      d[i] = p;\n      for (j = 0; j < n; j++) {\n        p = V.get(j, i);\n        V.set(j, i, V.get(j, k));\n        V.set(j, k, p);\n      }\n    }\n  }\n}\n\nfunction orthes(n, H, ort, V) {\n  let low = 0;\n  let high = n - 1;\n  let f, g, h, i, j, m;\n  let scale;\n\n  for (m = low + 1; m <= high - 1; m++) {\n    scale = 0;\n    for (i = m; i <= high; i++) {\n      scale = scale + Math.abs(H.get(i, m - 1));\n    }\n\n    if (scale !== 0) {\n      h = 0;\n      for (i = high; i >= m; i--) {\n        ort[i] = H.get(i, m - 1) / scale;\n        h += ort[i] * ort[i];\n      }\n\n      g = Math.sqrt(h);\n      if (ort[m] > 0) {\n        g = -g;\n      }\n\n      h = h - ort[m] * g;\n      ort[m] = ort[m] - g;\n\n      for (j = m; j < n; j++) {\n        f = 0;\n        for (i = high; i >= m; i--) {\n          f += ort[i] * H.get(i, j);\n        }\n\n        f = f / h;\n        for (i = m; i <= high; i++) {\n          H.set(i, j, H.get(i, j) - f * ort[i]);\n        }\n      }\n\n      for (i = 0; i <= high; i++) {\n        f = 0;\n        for (j = high; j >= m; j--) {\n          f += ort[j] * H.get(i, j);\n        }\n\n        f = f / h;\n        for (j = m; j <= high; j++) {\n          H.set(i, j, H.get(i, j) - f * ort[j]);\n        }\n      }\n\n      ort[m] = scale * ort[m];\n      H.set(m, m - 1, scale * g);\n    }\n  }\n\n  for (i = 0; i < n; i++) {\n    for (j = 0; j < n; j++) {\n      V.set(i, j, i === j ? 1 : 0);\n    }\n  }\n\n  for (m = high - 1; m >= low + 1; m--) {\n    if (H.get(m, m - 1) !== 0) {\n      for (i = m + 1; i <= high; i++) {\n        ort[i] = H.get(i, m - 1);\n      }\n\n      for (j = m; j <= high; j++) {\n        g = 0;\n        for (i = m; i <= high; i++) {\n          g += ort[i] * V.get(i, j);\n        }\n\n        g = g / ort[m] / H.get(m, m - 1);\n        for (i = m; i <= high; i++) {\n          V.set(i, j, V.get(i, j) + g * ort[i]);\n        }\n      }\n    }\n  }\n}\n\nfunction hqr2(nn, e, d, V, H) {\n  let n = nn - 1;\n  let low = 0;\n  let high = nn - 1;\n  let eps = Number.EPSILON;\n  let exshift = 0;\n  let norm = 0;\n  let p = 0;\n  let q = 0;\n  let r = 0;\n  let s = 0;\n  let z = 0;\n  let iter = 0;\n  let i, j, k, l, m, t, w, x, y;\n  let ra, sa, vr, vi;\n  let notlast, cdivres;\n\n  for (i = 0; i < nn; i++) {\n    if (i < low || i > high) {\n      d[i] = H.get(i, i);\n      e[i] = 0;\n    }\n\n    for (j = Math.max(i - 1, 0); j < nn; j++) {\n      norm = norm + Math.abs(H.get(i, j));\n    }\n  }\n\n  while (n >= low) {\n    l = n;\n    while (l > low) {\n      s = Math.abs(H.get(l - 1, l - 1)) + Math.abs(H.get(l, l));\n      if (s === 0) {\n        s = norm;\n      }\n      if (Math.abs(H.get(l, l - 1)) < eps * s) {\n        break;\n      }\n      l--;\n    }\n\n    if (l === n) {\n      H.set(n, n, H.get(n, n) + exshift);\n      d[n] = H.get(n, n);\n      e[n] = 0;\n      n--;\n      iter = 0;\n    } else if (l === n - 1) {\n      w = H.get(n, n - 1) * H.get(n - 1, n);\n      p = (H.get(n - 1, n - 1) - H.get(n, n)) / 2;\n      q = p * p + w;\n      z = Math.sqrt(Math.abs(q));\n      H.set(n, n, H.get(n, n) + exshift);\n      H.set(n - 1, n - 1, H.get(n - 1, n - 1) + exshift);\n      x = H.get(n, n);\n\n      if (q >= 0) {\n        z = p >= 0 ? p + z : p - z;\n        d[n - 1] = x + z;\n        d[n] = d[n - 1];\n        if (z !== 0) {\n          d[n] = x - w / z;\n        }\n        e[n - 1] = 0;\n        e[n] = 0;\n        x = H.get(n, n - 1);\n        s = Math.abs(x) + Math.abs(z);\n        p = x / s;\n        q = z / s;\n        r = Math.sqrt(p * p + q * q);\n        p = p / r;\n        q = q / r;\n\n        for (j = n - 1; j < nn; j++) {\n          z = H.get(n - 1, j);\n          H.set(n - 1, j, q * z + p * H.get(n, j));\n          H.set(n, j, q * H.get(n, j) - p * z);\n        }\n\n        for (i = 0; i <= n; i++) {\n          z = H.get(i, n - 1);\n          H.set(i, n - 1, q * z + p * H.get(i, n));\n          H.set(i, n, q * H.get(i, n) - p * z);\n        }\n\n        for (i = low; i <= high; i++) {\n          z = V.get(i, n - 1);\n          V.set(i, n - 1, q * z + p * V.get(i, n));\n          V.set(i, n, q * V.get(i, n) - p * z);\n        }\n      } else {\n        d[n - 1] = x + p;\n        d[n] = x + p;\n        e[n - 1] = z;\n        e[n] = -z;\n      }\n\n      n = n - 2;\n      iter = 0;\n    } else {\n      x = H.get(n, n);\n      y = 0;\n      w = 0;\n      if (l < n) {\n        y = H.get(n - 1, n - 1);\n        w = H.get(n, n - 1) * H.get(n - 1, n);\n      }\n\n      if (iter === 10) {\n        exshift += x;\n        for (i = low; i <= n; i++) {\n          H.set(i, i, H.get(i, i) - x);\n        }\n        s = Math.abs(H.get(n, n - 1)) + Math.abs(H.get(n - 1, n - 2));\n        // eslint-disable-next-line no-multi-assign\n        x = y = 0.75 * s;\n        w = -0.4375 * s * s;\n      }\n\n      if (iter === 30) {\n        s = (y - x) / 2;\n        s = s * s + w;\n        if (s > 0) {\n          s = Math.sqrt(s);\n          if (y < x) {\n            s = -s;\n          }\n          s = x - w / ((y - x) / 2 + s);\n          for (i = low; i <= n; i++) {\n            H.set(i, i, H.get(i, i) - s);\n          }\n          exshift += s;\n          // eslint-disable-next-line no-multi-assign\n          x = y = w = 0.964;\n        }\n      }\n\n      iter = iter + 1;\n\n      m = n - 2;\n      while (m >= l) {\n        z = H.get(m, m);\n        r = x - z;\n        s = y - z;\n        p = (r * s - w) / H.get(m + 1, m) + H.get(m, m + 1);\n        q = H.get(m + 1, m + 1) - z - r - s;\n        r = H.get(m + 2, m + 1);\n        s = Math.abs(p) + Math.abs(q) + Math.abs(r);\n        p = p / s;\n        q = q / s;\n        r = r / s;\n        if (m === l) {\n          break;\n        }\n        if (\n          Math.abs(H.get(m, m - 1)) * (Math.abs(q) + Math.abs(r)) <\n          eps *\n            (Math.abs(p) *\n              (Math.abs(H.get(m - 1, m - 1)) +\n                Math.abs(z) +\n                Math.abs(H.get(m + 1, m + 1))))\n        ) {\n          break;\n        }\n        m--;\n      }\n\n      for (i = m + 2; i <= n; i++) {\n        H.set(i, i - 2, 0);\n        if (i > m + 2) {\n          H.set(i, i - 3, 0);\n        }\n      }\n\n      for (k = m; k <= n - 1; k++) {\n        notlast = k !== n - 1;\n        if (k !== m) {\n          p = H.get(k, k - 1);\n          q = H.get(k + 1, k - 1);\n          r = notlast ? H.get(k + 2, k - 1) : 0;\n          x = Math.abs(p) + Math.abs(q) + Math.abs(r);\n          if (x !== 0) {\n            p = p / x;\n            q = q / x;\n            r = r / x;\n          }\n        }\n\n        if (x === 0) {\n          break;\n        }\n\n        s = Math.sqrt(p * p + q * q + r * r);\n        if (p < 0) {\n          s = -s;\n        }\n\n        if (s !== 0) {\n          if (k !== m) {\n            H.set(k, k - 1, -s * x);\n          } else if (l !== m) {\n            H.set(k, k - 1, -H.get(k, k - 1));\n          }\n\n          p = p + s;\n          x = p / s;\n          y = q / s;\n          z = r / s;\n          q = q / p;\n          r = r / p;\n\n          for (j = k; j < nn; j++) {\n            p = H.get(k, j) + q * H.get(k + 1, j);\n            if (notlast) {\n              p = p + r * H.get(k + 2, j);\n              H.set(k + 2, j, H.get(k + 2, j) - p * z);\n            }\n\n            H.set(k, j, H.get(k, j) - p * x);\n            H.set(k + 1, j, H.get(k + 1, j) - p * y);\n          }\n\n          for (i = 0; i <= Math.min(n, k + 3); i++) {\n            p = x * H.get(i, k) + y * H.get(i, k + 1);\n            if (notlast) {\n              p = p + z * H.get(i, k + 2);\n              H.set(i, k + 2, H.get(i, k + 2) - p * r);\n            }\n\n            H.set(i, k, H.get(i, k) - p);\n            H.set(i, k + 1, H.get(i, k + 1) - p * q);\n          }\n\n          for (i = low; i <= high; i++) {\n            p = x * V.get(i, k) + y * V.get(i, k + 1);\n            if (notlast) {\n              p = p + z * V.get(i, k + 2);\n              V.set(i, k + 2, V.get(i, k + 2) - p * r);\n            }\n\n            V.set(i, k, V.get(i, k) - p);\n            V.set(i, k + 1, V.get(i, k + 1) - p * q);\n          }\n        }\n      }\n    }\n  }\n\n  if (norm === 0) {\n    return;\n  }\n\n  for (n = nn - 1; n >= 0; n--) {\n    p = d[n];\n    q = e[n];\n\n    if (q === 0) {\n      l = n;\n      H.set(n, n, 1);\n      for (i = n - 1; i >= 0; i--) {\n        w = H.get(i, i) - p;\n        r = 0;\n        for (j = l; j <= n; j++) {\n          r = r + H.get(i, j) * H.get(j, n);\n        }\n\n        if (e[i] < 0) {\n          z = w;\n          s = r;\n        } else {\n          l = i;\n          if (e[i] === 0) {\n            H.set(i, n, w !== 0 ? -r / w : -r / (eps * norm));\n          } else {\n            x = H.get(i, i + 1);\n            y = H.get(i + 1, i);\n            q = (d[i] - p) * (d[i] - p) + e[i] * e[i];\n            t = (x * s - z * r) / q;\n            H.set(i, n, t);\n            H.set(\n              i + 1,\n              n,\n              Math.abs(x) > Math.abs(z) ? (-r - w * t) / x : (-s - y * t) / z,\n            );\n          }\n\n          t = Math.abs(H.get(i, n));\n          if (eps * t * t > 1) {\n            for (j = i; j <= n; j++) {\n              H.set(j, n, H.get(j, n) / t);\n            }\n          }\n        }\n      }\n    } else if (q < 0) {\n      l = n - 1;\n\n      if (Math.abs(H.get(n, n - 1)) > Math.abs(H.get(n - 1, n))) {\n        H.set(n - 1, n - 1, q / H.get(n, n - 1));\n        H.set(n - 1, n, -(H.get(n, n) - p) / H.get(n, n - 1));\n      } else {\n        cdivres = cdiv(0, -H.get(n - 1, n), H.get(n - 1, n - 1) - p, q);\n        H.set(n - 1, n - 1, cdivres[0]);\n        H.set(n - 1, n, cdivres[1]);\n      }\n\n      H.set(n, n - 1, 0);\n      H.set(n, n, 1);\n      for (i = n - 2; i >= 0; i--) {\n        ra = 0;\n        sa = 0;\n        for (j = l; j <= n; j++) {\n          ra = ra + H.get(i, j) * H.get(j, n - 1);\n          sa = sa + H.get(i, j) * H.get(j, n);\n        }\n\n        w = H.get(i, i) - p;\n\n        if (e[i] < 0) {\n          z = w;\n          r = ra;\n          s = sa;\n        } else {\n          l = i;\n          if (e[i] === 0) {\n            cdivres = cdiv(-ra, -sa, w, q);\n            H.set(i, n - 1, cdivres[0]);\n            H.set(i, n, cdivres[1]);\n          } else {\n            x = H.get(i, i + 1);\n            y = H.get(i + 1, i);\n            vr = (d[i] - p) * (d[i] - p) + e[i] * e[i] - q * q;\n            vi = (d[i] - p) * 2 * q;\n            if (vr === 0 && vi === 0) {\n              vr =\n                eps *\n                norm *\n                (Math.abs(w) +\n                  Math.abs(q) +\n                  Math.abs(x) +\n                  Math.abs(y) +\n                  Math.abs(z));\n            }\n            cdivres = cdiv(\n              x * r - z * ra + q * sa,\n              x * s - z * sa - q * ra,\n              vr,\n              vi,\n            );\n            H.set(i, n - 1, cdivres[0]);\n            H.set(i, n, cdivres[1]);\n            if (Math.abs(x) > Math.abs(z) + Math.abs(q)) {\n              H.set(\n                i + 1,\n                n - 1,\n                (-ra - w * H.get(i, n - 1) + q * H.get(i, n)) / x,\n              );\n              H.set(\n                i + 1,\n                n,\n                (-sa - w * H.get(i, n) - q * H.get(i, n - 1)) / x,\n              );\n            } else {\n              cdivres = cdiv(\n                -r - y * H.get(i, n - 1),\n                -s - y * H.get(i, n),\n                z,\n                q,\n              );\n              H.set(i + 1, n - 1, cdivres[0]);\n              H.set(i + 1, n, cdivres[1]);\n            }\n          }\n\n          t = Math.max(Math.abs(H.get(i, n - 1)), Math.abs(H.get(i, n)));\n          if (eps * t * t > 1) {\n            for (j = i; j <= n; j++) {\n              H.set(j, n - 1, H.get(j, n - 1) / t);\n              H.set(j, n, H.get(j, n) / t);\n            }\n          }\n        }\n      }\n    }\n  }\n\n  for (i = 0; i < nn; i++) {\n    if (i < low || i > high) {\n      for (j = i; j < nn; j++) {\n        V.set(i, j, H.get(i, j));\n      }\n    }\n  }\n\n  for (j = nn - 1; j >= low; j--) {\n    for (i = low; i <= high; i++) {\n      z = 0;\n      for (k = low; k <= Math.min(j, high); k++) {\n        z = z + V.get(i, k) * H.get(k, j);\n      }\n      V.set(i, j, z);\n    }\n  }\n}\n\nfunction cdiv(xr, xi, yr, yi) {\n  let r, d;\n  if (Math.abs(yr) > Math.abs(yi)) {\n    r = yi / yr;\n    d = yr + r * yi;\n    return [(xr + r * xi) / d, (xi - r * xr) / d];\n  } else {\n    r = yr / yi;\n    d = yi + r * yr;\n    return [(r * xr + xi) / d, (r * xi - xr) / d];\n  }\n}\n\nclass CholeskyDecomposition {\n  constructor(value) {\n    value = WrapperMatrix2D.checkMatrix(value);\n    if (!value.isSymmetric()) {\n      throw new Error('Matrix is not symmetric');\n    }\n\n    let a = value;\n    let dimension = a.rows;\n    let l = new Matrix(dimension, dimension);\n    let positiveDefinite = true;\n    let i, j, k;\n\n    for (j = 0; j < dimension; j++) {\n      let d = 0;\n      for (k = 0; k < j; k++) {\n        let s = 0;\n        for (i = 0; i < k; i++) {\n          s += l.get(k, i) * l.get(j, i);\n        }\n        s = (a.get(j, k) - s) / l.get(k, k);\n        l.set(j, k, s);\n        d = d + s * s;\n      }\n\n      d = a.get(j, j) - d;\n\n      positiveDefinite &&= d > 0;\n      l.set(j, j, Math.sqrt(Math.max(d, 0)));\n      for (k = j + 1; k < dimension; k++) {\n        l.set(j, k, 0);\n      }\n    }\n\n    this.L = l;\n    this.positiveDefinite = positiveDefinite;\n  }\n\n  isPositiveDefinite() {\n    return this.positiveDefinite;\n  }\n\n  solve(value) {\n    value = WrapperMatrix2D.checkMatrix(value);\n\n    let l = this.L;\n    let dimension = l.rows;\n\n    if (value.rows !== dimension) {\n      throw new Error('Matrix dimensions do not match');\n    }\n    if (this.isPositiveDefinite() === false) {\n      throw new Error('Matrix is not positive definite');\n    }\n\n    let count = value.columns;\n    let B = value.clone();\n    let i, j, k;\n\n    for (k = 0; k < dimension; k++) {\n      for (j = 0; j < count; j++) {\n        for (i = 0; i < k; i++) {\n          B.set(k, j, B.get(k, j) - B.get(i, j) * l.get(k, i));\n        }\n        B.set(k, j, B.get(k, j) / l.get(k, k));\n      }\n    }\n\n    for (k = dimension - 1; k >= 0; k--) {\n      for (j = 0; j < count; j++) {\n        for (i = k + 1; i < dimension; i++) {\n          B.set(k, j, B.get(k, j) - B.get(i, j) * l.get(i, k));\n        }\n        B.set(k, j, B.get(k, j) / l.get(k, k));\n      }\n    }\n\n    return B;\n  }\n\n  get lowerTriangularMatrix() {\n    return this.L;\n  }\n}\n\nclass nipals {\n  constructor(X, options = {}) {\n    X = WrapperMatrix2D.checkMatrix(X);\n    let { Y } = options;\n    const {\n      scaleScores = false,\n      maxIterations = 1000,\n      terminationCriteria = 1e-10,\n    } = options;\n\n    let u;\n    if (Y) {\n      if (isAnyArray(Y) && typeof Y[0] === 'number') {\n        Y = Matrix.columnVector(Y);\n      } else {\n        Y = WrapperMatrix2D.checkMatrix(Y);\n      }\n      if (Y.rows !== X.rows) {\n        throw new Error('Y should have the same number of rows as X');\n      }\n      u = Y.getColumnVector(0);\n    } else {\n      u = X.getColumnVector(0);\n    }\n\n    let diff = 1;\n    let t, q, w, tOld;\n\n    for (\n      let counter = 0;\n      counter < maxIterations && diff > terminationCriteria;\n      counter++\n    ) {\n      w = X.transpose().mmul(u).div(u.transpose().mmul(u).get(0, 0));\n      w = w.div(w.norm());\n\n      t = X.mmul(w).div(w.transpose().mmul(w).get(0, 0));\n\n      if (counter > 0) {\n        diff = t.clone().sub(tOld).pow(2).sum();\n      }\n      tOld = t.clone();\n\n      if (Y) {\n        q = Y.transpose().mmul(t).div(t.transpose().mmul(t).get(0, 0));\n        q = q.div(q.norm());\n\n        u = Y.mmul(q).div(q.transpose().mmul(q).get(0, 0));\n      } else {\n        u = t;\n      }\n    }\n\n    if (Y) {\n      let p = X.transpose().mmul(t).div(t.transpose().mmul(t).get(0, 0));\n      p = p.div(p.norm());\n      let xResidual = X.clone().sub(t.clone().mmul(p.transpose()));\n      let residual = u.transpose().mmul(t).div(t.transpose().mmul(t).get(0, 0));\n      let yResidual = Y.clone().sub(\n        t.clone().mulS(residual.get(0, 0)).mmul(q.transpose()),\n      );\n\n      this.t = t;\n      this.p = p.transpose();\n      this.w = w.transpose();\n      this.q = q;\n      this.u = u;\n      this.s = t.transpose().mmul(t);\n      this.xResidual = xResidual;\n      this.yResidual = yResidual;\n      this.betas = residual;\n    } else {\n      this.w = w.transpose();\n      this.s = t.transpose().mmul(t).sqrt();\n      if (scaleScores) {\n        this.t = t.clone().div(this.s.get(0, 0));\n      } else {\n        this.t = t;\n      }\n      this.xResidual = X.sub(t.mmul(w.transpose()));\n    }\n  }\n}\n\nexports.AbstractMatrix = AbstractMatrix;\nexports.CHO = CholeskyDecomposition;\nexports.CholeskyDecomposition = CholeskyDecomposition;\nexports.DistanceMatrix = DistanceMatrix;\nexports.EVD = EigenvalueDecomposition;\nexports.EigenvalueDecomposition = EigenvalueDecomposition;\nexports.LU = LuDecomposition;\nexports.LuDecomposition = LuDecomposition;\nexports.Matrix = Matrix;\nexports.MatrixColumnSelectionView = MatrixColumnSelectionView;\nexports.MatrixColumnView = MatrixColumnView;\nexports.MatrixFlipColumnView = MatrixFlipColumnView;\nexports.MatrixFlipRowView = MatrixFlipRowView;\nexports.MatrixRowSelectionView = MatrixRowSelectionView;\nexports.MatrixRowView = MatrixRowView;\nexports.MatrixSelectionView = MatrixSelectionView;\nexports.MatrixSubView = MatrixSubView;\nexports.MatrixTransposeView = MatrixTransposeView;\nexports.NIPALS = nipals;\nexports.Nipals = nipals;\nexports.QR = QrDecomposition;\nexports.QrDecomposition = QrDecomposition;\nexports.SVD = SingularValueDecomposition;\nexports.SingularValueDecomposition = SingularValueDecomposition;\nexports.SymmetricMatrix = SymmetricMatrix;\nexports.WrapperMatrix1D = WrapperMatrix1D;\nexports.WrapperMatrix2D = WrapperMatrix2D;\nexports.correlation = correlation;\nexports.covariance = covariance;\nexports.default = Matrix;\nexports.determinant = determinant;\nexports.inverse = inverse;\nexports.linearDependencies = linearDependencies;\nexports.pseudoInverse = pseudoInverse;\nexports.solve = solve;\nexports.wrap = wrap;\n//# sourceMappingURL=matrix.js.map\n","import * as matrix from './matrix.js';\n\nexport const AbstractMatrix = matrix.AbstractMatrix;\nexport const CHO = matrix.CHO;\nexport const CholeskyDecomposition = matrix.CholeskyDecomposition;\nexport const DistanceMatrix = matrix.DistanceMatrix;\nexport const EVD = matrix.EVD;\nexport const EigenvalueDecomposition = matrix.EigenvalueDecomposition;\nexport const LU = matrix.LU;\nexport const LuDecomposition = matrix.LuDecomposition;\nexport const Matrix = matrix.Matrix;\nexport const MatrixColumnSelectionView = matrix.MatrixColumnSelectionView;\nexport const MatrixColumnView = matrix.MatrixColumnView;\nexport const MatrixFlipColumnView = matrix.MatrixFlipColumnView;\nexport const MatrixFlipRowView = matrix.MatrixFlipRowView;\nexport const MatrixRowSelectionView = matrix.MatrixRowSelectionView;\nexport const MatrixRowView = matrix.MatrixRowView;\nexport const MatrixSelectionView = matrix.MatrixSelectionView;\nexport const MatrixSubView = matrix.MatrixSubView;\nexport const MatrixTransposeView = matrix.MatrixTransposeView;\nexport const NIPALS = matrix.NIPALS;\nexport const Nipals = matrix.Nipals;\nexport const QR = matrix.QR;\nexport const QrDecomposition = matrix.QrDecomposition;\nexport const SVD = matrix.SVD;\nexport const SingularValueDecomposition = matrix.SingularValueDecomposition;\nexport const SymmetricMatrix = matrix.SymmetricMatrix;\nexport const WrapperMatrix1D = matrix.WrapperMatrix1D;\nexport const WrapperMatrix2D = matrix.WrapperMatrix2D;\nexport const correlation = matrix.correlation;\nexport const covariance = matrix.covariance;\nexport default matrix.default.Matrix ? matrix.default.Matrix : matrix.Matrix;\nexport const determinant = matrix.determinant;\nexport const inverse = matrix.inverse;\nexport const linearDependencies = matrix.linearDependencies;\nexport const pseudoInverse = matrix.pseudoInverse;\nexport const solve = matrix.solve;\nexport const wrap = matrix.wrap;\n","import { xCheck } from \"./xCheck.js\";\nimport { xGetFromToIndex } from \"./xGetFromToIndex.js\";\n/**\n * Computes the maximal value of an array of values\n * @param array - array of numbers\n * @param options - options\n */\nexport function xMaxValue(array, options = {}) {\n    xCheck(array);\n    const { fromIndex, toIndex } = xGetFromToIndex(array, options);\n    let maxValue = array[fromIndex];\n    for (let i = fromIndex + 1; i <= toIndex; i++) {\n        if (array[i] > maxValue) {\n            maxValue = array[i];\n        }\n    }\n    return maxValue;\n}\n//# sourceMappingURL=xMaxValue.js.map","import { xCheck } from \"./xCheck.js\";\nimport { xGetFromToIndex } from \"./xGetFromToIndex.js\";\n/**\n * Computes the minimal value of an array of values.\n * @param array - array of numbers\n * @param options - options\n */\nexport function xMinValue(array, options = {}) {\n    xCheck(array);\n    const { fromIndex, toIndex } = xGetFromToIndex(array, options);\n    let minValue = array[fromIndex];\n    for (let i = fromIndex + 1; i <= toIndex; i++) {\n        if (array[i] < minValue) {\n            minValue = array[i];\n        }\n    }\n    return minValue;\n}\n//# sourceMappingURL=xMinValue.js.map","/**\n * This function calculate the norm of a vector.\n * @example xNorm([3, 4]) -> 5\n * @param array - array\n * @returns - calculated norm\n */\nexport function xNorm(array) {\n    let result = 0;\n    for (const element of array) {\n        result += element ** 2;\n    }\n    return Math.sqrt(result);\n}\n//# sourceMappingURL=xNorm.js.map","export const GAUSSIAN_EXP_FACTOR = -4 * Math.LN2;\nexport const ROOT_PI_OVER_LN2 = Math.sqrt(Math.PI / Math.LN2);\nexport const ROOT_THREE = Math.sqrt(3);\nexport const ROOT_2LN2 = Math.sqrt(2 * Math.LN2);\nexport const ROOT_2LN2_MINUS_ONE = Math.sqrt(2 * Math.LN2) - 1;\n//# sourceMappingURL=constants.js.map","import { ROOT_2LN2, GAUSSIAN_EXP_FACTOR, ROOT_PI_OVER_LN2, } from '../../../util/constants';\nimport erfinv from '../../../util/erfinv';\nexport class Gaussian {\n    constructor(options = {}) {\n        const { fwhm = 500, sd } = options;\n        this.fwhm = sd ? gaussianWidthToFWHM(2 * sd) : fwhm;\n    }\n    fwhmToWidth(fwhm = this.fwhm) {\n        return gaussianFwhmToWidth(fwhm);\n    }\n    widthToFWHM(width) {\n        return gaussianWidthToFWHM(width);\n    }\n    fct(x) {\n        return gaussianFct(x, this.fwhm);\n    }\n    getArea(height = calculateGaussianHeight({ fwhm: this.fwhm })) {\n        return getGaussianArea({ fwhm: this.fwhm, height });\n    }\n    getFactor(area) {\n        return getGaussianFactor(area);\n    }\n    getData(options = {}) {\n        return getGaussianData(this, options);\n    }\n    calculateHeight(area = 1) {\n        return calculateGaussianHeight({ fwhm: this.fwhm, area });\n    }\n    getParameters() {\n        return ['fwhm'];\n    }\n}\nexport function calculateGaussianHeight(options) {\n    let { fwhm = 500, area = 1, sd } = options;\n    if (sd)\n        fwhm = gaussianWidthToFWHM(2 * sd);\n    return (2 * area) / ROOT_PI_OVER_LN2 / fwhm;\n}\n/**\n * Calculate the height of the gaussian function of a specific width (fwhm) at a speicifc\n * x position (the gaussian is centered on x=0)\n * @param x\n * @param fwhm\n * @returns y\n */\nexport function gaussianFct(x, fwhm) {\n    return Math.exp(GAUSSIAN_EXP_FACTOR * Math.pow(x / fwhm, 2));\n}\nexport function gaussianWidthToFWHM(width) {\n    return width * ROOT_2LN2;\n}\nexport function gaussianFwhmToWidth(fwhm) {\n    return fwhm / ROOT_2LN2;\n}\nexport function getGaussianArea(options) {\n    let { fwhm = 500, sd, height = 1 } = options;\n    if (sd)\n        fwhm = gaussianWidthToFWHM(2 * sd);\n    return (height * ROOT_PI_OVER_LN2 * fwhm) / 2;\n}\nexport function getGaussianFactor(area = 0.9999) {\n    return Math.sqrt(2) * erfinv(area);\n}\nexport function getGaussianData(shape = {}, options = {}) {\n    let { fwhm = 500, sd } = shape;\n    if (sd)\n        fwhm = gaussianWidthToFWHM(2 * sd);\n    let { length, factor = getGaussianFactor(), height = calculateGaussianHeight({ fwhm }), } = options;\n    if (!length) {\n        length = Math.min(Math.ceil(fwhm * factor), Math.pow(2, 25) - 1);\n        if (length % 2 === 0)\n            length++;\n    }\n    const center = (length - 1) / 2;\n    const data = new Float64Array(length);\n    for (let i = 0; i <= center; i++) {\n        data[i] = gaussianFct(i - center, fwhm) * height;\n        data[length - 1 - i] = data[i];\n    }\n    return data;\n}\n//# sourceMappingURL=Gaussian.js.map","// https://en.wikipedia.org/wiki/Error_function#Inverse_functions\n// This code yields to a good approximation\n// If needed a better implementation using polynomial can be found on https://en.wikipedia.org/wiki/Error_function#Inverse_functions\nexport default function erfinv(x) {\n    let a = 0.147;\n    if (x === 0)\n        return 0;\n    let ln1MinusXSqrd = Math.log(1 - x * x);\n    let lnEtcBy2Plus2 = ln1MinusXSqrd / 2 + 2 / (Math.PI * a);\n    let firstSqrt = Math.sqrt(lnEtcBy2Plus2 ** 2 - ln1MinusXSqrd / a);\n    let secondSqrt = Math.sqrt(firstSqrt - lnEtcBy2Plus2);\n    return secondSqrt * (x > 0 ? 1 : -1);\n}\n//# sourceMappingURL=erfinv.js.map","import { ROOT_THREE } from '../../../util/constants';\nexport class Lorentzian {\n    constructor(options = {}) {\n        const { fwhm = 500 } = options;\n        this.fwhm = fwhm;\n    }\n    fwhmToWidth(fwhm = this.fwhm) {\n        return lorentzianFwhmToWidth(fwhm);\n    }\n    widthToFWHM(width) {\n        return lorentzianWidthToFWHM(width);\n    }\n    fct(x) {\n        return lorentzianFct(x, this.fwhm);\n    }\n    getArea(height = 1) {\n        return getLorentzianArea({ fwhm: this.fwhm, height });\n    }\n    getFactor(area) {\n        return getLorentzianFactor(area);\n    }\n    getData(options = {}) {\n        return getLorentzianData(this, options);\n    }\n    calculateHeight(area = 1) {\n        return calculateLorentzianHeight({ fwhm: this.fwhm, area });\n    }\n    getParameters() {\n        return ['fwhm'];\n    }\n}\nexport const calculateLorentzianHeight = ({ fwhm = 1, area = 1 }) => {\n    return (2 * area) / Math.PI / fwhm;\n};\nexport const getLorentzianArea = (options) => {\n    const { fwhm = 500, height = 1 } = options;\n    return (height * Math.PI * fwhm) / 2;\n};\nexport const lorentzianFct = (x, fwhm) => {\n    return fwhm ** 2 / (4 * x ** 2 + fwhm ** 2);\n};\nexport const lorentzianWidthToFWHM = (width) => {\n    return width * ROOT_THREE;\n};\nexport const lorentzianFwhmToWidth = (fwhm) => {\n    return fwhm / ROOT_THREE;\n};\nexport const getLorentzianFactor = (area = 0.9999) => {\n    if (area >= 1) {\n        throw new Error('area should be (0 - 1)');\n    }\n    const halfResidual = (1 - area) * 0.5;\n    const quantileFunction = (p) => Math.tan(Math.PI * (p - 0.5));\n    return ((quantileFunction(1 - halfResidual) - quantileFunction(halfResidual)) / 2);\n};\nexport const getLorentzianData = (shape = {}, options = {}) => {\n    let { fwhm = 500 } = shape;\n    let { length, factor = getLorentzianFactor(), height = calculateLorentzianHeight({ fwhm, area: 1 }), } = options;\n    if (!length) {\n        length = Math.min(Math.ceil(fwhm * factor), Math.pow(2, 25) - 1);\n        if (length % 2 === 0)\n            length++;\n    }\n    const center = (length - 1) / 2;\n    const data = new Float64Array(length);\n    for (let i = 0; i <= center; i++) {\n        data[i] = lorentzianFct(i - center, fwhm) * height;\n        data[length - 1 - i] = data[i];\n    }\n    return data;\n};\n//# sourceMappingURL=Lorentzian.js.map","import { calculateLorentzianHeight, getLorentzianFactor, lorentzianFwhmToWidth, lorentzianWidthToFWHM, } from '../lorentzian/Lorentzian';\nexport class LorentzianDispersive {\n    constructor(options = {}) {\n        const { fwhm = 500 } = options;\n        this.fwhm = fwhm;\n    }\n    fwhmToWidth(fwhm = this.fwhm) {\n        return lorentzianFwhmToWidth(fwhm);\n    }\n    widthToFWHM(width) {\n        return lorentzianWidthToFWHM(width);\n    }\n    fct(x) {\n        return lorentzianDispersiveFct(x, this.fwhm);\n    }\n    //eslint-disable-next-line\n    getArea(_height) {\n        return 0;\n    }\n    getFactor(area) {\n        return getLorentzianFactor(area);\n    }\n    getData(options = {}) {\n        return getLorentzianDispersiveData(this, options);\n    }\n    calculateHeight(area = 1) {\n        return calculateLorentzianHeight({ fwhm: this.fwhm, area });\n    }\n    getParameters() {\n        return ['fwhm'];\n    }\n}\nexport const lorentzianDispersiveFct = (x, fwhm) => {\n    return (2 * fwhm * x) / (4 * x ** 2 + fwhm ** 2);\n};\nexport const getLorentzianDispersiveData = (shape = {}, options = {}) => {\n    let { fwhm = 500 } = shape;\n    let { length, factor = getLorentzianFactor(), height = calculateLorentzianHeight({ fwhm, area: 1 }), } = options;\n    if (!length) {\n        length = Math.min(Math.ceil(fwhm * factor), Math.pow(2, 25) - 1);\n        if (length % 2 === 0)\n            length++;\n    }\n    const center = (length - 1) / 2;\n    const data = new Float64Array(length);\n    for (let i = 0; i <= center; i++) {\n        data[i] = lorentzianDispersiveFct(i - center, fwhm) * height;\n        data[length - 1 - i] = -data[i];\n    }\n    return data;\n};\n//# sourceMappingURL=LorentzianDispersive.js.map","import { GAUSSIAN_EXP_FACTOR, ROOT_2LN2_MINUS_ONE, ROOT_PI_OVER_LN2, } from '../../../util/constants';\nimport { gaussianFct, getGaussianFactor } from '../gaussian/Gaussian';\nimport { lorentzianFct, getLorentzianFactor } from '../lorentzian/Lorentzian';\nexport class PseudoVoigt {\n    constructor(options = {}) {\n        const { fwhm = 500, mu = 0.5 } = options;\n        this.mu = mu;\n        this.fwhm = fwhm;\n    }\n    fwhmToWidth(fwhm = this.fwhm, mu = this.mu) {\n        return pseudoVoigtFwhmToWidth(fwhm, mu);\n    }\n    widthToFWHM(width, mu = this.mu) {\n        return pseudoVoigtWidthToFWHM(width, mu);\n    }\n    fct(x) {\n        return pseudoVoigtFct(x, this.fwhm, this.mu);\n    }\n    getArea(height = 1) {\n        return getPseudoVoigtArea({ fwhm: this.fwhm, height, mu: this.mu });\n    }\n    getFactor(area) {\n        return getPseudoVoigtFactor(area);\n    }\n    getData(options = {}) {\n        const { length, factor, height = calculatePseudoVoigtHeight({\n            fwhm: this.fwhm,\n            mu: this.mu,\n            area: 1,\n        }), } = options;\n        return getPseudoVoigtData(this, { factor, length, height });\n    }\n    calculateHeight(area = 1) {\n        return calculatePseudoVoigtHeight({ fwhm: this.fwhm, mu: this.mu, area });\n    }\n    getParameters() {\n        return ['fwhm', 'mu'];\n    }\n}\nexport const calculatePseudoVoigtHeight = (options = {}) => {\n    let { fwhm = 1, mu = 0.5, area = 1 } = options;\n    return (2 * area) / (fwhm * (mu * ROOT_PI_OVER_LN2 + (1 - mu) * Math.PI));\n};\nexport const pseudoVoigtFct = (x, fwhm, mu) => {\n    return (1 - mu) * lorentzianFct(x, fwhm) + mu * gaussianFct(x, fwhm);\n};\nexport const pseudoVoigtWidthToFWHM = (width, mu = 0.5) => {\n    return width * (mu * ROOT_2LN2_MINUS_ONE + 1);\n};\nexport const pseudoVoigtFwhmToWidth = (fwhm, mu = 0.5) => {\n    return fwhm / (mu * ROOT_2LN2_MINUS_ONE + 1);\n};\nexport const getPseudoVoigtArea = (options) => {\n    const { fwhm = 500, height = 1, mu = 0.5 } = options;\n    return (fwhm * height * (mu * ROOT_PI_OVER_LN2 + (1 - mu) * Math.PI)) / 2;\n};\nexport const getPseudoVoigtFactor = (area = 0.9999, mu = 0.5) => {\n    return mu < 1 ? getLorentzianFactor(area) : getGaussianFactor(area);\n};\nexport const getPseudoVoigtData = (shape = {}, options = {}) => {\n    let { fwhm = 500, mu = 0.5 } = shape;\n    let { length, factor = getPseudoVoigtFactor(0.999, mu), height = calculatePseudoVoigtHeight({ fwhm, mu, area: 1 }), } = options;\n    if (!height) {\n        height =\n            1 /\n                ((mu / Math.sqrt(-GAUSSIAN_EXP_FACTOR / Math.PI)) * fwhm +\n                    ((1 - mu) * fwhm * Math.PI) / 2);\n    }\n    if (!length) {\n        length = Math.min(Math.ceil(fwhm * factor), Math.pow(2, 25) - 1);\n        if (length % 2 === 0)\n            length++;\n    }\n    const center = (length - 1) / 2;\n    const data = new Float64Array(length);\n    for (let i = 0; i <= center; i++) {\n        data[i] = pseudoVoigtFct(i - center, fwhm, mu) * height;\n        data[length - 1 - i] = data[i];\n    }\n    return data;\n};\n//# sourceMappingURL=PseudoVoigt.js.map","import { ROOT_THREE } from '../../../util/constants';\n/**\n * This shape is a linear combination of rational function (n|n+2), for n = 0 (lorentzian function) and n = 2\n * the parameter that combines those two functions is `gamma` and it is called the kurtosis parameter, it is an\n * implementation of generalized lorentzian shape published by Stanislav Sykora in the SMASH 2010. DOI:10.3247/SL3nmr10.006\n * @link http://www.ebyte.it/stan/Talk_ML_UserMeeting_SMASH_2010_GeneralizedLorentzian.html\n */\nexport class GeneralizedLorentzian {\n    constructor(options = {}) {\n        const { fwhm = 500, gamma = 0.5 } = options;\n        this.fwhm = fwhm;\n        this.gamma = gamma;\n    }\n    fwhmToWidth(fwhm = this.fwhm) {\n        return generalizedLorentzianFwhmToWidth(fwhm);\n    }\n    widthToFWHM(width) {\n        return generalizedLorentzianWidthToFWHM(width);\n    }\n    fct(x) {\n        return generalizedLorentzianFct(x, this.fwhm, this.gamma);\n    }\n    getArea(height = 1) {\n        return getGeneralizedLorentzianArea({\n            fwhm: this.fwhm,\n            height,\n            gamma: this.gamma,\n        });\n    }\n    getFactor(area) {\n        return getGeneralizedLorentzianFactor(area);\n    }\n    getData(options = {}) {\n        return getGeneralizedLorentzianData(this, options);\n    }\n    calculateHeight(area = 1) {\n        const { gamma, fwhm } = this;\n        return calculateGeneralizedLorentzianHeight({ fwhm, area, gamma });\n    }\n    getParameters() {\n        return ['fwhm', 'gamma'];\n    }\n}\nexport const calculateGeneralizedLorentzianHeight = ({ fwhm = 1, gamma = 1, area = 1, }) => {\n    return (area / fwhm / (3.14159 - 0.420894 * gamma)) * 2;\n};\n/**\n * expression of integral generated by Mathematica of the function\n */\nexport const getGeneralizedLorentzianArea = (options) => {\n    const { fwhm = 500, height = 1, gamma = 1 } = options;\n    return (height * fwhm * (3.14159 - 0.420894 * gamma)) / 2;\n};\nexport const generalizedLorentzianFct = (x, fwhm, gamma) => {\n    const u = ((2 * x) / fwhm) ** 2;\n    return (1 - gamma) / (1 + u) + (gamma * (1 + u / 2)) / (1 + u + u ** 2);\n};\nexport const generalizedLorentzianWidthToFWHM = (width) => {\n    return width * ROOT_THREE;\n};\nexport const generalizedLorentzianFwhmToWidth = (fwhm) => {\n    return fwhm / ROOT_THREE;\n};\nexport const getGeneralizedLorentzianFactor = (area = 0.9999) => {\n    if (area >= 1) {\n        throw new Error('area should be (0 - 1)');\n    }\n    const halfResidual = (1 - area) * 0.5;\n    const quantileFunction = (p) => Math.tan(Math.PI * (p - 0.5));\n    return ((quantileFunction(1 - halfResidual) - quantileFunction(halfResidual)) / 2);\n};\nexport const getGeneralizedLorentzianData = (shape = {}, options = {}) => {\n    let { fwhm = 500, gamma = 1 } = shape;\n    let { length, factor = getGeneralizedLorentzianFactor(), height = calculateGeneralizedLorentzianHeight({ fwhm, area: 1, gamma }), } = options;\n    if (!length) {\n        length = Math.min(Math.ceil(fwhm * factor), Math.pow(2, 25) - 1);\n        if (length % 2 === 0)\n            length++;\n    }\n    const center = (length - 1) / 2;\n    const data = new Float64Array(length);\n    for (let i = 0; i <= center; i++) {\n        data[i] = generalizedLorentzianFct(i - center, fwhm, gamma) * height;\n        data[length - 1 - i] = data[i];\n    }\n    return data;\n};\n//# sourceMappingURL=GeneralizedLorentzian.js.map","import { Gaussian } from './gaussian/Gaussian';\nimport { GeneralizedLorentzian } from './generalizedLorentzian/GeneralizedLorentzian';\nimport { Lorentzian } from './lorentzian/Lorentzian';\nimport { LorentzianDispersive } from './lorentzianDispersive/LorentzianDispersive';\nimport { PseudoVoigt } from './pseudoVoigt/PseudoVoigt';\n/**\n * Generate a instance of a specific kind of shape.\n */\nexport function getShape1D(shape) {\n    const { kind } = shape;\n    switch (kind) {\n        case 'gaussian':\n            return new Gaussian(shape);\n        case 'lorentzian':\n            return new Lorentzian(shape);\n        case 'pseudoVoigt':\n            return new PseudoVoigt(shape);\n        case 'lorentzianDispersive':\n            return new LorentzianDispersive(shape);\n        case 'generalizedLorentzian':\n            return new GeneralizedLorentzian(shape);\n        default: {\n            throw Error(`Unknown distribution ${kind}`);\n        }\n    }\n}\n//# sourceMappingURL=getShape1D.js.map","/**\n * Asserts that value is truthy.\n *\n * @param value - Value to check.\n * @param message - Optional error message to throw.\n */\nexport function assert(value, message) {\n    if (!value) {\n        throw new Error(message ? message : 'unreachable');\n    }\n}\n//# sourceMappingURL=assert.js.map","export const DefaultParameters = {\n    x: {\n        init: (peak) => peak.x,\n        min: (peak, peakShape) => peak.x - peakShape.fwhm * 2,\n        max: (peak, peakShape) => peak.x + peakShape.fwhm * 2,\n        gradientDifference: (peak, peakShape) => peakShape.fwhm * 2e-3,\n    },\n    y: {\n        init: (peak) => peak.y,\n        min: (peak) => (peak.y < 0 ? -1.1 : 0),\n        max: (peak) => (peak.y < 0 ? 0 : 1.1),\n        gradientDifference: () => 1e-3,\n    },\n    fwhm: {\n        init: (peak, peakShape) => peakShape.fwhm,\n        min: (peak, peakShape) => peakShape.fwhm * 0.25,\n        max: (peak, peakShape) => peakShape.fwhm * 4,\n        gradientDifference: (peak, peakShape) => peakShape.fwhm * 2e-3,\n    },\n    mu: {\n        init: (peak, peakShape) => peakShape.mu,\n        min: () => 0,\n        max: () => 1,\n        gradientDifference: () => 0.01,\n    },\n    gamma: {\n        init: (peak, peakShape) => peakShape.gamma || 0.5,\n        min: () => -1,\n        max: () => 2,\n        gradientDifference: () => 0.01,\n    },\n};\n//# sourceMappingURL=DefaultParameters.js.map","import { getShape1D } from 'ml-peak-shape-generator';\nimport { assert } from \"../assert.js\";\nimport { DefaultParameters } from \"./DefaultParameters.js\";\nconst properties = ['init', 'min', 'max', 'gradientDifference'];\n/**\n * Return an array of internalPeaks that contains the exact init, min, max values based on the options\n * @param peaks\n * @param minMaxY\n * @param options\n * @returns\n */\nexport function getInternalPeaks(peaks, yScale, options = {}) {\n    let index = 0;\n    const internalPeaks = [];\n    const normalizedPeaks = peaks.map((peak) => {\n        return {\n            ...peak,\n            y: peak.y / yScale,\n        };\n    });\n    for (const peak of normalizedPeaks) {\n        const { id, shape = options.shape ? options.shape : { kind: 'gaussian' } } = peak;\n        const shapeFct = getShape1D(shape);\n        const parameters = ['x', 'y', ...shapeFct.getParameters()];\n        const propertiesValues = {\n            min: [],\n            max: [],\n            init: [],\n            gradientDifference: [],\n        };\n        for (const parameter of parameters) {\n            for (const property of properties) {\n                // check if the property is specified in the peak\n                let propertyValue = peak?.parameters?.[parameter]?.[property];\n                if (propertyValue !== undefined) {\n                    propertyValue = getNormalizedValue(propertyValue, parameter, property, yScale);\n                    propertiesValues[property].push(propertyValue);\n                    continue;\n                }\n                // check if there are some global option, it could be a number or a callback\n                let generalParameterValue = options?.parameters?.[parameter]?.[property];\n                if (generalParameterValue !== undefined) {\n                    if (typeof generalParameterValue === 'number') {\n                        generalParameterValue = getNormalizedValue(generalParameterValue, parameter, property, yScale);\n                        propertiesValues[property].push(generalParameterValue);\n                        continue;\n                    }\n                    else {\n                        let value = generalParameterValue(peak);\n                        value = getNormalizedValue(value, parameter, property, yScale);\n                        propertiesValues[property].push(value);\n                        continue;\n                    }\n                }\n                // we just need to take the default parameters\n                assert(DefaultParameters[parameter], `No default parameter for ${parameter}`);\n                const defaultParameterValues = DefaultParameters[parameter][property];\n                //@ts-expect-error should never happen\n                propertiesValues[property].push(defaultParameterValues(peak, shapeFct));\n            }\n        }\n        const fromIndex = index;\n        const toIndex = fromIndex + parameters.length - 1;\n        index += toIndex - fromIndex + 1;\n        internalPeaks.push({\n            id,\n            shape,\n            shapeFct,\n            parameters,\n            propertiesValues,\n            fromIndex,\n            toIndex,\n        });\n    }\n    return internalPeaks;\n}\nfunction getNormalizedValue(value, parameter, property, yScale) {\n    if (parameter === 'y') {\n        if (property === 'gradientDifference') {\n            return value;\n        }\n        else {\n            return value / yScale;\n        }\n    }\n    return value;\n}\n//# sourceMappingURL=getInternalPeaks.js.map","import { isAnyArray } from 'is-any-array';\nexport default function checkOptions(data, options) {\n    const { timeout, initialValues, weights = 1, damping = 1e-2, dampingStepUp = 11, dampingStepDown = 9, maxIterations = 100, errorTolerance = 1e-7, centralDifference = false, gradientDifference = 10e-2, improvementThreshold = 1e-3, } = options;\n    let { minValues, maxValues } = options;\n    if (damping <= 0) {\n        throw new Error('The damping option must be a positive number');\n    }\n    else if (!data.x || !data.y) {\n        throw new Error('The data parameter must have x and y elements');\n    }\n    else if (!isAnyArray(data.x) ||\n        data.x.length < 2 ||\n        !isAnyArray(data.y) ||\n        data.y.length < 2) {\n        throw new Error('The data parameter elements must be an array with more than 2 points');\n    }\n    else if (data.x.length !== data.y.length) {\n        throw new Error('The data parameter elements must have the same size');\n    }\n    if (!(initialValues && initialValues.length > 0)) {\n        throw new Error('The initialValues option is mandatory and must be an array');\n    }\n    const parameters = Array.from(initialValues);\n    const parLen = parameters.length;\n    maxValues = maxValues || new Array(parLen).fill(Number.MAX_SAFE_INTEGER);\n    minValues = minValues || new Array(parLen).fill(Number.MIN_SAFE_INTEGER);\n    if (maxValues.length !== minValues.length) {\n        throw new Error('minValues and maxValues must be the same size');\n    }\n    const gradientDifferenceArray = getGradientDifferenceArray(gradientDifference, parameters);\n    const filler = getFiller(weights, data.x.length);\n    const checkTimeout = getCheckTimeout(timeout);\n    const weightSquare = Array.from({ length: data.x.length }, (_, i) => filler(i));\n    return {\n        checkTimeout,\n        minValues,\n        maxValues,\n        parameters,\n        weightSquare,\n        damping,\n        dampingStepUp,\n        dampingStepDown,\n        maxIterations,\n        errorTolerance,\n        centralDifference,\n        gradientDifference: gradientDifferenceArray,\n        improvementThreshold,\n    };\n}\nfunction getGradientDifferenceArray(gradientDifference, parameters) {\n    if (typeof gradientDifference === 'number') {\n        return new Array(parameters.length).fill(gradientDifference);\n    }\n    else if (isAnyArray(gradientDifference)) {\n        const parLen = parameters.length;\n        if (gradientDifference.length !== parLen) {\n            return new Array(parLen).fill(gradientDifference[0]);\n        }\n        return Array.from(gradientDifference);\n    }\n    throw new Error('gradientDifference should be a number or array with length equal to the number of parameters');\n}\nfunction getFiller(weights, dataLength) {\n    if (typeof weights === 'number') {\n        const value = 1 / weights ** 2;\n        return () => value;\n    }\n    else if (isAnyArray(weights)) {\n        if (weights.length < dataLength) {\n            const value = 1 / weights[0] ** 2;\n            return () => value;\n        }\n        return (i) => 1 / weights[i] ** 2;\n    }\n    throw new Error('weights should be a number or array with length equal to the number of data points');\n}\nfunction getCheckTimeout(timeout) {\n    if (timeout !== undefined) {\n        if (typeof timeout !== 'number') {\n            throw new Error('timeout should be a number');\n        }\n        const endTime = Date.now() + timeout * 1000;\n        return () => Date.now() > endTime;\n    }\n    else {\n        return () => false;\n    }\n}\n//# sourceMappingURL=check_options.js.map","/**\n * the sum of the weighted squares of the errors (or weighted residuals) between the data.y\n * and the curve-fit function.\n *\n * @param data - Array of points to fit in the format [x1, x2, ... ], [y1, y2, ... ]\n * @param parameters - Array of current parameter values\n * @param parameterizedFunction - The parameters and returns a function with the independent variable as a parameter\n * @param weightSquare - Square of weights (must be same length as data.x)\n */\nexport default function errorCalculation(data, parameters, parameterizedFunction, weightSquare) {\n    let error = 0;\n    const func = parameterizedFunction(parameters);\n    for (let i = 0; i < data.x.length; i++) {\n        error += (data.y[i] - func(data.x[i])) ** 2 / weightSquare[i];\n    }\n    return error;\n}\n//# sourceMappingURL=error_calculation.js.map","import { inverse, Matrix } from 'ml-matrix';\nimport gradientFunction from \"./gradient_function.js\";\n/**\n * Matrix function over the samples\n *\n * @param data - Array of points to fit in the format [x1, x2, ... ], [y1, y2, ... ]\n * @param evaluatedData - Array of previous evaluated function values\n */\nfunction matrixFunction(data, evaluatedData) {\n    const m = data.x.length;\n    const ans = new Matrix(m, 1);\n    for (let point = 0; point < m; point++) {\n        ans.set(point, 0, data.y[point] - evaluatedData[point]);\n    }\n    return ans;\n}\n/**\n * Iteration for Levenberg-Marquardt\n *\n * @param data - Array of points to fit in the format [x1, x2, ... ], [y1, y2, ... ]\n * @param params - Array of previous parameter values\n * @param damping - Levenberg-Marquardt parameter\n * @param gradientDifference - The step size to approximate the jacobian matrix\n * @param centralDifference - If true the jacobian matrix is approximated by central differences otherwise by forward differences\n * @param parameterizedFunction - The parameters and returns a function with the independent variable as a parameter\n * @param weights - scale the gradient and residual error by weights\n */\nexport default function step(data, params, damping, gradientDifference, parameterizedFunction, centralDifference, weights) {\n    const identity = Matrix.eye(params.length, params.length, damping);\n    const func = parameterizedFunction(params);\n    const evaluatedData = new Float64Array(data.x.length);\n    for (let i = 0; i < data.x.length; i++) {\n        evaluatedData[i] = func(data.x[i]);\n    }\n    const gradientFunc = gradientFunction(data, evaluatedData, params, gradientDifference, parameterizedFunction, centralDifference);\n    const residualError = matrixFunction(data, evaluatedData);\n    const inverseMatrix = inverse(identity.add(gradientFunc.mmul(gradientFunc.transpose().scale('row', { scale: weights }))));\n    const jacobianWeightResidualError = gradientFunc.mmul(residualError.scale('row', { scale: weights }));\n    const perturbations = inverseMatrix.mmul(jacobianWeightResidualError);\n    return {\n        perturbations,\n        jacobianWeightResidualError,\n    };\n}\n//# sourceMappingURL=step.js.map","import { Matrix } from 'ml-matrix';\n/**\n * Difference of the matrix function over the parameters\n * @param data Array of points to fit in the format [x1, x2, ... ], [y1, y2, ... ]\n * @param evaluatedData - Array of previous evaluated function values\n * @param params - Array of previous parameter values\n * @param gradientDifference - The step size to approximate the jacobian matrix\n * @param centralDifference - If true the jacobian matrix is approximated by central differences otherwise by forward differences\n * @param paramFunction - The parameters and returns a function with the independent variable as a parameter\n */\nexport default function gradientFunction(data, evaluatedData, params, gradientDifference, paramFunction, centralDifference) {\n    const nbParams = params.length;\n    const nbPoints = data.x.length;\n    const ans = Matrix.zeros(nbParams, nbPoints);\n    let rowIndex = 0;\n    for (let param = 0; param < nbParams; param++) {\n        if (gradientDifference[param] === 0)\n            continue;\n        let delta = gradientDifference[param];\n        let auxParams = params.slice();\n        auxParams[param] += delta;\n        const funcParam = paramFunction(auxParams);\n        if (!centralDifference) {\n            for (let point = 0; point < nbPoints; point++) {\n                ans.set(rowIndex, point, (evaluatedData[point] - funcParam(data.x[point])) / delta);\n            }\n        }\n        else {\n            auxParams = params.slice();\n            auxParams[param] -= delta;\n            delta *= 2;\n            const funcParam2 = paramFunction(auxParams);\n            for (let point = 0; point < nbPoints; point++) {\n                ans.set(rowIndex, point, (funcParam2(data.x[point]) - funcParam(data.x[point])) / delta);\n            }\n        }\n        rowIndex++;\n    }\n    return ans;\n}\n//# sourceMappingURL=gradient_function.js.map","import checkOptions from \"./check_options.js\";\nimport errorCalculation from \"./error_calculation.js\";\nimport step from \"./step.js\";\n/**\n * Curve fitting algorithm\n * @param data - Array of points to fit in the format [x1, x2, ... ], [y1, y2, ... ]\n * @param parameterizedFunction - Takes an array of parameters and returns a function with the independent variable as its sole argument\n * @param options - Options object\n */\nexport function levenbergMarquardt(data, parameterizedFunction, options) {\n    const checkedOptions = checkOptions(data, options);\n    const { checkTimeout, minValues, maxValues, parameters, weightSquare, dampingStepUp, dampingStepDown, maxIterations, errorTolerance, centralDifference, gradientDifference, improvementThreshold, } = checkedOptions;\n    let damping = checkedOptions.damping;\n    let error = errorCalculation(data, parameters, parameterizedFunction, weightSquare);\n    let optimalError = error;\n    let optimalParameters = parameters.slice();\n    let converged = error <= errorTolerance;\n    let iteration = 0;\n    for (; iteration < maxIterations && !converged; iteration++) {\n        const previousError = error;\n        const { perturbations, jacobianWeightResidualError } = step(data, parameters, damping, gradientDifference, parameterizedFunction, centralDifference, weightSquare);\n        for (let k = 0; k < parameters.length; k++) {\n            parameters[k] = Math.min(Math.max(minValues[k], parameters[k] - perturbations.get(k, 0)), maxValues[k]);\n        }\n        error = errorCalculation(data, parameters, parameterizedFunction, weightSquare);\n        if (isNaN(error))\n            break;\n        if (error < optimalError - errorTolerance) {\n            optimalError = error;\n            optimalParameters = parameters.slice();\n        }\n        const improvementMetric = (previousError - error) /\n            perturbations\n                .transpose()\n                .mmul(perturbations.mul(damping).add(jacobianWeightResidualError))\n                .get(0, 0);\n        if (improvementMetric > improvementThreshold) {\n            damping = Math.max(damping / dampingStepDown, 1e-7);\n        }\n        else {\n            damping = Math.min(damping * dampingStepUp, 1e7);\n        }\n        if (checkTimeout()) {\n            throw new Error(`The execution time is over to ${options.timeout} seconds`);\n        }\n        converged = error <= errorTolerance;\n    }\n    return {\n        parameterValues: optimalParameters,\n        parameterError: optimalError,\n        iterations: iteration,\n    };\n}\n//# sourceMappingURL=levenberg_marquardt.js.map","/**\n * Preparata, F. P., & Shamos, M. I. (2012). Computational geometry: an introduction. Springer Science & Business Media.\n * @param {Array} x - The array with x coordinates of the points.\n * @param {Array} y - The array with y coordinates of the points.\n * @return {Array} The indices of the points of anticlockwise lower convex hull\n * @private\n */\nexport default function antiLowerConvexHull(x, y) {\n  if (x.length !== y.length) {\n    throw new RangeError('X and Y vectors has different dimensions');\n  }\n\n  const nbPoints = x.length - 1;\n  if (nbPoints === 0) return [0];\n  if (nbPoints === 1) return [0, 1];\n\n  let currentPoint = 0;\n  let result = new Array(x.length).fill(true);\n  while (true) {\n    const a = currentPoint;\n    const b = moveOn(currentPoint, nbPoints, result);\n    const c = moveOn(moveOn(currentPoint, nbPoints, result), nbPoints, result);\n\n    const det =\n      x[c] * (y[a] - y[b]) + x[a] * (y[b] - y[c]) + x[b] * (y[c] - y[a]);\n\n    const leftTurn = det >= 0;\n\n    if (leftTurn) {\n      currentPoint = b;\n    } else {\n      result[b] = false;\n      currentPoint = moveBack(currentPoint, nbPoints, result);\n    }\n    if (c === nbPoints) break;\n  }\n\n  return result\n    .map((item, index) => (item === false ? false : index))\n    .filter((item) => item !== false);\n}\n\n/**\n * @param {number} currentPoint - The index of the current point to make the move\n * @param {number} nbPoints - The total number of points in the array\n * @param {Array} vector - The array with the points\n * @return {number} the index of the point after the move\n * @private\n */\n\nfunction moveBack(currentPoint, nbPoints, vector) {\n  let counter = currentPoint - 1;\n  while (vector[counter] === false) counter--;\n  return currentPoint === 0 ? nbPoints : counter;\n}\n\nfunction moveOn(currentPoint, nbPoints, vector) {\n  let counter = currentPoint + 1;\n  while (vector[counter] === false) counter++;\n  return currentPoint === nbPoints ? 0 : counter;\n}\n","import { xNorm, xMaxValue, xMinValue } from 'ml-spectra-processing';\n\nimport antiLowerConvexHull from './util/antiLowerConvexHull';\n\n/**\n * Performs a global optimization of required parameters\n * It will return an object containing:\n * - `minFunctionValue`: The minimum value found for the objetive function\n * - `optima`: Array of Array of values for all the variables where the function reach its minimum value\n * - `iterations`: Number of iterations performed in the process\n * - `finalState`: Internal state allowing to continue optimization (initialState)\n * @param {function} objectiveFunction Function to evaluate. It should accept an array of variables\n * @param {Array} lowerBoundaries Array containing for each variable the lower boundary\n * @param {Array} upperBoundaries Array containing for each variable the higher boundary\n * @param {Object} [options={}]\n * @param {number} [options.iterations] - Number of iterations.\n * @param {number} [options.epsilon] - Tolerance to choose best current value.\n * @param {number} [options.tolerance] - Minimum tollerance of the function.\n * @param {number} [options.tolerance2] - Minimum tollerance of the function.\n * @param {Object} [options.initialState={}}] - finalState of previous optimization.\n * @return {Object} {finalState, iterations, minFunctionValue}\n * */\n\nexport default function direct(\n  objectiveFunction,\n  lowerBoundaries,\n  upperBoundaries,\n  options = {},\n) {\n  const {\n    iterations = 50,\n    epsilon = 1e-4,\n    tolerance = 1e-16,\n    tolerance2 = 1e-12,\n    initialState = {},\n  } = options;\n\n  if (\n    objectiveFunction === undefined ||\n    lowerBoundaries === undefined ||\n    upperBoundaries === undefined\n  ) {\n    throw new RangeError('There is something undefined');\n  }\n\n  lowerBoundaries = new Float64Array(lowerBoundaries);\n  upperBoundaries = new Float64Array(upperBoundaries);\n\n  if (lowerBoundaries.length !== upperBoundaries.length) {\n    throw new Error(\n      'Lower bounds and Upper bounds for x are not of the same length',\n    );\n  }\n\n  //-------------------------------------------------------------------------\n  //                        STEP 1. Initialization\n  //-------------------------------------------------------------------------\n  let n = lowerBoundaries.length;\n  let diffBorders = upperBoundaries.map((x, i) => x - lowerBoundaries[i]);\n\n  let {\n    numberOfRectangles = 0,\n    totalIterations = 0,\n    unitaryCoordinates = [new Float64Array(n).fill(0.5)],\n    middlePoint = new Float64Array(n).map((value, index) => {\n      return (\n        lowerBoundaries[index] +\n        unitaryCoordinates[0][index] * diffBorders[index]\n      );\n    }),\n    bestCurrentValue = objectiveFunction(middlePoint),\n    fCalls = 1,\n    smallerDistance = 0,\n    edgeSizes = [new Float64Array(n).fill(0.5)],\n    diagonalDistances = [Math.sqrt(n * 0.5 ** 2)],\n    functionValues = [bestCurrentValue],\n    differentDistances = diagonalDistances,\n    smallerValuesByDistance = [bestCurrentValue],\n    choiceLimit = undefined,\n  } = initialState;\n  if (\n    initialState.originalCoordinates &&\n    initialState.originalCoordinates.length > 0\n  ) {\n    bestCurrentValue = xMinValue(functionValues);\n    choiceLimit =\n      epsilon * Math.abs(bestCurrentValue) > 1e-8\n        ? epsilon * Math.abs(bestCurrentValue)\n        : 1e-8;\n\n    smallerDistance = getMinIndex(\n      functionValues,\n      diagonalDistances,\n      choiceLimit,\n      bestCurrentValue,\n    );\n\n    unitaryCoordinates = initialState.originalCoordinates.slice();\n    for (let j = 0; j < unitaryCoordinates.length; j++) {\n      for (let i = 0; i < lowerBoundaries.length; i++) {\n        unitaryCoordinates[j][i] =\n          (unitaryCoordinates[j][i] - lowerBoundaries[i]) / diffBorders[i];\n      }\n    }\n  }\n\n  let iteration = 0;\n  //-------------------------------------------------------------------------\n  //                          Iteration loop\n  //-------------------------------------------------------------------------\n\n  while (iteration < iterations) {\n    //----------------------------------------------------------------------\n    //  STEP 2. Identify the set S of all potentially optimal rectangles\n    //----------------------------------------------------------------------\n\n    let S1 = [];\n    let idx = differentDistances.findIndex(\n      // eslint-disable-next-line no-loop-func\n      (e) => e === diagonalDistances[smallerDistance],\n    );\n    let counter = 0;\n    for (let i = idx; i < differentDistances.length; i++) {\n      for (let f = 0; f < functionValues.length; f++) {\n        if (\n          (functionValues[f] === smallerValuesByDistance[i]) &\n          (diagonalDistances[f] === differentDistances[i])\n        ) {\n          S1[counter++] = f;\n        }\n      }\n    }\n\n    let optimumValuesIndex, S3;\n    if (differentDistances.length - idx > 1) {\n      let a1 = diagonalDistances[smallerDistance];\n      let b1 = functionValues[smallerDistance];\n      let a2 = differentDistances[differentDistances.length - 1];\n      let b2 = smallerValuesByDistance[differentDistances.length - 1];\n      let slope = (b2 - b1) / (a2 - a1);\n      let constant = b1 - slope * a1;\n      let S2 = new Uint32Array(counter);\n      counter = 0;\n      for (let i = 0; i < S2.length; i++) {\n        let j = S1[i];\n        if (\n          functionValues[j] <=\n          slope * diagonalDistances[j] + constant + tolerance2\n        ) {\n          S2[counter++] = j;\n        }\n      }\n\n      let xHull = [];\n      let yHull = [];\n      for (let i = 0; i < counter; i++) {\n        xHull.push(diagonalDistances[S2[i]]);\n        yHull.push(functionValues[S2[i]]);\n      }\n\n      let lowerIndexHull = antiLowerConvexHull(xHull, yHull);\n\n      S3 = [];\n      for (let i = 0; i < lowerIndexHull.length; i++) {\n        S3.push(S2[lowerIndexHull[i]]);\n      }\n    } else {\n      S3 = S1.slice(0, counter);\n    }\n    optimumValuesIndex = S3;\n    //--------------------------------------------------------------\n    // STEPS 3,5: Select any rectangle j in S\n    //--------------------------------------------------------------\n    for (let k = 0; k < optimumValuesIndex.length; k++) {\n      let j = optimumValuesIndex[k];\n      let largerSide = xMaxValue(edgeSizes[j]);\n      let largeSidesIndex = new Uint32Array(edgeSizes[j].length);\n      counter = 0;\n      for (let i = 0; i < edgeSizes[j].length; i++) {\n        if (Math.abs(edgeSizes[j][i] - largerSide) < tolerance) {\n          largeSidesIndex[counter++] = i;\n        }\n      }\n      let delta = (2 * largerSide) / 3;\n      let bestFunctionValues = [];\n      for (let r = 0; r < counter; r++) {\n        let i = largeSidesIndex[r];\n        let firstMiddleCenter = unitaryCoordinates[j].slice();\n        let secondMiddleCenter = unitaryCoordinates[j].slice();\n        firstMiddleCenter[i] += delta;\n        secondMiddleCenter[i] -= delta;\n        let firstMiddleValue = new Float64Array(firstMiddleCenter.length);\n        let secondMiddleValue = new Float64Array(secondMiddleCenter.length);\n        for (let i = 0; i < firstMiddleCenter.length; i++) {\n          firstMiddleValue[i] =\n            lowerBoundaries[i] + firstMiddleCenter[i] * diffBorders[i];\n          secondMiddleValue[i] =\n            lowerBoundaries[i] + secondMiddleCenter[i] * diffBorders[i];\n        }\n        let firstMinValue = objectiveFunction(firstMiddleValue);\n        let secondMinValue = objectiveFunction(secondMiddleValue);\n        fCalls += 2;\n        bestFunctionValues.push({\n          minValue: Math.min(firstMinValue, secondMinValue),\n          index: r,\n        });\n        // [Math.min(firstMinValue, secondMinValue), r];\n        unitaryCoordinates.push(firstMiddleCenter, secondMiddleCenter);\n        functionValues.push(firstMinValue, secondMinValue);\n      }\n\n      let b = bestFunctionValues.sort((a, b) => a.minValue - b.minValue);\n      for (let r = 0; r < counter; r++) {\n        let u = largeSidesIndex[b[r].index];\n        let ix1 = numberOfRectangles + 2 * (b[r].index + 1) - 1;\n        let ix2 = numberOfRectangles + 2 * (b[r].index + 1);\n        edgeSizes[j][u] = delta / 2;\n        edgeSizes[ix1] = edgeSizes[j].slice();\n        edgeSizes[ix2] = edgeSizes[j].slice();\n        diagonalDistances[j] = xNorm(edgeSizes[j]);\n        diagonalDistances[ix1] = diagonalDistances[j];\n        diagonalDistances[ix2] = diagonalDistances[j];\n      }\n      numberOfRectangles += 2 * counter;\n    }\n\n    //--------------------------------------------------------------\n    //                  Update\n    //--------------------------------------------------------------\n\n    bestCurrentValue = xMinValue(functionValues);\n\n    choiceLimit =\n      epsilon * Math.abs(bestCurrentValue) > 1e-8\n        ? epsilon * Math.abs(bestCurrentValue)\n        : 1e-8;\n\n    smallerDistance = getMinIndex(\n      functionValues,\n      diagonalDistances,\n      choiceLimit,\n      bestCurrentValue,\n      iteration,\n    );\n\n    differentDistances = Array.from(new Set(diagonalDistances));\n    differentDistances = differentDistances.sort((a, b) => a - b);\n\n    smallerValuesByDistance = [];\n    for (let i = 0; i < differentDistances.length; i++) {\n      let minIndex;\n      let minValue = Number.POSITIVE_INFINITY;\n      for (let k = 0; k < diagonalDistances.length; k++) {\n        if (diagonalDistances[k] === differentDistances[i]) {\n          if (functionValues[k] < minValue) {\n            minValue = functionValues[k];\n            minIndex = k;\n          }\n        }\n      }\n      smallerValuesByDistance.push(functionValues[minIndex]);\n    }\n\n    let currentMin = [];\n    for (let j = 0; j < functionValues.length; j++) {\n      if (functionValues[j] === bestCurrentValue) {\n        let temp = [];\n        for (let i = 0; i < lowerBoundaries.length; i++) {\n          temp.push(\n            lowerBoundaries[i] + unitaryCoordinates[j][i] * diffBorders[i],\n          );\n        }\n        currentMin.push(temp);\n      }\n    }\n    iteration += 1;\n  }\n  //--------------------------------------------------------------\n  //                  Saving results\n  //--------------------------------------------------------------\n\n  let result = {};\n  result.minFunctionValue = bestCurrentValue;\n  result.iterations = iteration;\n  let originalCoordinates = [];\n  for (let j = 0; j < numberOfRectangles + 1; j++) {\n    let pair = [];\n    for (let i = 0; i < lowerBoundaries.length; i++) {\n      pair.push(lowerBoundaries[i] + unitaryCoordinates[j][i] * diffBorders[i]);\n    }\n    originalCoordinates.push(pair);\n  }\n\n  result.finalState = {\n    numberOfRectangles,\n    totalIterations: (totalIterations += iterations),\n    originalCoordinates,\n    middlePoint,\n    fCalls,\n    smallerDistance,\n    edgeSizes,\n    diagonalDistances,\n    functionValues,\n    differentDistances,\n    smallerValuesByDistance,\n    choiceLimit,\n  };\n\n  let minimizer = [];\n  for (let i = 0; i < functionValues.length; i++) {\n    if (functionValues[i] === bestCurrentValue) {\n      minimizer.push(originalCoordinates[i]);\n    }\n  }\n\n  result.optima = minimizer;\n  return result;\n}\n\nfunction getMinIndex(\n  functionValues,\n  diagonalDistances,\n  choiceLimit,\n  bestCurrentValue,\n) {\n  let item = [];\n  for (let i = 0; i < functionValues.length; i++) {\n    item[i] =\n      Math.abs(functionValues[i] - (bestCurrentValue + choiceLimit)) /\n      diagonalDistances[i];\n  }\n  const min = xMinValue(item);\n  let result = item.findIndex((x) => x === min);\n  return result;\n}\n","import direct from 'ml-direct';\nexport function directOptimization(data, sumOfShapes, options) {\n    const { minValues, maxValues, maxIterations, epsilon, tolerance, tolerance2, initialState, } = options;\n    const objectiveFunction = getObjectiveFunction(data, sumOfShapes);\n    const result = direct(objectiveFunction, \n    // direct internally converts ArrayLike to Float64Array,\n    // so we can safely cast minValues and maxValues to number[]\n    minValues, maxValues, {\n        iterations: maxIterations,\n        epsilon,\n        tolerance,\n        tolerance2,\n        initialState,\n    });\n    const { optima } = result;\n    return {\n        parameterError: result.minFunctionValue,\n        iterations: result.iterations,\n        parameterValues: optima[0],\n    };\n}\nfunction getObjectiveFunction(data, sumOfShapes) {\n    const { x, y } = data;\n    const nbPoints = x.length;\n    return (parameters) => {\n        const fct = sumOfShapes(parameters);\n        let error = 0;\n        for (let i = 0; i < nbPoints; i++) {\n            error += (y[i] - fct(x[i])) ** 2;\n        }\n        return error;\n    };\n}\n//# sourceMappingURL=directOptimization.js.map","import { xMaxAbsoluteValue } from 'ml-spectra-processing';\nimport { getSumOfShapes } from \"./shapes/getSumOfShapes.js\";\nimport { getFixedParametersResult } from \"./util/getFixedParametersResult.js\";\nimport { getGlobalParameterVectors } from \"./util/getGlobalParameterVectors.js\";\nimport { getInternalPeaks } from \"./util/internalPeaks/getInternalPeaks.js\";\nimport { selectMethod } from \"./util/selectMethod.js\";\n/**\n * Fits a set of points to the sum of a set of bell functions.\n *\n * @param data - An object containing the x and y data to be fitted.\n * @param peaks - A list of initial parameters to be optimized. e.g. coming from a peak picking [{x, y, width}].\n * @param options - Options for optimize\n * @returns - An object with fitting error and the list of optimized parameters { parameters: [ {x, y, width} ], error } if the kind of shape is pseudoVoigt mu parameter is optimized.\n */\nexport function optimize(data, peaks, options = {}) {\n    // rescale data so the maximum Y value becomes 1\n    const max = xMaxAbsoluteValue(data.y);\n    const yScale = max === 0 ? 1 : max;\n    const internalPeaks = getInternalPeaks(peaks, yScale, options);\n    // need to rescale what is related to Y\n    const normalizedY = new Float64Array(data.y.length);\n    for (let i = 0; i < data.y.length; i++) {\n        normalizedY[i] = data.y[i] / yScale;\n    }\n    const { freeIndices, globalMin, globalMax, globalInit, globalGrad } = getGlobalParameterVectors(internalPeaks, peaks, options);\n    const nbParams = globalInit.length;\n    const { algorithm, optimizationOptions } = selectMethod(options.optimization);\n    const baseSumOfShapes = getSumOfShapes(internalPeaks);\n    if (freeIndices.length === 0) {\n        return getFixedParametersResult(internalPeaks, normalizedY, data.x, globalInit, baseSumOfShapes, yScale);\n    }\n    // wrapper that maps reduced (free) parameters into the full parameter vector\n    const sumOfShapesForReduced = (reducedParameters) => {\n        const full = new Float64Array(nbParams);\n        full.set(globalInit);\n        for (let k = 0; k < freeIndices.length; k++) {\n            full[freeIndices[k]] = reducedParameters[k];\n        }\n        return baseSumOfShapes(Array.from(full));\n    };\n    // prepare arrays to pass to the algorithm (reduced if needed)\n    let minValues;\n    let maxValues;\n    let initialValues;\n    let gradientDifferences;\n    let sumOfShapesToUse = baseSumOfShapes;\n    if (freeIndices.length === nbParams) {\n        // nothing to reduce\n        minValues = globalMin;\n        maxValues = globalMax;\n        initialValues = globalInit;\n        gradientDifferences = globalGrad;\n    }\n    else {\n        minValues = new Float64Array(freeIndices.length);\n        maxValues = new Float64Array(freeIndices.length);\n        initialValues = new Float64Array(freeIndices.length);\n        gradientDifferences = new Float64Array(freeIndices.length);\n        for (let j = 0; j < freeIndices.length; j++) {\n            const i = freeIndices[j];\n            minValues[j] = globalMin[i];\n            maxValues[j] = globalMax[i];\n            initialValues[j] = globalInit[i];\n            gradientDifferences[j] = globalGrad[i];\n        }\n        sumOfShapesToUse = sumOfShapesForReduced;\n    }\n    const fitted = algorithm({ x: data.x, y: normalizedY }, sumOfShapesToUse, {\n        minValues,\n        maxValues,\n        initialValues,\n        gradientDifference: gradientDifferences,\n        ...optimizationOptions,\n    });\n    // reconstruct full parameter vector\n    let fittedValues;\n    if (freeIndices.length === nbParams) {\n        fittedValues = fitted.parameterValues;\n    }\n    else {\n        const full = Array.from(globalInit);\n        for (let k = 0; k < freeIndices.length; k++) {\n            full[freeIndices[k]] = fitted.parameterValues[k];\n        }\n        fittedValues = full;\n    }\n    const newPeaks = [];\n    for (const peak of internalPeaks) {\n        const { id, shape, parameters, fromIndex } = peak;\n        let newPeak = { x: 0, y: 0, shape };\n        if (id) {\n            newPeak = { ...newPeak, id };\n        }\n        newPeak.x = fittedValues[fromIndex];\n        newPeak.y = fittedValues[fromIndex + 1] * yScale;\n        for (let i = 2; i < parameters.length; i++) {\n            //@ts-expect-error should be fixed once\n            newPeak.shape[parameters[i]] = fittedValues[fromIndex + i];\n        }\n        newPeaks.push(newPeak);\n    }\n    return {\n        error: fitted.parameterError,\n        iterations: fitted.iterations,\n        peaks: newPeaks,\n    };\n}\n//# sourceMappingURL=index.js.map","import { xCheck } from \"./xCheck.js\";\nimport { xGetFromToIndex } from \"./xGetFromToIndex.js\";\n/**\n * Computes the maximal value of an array of values\n * @param array - array of numbers\n * @param options - options\n */\nexport function xMaxAbsoluteValue(array, options = {}) {\n    xCheck(array);\n    const { fromIndex, toIndex } = xGetFromToIndex(array, options);\n    let maxValue = array[fromIndex];\n    for (let i = fromIndex + 1; i <= toIndex; i++) {\n        if (array[i] >= 0) {\n            if (array[i] > maxValue) {\n                maxValue = array[i];\n            }\n        }\n        else if (-array[i] > maxValue) {\n            maxValue = -array[i];\n        }\n    }\n    return maxValue;\n}\n//# sourceMappingURL=xMaxAbsoluteValue.js.map","export function getGlobalParameterVectors(internalPeaks, peaks, options) {\n    const nbParams = internalPeaks[internalPeaks.length - 1].toIndex + 1;\n    const globalMin = new Float64Array(nbParams);\n    const globalMax = new Float64Array(nbParams);\n    const globalInit = new Float64Array(nbParams);\n    const globalGrad = new Float64Array(nbParams);\n    const isOptimizable = new Array(nbParams);\n    let index = 0;\n    for (let pIndex = 0; pIndex < internalPeaks.length; pIndex++) {\n        const peak = internalPeaks[pIndex];\n        for (let i = 0; i < peak.parameters.length; i++) {\n            const paramName = peak.parameters[i];\n            globalMin[index] = peak.propertiesValues.min[i];\n            globalMax[index] = peak.propertiesValues.max[i];\n            globalInit[index] = peak.propertiesValues.init[i];\n            globalGrad[index] = peak.propertiesValues.gradientDifference[i];\n            let optimizeFlag = true;\n            const perPeakParam = peaks[pIndex]?.parameters?.[paramName];\n            const globalParam = options.parameters?.[paramName];\n            if (perPeakParam?.optimize !== undefined) {\n                if (typeof perPeakParam.optimize === 'function') {\n                    optimizeFlag = perPeakParam.optimize(peaks[pIndex]);\n                }\n                else {\n                    const { optimize = true } = perPeakParam;\n                    optimizeFlag = optimize;\n                }\n            }\n            else if (globalParam?.optimize !== undefined) {\n                if (typeof globalParam.optimize === 'function') {\n                    optimizeFlag = globalParam.optimize(peaks[pIndex]);\n                }\n                else {\n                    const { optimize = true } = globalParam;\n                    optimizeFlag = optimize;\n                }\n            }\n            isOptimizable[index] = optimizeFlag;\n            index++;\n        }\n    }\n    const freeIndices = [];\n    for (let i = 0; i < nbParams; i++) {\n        if (isOptimizable[i])\n            freeIndices.push(i);\n    }\n    return { freeIndices, globalMin, globalMax, globalInit, globalGrad };\n}\n//# sourceMappingURL=getGlobalParameterVectors.js.map","import { levenbergMarquardt } from 'ml-levenberg-marquardt';\nimport { directOptimization } from \"./wrappers/directOptimization.js\";\n/** Algorithm to select the method.\n * @param optimizationOptions - Optimization options\n * @returns - The algorithm and optimization options\n */\nexport function selectMethod(optimizationOptions = {}) {\n    const { kind = 'lm', options } = optimizationOptions;\n    switch (kind) {\n        case 'lm':\n        case 'levenbergMarquardt':\n            return {\n                algorithm: levenbergMarquardt,\n                optimizationOptions: {\n                    damping: 1.5,\n                    maxIterations: 100,\n                    errorTolerance: 1e-8,\n                    ...options,\n                },\n            };\n        case 'direct': {\n            return {\n                algorithm: directOptimization,\n                optimizationOptions: {\n                    iterations: 20,\n                    epsilon: 1e-4,\n                    tolerance: 1e-16,\n                    tolerance2: 1e-12,\n                    initialState: {},\n                    ...options,\n                },\n            };\n        }\n        default:\n            throw new Error(`Unknown fitting algorithm`);\n    }\n}\n//# sourceMappingURL=selectMethod.js.map","/**\n * This function returns the sumOfShapes function\n * This function gives sumOfShapes access to the peak list and the associated data\n * @param internalPeaks\n */\nexport function getSumOfShapes(internalPeaks) {\n    return function sumOfShapes(parameters) {\n        return (x) => {\n            let totalY = 0;\n            for (const peak of internalPeaks) {\n                const peakX = parameters[peak.fromIndex];\n                const y = parameters[peak.fromIndex + 1];\n                for (let i = 2; i < parameters.length; i++) {\n                    const shapeFctKey = peak.parameters[i];\n                    peak.shapeFct[shapeFctKey] = parameters[peak.fromIndex + i];\n                }\n                totalY += y * peak.shapeFct.fct(x - peakX);\n            }\n            return totalY;\n        };\n    };\n}\n//# sourceMappingURL=getSumOfShapes.js.map","export function getFixedParametersResult(internalPeaks, normalizedY, x, globalInit, baseSumOfShapes, yScale) {\n    const fittedValues = Array.from(globalInit);\n    const newPeaks = [];\n    for (const peak of internalPeaks) {\n        const { id, shape, parameters, fromIndex } = peak;\n        let newPeak = { x: 0, y: 0, shape };\n        if (id) {\n            //@ts-expect-error it is right step\n            newPeak = { ...newPeak, id };\n        }\n        newPeak.x = fittedValues[fromIndex];\n        newPeak.y = fittedValues[fromIndex + 1] * yScale;\n        for (let i = 2; i < parameters.length; i++) {\n            //@ts-expect-error it is right step\n            newPeak.shape[parameters[i]] = fittedValues[fromIndex + i];\n        }\n        newPeaks.push(newPeak);\n    }\n    const fct = baseSumOfShapes(fittedValues);\n    let error = 0;\n    for (let i = 0; i < normalizedY.length; i++) {\n        error += (normalizedY[i] - fct(x[i])) ** 2;\n    }\n    return {\n        error,\n        iterations: 0,\n        peaks: newPeaks,\n    };\n}\n//# sourceMappingURL=getFixedParametersResult.js.map"],"names":["toString","Object","prototype","isAnyArray","value","tag","call","endsWith","includes","xCheck","input","options","minLength","TypeError","length","Error","xFindClosestIndex","array","target","sorted","low","high","middle","Math","abs","index","diff","Number","POSITIVE_INFINITY","i","currentDiff","xGetFromToIndex","x","fromIndex","toIndex","from","to","undefined","Matrix","matrix","inverse","xMaxValue","maxValue","xMinValue","minValue","xNorm","result","element","sqrt","GAUSSIAN_EXP_FACTOR","LN2","ROOT_PI_OVER_LN2","PI","ROOT_THREE","ROOT_2LN2","ROOT_2LN2_MINUS_ONE","Gaussian","constructor","fwhm","sd","this","gaussianWidthToFWHM","fwhmToWidth","gaussianFwhmToWidth","widthToFWHM","width","fct","gaussianFct","getArea","height","calculateGaussianHeight","getGaussianArea","getFactor","area","getGaussianFactor","getData","shape","factor","min","ceil","pow","center","data","Float64Array","getGaussianData","calculateHeight","getParameters","exp","ln1MinusXSqrd","log","lnEtcBy2Plus2","firstSqrt","erfinv","Lorentzian","lorentzianFwhmToWidth","lorentzianWidthToFWHM","lorentzianFct","getLorentzianArea","getLorentzianFactor","getLorentzianData","calculateLorentzianHeight","halfResidual","quantileFunction","p","tan","LorentzianDispersive","lorentzianDispersiveFct","_height","getLorentzianDispersiveData","PseudoVoigt","mu","pseudoVoigtFwhmToWidth","pseudoVoigtWidthToFWHM","pseudoVoigtFct","getPseudoVoigtArea","getPseudoVoigtFactor","calculatePseudoVoigtHeight","getPseudoVoigtData","GeneralizedLorentzian","gamma","generalizedLorentzianFwhmToWidth","generalizedLorentzianWidthToFWHM","generalizedLorentzianFct","getGeneralizedLorentzianArea","getGeneralizedLorentzianFactor","getGeneralizedLorentzianData","calculateGeneralizedLorentzianHeight","u","getShape1D","kind","assert","message","DefaultParameters","init","peak","peakShape","max","gradientDifference","y","properties","getNormalizedValue","parameter","property","yScale","checkOptions","timeout","initialValues","weights","damping","dampingStepUp","dampingStepDown","maxIterations","errorTolerance","centralDifference","improvementThreshold","minValues","maxValues","parameters","Array","parLen","fill","MAX_SAFE_INTEGER","MIN_SAFE_INTEGER","gradientDifferenceArray","getGradientDifferenceArray","filler","dataLength","getFiller","checkTimeout","endTime","Date","now","getCheckTimeout","weightSquare","_","errorCalculation","parameterizedFunction","error","func","step","params","identity","eye","evaluatedData","gradientFunc","paramFunction","nbParams","nbPoints","ans","zeros","rowIndex","param","delta","auxParams","slice","funcParam","funcParam2","point","set","gradientFunction","residualError","m","matrixFunction","inverseMatrix","add","mmul","transpose","scale","jacobianWeightResidualError","perturbations","levenbergMarquardt","checkedOptions","optimalError","optimalParameters","converged","iteration","previousError","k","get","isNaN","mul","parameterValues","parameterError","iterations","antiLowerConvexHull","RangeError","currentPoint","a","b","moveOn","c","moveBack","map","item","filter","vector","counter","getMinIndex","functionValues","diagonalDistances","choiceLimit","bestCurrentValue","findIndex","directOptimization","sumOfShapes","epsilon","tolerance","tolerance2","initialState","objectiveFunction","getObjectiveFunction","lowerBoundaries","upperBoundaries","n","diffBorders","numberOfRectangles","totalIterations","unitaryCoordinates","middlePoint","fCalls","smallerDistance","edgeSizes","differentDistances","smallerValuesByDistance","originalCoordinates","j","optimumValuesIndex","S3","S1","idx","e","f","a1","b1","a2","slope","constant","S2","Uint32Array","xHull","yHull","push","lowerIndexHull","largerSide","largeSidesIndex","bestFunctionValues","r","firstMiddleCenter","secondMiddleCenter","firstMiddleValue","secondMiddleValue","firstMinValue","secondMinValue","sort","ix1","ix2","Set","minIndex","temp","minFunctionValue","pair","finalState","minimizer","optima","direct","optimize","peaks","xMaxAbsoluteValue","internalPeaks","normalizedPeaks","id","shapeFct","propertiesValues","propertyValue","generalParameterValue","defaultParameterValues","getInternalPeaks","normalizedY","freeIndices","globalMin","globalMax","globalInit","globalGrad","isOptimizable","pIndex","paramName","optimizeFlag","perPeakParam","globalParam","getGlobalParameterVectors","algorithm","optimizationOptions","selectMethod","optimization","baseSumOfShapes","totalY","peakX","shapeFctKey","getSumOfShapes","fittedValues","newPeaks","newPeak","getFixedParametersResult","sumOfShapesForReduced","reducedParameters","full","gradientDifferences","sumOfShapesToUse","fitted"],"mappings":";AACA,MAAMA,EAAWC,OAAOC,UAAUF,SAmB5B,SAAUG,EAAWC,GACzB,MAAMC,EAAML,EAASM,KAAKF,GAC1B,OAAOC,EAAIE,SAAS,YAAcF,EAAIG,SAAS,MACjD,CCNM,SAAUC,EACdC,EACAC,EAAyB,IAEzB,MAAMC,UAAEA,GAAcD,EACtB,IAAKR,EAAWO,GACd,MAAM,IAAIG,UAAU,0BAEtB,GAAqB,IAAjBH,EAAMI,OACR,MAAM,IAAID,UAAU,2BAEtB,GAAwB,iBAAbH,EAAM,GACf,MAAM,IAAIG,UAAU,8BAEtB,GAAID,GAAaF,EAAMI,OAASF,EAC9B,MAAM,IAAIG,MAAM,wCAAwCH,IAE5D,CCjBM,SAAUI,EACdC,EACAC,EACAP,EAAoC,CAAA,GAEpC,MAAMQ,OAAEA,GAAS,GAASR,EAC1B,GAAIQ,EAAQ,CACV,IAAIC,EAAM,EACNC,EAAOJ,EAAMH,OAAS,EACtBQ,EAAS,EACb,KAAOD,EAAOD,EAAM,GAElB,GADAE,EAASF,GAAQC,EAAOD,GAAQ,GAC5BH,EAAMK,GAAUJ,EAClBE,EAAME,MACD,MAAIL,EAAMK,GAAUJ,GAGzB,OAAOI,EAFPD,EAAOC,CAGT,CAGF,OAAIF,EAAMH,EAAMH,OAAS,EACnBS,KAAKC,IAAIN,EAASD,EAAMG,IAAQG,KAAKC,IAAIP,EAAMG,EAAM,GAAKF,GACrDE,EAEAA,EAAM,EAGRA,CAEX,CAAO,CACL,IAAIK,EAAQ,EACRC,EAAOC,OAAOC,kBAClB,IAAK,IAAIC,EAAI,EAAGA,EAAIZ,EAAMH,OAAQe,IAAK,CACrC,MAAMC,EAAcP,KAAKC,IAAIP,EAAMY,GAAKX,GACpCY,EAAcJ,IAChBA,EAAOI,EACPL,EAAQI,EAEZ,CACA,OAAOJ,CAC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