From 6300cd503302df1e9943de10d99606e7c7831683 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philippe=20Rivi=C3=A8re?= Date: Tue, 2 Apr 2019 21:40:05 +0200 Subject: [PATCH 01/46] typo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit and saying hello! I made a small demo here https://observablehq.com/@fil/hello-umap-js — it's not super relevant as the "data" is just random, but it shows how to load and use the script. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 24b58da..d284711 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,7 @@ The UMAP constructor can accept a number of parameters via a `UMAPParameters` ob | ------------- | ----------------------------------------------------------------------------------------------------------------------------------- | ------------------------ | | `nComponents` | The number of components (dimensions) to project the data to | 2 | | `nEpochs` | The number of epochs to optimize embeddings via SGD | (computed automatically) | -| `nNeigbors` | The number of nearest neigbors to construct the fuzzy manifold | 15 | +| `nNeighbors` | The number of nearest neighbors to construct the fuzzy manifold | 15 | | `minDist` | The effective minimum distance between embedded points, used with `spread` to control the clumped/dispersed nature of the embedding | 0.1 | | `spread` | The effective scale of embedded points, used with `minDist` to control the clumped/dispersed nature of the embedding | 1.0 | | `random` | A pseudo-random-number generator for controlling stochastic processes | `Math.random` | From b5b0f903551ea25355f938a5fd801e987ecc6803 Mon Sep 17 00:00:00 2001 From: Andy Coenen Date: Thu, 25 Apr 2019 15:23:22 -0700 Subject: [PATCH 02/46] Update version for re-publish broken build to npm --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index b46ff61..b822a6c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "umap-js", - "version": "1.0.5", + "version": "1.0.6", "description": "JavaScript implementation of UMAP", "author": { "name": "Andy Coenen", From 367abe7bc54d2ed00a8ea3fb5dc143fa22152cf5 Mon Sep 17 00:00:00 2001 From: Andy Coenen Date: Thu, 25 Apr 2019 17:49:42 -0700 Subject: [PATCH 03/46] Implement transform method --- README.md | 15 +- lib/umap-js.js | 2734 ++++++++++++++++++++++----------------- lib/umap-js.min.js | 2 +- package.json | 2 +- src/heap.ts | 55 +- src/matrix.ts | 49 +- src/nn_descent.ts | 120 ++ src/tree.ts | 39 +- src/umap.ts | 372 +++++- src/utils.ts | 52 +- test/matrix.test.ts | 15 + test/nn_descent.test.ts | 15 + test/test_data.ts | 12 +- test/umap.test.ts | 129 +- test/utils.test.ts | 17 + 15 files changed, 2330 insertions(+), 1298 deletions(-) diff --git a/README.md b/README.md index d284711..051f91f 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,6 @@ Uniform Manifold Approximation and Projection (UMAP) is a dimension reduction te There are a few important differences between the python implementation and the JS port. - The optimization step is seeded with a random embedding rather than a spectral embedding. This gives comparable results for smaller datasets. The spectral embedding computation relies on efficient eigenvalue / eigenvector computations that are not easily done in JS. -- There is no implementation of any supervised dimension reduction or adding new points to an existing embedding. - The only distance function used is euclidean distance. - There is no specialized functionality for angular distances or sparse data representations. @@ -62,15 +61,25 @@ umap.setSupervisedProjection(labels); const embedding = umap.fit(data); ``` +#### Transforming additional points after fitting + +```typescript +import { UMAP } from 'umap-js'; + +const umap = new UMAP(); +umap.fit(data); +const transformed = umap.transform(additionalData); +``` + #### Parameters -The UMAP constructor can accept a number of parameters via a `UMAPParameters` object: +The UMAP constructor can accept a number of hyperparameters via a `UMAPParameters` object, with the most common described below. See [umap.ts](./src/umap.ts) for more details. | Parameter | Description | default | | ------------- | ----------------------------------------------------------------------------------------------------------------------------------- | ------------------------ | | `nComponents` | The number of components (dimensions) to project the data to | 2 | | `nEpochs` | The number of epochs to optimize embeddings via SGD | (computed automatically) | -| `nNeighbors` | The number of nearest neighbors to construct the fuzzy manifold | 15 | +| `nNeighbors` | The number of nearest neighbors to construct the fuzzy manifold | 15 | | `minDist` | The effective minimum distance between embedded points, used with `spread` to control the clumped/dispersed nature of the embedding | 0.1 | | `spread` | The effective scale of embedded points, used with `minDist` to control the clumped/dispersed nature of the embedding | 1.0 | | `random` | A pseudo-random-number generator for controlling stochastic processes | `Math.random` | diff --git a/lib/umap-js.js b/lib/umap-js.js index 6430a44..1b13db5 100644 --- a/lib/umap-js.js +++ b/lib/umap-js.js @@ -81,7 +81,7 @@ /******/ /******/ /******/ // Load entry module and return exports -/******/ return __webpack_require__(__webpack_require__.s = 2); +/******/ return __webpack_require__(__webpack_require__.s = 5); /******/ }) /************************************************************************/ /******/ ([ @@ -223,6 +223,47 @@ function max2d(input) { return max; } exports.max2d = max2d; +function rejectionSample(nSamples, poolSize) { + var result = zeros(nSamples); + for (var i = 0; i < nSamples; i++) { + var rejectSample = true; + while (rejectSample) { + var j = tauRandInt(poolSize); + var broken = false; + for (var k = 0; k < i; k++) { + if (j === result[k]) { + broken = true; + break; + } + } + if (!broken) { + rejectSample = false; + } + result[i] = j; + } + } + return result; +} +exports.rejectionSample = rejectionSample; +function reshape2d(x, a, b) { + var rows = []; + var count = 0; + var index = 0; + if (x.length !== a * b) { + throw new Error('Array dimensions must match input length.'); + } + for (var i = 0; i < a; i++) { + var col = []; + for (var j = 0; j < b; j++) { + col.push(x[index]); + index += 1; + } + rows.push(col); + count += 1; + } + return rows; +} +exports.reshape2d = reshape2d; /***/ }), @@ -232,8 +273,197 @@ exports.max2d = max2d; "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -var umap_1 = __webpack_require__(3); -window.UMAP = umap_1.UMAP; +var utils = __webpack_require__(1); +function makeHeap(nPoints, size) { + var makeArrays = function (fillValue) { + return utils.empty(nPoints).map(function () { + return utils.filled(size, fillValue); + }); + }; + var heap = []; + heap.push(makeArrays(-1)); + heap.push(makeArrays(Infinity)); + heap.push(makeArrays(0)); + return heap; +} +exports.makeHeap = makeHeap; +function rejectionSample(nSamples, poolSize, random) { + var result = utils.zeros(nSamples); + for (var i = 0; i < nSamples; i++) { + var rejectSample = true; + var j = 0; + while (rejectSample) { + j = utils.tauRandInt(poolSize, random); + var broken = false; + for (var k = 0; k < i; k++) { + if (j === result[k]) { + broken = true; + break; + } + } + if (!broken) + rejectSample = false; + } + result[i] = j; + } + return result; +} +exports.rejectionSample = rejectionSample; +function heapPush(heap, row, weight, index, flag) { + row = Math.floor(row); + var indices = heap[0][row]; + var weights = heap[1][row]; + var isNew = heap[2][row]; + if (weight >= weights[0]) { + return 0; + } + for (var i = 0; i < indices.length; i++) { + if (index === indices[i]) { + return 0; + } + } + return uncheckedHeapPush(heap, row, weight, index, flag); +} +exports.heapPush = heapPush; +function uncheckedHeapPush(heap, row, weight, index, flag) { + var indices = heap[0][row]; + var weights = heap[1][row]; + var isNew = heap[2][row]; + if (weight >= weights[0]) { + return 0; + } + weights[0] = weight; + indices[0] = index; + isNew[0] = flag; + var i = 0; + var iSwap = 0; + while (true) { + var ic1 = 2 * i + 1; + var ic2 = ic1 + 1; + var heapShape2 = heap[0][0].length; + if (ic1 >= heapShape2) { + break; + } + else if (ic2 >= heapShape2) { + if (weights[ic1] > weight) { + iSwap = ic1; + } + else { + break; + } + } + else if (weights[ic1] >= weights[ic2]) { + if (weight < weights[ic1]) { + iSwap = ic1; + } + else { + break; + } + } + else { + if (weight < weights[ic2]) { + iSwap = ic2; + } + else { + break; + } + } + weights[i] = weights[iSwap]; + indices[i] = indices[iSwap]; + isNew[i] = isNew[iSwap]; + i = iSwap; + } + weights[i] = weight; + indices[i] = index; + isNew[i] = flag; + return 1; +} +exports.uncheckedHeapPush = uncheckedHeapPush; +function buildCandidates(currentGraph, nVertices, nNeighbors, maxCandidates, random) { + var candidateNeighbors = makeHeap(nVertices, maxCandidates); + for (var i = 0; i < nVertices; i++) { + for (var j = 0; j < nNeighbors; j++) { + if (currentGraph[0][i][j] < 0) { + continue; + } + var idx = currentGraph[0][i][j]; + var isn = currentGraph[2][i][j]; + var d = utils.tauRand(random); + heapPush(candidateNeighbors, i, d, idx, isn); + heapPush(candidateNeighbors, idx, d, i, isn); + currentGraph[2][i][j] = 0; + } + } + return candidateNeighbors; +} +exports.buildCandidates = buildCandidates; +function deheapSort(heap) { + var indices = heap[0]; + var weights = heap[1]; + for (var i = 0; i < indices.length; i++) { + var indHeap = indices[i]; + var distHeap = weights[i]; + for (var j = 0; j < indHeap.length - 1; j++) { + var indHeapIndex = indHeap.length - j - 1; + var distHeapIndex = distHeap.length - j - 1; + var temp1 = indHeap[0]; + indHeap[0] = indHeap[indHeapIndex]; + indHeap[indHeapIndex] = temp1; + var temp2 = distHeap[0]; + distHeap[0] = distHeap[distHeapIndex]; + distHeap[distHeapIndex] = temp2; + siftDown(distHeap, indHeap, distHeapIndex, 0); + } + } + return { indices: indices, weights: weights }; +} +exports.deheapSort = deheapSort; +function siftDown(heap1, heap2, ceiling, elt) { + while (elt * 2 + 1 < ceiling) { + var leftChild = elt * 2 + 1; + var rightChild = leftChild + 1; + var swap = elt; + if (heap1[swap] < heap1[leftChild]) { + swap = leftChild; + } + if (rightChild < ceiling && heap1[swap] < heap1[rightChild]) { + swap = rightChild; + } + if (swap === elt) { + break; + } + else { + var temp1 = heap1[elt]; + heap1[elt] = heap1[swap]; + heap1[swap] = temp1; + var temp2 = heap2[elt]; + heap2[elt] = heap2[swap]; + heap2[swap] = temp2; + elt = swap; + } + } +} +function smallestFlagged(heap, row) { + var ind = heap[0][row]; + var dist = heap[1][row]; + var flag = heap[2][row]; + var minDist = Infinity; + var resultIndex = -1; + for (var i = 0; i > ind.length; i++) { + if (flag[i] === 1 && dist[i] < minDist) { + minDist = dist[i]; + resultIndex = i; + } + } + if (resultIndex >= 0) { + flag[resultIndex] = 0; + return Math.floor(ind[resultIndex]); + } + else { + return -1; + } +} +exports.smallestFlagged = smallestFlagged; /***/ }), @@ -242,41 +472,6 @@ window.UMAP = umap_1.UMAP; "use strict"; -var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { - return new (P || (P = Promise))(function (resolve, reject) { - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } - function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } - function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } - step((generator = generator.apply(thisArg, _arguments || [])).next()); - }); -}; -var __generator = (this && this.__generator) || function (thisArg, body) { - var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; - return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; - function verb(n) { return function (v) { return step([n, v]); }; } - function step(op) { - if (f) throw new TypeError("Generator is already executing."); - while (_) try { - if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; - if (y = 0, t) op = [op[0] & 2, t.value]; - switch (op[0]) { - case 0: case 1: t = op; break; - case 4: _.label++; return { value: op[1], done: false }; - case 5: _.label++; y = op[1]; op = [0]; continue; - case 7: op = _.ops.pop(); _.trys.pop(); continue; - default: - if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } - if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } - if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } - if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } - if (t[2]) _.ops.pop(); - _.trys.pop(); continue; - } - op = body.call(thisArg, _); - } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } - if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; - } -}; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; @@ -297,574 +492,593 @@ var __spread = (this && this.__spread) || function () { for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); return ar; }; +var __values = (this && this.__values) || function (o) { + var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0; + if (m) return m.call(o); + return { + next: function () { + if (o && i >= o.length) o = void 0; + return { value: o && o[i++], done: !o }; + } + }; +}; Object.defineProperty(exports, "__esModule", { value: true }); -var matrix = __webpack_require__(4); -var nnDescent = __webpack_require__(5); -var tree = __webpack_require__(7); +var _a; var utils = __webpack_require__(1); -var LM = __webpack_require__(8); -var SMOOTH_K_TOLERANCE = 1e-5; -var MIN_K_DIST_SCALE = 1e-3; -var UMAP = (function () { - function UMAP(params) { - if (params === void 0) { params = {}; } - this.minDist = 0.1; - this.nComponents = 2; - this.nEpochs = 0; - this.nNeighbors = 15; - this.random = Math.random; - this.spread = 1.0; - this.targetMetric = "categorical"; - this.targetWeight = 0.5; - this.targetNNeighbors = this.nNeighbors; - this.distanceFn = euclidean; - this.isInitialized = false; - this.embedding = []; - this.optimizationState = new OptimizationState(); - this.minDist = params.minDist || this.minDist; - this.nComponents = params.nComponents || this.nComponents; - this.nEpochs = params.nEpochs || this.nEpochs; - this.nNeighbors = params.nNeighbors || this.nNeighbors; - this.random = params.random || this.random; - this.spread = params.spread || this.spread; +var SparseMatrix = (function () { + function SparseMatrix(rows, cols, values, dims) { + this.entries = new Map(); + this.nRows = 0; + this.nCols = 0; + this.rows = __spread(rows); + this.cols = __spread(cols); + this.values = __spread(values); + for (var i = 0; i < values.length; i++) { + var key = this.makeKey(this.rows[i], this.cols[i]); + this.entries.set(key, i); + } + this.nRows = dims[0]; + this.nCols = dims[1]; } - UMAP.prototype.fit = function (X) { - this.initializeFit(X); - this.optimizeLayout(); - return this.embedding; - }; - UMAP.prototype.fitAsync = function (X, callback) { - if (callback === void 0) { callback = function () { return true; }; } - return __awaiter(this, void 0, void 0, function () { - return __generator(this, function (_a) { - switch (_a.label) { - case 0: - this.initializeFit(X); - return [4, this.optimizeLayout(callback)]; - case 1: - _a.sent(); - return [2, this.embedding]; - } - }); - }); - }; - UMAP.prototype.setSupervisedProjection = function (Y, params) { - if (params === void 0) { params = {}; } - this.Y = Y; - this.targetMetric = params.targetMetric || this.targetMetric; - this.targetWeight = params.targetWeight || this.targetWeight; - this.targetNNeighbors = params.targetNNeighbors || this.targetNNeighbors; - }; - UMAP.prototype.setPrecomputedKNN = function (knnIndices, knnDistances) { - this.knnIndices = knnIndices; - this.knnDistances = knnDistances; + SparseMatrix.prototype.makeKey = function (row, col) { + return row + ":" + col; }; - UMAP.prototype.initializeFit = function (X) { - if (this.X === X && this.isInitialized) { - return this.getNEpochs(); - } - this.X = X; - if (!this.knnIndices && !this.knnDistances) { - var knnResults = this.nearestNeighbors(X); - this.knnIndices = knnResults.knnIndices; - this.knnDistances = knnResults.knnDistances; + SparseMatrix.prototype.checkDims = function (row, col) { + var withinBounds = row < this.nRows && col < this.nCols; + if (!withinBounds) { + throw new Error('array index out of bounds'); } - this.graph = this.fuzzySimplicialSet(X, this.nNeighbors); - this.processGraphForSupervisedProjection(); - var _a = this.initializeSimplicialSetEmbedding(), head = _a.head, tail = _a.tail, epochsPerSample = _a.epochsPerSample; - this.optimizationState.head = head; - this.optimizationState.tail = tail; - this.optimizationState.epochsPerSample = epochsPerSample; - this.isInitialized = true; - return this.getNEpochs(); }; - UMAP.prototype.processGraphForSupervisedProjection = function () { - var _a = this, Y = _a.Y, X = _a.X; - if (Y) { - if (Y.length !== X.length) { - throw new Error('Length of X and y must be equal'); - } - if (this.targetMetric === "categorical") { - var lt = this.targetWeight < 1.0; - var farDist = lt ? 2.5 * (1.0 / (1.0 - this.targetWeight)) : 1.0e12; - this.graph = this.categoricalSimplicialSetIntersection(this.graph, Y, farDist); - } + SparseMatrix.prototype.set = function (row, col, value) { + this.checkDims(row, col); + var key = this.makeKey(row, col); + if (!this.entries.has(key)) { + this.rows.push(row); + this.cols.push(col); + this.values.push(value); + this.entries.set(key, this.values.length - 1); + } + else { + var index = this.entries.get(key); + this.values[index] = value; } }; - UMAP.prototype.step = function () { - var _a = this.optimizationState, currentEpoch = _a.currentEpoch, isInitialized = _a.isInitialized; - if (!isInitialized) { - this.initializeOptimization(); + SparseMatrix.prototype.get = function (row, col, defaultValue) { + if (defaultValue === void 0) { defaultValue = 0; } + this.checkDims(row, col); + var key = this.makeKey(row, col); + if (this.entries.has(key)) { + var index = this.entries.get(key); + return this.values[index]; } - if (currentEpoch < this.getNEpochs()) { - this.optimizeLayoutStep(currentEpoch); + else { + return defaultValue; } - return this.optimizationState.currentEpoch; }; - UMAP.prototype.getEmbedding = function () { - return this.embedding; + SparseMatrix.prototype.getDims = function () { + return [this.nRows, this.nCols]; }; - UMAP.prototype.nearestNeighbors = function (X) { - var _a = this, distanceFn = _a.distanceFn, nNeighbors = _a.nNeighbors; - var log2 = function (n) { return Math.log(n) / Math.log(2); }; - var metricNNDescent = nnDescent.makeNNDescent(distanceFn, this.random); - var round = function (n) { - return n === 0.5 ? 0 : Math.round(n); - }; - var nTrees = 5 + Math.floor(round(Math.pow(X.length, 0.5) / 20.0)); - var nIters = Math.max(5, Math.floor(Math.round(log2(X.length)))); - var rpForest = tree.makeForest(X, nNeighbors, nTrees, this.random); - var leafArray = tree.makeLeafArray(rpForest); - var _b = metricNNDescent(X, leafArray, nNeighbors, nIters), indices = _b.indices, weights = _b.weights; - return { knnIndices: indices, knnDistances: weights }; + SparseMatrix.prototype.getRows = function () { + return __spread(this.rows); }; - UMAP.prototype.fuzzySimplicialSet = function (X, nNeighbors, localConnectivity, setOpMixRatio) { - if (localConnectivity === void 0) { localConnectivity = 1.0; } - if (setOpMixRatio === void 0) { setOpMixRatio = 1.0; } - var _a = this, _b = _a.knnIndices, knnIndices = _b === void 0 ? [] : _b, _c = _a.knnDistances, knnDistances = _c === void 0 ? [] : _c; - var _d = this.smoothKNNDistance(knnDistances, nNeighbors, localConnectivity), sigmas = _d.sigmas, rhos = _d.rhos; - var _e = this.computeMembershipStrengths(knnIndices, knnDistances, sigmas, rhos), rows = _e.rows, cols = _e.cols, vals = _e.vals; - var size = [X.length, X.length]; - var sparseMatrix = new matrix.SparseMatrix(rows, cols, vals, size); - var transpose = matrix.transpose(sparseMatrix); - var prodMatrix = matrix.pairwiseMultiply(sparseMatrix, transpose); - var a = matrix.subtract(matrix.add(sparseMatrix, transpose), prodMatrix); - var b = matrix.multiplyScalar(a, setOpMixRatio); - var c = matrix.multiplyScalar(prodMatrix, 1.0 - setOpMixRatio); - var result = matrix.add(b, c); - return result; + SparseMatrix.prototype.getCols = function () { + return __spread(this.cols); }; - UMAP.prototype.categoricalSimplicialSetIntersection = function (simplicialSet, target, farDist, unknownDist) { - if (unknownDist === void 0) { unknownDist = 1.0; } - var intersection = fastIntersection(simplicialSet, target, unknownDist, farDist); - intersection = matrix.eliminateZeros(intersection); - return resetLocalConnectivity(intersection); + SparseMatrix.prototype.getValues = function () { + return __spread(this.values); }; - UMAP.prototype.smoothKNNDistance = function (distances, k, localConnectivity, nIter, bandwidth) { - if (localConnectivity === void 0) { localConnectivity = 1.0; } - if (nIter === void 0) { nIter = 64; } - if (bandwidth === void 0) { bandwidth = 1.0; } - var target = (Math.log(k) / Math.log(2)) * bandwidth; - var rho = utils.zeros(distances.length); - var result = utils.zeros(distances.length); - for (var i = 0; i < distances.length; i++) { - var lo = 0.0; - var hi = Infinity; - var mid = 1.0; - var ithDistances = distances[i]; - var nonZeroDists = ithDistances.filter(function (d) { return d > 0.0; }); - if (nonZeroDists.length >= localConnectivity) { - var index = Math.floor(localConnectivity); - var interpolation = localConnectivity - index; - if (index > 0) { - rho[i] = nonZeroDists[index - 1]; - if (interpolation > SMOOTH_K_TOLERANCE) { - rho[i] += - interpolation * (nonZeroDists[index] - nonZeroDists[index - 1]); - } - } - else { - rho[i] = interpolation * nonZeroDists[0]; - } - } - else if (nonZeroDists.length > 0) { - rho[i] = utils.max(nonZeroDists); - } - for (var n = 0; n < nIter; n++) { - var psum = 0.0; - for (var j = 1; j < distances[i].length; j++) { - var d = distances[i][j] - rho[i]; - if (d > 0) { - psum += Math.exp(-(d / mid)); - } - else { - psum += 1.0; - } - } - if (Math.abs(psum - target) < SMOOTH_K_TOLERANCE) { - break; - } - if (psum > target) { - hi = mid; - mid = (lo + hi) / 2.0; - } - else { - lo = mid; - if (hi === Infinity) { - mid *= 2; - } - else { - mid = (lo + hi) / 2.0; - } - } - } - result[i] = mid; - if (rho[i] > 0.0) { - var meanIthDistances = utils.mean(ithDistances); - if (result[i] < MIN_K_DIST_SCALE * meanIthDistances) { - result[i] = MIN_K_DIST_SCALE * meanIthDistances; - } - } - else { - var meanDistances = utils.mean(distances.map(utils.mean)); - if (result[i] < MIN_K_DIST_SCALE * meanDistances) { - result[i] = MIN_K_DIST_SCALE * meanDistances; - } - } + SparseMatrix.prototype.forEach = function (fn) { + for (var i = 0; i < this.values.length; i++) { + fn(this.values[i], this.rows[i], this.cols[i]); } - return { sigmas: result, rhos: rho }; }; - UMAP.prototype.computeMembershipStrengths = function (knnIndices, knnDistances, sigmas, rhos) { - var nSamples = knnIndices.length; - var nNeighbors = knnIndices[0].length; - var rows = utils.zeros(nSamples * nNeighbors); - var cols = utils.zeros(nSamples * nNeighbors); - var vals = utils.zeros(nSamples * nNeighbors); - for (var i = 0; i < nSamples; i++) { - for (var j = 0; j < nNeighbors; j++) { - var val = 0; - if (knnIndices[i][j] === -1) { - continue; - } - if (knnIndices[i][j] === i) { - val = 0.0; - } - else if (knnDistances[i][j] - rhos[i] <= 0.0) { - val = 1.0; - } - else { - val = Math.exp(-((knnDistances[i][j] - rhos[i]) / sigmas[i])); - } - rows[i * nNeighbors + j] = i; - cols[i * nNeighbors + j] = knnIndices[i][j]; - vals[i * nNeighbors + j] = val; - } + SparseMatrix.prototype.map = function (fn) { + var vals = []; + for (var i = 0; i < this.values.length; i++) { + vals.push(fn(this.values[i], this.rows[i], this.cols[i])); } - return { rows: rows, cols: cols, vals: vals }; + var dims = [this.nRows, this.nCols]; + return new SparseMatrix(this.rows, this.cols, vals, dims); }; - UMAP.prototype.initializeSimplicialSetEmbedding = function () { + SparseMatrix.prototype.toArray = function () { var _this = this; - var nEpochs = this.getNEpochs(); - var nComponents = this.nComponents; - var graphValues = this.graph.getValues(); - var graphMax = 0; - for (var i = 0; i < graphValues.length; i++) { - var value = graphValues[i]; - if (graphMax < graphValues[i]) { - graphMax = value; - } - } - var graph = this.graph.map(function (value) { - if (value < graphMax / nEpochs) { - return 0; - } - else { - return value; - } - }); - this.embedding = utils.zeros(graph.nRows).map(function () { - return utils.zeros(nComponents).map(function () { - return utils.tauRand(_this.random) * 20 + -10; - }); + var rows = utils.empty(this.nRows); + var output = rows.map(function () { + return utils.zeros(_this.nCols); }); - var weights = []; - var head = []; - var tail = []; - for (var i = 0; i < graph.nRows; i++) { - for (var j = 0; j < graph.nCols; j++) { - var value = graph.get(i, j); - if (value) { - weights.push(value); - tail.push(i); - head.push(j); - } - } + for (var i = 0; i < this.values.length; i++) { + output[this.rows[i]][this.cols[i]] = this.values[i]; } - var epochsPerSample = this.makeEpochsPerSample(weights, nEpochs); - return { head: head, tail: tail, epochsPerSample: epochsPerSample }; + return output; }; - UMAP.prototype.makeEpochsPerSample = function (weights, nEpochs) { - var result = utils.filled(weights.length, -1.0); - var max = utils.max(weights); - var nSamples = weights.map(function (w) { return (w / max) * nEpochs; }); - nSamples.forEach(function (n, i) { - if (n > 0) - result[i] = nEpochs / nSamples[i]; - }); - return result; + return SparseMatrix; +}()); +exports.SparseMatrix = SparseMatrix; +function transpose(matrix) { + var cols = []; + var rows = []; + var vals = []; + matrix.forEach(function (value, row, col) { + cols.push(row); + rows.push(col); + vals.push(value); + }); + var dims = [matrix.nCols, matrix.nRows]; + return new SparseMatrix(rows, cols, vals, dims); +} +exports.transpose = transpose; +function identity(size) { + var _a = __read(size, 1), rows = _a[0]; + var matrix = new SparseMatrix([], [], [], size); + for (var i = 0; i < rows; i++) { + matrix.set(i, i, 1); + } + return matrix; +} +exports.identity = identity; +function pairwiseMultiply(a, b) { + return elementWise(a, b, function (x, y) { return x * y; }); +} +exports.pairwiseMultiply = pairwiseMultiply; +function add(a, b) { + return elementWise(a, b, function (x, y) { return x + y; }); +} +exports.add = add; +function subtract(a, b) { + return elementWise(a, b, function (x, y) { return x - y; }); +} +exports.subtract = subtract; +function maximum(a, b) { + return elementWise(a, b, function (x, y) { return (x > y ? x : y); }); +} +exports.maximum = maximum; +function multiplyScalar(a, scalar) { + return a.map(function (value) { + return value * scalar; + }); +} +exports.multiplyScalar = multiplyScalar; +function eliminateZeros(m) { + var zeroIndices = new Set(); + var values = m.getValues(); + var rows = m.getRows(); + var cols = m.getCols(); + for (var i = 0; i < values.length; i++) { + if (values[i] === 0) { + zeroIndices.add(i); + } + } + var removeByZeroIndex = function (_, index) { return !zeroIndices.has(index); }; + var nextValues = values.filter(removeByZeroIndex); + var nextRows = rows.filter(removeByZeroIndex); + var nextCols = cols.filter(removeByZeroIndex); + return new SparseMatrix(nextRows, nextCols, nextValues, m.getDims()); +} +exports.eliminateZeros = eliminateZeros; +function normalize(m, normType) { + if (normType === void 0) { normType = "l2"; } + var e_1, _a; + var normFn = normFns[normType]; + var colsByRow = new Map(); + m.forEach(function (_, row, col) { + var cols = colsByRow.get(row) || []; + cols.push(col); + colsByRow.set(row, cols); + }); + var nextMatrix = new SparseMatrix([], [], [], m.getDims()); + var _loop_1 = function (row) { + var cols = colsByRow.get(row).sort(); + var vals = cols.map(function (col) { return m.get(row, col); }); + var norm = normFn(vals); + for (var i = 0; i < norm.length; i++) { + nextMatrix.set(row, cols[i], norm[i]); + } }; - UMAP.prototype.initializeOptimization = function () { - var headEmbedding = this.embedding; - var tailEmbedding = this.embedding; - var _a = this.optimizationState, head = _a.head, tail = _a.tail, epochsPerSample = _a.epochsPerSample; - var gamma = 1.0; - var initialAlpha = 1.0; - var negativeSampleRate = 5; - var nEpochs = this.getNEpochs(); - var nVertices = this.graph.nCols; - var _b = findABParams(this.spread, this.minDist), a = _b.a, b = _b.b; - var dim = headEmbedding[0].length; - var moveOther = headEmbedding.length === tailEmbedding.length; - var alpha = initialAlpha; - var epochsPerNegativeSample = epochsPerSample.map(function (e) { return e / negativeSampleRate; }); - var epochOfNextNegativeSample = __spread(epochsPerNegativeSample); - var epochOfNextSample = __spread(epochsPerSample); - Object.assign(this.optimizationState, { - isInitialized: true, - headEmbedding: headEmbedding, - tailEmbedding: tailEmbedding, - head: head, - tail: tail, - epochsPerSample: epochsPerSample, - epochOfNextSample: epochOfNextSample, - epochOfNextNegativeSample: epochOfNextNegativeSample, - epochsPerNegativeSample: epochsPerNegativeSample, - moveOther: moveOther, - initialAlpha: initialAlpha, - alpha: alpha, - gamma: gamma, - a: a, - b: b, - dim: dim, - nEpochs: nEpochs, - nVertices: nVertices, - }); + try { + for (var _b = __values(colsByRow.keys()), _c = _b.next(); !_c.done; _c = _b.next()) { + var row = _c.value; + _loop_1(row); + } + } + catch (e_1_1) { e_1 = { error: e_1_1 }; } + finally { + try { + if (_c && !_c.done && (_a = _b.return)) _a.call(_b); + } + finally { if (e_1) throw e_1.error; } + } + return nextMatrix; +} +exports.normalize = normalize; +var normFns = (_a = {}, + _a["max"] = function (xs) { + var max = -Infinity; + for (var i = 0; i < xs.length; i++) { + max = xs[i] > max ? xs[i] : max; + } + return xs.map(function (x) { return x / max; }); + }, + _a["l1"] = function (xs) { + var sum = 0; + for (var i = 0; i < xs.length; i++) { + sum += xs[i]; + } + return xs.map(function (x) { return x / sum; }); + }, + _a["l2"] = function (xs) { + var sum = 0; + for (var i = 0; i < xs.length; i++) { + sum += Math.pow(xs[i], 2); + } + return xs.map(function (x) { return Math.sqrt(Math.pow(x, 2) / sum); }); + }, + _a); +function elementWise(a, b, op) { + var visited = new Set(); + var rows = []; + var cols = []; + var vals = []; + var operate = function (row, col) { + rows.push(row); + cols.push(col); + var nextValue = op(a.get(row, col), b.get(row, col)); + vals.push(nextValue); }; - UMAP.prototype.optimizeLayoutStep = function (n) { - var optimizationState = this.optimizationState; - var head = optimizationState.head, tail = optimizationState.tail, headEmbedding = optimizationState.headEmbedding, tailEmbedding = optimizationState.tailEmbedding, epochsPerSample = optimizationState.epochsPerSample, epochOfNextSample = optimizationState.epochOfNextSample, epochOfNextNegativeSample = optimizationState.epochOfNextNegativeSample, epochsPerNegativeSample = optimizationState.epochsPerNegativeSample, moveOther = optimizationState.moveOther, initialAlpha = optimizationState.initialAlpha, alpha = optimizationState.alpha, gamma = optimizationState.gamma, a = optimizationState.a, b = optimizationState.b, dim = optimizationState.dim, nEpochs = optimizationState.nEpochs, nVertices = optimizationState.nVertices; - var clipValue = 4.0; - for (var i = 0; i < epochsPerSample.length; i++) { - if (epochOfNextSample[i] > n) { - continue; - } - var j = head[i]; - var k = tail[i]; - var current = headEmbedding[j]; - var other = tailEmbedding[k]; - var distSquared = rDist(current, other); - var gradCoeff = 0; - if (distSquared > 0) { - gradCoeff = -2.0 * a * b * Math.pow(distSquared, b - 1.0); - gradCoeff /= a * Math.pow(distSquared, b) + 1.0; - } - for (var d = 0; d < dim; d++) { - var gradD = clip(gradCoeff * (current[d] - other[d]), clipValue); - current[d] += gradD * alpha; - if (moveOther) { - other[d] += -gradD * alpha; - } - } - epochOfNextSample[i] += epochsPerSample[i]; - var nNegSamples = Math.floor((n - epochOfNextNegativeSample[i]) / epochsPerNegativeSample[i]); - for (var p = 0; p < nNegSamples; p++) { - var k_1 = utils.tauRandInt(nVertices, this.random); - var other_1 = tailEmbedding[k_1]; - var distSquared_1 = rDist(current, other_1); - var gradCoeff_1 = 0.0; - if (distSquared_1 > 0.0) { - gradCoeff_1 = 2.0 * gamma * b; - gradCoeff_1 /= - (0.001 + distSquared_1) * (a * Math.pow(distSquared_1, b) + 1); - } - else if (j === k_1) { - continue; - } - for (var d = 0; d < dim; d++) { - var gradD = 4.0; - if (gradCoeff_1 > 0.0) { - gradD = clip(gradCoeff_1 * (current[d] - other_1[d]), clipValue); - } - current[d] += gradD * alpha; - } - } - epochOfNextNegativeSample[i] += nNegSamples * epochsPerNegativeSample[i]; + var valuesA = a.getValues(); + var rowsA = a.getRows(); + var colsA = a.getCols(); + for (var i = 0; i < valuesA.length; i++) { + var row = rowsA[i]; + var col = colsA[i]; + var key = row + ":" + col; + visited.add(key); + operate(row, col); + } + var valuesB = b.getValues(); + var rowsB = b.getRows(); + var colsB = b.getCols(); + for (var i = 0; i < valuesB.length; i++) { + var row = rowsB[i]; + var col = colsB[i]; + var key = row + ":" + col; + if (visited.has(key)) + continue; + operate(row, col); + } + var dims = [a.nRows, a.nCols]; + return new SparseMatrix(rows, cols, vals, dims); +} +function getCSR(x) { + var entries = []; + x.forEach(function (value, row, col) { + entries.push({ value: value, row: row, col: col }); + }); + entries.sort(function (a, b) { + if (a.row === b.row) { + return a.col - b.col; + } + else { + return a.row - b.col; + } + }); + var indices = []; + var values = []; + var indptr = []; + var currentRow = -1; + for (var i = 0; i < entries.length; i++) { + var _a = entries[i], row = _a.row, col = _a.col, value = _a.value; + if (row !== currentRow) { + currentRow = row; + indptr.push(i); + } + indices.push(col); + values.push(value); + } + return { indices: indices, values: values, indptr: indptr }; +} +exports.getCSR = getCSR; + + +/***/ }), +/* 4 */ +/***/ (function(module, exports, __webpack_require__) { + +"use strict"; + +var __read = (this && this.__read) || function (o, n) { + var m = typeof Symbol === "function" && o[Symbol.iterator]; + if (!m) return o; + var i = m.call(o), r, ar = [], e; + try { + while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); + } + catch (error) { e = { error: error }; } + finally { + try { + if (r && !r.done && (m = i["return"])) m.call(i); + } + finally { if (e) throw e.error; } + } + return ar; +}; +var __spread = (this && this.__spread) || function () { + for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); + return ar; +}; +var __values = (this && this.__values) || function (o) { + var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0; + if (m) return m.call(o); + return { + next: function () { + if (o && i >= o.length) o = void 0; + return { value: o && o[i++], done: !o }; } - optimizationState.alpha = initialAlpha * (1.0 - n / nEpochs); - optimizationState.currentEpoch += 1; - this.embedding = headEmbedding; - return optimizationState.currentEpoch; }; - UMAP.prototype.optimizeLayout = function (epochCallback) { - var _this = this; - if (epochCallback === void 0) { epochCallback = function () { return true; }; } - if (!this.optimizationState.isInitialized) { - this.initializeOptimization(); +}; +Object.defineProperty(exports, "__esModule", { value: true }); +var utils = __webpack_require__(1); +var FlatTree = (function () { + function FlatTree(hyperplanes, offsets, children, indices) { + this.hyperplanes = hyperplanes; + this.offsets = offsets; + this.children = children; + this.indices = indices; + } + return FlatTree; +}()); +exports.FlatTree = FlatTree; +function makeForest(data, nNeighbors, nTrees, random) { + var leafSize = Math.max(10, nNeighbors); + var trees = utils + .range(nTrees) + .map(function (_, i) { return makeTree(data, leafSize, i, random); }); + var forest = trees.map(function (tree) { return flattenTree(tree, leafSize); }); + return forest; +} +exports.makeForest = makeForest; +function makeTree(data, leafSize, n, random) { + if (leafSize === void 0) { leafSize = 30; } + var indices = utils.range(data.length); + var tree = makeEuclideanTree(data, indices, leafSize, n, random); + return tree; +} +function makeEuclideanTree(data, indices, leafSize, q, random) { + if (leafSize === void 0) { leafSize = 30; } + if (indices.length > leafSize) { + var splitResults = euclideanRandomProjectionSplit(data, indices, random); + var indicesLeft = splitResults.indicesLeft, indicesRight = splitResults.indicesRight, hyperplane = splitResults.hyperplane, offset = splitResults.offset; + var leftChild = makeEuclideanTree(data, indicesLeft, leafSize, q + 1, random); + var rightChild = makeEuclideanTree(data, indicesRight, leafSize, q + 1, random); + var node = { leftChild: leftChild, rightChild: rightChild, isLeaf: false, hyperplane: hyperplane, offset: offset }; + return node; + } + else { + var node = { indices: indices, isLeaf: true }; + return node; + } +} +function euclideanRandomProjectionSplit(data, indices, random) { + var dim = data[0].length; + var leftIndex = utils.tauRandInt(indices.length, random); + var rightIndex = utils.tauRandInt(indices.length, random); + rightIndex += leftIndex === rightIndex ? 1 : 0; + rightIndex = rightIndex % indices.length; + var left = indices[leftIndex]; + var right = indices[rightIndex]; + var hyperplaneOffset = 0; + var hyperplaneVector = utils.zeros(dim); + for (var i = 0; i < hyperplaneVector.length; i++) { + hyperplaneVector[i] = data[left][i] - data[right][i]; + hyperplaneOffset -= + (hyperplaneVector[i] * (data[left][i] + data[right][i])) / 2.0; + } + var nLeft = 0; + var nRight = 0; + var side = utils.zeros(indices.length); + for (var i = 0; i < indices.length; i++) { + var margin = hyperplaneOffset; + for (var d = 0; d < dim; d++) { + margin += hyperplaneVector[d] * data[indices[i]][d]; } - return new Promise(function (resolve, reject) { - var step = function () { return __awaiter(_this, void 0, void 0, function () { - var _a, nEpochs, currentEpoch, epochCompleted, shouldStop, isFinished; - return __generator(this, function (_b) { - try { - _a = this.optimizationState, nEpochs = _a.nEpochs, currentEpoch = _a.currentEpoch; - epochCompleted = this.optimizeLayoutStep(currentEpoch); - shouldStop = epochCallback(epochCompleted) === false; - isFinished = epochCompleted === nEpochs; - if (!shouldStop && !isFinished) { - step(); - } - else { - return [2, resolve(isFinished)]; - } - } - catch (err) { - reject(err); - } - return [2]; - }); - }); }; - step(); - }); - }; - UMAP.prototype.getNEpochs = function () { - var graph = this.graph; - if (this.nEpochs > 0) { - return this.nEpochs; + if (margin === 0) { + side[i] = utils.tauRandInt(2, random); + if (side[i] === 0) { + nLeft += 1; + } + else { + nRight += 1; + } } - var length = graph.nRows; - if (length <= 2500) { - return 500; + else if (margin > 0) { + side[i] = 0; + nLeft += 1; } - else if (length <= 5000) { - return 400; + else { + side[i] = 1; + nRight += 1; } - else if (length <= 7500) { - return 300; + } + var indicesLeft = utils.zeros(nLeft); + var indicesRight = utils.zeros(nRight); + nLeft = 0; + nRight = 0; + for (var i in utils.range(side.length)) { + if (side[i] === 0) { + indicesLeft[nLeft] = indices[i]; + nLeft += 1; } else { - return 200; + indicesRight[nRight] = indices[i]; + nRight += 1; } - }; - return UMAP; -}()); -exports.UMAP = UMAP; -function euclidean(x, y) { - var result = 0; - for (var i = 0; i < x.length; i++) { - result += Math.pow((x[i] - y[i]), 2); } - return Math.sqrt(result); + return { + indicesLeft: indicesLeft, + indicesRight: indicesRight, + hyperplane: hyperplaneVector, + offset: hyperplaneOffset, + }; } -exports.euclidean = euclidean; -function cosine(x, y) { - var result = 0.0; - var normX = 0.0; - var normY = 0.0; - for (var i = 0; i < x.length; i++) { - result += x[i] * y[i]; - normX += Math.pow(x[i], 2); - normY += Math.pow(y[i], 2); +function flattenTree(tree, leafSize) { + var nNodes = numNodes(tree); + var nLeaves = numLeaves(tree); + var hyperplanes = utils + .range(nNodes) + .map(function () { return utils.zeros(tree.hyperplane.length); }); + var offsets = utils.zeros(nNodes); + var children = utils.range(nNodes).map(function () { return [-1, -1]; }); + var indices = utils + .range(nLeaves) + .map(function () { return utils.range(leafSize).map(function () { return -1; }); }); + recursiveFlatten(tree, hyperplanes, offsets, children, indices, 0, 0); + return new FlatTree(hyperplanes, offsets, children, indices); +} +function recursiveFlatten(tree, hyperplanes, offsets, children, indices, nodeNum, leafNum) { + var _a; + if (tree.isLeaf) { + children[nodeNum][0] = -leafNum; + (_a = indices[leafNum]).splice.apply(_a, __spread([0, tree.indices.length], tree.indices)); + leafNum += 1; + return { nodeNum: nodeNum, leafNum: leafNum }; } - if (normX === 0 && normY === 0) { - return 0; + else { + hyperplanes[nodeNum] = tree.hyperplane; + offsets[nodeNum] = tree.offset; + children[nodeNum][0] = nodeNum + 1; + var oldNodeNum = nodeNum; + var res = recursiveFlatten(tree.leftChild, hyperplanes, offsets, children, indices, nodeNum + 1, leafNum); + nodeNum = res.nodeNum; + leafNum = res.leafNum; + children[oldNodeNum][1] = nodeNum + 1; + res = recursiveFlatten(tree.rightChild, hyperplanes, offsets, children, indices, nodeNum + 1, leafNum); + return { nodeNum: res.nodeNum, leafNum: res.leafNum }; } - else if (normX === 0 || normY === 0) { - return 1.0; +} +function numNodes(tree) { + if (tree.isLeaf) { + return 1; } else { - return 1.0 - result / Math.sqrt(normX * normY); + return 1 + numNodes(tree.leftChild) + numNodes(tree.rightChild); } } -exports.cosine = cosine; -var OptimizationState = (function () { - function OptimizationState() { - this.currentEpoch = 0; - this.isInitialized = false; - this.headEmbedding = []; - this.tailEmbedding = []; - this.head = []; - this.tail = []; - this.epochsPerSample = []; - this.epochOfNextSample = []; - this.epochOfNextNegativeSample = []; - this.epochsPerNegativeSample = []; - this.moveOther = true; - this.initialAlpha = 1.0; - this.alpha = 1.0; - this.gamma = 1.0; - this.a = 1.5769434603113077; - this.b = 0.8950608779109733; - this.dim = 2; - this.nEpochs = 500; - this.nVertices = 0; +function numLeaves(tree) { + if (tree.isLeaf) { + return 1; } - return OptimizationState; -}()); -function clip(x, clipValue) { - if (x > clipValue) - return clipValue; - else if (x < -clipValue) - return -clipValue; - else - return x; -} -function rDist(x, y) { - var result = 0.0; - for (var i = 0; i < x.length; i++) { - result += Math.pow(x[i] - y[i], 2); + else { + return numLeaves(tree.leftChild) + numLeaves(tree.rightChild); } - return result; -} -function findABParams(spread, minDist) { - var curve = function (_a) { - var _b = __read(_a, 2), a = _b[0], b = _b[1]; - return function (x) { - return 1.0 / (1.0 + a * Math.pow(x, (2 * b))); - }; - }; - var xv = utils - .linear(0, spread * 3, 300) - .map(function (val) { return (val < minDist ? 1.0 : val); }); - var yv = utils.zeros(xv.length).map(function (val, index) { - var gte = xv[index] >= minDist; - return gte ? Math.exp(-(xv[index] - minDist) / spread) : val; - }); - var initialValues = [0.5, 0.5]; - var data = { x: xv, y: yv }; - var options = { - damping: 1.5, - initialValues: initialValues, - gradientDifference: 10e-2, - maxIterations: 100, - errorTolerance: 10e-3, - }; - var parameterValues = LM(data, curve, options).parameterValues; - var _a = __read(parameterValues, 2), a = _a[0], b = _a[1]; - return { a: a, b: b }; } -exports.findABParams = findABParams; -function fastIntersection(graph, target, unknownDist, farDist) { - if (unknownDist === void 0) { unknownDist = 1.0; } - if (farDist === void 0) { farDist = 5.0; } - return graph.map(function (value, row, col) { - if (target[row] === -1 || target[col] === -1) { - return value * Math.exp(-unknownDist); - } - else if (target[row] !== target[col]) { - return value * Math.exp(-farDist); +function makeLeafArray(rpForest) { + var e_1, _a; + if (rpForest.length > 0) { + var output = []; + try { + for (var rpForest_1 = __values(rpForest), rpForest_1_1 = rpForest_1.next(); !rpForest_1_1.done; rpForest_1_1 = rpForest_1.next()) { + var tree = rpForest_1_1.value; + output.push.apply(output, __spread(tree.indices)); + } } - else { - return value; + catch (e_1_1) { e_1 = { error: e_1_1 }; } + finally { + try { + if (rpForest_1_1 && !rpForest_1_1.done && (_a = rpForest_1.return)) _a.call(rpForest_1); + } + finally { if (e_1) throw e_1.error; } } - }); + return output; + } + else { + return [[-1]]; + } } -exports.fastIntersection = fastIntersection; -function resetLocalConnectivity(simplicialSet) { - simplicialSet = matrix.normalize(simplicialSet, "max"); - var transpose = matrix.transpose(simplicialSet); - var prodMatrix = matrix.pairwiseMultiply(transpose, simplicialSet); - simplicialSet = matrix.add(simplicialSet, matrix.subtract(transpose, prodMatrix)); - return matrix.eliminateZeros(simplicialSet); +exports.makeLeafArray = makeLeafArray; +function selectSide(hyperplane, offset, point) { + var margin = offset; + for (var d = 0; d < point.length; d++) { + margin += hyperplane[d] * point[d]; + } + if (margin === 0) { + var side = utils.tauRandInt(2); + return side; + } + else if (margin > 0) { + return 0; + } + else { + return 1; + } } -exports.resetLocalConnectivity = resetLocalConnectivity; +function searchFlatTree(point, tree) { + var node = 0; + while (tree.children[node][0] > 0) { + var side = selectSide(tree.hyperplanes[node], tree.offsets[node], point); + if (side === 0) { + node = tree.children[node][0]; + } + else { + node = tree.children[node][1]; + } + } + var index = -1 * tree.children[node][0]; + return tree.indices[index]; +} +exports.searchFlatTree = searchFlatTree; /***/ }), -/* 4 */ +/* 5 */ +/***/ (function(module, exports, __webpack_require__) { + +"use strict"; + +Object.defineProperty(exports, "__esModule", { value: true }); +var umap_1 = __webpack_require__(6); +window.UMAP = umap_1.UMAP; + + +/***/ }), +/* 6 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __generator = (this && this.__generator) || function (thisArg, body) { + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; + function verb(n) { return function (v) { return step([n, v]); }; } + function step(op) { + if (f) throw new TypeError("Generator is already executing."); + while (_) try { + if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; + if (y = 0, t) op = [op[0] & 2, t.value]; + switch (op[0]) { + case 0: case 1: t = op; break; + case 4: _.label++; return { value: op[1], done: false }; + case 5: _.label++; y = op[1]; op = [0]; continue; + case 7: op = _.ops.pop(); _.trys.pop(); continue; + default: + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } + if (t[2]) _.ops.pop(); + _.trys.pop(); continue; + } + op = body.call(thisArg, _); + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; + } +}; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; @@ -885,531 +1099,695 @@ var __spread = (this && this.__spread) || function () { for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); return ar; }; -var __values = (this && this.__values) || function (o) { - var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0; - if (m) return m.call(o); - return { - next: function () { - if (o && i >= o.length) o = void 0; - return { value: o && o[i++], done: !o }; - } - }; -}; Object.defineProperty(exports, "__esModule", { value: true }); -var _a; +var heap = __webpack_require__(2); +var matrix = __webpack_require__(3); +var nnDescent = __webpack_require__(7); +var tree = __webpack_require__(4); var utils = __webpack_require__(1); -var SparseMatrix = (function () { - function SparseMatrix(rows, cols, values, dims) { - this.entries = new Map(); - this.nRows = 0; - this.nCols = 0; - this.rows = __spread(rows); - this.cols = __spread(cols); - this.values = __spread(values); - for (var i = 0; i < values.length; i++) { - var key = this.makeKey(this.rows[i], this.cols[i]); - this.entries.set(key, i); - } - this.nRows = dims[0]; - this.nCols = dims[0]; - } - SparseMatrix.prototype.makeKey = function (row, col) { - return row + ":" + col; - }; - SparseMatrix.prototype.checkDims = function (row, col) { - var withinBounds = row < this.nRows && col < this.nCols; - if (!withinBounds) { - throw new Error('array index out of bounds'); - } - }; - SparseMatrix.prototype.set = function (row, col, value) { - this.checkDims(row, col); - var key = this.makeKey(row, col); - if (!this.entries.has(key)) { - this.rows.push(row); - this.cols.push(col); - this.values.push(value); - this.entries.set(key, this.values.length - 1); - } - else { - var index = this.entries.get(key); - this.values[index] = value; - } - }; - SparseMatrix.prototype.get = function (row, col, defaultValue) { - if (defaultValue === void 0) { defaultValue = 0; } - this.checkDims(row, col); - var key = this.makeKey(row, col); - if (this.entries.has(key)) { - var index = this.entries.get(key); - return this.values[index]; - } - else { - return defaultValue; - } - }; - SparseMatrix.prototype.getDims = function () { - return [this.nRows, this.nCols]; - }; - SparseMatrix.prototype.getRows = function () { - return __spread(this.rows); - }; - SparseMatrix.prototype.getCols = function () { - return __spread(this.cols); - }; - SparseMatrix.prototype.getValues = function () { - return __spread(this.values); - }; - SparseMatrix.prototype.forEach = function (fn) { - for (var i = 0; i < this.values.length; i++) { - fn(this.values[i], this.rows[i], this.cols[i]); - } - }; - SparseMatrix.prototype.map = function (fn) { - var vals = []; - for (var i = 0; i < this.values.length; i++) { - vals.push(fn(this.values[i], this.rows[i], this.cols[i])); - } - var dims = [this.nRows, this.nCols]; - return new SparseMatrix(this.rows, this.cols, vals, dims); - }; - SparseMatrix.prototype.toArray = function () { +var LM = __webpack_require__(8); +var SMOOTH_K_TOLERANCE = 1e-5; +var MIN_K_DIST_SCALE = 1e-3; +var UMAP = (function () { + function UMAP(params) { + if (params === void 0) { params = {}; } var _this = this; - var rows = utils.empty(this.nRows); - var output = rows.map(function () { - return utils.zeros(_this.nCols); - }); - for (var i = 0; i < this.values.length; i++) { - output[this.rows[i]][this.cols[i]] = this.values[i]; - } - return output; - }; - return SparseMatrix; -}()); -exports.SparseMatrix = SparseMatrix; -function transpose(matrix) { - var cols = []; - var rows = []; - var vals = []; - matrix.forEach(function (value, row, col) { - cols.push(row); - rows.push(col); - vals.push(value); - }); - var dims = [matrix.nCols, matrix.nRows]; - return new SparseMatrix(rows, cols, vals, dims); -} -exports.transpose = transpose; -function identity(size) { - var _a = __read(size, 1), rows = _a[0]; - var matrix = new SparseMatrix([], [], [], size); - for (var i = 0; i < rows; i++) { - matrix.set(i, i, 1); - } - return matrix; -} -exports.identity = identity; -function pairwiseMultiply(a, b) { - return elementWise(a, b, function (x, y) { return x * y; }); -} -exports.pairwiseMultiply = pairwiseMultiply; -function add(a, b) { - return elementWise(a, b, function (x, y) { return x + y; }); -} -exports.add = add; -function subtract(a, b) { - return elementWise(a, b, function (x, y) { return x - y; }); -} -exports.subtract = subtract; -function multiplyScalar(a, scalar) { - return a.map(function (value) { - return value * scalar; - }); -} -exports.multiplyScalar = multiplyScalar; -function eliminateZeros(m) { - var zeroIndices = new Set(); - var values = m.getValues(); - var rows = m.getRows(); - var cols = m.getCols(); - for (var i = 0; i < values.length; i++) { - if (values[i] === 0) { - zeroIndices.add(i); - } + this.learningRate = 1.0; + this.localConnectivity = 1.0; + this.minDist = 0.1; + this.nComponents = 2; + this.nEpochs = 0; + this.nNeighbors = 15; + this.negativeSampleRate = 5; + this.random = Math.random; + this.repulsionStrength = 1.0; + this.setOpMixRatio = 1.0; + this.spread = 1.0; + this.transformQueueSize = 4.0; + this.targetMetric = "categorical"; + this.targetWeight = 0.5; + this.targetNNeighbors = this.nNeighbors; + this.distanceFn = euclidean; + this.isInitialized = false; + this.rpForest = []; + this.embedding = []; + this.optimizationState = new OptimizationState(); + var setParam = function (key) { + if (params[key] !== undefined) + _this[key] = params[key]; + }; + setParam('learningRate'); + setParam('localConnectivity'); + setParam('minDist'); + setParam('nComponents'); + setParam('nEpochs'); + setParam('nNeighbors'); + setParam('negativeSampleRate'); + setParam('random'); + setParam('repulsionStrength'); + setParam('setOpMixRatio'); + setParam('spread'); + setParam('transformQueueSize'); } - var removeByZeroIndex = function (_, index) { return !zeroIndices.has(index); }; - var nextValues = values.filter(removeByZeroIndex); - var nextRows = rows.filter(removeByZeroIndex); - var nextCols = cols.filter(removeByZeroIndex); - return new SparseMatrix(nextRows, nextCols, nextValues, m.getDims()); -} -exports.eliminateZeros = eliminateZeros; -function normalize(m, normType) { - if (normType === void 0) { normType = "l2"; } - var e_1, _a; - var normFn = normFns[normType]; - var colsByRow = new Map(); - m.forEach(function (_, row, col) { - var cols = colsByRow.get(row) || []; - cols.push(col); - colsByRow.set(row, cols); - }); - var nextMatrix = new SparseMatrix([], [], [], m.getDims()); - var _loop_1 = function (row) { - var cols = colsByRow.get(row).sort(); - var vals = cols.map(function (col) { return m.get(row, col); }); - var norm = normFn(vals); - for (var i = 0; i < norm.length; i++) { - nextMatrix.set(row, cols[i], norm[i]); - } + UMAP.prototype.fit = function (X) { + this.initializeFit(X); + this.optimizeLayout(); + return this.embedding; + }; + UMAP.prototype.fitAsync = function (X, callback) { + if (callback === void 0) { callback = function () { return true; }; } + return __awaiter(this, void 0, void 0, function () { + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + this.initializeFit(X); + return [4, this.optimizeLayoutAsync(callback)]; + case 1: + _a.sent(); + return [2, this.embedding]; + } + }); + }); }; - try { - for (var _b = __values(colsByRow.keys()), _c = _b.next(); !_c.done; _c = _b.next()) { - var row = _c.value; - _loop_1(row); + UMAP.prototype.setSupervisedProjection = function (Y, params) { + if (params === void 0) { params = {}; } + this.Y = Y; + this.targetMetric = params.targetMetric || this.targetMetric; + this.targetWeight = params.targetWeight || this.targetWeight; + this.targetNNeighbors = params.targetNNeighbors || this.targetNNeighbors; + }; + UMAP.prototype.setPrecomputedKNN = function (knnIndices, knnDistances) { + this.knnIndices = knnIndices; + this.knnDistances = knnDistances; + }; + UMAP.prototype.initializeFit = function (X) { + if (this.X === X && this.isInitialized) { + return this.getNEpochs(); } - } - catch (e_1_1) { e_1 = { error: e_1_1 }; } - finally { - try { - if (_c && !_c.done && (_a = _b.return)) _a.call(_b); + this.X = X; + if (!this.knnIndices && !this.knnDistances) { + var knnResults = this.nearestNeighbors(X); + this.knnIndices = knnResults.knnIndices; + this.knnDistances = knnResults.knnDistances; } - finally { if (e_1) throw e_1.error; } - } - return nextMatrix; -} -exports.normalize = normalize; -var normFns = (_a = {}, - _a["max"] = function (xs) { - var max = -Infinity; - for (var i = 0; i < xs.length; i++) { - max = xs[i] > max ? xs[i] : max; + this.graph = this.fuzzySimplicialSet(X, this.nNeighbors, this.setOpMixRatio); + this.makeSearchFns(); + this.searchGraph = this.makeSearchGraph(X); + this.processGraphForSupervisedProjection(); + var _a = this.initializeSimplicialSetEmbedding(), head = _a.head, tail = _a.tail, epochsPerSample = _a.epochsPerSample; + this.optimizationState.head = head; + this.optimizationState.tail = tail; + this.optimizationState.epochsPerSample = epochsPerSample; + this.initializeOptimization(); + this.prepareForOptimizationLoop(); + this.isInitialized = true; + return this.getNEpochs(); + }; + UMAP.prototype.makeSearchFns = function () { + var _a = nnDescent.makeInitializations(this.distanceFn), initFromTree = _a.initFromTree, initFromRandom = _a.initFromRandom; + this.initFromTree = initFromTree; + this.initFromRandom = initFromRandom; + this.search = nnDescent.makeInitializedNNSearch(this.distanceFn); + }; + UMAP.prototype.makeSearchGraph = function (X) { + var knnIndices = this.knnIndices; + var knnDistances = this.knnDistances; + var dims = [X.length, X.length]; + var searchGraph = new matrix.SparseMatrix([], [], [], dims); + for (var i = 0; i < knnIndices.length; i++) { + var knn = knnIndices[i]; + var distances = knnDistances[i]; + for (var j = 0; j < knn.length; j++) { + var neighbor = knn[j]; + var distance = distances[j]; + if (distance > 0) { + searchGraph.set(i, neighbor, distance); + } + } } - return xs.map(function (x) { return x / max; }); - }, - _a["l1"] = function (xs) { - var sum = 0; - for (var i = 0; i < xs.length; i++) { - sum += xs[i]; + var transpose = matrix.transpose(searchGraph); + return matrix.maximum(searchGraph, transpose); + }; + UMAP.prototype.transform = function (toTransform) { + var _this = this; + var rawData = this.X; + if (rawData === undefined || rawData.length === 0) { + throw new Error('No data has been fit.'); + } + var nNeighbors = Math.floor(this.nNeighbors * this.transformQueueSize); + var init = nnDescent.initializeSearch(this.rpForest, rawData, toTransform, nNeighbors, this.initFromRandom, this.initFromTree); + var result = this.search(rawData, this.searchGraph, init, toTransform); + var _a = heap.deheapSort(result), indices = _a.indices, distances = _a.weights; + indices = indices.map(function (x) { return x.slice(0, _this.nNeighbors); }); + distances = distances.map(function (x) { return x.slice(0, _this.nNeighbors); }); + var adjustedLocalConnectivity = Math.max(0, this.localConnectivity - 1); + var _b = this.smoothKNNDistance(distances, this.nNeighbors, adjustedLocalConnectivity), sigmas = _b.sigmas, rhos = _b.rhos; + var _c = this.computeMembershipStrengths(indices, distances, sigmas, rhos), rows = _c.rows, cols = _c.cols, vals = _c.vals; + var size = [toTransform.length, rawData.length]; + var graph = new matrix.SparseMatrix(rows, cols, vals, size); + var normed = matrix.normalize(graph, "l1"); + var csrMatrix = matrix.getCSR(normed); + var nPoints = toTransform.length; + var eIndices = utils.reshape2d(csrMatrix.indices, nPoints, this.nNeighbors); + var eWeights = utils.reshape2d(csrMatrix.values, nPoints, this.nNeighbors); + var embedding = initTransform(eIndices, eWeights, this.embedding); + var nEpochs = this.nEpochs + ? this.nEpochs / 3 + : graph.nRows <= 10000 + ? 100 + : 30; + var graphMax = graph + .getValues() + .reduce(function (max, val) { return (val > max ? val : max); }, 0); + graph = graph.map(function (value) { return (value < graphMax / nEpochs ? 0 : value); }); + graph = matrix.eliminateZeros(graph); + var epochsPerSample = this.makeEpochsPerSample(graph.getValues(), nEpochs); + var head = graph.getRows(); + var tail = graph.getCols(); + this.assignOptimizationStateParameters({ + headEmbedding: embedding, + tailEmbedding: this.embedding, + head: head, + tail: tail, + currentEpoch: 0, + nEpochs: nEpochs, + nVertices: graph.getDims()[1], + epochsPerSample: epochsPerSample, + }); + this.prepareForOptimizationLoop(); + return this.optimizeLayout(); + }; + UMAP.prototype.processGraphForSupervisedProjection = function () { + var _a = this, Y = _a.Y, X = _a.X; + if (Y) { + if (Y.length !== X.length) { + throw new Error('Length of X and y must be equal'); + } + if (this.targetMetric === "categorical") { + var lt = this.targetWeight < 1.0; + var farDist = lt ? 2.5 * (1.0 / (1.0 - this.targetWeight)) : 1.0e12; + this.graph = this.categoricalSimplicialSetIntersection(this.graph, Y, farDist); + } } - return xs.map(function (x) { return x / sum; }); - }, - _a["l2"] = function (xs) { - var sum = 0; - for (var i = 0; i < xs.length; i++) { - sum += Math.pow(xs[i], 2); + }; + UMAP.prototype.step = function () { + var currentEpoch = this.optimizationState.currentEpoch; + if (currentEpoch < this.getNEpochs()) { + this.optimizeLayoutStep(currentEpoch); } - return xs.map(function (x) { return Math.sqrt(Math.pow(x, 2) / sum); }); - }, - _a); -function elementWise(a, b, op) { - var visited = new Set(); - var rows = []; - var cols = []; - var vals = []; - var operate = function (row, col) { - rows.push(row); - cols.push(col); - var nextValue = op(a.get(row, col), b.get(row, col)); - vals.push(nextValue); + return this.optimizationState.currentEpoch; }; - var valuesA = a.getValues(); - var rowsA = a.getRows(); - var colsA = a.getCols(); - for (var i = 0; i < valuesA.length; i++) { - var row = rowsA[i]; - var col = colsA[i]; - var key = row + ":" + col; - visited.add(key); - operate(row, col); - } - var valuesB = b.getValues(); - var rowsB = b.getRows(); - var colsB = b.getCols(); - for (var i = 0; i < valuesB.length; i++) { - var row = rowsB[i]; - var col = colsB[i]; - var key = row + ":" + col; - if (visited.has(key)) - continue; - operate(row, col); - } - var dims = [a.nRows, a.nCols]; - return new SparseMatrix(rows, cols, vals, dims); -} - - -/***/ }), -/* 5 */ -/***/ (function(module, exports, __webpack_require__) { - -"use strict"; - -Object.defineProperty(exports, "__esModule", { value: true }); -var heap = __webpack_require__(6); -var utils = __webpack_require__(1); -function makeNNDescent(distanceFn, random) { - return function nNDescent(data, leafArray, nNeighbors, nIters, maxCandidates, delta, rho, rpTreeInit) { - if (nIters === void 0) { nIters = 10; } - if (maxCandidates === void 0) { maxCandidates = 50; } - if (delta === void 0) { delta = 0.001; } - if (rho === void 0) { rho = 0.5; } - if (rpTreeInit === void 0) { rpTreeInit = true; } - var nVertices = data.length; - var currentGraph = heap.makeHeap(data.length, nNeighbors); - for (var i = 0; i < data.length; i++) { - var indices = heap.rejectionSample(nNeighbors, data.length, random); - for (var j = 0; j < indices.length; j++) { - var d = distanceFn(data[i], data[indices[j]]); - heap.heapPush(currentGraph, i, d, indices[j], 1); - heap.heapPush(currentGraph, indices[j], d, i, 1); + UMAP.prototype.getEmbedding = function () { + return this.embedding; + }; + UMAP.prototype.nearestNeighbors = function (X) { + var _a = this, distanceFn = _a.distanceFn, nNeighbors = _a.nNeighbors; + var log2 = function (n) { return Math.log(n) / Math.log(2); }; + var metricNNDescent = nnDescent.makeNNDescent(distanceFn, this.random); + var round = function (n) { + return n === 0.5 ? 0 : Math.round(n); + }; + var nTrees = 5 + Math.floor(round(Math.pow(X.length, 0.5) / 20.0)); + var nIters = Math.max(5, Math.floor(Math.round(log2(X.length)))); + this.rpForest = tree.makeForest(X, nNeighbors, nTrees, this.random); + var leafArray = tree.makeLeafArray(this.rpForest); + var _b = metricNNDescent(X, leafArray, nNeighbors, nIters), indices = _b.indices, weights = _b.weights; + return { knnIndices: indices, knnDistances: weights }; + }; + UMAP.prototype.fuzzySimplicialSet = function (X, nNeighbors, setOpMixRatio) { + if (setOpMixRatio === void 0) { setOpMixRatio = 1.0; } + var _a = this, _b = _a.knnIndices, knnIndices = _b === void 0 ? [] : _b, _c = _a.knnDistances, knnDistances = _c === void 0 ? [] : _c, localConnectivity = _a.localConnectivity; + var _d = this.smoothKNNDistance(knnDistances, nNeighbors, localConnectivity), sigmas = _d.sigmas, rhos = _d.rhos; + var _e = this.computeMembershipStrengths(knnIndices, knnDistances, sigmas, rhos), rows = _e.rows, cols = _e.cols, vals = _e.vals; + var size = [X.length, X.length]; + var sparseMatrix = new matrix.SparseMatrix(rows, cols, vals, size); + var transpose = matrix.transpose(sparseMatrix); + var prodMatrix = matrix.pairwiseMultiply(sparseMatrix, transpose); + var a = matrix.subtract(matrix.add(sparseMatrix, transpose), prodMatrix); + var b = matrix.multiplyScalar(a, setOpMixRatio); + var c = matrix.multiplyScalar(prodMatrix, 1.0 - setOpMixRatio); + var result = matrix.add(b, c); + return result; + }; + UMAP.prototype.categoricalSimplicialSetIntersection = function (simplicialSet, target, farDist, unknownDist) { + if (unknownDist === void 0) { unknownDist = 1.0; } + var intersection = fastIntersection(simplicialSet, target, unknownDist, farDist); + intersection = matrix.eliminateZeros(intersection); + return resetLocalConnectivity(intersection); + }; + UMAP.prototype.smoothKNNDistance = function (distances, k, localConnectivity, nIter, bandwidth) { + if (localConnectivity === void 0) { localConnectivity = 1.0; } + if (nIter === void 0) { nIter = 64; } + if (bandwidth === void 0) { bandwidth = 1.0; } + var target = (Math.log(k) / Math.log(2)) * bandwidth; + var rho = utils.zeros(distances.length); + var result = utils.zeros(distances.length); + for (var i = 0; i < distances.length; i++) { + var lo = 0.0; + var hi = Infinity; + var mid = 1.0; + var ithDistances = distances[i]; + var nonZeroDists = ithDistances.filter(function (d) { return d > 0.0; }); + if (nonZeroDists.length >= localConnectivity) { + var index = Math.floor(localConnectivity); + var interpolation = localConnectivity - index; + if (index > 0) { + rho[i] = nonZeroDists[index - 1]; + if (interpolation > SMOOTH_K_TOLERANCE) { + rho[i] += + interpolation * (nonZeroDists[index] - nonZeroDists[index - 1]); + } + } + else { + rho[i] = interpolation * nonZeroDists[0]; + } } - } - if (rpTreeInit) { - for (var n = 0; n < leafArray.length; n++) { - for (var i = 0; i < leafArray[n].length; i++) { - if (leafArray[n][i] < 0) { - break; + else if (nonZeroDists.length > 0) { + rho[i] = utils.max(nonZeroDists); + } + for (var n = 0; n < nIter; n++) { + var psum = 0.0; + for (var j = 1; j < distances[i].length; j++) { + var d = distances[i][j] - rho[i]; + if (d > 0) { + psum += Math.exp(-(d / mid)); } - for (var j = i + 1; j < leafArray[n].length; j++) { - if (leafArray[n][j] < 0) { - break; - } - var d = distanceFn(data[leafArray[n][i]], data[leafArray[n][j]]); - heap.heapPush(currentGraph, leafArray[n][i], d, leafArray[n][j], 1); - heap.heapPush(currentGraph, leafArray[n][j], d, leafArray[n][i], 1); + else { + psum += 1.0; + } + } + if (Math.abs(psum - target) < SMOOTH_K_TOLERANCE) { + break; + } + if (psum > target) { + hi = mid; + mid = (lo + hi) / 2.0; + } + else { + lo = mid; + if (hi === Infinity) { + mid *= 2; } + else { + mid = (lo + hi) / 2.0; + } + } + } + result[i] = mid; + if (rho[i] > 0.0) { + var meanIthDistances = utils.mean(ithDistances); + if (result[i] < MIN_K_DIST_SCALE * meanIthDistances) { + result[i] = MIN_K_DIST_SCALE * meanIthDistances; + } + } + else { + var meanDistances = utils.mean(distances.map(utils.mean)); + if (result[i] < MIN_K_DIST_SCALE * meanDistances) { + result[i] = MIN_K_DIST_SCALE * meanDistances; } } } - for (var n = 0; n < nIters; n++) { - var candidateNeighbors = heap.buildCandidates(currentGraph, nVertices, nNeighbors, maxCandidates, random); - var c = 0; - for (var i = 0; i < nVertices; i++) { - for (var j = 0; j < maxCandidates; j++) { - var p = Math.floor(candidateNeighbors[0][i][j]); - if (p < 0 || utils.tauRand(random) < rho) { - continue; - } - for (var k = 0; k < maxCandidates; k++) { - var q = Math.floor(candidateNeighbors[0][i][k]); - var cj = candidateNeighbors[2][i][j]; - var ck = candidateNeighbors[2][i][k]; - if (q < 0 || (!cj && !ck)) { - continue; - } - var d = distanceFn(data[p], data[q]); - c += heap.heapPush(currentGraph, p, d, q, 1); - c += heap.heapPush(currentGraph, q, d, p, 1); - } + return { sigmas: result, rhos: rho }; + }; + UMAP.prototype.computeMembershipStrengths = function (knnIndices, knnDistances, sigmas, rhos) { + var nSamples = knnIndices.length; + var nNeighbors = knnIndices[0].length; + var rows = utils.zeros(nSamples * nNeighbors); + var cols = utils.zeros(nSamples * nNeighbors); + var vals = utils.zeros(nSamples * nNeighbors); + for (var i = 0; i < nSamples; i++) { + for (var j = 0; j < nNeighbors; j++) { + var val = 0; + if (knnIndices[i][j] === -1) { + continue; + } + if (knnIndices[i][j] === i) { + val = 0.0; } + else if (knnDistances[i][j] - rhos[i] <= 0.0) { + val = 1.0; + } + else { + val = Math.exp(-((knnDistances[i][j] - rhos[i]) / sigmas[i])); + } + rows[i * nNeighbors + j] = i; + cols[i * nNeighbors + j] = knnIndices[i][j]; + vals[i * nNeighbors + j] = val; } - if (c <= delta * nNeighbors * data.length) { - break; + } + return { rows: rows, cols: cols, vals: vals }; + }; + UMAP.prototype.initializeSimplicialSetEmbedding = function () { + var _this = this; + var nEpochs = this.getNEpochs(); + var nComponents = this.nComponents; + var graphValues = this.graph.getValues(); + var graphMax = 0; + for (var i = 0; i < graphValues.length; i++) { + var value = graphValues[i]; + if (graphMax < graphValues[i]) { + graphMax = value; } } - var sorted = heap.deheapSort(currentGraph); - return sorted; + var graph = this.graph.map(function (value) { + if (value < graphMax / nEpochs) { + return 0; + } + else { + return value; + } + }); + this.embedding = utils.zeros(graph.nRows).map(function () { + return utils.zeros(nComponents).map(function () { + return utils.tauRand(_this.random) * 20 + -10; + }); + }); + var weights = []; + var head = []; + var tail = []; + for (var i = 0; i < graph.nRows; i++) { + for (var j = 0; j < graph.nCols; j++) { + var value = graph.get(i, j); + if (value) { + weights.push(value); + tail.push(i); + head.push(j); + } + } + } + var epochsPerSample = this.makeEpochsPerSample(weights, nEpochs); + return { head: head, tail: tail, epochsPerSample: epochsPerSample }; }; -} -exports.makeNNDescent = makeNNDescent; - - -/***/ }), -/* 6 */ -/***/ (function(module, exports, __webpack_require__) { - -"use strict"; - -Object.defineProperty(exports, "__esModule", { value: true }); -var utils = __webpack_require__(1); -function makeHeap(nPoints, size) { - var makeArrays = function (fillValue) { - return utils.empty(nPoints).map(function () { - return utils.filled(size, fillValue); + UMAP.prototype.makeEpochsPerSample = function (weights, nEpochs) { + var result = utils.filled(weights.length, -1.0); + var max = utils.max(weights); + var nSamples = weights.map(function (w) { return (w / max) * nEpochs; }); + nSamples.forEach(function (n, i) { + if (n > 0) + result[i] = nEpochs / nSamples[i]; }); + return result; }; - var heap = []; - heap.push(makeArrays(-1)); - heap.push(makeArrays(Infinity)); - heap.push(makeArrays(0)); - return heap; -} -exports.makeHeap = makeHeap; -function rejectionSample(nSamples, poolSize, random) { - var result = utils.zeros(nSamples); - for (var i = 0; i < nSamples; i++) { - var rejectSample = true; - var j = 0; - while (rejectSample) { - j = utils.tauRandInt(poolSize, random); - var broken = false; - for (var k = 0; k < i; k++) { - if (j === result[k]) { - broken = true; - break; + UMAP.prototype.assignOptimizationStateParameters = function (state) { + Object.assign(this.optimizationState, state); + }; + UMAP.prototype.prepareForOptimizationLoop = function () { + var _a = this, repulsionStrength = _a.repulsionStrength, learningRate = _a.learningRate, negativeSampleRate = _a.negativeSampleRate; + var _b = this.optimizationState, epochsPerSample = _b.epochsPerSample, headEmbedding = _b.headEmbedding, tailEmbedding = _b.tailEmbedding; + var dim = headEmbedding[0].length; + var moveOther = headEmbedding.length === tailEmbedding.length; + var epochsPerNegativeSample = epochsPerSample.map(function (e) { return e / negativeSampleRate; }); + var epochOfNextNegativeSample = __spread(epochsPerNegativeSample); + var epochOfNextSample = __spread(epochsPerSample); + this.assignOptimizationStateParameters({ + epochOfNextSample: epochOfNextSample, + epochOfNextNegativeSample: epochOfNextNegativeSample, + epochsPerNegativeSample: epochsPerNegativeSample, + moveOther: moveOther, + initialAlpha: learningRate, + alpha: learningRate, + gamma: repulsionStrength, + dim: dim, + }); + }; + UMAP.prototype.initializeOptimization = function () { + var headEmbedding = this.embedding; + var tailEmbedding = this.embedding; + var _a = this.optimizationState, head = _a.head, tail = _a.tail, epochsPerSample = _a.epochsPerSample; + var nEpochs = this.getNEpochs(); + var nVertices = this.graph.nCols; + var _b = findABParams(this.spread, this.minDist), a = _b.a, b = _b.b; + this.assignOptimizationStateParameters({ + headEmbedding: headEmbedding, + tailEmbedding: tailEmbedding, + head: head, + tail: tail, + epochsPerSample: epochsPerSample, + a: a, + b: b, + nEpochs: nEpochs, + nVertices: nVertices, + }); + }; + UMAP.prototype.optimizeLayoutStep = function (n) { + var optimizationState = this.optimizationState; + var head = optimizationState.head, tail = optimizationState.tail, headEmbedding = optimizationState.headEmbedding, tailEmbedding = optimizationState.tailEmbedding, epochsPerSample = optimizationState.epochsPerSample, epochOfNextSample = optimizationState.epochOfNextSample, epochOfNextNegativeSample = optimizationState.epochOfNextNegativeSample, epochsPerNegativeSample = optimizationState.epochsPerNegativeSample, moveOther = optimizationState.moveOther, initialAlpha = optimizationState.initialAlpha, alpha = optimizationState.alpha, gamma = optimizationState.gamma, a = optimizationState.a, b = optimizationState.b, dim = optimizationState.dim, nEpochs = optimizationState.nEpochs, nVertices = optimizationState.nVertices; + var clipValue = 4.0; + for (var i = 0; i < epochsPerSample.length; i++) { + if (epochOfNextSample[i] > n) { + continue; + } + var j = head[i]; + var k = tail[i]; + var current = headEmbedding[j]; + var other = tailEmbedding[k]; + var distSquared = rDist(current, other); + var gradCoeff = 0; + if (distSquared > 0) { + gradCoeff = -2.0 * a * b * Math.pow(distSquared, b - 1.0); + gradCoeff /= a * Math.pow(distSquared, b) + 1.0; + } + for (var d = 0; d < dim; d++) { + var gradD = clip(gradCoeff * (current[d] - other[d]), clipValue); + current[d] += gradD * alpha; + if (moveOther) { + other[d] += -gradD * alpha; } } - if (!broken) - rejectSample = false; + epochOfNextSample[i] += epochsPerSample[i]; + var nNegSamples = Math.floor((n - epochOfNextNegativeSample[i]) / epochsPerNegativeSample[i]); + for (var p = 0; p < nNegSamples; p++) { + var k_1 = utils.tauRandInt(nVertices, this.random); + var other_1 = tailEmbedding[k_1]; + var distSquared_1 = rDist(current, other_1); + var gradCoeff_1 = 0.0; + if (distSquared_1 > 0.0) { + gradCoeff_1 = 2.0 * gamma * b; + gradCoeff_1 /= + (0.001 + distSquared_1) * (a * Math.pow(distSquared_1, b) + 1); + } + else if (j === k_1) { + continue; + } + for (var d = 0; d < dim; d++) { + var gradD = 4.0; + if (gradCoeff_1 > 0.0) { + gradD = clip(gradCoeff_1 * (current[d] - other_1[d]), clipValue); + } + current[d] += gradD * alpha; + } + } + epochOfNextNegativeSample[i] += nNegSamples * epochsPerNegativeSample[i]; } - result[i] = j; - } - return result; -} -exports.rejectionSample = rejectionSample; -function heapPush(heap, row, weight, index, flag) { - row = Math.floor(row); - var indices = heap[0][row]; - var weights = heap[1][row]; - var isNew = heap[2][row]; - if (weight >= weights[0]) { - return 0; - } - for (var i_1 = 0; i_1 < indices.length; i_1++) { - if (index === indices[i_1]) { - return 0; + optimizationState.alpha = initialAlpha * (1.0 - n / nEpochs); + optimizationState.currentEpoch += 1; + return headEmbedding; + }; + UMAP.prototype.optimizeLayoutAsync = function (epochCallback) { + var _this = this; + if (epochCallback === void 0) { epochCallback = function () { return true; }; } + return new Promise(function (resolve, reject) { + var step = function () { return __awaiter(_this, void 0, void 0, function () { + var _a, nEpochs, currentEpoch, epochCompleted, shouldStop, isFinished; + return __generator(this, function (_b) { + try { + _a = this.optimizationState, nEpochs = _a.nEpochs, currentEpoch = _a.currentEpoch; + this.embedding = this.optimizeLayoutStep(currentEpoch); + epochCompleted = this.optimizationState.currentEpoch; + shouldStop = epochCallback(epochCompleted) === false; + isFinished = epochCompleted === nEpochs; + if (!shouldStop && !isFinished) { + step(); + } + else { + return [2, resolve(isFinished)]; + } + } + catch (err) { + reject(err); + } + return [2]; + }); + }); }; + step(); + }); + }; + UMAP.prototype.optimizeLayout = function (epochCallback) { + if (epochCallback === void 0) { epochCallback = function () { return true; }; } + var isFinished = false; + var embedding = []; + while (!isFinished) { + var _a = this.optimizationState, nEpochs = _a.nEpochs, currentEpoch = _a.currentEpoch; + embedding = this.optimizeLayoutStep(currentEpoch); + var epochCompleted = this.optimizationState.currentEpoch; + var shouldStop = epochCallback(epochCompleted) === false; + isFinished = epochCompleted === nEpochs || shouldStop; + } + return embedding; + }; + UMAP.prototype.getNEpochs = function () { + var graph = this.graph; + if (this.nEpochs > 0) { + return this.nEpochs; } - } - weights[0] = weight; - indices[0] = index; - isNew[0] = flag; - var i = 0; - var iSwap = 0; - while (true) { - var ic1 = 2 * i + 1; - var ic2 = ic1 + 1; - var heapShape2 = heap[0][0].length; - if (ic1 >= heapShape2) { - break; + var length = graph.nRows; + if (length <= 2500) { + return 500; } - else if (ic2 >= heapShape2) { - if (weights[ic1] > weight) { - iSwap = ic1; - } - else { - break; - } + else if (length <= 5000) { + return 400; } - else if (weights[ic1] >= weights[ic2]) { - if (weight < weights[ic1]) { - iSwap = ic1; - } - else { - break; - } + else if (length <= 7500) { + return 300; } else { - if (weight < weights[ic2]) { - iSwap = ic2; - } - else { - break; - } + return 200; } - weights[i] = weights[iSwap]; - indices[i] = indices[iSwap]; - isNew[i] = isNew[iSwap]; - i = iSwap; + }; + return UMAP; +}()); +exports.UMAP = UMAP; +function euclidean(x, y) { + var result = 0; + for (var i = 0; i < x.length; i++) { + result += Math.pow((x[i] - y[i]), 2); } - weights[i] = weight; - indices[i] = index; - isNew[i] = flag; - return 1; + return Math.sqrt(result); } -exports.heapPush = heapPush; -function buildCandidates(currentGraph, nVertices, nNeighbors, maxCandidates, random) { - var candidateNeighbors = makeHeap(nVertices, maxCandidates); - for (var i = 0; i < nVertices; i++) { - for (var j = 0; j < nNeighbors; j++) { - if (currentGraph[0][i][j] < 0) { - continue; - } - var idx = currentGraph[0][i][j]; - var isn = currentGraph[2][i][j]; - var d = utils.tauRand(random); - heapPush(candidateNeighbors, i, d, idx, isn); - heapPush(candidateNeighbors, idx, d, i, isn); - currentGraph[2][i][j] = 0; - } +exports.euclidean = euclidean; +function cosine(x, y) { + var result = 0.0; + var normX = 0.0; + var normY = 0.0; + for (var i = 0; i < x.length; i++) { + result += x[i] * y[i]; + normX += Math.pow(x[i], 2); + normY += Math.pow(y[i], 2); + } + if (normX === 0 && normY === 0) { + return 0; + } + else if (normX === 0 || normY === 0) { + return 1.0; + } + else { + return 1.0 - result / Math.sqrt(normX * normY); } - return candidateNeighbors; } -exports.buildCandidates = buildCandidates; -function deheapSort(heap) { - var indices = heap[0]; - var weights = heap[1]; - for (var i = 0; i < indices.length; i++) { - var indHeap = indices[i]; - var distHeap = weights[i]; - for (var j = 0; j < indHeap.length - 1; j++) { - var indHeapIndex = indHeap.length - j - 1; - var distHeapIndex = distHeap.length - j - 1; - var temp1 = indHeap[0]; - indHeap[0] = indHeap[indHeapIndex]; - indHeap[indHeapIndex] = temp1; - var temp2 = distHeap[0]; - distHeap[0] = distHeap[distHeapIndex]; - distHeap[distHeapIndex] = temp2; - siftDown(distHeap, indHeap, distHeapIndex, 0); - } +exports.cosine = cosine; +var OptimizationState = (function () { + function OptimizationState() { + this.currentEpoch = 0; + this.headEmbedding = []; + this.tailEmbedding = []; + this.head = []; + this.tail = []; + this.epochsPerSample = []; + this.epochOfNextSample = []; + this.epochOfNextNegativeSample = []; + this.epochsPerNegativeSample = []; + this.moveOther = true; + this.initialAlpha = 1.0; + this.alpha = 1.0; + this.gamma = 1.0; + this.a = 1.5769434603113077; + this.b = 0.8950608779109733; + this.dim = 2; + this.nEpochs = 500; + this.nVertices = 0; } - return { indices: indices, weights: weights }; + return OptimizationState; +}()); +function clip(x, clipValue) { + if (x > clipValue) + return clipValue; + else if (x < -clipValue) + return -clipValue; + else + return x; } -exports.deheapSort = deheapSort; -function siftDown(heap1, heap2, ceiling, elt) { - while (elt * 2 + 1 < ceiling) { - var leftChild = elt * 2 + 1; - var rightChild = leftChild + 1; - var swap = elt; - if (heap1[swap] < heap1[leftChild]) { - swap = leftChild; - } - if (rightChild < ceiling && heap1[swap] < heap1[rightChild]) { - swap = rightChild; +function rDist(x, y) { + var result = 0.0; + for (var i = 0; i < x.length; i++) { + result += Math.pow(x[i] - y[i], 2); + } + return result; +} +function findABParams(spread, minDist) { + var curve = function (_a) { + var _b = __read(_a, 2), a = _b[0], b = _b[1]; + return function (x) { + return 1.0 / (1.0 + a * Math.pow(x, (2 * b))); + }; + }; + var xv = utils + .linear(0, spread * 3, 300) + .map(function (val) { return (val < minDist ? 1.0 : val); }); + var yv = utils.zeros(xv.length).map(function (val, index) { + var gte = xv[index] >= minDist; + return gte ? Math.exp(-(xv[index] - minDist) / spread) : val; + }); + var initialValues = [0.5, 0.5]; + var data = { x: xv, y: yv }; + var options = { + damping: 1.5, + initialValues: initialValues, + gradientDifference: 10e-2, + maxIterations: 100, + errorTolerance: 10e-3, + }; + var parameterValues = LM(data, curve, options).parameterValues; + var _a = __read(parameterValues, 2), a = _a[0], b = _a[1]; + return { a: a, b: b }; +} +exports.findABParams = findABParams; +function fastIntersection(graph, target, unknownDist, farDist) { + if (unknownDist === void 0) { unknownDist = 1.0; } + if (farDist === void 0) { farDist = 5.0; } + return graph.map(function (value, row, col) { + if (target[row] === -1 || target[col] === -1) { + return value * Math.exp(-unknownDist); } - if (swap === elt) { - break; + else if (target[row] !== target[col]) { + return value * Math.exp(-farDist); } else { - var temp1 = heap1[elt]; - heap1[elt] = heap1[swap]; - heap1[swap] = temp1; - var temp2 = heap2[elt]; - heap2[elt] = heap2[swap]; - heap2[swap] = temp2; - elt = swap; + return value; } - } + }); } - - -/***/ }), -/* 7 */ -/***/ (function(module, exports, __webpack_require__) { - -"use strict"; - -var __read = (this && this.__read) || function (o, n) { - var m = typeof Symbol === "function" && o[Symbol.iterator]; - if (!m) return o; - var i = m.call(o), r, ar = [], e; - try { - while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); - } - catch (error) { e = { error: error }; } - finally { - try { - if (r && !r.done && (m = i["return"])) m.call(i); +exports.fastIntersection = fastIntersection; +function resetLocalConnectivity(simplicialSet) { + simplicialSet = matrix.normalize(simplicialSet, "max"); + var transpose = matrix.transpose(simplicialSet); + var prodMatrix = matrix.pairwiseMultiply(transpose, simplicialSet); + simplicialSet = matrix.add(simplicialSet, matrix.subtract(transpose, prodMatrix)); + return matrix.eliminateZeros(simplicialSet); +} +exports.resetLocalConnectivity = resetLocalConnectivity; +function initTransform(indices, weights, embedding) { + var result = utils + .zeros(indices.length) + .map(function (z) { return utils.zeros(embedding[0].length); }); + for (var i = 0; i < indices.length; i++) { + for (var j = 0; j < indices[0].length; j++) { + for (var d = 0; d < embedding[0].length; d++) { + var a = indices[i][j]; + result[i][d] += weights[i][j] * embedding[a][d]; + } } - finally { if (e) throw e.error; } } - return ar; -}; -var __spread = (this && this.__spread) || function () { - for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); - return ar; -}; + return result; +} +exports.initTransform = initTransform; + + +/***/ }), +/* 7 */ +/***/ (function(module, exports, __webpack_require__) { + +"use strict"; + var __values = (this && this.__values) || function (o) { var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0; if (m) return m.call(o); @@ -1421,184 +1799,164 @@ var __values = (this && this.__values) || function (o) { }; }; Object.defineProperty(exports, "__esModule", { value: true }); +var heap = __webpack_require__(2); +var matrix = __webpack_require__(3); +var tree = __webpack_require__(4); var utils = __webpack_require__(1); -var FlatTree = (function () { - function FlatTree(hyperplanes, offsets, children, indices) { - this.hyperplanes = hyperplanes; - this.offsets = offsets; - this.children = children; - this.indices = indices; - } - return FlatTree; -}()); -exports.FlatTree = FlatTree; -function makeForest(data, nNeighbors, nTrees, random) { - var leafSize = Math.max(10, nNeighbors); - var trees = utils - .range(nTrees) - .map(function (_, i) { return makeTree(data, leafSize, i, random); }); - var forest = trees.map(function (tree) { return flattenTree(tree, leafSize); }); - return forest; -} -exports.makeForest = makeForest; -function makeTree(data, leafSize, n, random) { - if (leafSize === void 0) { leafSize = 30; } - var indices = utils.range(data.length); - var tree = makeEuclideanTree(data, indices, leafSize, n, random); - return tree; -} -function makeEuclideanTree(data, indices, leafSize, q, random) { - if (leafSize === void 0) { leafSize = 30; } - if (indices.length > leafSize) { - var splitResults = euclideanRandomProjectionSplit(data, indices, random); - var indicesLeft = splitResults.indicesLeft, indicesRight = splitResults.indicesRight, hyperplane = splitResults.hyperplane, offset = splitResults.offset; - var leftChild = makeEuclideanTree(data, indicesLeft, leafSize, q + 1, random); - var rightChild = makeEuclideanTree(data, indicesRight, leafSize, q + 1, random); - var node = { leftChild: leftChild, rightChild: rightChild, isLeaf: false, hyperplane: hyperplane, offset: offset }; - return node; - } - else { - var node = { indices: indices, isLeaf: true }; - return node; - } -} -function euclideanRandomProjectionSplit(data, indices, random) { - var dim = data[0].length; - var leftIndex = utils.tauRandInt(indices.length, random); - var rightIndex = utils.tauRandInt(indices.length, random); - rightIndex += leftIndex === rightIndex ? 1 : 0; - rightIndex = rightIndex % indices.length; - var left = indices[leftIndex]; - var right = indices[rightIndex]; - var hyperplaneOffset = 0; - var hyperplaneVector = utils.zeros(dim); - for (var i = 0; i < hyperplaneVector.length; i++) { - hyperplaneVector[i] = data[left][i] - data[right][i]; - hyperplaneOffset -= - (hyperplaneVector[i] * (data[left][i] + data[right][i])) / 2.0; - } - var nLeft = 0; - var nRight = 0; - var side = utils.zeros(indices.length); - for (var i = 0; i < indices.length; i++) { - var margin = hyperplaneOffset; - for (var d = 0; d < dim; d++) { - margin += hyperplaneVector[d] * data[indices[i]][d]; - } - if (margin === 0) { - side[i] = utils.tauRandInt(2, random); - if (side[i] === 0) { - nLeft += 1; - } - else { - nRight += 1; +function makeNNDescent(distanceFn, random) { + return function nNDescent(data, leafArray, nNeighbors, nIters, maxCandidates, delta, rho, rpTreeInit) { + if (nIters === void 0) { nIters = 10; } + if (maxCandidates === void 0) { maxCandidates = 50; } + if (delta === void 0) { delta = 0.001; } + if (rho === void 0) { rho = 0.5; } + if (rpTreeInit === void 0) { rpTreeInit = true; } + var nVertices = data.length; + var currentGraph = heap.makeHeap(data.length, nNeighbors); + for (var i = 0; i < data.length; i++) { + var indices = heap.rejectionSample(nNeighbors, data.length, random); + for (var j = 0; j < indices.length; j++) { + var d = distanceFn(data[i], data[indices[j]]); + heap.heapPush(currentGraph, i, d, indices[j], 1); + heap.heapPush(currentGraph, indices[j], d, i, 1); } } - else if (margin > 0) { - side[i] = 0; - nLeft += 1; + if (rpTreeInit) { + for (var n = 0; n < leafArray.length; n++) { + for (var i = 0; i < leafArray[n].length; i++) { + if (leafArray[n][i] < 0) { + break; + } + for (var j = i + 1; j < leafArray[n].length; j++) { + if (leafArray[n][j] < 0) { + break; + } + var d = distanceFn(data[leafArray[n][i]], data[leafArray[n][j]]); + heap.heapPush(currentGraph, leafArray[n][i], d, leafArray[n][j], 1); + heap.heapPush(currentGraph, leafArray[n][j], d, leafArray[n][i], 1); + } + } + } } - else { - side[i] = 1; - nRight += 1; + for (var n = 0; n < nIters; n++) { + var candidateNeighbors = heap.buildCandidates(currentGraph, nVertices, nNeighbors, maxCandidates, random); + var c = 0; + for (var i = 0; i < nVertices; i++) { + for (var j = 0; j < maxCandidates; j++) { + var p = Math.floor(candidateNeighbors[0][i][j]); + if (p < 0 || utils.tauRand(random) < rho) { + continue; + } + for (var k = 0; k < maxCandidates; k++) { + var q = Math.floor(candidateNeighbors[0][i][k]); + var cj = candidateNeighbors[2][i][j]; + var ck = candidateNeighbors[2][i][k]; + if (q < 0 || (!cj && !ck)) { + continue; + } + var d = distanceFn(data[p], data[q]); + c += heap.heapPush(currentGraph, p, d, q, 1); + c += heap.heapPush(currentGraph, q, d, p, 1); + } + } + } + if (c <= delta * nNeighbors * data.length) { + break; + } } - } - var indicesLeft = utils.zeros(nLeft); - var indicesRight = utils.zeros(nRight); - nLeft = 0; - nRight = 0; - for (var i in utils.range(side.length)) { - if (side[i] === 0) { - indicesLeft[nLeft] = indices[i]; - nLeft += 1; + var sorted = heap.deheapSort(currentGraph); + return sorted; + }; +} +exports.makeNNDescent = makeNNDescent; +function makeInitializations(distanceFn) { + var initFromRandom = function (nNeighbors, data, queryPoints, _heap) { + for (var i = 0; i < queryPoints.length; i++) { + var indices = utils.rejectionSample(nNeighbors, data.length); + for (var j = 0; j < indices.length; j++) { + if (indices[j] < 0) { + continue; + } + var d = distanceFn(data[indices[j]], queryPoints[i]); + heap.heapPush(_heap, i, d, indices[j], 1); + } } - else { - indicesRight[nRight] = indices[i]; - nRight += 1; + }; + var initFromTree = function (_tree, data, queryPoints, _heap) { + for (var i = 0; i < queryPoints.length; i++) { + var indices = tree.searchFlatTree(queryPoints[i], _tree); + for (var j = 0; j < indices.length; j++) { + if (indices[j] < 0) { + return; + } + var d = distanceFn(data[indices[j]], queryPoints[i]); + heap.heapPush(_heap, i, d, indices[j], 1); + } } - } - return { - indicesLeft: indicesLeft, - indicesRight: indicesRight, - hyperplane: hyperplaneVector, - offset: hyperplaneOffset, + return; }; + return { initFromRandom: initFromRandom, initFromTree: initFromTree }; } -function flattenTree(tree, leafSize) { - var nNodes = numNodes(tree); - var nLeaves = numLeaves(tree); - var hyperplanes = utils - .range(nNodes) - .map(function () { return utils.zeros(tree.hyperplane.length); }); - var offsets = utils.zeros(nNodes); - var children = utils.range(nNodes).map(function () { return [-1, -1]; }); - var indices = utils - .range(nLeaves) - .map(function () { return utils.range(leafSize).map(function () { return -1; }); }); - recursiveFlatten(tree, hyperplanes, offsets, children, indices, 0, 0); - return new FlatTree(hyperplanes, offsets, children, indices); -} -function recursiveFlatten(tree, hyperplanes, offsets, children, indices, nodeNum, leafNum) { - var _a; - if (tree.isLeaf) { - children[nodeNum][0] = -leafNum; - (_a = indices[leafNum]).splice.apply(_a, __spread([0, tree.indices.length], tree.indices)); - leafNum += 1; - return { nodeNum: nodeNum, leafNum: leafNum }; - } - else { - hyperplanes[nodeNum] = tree.hyperplane; - offsets[nodeNum] = tree.offset; - children[nodeNum][0] = nodeNum + 1; - var oldNodeNum = nodeNum; - var res = recursiveFlatten(tree.leftChild, hyperplanes, offsets, children, indices, nodeNum + 1, leafNum); - nodeNum = res.nodeNum; - leafNum = res.leafNum; - children[oldNodeNum][1] = nodeNum + 1; - res = recursiveFlatten(tree.rightChild, hyperplanes, offsets, children, indices, nodeNum + 1, leafNum); - return { nodeNum: res.nodeNum, leafNum: res.leafNum }; - } -} -function numNodes(tree) { - if (tree.isLeaf) { - return 1; - } - else { - return 1 + numNodes(tree.leftChild) + numNodes(tree.rightChild); - } -} -function numLeaves(tree) { - if (tree.isLeaf) { - return 1; - } - else { - return numLeaves(tree.leftChild) + numLeaves(tree.rightChild); - } +exports.makeInitializations = makeInitializations; +function makeInitializedNNSearch(distanceFn) { + return function (data, graph, initialization, queryPoints) { + var e_1, _a; + var _b = matrix.getCSR(graph), indices = _b.indices, indptr = _b.indptr; + for (var i = 0; i < queryPoints.length; i++) { + var tried = new Set(initialization[0][i]); + while (true) { + var vertex = heap.smallestFlagged(initialization, i); + if (vertex === -1) { + break; + } + var candidates = indices.slice(indptr[vertex], indptr[vertex + 1]); + try { + for (var candidates_1 = __values(candidates), candidates_1_1 = candidates_1.next(); !candidates_1_1.done; candidates_1_1 = candidates_1.next()) { + var candidate = candidates_1_1.value; + if (candidate === vertex || + candidate === -1 || + tried.has(candidate)) { + continue; + } + var d = distanceFn(data[candidate], queryPoints[i]); + heap.uncheckedHeapPush(initialization, i, d, candidate, 1); + tried.add(candidate); + } + } + catch (e_1_1) { e_1 = { error: e_1_1 }; } + finally { + try { + if (candidates_1_1 && !candidates_1_1.done && (_a = candidates_1.return)) _a.call(candidates_1); + } + finally { if (e_1) throw e_1.error; } + } + } + } + return initialization; + }; } -function makeLeafArray(rpForest) { - var e_1, _a; - if (rpForest.length > 0) { - var output = []; +exports.makeInitializedNNSearch = makeInitializedNNSearch; +function initializeSearch(forest, data, queryPoints, nNeighbors, initFromRandom, initFromTree) { + var e_2, _a; + var results = heap.makeHeap(queryPoints.length, nNeighbors); + initFromRandom(nNeighbors, data, queryPoints, results); + if (forest) { try { - for (var rpForest_1 = __values(rpForest), rpForest_1_1 = rpForest_1.next(); !rpForest_1_1.done; rpForest_1_1 = rpForest_1.next()) { - var tree = rpForest_1_1.value; - output.push.apply(output, __spread(tree.indices)); + for (var forest_1 = __values(forest), forest_1_1 = forest_1.next(); !forest_1_1.done; forest_1_1 = forest_1.next()) { + var tree_1 = forest_1_1.value; + initFromTree(tree_1, data, queryPoints, results); } } - catch (e_1_1) { e_1 = { error: e_1_1 }; } + catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { - if (rpForest_1_1 && !rpForest_1_1.done && (_a = rpForest_1.return)) _a.call(rpForest_1); + if (forest_1_1 && !forest_1_1.done && (_a = forest_1.return)) _a.call(forest_1); } - finally { if (e_1) throw e_1.error; } + finally { if (e_2) throw e_2.error; } } - return output; - } - else { - return [[-1]]; } + return results; } -exports.makeLeafArray = makeLeafArray; +exports.initializeSearch = initializeSearch; /***/ }), diff --git a/lib/umap-js.min.js b/lib/umap-js.min.js index 6494325..767059c 100644 --- a/lib/umap-js.min.js +++ b/lib/umap-js.min.js @@ -1 +1 @@ -!function(t){var r={};function e(n){if(r[n])return r[n].exports;var i=r[n]={i:n,l:!1,exports:{}};return t[n].call(i.exports,i,i.exports,e),i.l=!0,i.exports}e.m=t,e.c=r,e.d=function(t,r,n){e.o(t,r)||Object.defineProperty(t,r,{enumerable:!0,get:n})},e.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},e.t=function(t,r){if(1&r&&(t=e(t)),8&r)return t;if(4&r&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(e.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&r&&"string"!=typeof t)for(var i in t)e.d(n,i,function(r){return t[r]}.bind(null,i));return n},e.n=function(t){var r=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(r,"a",r),r},e.o=function(t,r){return Object.prototype.hasOwnProperty.call(t,r)},e.p="",e(e.s=2)}([function(t,r,e){"use strict";const n=Object.prototype.toString;t.exports=function(t){return n.call(t).endsWith("Array]")}},function(t,r,e){"use strict";var n=this&&this.__values||function(t){var r="function"==typeof Symbol&&t[Symbol.iterator],e=0;return r?r.call(t):{next:function(){return t&&e>=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}};function i(t){return void 0===t&&(t=Math.random),t()}function o(t){for(var r=[],e=0;er?t[e]:r;return r},r.max2d=function(t){for(var r=0,e=0;er?t[e][n]:r;return r}},function(t,r,e){"use strict";Object.defineProperty(r,"__esModule",{value:!0});var n=e(3);window.UMAP=n.UMAP},function(t,r,e){"use strict";var n=this&&this.__awaiter||function(t,r,e,n){return new(e||(e=Promise))(function(i,o){function s(t){try{h(n.next(t))}catch(t){o(t)}}function a(t){try{h(n.throw(t))}catch(t){o(t)}}function h(t){t.done?i(t.value):new e(function(r){r(t.value)}).then(s,a)}h((n=n.apply(t,r||[])).next())})},i=this&&this.__generator||function(t,r){var e,n,i,o,s={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function a(o){return function(a){return function(o){if(e)throw new TypeError("Generator is already executing.");for(;s;)try{if(e=1,n&&(i=2&o[0]?n.return:o[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,o[1])).done)return i;switch(n=0,i&&(o=[2&o[0],i.value]),o[0]){case 0:case 1:i=o;break;case 4:return s.label++,{value:o[1],done:!1};case 5:s.label++,n=o[1],o=[0];continue;case 7:o=s.ops.pop(),s.trys.pop();continue;default:if(!(i=(i=s.trys).length>0&&i[i.length-1])&&(6===o[0]||2===o[0])){s=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},s=this&&this.__spread||function(){for(var t=[],r=0;r0});if(g.length>=e){var v=Math.floor(e),p=e-v;v>0?(s[h]=g[v-1],p>1e-5&&(s[h]+=p*(g[v]-g[v-1]))):s[h]=p*g[0]}else g.length>0&&(s[h]=l.max(g));for(var w=0;w0?Math.exp(-b/c):1}if(Math.abs(d-o)<1e-5)break;d>o?c=(u+(f=c))/2:(u=c,f===1/0?c*=2:c=(u+f)/2)}if(a[h]=c,s[h]>0){var M=l.mean(m);a[h]<.001*M&&(a[h]=.001*M)}else{var x=l.mean(t.map(l.mean));a[h]<.001*x&&(a[h]=.001*x)}}return{sigmas:a,rhos:s}},t.prototype.computeMembershipStrengths=function(t,r,e,n){for(var i=t.length,o=t[0].length,s=l.zeros(i*o),a=l.zeros(i*o),h=l.zeros(i*o),u=0;u0&&(e[n]=r/i[n])}),e},t.prototype.initializeOptimization=function(){var t=this.embedding,r=this.embedding,e=this.optimizationState,n=e.head,i=e.tail,o=e.epochsPerSample,a=this.getNEpochs(),h=this.graph.nCols,u=w(this.spread,this.minDist),l=u.a,f=u.b,c=t[0].length,m=t.length===r.length,g=o.map(function(t){return t/5}),v=s(g),p=s(o);Object.assign(this.optimizationState,{isInitialized:!0,headEmbedding:t,tailEmbedding:r,head:n,tail:i,epochsPerSample:o,epochOfNextSample:p,epochOfNextNegativeSample:v,epochsPerNegativeSample:g,moveOther:m,initialAlpha:1,alpha:1,gamma:1,a:l,b:f,dim:c,nEpochs:a,nVertices:h})},t.prototype.optimizeLayoutStep=function(t){for(var r=this.optimizationState,e=r.head,n=r.tail,i=r.headEmbedding,o=r.tailEmbedding,s=r.epochsPerSample,a=r.epochOfNextSample,h=r.epochOfNextNegativeSample,u=r.epochsPerNegativeSample,f=r.moveOther,c=r.initialAlpha,m=r.alpha,g=r.gamma,w=r.a,d=r.b,y=r.dim,b=r.nEpochs,M=r.nVertices,x=0;xt)){var S=e[x],E=n[x],R=i[S],k=o[E],A=p(R,k),N=0;A>0&&(N=-2*w*d*Math.pow(A,d-1),N/=w*Math.pow(A,d)+1);for(var V=0;V0)_=2*g*d,_/=(.001+D)*(w*Math.pow(D,d)+1);else if(S===j)continue;for(V=0;V0&&(z=v(_*(R[V]-P[V]),4)),R[V]+=z*m}}h[x]+=C*u[x]}return r.alpha=c*(1-t/b),r.currentEpoch+=1,this.embedding=i,r.currentEpoch},t.prototype.optimizeLayout=function(t){var r=this;return void 0===t&&(t=function(){return!0}),this.optimizationState.isInitialized||this.initializeOptimization(),new Promise(function(e,o){var s=function(){return n(r,void 0,void 0,function(){var r,n,a,h,u,l;return i(this,function(i){try{if(r=this.optimizationState,n=r.nEpochs,a=r.currentEpoch,h=this.optimizeLayoutStep(a),u=!1===t(h),l=h===n,u||l)return[2,e(l)];s()}catch(t){o(t)}return[2]})})};s()})},t.prototype.getNEpochs=function(){var t=this.graph;if(this.nEpochs>0)return this.nEpochs;var r=t.nRows;return r<=2500?500:r<=5e3?400:r<=7500?300:200},t}();function m(t,r){for(var e=0,n=0;nr?r:t<-r?-r:t}function p(t,r){for(var e=0,n=0;n=r?Math.exp(-(e[i]-r)/t):n}),i=f({x:e,y:n},function(t){var r=o(t,2),e=r[0],n=r[1];return function(t){return 1/(1+e*Math.pow(t,2*n))}},{damping:1.5,initialValues:[.5,.5],gradientDifference:.1,maxIterations:100,errorTolerance:.01}).parameterValues,s=o(i,2);return{a:s[0],b:s[1]}}function d(t,r,e,n){return void 0===e&&(e=1),void 0===n&&(n=5),t.map(function(t,i,o){return-1===r[i]||-1===r[o]?t*Math.exp(-e):r[i]!==r[o]?t*Math.exp(-n):t})}function y(t){t=a.normalize(t,"max");var r=a.transpose(t),e=a.pairwiseMultiply(r,t);return t=a.add(t,a.subtract(r,e)),a.eliminateZeros(t)}r.findABParams=w,r.fastIntersection=d,r.resetLocalConnectivity=y},function(t,r,e){"use strict";var n,i=this&&this.__read||function(t,r){var e="function"==typeof Symbol&&t[Symbol.iterator];if(!e)return t;var n,i,o=e.call(t),s=[];try{for(;(void 0===r||r-- >0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},o=this&&this.__spread||function(){for(var t=[],r=0;r=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}};Object.defineProperty(r,"__esModule",{value:!0});var a=e(1),h=function(){function t(t,r,e,n){this.entries=new Map,this.nRows=0,this.nCols=0,this.rows=o(t),this.cols=o(r),this.values=o(e);for(var i=0;ir?t[e]:r;return t.map(function(t){return t/r})},n.l1=function(t){for(var r=0,e=0;e=s[0])return 0;for(var h=0;h=m)break;if(c>=m){if(!(s[f]>e))break;l=f}else if(s[f]>=s[c]){if(!(e0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},i=this&&this.__spread||function(){for(var t=[],r=0;r=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}};Object.defineProperty(r,"__esModule",{value:!0});var s=e(1),a=function(){return function(t,r,e,n){this.hyperplanes=t,this.offsets=r,this.children=e,this.indices=n}}();r.FlatTree=a,r.makeForest=function(t,r,e,n){var o=Math.max(10,r);return s.range(e).map(function(r,e){return function(t,r,e,n){void 0===r&&(r=30);var i=s.range(t.length);return function t(r,e,n,i,o){if(void 0===n&&(n=30),e.length>n){var a=function(t,r,e){var n=t[0].length,i=s.tauRandInt(r.length,e),o=s.tauRandInt(r.length,e);o=(o+=i===o?1:0)%r.length;for(var a=r[i],h=r[o],u=0,l=s.zeros(n),f=0;f0?(g[f]=0,c+=1):(g[f]=1,m+=1)}var w=s.zeros(c),d=s.zeros(m);for(var f in c=0,m=0,s.range(g.length))0===g[f]?(w[c]=r[f],c+=1):(d[m]=r[f],m+=1);return{indicesLeft:w,indicesRight:d,hyperplane:l,offset:u}}(r,e,o),h=a.indicesLeft,u=a.indicesRight,l=a.hyperplane,f=a.offset,c=t(r,h,n,i+1,o),m=t(r,u,n,i+1,o),g={leftChild:c,rightChild:m,isLeaf:!1,hyperplane:l,offset:f};return g}var g={indices:e,isLeaf:!0};return g}(t,i,r,e,n)}(t,o,e,n)}).map(function(t){return function(t,r){var e=function t(r){return r.isLeaf?1:1+t(r.leftChild)+t(r.rightChild)}(t),n=function t(r){return r.isLeaf?1:t(r.leftChild)+t(r.rightChild)}(t),o=s.range(e).map(function(){return s.zeros(t.hyperplane.length)}),h=s.zeros(e),u=s.range(e).map(function(){return[-1,-1]}),l=s.range(n).map(function(){return s.range(r).map(function(){return-1})});return function t(r,e,n,o,s,a,h){var u;if(r.isLeaf)return o[a][0]=-h,(u=s[h]).splice.apply(u,i([0,r.indices.length],r.indices)),{nodeNum:a,leafNum:h+=1};e[a]=r.hyperplane,n[a]=r.offset,o[a][0]=a+1;var l=a,f=t(r.leftChild,e,n,o,s,a+1,h);return a=f.nodeNum,h=f.leafNum,o[l][1]=a+1,{nodeNum:(f=t(r.rightChild,e,n,o,s,a+1,h)).nodeNum,leafNum:f.leafNum}}(t,o,h,u,l,0,0),new a(o,h,u,l)}(t,o)})},r.makeLeafArray=function(t){var r,e;if(t.length>0){var n=[];try{for(var s=o(t),a=s.next();!a.done;a=s.next()){var h=a.value;n.push.apply(n,i(h.indices))}}catch(t){r={error:t}}finally{try{a&&!a.done&&(e=s.return)&&e.call(s)}finally{if(r)throw r.error}}return n}return[[-1]]}},function(t,r,e){"use strict";var n=e(9);function i(t,r,e){var n=0;const i=e(r);for(var o=0;or&&(r=t[e]);return r};var s=function(t){if(!i()(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");for(var r=t[0],e=1;e1&&void 0!==arguments[1]?arguments[1]:{};if(!i()(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");if(void 0!==e.output){if(!i()(e.output))throw new TypeError("output option must be an array if specified");r=e.output}else r=new Array(t.length);var n=s(t),a=o(t);if(n===a)throw new RangeError("minimum and maximum input values are equal. Cannot rescale a constant array");var h=e.min,u=void 0===h?e.autoMinMax?n:0:h,l=e.max,f=void 0===l?e.autoMinMax?a:1:l;if(u>=f)throw new RangeError("min option must be smaller than max option");for(var c=(f-u)/(a-n),m=0;mMath.abs(h[i])&&(i=r);if(i!==e){for(n=0;n=0;i--){for(n=0;no?t[i][o]:i===o?1:0;return n}get upperTriangularMatrix(){for(var t=this.LU,r=t.rows,e=t.columns,n=new I(r,e),i=0;iMath.abs(r)?(e=r/t,Math.abs(t)*Math.sqrt(1+e*e)):0!==r?(e=t/r,Math.abs(r)*Math.sqrt(1+e*e)):0}function l(t,r,e){for(var n=new Array(t),i=0;i=0;t--)if(0!==p[t]){for(let r=t+1;r=0;t--){if(t0;){let t,r;for(t=R-2;t>=-1&&-1!==t;t--){const r=Number.MIN_VALUE+A*Math.abs(p[t]+Math.abs(p[t+1]));if(Math.abs(y[t])<=r||Number.isNaN(y[t])){y[t]=0;break}}if(t===R-2)r=4;else{let e;for(e=R-1;e>=t&&e!==t;e--){let r=(e!==R?Math.abs(y[e]):0)+(e!==t+1?Math.abs(y[e-1]):0);if(Math.abs(p[e])<=A*r){p[e]=0;break}}e===t?r=3:e===R-1?r=1:(r=2,t=e)}switch(t++,r){case 1:{let r=y[R-2];y[R-2]=0;for(let e=R-2;e>=t;e--){let i=u(p[e],r),o=p[e]/i,s=r/i;if(p[e]=i,e!==t&&(r=-s*y[e-1],y[e-1]=o*y[e-1]),f)for(let t=0;t=p[t+1]);){let r=p[t];if(p[t]=p[t+1],p[t+1]=r,f&&tr?i[o][e]=t[o][e]/this.s[e]:i[o][e]=0;var o=this.U,s=o.length,a=o[0].length,h=new I(e,s);for(let t=0;tt&&r++;return r}get diagonal(){return this.s}get threshold(){return Number.EPSILON/2*Math.max(this.m,this.n)*this.s[0]}get leftSingularVectors(){return I.isMatrix(this.U)||(this.U=new I(this.U)),this.U}get rightSingularVectors(){return I.isMatrix(this.V)||(this.V=new I(this.V)),this.V}get diagonalMatrix(){return I.diag(this.s)}}function c(t,r,e){var n=e?t.rows:t.rows-1;if(r<0||r>n)throw new RangeError("Row index out of range")}function m(t,r,e){var n=e?t.columns:t.columns-1;if(r<0||r>n)throw new RangeError("Column index out of range")}function g(t,r){if(r.to1DArray&&(r=r.to1DArray()),r.length!==t.columns)throw new RangeError("vector size must be the same as the number of columns");return r}function v(t,r){if(r.to1DArray&&(r=r.to1DArray()),r.length!==t.rows)throw new RangeError("vector size must be the same as the number of rows");return r}function p(t,r,e){return{row:w(t,r),column:d(t,e)}}function w(t,r){if("object"!=typeof r)throw new TypeError("unexpected type for row indices");if(r.some(r=>r<0||r>=t.rows))throw new RangeError("row indices are out of range");return Array.isArray(r)||(r=Array.from(r)),r}function d(t,r){if("object"!=typeof r)throw new TypeError("unexpected type for column indices");if(r.some(r=>r<0||r>=t.columns))throw new RangeError("column indices are out of range");return Array.isArray(r)||(r=Array.from(r)),r}function y(t,r,e,n,i){if(5!==arguments.length)throw new RangeError("expected 4 arguments");if(b("startRow",r),b("endRow",e),b("startColumn",n),b("endColumn",i),r>e||n>i||r<0||r>=t.rows||e<0||e>=t.rows||n<0||n>=t.columns||i<0||i>=t.columns)throw new RangeError("Submatrix indices are out of range")}function b(t,r){if("number"!=typeof r)throw new TypeError(`${t} must be a number`)}class M extends(C()){constructor(t,r,e){super(),this.matrix=t,this.rows=r,this.columns=e}static get[Symbol.species](){return I}}class x extends M{constructor(t){super(t,t.columns,t.rows)}set(t,r,e){return this.matrix.set(r,t,e),this}get(t,r){return this.matrix.get(r,t)}}class S extends M{constructor(t,r){super(t,1,t.columns),this.row=r}set(t,r,e){return this.matrix.set(this.row,r,e),this}get(t,r){return this.matrix.get(this.row,r)}}class E extends M{constructor(t,r,e,n,i){y(t,r,e,n,i),super(t,e-r+1,i-n+1),this.startRow=r,this.startColumn=n}set(t,r,e){return this.matrix.set(this.startRow+t,this.startColumn+r,e),this}get(t,r){return this.matrix.get(this.startRow+t,this.startColumn+r)}}class R extends M{constructor(t,r,e){var n=p(t,r,e);super(t,n.row.length,n.column.length),this.rowIndices=n.row,this.columnIndices=n.column}set(t,r,e){return this.matrix.set(this.rowIndices[t],this.columnIndices[r],e),this}get(t,r){return this.matrix.get(this.rowIndices[t],this.columnIndices[r])}}class k extends M{constructor(t,r){super(t,(r=w(t,r)).length,t.columns),this.rowIndices=r}set(t,r,e){return this.matrix.set(this.rowIndices[t],r,e),this}get(t,r){return this.matrix.get(this.rowIndices[t],r)}}class A extends M{constructor(t,r){r=d(t,r),super(t,t.rows,r.length),this.columnIndices=r}set(t,r,e){return this.matrix.set(t,this.columnIndices[r],e),this}get(t,r){return this.matrix.get(t,this.columnIndices[r])}}class N extends M{constructor(t,r){super(t,t.rows,1),this.column=r}set(t,r,e){return this.matrix.set(t,this.column,e),this}get(t){return this.matrix.get(t,this.column)}}class V extends M{constructor(t){super(t,t.rows,t.columns)}set(t,r,e){return this.matrix.set(this.rows-t-1,r,e),this}get(t,r){return this.matrix.get(this.rows-t-1,r)}}class z extends M{constructor(t){super(t,t.rows,t.columns)}set(t,r,e){return this.matrix.set(t,this.columns-r-1,e),this}get(t,r){return this.matrix.get(t,this.columns-r-1)}}function C(t){void 0===t&&(t=Object);class r extends t{static get[Symbol.species](){return this}static from1DArray(t,r,e){if(t*r!==e.length)throw new RangeError("Data length does not match given dimensions");for(var n=new this(t,r),i=0;it&&(t=this.get(r,e));return t}maxIndex(){for(var t=this.get(0,0),r=[0,0],e=0;et&&(t=this.get(e,n),r[0]=e,r[1]=n);return r}min(){for(var t=this.get(0,0),r=0;rr&&(r=this.get(t,e));return r}maxRowIndex(t){c(this,t);for(var r=this.get(t,0),e=[t,0],n=1;nr&&(r=this.get(t,n),e[1]=n);return e}minRow(t){c(this,t);for(var r=this.get(t,0),e=1;er&&(r=this.get(e,t));return r}maxColumnIndex(t){m(this,t);for(var r=this.get(0,t),e=[0,t],n=1;nr&&(r=this.get(n,t),e[0]=n);return e}minColumn(t){m(this,t);for(var r=this.get(0,t),e=1;e=(r=void 0===r?1:r))throw new RangeError("min should be strictly smaller than max");for(var e=this.constructor.empty(this.rows,this.columns),n=0;n=(r=void 0===r?1:r))throw new RangeError("min should be strictly smaller than max");for(var e=this.constructor.empty(this.rows,this.columns),n=0;ne||r<0||r>=this.columns||e<0||e>=this.columns)throw new RangeError("Argument out of range");for(var n=new this.constructor[Symbol.species](t.length,e-r+1),i=0;i=this.rows)throw new RangeError(`Row index out of range: ${t[i]}`);n.set(i,o-r,this.get(t[i],o))}return n}subMatrixColumn(t,r,e){if(void 0===r&&(r=0),void 0===e&&(e=this.rows-1),r>e||r<0||r>=this.rows||e<0||e>=this.rows)throw new RangeError("Argument out of range");for(var n=new this.constructor[Symbol.species](e-r+1,t.length),i=0;i=this.columns)throw new RangeError(`Column index out of range: ${t[i]}`);n.set(o-r,i,this.get(o,t[i]))}return n}setSubMatrix(t,r,e){y(this,r,r+(t=this.constructor.checkMatrix(t)).rows-1,e,e+t.columns-1);for(var n=0;nt?i[o]=1/i[o]:i[o]=0;return i=this.constructor[Symbol.species].diag(i),n.mmul(i.mmul(e.transposeView()))}clone(){for(var t=new this.constructor[Symbol.species](this.rows,this.columns),r=0;r>","signPropagatingRightShift"],[">>>","rightShift","zeroFillRightShift"]]){var u=o(F("\n(function %name%(value) {\n if (typeof value === 'number') return this.%name%S(value);\n return this.%name%M(value);\n})\n",{name:s[1],op:s[0]})),l=o(F("\n(function %name%S(value) {\n for (var i = 0; i < this.rows; i++) {\n for (var j = 0; j < this.columns; j++) {\n this.set(i, j, this.get(i, j) %op% value);\n }\n }\n return this;\n})\n",{name:`${s[1]}S`,op:s[0]})),w=o(F("\n(function %name%M(matrix) {\n matrix = this.constructor.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 (var i = 0; i < this.rows; i++) {\n for (var j = 0; j < this.columns; j++) {\n this.set(i, j, this.get(i, j) %op% matrix.get(i, j));\n }\n }\n return this;\n})\n",{name:`${s[1]}M`,op:s[0]})),d=o(F("\n(function %name%(matrix, value) {\n var newMatrix = new this[Symbol.species](matrix);\n return newMatrix.%name%(value);\n})\n",{name:s[1]}));for(n=1;n0){if(super(t),!(Number.isInteger(r)&&r>0))throw new TypeError("nColumns must be a positive integer");for(e=0;e=0;o--){for(i=0;i=0;e--){for(t=0;ti)return new Array(r.rows+1).fill(0);for(var o=r.addRow(e,[0]),s=0;s0;a--){for(f=0,s=0,u=0;u0&&(o=-o),r[a]=f*o,s-=i*o,e[a-1]=i-o,h=0;hl){0;do{for(1,i=e[l],c=(e[l+1]-i)/(2*r[l]),m=u(c,1),c<0&&(m=-m),e[l]=r[l]/(c+m),e[l+1]=r[l]*(c+m),g=e[l+1],o=i-e[l],s=l+2;s=l;s--)for(w=p,p=v,b=y,i=v*r[s],o=v*c,m=u(c,r[s]),r[s+1]=y*m,y=r[s]/m,c=(v=c/m)*e[s]-y*i,e[s+1]=o+y*(v*i+y*e[s]),h=0;hS*x)}e[l]=e[l]+M,r[l]=0}for(s=0;s=u;a--)e[a]=r[a][u-1]/l,s+=e[a]*e[a];for(o=Math.sqrt(s),e[u]>0&&(o=-o),s-=e[u]*o,e[u]=e[u]-o,h=u;h=u;a--)i+=e[a]*r[a][h];for(i/=s,a=u;a<=f;a++)r[a][h]-=i*e[a]}for(a=0;a<=f;a++){for(i=0,h=f;h>=u;h--)i+=e[h]*r[a][h];for(i/=s,h=u;h<=f;h++)r[a][h]-=i*e[h]}e[u]=l*e[u],r[u][u-1]=l*o}}for(a=0;a=1;u--)if(0!==r[u][u-1]){for(a=u+1;a<=f;a++)e[a]=r[a][u-1];for(h=u;h<=f;h++){for(o=0,a=u;a<=f;a++)o+=e[a]*n[a][h];for(o=o/e[u]/r[u][u-1],a=u;a<=f;a++)n[a][h]+=o*e[a]}}}(o,c,m,s),function(t,r,e,n,i){var o,s,a,h,u,l,f,c,m,g,v,p,w,d,y,b=t-1,M=t-1,x=Number.EPSILON,S=0,E=0,R=0,k=0,A=0,N=0,V=0,z=0;for(o=0;oM)&&(e[o]=i[o][o],r[o]=0),s=Math.max(o-1,0);s=0;){for(h=b;h>0&&(0===(N=Math.abs(i[h-1][h-1])+Math.abs(i[h][h]))&&(N=E),!(Math.abs(i[h][h-1])=0){for(V=R>=0?R+V:R-V,e[b-1]=c+V,e[b]=e[b-1],0!==V&&(e[b]=c-f/V),r[b-1]=0,r[b]=0,c=i[b][b-1],N=Math.abs(c)+Math.abs(V),R=c/N,k=V/N,A=Math.sqrt(R*R+k*k),R/=A,k/=A,s=b-1;s0){for(N=Math.sqrt(N),m=h&&(V=i[u][u],R=((A=c-V)*(N=m-V)-f)/i[u+1][u]+i[u][u+1],k=i[u+1][u+1]-V-A-N,A=i[u+2][u+1],N=Math.abs(R)+Math.abs(k)+Math.abs(A),R/=N,k/=N,A/=N,u!==h)&&!(Math.abs(i[u][u-1])*(Math.abs(k)+Math.abs(A))u+2&&(i[o][o-3]=0);for(a=u;a<=b-1&&(d=a!==b-1,a!==u&&(R=i[a][a-1],k=i[a+1][a-1],A=d?i[a+2][a-1]:0,0!==(c=Math.abs(R)+Math.abs(k)+Math.abs(A))&&(R/=c,k/=c,A/=c)),0!==c);a++)if(N=Math.sqrt(R*R+k*k+A*A),R<0&&(N=-N),0!==N){for(a!==u?i[a][a-1]=-N*c:h!==u&&(i[a][a-1]=-i[a][a-1]),c=(R+=N)/N,m=k/N,V=A/N,k/=R,A/=R,s=a;s=0;b--)if(R=e[b],0===(k=r[b]))for(h=b,i[b][b]=1,o=b-1;o>=0;o--){for(f=i[o][o]-R,A=0,s=h;s<=b;s++)A+=i[o][s]*i[s][b];if(r[o]<0)V=f,N=A;else if(h=o,0===r[o]?i[o][b]=0!==f?-A/f:-A/(x*E):(c=i[o][o+1],m=i[o+1][o],k=(e[o]-R)*(e[o]-R)+r[o]*r[o],l=(c*N-V*A)/k,i[o][b]=l,i[o+1][b]=Math.abs(c)>Math.abs(V)?(-A-f*l)/c:(-N-m*l)/V),l=Math.abs(i[o][b]),x*l*l>1)for(s=o;s<=b;s++)i[s][b]=i[s][b]/l}else if(k<0)for(h=b-1,Math.abs(i[b][b-1])>Math.abs(i[b-1][b])?(i[b-1][b-1]=k/i[b][b-1],i[b-1][b]=-(i[b][b]-R)/i[b][b-1]):(y=F(0,-i[b-1][b],i[b-1][b-1]-R,k),i[b-1][b-1]=y[0],i[b-1][b]=y[1]),i[b][b-1]=0,i[b][b]=1,o=b-2;o>=0;o--){for(g=0,v=0,s=h;s<=b;s++)g+=i[o][s]*i[s][b-1],v+=i[o][s]*i[s][b];if(f=i[o][o]-R,r[o]<0)V=f,A=g,N=v;else if(h=o,0===r[o]?(y=F(-g,-v,f,k),i[o][b-1]=y[0],i[o][b]=y[1]):(c=i[o][o+1],m=i[o+1][o],p=(e[o]-R)*(e[o]-R)+r[o]*r[o]-k*k,w=2*(e[o]-R)*k,0===p&&0===w&&(p=x*E*(Math.abs(f)+Math.abs(k)+Math.abs(c)+Math.abs(m)+Math.abs(V))),y=F(c*A-V*g+k*v,c*N-V*v-k*g,p,w),i[o][b-1]=y[0],i[o][b]=y[1],Math.abs(c)>Math.abs(V)+Math.abs(k)?(i[o+1][b-1]=(-g-f*i[o][b-1]+k*i[o][b])/c,i[o+1][b]=(-v-f*i[o][b]-k*i[o][b-1])/c):(y=F(-A-m*i[o][b-1],-N-m*i[o][b],V,k),i[o+1][b-1]=y[0],i[o+1][b]=y[1])),l=Math.max(Math.abs(i[o][b-1]),Math.abs(i[o][b])),x*l*l>1)for(s=o;s<=b;s++)i[s][b-1]=i[s][b-1]/l,i[s][b]=i[s][b]/l}for(o=0;oM)for(s=o;s=0;s--)for(o=0;o<=M;o++){for(V=0,a=0;a<=Math.min(s,M);a++)V+=n[o][a]*i[a][s];n[o][s]=V}}(o,h,a,s,c)}this.n=o,this.e=h,this.d=a,this.V=s}get realEigenvalues(){return this.d}get imaginaryEigenvalues(){return this.e}get eigenvectorMatrix(){return I.isMatrix(this.V)||(this.V=new I(this.V)),this.V}get diagonalMatrix(){var t,r,e=this.n,n=this.e,i=this.d,o=new I(e,e);for(t=0;t0?o[t][t+1]=n[t]:n[t]<0&&(o[t][t-1]=n[t])}return o}}function F(t,r,e,n){var i,o;return Math.abs(e)>Math.abs(n)?[(t+(i=n/e)*r)/(o=e+i*n),(r-i*t)/o]:[((i=e/n)*t+r)/(o=n+i*e),(i*r-t)/o]}class W{constructor(t){if(!(t=P.checkMatrix(t)).isSymmetric())throw new Error("Matrix is not symmetric");var r,e,n,i=t,o=i.rows,s=new I(o,o),a=!0;for(e=0;e0,s[e][e]=Math.sqrt(Math.max(u,0)),n=e+1;n=0;o--)for(i=0;i=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}};function i(t,r){return void 0===r&&(r=Math.random),Math.floor(r()*t)}function o(t){return void 0===t&&(t=Math.random),t()}function s(t){for(var r=[],e=0;er?t[e]:r;return r},r.max2d=function(t){for(var r=0,e=0;er?t[e][n]:r;return r},r.rejectionSample=function(t,r){for(var e=h(t),n=0;n=a[0])return 0;for(var h=0;h=s[0])return 0;s[0]=e,o[0]=n,a[0]=i;for(var h=0,u=0;;){var l=2*h+1,c=l+1,f=t[0][0].length;if(l>=f)break;if(c>=f){if(!(s[l]>e))break;u=l}else if(s[l]>=s[c]){if(!(ee.length;a++)1===i[a]&&n[a]=0?(i[s]=0,Math.floor(e[s])):-1}},function(t,r,e){"use strict";var n,i=this&&this.__read||function(t,r){var e="function"==typeof Symbol&&t[Symbol.iterator];if(!e)return t;var n,i,o=e.call(t),s=[];try{for(;(void 0===r||r-- >0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},o=this&&this.__spread||function(){for(var t=[],r=0;r=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}};Object.defineProperty(r,"__esModule",{value:!0});var a=e(1),h=function(){function t(t,r,e,n){this.entries=new Map,this.nRows=0,this.nCols=0,this.rows=o(t),this.cols=o(r),this.values=o(e);for(var i=0;ir?t:r})},r.multiplyScalar=function(t,r){return t.map(function(t){return t*r})},r.eliminateZeros=function(t){for(var r=new Set,e=t.getValues(),n=t.getRows(),i=t.getCols(),o=0;or?t[e]:r;return t.map(function(t){return t/r})},n.l1=function(t){for(var r=0,e=0;e0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},i=this&&this.__spread||function(){for(var t=[],r=0;r=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}};Object.defineProperty(r,"__esModule",{value:!0});var s=e(1),a=function(){return function(t,r,e,n){this.hyperplanes=t,this.offsets=r,this.children=e,this.indices=n}}();function h(t,r,e){for(var n=r,i=0;i0?0:1}r.FlatTree=a,r.makeForest=function(t,r,e,n){var o=Math.max(10,r);return s.range(e).map(function(r,e){return function(t,r,e,n){void 0===r&&(r=30);var i=s.range(t.length);return function t(r,e,n,i,o){if(void 0===n&&(n=30),e.length>n){var a=function(t,r,e){var n=t[0].length,i=s.tauRandInt(r.length,e),o=s.tauRandInt(r.length,e);o=(o+=i===o?1:0)%r.length;for(var a=r[i],h=r[o],u=0,l=s.zeros(n),c=0;c0?(g[c]=0,f+=1):(g[c]=1,m+=1)}var d=s.zeros(f),w=s.zeros(m);for(var c in f=0,m=0,s.range(g.length))0===g[c]?(d[f]=r[c],f+=1):(w[m]=r[c],m+=1);return{indicesLeft:d,indicesRight:w,hyperplane:l,offset:u}}(r,e,o),h=a.indicesLeft,u=a.indicesRight,l=a.hyperplane,c=a.offset,f=t(r,h,n,i+1,o),m=t(r,u,n,i+1,o),g={leftChild:f,rightChild:m,isLeaf:!1,hyperplane:l,offset:c};return g}var g={indices:e,isLeaf:!0};return g}(t,i,r,e,n)}(t,o,e,n)}).map(function(t){return function(t,r){var e=function t(r){return r.isLeaf?1:1+t(r.leftChild)+t(r.rightChild)}(t),n=function t(r){return r.isLeaf?1:t(r.leftChild)+t(r.rightChild)}(t),o=s.range(e).map(function(){return s.zeros(t.hyperplane.length)}),h=s.zeros(e),u=s.range(e).map(function(){return[-1,-1]}),l=s.range(n).map(function(){return s.range(r).map(function(){return-1})});return function t(r,e,n,o,s,a,h){var u;if(r.isLeaf)return o[a][0]=-h,(u=s[h]).splice.apply(u,i([0,r.indices.length],r.indices)),{nodeNum:a,leafNum:h+=1};e[a]=r.hyperplane,n[a]=r.offset,o[a][0]=a+1;var l=a,c=t(r.leftChild,e,n,o,s,a+1,h);return a=c.nodeNum,h=c.leafNum,o[l][1]=a+1,{nodeNum:(c=t(r.rightChild,e,n,o,s,a+1,h)).nodeNum,leafNum:c.leafNum}}(t,o,h,u,l,0,0),new a(o,h,u,l)}(t,o)})},r.makeLeafArray=function(t){var r,e;if(t.length>0){var n=[];try{for(var s=o(t),a=s.next();!a.done;a=s.next()){var h=a.value;n.push.apply(n,i(h.indices))}}catch(t){r={error:t}}finally{try{a&&!a.done&&(e=s.return)&&e.call(s)}finally{if(r)throw r.error}}return n}return[[-1]]},r.searchFlatTree=function(t,r){for(var e=0;r.children[e][0]>0;)e=0===h(r.hyperplanes[e],r.offsets[e],t)?r.children[e][0]:r.children[e][1];var n=-1*r.children[e][0];return r.indices[n]}},function(t,r,e){"use strict";Object.defineProperty(r,"__esModule",{value:!0});var n=e(6);window.UMAP=n.UMAP},function(t,r,e){"use strict";var n=this&&this.__awaiter||function(t,r,e,n){return new(e||(e=Promise))(function(i,o){function s(t){try{h(n.next(t))}catch(t){o(t)}}function a(t){try{h(n.throw(t))}catch(t){o(t)}}function h(t){t.done?i(t.value):new e(function(r){r(t.value)}).then(s,a)}h((n=n.apply(t,r||[])).next())})},i=this&&this.__generator||function(t,r){var e,n,i,o,s={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function a(o){return function(a){return function(o){if(e)throw new TypeError("Generator is already executing.");for(;s;)try{if(e=1,n&&(i=2&o[0]?n.return:o[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,o[1])).done)return i;switch(n=0,i&&(o=[2&o[0],i.value]),o[0]){case 0:case 1:i=o;break;case 4:return s.label++,{value:o[1],done:!1};case 5:s.label++,n=o[1],o=[0];continue;case 7:o=s.ops.pop(),s.trys.pop();continue;default:if(!(i=(i=s.trys).length>0&&i[i.length-1])&&(6===o[0]||2===o[0])){s=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},s=this&&this.__spread||function(){for(var t=[],r=0;r0&&i.set(o,l,c)}var f=h.transpose(i);return h.maximum(i,f)},t.prototype.transform=function(t){var r=this,e=this.X;if(void 0===e||0===e.length)throw new Error("No data has been fit.");var n=Math.floor(this.nNeighbors*this.transformQueueSize),i=u.initializeSearch(this.rpForest,e,t,n,this.initFromRandom,this.initFromTree),o=this.search(e,this.searchGraph,i,t),s=a.deheapSort(o),l=s.indices,f=s.weights;l=l.map(function(t){return t.slice(0,r.nNeighbors)}),f=f.map(function(t){return t.slice(0,r.nNeighbors)});var m=Math.max(0,this.localConnectivity-1),g=this.smoothKNNDistance(f,this.nNeighbors,m),p=g.sigmas,v=g.rhos,d=this.computeMembershipStrengths(l,f,p,v),w=d.rows,y=d.cols,b=d.vals,x=[t.length,e.length],S=new h.SparseMatrix(w,y,b,x),E=h.normalize(S,"l1"),R=h.getCSR(E),k=t.length,z=M(c.reshape2d(R.indices,k,this.nNeighbors),c.reshape2d(R.values,k,this.nNeighbors),this.embedding),N=this.nEpochs?this.nEpochs/3:S.nRows<=1e4?100:30,A=S.getValues().reduce(function(t,r){return r>t?r:t},0);S=S.map(function(t){return t0});if(g.length>=e){var p=Math.floor(e),v=e-p;p>0?(s[h]=g[p-1],v>1e-5&&(s[h]+=v*(g[p]-g[p-1]))):s[h]=v*g[0]}else g.length>0&&(s[h]=c.max(g));for(var d=0;d0?Math.exp(-b/f):1}if(Math.abs(w-o)<1e-5)break;w>o?f=(u+(l=f))/2:(u=f,l===1/0?f*=2:f=(u+l)/2)}if(a[h]=f,s[h]>0){var M=c.mean(m);a[h]<.001*M&&(a[h]=.001*M)}else{var x=c.mean(t.map(c.mean));a[h]<.001*x&&(a[h]=.001*x)}}return{sigmas:a,rhos:s}},t.prototype.computeMembershipStrengths=function(t,r,e,n){for(var i=t.length,o=t[0].length,s=c.zeros(i*o),a=c.zeros(i*o),h=c.zeros(i*o),u=0;u0&&(e[n]=r/i[n])}),e},t.prototype.assignOptimizationStateParameters=function(t){Object.assign(this.optimizationState,t)},t.prototype.prepareForOptimizationLoop=function(){var t=this.repulsionStrength,r=this.learningRate,e=this.negativeSampleRate,n=this.optimizationState,i=n.epochsPerSample,o=n.headEmbedding,a=n.tailEmbedding,h=o[0].length,u=o.length===a.length,l=i.map(function(t){return t/e}),c=s(l),f=s(i);this.assignOptimizationStateParameters({epochOfNextSample:f,epochOfNextNegativeSample:c,epochsPerNegativeSample:l,moveOther:u,initialAlpha:r,alpha:r,gamma:t,dim:h})},t.prototype.initializeOptimization=function(){var t=this.embedding,r=this.embedding,e=this.optimizationState,n=e.head,i=e.tail,o=e.epochsPerSample,s=this.getNEpochs(),a=this.graph.nCols,h=w(this.spread,this.minDist),u=h.a,l=h.b;this.assignOptimizationStateParameters({headEmbedding:t,tailEmbedding:r,head:n,tail:i,epochsPerSample:o,a:u,b:l,nEpochs:s,nVertices:a})},t.prototype.optimizeLayoutStep=function(t){for(var r=this.optimizationState,e=r.head,n=r.tail,i=r.headEmbedding,o=r.tailEmbedding,s=r.epochsPerSample,a=r.epochOfNextSample,h=r.epochOfNextNegativeSample,u=r.epochsPerNegativeSample,l=r.moveOther,f=r.initialAlpha,m=r.alpha,g=r.gamma,p=r.a,w=r.b,y=r.dim,b=r.nEpochs,M=r.nVertices,x=0;xt)){var S=e[x],E=n[x],R=i[S],k=o[E],z=d(R,k),N=0;z>0&&(N=-2*p*w*Math.pow(z,w-1),N/=p*Math.pow(z,w)+1);for(var A=0;A0)_=2*g*w,_/=(.001+D)*(p*Math.pow(D,w)+1);else if(S===P)continue;for(A=0;A0&&(V=v(_*(R[A]-j[A]),4)),R[A]+=V*m}}h[x]+=C*u[x]}return r.alpha=f*(1-t/b),r.currentEpoch+=1,i},t.prototype.optimizeLayoutAsync=function(t){var r=this;return void 0===t&&(t=function(){return!0}),new Promise(function(e,o){var s=function(){return n(r,void 0,void 0,function(){var r,n,a,h,u,l;return i(this,function(i){try{if(r=this.optimizationState,n=r.nEpochs,a=r.currentEpoch,this.embedding=this.optimizeLayoutStep(a),h=this.optimizationState.currentEpoch,u=!1===t(h),l=h===n,u||l)return[2,e(l)];s()}catch(t){o(t)}return[2]})})};s()})},t.prototype.optimizeLayout=function(t){void 0===t&&(t=function(){return!0});for(var r=!1,e=[];!r;){var n=this.optimizationState,i=n.nEpochs,o=n.currentEpoch;e=this.optimizeLayoutStep(o);var s=this.optimizationState.currentEpoch,a=!1===t(s);r=s===i||a}return e},t.prototype.getNEpochs=function(){var t=this.graph;if(this.nEpochs>0)return this.nEpochs;var r=t.nRows;return r<=2500?500:r<=5e3?400:r<=7500?300:200},t}();function g(t,r){for(var e=0,n=0;nr?r:t<-r?-r:t}function d(t,r){for(var e=0,n=0;n=r?Math.exp(-(e[i]-r)/t):n}),i=f({x:e,y:n},function(t){var r=o(t,2),e=r[0],n=r[1];return function(t){return 1/(1+e*Math.pow(t,2*n))}},{damping:1.5,initialValues:[.5,.5],gradientDifference:.1,maxIterations:100,errorTolerance:.01}).parameterValues,s=o(i,2);return{a:s[0],b:s[1]}}function y(t,r,e,n){return void 0===e&&(e=1),void 0===n&&(n=5),t.map(function(t,i,o){return-1===r[i]||-1===r[o]?t*Math.exp(-e):r[i]!==r[o]?t*Math.exp(-n):t})}function b(t){t=h.normalize(t,"max");var r=h.transpose(t),e=h.pairwiseMultiply(r,t);return t=h.add(t,h.subtract(r,e)),h.eliminateZeros(t)}function M(t,r,e){for(var n=c.zeros(t.length).map(function(t){return c.zeros(e[0].length)}),i=0;i=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}};Object.defineProperty(r,"__esModule",{value:!0});var i=e(2),o=e(3),s=e(4),a=e(1);r.makeNNDescent=function(t,r){return function(e,n,o,s,h,u,l,c){void 0===s&&(s=10),void 0===h&&(h=50),void 0===u&&(u=.001),void 0===l&&(l=.5),void 0===c&&(c=!0);for(var f=e.length,m=i.makeHeap(e.length,o),g=0;gr&&(r=t[e]);return r};var s=function(t){if(!i()(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");for(var r=t[0],e=1;e1&&void 0!==arguments[1]?arguments[1]:{};if(!i()(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");if(void 0!==e.output){if(!i()(e.output))throw new TypeError("output option must be an array if specified");r=e.output}else r=new Array(t.length);var n=s(t),a=o(t);if(n===a)throw new RangeError("minimum and maximum input values are equal. Cannot rescale a constant array");var h=e.min,u=void 0===h?e.autoMinMax?n:0:h,l=e.max,c=void 0===l?e.autoMinMax?a:1:l;if(u>=c)throw new RangeError("min option must be smaller than max option");for(var f=(c-u)/(a-n),m=0;mMath.abs(h[i])&&(i=r);if(i!==e){for(n=0;n=0;i--){for(n=0;no?t[i][o]:i===o?1:0;return n}get upperTriangularMatrix(){for(var t=this.LU,r=t.rows,e=t.columns,n=new I(r,e),i=0;iMath.abs(r)?(e=r/t,Math.abs(t)*Math.sqrt(1+e*e)):0!==r?(e=t/r,Math.abs(r)*Math.sqrt(1+e*e)):0}function l(t,r,e){for(var n=new Array(t),i=0;i=0;t--)if(0!==v[t]){for(let r=t+1;r=0;t--){if(t0;){let t,r;for(t=R-2;t>=-1&&-1!==t;t--){const r=Number.MIN_VALUE+z*Math.abs(v[t]+Math.abs(v[t+1]));if(Math.abs(y[t])<=r||Number.isNaN(y[t])){y[t]=0;break}}if(t===R-2)r=4;else{let e;for(e=R-1;e>=t&&e!==t;e--){let r=(e!==R?Math.abs(y[e]):0)+(e!==t+1?Math.abs(y[e-1]):0);if(Math.abs(v[e])<=z*r){v[e]=0;break}}e===t?r=3:e===R-1?r=1:(r=2,t=e)}switch(t++,r){case 1:{let r=y[R-2];y[R-2]=0;for(let e=R-2;e>=t;e--){let i=u(v[e],r),o=v[e]/i,s=r/i;if(v[e]=i,e!==t&&(r=-s*y[e-1],y[e-1]=o*y[e-1]),c)for(let t=0;t=v[t+1]);){let r=v[t];if(v[t]=v[t+1],v[t+1]=r,c&&tr?i[o][e]=t[o][e]/this.s[e]:i[o][e]=0;var o=this.U,s=o.length,a=o[0].length,h=new I(e,s);for(let t=0;tt&&r++;return r}get diagonal(){return this.s}get threshold(){return Number.EPSILON/2*Math.max(this.m,this.n)*this.s[0]}get leftSingularVectors(){return I.isMatrix(this.U)||(this.U=new I(this.U)),this.U}get rightSingularVectors(){return I.isMatrix(this.V)||(this.V=new I(this.V)),this.V}get diagonalMatrix(){return I.diag(this.s)}}function f(t,r,e){var n=e?t.rows:t.rows-1;if(r<0||r>n)throw new RangeError("Row index out of range")}function m(t,r,e){var n=e?t.columns:t.columns-1;if(r<0||r>n)throw new RangeError("Column index out of range")}function g(t,r){if(r.to1DArray&&(r=r.to1DArray()),r.length!==t.columns)throw new RangeError("vector size must be the same as the number of columns");return r}function p(t,r){if(r.to1DArray&&(r=r.to1DArray()),r.length!==t.rows)throw new RangeError("vector size must be the same as the number of rows");return r}function v(t,r,e){return{row:d(t,r),column:w(t,e)}}function d(t,r){if("object"!=typeof r)throw new TypeError("unexpected type for row indices");if(r.some(r=>r<0||r>=t.rows))throw new RangeError("row indices are out of range");return Array.isArray(r)||(r=Array.from(r)),r}function w(t,r){if("object"!=typeof r)throw new TypeError("unexpected type for column indices");if(r.some(r=>r<0||r>=t.columns))throw new RangeError("column indices are out of range");return Array.isArray(r)||(r=Array.from(r)),r}function y(t,r,e,n,i){if(5!==arguments.length)throw new RangeError("expected 4 arguments");if(b("startRow",r),b("endRow",e),b("startColumn",n),b("endColumn",i),r>e||n>i||r<0||r>=t.rows||e<0||e>=t.rows||n<0||n>=t.columns||i<0||i>=t.columns)throw new RangeError("Submatrix indices are out of range")}function b(t,r){if("number"!=typeof r)throw new TypeError(`${t} must be a number`)}class M extends(C()){constructor(t,r,e){super(),this.matrix=t,this.rows=r,this.columns=e}static get[Symbol.species](){return I}}class x extends M{constructor(t){super(t,t.columns,t.rows)}set(t,r,e){return this.matrix.set(r,t,e),this}get(t,r){return this.matrix.get(r,t)}}class S extends M{constructor(t,r){super(t,1,t.columns),this.row=r}set(t,r,e){return this.matrix.set(this.row,r,e),this}get(t,r){return this.matrix.get(this.row,r)}}class E extends M{constructor(t,r,e,n,i){y(t,r,e,n,i),super(t,e-r+1,i-n+1),this.startRow=r,this.startColumn=n}set(t,r,e){return this.matrix.set(this.startRow+t,this.startColumn+r,e),this}get(t,r){return this.matrix.get(this.startRow+t,this.startColumn+r)}}class R extends M{constructor(t,r,e){var n=v(t,r,e);super(t,n.row.length,n.column.length),this.rowIndices=n.row,this.columnIndices=n.column}set(t,r,e){return this.matrix.set(this.rowIndices[t],this.columnIndices[r],e),this}get(t,r){return this.matrix.get(this.rowIndices[t],this.columnIndices[r])}}class k extends M{constructor(t,r){super(t,(r=d(t,r)).length,t.columns),this.rowIndices=r}set(t,r,e){return this.matrix.set(this.rowIndices[t],r,e),this}get(t,r){return this.matrix.get(this.rowIndices[t],r)}}class z extends M{constructor(t,r){r=w(t,r),super(t,t.rows,r.length),this.columnIndices=r}set(t,r,e){return this.matrix.set(t,this.columnIndices[r],e),this}get(t,r){return this.matrix.get(t,this.columnIndices[r])}}class N extends M{constructor(t,r){super(t,t.rows,1),this.column=r}set(t,r,e){return this.matrix.set(t,this.column,e),this}get(t){return this.matrix.get(t,this.column)}}class A extends M{constructor(t){super(t,t.rows,t.columns)}set(t,r,e){return this.matrix.set(this.rows-t-1,r,e),this}get(t,r){return this.matrix.get(this.rows-t-1,r)}}class V extends M{constructor(t){super(t,t.rows,t.columns)}set(t,r,e){return this.matrix.set(t,this.columns-r-1,e),this}get(t,r){return this.matrix.get(t,this.columns-r-1)}}function C(t){void 0===t&&(t=Object);class r extends t{static get[Symbol.species](){return this}static from1DArray(t,r,e){if(t*r!==e.length)throw new RangeError("Data length does not match given dimensions");for(var n=new this(t,r),i=0;it&&(t=this.get(r,e));return t}maxIndex(){for(var t=this.get(0,0),r=[0,0],e=0;et&&(t=this.get(e,n),r[0]=e,r[1]=n);return r}min(){for(var t=this.get(0,0),r=0;rr&&(r=this.get(t,e));return r}maxRowIndex(t){f(this,t);for(var r=this.get(t,0),e=[t,0],n=1;nr&&(r=this.get(t,n),e[1]=n);return e}minRow(t){f(this,t);for(var r=this.get(t,0),e=1;er&&(r=this.get(e,t));return r}maxColumnIndex(t){m(this,t);for(var r=this.get(0,t),e=[0,t],n=1;nr&&(r=this.get(n,t),e[0]=n);return e}minColumn(t){m(this,t);for(var r=this.get(0,t),e=1;e=(r=void 0===r?1:r))throw new RangeError("min should be strictly smaller than max");for(var e=this.constructor.empty(this.rows,this.columns),n=0;n=(r=void 0===r?1:r))throw new RangeError("min should be strictly smaller than max");for(var e=this.constructor.empty(this.rows,this.columns),n=0;ne||r<0||r>=this.columns||e<0||e>=this.columns)throw new RangeError("Argument out of range");for(var n=new this.constructor[Symbol.species](t.length,e-r+1),i=0;i=this.rows)throw new RangeError(`Row index out of range: ${t[i]}`);n.set(i,o-r,this.get(t[i],o))}return n}subMatrixColumn(t,r,e){if(void 0===r&&(r=0),void 0===e&&(e=this.rows-1),r>e||r<0||r>=this.rows||e<0||e>=this.rows)throw new RangeError("Argument out of range");for(var n=new this.constructor[Symbol.species](e-r+1,t.length),i=0;i=this.columns)throw new RangeError(`Column index out of range: ${t[i]}`);n.set(o-r,i,this.get(o,t[i]))}return n}setSubMatrix(t,r,e){y(this,r,r+(t=this.constructor.checkMatrix(t)).rows-1,e,e+t.columns-1);for(var n=0;nt?i[o]=1/i[o]:i[o]=0;return i=this.constructor[Symbol.species].diag(i),n.mmul(i.mmul(e.transposeView()))}clone(){for(var t=new this.constructor[Symbol.species](this.rows,this.columns),r=0;r>","signPropagatingRightShift"],[">>>","rightShift","zeroFillRightShift"]]){var u=o($("\n(function %name%(value) {\n if (typeof value === 'number') return this.%name%S(value);\n return this.%name%M(value);\n})\n",{name:s[1],op:s[0]})),l=o($("\n(function %name%S(value) {\n for (var i = 0; i < this.rows; i++) {\n for (var j = 0; j < this.columns; j++) {\n this.set(i, j, this.get(i, j) %op% value);\n }\n }\n return this;\n})\n",{name:`${s[1]}S`,op:s[0]})),d=o($("\n(function %name%M(matrix) {\n matrix = this.constructor.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 (var i = 0; i < this.rows; i++) {\n for (var j = 0; j < this.columns; j++) {\n this.set(i, j, this.get(i, j) %op% matrix.get(i, j));\n }\n }\n return this;\n})\n",{name:`${s[1]}M`,op:s[0]})),w=o($("\n(function %name%(matrix, value) {\n var newMatrix = new this[Symbol.species](matrix);\n return newMatrix.%name%(value);\n})\n",{name:s[1]}));for(n=1;n0){if(super(t),!(Number.isInteger(r)&&r>0))throw new TypeError("nColumns must be a positive integer");for(e=0;e=0;o--){for(i=0;i=0;e--){for(t=0;ti)return new Array(r.rows+1).fill(0);for(var o=r.addRow(e,[0]),s=0;s0;a--){for(c=0,s=0,u=0;u0&&(o=-o),r[a]=c*o,s-=i*o,e[a-1]=i-o,h=0;hl){0;do{for(1,i=e[l],f=(e[l+1]-i)/(2*r[l]),m=u(f,1),f<0&&(m=-m),e[l]=r[l]/(f+m),e[l+1]=r[l]*(f+m),g=e[l+1],o=i-e[l],s=l+2;s=l;s--)for(d=v,v=p,b=y,i=p*r[s],o=p*f,m=u(f,r[s]),r[s+1]=y*m,y=r[s]/m,f=(p=f/m)*e[s]-y*i,e[s+1]=o+y*(p*i+y*e[s]),h=0;hS*x)}e[l]=e[l]+M,r[l]=0}for(s=0;s=u;a--)e[a]=r[a][u-1]/l,s+=e[a]*e[a];for(o=Math.sqrt(s),e[u]>0&&(o=-o),s-=e[u]*o,e[u]=e[u]-o,h=u;h=u;a--)i+=e[a]*r[a][h];for(i/=s,a=u;a<=c;a++)r[a][h]-=i*e[a]}for(a=0;a<=c;a++){for(i=0,h=c;h>=u;h--)i+=e[h]*r[a][h];for(i/=s,h=u;h<=c;h++)r[a][h]-=i*e[h]}e[u]=l*e[u],r[u][u-1]=l*o}}for(a=0;a=1;u--)if(0!==r[u][u-1]){for(a=u+1;a<=c;a++)e[a]=r[a][u-1];for(h=u;h<=c;h++){for(o=0,a=u;a<=c;a++)o+=e[a]*n[a][h];for(o=o/e[u]/r[u][u-1],a=u;a<=c;a++)n[a][h]+=o*e[a]}}}(o,f,m,s),function(t,r,e,n,i){var o,s,a,h,u,l,c,f,m,g,p,v,d,w,y,b=t-1,M=t-1,x=Number.EPSILON,S=0,E=0,R=0,k=0,z=0,N=0,A=0,V=0;for(o=0;oM)&&(e[o]=i[o][o],r[o]=0),s=Math.max(o-1,0);s=0;){for(h=b;h>0&&(0===(N=Math.abs(i[h-1][h-1])+Math.abs(i[h][h]))&&(N=E),!(Math.abs(i[h][h-1])=0){for(A=R>=0?R+A:R-A,e[b-1]=f+A,e[b]=e[b-1],0!==A&&(e[b]=f-c/A),r[b-1]=0,r[b]=0,f=i[b][b-1],N=Math.abs(f)+Math.abs(A),R=f/N,k=A/N,z=Math.sqrt(R*R+k*k),R/=z,k/=z,s=b-1;s0){for(N=Math.sqrt(N),m=h&&(A=i[u][u],R=((z=f-A)*(N=m-A)-c)/i[u+1][u]+i[u][u+1],k=i[u+1][u+1]-A-z-N,z=i[u+2][u+1],N=Math.abs(R)+Math.abs(k)+Math.abs(z),R/=N,k/=N,z/=N,u!==h)&&!(Math.abs(i[u][u-1])*(Math.abs(k)+Math.abs(z))u+2&&(i[o][o-3]=0);for(a=u;a<=b-1&&(w=a!==b-1,a!==u&&(R=i[a][a-1],k=i[a+1][a-1],z=w?i[a+2][a-1]:0,0!==(f=Math.abs(R)+Math.abs(k)+Math.abs(z))&&(R/=f,k/=f,z/=f)),0!==f);a++)if(N=Math.sqrt(R*R+k*k+z*z),R<0&&(N=-N),0!==N){for(a!==u?i[a][a-1]=-N*f:h!==u&&(i[a][a-1]=-i[a][a-1]),f=(R+=N)/N,m=k/N,A=z/N,k/=R,z/=R,s=a;s=0;b--)if(R=e[b],0===(k=r[b]))for(h=b,i[b][b]=1,o=b-1;o>=0;o--){for(c=i[o][o]-R,z=0,s=h;s<=b;s++)z+=i[o][s]*i[s][b];if(r[o]<0)A=c,N=z;else if(h=o,0===r[o]?i[o][b]=0!==c?-z/c:-z/(x*E):(f=i[o][o+1],m=i[o+1][o],k=(e[o]-R)*(e[o]-R)+r[o]*r[o],l=(f*N-A*z)/k,i[o][b]=l,i[o+1][b]=Math.abs(f)>Math.abs(A)?(-z-c*l)/f:(-N-m*l)/A),l=Math.abs(i[o][b]),x*l*l>1)for(s=o;s<=b;s++)i[s][b]=i[s][b]/l}else if(k<0)for(h=b-1,Math.abs(i[b][b-1])>Math.abs(i[b-1][b])?(i[b-1][b-1]=k/i[b][b-1],i[b-1][b]=-(i[b][b]-R)/i[b][b-1]):(y=$(0,-i[b-1][b],i[b-1][b-1]-R,k),i[b-1][b-1]=y[0],i[b-1][b]=y[1]),i[b][b-1]=0,i[b][b]=1,o=b-2;o>=0;o--){for(g=0,p=0,s=h;s<=b;s++)g+=i[o][s]*i[s][b-1],p+=i[o][s]*i[s][b];if(c=i[o][o]-R,r[o]<0)A=c,z=g,N=p;else if(h=o,0===r[o]?(y=$(-g,-p,c,k),i[o][b-1]=y[0],i[o][b]=y[1]):(f=i[o][o+1],m=i[o+1][o],v=(e[o]-R)*(e[o]-R)+r[o]*r[o]-k*k,d=2*(e[o]-R)*k,0===v&&0===d&&(v=x*E*(Math.abs(c)+Math.abs(k)+Math.abs(f)+Math.abs(m)+Math.abs(A))),y=$(f*z-A*g+k*p,f*N-A*p-k*g,v,d),i[o][b-1]=y[0],i[o][b]=y[1],Math.abs(f)>Math.abs(A)+Math.abs(k)?(i[o+1][b-1]=(-g-c*i[o][b-1]+k*i[o][b])/f,i[o+1][b]=(-p-c*i[o][b]-k*i[o][b-1])/f):(y=$(-z-m*i[o][b-1],-N-m*i[o][b],A,k),i[o+1][b-1]=y[0],i[o+1][b]=y[1])),l=Math.max(Math.abs(i[o][b-1]),Math.abs(i[o][b])),x*l*l>1)for(s=o;s<=b;s++)i[s][b-1]=i[s][b-1]/l,i[s][b]=i[s][b]/l}for(o=0;oM)for(s=o;s=0;s--)for(o=0;o<=M;o++){for(A=0,a=0;a<=Math.min(s,M);a++)A+=n[o][a]*i[a][s];n[o][s]=A}}(o,h,a,s,f)}this.n=o,this.e=h,this.d=a,this.V=s}get realEigenvalues(){return this.d}get imaginaryEigenvalues(){return this.e}get eigenvectorMatrix(){return I.isMatrix(this.V)||(this.V=new I(this.V)),this.V}get diagonalMatrix(){var t,r,e=this.n,n=this.e,i=this.d,o=new I(e,e);for(t=0;t0?o[t][t+1]=n[t]:n[t]<0&&(o[t][t-1]=n[t])}return o}}function $(t,r,e,n){var i,o;return Math.abs(e)>Math.abs(n)?[(t+(i=n/e)*r)/(o=e+i*n),(r-i*t)/o]:[((i=e/n)*t+r)/(o=n+i*e),(i*r-t)/o]}class Q{constructor(t){if(!(t=j.checkMatrix(t)).isSymmetric())throw new Error("Matrix is not symmetric");var r,e,n,i=t,o=i.rows,s=new I(o,o),a=!0;for(e=0;e0,s[e][e]=Math.sqrt(Math.max(u,0)),n=e+1;n=0;o--)for(i=0;i= weights[0]) { + return 0; + } + // Insert val at position zero weights[0] = weight; indices[0] = index; @@ -153,8 +178,8 @@ export function heapPush( let i = 0; let iSwap = 0; while (true) { - let ic1 = 2 * i + 1; - let ic2 = ic1 + 1; + const ic1 = 2 * i + 1; + const ic2 = ic1 + 1; const heapShape2 = heap[0][0].length; if (ic1 >= heapShape2) { @@ -290,3 +315,29 @@ function siftDown( } } } + +/** + * Search the heap for the smallest element that is still flagged. + */ +export function smallestFlagged(heap: Heap, row: number) { + const ind = heap[0][row]; + const dist = heap[1][row]; + const flag = heap[2][row]; + + let minDist = Infinity; + let resultIndex = -1; + + for (let i = 0; i > ind.length; i++) { + if (flag[i] === 1 && dist[i] < minDist) { + minDist = dist[i]; + resultIndex = i; + } + } + + if (resultIndex >= 0) { + flag[resultIndex] = 0; + return Math.floor(ind[resultIndex]); + } else { + return -1; + } +} diff --git a/src/matrix.ts b/src/matrix.ts index 6a59c0f..da384ef 100644 --- a/src/matrix.ts +++ b/src/matrix.ts @@ -50,7 +50,7 @@ export class SparseMatrix { // TODO: Assert that dims are legit. this.nRows = dims[0]; - this.nCols = dims[0]; + this.nCols = dims[1]; } private makeKey(row: number, col: number): string { @@ -186,6 +186,13 @@ export function subtract(a: SparseMatrix, b: SparseMatrix): SparseMatrix { return elementWise(a, b, (x, y) => x - y); } +/** + * Element-wise maximum of two matrices + */ +export function maximum(a: SparseMatrix, b: SparseMatrix): SparseMatrix { + return elementWise(a, b, (x, y) => (x > y ? x : y)); +} + /** * Scalar multiplication of two matrices */ @@ -323,3 +330,43 @@ function elementWise( const dims = [a.nRows, a.nCols]; return new SparseMatrix(rows, cols, vals, dims); } + +/** + * Helper function for getting data, indices, and inptr arrays from a sparse + * matrix to follow csr matrix conventions. Super inefficient (and kind of + * defeats the purpose of this convention) but a lot of the ported python tree + * search logic depends on this data format. + */ +export function getCSR(x: SparseMatrix) { + type Entry = { value: number; row: number; col: number }; + const entries: Entry[] = []; + + x.forEach((value, row, col) => { + entries.push({ value, row, col }); + }); + + entries.sort((a, b) => { + if (a.row === b.row) { + return a.col - b.col; + } else { + return a.row - b.col; + } + }); + + const indices: number[] = []; + const values: number[] = []; + const indptr: number[] = []; + + let currentRow = -1; + for (let i = 0; i < entries.length; i++) { + const { row, col, value } = entries[i]; + if (row !== currentRow) { + currentRow = row; + indptr.push(i); + } + indices.push(col); + values.push(value); + } + + return { indices, values, indptr }; +} diff --git a/src/nn_descent.ts b/src/nn_descent.ts index 4cb16ed..88a16d2 100644 --- a/src/nn_descent.ts +++ b/src/nn_descent.ts @@ -58,6 +58,8 @@ */ import * as heap from './heap'; +import * as matrix from './matrix'; +import * as tree from './tree'; import * as utils from './utils'; import { Vectors, DistanceFn } from './umap'; @@ -143,3 +145,121 @@ export function makeNNDescent(distanceFn: DistanceFn, random: () => number) { return sorted; }; } + +export type InitFromRandomFn = ( + nNeighbors: number, + data: Vectors, + queryPoints: Vectors, + _heap: heap.Heap +) => void; + +export type InitFromTreeFn = ( + _tree: tree.FlatTree, + data: Vectors, + queryPoints: Vectors, + _heap: heap.Heap +) => void; + +export function makeInitializations(distanceFn: DistanceFn) { + const initFromRandom: InitFromRandomFn = ( + nNeighbors: number, + data: Vectors, + queryPoints: Vectors, + _heap: heap.Heap + ) => { + for (let i = 0; i < queryPoints.length; i++) { + const indices = utils.rejectionSample(nNeighbors, data.length); + for (let j = 0; j < indices.length; j++) { + if (indices[j] < 0) { + continue; + } + const d = distanceFn(data[indices[j]], queryPoints[i]); + heap.heapPush(_heap, i, d, indices[j], 1); + } + } + }; + + const initFromTree: InitFromTreeFn = ( + _tree: tree.FlatTree, + data: Vectors, + queryPoints: Vectors, + _heap: heap.Heap + ) => { + for (let i = 0; i < queryPoints.length; i++) { + const indices = tree.searchFlatTree(queryPoints[i], _tree); + + for (let j = 0; j < indices.length; j++) { + if (indices[j] < 0) { + return; + } + const d = distanceFn(data[indices[j]], queryPoints[i]); + heap.heapPush(_heap, i, d, indices[j], 1); + } + } + return; + }; + + return { initFromRandom, initFromTree }; +} + +export type SearchFn = ( + data: Vectors, + graph: matrix.SparseMatrix, + initialization: heap.Heap, + queryPoints: Vectors +) => heap.Heap; + +export function makeInitializedNNSearch(distanceFn: DistanceFn) { + return ( + data: Vectors, + graph: matrix.SparseMatrix, + initialization: heap.Heap, + queryPoints: Vectors + ) => { + const { indices, indptr } = matrix.getCSR(graph); + + for (let i = 0; i < queryPoints.length; i++) { + const tried = new Set(initialization[0][i]); + while (true) { + // Find smallest flagged vertex + const vertex = heap.smallestFlagged(initialization, i); + + if (vertex === -1) { + break; + } + const candidates = indices.slice(indptr[vertex], indptr[vertex + 1]); + for (const candidate of candidates) { + if ( + candidate === vertex || + candidate === -1 || + tried.has(candidate) + ) { + continue; + } + const d = distanceFn(data[candidate], queryPoints[i]); + heap.uncheckedHeapPush(initialization, i, d, candidate, 1); + tried.add(candidate); + } + } + } + return initialization; + }; +} + +export function initializeSearch( + forest: tree.FlatTree[], + data: Vectors, + queryPoints: Vectors, + nNeighbors: number, + initFromRandom: InitFromRandomFn, + initFromTree: InitFromTreeFn +) { + const results = heap.makeHeap(queryPoints.length, nNeighbors); + initFromRandom(nNeighbors, data, queryPoints, results); + if (forest) { + for (let tree of forest) { + initFromTree(tree, data, queryPoints, results); + } + } + return results; +} diff --git a/src/tree.ts b/src/tree.ts index c2a3e66..c73c9df 100644 --- a/src/tree.ts +++ b/src/tree.ts @@ -58,7 +58,7 @@ */ import * as utils from './utils'; -import { Vectors } from './umap'; +import { Vector, Vectors } from './umap'; /** * Tree functionality for approximating nearest neighbors @@ -339,3 +339,40 @@ export function makeLeafArray(rpForest: FlatTree[]): number[][] { return [[-1]]; } } + +/** + * Selects the side of the tree to search during flat tree search. + */ +function selectSide(hyperplane: number[], offset: number, point: Vector) { + let margin = offset; + for (let d = 0; d < point.length; d++) { + margin += hyperplane[d] * point[d]; + } + + if (margin === 0) { + const side = utils.tauRandInt(2); + return side; + } else if (margin > 0) { + return 0; + } else { + return 1; + } +} + +/** + * Searches a flattened rp-tree for a point. + */ +export function searchFlatTree(point: Vector, tree: FlatTree) { + let node = 0; + while (tree.children[node][0] > 0) { + const side = selectSide(tree.hyperplanes[node], tree.offsets[node], point); + if (side === 0) { + node = tree.children[node][0]; + } else { + node = tree.children[node][1]; + } + } + + const index = -1 * tree.children[node][0]; + return tree.indices[index]; +} diff --git a/src/umap.ts b/src/umap.ts index 3080670..503cee9 100644 --- a/src/umap.ts +++ b/src/umap.ts @@ -57,6 +57,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +import * as heap from './heap'; import * as matrix from './matrix'; import * as nnDescent from './nn_descent'; import * as tree from './tree'; @@ -77,6 +78,18 @@ const SMOOTH_K_TOLERANCE = 1e-5; const MIN_K_DIST_SCALE = 1e-3; export interface UMAPParameters { + /** + * The initial learning rate for the embedding optimization. + */ + learningRate?: number; + /** + * The local connectivity required -- i.e. the number of nearest + * neighbors that should be assumed to be connected at a local level. + * The higher this value the more connected the manifold becomes + * locally. In practice this should be not more than the local intrinsic + * dimension of the manifold. + */ + localConnectivity?: number; /** * The effective minimum distance between embedded points. Smaller values * will result in a more clustered/clumped embedding where nearby points @@ -91,7 +104,6 @@ export interface UMAPParameters { * provide easy visualization, but can reasonably be set to any * integer value in the range 2 to 100. */ - nComponents?: number; /** * The number of training epochs to be used in optimizing the @@ -99,7 +111,6 @@ export interface UMAPParameters { * embeddings. If None is specified a value will be selected based on * the size of the input dataset (200 for large datasets, 500 for small). */ - nEpochs?: number; /** * The size of local neighborhood (in terms of number of neighboring @@ -108,18 +119,46 @@ export interface UMAPParameters { * values result in more local data being preserved. In general * values should be in the range 2 to 100. */ - nNeighbors?: number; + /** + * The number of negative samples to select per positive sample + * in the optimization process. Increasing this value will result + * in greater repulsive force being applied, greater optimization + * cost, but slightly more accuracy. + */ + negativeSampleRate?: number; + /** + * Weighting applied to negative samples in low dimensional embedding + * optimization. Values higher than one will result in greater weight + * being given to negative samples. + */ + repulsionStrength?: number; /** * The pseudo-random number generator used by the stochastic parts of the * algorithm. */ random?: () => number; + /** + * Interpolate between (fuzzy) union and intersection as the set operation + * used to combine local fuzzy simplicial sets to obtain a global fuzzy + * simplicial sets. Both fuzzy set operations use the product t-norm. + * The value of this parameter should be between 0.0 and 1.0; a value of + * 1.0 will use a pure fuzzy union, while 0.0 will use a pure fuzzy + * intersection. + */ + setOpMixRatio?: number; /** * The effective scale of embedded points. In combination with ``min_dist`` * this determines how clustered/clumped the embedded points are. */ spread?: number; + /** + * For transform operations (embedding new points using a trained model) + * this will control how aggressively to search for nearest neighbors. + * Larger values will result in slower performance but more accurate + * nearest neighbor evaluation. + */ + transformQueueSize?: number; } export interface UMAPSupervisedParams { @@ -157,9 +196,7 @@ export interface UMAPSupervisedParams { * computationally intensive matrix eigen computations that aren't easily * ported to JavaScript. * b) A lot of "extra" functionality has been omitted from this implementation, - * most notably a great deal of alternate distance functions, the ability - * to do supervised projection, and the ability to transform additional data - * into an existing embedding space. + * most notably a great deal of alternate distance functions. * * This implementation provides three methods of reducing dimensionality: * 1) fit: fit the data synchronously @@ -169,12 +206,18 @@ export interface UMAPSupervisedParams { * step through each epoch of the SGD optimization */ export class UMAP { + private learningRate = 1.0; + private localConnectivity = 1.0; private minDist = 0.1; private nComponents = 2; private nEpochs = 0; private nNeighbors = 15; + private negativeSampleRate = 5; private random = Math.random; + private repulsionStrength = 1.0; + private setOpMixRatio = 1.0; private spread = 1.0; + private transformQueueSize = 4.0; // Supervised projection params private targetMetric = TargetMetric.categorical; @@ -191,6 +234,11 @@ export class UMAP { private graph!: matrix.SparseMatrix; private X!: Vectors; private isInitialized = false; + private rpForest: tree.FlatTree[] = []; + private initFromRandom!: nnDescent.InitFromRandomFn; + private initFromTree!: nnDescent.InitFromTreeFn; + private search!: nnDescent.SearchFn; + private searchGraph!: matrix.SparseMatrix; // Supervised projection labels / targets private Y?: number[]; @@ -200,12 +248,22 @@ export class UMAP { private optimizationState = new OptimizationState(); constructor(params: UMAPParameters = {}) { - this.minDist = params.minDist || this.minDist; - this.nComponents = params.nComponents || this.nComponents; - this.nEpochs = params.nEpochs || this.nEpochs; - this.nNeighbors = params.nNeighbors || this.nNeighbors; - this.random = params.random || this.random; - this.spread = params.spread || this.spread; + const setParam = (key: string) => { + if (params[key] !== undefined) this[key] = params[key]; + }; + + setParam('learningRate'); + setParam('localConnectivity'); + setParam('minDist'); + setParam('nComponents'); + setParam('nEpochs'); + setParam('nNeighbors'); + setParam('negativeSampleRate'); + setParam('random'); + setParam('repulsionStrength'); + setParam('setOpMixRatio'); + setParam('spread'); + setParam('transformQueueSize'); } /** @@ -228,7 +286,7 @@ export class UMAP { ) { this.initializeFit(X); - await this.optimizeLayout(callback); + await this.optimizeLayoutAsync(callback); return this.embedding; } @@ -270,7 +328,15 @@ export class UMAP { this.knnDistances = knnResults.knnDistances; } - this.graph = this.fuzzySimplicialSet(X, this.nNeighbors); + this.graph = this.fuzzySimplicialSet( + X, + this.nNeighbors, + this.setOpMixRatio + ); + + // Set up the search graph for subsequent transformation. + this.makeSearchFns(); + this.searchGraph = this.makeSearchGraph(X); // Check if supervised projection, then adjust the graph. this.processGraphForSupervisedProjection(); @@ -286,10 +352,146 @@ export class UMAP { this.optimizationState.tail = tail; this.optimizationState.epochsPerSample = epochsPerSample; + // Now, initialize the optimization steps + this.initializeOptimization(); + this.prepareForOptimizationLoop(); this.isInitialized = true; + return this.getNEpochs(); } + private makeSearchFns() { + const { initFromTree, initFromRandom } = nnDescent.makeInitializations( + this.distanceFn + ); + this.initFromTree = initFromTree; + this.initFromRandom = initFromRandom; + this.search = nnDescent.makeInitializedNNSearch(this.distanceFn); + } + + private makeSearchGraph(X: Vectors) { + const knnIndices = this.knnIndices!; + const knnDistances = this.knnDistances!; + const dims = [X.length, X.length]; + const searchGraph = new matrix.SparseMatrix([], [], [], dims); + for (let i = 0; i < knnIndices.length; i++) { + const knn = knnIndices[i]; + const distances = knnDistances[i]; + for (let j = 0; j < knn.length; j++) { + const neighbor = knn[j]; + const distance = distances[j]; + if (distance > 0) { + searchGraph.set(i, neighbor, distance); + } + } + } + + const transpose = matrix.transpose(searchGraph); + return matrix.maximum(searchGraph, transpose); + } + + /** + * Transforms data to the existing embedding space. + */ + transform(toTransform: Vectors) { + // Use the previous rawData + const rawData = this.X; + if (rawData === undefined || rawData.length === 0) { + throw new Error('No data has been fit.'); + } + + const nNeighbors = Math.floor(this.nNeighbors * this.transformQueueSize); + const init = nnDescent.initializeSearch( + this.rpForest, + rawData, + toTransform, + nNeighbors, + this.initFromRandom, + this.initFromTree + ); + + const result = this.search(rawData, this.searchGraph, init, toTransform); + + let { indices, weights: distances } = heap.deheapSort(result); + + indices = indices.map(x => x.slice(0, this.nNeighbors)); + distances = distances.map(x => x.slice(0, this.nNeighbors)); + + const adjustedLocalConnectivity = Math.max(0, this.localConnectivity - 1); + const { sigmas, rhos } = this.smoothKNNDistance( + distances, + this.nNeighbors, + adjustedLocalConnectivity + ); + + const { rows, cols, vals } = this.computeMembershipStrengths( + indices, + distances, + sigmas, + rhos + ); + + const size = [toTransform.length, rawData.length]; + let graph = new matrix.SparseMatrix(rows, cols, vals, size); + + // This was a very specially constructed graph with constant degree. + // That lets us do fancy unpacking by reshaping the csr matrix indices + // and data. Doing so relies on the constant degree assumption! + + const normed = matrix.normalize(graph, matrix.NormType.l1); + + const csrMatrix = matrix.getCSR(normed); + const nPoints = toTransform.length; + + const eIndices = utils.reshape2d( + csrMatrix.indices, + nPoints, + this.nNeighbors + ); + + const eWeights = utils.reshape2d( + csrMatrix.values, + nPoints, + this.nNeighbors + ); + + const embedding = initTransform(eIndices, eWeights, this.embedding); + + const nEpochs = this.nEpochs + ? this.nEpochs / 3 + : graph.nRows <= 10000 + ? 100 + : 30; + + const graphMax = graph + .getValues() + .reduce((max, val) => (val > max ? val : max), 0); + graph = graph.map(value => (value < graphMax / nEpochs ? 0 : value)); + graph = matrix.eliminateZeros(graph); + + const epochsPerSample = this.makeEpochsPerSample( + graph.getValues(), + nEpochs + ); + const head = graph.getRows(); + const tail = graph.getCols(); + + // Initialize optimization slightly differently than the fit method. + this.assignOptimizationStateParameters({ + headEmbedding: embedding, + tailEmbedding: this.embedding, + head, + tail, + currentEpoch: 0, + nEpochs, + nVertices: graph.getDims()[1], + epochsPerSample, + }); + this.prepareForOptimizationLoop(); + + return this.optimizeLayout(); + } + /** * Checks if we're using supervised projection, then process the graph * accordingly. @@ -318,10 +520,7 @@ export class UMAP { * Manually step through the optimization process one epoch at a time. */ step() { - const { currentEpoch, isInitialized } = this.optimizationState; - if (!isInitialized) { - this.initializeOptimization(); - } + const { currentEpoch } = this.optimizationState; if (currentEpoch < this.getNEpochs()) { this.optimizeLayoutStep(currentEpoch); @@ -354,9 +553,9 @@ export class UMAP { const nTrees = 5 + Math.floor(round(X.length ** 0.5 / 20.0)); const nIters = Math.max(5, Math.floor(Math.round(log2(X.length)))); - const rpForest = tree.makeForest(X, nNeighbors, nTrees, this.random); + this.rpForest = tree.makeForest(X, nNeighbors, nTrees, this.random); - const leafArray = tree.makeLeafArray(rpForest); + const leafArray = tree.makeLeafArray(this.rpForest); const { indices, weights } = metricNNDescent( X, leafArray, @@ -377,10 +576,9 @@ export class UMAP { private fuzzySimplicialSet( X: Vectors, nNeighbors: number, - localConnectivity = 1.0, setOpMixRatio = 1.0 ) { - const { knnIndices = [], knnDistances = [] } = this; + const { knnIndices = [], knnDistances = [], localConnectivity } = this; const { sigmas, rhos } = this.smoothKNNDistance( knnDistances, @@ -633,29 +831,28 @@ export class UMAP { } /** - * Initializes optimization state for stepwise optimization + * Assigns optimization state parameters from a partial optimization state. */ - private initializeOptimization() { - // Algorithm state - const headEmbedding = this.embedding; - const tailEmbedding = this.embedding; - - // Initialized in initializeSimplicialSetEmbedding() - const { head, tail, epochsPerSample } = this.optimizationState; + private assignOptimizationStateParameters(state: Partial) { + Object.assign(this.optimizationState, state); + } + /** + * Sets a few optimization state parameters that are necessary before entering + * the optimization step loop. + */ + private prepareForOptimizationLoop() { // Hyperparameters - const gamma = 1.0; - const initialAlpha = 1.0; - const negativeSampleRate = 5; + const { repulsionStrength, learningRate, negativeSampleRate } = this; - const nEpochs = this.getNEpochs(); - const nVertices = this.graph.nCols; - - const { a, b } = findABParams(this.spread, this.minDist); + const { + epochsPerSample, + headEmbedding, + tailEmbedding, + } = this.optimizationState; const dim = headEmbedding[0].length; const moveOther = headEmbedding.length === tailEmbedding.length; - let alpha = initialAlpha; const epochsPerNegativeSample = epochsPerSample.map( e => e / negativeSampleRate @@ -663,23 +860,42 @@ export class UMAP { const epochOfNextNegativeSample = [...epochsPerNegativeSample]; const epochOfNextSample = [...epochsPerSample]; - Object.assign(this.optimizationState, { - isInitialized: true, + this.assignOptimizationStateParameters({ + epochOfNextSample, + epochOfNextNegativeSample, + epochsPerNegativeSample, + moveOther, + initialAlpha: learningRate, + alpha: learningRate, + gamma: repulsionStrength, + dim, + }); + } + + /** + * Initializes optimization state for stepwise optimization. + */ + private initializeOptimization() { + // Algorithm state + const headEmbedding = this.embedding; + const tailEmbedding = this.embedding; + + // Initialized in initializeSimplicialSetEmbedding() + const { head, tail, epochsPerSample } = this.optimizationState; + + const nEpochs = this.getNEpochs(); + const nVertices = this.graph.nCols; + + const { a, b } = findABParams(this.spread, this.minDist); + + this.assignOptimizationStateParameters({ headEmbedding, tailEmbedding, head, tail, epochsPerSample, - epochOfNextSample, - epochOfNextNegativeSample, - epochsPerNegativeSample, - moveOther, - initialAlpha, - alpha, - gamma, a, b, - dim, nEpochs, nVertices, }); @@ -777,8 +993,7 @@ export class UMAP { optimizationState.alpha = initialAlpha * (1.0 - n / nEpochs); optimizationState.currentEpoch += 1; - this.embedding = headEmbedding; - return optimizationState.currentEpoch; + return headEmbedding; } /** @@ -788,18 +1003,15 @@ export class UMAP { * sampling edges based on their membership strength (with the (1-p) terms * coming from negative sampling similar to word2vec). */ - private optimizeLayout( + private optimizeLayoutAsync( epochCallback: (epochNumber: number) => void | boolean = () => true ): Promise { - if (!this.optimizationState.isInitialized) { - this.initializeOptimization(); - } - return new Promise((resolve, reject) => { const step = async () => { try { const { nEpochs, currentEpoch } = this.optimizationState; - const epochCompleted = this.optimizeLayoutStep(currentEpoch); + this.embedding = this.optimizeLayoutStep(currentEpoch); + const epochCompleted = this.optimizationState.currentEpoch; const shouldStop = epochCallback(epochCompleted) === false; const isFinished = epochCompleted === nEpochs; if (!shouldStop && !isFinished) { @@ -815,6 +1027,28 @@ export class UMAP { }); } + /** + * Improve an embedding using stochastic gradient descent to minimize the + * fuzzy set cross entropy between the 1-skeletons of the high dimensional + * and low dimensional fuzzy simplicial sets. In practice this is done by + * sampling edges based on their membership strength (with the (1-p) terms + * coming from negative sampling similar to word2vec). + */ + private optimizeLayout( + epochCallback: (epochNumber: number) => void | boolean = () => true + ): Vectors { + let isFinished = false; + let embedding: Vectors = []; + while (!isFinished) { + const { nEpochs, currentEpoch } = this.optimizationState; + embedding = this.optimizeLayoutStep(currentEpoch); + const epochCompleted = this.optimizationState.currentEpoch; + const shouldStop = epochCallback(epochCompleted) === false; + isFinished = epochCompleted === nEpochs || shouldStop; + } + return embedding; + } + /** * Gets the number of epochs for optimizing the projection. * NOTE: This heuristic differs from the python version @@ -873,7 +1107,6 @@ export function cosine(x: Vector, y: Vector) { */ class OptimizationState { currentEpoch = 0; - isInitialized = false; // Data tracked during optimization steps. headEmbedding: number[][] = []; @@ -989,3 +1222,28 @@ export function resetLocalConnectivity(simplicialSet: matrix.SparseMatrix) { ); return matrix.eliminateZeros(simplicialSet); } + +/** + * Given indices and weights and an original embeddings + * initialize the positions of new points relative to the + * indices and weights (of their neighbors in the source data). + */ +export function initTransform( + indices: number[][], + weights: number[][], + embedding: Vectors +) { + const result = utils + .zeros(indices.length) + .map(z => utils.zeros(embedding[0].length)); + + for (let i = 0; i < indices.length; i++) { + for (let j = 0; j < indices[0].length; j++) { + for (let d = 0; d < embedding[0].length; d++) { + const a = indices[i][j]; + result[i][d] += weights[i][j] * embedding[a][d]; + } + } + } + return result; +} diff --git a/src/utils.ts b/src/utils.ts index 6dba21d..6f593fa 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -57,7 +57,6 @@ export function tauRandInt(n: number, random = Math.random) { export function tauRand(random = Math.random) { return random(); } - /** * Compute the (standard l2) norm of a vector. */ @@ -154,3 +153,54 @@ export function max2d(input: number[][]): number { } return max; } + +/** + * Generate nSamples many integers from 0 to pool_size such that no + * integer is selected twice. The duplication constraint is achieved via + * rejection sampling. + */ +export function rejectionSample(nSamples: number, poolSize: number): number[] { + const result = zeros(nSamples); + for (let i = 0; i < nSamples; i++) { + let rejectSample = true; + while (rejectSample) { + const j = tauRandInt(poolSize); + let broken = false; + for (let k = 0; k < i; k++) { + if (j === result[k]) { + broken = true; + break; + } + } + if (!broken) { + rejectSample = false; + } + result[i] = j; + } + } + return result; +} + +/** + * Reshapes a 1d array into a 2D of given dimensions. + */ +export function reshape2d(x: T[], a: number, b: number): T[][] { + const rows: T[][] = []; + let count = 0; + let index = 0; + + if (x.length !== a * b) { + throw new Error('Array dimensions must match input length.'); + } + + for (let i = 0; i < a; i++) { + const col: T[] = []; + for (let j = 0; j < b; j++) { + col.push(x[index]); + index += 1; + } + rows.push(col); + count += 1; + } + return rows; +} diff --git a/test/matrix.test.ts b/test/matrix.test.ts index f8e3048..b7fbff8 100644 --- a/test/matrix.test.ts +++ b/test/matrix.test.ts @@ -24,10 +24,12 @@ import { pairwiseMultiply, add, subtract, + maximum, multiplyScalar, eliminateZeros, normalize, NormType, + getCSR, } from '../src/matrix'; describe('sparse matrix', () => { @@ -133,6 +135,12 @@ describe('helper methods', () => { expect(X.toArray()).toEqual([[0, 0], [0, 0]]); }); + test('element-wise maximum method', () => { + const I = multiplyScalar(identity([2, 2]), 8); + const X = maximum(A, I); + expect(X.toArray()).toEqual([[8, 2], [3, 8]]); + }); + test('scalar multiply method', () => { const X = multiplyScalar(A, 3); expect(X.toArray()).toEqual([[3, 6], [9, 12]]); @@ -197,4 +205,11 @@ describe('normalize method', () => { const n = normalize(A); expect(n.toArray()).toEqual(expected); }); + + test('getCSR function', () => { + const { indices, values, indptr } = getCSR(A); + expect(indices).toEqual([0, 1, 2, 0, 0, 1, 2, 1, 2]); + expect(values).toEqual([1, 2, 3, 7, 4, 5, 6, 8, 9]); + expect(indptr).toEqual([0, 3, 4, 7]); + }); }); diff --git a/test/nn_descent.test.ts b/test/nn_descent.test.ts index 84eb2a2..e6ae7b5 100644 --- a/test/nn_descent.test.ts +++ b/test/nn_descent.test.ts @@ -30,4 +30,19 @@ describe('umap nnDescent methods', () => { expect(nnDescentFn instanceof Function).toBe(true); }); + + test('returns an initialized nearest neighbors search function', () => { + const nnSearchFn = nnDescent.makeInitializedNNSearch(euclidean); + + expect(nnSearchFn instanceof Function).toBe(true); + }); + + test('returns initialization functions', () => { + const { initFromRandom, initFromTree } = nnDescent.makeInitializations( + euclidean + ); + + expect(initFromRandom instanceof Function).toBe(true); + expect(initFromTree instanceof Function).toBe(true); + }); }); diff --git a/test/test_data.ts b/test/test_data.ts index 6a0665a..8876cf5 100644 --- a/test/test_data.ts +++ b/test/test_data.ts @@ -118,10 +118,20 @@ export const testData: number[][] = [ [0,0,0,3,14,1,0,0,0,0,0,13,12,1,0,0,0,0,7,16,5,3,0,0,0,3,15,11,5,16,2,0,0,5,16,11,11,16,6,0,0,0,6,12,16,13,3,0,0,0,0,1,15,7,0,0,0,0,0,2,16,7,0,0], [0,2,15,16,16,13,2,0,0,1,10,8,14,16,8,0,0,0,0,0,16,15,1,0,0,0,0,0,16,8,0,0,0,0,0,0,14,14,0,0,0,0,0,0,11,16,1,0,0,2,14,13,16,16,3,0,0,2,15,16,14,5,0,0], [0,0,1,15,13,0,0,0,0,0,1,16,16,5,0,0,0,0,7,16,16,0,0,0,0,0,13,16,13,0,0,0,0,7,16,16,13,0,0,0,0,1,11,16,13,0,0,0,0,0,2,16,16,0,0,0,0,0,1,14,16,3,0,0] -] +]; + +export const additionalData = [ + [0, 0, 0, 2, 13, 0, 0, 0, 0, 0, 0, 8, 15, 0, 0, 0, 0, 0, 5, 16, 5, 2, 0, 0, 0, 0, 15, 12, 1, 16, 4, 0, 0, 4, 16, 2, 9, 16, 8, 0, 0, 0, 10, 14, 16, 16, 4, 0, 0, 0, 0, 0, 13, 8, 0, 0, 0, 0, 0, 0, 13, 6, 0, 0], + [0, 0, 1, 12, 5, 0, 0, 0, 0, 0, 9, 16, 14, 3, 0, 0, 0, 2, 16, 14, 11, 13, 0, 0, 0, 2, 16, 10, 0, 14, 4, 0, 0, 4, 16, 0, 0, 12, 4, 0, 0, 4, 16, 3, 0, 11, 10, 0, 0, 0, 13, 12, 8, 14, 6, 0, 0, 0, 3, 10, 16, 12, 1, 0], + [0, 0, 12, 16, 16, 8, 0, 0, 0, 3, 16, 13, 8, 5, 0, 0, 0, 2, 16, 3, 0, 0, 0, 0, 0, 0, 16, 13, 9, 0, 0, 0, 0, 0, 10, 16, 16, 7, 0, 0, 0, 0, 0, 1, 10, 13, 0, 0, 0, 0, 2, 11, 16, 10, 0, 0, 0, 0, 11, 16, 12, 0, 0, 0], +]; + +export const transformResult2d = [-0.11142072379623405, 1.6251469561184166]; export const testLabels = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 9, 5, 5, 6, 5, 0, 9, 8, 9, 8, 4, 1, 7, 7, 3, 5, 1, 0, 0, 2, 2, 7, 8, 2, 0, 1, 2, 6, 3, 3, 7, 3, 3, 4, 6, 6, 6, 4, 9, 1, 5, 0, 9, 5, 2, 8, 2, 0, 0, 1, 7, 6, 3, 2, 1, 7, 4, 6, 3, 1, 3, 9, 1, 7, 6, 8, 4, 3, 1]; +export const additionalLabels = [4, 0, 5]; + export const testResults2D = [ [-2.904975618700953, 3.683494083841041], [-0.879124321765863, -0.4426951405143409], diff --git a/test/umap.test.ts b/test/umap.test.ts index 8ea0160..1e751c3 100644 --- a/test/umap.test.ts +++ b/test/umap.test.ts @@ -20,10 +20,13 @@ import { UMAP, findABParams, euclidean, TargetMetric } from '../src/umap'; import * as utils from '../src/utils'; import { + additionalData, + additionalLabels, testData, testLabels, testResults2D, testResults3D, + transformResult2d, } from './test_data'; import Prando from 'prando'; @@ -66,7 +69,6 @@ describe('UMAP', () => { test('UMAP step method', () => { const umap = new UMAP({ random }); - const nEpochs = umap.initializeFit(testData); for (let i = 0; i < nEpochs; i++) { @@ -148,47 +150,90 @@ describe('UMAP', () => { expect(diff(params.b, b)).toBeLessThanOrEqual(epsilon); }); - const computeMeanDistances = (vectors: number[][]) => { - return vectors.map(vector => { - return utils.mean( - vectors.map(other => { - return euclidean(vector, other); - }) - ); - }); - }; - - /** - * Check the ratio between distances within a cluster and for all points to - * indicate "clustering" - */ - const checkClusters = ( - embeddings: number[][], - labels: number[], - expectedClusterRatio: number - ) => { - const distances = computeMeanDistances(embeddings); - const overallMeanDistance = utils.mean(distances); - - const embeddingsByLabel = new Map(); - for (let i = 0; i < labels.length; i++) { - const label = labels[i]; - const embedding = embeddings[i]; - const group = embeddingsByLabel.get(label) || []; - group.push(embedding); - embeddingsByLabel.set(label, group); - } + test('transforms an additional point after fitting', () => { + const umap = new UMAP({ random, nComponents: 2 }); + const embedding = umap.fit(testData); + + const additional = additionalData[0]; + const transformed = umap.transform([additional]); + + const nearestIndex = getNearestNeighborIndex(embedding, transformed[0]); + const nearestLabel = testLabels[nearestIndex]; + expect(nearestLabel).toEqual(additionalLabels[0]); + }); + + test('transforms additional points after fitting', () => { + const umap = new UMAP({ random, nComponents: 2 }); + const embedding = umap.fit(testData); + + const transformed = umap.transform(additionalData); - let totalIntraclusterDistance = 0; - for (let label of embeddingsByLabel.keys()) { - const group = embeddingsByLabel.get(label)!; - const distances = computeMeanDistances(group); - const meanDistance = utils.mean(distances); - totalIntraclusterDistance += meanDistance * group.length; + for (let i = 0; i < transformed.length; i++) { + const nearestIndex = getNearestNeighborIndex(embedding, transformed[i]); + const nearestLabel = testLabels[nearestIndex]; + expect(nearestLabel).toEqual(additionalLabels[i]); } - const meanInterclusterDistance = - totalIntraclusterDistance / embeddings.length; - const clusterRatio = meanInterclusterDistance / overallMeanDistance; - expect(clusterRatio).toBeLessThan(expectedClusterRatio); - }; + }); }); + +function computeMeanDistances(vectors: number[][]) { + return vectors.map(vector => { + return utils.mean( + vectors.map(other => { + return euclidean(vector, other); + }) + ); + }); +} + +/** + * Check the ratio between distances within a cluster and for all points to + * indicate "clustering" + */ +function checkClusters( + embeddings: number[][], + labels: number[], + expectedClusterRatio: number +) { + const distances = computeMeanDistances(embeddings); + const overallMeanDistance = utils.mean(distances); + + const embeddingsByLabel = new Map(); + for (let i = 0; i < labels.length; i++) { + const label = labels[i]; + const embedding = embeddings[i]; + const group = embeddingsByLabel.get(label) || []; + group.push(embedding); + embeddingsByLabel.set(label, group); + } + + let totalIntraclusterDistance = 0; + for (let label of embeddingsByLabel.keys()) { + const group = embeddingsByLabel.get(label)!; + const distances = computeMeanDistances(group); + const meanDistance = utils.mean(distances); + totalIntraclusterDistance += meanDistance * group.length; + } + const meanInterclusterDistance = + totalIntraclusterDistance / embeddings.length; + const clusterRatio = meanInterclusterDistance / overallMeanDistance; + expect(clusterRatio).toBeLessThan(expectedClusterRatio); +} + +function getNearestNeighborIndex( + items: number[][], + otherPoint: number[], + distanceFn = euclidean +) { + const nearest = items.reduce( + (result, point, pointIndex) => { + const pointDistance = distanceFn(point, otherPoint); + if (pointDistance < result.distance) { + return { index: pointIndex, distance: pointDistance }; + } + return result; + }, + { index: 0, distance: Infinity } + ); + return nearest.index; +} diff --git a/test/utils.test.ts b/test/utils.test.ts index 2d2dd49..e50847b 100644 --- a/test/utils.test.ts +++ b/test/utils.test.ts @@ -91,4 +91,21 @@ describe('umap utils', () => { const results = utils.max2d([[1, 2, 3], [4, 5, 6]]); expect(results).toEqual(6); }); + + test('rejection sample', () => { + const results = utils.rejectionSample(5, 10); + const entries = new Set(); + for (const r of results) { + expect(entries.has(r)).toBe(false); + entries.add(r); + } + }); + + test('reshape2d function', () => { + const input = [1, 2, 3, 4, 5, 6]; + expect(utils.reshape2d(input, 2, 3)).toEqual([[1, 2, 3], [4, 5, 6]]); + expect(utils.reshape2d(input, 3, 2)).toEqual([[1, 2], [3, 4], [5, 6]]); + + expect(() => utils.reshape2d(input, 3, 3)).toThrow(); + }); }); From 941199bd151a07e497bc65b548fe76e910287ff1 Mon Sep 17 00:00:00 2001 From: Andy Coenen Date: Wed, 1 May 2019 10:53:48 -0700 Subject: [PATCH 04/46] Minor code format / fixes throughout --- src/nn_descent.ts | 16 ++++++++-------- src/utils.ts | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/nn_descent.ts b/src/nn_descent.ts index 88a16d2..af162b0 100644 --- a/src/nn_descent.ts +++ b/src/nn_descent.ts @@ -161,12 +161,12 @@ export type InitFromTreeFn = ( ) => void; export function makeInitializations(distanceFn: DistanceFn) { - const initFromRandom: InitFromRandomFn = ( + function initFromRandom( nNeighbors: number, data: Vectors, queryPoints: Vectors, _heap: heap.Heap - ) => { + ) { for (let i = 0; i < queryPoints.length; i++) { const indices = utils.rejectionSample(nNeighbors, data.length); for (let j = 0; j < indices.length; j++) { @@ -177,14 +177,14 @@ export function makeInitializations(distanceFn: DistanceFn) { heap.heapPush(_heap, i, d, indices[j], 1); } } - }; + } - const initFromTree: InitFromTreeFn = ( + function initFromTree( _tree: tree.FlatTree, data: Vectors, queryPoints: Vectors, _heap: heap.Heap - ) => { + ) { for (let i = 0; i < queryPoints.length; i++) { const indices = tree.searchFlatTree(queryPoints[i], _tree); @@ -197,7 +197,7 @@ export function makeInitializations(distanceFn: DistanceFn) { } } return; - }; + } return { initFromRandom, initFromTree }; } @@ -210,12 +210,12 @@ export type SearchFn = ( ) => heap.Heap; export function makeInitializedNNSearch(distanceFn: DistanceFn) { - return ( + return function nnSearchFn( data: Vectors, graph: matrix.SparseMatrix, initialization: heap.Heap, queryPoints: Vectors - ) => { + ) { const { indices, indptr } = matrix.getCSR(graph); for (let i = 0; i < queryPoints.length; i++) { diff --git a/src/utils.ts b/src/utils.ts index 6f593fa..6b67b16 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -155,7 +155,7 @@ export function max2d(input: number[][]): number { } /** - * Generate nSamples many integers from 0 to pool_size such that no + * Generate nSamples many integers from 0 to poolSize such that no * integer is selected twice. The duplication constraint is achieved via * rejection sampling. */ From fe1b0f90592645c7fe59e5e66a9b8ff0c5cc0390 Mon Sep 17 00:00:00 2001 From: Andy Coenen Date: Fri, 3 May 2019 12:09:16 -0700 Subject: [PATCH 05/46] Update umap ignore files to properly publish dist folder to npm --- .gitignore | 2 ++ .npmignore | 16 ++++++++++++++++ package.json | 5 +++-- 3 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 .npmignore diff --git a/.gitignore b/.gitignore index bf56190..c369a4d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ .DS_Store node_modules/ dist/ +npm-debug.log +yarn-error.log diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..6bd1f71 --- /dev/null +++ b/.npmignore @@ -0,0 +1,16 @@ +.vscode/ +.DS_Store + +node_modules/ +webpack/ + +.prettierignore +.prettierrc +.travis.yml +CONTRIBUTING.md +jest.config.js +tsconfig.json +yarn.lock + +npm-debug.log +yarn-error.log diff --git a/package.json b/package.json index e7a98b6..61a7183 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "umap-js", - "version": "1.1.0", + "version": "1.1.1", "description": "JavaScript implementation of UMAP", "author": { "name": "Andy Coenen", @@ -13,7 +13,8 @@ "scripts": { "test": "jest", "bundle": "rm -rf lib && webpack --config ./webpack/lib.config.ts && webpack --config ./webpack/lib.min.config.ts", - "build": "rm -rf dist && tsc && yarn bundle" + "build": "rm -rf dist && tsc && yarn bundle", + "publish": "yarn build && yarn publish" }, "devDependencies": { "@types/jest": "^24.0.3", From e88d70652253bfc0ae69acfeac5e051f1708d57b Mon Sep 17 00:00:00 2001 From: Jana Beck Date: Mon, 20 May 2019 17:37:55 -0700 Subject: [PATCH 06/46] import LM default export directly --- src/umap.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/umap.ts b/src/umap.ts index 503cee9..589e30d 100644 --- a/src/umap.ts +++ b/src/umap.ts @@ -62,7 +62,7 @@ import * as matrix from './matrix'; import * as nnDescent from './nn_descent'; import * as tree from './tree'; import * as utils from './utils'; -import * as LM from 'ml-levenberg-marquardt'; +import LM from 'ml-levenberg-marquardt'; export type DistanceFn = (x: Vector, y: Vector) => number; export type EpochCallback = (epoch: number) => boolean | void; From 46bc07b2d9677b380741a6bba5e46a87a175ef50 Mon Sep 17 00:00:00 2001 From: Jana Beck Date: Mon, 20 May 2019 17:38:08 -0700 Subject: [PATCH 07/46] resulting changes to bundles --- lib/umap-js.js | 9435 ++++++++++++++++++++++---------------------- lib/umap-js.min.js | 2 +- 2 files changed, 4745 insertions(+), 4692 deletions(-) diff --git a/lib/umap-js.js b/lib/umap-js.js index 1b13db5..54dca94 100644 --- a/lib/umap-js.js +++ b/lib/umap-js.js @@ -656,8 +656,8 @@ function eliminateZeros(m) { } exports.eliminateZeros = eliminateZeros; function normalize(m, normType) { - if (normType === void 0) { normType = "l2"; } var e_1, _a; + if (normType === void 0) { normType = "l2"; } var normFn = normFns[normType]; var colsByRow = new Map(); m.forEach(function (_, row, col) { @@ -1105,13 +1105,13 @@ var matrix = __webpack_require__(3); var nnDescent = __webpack_require__(7); var tree = __webpack_require__(4); var utils = __webpack_require__(1); -var LM = __webpack_require__(8); +var ml_levenberg_marquardt_1 = __webpack_require__(8); var SMOOTH_K_TOLERANCE = 1e-5; var MIN_K_DIST_SCALE = 1e-3; var UMAP = (function () { function UMAP(params) { - if (params === void 0) { params = {}; } var _this = this; + if (params === void 0) { params = {}; } this.learningRate = 1.0; this.localConnectivity = 1.0; this.minDist = 0.1; @@ -1736,7 +1736,7 @@ function findABParams(spread, minDist) { maxIterations: 100, errorTolerance: 10e-3, }; - var parameterValues = LM(data, curve, options).parameterValues; + var parameterValues = ml_levenberg_marquardt_1.default(data, curve, options).parameterValues; var _a = __read(parameterValues, 2), a = _a[0], b = _a[1]; return { a: a, b: b }; } @@ -1869,7 +1869,7 @@ function makeNNDescent(distanceFn, random) { } exports.makeNNDescent = makeNNDescent; function makeInitializations(distanceFn) { - var initFromRandom = function (nNeighbors, data, queryPoints, _heap) { + function initFromRandom(nNeighbors, data, queryPoints, _heap) { for (var i = 0; i < queryPoints.length; i++) { var indices = utils.rejectionSample(nNeighbors, data.length); for (var j = 0; j < indices.length; j++) { @@ -1880,8 +1880,8 @@ function makeInitializations(distanceFn) { heap.heapPush(_heap, i, d, indices[j], 1); } } - }; - var initFromTree = function (_tree, data, queryPoints, _heap) { + } + function initFromTree(_tree, data, queryPoints, _heap) { for (var i = 0; i < queryPoints.length; i++) { var indices = tree.searchFlatTree(queryPoints[i], _tree); for (var j = 0; j < indices.length; j++) { @@ -1893,12 +1893,12 @@ function makeInitializations(distanceFn) { } } return; - }; + } return { initFromRandom: initFromRandom, initFromTree: initFromTree }; } exports.makeInitializations = makeInitializations; function makeInitializedNNSearch(distanceFn) { - return function (data, graph, initialization, queryPoints) { + return function nnSearchFn(data, graph, initialization, queryPoints) { var e_1, _a; var _b = matrix.getCSR(graph), indices = _b.indices, indptr = _b.indptr; for (var i = 0; i < queryPoints.length; i++) { @@ -1961,13 +1961,12 @@ exports.initializeSearch = initializeSearch; /***/ }), /* 8 */ -/***/ (function(module, exports, __webpack_require__) { +/***/ (function(module, __webpack_exports__, __webpack_require__) { "use strict"; +__webpack_require__.r(__webpack_exports__); - -var mlMatrix = __webpack_require__(9); - +// CONCATENATED MODULE: ./node_modules/ml-levenberg-marquardt/src/errorCalculation.js /** * Calculate current error * @ignore @@ -1991,6 +1990,4698 @@ function errorCalculation( return error; } +// EXTERNAL MODULE: ./node_modules/is-any-array/src/index.js +var src = __webpack_require__(0); +var src_default = /*#__PURE__*/__webpack_require__.n(src); + +// CONCATENATED MODULE: ./node_modules/ml-array-max/lib-es6/index.js + + +/** + * Computes the maximum of the given values + * @param {Array} input + * @return {number} + */ + +function lib_es6_max(input) { + if (!src_default()(input)) { + throw new TypeError('input must be an array'); + } + + if (input.length === 0) { + throw new TypeError('input must not be empty'); + } + + var max = input[0]; + + for (var i = 1; i < input.length; i++) { + if (input[i] > max) max = input[i]; + } + + return max; +} + +/* harmony default export */ var lib_es6 = (lib_es6_max); + +// CONCATENATED MODULE: ./node_modules/ml-array-min/lib-es6/index.js + + +/** + * Computes the minimum of the given values + * @param {Array} input + * @return {number} + */ + +function lib_es6_min(input) { + if (!src_default()(input)) { + throw new TypeError('input must be an array'); + } + + if (input.length === 0) { + throw new TypeError('input must not be empty'); + } + + var min = input[0]; + + for (var i = 1; i < input.length; i++) { + if (input[i] < min) min = input[i]; + } + + return min; +} + +/* harmony default export */ var ml_array_min_lib_es6 = (lib_es6_min); + +// CONCATENATED MODULE: ./node_modules/ml-array-rescale/lib-es6/index.js + + + + +function rescale(input) { + var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; + + if (!src_default()(input)) { + throw new TypeError('input must be an array'); + } else if (input.length === 0) { + throw new TypeError('input must not be empty'); + } + + var output; + + if (options.output !== undefined) { + if (!src_default()(options.output)) { + throw new TypeError('output option must be an array if specified'); + } + + output = options.output; + } else { + output = new Array(input.length); + } + + var currentMin = ml_array_min_lib_es6(input); + var currentMax = lib_es6(input); + + if (currentMin === currentMax) { + throw new RangeError('minimum and maximum input values are equal. Cannot rescale a constant array'); + } + + var _options$min = options.min, + minValue = _options$min === void 0 ? options.autoMinMax ? currentMin : 0 : _options$min, + _options$max = options.max, + maxValue = _options$max === void 0 ? options.autoMinMax ? currentMax : 1 : _options$max; + + if (minValue >= maxValue) { + throw new RangeError('min option must be smaller than max option'); + } + + var factor = (maxValue - minValue) / (currentMax - currentMin); + + for (var i = 0; i < input.length; i++) { + output[i] = (input[i] - currentMin) * factor + minValue; + } + + return output; +} + +/* harmony default export */ var ml_array_rescale_lib_es6 = (rescale); + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/dc/lu.js + + +/** + * @class LuDecomposition + * @link https://github.com/lutzroeder/Mapack/blob/master/Source/LuDecomposition.cs + * @param {Matrix} matrix + */ +class lu_LuDecomposition { + constructor(matrix) { + matrix = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(matrix); + + var lu = matrix.clone(); + var rows = lu.rows; + var columns = lu.columns; + var pivotVector = new Array(rows); + var pivotSign = 1; + var i, j, k, p, s, t, v; + var LUcolj, kmax; + + for (i = 0; i < rows; i++) { + pivotVector[i] = i; + } + + LUcolj = new Array(rows); + + for (j = 0; j < columns; j++) { + for (i = 0; i < rows; i++) { + LUcolj[i] = lu.get(i, j); + } + + for (i = 0; i < rows; i++) { + kmax = Math.min(i, j); + s = 0; + for (k = 0; k < kmax; k++) { + s += lu.get(i, k) * LUcolj[k]; + } + LUcolj[i] -= s; + lu.set(i, j, LUcolj[i]); + } + + p = j; + for (i = j + 1; i < rows; i++) { + if (Math.abs(LUcolj[i]) > Math.abs(LUcolj[p])) { + p = i; + } + } + + if (p !== j) { + for (k = 0; k < columns; k++) { + t = lu.get(p, k); + lu.set(p, k, lu.get(j, k)); + lu.set(j, k, t); + } + + v = pivotVector[p]; + pivotVector[p] = pivotVector[j]; + pivotVector[j] = v; + + pivotSign = -pivotSign; + } + + if (j < rows && lu.get(j, j) !== 0) { + for (i = j + 1; i < rows; i++) { + lu.set(i, j, lu.get(i, j) / lu.get(j, j)); + } + } + } + + this.LU = lu; + this.pivotVector = pivotVector; + this.pivotSign = pivotSign; + } + + /** + * + * @return {boolean} + */ + isSingular() { + var data = this.LU; + var col = data.columns; + for (var j = 0; j < col; j++) { + if (data[j][j] === 0) { + return true; + } + } + return false; + } + + /** + * + * @param {Matrix} value + * @return {Matrix} + */ + solve(value) { + value = matrix_Matrix.checkMatrix(value); + + var lu = this.LU; + var rows = lu.rows; + + if (rows !== value.rows) { + throw new Error('Invalid matrix dimensions'); + } + if (this.isSingular()) { + throw new Error('LU matrix is singular'); + } + + var count = value.columns; + var X = value.subMatrixRow(this.pivotVector, 0, count - 1); + var columns = lu.columns; + var i, j, k; + + for (k = 0; k < columns; k++) { + for (i = k + 1; i < columns; i++) { + for (j = 0; j < count; j++) { + X[i][j] -= X[k][j] * lu[i][k]; + } + } + } + for (k = columns - 1; k >= 0; k--) { + for (j = 0; j < count; j++) { + X[k][j] /= lu[k][k]; + } + for (i = 0; i < k; i++) { + for (j = 0; j < count; j++) { + X[i][j] -= X[k][j] * lu[i][k]; + } + } + } + return X; + } + + /** + * + * @return {number} + */ + get determinant() { + var data = this.LU; + if (!data.isSquare()) { + throw new Error('Matrix must be square'); + } + var determinant = this.pivotSign; + var col = data.columns; + for (var j = 0; j < col; j++) { + determinant *= data[j][j]; + } + return determinant; + } + + /** + * + * @return {Matrix} + */ + get lowerTriangularMatrix() { + var data = this.LU; + var rows = data.rows; + var columns = data.columns; + var X = new matrix_Matrix(rows, columns); + for (var i = 0; i < rows; i++) { + for (var j = 0; j < columns; j++) { + if (i > j) { + X[i][j] = data[i][j]; + } else if (i === j) { + X[i][j] = 1; + } else { + X[i][j] = 0; + } + } + } + return X; + } + + /** + * + * @return {Matrix} + */ + get upperTriangularMatrix() { + var data = this.LU; + var rows = data.rows; + var columns = data.columns; + var X = new matrix_Matrix(rows, columns); + for (var i = 0; i < rows; i++) { + for (var j = 0; j < columns; j++) { + if (i <= j) { + X[i][j] = data[i][j]; + } else { + X[i][j] = 0; + } + } + } + return X; + } + + /** + * + * @return {Array} + */ + get pivotPermutationVector() { + return this.pivotVector.slice(); + } +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/dc/util.js +function hypotenuse(a, b) { + var r = 0; + if (Math.abs(a) > Math.abs(b)) { + r = b / a; + return Math.abs(a) * Math.sqrt(1 + r * r); + } + if (b !== 0) { + r = a / b; + return Math.abs(b) * Math.sqrt(1 + r * r); + } + return 0; +} + +function getFilled2DArray(rows, columns, value) { + var array = new Array(rows); + for (var i = 0; i < rows; i++) { + array[i] = new Array(columns); + for (var j = 0; j < columns; j++) { + array[i][j] = value; + } + } + return array; +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/dc/svd.js + + + + +/** + * @class SingularValueDecomposition + * @see https://github.com/accord-net/framework/blob/development/Sources/Accord.Math/Decompositions/SingularValueDecomposition.cs + * @param {Matrix} value + * @param {object} [options] + * @param {boolean} [options.computeLeftSingularVectors=true] + * @param {boolean} [options.computeRightSingularVectors=true] + * @param {boolean} [options.autoTranspose=false] + */ +class svd_SingularValueDecomposition { + constructor(value, options = {}) { + value = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(value); + + var m = value.rows; + var n = value.columns; + + const { + computeLeftSingularVectors = true, + computeRightSingularVectors = true, + autoTranspose = false + } = options; + + var wantu = Boolean(computeLeftSingularVectors); + var wantv = Boolean(computeRightSingularVectors); + + var swapped = false; + var a; + if (m < n) { + if (!autoTranspose) { + a = value.clone(); + // eslint-disable-next-line no-console + console.warn( + 'Computing SVD on a matrix with more columns than rows. Consider enabling autoTranspose' + ); + } else { + a = value.transpose(); + m = a.rows; + n = a.columns; + swapped = true; + var aux = wantu; + wantu = wantv; + wantv = aux; + } + } else { + a = value.clone(); + } + + var nu = Math.min(m, n); + var ni = Math.min(m + 1, n); + var s = new Array(ni); + var U = getFilled2DArray(m, nu, 0); + var V = getFilled2DArray(n, n, 0); + + var e = new Array(n); + var work = new Array(m); + + var si = new Array(ni); + for (let i = 0; i < ni; i++) si[i] = i; + + var nct = Math.min(m - 1, n); + var nrt = Math.max(0, Math.min(n - 2, m)); + var mrc = Math.max(nct, nrt); + + for (let k = 0; k < mrc; k++) { + if (k < nct) { + s[k] = 0; + for (let i = k; i < m; i++) { + s[k] = hypotenuse(s[k], a[i][k]); + } + if (s[k] !== 0) { + if (a[k][k] < 0) { + s[k] = -s[k]; + } + for (let i = k; i < m; i++) { + a[i][k] /= s[k]; + } + a[k][k] += 1; + } + s[k] = -s[k]; + } + + for (let j = k + 1; j < n; j++) { + if (k < nct && s[k] !== 0) { + let t = 0; + for (let i = k; i < m; i++) { + t += a[i][k] * a[i][j]; + } + t = -t / a[k][k]; + for (let i = k; i < m; i++) { + a[i][j] += t * a[i][k]; + } + } + e[j] = a[k][j]; + } + + if (wantu && k < nct) { + for (let i = k; i < m; i++) { + U[i][k] = a[i][k]; + } + } + + if (k < nrt) { + e[k] = 0; + for (let i = k + 1; i < n; i++) { + e[k] = hypotenuse(e[k], e[i]); + } + if (e[k] !== 0) { + if (e[k + 1] < 0) { + e[k] = 0 - e[k]; + } + for (let i = k + 1; i < n; i++) { + e[i] /= e[k]; + } + e[k + 1] += 1; + } + e[k] = -e[k]; + if (k + 1 < m && e[k] !== 0) { + for (let i = k + 1; i < m; i++) { + work[i] = 0; + } + for (let i = k + 1; i < m; i++) { + for (let j = k + 1; j < n; j++) { + work[i] += e[j] * a[i][j]; + } + } + for (let j = k + 1; j < n; j++) { + let t = -e[j] / e[k + 1]; + for (let i = k + 1; i < m; i++) { + a[i][j] += t * work[i]; + } + } + } + if (wantv) { + for (let i = k + 1; i < n; i++) { + V[i][k] = e[i]; + } + } + } + } + + let p = Math.min(n, m + 1); + if (nct < n) { + s[nct] = a[nct][nct]; + } + if (m < p) { + s[p - 1] = 0; + } + if (nrt + 1 < p) { + e[nrt] = a[nrt][p - 1]; + } + e[p - 1] = 0; + + if (wantu) { + for (let j = nct; j < nu; j++) { + for (let i = 0; i < m; i++) { + U[i][j] = 0; + } + U[j][j] = 1; + } + for (let k = nct - 1; k >= 0; k--) { + if (s[k] !== 0) { + for (let j = k + 1; j < nu; j++) { + let t = 0; + for (let i = k; i < m; i++) { + t += U[i][k] * U[i][j]; + } + t = -t / U[k][k]; + for (let i = k; i < m; i++) { + U[i][j] += t * U[i][k]; + } + } + for (let i = k; i < m; i++) { + U[i][k] = -U[i][k]; + } + U[k][k] = 1 + U[k][k]; + for (let i = 0; i < k - 1; i++) { + U[i][k] = 0; + } + } else { + for (let i = 0; i < m; i++) { + U[i][k] = 0; + } + U[k][k] = 1; + } + } + } + + if (wantv) { + for (let k = n - 1; k >= 0; k--) { + if (k < nrt && e[k] !== 0) { + for (let j = k + 1; j < n; j++) { + let t = 0; + for (let i = k + 1; i < n; i++) { + t += V[i][k] * V[i][j]; + } + t = -t / V[k + 1][k]; + for (let i = k + 1; i < n; i++) { + V[i][j] += t * V[i][k]; + } + } + } + for (let i = 0; i < n; i++) { + V[i][k] = 0; + } + V[k][k] = 1; + } + } + + var pp = p - 1; + var iter = 0; + var eps = Number.EPSILON; + while (p > 0) { + let k, kase; + for (k = p - 2; k >= -1; k--) { + if (k === -1) { + break; + } + const alpha = + Number.MIN_VALUE + eps * Math.abs(s[k] + Math.abs(s[k + 1])); + if (Math.abs(e[k]) <= alpha || Number.isNaN(e[k])) { + e[k] = 0; + break; + } + } + if (k === p - 2) { + kase = 4; + } else { + let ks; + for (ks = p - 1; ks >= k; ks--) { + if (ks === k) { + break; + } + let t = + (ks !== p ? Math.abs(e[ks]) : 0) + + (ks !== k + 1 ? Math.abs(e[ks - 1]) : 0); + if (Math.abs(s[ks]) <= eps * t) { + s[ks] = 0; + break; + } + } + if (ks === k) { + kase = 3; + } else if (ks === p - 1) { + kase = 1; + } else { + kase = 2; + k = ks; + } + } + + k++; + + switch (kase) { + case 1: { + let f = e[p - 2]; + e[p - 2] = 0; + for (let j = p - 2; j >= k; j--) { + let t = hypotenuse(s[j], f); + let cs = s[j] / t; + let sn = f / t; + s[j] = t; + if (j !== k) { + f = -sn * e[j - 1]; + e[j - 1] = cs * e[j - 1]; + } + if (wantv) { + for (let i = 0; i < n; i++) { + t = cs * V[i][j] + sn * V[i][p - 1]; + V[i][p - 1] = -sn * V[i][j] + cs * V[i][p - 1]; + V[i][j] = t; + } + } + } + break; + } + case 2: { + let f = e[k - 1]; + e[k - 1] = 0; + for (let j = k; j < p; j++) { + let t = hypotenuse(s[j], f); + let cs = s[j] / t; + let sn = f / t; + s[j] = t; + f = -sn * e[j]; + e[j] = cs * e[j]; + if (wantu) { + for (let i = 0; i < m; i++) { + t = cs * U[i][j] + sn * U[i][k - 1]; + U[i][k - 1] = -sn * U[i][j] + cs * U[i][k - 1]; + U[i][j] = t; + } + } + } + break; + } + case 3: { + const scale = Math.max( + Math.abs(s[p - 1]), + Math.abs(s[p - 2]), + Math.abs(e[p - 2]), + Math.abs(s[k]), + Math.abs(e[k]) + ); + const sp = s[p - 1] / scale; + const spm1 = s[p - 2] / scale; + const epm1 = e[p - 2] / scale; + const sk = s[k] / scale; + const ek = e[k] / scale; + const b = ((spm1 + sp) * (spm1 - sp) + epm1 * epm1) / 2; + const c = sp * epm1 * (sp * epm1); + let shift = 0; + if (b !== 0 || c !== 0) { + if (b < 0) { + shift = 0 - Math.sqrt(b * b + c); + } else { + shift = Math.sqrt(b * b + c); + } + shift = c / (b + shift); + } + let f = (sk + sp) * (sk - sp) + shift; + let g = sk * ek; + for (let j = k; j < p - 1; j++) { + let t = hypotenuse(f, g); + if (t === 0) t = Number.MIN_VALUE; + let cs = f / t; + let sn = g / t; + if (j !== k) { + e[j - 1] = t; + } + f = cs * s[j] + sn * e[j]; + e[j] = cs * e[j] - sn * s[j]; + g = sn * s[j + 1]; + s[j + 1] = cs * s[j + 1]; + if (wantv) { + for (let i = 0; i < n; i++) { + t = cs * V[i][j] + sn * V[i][j + 1]; + V[i][j + 1] = -sn * V[i][j] + cs * V[i][j + 1]; + V[i][j] = t; + } + } + t = hypotenuse(f, g); + if (t === 0) t = Number.MIN_VALUE; + cs = f / t; + sn = g / t; + s[j] = t; + f = cs * e[j] + sn * s[j + 1]; + s[j + 1] = -sn * e[j] + cs * s[j + 1]; + g = sn * e[j + 1]; + e[j + 1] = cs * e[j + 1]; + if (wantu && j < m - 1) { + for (let i = 0; i < m; i++) { + t = cs * U[i][j] + sn * U[i][j + 1]; + U[i][j + 1] = -sn * U[i][j] + cs * U[i][j + 1]; + U[i][j] = t; + } + } + } + e[p - 2] = f; + iter = iter + 1; + break; + } + case 4: { + if (s[k] <= 0) { + s[k] = s[k] < 0 ? -s[k] : 0; + if (wantv) { + for (let i = 0; i <= pp; i++) { + V[i][k] = -V[i][k]; + } + } + } + while (k < pp) { + if (s[k] >= s[k + 1]) { + break; + } + let t = s[k]; + s[k] = s[k + 1]; + s[k + 1] = t; + if (wantv && k < n - 1) { + for (let i = 0; i < n; i++) { + t = V[i][k + 1]; + V[i][k + 1] = V[i][k]; + V[i][k] = t; + } + } + if (wantu && k < m - 1) { + for (let i = 0; i < m; i++) { + t = U[i][k + 1]; + U[i][k + 1] = U[i][k]; + U[i][k] = t; + } + } + k++; + } + iter = 0; + p--; + break; + } + // no default + } + } + + if (swapped) { + var tmp = V; + V = U; + U = tmp; + } + + this.m = m; + this.n = n; + this.s = s; + this.U = U; + this.V = V; + } + + /** + * Solve a problem of least square (Ax=b) by using the SVD. Useful when A is singular. When A is not singular, it would be better to use qr.solve(value). + * Example : We search to approximate x, with A matrix shape m*n, x vector size n, b vector size m (m > n). We will use : + * var svd = SingularValueDecomposition(A); + * var x = svd.solve(b); + * @param {Matrix} value - Matrix 1D which is the vector b (in the equation Ax = b) + * @return {Matrix} - The vector x + */ + solve(value) { + var Y = value; + var e = this.threshold; + var scols = this.s.length; + var Ls = matrix_Matrix.zeros(scols, scols); + + for (let i = 0; i < scols; i++) { + if (Math.abs(this.s[i]) <= e) { + Ls[i][i] = 0; + } else { + Ls[i][i] = 1 / this.s[i]; + } + } + + var U = this.U; + var V = this.rightSingularVectors; + + var VL = V.mmul(Ls); + var vrows = V.rows; + var urows = U.length; + var VLU = matrix_Matrix.zeros(vrows, urows); + + for (let i = 0; i < vrows; i++) { + for (let j = 0; j < urows; j++) { + let sum = 0; + for (let k = 0; k < scols; k++) { + sum += VL[i][k] * U[j][k]; + } + VLU[i][j] = sum; + } + } + + return VLU.mmul(Y); + } + + /** + * + * @param {Array} value + * @return {Matrix} + */ + solveForDiagonal(value) { + return this.solve(matrix_Matrix.diag(value)); + } + + /** + * Get the inverse of the matrix. We compute the inverse of a matrix using SVD when this matrix is singular or ill-conditioned. Example : + * var svd = SingularValueDecomposition(A); + * var inverseA = svd.inverse(); + * @return {Matrix} - The approximation of the inverse of the matrix + */ + inverse() { + var V = this.V; + var e = this.threshold; + var vrows = V.length; + var vcols = V[0].length; + var X = new matrix_Matrix(vrows, this.s.length); + + for (let i = 0; i < vrows; i++) { + for (let j = 0; j < vcols; j++) { + if (Math.abs(this.s[j]) > e) { + X[i][j] = V[i][j] / this.s[j]; + } else { + X[i][j] = 0; + } + } + } + + var U = this.U; + + var urows = U.length; + var ucols = U[0].length; + var Y = new matrix_Matrix(vrows, urows); + + for (let i = 0; i < vrows; i++) { + for (let j = 0; j < urows; j++) { + let sum = 0; + for (let k = 0; k < ucols; k++) { + sum += X[i][k] * U[j][k]; + } + Y[i][j] = sum; + } + } + + return Y; + } + + /** + * + * @return {number} + */ + get condition() { + return this.s[0] / this.s[Math.min(this.m, this.n) - 1]; + } + + /** + * + * @return {number} + */ + get norm2() { + return this.s[0]; + } + + /** + * + * @return {number} + */ + get rank() { + var tol = Math.max(this.m, this.n) * this.s[0] * Number.EPSILON; + var r = 0; + var s = this.s; + for (var i = 0, ii = s.length; i < ii; i++) { + if (s[i] > tol) { + r++; + } + } + return r; + } + + /** + * + * @return {Array} + */ + get diagonal() { + return this.s; + } + + /** + * + * @return {number} + */ + get threshold() { + return Number.EPSILON / 2 * Math.max(this.m, this.n) * this.s[0]; + } + + /** + * + * @return {Matrix} + */ + get leftSingularVectors() { + if (!matrix_Matrix.isMatrix(this.U)) { + this.U = new matrix_Matrix(this.U); + } + return this.U; + } + + /** + * + * @return {Matrix} + */ + get rightSingularVectors() { + if (!matrix_Matrix.isMatrix(this.V)) { + this.V = new matrix_Matrix(this.V); + } + return this.V; + } + + /** + * + * @return {Matrix} + */ + get diagonalMatrix() { + return matrix_Matrix.diag(this.s); + } +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/util.js + + +/** + * @private + * Check that a row index is not out of bounds + * @param {Matrix} matrix + * @param {number} index + * @param {boolean} [outer] + */ +function checkRowIndex(matrix, index, outer) { + var max = outer ? matrix.rows : matrix.rows - 1; + if (index < 0 || index > max) { + throw new RangeError('Row index out of range'); + } +} + +/** + * @private + * Check that a column index is not out of bounds + * @param {Matrix} matrix + * @param {number} index + * @param {boolean} [outer] + */ +function checkColumnIndex(matrix, index, outer) { + var max = outer ? matrix.columns : matrix.columns - 1; + if (index < 0 || index > max) { + throw new RangeError('Column index out of range'); + } +} + +/** + * @private + * Check that the provided vector is an array with the right length + * @param {Matrix} matrix + * @param {Array|Matrix} vector + * @return {Array} + * @throws {RangeError} + */ +function checkRowVector(matrix, vector) { + if (vector.to1DArray) { + vector = vector.to1DArray(); + } + if (vector.length !== matrix.columns) { + throw new RangeError( + 'vector size must be the same as the number of columns' + ); + } + return vector; +} + +/** + * @private + * Check that the provided vector is an array with the right length + * @param {Matrix} matrix + * @param {Array|Matrix} vector + * @return {Array} + * @throws {RangeError} + */ +function checkColumnVector(matrix, vector) { + if (vector.to1DArray) { + vector = vector.to1DArray(); + } + if (vector.length !== matrix.rows) { + throw new RangeError('vector size must be the same as the number of rows'); + } + return vector; +} + +function checkIndices(matrix, rowIndices, columnIndices) { + return { + row: checkRowIndices(matrix, rowIndices), + column: checkColumnIndices(matrix, columnIndices) + }; +} + +function checkRowIndices(matrix, rowIndices) { + if (typeof rowIndices !== 'object') { + throw new TypeError('unexpected type for row indices'); + } + + var rowOut = rowIndices.some((r) => { + return r < 0 || r >= matrix.rows; + }); + + if (rowOut) { + throw new RangeError('row indices are out of range'); + } + + if (!Array.isArray(rowIndices)) rowIndices = Array.from(rowIndices); + + return rowIndices; +} + +function checkColumnIndices(matrix, columnIndices) { + if (typeof columnIndices !== 'object') { + throw new TypeError('unexpected type for column indices'); + } + + var columnOut = columnIndices.some((c) => { + return c < 0 || c >= matrix.columns; + }); + + if (columnOut) { + throw new RangeError('column indices are out of range'); + } + if (!Array.isArray(columnIndices)) columnIndices = Array.from(columnIndices); + + return columnIndices; +} + +function checkRange(matrix, startRow, endRow, startColumn, endColumn) { + if (arguments.length !== 5) { + throw new RangeError('expected 4 arguments'); + } + checkNumber('startRow', startRow); + checkNumber('endRow', endRow); + checkNumber('startColumn', startColumn); + checkNumber('endColumn', endColumn); + if ( + startRow > endRow || + startColumn > endColumn || + startRow < 0 || + startRow >= matrix.rows || + endRow < 0 || + endRow >= matrix.rows || + startColumn < 0 || + startColumn >= matrix.columns || + endColumn < 0 || + endColumn >= matrix.columns + ) { + throw new RangeError('Submatrix indices are out of range'); + } +} + +function getRange(from, to) { + var arr = new Array(to - from + 1); + for (var i = 0; i < arr.length; i++) { + arr[i] = from + i; + } + return arr; +} + +function sumByRow(matrix) { + var sum = matrix_Matrix.zeros(matrix.rows, 1); + for (var i = 0; i < matrix.rows; ++i) { + for (var j = 0; j < matrix.columns; ++j) { + sum.set(i, 0, sum.get(i, 0) + matrix.get(i, j)); + } + } + return sum; +} + +function sumByColumn(matrix) { + var sum = matrix_Matrix.zeros(1, matrix.columns); + for (var i = 0; i < matrix.rows; ++i) { + for (var j = 0; j < matrix.columns; ++j) { + sum.set(0, j, sum.get(0, j) + matrix.get(i, j)); + } + } + return sum; +} + +function sumAll(matrix) { + var v = 0; + for (var i = 0; i < matrix.rows; i++) { + for (var j = 0; j < matrix.columns; j++) { + v += matrix.get(i, j); + } + } + return v; +} + +function checkNumber(name, value) { + if (typeof value !== 'number') { + throw new TypeError(`${name} must be a number`); + } +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/base.js + + + +class base_BaseView extends AbstractMatrix() { + constructor(matrix, rows, columns) { + super(); + this.matrix = matrix; + this.rows = rows; + this.columns = columns; + } + + static get [Symbol.species]() { + return matrix_Matrix; + } +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/transpose.js + + +class transpose_MatrixTransposeView extends base_BaseView { + constructor(matrix) { + super(matrix, matrix.columns, matrix.rows); + } + + set(rowIndex, columnIndex, value) { + this.matrix.set(columnIndex, rowIndex, value); + return this; + } + + get(rowIndex, columnIndex) { + return this.matrix.get(columnIndex, rowIndex); + } +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/row.js + + +class row_MatrixRowView extends base_BaseView { + constructor(matrix, row) { + super(matrix, 1, matrix.columns); + this.row = row; + } + + set(rowIndex, columnIndex, value) { + this.matrix.set(this.row, columnIndex, value); + return this; + } + + get(rowIndex, columnIndex) { + return this.matrix.get(this.row, columnIndex); + } +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/sub.js + + + + +class sub_MatrixSubView extends base_BaseView { + constructor(matrix, startRow, endRow, startColumn, endColumn) { + checkRange(matrix, startRow, endRow, startColumn, endColumn); + super(matrix, endRow - startRow + 1, endColumn - startColumn + 1); + this.startRow = startRow; + this.startColumn = startColumn; + } + + set(rowIndex, columnIndex, value) { + this.matrix.set( + this.startRow + rowIndex, + this.startColumn + columnIndex, + value + ); + return this; + } + + get(rowIndex, columnIndex) { + return this.matrix.get( + this.startRow + rowIndex, + this.startColumn + columnIndex + ); + } +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/selection.js + + + + +class selection_MatrixSelectionView extends base_BaseView { + constructor(matrix, rowIndices, columnIndices) { + var indices = checkIndices(matrix, rowIndices, columnIndices); + super(matrix, indices.row.length, indices.column.length); + this.rowIndices = indices.row; + this.columnIndices = indices.column; + } + + set(rowIndex, columnIndex, value) { + this.matrix.set( + this.rowIndices[rowIndex], + this.columnIndices[columnIndex], + value + ); + return this; + } + + get(rowIndex, columnIndex) { + return this.matrix.get( + this.rowIndices[rowIndex], + this.columnIndices[columnIndex] + ); + } +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/rowSelection.js + + + + +class rowSelection_MatrixRowSelectionView extends base_BaseView { + constructor(matrix, rowIndices) { + rowIndices = checkRowIndices(matrix, rowIndices); + super(matrix, rowIndices.length, matrix.columns); + this.rowIndices = rowIndices; + } + + set(rowIndex, columnIndex, value) { + this.matrix.set(this.rowIndices[rowIndex], columnIndex, value); + return this; + } + + get(rowIndex, columnIndex) { + return this.matrix.get(this.rowIndices[rowIndex], columnIndex); + } +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/columnSelection.js + + + + +class columnSelection_MatrixColumnSelectionView extends base_BaseView { + constructor(matrix, columnIndices) { + columnIndices = checkColumnIndices(matrix, columnIndices); + super(matrix, matrix.rows, columnIndices.length); + this.columnIndices = columnIndices; + } + + set(rowIndex, columnIndex, value) { + this.matrix.set(rowIndex, this.columnIndices[columnIndex], value); + return this; + } + + get(rowIndex, columnIndex) { + return this.matrix.get(rowIndex, this.columnIndices[columnIndex]); + } +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/column.js + + +class column_MatrixColumnView extends base_BaseView { + constructor(matrix, column) { + super(matrix, matrix.rows, 1); + this.column = column; + } + + set(rowIndex, columnIndex, value) { + this.matrix.set(rowIndex, this.column, value); + return this; + } + + get(rowIndex) { + return this.matrix.get(rowIndex, this.column); + } +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/flipRow.js + + +class flipRow_MatrixFlipRowView extends base_BaseView { + constructor(matrix) { + super(matrix, matrix.rows, matrix.columns); + } + + set(rowIndex, columnIndex, value) { + this.matrix.set(this.rows - rowIndex - 1, columnIndex, value); + return this; + } + + get(rowIndex, columnIndex) { + return this.matrix.get(this.rows - rowIndex - 1, columnIndex); + } +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/flipColumn.js + + +class flipColumn_MatrixFlipColumnView extends base_BaseView { + constructor(matrix) { + super(matrix, matrix.rows, matrix.columns); + } + + set(rowIndex, columnIndex, value) { + this.matrix.set(rowIndex, this.columns - columnIndex - 1, value); + return this; + } + + get(rowIndex, columnIndex) { + return this.matrix.get(rowIndex, this.columns - columnIndex - 1); + } +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/abstractMatrix.js + + + + + + + + + + + + + + + +function AbstractMatrix(superCtor) { + if (superCtor === undefined) superCtor = Object; + + /** + * Real matrix + * @class Matrix + * @param {number|Array|Matrix} nRows - Number of rows of the new matrix, + * 2D array containing the data or Matrix instance to clone + * @param {number} [nColumns] - Number of columns of the new matrix + */ + class Matrix extends superCtor { + static get [Symbol.species]() { + return this; + } + + /** + * Constructs a Matrix with the chosen dimensions from a 1D array + * @param {number} newRows - Number of rows + * @param {number} newColumns - Number of columns + * @param {Array} newData - A 1D array containing data for the matrix + * @return {Matrix} - The new matrix + */ + static from1DArray(newRows, newColumns, newData) { + var length = newRows * newColumns; + if (length !== newData.length) { + throw new RangeError('Data length does not match given dimensions'); + } + var newMatrix = new this(newRows, newColumns); + for (var row = 0; row < newRows; row++) { + for (var column = 0; column < newColumns; column++) { + newMatrix.set(row, column, newData[row * newColumns + column]); + } + } + return newMatrix; + } + + /** + * Creates a row vector, a matrix with only one row. + * @param {Array} newData - A 1D array containing data for the vector + * @return {Matrix} - The new matrix + */ + static rowVector(newData) { + var vector = new this(1, newData.length); + for (var i = 0; i < newData.length; i++) { + vector.set(0, i, newData[i]); + } + return vector; + } + + /** + * Creates a column vector, a matrix with only one column. + * @param {Array} newData - A 1D array containing data for the vector + * @return {Matrix} - The new matrix + */ + static columnVector(newData) { + var vector = new this(newData.length, 1); + for (var i = 0; i < newData.length; i++) { + vector.set(i, 0, newData[i]); + } + return vector; + } + + /** + * Creates an empty matrix with the given dimensions. Values will be undefined. Same as using new Matrix(rows, columns). + * @param {number} rows - Number of rows + * @param {number} columns - Number of columns + * @return {Matrix} - The new matrix + */ + static empty(rows, columns) { + return new this(rows, columns); + } + + /** + * Creates a matrix with the given dimensions. Values will be set to zero. + * @param {number} rows - Number of rows + * @param {number} columns - Number of columns + * @return {Matrix} - The new matrix + */ + static zeros(rows, columns) { + return this.empty(rows, columns).fill(0); + } + + /** + * Creates a matrix with the given dimensions. Values will be set to one. + * @param {number} rows - Number of rows + * @param {number} columns - Number of columns + * @return {Matrix} - The new matrix + */ + static ones(rows, columns) { + return this.empty(rows, columns).fill(1); + } + + /** + * Creates a matrix with the given dimensions. Values will be randomly set. + * @param {number} rows - Number of rows + * @param {number} columns - Number of columns + * @param {function} [rng=Math.random] - Random number generator + * @return {Matrix} The new matrix + */ + static rand(rows, columns, rng) { + if (rng === undefined) rng = Math.random; + var matrix = this.empty(rows, columns); + for (var i = 0; i < rows; i++) { + for (var j = 0; j < columns; j++) { + matrix.set(i, j, rng()); + } + } + return matrix; + } + + /** + * Creates a matrix with the given dimensions. Values will be random integers. + * @param {number} rows - Number of rows + * @param {number} columns - Number of columns + * @param {number} [maxValue=1000] - Maximum value + * @param {function} [rng=Math.random] - Random number generator + * @return {Matrix} The new matrix + */ + static randInt(rows, columns, maxValue, rng) { + if (maxValue === undefined) maxValue = 1000; + if (rng === undefined) rng = Math.random; + var matrix = this.empty(rows, columns); + for (var i = 0; i < rows; i++) { + for (var j = 0; j < columns; j++) { + var value = Math.floor(rng() * maxValue); + matrix.set(i, j, value); + } + } + return matrix; + } + + /** + * Creates an identity matrix with the given dimension. Values of the diagonal will be 1 and others will be 0. + * @param {number} rows - Number of rows + * @param {number} [columns=rows] - Number of columns + * @param {number} [value=1] - Value to fill the diagonal with + * @return {Matrix} - The new identity matrix + */ + static eye(rows, columns, value) { + if (columns === undefined) columns = rows; + if (value === undefined) value = 1; + var min = Math.min(rows, columns); + var matrix = this.zeros(rows, columns); + for (var i = 0; i < min; i++) { + matrix.set(i, i, value); + } + return matrix; + } + + /** + * Creates a diagonal matrix based on the given array. + * @param {Array} data - Array containing the data for the diagonal + * @param {number} [rows] - Number of rows (Default: data.length) + * @param {number} [columns] - Number of columns (Default: rows) + * @return {Matrix} - The new diagonal matrix + */ + static diag(data, rows, columns) { + var l = data.length; + if (rows === undefined) rows = l; + if (columns === undefined) columns = rows; + var min = Math.min(l, rows, columns); + var matrix = this.zeros(rows, columns); + for (var i = 0; i < min; i++) { + matrix.set(i, i, data[i]); + } + return matrix; + } + + /** + * Returns a matrix whose elements are the minimum between matrix1 and matrix2 + * @param {Matrix} matrix1 + * @param {Matrix} matrix2 + * @return {Matrix} + */ + static min(matrix1, matrix2) { + matrix1 = this.checkMatrix(matrix1); + matrix2 = this.checkMatrix(matrix2); + var rows = matrix1.rows; + var columns = matrix1.columns; + var result = new this(rows, columns); + for (var i = 0; i < rows; i++) { + for (var j = 0; j < columns; j++) { + result.set(i, j, Math.min(matrix1.get(i, j), matrix2.get(i, j))); + } + } + return result; + } + + /** + * Returns a matrix whose elements are the maximum between matrix1 and matrix2 + * @param {Matrix} matrix1 + * @param {Matrix} matrix2 + * @return {Matrix} + */ + static max(matrix1, matrix2) { + matrix1 = this.checkMatrix(matrix1); + matrix2 = this.checkMatrix(matrix2); + var rows = matrix1.rows; + var columns = matrix1.columns; + var result = new this(rows, columns); + for (var i = 0; i < rows; i++) { + for (var j = 0; j < columns; j++) { + result.set(i, j, Math.max(matrix1.get(i, j), matrix2.get(i, j))); + } + } + return result; + } + + /** + * Check that the provided value is a Matrix and tries to instantiate one if not + * @param {*} value - The value to check + * @return {Matrix} + */ + static checkMatrix(value) { + return Matrix.isMatrix(value) ? value : new this(value); + } + + /** + * Returns true if the argument is a Matrix, false otherwise + * @param {*} value - The value to check + * @return {boolean} + */ + static isMatrix(value) { + return (value != null) && (value.klass === 'Matrix'); + } + + /** + * @prop {number} size - The number of elements in the matrix. + */ + get size() { + return this.rows * this.columns; + } + + /** + * Applies a callback for each element of the matrix. The function is called in the matrix (this) context. + * @param {function} callback - Function that will be called with two parameters : i (row) and j (column) + * @return {Matrix} this + */ + apply(callback) { + if (typeof callback !== 'function') { + throw new TypeError('callback must be a function'); + } + var ii = this.rows; + var jj = this.columns; + for (var i = 0; i < ii; i++) { + for (var j = 0; j < jj; j++) { + callback.call(this, i, j); + } + } + return this; + } + + /** + * Returns a new 1D array filled row by row with the matrix values + * @return {Array} + */ + to1DArray() { + var array = new Array(this.size); + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + array[i * this.columns + j] = this.get(i, j); + } + } + return array; + } + + /** + * Returns a 2D array containing a copy of the data + * @return {Array} + */ + to2DArray() { + var copy = new Array(this.rows); + for (var i = 0; i < this.rows; i++) { + copy[i] = new Array(this.columns); + for (var j = 0; j < this.columns; j++) { + copy[i][j] = this.get(i, j); + } + } + return copy; + } + + /** + * @return {boolean} true if the matrix has one row + */ + isRowVector() { + return this.rows === 1; + } + + /** + * @return {boolean} true if the matrix has one column + */ + isColumnVector() { + return this.columns === 1; + } + + /** + * @return {boolean} true if the matrix has one row or one column + */ + isVector() { + return (this.rows === 1) || (this.columns === 1); + } + + /** + * @return {boolean} true if the matrix has the same number of rows and columns + */ + isSquare() { + return this.rows === this.columns; + } + + /** + * @return {boolean} true if the matrix is square and has the same values on both sides of the diagonal + */ + isSymmetric() { + if (this.isSquare()) { + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j <= i; j++) { + if (this.get(i, j) !== this.get(j, i)) { + return false; + } + } + } + return true; + } + return false; + } + + /** + * @return true if the matrix is in echelon form + */ + isEchelonForm() { + let i = 0; + let j = 0; + let previousColumn = -1; + let isEchelonForm = true; + let checked = false; + while ((i < this.rows) && (isEchelonForm)) { + j = 0; + checked = false; + while ((j < this.columns) && (checked === false)) { + if (this.get(i, j) === 0) { + j++; + } else if ((this.get(i, j) === 1) && (j > previousColumn)) { + checked = true; + previousColumn = j; + } else { + isEchelonForm = false; + checked = true; + } + } + i++; + } + return isEchelonForm; + } + + /** + * @return true if the matrix is in reduced echelon form + */ + isReducedEchelonForm() { + let i = 0; + let j = 0; + let previousColumn = -1; + let isReducedEchelonForm = true; + let checked = false; + while ((i < this.rows) && (isReducedEchelonForm)) { + j = 0; + checked = false; + while ((j < this.columns) && (checked === false)) { + if (this.get(i, j) === 0) { + j++; + } else if ((this.get(i, j) === 1) && (j > previousColumn)) { + checked = true; + previousColumn = j; + } else { + isReducedEchelonForm = false; + checked = true; + } + } + for (let k = j + 1; k < this.rows; k++) { + if (this.get(i, k) !== 0) { + isReducedEchelonForm = false; + } + } + i++; + } + return isReducedEchelonForm; + } + + /** + * Sets a given element of the matrix. mat.set(3,4,1) is equivalent to mat[3][4]=1 + * @abstract + * @param {number} rowIndex - Index of the row + * @param {number} columnIndex - Index of the column + * @param {number} value - The new value for the element + * @return {Matrix} this + */ + set(rowIndex, columnIndex, value) { // eslint-disable-line no-unused-vars + throw new Error('set method is unimplemented'); + } + + /** + * Returns the given element of the matrix. mat.get(3,4) is equivalent to matrix[3][4] + * @abstract + * @param {number} rowIndex - Index of the row + * @param {number} columnIndex - Index of the column + * @return {number} + */ + get(rowIndex, columnIndex) { // eslint-disable-line no-unused-vars + throw new Error('get method is unimplemented'); + } + + /** + * Creates a new matrix that is a repetition of the current matrix. New matrix has rowRep times the number of + * rows of the matrix, and colRep times the number of columns of the matrix + * @param {number} rowRep - Number of times the rows should be repeated + * @param {number} colRep - Number of times the columns should be re + * @return {Matrix} + * @example + * var matrix = new Matrix([[1,2]]); + * matrix.repeat(2); // [[1,2],[1,2]] + */ + repeat(rowRep, colRep) { + rowRep = rowRep || 1; + colRep = colRep || 1; + var matrix = new this.constructor[Symbol.species](this.rows * rowRep, this.columns * colRep); + for (var i = 0; i < rowRep; i++) { + for (var j = 0; j < colRep; j++) { + matrix.setSubMatrix(this, this.rows * i, this.columns * j); + } + } + return matrix; + } + + /** + * Fills the matrix with a given value. All elements will be set to this value. + * @param {number} value - New value + * @return {Matrix} this + */ + fill(value) { + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + this.set(i, j, value); + } + } + return this; + } + + /** + * Negates the matrix. All elements will be multiplied by (-1) + * @return {Matrix} this + */ + neg() { + return this.mulS(-1); + } + + /** + * Returns a new array from the given row index + * @param {number} index - Row index + * @return {Array} + */ + getRow(index) { + checkRowIndex(this, index); + var row = new Array(this.columns); + for (var i = 0; i < this.columns; i++) { + row[i] = this.get(index, i); + } + return row; + } + + /** + * Returns a new row vector from the given row index + * @param {number} index - Row index + * @return {Matrix} + */ + getRowVector(index) { + return this.constructor.rowVector(this.getRow(index)); + } + + /** + * Sets a row at the given index + * @param {number} index - Row index + * @param {Array|Matrix} array - Array or vector + * @return {Matrix} this + */ + setRow(index, array) { + checkRowIndex(this, index); + array = checkRowVector(this, array); + for (var i = 0; i < this.columns; i++) { + this.set(index, i, array[i]); + } + return this; + } + + /** + * Swaps two rows + * @param {number} row1 - First row index + * @param {number} row2 - Second row index + * @return {Matrix} this + */ + swapRows(row1, row2) { + checkRowIndex(this, row1); + checkRowIndex(this, row2); + for (var i = 0; i < this.columns; i++) { + var temp = this.get(row1, i); + this.set(row1, i, this.get(row2, i)); + this.set(row2, i, temp); + } + return this; + } + + /** + * Returns a new array from the given column index + * @param {number} index - Column index + * @return {Array} + */ + getColumn(index) { + checkColumnIndex(this, index); + var column = new Array(this.rows); + for (var i = 0; i < this.rows; i++) { + column[i] = this.get(i, index); + } + return column; + } + + /** + * Returns a new column vector from the given column index + * @param {number} index - Column index + * @return {Matrix} + */ + getColumnVector(index) { + return this.constructor.columnVector(this.getColumn(index)); + } + + /** + * Sets a column at the given index + * @param {number} index - Column index + * @param {Array|Matrix} array - Array or vector + * @return {Matrix} this + */ + setColumn(index, array) { + checkColumnIndex(this, index); + array = checkColumnVector(this, array); + for (var i = 0; i < this.rows; i++) { + this.set(i, index, array[i]); + } + return this; + } + + /** + * Swaps two columns + * @param {number} column1 - First column index + * @param {number} column2 - Second column index + * @return {Matrix} this + */ + swapColumns(column1, column2) { + checkColumnIndex(this, column1); + checkColumnIndex(this, column2); + for (var i = 0; i < this.rows; i++) { + var temp = this.get(i, column1); + this.set(i, column1, this.get(i, column2)); + this.set(i, column2, temp); + } + return this; + } + + /** + * Adds the values of a vector to each row + * @param {Array|Matrix} vector - Array or vector + * @return {Matrix} this + */ + addRowVector(vector) { + vector = checkRowVector(this, vector); + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) + vector[j]); + } + } + return this; + } + + /** + * Subtracts the values of a vector from each row + * @param {Array|Matrix} vector - Array or vector + * @return {Matrix} this + */ + subRowVector(vector) { + vector = checkRowVector(this, vector); + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) - vector[j]); + } + } + return this; + } + + /** + * Multiplies the values of a vector with each row + * @param {Array|Matrix} vector - Array or vector + * @return {Matrix} this + */ + mulRowVector(vector) { + vector = checkRowVector(this, vector); + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) * vector[j]); + } + } + return this; + } + + /** + * Divides the values of each row by those of a vector + * @param {Array|Matrix} vector - Array or vector + * @return {Matrix} this + */ + divRowVector(vector) { + vector = checkRowVector(this, vector); + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) / vector[j]); + } + } + return this; + } + + /** + * Adds the values of a vector to each column + * @param {Array|Matrix} vector - Array or vector + * @return {Matrix} this + */ + addColumnVector(vector) { + vector = checkColumnVector(this, vector); + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) + vector[i]); + } + } + return this; + } + + /** + * Subtracts the values of a vector from each column + * @param {Array|Matrix} vector - Array or vector + * @return {Matrix} this + */ + subColumnVector(vector) { + vector = checkColumnVector(this, vector); + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) - vector[i]); + } + } + return this; + } + + /** + * Multiplies the values of a vector with each column + * @param {Array|Matrix} vector - Array or vector + * @return {Matrix} this + */ + mulColumnVector(vector) { + vector = checkColumnVector(this, vector); + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) * vector[i]); + } + } + return this; + } + + /** + * Divides the values of each column by those of a vector + * @param {Array|Matrix} vector - Array or vector + * @return {Matrix} this + */ + divColumnVector(vector) { + vector = checkColumnVector(this, vector); + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) / vector[i]); + } + } + return this; + } + + /** + * Multiplies the values of a row with a scalar + * @param {number} index - Row index + * @param {number} value + * @return {Matrix} this + */ + mulRow(index, value) { + checkRowIndex(this, index); + for (var i = 0; i < this.columns; i++) { + this.set(index, i, this.get(index, i) * value); + } + return this; + } + + /** + * Multiplies the values of a column with a scalar + * @param {number} index - Column index + * @param {number} value + * @return {Matrix} this + */ + mulColumn(index, value) { + checkColumnIndex(this, index); + for (var i = 0; i < this.rows; i++) { + this.set(i, index, this.get(i, index) * value); + } + return this; + } + + /** + * Returns the maximum value of the matrix + * @return {number} + */ + max() { + var v = this.get(0, 0); + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + if (this.get(i, j) > v) { + v = this.get(i, j); + } + } + } + return v; + } + + /** + * Returns the index of the maximum value + * @return {Array} + */ + maxIndex() { + var v = this.get(0, 0); + var idx = [0, 0]; + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + if (this.get(i, j) > v) { + v = this.get(i, j); + idx[0] = i; + idx[1] = j; + } + } + } + return idx; + } + + /** + * Returns the minimum value of the matrix + * @return {number} + */ + min() { + var v = this.get(0, 0); + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + if (this.get(i, j) < v) { + v = this.get(i, j); + } + } + } + return v; + } + + /** + * Returns the index of the minimum value + * @return {Array} + */ + minIndex() { + var v = this.get(0, 0); + var idx = [0, 0]; + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + if (this.get(i, j) < v) { + v = this.get(i, j); + idx[0] = i; + idx[1] = j; + } + } + } + return idx; + } + + /** + * Returns the maximum value of one row + * @param {number} row - Row index + * @return {number} + */ + maxRow(row) { + checkRowIndex(this, row); + var v = this.get(row, 0); + for (var i = 1; i < this.columns; i++) { + if (this.get(row, i) > v) { + v = this.get(row, i); + } + } + return v; + } + + /** + * Returns the index of the maximum value of one row + * @param {number} row - Row index + * @return {Array} + */ + maxRowIndex(row) { + checkRowIndex(this, row); + var v = this.get(row, 0); + var idx = [row, 0]; + for (var i = 1; i < this.columns; i++) { + if (this.get(row, i) > v) { + v = this.get(row, i); + idx[1] = i; + } + } + return idx; + } + + /** + * Returns the minimum value of one row + * @param {number} row - Row index + * @return {number} + */ + minRow(row) { + checkRowIndex(this, row); + var v = this.get(row, 0); + for (var i = 1; i < this.columns; i++) { + if (this.get(row, i) < v) { + v = this.get(row, i); + } + } + return v; + } + + /** + * Returns the index of the maximum value of one row + * @param {number} row - Row index + * @return {Array} + */ + minRowIndex(row) { + checkRowIndex(this, row); + var v = this.get(row, 0); + var idx = [row, 0]; + for (var i = 1; i < this.columns; i++) { + if (this.get(row, i) < v) { + v = this.get(row, i); + idx[1] = i; + } + } + return idx; + } + + /** + * Returns the maximum value of one column + * @param {number} column - Column index + * @return {number} + */ + maxColumn(column) { + checkColumnIndex(this, column); + var v = this.get(0, column); + for (var i = 1; i < this.rows; i++) { + if (this.get(i, column) > v) { + v = this.get(i, column); + } + } + return v; + } + + /** + * Returns the index of the maximum value of one column + * @param {number} column - Column index + * @return {Array} + */ + maxColumnIndex(column) { + checkColumnIndex(this, column); + var v = this.get(0, column); + var idx = [0, column]; + for (var i = 1; i < this.rows; i++) { + if (this.get(i, column) > v) { + v = this.get(i, column); + idx[0] = i; + } + } + return idx; + } + + /** + * Returns the minimum value of one column + * @param {number} column - Column index + * @return {number} + */ + minColumn(column) { + checkColumnIndex(this, column); + var v = this.get(0, column); + for (var i = 1; i < this.rows; i++) { + if (this.get(i, column) < v) { + v = this.get(i, column); + } + } + return v; + } + + /** + * Returns the index of the minimum value of one column + * @param {number} column - Column index + * @return {Array} + */ + minColumnIndex(column) { + checkColumnIndex(this, column); + var v = this.get(0, column); + var idx = [0, column]; + for (var i = 1; i < this.rows; i++) { + if (this.get(i, column) < v) { + v = this.get(i, column); + idx[0] = i; + } + } + return idx; + } + + /** + * Returns an array containing the diagonal values of the matrix + * @return {Array} + */ + diag() { + var min = Math.min(this.rows, this.columns); + var diag = new Array(min); + for (var i = 0; i < min; i++) { + diag[i] = this.get(i, i); + } + return diag; + } + + /** + * Returns the sum by the argument given, if no argument given, + * it returns the sum of all elements of the matrix. + * @param {string} by - sum by 'row' or 'column'. + * @return {Matrix|number} + */ + sum(by) { + switch (by) { + case 'row': + return sumByRow(this); + case 'column': + return sumByColumn(this); + default: + return sumAll(this); + } + } + + /** + * Returns the mean of all elements of the matrix + * @return {number} + */ + mean() { + return this.sum() / this.size; + } + + /** + * Returns the product of all elements of the matrix + * @return {number} + */ + prod() { + var prod = 1; + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + prod *= this.get(i, j); + } + } + return prod; + } + + /** + * Returns the norm of a matrix. + * @param {string} type - "frobenius" (default) or "max" return resp. the Frobenius norm and the max norm. + * @return {number} + */ + norm(type = 'frobenius') { + var result = 0; + if (type === 'max') { + return this.max(); + } else if (type === 'frobenius') { + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + result = result + this.get(i, j) * this.get(i, j); + } + } + return Math.sqrt(result); + } else { + throw new RangeError(`unknown norm type: ${type}`); + } + } + + /** + * Computes the cumulative sum of the matrix elements (in place, row by row) + * @return {Matrix} this + */ + cumulativeSum() { + var sum = 0; + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + sum += this.get(i, j); + this.set(i, j, sum); + } + } + return this; + } + + /** + * Computes the dot (scalar) product between the matrix and another + * @param {Matrix} vector2 vector + * @return {number} + */ + dot(vector2) { + if (Matrix.isMatrix(vector2)) vector2 = vector2.to1DArray(); + var vector1 = this.to1DArray(); + if (vector1.length !== vector2.length) { + throw new RangeError('vectors do not have the same size'); + } + var dot = 0; + for (var i = 0; i < vector1.length; i++) { + dot += vector1[i] * vector2[i]; + } + return dot; + } + + /** + * Returns the matrix product between this and other + * @param {Matrix} other + * @return {Matrix} + */ + mmul(other) { + other = this.constructor.checkMatrix(other); + if (this.columns !== other.rows) { + // eslint-disable-next-line no-console + console.warn('Number of columns of left matrix are not equal to number of rows of right matrix.'); + } + + var m = this.rows; + var n = this.columns; + var p = other.columns; + + var result = new this.constructor[Symbol.species](m, p); + + var Bcolj = new Array(n); + for (var j = 0; j < p; j++) { + for (var k = 0; k < n; k++) { + Bcolj[k] = other.get(k, j); + } + + for (var i = 0; i < m; i++) { + var s = 0; + for (k = 0; k < n; k++) { + s += this.get(i, k) * Bcolj[k]; + } + + result.set(i, j, s); + } + } + return result; + } + + strassen2x2(other) { + var result = new this.constructor[Symbol.species](2, 2); + const a11 = this.get(0, 0); + const b11 = other.get(0, 0); + const a12 = this.get(0, 1); + const b12 = other.get(0, 1); + const a21 = this.get(1, 0); + const b21 = other.get(1, 0); + const a22 = this.get(1, 1); + const b22 = other.get(1, 1); + + // Compute intermediate values. + const m1 = (a11 + a22) * (b11 + b22); + const m2 = (a21 + a22) * b11; + const m3 = a11 * (b12 - b22); + const m4 = a22 * (b21 - b11); + const m5 = (a11 + a12) * b22; + const m6 = (a21 - a11) * (b11 + b12); + const m7 = (a12 - a22) * (b21 + b22); + + // Combine intermediate values into the output. + const c00 = m1 + m4 - m5 + m7; + const c01 = m3 + m5; + const c10 = m2 + m4; + const c11 = m1 - m2 + m3 + m6; + + result.set(0, 0, c00); + result.set(0, 1, c01); + result.set(1, 0, c10); + result.set(1, 1, c11); + return result; + } + + strassen3x3(other) { + var result = new this.constructor[Symbol.species](3, 3); + + const a00 = this.get(0, 0); + const a01 = this.get(0, 1); + const a02 = this.get(0, 2); + const a10 = this.get(1, 0); + const a11 = this.get(1, 1); + const a12 = this.get(1, 2); + const a20 = this.get(2, 0); + const a21 = this.get(2, 1); + const a22 = this.get(2, 2); + + const b00 = other.get(0, 0); + const b01 = other.get(0, 1); + const b02 = other.get(0, 2); + const b10 = other.get(1, 0); + const b11 = other.get(1, 1); + const b12 = other.get(1, 2); + const b20 = other.get(2, 0); + const b21 = other.get(2, 1); + const b22 = other.get(2, 2); + + const m1 = (a00 + a01 + a02 - a10 - a11 - a21 - a22) * b11; + const m2 = (a00 - a10) * (-b01 + b11); + const m3 = a11 * (-b00 + b01 + b10 - b11 - b12 - b20 + b22); + const m4 = (-a00 + a10 + a11) * (b00 - b01 + b11); + const m5 = (a10 + a11) * (-b00 + b01); + const m6 = a00 * b00; + const m7 = (-a00 + a20 + a21) * (b00 - b02 + b12); + const m8 = (-a00 + a20) * (b02 - b12); + const m9 = (a20 + a21) * (-b00 + b02); + const m10 = (a00 + a01 + a02 - a11 - a12 - a20 - a21) * b12; + const m11 = a21 * (-b00 + b02 + b10 - b11 - b12 - b20 + b21); + const m12 = (-a02 + a21 + a22) * (b11 + b20 - b21); + const m13 = (a02 - a22) * (b11 - b21); + const m14 = a02 * b20; + const m15 = (a21 + a22) * (-b20 + b21); + const m16 = (-a02 + a11 + a12) * (b12 + b20 - b22); + const m17 = (a02 - a12) * (b12 - b22); + const m18 = (a11 + a12) * (-b20 + b22); + const m19 = a01 * b10; + const m20 = a12 * b21; + const m21 = a10 * b02; + const m22 = a20 * b01; + const m23 = a22 * b22; + + const c00 = m6 + m14 + m19; + const c01 = m1 + m4 + m5 + m6 + m12 + m14 + m15; + const c02 = m6 + m7 + m9 + m10 + m14 + m16 + m18; + const c10 = m2 + m3 + m4 + m6 + m14 + m16 + m17; + const c11 = m2 + m4 + m5 + m6 + m20; + const c12 = m14 + m16 + m17 + m18 + m21; + const c20 = m6 + m7 + m8 + m11 + m12 + m13 + m14; + const c21 = m12 + m13 + m14 + m15 + m22; + const c22 = m6 + m7 + m8 + m9 + m23; + + result.set(0, 0, c00); + result.set(0, 1, c01); + result.set(0, 2, c02); + result.set(1, 0, c10); + result.set(1, 1, c11); + result.set(1, 2, c12); + result.set(2, 0, c20); + result.set(2, 1, c21); + result.set(2, 2, c22); + return result; + } + + /** + * Returns the matrix product between x and y. More efficient than mmul(other) only when we multiply squared matrix and when the size of the matrix is > 1000. + * @param {Matrix} y + * @return {Matrix} + */ + mmulStrassen(y) { + var x = this.clone(); + var r1 = x.rows; + var c1 = x.columns; + var r2 = y.rows; + var c2 = y.columns; + if (c1 !== r2) { + // eslint-disable-next-line no-console + console.warn(`Multiplying ${r1} x ${c1} and ${r2} x ${c2} matrix: dimensions do not match.`); + } + + // Put a matrix into the top left of a matrix of zeros. + // `rows` and `cols` are the dimensions of the output matrix. + function embed(mat, rows, cols) { + var r = mat.rows; + var c = mat.columns; + if ((r === rows) && (c === cols)) { + return mat; + } else { + var resultat = Matrix.zeros(rows, cols); + resultat = resultat.setSubMatrix(mat, 0, 0); + return resultat; + } + } + + + // Make sure both matrices are the same size. + // This is exclusively for simplicity: + // this algorithm can be implemented with matrices of different sizes. + + var r = Math.max(r1, r2); + var c = Math.max(c1, c2); + x = embed(x, r, c); + y = embed(y, r, c); + + // Our recursive multiplication function. + function blockMult(a, b, rows, cols) { + // For small matrices, resort to naive multiplication. + if (rows <= 512 || cols <= 512) { + return a.mmul(b); // a is equivalent to this + } + + // Apply dynamic padding. + if ((rows % 2 === 1) && (cols % 2 === 1)) { + a = embed(a, rows + 1, cols + 1); + b = embed(b, rows + 1, cols + 1); + } else if (rows % 2 === 1) { + a = embed(a, rows + 1, cols); + b = embed(b, rows + 1, cols); + } else if (cols % 2 === 1) { + a = embed(a, rows, cols + 1); + b = embed(b, rows, cols + 1); + } + + var halfRows = parseInt(a.rows / 2, 10); + var halfCols = parseInt(a.columns / 2, 10); + // Subdivide input matrices. + var a11 = a.subMatrix(0, halfRows - 1, 0, halfCols - 1); + var b11 = b.subMatrix(0, halfRows - 1, 0, halfCols - 1); + + var a12 = a.subMatrix(0, halfRows - 1, halfCols, a.columns - 1); + var b12 = b.subMatrix(0, halfRows - 1, halfCols, b.columns - 1); + + var a21 = a.subMatrix(halfRows, a.rows - 1, 0, halfCols - 1); + var b21 = b.subMatrix(halfRows, b.rows - 1, 0, halfCols - 1); + + var a22 = a.subMatrix(halfRows, a.rows - 1, halfCols, a.columns - 1); + var b22 = b.subMatrix(halfRows, b.rows - 1, halfCols, b.columns - 1); + + // Compute intermediate values. + var m1 = blockMult(Matrix.add(a11, a22), Matrix.add(b11, b22), halfRows, halfCols); + var m2 = blockMult(Matrix.add(a21, a22), b11, halfRows, halfCols); + var m3 = blockMult(a11, Matrix.sub(b12, b22), halfRows, halfCols); + var m4 = blockMult(a22, Matrix.sub(b21, b11), halfRows, halfCols); + var m5 = blockMult(Matrix.add(a11, a12), b22, halfRows, halfCols); + var m6 = blockMult(Matrix.sub(a21, a11), Matrix.add(b11, b12), halfRows, halfCols); + var m7 = blockMult(Matrix.sub(a12, a22), Matrix.add(b21, b22), halfRows, halfCols); + + // Combine intermediate values into the output. + var c11 = Matrix.add(m1, m4); + c11.sub(m5); + c11.add(m7); + var c12 = Matrix.add(m3, m5); + var c21 = Matrix.add(m2, m4); + var c22 = Matrix.sub(m1, m2); + c22.add(m3); + c22.add(m6); + + // Crop output to the desired size (undo dynamic padding). + var resultat = Matrix.zeros(2 * c11.rows, 2 * c11.columns); + resultat = resultat.setSubMatrix(c11, 0, 0); + resultat = resultat.setSubMatrix(c12, c11.rows, 0); + resultat = resultat.setSubMatrix(c21, 0, c11.columns); + resultat = resultat.setSubMatrix(c22, c11.rows, c11.columns); + return resultat.subMatrix(0, rows - 1, 0, cols - 1); + } + return blockMult(x, y, r, c); + } + + /** + * Returns a row-by-row scaled matrix + * @param {number} [min=0] - Minimum scaled value + * @param {number} [max=1] - Maximum scaled value + * @return {Matrix} - The scaled matrix + */ + scaleRows(min, max) { + min = min === undefined ? 0 : min; + max = max === undefined ? 1 : max; + if (min >= max) { + throw new RangeError('min should be strictly smaller than max'); + } + var newMatrix = this.constructor.empty(this.rows, this.columns); + for (var i = 0; i < this.rows; i++) { + var scaled = ml_array_rescale_lib_es6(this.getRow(i), { min, max }); + newMatrix.setRow(i, scaled); + } + return newMatrix; + } + + /** + * Returns a new column-by-column scaled matrix + * @param {number} [min=0] - Minimum scaled value + * @param {number} [max=1] - Maximum scaled value + * @return {Matrix} - The new scaled matrix + * @example + * var matrix = new Matrix([[1,2],[-1,0]]); + * var scaledMatrix = matrix.scaleColumns(); // [[1,1],[0,0]] + */ + scaleColumns(min, max) { + min = min === undefined ? 0 : min; + max = max === undefined ? 1 : max; + if (min >= max) { + throw new RangeError('min should be strictly smaller than max'); + } + var newMatrix = this.constructor.empty(this.rows, this.columns); + for (var i = 0; i < this.columns; i++) { + var scaled = ml_array_rescale_lib_es6(this.getColumn(i), { + min: min, + max: max + }); + newMatrix.setColumn(i, scaled); + } + return newMatrix; + } + + + /** + * Returns the Kronecker product (also known as tensor product) between this and other + * See https://en.wikipedia.org/wiki/Kronecker_product + * @param {Matrix} other + * @return {Matrix} + */ + kroneckerProduct(other) { + other = this.constructor.checkMatrix(other); + + var m = this.rows; + var n = this.columns; + var p = other.rows; + var q = other.columns; + + var result = new this.constructor[Symbol.species](m * p, n * q); + for (var i = 0; i < m; i++) { + for (var j = 0; j < n; j++) { + for (var k = 0; k < p; k++) { + for (var l = 0; l < q; l++) { + result[p * i + k][q * j + l] = this.get(i, j) * other.get(k, l); + } + } + } + } + return result; + } + + /** + * Transposes the matrix and returns a new one containing the result + * @return {Matrix} + */ + transpose() { + var result = new this.constructor[Symbol.species](this.columns, this.rows); + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + result.set(j, i, this.get(i, j)); + } + } + return result; + } + + /** + * Sorts the rows (in place) + * @param {function} compareFunction - usual Array.prototype.sort comparison function + * @return {Matrix} this + */ + sortRows(compareFunction) { + if (compareFunction === undefined) compareFunction = compareNumbers; + for (var i = 0; i < this.rows; i++) { + this.setRow(i, this.getRow(i).sort(compareFunction)); + } + return this; + } + + /** + * Sorts the columns (in place) + * @param {function} compareFunction - usual Array.prototype.sort comparison function + * @return {Matrix} this + */ + sortColumns(compareFunction) { + if (compareFunction === undefined) compareFunction = compareNumbers; + for (var i = 0; i < this.columns; i++) { + this.setColumn(i, this.getColumn(i).sort(compareFunction)); + } + return this; + } + + /** + * Returns a subset of the matrix + * @param {number} startRow - First row index + * @param {number} endRow - Last row index + * @param {number} startColumn - First column index + * @param {number} endColumn - Last column index + * @return {Matrix} + */ + subMatrix(startRow, endRow, startColumn, endColumn) { + checkRange(this, startRow, endRow, startColumn, endColumn); + var newMatrix = new this.constructor[Symbol.species](endRow - startRow + 1, endColumn - startColumn + 1); + for (var i = startRow; i <= endRow; i++) { + for (var j = startColumn; j <= endColumn; j++) { + newMatrix[i - startRow][j - startColumn] = this.get(i, j); + } + } + return newMatrix; + } + + /** + * Returns a subset of the matrix based on an array of row indices + * @param {Array} indices - Array containing the row indices + * @param {number} [startColumn = 0] - First column index + * @param {number} [endColumn = this.columns-1] - Last column index + * @return {Matrix} + */ + subMatrixRow(indices, startColumn, endColumn) { + if (startColumn === undefined) startColumn = 0; + if (endColumn === undefined) endColumn = this.columns - 1; + if ((startColumn > endColumn) || (startColumn < 0) || (startColumn >= this.columns) || (endColumn < 0) || (endColumn >= this.columns)) { + throw new RangeError('Argument out of range'); + } + + var newMatrix = new this.constructor[Symbol.species](indices.length, endColumn - startColumn + 1); + for (var i = 0; i < indices.length; i++) { + for (var j = startColumn; j <= endColumn; j++) { + if (indices[i] < 0 || indices[i] >= this.rows) { + throw new RangeError(`Row index out of range: ${indices[i]}`); + } + newMatrix.set(i, j - startColumn, this.get(indices[i], j)); + } + } + return newMatrix; + } + + /** + * Returns a subset of the matrix based on an array of column indices + * @param {Array} indices - Array containing the column indices + * @param {number} [startRow = 0] - First row index + * @param {number} [endRow = this.rows-1] - Last row index + * @return {Matrix} + */ + subMatrixColumn(indices, startRow, endRow) { + if (startRow === undefined) startRow = 0; + if (endRow === undefined) endRow = this.rows - 1; + if ((startRow > endRow) || (startRow < 0) || (startRow >= this.rows) || (endRow < 0) || (endRow >= this.rows)) { + throw new RangeError('Argument out of range'); + } + + var newMatrix = new this.constructor[Symbol.species](endRow - startRow + 1, indices.length); + for (var i = 0; i < indices.length; i++) { + for (var j = startRow; j <= endRow; j++) { + if (indices[i] < 0 || indices[i] >= this.columns) { + throw new RangeError(`Column index out of range: ${indices[i]}`); + } + newMatrix.set(j - startRow, i, this.get(j, indices[i])); + } + } + return newMatrix; + } + + /** + * Set a part of the matrix to the given sub-matrix + * @param {Matrix|Array< Array >} matrix - The source matrix from which to extract values. + * @param {number} startRow - The index of the first row to set + * @param {number} startColumn - The index of the first column to set + * @return {Matrix} + */ + setSubMatrix(matrix, startRow, startColumn) { + matrix = this.constructor.checkMatrix(matrix); + var endRow = startRow + matrix.rows - 1; + var endColumn = startColumn + matrix.columns - 1; + checkRange(this, startRow, endRow, startColumn, endColumn); + for (var i = 0; i < matrix.rows; i++) { + for (var j = 0; j < matrix.columns; j++) { + this[startRow + i][startColumn + j] = matrix.get(i, j); + } + } + return this; + } + + /** + * Return a new matrix based on a selection of rows and columns + * @param {Array} rowIndices - The row indices to select. Order matters and an index can be more than once. + * @param {Array} columnIndices - The column indices to select. Order matters and an index can be use more than once. + * @return {Matrix} The new matrix + */ + selection(rowIndices, columnIndices) { + var indices = checkIndices(this, rowIndices, columnIndices); + var newMatrix = new this.constructor[Symbol.species](rowIndices.length, columnIndices.length); + for (var i = 0; i < indices.row.length; i++) { + var rowIndex = indices.row[i]; + for (var j = 0; j < indices.column.length; j++) { + var columnIndex = indices.column[j]; + newMatrix[i][j] = this.get(rowIndex, columnIndex); + } + } + return newMatrix; + } + + /** + * Returns the trace of the matrix (sum of the diagonal elements) + * @return {number} + */ + trace() { + var min = Math.min(this.rows, this.columns); + var trace = 0; + for (var i = 0; i < min; i++) { + trace += this.get(i, i); + } + return trace; + } + + /* + Matrix views + */ + + /** + * Returns a view of the transposition of the matrix + * @return {MatrixTransposeView} + */ + transposeView() { + return new transpose_MatrixTransposeView(this); + } + + /** + * Returns a view of the row vector with the given index + * @param {number} row - row index of the vector + * @return {MatrixRowView} + */ + rowView(row) { + checkRowIndex(this, row); + return new row_MatrixRowView(this, row); + } + + /** + * Returns a view of the column vector with the given index + * @param {number} column - column index of the vector + * @return {MatrixColumnView} + */ + columnView(column) { + checkColumnIndex(this, column); + return new column_MatrixColumnView(this, column); + } + + /** + * Returns a view of the matrix flipped in the row axis + * @return {MatrixFlipRowView} + */ + flipRowView() { + return new flipRow_MatrixFlipRowView(this); + } + + /** + * Returns a view of the matrix flipped in the column axis + * @return {MatrixFlipColumnView} + */ + flipColumnView() { + return new flipColumn_MatrixFlipColumnView(this); + } + + /** + * Returns a view of a submatrix giving the index boundaries + * @param {number} startRow - first row index of the submatrix + * @param {number} endRow - last row index of the submatrix + * @param {number} startColumn - first column index of the submatrix + * @param {number} endColumn - last column index of the submatrix + * @return {MatrixSubView} + */ + subMatrixView(startRow, endRow, startColumn, endColumn) { + return new sub_MatrixSubView(this, startRow, endRow, startColumn, endColumn); + } + + /** + * Returns a view of the cross of the row indices and the column indices + * @example + * // resulting vector is [[2], [2]] + * var matrix = new Matrix([[1,2,3], [4,5,6]]).selectionView([0, 0], [1]) + * @param {Array} rowIndices + * @param {Array} columnIndices + * @return {MatrixSelectionView} + */ + selectionView(rowIndices, columnIndices) { + return new selection_MatrixSelectionView(this, rowIndices, columnIndices); + } + + /** + * Returns a view of the row indices + * @example + * // resulting vector is [[1,2,3], [1,2,3]] + * var matrix = new Matrix([[1,2,3], [4,5,6]]).rowSelectionView([0, 0]) + * @param {Array} rowIndices + * @return {MatrixRowSelectionView} + */ + rowSelectionView(rowIndices) { + return new rowSelection_MatrixRowSelectionView(this, rowIndices); + } + + /** + * Returns a view of the column indices + * @example + * // resulting vector is [[2, 2], [5, 5]] + * var matrix = new Matrix([[1,2,3], [4,5,6]]).columnSelectionView([1, 1]) + * @param {Array} columnIndices + * @return {MatrixColumnSelectionView} + */ + columnSelectionView(columnIndices) { + return new columnSelection_MatrixColumnSelectionView(this, columnIndices); + } + + + /** + * Calculates and returns the determinant of a matrix as a Number + * @example + * new Matrix([[1,2,3], [4,5,6]]).det() + * @return {number} + */ + det() { + if (this.isSquare()) { + var a, b, c, d; + if (this.columns === 2) { + // 2 x 2 matrix + a = this.get(0, 0); + b = this.get(0, 1); + c = this.get(1, 0); + d = this.get(1, 1); + + return a * d - (b * c); + } else if (this.columns === 3) { + // 3 x 3 matrix + var subMatrix0, subMatrix1, subMatrix2; + subMatrix0 = this.selectionView([1, 2], [1, 2]); + subMatrix1 = this.selectionView([1, 2], [0, 2]); + subMatrix2 = this.selectionView([1, 2], [0, 1]); + a = this.get(0, 0); + b = this.get(0, 1); + c = this.get(0, 2); + + return a * subMatrix0.det() - b * subMatrix1.det() + c * subMatrix2.det(); + } else { + // general purpose determinant using the LU decomposition + return new lu_LuDecomposition(this).determinant; + } + } else { + throw Error('Determinant can only be calculated for a square matrix.'); + } + } + + /** + * Returns inverse of a matrix if it exists or the pseudoinverse + * @param {number} threshold - threshold for taking inverse of singular values (default = 1e-15) + * @return {Matrix} the (pseudo)inverted matrix. + */ + pseudoInverse(threshold) { + if (threshold === undefined) threshold = Number.EPSILON; + var svdSolution = new svd_SingularValueDecomposition(this, { autoTranspose: true }); + + var U = svdSolution.leftSingularVectors; + var V = svdSolution.rightSingularVectors; + var s = svdSolution.diagonal; + + for (var i = 0; i < s.length; i++) { + if (Math.abs(s[i]) > threshold) { + s[i] = 1.0 / s[i]; + } else { + s[i] = 0.0; + } + } + + // convert list to diagonal + s = this.constructor[Symbol.species].diag(s); + return V.mmul(s.mmul(U.transposeView())); + } + + /** + * Creates an exact and independent copy of the matrix + * @return {Matrix} + */ + clone() { + var newMatrix = new this.constructor[Symbol.species](this.rows, this.columns); + for (var row = 0; row < this.rows; row++) { + for (var column = 0; column < this.columns; column++) { + newMatrix.set(row, column, this.get(row, column)); + } + } + return newMatrix; + } + } + + Matrix.prototype.klass = 'Matrix'; + + function compareNumbers(a, b) { + return a - b; + } + + /* + Synonyms + */ + + Matrix.random = Matrix.rand; + Matrix.diagonal = Matrix.diag; + Matrix.prototype.diagonal = Matrix.prototype.diag; + Matrix.identity = Matrix.eye; + Matrix.prototype.negate = Matrix.prototype.neg; + Matrix.prototype.tensorProduct = Matrix.prototype.kroneckerProduct; + Matrix.prototype.determinant = Matrix.prototype.det; + + /* + Add dynamically instance and static methods for mathematical operations + */ + + var inplaceOperator = ` +(function %name%(value) { + if (typeof value === 'number') return this.%name%S(value); + return this.%name%M(value); +}) +`; + + var inplaceOperatorScalar = ` +(function %name%S(value) { + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) %op% value); + } + } + return this; +}) +`; + + var inplaceOperatorMatrix = ` +(function %name%M(matrix) { + matrix = this.constructor.checkMatrix(matrix); + if (this.rows !== matrix.rows || + this.columns !== matrix.columns) { + throw new RangeError('Matrices dimensions must be equal'); + } + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) %op% matrix.get(i, j)); + } + } + return this; +}) +`; + + var staticOperator = ` +(function %name%(matrix, value) { + var newMatrix = new this[Symbol.species](matrix); + return newMatrix.%name%(value); +}) +`; + + var inplaceMethod = ` +(function %name%() { + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + this.set(i, j, %method%(this.get(i, j))); + } + } + return this; +}) +`; + + var staticMethod = ` +(function %name%(matrix) { + var newMatrix = new this[Symbol.species](matrix); + return newMatrix.%name%(); +}) +`; + + var inplaceMethodWithArgs = ` +(function %name%(%args%) { + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + this.set(i, j, %method%(this.get(i, j), %args%)); + } + } + return this; +}) +`; + + var staticMethodWithArgs = ` +(function %name%(matrix, %args%) { + var newMatrix = new this[Symbol.species](matrix); + return newMatrix.%name%(%args%); +}) +`; + + + var inplaceMethodWithOneArgScalar = ` +(function %name%S(value) { + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + this.set(i, j, %method%(this.get(i, j), value)); + } + } + return this; +}) +`; + var inplaceMethodWithOneArgMatrix = ` +(function %name%M(matrix) { + matrix = this.constructor.checkMatrix(matrix); + if (this.rows !== matrix.rows || + this.columns !== matrix.columns) { + throw new RangeError('Matrices dimensions must be equal'); + } + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + this.set(i, j, %method%(this.get(i, j), matrix.get(i, j))); + } + } + return this; +}) +`; + + var inplaceMethodWithOneArg = ` +(function %name%(value) { + if (typeof value === 'number') return this.%name%S(value); + return this.%name%M(value); +}) +`; + + var staticMethodWithOneArg = staticMethodWithArgs; + + var operators = [ + // Arithmetic operators + ['+', 'add'], + ['-', 'sub', 'subtract'], + ['*', 'mul', 'multiply'], + ['/', 'div', 'divide'], + ['%', 'mod', 'modulus'], + // Bitwise operators + ['&', 'and'], + ['|', 'or'], + ['^', 'xor'], + ['<<', 'leftShift'], + ['>>', 'signPropagatingRightShift'], + ['>>>', 'rightShift', 'zeroFillRightShift'] + ]; + + var i; + var eval2 = eval; // eslint-disable-line no-eval + for (var operator of operators) { + var inplaceOp = eval2(fillTemplateFunction(inplaceOperator, { name: operator[1], op: operator[0] })); + var inplaceOpS = eval2(fillTemplateFunction(inplaceOperatorScalar, { name: `${operator[1]}S`, op: operator[0] })); + var inplaceOpM = eval2(fillTemplateFunction(inplaceOperatorMatrix, { name: `${operator[1]}M`, op: operator[0] })); + var staticOp = eval2(fillTemplateFunction(staticOperator, { name: operator[1] })); + for (i = 1; i < operator.length; i++) { + Matrix.prototype[operator[i]] = inplaceOp; + Matrix.prototype[`${operator[i]}S`] = inplaceOpS; + Matrix.prototype[`${operator[i]}M`] = inplaceOpM; + Matrix[operator[i]] = staticOp; + } + } + + var methods = [['~', 'not']]; + + [ + 'abs', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atanh', 'cbrt', 'ceil', + 'clz32', 'cos', 'cosh', 'exp', 'expm1', 'floor', 'fround', 'log', 'log1p', + 'log10', 'log2', 'round', 'sign', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc' + ].forEach(function (mathMethod) { + methods.push([`Math.${mathMethod}`, mathMethod]); + }); + + for (var method of methods) { + var inplaceMeth = eval2(fillTemplateFunction(inplaceMethod, { name: method[1], method: method[0] })); + var staticMeth = eval2(fillTemplateFunction(staticMethod, { name: method[1] })); + for (i = 1; i < method.length; i++) { + Matrix.prototype[method[i]] = inplaceMeth; + Matrix[method[i]] = staticMeth; + } + } + + var methodsWithArgs = [['Math.pow', 1, 'pow']]; + + for (var methodWithArg of methodsWithArgs) { + var args = 'arg0'; + for (i = 1; i < methodWithArg[1]; i++) { + args += `, arg${i}`; + } + if (methodWithArg[1] !== 1) { + var inplaceMethWithArgs = eval2(fillTemplateFunction(inplaceMethodWithArgs, { + name: methodWithArg[2], + method: methodWithArg[0], + args: args + })); + var staticMethWithArgs = eval2(fillTemplateFunction(staticMethodWithArgs, { name: methodWithArg[2], args: args })); + for (i = 2; i < methodWithArg.length; i++) { + Matrix.prototype[methodWithArg[i]] = inplaceMethWithArgs; + Matrix[methodWithArg[i]] = staticMethWithArgs; + } + } else { + var tmplVar = { + name: methodWithArg[2], + args: args, + method: methodWithArg[0] + }; + var inplaceMethod2 = eval2(fillTemplateFunction(inplaceMethodWithOneArg, tmplVar)); + var inplaceMethodS = eval2(fillTemplateFunction(inplaceMethodWithOneArgScalar, tmplVar)); + var inplaceMethodM = eval2(fillTemplateFunction(inplaceMethodWithOneArgMatrix, tmplVar)); + var staticMethod2 = eval2(fillTemplateFunction(staticMethodWithOneArg, tmplVar)); + for (i = 2; i < methodWithArg.length; i++) { + Matrix.prototype[methodWithArg[i]] = inplaceMethod2; + Matrix.prototype[`${methodWithArg[i]}M`] = inplaceMethodM; + Matrix.prototype[`${methodWithArg[i]}S`] = inplaceMethodS; + Matrix[methodWithArg[i]] = staticMethod2; + } + } + } + + function fillTemplateFunction(template, values) { + for (var value in values) { + template = template.replace(new RegExp(`%${value}%`, 'g'), values[value]); + } + return template; + } + + return Matrix; +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/matrix.js + + + +class matrix_Matrix extends AbstractMatrix(Array) { + constructor(nRows, nColumns) { + var i; + if (arguments.length === 1 && typeof nRows === 'number') { + return new Array(nRows); + } + if (matrix_Matrix.isMatrix(nRows)) { + return nRows.clone(); + } else if (Number.isInteger(nRows) && nRows > 0) { + // Create an empty matrix + super(nRows); + if (Number.isInteger(nColumns) && nColumns > 0) { + for (i = 0; i < nRows; i++) { + this[i] = new Array(nColumns); + } + } else { + throw new TypeError('nColumns must be a positive integer'); + } + } else if (Array.isArray(nRows)) { + // Copy the values from the 2D array + const matrix = nRows; + nRows = matrix.length; + nColumns = matrix[0].length; + if (typeof nColumns !== 'number' || nColumns === 0) { + throw new TypeError( + 'Data must be a 2D array with at least one element' + ); + } + super(nRows); + for (i = 0; i < nRows; i++) { + if (matrix[i].length !== nColumns) { + throw new RangeError('Inconsistent array dimensions'); + } + this[i] = [].concat(matrix[i]); + } + } else { + throw new TypeError( + 'First argument must be a positive number or an array' + ); + } + this.rows = nRows; + this.columns = nColumns; + return this; + } + + set(rowIndex, columnIndex, value) { + this[rowIndex][columnIndex] = value; + return this; + } + + get(rowIndex, columnIndex) { + return this[rowIndex][columnIndex]; + } + + /** + * Removes a row from the given index + * @param {number} index - Row index + * @return {Matrix} this + */ + removeRow(index) { + checkRowIndex(this, index); + if (this.rows === 1) { + throw new RangeError('A matrix cannot have less than one row'); + } + this.splice(index, 1); + this.rows -= 1; + return this; + } + + /** + * Adds a row at the given index + * @param {number} [index = this.rows] - Row index + * @param {Array|Matrix} array - Array or vector + * @return {Matrix} this + */ + addRow(index, array) { + if (array === undefined) { + array = index; + index = this.rows; + } + checkRowIndex(this, index, true); + array = checkRowVector(this, array, true); + this.splice(index, 0, array); + this.rows += 1; + return this; + } + + /** + * Removes a column from the given index + * @param {number} index - Column index + * @return {Matrix} this + */ + removeColumn(index) { + checkColumnIndex(this, index); + if (this.columns === 1) { + throw new RangeError('A matrix cannot have less than one column'); + } + for (var i = 0; i < this.rows; i++) { + this[i].splice(index, 1); + } + this.columns -= 1; + return this; + } + + /** + * Adds a column at the given index + * @param {number} [index = this.columns] - Column index + * @param {Array|Matrix} array - Array or vector + * @return {Matrix} this + */ + addColumn(index, array) { + if (typeof array === 'undefined') { + array = index; + index = this.columns; + } + checkColumnIndex(this, index, true); + array = checkColumnVector(this, array); + for (var i = 0; i < this.rows; i++) { + this[i].splice(index, 0, array[i]); + } + this.columns += 1; + return this; + } +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/wrap/WrapperMatrix1D.js + + + +class WrapperMatrix1D_WrapperMatrix1D extends AbstractMatrix() { + /** + * @class WrapperMatrix1D + * @param {Array} data + * @param {object} [options] + * @param {object} [options.rows = 1] + */ + constructor(data, options = {}) { + const { rows = 1 } = options; + + if (data.length % rows !== 0) { + throw new Error('the data length is not divisible by the number of rows'); + } + super(); + this.rows = rows; + this.columns = data.length / rows; + this.data = data; + } + + set(rowIndex, columnIndex, value) { + var index = this._calculateIndex(rowIndex, columnIndex); + this.data[index] = value; + return this; + } + + get(rowIndex, columnIndex) { + var index = this._calculateIndex(rowIndex, columnIndex); + return this.data[index]; + } + + _calculateIndex(row, column) { + return row * this.columns + column; + } + + static get [Symbol.species]() { + return matrix_Matrix; + } +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/wrap/WrapperMatrix2D.js + + + +class WrapperMatrix2D_WrapperMatrix2D extends AbstractMatrix() { + /** + * @class WrapperMatrix2D + * @param {Array>} data + */ + constructor(data) { + super(); + this.data = data; + this.rows = data.length; + this.columns = data[0].length; + } + + set(rowIndex, columnIndex, value) { + this.data[rowIndex][columnIndex] = value; + return this; + } + + get(rowIndex, columnIndex) { + return this.data[rowIndex][columnIndex]; + } + + static get [Symbol.species]() { + return matrix_Matrix; + } +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/wrap/wrap.js + + + +/** + * @param {Array>|Array} array + * @param {object} [options] + * @param {object} [options.rows = 1] + * @return {WrapperMatrix1D|WrapperMatrix2D} + */ +function wrap(array, options) { + if (Array.isArray(array)) { + if (array[0] && Array.isArray(array[0])) { + return new WrapperMatrix2D_WrapperMatrix2D(array); + } else { + return new WrapperMatrix1D_WrapperMatrix1D(array, options); + } + } else { + throw new Error('the argument is not an array'); + } +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/dc/qr.js + + + + +/** + * @class QrDecomposition + * @link https://github.com/lutzroeder/Mapack/blob/master/Source/QrDecomposition.cs + * @param {Matrix} value + */ +class qr_QrDecomposition { + constructor(value) { + value = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(value); + + var qr = value.clone(); + var m = value.rows; + var n = value.columns; + var rdiag = new Array(n); + var i, j, k, s; + + for (k = 0; k < n; k++) { + var nrm = 0; + for (i = k; i < m; i++) { + nrm = hypotenuse(nrm, qr.get(i, k)); + } + if (nrm !== 0) { + if (qr.get(k, k) < 0) { + nrm = -nrm; + } + for (i = k; i < m; i++) { + qr.set(i, k, qr.get(i, k) / nrm); + } + qr.set(k, k, qr.get(k, k) + 1); + for (j = k + 1; j < n; j++) { + s = 0; + for (i = k; i < m; i++) { + s += qr.get(i, k) * qr.get(i, j); + } + s = -s / qr.get(k, k); + for (i = k; i < m; i++) { + qr.set(i, j, qr.get(i, j) + s * qr.get(i, k)); + } + } + } + rdiag[k] = -nrm; + } + + this.QR = qr; + this.Rdiag = rdiag; + } + + /** + * Solve a problem of least square (Ax=b) by using the QR decomposition. Useful when A is rectangular, but not working when A is singular. + * Example : We search to approximate x, with A matrix shape m*n, x vector size n, b vector size m (m > n). We will use : + * var qr = QrDecomposition(A); + * var x = qr.solve(b); + * @param {Matrix} value - Matrix 1D which is the vector b (in the equation Ax = b) + * @return {Matrix} - The vector x + */ + solve(value) { + value = matrix_Matrix.checkMatrix(value); + + var qr = this.QR; + var m = qr.rows; + + if (value.rows !== m) { + throw new Error('Matrix row dimensions must agree'); + } + if (!this.isFullRank()) { + throw new Error('Matrix is rank deficient'); + } + + var count = value.columns; + var X = value.clone(); + var n = qr.columns; + var i, j, k, s; + + for (k = 0; k < n; k++) { + for (j = 0; j < count; j++) { + s = 0; + for (i = k; i < m; i++) { + s += qr[i][k] * X[i][j]; + } + s = -s / qr[k][k]; + for (i = k; i < m; i++) { + X[i][j] += s * qr[i][k]; + } + } + } + for (k = n - 1; k >= 0; k--) { + for (j = 0; j < count; j++) { + X[k][j] /= this.Rdiag[k]; + } + for (i = 0; i < k; i++) { + for (j = 0; j < count; j++) { + X[i][j] -= X[k][j] * qr[i][k]; + } + } + } + + return X.subMatrix(0, n - 1, 0, count - 1); + } + + /** + * + * @return {boolean} + */ + isFullRank() { + var columns = this.QR.columns; + for (var i = 0; i < columns; i++) { + if (this.Rdiag[i] === 0) { + return false; + } + } + return true; + } + + /** + * + * @return {Matrix} + */ + get upperTriangularMatrix() { + var qr = this.QR; + var n = qr.columns; + var X = new matrix_Matrix(n, n); + var i, j; + for (i = 0; i < n; i++) { + for (j = 0; j < n; j++) { + if (i < j) { + X[i][j] = qr[i][j]; + } else if (i === j) { + X[i][j] = this.Rdiag[i]; + } else { + X[i][j] = 0; + } + } + } + return X; + } + + /** + * + * @return {Matrix} + */ + get orthogonalMatrix() { + var qr = this.QR; + var rows = qr.rows; + var columns = qr.columns; + var X = new matrix_Matrix(rows, columns); + var i, j, k, s; + + for (k = columns - 1; k >= 0; k--) { + for (i = 0; i < rows; i++) { + X[i][k] = 0; + } + X[k][k] = 1; + for (j = k; j < columns; j++) { + if (qr[k][k] !== 0) { + s = 0; + for (i = k; i < rows; i++) { + s += qr[i][k] * X[i][j]; + } + + s = -s / qr[k][k]; + + for (i = k; i < rows; i++) { + X[i][j] += s * qr[i][k]; + } + } + } + } + return X; + } +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/decompositions.js + + + + + + +/** + * Computes the inverse of a Matrix + * @param {Matrix} matrix + * @param {boolean} [useSVD=false] + * @return {Matrix} + */ +function inverse(matrix, useSVD = false) { + matrix = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(matrix); + if (useSVD) { + return new svd_SingularValueDecomposition(matrix).inverse(); + } else { + return solve(matrix, matrix_Matrix.eye(matrix.rows)); + } +} + +/** + * + * @param {Matrix} leftHandSide + * @param {Matrix} rightHandSide + * @param {boolean} [useSVD = false] + * @return {Matrix} + */ +function solve(leftHandSide, rightHandSide, useSVD = false) { + leftHandSide = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(leftHandSide); + rightHandSide = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(rightHandSide); + if (useSVD) { + return new svd_SingularValueDecomposition(leftHandSide).solve(rightHandSide); + } else { + return leftHandSide.isSquare() + ? new lu_LuDecomposition(leftHandSide).solve(rightHandSide) + : new qr_QrDecomposition(leftHandSide).solve(rightHandSide); + } +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/linearDependencies.js + + + + + +// function used by rowsDependencies +function xrange(n, exception) { + var range = []; + for (var i = 0; i < n; i++) { + if (i !== exception) { + range.push(i); + } + } + return range; +} + +// function used by rowsDependencies +function dependenciesOneRow( + error, + matrix, + index, + thresholdValue = 10e-10, + thresholdError = 10e-10 +) { + if (error > thresholdError) { + return new Array(matrix.rows + 1).fill(0); + } else { + var returnArray = matrix.addRow(index, [0]); + for (var i = 0; i < returnArray.rows; i++) { + if (Math.abs(returnArray.get(i, 0)) < thresholdValue) { + returnArray.set(i, 0, 0); + } + } + return returnArray.to1DArray(); + } +} + +/** + * Creates a matrix which represents the dependencies between rows. + * If a row is a linear combination of others rows, the result will be a row with the coefficients of this combination. + * For example : for A = [[2, 0, 0, 1], [0, 1, 6, 0], [0, 3, 0, 1], [0, 0, 1, 0], [0, 1, 2, 0]], the result will be [[0, 0, 0, 0, 0], [0, 0, 0, 4, 1], [0, 0, 0, 0, 0], [0, 0.25, 0, 0, -0.25], [0, 1, 0, -4, 0]] + * @param {Matrix} matrix + * @param {Object} [options] includes thresholdValue and thresholdError. + * @param {number} [options.thresholdValue = 10e-10] If an absolute value is inferior to this threshold, it will equals zero. + * @param {number} [options.thresholdError = 10e-10] If the error is inferior to that threshold, the linear combination found is accepted and the row is dependent from other rows. + * @return {Matrix} the matrix which represents the dependencies between rows. + */ + +function linearDependencies(matrix, options = {}) { + const { thresholdValue = 10e-10, thresholdError = 10e-10 } = options; + + var n = matrix.rows; + var results = new matrix_Matrix(n, n); + + for (var i = 0; i < n; i++) { + var b = matrix_Matrix.columnVector(matrix.getRow(i)); + var Abis = matrix.subMatrixRow(xrange(n, i)).transposeView(); + var svd = new svd_SingularValueDecomposition(Abis); + var x = svd.solve(b); + var error = lib_es6( + matrix_Matrix.sub(b, Abis.mmul(x)) + .abs() + .to1DArray() + ); + results.setRow( + i, + dependenciesOneRow(error, x, i, thresholdValue, thresholdError) + ); + } + return results; +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/dc/evd.js + + + + +/** + * @class EigenvalueDecomposition + * @link https://github.com/lutzroeder/Mapack/blob/master/Source/EigenvalueDecomposition.cs + * @param {Matrix} matrix + * @param {object} [options] + * @param {boolean} [options.assumeSymmetric=false] + */ +class evd_EigenvalueDecomposition { + constructor(matrix, options = {}) { + const { assumeSymmetric = false } = options; + + matrix = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(matrix); + if (!matrix.isSquare()) { + throw new Error('Matrix is not a square matrix'); + } + + var n = matrix.columns; + var V = getFilled2DArray(n, n, 0); + var d = new Array(n); + var e = new Array(n); + var value = matrix; + var i, j; + + var isSymmetric = false; + if (assumeSymmetric) { + isSymmetric = true; + } else { + isSymmetric = matrix.isSymmetric(); + } + + if (isSymmetric) { + for (i = 0; i < n; i++) { + for (j = 0; j < n; j++) { + V[i][j] = value.get(i, j); + } + } + tred2(n, e, d, V); + tql2(n, e, d, V); + } else { + var H = getFilled2DArray(n, n, 0); + var ort = new Array(n); + for (j = 0; j < n; j++) { + for (i = 0; i < n; i++) { + H[i][j] = value.get(i, j); + } + } + orthes(n, H, ort, V); + hqr2(n, e, d, V, H); + } + + this.n = n; + this.e = e; + this.d = d; + this.V = V; + } + + /** + * + * @return {Array} + */ + get realEigenvalues() { + return this.d; + } + + /** + * + * @return {Array} + */ + get imaginaryEigenvalues() { + return this.e; + } + + /** + * + * @return {Matrix} + */ + get eigenvectorMatrix() { + if (!matrix_Matrix.isMatrix(this.V)) { + this.V = new matrix_Matrix(this.V); + } + return this.V; + } + + /** + * + * @return {Matrix} + */ + get diagonalMatrix() { + var n = this.n; + var e = this.e; + var d = this.d; + var X = new matrix_Matrix(n, n); + var i, j; + for (i = 0; i < n; i++) { + for (j = 0; j < n; j++) { + X[i][j] = 0; + } + X[i][i] = d[i]; + if (e[i] > 0) { + X[i][i + 1] = e[i]; + } else if (e[i] < 0) { + X[i][i - 1] = e[i]; + } + } + return X; + } +} + +function tred2(n, e, d, V) { + var f, g, h, i, j, k, hh, scale; + + for (j = 0; j < n; j++) { + d[j] = V[n - 1][j]; + } + + for (i = n - 1; i > 0; i--) { + scale = 0; + h = 0; + for (k = 0; k < i; k++) { + scale = scale + Math.abs(d[k]); + } + + if (scale === 0) { + e[i] = d[i - 1]; + for (j = 0; j < i; j++) { + d[j] = V[i - 1][j]; + V[i][j] = 0; + V[j][i] = 0; + } + } else { + for (k = 0; k < i; k++) { + d[k] /= scale; + h += d[k] * d[k]; + } + + f = d[i - 1]; + g = Math.sqrt(h); + if (f > 0) { + g = -g; + } + + e[i] = scale * g; + h = h - f * g; + d[i - 1] = f - g; + for (j = 0; j < i; j++) { + e[j] = 0; + } + + for (j = 0; j < i; j++) { + f = d[j]; + V[j][i] = f; + g = e[j] + V[j][j] * f; + for (k = j + 1; k <= i - 1; k++) { + g += V[k][j] * d[k]; + e[k] += V[k][j] * f; + } + e[j] = g; + } + + f = 0; + for (j = 0; j < i; j++) { + e[j] /= h; + f += e[j] * d[j]; + } + + hh = f / (h + h); + for (j = 0; j < i; j++) { + e[j] -= hh * d[j]; + } + + for (j = 0; j < i; j++) { + f = d[j]; + g = e[j]; + for (k = j; k <= i - 1; k++) { + V[k][j] -= f * e[k] + g * d[k]; + } + d[j] = V[i - 1][j]; + V[i][j] = 0; + } + } + d[i] = h; + } + + for (i = 0; i < n - 1; i++) { + V[n - 1][i] = V[i][i]; + V[i][i] = 1; + h = d[i + 1]; + if (h !== 0) { + for (k = 0; k <= i; k++) { + d[k] = V[k][i + 1] / h; + } + + for (j = 0; j <= i; j++) { + g = 0; + for (k = 0; k <= i; k++) { + g += V[k][i + 1] * V[k][j]; + } + for (k = 0; k <= i; k++) { + V[k][j] -= g * d[k]; + } + } + } + + for (k = 0; k <= i; k++) { + V[k][i + 1] = 0; + } + } + + for (j = 0; j < n; j++) { + d[j] = V[n - 1][j]; + V[n - 1][j] = 0; + } + + V[n - 1][n - 1] = 1; + e[0] = 0; +} + +function tql2(n, e, d, V) { + var g, h, i, j, k, l, m, p, r, dl1, c, c2, c3, el1, s, s2, iter; + + for (i = 1; i < n; i++) { + e[i - 1] = e[i]; + } + + e[n - 1] = 0; + + var f = 0; + var tst1 = 0; + var eps = Number.EPSILON; + + for (l = 0; l < n; l++) { + tst1 = Math.max(tst1, Math.abs(d[l]) + Math.abs(e[l])); + m = l; + while (m < n) { + if (Math.abs(e[m]) <= eps * tst1) { + break; + } + m++; + } + + if (m > l) { + iter = 0; + do { + iter = iter + 1; + + g = d[l]; + p = (d[l + 1] - g) / (2 * e[l]); + r = hypotenuse(p, 1); + if (p < 0) { + r = -r; + } + + d[l] = e[l] / (p + r); + d[l + 1] = e[l] * (p + r); + dl1 = d[l + 1]; + h = g - d[l]; + for (i = l + 2; i < n; i++) { + d[i] -= h; + } + + f = f + h; + + p = d[m]; + c = 1; + c2 = c; + c3 = c; + el1 = e[l + 1]; + s = 0; + s2 = 0; + for (i = m - 1; i >= l; i--) { + c3 = c2; + c2 = c; + s2 = s; + g = c * e[i]; + h = c * p; + r = hypotenuse(p, e[i]); + e[i + 1] = s * r; + s = e[i] / r; + c = p / r; + p = c * d[i] - s * g; + d[i + 1] = h + s * (c * g + s * d[i]); + + for (k = 0; k < n; k++) { + h = V[k][i + 1]; + V[k][i + 1] = s * V[k][i] + c * h; + V[k][i] = c * V[k][i] - s * h; + } + } + + p = -s * s2 * c3 * el1 * e[l] / dl1; + e[l] = s * p; + d[l] = c * p; + } while (Math.abs(e[l]) > eps * tst1); + } + d[l] = d[l] + f; + e[l] = 0; + } + + for (i = 0; i < n - 1; i++) { + k = i; + p = d[i]; + for (j = i + 1; j < n; j++) { + if (d[j] < p) { + k = j; + p = d[j]; + } + } + + if (k !== i) { + d[k] = d[i]; + d[i] = p; + for (j = 0; j < n; j++) { + p = V[j][i]; + V[j][i] = V[j][k]; + V[j][k] = p; + } + } + } +} + +function orthes(n, H, ort, V) { + var low = 0; + var high = n - 1; + var f, g, h, i, j, m; + var scale; + + for (m = low + 1; m <= high - 1; m++) { + scale = 0; + for (i = m; i <= high; i++) { + scale = scale + Math.abs(H[i][m - 1]); + } + + if (scale !== 0) { + h = 0; + for (i = high; i >= m; i--) { + ort[i] = H[i][m - 1] / scale; + h += ort[i] * ort[i]; + } + + g = Math.sqrt(h); + if (ort[m] > 0) { + g = -g; + } + + h = h - ort[m] * g; + ort[m] = ort[m] - g; + + for (j = m; j < n; j++) { + f = 0; + for (i = high; i >= m; i--) { + f += ort[i] * H[i][j]; + } + + f = f / h; + for (i = m; i <= high; i++) { + H[i][j] -= f * ort[i]; + } + } + + for (i = 0; i <= high; i++) { + f = 0; + for (j = high; j >= m; j--) { + f += ort[j] * H[i][j]; + } + + f = f / h; + for (j = m; j <= high; j++) { + H[i][j] -= f * ort[j]; + } + } + + ort[m] = scale * ort[m]; + H[m][m - 1] = scale * g; + } + } + + for (i = 0; i < n; i++) { + for (j = 0; j < n; j++) { + V[i][j] = i === j ? 1 : 0; + } + } + + for (m = high - 1; m >= low + 1; m--) { + if (H[m][m - 1] !== 0) { + for (i = m + 1; i <= high; i++) { + ort[i] = H[i][m - 1]; + } + + for (j = m; j <= high; j++) { + g = 0; + for (i = m; i <= high; i++) { + g += ort[i] * V[i][j]; + } + + g = g / ort[m] / H[m][m - 1]; + for (i = m; i <= high; i++) { + V[i][j] += g * ort[i]; + } + } + } + } +} + +function hqr2(nn, e, d, V, H) { + var n = nn - 1; + var low = 0; + var high = nn - 1; + var eps = Number.EPSILON; + var exshift = 0; + var norm = 0; + var p = 0; + var q = 0; + var r = 0; + var s = 0; + var z = 0; + var iter = 0; + var i, j, k, l, m, t, w, x, y; + var ra, sa, vr, vi; + var notlast, cdivres; + + for (i = 0; i < nn; i++) { + if (i < low || i > high) { + d[i] = H[i][i]; + e[i] = 0; + } + + for (j = Math.max(i - 1, 0); j < nn; j++) { + norm = norm + Math.abs(H[i][j]); + } + } + + while (n >= low) { + l = n; + while (l > low) { + s = Math.abs(H[l - 1][l - 1]) + Math.abs(H[l][l]); + if (s === 0) { + s = norm; + } + if (Math.abs(H[l][l - 1]) < eps * s) { + break; + } + l--; + } + + if (l === n) { + H[n][n] = H[n][n] + exshift; + d[n] = H[n][n]; + e[n] = 0; + n--; + iter = 0; + } else if (l === n - 1) { + w = H[n][n - 1] * H[n - 1][n]; + p = (H[n - 1][n - 1] - H[n][n]) / 2; + q = p * p + w; + z = Math.sqrt(Math.abs(q)); + H[n][n] = H[n][n] + exshift; + H[n - 1][n - 1] = H[n - 1][n - 1] + exshift; + x = H[n][n]; + + if (q >= 0) { + z = p >= 0 ? p + z : p - z; + d[n - 1] = x + z; + d[n] = d[n - 1]; + if (z !== 0) { + d[n] = x - w / z; + } + e[n - 1] = 0; + e[n] = 0; + x = H[n][n - 1]; + s = Math.abs(x) + Math.abs(z); + p = x / s; + q = z / s; + r = Math.sqrt(p * p + q * q); + p = p / r; + q = q / r; + + for (j = n - 1; j < nn; j++) { + z = H[n - 1][j]; + H[n - 1][j] = q * z + p * H[n][j]; + H[n][j] = q * H[n][j] - p * z; + } + + for (i = 0; i <= n; i++) { + z = H[i][n - 1]; + H[i][n - 1] = q * z + p * H[i][n]; + H[i][n] = q * H[i][n] - p * z; + } + + for (i = low; i <= high; i++) { + z = V[i][n - 1]; + V[i][n - 1] = q * z + p * V[i][n]; + V[i][n] = q * V[i][n] - p * z; + } + } else { + d[n - 1] = x + p; + d[n] = x + p; + e[n - 1] = z; + e[n] = -z; + } + + n = n - 2; + iter = 0; + } else { + x = H[n][n]; + y = 0; + w = 0; + if (l < n) { + y = H[n - 1][n - 1]; + w = H[n][n - 1] * H[n - 1][n]; + } + + if (iter === 10) { + exshift += x; + for (i = low; i <= n; i++) { + H[i][i] -= x; + } + s = Math.abs(H[n][n - 1]) + Math.abs(H[n - 1][n - 2]); + x = y = 0.75 * s; + w = -0.4375 * s * s; + } + + if (iter === 30) { + s = (y - x) / 2; + s = s * s + w; + if (s > 0) { + s = Math.sqrt(s); + if (y < x) { + s = -s; + } + s = x - w / ((y - x) / 2 + s); + for (i = low; i <= n; i++) { + H[i][i] -= s; + } + exshift += s; + x = y = w = 0.964; + } + } + + iter = iter + 1; + + m = n - 2; + while (m >= l) { + z = H[m][m]; + r = x - z; + s = y - z; + p = (r * s - w) / H[m + 1][m] + H[m][m + 1]; + q = H[m + 1][m + 1] - z - r - s; + r = H[m + 2][m + 1]; + s = Math.abs(p) + Math.abs(q) + Math.abs(r); + p = p / s; + q = q / s; + r = r / s; + if (m === l) { + break; + } + if ( + Math.abs(H[m][m - 1]) * (Math.abs(q) + Math.abs(r)) < + eps * + (Math.abs(p) * + (Math.abs(H[m - 1][m - 1]) + + Math.abs(z) + + Math.abs(H[m + 1][m + 1]))) + ) { + break; + } + m--; + } + + for (i = m + 2; i <= n; i++) { + H[i][i - 2] = 0; + if (i > m + 2) { + H[i][i - 3] = 0; + } + } + + for (k = m; k <= n - 1; k++) { + notlast = k !== n - 1; + if (k !== m) { + p = H[k][k - 1]; + q = H[k + 1][k - 1]; + r = notlast ? H[k + 2][k - 1] : 0; + x = Math.abs(p) + Math.abs(q) + Math.abs(r); + if (x !== 0) { + p = p / x; + q = q / x; + r = r / x; + } + } + + if (x === 0) { + break; + } + + s = Math.sqrt(p * p + q * q + r * r); + if (p < 0) { + s = -s; + } + + if (s !== 0) { + if (k !== m) { + H[k][k - 1] = -s * x; + } else if (l !== m) { + H[k][k - 1] = -H[k][k - 1]; + } + + p = p + s; + x = p / s; + y = q / s; + z = r / s; + q = q / p; + r = r / p; + + for (j = k; j < nn; j++) { + p = H[k][j] + q * H[k + 1][j]; + if (notlast) { + p = p + r * H[k + 2][j]; + H[k + 2][j] = H[k + 2][j] - p * z; + } + + H[k][j] = H[k][j] - p * x; + H[k + 1][j] = H[k + 1][j] - p * y; + } + + for (i = 0; i <= Math.min(n, k + 3); i++) { + p = x * H[i][k] + y * H[i][k + 1]; + if (notlast) { + p = p + z * H[i][k + 2]; + H[i][k + 2] = H[i][k + 2] - p * r; + } + + H[i][k] = H[i][k] - p; + H[i][k + 1] = H[i][k + 1] - p * q; + } + + for (i = low; i <= high; i++) { + p = x * V[i][k] + y * V[i][k + 1]; + if (notlast) { + p = p + z * V[i][k + 2]; + V[i][k + 2] = V[i][k + 2] - p * r; + } + + V[i][k] = V[i][k] - p; + V[i][k + 1] = V[i][k + 1] - p * q; + } + } + } + } + } + + if (norm === 0) { + return; + } + + for (n = nn - 1; n >= 0; n--) { + p = d[n]; + q = e[n]; + + if (q === 0) { + l = n; + H[n][n] = 1; + for (i = n - 1; i >= 0; i--) { + w = H[i][i] - p; + r = 0; + for (j = l; j <= n; j++) { + r = r + H[i][j] * H[j][n]; + } + + if (e[i] < 0) { + z = w; + s = r; + } else { + l = i; + if (e[i] === 0) { + H[i][n] = w !== 0 ? -r / w : -r / (eps * norm); + } else { + x = H[i][i + 1]; + y = H[i + 1][i]; + q = (d[i] - p) * (d[i] - p) + e[i] * e[i]; + t = (x * s - z * r) / q; + H[i][n] = t; + H[i + 1][n] = + Math.abs(x) > Math.abs(z) ? (-r - w * t) / x : (-s - y * t) / z; + } + + t = Math.abs(H[i][n]); + if (eps * t * t > 1) { + for (j = i; j <= n; j++) { + H[j][n] = H[j][n] / t; + } + } + } + } + } else if (q < 0) { + l = n - 1; + + if (Math.abs(H[n][n - 1]) > Math.abs(H[n - 1][n])) { + H[n - 1][n - 1] = q / H[n][n - 1]; + H[n - 1][n] = -(H[n][n] - p) / H[n][n - 1]; + } else { + cdivres = cdiv(0, -H[n - 1][n], H[n - 1][n - 1] - p, q); + H[n - 1][n - 1] = cdivres[0]; + H[n - 1][n] = cdivres[1]; + } + + H[n][n - 1] = 0; + H[n][n] = 1; + for (i = n - 2; i >= 0; i--) { + ra = 0; + sa = 0; + for (j = l; j <= n; j++) { + ra = ra + H[i][j] * H[j][n - 1]; + sa = sa + H[i][j] * H[j][n]; + } + + w = H[i][i] - p; + + if (e[i] < 0) { + z = w; + r = ra; + s = sa; + } else { + l = i; + if (e[i] === 0) { + cdivres = cdiv(-ra, -sa, w, q); + H[i][n - 1] = cdivres[0]; + H[i][n] = cdivres[1]; + } else { + x = H[i][i + 1]; + y = H[i + 1][i]; + vr = (d[i] - p) * (d[i] - p) + e[i] * e[i] - q * q; + vi = (d[i] - p) * 2 * q; + if (vr === 0 && vi === 0) { + vr = + eps * + norm * + (Math.abs(w) + + Math.abs(q) + + Math.abs(x) + + Math.abs(y) + + Math.abs(z)); + } + cdivres = cdiv( + x * r - z * ra + q * sa, + x * s - z * sa - q * ra, + vr, + vi + ); + H[i][n - 1] = cdivres[0]; + H[i][n] = cdivres[1]; + if (Math.abs(x) > Math.abs(z) + Math.abs(q)) { + H[i + 1][n - 1] = (-ra - w * H[i][n - 1] + q * H[i][n]) / x; + H[i + 1][n] = (-sa - w * H[i][n] - q * H[i][n - 1]) / x; + } else { + cdivres = cdiv(-r - y * H[i][n - 1], -s - y * H[i][n], z, q); + H[i + 1][n - 1] = cdivres[0]; + H[i + 1][n] = cdivres[1]; + } + } + + t = Math.max(Math.abs(H[i][n - 1]), Math.abs(H[i][n])); + if (eps * t * t > 1) { + for (j = i; j <= n; j++) { + H[j][n - 1] = H[j][n - 1] / t; + H[j][n] = H[j][n] / t; + } + } + } + } + } + } + + for (i = 0; i < nn; i++) { + if (i < low || i > high) { + for (j = i; j < nn; j++) { + V[i][j] = H[i][j]; + } + } + } + + for (j = nn - 1; j >= low; j--) { + for (i = low; i <= high; i++) { + z = 0; + for (k = low; k <= Math.min(j, high); k++) { + z = z + V[i][k] * H[k][j]; + } + V[i][j] = z; + } + } +} + +function cdiv(xr, xi, yr, yi) { + var r, d; + if (Math.abs(yr) > Math.abs(yi)) { + r = yi / yr; + d = yr + r * yi; + return [(xr + r * xi) / d, (xi - r * xr) / d]; + } else { + r = yr / yi; + d = yi + r * yr; + return [(r * xr + xi) / d, (r * xi - xr) / d]; + } +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/dc/cholesky.js + + +/** + * @class CholeskyDecomposition + * @link https://github.com/lutzroeder/Mapack/blob/master/Source/CholeskyDecomposition.cs + * @param {Matrix} value + */ +class cholesky_CholeskyDecomposition { + constructor(value) { + value = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(value); + if (!value.isSymmetric()) { + throw new Error('Matrix is not symmetric'); + } + + var a = value; + var dimension = a.rows; + var l = new matrix_Matrix(dimension, dimension); + var positiveDefinite = true; + var i, j, k; + + for (j = 0; j < dimension; j++) { + var Lrowj = l[j]; + var d = 0; + for (k = 0; k < j; k++) { + var Lrowk = l[k]; + var s = 0; + for (i = 0; i < k; i++) { + s += Lrowk[i] * Lrowj[i]; + } + Lrowj[k] = s = (a.get(j, k) - s) / l[k][k]; + d = d + s * s; + } + + d = a.get(j, j) - d; + + positiveDefinite &= d > 0; + l[j][j] = Math.sqrt(Math.max(d, 0)); + for (k = j + 1; k < dimension; k++) { + l[j][k] = 0; + } + } + + if (!positiveDefinite) { + throw new Error('Matrix is not positive definite'); + } + + this.L = l; + } + + /** + * + * @param {Matrix} value + * @return {Matrix} + */ + solve(value) { + value = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(value); + + var l = this.L; + var dimension = l.rows; + + if (value.rows !== dimension) { + throw new Error('Matrix dimensions do not match'); + } + + var count = value.columns; + var B = value.clone(); + var i, j, k; + + for (k = 0; k < dimension; k++) { + for (j = 0; j < count; j++) { + for (i = 0; i < k; i++) { + B[k][j] -= B[i][j] * l[k][i]; + } + B[k][j] /= l[k][k]; + } + } + + for (k = dimension - 1; k >= 0; k--) { + for (j = 0; j < count; j++) { + for (i = k + 1; i < dimension; i++) { + B[k][j] -= B[i][j] * l[i][k]; + } + B[k][j] /= l[k][k]; + } + } + + return B; + } + + /** + * + * @return {Matrix} + */ + get lowerTriangularMatrix() { + return this.L; + } +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/index.js + + + + + + + + + + + + + + + +// CONCATENATED MODULE: ./node_modules/ml-levenberg-marquardt/src/step.js + + /** * Difference of the matrix function over the parameters * @ignore @@ -2023,7 +6714,7 @@ function gradientFunction( ans[param][point] = evaluatedData[point] - funcParam(data.x[point]); } } - return new mlMatrix.Matrix(ans); + return new matrix_Matrix(ans); } /** @@ -2039,10 +6730,10 @@ function matrixFunction(data, evaluatedData) { var ans = new Array(m); for (var point = 0; point < m; point++) { - ans[point] = data.y[point] - evaluatedData[point]; + ans[point] = [data.y[point] - evaluatedData[point]]; } - return new mlMatrix.Matrix([ans]); + return new matrix_Matrix(ans); } /** @@ -2062,16 +6753,12 @@ function step( gradientDifference, parameterizedFunction ) { - var identity = mlMatrix.Matrix.eye(params.length).mul( - damping * gradientDifference * gradientDifference - ); + var value = damping * gradientDifference * gradientDifference; + var identity = matrix_Matrix.eye(params.length, params.length, value); - var l = data.x.length; - var evaluatedData = new Array(l); const func = parameterizedFunction(params); - for (var i = 0; i < l; i++) { - evaluatedData[i] = func(data.x[i]); - } + var evaluatedData = data.x.map((e) => func(e)); + var gradientFunc = gradientFunction( data, evaluatedData, @@ -2079,22 +6766,28 @@ function step( gradientDifference, parameterizedFunction ); - var matrixFunc = matrixFunction(data, evaluatedData).transposeView(); - var inverseMatrix = mlMatrix.inverse( - identity.add(gradientFunc.mmul(gradientFunc.transposeView())) + var matrixFunc = matrixFunction(data, evaluatedData); + var inverseMatrix = inverse( + identity.add(gradientFunc.mmul(gradientFunc.transpose())) ); - params = new mlMatrix.Matrix([params]); + + params = new matrix_Matrix([params]); params = params.sub( inverseMatrix .mmul(gradientFunc) .mmul(matrixFunc) .mul(gradientDifference) - .transposeView() + .transpose() ); return params.to1DArray(); } +// CONCATENATED MODULE: ./node_modules/ml-levenberg-marquardt/src/index.js +/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "default", function() { return levenbergMarquardt; }); + + + /** * Curve fitting algorithm * @param {{x:Array, y:Array}} data - Array of points to fit in the format [x1, x2, ... ], [y1, y2, ... ] @@ -2102,6 +6795,8 @@ function step( * @param {object} [options] - Options object * @param {number} [options.damping] - Levenberg-Marquardt parameter * @param {number} [options.gradientDifference = 10e-2] - Adjustment for decrease the damping parameter + * @param {Array} [options.minValues] - Minimum allowed values for parameters + * @param {Array} [options.maxValues] - Maximum allowed values for parameters * @param {Array} [options.initialValues] - Array of initial parameter values * @param {number} [options.maxIterations = 100] - Maximum of allowed iterations * @param {number} [options.errorTolerance = 10e-3] - Minimum uncertainty allowed for each point @@ -2117,6 +6812,8 @@ function levenbergMarquardt( gradientDifference = 10e-2, damping = 0, errorTolerance = 10e-3, + minValues, + maxValues, initialValues } = options; @@ -2133,15 +6830,19 @@ function levenbergMarquardt( throw new Error( 'The data parameter elements must be an array with more than 2 points' ); - } else { - let dataLen = data.x.length; - if (dataLen !== data.y.length) { - throw new Error('The data parameter elements must have the same size'); - } + } else if (data.x.length !== data.y.length) { + throw new Error('The data parameter elements must have the same size'); } var parameters = initialValues || new Array(parameterizedFunction.length).fill(1); + let parLen = parameters.length; + maxValues = maxValues || new Array(parLen).fill(Number.MAX_SAFE_INTEGER); + minValues = minValues || new Array(parLen).fill(Number.MIN_SAFE_INTEGER); + + if (maxValues.length !== minValues.length) { + throw new Error('minValues and maxValues must be the same size'); + } if (!Array.isArray(parameters)) { throw new Error('initialValues must be an array'); @@ -2163,7 +6864,16 @@ function levenbergMarquardt( gradientDifference, parameterizedFunction ); + + for (let k = 0; k < parLen; k++) { + parameters[k] = Math.min( + Math.max(minValues[k], parameters[k]), + maxValues[k] + ); + } + error = errorCalculation(data, parameters, parameterizedFunction); + if (isNaN(error)) break; converged = error <= errorTolerance; } @@ -2174,4663 +6884,6 @@ function levenbergMarquardt( }; } -module.exports = levenbergMarquardt; - - -/***/ }), -/* 9 */ -/***/ (function(module, __webpack_exports__, __webpack_require__) { - -"use strict"; -__webpack_require__.r(__webpack_exports__); - -// EXTERNAL MODULE: ./node_modules/is-any-array/src/index.js -var src = __webpack_require__(0); -var src_default = /*#__PURE__*/__webpack_require__.n(src); - -// CONCATENATED MODULE: ./node_modules/ml-array-max/lib-es6/index.js - - -/** - * Computes the maximum of the given values - * @param {Array} input - * @return {number} - */ - -function lib_es6_max(input) { - if (!src_default()(input)) { - throw new TypeError('input must be an array'); - } - - if (input.length === 0) { - throw new TypeError('input must not be empty'); - } - - var max = input[0]; - - for (var i = 1; i < input.length; i++) { - if (input[i] > max) max = input[i]; - } - - return max; -} - -/* harmony default export */ var lib_es6 = (lib_es6_max); - -// CONCATENATED MODULE: ./node_modules/ml-array-min/lib-es6/index.js - - -/** - * Computes the minimum of the given values - * @param {Array} input - * @return {number} - */ - -function lib_es6_min(input) { - if (!src_default()(input)) { - throw new TypeError('input must be an array'); - } - - if (input.length === 0) { - throw new TypeError('input must not be empty'); - } - - var min = input[0]; - - for (var i = 1; i < input.length; i++) { - if (input[i] < min) min = input[i]; - } - - return min; -} - -/* harmony default export */ var ml_array_min_lib_es6 = (lib_es6_min); - -// CONCATENATED MODULE: ./node_modules/ml-array-rescale/lib-es6/index.js - - - - -function rescale(input) { - var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; - - if (!src_default()(input)) { - throw new TypeError('input must be an array'); - } else if (input.length === 0) { - throw new TypeError('input must not be empty'); - } - - var output; - - if (options.output !== undefined) { - if (!src_default()(options.output)) { - throw new TypeError('output option must be an array if specified'); - } - - output = options.output; - } else { - output = new Array(input.length); - } - - var currentMin = ml_array_min_lib_es6(input); - var currentMax = lib_es6(input); - - if (currentMin === currentMax) { - throw new RangeError('minimum and maximum input values are equal. Cannot rescale a constant array'); - } - - var _options$min = options.min, - minValue = _options$min === void 0 ? options.autoMinMax ? currentMin : 0 : _options$min, - _options$max = options.max, - maxValue = _options$max === void 0 ? options.autoMinMax ? currentMax : 1 : _options$max; - - if (minValue >= maxValue) { - throw new RangeError('min option must be smaller than max option'); - } - - var factor = (maxValue - minValue) / (currentMax - currentMin); - - for (var i = 0; i < input.length; i++) { - output[i] = (input[i] - currentMin) * factor + minValue; - } - - return output; -} - -/* harmony default export */ var ml_array_rescale_lib_es6 = (rescale); - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/dc/lu.js - - -/** - * @class LuDecomposition - * @link https://github.com/lutzroeder/Mapack/blob/master/Source/LuDecomposition.cs - * @param {Matrix} matrix - */ -class lu_LuDecomposition { - constructor(matrix) { - matrix = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(matrix); - - var lu = matrix.clone(); - var rows = lu.rows; - var columns = lu.columns; - var pivotVector = new Array(rows); - var pivotSign = 1; - var i, j, k, p, s, t, v; - var LUcolj, kmax; - - for (i = 0; i < rows; i++) { - pivotVector[i] = i; - } - - LUcolj = new Array(rows); - - for (j = 0; j < columns; j++) { - for (i = 0; i < rows; i++) { - LUcolj[i] = lu.get(i, j); - } - - for (i = 0; i < rows; i++) { - kmax = Math.min(i, j); - s = 0; - for (k = 0; k < kmax; k++) { - s += lu.get(i, k) * LUcolj[k]; - } - LUcolj[i] -= s; - lu.set(i, j, LUcolj[i]); - } - - p = j; - for (i = j + 1; i < rows; i++) { - if (Math.abs(LUcolj[i]) > Math.abs(LUcolj[p])) { - p = i; - } - } - - if (p !== j) { - for (k = 0; k < columns; k++) { - t = lu.get(p, k); - lu.set(p, k, lu.get(j, k)); - lu.set(j, k, t); - } - - v = pivotVector[p]; - pivotVector[p] = pivotVector[j]; - pivotVector[j] = v; - - pivotSign = -pivotSign; - } - - if (j < rows && lu.get(j, j) !== 0) { - for (i = j + 1; i < rows; i++) { - lu.set(i, j, lu.get(i, j) / lu.get(j, j)); - } - } - } - - this.LU = lu; - this.pivotVector = pivotVector; - this.pivotSign = pivotSign; - } - - /** - * - * @return {boolean} - */ - isSingular() { - var data = this.LU; - var col = data.columns; - for (var j = 0; j < col; j++) { - if (data[j][j] === 0) { - return true; - } - } - return false; - } - - /** - * - * @param {Matrix} value - * @return {Matrix} - */ - solve(value) { - value = matrix_Matrix.checkMatrix(value); - - var lu = this.LU; - var rows = lu.rows; - - if (rows !== value.rows) { - throw new Error('Invalid matrix dimensions'); - } - if (this.isSingular()) { - throw new Error('LU matrix is singular'); - } - - var count = value.columns; - var X = value.subMatrixRow(this.pivotVector, 0, count - 1); - var columns = lu.columns; - var i, j, k; - - for (k = 0; k < columns; k++) { - for (i = k + 1; i < columns; i++) { - for (j = 0; j < count; j++) { - X[i][j] -= X[k][j] * lu[i][k]; - } - } - } - for (k = columns - 1; k >= 0; k--) { - for (j = 0; j < count; j++) { - X[k][j] /= lu[k][k]; - } - for (i = 0; i < k; i++) { - for (j = 0; j < count; j++) { - X[i][j] -= X[k][j] * lu[i][k]; - } - } - } - return X; - } - - /** - * - * @return {number} - */ - get determinant() { - var data = this.LU; - if (!data.isSquare()) { - throw new Error('Matrix must be square'); - } - var determinant = this.pivotSign; - var col = data.columns; - for (var j = 0; j < col; j++) { - determinant *= data[j][j]; - } - return determinant; - } - - /** - * - * @return {Matrix} - */ - get lowerTriangularMatrix() { - var data = this.LU; - var rows = data.rows; - var columns = data.columns; - var X = new matrix_Matrix(rows, columns); - for (var i = 0; i < rows; i++) { - for (var j = 0; j < columns; j++) { - if (i > j) { - X[i][j] = data[i][j]; - } else if (i === j) { - X[i][j] = 1; - } else { - X[i][j] = 0; - } - } - } - return X; - } - - /** - * - * @return {Matrix} - */ - get upperTriangularMatrix() { - var data = this.LU; - var rows = data.rows; - var columns = data.columns; - var X = new matrix_Matrix(rows, columns); - for (var i = 0; i < rows; i++) { - for (var j = 0; j < columns; j++) { - if (i <= j) { - X[i][j] = data[i][j]; - } else { - X[i][j] = 0; - } - } - } - return X; - } - - /** - * - * @return {Array} - */ - get pivotPermutationVector() { - return this.pivotVector.slice(); - } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/dc/util.js -function hypotenuse(a, b) { - var r = 0; - if (Math.abs(a) > Math.abs(b)) { - r = b / a; - return Math.abs(a) * Math.sqrt(1 + r * r); - } - if (b !== 0) { - r = a / b; - return Math.abs(b) * Math.sqrt(1 + r * r); - } - return 0; -} - -function getFilled2DArray(rows, columns, value) { - var array = new Array(rows); - for (var i = 0; i < rows; i++) { - array[i] = new Array(columns); - for (var j = 0; j < columns; j++) { - array[i][j] = value; - } - } - return array; -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/dc/svd.js - - - - -/** - * @class SingularValueDecomposition - * @see https://github.com/accord-net/framework/blob/development/Sources/Accord.Math/Decompositions/SingularValueDecomposition.cs - * @param {Matrix} value - * @param {object} [options] - * @param {boolean} [options.computeLeftSingularVectors=true] - * @param {boolean} [options.computeRightSingularVectors=true] - * @param {boolean} [options.autoTranspose=false] - */ -class svd_SingularValueDecomposition { - constructor(value, options = {}) { - value = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(value); - - var m = value.rows; - var n = value.columns; - - const { - computeLeftSingularVectors = true, - computeRightSingularVectors = true, - autoTranspose = false - } = options; - - var wantu = Boolean(computeLeftSingularVectors); - var wantv = Boolean(computeRightSingularVectors); - - var swapped = false; - var a; - if (m < n) { - if (!autoTranspose) { - a = value.clone(); - // eslint-disable-next-line no-console - console.warn( - 'Computing SVD on a matrix with more columns than rows. Consider enabling autoTranspose' - ); - } else { - a = value.transpose(); - m = a.rows; - n = a.columns; - swapped = true; - var aux = wantu; - wantu = wantv; - wantv = aux; - } - } else { - a = value.clone(); - } - - var nu = Math.min(m, n); - var ni = Math.min(m + 1, n); - var s = new Array(ni); - var U = getFilled2DArray(m, nu, 0); - var V = getFilled2DArray(n, n, 0); - - var e = new Array(n); - var work = new Array(m); - - var si = new Array(ni); - for (let i = 0; i < ni; i++) si[i] = i; - - var nct = Math.min(m - 1, n); - var nrt = Math.max(0, Math.min(n - 2, m)); - var mrc = Math.max(nct, nrt); - - for (let k = 0; k < mrc; k++) { - if (k < nct) { - s[k] = 0; - for (let i = k; i < m; i++) { - s[k] = hypotenuse(s[k], a[i][k]); - } - if (s[k] !== 0) { - if (a[k][k] < 0) { - s[k] = -s[k]; - } - for (let i = k; i < m; i++) { - a[i][k] /= s[k]; - } - a[k][k] += 1; - } - s[k] = -s[k]; - } - - for (let j = k + 1; j < n; j++) { - if (k < nct && s[k] !== 0) { - let t = 0; - for (let i = k; i < m; i++) { - t += a[i][k] * a[i][j]; - } - t = -t / a[k][k]; - for (let i = k; i < m; i++) { - a[i][j] += t * a[i][k]; - } - } - e[j] = a[k][j]; - } - - if (wantu && k < nct) { - for (let i = k; i < m; i++) { - U[i][k] = a[i][k]; - } - } - - if (k < nrt) { - e[k] = 0; - for (let i = k + 1; i < n; i++) { - e[k] = hypotenuse(e[k], e[i]); - } - if (e[k] !== 0) { - if (e[k + 1] < 0) { - e[k] = 0 - e[k]; - } - for (let i = k + 1; i < n; i++) { - e[i] /= e[k]; - } - e[k + 1] += 1; - } - e[k] = -e[k]; - if (k + 1 < m && e[k] !== 0) { - for (let i = k + 1; i < m; i++) { - work[i] = 0; - } - for (let i = k + 1; i < m; i++) { - for (let j = k + 1; j < n; j++) { - work[i] += e[j] * a[i][j]; - } - } - for (let j = k + 1; j < n; j++) { - let t = -e[j] / e[k + 1]; - for (let i = k + 1; i < m; i++) { - a[i][j] += t * work[i]; - } - } - } - if (wantv) { - for (let i = k + 1; i < n; i++) { - V[i][k] = e[i]; - } - } - } - } - - let p = Math.min(n, m + 1); - if (nct < n) { - s[nct] = a[nct][nct]; - } - if (m < p) { - s[p - 1] = 0; - } - if (nrt + 1 < p) { - e[nrt] = a[nrt][p - 1]; - } - e[p - 1] = 0; - - if (wantu) { - for (let j = nct; j < nu; j++) { - for (let i = 0; i < m; i++) { - U[i][j] = 0; - } - U[j][j] = 1; - } - for (let k = nct - 1; k >= 0; k--) { - if (s[k] !== 0) { - for (let j = k + 1; j < nu; j++) { - let t = 0; - for (let i = k; i < m; i++) { - t += U[i][k] * U[i][j]; - } - t = -t / U[k][k]; - for (let i = k; i < m; i++) { - U[i][j] += t * U[i][k]; - } - } - for (let i = k; i < m; i++) { - U[i][k] = -U[i][k]; - } - U[k][k] = 1 + U[k][k]; - for (let i = 0; i < k - 1; i++) { - U[i][k] = 0; - } - } else { - for (let i = 0; i < m; i++) { - U[i][k] = 0; - } - U[k][k] = 1; - } - } - } - - if (wantv) { - for (let k = n - 1; k >= 0; k--) { - if (k < nrt && e[k] !== 0) { - for (let j = k + 1; j < n; j++) { - let t = 0; - for (let i = k + 1; i < n; i++) { - t += V[i][k] * V[i][j]; - } - t = -t / V[k + 1][k]; - for (let i = k + 1; i < n; i++) { - V[i][j] += t * V[i][k]; - } - } - } - for (let i = 0; i < n; i++) { - V[i][k] = 0; - } - V[k][k] = 1; - } - } - - var pp = p - 1; - var iter = 0; - var eps = Number.EPSILON; - while (p > 0) { - let k, kase; - for (k = p - 2; k >= -1; k--) { - if (k === -1) { - break; - } - const alpha = - Number.MIN_VALUE + eps * Math.abs(s[k] + Math.abs(s[k + 1])); - if (Math.abs(e[k]) <= alpha || Number.isNaN(e[k])) { - e[k] = 0; - break; - } - } - if (k === p - 2) { - kase = 4; - } else { - let ks; - for (ks = p - 1; ks >= k; ks--) { - if (ks === k) { - break; - } - let t = - (ks !== p ? Math.abs(e[ks]) : 0) + - (ks !== k + 1 ? Math.abs(e[ks - 1]) : 0); - if (Math.abs(s[ks]) <= eps * t) { - s[ks] = 0; - break; - } - } - if (ks === k) { - kase = 3; - } else if (ks === p - 1) { - kase = 1; - } else { - kase = 2; - k = ks; - } - } - - k++; - - switch (kase) { - case 1: { - let f = e[p - 2]; - e[p - 2] = 0; - for (let j = p - 2; j >= k; j--) { - let t = hypotenuse(s[j], f); - let cs = s[j] / t; - let sn = f / t; - s[j] = t; - if (j !== k) { - f = -sn * e[j - 1]; - e[j - 1] = cs * e[j - 1]; - } - if (wantv) { - for (let i = 0; i < n; i++) { - t = cs * V[i][j] + sn * V[i][p - 1]; - V[i][p - 1] = -sn * V[i][j] + cs * V[i][p - 1]; - V[i][j] = t; - } - } - } - break; - } - case 2: { - let f = e[k - 1]; - e[k - 1] = 0; - for (let j = k; j < p; j++) { - let t = hypotenuse(s[j], f); - let cs = s[j] / t; - let sn = f / t; - s[j] = t; - f = -sn * e[j]; - e[j] = cs * e[j]; - if (wantu) { - for (let i = 0; i < m; i++) { - t = cs * U[i][j] + sn * U[i][k - 1]; - U[i][k - 1] = -sn * U[i][j] + cs * U[i][k - 1]; - U[i][j] = t; - } - } - } - break; - } - case 3: { - const scale = Math.max( - Math.abs(s[p - 1]), - Math.abs(s[p - 2]), - Math.abs(e[p - 2]), - Math.abs(s[k]), - Math.abs(e[k]) - ); - const sp = s[p - 1] / scale; - const spm1 = s[p - 2] / scale; - const epm1 = e[p - 2] / scale; - const sk = s[k] / scale; - const ek = e[k] / scale; - const b = ((spm1 + sp) * (spm1 - sp) + epm1 * epm1) / 2; - const c = sp * epm1 * (sp * epm1); - let shift = 0; - if (b !== 0 || c !== 0) { - if (b < 0) { - shift = 0 - Math.sqrt(b * b + c); - } else { - shift = Math.sqrt(b * b + c); - } - shift = c / (b + shift); - } - let f = (sk + sp) * (sk - sp) + shift; - let g = sk * ek; - for (let j = k; j < p - 1; j++) { - let t = hypotenuse(f, g); - if (t === 0) t = Number.MIN_VALUE; - let cs = f / t; - let sn = g / t; - if (j !== k) { - e[j - 1] = t; - } - f = cs * s[j] + sn * e[j]; - e[j] = cs * e[j] - sn * s[j]; - g = sn * s[j + 1]; - s[j + 1] = cs * s[j + 1]; - if (wantv) { - for (let i = 0; i < n; i++) { - t = cs * V[i][j] + sn * V[i][j + 1]; - V[i][j + 1] = -sn * V[i][j] + cs * V[i][j + 1]; - V[i][j] = t; - } - } - t = hypotenuse(f, g); - if (t === 0) t = Number.MIN_VALUE; - cs = f / t; - sn = g / t; - s[j] = t; - f = cs * e[j] + sn * s[j + 1]; - s[j + 1] = -sn * e[j] + cs * s[j + 1]; - g = sn * e[j + 1]; - e[j + 1] = cs * e[j + 1]; - if (wantu && j < m - 1) { - for (let i = 0; i < m; i++) { - t = cs * U[i][j] + sn * U[i][j + 1]; - U[i][j + 1] = -sn * U[i][j] + cs * U[i][j + 1]; - U[i][j] = t; - } - } - } - e[p - 2] = f; - iter = iter + 1; - break; - } - case 4: { - if (s[k] <= 0) { - s[k] = s[k] < 0 ? -s[k] : 0; - if (wantv) { - for (let i = 0; i <= pp; i++) { - V[i][k] = -V[i][k]; - } - } - } - while (k < pp) { - if (s[k] >= s[k + 1]) { - break; - } - let t = s[k]; - s[k] = s[k + 1]; - s[k + 1] = t; - if (wantv && k < n - 1) { - for (let i = 0; i < n; i++) { - t = V[i][k + 1]; - V[i][k + 1] = V[i][k]; - V[i][k] = t; - } - } - if (wantu && k < m - 1) { - for (let i = 0; i < m; i++) { - t = U[i][k + 1]; - U[i][k + 1] = U[i][k]; - U[i][k] = t; - } - } - k++; - } - iter = 0; - p--; - break; - } - // no default - } - } - - if (swapped) { - var tmp = V; - V = U; - U = tmp; - } - - this.m = m; - this.n = n; - this.s = s; - this.U = U; - this.V = V; - } - - /** - * Solve a problem of least square (Ax=b) by using the SVD. Useful when A is singular. When A is not singular, it would be better to use qr.solve(value). - * Example : We search to approximate x, with A matrix shape m*n, x vector size n, b vector size m (m > n). We will use : - * var svd = SingularValueDecomposition(A); - * var x = svd.solve(b); - * @param {Matrix} value - Matrix 1D which is the vector b (in the equation Ax = b) - * @return {Matrix} - The vector x - */ - solve(value) { - var Y = value; - var e = this.threshold; - var scols = this.s.length; - var Ls = matrix_Matrix.zeros(scols, scols); - - for (let i = 0; i < scols; i++) { - if (Math.abs(this.s[i]) <= e) { - Ls[i][i] = 0; - } else { - Ls[i][i] = 1 / this.s[i]; - } - } - - var U = this.U; - var V = this.rightSingularVectors; - - var VL = V.mmul(Ls); - var vrows = V.rows; - var urows = U.length; - var VLU = matrix_Matrix.zeros(vrows, urows); - - for (let i = 0; i < vrows; i++) { - for (let j = 0; j < urows; j++) { - let sum = 0; - for (let k = 0; k < scols; k++) { - sum += VL[i][k] * U[j][k]; - } - VLU[i][j] = sum; - } - } - - return VLU.mmul(Y); - } - - /** - * - * @param {Array} value - * @return {Matrix} - */ - solveForDiagonal(value) { - return this.solve(matrix_Matrix.diag(value)); - } - - /** - * Get the inverse of the matrix. We compute the inverse of a matrix using SVD when this matrix is singular or ill-conditioned. Example : - * var svd = SingularValueDecomposition(A); - * var inverseA = svd.inverse(); - * @return {Matrix} - The approximation of the inverse of the matrix - */ - inverse() { - var V = this.V; - var e = this.threshold; - var vrows = V.length; - var vcols = V[0].length; - var X = new matrix_Matrix(vrows, this.s.length); - - for (let i = 0; i < vrows; i++) { - for (let j = 0; j < vcols; j++) { - if (Math.abs(this.s[j]) > e) { - X[i][j] = V[i][j] / this.s[j]; - } else { - X[i][j] = 0; - } - } - } - - var U = this.U; - - var urows = U.length; - var ucols = U[0].length; - var Y = new matrix_Matrix(vrows, urows); - - for (let i = 0; i < vrows; i++) { - for (let j = 0; j < urows; j++) { - let sum = 0; - for (let k = 0; k < ucols; k++) { - sum += X[i][k] * U[j][k]; - } - Y[i][j] = sum; - } - } - - return Y; - } - - /** - * - * @return {number} - */ - get condition() { - return this.s[0] / this.s[Math.min(this.m, this.n) - 1]; - } - - /** - * - * @return {number} - */ - get norm2() { - return this.s[0]; - } - - /** - * - * @return {number} - */ - get rank() { - var tol = Math.max(this.m, this.n) * this.s[0] * Number.EPSILON; - var r = 0; - var s = this.s; - for (var i = 0, ii = s.length; i < ii; i++) { - if (s[i] > tol) { - r++; - } - } - return r; - } - - /** - * - * @return {Array} - */ - get diagonal() { - return this.s; - } - - /** - * - * @return {number} - */ - get threshold() { - return Number.EPSILON / 2 * Math.max(this.m, this.n) * this.s[0]; - } - - /** - * - * @return {Matrix} - */ - get leftSingularVectors() { - if (!matrix_Matrix.isMatrix(this.U)) { - this.U = new matrix_Matrix(this.U); - } - return this.U; - } - - /** - * - * @return {Matrix} - */ - get rightSingularVectors() { - if (!matrix_Matrix.isMatrix(this.V)) { - this.V = new matrix_Matrix(this.V); - } - return this.V; - } - - /** - * - * @return {Matrix} - */ - get diagonalMatrix() { - return matrix_Matrix.diag(this.s); - } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/util.js - - -/** - * @private - * Check that a row index is not out of bounds - * @param {Matrix} matrix - * @param {number} index - * @param {boolean} [outer] - */ -function checkRowIndex(matrix, index, outer) { - var max = outer ? matrix.rows : matrix.rows - 1; - if (index < 0 || index > max) { - throw new RangeError('Row index out of range'); - } -} - -/** - * @private - * Check that a column index is not out of bounds - * @param {Matrix} matrix - * @param {number} index - * @param {boolean} [outer] - */ -function checkColumnIndex(matrix, index, outer) { - var max = outer ? matrix.columns : matrix.columns - 1; - if (index < 0 || index > max) { - throw new RangeError('Column index out of range'); - } -} - -/** - * @private - * Check that the provided vector is an array with the right length - * @param {Matrix} matrix - * @param {Array|Matrix} vector - * @return {Array} - * @throws {RangeError} - */ -function checkRowVector(matrix, vector) { - if (vector.to1DArray) { - vector = vector.to1DArray(); - } - if (vector.length !== matrix.columns) { - throw new RangeError( - 'vector size must be the same as the number of columns' - ); - } - return vector; -} - -/** - * @private - * Check that the provided vector is an array with the right length - * @param {Matrix} matrix - * @param {Array|Matrix} vector - * @return {Array} - * @throws {RangeError} - */ -function checkColumnVector(matrix, vector) { - if (vector.to1DArray) { - vector = vector.to1DArray(); - } - if (vector.length !== matrix.rows) { - throw new RangeError('vector size must be the same as the number of rows'); - } - return vector; -} - -function checkIndices(matrix, rowIndices, columnIndices) { - return { - row: checkRowIndices(matrix, rowIndices), - column: checkColumnIndices(matrix, columnIndices) - }; -} - -function checkRowIndices(matrix, rowIndices) { - if (typeof rowIndices !== 'object') { - throw new TypeError('unexpected type for row indices'); - } - - var rowOut = rowIndices.some((r) => { - return r < 0 || r >= matrix.rows; - }); - - if (rowOut) { - throw new RangeError('row indices are out of range'); - } - - if (!Array.isArray(rowIndices)) rowIndices = Array.from(rowIndices); - - return rowIndices; -} - -function checkColumnIndices(matrix, columnIndices) { - if (typeof columnIndices !== 'object') { - throw new TypeError('unexpected type for column indices'); - } - - var columnOut = columnIndices.some((c) => { - return c < 0 || c >= matrix.columns; - }); - - if (columnOut) { - throw new RangeError('column indices are out of range'); - } - if (!Array.isArray(columnIndices)) columnIndices = Array.from(columnIndices); - - return columnIndices; -} - -function checkRange(matrix, startRow, endRow, startColumn, endColumn) { - if (arguments.length !== 5) { - throw new RangeError('expected 4 arguments'); - } - checkNumber('startRow', startRow); - checkNumber('endRow', endRow); - checkNumber('startColumn', startColumn); - checkNumber('endColumn', endColumn); - if ( - startRow > endRow || - startColumn > endColumn || - startRow < 0 || - startRow >= matrix.rows || - endRow < 0 || - endRow >= matrix.rows || - startColumn < 0 || - startColumn >= matrix.columns || - endColumn < 0 || - endColumn >= matrix.columns - ) { - throw new RangeError('Submatrix indices are out of range'); - } -} - -function getRange(from, to) { - var arr = new Array(to - from + 1); - for (var i = 0; i < arr.length; i++) { - arr[i] = from + i; - } - return arr; -} - -function sumByRow(matrix) { - var sum = matrix_Matrix.zeros(matrix.rows, 1); - for (var i = 0; i < matrix.rows; ++i) { - for (var j = 0; j < matrix.columns; ++j) { - sum.set(i, 0, sum.get(i, 0) + matrix.get(i, j)); - } - } - return sum; -} - -function sumByColumn(matrix) { - var sum = matrix_Matrix.zeros(1, matrix.columns); - for (var i = 0; i < matrix.rows; ++i) { - for (var j = 0; j < matrix.columns; ++j) { - sum.set(0, j, sum.get(0, j) + matrix.get(i, j)); - } - } - return sum; -} - -function sumAll(matrix) { - var v = 0; - for (var i = 0; i < matrix.rows; i++) { - for (var j = 0; j < matrix.columns; j++) { - v += matrix.get(i, j); - } - } - return v; -} - -function checkNumber(name, value) { - if (typeof value !== 'number') { - throw new TypeError(`${name} must be a number`); - } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/base.js - - - -class base_BaseView extends AbstractMatrix() { - constructor(matrix, rows, columns) { - super(); - this.matrix = matrix; - this.rows = rows; - this.columns = columns; - } - - static get [Symbol.species]() { - return matrix_Matrix; - } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/transpose.js - - -class transpose_MatrixTransposeView extends base_BaseView { - constructor(matrix) { - super(matrix, matrix.columns, matrix.rows); - } - - set(rowIndex, columnIndex, value) { - this.matrix.set(columnIndex, rowIndex, value); - return this; - } - - get(rowIndex, columnIndex) { - return this.matrix.get(columnIndex, rowIndex); - } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/row.js - - -class row_MatrixRowView extends base_BaseView { - constructor(matrix, row) { - super(matrix, 1, matrix.columns); - this.row = row; - } - - set(rowIndex, columnIndex, value) { - this.matrix.set(this.row, columnIndex, value); - return this; - } - - get(rowIndex, columnIndex) { - return this.matrix.get(this.row, columnIndex); - } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/sub.js - - - - -class sub_MatrixSubView extends base_BaseView { - constructor(matrix, startRow, endRow, startColumn, endColumn) { - checkRange(matrix, startRow, endRow, startColumn, endColumn); - super(matrix, endRow - startRow + 1, endColumn - startColumn + 1); - this.startRow = startRow; - this.startColumn = startColumn; - } - - set(rowIndex, columnIndex, value) { - this.matrix.set( - this.startRow + rowIndex, - this.startColumn + columnIndex, - value - ); - return this; - } - - get(rowIndex, columnIndex) { - return this.matrix.get( - this.startRow + rowIndex, - this.startColumn + columnIndex - ); - } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/selection.js - - - - -class selection_MatrixSelectionView extends base_BaseView { - constructor(matrix, rowIndices, columnIndices) { - var indices = checkIndices(matrix, rowIndices, columnIndices); - super(matrix, indices.row.length, indices.column.length); - this.rowIndices = indices.row; - this.columnIndices = indices.column; - } - - set(rowIndex, columnIndex, value) { - this.matrix.set( - this.rowIndices[rowIndex], - this.columnIndices[columnIndex], - value - ); - return this; - } - - get(rowIndex, columnIndex) { - return this.matrix.get( - this.rowIndices[rowIndex], - this.columnIndices[columnIndex] - ); - } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/rowSelection.js - - - - -class rowSelection_MatrixRowSelectionView extends base_BaseView { - constructor(matrix, rowIndices) { - rowIndices = checkRowIndices(matrix, rowIndices); - super(matrix, rowIndices.length, matrix.columns); - this.rowIndices = rowIndices; - } - - set(rowIndex, columnIndex, value) { - this.matrix.set(this.rowIndices[rowIndex], columnIndex, value); - return this; - } - - get(rowIndex, columnIndex) { - return this.matrix.get(this.rowIndices[rowIndex], columnIndex); - } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/columnSelection.js - - - - -class columnSelection_MatrixColumnSelectionView extends base_BaseView { - constructor(matrix, columnIndices) { - columnIndices = checkColumnIndices(matrix, columnIndices); - super(matrix, matrix.rows, columnIndices.length); - this.columnIndices = columnIndices; - } - - set(rowIndex, columnIndex, value) { - this.matrix.set(rowIndex, this.columnIndices[columnIndex], value); - return this; - } - - get(rowIndex, columnIndex) { - return this.matrix.get(rowIndex, this.columnIndices[columnIndex]); - } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/column.js - - -class column_MatrixColumnView extends base_BaseView { - constructor(matrix, column) { - super(matrix, matrix.rows, 1); - this.column = column; - } - - set(rowIndex, columnIndex, value) { - this.matrix.set(rowIndex, this.column, value); - return this; - } - - get(rowIndex) { - return this.matrix.get(rowIndex, this.column); - } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/flipRow.js - - -class flipRow_MatrixFlipRowView extends base_BaseView { - constructor(matrix) { - super(matrix, matrix.rows, matrix.columns); - } - - set(rowIndex, columnIndex, value) { - this.matrix.set(this.rows - rowIndex - 1, columnIndex, value); - return this; - } - - get(rowIndex, columnIndex) { - return this.matrix.get(this.rows - rowIndex - 1, columnIndex); - } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/flipColumn.js - - -class flipColumn_MatrixFlipColumnView extends base_BaseView { - constructor(matrix) { - super(matrix, matrix.rows, matrix.columns); - } - - set(rowIndex, columnIndex, value) { - this.matrix.set(rowIndex, this.columns - columnIndex - 1, value); - return this; - } - - get(rowIndex, columnIndex) { - return this.matrix.get(rowIndex, this.columns - columnIndex - 1); - } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/abstractMatrix.js - - - - - - - - - - - - - - - -function AbstractMatrix(superCtor) { - if (superCtor === undefined) superCtor = Object; - - /** - * Real matrix - * @class Matrix - * @param {number|Array|Matrix} nRows - Number of rows of the new matrix, - * 2D array containing the data or Matrix instance to clone - * @param {number} [nColumns] - Number of columns of the new matrix - */ - class Matrix extends superCtor { - static get [Symbol.species]() { - return this; - } - - /** - * Constructs a Matrix with the chosen dimensions from a 1D array - * @param {number} newRows - Number of rows - * @param {number} newColumns - Number of columns - * @param {Array} newData - A 1D array containing data for the matrix - * @return {Matrix} - The new matrix - */ - static from1DArray(newRows, newColumns, newData) { - var length = newRows * newColumns; - if (length !== newData.length) { - throw new RangeError('Data length does not match given dimensions'); - } - var newMatrix = new this(newRows, newColumns); - for (var row = 0; row < newRows; row++) { - for (var column = 0; column < newColumns; column++) { - newMatrix.set(row, column, newData[row * newColumns + column]); - } - } - return newMatrix; - } - - /** - * Creates a row vector, a matrix with only one row. - * @param {Array} newData - A 1D array containing data for the vector - * @return {Matrix} - The new matrix - */ - static rowVector(newData) { - var vector = new this(1, newData.length); - for (var i = 0; i < newData.length; i++) { - vector.set(0, i, newData[i]); - } - return vector; - } - - /** - * Creates a column vector, a matrix with only one column. - * @param {Array} newData - A 1D array containing data for the vector - * @return {Matrix} - The new matrix - */ - static columnVector(newData) { - var vector = new this(newData.length, 1); - for (var i = 0; i < newData.length; i++) { - vector.set(i, 0, newData[i]); - } - return vector; - } - - /** - * Creates an empty matrix with the given dimensions. Values will be undefined. Same as using new Matrix(rows, columns). - * @param {number} rows - Number of rows - * @param {number} columns - Number of columns - * @return {Matrix} - The new matrix - */ - static empty(rows, columns) { - return new this(rows, columns); - } - - /** - * Creates a matrix with the given dimensions. Values will be set to zero. - * @param {number} rows - Number of rows - * @param {number} columns - Number of columns - * @return {Matrix} - The new matrix - */ - static zeros(rows, columns) { - return this.empty(rows, columns).fill(0); - } - - /** - * Creates a matrix with the given dimensions. Values will be set to one. - * @param {number} rows - Number of rows - * @param {number} columns - Number of columns - * @return {Matrix} - The new matrix - */ - static ones(rows, columns) { - return this.empty(rows, columns).fill(1); - } - - /** - * Creates a matrix with the given dimensions. Values will be randomly set. - * @param {number} rows - Number of rows - * @param {number} columns - Number of columns - * @param {function} [rng=Math.random] - Random number generator - * @return {Matrix} The new matrix - */ - static rand(rows, columns, rng) { - if (rng === undefined) rng = Math.random; - var matrix = this.empty(rows, columns); - for (var i = 0; i < rows; i++) { - for (var j = 0; j < columns; j++) { - matrix.set(i, j, rng()); - } - } - return matrix; - } - - /** - * Creates a matrix with the given dimensions. Values will be random integers. - * @param {number} rows - Number of rows - * @param {number} columns - Number of columns - * @param {number} [maxValue=1000] - Maximum value - * @param {function} [rng=Math.random] - Random number generator - * @return {Matrix} The new matrix - */ - static randInt(rows, columns, maxValue, rng) { - if (maxValue === undefined) maxValue = 1000; - if (rng === undefined) rng = Math.random; - var matrix = this.empty(rows, columns); - for (var i = 0; i < rows; i++) { - for (var j = 0; j < columns; j++) { - var value = Math.floor(rng() * maxValue); - matrix.set(i, j, value); - } - } - return matrix; - } - - /** - * Creates an identity matrix with the given dimension. Values of the diagonal will be 1 and others will be 0. - * @param {number} rows - Number of rows - * @param {number} [columns=rows] - Number of columns - * @param {number} [value=1] - Value to fill the diagonal with - * @return {Matrix} - The new identity matrix - */ - static eye(rows, columns, value) { - if (columns === undefined) columns = rows; - if (value === undefined) value = 1; - var min = Math.min(rows, columns); - var matrix = this.zeros(rows, columns); - for (var i = 0; i < min; i++) { - matrix.set(i, i, value); - } - return matrix; - } - - /** - * Creates a diagonal matrix based on the given array. - * @param {Array} data - Array containing the data for the diagonal - * @param {number} [rows] - Number of rows (Default: data.length) - * @param {number} [columns] - Number of columns (Default: rows) - * @return {Matrix} - The new diagonal matrix - */ - static diag(data, rows, columns) { - var l = data.length; - if (rows === undefined) rows = l; - if (columns === undefined) columns = rows; - var min = Math.min(l, rows, columns); - var matrix = this.zeros(rows, columns); - for (var i = 0; i < min; i++) { - matrix.set(i, i, data[i]); - } - return matrix; - } - - /** - * Returns a matrix whose elements are the minimum between matrix1 and matrix2 - * @param {Matrix} matrix1 - * @param {Matrix} matrix2 - * @return {Matrix} - */ - static min(matrix1, matrix2) { - matrix1 = this.checkMatrix(matrix1); - matrix2 = this.checkMatrix(matrix2); - var rows = matrix1.rows; - var columns = matrix1.columns; - var result = new this(rows, columns); - for (var i = 0; i < rows; i++) { - for (var j = 0; j < columns; j++) { - result.set(i, j, Math.min(matrix1.get(i, j), matrix2.get(i, j))); - } - } - return result; - } - - /** - * Returns a matrix whose elements are the maximum between matrix1 and matrix2 - * @param {Matrix} matrix1 - * @param {Matrix} matrix2 - * @return {Matrix} - */ - static max(matrix1, matrix2) { - matrix1 = this.checkMatrix(matrix1); - matrix2 = this.checkMatrix(matrix2); - var rows = matrix1.rows; - var columns = matrix1.columns; - var result = new this(rows, columns); - for (var i = 0; i < rows; i++) { - for (var j = 0; j < columns; j++) { - result.set(i, j, Math.max(matrix1.get(i, j), matrix2.get(i, j))); - } - } - return result; - } - - /** - * Check that the provided value is a Matrix and tries to instantiate one if not - * @param {*} value - The value to check - * @return {Matrix} - */ - static checkMatrix(value) { - return Matrix.isMatrix(value) ? value : new this(value); - } - - /** - * Returns true if the argument is a Matrix, false otherwise - * @param {*} value - The value to check - * @return {boolean} - */ - static isMatrix(value) { - return (value != null) && (value.klass === 'Matrix'); - } - - /** - * @prop {number} size - The number of elements in the matrix. - */ - get size() { - return this.rows * this.columns; - } - - /** - * Applies a callback for each element of the matrix. The function is called in the matrix (this) context. - * @param {function} callback - Function that will be called with two parameters : i (row) and j (column) - * @return {Matrix} this - */ - apply(callback) { - if (typeof callback !== 'function') { - throw new TypeError('callback must be a function'); - } - var ii = this.rows; - var jj = this.columns; - for (var i = 0; i < ii; i++) { - for (var j = 0; j < jj; j++) { - callback.call(this, i, j); - } - } - return this; - } - - /** - * Returns a new 1D array filled row by row with the matrix values - * @return {Array} - */ - to1DArray() { - var array = new Array(this.size); - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - array[i * this.columns + j] = this.get(i, j); - } - } - return array; - } - - /** - * Returns a 2D array containing a copy of the data - * @return {Array} - */ - to2DArray() { - var copy = new Array(this.rows); - for (var i = 0; i < this.rows; i++) { - copy[i] = new Array(this.columns); - for (var j = 0; j < this.columns; j++) { - copy[i][j] = this.get(i, j); - } - } - return copy; - } - - /** - * @return {boolean} true if the matrix has one row - */ - isRowVector() { - return this.rows === 1; - } - - /** - * @return {boolean} true if the matrix has one column - */ - isColumnVector() { - return this.columns === 1; - } - - /** - * @return {boolean} true if the matrix has one row or one column - */ - isVector() { - return (this.rows === 1) || (this.columns === 1); - } - - /** - * @return {boolean} true if the matrix has the same number of rows and columns - */ - isSquare() { - return this.rows === this.columns; - } - - /** - * @return {boolean} true if the matrix is square and has the same values on both sides of the diagonal - */ - isSymmetric() { - if (this.isSquare()) { - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j <= i; j++) { - if (this.get(i, j) !== this.get(j, i)) { - return false; - } - } - } - return true; - } - return false; - } - - /** - * Sets a given element of the matrix. mat.set(3,4,1) is equivalent to mat[3][4]=1 - * @abstract - * @param {number} rowIndex - Index of the row - * @param {number} columnIndex - Index of the column - * @param {number} value - The new value for the element - * @return {Matrix} this - */ - set(rowIndex, columnIndex, value) { // eslint-disable-line no-unused-vars - throw new Error('set method is unimplemented'); - } - - /** - * Returns the given element of the matrix. mat.get(3,4) is equivalent to matrix[3][4] - * @abstract - * @param {number} rowIndex - Index of the row - * @param {number} columnIndex - Index of the column - * @return {number} - */ - get(rowIndex, columnIndex) { // eslint-disable-line no-unused-vars - throw new Error('get method is unimplemented'); - } - - /** - * Creates a new matrix that is a repetition of the current matrix. New matrix has rowRep times the number of - * rows of the matrix, and colRep times the number of columns of the matrix - * @param {number} rowRep - Number of times the rows should be repeated - * @param {number} colRep - Number of times the columns should be re - * @return {Matrix} - * @example - * var matrix = new Matrix([[1,2]]); - * matrix.repeat(2); // [[1,2],[1,2]] - */ - repeat(rowRep, colRep) { - rowRep = rowRep || 1; - colRep = colRep || 1; - var matrix = new this.constructor[Symbol.species](this.rows * rowRep, this.columns * colRep); - for (var i = 0; i < rowRep; i++) { - for (var j = 0; j < colRep; j++) { - matrix.setSubMatrix(this, this.rows * i, this.columns * j); - } - } - return matrix; - } - - /** - * Fills the matrix with a given value. All elements will be set to this value. - * @param {number} value - New value - * @return {Matrix} this - */ - fill(value) { - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, value); - } - } - return this; - } - - /** - * Negates the matrix. All elements will be multiplied by (-1) - * @return {Matrix} this - */ - neg() { - return this.mulS(-1); - } - - /** - * Returns a new array from the given row index - * @param {number} index - Row index - * @return {Array} - */ - getRow(index) { - checkRowIndex(this, index); - var row = new Array(this.columns); - for (var i = 0; i < this.columns; i++) { - row[i] = this.get(index, i); - } - return row; - } - - /** - * Returns a new row vector from the given row index - * @param {number} index - Row index - * @return {Matrix} - */ - getRowVector(index) { - return this.constructor.rowVector(this.getRow(index)); - } - - /** - * Sets a row at the given index - * @param {number} index - Row index - * @param {Array|Matrix} array - Array or vector - * @return {Matrix} this - */ - setRow(index, array) { - checkRowIndex(this, index); - array = checkRowVector(this, array); - for (var i = 0; i < this.columns; i++) { - this.set(index, i, array[i]); - } - return this; - } - - /** - * Swaps two rows - * @param {number} row1 - First row index - * @param {number} row2 - Second row index - * @return {Matrix} this - */ - swapRows(row1, row2) { - checkRowIndex(this, row1); - checkRowIndex(this, row2); - for (var i = 0; i < this.columns; i++) { - var temp = this.get(row1, i); - this.set(row1, i, this.get(row2, i)); - this.set(row2, i, temp); - } - return this; - } - - /** - * Returns a new array from the given column index - * @param {number} index - Column index - * @return {Array} - */ - getColumn(index) { - checkColumnIndex(this, index); - var column = new Array(this.rows); - for (var i = 0; i < this.rows; i++) { - column[i] = this.get(i, index); - } - return column; - } - - /** - * Returns a new column vector from the given column index - * @param {number} index - Column index - * @return {Matrix} - */ - getColumnVector(index) { - return this.constructor.columnVector(this.getColumn(index)); - } - - /** - * Sets a column at the given index - * @param {number} index - Column index - * @param {Array|Matrix} array - Array or vector - * @return {Matrix} this - */ - setColumn(index, array) { - checkColumnIndex(this, index); - array = checkColumnVector(this, array); - for (var i = 0; i < this.rows; i++) { - this.set(i, index, array[i]); - } - return this; - } - - /** - * Swaps two columns - * @param {number} column1 - First column index - * @param {number} column2 - Second column index - * @return {Matrix} this - */ - swapColumns(column1, column2) { - checkColumnIndex(this, column1); - checkColumnIndex(this, column2); - for (var i = 0; i < this.rows; i++) { - var temp = this.get(i, column1); - this.set(i, column1, this.get(i, column2)); - this.set(i, column2, temp); - } - return this; - } - - /** - * Adds the values of a vector to each row - * @param {Array|Matrix} vector - Array or vector - * @return {Matrix} this - */ - addRowVector(vector) { - vector = checkRowVector(this, vector); - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, this.get(i, j) + vector[j]); - } - } - return this; - } - - /** - * Subtracts the values of a vector from each row - * @param {Array|Matrix} vector - Array or vector - * @return {Matrix} this - */ - subRowVector(vector) { - vector = checkRowVector(this, vector); - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, this.get(i, j) - vector[j]); - } - } - return this; - } - - /** - * Multiplies the values of a vector with each row - * @param {Array|Matrix} vector - Array or vector - * @return {Matrix} this - */ - mulRowVector(vector) { - vector = checkRowVector(this, vector); - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, this.get(i, j) * vector[j]); - } - } - return this; - } - - /** - * Divides the values of each row by those of a vector - * @param {Array|Matrix} vector - Array or vector - * @return {Matrix} this - */ - divRowVector(vector) { - vector = checkRowVector(this, vector); - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, this.get(i, j) / vector[j]); - } - } - return this; - } - - /** - * Adds the values of a vector to each column - * @param {Array|Matrix} vector - Array or vector - * @return {Matrix} this - */ - addColumnVector(vector) { - vector = checkColumnVector(this, vector); - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, this.get(i, j) + vector[i]); - } - } - return this; - } - - /** - * Subtracts the values of a vector from each column - * @param {Array|Matrix} vector - Array or vector - * @return {Matrix} this - */ - subColumnVector(vector) { - vector = checkColumnVector(this, vector); - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, this.get(i, j) - vector[i]); - } - } - return this; - } - - /** - * Multiplies the values of a vector with each column - * @param {Array|Matrix} vector - Array or vector - * @return {Matrix} this - */ - mulColumnVector(vector) { - vector = checkColumnVector(this, vector); - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, this.get(i, j) * vector[i]); - } - } - return this; - } - - /** - * Divides the values of each column by those of a vector - * @param {Array|Matrix} vector - Array or vector - * @return {Matrix} this - */ - divColumnVector(vector) { - vector = checkColumnVector(this, vector); - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, this.get(i, j) / vector[i]); - } - } - return this; - } - - /** - * Multiplies the values of a row with a scalar - * @param {number} index - Row index - * @param {number} value - * @return {Matrix} this - */ - mulRow(index, value) { - checkRowIndex(this, index); - for (var i = 0; i < this.columns; i++) { - this.set(index, i, this.get(index, i) * value); - } - return this; - } - - /** - * Multiplies the values of a column with a scalar - * @param {number} index - Column index - * @param {number} value - * @return {Matrix} this - */ - mulColumn(index, value) { - checkColumnIndex(this, index); - for (var i = 0; i < this.rows; i++) { - this.set(i, index, this.get(i, index) * value); - } - return this; - } - - /** - * Returns the maximum value of the matrix - * @return {number} - */ - max() { - var v = this.get(0, 0); - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - if (this.get(i, j) > v) { - v = this.get(i, j); - } - } - } - return v; - } - - /** - * Returns the index of the maximum value - * @return {Array} - */ - maxIndex() { - var v = this.get(0, 0); - var idx = [0, 0]; - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - if (this.get(i, j) > v) { - v = this.get(i, j); - idx[0] = i; - idx[1] = j; - } - } - } - return idx; - } - - /** - * Returns the minimum value of the matrix - * @return {number} - */ - min() { - var v = this.get(0, 0); - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - if (this.get(i, j) < v) { - v = this.get(i, j); - } - } - } - return v; - } - - /** - * Returns the index of the minimum value - * @return {Array} - */ - minIndex() { - var v = this.get(0, 0); - var idx = [0, 0]; - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - if (this.get(i, j) < v) { - v = this.get(i, j); - idx[0] = i; - idx[1] = j; - } - } - } - return idx; - } - - /** - * Returns the maximum value of one row - * @param {number} row - Row index - * @return {number} - */ - maxRow(row) { - checkRowIndex(this, row); - var v = this.get(row, 0); - for (var i = 1; i < this.columns; i++) { - if (this.get(row, i) > v) { - v = this.get(row, i); - } - } - return v; - } - - /** - * Returns the index of the maximum value of one row - * @param {number} row - Row index - * @return {Array} - */ - maxRowIndex(row) { - checkRowIndex(this, row); - var v = this.get(row, 0); - var idx = [row, 0]; - for (var i = 1; i < this.columns; i++) { - if (this.get(row, i) > v) { - v = this.get(row, i); - idx[1] = i; - } - } - return idx; - } - - /** - * Returns the minimum value of one row - * @param {number} row - Row index - * @return {number} - */ - minRow(row) { - checkRowIndex(this, row); - var v = this.get(row, 0); - for (var i = 1; i < this.columns; i++) { - if (this.get(row, i) < v) { - v = this.get(row, i); - } - } - return v; - } - - /** - * Returns the index of the maximum value of one row - * @param {number} row - Row index - * @return {Array} - */ - minRowIndex(row) { - checkRowIndex(this, row); - var v = this.get(row, 0); - var idx = [row, 0]; - for (var i = 1; i < this.columns; i++) { - if (this.get(row, i) < v) { - v = this.get(row, i); - idx[1] = i; - } - } - return idx; - } - - /** - * Returns the maximum value of one column - * @param {number} column - Column index - * @return {number} - */ - maxColumn(column) { - checkColumnIndex(this, column); - var v = this.get(0, column); - for (var i = 1; i < this.rows; i++) { - if (this.get(i, column) > v) { - v = this.get(i, column); - } - } - return v; - } - - /** - * Returns the index of the maximum value of one column - * @param {number} column - Column index - * @return {Array} - */ - maxColumnIndex(column) { - checkColumnIndex(this, column); - var v = this.get(0, column); - var idx = [0, column]; - for (var i = 1; i < this.rows; i++) { - if (this.get(i, column) > v) { - v = this.get(i, column); - idx[0] = i; - } - } - return idx; - } - - /** - * Returns the minimum value of one column - * @param {number} column - Column index - * @return {number} - */ - minColumn(column) { - checkColumnIndex(this, column); - var v = this.get(0, column); - for (var i = 1; i < this.rows; i++) { - if (this.get(i, column) < v) { - v = this.get(i, column); - } - } - return v; - } - - /** - * Returns the index of the minimum value of one column - * @param {number} column - Column index - * @return {Array} - */ - minColumnIndex(column) { - checkColumnIndex(this, column); - var v = this.get(0, column); - var idx = [0, column]; - for (var i = 1; i < this.rows; i++) { - if (this.get(i, column) < v) { - v = this.get(i, column); - idx[0] = i; - } - } - return idx; - } - - /** - * Returns an array containing the diagonal values of the matrix - * @return {Array} - */ - diag() { - var min = Math.min(this.rows, this.columns); - var diag = new Array(min); - for (var i = 0; i < min; i++) { - diag[i] = this.get(i, i); - } - return diag; - } - - /** - * Returns the sum by the argument given, if no argument given, - * it returns the sum of all elements of the matrix. - * @param {string} by - sum by 'row' or 'column'. - * @return {Matrix|number} - */ - sum(by) { - switch (by) { - case 'row': - return sumByRow(this); - case 'column': - return sumByColumn(this); - default: - return sumAll(this); - } - } - - /** - * Returns the mean of all elements of the matrix - * @return {number} - */ - mean() { - return this.sum() / this.size; - } - - /** - * Returns the product of all elements of the matrix - * @return {number} - */ - prod() { - var prod = 1; - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - prod *= this.get(i, j); - } - } - return prod; - } - - /** - * Returns the norm of a matrix. - * @param {string} type - "frobenius" (default) or "max" return resp. the Frobenius norm and the max norm. - * @return {number} - */ - norm(type = 'frobenius') { - var result = 0; - if (type === 'max') { - return this.max(); - } else if (type === 'frobenius') { - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - result = result + this.get(i, j) * this.get(i, j); - } - } - return Math.sqrt(result); - } else { - throw new RangeError(`unknown norm type: ${type}`); - } - } - - /** - * Computes the cumulative sum of the matrix elements (in place, row by row) - * @return {Matrix} this - */ - cumulativeSum() { - var sum = 0; - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - sum += this.get(i, j); - this.set(i, j, sum); - } - } - return this; - } - - /** - * Computes the dot (scalar) product between the matrix and another - * @param {Matrix} vector2 vector - * @return {number} - */ - dot(vector2) { - if (Matrix.isMatrix(vector2)) vector2 = vector2.to1DArray(); - var vector1 = this.to1DArray(); - if (vector1.length !== vector2.length) { - throw new RangeError('vectors do not have the same size'); - } - var dot = 0; - for (var i = 0; i < vector1.length; i++) { - dot += vector1[i] * vector2[i]; - } - return dot; - } - - /** - * Returns the matrix product between this and other - * @param {Matrix} other - * @return {Matrix} - */ - mmul(other) { - other = this.constructor.checkMatrix(other); - if (this.columns !== other.rows) { - // eslint-disable-next-line no-console - console.warn('Number of columns of left matrix are not equal to number of rows of right matrix.'); - } - - var m = this.rows; - var n = this.columns; - var p = other.columns; - - var result = new this.constructor[Symbol.species](m, p); - - var Bcolj = new Array(n); - for (var j = 0; j < p; j++) { - for (var k = 0; k < n; k++) { - Bcolj[k] = other.get(k, j); - } - - for (var i = 0; i < m; i++) { - var s = 0; - for (k = 0; k < n; k++) { - s += this.get(i, k) * Bcolj[k]; - } - - result.set(i, j, s); - } - } - return result; - } - - strassen2x2(other) { - var result = new this.constructor[Symbol.species](2, 2); - const a11 = this.get(0, 0); - const b11 = other.get(0, 0); - const a12 = this.get(0, 1); - const b12 = other.get(0, 1); - const a21 = this.get(1, 0); - const b21 = other.get(1, 0); - const a22 = this.get(1, 1); - const b22 = other.get(1, 1); - - // Compute intermediate values. - const m1 = (a11 + a22) * (b11 + b22); - const m2 = (a21 + a22) * b11; - const m3 = a11 * (b12 - b22); - const m4 = a22 * (b21 - b11); - const m5 = (a11 + a12) * b22; - const m6 = (a21 - a11) * (b11 + b12); - const m7 = (a12 - a22) * (b21 + b22); - - // Combine intermediate values into the output. - const c00 = m1 + m4 - m5 + m7; - const c01 = m3 + m5; - const c10 = m2 + m4; - const c11 = m1 - m2 + m3 + m6; - - result.set(0, 0, c00); - result.set(0, 1, c01); - result.set(1, 0, c10); - result.set(1, 1, c11); - return result; - } - - strassen3x3(other) { - var result = new this.constructor[Symbol.species](3, 3); - - const a00 = this.get(0, 0); - const a01 = this.get(0, 1); - const a02 = this.get(0, 2); - const a10 = this.get(1, 0); - const a11 = this.get(1, 1); - const a12 = this.get(1, 2); - const a20 = this.get(2, 0); - const a21 = this.get(2, 1); - const a22 = this.get(2, 2); - - const b00 = other.get(0, 0); - const b01 = other.get(0, 1); - const b02 = other.get(0, 2); - const b10 = other.get(1, 0); - const b11 = other.get(1, 1); - const b12 = other.get(1, 2); - const b20 = other.get(2, 0); - const b21 = other.get(2, 1); - const b22 = other.get(2, 2); - - const m1 = (a00 + a01 + a02 - a10 - a11 - a21 - a22) * b11; - const m2 = (a00 - a10) * (-b01 + b11); - const m3 = a11 * (-b00 + b01 + b10 - b11 - b12 - b20 + b22); - const m4 = (-a00 + a10 + a11) * (b00 - b01 + b11); - const m5 = (a10 + a11) * (-b00 + b01); - const m6 = a00 * b00; - const m7 = (-a00 + a20 + a21) * (b00 - b02 + b12); - const m8 = (-a00 + a20) * (b02 - b12); - const m9 = (a20 + a21) * (-b00 + b02); - const m10 = (a00 + a01 + a02 - a11 - a12 - a20 - a21) * b12; - const m11 = a21 * (-b00 + b02 + b10 - b11 - b12 - b20 + b21); - const m12 = (-a02 + a21 + a22) * (b11 + b20 - b21); - const m13 = (a02 - a22) * (b11 - b21); - const m14 = a02 * b20; - const m15 = (a21 + a22) * (-b20 + b21); - const m16 = (-a02 + a11 + a12) * (b12 + b20 - b22); - const m17 = (a02 - a12) * (b12 - b22); - const m18 = (a11 + a12) * (-b20 + b22); - const m19 = a01 * b10; - const m20 = a12 * b21; - const m21 = a10 * b02; - const m22 = a20 * b01; - const m23 = a22 * b22; - - const c00 = m6 + m14 + m19; - const c01 = m1 + m4 + m5 + m6 + m12 + m14 + m15; - const c02 = m6 + m7 + m9 + m10 + m14 + m16 + m18; - const c10 = m2 + m3 + m4 + m6 + m14 + m16 + m17; - const c11 = m2 + m4 + m5 + m6 + m20; - const c12 = m14 + m16 + m17 + m18 + m21; - const c20 = m6 + m7 + m8 + m11 + m12 + m13 + m14; - const c21 = m12 + m13 + m14 + m15 + m22; - const c22 = m6 + m7 + m8 + m9 + m23; - - result.set(0, 0, c00); - result.set(0, 1, c01); - result.set(0, 2, c02); - result.set(1, 0, c10); - result.set(1, 1, c11); - result.set(1, 2, c12); - result.set(2, 0, c20); - result.set(2, 1, c21); - result.set(2, 2, c22); - return result; - } - - /** - * Returns the matrix product between x and y. More efficient than mmul(other) only when we multiply squared matrix and when the size of the matrix is > 1000. - * @param {Matrix} y - * @return {Matrix} - */ - mmulStrassen(y) { - var x = this.clone(); - var r1 = x.rows; - var c1 = x.columns; - var r2 = y.rows; - var c2 = y.columns; - if (c1 !== r2) { - // eslint-disable-next-line no-console - console.warn(`Multiplying ${r1} x ${c1} and ${r2} x ${c2} matrix: dimensions do not match.`); - } - - // Put a matrix into the top left of a matrix of zeros. - // `rows` and `cols` are the dimensions of the output matrix. - function embed(mat, rows, cols) { - var r = mat.rows; - var c = mat.columns; - if ((r === rows) && (c === cols)) { - return mat; - } else { - var resultat = Matrix.zeros(rows, cols); - resultat = resultat.setSubMatrix(mat, 0, 0); - return resultat; - } - } - - - // Make sure both matrices are the same size. - // This is exclusively for simplicity: - // this algorithm can be implemented with matrices of different sizes. - - var r = Math.max(r1, r2); - var c = Math.max(c1, c2); - x = embed(x, r, c); - y = embed(y, r, c); - - // Our recursive multiplication function. - function blockMult(a, b, rows, cols) { - // For small matrices, resort to naive multiplication. - if (rows <= 512 || cols <= 512) { - return a.mmul(b); // a is equivalent to this - } - - // Apply dynamic padding. - if ((rows % 2 === 1) && (cols % 2 === 1)) { - a = embed(a, rows + 1, cols + 1); - b = embed(b, rows + 1, cols + 1); - } else if (rows % 2 === 1) { - a = embed(a, rows + 1, cols); - b = embed(b, rows + 1, cols); - } else if (cols % 2 === 1) { - a = embed(a, rows, cols + 1); - b = embed(b, rows, cols + 1); - } - - var halfRows = parseInt(a.rows / 2, 10); - var halfCols = parseInt(a.columns / 2, 10); - // Subdivide input matrices. - var a11 = a.subMatrix(0, halfRows - 1, 0, halfCols - 1); - var b11 = b.subMatrix(0, halfRows - 1, 0, halfCols - 1); - - var a12 = a.subMatrix(0, halfRows - 1, halfCols, a.columns - 1); - var b12 = b.subMatrix(0, halfRows - 1, halfCols, b.columns - 1); - - var a21 = a.subMatrix(halfRows, a.rows - 1, 0, halfCols - 1); - var b21 = b.subMatrix(halfRows, b.rows - 1, 0, halfCols - 1); - - var a22 = a.subMatrix(halfRows, a.rows - 1, halfCols, a.columns - 1); - var b22 = b.subMatrix(halfRows, b.rows - 1, halfCols, b.columns - 1); - - // Compute intermediate values. - var m1 = blockMult(Matrix.add(a11, a22), Matrix.add(b11, b22), halfRows, halfCols); - var m2 = blockMult(Matrix.add(a21, a22), b11, halfRows, halfCols); - var m3 = blockMult(a11, Matrix.sub(b12, b22), halfRows, halfCols); - var m4 = blockMult(a22, Matrix.sub(b21, b11), halfRows, halfCols); - var m5 = blockMult(Matrix.add(a11, a12), b22, halfRows, halfCols); - var m6 = blockMult(Matrix.sub(a21, a11), Matrix.add(b11, b12), halfRows, halfCols); - var m7 = blockMult(Matrix.sub(a12, a22), Matrix.add(b21, b22), halfRows, halfCols); - - // Combine intermediate values into the output. - var c11 = Matrix.add(m1, m4); - c11.sub(m5); - c11.add(m7); - var c12 = Matrix.add(m3, m5); - var c21 = Matrix.add(m2, m4); - var c22 = Matrix.sub(m1, m2); - c22.add(m3); - c22.add(m6); - - // Crop output to the desired size (undo dynamic padding). - var resultat = Matrix.zeros(2 * c11.rows, 2 * c11.columns); - resultat = resultat.setSubMatrix(c11, 0, 0); - resultat = resultat.setSubMatrix(c12, c11.rows, 0); - resultat = resultat.setSubMatrix(c21, 0, c11.columns); - resultat = resultat.setSubMatrix(c22, c11.rows, c11.columns); - return resultat.subMatrix(0, rows - 1, 0, cols - 1); - } - return blockMult(x, y, r, c); - } - - /** - * Returns a row-by-row scaled matrix - * @param {number} [min=0] - Minimum scaled value - * @param {number} [max=1] - Maximum scaled value - * @return {Matrix} - The scaled matrix - */ - scaleRows(min, max) { - min = min === undefined ? 0 : min; - max = max === undefined ? 1 : max; - if (min >= max) { - throw new RangeError('min should be strictly smaller than max'); - } - var newMatrix = this.constructor.empty(this.rows, this.columns); - for (var i = 0; i < this.rows; i++) { - var scaled = ml_array_rescale_lib_es6(this.getRow(i), { min, max }); - newMatrix.setRow(i, scaled); - } - return newMatrix; - } - - /** - * Returns a new column-by-column scaled matrix - * @param {number} [min=0] - Minimum scaled value - * @param {number} [max=1] - Maximum scaled value - * @return {Matrix} - The new scaled matrix - * @example - * var matrix = new Matrix([[1,2],[-1,0]]); - * var scaledMatrix = matrix.scaleColumns(); // [[1,1],[0,0]] - */ - scaleColumns(min, max) { - min = min === undefined ? 0 : min; - max = max === undefined ? 1 : max; - if (min >= max) { - throw new RangeError('min should be strictly smaller than max'); - } - var newMatrix = this.constructor.empty(this.rows, this.columns); - for (var i = 0; i < this.columns; i++) { - var scaled = ml_array_rescale_lib_es6(this.getColumn(i), { - min: min, - max: max - }); - newMatrix.setColumn(i, scaled); - } - return newMatrix; - } - - - /** - * Returns the Kronecker product (also known as tensor product) between this and other - * See https://en.wikipedia.org/wiki/Kronecker_product - * @param {Matrix} other - * @return {Matrix} - */ - kroneckerProduct(other) { - other = this.constructor.checkMatrix(other); - - var m = this.rows; - var n = this.columns; - var p = other.rows; - var q = other.columns; - - var result = new this.constructor[Symbol.species](m * p, n * q); - for (var i = 0; i < m; i++) { - for (var j = 0; j < n; j++) { - for (var k = 0; k < p; k++) { - for (var l = 0; l < q; l++) { - result[p * i + k][q * j + l] = this.get(i, j) * other.get(k, l); - } - } - } - } - return result; - } - - /** - * Transposes the matrix and returns a new one containing the result - * @return {Matrix} - */ - transpose() { - var result = new this.constructor[Symbol.species](this.columns, this.rows); - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - result.set(j, i, this.get(i, j)); - } - } - return result; - } - - /** - * Sorts the rows (in place) - * @param {function} compareFunction - usual Array.prototype.sort comparison function - * @return {Matrix} this - */ - sortRows(compareFunction) { - if (compareFunction === undefined) compareFunction = compareNumbers; - for (var i = 0; i < this.rows; i++) { - this.setRow(i, this.getRow(i).sort(compareFunction)); - } - return this; - } - - /** - * Sorts the columns (in place) - * @param {function} compareFunction - usual Array.prototype.sort comparison function - * @return {Matrix} this - */ - sortColumns(compareFunction) { - if (compareFunction === undefined) compareFunction = compareNumbers; - for (var i = 0; i < this.columns; i++) { - this.setColumn(i, this.getColumn(i).sort(compareFunction)); - } - return this; - } - - /** - * Returns a subset of the matrix - * @param {number} startRow - First row index - * @param {number} endRow - Last row index - * @param {number} startColumn - First column index - * @param {number} endColumn - Last column index - * @return {Matrix} - */ - subMatrix(startRow, endRow, startColumn, endColumn) { - checkRange(this, startRow, endRow, startColumn, endColumn); - var newMatrix = new this.constructor[Symbol.species](endRow - startRow + 1, endColumn - startColumn + 1); - for (var i = startRow; i <= endRow; i++) { - for (var j = startColumn; j <= endColumn; j++) { - newMatrix[i - startRow][j - startColumn] = this.get(i, j); - } - } - return newMatrix; - } - - /** - * Returns a subset of the matrix based on an array of row indices - * @param {Array} indices - Array containing the row indices - * @param {number} [startColumn = 0] - First column index - * @param {number} [endColumn = this.columns-1] - Last column index - * @return {Matrix} - */ - subMatrixRow(indices, startColumn, endColumn) { - if (startColumn === undefined) startColumn = 0; - if (endColumn === undefined) endColumn = this.columns - 1; - if ((startColumn > endColumn) || (startColumn < 0) || (startColumn >= this.columns) || (endColumn < 0) || (endColumn >= this.columns)) { - throw new RangeError('Argument out of range'); - } - - var newMatrix = new this.constructor[Symbol.species](indices.length, endColumn - startColumn + 1); - for (var i = 0; i < indices.length; i++) { - for (var j = startColumn; j <= endColumn; j++) { - if (indices[i] < 0 || indices[i] >= this.rows) { - throw new RangeError(`Row index out of range: ${indices[i]}`); - } - newMatrix.set(i, j - startColumn, this.get(indices[i], j)); - } - } - return newMatrix; - } - - /** - * Returns a subset of the matrix based on an array of column indices - * @param {Array} indices - Array containing the column indices - * @param {number} [startRow = 0] - First row index - * @param {number} [endRow = this.rows-1] - Last row index - * @return {Matrix} - */ - subMatrixColumn(indices, startRow, endRow) { - if (startRow === undefined) startRow = 0; - if (endRow === undefined) endRow = this.rows - 1; - if ((startRow > endRow) || (startRow < 0) || (startRow >= this.rows) || (endRow < 0) || (endRow >= this.rows)) { - throw new RangeError('Argument out of range'); - } - - var newMatrix = new this.constructor[Symbol.species](endRow - startRow + 1, indices.length); - for (var i = 0; i < indices.length; i++) { - for (var j = startRow; j <= endRow; j++) { - if (indices[i] < 0 || indices[i] >= this.columns) { - throw new RangeError(`Column index out of range: ${indices[i]}`); - } - newMatrix.set(j - startRow, i, this.get(j, indices[i])); - } - } - return newMatrix; - } - - /** - * Set a part of the matrix to the given sub-matrix - * @param {Matrix|Array< Array >} matrix - The source matrix from which to extract values. - * @param {number} startRow - The index of the first row to set - * @param {number} startColumn - The index of the first column to set - * @return {Matrix} - */ - setSubMatrix(matrix, startRow, startColumn) { - matrix = this.constructor.checkMatrix(matrix); - var endRow = startRow + matrix.rows - 1; - var endColumn = startColumn + matrix.columns - 1; - checkRange(this, startRow, endRow, startColumn, endColumn); - for (var i = 0; i < matrix.rows; i++) { - for (var j = 0; j < matrix.columns; j++) { - this[startRow + i][startColumn + j] = matrix.get(i, j); - } - } - return this; - } - - /** - * Return a new matrix based on a selection of rows and columns - * @param {Array} rowIndices - The row indices to select. Order matters and an index can be more than once. - * @param {Array} columnIndices - The column indices to select. Order matters and an index can be use more than once. - * @return {Matrix} The new matrix - */ - selection(rowIndices, columnIndices) { - var indices = checkIndices(this, rowIndices, columnIndices); - var newMatrix = new this.constructor[Symbol.species](rowIndices.length, columnIndices.length); - for (var i = 0; i < indices.row.length; i++) { - var rowIndex = indices.row[i]; - for (var j = 0; j < indices.column.length; j++) { - var columnIndex = indices.column[j]; - newMatrix[i][j] = this.get(rowIndex, columnIndex); - } - } - return newMatrix; - } - - /** - * Returns the trace of the matrix (sum of the diagonal elements) - * @return {number} - */ - trace() { - var min = Math.min(this.rows, this.columns); - var trace = 0; - for (var i = 0; i < min; i++) { - trace += this.get(i, i); - } - return trace; - } - - /* - Matrix views - */ - - /** - * Returns a view of the transposition of the matrix - * @return {MatrixTransposeView} - */ - transposeView() { - return new transpose_MatrixTransposeView(this); - } - - /** - * Returns a view of the row vector with the given index - * @param {number} row - row index of the vector - * @return {MatrixRowView} - */ - rowView(row) { - checkRowIndex(this, row); - return new row_MatrixRowView(this, row); - } - - /** - * Returns a view of the column vector with the given index - * @param {number} column - column index of the vector - * @return {MatrixColumnView} - */ - columnView(column) { - checkColumnIndex(this, column); - return new column_MatrixColumnView(this, column); - } - - /** - * Returns a view of the matrix flipped in the row axis - * @return {MatrixFlipRowView} - */ - flipRowView() { - return new flipRow_MatrixFlipRowView(this); - } - - /** - * Returns a view of the matrix flipped in the column axis - * @return {MatrixFlipColumnView} - */ - flipColumnView() { - return new flipColumn_MatrixFlipColumnView(this); - } - - /** - * Returns a view of a submatrix giving the index boundaries - * @param {number} startRow - first row index of the submatrix - * @param {number} endRow - last row index of the submatrix - * @param {number} startColumn - first column index of the submatrix - * @param {number} endColumn - last column index of the submatrix - * @return {MatrixSubView} - */ - subMatrixView(startRow, endRow, startColumn, endColumn) { - return new sub_MatrixSubView(this, startRow, endRow, startColumn, endColumn); - } - - /** - * Returns a view of the cross of the row indices and the column indices - * @example - * // resulting vector is [[2], [2]] - * var matrix = new Matrix([[1,2,3], [4,5,6]]).selectionView([0, 0], [1]) - * @param {Array} rowIndices - * @param {Array} columnIndices - * @return {MatrixSelectionView} - */ - selectionView(rowIndices, columnIndices) { - return new selection_MatrixSelectionView(this, rowIndices, columnIndices); - } - - /** - * Returns a view of the row indices - * @example - * // resulting vector is [[1,2,3], [1,2,3]] - * var matrix = new Matrix([[1,2,3], [4,5,6]]).rowSelectionView([0, 0]) - * @param {Array} rowIndices - * @return {MatrixRowSelectionView} - */ - rowSelectionView(rowIndices) { - return new rowSelection_MatrixRowSelectionView(this, rowIndices); - } - - /** - * Returns a view of the column indices - * @example - * // resulting vector is [[2, 2], [5, 5]] - * var matrix = new Matrix([[1,2,3], [4,5,6]]).columnSelectionView([1, 1]) - * @param {Array} columnIndices - * @return {MatrixColumnSelectionView} - */ - columnSelectionView(columnIndices) { - return new columnSelection_MatrixColumnSelectionView(this, columnIndices); - } - - - /** - * Calculates and returns the determinant of a matrix as a Number - * @example - * new Matrix([[1,2,3], [4,5,6]]).det() - * @return {number} - */ - det() { - if (this.isSquare()) { - var a, b, c, d; - if (this.columns === 2) { - // 2 x 2 matrix - a = this.get(0, 0); - b = this.get(0, 1); - c = this.get(1, 0); - d = this.get(1, 1); - - return a * d - (b * c); - } else if (this.columns === 3) { - // 3 x 3 matrix - var subMatrix0, subMatrix1, subMatrix2; - subMatrix0 = this.selectionView([1, 2], [1, 2]); - subMatrix1 = this.selectionView([1, 2], [0, 2]); - subMatrix2 = this.selectionView([1, 2], [0, 1]); - a = this.get(0, 0); - b = this.get(0, 1); - c = this.get(0, 2); - - return a * subMatrix0.det() - b * subMatrix1.det() + c * subMatrix2.det(); - } else { - // general purpose determinant using the LU decomposition - return new lu_LuDecomposition(this).determinant; - } - } else { - throw Error('Determinant can only be calculated for a square matrix.'); - } - } - - /** - * Returns inverse of a matrix if it exists or the pseudoinverse - * @param {number} threshold - threshold for taking inverse of singular values (default = 1e-15) - * @return {Matrix} the (pseudo)inverted matrix. - */ - pseudoInverse(threshold) { - if (threshold === undefined) threshold = Number.EPSILON; - var svdSolution = new svd_SingularValueDecomposition(this, { autoTranspose: true }); - - var U = svdSolution.leftSingularVectors; - var V = svdSolution.rightSingularVectors; - var s = svdSolution.diagonal; - - for (var i = 0; i < s.length; i++) { - if (Math.abs(s[i]) > threshold) { - s[i] = 1.0 / s[i]; - } else { - s[i] = 0.0; - } - } - - // convert list to diagonal - s = this.constructor[Symbol.species].diag(s); - return V.mmul(s.mmul(U.transposeView())); - } - - /** - * Creates an exact and independent copy of the matrix - * @return {Matrix} - */ - clone() { - var newMatrix = new this.constructor[Symbol.species](this.rows, this.columns); - for (var row = 0; row < this.rows; row++) { - for (var column = 0; column < this.columns; column++) { - newMatrix.set(row, column, this.get(row, column)); - } - } - return newMatrix; - } - } - - Matrix.prototype.klass = 'Matrix'; - - function compareNumbers(a, b) { - return a - b; - } - - /* - Synonyms - */ - - Matrix.random = Matrix.rand; - Matrix.diagonal = Matrix.diag; - Matrix.prototype.diagonal = Matrix.prototype.diag; - Matrix.identity = Matrix.eye; - Matrix.prototype.negate = Matrix.prototype.neg; - Matrix.prototype.tensorProduct = Matrix.prototype.kroneckerProduct; - Matrix.prototype.determinant = Matrix.prototype.det; - - /* - Add dynamically instance and static methods for mathematical operations - */ - - var inplaceOperator = ` -(function %name%(value) { - if (typeof value === 'number') return this.%name%S(value); - return this.%name%M(value); -}) -`; - - var inplaceOperatorScalar = ` -(function %name%S(value) { - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, this.get(i, j) %op% value); - } - } - return this; -}) -`; - - var inplaceOperatorMatrix = ` -(function %name%M(matrix) { - matrix = this.constructor.checkMatrix(matrix); - if (this.rows !== matrix.rows || - this.columns !== matrix.columns) { - throw new RangeError('Matrices dimensions must be equal'); - } - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, this.get(i, j) %op% matrix.get(i, j)); - } - } - return this; -}) -`; - - var staticOperator = ` -(function %name%(matrix, value) { - var newMatrix = new this[Symbol.species](matrix); - return newMatrix.%name%(value); -}) -`; - - var inplaceMethod = ` -(function %name%() { - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, %method%(this.get(i, j))); - } - } - return this; -}) -`; - - var staticMethod = ` -(function %name%(matrix) { - var newMatrix = new this[Symbol.species](matrix); - return newMatrix.%name%(); -}) -`; - - var inplaceMethodWithArgs = ` -(function %name%(%args%) { - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, %method%(this.get(i, j), %args%)); - } - } - return this; -}) -`; - - var staticMethodWithArgs = ` -(function %name%(matrix, %args%) { - var newMatrix = new this[Symbol.species](matrix); - return newMatrix.%name%(%args%); -}) -`; - - - var inplaceMethodWithOneArgScalar = ` -(function %name%S(value) { - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, %method%(this.get(i, j), value)); - } - } - return this; -}) -`; - var inplaceMethodWithOneArgMatrix = ` -(function %name%M(matrix) { - matrix = this.constructor.checkMatrix(matrix); - if (this.rows !== matrix.rows || - this.columns !== matrix.columns) { - throw new RangeError('Matrices dimensions must be equal'); - } - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, %method%(this.get(i, j), matrix.get(i, j))); - } - } - return this; -}) -`; - - var inplaceMethodWithOneArg = ` -(function %name%(value) { - if (typeof value === 'number') return this.%name%S(value); - return this.%name%M(value); -}) -`; - - var staticMethodWithOneArg = staticMethodWithArgs; - - var operators = [ - // Arithmetic operators - ['+', 'add'], - ['-', 'sub', 'subtract'], - ['*', 'mul', 'multiply'], - ['/', 'div', 'divide'], - ['%', 'mod', 'modulus'], - // Bitwise operators - ['&', 'and'], - ['|', 'or'], - ['^', 'xor'], - ['<<', 'leftShift'], - ['>>', 'signPropagatingRightShift'], - ['>>>', 'rightShift', 'zeroFillRightShift'] - ]; - - var i; - var eval2 = eval; // eslint-disable-line no-eval - for (var operator of operators) { - var inplaceOp = eval2(fillTemplateFunction(inplaceOperator, { name: operator[1], op: operator[0] })); - var inplaceOpS = eval2(fillTemplateFunction(inplaceOperatorScalar, { name: `${operator[1]}S`, op: operator[0] })); - var inplaceOpM = eval2(fillTemplateFunction(inplaceOperatorMatrix, { name: `${operator[1]}M`, op: operator[0] })); - var staticOp = eval2(fillTemplateFunction(staticOperator, { name: operator[1] })); - for (i = 1; i < operator.length; i++) { - Matrix.prototype[operator[i]] = inplaceOp; - Matrix.prototype[`${operator[i]}S`] = inplaceOpS; - Matrix.prototype[`${operator[i]}M`] = inplaceOpM; - Matrix[operator[i]] = staticOp; - } - } - - var methods = [['~', 'not']]; - - [ - 'abs', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atanh', 'cbrt', 'ceil', - 'clz32', 'cos', 'cosh', 'exp', 'expm1', 'floor', 'fround', 'log', 'log1p', - 'log10', 'log2', 'round', 'sign', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc' - ].forEach(function (mathMethod) { - methods.push([`Math.${mathMethod}`, mathMethod]); - }); - - for (var method of methods) { - var inplaceMeth = eval2(fillTemplateFunction(inplaceMethod, { name: method[1], method: method[0] })); - var staticMeth = eval2(fillTemplateFunction(staticMethod, { name: method[1] })); - for (i = 1; i < method.length; i++) { - Matrix.prototype[method[i]] = inplaceMeth; - Matrix[method[i]] = staticMeth; - } - } - - var methodsWithArgs = [['Math.pow', 1, 'pow']]; - - for (var methodWithArg of methodsWithArgs) { - var args = 'arg0'; - for (i = 1; i < methodWithArg[1]; i++) { - args += `, arg${i}`; - } - if (methodWithArg[1] !== 1) { - var inplaceMethWithArgs = eval2(fillTemplateFunction(inplaceMethodWithArgs, { - name: methodWithArg[2], - method: methodWithArg[0], - args: args - })); - var staticMethWithArgs = eval2(fillTemplateFunction(staticMethodWithArgs, { name: methodWithArg[2], args: args })); - for (i = 2; i < methodWithArg.length; i++) { - Matrix.prototype[methodWithArg[i]] = inplaceMethWithArgs; - Matrix[methodWithArg[i]] = staticMethWithArgs; - } - } else { - var tmplVar = { - name: methodWithArg[2], - args: args, - method: methodWithArg[0] - }; - var inplaceMethod2 = eval2(fillTemplateFunction(inplaceMethodWithOneArg, tmplVar)); - var inplaceMethodS = eval2(fillTemplateFunction(inplaceMethodWithOneArgScalar, tmplVar)); - var inplaceMethodM = eval2(fillTemplateFunction(inplaceMethodWithOneArgMatrix, tmplVar)); - var staticMethod2 = eval2(fillTemplateFunction(staticMethodWithOneArg, tmplVar)); - for (i = 2; i < methodWithArg.length; i++) { - Matrix.prototype[methodWithArg[i]] = inplaceMethod2; - Matrix.prototype[`${methodWithArg[i]}M`] = inplaceMethodM; - Matrix.prototype[`${methodWithArg[i]}S`] = inplaceMethodS; - Matrix[methodWithArg[i]] = staticMethod2; - } - } - } - - function fillTemplateFunction(template, values) { - for (var value in values) { - template = template.replace(new RegExp(`%${value}%`, 'g'), values[value]); - } - return template; - } - - return Matrix; -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/matrix.js - - - -class matrix_Matrix extends AbstractMatrix(Array) { - constructor(nRows, nColumns) { - var i; - if (arguments.length === 1 && typeof nRows === 'number') { - return new Array(nRows); - } - if (matrix_Matrix.isMatrix(nRows)) { - return nRows.clone(); - } else if (Number.isInteger(nRows) && nRows > 0) { - // Create an empty matrix - super(nRows); - if (Number.isInteger(nColumns) && nColumns > 0) { - for (i = 0; i < nRows; i++) { - this[i] = new Array(nColumns); - } - } else { - throw new TypeError('nColumns must be a positive integer'); - } - } else if (Array.isArray(nRows)) { - // Copy the values from the 2D array - const matrix = nRows; - nRows = matrix.length; - nColumns = matrix[0].length; - if (typeof nColumns !== 'number' || nColumns === 0) { - throw new TypeError( - 'Data must be a 2D array with at least one element' - ); - } - super(nRows); - for (i = 0; i < nRows; i++) { - if (matrix[i].length !== nColumns) { - throw new RangeError('Inconsistent array dimensions'); - } - this[i] = [].concat(matrix[i]); - } - } else { - throw new TypeError( - 'First argument must be a positive number or an array' - ); - } - this.rows = nRows; - this.columns = nColumns; - return this; - } - - set(rowIndex, columnIndex, value) { - this[rowIndex][columnIndex] = value; - return this; - } - - get(rowIndex, columnIndex) { - return this[rowIndex][columnIndex]; - } - - /** - * Removes a row from the given index - * @param {number} index - Row index - * @return {Matrix} this - */ - removeRow(index) { - checkRowIndex(this, index); - if (this.rows === 1) { - throw new RangeError('A matrix cannot have less than one row'); - } - this.splice(index, 1); - this.rows -= 1; - return this; - } - - /** - * Adds a row at the given index - * @param {number} [index = this.rows] - Row index - * @param {Array|Matrix} array - Array or vector - * @return {Matrix} this - */ - addRow(index, array) { - if (array === undefined) { - array = index; - index = this.rows; - } - checkRowIndex(this, index, true); - array = checkRowVector(this, array, true); - this.splice(index, 0, array); - this.rows += 1; - return this; - } - - /** - * Removes a column from the given index - * @param {number} index - Column index - * @return {Matrix} this - */ - removeColumn(index) { - checkColumnIndex(this, index); - if (this.columns === 1) { - throw new RangeError('A matrix cannot have less than one column'); - } - for (var i = 0; i < this.rows; i++) { - this[i].splice(index, 1); - } - this.columns -= 1; - return this; - } - - /** - * Adds a column at the given index - * @param {number} [index = this.columns] - Column index - * @param {Array|Matrix} array - Array or vector - * @return {Matrix} this - */ - addColumn(index, array) { - if (typeof array === 'undefined') { - array = index; - index = this.columns; - } - checkColumnIndex(this, index, true); - array = checkColumnVector(this, array); - for (var i = 0; i < this.rows; i++) { - this[i].splice(index, 0, array[i]); - } - this.columns += 1; - return this; - } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/wrap/WrapperMatrix1D.js - - - -class WrapperMatrix1D_WrapperMatrix1D extends AbstractMatrix() { - /** - * @class WrapperMatrix1D - * @param {Array} data - * @param {object} [options] - * @param {object} [options.rows = 1] - */ - constructor(data, options = {}) { - const { rows = 1 } = options; - - if (data.length % rows !== 0) { - throw new Error('the data length is not divisible by the number of rows'); - } - super(); - this.rows = rows; - this.columns = data.length / rows; - this.data = data; - } - - set(rowIndex, columnIndex, value) { - var index = this._calculateIndex(rowIndex, columnIndex); - this.data[index] = value; - return this; - } - - get(rowIndex, columnIndex) { - var index = this._calculateIndex(rowIndex, columnIndex); - return this.data[index]; - } - - _calculateIndex(row, column) { - return row * this.columns + column; - } - - static get [Symbol.species]() { - return matrix_Matrix; - } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/wrap/WrapperMatrix2D.js - - - -class WrapperMatrix2D_WrapperMatrix2D extends AbstractMatrix() { - /** - * @class WrapperMatrix2D - * @param {Array>} data - */ - constructor(data) { - super(); - this.data = data; - this.rows = data.length; - this.columns = data[0].length; - } - - set(rowIndex, columnIndex, value) { - this.data[rowIndex][columnIndex] = value; - return this; - } - - get(rowIndex, columnIndex) { - return this.data[rowIndex][columnIndex]; - } - - static get [Symbol.species]() { - return matrix_Matrix; - } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/wrap/wrap.js - - - -/** - * @param {Array>|Array} array - * @param {object} [options] - * @param {object} [options.rows = 1] - * @return {WrapperMatrix1D|WrapperMatrix2D} - */ -function wrap(array, options) { - if (Array.isArray(array)) { - if (array[0] && Array.isArray(array[0])) { - return new WrapperMatrix2D_WrapperMatrix2D(array); - } else { - return new WrapperMatrix1D_WrapperMatrix1D(array, options); - } - } else { - throw new Error('the argument is not an array'); - } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/dc/qr.js - - - - -/** - * @class QrDecomposition - * @link https://github.com/lutzroeder/Mapack/blob/master/Source/QrDecomposition.cs - * @param {Matrix} value - */ -class qr_QrDecomposition { - constructor(value) { - value = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(value); - - var qr = value.clone(); - var m = value.rows; - var n = value.columns; - var rdiag = new Array(n); - var i, j, k, s; - - for (k = 0; k < n; k++) { - var nrm = 0; - for (i = k; i < m; i++) { - nrm = hypotenuse(nrm, qr.get(i, k)); - } - if (nrm !== 0) { - if (qr.get(k, k) < 0) { - nrm = -nrm; - } - for (i = k; i < m; i++) { - qr.set(i, k, qr.get(i, k) / nrm); - } - qr.set(k, k, qr.get(k, k) + 1); - for (j = k + 1; j < n; j++) { - s = 0; - for (i = k; i < m; i++) { - s += qr.get(i, k) * qr.get(i, j); - } - s = -s / qr.get(k, k); - for (i = k; i < m; i++) { - qr.set(i, j, qr.get(i, j) + s * qr.get(i, k)); - } - } - } - rdiag[k] = -nrm; - } - - this.QR = qr; - this.Rdiag = rdiag; - } - - /** - * Solve a problem of least square (Ax=b) by using the QR decomposition. Useful when A is rectangular, but not working when A is singular. - * Example : We search to approximate x, with A matrix shape m*n, x vector size n, b vector size m (m > n). We will use : - * var qr = QrDecomposition(A); - * var x = qr.solve(b); - * @param {Matrix} value - Matrix 1D which is the vector b (in the equation Ax = b) - * @return {Matrix} - The vector x - */ - solve(value) { - value = matrix_Matrix.checkMatrix(value); - - var qr = this.QR; - var m = qr.rows; - - if (value.rows !== m) { - throw new Error('Matrix row dimensions must agree'); - } - if (!this.isFullRank()) { - throw new Error('Matrix is rank deficient'); - } - - var count = value.columns; - var X = value.clone(); - var n = qr.columns; - var i, j, k, s; - - for (k = 0; k < n; k++) { - for (j = 0; j < count; j++) { - s = 0; - for (i = k; i < m; i++) { - s += qr[i][k] * X[i][j]; - } - s = -s / qr[k][k]; - for (i = k; i < m; i++) { - X[i][j] += s * qr[i][k]; - } - } - } - for (k = n - 1; k >= 0; k--) { - for (j = 0; j < count; j++) { - X[k][j] /= this.Rdiag[k]; - } - for (i = 0; i < k; i++) { - for (j = 0; j < count; j++) { - X[i][j] -= X[k][j] * qr[i][k]; - } - } - } - - return X.subMatrix(0, n - 1, 0, count - 1); - } - - /** - * - * @return {boolean} - */ - isFullRank() { - var columns = this.QR.columns; - for (var i = 0; i < columns; i++) { - if (this.Rdiag[i] === 0) { - return false; - } - } - return true; - } - - /** - * - * @return {Matrix} - */ - get upperTriangularMatrix() { - var qr = this.QR; - var n = qr.columns; - var X = new matrix_Matrix(n, n); - var i, j; - for (i = 0; i < n; i++) { - for (j = 0; j < n; j++) { - if (i < j) { - X[i][j] = qr[i][j]; - } else if (i === j) { - X[i][j] = this.Rdiag[i]; - } else { - X[i][j] = 0; - } - } - } - return X; - } - - /** - * - * @return {Matrix} - */ - get orthogonalMatrix() { - var qr = this.QR; - var rows = qr.rows; - var columns = qr.columns; - var X = new matrix_Matrix(rows, columns); - var i, j, k, s; - - for (k = columns - 1; k >= 0; k--) { - for (i = 0; i < rows; i++) { - X[i][k] = 0; - } - X[k][k] = 1; - for (j = k; j < columns; j++) { - if (qr[k][k] !== 0) { - s = 0; - for (i = k; i < rows; i++) { - s += qr[i][k] * X[i][j]; - } - - s = -s / qr[k][k]; - - for (i = k; i < rows; i++) { - X[i][j] += s * qr[i][k]; - } - } - } - } - return X; - } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/decompositions.js - - - - - - -/** - * Computes the inverse of a Matrix - * @param {Matrix} matrix - * @param {boolean} [useSVD=false] - * @return {Matrix} - */ -function inverse(matrix, useSVD = false) { - matrix = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(matrix); - if (useSVD) { - return new svd_SingularValueDecomposition(matrix).inverse(); - } else { - return solve(matrix, matrix_Matrix.eye(matrix.rows)); - } -} - -/** - * - * @param {Matrix} leftHandSide - * @param {Matrix} rightHandSide - * @param {boolean} [useSVD = false] - * @return {Matrix} - */ -function solve(leftHandSide, rightHandSide, useSVD = false) { - leftHandSide = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(leftHandSide); - rightHandSide = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(rightHandSide); - if (useSVD) { - return new svd_SingularValueDecomposition(leftHandSide).solve(rightHandSide); - } else { - return leftHandSide.isSquare() - ? new lu_LuDecomposition(leftHandSide).solve(rightHandSide) - : new qr_QrDecomposition(leftHandSide).solve(rightHandSide); - } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/linearDependencies.js - - - - - -// function used by rowsDependencies -function xrange(n, exception) { - var range = []; - for (var i = 0; i < n; i++) { - if (i !== exception) { - range.push(i); - } - } - return range; -} - -// function used by rowsDependencies -function dependenciesOneRow( - error, - matrix, - index, - thresholdValue = 10e-10, - thresholdError = 10e-10 -) { - if (error > thresholdError) { - return new Array(matrix.rows + 1).fill(0); - } else { - var returnArray = matrix.addRow(index, [0]); - for (var i = 0; i < returnArray.rows; i++) { - if (Math.abs(returnArray.get(i, 0)) < thresholdValue) { - returnArray.set(i, 0, 0); - } - } - return returnArray.to1DArray(); - } -} - -/** - * Creates a matrix which represents the dependencies between rows. - * If a row is a linear combination of others rows, the result will be a row with the coefficients of this combination. - * For example : for A = [[2, 0, 0, 1], [0, 1, 6, 0], [0, 3, 0, 1], [0, 0, 1, 0], [0, 1, 2, 0]], the result will be [[0, 0, 0, 0, 0], [0, 0, 0, 4, 1], [0, 0, 0, 0, 0], [0, 0.25, 0, 0, -0.25], [0, 1, 0, -4, 0]] - * @param {Matrix} matrix - * @param {Object} [options] includes thresholdValue and thresholdError. - * @param {number} [options.thresholdValue = 10e-10] If an absolute value is inferior to this threshold, it will equals zero. - * @param {number} [options.thresholdError = 10e-10] If the error is inferior to that threshold, the linear combination found is accepted and the row is dependent from other rows. - * @return {Matrix} the matrix which represents the dependencies between rows. - */ - -function linearDependencies(matrix, options = {}) { - const { thresholdValue = 10e-10, thresholdError = 10e-10 } = options; - - var n = matrix.rows; - var results = new matrix_Matrix(n, n); - - for (var i = 0; i < n; i++) { - var b = matrix_Matrix.columnVector(matrix.getRow(i)); - var Abis = matrix.subMatrixRow(xrange(n, i)).transposeView(); - var svd = new svd_SingularValueDecomposition(Abis); - var x = svd.solve(b); - var error = lib_es6( - matrix_Matrix.sub(b, Abis.mmul(x)) - .abs() - .to1DArray() - ); - results.setRow( - i, - dependenciesOneRow(error, x, i, thresholdValue, thresholdError) - ); - } - return results; -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/dc/evd.js - - - - -/** - * @class EigenvalueDecomposition - * @link https://github.com/lutzroeder/Mapack/blob/master/Source/EigenvalueDecomposition.cs - * @param {Matrix} matrix - * @param {object} [options] - * @param {boolean} [options.assumeSymmetric=false] - */ -class evd_EigenvalueDecomposition { - constructor(matrix, options = {}) { - const { assumeSymmetric = false } = options; - - matrix = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(matrix); - if (!matrix.isSquare()) { - throw new Error('Matrix is not a square matrix'); - } - - var n = matrix.columns; - var V = getFilled2DArray(n, n, 0); - var d = new Array(n); - var e = new Array(n); - var value = matrix; - var i, j; - - var isSymmetric = false; - if (assumeSymmetric) { - isSymmetric = true; - } else { - isSymmetric = matrix.isSymmetric(); - } - - if (isSymmetric) { - for (i = 0; i < n; i++) { - for (j = 0; j < n; j++) { - V[i][j] = value.get(i, j); - } - } - tred2(n, e, d, V); - tql2(n, e, d, V); - } else { - var H = getFilled2DArray(n, n, 0); - var ort = new Array(n); - for (j = 0; j < n; j++) { - for (i = 0; i < n; i++) { - H[i][j] = value.get(i, j); - } - } - orthes(n, H, ort, V); - hqr2(n, e, d, V, H); - } - - this.n = n; - this.e = e; - this.d = d; - this.V = V; - } - - /** - * - * @return {Array} - */ - get realEigenvalues() { - return this.d; - } - - /** - * - * @return {Array} - */ - get imaginaryEigenvalues() { - return this.e; - } - - /** - * - * @return {Matrix} - */ - get eigenvectorMatrix() { - if (!matrix_Matrix.isMatrix(this.V)) { - this.V = new matrix_Matrix(this.V); - } - return this.V; - } - - /** - * - * @return {Matrix} - */ - get diagonalMatrix() { - var n = this.n; - var e = this.e; - var d = this.d; - var X = new matrix_Matrix(n, n); - var i, j; - for (i = 0; i < n; i++) { - for (j = 0; j < n; j++) { - X[i][j] = 0; - } - X[i][i] = d[i]; - if (e[i] > 0) { - X[i][i + 1] = e[i]; - } else if (e[i] < 0) { - X[i][i - 1] = e[i]; - } - } - return X; - } -} - -function tred2(n, e, d, V) { - var f, g, h, i, j, k, hh, scale; - - for (j = 0; j < n; j++) { - d[j] = V[n - 1][j]; - } - - for (i = n - 1; i > 0; i--) { - scale = 0; - h = 0; - for (k = 0; k < i; k++) { - scale = scale + Math.abs(d[k]); - } - - if (scale === 0) { - e[i] = d[i - 1]; - for (j = 0; j < i; j++) { - d[j] = V[i - 1][j]; - V[i][j] = 0; - V[j][i] = 0; - } - } else { - for (k = 0; k < i; k++) { - d[k] /= scale; - h += d[k] * d[k]; - } - - f = d[i - 1]; - g = Math.sqrt(h); - if (f > 0) { - g = -g; - } - - e[i] = scale * g; - h = h - f * g; - d[i - 1] = f - g; - for (j = 0; j < i; j++) { - e[j] = 0; - } - - for (j = 0; j < i; j++) { - f = d[j]; - V[j][i] = f; - g = e[j] + V[j][j] * f; - for (k = j + 1; k <= i - 1; k++) { - g += V[k][j] * d[k]; - e[k] += V[k][j] * f; - } - e[j] = g; - } - - f = 0; - for (j = 0; j < i; j++) { - e[j] /= h; - f += e[j] * d[j]; - } - - hh = f / (h + h); - for (j = 0; j < i; j++) { - e[j] -= hh * d[j]; - } - - for (j = 0; j < i; j++) { - f = d[j]; - g = e[j]; - for (k = j; k <= i - 1; k++) { - V[k][j] -= f * e[k] + g * d[k]; - } - d[j] = V[i - 1][j]; - V[i][j] = 0; - } - } - d[i] = h; - } - - for (i = 0; i < n - 1; i++) { - V[n - 1][i] = V[i][i]; - V[i][i] = 1; - h = d[i + 1]; - if (h !== 0) { - for (k = 0; k <= i; k++) { - d[k] = V[k][i + 1] / h; - } - - for (j = 0; j <= i; j++) { - g = 0; - for (k = 0; k <= i; k++) { - g += V[k][i + 1] * V[k][j]; - } - for (k = 0; k <= i; k++) { - V[k][j] -= g * d[k]; - } - } - } - - for (k = 0; k <= i; k++) { - V[k][i + 1] = 0; - } - } - - for (j = 0; j < n; j++) { - d[j] = V[n - 1][j]; - V[n - 1][j] = 0; - } - - V[n - 1][n - 1] = 1; - e[0] = 0; -} - -function tql2(n, e, d, V) { - var g, h, i, j, k, l, m, p, r, dl1, c, c2, c3, el1, s, s2, iter; - - for (i = 1; i < n; i++) { - e[i - 1] = e[i]; - } - - e[n - 1] = 0; - - var f = 0; - var tst1 = 0; - var eps = Number.EPSILON; - - for (l = 0; l < n; l++) { - tst1 = Math.max(tst1, Math.abs(d[l]) + Math.abs(e[l])); - m = l; - while (m < n) { - if (Math.abs(e[m]) <= eps * tst1) { - break; - } - m++; - } - - if (m > l) { - iter = 0; - do { - iter = iter + 1; - - g = d[l]; - p = (d[l + 1] - g) / (2 * e[l]); - r = hypotenuse(p, 1); - if (p < 0) { - r = -r; - } - - d[l] = e[l] / (p + r); - d[l + 1] = e[l] * (p + r); - dl1 = d[l + 1]; - h = g - d[l]; - for (i = l + 2; i < n; i++) { - d[i] -= h; - } - - f = f + h; - - p = d[m]; - c = 1; - c2 = c; - c3 = c; - el1 = e[l + 1]; - s = 0; - s2 = 0; - for (i = m - 1; i >= l; i--) { - c3 = c2; - c2 = c; - s2 = s; - g = c * e[i]; - h = c * p; - r = hypotenuse(p, e[i]); - e[i + 1] = s * r; - s = e[i] / r; - c = p / r; - p = c * d[i] - s * g; - d[i + 1] = h + s * (c * g + s * d[i]); - - for (k = 0; k < n; k++) { - h = V[k][i + 1]; - V[k][i + 1] = s * V[k][i] + c * h; - V[k][i] = c * V[k][i] - s * h; - } - } - - p = -s * s2 * c3 * el1 * e[l] / dl1; - e[l] = s * p; - d[l] = c * p; - } while (Math.abs(e[l]) > eps * tst1); - } - d[l] = d[l] + f; - e[l] = 0; - } - - for (i = 0; i < n - 1; i++) { - k = i; - p = d[i]; - for (j = i + 1; j < n; j++) { - if (d[j] < p) { - k = j; - p = d[j]; - } - } - - if (k !== i) { - d[k] = d[i]; - d[i] = p; - for (j = 0; j < n; j++) { - p = V[j][i]; - V[j][i] = V[j][k]; - V[j][k] = p; - } - } - } -} - -function orthes(n, H, ort, V) { - var low = 0; - var high = n - 1; - var f, g, h, i, j, m; - var scale; - - for (m = low + 1; m <= high - 1; m++) { - scale = 0; - for (i = m; i <= high; i++) { - scale = scale + Math.abs(H[i][m - 1]); - } - - if (scale !== 0) { - h = 0; - for (i = high; i >= m; i--) { - ort[i] = H[i][m - 1] / scale; - h += ort[i] * ort[i]; - } - - g = Math.sqrt(h); - if (ort[m] > 0) { - g = -g; - } - - h = h - ort[m] * g; - ort[m] = ort[m] - g; - - for (j = m; j < n; j++) { - f = 0; - for (i = high; i >= m; i--) { - f += ort[i] * H[i][j]; - } - - f = f / h; - for (i = m; i <= high; i++) { - H[i][j] -= f * ort[i]; - } - } - - for (i = 0; i <= high; i++) { - f = 0; - for (j = high; j >= m; j--) { - f += ort[j] * H[i][j]; - } - - f = f / h; - for (j = m; j <= high; j++) { - H[i][j] -= f * ort[j]; - } - } - - ort[m] = scale * ort[m]; - H[m][m - 1] = scale * g; - } - } - - for (i = 0; i < n; i++) { - for (j = 0; j < n; j++) { - V[i][j] = i === j ? 1 : 0; - } - } - - for (m = high - 1; m >= low + 1; m--) { - if (H[m][m - 1] !== 0) { - for (i = m + 1; i <= high; i++) { - ort[i] = H[i][m - 1]; - } - - for (j = m; j <= high; j++) { - g = 0; - for (i = m; i <= high; i++) { - g += ort[i] * V[i][j]; - } - - g = g / ort[m] / H[m][m - 1]; - for (i = m; i <= high; i++) { - V[i][j] += g * ort[i]; - } - } - } - } -} - -function hqr2(nn, e, d, V, H) { - var n = nn - 1; - var low = 0; - var high = nn - 1; - var eps = Number.EPSILON; - var exshift = 0; - var norm = 0; - var p = 0; - var q = 0; - var r = 0; - var s = 0; - var z = 0; - var iter = 0; - var i, j, k, l, m, t, w, x, y; - var ra, sa, vr, vi; - var notlast, cdivres; - - for (i = 0; i < nn; i++) { - if (i < low || i > high) { - d[i] = H[i][i]; - e[i] = 0; - } - - for (j = Math.max(i - 1, 0); j < nn; j++) { - norm = norm + Math.abs(H[i][j]); - } - } - - while (n >= low) { - l = n; - while (l > low) { - s = Math.abs(H[l - 1][l - 1]) + Math.abs(H[l][l]); - if (s === 0) { - s = norm; - } - if (Math.abs(H[l][l - 1]) < eps * s) { - break; - } - l--; - } - - if (l === n) { - H[n][n] = H[n][n] + exshift; - d[n] = H[n][n]; - e[n] = 0; - n--; - iter = 0; - } else if (l === n - 1) { - w = H[n][n - 1] * H[n - 1][n]; - p = (H[n - 1][n - 1] - H[n][n]) / 2; - q = p * p + w; - z = Math.sqrt(Math.abs(q)); - H[n][n] = H[n][n] + exshift; - H[n - 1][n - 1] = H[n - 1][n - 1] + exshift; - x = H[n][n]; - - if (q >= 0) { - z = p >= 0 ? p + z : p - z; - d[n - 1] = x + z; - d[n] = d[n - 1]; - if (z !== 0) { - d[n] = x - w / z; - } - e[n - 1] = 0; - e[n] = 0; - x = H[n][n - 1]; - s = Math.abs(x) + Math.abs(z); - p = x / s; - q = z / s; - r = Math.sqrt(p * p + q * q); - p = p / r; - q = q / r; - - for (j = n - 1; j < nn; j++) { - z = H[n - 1][j]; - H[n - 1][j] = q * z + p * H[n][j]; - H[n][j] = q * H[n][j] - p * z; - } - - for (i = 0; i <= n; i++) { - z = H[i][n - 1]; - H[i][n - 1] = q * z + p * H[i][n]; - H[i][n] = q * H[i][n] - p * z; - } - - for (i = low; i <= high; i++) { - z = V[i][n - 1]; - V[i][n - 1] = q * z + p * V[i][n]; - V[i][n] = q * V[i][n] - p * z; - } - } else { - d[n - 1] = x + p; - d[n] = x + p; - e[n - 1] = z; - e[n] = -z; - } - - n = n - 2; - iter = 0; - } else { - x = H[n][n]; - y = 0; - w = 0; - if (l < n) { - y = H[n - 1][n - 1]; - w = H[n][n - 1] * H[n - 1][n]; - } - - if (iter === 10) { - exshift += x; - for (i = low; i <= n; i++) { - H[i][i] -= x; - } - s = Math.abs(H[n][n - 1]) + Math.abs(H[n - 1][n - 2]); - x = y = 0.75 * s; - w = -0.4375 * s * s; - } - - if (iter === 30) { - s = (y - x) / 2; - s = s * s + w; - if (s > 0) { - s = Math.sqrt(s); - if (y < x) { - s = -s; - } - s = x - w / ((y - x) / 2 + s); - for (i = low; i <= n; i++) { - H[i][i] -= s; - } - exshift += s; - x = y = w = 0.964; - } - } - - iter = iter + 1; - - m = n - 2; - while (m >= l) { - z = H[m][m]; - r = x - z; - s = y - z; - p = (r * s - w) / H[m + 1][m] + H[m][m + 1]; - q = H[m + 1][m + 1] - z - r - s; - r = H[m + 2][m + 1]; - s = Math.abs(p) + Math.abs(q) + Math.abs(r); - p = p / s; - q = q / s; - r = r / s; - if (m === l) { - break; - } - if ( - Math.abs(H[m][m - 1]) * (Math.abs(q) + Math.abs(r)) < - eps * - (Math.abs(p) * - (Math.abs(H[m - 1][m - 1]) + - Math.abs(z) + - Math.abs(H[m + 1][m + 1]))) - ) { - break; - } - m--; - } - - for (i = m + 2; i <= n; i++) { - H[i][i - 2] = 0; - if (i > m + 2) { - H[i][i - 3] = 0; - } - } - - for (k = m; k <= n - 1; k++) { - notlast = k !== n - 1; - if (k !== m) { - p = H[k][k - 1]; - q = H[k + 1][k - 1]; - r = notlast ? H[k + 2][k - 1] : 0; - x = Math.abs(p) + Math.abs(q) + Math.abs(r); - if (x !== 0) { - p = p / x; - q = q / x; - r = r / x; - } - } - - if (x === 0) { - break; - } - - s = Math.sqrt(p * p + q * q + r * r); - if (p < 0) { - s = -s; - } - - if (s !== 0) { - if (k !== m) { - H[k][k - 1] = -s * x; - } else if (l !== m) { - H[k][k - 1] = -H[k][k - 1]; - } - - p = p + s; - x = p / s; - y = q / s; - z = r / s; - q = q / p; - r = r / p; - - for (j = k; j < nn; j++) { - p = H[k][j] + q * H[k + 1][j]; - if (notlast) { - p = p + r * H[k + 2][j]; - H[k + 2][j] = H[k + 2][j] - p * z; - } - - H[k][j] = H[k][j] - p * x; - H[k + 1][j] = H[k + 1][j] - p * y; - } - - for (i = 0; i <= Math.min(n, k + 3); i++) { - p = x * H[i][k] + y * H[i][k + 1]; - if (notlast) { - p = p + z * H[i][k + 2]; - H[i][k + 2] = H[i][k + 2] - p * r; - } - - H[i][k] = H[i][k] - p; - H[i][k + 1] = H[i][k + 1] - p * q; - } - - for (i = low; i <= high; i++) { - p = x * V[i][k] + y * V[i][k + 1]; - if (notlast) { - p = p + z * V[i][k + 2]; - V[i][k + 2] = V[i][k + 2] - p * r; - } - - V[i][k] = V[i][k] - p; - V[i][k + 1] = V[i][k + 1] - p * q; - } - } - } - } - } - - if (norm === 0) { - return; - } - - for (n = nn - 1; n >= 0; n--) { - p = d[n]; - q = e[n]; - - if (q === 0) { - l = n; - H[n][n] = 1; - for (i = n - 1; i >= 0; i--) { - w = H[i][i] - p; - r = 0; - for (j = l; j <= n; j++) { - r = r + H[i][j] * H[j][n]; - } - - if (e[i] < 0) { - z = w; - s = r; - } else { - l = i; - if (e[i] === 0) { - H[i][n] = w !== 0 ? -r / w : -r / (eps * norm); - } else { - x = H[i][i + 1]; - y = H[i + 1][i]; - q = (d[i] - p) * (d[i] - p) + e[i] * e[i]; - t = (x * s - z * r) / q; - H[i][n] = t; - H[i + 1][n] = - Math.abs(x) > Math.abs(z) ? (-r - w * t) / x : (-s - y * t) / z; - } - - t = Math.abs(H[i][n]); - if (eps * t * t > 1) { - for (j = i; j <= n; j++) { - H[j][n] = H[j][n] / t; - } - } - } - } - } else if (q < 0) { - l = n - 1; - - if (Math.abs(H[n][n - 1]) > Math.abs(H[n - 1][n])) { - H[n - 1][n - 1] = q / H[n][n - 1]; - H[n - 1][n] = -(H[n][n] - p) / H[n][n - 1]; - } else { - cdivres = cdiv(0, -H[n - 1][n], H[n - 1][n - 1] - p, q); - H[n - 1][n - 1] = cdivres[0]; - H[n - 1][n] = cdivres[1]; - } - - H[n][n - 1] = 0; - H[n][n] = 1; - for (i = n - 2; i >= 0; i--) { - ra = 0; - sa = 0; - for (j = l; j <= n; j++) { - ra = ra + H[i][j] * H[j][n - 1]; - sa = sa + H[i][j] * H[j][n]; - } - - w = H[i][i] - p; - - if (e[i] < 0) { - z = w; - r = ra; - s = sa; - } else { - l = i; - if (e[i] === 0) { - cdivres = cdiv(-ra, -sa, w, q); - H[i][n - 1] = cdivres[0]; - H[i][n] = cdivres[1]; - } else { - x = H[i][i + 1]; - y = H[i + 1][i]; - vr = (d[i] - p) * (d[i] - p) + e[i] * e[i] - q * q; - vi = (d[i] - p) * 2 * q; - if (vr === 0 && vi === 0) { - vr = - eps * - norm * - (Math.abs(w) + - Math.abs(q) + - Math.abs(x) + - Math.abs(y) + - Math.abs(z)); - } - cdivres = cdiv( - x * r - z * ra + q * sa, - x * s - z * sa - q * ra, - vr, - vi - ); - H[i][n - 1] = cdivres[0]; - H[i][n] = cdivres[1]; - if (Math.abs(x) > Math.abs(z) + Math.abs(q)) { - H[i + 1][n - 1] = (-ra - w * H[i][n - 1] + q * H[i][n]) / x; - H[i + 1][n] = (-sa - w * H[i][n] - q * H[i][n - 1]) / x; - } else { - cdivres = cdiv(-r - y * H[i][n - 1], -s - y * H[i][n], z, q); - H[i + 1][n - 1] = cdivres[0]; - H[i + 1][n] = cdivres[1]; - } - } - - t = Math.max(Math.abs(H[i][n - 1]), Math.abs(H[i][n])); - if (eps * t * t > 1) { - for (j = i; j <= n; j++) { - H[j][n - 1] = H[j][n - 1] / t; - H[j][n] = H[j][n] / t; - } - } - } - } - } - } - - for (i = 0; i < nn; i++) { - if (i < low || i > high) { - for (j = i; j < nn; j++) { - V[i][j] = H[i][j]; - } - } - } - - for (j = nn - 1; j >= low; j--) { - for (i = low; i <= high; i++) { - z = 0; - for (k = low; k <= Math.min(j, high); k++) { - z = z + V[i][k] * H[k][j]; - } - V[i][j] = z; - } - } -} - -function cdiv(xr, xi, yr, yi) { - var r, d; - if (Math.abs(yr) > Math.abs(yi)) { - r = yi / yr; - d = yr + r * yi; - return [(xr + r * xi) / d, (xi - r * xr) / d]; - } else { - r = yr / yi; - d = yi + r * yr; - return [(r * xr + xi) / d, (r * xi - xr) / d]; - } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/dc/cholesky.js - - -/** - * @class CholeskyDecomposition - * @link https://github.com/lutzroeder/Mapack/blob/master/Source/CholeskyDecomposition.cs - * @param {Matrix} value - */ -class cholesky_CholeskyDecomposition { - constructor(value) { - value = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(value); - if (!value.isSymmetric()) { - throw new Error('Matrix is not symmetric'); - } - - var a = value; - var dimension = a.rows; - var l = new matrix_Matrix(dimension, dimension); - var positiveDefinite = true; - var i, j, k; - - for (j = 0; j < dimension; j++) { - var Lrowj = l[j]; - var d = 0; - for (k = 0; k < j; k++) { - var Lrowk = l[k]; - var s = 0; - for (i = 0; i < k; i++) { - s += Lrowk[i] * Lrowj[i]; - } - Lrowj[k] = s = (a.get(j, k) - s) / l[k][k]; - d = d + s * s; - } - - d = a.get(j, j) - d; - - positiveDefinite &= d > 0; - l[j][j] = Math.sqrt(Math.max(d, 0)); - for (k = j + 1; k < dimension; k++) { - l[j][k] = 0; - } - } - - if (!positiveDefinite) { - throw new Error('Matrix is not positive definite'); - } - - this.L = l; - } - - /** - * - * @param {Matrix} value - * @return {Matrix} - */ - solve(value) { - value = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(value); - - var l = this.L; - var dimension = l.rows; - - if (value.rows !== dimension) { - throw new Error('Matrix dimensions do not match'); - } - - var count = value.columns; - var B = value.clone(); - var i, j, k; - - for (k = 0; k < dimension; k++) { - for (j = 0; j < count; j++) { - for (i = 0; i < k; i++) { - B[k][j] -= B[i][j] * l[k][i]; - } - B[k][j] /= l[k][k]; - } - } - - for (k = dimension - 1; k >= 0; k--) { - for (j = 0; j < count; j++) { - for (i = k + 1; i < dimension; i++) { - B[k][j] -= B[i][j] * l[i][k]; - } - B[k][j] /= l[k][k]; - } - } - - return B; - } - - /** - * - * @return {Matrix} - */ - get lowerTriangularMatrix() { - return this.L; - } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/index.js -/* concated harmony reexport default */__webpack_require__.d(__webpack_exports__, "default", function() { return matrix_Matrix; }); -/* concated harmony reexport Matrix */__webpack_require__.d(__webpack_exports__, "Matrix", function() { return matrix_Matrix; }); -/* concated harmony reexport abstractMatrix */__webpack_require__.d(__webpack_exports__, "abstractMatrix", function() { return AbstractMatrix; }); -/* concated harmony reexport wrap */__webpack_require__.d(__webpack_exports__, "wrap", function() { return wrap; }); -/* concated harmony reexport WrapperMatrix2D */__webpack_require__.d(__webpack_exports__, "WrapperMatrix2D", function() { return WrapperMatrix2D_WrapperMatrix2D; }); -/* concated harmony reexport WrapperMatrix1D */__webpack_require__.d(__webpack_exports__, "WrapperMatrix1D", function() { return WrapperMatrix1D_WrapperMatrix1D; }); -/* concated harmony reexport solve */__webpack_require__.d(__webpack_exports__, "solve", function() { return solve; }); -/* concated harmony reexport inverse */__webpack_require__.d(__webpack_exports__, "inverse", function() { return inverse; }); -/* concated harmony reexport linearDependencies */__webpack_require__.d(__webpack_exports__, "linearDependencies", function() { return linearDependencies; }); -/* concated harmony reexport SingularValueDecomposition */__webpack_require__.d(__webpack_exports__, "SingularValueDecomposition", function() { return svd_SingularValueDecomposition; }); -/* concated harmony reexport SVD */__webpack_require__.d(__webpack_exports__, "SVD", function() { return svd_SingularValueDecomposition; }); -/* concated harmony reexport EigenvalueDecomposition */__webpack_require__.d(__webpack_exports__, "EigenvalueDecomposition", function() { return evd_EigenvalueDecomposition; }); -/* concated harmony reexport EVD */__webpack_require__.d(__webpack_exports__, "EVD", function() { return evd_EigenvalueDecomposition; }); -/* concated harmony reexport CholeskyDecomposition */__webpack_require__.d(__webpack_exports__, "CholeskyDecomposition", function() { return cholesky_CholeskyDecomposition; }); -/* concated harmony reexport CHO */__webpack_require__.d(__webpack_exports__, "CHO", function() { return cholesky_CholeskyDecomposition; }); -/* concated harmony reexport LuDecomposition */__webpack_require__.d(__webpack_exports__, "LuDecomposition", function() { return lu_LuDecomposition; }); -/* concated harmony reexport LU */__webpack_require__.d(__webpack_exports__, "LU", function() { return lu_LuDecomposition; }); -/* concated harmony reexport QrDecomposition */__webpack_require__.d(__webpack_exports__, "QrDecomposition", function() { return qr_QrDecomposition; }); -/* concated harmony reexport QR */__webpack_require__.d(__webpack_exports__, "QR", function() { return qr_QrDecomposition; }); - - - - - - - - - - - - - - - /***/ }) /******/ ]); \ No newline at end of file diff --git a/lib/umap-js.min.js b/lib/umap-js.min.js index 767059c..f095978 100644 --- a/lib/umap-js.min.js +++ b/lib/umap-js.min.js @@ -1 +1 @@ -!function(t){var r={};function e(n){if(r[n])return r[n].exports;var i=r[n]={i:n,l:!1,exports:{}};return t[n].call(i.exports,i,i.exports,e),i.l=!0,i.exports}e.m=t,e.c=r,e.d=function(t,r,n){e.o(t,r)||Object.defineProperty(t,r,{enumerable:!0,get:n})},e.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},e.t=function(t,r){if(1&r&&(t=e(t)),8&r)return t;if(4&r&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(e.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&r&&"string"!=typeof t)for(var i in t)e.d(n,i,function(r){return t[r]}.bind(null,i));return n},e.n=function(t){var r=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(r,"a",r),r},e.o=function(t,r){return Object.prototype.hasOwnProperty.call(t,r)},e.p="",e(e.s=5)}([function(t,r,e){"use strict";const n=Object.prototype.toString;t.exports=function(t){return n.call(t).endsWith("Array]")}},function(t,r,e){"use strict";var n=this&&this.__values||function(t){var r="function"==typeof Symbol&&t[Symbol.iterator],e=0;return r?r.call(t):{next:function(){return t&&e>=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}};function i(t,r){return void 0===r&&(r=Math.random),Math.floor(r()*t)}function o(t){return void 0===t&&(t=Math.random),t()}function s(t){for(var r=[],e=0;er?t[e]:r;return r},r.max2d=function(t){for(var r=0,e=0;er?t[e][n]:r;return r},r.rejectionSample=function(t,r){for(var e=h(t),n=0;n=a[0])return 0;for(var h=0;h=s[0])return 0;s[0]=e,o[0]=n,a[0]=i;for(var h=0,u=0;;){var l=2*h+1,c=l+1,f=t[0][0].length;if(l>=f)break;if(c>=f){if(!(s[l]>e))break;u=l}else if(s[l]>=s[c]){if(!(ee.length;a++)1===i[a]&&n[a]=0?(i[s]=0,Math.floor(e[s])):-1}},function(t,r,e){"use strict";var n,i=this&&this.__read||function(t,r){var e="function"==typeof Symbol&&t[Symbol.iterator];if(!e)return t;var n,i,o=e.call(t),s=[];try{for(;(void 0===r||r-- >0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},o=this&&this.__spread||function(){for(var t=[],r=0;r=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}};Object.defineProperty(r,"__esModule",{value:!0});var a=e(1),h=function(){function t(t,r,e,n){this.entries=new Map,this.nRows=0,this.nCols=0,this.rows=o(t),this.cols=o(r),this.values=o(e);for(var i=0;ir?t:r})},r.multiplyScalar=function(t,r){return t.map(function(t){return t*r})},r.eliminateZeros=function(t){for(var r=new Set,e=t.getValues(),n=t.getRows(),i=t.getCols(),o=0;or?t[e]:r;return t.map(function(t){return t/r})},n.l1=function(t){for(var r=0,e=0;e0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},i=this&&this.__spread||function(){for(var t=[],r=0;r=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}};Object.defineProperty(r,"__esModule",{value:!0});var s=e(1),a=function(){return function(t,r,e,n){this.hyperplanes=t,this.offsets=r,this.children=e,this.indices=n}}();function h(t,r,e){for(var n=r,i=0;i0?0:1}r.FlatTree=a,r.makeForest=function(t,r,e,n){var o=Math.max(10,r);return s.range(e).map(function(r,e){return function(t,r,e,n){void 0===r&&(r=30);var i=s.range(t.length);return function t(r,e,n,i,o){if(void 0===n&&(n=30),e.length>n){var a=function(t,r,e){var n=t[0].length,i=s.tauRandInt(r.length,e),o=s.tauRandInt(r.length,e);o=(o+=i===o?1:0)%r.length;for(var a=r[i],h=r[o],u=0,l=s.zeros(n),c=0;c0?(g[c]=0,f+=1):(g[c]=1,m+=1)}var d=s.zeros(f),w=s.zeros(m);for(var c in f=0,m=0,s.range(g.length))0===g[c]?(d[f]=r[c],f+=1):(w[m]=r[c],m+=1);return{indicesLeft:d,indicesRight:w,hyperplane:l,offset:u}}(r,e,o),h=a.indicesLeft,u=a.indicesRight,l=a.hyperplane,c=a.offset,f=t(r,h,n,i+1,o),m=t(r,u,n,i+1,o),g={leftChild:f,rightChild:m,isLeaf:!1,hyperplane:l,offset:c};return g}var g={indices:e,isLeaf:!0};return g}(t,i,r,e,n)}(t,o,e,n)}).map(function(t){return function(t,r){var e=function t(r){return r.isLeaf?1:1+t(r.leftChild)+t(r.rightChild)}(t),n=function t(r){return r.isLeaf?1:t(r.leftChild)+t(r.rightChild)}(t),o=s.range(e).map(function(){return s.zeros(t.hyperplane.length)}),h=s.zeros(e),u=s.range(e).map(function(){return[-1,-1]}),l=s.range(n).map(function(){return s.range(r).map(function(){return-1})});return function t(r,e,n,o,s,a,h){var u;if(r.isLeaf)return o[a][0]=-h,(u=s[h]).splice.apply(u,i([0,r.indices.length],r.indices)),{nodeNum:a,leafNum:h+=1};e[a]=r.hyperplane,n[a]=r.offset,o[a][0]=a+1;var l=a,c=t(r.leftChild,e,n,o,s,a+1,h);return a=c.nodeNum,h=c.leafNum,o[l][1]=a+1,{nodeNum:(c=t(r.rightChild,e,n,o,s,a+1,h)).nodeNum,leafNum:c.leafNum}}(t,o,h,u,l,0,0),new a(o,h,u,l)}(t,o)})},r.makeLeafArray=function(t){var r,e;if(t.length>0){var n=[];try{for(var s=o(t),a=s.next();!a.done;a=s.next()){var h=a.value;n.push.apply(n,i(h.indices))}}catch(t){r={error:t}}finally{try{a&&!a.done&&(e=s.return)&&e.call(s)}finally{if(r)throw r.error}}return n}return[[-1]]},r.searchFlatTree=function(t,r){for(var e=0;r.children[e][0]>0;)e=0===h(r.hyperplanes[e],r.offsets[e],t)?r.children[e][0]:r.children[e][1];var n=-1*r.children[e][0];return r.indices[n]}},function(t,r,e){"use strict";Object.defineProperty(r,"__esModule",{value:!0});var n=e(6);window.UMAP=n.UMAP},function(t,r,e){"use strict";var n=this&&this.__awaiter||function(t,r,e,n){return new(e||(e=Promise))(function(i,o){function s(t){try{h(n.next(t))}catch(t){o(t)}}function a(t){try{h(n.throw(t))}catch(t){o(t)}}function h(t){t.done?i(t.value):new e(function(r){r(t.value)}).then(s,a)}h((n=n.apply(t,r||[])).next())})},i=this&&this.__generator||function(t,r){var e,n,i,o,s={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function a(o){return function(a){return function(o){if(e)throw new TypeError("Generator is already executing.");for(;s;)try{if(e=1,n&&(i=2&o[0]?n.return:o[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,o[1])).done)return i;switch(n=0,i&&(o=[2&o[0],i.value]),o[0]){case 0:case 1:i=o;break;case 4:return s.label++,{value:o[1],done:!1};case 5:s.label++,n=o[1],o=[0];continue;case 7:o=s.ops.pop(),s.trys.pop();continue;default:if(!(i=(i=s.trys).length>0&&i[i.length-1])&&(6===o[0]||2===o[0])){s=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},s=this&&this.__spread||function(){for(var t=[],r=0;r0&&i.set(o,l,c)}var f=h.transpose(i);return h.maximum(i,f)},t.prototype.transform=function(t){var r=this,e=this.X;if(void 0===e||0===e.length)throw new Error("No data has been fit.");var n=Math.floor(this.nNeighbors*this.transformQueueSize),i=u.initializeSearch(this.rpForest,e,t,n,this.initFromRandom,this.initFromTree),o=this.search(e,this.searchGraph,i,t),s=a.deheapSort(o),l=s.indices,f=s.weights;l=l.map(function(t){return t.slice(0,r.nNeighbors)}),f=f.map(function(t){return t.slice(0,r.nNeighbors)});var m=Math.max(0,this.localConnectivity-1),g=this.smoothKNNDistance(f,this.nNeighbors,m),p=g.sigmas,v=g.rhos,d=this.computeMembershipStrengths(l,f,p,v),w=d.rows,y=d.cols,b=d.vals,x=[t.length,e.length],S=new h.SparseMatrix(w,y,b,x),E=h.normalize(S,"l1"),R=h.getCSR(E),k=t.length,z=M(c.reshape2d(R.indices,k,this.nNeighbors),c.reshape2d(R.values,k,this.nNeighbors),this.embedding),N=this.nEpochs?this.nEpochs/3:S.nRows<=1e4?100:30,A=S.getValues().reduce(function(t,r){return r>t?r:t},0);S=S.map(function(t){return t0});if(g.length>=e){var p=Math.floor(e),v=e-p;p>0?(s[h]=g[p-1],v>1e-5&&(s[h]+=v*(g[p]-g[p-1]))):s[h]=v*g[0]}else g.length>0&&(s[h]=c.max(g));for(var d=0;d0?Math.exp(-b/f):1}if(Math.abs(w-o)<1e-5)break;w>o?f=(u+(l=f))/2:(u=f,l===1/0?f*=2:f=(u+l)/2)}if(a[h]=f,s[h]>0){var M=c.mean(m);a[h]<.001*M&&(a[h]=.001*M)}else{var x=c.mean(t.map(c.mean));a[h]<.001*x&&(a[h]=.001*x)}}return{sigmas:a,rhos:s}},t.prototype.computeMembershipStrengths=function(t,r,e,n){for(var i=t.length,o=t[0].length,s=c.zeros(i*o),a=c.zeros(i*o),h=c.zeros(i*o),u=0;u0&&(e[n]=r/i[n])}),e},t.prototype.assignOptimizationStateParameters=function(t){Object.assign(this.optimizationState,t)},t.prototype.prepareForOptimizationLoop=function(){var t=this.repulsionStrength,r=this.learningRate,e=this.negativeSampleRate,n=this.optimizationState,i=n.epochsPerSample,o=n.headEmbedding,a=n.tailEmbedding,h=o[0].length,u=o.length===a.length,l=i.map(function(t){return t/e}),c=s(l),f=s(i);this.assignOptimizationStateParameters({epochOfNextSample:f,epochOfNextNegativeSample:c,epochsPerNegativeSample:l,moveOther:u,initialAlpha:r,alpha:r,gamma:t,dim:h})},t.prototype.initializeOptimization=function(){var t=this.embedding,r=this.embedding,e=this.optimizationState,n=e.head,i=e.tail,o=e.epochsPerSample,s=this.getNEpochs(),a=this.graph.nCols,h=w(this.spread,this.minDist),u=h.a,l=h.b;this.assignOptimizationStateParameters({headEmbedding:t,tailEmbedding:r,head:n,tail:i,epochsPerSample:o,a:u,b:l,nEpochs:s,nVertices:a})},t.prototype.optimizeLayoutStep=function(t){for(var r=this.optimizationState,e=r.head,n=r.tail,i=r.headEmbedding,o=r.tailEmbedding,s=r.epochsPerSample,a=r.epochOfNextSample,h=r.epochOfNextNegativeSample,u=r.epochsPerNegativeSample,l=r.moveOther,f=r.initialAlpha,m=r.alpha,g=r.gamma,p=r.a,w=r.b,y=r.dim,b=r.nEpochs,M=r.nVertices,x=0;xt)){var S=e[x],E=n[x],R=i[S],k=o[E],z=d(R,k),N=0;z>0&&(N=-2*p*w*Math.pow(z,w-1),N/=p*Math.pow(z,w)+1);for(var A=0;A0)_=2*g*w,_/=(.001+D)*(p*Math.pow(D,w)+1);else if(S===P)continue;for(A=0;A0&&(V=v(_*(R[A]-j[A]),4)),R[A]+=V*m}}h[x]+=C*u[x]}return r.alpha=f*(1-t/b),r.currentEpoch+=1,i},t.prototype.optimizeLayoutAsync=function(t){var r=this;return void 0===t&&(t=function(){return!0}),new Promise(function(e,o){var s=function(){return n(r,void 0,void 0,function(){var r,n,a,h,u,l;return i(this,function(i){try{if(r=this.optimizationState,n=r.nEpochs,a=r.currentEpoch,this.embedding=this.optimizeLayoutStep(a),h=this.optimizationState.currentEpoch,u=!1===t(h),l=h===n,u||l)return[2,e(l)];s()}catch(t){o(t)}return[2]})})};s()})},t.prototype.optimizeLayout=function(t){void 0===t&&(t=function(){return!0});for(var r=!1,e=[];!r;){var n=this.optimizationState,i=n.nEpochs,o=n.currentEpoch;e=this.optimizeLayoutStep(o);var s=this.optimizationState.currentEpoch,a=!1===t(s);r=s===i||a}return e},t.prototype.getNEpochs=function(){var t=this.graph;if(this.nEpochs>0)return this.nEpochs;var r=t.nRows;return r<=2500?500:r<=5e3?400:r<=7500?300:200},t}();function g(t,r){for(var e=0,n=0;nr?r:t<-r?-r:t}function d(t,r){for(var e=0,n=0;n=r?Math.exp(-(e[i]-r)/t):n}),i=f({x:e,y:n},function(t){var r=o(t,2),e=r[0],n=r[1];return function(t){return 1/(1+e*Math.pow(t,2*n))}},{damping:1.5,initialValues:[.5,.5],gradientDifference:.1,maxIterations:100,errorTolerance:.01}).parameterValues,s=o(i,2);return{a:s[0],b:s[1]}}function y(t,r,e,n){return void 0===e&&(e=1),void 0===n&&(n=5),t.map(function(t,i,o){return-1===r[i]||-1===r[o]?t*Math.exp(-e):r[i]!==r[o]?t*Math.exp(-n):t})}function b(t){t=h.normalize(t,"max");var r=h.transpose(t),e=h.pairwiseMultiply(r,t);return t=h.add(t,h.subtract(r,e)),h.eliminateZeros(t)}function M(t,r,e){for(var n=c.zeros(t.length).map(function(t){return c.zeros(e[0].length)}),i=0;i=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}};Object.defineProperty(r,"__esModule",{value:!0});var i=e(2),o=e(3),s=e(4),a=e(1);r.makeNNDescent=function(t,r){return function(e,n,o,s,h,u,l,c){void 0===s&&(s=10),void 0===h&&(h=50),void 0===u&&(u=.001),void 0===l&&(l=.5),void 0===c&&(c=!0);for(var f=e.length,m=i.makeHeap(e.length,o),g=0;gr&&(r=t[e]);return r};var s=function(t){if(!i()(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");for(var r=t[0],e=1;e1&&void 0!==arguments[1]?arguments[1]:{};if(!i()(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");if(void 0!==e.output){if(!i()(e.output))throw new TypeError("output option must be an array if specified");r=e.output}else r=new Array(t.length);var n=s(t),a=o(t);if(n===a)throw new RangeError("minimum and maximum input values are equal. Cannot rescale a constant array");var h=e.min,u=void 0===h?e.autoMinMax?n:0:h,l=e.max,c=void 0===l?e.autoMinMax?a:1:l;if(u>=c)throw new RangeError("min option must be smaller than max option");for(var f=(c-u)/(a-n),m=0;mMath.abs(h[i])&&(i=r);if(i!==e){for(n=0;n=0;i--){for(n=0;no?t[i][o]:i===o?1:0;return n}get upperTriangularMatrix(){for(var t=this.LU,r=t.rows,e=t.columns,n=new I(r,e),i=0;iMath.abs(r)?(e=r/t,Math.abs(t)*Math.sqrt(1+e*e)):0!==r?(e=t/r,Math.abs(r)*Math.sqrt(1+e*e)):0}function l(t,r,e){for(var n=new Array(t),i=0;i=0;t--)if(0!==v[t]){for(let r=t+1;r=0;t--){if(t0;){let t,r;for(t=R-2;t>=-1&&-1!==t;t--){const r=Number.MIN_VALUE+z*Math.abs(v[t]+Math.abs(v[t+1]));if(Math.abs(y[t])<=r||Number.isNaN(y[t])){y[t]=0;break}}if(t===R-2)r=4;else{let e;for(e=R-1;e>=t&&e!==t;e--){let r=(e!==R?Math.abs(y[e]):0)+(e!==t+1?Math.abs(y[e-1]):0);if(Math.abs(v[e])<=z*r){v[e]=0;break}}e===t?r=3:e===R-1?r=1:(r=2,t=e)}switch(t++,r){case 1:{let r=y[R-2];y[R-2]=0;for(let e=R-2;e>=t;e--){let i=u(v[e],r),o=v[e]/i,s=r/i;if(v[e]=i,e!==t&&(r=-s*y[e-1],y[e-1]=o*y[e-1]),c)for(let t=0;t=v[t+1]);){let r=v[t];if(v[t]=v[t+1],v[t+1]=r,c&&tr?i[o][e]=t[o][e]/this.s[e]:i[o][e]=0;var o=this.U,s=o.length,a=o[0].length,h=new I(e,s);for(let t=0;tt&&r++;return r}get diagonal(){return this.s}get threshold(){return Number.EPSILON/2*Math.max(this.m,this.n)*this.s[0]}get leftSingularVectors(){return I.isMatrix(this.U)||(this.U=new I(this.U)),this.U}get rightSingularVectors(){return I.isMatrix(this.V)||(this.V=new I(this.V)),this.V}get diagonalMatrix(){return I.diag(this.s)}}function f(t,r,e){var n=e?t.rows:t.rows-1;if(r<0||r>n)throw new RangeError("Row index out of range")}function m(t,r,e){var n=e?t.columns:t.columns-1;if(r<0||r>n)throw new RangeError("Column index out of range")}function g(t,r){if(r.to1DArray&&(r=r.to1DArray()),r.length!==t.columns)throw new RangeError("vector size must be the same as the number of columns");return r}function p(t,r){if(r.to1DArray&&(r=r.to1DArray()),r.length!==t.rows)throw new RangeError("vector size must be the same as the number of rows");return r}function v(t,r,e){return{row:d(t,r),column:w(t,e)}}function d(t,r){if("object"!=typeof r)throw new TypeError("unexpected type for row indices");if(r.some(r=>r<0||r>=t.rows))throw new RangeError("row indices are out of range");return Array.isArray(r)||(r=Array.from(r)),r}function w(t,r){if("object"!=typeof r)throw new TypeError("unexpected type for column indices");if(r.some(r=>r<0||r>=t.columns))throw new RangeError("column indices are out of range");return Array.isArray(r)||(r=Array.from(r)),r}function y(t,r,e,n,i){if(5!==arguments.length)throw new RangeError("expected 4 arguments");if(b("startRow",r),b("endRow",e),b("startColumn",n),b("endColumn",i),r>e||n>i||r<0||r>=t.rows||e<0||e>=t.rows||n<0||n>=t.columns||i<0||i>=t.columns)throw new RangeError("Submatrix indices are out of range")}function b(t,r){if("number"!=typeof r)throw new TypeError(`${t} must be a number`)}class M extends(C()){constructor(t,r,e){super(),this.matrix=t,this.rows=r,this.columns=e}static get[Symbol.species](){return I}}class x extends M{constructor(t){super(t,t.columns,t.rows)}set(t,r,e){return this.matrix.set(r,t,e),this}get(t,r){return this.matrix.get(r,t)}}class S extends M{constructor(t,r){super(t,1,t.columns),this.row=r}set(t,r,e){return this.matrix.set(this.row,r,e),this}get(t,r){return this.matrix.get(this.row,r)}}class E extends M{constructor(t,r,e,n,i){y(t,r,e,n,i),super(t,e-r+1,i-n+1),this.startRow=r,this.startColumn=n}set(t,r,e){return this.matrix.set(this.startRow+t,this.startColumn+r,e),this}get(t,r){return this.matrix.get(this.startRow+t,this.startColumn+r)}}class R extends M{constructor(t,r,e){var n=v(t,r,e);super(t,n.row.length,n.column.length),this.rowIndices=n.row,this.columnIndices=n.column}set(t,r,e){return this.matrix.set(this.rowIndices[t],this.columnIndices[r],e),this}get(t,r){return this.matrix.get(this.rowIndices[t],this.columnIndices[r])}}class k extends M{constructor(t,r){super(t,(r=d(t,r)).length,t.columns),this.rowIndices=r}set(t,r,e){return this.matrix.set(this.rowIndices[t],r,e),this}get(t,r){return this.matrix.get(this.rowIndices[t],r)}}class z extends M{constructor(t,r){r=w(t,r),super(t,t.rows,r.length),this.columnIndices=r}set(t,r,e){return this.matrix.set(t,this.columnIndices[r],e),this}get(t,r){return this.matrix.get(t,this.columnIndices[r])}}class N extends M{constructor(t,r){super(t,t.rows,1),this.column=r}set(t,r,e){return this.matrix.set(t,this.column,e),this}get(t){return this.matrix.get(t,this.column)}}class A extends M{constructor(t){super(t,t.rows,t.columns)}set(t,r,e){return this.matrix.set(this.rows-t-1,r,e),this}get(t,r){return this.matrix.get(this.rows-t-1,r)}}class V extends M{constructor(t){super(t,t.rows,t.columns)}set(t,r,e){return this.matrix.set(t,this.columns-r-1,e),this}get(t,r){return this.matrix.get(t,this.columns-r-1)}}function C(t){void 0===t&&(t=Object);class r extends t{static get[Symbol.species](){return this}static from1DArray(t,r,e){if(t*r!==e.length)throw new RangeError("Data length does not match given dimensions");for(var n=new this(t,r),i=0;it&&(t=this.get(r,e));return t}maxIndex(){for(var t=this.get(0,0),r=[0,0],e=0;et&&(t=this.get(e,n),r[0]=e,r[1]=n);return r}min(){for(var t=this.get(0,0),r=0;rr&&(r=this.get(t,e));return r}maxRowIndex(t){f(this,t);for(var r=this.get(t,0),e=[t,0],n=1;nr&&(r=this.get(t,n),e[1]=n);return e}minRow(t){f(this,t);for(var r=this.get(t,0),e=1;er&&(r=this.get(e,t));return r}maxColumnIndex(t){m(this,t);for(var r=this.get(0,t),e=[0,t],n=1;nr&&(r=this.get(n,t),e[0]=n);return e}minColumn(t){m(this,t);for(var r=this.get(0,t),e=1;e=(r=void 0===r?1:r))throw new RangeError("min should be strictly smaller than max");for(var e=this.constructor.empty(this.rows,this.columns),n=0;n=(r=void 0===r?1:r))throw new RangeError("min should be strictly smaller than max");for(var e=this.constructor.empty(this.rows,this.columns),n=0;ne||r<0||r>=this.columns||e<0||e>=this.columns)throw new RangeError("Argument out of range");for(var n=new this.constructor[Symbol.species](t.length,e-r+1),i=0;i=this.rows)throw new RangeError(`Row index out of range: ${t[i]}`);n.set(i,o-r,this.get(t[i],o))}return n}subMatrixColumn(t,r,e){if(void 0===r&&(r=0),void 0===e&&(e=this.rows-1),r>e||r<0||r>=this.rows||e<0||e>=this.rows)throw new RangeError("Argument out of range");for(var n=new this.constructor[Symbol.species](e-r+1,t.length),i=0;i=this.columns)throw new RangeError(`Column index out of range: ${t[i]}`);n.set(o-r,i,this.get(o,t[i]))}return n}setSubMatrix(t,r,e){y(this,r,r+(t=this.constructor.checkMatrix(t)).rows-1,e,e+t.columns-1);for(var n=0;nt?i[o]=1/i[o]:i[o]=0;return i=this.constructor[Symbol.species].diag(i),n.mmul(i.mmul(e.transposeView()))}clone(){for(var t=new this.constructor[Symbol.species](this.rows,this.columns),r=0;r>","signPropagatingRightShift"],[">>>","rightShift","zeroFillRightShift"]]){var u=o($("\n(function %name%(value) {\n if (typeof value === 'number') return this.%name%S(value);\n return this.%name%M(value);\n})\n",{name:s[1],op:s[0]})),l=o($("\n(function %name%S(value) {\n for (var i = 0; i < this.rows; i++) {\n for (var j = 0; j < this.columns; j++) {\n this.set(i, j, this.get(i, j) %op% value);\n }\n }\n return this;\n})\n",{name:`${s[1]}S`,op:s[0]})),d=o($("\n(function %name%M(matrix) {\n matrix = this.constructor.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 (var i = 0; i < this.rows; i++) {\n for (var j = 0; j < this.columns; j++) {\n this.set(i, j, this.get(i, j) %op% matrix.get(i, j));\n }\n }\n return this;\n})\n",{name:`${s[1]}M`,op:s[0]})),w=o($("\n(function %name%(matrix, value) {\n var newMatrix = new this[Symbol.species](matrix);\n return newMatrix.%name%(value);\n})\n",{name:s[1]}));for(n=1;n0){if(super(t),!(Number.isInteger(r)&&r>0))throw new TypeError("nColumns must be a positive integer");for(e=0;e=0;o--){for(i=0;i=0;e--){for(t=0;ti)return new Array(r.rows+1).fill(0);for(var o=r.addRow(e,[0]),s=0;s0;a--){for(c=0,s=0,u=0;u0&&(o=-o),r[a]=c*o,s-=i*o,e[a-1]=i-o,h=0;hl){0;do{for(1,i=e[l],f=(e[l+1]-i)/(2*r[l]),m=u(f,1),f<0&&(m=-m),e[l]=r[l]/(f+m),e[l+1]=r[l]*(f+m),g=e[l+1],o=i-e[l],s=l+2;s=l;s--)for(d=v,v=p,b=y,i=p*r[s],o=p*f,m=u(f,r[s]),r[s+1]=y*m,y=r[s]/m,f=(p=f/m)*e[s]-y*i,e[s+1]=o+y*(p*i+y*e[s]),h=0;hS*x)}e[l]=e[l]+M,r[l]=0}for(s=0;s=u;a--)e[a]=r[a][u-1]/l,s+=e[a]*e[a];for(o=Math.sqrt(s),e[u]>0&&(o=-o),s-=e[u]*o,e[u]=e[u]-o,h=u;h=u;a--)i+=e[a]*r[a][h];for(i/=s,a=u;a<=c;a++)r[a][h]-=i*e[a]}for(a=0;a<=c;a++){for(i=0,h=c;h>=u;h--)i+=e[h]*r[a][h];for(i/=s,h=u;h<=c;h++)r[a][h]-=i*e[h]}e[u]=l*e[u],r[u][u-1]=l*o}}for(a=0;a=1;u--)if(0!==r[u][u-1]){for(a=u+1;a<=c;a++)e[a]=r[a][u-1];for(h=u;h<=c;h++){for(o=0,a=u;a<=c;a++)o+=e[a]*n[a][h];for(o=o/e[u]/r[u][u-1],a=u;a<=c;a++)n[a][h]+=o*e[a]}}}(o,f,m,s),function(t,r,e,n,i){var o,s,a,h,u,l,c,f,m,g,p,v,d,w,y,b=t-1,M=t-1,x=Number.EPSILON,S=0,E=0,R=0,k=0,z=0,N=0,A=0,V=0;for(o=0;oM)&&(e[o]=i[o][o],r[o]=0),s=Math.max(o-1,0);s=0;){for(h=b;h>0&&(0===(N=Math.abs(i[h-1][h-1])+Math.abs(i[h][h]))&&(N=E),!(Math.abs(i[h][h-1])=0){for(A=R>=0?R+A:R-A,e[b-1]=f+A,e[b]=e[b-1],0!==A&&(e[b]=f-c/A),r[b-1]=0,r[b]=0,f=i[b][b-1],N=Math.abs(f)+Math.abs(A),R=f/N,k=A/N,z=Math.sqrt(R*R+k*k),R/=z,k/=z,s=b-1;s0){for(N=Math.sqrt(N),m=h&&(A=i[u][u],R=((z=f-A)*(N=m-A)-c)/i[u+1][u]+i[u][u+1],k=i[u+1][u+1]-A-z-N,z=i[u+2][u+1],N=Math.abs(R)+Math.abs(k)+Math.abs(z),R/=N,k/=N,z/=N,u!==h)&&!(Math.abs(i[u][u-1])*(Math.abs(k)+Math.abs(z))u+2&&(i[o][o-3]=0);for(a=u;a<=b-1&&(w=a!==b-1,a!==u&&(R=i[a][a-1],k=i[a+1][a-1],z=w?i[a+2][a-1]:0,0!==(f=Math.abs(R)+Math.abs(k)+Math.abs(z))&&(R/=f,k/=f,z/=f)),0!==f);a++)if(N=Math.sqrt(R*R+k*k+z*z),R<0&&(N=-N),0!==N){for(a!==u?i[a][a-1]=-N*f:h!==u&&(i[a][a-1]=-i[a][a-1]),f=(R+=N)/N,m=k/N,A=z/N,k/=R,z/=R,s=a;s=0;b--)if(R=e[b],0===(k=r[b]))for(h=b,i[b][b]=1,o=b-1;o>=0;o--){for(c=i[o][o]-R,z=0,s=h;s<=b;s++)z+=i[o][s]*i[s][b];if(r[o]<0)A=c,N=z;else if(h=o,0===r[o]?i[o][b]=0!==c?-z/c:-z/(x*E):(f=i[o][o+1],m=i[o+1][o],k=(e[o]-R)*(e[o]-R)+r[o]*r[o],l=(f*N-A*z)/k,i[o][b]=l,i[o+1][b]=Math.abs(f)>Math.abs(A)?(-z-c*l)/f:(-N-m*l)/A),l=Math.abs(i[o][b]),x*l*l>1)for(s=o;s<=b;s++)i[s][b]=i[s][b]/l}else if(k<0)for(h=b-1,Math.abs(i[b][b-1])>Math.abs(i[b-1][b])?(i[b-1][b-1]=k/i[b][b-1],i[b-1][b]=-(i[b][b]-R)/i[b][b-1]):(y=$(0,-i[b-1][b],i[b-1][b-1]-R,k),i[b-1][b-1]=y[0],i[b-1][b]=y[1]),i[b][b-1]=0,i[b][b]=1,o=b-2;o>=0;o--){for(g=0,p=0,s=h;s<=b;s++)g+=i[o][s]*i[s][b-1],p+=i[o][s]*i[s][b];if(c=i[o][o]-R,r[o]<0)A=c,z=g,N=p;else if(h=o,0===r[o]?(y=$(-g,-p,c,k),i[o][b-1]=y[0],i[o][b]=y[1]):(f=i[o][o+1],m=i[o+1][o],v=(e[o]-R)*(e[o]-R)+r[o]*r[o]-k*k,d=2*(e[o]-R)*k,0===v&&0===d&&(v=x*E*(Math.abs(c)+Math.abs(k)+Math.abs(f)+Math.abs(m)+Math.abs(A))),y=$(f*z-A*g+k*p,f*N-A*p-k*g,v,d),i[o][b-1]=y[0],i[o][b]=y[1],Math.abs(f)>Math.abs(A)+Math.abs(k)?(i[o+1][b-1]=(-g-c*i[o][b-1]+k*i[o][b])/f,i[o+1][b]=(-p-c*i[o][b]-k*i[o][b-1])/f):(y=$(-z-m*i[o][b-1],-N-m*i[o][b],A,k),i[o+1][b-1]=y[0],i[o+1][b]=y[1])),l=Math.max(Math.abs(i[o][b-1]),Math.abs(i[o][b])),x*l*l>1)for(s=o;s<=b;s++)i[s][b-1]=i[s][b-1]/l,i[s][b]=i[s][b]/l}for(o=0;oM)for(s=o;s=0;s--)for(o=0;o<=M;o++){for(A=0,a=0;a<=Math.min(s,M);a++)A+=n[o][a]*i[a][s];n[o][s]=A}}(o,h,a,s,f)}this.n=o,this.e=h,this.d=a,this.V=s}get realEigenvalues(){return this.d}get imaginaryEigenvalues(){return this.e}get eigenvectorMatrix(){return I.isMatrix(this.V)||(this.V=new I(this.V)),this.V}get diagonalMatrix(){var t,r,e=this.n,n=this.e,i=this.d,o=new I(e,e);for(t=0;t0?o[t][t+1]=n[t]:n[t]<0&&(o[t][t-1]=n[t])}return o}}function $(t,r,e,n){var i,o;return Math.abs(e)>Math.abs(n)?[(t+(i=n/e)*r)/(o=e+i*n),(r-i*t)/o]:[((i=e/n)*t+r)/(o=n+i*e),(i*r-t)/o]}class Q{constructor(t){if(!(t=j.checkMatrix(t)).isSymmetric())throw new Error("Matrix is not symmetric");var r,e,n,i=t,o=i.rows,s=new I(o,o),a=!0;for(e=0;e0,s[e][e]=Math.sqrt(Math.max(u,0)),n=e+1;n=0;o--)for(i=0;i=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}};function i(t,r){return void 0===r&&(r=Math.random),Math.floor(r()*t)}function o(t){return void 0===t&&(t=Math.random),t()}function s(t){for(var r=[],e=0;er?t[e]:r;return r},r.max2d=function(t){for(var r=0,e=0;er?t[e][n]:r;return r},r.rejectionSample=function(t,r){for(var e=h(t),n=0;n=a[0])return 0;for(var h=0;h=s[0])return 0;s[0]=e,o[0]=n,a[0]=i;for(var h=0,u=0;;){var l=2*h+1,c=l+1,f=t[0][0].length;if(l>=f)break;if(c>=f){if(!(s[l]>e))break;u=l}else if(s[l]>=s[c]){if(!(ee.length;a++)1===i[a]&&n[a]=0?(i[s]=0,Math.floor(e[s])):-1}},function(t,r,e){"use strict";var n,i=this&&this.__read||function(t,r){var e="function"==typeof Symbol&&t[Symbol.iterator];if(!e)return t;var n,i,o=e.call(t),s=[];try{for(;(void 0===r||r-- >0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},o=this&&this.__spread||function(){for(var t=[],r=0;r=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}};Object.defineProperty(r,"__esModule",{value:!0});var a=e(1),h=function(){function t(t,r,e,n){this.entries=new Map,this.nRows=0,this.nCols=0,this.rows=o(t),this.cols=o(r),this.values=o(e);for(var i=0;ir?t:r})},r.multiplyScalar=function(t,r){return t.map(function(t){return t*r})},r.eliminateZeros=function(t){for(var r=new Set,e=t.getValues(),n=t.getRows(),i=t.getCols(),o=0;or?t[e]:r;return t.map(function(t){return t/r})},n.l1=function(t){for(var r=0,e=0;e0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},i=this&&this.__spread||function(){for(var t=[],r=0;r=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}};Object.defineProperty(r,"__esModule",{value:!0});var s=e(1),a=function(){return function(t,r,e,n){this.hyperplanes=t,this.offsets=r,this.children=e,this.indices=n}}();function h(t,r,e){for(var n=r,i=0;i0?0:1}r.FlatTree=a,r.makeForest=function(t,r,e,n){var o=Math.max(10,r);return s.range(e).map(function(r,e){return function(t,r,e,n){void 0===r&&(r=30);var i=s.range(t.length);return function t(r,e,n,i,o){if(void 0===n&&(n=30),e.length>n){var a=function(t,r,e){var n=t[0].length,i=s.tauRandInt(r.length,e),o=s.tauRandInt(r.length,e);o=(o+=i===o?1:0)%r.length;for(var a=r[i],h=r[o],u=0,l=s.zeros(n),c=0;c0?(g[c]=0,f+=1):(g[c]=1,m+=1)}var d=s.zeros(f),w=s.zeros(m);for(var c in f=0,m=0,s.range(g.length))0===g[c]?(d[f]=r[c],f+=1):(w[m]=r[c],m+=1);return{indicesLeft:d,indicesRight:w,hyperplane:l,offset:u}}(r,e,o),h=a.indicesLeft,u=a.indicesRight,l=a.hyperplane,c=a.offset,f=t(r,h,n,i+1,o),m=t(r,u,n,i+1,o),g={leftChild:f,rightChild:m,isLeaf:!1,hyperplane:l,offset:c};return g}var g={indices:e,isLeaf:!0};return g}(t,i,r,e,n)}(t,o,e,n)}).map(function(t){return function(t,r){var e=function t(r){return r.isLeaf?1:1+t(r.leftChild)+t(r.rightChild)}(t),n=function t(r){return r.isLeaf?1:t(r.leftChild)+t(r.rightChild)}(t),o=s.range(e).map(function(){return s.zeros(t.hyperplane.length)}),h=s.zeros(e),u=s.range(e).map(function(){return[-1,-1]}),l=s.range(n).map(function(){return s.range(r).map(function(){return-1})});return function t(r,e,n,o,s,a,h){var u;if(r.isLeaf)return o[a][0]=-h,(u=s[h]).splice.apply(u,i([0,r.indices.length],r.indices)),{nodeNum:a,leafNum:h+=1};e[a]=r.hyperplane,n[a]=r.offset,o[a][0]=a+1;var l=a,c=t(r.leftChild,e,n,o,s,a+1,h);return a=c.nodeNum,h=c.leafNum,o[l][1]=a+1,{nodeNum:(c=t(r.rightChild,e,n,o,s,a+1,h)).nodeNum,leafNum:c.leafNum}}(t,o,h,u,l,0,0),new a(o,h,u,l)}(t,o)})},r.makeLeafArray=function(t){var r,e;if(t.length>0){var n=[];try{for(var s=o(t),a=s.next();!a.done;a=s.next()){var h=a.value;n.push.apply(n,i(h.indices))}}catch(t){r={error:t}}finally{try{a&&!a.done&&(e=s.return)&&e.call(s)}finally{if(r)throw r.error}}return n}return[[-1]]},r.searchFlatTree=function(t,r){for(var e=0;r.children[e][0]>0;)e=0===h(r.hyperplanes[e],r.offsets[e],t)?r.children[e][0]:r.children[e][1];var n=-1*r.children[e][0];return r.indices[n]}},function(t,r,e){"use strict";Object.defineProperty(r,"__esModule",{value:!0});var n=e(6);window.UMAP=n.UMAP},function(t,r,e){"use strict";var n=this&&this.__awaiter||function(t,r,e,n){return new(e||(e=Promise))(function(i,o){function s(t){try{h(n.next(t))}catch(t){o(t)}}function a(t){try{h(n.throw(t))}catch(t){o(t)}}function h(t){t.done?i(t.value):new e(function(r){r(t.value)}).then(s,a)}h((n=n.apply(t,r||[])).next())})},i=this&&this.__generator||function(t,r){var e,n,i,o,s={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function a(o){return function(a){return function(o){if(e)throw new TypeError("Generator is already executing.");for(;s;)try{if(e=1,n&&(i=2&o[0]?n.return:o[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,o[1])).done)return i;switch(n=0,i&&(o=[2&o[0],i.value]),o[0]){case 0:case 1:i=o;break;case 4:return s.label++,{value:o[1],done:!1};case 5:s.label++,n=o[1],o=[0];continue;case 7:o=s.ops.pop(),s.trys.pop();continue;default:if(!(i=(i=s.trys).length>0&&i[i.length-1])&&(6===o[0]||2===o[0])){s=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},s=this&&this.__spread||function(){for(var t=[],r=0;r0&&i.set(o,l,c)}var f=h.transpose(i);return h.maximum(i,f)},t.prototype.transform=function(t){var r=this,e=this.X;if(void 0===e||0===e.length)throw new Error("No data has been fit.");var n=Math.floor(this.nNeighbors*this.transformQueueSize),i=u.initializeSearch(this.rpForest,e,t,n,this.initFromRandom,this.initFromTree),o=this.search(e,this.searchGraph,i,t),s=a.deheapSort(o),l=s.indices,f=s.weights;l=l.map(function(t){return t.slice(0,r.nNeighbors)}),f=f.map(function(t){return t.slice(0,r.nNeighbors)});var m=Math.max(0,this.localConnectivity-1),g=this.smoothKNNDistance(f,this.nNeighbors,m),p=g.sigmas,v=g.rhos,d=this.computeMembershipStrengths(l,f,p,v),w=d.rows,y=d.cols,b=d.vals,M=[t.length,e.length],S=new h.SparseMatrix(w,y,b,M),E=h.normalize(S,"l1"),R=h.getCSR(E),z=t.length,k=x(c.reshape2d(R.indices,z,this.nNeighbors),c.reshape2d(R.values,z,this.nNeighbors),this.embedding),N=this.nEpochs?this.nEpochs/3:S.nRows<=1e4?100:30,A=S.getValues().reduce(function(t,r){return r>t?r:t},0);S=S.map(function(t){return t0});if(g.length>=e){var p=Math.floor(e),v=e-p;p>0?(s[h]=g[p-1],v>1e-5&&(s[h]+=v*(g[p]-g[p-1]))):s[h]=v*g[0]}else g.length>0&&(s[h]=c.max(g));for(var d=0;d0?Math.exp(-b/f):1}if(Math.abs(w-o)<1e-5)break;w>o?f=(u+(l=f))/2:(u=f,l===1/0?f*=2:f=(u+l)/2)}if(a[h]=f,s[h]>0){var x=c.mean(m);a[h]<.001*x&&(a[h]=.001*x)}else{var M=c.mean(t.map(c.mean));a[h]<.001*M&&(a[h]=.001*M)}}return{sigmas:a,rhos:s}},t.prototype.computeMembershipStrengths=function(t,r,e,n){for(var i=t.length,o=t[0].length,s=c.zeros(i*o),a=c.zeros(i*o),h=c.zeros(i*o),u=0;u0&&(e[n]=r/i[n])}),e},t.prototype.assignOptimizationStateParameters=function(t){Object.assign(this.optimizationState,t)},t.prototype.prepareForOptimizationLoop=function(){var t=this.repulsionStrength,r=this.learningRate,e=this.negativeSampleRate,n=this.optimizationState,i=n.epochsPerSample,o=n.headEmbedding,a=n.tailEmbedding,h=o[0].length,u=o.length===a.length,l=i.map(function(t){return t/e}),c=s(l),f=s(i);this.assignOptimizationStateParameters({epochOfNextSample:f,epochOfNextNegativeSample:c,epochsPerNegativeSample:l,moveOther:u,initialAlpha:r,alpha:r,gamma:t,dim:h})},t.prototype.initializeOptimization=function(){var t=this.embedding,r=this.embedding,e=this.optimizationState,n=e.head,i=e.tail,o=e.epochsPerSample,s=this.getNEpochs(),a=this.graph.nCols,h=w(this.spread,this.minDist),u=h.a,l=h.b;this.assignOptimizationStateParameters({headEmbedding:t,tailEmbedding:r,head:n,tail:i,epochsPerSample:o,a:u,b:l,nEpochs:s,nVertices:a})},t.prototype.optimizeLayoutStep=function(t){for(var r=this.optimizationState,e=r.head,n=r.tail,i=r.headEmbedding,o=r.tailEmbedding,s=r.epochsPerSample,a=r.epochOfNextSample,h=r.epochOfNextNegativeSample,u=r.epochsPerNegativeSample,l=r.moveOther,f=r.initialAlpha,m=r.alpha,g=r.gamma,p=r.a,w=r.b,y=r.dim,b=r.nEpochs,x=r.nVertices,M=0;Mt)){var S=e[M],E=n[M],R=i[S],z=o[E],k=d(R,z),N=0;k>0&&(N=-2*p*w*Math.pow(k,w-1),N/=p*Math.pow(k,w)+1);for(var A=0;A0)F=2*g*w,F/=(.001+_)*(p*Math.pow(_,w)+1);else if(S===P)continue;for(A=0;A0&&(C=v(F*(R[A]-j[A]),4)),R[A]+=C*m}}h[M]+=V*u[M]}return r.alpha=f*(1-t/b),r.currentEpoch+=1,i},t.prototype.optimizeLayoutAsync=function(t){var r=this;return void 0===t&&(t=function(){return!0}),new Promise(function(e,o){var s=function(){return n(r,void 0,void 0,function(){var r,n,a,h,u,l;return i(this,function(i){try{if(r=this.optimizationState,n=r.nEpochs,a=r.currentEpoch,this.embedding=this.optimizeLayoutStep(a),h=this.optimizationState.currentEpoch,u=!1===t(h),l=h===n,u||l)return[2,e(l)];s()}catch(t){o(t)}return[2]})})};s()})},t.prototype.optimizeLayout=function(t){void 0===t&&(t=function(){return!0});for(var r=!1,e=[];!r;){var n=this.optimizationState,i=n.nEpochs,o=n.currentEpoch;e=this.optimizeLayoutStep(o);var s=this.optimizationState.currentEpoch,a=!1===t(s);r=s===i||a}return e},t.prototype.getNEpochs=function(){var t=this.graph;if(this.nEpochs>0)return this.nEpochs;var r=t.nRows;return r<=2500?500:r<=5e3?400:r<=7500?300:200},t}();function g(t,r){for(var e=0,n=0;nr?r:t<-r?-r:t}function d(t,r){for(var e=0,n=0;n=r?Math.exp(-(e[i]-r)/t):n}),i={x:e,y:n},s={damping:1.5,initialValues:[.5,.5],gradientDifference:.1,maxIterations:100,errorTolerance:.01},a=f.default(i,function(t){var r=o(t,2),e=r[0],n=r[1];return function(t){return 1/(1+e*Math.pow(t,2*n))}},s).parameterValues,h=o(a,2);return{a:h[0],b:h[1]}}function y(t,r,e,n){return void 0===e&&(e=1),void 0===n&&(n=5),t.map(function(t,i,o){return-1===r[i]||-1===r[o]?t*Math.exp(-e):r[i]!==r[o]?t*Math.exp(-n):t})}function b(t){t=h.normalize(t,"max");var r=h.transpose(t),e=h.pairwiseMultiply(r,t);return t=h.add(t,h.subtract(r,e)),h.eliminateZeros(t)}function x(t,r,e){for(var n=c.zeros(t.length).map(function(t){return c.zeros(e[0].length)}),i=0;i=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}};Object.defineProperty(r,"__esModule",{value:!0});var i=e(2),o=e(3),s=e(4),a=e(1);r.makeNNDescent=function(t,r){return function(e,n,o,s,h,u,l,c){void 0===s&&(s=10),void 0===h&&(h=50),void 0===u&&(u=.001),void 0===l&&(l=.5),void 0===c&&(c=!0);for(var f=e.length,m=i.makeHeap(e.length,o),g=0;gr&&(r=t[e]);return r};var a=function(t){if(!o()(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");for(var r=t[0],e=1;e1&&void 0!==arguments[1]?arguments[1]:{};if(!o()(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");if(void 0!==e.output){if(!o()(e.output))throw new TypeError("output option must be an array if specified");r=e.output}else r=new Array(t.length);var n=a(t),i=s(t);if(n===i)throw new RangeError("minimum and maximum input values are equal. Cannot rescale a constant array");var h=e.min,u=void 0===h?e.autoMinMax?n:0:h,l=e.max,c=void 0===l?e.autoMinMax?i:1:l;if(u>=c)throw new RangeError("min option must be smaller than max option");for(var f=(c-u)/(i-n),m=0;mMath.abs(h[i])&&(i=r);if(i!==e){for(n=0;n=0;i--){for(n=0;no?t[i][o]:i===o?1:0;return n}get upperTriangularMatrix(){for(var t=this.LU,r=t.rows,e=t.columns,n=new P(r,e),i=0;iMath.abs(r)?(e=r/t,Math.abs(t)*Math.sqrt(1+e*e)):0!==r?(e=t/r,Math.abs(r)*Math.sqrt(1+e*e)):0}function c(t,r,e){for(var n=new Array(t),i=0;i=0;t--)if(0!==v[t]){for(let r=t+1;r=0;t--){if(t0;){let t,r;for(t=R-2;t>=-1&&-1!==t;t--){const r=Number.MIN_VALUE+k*Math.abs(v[t]+Math.abs(v[t+1]));if(Math.abs(y[t])<=r||Number.isNaN(y[t])){y[t]=0;break}}if(t===R-2)r=4;else{let e;for(e=R-1;e>=t&&e!==t;e--){let r=(e!==R?Math.abs(y[e]):0)+(e!==t+1?Math.abs(y[e-1]):0);if(Math.abs(v[e])<=k*r){v[e]=0;break}}e===t?r=3:e===R-1?r=1:(r=2,t=e)}switch(t++,r){case 1:{let r=y[R-2];y[R-2]=0;for(let e=R-2;e>=t;e--){let i=l(v[e],r),o=v[e]/i,s=r/i;if(v[e]=i,e!==t&&(r=-s*y[e-1],y[e-1]=o*y[e-1]),u)for(let t=0;t=v[t+1]);){let r=v[t];if(v[t]=v[t+1],v[t+1]=r,u&&tr?i[o][e]=t[o][e]/this.s[e]:i[o][e]=0;var o=this.U,s=o.length,a=o[0].length,h=new P(e,s);for(let t=0;tt&&r++;return r}get diagonal(){return this.s}get threshold(){return Number.EPSILON/2*Math.max(this.m,this.n)*this.s[0]}get leftSingularVectors(){return P.isMatrix(this.U)||(this.U=new P(this.U)),this.U}get rightSingularVectors(){return P.isMatrix(this.V)||(this.V=new P(this.V)),this.V}get diagonalMatrix(){return P.diag(this.s)}}function m(t,r,e){var n=e?t.rows:t.rows-1;if(r<0||r>n)throw new RangeError("Row index out of range")}function g(t,r,e){var n=e?t.columns:t.columns-1;if(r<0||r>n)throw new RangeError("Column index out of range")}function p(t,r){if(r.to1DArray&&(r=r.to1DArray()),r.length!==t.columns)throw new RangeError("vector size must be the same as the number of columns");return r}function v(t,r){if(r.to1DArray&&(r=r.to1DArray()),r.length!==t.rows)throw new RangeError("vector size must be the same as the number of rows");return r}function d(t,r,e){return{row:w(t,r),column:y(t,e)}}function w(t,r){if("object"!=typeof r)throw new TypeError("unexpected type for row indices");if(r.some(r=>r<0||r>=t.rows))throw new RangeError("row indices are out of range");return Array.isArray(r)||(r=Array.from(r)),r}function y(t,r){if("object"!=typeof r)throw new TypeError("unexpected type for column indices");if(r.some(r=>r<0||r>=t.columns))throw new RangeError("column indices are out of range");return Array.isArray(r)||(r=Array.from(r)),r}function b(t,r,e,n,i){if(5!==arguments.length)throw new RangeError("expected 4 arguments");if(x("startRow",r),x("endRow",e),x("startColumn",n),x("endColumn",i),r>e||n>i||r<0||r>=t.rows||e<0||e>=t.rows||n<0||n>=t.columns||i<0||i>=t.columns)throw new RangeError("Submatrix indices are out of range")}function x(t,r){if("number"!=typeof r)throw new TypeError(`${t} must be a number`)}class M extends(I()){constructor(t,r,e){super(),this.matrix=t,this.rows=r,this.columns=e}static get[Symbol.species](){return P}}class S extends M{constructor(t){super(t,t.columns,t.rows)}set(t,r,e){return this.matrix.set(r,t,e),this}get(t,r){return this.matrix.get(r,t)}}class E extends M{constructor(t,r){super(t,1,t.columns),this.row=r}set(t,r,e){return this.matrix.set(this.row,r,e),this}get(t,r){return this.matrix.get(this.row,r)}}class R extends M{constructor(t,r,e,n,i){b(t,r,e,n,i),super(t,e-r+1,i-n+1),this.startRow=r,this.startColumn=n}set(t,r,e){return this.matrix.set(this.startRow+t,this.startColumn+r,e),this}get(t,r){return this.matrix.get(this.startRow+t,this.startColumn+r)}}class z extends M{constructor(t,r,e){var n=d(t,r,e);super(t,n.row.length,n.column.length),this.rowIndices=n.row,this.columnIndices=n.column}set(t,r,e){return this.matrix.set(this.rowIndices[t],this.columnIndices[r],e),this}get(t,r){return this.matrix.get(this.rowIndices[t],this.columnIndices[r])}}class k extends M{constructor(t,r){super(t,(r=w(t,r)).length,t.columns),this.rowIndices=r}set(t,r,e){return this.matrix.set(this.rowIndices[t],r,e),this}get(t,r){return this.matrix.get(this.rowIndices[t],r)}}class N extends M{constructor(t,r){r=y(t,r),super(t,t.rows,r.length),this.columnIndices=r}set(t,r,e){return this.matrix.set(t,this.columnIndices[r],e),this}get(t,r){return this.matrix.get(t,this.columnIndices[r])}}class A extends M{constructor(t,r){super(t,t.rows,1),this.column=r}set(t,r,e){return this.matrix.set(t,this.column,e),this}get(t){return this.matrix.get(t,this.column)}}class C extends M{constructor(t){super(t,t.rows,t.columns)}set(t,r,e){return this.matrix.set(this.rows-t-1,r,e),this}get(t,r){return this.matrix.get(this.rows-t-1,r)}}class V extends M{constructor(t){super(t,t.rows,t.columns)}set(t,r,e){return this.matrix.set(t,this.columns-r-1,e),this}get(t,r){return this.matrix.get(t,this.columns-r-1)}}function I(t){void 0===t&&(t=Object);class r extends t{static get[Symbol.species](){return this}static from1DArray(t,r,e){if(t*r!==e.length)throw new RangeError("Data length does not match given dimensions");for(var n=new this(t,r),i=0;ie?(i=!0,e=r):(n=!1,i=!0);t++}return n}isReducedEchelonForm(){let t=0,r=0,e=-1,n=!0,i=!1;for(;te?(i=!0,e=r):(n=!1,i=!0);for(let e=r+1;et&&(t=this.get(r,e));return t}maxIndex(){for(var t=this.get(0,0),r=[0,0],e=0;et&&(t=this.get(e,n),r[0]=e,r[1]=n);return r}min(){for(var t=this.get(0,0),r=0;rr&&(r=this.get(t,e));return r}maxRowIndex(t){m(this,t);for(var r=this.get(t,0),e=[t,0],n=1;nr&&(r=this.get(t,n),e[1]=n);return e}minRow(t){m(this,t);for(var r=this.get(t,0),e=1;er&&(r=this.get(e,t));return r}maxColumnIndex(t){g(this,t);for(var r=this.get(0,t),e=[0,t],n=1;nr&&(r=this.get(n,t),e[0]=n);return e}minColumn(t){g(this,t);for(var r=this.get(0,t),e=1;e=(r=void 0===r?1:r))throw new RangeError("min should be strictly smaller than max");for(var e=this.constructor.empty(this.rows,this.columns),n=0;n=(r=void 0===r?1:r))throw new RangeError("min should be strictly smaller than max");for(var e=this.constructor.empty(this.rows,this.columns),n=0;ne||r<0||r>=this.columns||e<0||e>=this.columns)throw new RangeError("Argument out of range");for(var n=new this.constructor[Symbol.species](t.length,e-r+1),i=0;i=this.rows)throw new RangeError(`Row index out of range: ${t[i]}`);n.set(i,o-r,this.get(t[i],o))}return n}subMatrixColumn(t,r,e){if(void 0===r&&(r=0),void 0===e&&(e=this.rows-1),r>e||r<0||r>=this.rows||e<0||e>=this.rows)throw new RangeError("Argument out of range");for(var n=new this.constructor[Symbol.species](e-r+1,t.length),i=0;i=this.columns)throw new RangeError(`Column index out of range: ${t[i]}`);n.set(o-r,i,this.get(o,t[i]))}return n}setSubMatrix(t,r,e){b(this,r,r+(t=this.constructor.checkMatrix(t)).rows-1,e,e+t.columns-1);for(var n=0;nt?i[o]=1/i[o]:i[o]=0;return i=this.constructor[Symbol.species].diag(i),n.mmul(i.mmul(e.transposeView()))}clone(){for(var t=new this.constructor[Symbol.species](this.rows,this.columns),r=0;r>","signPropagatingRightShift"],[">>>","rightShift","zeroFillRightShift"]]){var a=o($("\n(function %name%(value) {\n if (typeof value === 'number') return this.%name%S(value);\n return this.%name%M(value);\n})\n",{name:s[1],op:s[0]})),l=o($("\n(function %name%S(value) {\n for (var i = 0; i < this.rows; i++) {\n for (var j = 0; j < this.columns; j++) {\n this.set(i, j, this.get(i, j) %op% value);\n }\n }\n return this;\n})\n",{name:`${s[1]}S`,op:s[0]})),c=o($("\n(function %name%M(matrix) {\n matrix = this.constructor.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 (var i = 0; i < this.rows; i++) {\n for (var j = 0; j < this.columns; j++) {\n this.set(i, j, this.get(i, j) %op% matrix.get(i, j));\n }\n }\n return this;\n})\n",{name:`${s[1]}M`,op:s[0]})),w=o($("\n(function %name%(matrix, value) {\n var newMatrix = new this[Symbol.species](matrix);\n return newMatrix.%name%(value);\n})\n",{name:s[1]}));for(n=1;n0){if(super(t),!(Number.isInteger(r)&&r>0))throw new TypeError("nColumns must be a positive integer");for(e=0;e=0;o--){for(i=0;i=0;e--){for(t=0;ta(t)),u=function(t,r,e,n,i){const o=e.length,s=t.x.length;for(var a=new Array(o),h=0;h Date: Mon, 20 May 2019 18:19:48 -0700 Subject: [PATCH 08/46] ah, turn on esModuleInterop to get tests to pass w/LM import change --- tsconfig.json | 1 + 1 file changed, 1 insertion(+) diff --git a/tsconfig.json b/tsconfig.json index 85d8dfe..770e403 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,6 +10,7 @@ "baseUrl": ".", "target": "es5", "module": "commonjs", + "esModuleInterop": true, "strict": true, "noImplicitAny": false, "removeComments": true, From 7114f846d10d7422dc5400ed8e1b6b89f7620926 Mon Sep 17 00:00:00 2001 From: Jana Beck Date: Mon, 20 May 2019 18:19:58 -0700 Subject: [PATCH 09/46] more bundle updates --- lib/umap-js.js | 64 ++++++++++++++++++++++++++++++++++++---------- lib/umap-js.min.js | 2 +- 2 files changed, 52 insertions(+), 14 deletions(-) diff --git a/lib/umap-js.js b/lib/umap-js.js index 54dca94..091fe80 100644 --- a/lib/umap-js.js +++ b/lib/umap-js.js @@ -272,8 +272,15 @@ exports.reshape2d = reshape2d; "use strict"; +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; + result["default"] = mod; + return result; +}; Object.defineProperty(exports, "__esModule", { value: true }); -var utils = __webpack_require__(1); +var utils = __importStar(__webpack_require__(1)); function makeHeap(nPoints, size) { var makeArrays = function (fillValue) { return utils.empty(nPoints).map(function () { @@ -502,9 +509,16 @@ var __values = (this && this.__values) || function (o) { } }; }; +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; + result["default"] = mod; + return result; +}; Object.defineProperty(exports, "__esModule", { value: true }); var _a; -var utils = __webpack_require__(1); +var utils = __importStar(__webpack_require__(1)); var SparseMatrix = (function () { function SparseMatrix(rows, cols, values, dims) { this.entries = new Map(); @@ -815,8 +829,15 @@ var __values = (this && this.__values) || function (o) { } }; }; +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; + result["default"] = mod; + return result; +}; Object.defineProperty(exports, "__esModule", { value: true }); -var utils = __webpack_require__(1); +var utils = __importStar(__webpack_require__(1)); var FlatTree = (function () { function FlatTree(hyperplanes, offsets, children, indices) { this.hyperplanes = hyperplanes; @@ -1099,13 +1120,23 @@ var __spread = (this && this.__spread) || function () { for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); return ar; }; +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; + result["default"] = mod; + return result; +}; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; Object.defineProperty(exports, "__esModule", { value: true }); -var heap = __webpack_require__(2); -var matrix = __webpack_require__(3); -var nnDescent = __webpack_require__(7); -var tree = __webpack_require__(4); -var utils = __webpack_require__(1); -var ml_levenberg_marquardt_1 = __webpack_require__(8); +var heap = __importStar(__webpack_require__(2)); +var matrix = __importStar(__webpack_require__(3)); +var nnDescent = __importStar(__webpack_require__(7)); +var tree = __importStar(__webpack_require__(4)); +var utils = __importStar(__webpack_require__(1)); +var ml_levenberg_marquardt_1 = __importDefault(__webpack_require__(8)); var SMOOTH_K_TOLERANCE = 1e-5; var MIN_K_DIST_SCALE = 1e-3; var UMAP = (function () { @@ -1798,11 +1829,18 @@ var __values = (this && this.__values) || function (o) { } }; }; +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; + result["default"] = mod; + return result; +}; Object.defineProperty(exports, "__esModule", { value: true }); -var heap = __webpack_require__(2); -var matrix = __webpack_require__(3); -var tree = __webpack_require__(4); -var utils = __webpack_require__(1); +var heap = __importStar(__webpack_require__(2)); +var matrix = __importStar(__webpack_require__(3)); +var tree = __importStar(__webpack_require__(4)); +var utils = __importStar(__webpack_require__(1)); function makeNNDescent(distanceFn, random) { return function nNDescent(data, leafArray, nNeighbors, nIters, maxCandidates, delta, rho, rpTreeInit) { if (nIters === void 0) { nIters = 10; } diff --git a/lib/umap-js.min.js b/lib/umap-js.min.js index f095978..2f3a4ba 100644 --- a/lib/umap-js.min.js +++ b/lib/umap-js.min.js @@ -1 +1 @@ -!function(t){var r={};function e(n){if(r[n])return r[n].exports;var i=r[n]={i:n,l:!1,exports:{}};return t[n].call(i.exports,i,i.exports,e),i.l=!0,i.exports}e.m=t,e.c=r,e.d=function(t,r,n){e.o(t,r)||Object.defineProperty(t,r,{enumerable:!0,get:n})},e.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},e.t=function(t,r){if(1&r&&(t=e(t)),8&r)return t;if(4&r&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(e.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&r&&"string"!=typeof t)for(var i in t)e.d(n,i,function(r){return t[r]}.bind(null,i));return n},e.n=function(t){var r=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(r,"a",r),r},e.o=function(t,r){return Object.prototype.hasOwnProperty.call(t,r)},e.p="",e(e.s=5)}([function(t,r,e){"use strict";const n=Object.prototype.toString;t.exports=function(t){return n.call(t).endsWith("Array]")}},function(t,r,e){"use strict";var n=this&&this.__values||function(t){var r="function"==typeof Symbol&&t[Symbol.iterator],e=0;return r?r.call(t):{next:function(){return t&&e>=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}};function i(t,r){return void 0===r&&(r=Math.random),Math.floor(r()*t)}function o(t){return void 0===t&&(t=Math.random),t()}function s(t){for(var r=[],e=0;er?t[e]:r;return r},r.max2d=function(t){for(var r=0,e=0;er?t[e][n]:r;return r},r.rejectionSample=function(t,r){for(var e=h(t),n=0;n=a[0])return 0;for(var h=0;h=s[0])return 0;s[0]=e,o[0]=n,a[0]=i;for(var h=0,u=0;;){var l=2*h+1,c=l+1,f=t[0][0].length;if(l>=f)break;if(c>=f){if(!(s[l]>e))break;u=l}else if(s[l]>=s[c]){if(!(ee.length;a++)1===i[a]&&n[a]=0?(i[s]=0,Math.floor(e[s])):-1}},function(t,r,e){"use strict";var n,i=this&&this.__read||function(t,r){var e="function"==typeof Symbol&&t[Symbol.iterator];if(!e)return t;var n,i,o=e.call(t),s=[];try{for(;(void 0===r||r-- >0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},o=this&&this.__spread||function(){for(var t=[],r=0;r=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}};Object.defineProperty(r,"__esModule",{value:!0});var a=e(1),h=function(){function t(t,r,e,n){this.entries=new Map,this.nRows=0,this.nCols=0,this.rows=o(t),this.cols=o(r),this.values=o(e);for(var i=0;ir?t:r})},r.multiplyScalar=function(t,r){return t.map(function(t){return t*r})},r.eliminateZeros=function(t){for(var r=new Set,e=t.getValues(),n=t.getRows(),i=t.getCols(),o=0;or?t[e]:r;return t.map(function(t){return t/r})},n.l1=function(t){for(var r=0,e=0;e0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},i=this&&this.__spread||function(){for(var t=[],r=0;r=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}};Object.defineProperty(r,"__esModule",{value:!0});var s=e(1),a=function(){return function(t,r,e,n){this.hyperplanes=t,this.offsets=r,this.children=e,this.indices=n}}();function h(t,r,e){for(var n=r,i=0;i0?0:1}r.FlatTree=a,r.makeForest=function(t,r,e,n){var o=Math.max(10,r);return s.range(e).map(function(r,e){return function(t,r,e,n){void 0===r&&(r=30);var i=s.range(t.length);return function t(r,e,n,i,o){if(void 0===n&&(n=30),e.length>n){var a=function(t,r,e){var n=t[0].length,i=s.tauRandInt(r.length,e),o=s.tauRandInt(r.length,e);o=(o+=i===o?1:0)%r.length;for(var a=r[i],h=r[o],u=0,l=s.zeros(n),c=0;c0?(g[c]=0,f+=1):(g[c]=1,m+=1)}var d=s.zeros(f),w=s.zeros(m);for(var c in f=0,m=0,s.range(g.length))0===g[c]?(d[f]=r[c],f+=1):(w[m]=r[c],m+=1);return{indicesLeft:d,indicesRight:w,hyperplane:l,offset:u}}(r,e,o),h=a.indicesLeft,u=a.indicesRight,l=a.hyperplane,c=a.offset,f=t(r,h,n,i+1,o),m=t(r,u,n,i+1,o),g={leftChild:f,rightChild:m,isLeaf:!1,hyperplane:l,offset:c};return g}var g={indices:e,isLeaf:!0};return g}(t,i,r,e,n)}(t,o,e,n)}).map(function(t){return function(t,r){var e=function t(r){return r.isLeaf?1:1+t(r.leftChild)+t(r.rightChild)}(t),n=function t(r){return r.isLeaf?1:t(r.leftChild)+t(r.rightChild)}(t),o=s.range(e).map(function(){return s.zeros(t.hyperplane.length)}),h=s.zeros(e),u=s.range(e).map(function(){return[-1,-1]}),l=s.range(n).map(function(){return s.range(r).map(function(){return-1})});return function t(r,e,n,o,s,a,h){var u;if(r.isLeaf)return o[a][0]=-h,(u=s[h]).splice.apply(u,i([0,r.indices.length],r.indices)),{nodeNum:a,leafNum:h+=1};e[a]=r.hyperplane,n[a]=r.offset,o[a][0]=a+1;var l=a,c=t(r.leftChild,e,n,o,s,a+1,h);return a=c.nodeNum,h=c.leafNum,o[l][1]=a+1,{nodeNum:(c=t(r.rightChild,e,n,o,s,a+1,h)).nodeNum,leafNum:c.leafNum}}(t,o,h,u,l,0,0),new a(o,h,u,l)}(t,o)})},r.makeLeafArray=function(t){var r,e;if(t.length>0){var n=[];try{for(var s=o(t),a=s.next();!a.done;a=s.next()){var h=a.value;n.push.apply(n,i(h.indices))}}catch(t){r={error:t}}finally{try{a&&!a.done&&(e=s.return)&&e.call(s)}finally{if(r)throw r.error}}return n}return[[-1]]},r.searchFlatTree=function(t,r){for(var e=0;r.children[e][0]>0;)e=0===h(r.hyperplanes[e],r.offsets[e],t)?r.children[e][0]:r.children[e][1];var n=-1*r.children[e][0];return r.indices[n]}},function(t,r,e){"use strict";Object.defineProperty(r,"__esModule",{value:!0});var n=e(6);window.UMAP=n.UMAP},function(t,r,e){"use strict";var n=this&&this.__awaiter||function(t,r,e,n){return new(e||(e=Promise))(function(i,o){function s(t){try{h(n.next(t))}catch(t){o(t)}}function a(t){try{h(n.throw(t))}catch(t){o(t)}}function h(t){t.done?i(t.value):new e(function(r){r(t.value)}).then(s,a)}h((n=n.apply(t,r||[])).next())})},i=this&&this.__generator||function(t,r){var e,n,i,o,s={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function a(o){return function(a){return function(o){if(e)throw new TypeError("Generator is already executing.");for(;s;)try{if(e=1,n&&(i=2&o[0]?n.return:o[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,o[1])).done)return i;switch(n=0,i&&(o=[2&o[0],i.value]),o[0]){case 0:case 1:i=o;break;case 4:return s.label++,{value:o[1],done:!1};case 5:s.label++,n=o[1],o=[0];continue;case 7:o=s.ops.pop(),s.trys.pop();continue;default:if(!(i=(i=s.trys).length>0&&i[i.length-1])&&(6===o[0]||2===o[0])){s=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},s=this&&this.__spread||function(){for(var t=[],r=0;r0&&i.set(o,l,c)}var f=h.transpose(i);return h.maximum(i,f)},t.prototype.transform=function(t){var r=this,e=this.X;if(void 0===e||0===e.length)throw new Error("No data has been fit.");var n=Math.floor(this.nNeighbors*this.transformQueueSize),i=u.initializeSearch(this.rpForest,e,t,n,this.initFromRandom,this.initFromTree),o=this.search(e,this.searchGraph,i,t),s=a.deheapSort(o),l=s.indices,f=s.weights;l=l.map(function(t){return t.slice(0,r.nNeighbors)}),f=f.map(function(t){return t.slice(0,r.nNeighbors)});var m=Math.max(0,this.localConnectivity-1),g=this.smoothKNNDistance(f,this.nNeighbors,m),p=g.sigmas,v=g.rhos,d=this.computeMembershipStrengths(l,f,p,v),w=d.rows,y=d.cols,b=d.vals,M=[t.length,e.length],S=new h.SparseMatrix(w,y,b,M),E=h.normalize(S,"l1"),R=h.getCSR(E),z=t.length,k=x(c.reshape2d(R.indices,z,this.nNeighbors),c.reshape2d(R.values,z,this.nNeighbors),this.embedding),N=this.nEpochs?this.nEpochs/3:S.nRows<=1e4?100:30,A=S.getValues().reduce(function(t,r){return r>t?r:t},0);S=S.map(function(t){return t0});if(g.length>=e){var p=Math.floor(e),v=e-p;p>0?(s[h]=g[p-1],v>1e-5&&(s[h]+=v*(g[p]-g[p-1]))):s[h]=v*g[0]}else g.length>0&&(s[h]=c.max(g));for(var d=0;d0?Math.exp(-b/f):1}if(Math.abs(w-o)<1e-5)break;w>o?f=(u+(l=f))/2:(u=f,l===1/0?f*=2:f=(u+l)/2)}if(a[h]=f,s[h]>0){var x=c.mean(m);a[h]<.001*x&&(a[h]=.001*x)}else{var M=c.mean(t.map(c.mean));a[h]<.001*M&&(a[h]=.001*M)}}return{sigmas:a,rhos:s}},t.prototype.computeMembershipStrengths=function(t,r,e,n){for(var i=t.length,o=t[0].length,s=c.zeros(i*o),a=c.zeros(i*o),h=c.zeros(i*o),u=0;u0&&(e[n]=r/i[n])}),e},t.prototype.assignOptimizationStateParameters=function(t){Object.assign(this.optimizationState,t)},t.prototype.prepareForOptimizationLoop=function(){var t=this.repulsionStrength,r=this.learningRate,e=this.negativeSampleRate,n=this.optimizationState,i=n.epochsPerSample,o=n.headEmbedding,a=n.tailEmbedding,h=o[0].length,u=o.length===a.length,l=i.map(function(t){return t/e}),c=s(l),f=s(i);this.assignOptimizationStateParameters({epochOfNextSample:f,epochOfNextNegativeSample:c,epochsPerNegativeSample:l,moveOther:u,initialAlpha:r,alpha:r,gamma:t,dim:h})},t.prototype.initializeOptimization=function(){var t=this.embedding,r=this.embedding,e=this.optimizationState,n=e.head,i=e.tail,o=e.epochsPerSample,s=this.getNEpochs(),a=this.graph.nCols,h=w(this.spread,this.minDist),u=h.a,l=h.b;this.assignOptimizationStateParameters({headEmbedding:t,tailEmbedding:r,head:n,tail:i,epochsPerSample:o,a:u,b:l,nEpochs:s,nVertices:a})},t.prototype.optimizeLayoutStep=function(t){for(var r=this.optimizationState,e=r.head,n=r.tail,i=r.headEmbedding,o=r.tailEmbedding,s=r.epochsPerSample,a=r.epochOfNextSample,h=r.epochOfNextNegativeSample,u=r.epochsPerNegativeSample,l=r.moveOther,f=r.initialAlpha,m=r.alpha,g=r.gamma,p=r.a,w=r.b,y=r.dim,b=r.nEpochs,x=r.nVertices,M=0;Mt)){var S=e[M],E=n[M],R=i[S],z=o[E],k=d(R,z),N=0;k>0&&(N=-2*p*w*Math.pow(k,w-1),N/=p*Math.pow(k,w)+1);for(var A=0;A0)F=2*g*w,F/=(.001+_)*(p*Math.pow(_,w)+1);else if(S===P)continue;for(A=0;A0&&(C=v(F*(R[A]-j[A]),4)),R[A]+=C*m}}h[M]+=V*u[M]}return r.alpha=f*(1-t/b),r.currentEpoch+=1,i},t.prototype.optimizeLayoutAsync=function(t){var r=this;return void 0===t&&(t=function(){return!0}),new Promise(function(e,o){var s=function(){return n(r,void 0,void 0,function(){var r,n,a,h,u,l;return i(this,function(i){try{if(r=this.optimizationState,n=r.nEpochs,a=r.currentEpoch,this.embedding=this.optimizeLayoutStep(a),h=this.optimizationState.currentEpoch,u=!1===t(h),l=h===n,u||l)return[2,e(l)];s()}catch(t){o(t)}return[2]})})};s()})},t.prototype.optimizeLayout=function(t){void 0===t&&(t=function(){return!0});for(var r=!1,e=[];!r;){var n=this.optimizationState,i=n.nEpochs,o=n.currentEpoch;e=this.optimizeLayoutStep(o);var s=this.optimizationState.currentEpoch,a=!1===t(s);r=s===i||a}return e},t.prototype.getNEpochs=function(){var t=this.graph;if(this.nEpochs>0)return this.nEpochs;var r=t.nRows;return r<=2500?500:r<=5e3?400:r<=7500?300:200},t}();function g(t,r){for(var e=0,n=0;nr?r:t<-r?-r:t}function d(t,r){for(var e=0,n=0;n=r?Math.exp(-(e[i]-r)/t):n}),i={x:e,y:n},s={damping:1.5,initialValues:[.5,.5],gradientDifference:.1,maxIterations:100,errorTolerance:.01},a=f.default(i,function(t){var r=o(t,2),e=r[0],n=r[1];return function(t){return 1/(1+e*Math.pow(t,2*n))}},s).parameterValues,h=o(a,2);return{a:h[0],b:h[1]}}function y(t,r,e,n){return void 0===e&&(e=1),void 0===n&&(n=5),t.map(function(t,i,o){return-1===r[i]||-1===r[o]?t*Math.exp(-e):r[i]!==r[o]?t*Math.exp(-n):t})}function b(t){t=h.normalize(t,"max");var r=h.transpose(t),e=h.pairwiseMultiply(r,t);return t=h.add(t,h.subtract(r,e)),h.eliminateZeros(t)}function x(t,r,e){for(var n=c.zeros(t.length).map(function(t){return c.zeros(e[0].length)}),i=0;i=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}};Object.defineProperty(r,"__esModule",{value:!0});var i=e(2),o=e(3),s=e(4),a=e(1);r.makeNNDescent=function(t,r){return function(e,n,o,s,h,u,l,c){void 0===s&&(s=10),void 0===h&&(h=50),void 0===u&&(u=.001),void 0===l&&(l=.5),void 0===c&&(c=!0);for(var f=e.length,m=i.makeHeap(e.length,o),g=0;gr&&(r=t[e]);return r};var a=function(t){if(!o()(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");for(var r=t[0],e=1;e1&&void 0!==arguments[1]?arguments[1]:{};if(!o()(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");if(void 0!==e.output){if(!o()(e.output))throw new TypeError("output option must be an array if specified");r=e.output}else r=new Array(t.length);var n=a(t),i=s(t);if(n===i)throw new RangeError("minimum and maximum input values are equal. Cannot rescale a constant array");var h=e.min,u=void 0===h?e.autoMinMax?n:0:h,l=e.max,c=void 0===l?e.autoMinMax?i:1:l;if(u>=c)throw new RangeError("min option must be smaller than max option");for(var f=(c-u)/(i-n),m=0;mMath.abs(h[i])&&(i=r);if(i!==e){for(n=0;n=0;i--){for(n=0;no?t[i][o]:i===o?1:0;return n}get upperTriangularMatrix(){for(var t=this.LU,r=t.rows,e=t.columns,n=new P(r,e),i=0;iMath.abs(r)?(e=r/t,Math.abs(t)*Math.sqrt(1+e*e)):0!==r?(e=t/r,Math.abs(r)*Math.sqrt(1+e*e)):0}function c(t,r,e){for(var n=new Array(t),i=0;i=0;t--)if(0!==v[t]){for(let r=t+1;r=0;t--){if(t0;){let t,r;for(t=R-2;t>=-1&&-1!==t;t--){const r=Number.MIN_VALUE+k*Math.abs(v[t]+Math.abs(v[t+1]));if(Math.abs(y[t])<=r||Number.isNaN(y[t])){y[t]=0;break}}if(t===R-2)r=4;else{let e;for(e=R-1;e>=t&&e!==t;e--){let r=(e!==R?Math.abs(y[e]):0)+(e!==t+1?Math.abs(y[e-1]):0);if(Math.abs(v[e])<=k*r){v[e]=0;break}}e===t?r=3:e===R-1?r=1:(r=2,t=e)}switch(t++,r){case 1:{let r=y[R-2];y[R-2]=0;for(let e=R-2;e>=t;e--){let i=l(v[e],r),o=v[e]/i,s=r/i;if(v[e]=i,e!==t&&(r=-s*y[e-1],y[e-1]=o*y[e-1]),u)for(let t=0;t=v[t+1]);){let r=v[t];if(v[t]=v[t+1],v[t+1]=r,u&&tr?i[o][e]=t[o][e]/this.s[e]:i[o][e]=0;var o=this.U,s=o.length,a=o[0].length,h=new P(e,s);for(let t=0;tt&&r++;return r}get diagonal(){return this.s}get threshold(){return Number.EPSILON/2*Math.max(this.m,this.n)*this.s[0]}get leftSingularVectors(){return P.isMatrix(this.U)||(this.U=new P(this.U)),this.U}get rightSingularVectors(){return P.isMatrix(this.V)||(this.V=new P(this.V)),this.V}get diagonalMatrix(){return P.diag(this.s)}}function m(t,r,e){var n=e?t.rows:t.rows-1;if(r<0||r>n)throw new RangeError("Row index out of range")}function g(t,r,e){var n=e?t.columns:t.columns-1;if(r<0||r>n)throw new RangeError("Column index out of range")}function p(t,r){if(r.to1DArray&&(r=r.to1DArray()),r.length!==t.columns)throw new RangeError("vector size must be the same as the number of columns");return r}function v(t,r){if(r.to1DArray&&(r=r.to1DArray()),r.length!==t.rows)throw new RangeError("vector size must be the same as the number of rows");return r}function d(t,r,e){return{row:w(t,r),column:y(t,e)}}function w(t,r){if("object"!=typeof r)throw new TypeError("unexpected type for row indices");if(r.some(r=>r<0||r>=t.rows))throw new RangeError("row indices are out of range");return Array.isArray(r)||(r=Array.from(r)),r}function y(t,r){if("object"!=typeof r)throw new TypeError("unexpected type for column indices");if(r.some(r=>r<0||r>=t.columns))throw new RangeError("column indices are out of range");return Array.isArray(r)||(r=Array.from(r)),r}function b(t,r,e,n,i){if(5!==arguments.length)throw new RangeError("expected 4 arguments");if(x("startRow",r),x("endRow",e),x("startColumn",n),x("endColumn",i),r>e||n>i||r<0||r>=t.rows||e<0||e>=t.rows||n<0||n>=t.columns||i<0||i>=t.columns)throw new RangeError("Submatrix indices are out of range")}function x(t,r){if("number"!=typeof r)throw new TypeError(`${t} must be a number`)}class M extends(I()){constructor(t,r,e){super(),this.matrix=t,this.rows=r,this.columns=e}static get[Symbol.species](){return P}}class S extends M{constructor(t){super(t,t.columns,t.rows)}set(t,r,e){return this.matrix.set(r,t,e),this}get(t,r){return this.matrix.get(r,t)}}class E extends M{constructor(t,r){super(t,1,t.columns),this.row=r}set(t,r,e){return this.matrix.set(this.row,r,e),this}get(t,r){return this.matrix.get(this.row,r)}}class R extends M{constructor(t,r,e,n,i){b(t,r,e,n,i),super(t,e-r+1,i-n+1),this.startRow=r,this.startColumn=n}set(t,r,e){return this.matrix.set(this.startRow+t,this.startColumn+r,e),this}get(t,r){return this.matrix.get(this.startRow+t,this.startColumn+r)}}class z extends M{constructor(t,r,e){var n=d(t,r,e);super(t,n.row.length,n.column.length),this.rowIndices=n.row,this.columnIndices=n.column}set(t,r,e){return this.matrix.set(this.rowIndices[t],this.columnIndices[r],e),this}get(t,r){return this.matrix.get(this.rowIndices[t],this.columnIndices[r])}}class k extends M{constructor(t,r){super(t,(r=w(t,r)).length,t.columns),this.rowIndices=r}set(t,r,e){return this.matrix.set(this.rowIndices[t],r,e),this}get(t,r){return this.matrix.get(this.rowIndices[t],r)}}class N extends M{constructor(t,r){r=y(t,r),super(t,t.rows,r.length),this.columnIndices=r}set(t,r,e){return this.matrix.set(t,this.columnIndices[r],e),this}get(t,r){return this.matrix.get(t,this.columnIndices[r])}}class A extends M{constructor(t,r){super(t,t.rows,1),this.column=r}set(t,r,e){return this.matrix.set(t,this.column,e),this}get(t){return this.matrix.get(t,this.column)}}class C extends M{constructor(t){super(t,t.rows,t.columns)}set(t,r,e){return this.matrix.set(this.rows-t-1,r,e),this}get(t,r){return this.matrix.get(this.rows-t-1,r)}}class V extends M{constructor(t){super(t,t.rows,t.columns)}set(t,r,e){return this.matrix.set(t,this.columns-r-1,e),this}get(t,r){return this.matrix.get(t,this.columns-r-1)}}function I(t){void 0===t&&(t=Object);class r extends t{static get[Symbol.species](){return this}static from1DArray(t,r,e){if(t*r!==e.length)throw new RangeError("Data length does not match given dimensions");for(var n=new this(t,r),i=0;ie?(i=!0,e=r):(n=!1,i=!0);t++}return n}isReducedEchelonForm(){let t=0,r=0,e=-1,n=!0,i=!1;for(;te?(i=!0,e=r):(n=!1,i=!0);for(let e=r+1;et&&(t=this.get(r,e));return t}maxIndex(){for(var t=this.get(0,0),r=[0,0],e=0;et&&(t=this.get(e,n),r[0]=e,r[1]=n);return r}min(){for(var t=this.get(0,0),r=0;rr&&(r=this.get(t,e));return r}maxRowIndex(t){m(this,t);for(var r=this.get(t,0),e=[t,0],n=1;nr&&(r=this.get(t,n),e[1]=n);return e}minRow(t){m(this,t);for(var r=this.get(t,0),e=1;er&&(r=this.get(e,t));return r}maxColumnIndex(t){g(this,t);for(var r=this.get(0,t),e=[0,t],n=1;nr&&(r=this.get(n,t),e[0]=n);return e}minColumn(t){g(this,t);for(var r=this.get(0,t),e=1;e=(r=void 0===r?1:r))throw new RangeError("min should be strictly smaller than max");for(var e=this.constructor.empty(this.rows,this.columns),n=0;n=(r=void 0===r?1:r))throw new RangeError("min should be strictly smaller than max");for(var e=this.constructor.empty(this.rows,this.columns),n=0;ne||r<0||r>=this.columns||e<0||e>=this.columns)throw new RangeError("Argument out of range");for(var n=new this.constructor[Symbol.species](t.length,e-r+1),i=0;i=this.rows)throw new RangeError(`Row index out of range: ${t[i]}`);n.set(i,o-r,this.get(t[i],o))}return n}subMatrixColumn(t,r,e){if(void 0===r&&(r=0),void 0===e&&(e=this.rows-1),r>e||r<0||r>=this.rows||e<0||e>=this.rows)throw new RangeError("Argument out of range");for(var n=new this.constructor[Symbol.species](e-r+1,t.length),i=0;i=this.columns)throw new RangeError(`Column index out of range: ${t[i]}`);n.set(o-r,i,this.get(o,t[i]))}return n}setSubMatrix(t,r,e){b(this,r,r+(t=this.constructor.checkMatrix(t)).rows-1,e,e+t.columns-1);for(var n=0;nt?i[o]=1/i[o]:i[o]=0;return i=this.constructor[Symbol.species].diag(i),n.mmul(i.mmul(e.transposeView()))}clone(){for(var t=new this.constructor[Symbol.species](this.rows,this.columns),r=0;r>","signPropagatingRightShift"],[">>>","rightShift","zeroFillRightShift"]]){var a=o($("\n(function %name%(value) {\n if (typeof value === 'number') return this.%name%S(value);\n return this.%name%M(value);\n})\n",{name:s[1],op:s[0]})),l=o($("\n(function %name%S(value) {\n for (var i = 0; i < this.rows; i++) {\n for (var j = 0; j < this.columns; j++) {\n this.set(i, j, this.get(i, j) %op% value);\n }\n }\n return this;\n})\n",{name:`${s[1]}S`,op:s[0]})),c=o($("\n(function %name%M(matrix) {\n matrix = this.constructor.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 (var i = 0; i < this.rows; i++) {\n for (var j = 0; j < this.columns; j++) {\n this.set(i, j, this.get(i, j) %op% matrix.get(i, j));\n }\n }\n return this;\n})\n",{name:`${s[1]}M`,op:s[0]})),w=o($("\n(function %name%(matrix, value) {\n var newMatrix = new this[Symbol.species](matrix);\n return newMatrix.%name%(value);\n})\n",{name:s[1]}));for(n=1;n0){if(super(t),!(Number.isInteger(r)&&r>0))throw new TypeError("nColumns must be a positive integer");for(e=0;e=0;o--){for(i=0;i=0;e--){for(t=0;ta(t)),u=function(t,r,e,n,i){const o=e.length,s=t.x.length;for(var a=new Array(o),h=0;h=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}};function i(t,r){return void 0===r&&(r=Math.random),Math.floor(r()*t)}function o(t){return void 0===t&&(t=Math.random),t()}function s(t){for(var r=[],e=0;er?t[e]:r;return r},r.max2d=function(t){for(var r=0,e=0;er?t[e][n]:r;return r},r.rejectionSample=function(t,r){for(var e=h(t),n=0;n=s[0])return 0;for(var h=0;h=s[0])return 0;s[0]=e,o[0]=n,a[0]=i;for(var h=0,u=0;;){var l=2*h+1,c=l+1,f=t[0][0].length;if(l>=f)break;if(c>=f){if(!(s[l]>e))break;u=l}else if(s[l]>=s[c]){if(!(ee.length;a++)1===i[a]&&n[a]=0?(i[s]=0,Math.floor(e[s])):-1}},function(t,r,e){"use strict";var n,i=this&&this.__read||function(t,r){var e="function"==typeof Symbol&&t[Symbol.iterator];if(!e)return t;var n,i,o=e.call(t),s=[];try{for(;(void 0===r||r-- >0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},o=this&&this.__spread||function(){for(var t=[],r=0;r=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}},a=this&&this.__importStar||function(t){if(t&&t.__esModule)return t;var r={};if(null!=t)for(var e in t)Object.hasOwnProperty.call(t,e)&&(r[e]=t[e]);return r.default=t,r};Object.defineProperty(r,"__esModule",{value:!0});var h=a(e(1)),u=function(){function t(t,r,e,n){this.entries=new Map,this.nRows=0,this.nCols=0,this.rows=o(t),this.cols=o(r),this.values=o(e);for(var i=0;ir?t:r})},r.multiplyScalar=function(t,r){return t.map(function(t){return t*r})},r.eliminateZeros=function(t){for(var r=new Set,e=t.getValues(),n=t.getRows(),i=t.getCols(),o=0;or?t[e]:r;return t.map(function(t){return t/r})},n.l1=function(t){for(var r=0,e=0;e0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},i=this&&this.__spread||function(){for(var t=[],r=0;r=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}},s=this&&this.__importStar||function(t){if(t&&t.__esModule)return t;var r={};if(null!=t)for(var e in t)Object.hasOwnProperty.call(t,e)&&(r[e]=t[e]);return r.default=t,r};Object.defineProperty(r,"__esModule",{value:!0});var a=s(e(1)),h=function(){return function(t,r,e,n){this.hyperplanes=t,this.offsets=r,this.children=e,this.indices=n}}();function u(t,r,e){for(var n=r,i=0;i0?0:1}r.FlatTree=h,r.makeForest=function(t,r,e,n){var o=Math.max(10,r);return a.range(e).map(function(r,e){return function(t,r,e,n){void 0===r&&(r=30);var i=a.range(t.length);return function t(r,e,n,i,o){if(void 0===n&&(n=30),e.length>n){var s=function(t,r,e){var n=t[0].length,i=a.tauRandInt(r.length,e),o=a.tauRandInt(r.length,e);o=(o+=i===o?1:0)%r.length;for(var s=r[i],h=r[o],u=0,l=a.zeros(n),c=0;c0?(g[c]=0,f+=1):(g[c]=1,m+=1)}var d=a.zeros(f),w=a.zeros(m);for(var c in f=0,m=0,a.range(g.length))0===g[c]?(d[f]=r[c],f+=1):(w[m]=r[c],m+=1);return{indicesLeft:d,indicesRight:w,hyperplane:l,offset:u}}(r,e,o),h=s.indicesLeft,u=s.indicesRight,l=s.hyperplane,c=s.offset,f=t(r,h,n,i+1,o),m=t(r,u,n,i+1,o),g={leftChild:f,rightChild:m,isLeaf:!1,hyperplane:l,offset:c};return g}var g={indices:e,isLeaf:!0};return g}(t,i,r,e,n)}(t,o,e,n)}).map(function(t){return function(t,r){var e=function t(r){return r.isLeaf?1:1+t(r.leftChild)+t(r.rightChild)}(t),n=function t(r){return r.isLeaf?1:t(r.leftChild)+t(r.rightChild)}(t),o=a.range(e).map(function(){return a.zeros(t.hyperplane.length)}),s=a.zeros(e),u=a.range(e).map(function(){return[-1,-1]}),l=a.range(n).map(function(){return a.range(r).map(function(){return-1})});return function t(r,e,n,o,s,a,h){var u;if(r.isLeaf)return o[a][0]=-h,(u=s[h]).splice.apply(u,i([0,r.indices.length],r.indices)),{nodeNum:a,leafNum:h+=1};e[a]=r.hyperplane,n[a]=r.offset,o[a][0]=a+1;var l=a,c=t(r.leftChild,e,n,o,s,a+1,h);return a=c.nodeNum,h=c.leafNum,o[l][1]=a+1,{nodeNum:(c=t(r.rightChild,e,n,o,s,a+1,h)).nodeNum,leafNum:c.leafNum}}(t,o,s,u,l,0,0),new h(o,s,u,l)}(t,o)})},r.makeLeafArray=function(t){var r,e;if(t.length>0){var n=[];try{for(var s=o(t),a=s.next();!a.done;a=s.next()){var h=a.value;n.push.apply(n,i(h.indices))}}catch(t){r={error:t}}finally{try{a&&!a.done&&(e=s.return)&&e.call(s)}finally{if(r)throw r.error}}return n}return[[-1]]},r.searchFlatTree=function(t,r){for(var e=0;r.children[e][0]>0;)e=0===u(r.hyperplanes[e],r.offsets[e],t)?r.children[e][0]:r.children[e][1];var n=-1*r.children[e][0];return r.indices[n]}},function(t,r,e){"use strict";Object.defineProperty(r,"__esModule",{value:!0});var n=e(6);window.UMAP=n.UMAP},function(t,r,e){"use strict";var n=this&&this.__awaiter||function(t,r,e,n){return new(e||(e=Promise))(function(i,o){function s(t){try{h(n.next(t))}catch(t){o(t)}}function a(t){try{h(n.throw(t))}catch(t){o(t)}}function h(t){t.done?i(t.value):new e(function(r){r(t.value)}).then(s,a)}h((n=n.apply(t,r||[])).next())})},i=this&&this.__generator||function(t,r){var e,n,i,o,s={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function a(o){return function(a){return function(o){if(e)throw new TypeError("Generator is already executing.");for(;s;)try{if(e=1,n&&(i=2&o[0]?n.return:o[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,o[1])).done)return i;switch(n=0,i&&(o=[2&o[0],i.value]),o[0]){case 0:case 1:i=o;break;case 4:return s.label++,{value:o[1],done:!1};case 5:s.label++,n=o[1],o=[0];continue;case 7:o=s.ops.pop(),s.trys.pop();continue;default:if(!(i=(i=s.trys).length>0&&i[i.length-1])&&(6===o[0]||2===o[0])){s=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},s=this&&this.__spread||function(){for(var t=[],r=0;r0&&i.set(o,u,c)}var f=l.transpose(i);return l.maximum(i,f)},t.prototype.transform=function(t){var r=this,e=this.X;if(void 0===e||0===e.length)throw new Error("No data has been fit.");var n=Math.floor(this.nNeighbors*this.transformQueueSize),i=c.initializeSearch(this.rpForest,e,t,n,this.initFromRandom,this.initFromTree),o=this.search(e,this.searchGraph,i,t),s=u.deheapSort(o),a=s.indices,h=s.weights;a=a.map(function(t){return t.slice(0,r.nNeighbors)}),h=h.map(function(t){return t.slice(0,r.nNeighbors)});var f=Math.max(0,this.localConnectivity-1),g=this.smoothKNNDistance(h,this.nNeighbors,f),p=g.sigmas,v=g.rhos,d=this.computeMembershipStrengths(a,h,p,v),w=d.rows,y=d.cols,b=d.vals,x=[t.length,e.length],M=new l.SparseMatrix(w,y,b,x),E=l.normalize(M,"l1"),R=l.getCSR(E),z=t.length,k=S(m.reshape2d(R.indices,z,this.nNeighbors),m.reshape2d(R.values,z,this.nNeighbors),this.embedding),N=this.nEpochs?this.nEpochs/3:M.nRows<=1e4?100:30,A=M.getValues().reduce(function(t,r){return r>t?r:t},0);M=M.map(function(t){return t0});if(g.length>=e){var p=Math.floor(e),v=e-p;p>0?(s[h]=g[p-1],v>1e-5&&(s[h]+=v*(g[p]-g[p-1]))):s[h]=v*g[0]}else g.length>0&&(s[h]=m.max(g));for(var d=0;d0?Math.exp(-b/c):1}if(Math.abs(w-o)<1e-5)break;w>o?c=(u+(l=c))/2:(u=c,l===1/0?c*=2:c=(u+l)/2)}if(a[h]=c,s[h]>0){var x=m.mean(f);a[h]<.001*x&&(a[h]=.001*x)}else{var M=m.mean(t.map(m.mean));a[h]<.001*M&&(a[h]=.001*M)}}return{sigmas:a,rhos:s}},t.prototype.computeMembershipStrengths=function(t,r,e,n){for(var i=t.length,o=t[0].length,s=m.zeros(i*o),a=m.zeros(i*o),h=m.zeros(i*o),u=0;u0&&(e[n]=r/i[n])}),e},t.prototype.assignOptimizationStateParameters=function(t){Object.assign(this.optimizationState,t)},t.prototype.prepareForOptimizationLoop=function(){var t=this.repulsionStrength,r=this.learningRate,e=this.negativeSampleRate,n=this.optimizationState,i=n.epochsPerSample,o=n.headEmbedding,a=n.tailEmbedding,h=o[0].length,u=o.length===a.length,l=i.map(function(t){return t/e}),c=s(l),f=s(i);this.assignOptimizationStateParameters({epochOfNextSample:f,epochOfNextNegativeSample:c,epochsPerNegativeSample:l,moveOther:u,initialAlpha:r,alpha:r,gamma:t,dim:h})},t.prototype.initializeOptimization=function(){var t=this.embedding,r=this.embedding,e=this.optimizationState,n=e.head,i=e.tail,o=e.epochsPerSample,s=this.getNEpochs(),a=this.graph.nCols,h=b(this.spread,this.minDist),u=h.a,l=h.b;this.assignOptimizationStateParameters({headEmbedding:t,tailEmbedding:r,head:n,tail:i,epochsPerSample:o,a:u,b:l,nEpochs:s,nVertices:a})},t.prototype.optimizeLayoutStep=function(t){for(var r=this.optimizationState,e=r.head,n=r.tail,i=r.headEmbedding,o=r.tailEmbedding,s=r.epochsPerSample,a=r.epochOfNextSample,h=r.epochOfNextNegativeSample,u=r.epochsPerNegativeSample,l=r.moveOther,c=r.initialAlpha,f=r.alpha,g=r.gamma,p=r.a,v=r.b,d=r.dim,b=r.nEpochs,x=r.nVertices,M=0;Mt)){var S=e[M],E=n[M],R=i[S],z=o[E],k=y(R,z),N=0;k>0&&(N=-2*p*v*Math.pow(k,v-1),N/=p*Math.pow(k,v)+1);for(var A=0;A0)O=2*g*v,O/=(.001+I)*(p*Math.pow(I,v)+1);else if(S===P)continue;for(A=0;A0&&(_=w(O*(R[A]-j[A]),4)),R[A]+=_*f}}h[M]+=C*u[M]}return r.alpha=c*(1-t/b),r.currentEpoch+=1,i},t.prototype.optimizeLayoutAsync=function(t){var r=this;return void 0===t&&(t=function(){return!0}),new Promise(function(e,o){var s=function(){return n(r,void 0,void 0,function(){var r,n,a,h,u,l;return i(this,function(i){try{if(r=this.optimizationState,n=r.nEpochs,a=r.currentEpoch,this.embedding=this.optimizeLayoutStep(a),h=this.optimizationState.currentEpoch,u=!1===t(h),l=h===n,u||l)return[2,e(l)];s()}catch(t){o(t)}return[2]})})};s()})},t.prototype.optimizeLayout=function(t){void 0===t&&(t=function(){return!0});for(var r=!1,e=[];!r;){var n=this.optimizationState,i=n.nEpochs,o=n.currentEpoch;e=this.optimizeLayoutStep(o);var s=this.optimizationState.currentEpoch,a=!1===t(s);r=s===i||a}return e},t.prototype.getNEpochs=function(){var t=this.graph;if(this.nEpochs>0)return this.nEpochs;var r=t.nRows;return r<=2500?500:r<=5e3?400:r<=7500?300:200},t}();function v(t,r){for(var e=0,n=0;nr?r:t<-r?-r:t}function y(t,r){for(var e=0,n=0;n=r?Math.exp(-(e[i]-r)/t):n}),i={x:e,y:n},s={damping:1.5,initialValues:[.5,.5],gradientDifference:.1,maxIterations:100,errorTolerance:.01},a=g.default(i,function(t){var r=o(t,2),e=r[0],n=r[1];return function(t){return 1/(1+e*Math.pow(t,2*n))}},s).parameterValues,h=o(a,2);return{a:h[0],b:h[1]}}function x(t,r,e,n){return void 0===e&&(e=1),void 0===n&&(n=5),t.map(function(t,i,o){return-1===r[i]||-1===r[o]?t*Math.exp(-e):r[i]!==r[o]?t*Math.exp(-n):t})}function M(t){t=l.normalize(t,"max");var r=l.transpose(t),e=l.pairwiseMultiply(r,t);return t=l.add(t,l.subtract(r,e)),l.eliminateZeros(t)}function S(t,r,e){for(var n=m.zeros(t.length).map(function(t){return m.zeros(e[0].length)}),i=0;i=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}},i=this&&this.__importStar||function(t){if(t&&t.__esModule)return t;var r={};if(null!=t)for(var e in t)Object.hasOwnProperty.call(t,e)&&(r[e]=t[e]);return r.default=t,r};Object.defineProperty(r,"__esModule",{value:!0});var o=i(e(2)),s=i(e(3)),a=i(e(4)),h=i(e(1));r.makeNNDescent=function(t,r){return function(e,n,i,s,a,u,l,c){void 0===s&&(s=10),void 0===a&&(a=50),void 0===u&&(u=.001),void 0===l&&(l=.5),void 0===c&&(c=!0);for(var f=e.length,m=o.makeHeap(e.length,i),g=0;gr&&(r=t[e]);return r};var a=function(t){if(!o()(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");for(var r=t[0],e=1;e1&&void 0!==arguments[1]?arguments[1]:{};if(!o()(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");if(void 0!==e.output){if(!o()(e.output))throw new TypeError("output option must be an array if specified");r=e.output}else r=new Array(t.length);var n=a(t),i=s(t);if(n===i)throw new RangeError("minimum and maximum input values are equal. Cannot rescale a constant array");var h=e.min,u=void 0===h?e.autoMinMax?n:0:h,l=e.max,c=void 0===l?e.autoMinMax?i:1:l;if(u>=c)throw new RangeError("min option must be smaller than max option");for(var f=(c-u)/(i-n),m=0;mMath.abs(h[i])&&(i=r);if(i!==e){for(n=0;n=0;i--){for(n=0;no?t[i][o]:i===o?1:0;return n}get upperTriangularMatrix(){for(var t=this.LU,r=t.rows,e=t.columns,n=new P(r,e),i=0;iMath.abs(r)?(e=r/t,Math.abs(t)*Math.sqrt(1+e*e)):0!==r?(e=t/r,Math.abs(r)*Math.sqrt(1+e*e)):0}function c(t,r,e){for(var n=new Array(t),i=0;i=0;t--)if(0!==v[t]){for(let r=t+1;r=0;t--){if(t0;){let t,r;for(t=R-2;t>=-1&&-1!==t;t--){const r=Number.MIN_VALUE+k*Math.abs(v[t]+Math.abs(v[t+1]));if(Math.abs(y[t])<=r||Number.isNaN(y[t])){y[t]=0;break}}if(t===R-2)r=4;else{let e;for(e=R-1;e>=t&&e!==t;e--){let r=(e!==R?Math.abs(y[e]):0)+(e!==t+1?Math.abs(y[e-1]):0);if(Math.abs(v[e])<=k*r){v[e]=0;break}}e===t?r=3:e===R-1?r=1:(r=2,t=e)}switch(t++,r){case 1:{let r=y[R-2];y[R-2]=0;for(let e=R-2;e>=t;e--){let i=l(v[e],r),o=v[e]/i,s=r/i;if(v[e]=i,e!==t&&(r=-s*y[e-1],y[e-1]=o*y[e-1]),u)for(let t=0;t=v[t+1]);){let r=v[t];if(v[t]=v[t+1],v[t+1]=r,u&&tr?i[o][e]=t[o][e]/this.s[e]:i[o][e]=0;var o=this.U,s=o.length,a=o[0].length,h=new P(e,s);for(let t=0;tt&&r++;return r}get diagonal(){return this.s}get threshold(){return Number.EPSILON/2*Math.max(this.m,this.n)*this.s[0]}get leftSingularVectors(){return P.isMatrix(this.U)||(this.U=new P(this.U)),this.U}get rightSingularVectors(){return P.isMatrix(this.V)||(this.V=new P(this.V)),this.V}get diagonalMatrix(){return P.diag(this.s)}}function m(t,r,e){var n=e?t.rows:t.rows-1;if(r<0||r>n)throw new RangeError("Row index out of range")}function g(t,r,e){var n=e?t.columns:t.columns-1;if(r<0||r>n)throw new RangeError("Column index out of range")}function p(t,r){if(r.to1DArray&&(r=r.to1DArray()),r.length!==t.columns)throw new RangeError("vector size must be the same as the number of columns");return r}function v(t,r){if(r.to1DArray&&(r=r.to1DArray()),r.length!==t.rows)throw new RangeError("vector size must be the same as the number of rows");return r}function d(t,r,e){return{row:w(t,r),column:y(t,e)}}function w(t,r){if("object"!=typeof r)throw new TypeError("unexpected type for row indices");if(r.some(r=>r<0||r>=t.rows))throw new RangeError("row indices are out of range");return Array.isArray(r)||(r=Array.from(r)),r}function y(t,r){if("object"!=typeof r)throw new TypeError("unexpected type for column indices");if(r.some(r=>r<0||r>=t.columns))throw new RangeError("column indices are out of range");return Array.isArray(r)||(r=Array.from(r)),r}function b(t,r,e,n,i){if(5!==arguments.length)throw new RangeError("expected 4 arguments");if(x("startRow",r),x("endRow",e),x("startColumn",n),x("endColumn",i),r>e||n>i||r<0||r>=t.rows||e<0||e>=t.rows||n<0||n>=t.columns||i<0||i>=t.columns)throw new RangeError("Submatrix indices are out of range")}function x(t,r){if("number"!=typeof r)throw new TypeError(`${t} must be a number`)}class M extends(V()){constructor(t,r,e){super(),this.matrix=t,this.rows=r,this.columns=e}static get[Symbol.species](){return P}}class S extends M{constructor(t){super(t,t.columns,t.rows)}set(t,r,e){return this.matrix.set(r,t,e),this}get(t,r){return this.matrix.get(r,t)}}class E extends M{constructor(t,r){super(t,1,t.columns),this.row=r}set(t,r,e){return this.matrix.set(this.row,r,e),this}get(t,r){return this.matrix.get(this.row,r)}}class R extends M{constructor(t,r,e,n,i){b(t,r,e,n,i),super(t,e-r+1,i-n+1),this.startRow=r,this.startColumn=n}set(t,r,e){return this.matrix.set(this.startRow+t,this.startColumn+r,e),this}get(t,r){return this.matrix.get(this.startRow+t,this.startColumn+r)}}class z extends M{constructor(t,r,e){var n=d(t,r,e);super(t,n.row.length,n.column.length),this.rowIndices=n.row,this.columnIndices=n.column}set(t,r,e){return this.matrix.set(this.rowIndices[t],this.columnIndices[r],e),this}get(t,r){return this.matrix.get(this.rowIndices[t],this.columnIndices[r])}}class k extends M{constructor(t,r){super(t,(r=w(t,r)).length,t.columns),this.rowIndices=r}set(t,r,e){return this.matrix.set(this.rowIndices[t],r,e),this}get(t,r){return this.matrix.get(this.rowIndices[t],r)}}class N extends M{constructor(t,r){r=y(t,r),super(t,t.rows,r.length),this.columnIndices=r}set(t,r,e){return this.matrix.set(t,this.columnIndices[r],e),this}get(t,r){return this.matrix.get(t,this.columnIndices[r])}}class A extends M{constructor(t,r){super(t,t.rows,1),this.column=r}set(t,r,e){return this.matrix.set(t,this.column,e),this}get(t){return this.matrix.get(t,this.column)}}class _ extends M{constructor(t){super(t,t.rows,t.columns)}set(t,r,e){return this.matrix.set(this.rows-t-1,r,e),this}get(t,r){return this.matrix.get(this.rows-t-1,r)}}class C extends M{constructor(t){super(t,t.rows,t.columns)}set(t,r,e){return this.matrix.set(t,this.columns-r-1,e),this}get(t,r){return this.matrix.get(t,this.columns-r-1)}}function V(t){void 0===t&&(t=Object);class r extends t{static get[Symbol.species](){return this}static from1DArray(t,r,e){if(t*r!==e.length)throw new RangeError("Data length does not match given dimensions");for(var n=new this(t,r),i=0;ie?(i=!0,e=r):(n=!1,i=!0);t++}return n}isReducedEchelonForm(){let t=0,r=0,e=-1,n=!0,i=!1;for(;te?(i=!0,e=r):(n=!1,i=!0);for(let e=r+1;et&&(t=this.get(r,e));return t}maxIndex(){for(var t=this.get(0,0),r=[0,0],e=0;et&&(t=this.get(e,n),r[0]=e,r[1]=n);return r}min(){for(var t=this.get(0,0),r=0;rr&&(r=this.get(t,e));return r}maxRowIndex(t){m(this,t);for(var r=this.get(t,0),e=[t,0],n=1;nr&&(r=this.get(t,n),e[1]=n);return e}minRow(t){m(this,t);for(var r=this.get(t,0),e=1;er&&(r=this.get(e,t));return r}maxColumnIndex(t){g(this,t);for(var r=this.get(0,t),e=[0,t],n=1;nr&&(r=this.get(n,t),e[0]=n);return e}minColumn(t){g(this,t);for(var r=this.get(0,t),e=1;e=(r=void 0===r?1:r))throw new RangeError("min should be strictly smaller than max");for(var e=this.constructor.empty(this.rows,this.columns),n=0;n=(r=void 0===r?1:r))throw new RangeError("min should be strictly smaller than max");for(var e=this.constructor.empty(this.rows,this.columns),n=0;ne||r<0||r>=this.columns||e<0||e>=this.columns)throw new RangeError("Argument out of range");for(var n=new this.constructor[Symbol.species](t.length,e-r+1),i=0;i=this.rows)throw new RangeError(`Row index out of range: ${t[i]}`);n.set(i,o-r,this.get(t[i],o))}return n}subMatrixColumn(t,r,e){if(void 0===r&&(r=0),void 0===e&&(e=this.rows-1),r>e||r<0||r>=this.rows||e<0||e>=this.rows)throw new RangeError("Argument out of range");for(var n=new this.constructor[Symbol.species](e-r+1,t.length),i=0;i=this.columns)throw new RangeError(`Column index out of range: ${t[i]}`);n.set(o-r,i,this.get(o,t[i]))}return n}setSubMatrix(t,r,e){b(this,r,r+(t=this.constructor.checkMatrix(t)).rows-1,e,e+t.columns-1);for(var n=0;nt?i[o]=1/i[o]:i[o]=0;return i=this.constructor[Symbol.species].diag(i),n.mmul(i.mmul(e.transposeView()))}clone(){for(var t=new this.constructor[Symbol.species](this.rows,this.columns),r=0;r>","signPropagatingRightShift"],[">>>","rightShift","zeroFillRightShift"]]){var a=o($("\n(function %name%(value) {\n if (typeof value === 'number') return this.%name%S(value);\n return this.%name%M(value);\n})\n",{name:s[1],op:s[0]})),l=o($("\n(function %name%S(value) {\n for (var i = 0; i < this.rows; i++) {\n for (var j = 0; j < this.columns; j++) {\n this.set(i, j, this.get(i, j) %op% value);\n }\n }\n return this;\n})\n",{name:`${s[1]}S`,op:s[0]})),c=o($("\n(function %name%M(matrix) {\n matrix = this.constructor.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 (var i = 0; i < this.rows; i++) {\n for (var j = 0; j < this.columns; j++) {\n this.set(i, j, this.get(i, j) %op% matrix.get(i, j));\n }\n }\n return this;\n})\n",{name:`${s[1]}M`,op:s[0]})),w=o($("\n(function %name%(matrix, value) {\n var newMatrix = new this[Symbol.species](matrix);\n return newMatrix.%name%(value);\n})\n",{name:s[1]}));for(n=1;n0){if(super(t),!(Number.isInteger(r)&&r>0))throw new TypeError("nColumns must be a positive integer");for(e=0;e=0;o--){for(i=0;i=0;e--){for(t=0;ta(t)),u=function(t,r,e,n,i){const o=e.length,s=t.x.length;for(var a=new Array(o),h=0;h Date: Thu, 23 May 2019 11:28:06 -0700 Subject: [PATCH 10/46] Bump to 1.2.0 for new npm publish --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 61a7183..42bc77b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "umap-js", - "version": "1.1.1", + "version": "1.2.0", "description": "JavaScript implementation of UMAP", "author": { "name": "Andy Coenen", From acb31e7ac6d1ee56b913d076cb2a8a56819abddc Mon Sep 17 00:00:00 2001 From: Andy Coenen Date: Thu, 23 May 2019 11:28:39 -0700 Subject: [PATCH 11/46] Build artifacts --- lib/umap-js.js | 9421 ++++++++++++++++++++++---------------------- lib/umap-js.min.js | 2 +- 2 files changed, 4685 insertions(+), 4738 deletions(-) diff --git a/lib/umap-js.js b/lib/umap-js.js index 091fe80..78a544e 100644 --- a/lib/umap-js.js +++ b/lib/umap-js.js @@ -670,8 +670,8 @@ function eliminateZeros(m) { } exports.eliminateZeros = eliminateZeros; function normalize(m, normType) { - var e_1, _a; if (normType === void 0) { normType = "l2"; } + var e_1, _a; var normFn = normFns[normType]; var colsByRow = new Map(); m.forEach(function (_, row, col) { @@ -1141,8 +1141,8 @@ var SMOOTH_K_TOLERANCE = 1e-5; var MIN_K_DIST_SCALE = 1e-3; var UMAP = (function () { function UMAP(params) { - var _this = this; if (params === void 0) { params = {}; } + var _this = this; this.learningRate = 1.0; this.localConnectivity = 1.0; this.minDist = 0.1; @@ -1999,12 +1999,13 @@ exports.initializeSearch = initializeSearch; /***/ }), /* 8 */ -/***/ (function(module, __webpack_exports__, __webpack_require__) { +/***/ (function(module, exports, __webpack_require__) { "use strict"; -__webpack_require__.r(__webpack_exports__); -// CONCATENATED MODULE: ./node_modules/ml-levenberg-marquardt/src/errorCalculation.js + +var mlMatrix = __webpack_require__(9); + /** * Calculate current error * @ignore @@ -2028,4698 +2029,6 @@ function errorCalculation( return error; } -// EXTERNAL MODULE: ./node_modules/is-any-array/src/index.js -var src = __webpack_require__(0); -var src_default = /*#__PURE__*/__webpack_require__.n(src); - -// CONCATENATED MODULE: ./node_modules/ml-array-max/lib-es6/index.js - - -/** - * Computes the maximum of the given values - * @param {Array} input - * @return {number} - */ - -function lib_es6_max(input) { - if (!src_default()(input)) { - throw new TypeError('input must be an array'); - } - - if (input.length === 0) { - throw new TypeError('input must not be empty'); - } - - var max = input[0]; - - for (var i = 1; i < input.length; i++) { - if (input[i] > max) max = input[i]; - } - - return max; -} - -/* harmony default export */ var lib_es6 = (lib_es6_max); - -// CONCATENATED MODULE: ./node_modules/ml-array-min/lib-es6/index.js - - -/** - * Computes the minimum of the given values - * @param {Array} input - * @return {number} - */ - -function lib_es6_min(input) { - if (!src_default()(input)) { - throw new TypeError('input must be an array'); - } - - if (input.length === 0) { - throw new TypeError('input must not be empty'); - } - - var min = input[0]; - - for (var i = 1; i < input.length; i++) { - if (input[i] < min) min = input[i]; - } - - return min; -} - -/* harmony default export */ var ml_array_min_lib_es6 = (lib_es6_min); - -// CONCATENATED MODULE: ./node_modules/ml-array-rescale/lib-es6/index.js - - - - -function rescale(input) { - var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; - - if (!src_default()(input)) { - throw new TypeError('input must be an array'); - } else if (input.length === 0) { - throw new TypeError('input must not be empty'); - } - - var output; - - if (options.output !== undefined) { - if (!src_default()(options.output)) { - throw new TypeError('output option must be an array if specified'); - } - - output = options.output; - } else { - output = new Array(input.length); - } - - var currentMin = ml_array_min_lib_es6(input); - var currentMax = lib_es6(input); - - if (currentMin === currentMax) { - throw new RangeError('minimum and maximum input values are equal. Cannot rescale a constant array'); - } - - var _options$min = options.min, - minValue = _options$min === void 0 ? options.autoMinMax ? currentMin : 0 : _options$min, - _options$max = options.max, - maxValue = _options$max === void 0 ? options.autoMinMax ? currentMax : 1 : _options$max; - - if (minValue >= maxValue) { - throw new RangeError('min option must be smaller than max option'); - } - - var factor = (maxValue - minValue) / (currentMax - currentMin); - - for (var i = 0; i < input.length; i++) { - output[i] = (input[i] - currentMin) * factor + minValue; - } - - return output; -} - -/* harmony default export */ var ml_array_rescale_lib_es6 = (rescale); - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/dc/lu.js - - -/** - * @class LuDecomposition - * @link https://github.com/lutzroeder/Mapack/blob/master/Source/LuDecomposition.cs - * @param {Matrix} matrix - */ -class lu_LuDecomposition { - constructor(matrix) { - matrix = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(matrix); - - var lu = matrix.clone(); - var rows = lu.rows; - var columns = lu.columns; - var pivotVector = new Array(rows); - var pivotSign = 1; - var i, j, k, p, s, t, v; - var LUcolj, kmax; - - for (i = 0; i < rows; i++) { - pivotVector[i] = i; - } - - LUcolj = new Array(rows); - - for (j = 0; j < columns; j++) { - for (i = 0; i < rows; i++) { - LUcolj[i] = lu.get(i, j); - } - - for (i = 0; i < rows; i++) { - kmax = Math.min(i, j); - s = 0; - for (k = 0; k < kmax; k++) { - s += lu.get(i, k) * LUcolj[k]; - } - LUcolj[i] -= s; - lu.set(i, j, LUcolj[i]); - } - - p = j; - for (i = j + 1; i < rows; i++) { - if (Math.abs(LUcolj[i]) > Math.abs(LUcolj[p])) { - p = i; - } - } - - if (p !== j) { - for (k = 0; k < columns; k++) { - t = lu.get(p, k); - lu.set(p, k, lu.get(j, k)); - lu.set(j, k, t); - } - - v = pivotVector[p]; - pivotVector[p] = pivotVector[j]; - pivotVector[j] = v; - - pivotSign = -pivotSign; - } - - if (j < rows && lu.get(j, j) !== 0) { - for (i = j + 1; i < rows; i++) { - lu.set(i, j, lu.get(i, j) / lu.get(j, j)); - } - } - } - - this.LU = lu; - this.pivotVector = pivotVector; - this.pivotSign = pivotSign; - } - - /** - * - * @return {boolean} - */ - isSingular() { - var data = this.LU; - var col = data.columns; - for (var j = 0; j < col; j++) { - if (data[j][j] === 0) { - return true; - } - } - return false; - } - - /** - * - * @param {Matrix} value - * @return {Matrix} - */ - solve(value) { - value = matrix_Matrix.checkMatrix(value); - - var lu = this.LU; - var rows = lu.rows; - - if (rows !== value.rows) { - throw new Error('Invalid matrix dimensions'); - } - if (this.isSingular()) { - throw new Error('LU matrix is singular'); - } - - var count = value.columns; - var X = value.subMatrixRow(this.pivotVector, 0, count - 1); - var columns = lu.columns; - var i, j, k; - - for (k = 0; k < columns; k++) { - for (i = k + 1; i < columns; i++) { - for (j = 0; j < count; j++) { - X[i][j] -= X[k][j] * lu[i][k]; - } - } - } - for (k = columns - 1; k >= 0; k--) { - for (j = 0; j < count; j++) { - X[k][j] /= lu[k][k]; - } - for (i = 0; i < k; i++) { - for (j = 0; j < count; j++) { - X[i][j] -= X[k][j] * lu[i][k]; - } - } - } - return X; - } - - /** - * - * @return {number} - */ - get determinant() { - var data = this.LU; - if (!data.isSquare()) { - throw new Error('Matrix must be square'); - } - var determinant = this.pivotSign; - var col = data.columns; - for (var j = 0; j < col; j++) { - determinant *= data[j][j]; - } - return determinant; - } - - /** - * - * @return {Matrix} - */ - get lowerTriangularMatrix() { - var data = this.LU; - var rows = data.rows; - var columns = data.columns; - var X = new matrix_Matrix(rows, columns); - for (var i = 0; i < rows; i++) { - for (var j = 0; j < columns; j++) { - if (i > j) { - X[i][j] = data[i][j]; - } else if (i === j) { - X[i][j] = 1; - } else { - X[i][j] = 0; - } - } - } - return X; - } - - /** - * - * @return {Matrix} - */ - get upperTriangularMatrix() { - var data = this.LU; - var rows = data.rows; - var columns = data.columns; - var X = new matrix_Matrix(rows, columns); - for (var i = 0; i < rows; i++) { - for (var j = 0; j < columns; j++) { - if (i <= j) { - X[i][j] = data[i][j]; - } else { - X[i][j] = 0; - } - } - } - return X; - } - - /** - * - * @return {Array} - */ - get pivotPermutationVector() { - return this.pivotVector.slice(); - } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/dc/util.js -function hypotenuse(a, b) { - var r = 0; - if (Math.abs(a) > Math.abs(b)) { - r = b / a; - return Math.abs(a) * Math.sqrt(1 + r * r); - } - if (b !== 0) { - r = a / b; - return Math.abs(b) * Math.sqrt(1 + r * r); - } - return 0; -} - -function getFilled2DArray(rows, columns, value) { - var array = new Array(rows); - for (var i = 0; i < rows; i++) { - array[i] = new Array(columns); - for (var j = 0; j < columns; j++) { - array[i][j] = value; - } - } - return array; -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/dc/svd.js - - - - -/** - * @class SingularValueDecomposition - * @see https://github.com/accord-net/framework/blob/development/Sources/Accord.Math/Decompositions/SingularValueDecomposition.cs - * @param {Matrix} value - * @param {object} [options] - * @param {boolean} [options.computeLeftSingularVectors=true] - * @param {boolean} [options.computeRightSingularVectors=true] - * @param {boolean} [options.autoTranspose=false] - */ -class svd_SingularValueDecomposition { - constructor(value, options = {}) { - value = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(value); - - var m = value.rows; - var n = value.columns; - - const { - computeLeftSingularVectors = true, - computeRightSingularVectors = true, - autoTranspose = false - } = options; - - var wantu = Boolean(computeLeftSingularVectors); - var wantv = Boolean(computeRightSingularVectors); - - var swapped = false; - var a; - if (m < n) { - if (!autoTranspose) { - a = value.clone(); - // eslint-disable-next-line no-console - console.warn( - 'Computing SVD on a matrix with more columns than rows. Consider enabling autoTranspose' - ); - } else { - a = value.transpose(); - m = a.rows; - n = a.columns; - swapped = true; - var aux = wantu; - wantu = wantv; - wantv = aux; - } - } else { - a = value.clone(); - } - - var nu = Math.min(m, n); - var ni = Math.min(m + 1, n); - var s = new Array(ni); - var U = getFilled2DArray(m, nu, 0); - var V = getFilled2DArray(n, n, 0); - - var e = new Array(n); - var work = new Array(m); - - var si = new Array(ni); - for (let i = 0; i < ni; i++) si[i] = i; - - var nct = Math.min(m - 1, n); - var nrt = Math.max(0, Math.min(n - 2, m)); - var mrc = Math.max(nct, nrt); - - for (let k = 0; k < mrc; k++) { - if (k < nct) { - s[k] = 0; - for (let i = k; i < m; i++) { - s[k] = hypotenuse(s[k], a[i][k]); - } - if (s[k] !== 0) { - if (a[k][k] < 0) { - s[k] = -s[k]; - } - for (let i = k; i < m; i++) { - a[i][k] /= s[k]; - } - a[k][k] += 1; - } - s[k] = -s[k]; - } - - for (let j = k + 1; j < n; j++) { - if (k < nct && s[k] !== 0) { - let t = 0; - for (let i = k; i < m; i++) { - t += a[i][k] * a[i][j]; - } - t = -t / a[k][k]; - for (let i = k; i < m; i++) { - a[i][j] += t * a[i][k]; - } - } - e[j] = a[k][j]; - } - - if (wantu && k < nct) { - for (let i = k; i < m; i++) { - U[i][k] = a[i][k]; - } - } - - if (k < nrt) { - e[k] = 0; - for (let i = k + 1; i < n; i++) { - e[k] = hypotenuse(e[k], e[i]); - } - if (e[k] !== 0) { - if (e[k + 1] < 0) { - e[k] = 0 - e[k]; - } - for (let i = k + 1; i < n; i++) { - e[i] /= e[k]; - } - e[k + 1] += 1; - } - e[k] = -e[k]; - if (k + 1 < m && e[k] !== 0) { - for (let i = k + 1; i < m; i++) { - work[i] = 0; - } - for (let i = k + 1; i < m; i++) { - for (let j = k + 1; j < n; j++) { - work[i] += e[j] * a[i][j]; - } - } - for (let j = k + 1; j < n; j++) { - let t = -e[j] / e[k + 1]; - for (let i = k + 1; i < m; i++) { - a[i][j] += t * work[i]; - } - } - } - if (wantv) { - for (let i = k + 1; i < n; i++) { - V[i][k] = e[i]; - } - } - } - } - - let p = Math.min(n, m + 1); - if (nct < n) { - s[nct] = a[nct][nct]; - } - if (m < p) { - s[p - 1] = 0; - } - if (nrt + 1 < p) { - e[nrt] = a[nrt][p - 1]; - } - e[p - 1] = 0; - - if (wantu) { - for (let j = nct; j < nu; j++) { - for (let i = 0; i < m; i++) { - U[i][j] = 0; - } - U[j][j] = 1; - } - for (let k = nct - 1; k >= 0; k--) { - if (s[k] !== 0) { - for (let j = k + 1; j < nu; j++) { - let t = 0; - for (let i = k; i < m; i++) { - t += U[i][k] * U[i][j]; - } - t = -t / U[k][k]; - for (let i = k; i < m; i++) { - U[i][j] += t * U[i][k]; - } - } - for (let i = k; i < m; i++) { - U[i][k] = -U[i][k]; - } - U[k][k] = 1 + U[k][k]; - for (let i = 0; i < k - 1; i++) { - U[i][k] = 0; - } - } else { - for (let i = 0; i < m; i++) { - U[i][k] = 0; - } - U[k][k] = 1; - } - } - } - - if (wantv) { - for (let k = n - 1; k >= 0; k--) { - if (k < nrt && e[k] !== 0) { - for (let j = k + 1; j < n; j++) { - let t = 0; - for (let i = k + 1; i < n; i++) { - t += V[i][k] * V[i][j]; - } - t = -t / V[k + 1][k]; - for (let i = k + 1; i < n; i++) { - V[i][j] += t * V[i][k]; - } - } - } - for (let i = 0; i < n; i++) { - V[i][k] = 0; - } - V[k][k] = 1; - } - } - - var pp = p - 1; - var iter = 0; - var eps = Number.EPSILON; - while (p > 0) { - let k, kase; - for (k = p - 2; k >= -1; k--) { - if (k === -1) { - break; - } - const alpha = - Number.MIN_VALUE + eps * Math.abs(s[k] + Math.abs(s[k + 1])); - if (Math.abs(e[k]) <= alpha || Number.isNaN(e[k])) { - e[k] = 0; - break; - } - } - if (k === p - 2) { - kase = 4; - } else { - let ks; - for (ks = p - 1; ks >= k; ks--) { - if (ks === k) { - break; - } - let t = - (ks !== p ? Math.abs(e[ks]) : 0) + - (ks !== k + 1 ? Math.abs(e[ks - 1]) : 0); - if (Math.abs(s[ks]) <= eps * t) { - s[ks] = 0; - break; - } - } - if (ks === k) { - kase = 3; - } else if (ks === p - 1) { - kase = 1; - } else { - kase = 2; - k = ks; - } - } - - k++; - - switch (kase) { - case 1: { - let f = e[p - 2]; - e[p - 2] = 0; - for (let j = p - 2; j >= k; j--) { - let t = hypotenuse(s[j], f); - let cs = s[j] / t; - let sn = f / t; - s[j] = t; - if (j !== k) { - f = -sn * e[j - 1]; - e[j - 1] = cs * e[j - 1]; - } - if (wantv) { - for (let i = 0; i < n; i++) { - t = cs * V[i][j] + sn * V[i][p - 1]; - V[i][p - 1] = -sn * V[i][j] + cs * V[i][p - 1]; - V[i][j] = t; - } - } - } - break; - } - case 2: { - let f = e[k - 1]; - e[k - 1] = 0; - for (let j = k; j < p; j++) { - let t = hypotenuse(s[j], f); - let cs = s[j] / t; - let sn = f / t; - s[j] = t; - f = -sn * e[j]; - e[j] = cs * e[j]; - if (wantu) { - for (let i = 0; i < m; i++) { - t = cs * U[i][j] + sn * U[i][k - 1]; - U[i][k - 1] = -sn * U[i][j] + cs * U[i][k - 1]; - U[i][j] = t; - } - } - } - break; - } - case 3: { - const scale = Math.max( - Math.abs(s[p - 1]), - Math.abs(s[p - 2]), - Math.abs(e[p - 2]), - Math.abs(s[k]), - Math.abs(e[k]) - ); - const sp = s[p - 1] / scale; - const spm1 = s[p - 2] / scale; - const epm1 = e[p - 2] / scale; - const sk = s[k] / scale; - const ek = e[k] / scale; - const b = ((spm1 + sp) * (spm1 - sp) + epm1 * epm1) / 2; - const c = sp * epm1 * (sp * epm1); - let shift = 0; - if (b !== 0 || c !== 0) { - if (b < 0) { - shift = 0 - Math.sqrt(b * b + c); - } else { - shift = Math.sqrt(b * b + c); - } - shift = c / (b + shift); - } - let f = (sk + sp) * (sk - sp) + shift; - let g = sk * ek; - for (let j = k; j < p - 1; j++) { - let t = hypotenuse(f, g); - if (t === 0) t = Number.MIN_VALUE; - let cs = f / t; - let sn = g / t; - if (j !== k) { - e[j - 1] = t; - } - f = cs * s[j] + sn * e[j]; - e[j] = cs * e[j] - sn * s[j]; - g = sn * s[j + 1]; - s[j + 1] = cs * s[j + 1]; - if (wantv) { - for (let i = 0; i < n; i++) { - t = cs * V[i][j] + sn * V[i][j + 1]; - V[i][j + 1] = -sn * V[i][j] + cs * V[i][j + 1]; - V[i][j] = t; - } - } - t = hypotenuse(f, g); - if (t === 0) t = Number.MIN_VALUE; - cs = f / t; - sn = g / t; - s[j] = t; - f = cs * e[j] + sn * s[j + 1]; - s[j + 1] = -sn * e[j] + cs * s[j + 1]; - g = sn * e[j + 1]; - e[j + 1] = cs * e[j + 1]; - if (wantu && j < m - 1) { - for (let i = 0; i < m; i++) { - t = cs * U[i][j] + sn * U[i][j + 1]; - U[i][j + 1] = -sn * U[i][j] + cs * U[i][j + 1]; - U[i][j] = t; - } - } - } - e[p - 2] = f; - iter = iter + 1; - break; - } - case 4: { - if (s[k] <= 0) { - s[k] = s[k] < 0 ? -s[k] : 0; - if (wantv) { - for (let i = 0; i <= pp; i++) { - V[i][k] = -V[i][k]; - } - } - } - while (k < pp) { - if (s[k] >= s[k + 1]) { - break; - } - let t = s[k]; - s[k] = s[k + 1]; - s[k + 1] = t; - if (wantv && k < n - 1) { - for (let i = 0; i < n; i++) { - t = V[i][k + 1]; - V[i][k + 1] = V[i][k]; - V[i][k] = t; - } - } - if (wantu && k < m - 1) { - for (let i = 0; i < m; i++) { - t = U[i][k + 1]; - U[i][k + 1] = U[i][k]; - U[i][k] = t; - } - } - k++; - } - iter = 0; - p--; - break; - } - // no default - } - } - - if (swapped) { - var tmp = V; - V = U; - U = tmp; - } - - this.m = m; - this.n = n; - this.s = s; - this.U = U; - this.V = V; - } - - /** - * Solve a problem of least square (Ax=b) by using the SVD. Useful when A is singular. When A is not singular, it would be better to use qr.solve(value). - * Example : We search to approximate x, with A matrix shape m*n, x vector size n, b vector size m (m > n). We will use : - * var svd = SingularValueDecomposition(A); - * var x = svd.solve(b); - * @param {Matrix} value - Matrix 1D which is the vector b (in the equation Ax = b) - * @return {Matrix} - The vector x - */ - solve(value) { - var Y = value; - var e = this.threshold; - var scols = this.s.length; - var Ls = matrix_Matrix.zeros(scols, scols); - - for (let i = 0; i < scols; i++) { - if (Math.abs(this.s[i]) <= e) { - Ls[i][i] = 0; - } else { - Ls[i][i] = 1 / this.s[i]; - } - } - - var U = this.U; - var V = this.rightSingularVectors; - - var VL = V.mmul(Ls); - var vrows = V.rows; - var urows = U.length; - var VLU = matrix_Matrix.zeros(vrows, urows); - - for (let i = 0; i < vrows; i++) { - for (let j = 0; j < urows; j++) { - let sum = 0; - for (let k = 0; k < scols; k++) { - sum += VL[i][k] * U[j][k]; - } - VLU[i][j] = sum; - } - } - - return VLU.mmul(Y); - } - - /** - * - * @param {Array} value - * @return {Matrix} - */ - solveForDiagonal(value) { - return this.solve(matrix_Matrix.diag(value)); - } - - /** - * Get the inverse of the matrix. We compute the inverse of a matrix using SVD when this matrix is singular or ill-conditioned. Example : - * var svd = SingularValueDecomposition(A); - * var inverseA = svd.inverse(); - * @return {Matrix} - The approximation of the inverse of the matrix - */ - inverse() { - var V = this.V; - var e = this.threshold; - var vrows = V.length; - var vcols = V[0].length; - var X = new matrix_Matrix(vrows, this.s.length); - - for (let i = 0; i < vrows; i++) { - for (let j = 0; j < vcols; j++) { - if (Math.abs(this.s[j]) > e) { - X[i][j] = V[i][j] / this.s[j]; - } else { - X[i][j] = 0; - } - } - } - - var U = this.U; - - var urows = U.length; - var ucols = U[0].length; - var Y = new matrix_Matrix(vrows, urows); - - for (let i = 0; i < vrows; i++) { - for (let j = 0; j < urows; j++) { - let sum = 0; - for (let k = 0; k < ucols; k++) { - sum += X[i][k] * U[j][k]; - } - Y[i][j] = sum; - } - } - - return Y; - } - - /** - * - * @return {number} - */ - get condition() { - return this.s[0] / this.s[Math.min(this.m, this.n) - 1]; - } - - /** - * - * @return {number} - */ - get norm2() { - return this.s[0]; - } - - /** - * - * @return {number} - */ - get rank() { - var tol = Math.max(this.m, this.n) * this.s[0] * Number.EPSILON; - var r = 0; - var s = this.s; - for (var i = 0, ii = s.length; i < ii; i++) { - if (s[i] > tol) { - r++; - } - } - return r; - } - - /** - * - * @return {Array} - */ - get diagonal() { - return this.s; - } - - /** - * - * @return {number} - */ - get threshold() { - return Number.EPSILON / 2 * Math.max(this.m, this.n) * this.s[0]; - } - - /** - * - * @return {Matrix} - */ - get leftSingularVectors() { - if (!matrix_Matrix.isMatrix(this.U)) { - this.U = new matrix_Matrix(this.U); - } - return this.U; - } - - /** - * - * @return {Matrix} - */ - get rightSingularVectors() { - if (!matrix_Matrix.isMatrix(this.V)) { - this.V = new matrix_Matrix(this.V); - } - return this.V; - } - - /** - * - * @return {Matrix} - */ - get diagonalMatrix() { - return matrix_Matrix.diag(this.s); - } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/util.js - - -/** - * @private - * Check that a row index is not out of bounds - * @param {Matrix} matrix - * @param {number} index - * @param {boolean} [outer] - */ -function checkRowIndex(matrix, index, outer) { - var max = outer ? matrix.rows : matrix.rows - 1; - if (index < 0 || index > max) { - throw new RangeError('Row index out of range'); - } -} - -/** - * @private - * Check that a column index is not out of bounds - * @param {Matrix} matrix - * @param {number} index - * @param {boolean} [outer] - */ -function checkColumnIndex(matrix, index, outer) { - var max = outer ? matrix.columns : matrix.columns - 1; - if (index < 0 || index > max) { - throw new RangeError('Column index out of range'); - } -} - -/** - * @private - * Check that the provided vector is an array with the right length - * @param {Matrix} matrix - * @param {Array|Matrix} vector - * @return {Array} - * @throws {RangeError} - */ -function checkRowVector(matrix, vector) { - if (vector.to1DArray) { - vector = vector.to1DArray(); - } - if (vector.length !== matrix.columns) { - throw new RangeError( - 'vector size must be the same as the number of columns' - ); - } - return vector; -} - -/** - * @private - * Check that the provided vector is an array with the right length - * @param {Matrix} matrix - * @param {Array|Matrix} vector - * @return {Array} - * @throws {RangeError} - */ -function checkColumnVector(matrix, vector) { - if (vector.to1DArray) { - vector = vector.to1DArray(); - } - if (vector.length !== matrix.rows) { - throw new RangeError('vector size must be the same as the number of rows'); - } - return vector; -} - -function checkIndices(matrix, rowIndices, columnIndices) { - return { - row: checkRowIndices(matrix, rowIndices), - column: checkColumnIndices(matrix, columnIndices) - }; -} - -function checkRowIndices(matrix, rowIndices) { - if (typeof rowIndices !== 'object') { - throw new TypeError('unexpected type for row indices'); - } - - var rowOut = rowIndices.some((r) => { - return r < 0 || r >= matrix.rows; - }); - - if (rowOut) { - throw new RangeError('row indices are out of range'); - } - - if (!Array.isArray(rowIndices)) rowIndices = Array.from(rowIndices); - - return rowIndices; -} - -function checkColumnIndices(matrix, columnIndices) { - if (typeof columnIndices !== 'object') { - throw new TypeError('unexpected type for column indices'); - } - - var columnOut = columnIndices.some((c) => { - return c < 0 || c >= matrix.columns; - }); - - if (columnOut) { - throw new RangeError('column indices are out of range'); - } - if (!Array.isArray(columnIndices)) columnIndices = Array.from(columnIndices); - - return columnIndices; -} - -function checkRange(matrix, startRow, endRow, startColumn, endColumn) { - if (arguments.length !== 5) { - throw new RangeError('expected 4 arguments'); - } - checkNumber('startRow', startRow); - checkNumber('endRow', endRow); - checkNumber('startColumn', startColumn); - checkNumber('endColumn', endColumn); - if ( - startRow > endRow || - startColumn > endColumn || - startRow < 0 || - startRow >= matrix.rows || - endRow < 0 || - endRow >= matrix.rows || - startColumn < 0 || - startColumn >= matrix.columns || - endColumn < 0 || - endColumn >= matrix.columns - ) { - throw new RangeError('Submatrix indices are out of range'); - } -} - -function getRange(from, to) { - var arr = new Array(to - from + 1); - for (var i = 0; i < arr.length; i++) { - arr[i] = from + i; - } - return arr; -} - -function sumByRow(matrix) { - var sum = matrix_Matrix.zeros(matrix.rows, 1); - for (var i = 0; i < matrix.rows; ++i) { - for (var j = 0; j < matrix.columns; ++j) { - sum.set(i, 0, sum.get(i, 0) + matrix.get(i, j)); - } - } - return sum; -} - -function sumByColumn(matrix) { - var sum = matrix_Matrix.zeros(1, matrix.columns); - for (var i = 0; i < matrix.rows; ++i) { - for (var j = 0; j < matrix.columns; ++j) { - sum.set(0, j, sum.get(0, j) + matrix.get(i, j)); - } - } - return sum; -} - -function sumAll(matrix) { - var v = 0; - for (var i = 0; i < matrix.rows; i++) { - for (var j = 0; j < matrix.columns; j++) { - v += matrix.get(i, j); - } - } - return v; -} - -function checkNumber(name, value) { - if (typeof value !== 'number') { - throw new TypeError(`${name} must be a number`); - } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/base.js - - - -class base_BaseView extends AbstractMatrix() { - constructor(matrix, rows, columns) { - super(); - this.matrix = matrix; - this.rows = rows; - this.columns = columns; - } - - static get [Symbol.species]() { - return matrix_Matrix; - } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/transpose.js - - -class transpose_MatrixTransposeView extends base_BaseView { - constructor(matrix) { - super(matrix, matrix.columns, matrix.rows); - } - - set(rowIndex, columnIndex, value) { - this.matrix.set(columnIndex, rowIndex, value); - return this; - } - - get(rowIndex, columnIndex) { - return this.matrix.get(columnIndex, rowIndex); - } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/row.js - - -class row_MatrixRowView extends base_BaseView { - constructor(matrix, row) { - super(matrix, 1, matrix.columns); - this.row = row; - } - - set(rowIndex, columnIndex, value) { - this.matrix.set(this.row, columnIndex, value); - return this; - } - - get(rowIndex, columnIndex) { - return this.matrix.get(this.row, columnIndex); - } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/sub.js - - - - -class sub_MatrixSubView extends base_BaseView { - constructor(matrix, startRow, endRow, startColumn, endColumn) { - checkRange(matrix, startRow, endRow, startColumn, endColumn); - super(matrix, endRow - startRow + 1, endColumn - startColumn + 1); - this.startRow = startRow; - this.startColumn = startColumn; - } - - set(rowIndex, columnIndex, value) { - this.matrix.set( - this.startRow + rowIndex, - this.startColumn + columnIndex, - value - ); - return this; - } - - get(rowIndex, columnIndex) { - return this.matrix.get( - this.startRow + rowIndex, - this.startColumn + columnIndex - ); - } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/selection.js - - - - -class selection_MatrixSelectionView extends base_BaseView { - constructor(matrix, rowIndices, columnIndices) { - var indices = checkIndices(matrix, rowIndices, columnIndices); - super(matrix, indices.row.length, indices.column.length); - this.rowIndices = indices.row; - this.columnIndices = indices.column; - } - - set(rowIndex, columnIndex, value) { - this.matrix.set( - this.rowIndices[rowIndex], - this.columnIndices[columnIndex], - value - ); - return this; - } - - get(rowIndex, columnIndex) { - return this.matrix.get( - this.rowIndices[rowIndex], - this.columnIndices[columnIndex] - ); - } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/rowSelection.js - - - - -class rowSelection_MatrixRowSelectionView extends base_BaseView { - constructor(matrix, rowIndices) { - rowIndices = checkRowIndices(matrix, rowIndices); - super(matrix, rowIndices.length, matrix.columns); - this.rowIndices = rowIndices; - } - - set(rowIndex, columnIndex, value) { - this.matrix.set(this.rowIndices[rowIndex], columnIndex, value); - return this; - } - - get(rowIndex, columnIndex) { - return this.matrix.get(this.rowIndices[rowIndex], columnIndex); - } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/columnSelection.js - - - - -class columnSelection_MatrixColumnSelectionView extends base_BaseView { - constructor(matrix, columnIndices) { - columnIndices = checkColumnIndices(matrix, columnIndices); - super(matrix, matrix.rows, columnIndices.length); - this.columnIndices = columnIndices; - } - - set(rowIndex, columnIndex, value) { - this.matrix.set(rowIndex, this.columnIndices[columnIndex], value); - return this; - } - - get(rowIndex, columnIndex) { - return this.matrix.get(rowIndex, this.columnIndices[columnIndex]); - } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/column.js - - -class column_MatrixColumnView extends base_BaseView { - constructor(matrix, column) { - super(matrix, matrix.rows, 1); - this.column = column; - } - - set(rowIndex, columnIndex, value) { - this.matrix.set(rowIndex, this.column, value); - return this; - } - - get(rowIndex) { - return this.matrix.get(rowIndex, this.column); - } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/flipRow.js - - -class flipRow_MatrixFlipRowView extends base_BaseView { - constructor(matrix) { - super(matrix, matrix.rows, matrix.columns); - } - - set(rowIndex, columnIndex, value) { - this.matrix.set(this.rows - rowIndex - 1, columnIndex, value); - return this; - } - - get(rowIndex, columnIndex) { - return this.matrix.get(this.rows - rowIndex - 1, columnIndex); - } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/flipColumn.js - - -class flipColumn_MatrixFlipColumnView extends base_BaseView { - constructor(matrix) { - super(matrix, matrix.rows, matrix.columns); - } - - set(rowIndex, columnIndex, value) { - this.matrix.set(rowIndex, this.columns - columnIndex - 1, value); - return this; - } - - get(rowIndex, columnIndex) { - return this.matrix.get(rowIndex, this.columns - columnIndex - 1); - } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/abstractMatrix.js - - - - - - - - - - - - - - - -function AbstractMatrix(superCtor) { - if (superCtor === undefined) superCtor = Object; - - /** - * Real matrix - * @class Matrix - * @param {number|Array|Matrix} nRows - Number of rows of the new matrix, - * 2D array containing the data or Matrix instance to clone - * @param {number} [nColumns] - Number of columns of the new matrix - */ - class Matrix extends superCtor { - static get [Symbol.species]() { - return this; - } - - /** - * Constructs a Matrix with the chosen dimensions from a 1D array - * @param {number} newRows - Number of rows - * @param {number} newColumns - Number of columns - * @param {Array} newData - A 1D array containing data for the matrix - * @return {Matrix} - The new matrix - */ - static from1DArray(newRows, newColumns, newData) { - var length = newRows * newColumns; - if (length !== newData.length) { - throw new RangeError('Data length does not match given dimensions'); - } - var newMatrix = new this(newRows, newColumns); - for (var row = 0; row < newRows; row++) { - for (var column = 0; column < newColumns; column++) { - newMatrix.set(row, column, newData[row * newColumns + column]); - } - } - return newMatrix; - } - - /** - * Creates a row vector, a matrix with only one row. - * @param {Array} newData - A 1D array containing data for the vector - * @return {Matrix} - The new matrix - */ - static rowVector(newData) { - var vector = new this(1, newData.length); - for (var i = 0; i < newData.length; i++) { - vector.set(0, i, newData[i]); - } - return vector; - } - - /** - * Creates a column vector, a matrix with only one column. - * @param {Array} newData - A 1D array containing data for the vector - * @return {Matrix} - The new matrix - */ - static columnVector(newData) { - var vector = new this(newData.length, 1); - for (var i = 0; i < newData.length; i++) { - vector.set(i, 0, newData[i]); - } - return vector; - } - - /** - * Creates an empty matrix with the given dimensions. Values will be undefined. Same as using new Matrix(rows, columns). - * @param {number} rows - Number of rows - * @param {number} columns - Number of columns - * @return {Matrix} - The new matrix - */ - static empty(rows, columns) { - return new this(rows, columns); - } - - /** - * Creates a matrix with the given dimensions. Values will be set to zero. - * @param {number} rows - Number of rows - * @param {number} columns - Number of columns - * @return {Matrix} - The new matrix - */ - static zeros(rows, columns) { - return this.empty(rows, columns).fill(0); - } - - /** - * Creates a matrix with the given dimensions. Values will be set to one. - * @param {number} rows - Number of rows - * @param {number} columns - Number of columns - * @return {Matrix} - The new matrix - */ - static ones(rows, columns) { - return this.empty(rows, columns).fill(1); - } - - /** - * Creates a matrix with the given dimensions. Values will be randomly set. - * @param {number} rows - Number of rows - * @param {number} columns - Number of columns - * @param {function} [rng=Math.random] - Random number generator - * @return {Matrix} The new matrix - */ - static rand(rows, columns, rng) { - if (rng === undefined) rng = Math.random; - var matrix = this.empty(rows, columns); - for (var i = 0; i < rows; i++) { - for (var j = 0; j < columns; j++) { - matrix.set(i, j, rng()); - } - } - return matrix; - } - - /** - * Creates a matrix with the given dimensions. Values will be random integers. - * @param {number} rows - Number of rows - * @param {number} columns - Number of columns - * @param {number} [maxValue=1000] - Maximum value - * @param {function} [rng=Math.random] - Random number generator - * @return {Matrix} The new matrix - */ - static randInt(rows, columns, maxValue, rng) { - if (maxValue === undefined) maxValue = 1000; - if (rng === undefined) rng = Math.random; - var matrix = this.empty(rows, columns); - for (var i = 0; i < rows; i++) { - for (var j = 0; j < columns; j++) { - var value = Math.floor(rng() * maxValue); - matrix.set(i, j, value); - } - } - return matrix; - } - - /** - * Creates an identity matrix with the given dimension. Values of the diagonal will be 1 and others will be 0. - * @param {number} rows - Number of rows - * @param {number} [columns=rows] - Number of columns - * @param {number} [value=1] - Value to fill the diagonal with - * @return {Matrix} - The new identity matrix - */ - static eye(rows, columns, value) { - if (columns === undefined) columns = rows; - if (value === undefined) value = 1; - var min = Math.min(rows, columns); - var matrix = this.zeros(rows, columns); - for (var i = 0; i < min; i++) { - matrix.set(i, i, value); - } - return matrix; - } - - /** - * Creates a diagonal matrix based on the given array. - * @param {Array} data - Array containing the data for the diagonal - * @param {number} [rows] - Number of rows (Default: data.length) - * @param {number} [columns] - Number of columns (Default: rows) - * @return {Matrix} - The new diagonal matrix - */ - static diag(data, rows, columns) { - var l = data.length; - if (rows === undefined) rows = l; - if (columns === undefined) columns = rows; - var min = Math.min(l, rows, columns); - var matrix = this.zeros(rows, columns); - for (var i = 0; i < min; i++) { - matrix.set(i, i, data[i]); - } - return matrix; - } - - /** - * Returns a matrix whose elements are the minimum between matrix1 and matrix2 - * @param {Matrix} matrix1 - * @param {Matrix} matrix2 - * @return {Matrix} - */ - static min(matrix1, matrix2) { - matrix1 = this.checkMatrix(matrix1); - matrix2 = this.checkMatrix(matrix2); - var rows = matrix1.rows; - var columns = matrix1.columns; - var result = new this(rows, columns); - for (var i = 0; i < rows; i++) { - for (var j = 0; j < columns; j++) { - result.set(i, j, Math.min(matrix1.get(i, j), matrix2.get(i, j))); - } - } - return result; - } - - /** - * Returns a matrix whose elements are the maximum between matrix1 and matrix2 - * @param {Matrix} matrix1 - * @param {Matrix} matrix2 - * @return {Matrix} - */ - static max(matrix1, matrix2) { - matrix1 = this.checkMatrix(matrix1); - matrix2 = this.checkMatrix(matrix2); - var rows = matrix1.rows; - var columns = matrix1.columns; - var result = new this(rows, columns); - for (var i = 0; i < rows; i++) { - for (var j = 0; j < columns; j++) { - result.set(i, j, Math.max(matrix1.get(i, j), matrix2.get(i, j))); - } - } - return result; - } - - /** - * Check that the provided value is a Matrix and tries to instantiate one if not - * @param {*} value - The value to check - * @return {Matrix} - */ - static checkMatrix(value) { - return Matrix.isMatrix(value) ? value : new this(value); - } - - /** - * Returns true if the argument is a Matrix, false otherwise - * @param {*} value - The value to check - * @return {boolean} - */ - static isMatrix(value) { - return (value != null) && (value.klass === 'Matrix'); - } - - /** - * @prop {number} size - The number of elements in the matrix. - */ - get size() { - return this.rows * this.columns; - } - - /** - * Applies a callback for each element of the matrix. The function is called in the matrix (this) context. - * @param {function} callback - Function that will be called with two parameters : i (row) and j (column) - * @return {Matrix} this - */ - apply(callback) { - if (typeof callback !== 'function') { - throw new TypeError('callback must be a function'); - } - var ii = this.rows; - var jj = this.columns; - for (var i = 0; i < ii; i++) { - for (var j = 0; j < jj; j++) { - callback.call(this, i, j); - } - } - return this; - } - - /** - * Returns a new 1D array filled row by row with the matrix values - * @return {Array} - */ - to1DArray() { - var array = new Array(this.size); - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - array[i * this.columns + j] = this.get(i, j); - } - } - return array; - } - - /** - * Returns a 2D array containing a copy of the data - * @return {Array} - */ - to2DArray() { - var copy = new Array(this.rows); - for (var i = 0; i < this.rows; i++) { - copy[i] = new Array(this.columns); - for (var j = 0; j < this.columns; j++) { - copy[i][j] = this.get(i, j); - } - } - return copy; - } - - /** - * @return {boolean} true if the matrix has one row - */ - isRowVector() { - return this.rows === 1; - } - - /** - * @return {boolean} true if the matrix has one column - */ - isColumnVector() { - return this.columns === 1; - } - - /** - * @return {boolean} true if the matrix has one row or one column - */ - isVector() { - return (this.rows === 1) || (this.columns === 1); - } - - /** - * @return {boolean} true if the matrix has the same number of rows and columns - */ - isSquare() { - return this.rows === this.columns; - } - - /** - * @return {boolean} true if the matrix is square and has the same values on both sides of the diagonal - */ - isSymmetric() { - if (this.isSquare()) { - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j <= i; j++) { - if (this.get(i, j) !== this.get(j, i)) { - return false; - } - } - } - return true; - } - return false; - } - - /** - * @return true if the matrix is in echelon form - */ - isEchelonForm() { - let i = 0; - let j = 0; - let previousColumn = -1; - let isEchelonForm = true; - let checked = false; - while ((i < this.rows) && (isEchelonForm)) { - j = 0; - checked = false; - while ((j < this.columns) && (checked === false)) { - if (this.get(i, j) === 0) { - j++; - } else if ((this.get(i, j) === 1) && (j > previousColumn)) { - checked = true; - previousColumn = j; - } else { - isEchelonForm = false; - checked = true; - } - } - i++; - } - return isEchelonForm; - } - - /** - * @return true if the matrix is in reduced echelon form - */ - isReducedEchelonForm() { - let i = 0; - let j = 0; - let previousColumn = -1; - let isReducedEchelonForm = true; - let checked = false; - while ((i < this.rows) && (isReducedEchelonForm)) { - j = 0; - checked = false; - while ((j < this.columns) && (checked === false)) { - if (this.get(i, j) === 0) { - j++; - } else if ((this.get(i, j) === 1) && (j > previousColumn)) { - checked = true; - previousColumn = j; - } else { - isReducedEchelonForm = false; - checked = true; - } - } - for (let k = j + 1; k < this.rows; k++) { - if (this.get(i, k) !== 0) { - isReducedEchelonForm = false; - } - } - i++; - } - return isReducedEchelonForm; - } - - /** - * Sets a given element of the matrix. mat.set(3,4,1) is equivalent to mat[3][4]=1 - * @abstract - * @param {number} rowIndex - Index of the row - * @param {number} columnIndex - Index of the column - * @param {number} value - The new value for the element - * @return {Matrix} this - */ - set(rowIndex, columnIndex, value) { // eslint-disable-line no-unused-vars - throw new Error('set method is unimplemented'); - } - - /** - * Returns the given element of the matrix. mat.get(3,4) is equivalent to matrix[3][4] - * @abstract - * @param {number} rowIndex - Index of the row - * @param {number} columnIndex - Index of the column - * @return {number} - */ - get(rowIndex, columnIndex) { // eslint-disable-line no-unused-vars - throw new Error('get method is unimplemented'); - } - - /** - * Creates a new matrix that is a repetition of the current matrix. New matrix has rowRep times the number of - * rows of the matrix, and colRep times the number of columns of the matrix - * @param {number} rowRep - Number of times the rows should be repeated - * @param {number} colRep - Number of times the columns should be re - * @return {Matrix} - * @example - * var matrix = new Matrix([[1,2]]); - * matrix.repeat(2); // [[1,2],[1,2]] - */ - repeat(rowRep, colRep) { - rowRep = rowRep || 1; - colRep = colRep || 1; - var matrix = new this.constructor[Symbol.species](this.rows * rowRep, this.columns * colRep); - for (var i = 0; i < rowRep; i++) { - for (var j = 0; j < colRep; j++) { - matrix.setSubMatrix(this, this.rows * i, this.columns * j); - } - } - return matrix; - } - - /** - * Fills the matrix with a given value. All elements will be set to this value. - * @param {number} value - New value - * @return {Matrix} this - */ - fill(value) { - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, value); - } - } - return this; - } - - /** - * Negates the matrix. All elements will be multiplied by (-1) - * @return {Matrix} this - */ - neg() { - return this.mulS(-1); - } - - /** - * Returns a new array from the given row index - * @param {number} index - Row index - * @return {Array} - */ - getRow(index) { - checkRowIndex(this, index); - var row = new Array(this.columns); - for (var i = 0; i < this.columns; i++) { - row[i] = this.get(index, i); - } - return row; - } - - /** - * Returns a new row vector from the given row index - * @param {number} index - Row index - * @return {Matrix} - */ - getRowVector(index) { - return this.constructor.rowVector(this.getRow(index)); - } - - /** - * Sets a row at the given index - * @param {number} index - Row index - * @param {Array|Matrix} array - Array or vector - * @return {Matrix} this - */ - setRow(index, array) { - checkRowIndex(this, index); - array = checkRowVector(this, array); - for (var i = 0; i < this.columns; i++) { - this.set(index, i, array[i]); - } - return this; - } - - /** - * Swaps two rows - * @param {number} row1 - First row index - * @param {number} row2 - Second row index - * @return {Matrix} this - */ - swapRows(row1, row2) { - checkRowIndex(this, row1); - checkRowIndex(this, row2); - for (var i = 0; i < this.columns; i++) { - var temp = this.get(row1, i); - this.set(row1, i, this.get(row2, i)); - this.set(row2, i, temp); - } - return this; - } - - /** - * Returns a new array from the given column index - * @param {number} index - Column index - * @return {Array} - */ - getColumn(index) { - checkColumnIndex(this, index); - var column = new Array(this.rows); - for (var i = 0; i < this.rows; i++) { - column[i] = this.get(i, index); - } - return column; - } - - /** - * Returns a new column vector from the given column index - * @param {number} index - Column index - * @return {Matrix} - */ - getColumnVector(index) { - return this.constructor.columnVector(this.getColumn(index)); - } - - /** - * Sets a column at the given index - * @param {number} index - Column index - * @param {Array|Matrix} array - Array or vector - * @return {Matrix} this - */ - setColumn(index, array) { - checkColumnIndex(this, index); - array = checkColumnVector(this, array); - for (var i = 0; i < this.rows; i++) { - this.set(i, index, array[i]); - } - return this; - } - - /** - * Swaps two columns - * @param {number} column1 - First column index - * @param {number} column2 - Second column index - * @return {Matrix} this - */ - swapColumns(column1, column2) { - checkColumnIndex(this, column1); - checkColumnIndex(this, column2); - for (var i = 0; i < this.rows; i++) { - var temp = this.get(i, column1); - this.set(i, column1, this.get(i, column2)); - this.set(i, column2, temp); - } - return this; - } - - /** - * Adds the values of a vector to each row - * @param {Array|Matrix} vector - Array or vector - * @return {Matrix} this - */ - addRowVector(vector) { - vector = checkRowVector(this, vector); - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, this.get(i, j) + vector[j]); - } - } - return this; - } - - /** - * Subtracts the values of a vector from each row - * @param {Array|Matrix} vector - Array or vector - * @return {Matrix} this - */ - subRowVector(vector) { - vector = checkRowVector(this, vector); - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, this.get(i, j) - vector[j]); - } - } - return this; - } - - /** - * Multiplies the values of a vector with each row - * @param {Array|Matrix} vector - Array or vector - * @return {Matrix} this - */ - mulRowVector(vector) { - vector = checkRowVector(this, vector); - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, this.get(i, j) * vector[j]); - } - } - return this; - } - - /** - * Divides the values of each row by those of a vector - * @param {Array|Matrix} vector - Array or vector - * @return {Matrix} this - */ - divRowVector(vector) { - vector = checkRowVector(this, vector); - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, this.get(i, j) / vector[j]); - } - } - return this; - } - - /** - * Adds the values of a vector to each column - * @param {Array|Matrix} vector - Array or vector - * @return {Matrix} this - */ - addColumnVector(vector) { - vector = checkColumnVector(this, vector); - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, this.get(i, j) + vector[i]); - } - } - return this; - } - - /** - * Subtracts the values of a vector from each column - * @param {Array|Matrix} vector - Array or vector - * @return {Matrix} this - */ - subColumnVector(vector) { - vector = checkColumnVector(this, vector); - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, this.get(i, j) - vector[i]); - } - } - return this; - } - - /** - * Multiplies the values of a vector with each column - * @param {Array|Matrix} vector - Array or vector - * @return {Matrix} this - */ - mulColumnVector(vector) { - vector = checkColumnVector(this, vector); - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, this.get(i, j) * vector[i]); - } - } - return this; - } - - /** - * Divides the values of each column by those of a vector - * @param {Array|Matrix} vector - Array or vector - * @return {Matrix} this - */ - divColumnVector(vector) { - vector = checkColumnVector(this, vector); - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, this.get(i, j) / vector[i]); - } - } - return this; - } - - /** - * Multiplies the values of a row with a scalar - * @param {number} index - Row index - * @param {number} value - * @return {Matrix} this - */ - mulRow(index, value) { - checkRowIndex(this, index); - for (var i = 0; i < this.columns; i++) { - this.set(index, i, this.get(index, i) * value); - } - return this; - } - - /** - * Multiplies the values of a column with a scalar - * @param {number} index - Column index - * @param {number} value - * @return {Matrix} this - */ - mulColumn(index, value) { - checkColumnIndex(this, index); - for (var i = 0; i < this.rows; i++) { - this.set(i, index, this.get(i, index) * value); - } - return this; - } - - /** - * Returns the maximum value of the matrix - * @return {number} - */ - max() { - var v = this.get(0, 0); - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - if (this.get(i, j) > v) { - v = this.get(i, j); - } - } - } - return v; - } - - /** - * Returns the index of the maximum value - * @return {Array} - */ - maxIndex() { - var v = this.get(0, 0); - var idx = [0, 0]; - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - if (this.get(i, j) > v) { - v = this.get(i, j); - idx[0] = i; - idx[1] = j; - } - } - } - return idx; - } - - /** - * Returns the minimum value of the matrix - * @return {number} - */ - min() { - var v = this.get(0, 0); - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - if (this.get(i, j) < v) { - v = this.get(i, j); - } - } - } - return v; - } - - /** - * Returns the index of the minimum value - * @return {Array} - */ - minIndex() { - var v = this.get(0, 0); - var idx = [0, 0]; - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - if (this.get(i, j) < v) { - v = this.get(i, j); - idx[0] = i; - idx[1] = j; - } - } - } - return idx; - } - - /** - * Returns the maximum value of one row - * @param {number} row - Row index - * @return {number} - */ - maxRow(row) { - checkRowIndex(this, row); - var v = this.get(row, 0); - for (var i = 1; i < this.columns; i++) { - if (this.get(row, i) > v) { - v = this.get(row, i); - } - } - return v; - } - - /** - * Returns the index of the maximum value of one row - * @param {number} row - Row index - * @return {Array} - */ - maxRowIndex(row) { - checkRowIndex(this, row); - var v = this.get(row, 0); - var idx = [row, 0]; - for (var i = 1; i < this.columns; i++) { - if (this.get(row, i) > v) { - v = this.get(row, i); - idx[1] = i; - } - } - return idx; - } - - /** - * Returns the minimum value of one row - * @param {number} row - Row index - * @return {number} - */ - minRow(row) { - checkRowIndex(this, row); - var v = this.get(row, 0); - for (var i = 1; i < this.columns; i++) { - if (this.get(row, i) < v) { - v = this.get(row, i); - } - } - return v; - } - - /** - * Returns the index of the maximum value of one row - * @param {number} row - Row index - * @return {Array} - */ - minRowIndex(row) { - checkRowIndex(this, row); - var v = this.get(row, 0); - var idx = [row, 0]; - for (var i = 1; i < this.columns; i++) { - if (this.get(row, i) < v) { - v = this.get(row, i); - idx[1] = i; - } - } - return idx; - } - - /** - * Returns the maximum value of one column - * @param {number} column - Column index - * @return {number} - */ - maxColumn(column) { - checkColumnIndex(this, column); - var v = this.get(0, column); - for (var i = 1; i < this.rows; i++) { - if (this.get(i, column) > v) { - v = this.get(i, column); - } - } - return v; - } - - /** - * Returns the index of the maximum value of one column - * @param {number} column - Column index - * @return {Array} - */ - maxColumnIndex(column) { - checkColumnIndex(this, column); - var v = this.get(0, column); - var idx = [0, column]; - for (var i = 1; i < this.rows; i++) { - if (this.get(i, column) > v) { - v = this.get(i, column); - idx[0] = i; - } - } - return idx; - } - - /** - * Returns the minimum value of one column - * @param {number} column - Column index - * @return {number} - */ - minColumn(column) { - checkColumnIndex(this, column); - var v = this.get(0, column); - for (var i = 1; i < this.rows; i++) { - if (this.get(i, column) < v) { - v = this.get(i, column); - } - } - return v; - } - - /** - * Returns the index of the minimum value of one column - * @param {number} column - Column index - * @return {Array} - */ - minColumnIndex(column) { - checkColumnIndex(this, column); - var v = this.get(0, column); - var idx = [0, column]; - for (var i = 1; i < this.rows; i++) { - if (this.get(i, column) < v) { - v = this.get(i, column); - idx[0] = i; - } - } - return idx; - } - - /** - * Returns an array containing the diagonal values of the matrix - * @return {Array} - */ - diag() { - var min = Math.min(this.rows, this.columns); - var diag = new Array(min); - for (var i = 0; i < min; i++) { - diag[i] = this.get(i, i); - } - return diag; - } - - /** - * Returns the sum by the argument given, if no argument given, - * it returns the sum of all elements of the matrix. - * @param {string} by - sum by 'row' or 'column'. - * @return {Matrix|number} - */ - sum(by) { - switch (by) { - case 'row': - return sumByRow(this); - case 'column': - return sumByColumn(this); - default: - return sumAll(this); - } - } - - /** - * Returns the mean of all elements of the matrix - * @return {number} - */ - mean() { - return this.sum() / this.size; - } - - /** - * Returns the product of all elements of the matrix - * @return {number} - */ - prod() { - var prod = 1; - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - prod *= this.get(i, j); - } - } - return prod; - } - - /** - * Returns the norm of a matrix. - * @param {string} type - "frobenius" (default) or "max" return resp. the Frobenius norm and the max norm. - * @return {number} - */ - norm(type = 'frobenius') { - var result = 0; - if (type === 'max') { - return this.max(); - } else if (type === 'frobenius') { - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - result = result + this.get(i, j) * this.get(i, j); - } - } - return Math.sqrt(result); - } else { - throw new RangeError(`unknown norm type: ${type}`); - } - } - - /** - * Computes the cumulative sum of the matrix elements (in place, row by row) - * @return {Matrix} this - */ - cumulativeSum() { - var sum = 0; - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - sum += this.get(i, j); - this.set(i, j, sum); - } - } - return this; - } - - /** - * Computes the dot (scalar) product between the matrix and another - * @param {Matrix} vector2 vector - * @return {number} - */ - dot(vector2) { - if (Matrix.isMatrix(vector2)) vector2 = vector2.to1DArray(); - var vector1 = this.to1DArray(); - if (vector1.length !== vector2.length) { - throw new RangeError('vectors do not have the same size'); - } - var dot = 0; - for (var i = 0; i < vector1.length; i++) { - dot += vector1[i] * vector2[i]; - } - return dot; - } - - /** - * Returns the matrix product between this and other - * @param {Matrix} other - * @return {Matrix} - */ - mmul(other) { - other = this.constructor.checkMatrix(other); - if (this.columns !== other.rows) { - // eslint-disable-next-line no-console - console.warn('Number of columns of left matrix are not equal to number of rows of right matrix.'); - } - - var m = this.rows; - var n = this.columns; - var p = other.columns; - - var result = new this.constructor[Symbol.species](m, p); - - var Bcolj = new Array(n); - for (var j = 0; j < p; j++) { - for (var k = 0; k < n; k++) { - Bcolj[k] = other.get(k, j); - } - - for (var i = 0; i < m; i++) { - var s = 0; - for (k = 0; k < n; k++) { - s += this.get(i, k) * Bcolj[k]; - } - - result.set(i, j, s); - } - } - return result; - } - - strassen2x2(other) { - var result = new this.constructor[Symbol.species](2, 2); - const a11 = this.get(0, 0); - const b11 = other.get(0, 0); - const a12 = this.get(0, 1); - const b12 = other.get(0, 1); - const a21 = this.get(1, 0); - const b21 = other.get(1, 0); - const a22 = this.get(1, 1); - const b22 = other.get(1, 1); - - // Compute intermediate values. - const m1 = (a11 + a22) * (b11 + b22); - const m2 = (a21 + a22) * b11; - const m3 = a11 * (b12 - b22); - const m4 = a22 * (b21 - b11); - const m5 = (a11 + a12) * b22; - const m6 = (a21 - a11) * (b11 + b12); - const m7 = (a12 - a22) * (b21 + b22); - - // Combine intermediate values into the output. - const c00 = m1 + m4 - m5 + m7; - const c01 = m3 + m5; - const c10 = m2 + m4; - const c11 = m1 - m2 + m3 + m6; - - result.set(0, 0, c00); - result.set(0, 1, c01); - result.set(1, 0, c10); - result.set(1, 1, c11); - return result; - } - - strassen3x3(other) { - var result = new this.constructor[Symbol.species](3, 3); - - const a00 = this.get(0, 0); - const a01 = this.get(0, 1); - const a02 = this.get(0, 2); - const a10 = this.get(1, 0); - const a11 = this.get(1, 1); - const a12 = this.get(1, 2); - const a20 = this.get(2, 0); - const a21 = this.get(2, 1); - const a22 = this.get(2, 2); - - const b00 = other.get(0, 0); - const b01 = other.get(0, 1); - const b02 = other.get(0, 2); - const b10 = other.get(1, 0); - const b11 = other.get(1, 1); - const b12 = other.get(1, 2); - const b20 = other.get(2, 0); - const b21 = other.get(2, 1); - const b22 = other.get(2, 2); - - const m1 = (a00 + a01 + a02 - a10 - a11 - a21 - a22) * b11; - const m2 = (a00 - a10) * (-b01 + b11); - const m3 = a11 * (-b00 + b01 + b10 - b11 - b12 - b20 + b22); - const m4 = (-a00 + a10 + a11) * (b00 - b01 + b11); - const m5 = (a10 + a11) * (-b00 + b01); - const m6 = a00 * b00; - const m7 = (-a00 + a20 + a21) * (b00 - b02 + b12); - const m8 = (-a00 + a20) * (b02 - b12); - const m9 = (a20 + a21) * (-b00 + b02); - const m10 = (a00 + a01 + a02 - a11 - a12 - a20 - a21) * b12; - const m11 = a21 * (-b00 + b02 + b10 - b11 - b12 - b20 + b21); - const m12 = (-a02 + a21 + a22) * (b11 + b20 - b21); - const m13 = (a02 - a22) * (b11 - b21); - const m14 = a02 * b20; - const m15 = (a21 + a22) * (-b20 + b21); - const m16 = (-a02 + a11 + a12) * (b12 + b20 - b22); - const m17 = (a02 - a12) * (b12 - b22); - const m18 = (a11 + a12) * (-b20 + b22); - const m19 = a01 * b10; - const m20 = a12 * b21; - const m21 = a10 * b02; - const m22 = a20 * b01; - const m23 = a22 * b22; - - const c00 = m6 + m14 + m19; - const c01 = m1 + m4 + m5 + m6 + m12 + m14 + m15; - const c02 = m6 + m7 + m9 + m10 + m14 + m16 + m18; - const c10 = m2 + m3 + m4 + m6 + m14 + m16 + m17; - const c11 = m2 + m4 + m5 + m6 + m20; - const c12 = m14 + m16 + m17 + m18 + m21; - const c20 = m6 + m7 + m8 + m11 + m12 + m13 + m14; - const c21 = m12 + m13 + m14 + m15 + m22; - const c22 = m6 + m7 + m8 + m9 + m23; - - result.set(0, 0, c00); - result.set(0, 1, c01); - result.set(0, 2, c02); - result.set(1, 0, c10); - result.set(1, 1, c11); - result.set(1, 2, c12); - result.set(2, 0, c20); - result.set(2, 1, c21); - result.set(2, 2, c22); - return result; - } - - /** - * Returns the matrix product between x and y. More efficient than mmul(other) only when we multiply squared matrix and when the size of the matrix is > 1000. - * @param {Matrix} y - * @return {Matrix} - */ - mmulStrassen(y) { - var x = this.clone(); - var r1 = x.rows; - var c1 = x.columns; - var r2 = y.rows; - var c2 = y.columns; - if (c1 !== r2) { - // eslint-disable-next-line no-console - console.warn(`Multiplying ${r1} x ${c1} and ${r2} x ${c2} matrix: dimensions do not match.`); - } - - // Put a matrix into the top left of a matrix of zeros. - // `rows` and `cols` are the dimensions of the output matrix. - function embed(mat, rows, cols) { - var r = mat.rows; - var c = mat.columns; - if ((r === rows) && (c === cols)) { - return mat; - } else { - var resultat = Matrix.zeros(rows, cols); - resultat = resultat.setSubMatrix(mat, 0, 0); - return resultat; - } - } - - - // Make sure both matrices are the same size. - // This is exclusively for simplicity: - // this algorithm can be implemented with matrices of different sizes. - - var r = Math.max(r1, r2); - var c = Math.max(c1, c2); - x = embed(x, r, c); - y = embed(y, r, c); - - // Our recursive multiplication function. - function blockMult(a, b, rows, cols) { - // For small matrices, resort to naive multiplication. - if (rows <= 512 || cols <= 512) { - return a.mmul(b); // a is equivalent to this - } - - // Apply dynamic padding. - if ((rows % 2 === 1) && (cols % 2 === 1)) { - a = embed(a, rows + 1, cols + 1); - b = embed(b, rows + 1, cols + 1); - } else if (rows % 2 === 1) { - a = embed(a, rows + 1, cols); - b = embed(b, rows + 1, cols); - } else if (cols % 2 === 1) { - a = embed(a, rows, cols + 1); - b = embed(b, rows, cols + 1); - } - - var halfRows = parseInt(a.rows / 2, 10); - var halfCols = parseInt(a.columns / 2, 10); - // Subdivide input matrices. - var a11 = a.subMatrix(0, halfRows - 1, 0, halfCols - 1); - var b11 = b.subMatrix(0, halfRows - 1, 0, halfCols - 1); - - var a12 = a.subMatrix(0, halfRows - 1, halfCols, a.columns - 1); - var b12 = b.subMatrix(0, halfRows - 1, halfCols, b.columns - 1); - - var a21 = a.subMatrix(halfRows, a.rows - 1, 0, halfCols - 1); - var b21 = b.subMatrix(halfRows, b.rows - 1, 0, halfCols - 1); - - var a22 = a.subMatrix(halfRows, a.rows - 1, halfCols, a.columns - 1); - var b22 = b.subMatrix(halfRows, b.rows - 1, halfCols, b.columns - 1); - - // Compute intermediate values. - var m1 = blockMult(Matrix.add(a11, a22), Matrix.add(b11, b22), halfRows, halfCols); - var m2 = blockMult(Matrix.add(a21, a22), b11, halfRows, halfCols); - var m3 = blockMult(a11, Matrix.sub(b12, b22), halfRows, halfCols); - var m4 = blockMult(a22, Matrix.sub(b21, b11), halfRows, halfCols); - var m5 = blockMult(Matrix.add(a11, a12), b22, halfRows, halfCols); - var m6 = blockMult(Matrix.sub(a21, a11), Matrix.add(b11, b12), halfRows, halfCols); - var m7 = blockMult(Matrix.sub(a12, a22), Matrix.add(b21, b22), halfRows, halfCols); - - // Combine intermediate values into the output. - var c11 = Matrix.add(m1, m4); - c11.sub(m5); - c11.add(m7); - var c12 = Matrix.add(m3, m5); - var c21 = Matrix.add(m2, m4); - var c22 = Matrix.sub(m1, m2); - c22.add(m3); - c22.add(m6); - - // Crop output to the desired size (undo dynamic padding). - var resultat = Matrix.zeros(2 * c11.rows, 2 * c11.columns); - resultat = resultat.setSubMatrix(c11, 0, 0); - resultat = resultat.setSubMatrix(c12, c11.rows, 0); - resultat = resultat.setSubMatrix(c21, 0, c11.columns); - resultat = resultat.setSubMatrix(c22, c11.rows, c11.columns); - return resultat.subMatrix(0, rows - 1, 0, cols - 1); - } - return blockMult(x, y, r, c); - } - - /** - * Returns a row-by-row scaled matrix - * @param {number} [min=0] - Minimum scaled value - * @param {number} [max=1] - Maximum scaled value - * @return {Matrix} - The scaled matrix - */ - scaleRows(min, max) { - min = min === undefined ? 0 : min; - max = max === undefined ? 1 : max; - if (min >= max) { - throw new RangeError('min should be strictly smaller than max'); - } - var newMatrix = this.constructor.empty(this.rows, this.columns); - for (var i = 0; i < this.rows; i++) { - var scaled = ml_array_rescale_lib_es6(this.getRow(i), { min, max }); - newMatrix.setRow(i, scaled); - } - return newMatrix; - } - - /** - * Returns a new column-by-column scaled matrix - * @param {number} [min=0] - Minimum scaled value - * @param {number} [max=1] - Maximum scaled value - * @return {Matrix} - The new scaled matrix - * @example - * var matrix = new Matrix([[1,2],[-1,0]]); - * var scaledMatrix = matrix.scaleColumns(); // [[1,1],[0,0]] - */ - scaleColumns(min, max) { - min = min === undefined ? 0 : min; - max = max === undefined ? 1 : max; - if (min >= max) { - throw new RangeError('min should be strictly smaller than max'); - } - var newMatrix = this.constructor.empty(this.rows, this.columns); - for (var i = 0; i < this.columns; i++) { - var scaled = ml_array_rescale_lib_es6(this.getColumn(i), { - min: min, - max: max - }); - newMatrix.setColumn(i, scaled); - } - return newMatrix; - } - - - /** - * Returns the Kronecker product (also known as tensor product) between this and other - * See https://en.wikipedia.org/wiki/Kronecker_product - * @param {Matrix} other - * @return {Matrix} - */ - kroneckerProduct(other) { - other = this.constructor.checkMatrix(other); - - var m = this.rows; - var n = this.columns; - var p = other.rows; - var q = other.columns; - - var result = new this.constructor[Symbol.species](m * p, n * q); - for (var i = 0; i < m; i++) { - for (var j = 0; j < n; j++) { - for (var k = 0; k < p; k++) { - for (var l = 0; l < q; l++) { - result[p * i + k][q * j + l] = this.get(i, j) * other.get(k, l); - } - } - } - } - return result; - } - - /** - * Transposes the matrix and returns a new one containing the result - * @return {Matrix} - */ - transpose() { - var result = new this.constructor[Symbol.species](this.columns, this.rows); - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - result.set(j, i, this.get(i, j)); - } - } - return result; - } - - /** - * Sorts the rows (in place) - * @param {function} compareFunction - usual Array.prototype.sort comparison function - * @return {Matrix} this - */ - sortRows(compareFunction) { - if (compareFunction === undefined) compareFunction = compareNumbers; - for (var i = 0; i < this.rows; i++) { - this.setRow(i, this.getRow(i).sort(compareFunction)); - } - return this; - } - - /** - * Sorts the columns (in place) - * @param {function} compareFunction - usual Array.prototype.sort comparison function - * @return {Matrix} this - */ - sortColumns(compareFunction) { - if (compareFunction === undefined) compareFunction = compareNumbers; - for (var i = 0; i < this.columns; i++) { - this.setColumn(i, this.getColumn(i).sort(compareFunction)); - } - return this; - } - - /** - * Returns a subset of the matrix - * @param {number} startRow - First row index - * @param {number} endRow - Last row index - * @param {number} startColumn - First column index - * @param {number} endColumn - Last column index - * @return {Matrix} - */ - subMatrix(startRow, endRow, startColumn, endColumn) { - checkRange(this, startRow, endRow, startColumn, endColumn); - var newMatrix = new this.constructor[Symbol.species](endRow - startRow + 1, endColumn - startColumn + 1); - for (var i = startRow; i <= endRow; i++) { - for (var j = startColumn; j <= endColumn; j++) { - newMatrix[i - startRow][j - startColumn] = this.get(i, j); - } - } - return newMatrix; - } - - /** - * Returns a subset of the matrix based on an array of row indices - * @param {Array} indices - Array containing the row indices - * @param {number} [startColumn = 0] - First column index - * @param {number} [endColumn = this.columns-1] - Last column index - * @return {Matrix} - */ - subMatrixRow(indices, startColumn, endColumn) { - if (startColumn === undefined) startColumn = 0; - if (endColumn === undefined) endColumn = this.columns - 1; - if ((startColumn > endColumn) || (startColumn < 0) || (startColumn >= this.columns) || (endColumn < 0) || (endColumn >= this.columns)) { - throw new RangeError('Argument out of range'); - } - - var newMatrix = new this.constructor[Symbol.species](indices.length, endColumn - startColumn + 1); - for (var i = 0; i < indices.length; i++) { - for (var j = startColumn; j <= endColumn; j++) { - if (indices[i] < 0 || indices[i] >= this.rows) { - throw new RangeError(`Row index out of range: ${indices[i]}`); - } - newMatrix.set(i, j - startColumn, this.get(indices[i], j)); - } - } - return newMatrix; - } - - /** - * Returns a subset of the matrix based on an array of column indices - * @param {Array} indices - Array containing the column indices - * @param {number} [startRow = 0] - First row index - * @param {number} [endRow = this.rows-1] - Last row index - * @return {Matrix} - */ - subMatrixColumn(indices, startRow, endRow) { - if (startRow === undefined) startRow = 0; - if (endRow === undefined) endRow = this.rows - 1; - if ((startRow > endRow) || (startRow < 0) || (startRow >= this.rows) || (endRow < 0) || (endRow >= this.rows)) { - throw new RangeError('Argument out of range'); - } - - var newMatrix = new this.constructor[Symbol.species](endRow - startRow + 1, indices.length); - for (var i = 0; i < indices.length; i++) { - for (var j = startRow; j <= endRow; j++) { - if (indices[i] < 0 || indices[i] >= this.columns) { - throw new RangeError(`Column index out of range: ${indices[i]}`); - } - newMatrix.set(j - startRow, i, this.get(j, indices[i])); - } - } - return newMatrix; - } - - /** - * Set a part of the matrix to the given sub-matrix - * @param {Matrix|Array< Array >} matrix - The source matrix from which to extract values. - * @param {number} startRow - The index of the first row to set - * @param {number} startColumn - The index of the first column to set - * @return {Matrix} - */ - setSubMatrix(matrix, startRow, startColumn) { - matrix = this.constructor.checkMatrix(matrix); - var endRow = startRow + matrix.rows - 1; - var endColumn = startColumn + matrix.columns - 1; - checkRange(this, startRow, endRow, startColumn, endColumn); - for (var i = 0; i < matrix.rows; i++) { - for (var j = 0; j < matrix.columns; j++) { - this[startRow + i][startColumn + j] = matrix.get(i, j); - } - } - return this; - } - - /** - * Return a new matrix based on a selection of rows and columns - * @param {Array} rowIndices - The row indices to select. Order matters and an index can be more than once. - * @param {Array} columnIndices - The column indices to select. Order matters and an index can be use more than once. - * @return {Matrix} The new matrix - */ - selection(rowIndices, columnIndices) { - var indices = checkIndices(this, rowIndices, columnIndices); - var newMatrix = new this.constructor[Symbol.species](rowIndices.length, columnIndices.length); - for (var i = 0; i < indices.row.length; i++) { - var rowIndex = indices.row[i]; - for (var j = 0; j < indices.column.length; j++) { - var columnIndex = indices.column[j]; - newMatrix[i][j] = this.get(rowIndex, columnIndex); - } - } - return newMatrix; - } - - /** - * Returns the trace of the matrix (sum of the diagonal elements) - * @return {number} - */ - trace() { - var min = Math.min(this.rows, this.columns); - var trace = 0; - for (var i = 0; i < min; i++) { - trace += this.get(i, i); - } - return trace; - } - - /* - Matrix views - */ - - /** - * Returns a view of the transposition of the matrix - * @return {MatrixTransposeView} - */ - transposeView() { - return new transpose_MatrixTransposeView(this); - } - - /** - * Returns a view of the row vector with the given index - * @param {number} row - row index of the vector - * @return {MatrixRowView} - */ - rowView(row) { - checkRowIndex(this, row); - return new row_MatrixRowView(this, row); - } - - /** - * Returns a view of the column vector with the given index - * @param {number} column - column index of the vector - * @return {MatrixColumnView} - */ - columnView(column) { - checkColumnIndex(this, column); - return new column_MatrixColumnView(this, column); - } - - /** - * Returns a view of the matrix flipped in the row axis - * @return {MatrixFlipRowView} - */ - flipRowView() { - return new flipRow_MatrixFlipRowView(this); - } - - /** - * Returns a view of the matrix flipped in the column axis - * @return {MatrixFlipColumnView} - */ - flipColumnView() { - return new flipColumn_MatrixFlipColumnView(this); - } - - /** - * Returns a view of a submatrix giving the index boundaries - * @param {number} startRow - first row index of the submatrix - * @param {number} endRow - last row index of the submatrix - * @param {number} startColumn - first column index of the submatrix - * @param {number} endColumn - last column index of the submatrix - * @return {MatrixSubView} - */ - subMatrixView(startRow, endRow, startColumn, endColumn) { - return new sub_MatrixSubView(this, startRow, endRow, startColumn, endColumn); - } - - /** - * Returns a view of the cross of the row indices and the column indices - * @example - * // resulting vector is [[2], [2]] - * var matrix = new Matrix([[1,2,3], [4,5,6]]).selectionView([0, 0], [1]) - * @param {Array} rowIndices - * @param {Array} columnIndices - * @return {MatrixSelectionView} - */ - selectionView(rowIndices, columnIndices) { - return new selection_MatrixSelectionView(this, rowIndices, columnIndices); - } - - /** - * Returns a view of the row indices - * @example - * // resulting vector is [[1,2,3], [1,2,3]] - * var matrix = new Matrix([[1,2,3], [4,5,6]]).rowSelectionView([0, 0]) - * @param {Array} rowIndices - * @return {MatrixRowSelectionView} - */ - rowSelectionView(rowIndices) { - return new rowSelection_MatrixRowSelectionView(this, rowIndices); - } - - /** - * Returns a view of the column indices - * @example - * // resulting vector is [[2, 2], [5, 5]] - * var matrix = new Matrix([[1,2,3], [4,5,6]]).columnSelectionView([1, 1]) - * @param {Array} columnIndices - * @return {MatrixColumnSelectionView} - */ - columnSelectionView(columnIndices) { - return new columnSelection_MatrixColumnSelectionView(this, columnIndices); - } - - - /** - * Calculates and returns the determinant of a matrix as a Number - * @example - * new Matrix([[1,2,3], [4,5,6]]).det() - * @return {number} - */ - det() { - if (this.isSquare()) { - var a, b, c, d; - if (this.columns === 2) { - // 2 x 2 matrix - a = this.get(0, 0); - b = this.get(0, 1); - c = this.get(1, 0); - d = this.get(1, 1); - - return a * d - (b * c); - } else if (this.columns === 3) { - // 3 x 3 matrix - var subMatrix0, subMatrix1, subMatrix2; - subMatrix0 = this.selectionView([1, 2], [1, 2]); - subMatrix1 = this.selectionView([1, 2], [0, 2]); - subMatrix2 = this.selectionView([1, 2], [0, 1]); - a = this.get(0, 0); - b = this.get(0, 1); - c = this.get(0, 2); - - return a * subMatrix0.det() - b * subMatrix1.det() + c * subMatrix2.det(); - } else { - // general purpose determinant using the LU decomposition - return new lu_LuDecomposition(this).determinant; - } - } else { - throw Error('Determinant can only be calculated for a square matrix.'); - } - } - - /** - * Returns inverse of a matrix if it exists or the pseudoinverse - * @param {number} threshold - threshold for taking inverse of singular values (default = 1e-15) - * @return {Matrix} the (pseudo)inverted matrix. - */ - pseudoInverse(threshold) { - if (threshold === undefined) threshold = Number.EPSILON; - var svdSolution = new svd_SingularValueDecomposition(this, { autoTranspose: true }); - - var U = svdSolution.leftSingularVectors; - var V = svdSolution.rightSingularVectors; - var s = svdSolution.diagonal; - - for (var i = 0; i < s.length; i++) { - if (Math.abs(s[i]) > threshold) { - s[i] = 1.0 / s[i]; - } else { - s[i] = 0.0; - } - } - - // convert list to diagonal - s = this.constructor[Symbol.species].diag(s); - return V.mmul(s.mmul(U.transposeView())); - } - - /** - * Creates an exact and independent copy of the matrix - * @return {Matrix} - */ - clone() { - var newMatrix = new this.constructor[Symbol.species](this.rows, this.columns); - for (var row = 0; row < this.rows; row++) { - for (var column = 0; column < this.columns; column++) { - newMatrix.set(row, column, this.get(row, column)); - } - } - return newMatrix; - } - } - - Matrix.prototype.klass = 'Matrix'; - - function compareNumbers(a, b) { - return a - b; - } - - /* - Synonyms - */ - - Matrix.random = Matrix.rand; - Matrix.diagonal = Matrix.diag; - Matrix.prototype.diagonal = Matrix.prototype.diag; - Matrix.identity = Matrix.eye; - Matrix.prototype.negate = Matrix.prototype.neg; - Matrix.prototype.tensorProduct = Matrix.prototype.kroneckerProduct; - Matrix.prototype.determinant = Matrix.prototype.det; - - /* - Add dynamically instance and static methods for mathematical operations - */ - - var inplaceOperator = ` -(function %name%(value) { - if (typeof value === 'number') return this.%name%S(value); - return this.%name%M(value); -}) -`; - - var inplaceOperatorScalar = ` -(function %name%S(value) { - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, this.get(i, j) %op% value); - } - } - return this; -}) -`; - - var inplaceOperatorMatrix = ` -(function %name%M(matrix) { - matrix = this.constructor.checkMatrix(matrix); - if (this.rows !== matrix.rows || - this.columns !== matrix.columns) { - throw new RangeError('Matrices dimensions must be equal'); - } - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, this.get(i, j) %op% matrix.get(i, j)); - } - } - return this; -}) -`; - - var staticOperator = ` -(function %name%(matrix, value) { - var newMatrix = new this[Symbol.species](matrix); - return newMatrix.%name%(value); -}) -`; - - var inplaceMethod = ` -(function %name%() { - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, %method%(this.get(i, j))); - } - } - return this; -}) -`; - - var staticMethod = ` -(function %name%(matrix) { - var newMatrix = new this[Symbol.species](matrix); - return newMatrix.%name%(); -}) -`; - - var inplaceMethodWithArgs = ` -(function %name%(%args%) { - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, %method%(this.get(i, j), %args%)); - } - } - return this; -}) -`; - - var staticMethodWithArgs = ` -(function %name%(matrix, %args%) { - var newMatrix = new this[Symbol.species](matrix); - return newMatrix.%name%(%args%); -}) -`; - - - var inplaceMethodWithOneArgScalar = ` -(function %name%S(value) { - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, %method%(this.get(i, j), value)); - } - } - return this; -}) -`; - var inplaceMethodWithOneArgMatrix = ` -(function %name%M(matrix) { - matrix = this.constructor.checkMatrix(matrix); - if (this.rows !== matrix.rows || - this.columns !== matrix.columns) { - throw new RangeError('Matrices dimensions must be equal'); - } - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, %method%(this.get(i, j), matrix.get(i, j))); - } - } - return this; -}) -`; - - var inplaceMethodWithOneArg = ` -(function %name%(value) { - if (typeof value === 'number') return this.%name%S(value); - return this.%name%M(value); -}) -`; - - var staticMethodWithOneArg = staticMethodWithArgs; - - var operators = [ - // Arithmetic operators - ['+', 'add'], - ['-', 'sub', 'subtract'], - ['*', 'mul', 'multiply'], - ['/', 'div', 'divide'], - ['%', 'mod', 'modulus'], - // Bitwise operators - ['&', 'and'], - ['|', 'or'], - ['^', 'xor'], - ['<<', 'leftShift'], - ['>>', 'signPropagatingRightShift'], - ['>>>', 'rightShift', 'zeroFillRightShift'] - ]; - - var i; - var eval2 = eval; // eslint-disable-line no-eval - for (var operator of operators) { - var inplaceOp = eval2(fillTemplateFunction(inplaceOperator, { name: operator[1], op: operator[0] })); - var inplaceOpS = eval2(fillTemplateFunction(inplaceOperatorScalar, { name: `${operator[1]}S`, op: operator[0] })); - var inplaceOpM = eval2(fillTemplateFunction(inplaceOperatorMatrix, { name: `${operator[1]}M`, op: operator[0] })); - var staticOp = eval2(fillTemplateFunction(staticOperator, { name: operator[1] })); - for (i = 1; i < operator.length; i++) { - Matrix.prototype[operator[i]] = inplaceOp; - Matrix.prototype[`${operator[i]}S`] = inplaceOpS; - Matrix.prototype[`${operator[i]}M`] = inplaceOpM; - Matrix[operator[i]] = staticOp; - } - } - - var methods = [['~', 'not']]; - - [ - 'abs', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atanh', 'cbrt', 'ceil', - 'clz32', 'cos', 'cosh', 'exp', 'expm1', 'floor', 'fround', 'log', 'log1p', - 'log10', 'log2', 'round', 'sign', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc' - ].forEach(function (mathMethod) { - methods.push([`Math.${mathMethod}`, mathMethod]); - }); - - for (var method of methods) { - var inplaceMeth = eval2(fillTemplateFunction(inplaceMethod, { name: method[1], method: method[0] })); - var staticMeth = eval2(fillTemplateFunction(staticMethod, { name: method[1] })); - for (i = 1; i < method.length; i++) { - Matrix.prototype[method[i]] = inplaceMeth; - Matrix[method[i]] = staticMeth; - } - } - - var methodsWithArgs = [['Math.pow', 1, 'pow']]; - - for (var methodWithArg of methodsWithArgs) { - var args = 'arg0'; - for (i = 1; i < methodWithArg[1]; i++) { - args += `, arg${i}`; - } - if (methodWithArg[1] !== 1) { - var inplaceMethWithArgs = eval2(fillTemplateFunction(inplaceMethodWithArgs, { - name: methodWithArg[2], - method: methodWithArg[0], - args: args - })); - var staticMethWithArgs = eval2(fillTemplateFunction(staticMethodWithArgs, { name: methodWithArg[2], args: args })); - for (i = 2; i < methodWithArg.length; i++) { - Matrix.prototype[methodWithArg[i]] = inplaceMethWithArgs; - Matrix[methodWithArg[i]] = staticMethWithArgs; - } - } else { - var tmplVar = { - name: methodWithArg[2], - args: args, - method: methodWithArg[0] - }; - var inplaceMethod2 = eval2(fillTemplateFunction(inplaceMethodWithOneArg, tmplVar)); - var inplaceMethodS = eval2(fillTemplateFunction(inplaceMethodWithOneArgScalar, tmplVar)); - var inplaceMethodM = eval2(fillTemplateFunction(inplaceMethodWithOneArgMatrix, tmplVar)); - var staticMethod2 = eval2(fillTemplateFunction(staticMethodWithOneArg, tmplVar)); - for (i = 2; i < methodWithArg.length; i++) { - Matrix.prototype[methodWithArg[i]] = inplaceMethod2; - Matrix.prototype[`${methodWithArg[i]}M`] = inplaceMethodM; - Matrix.prototype[`${methodWithArg[i]}S`] = inplaceMethodS; - Matrix[methodWithArg[i]] = staticMethod2; - } - } - } - - function fillTemplateFunction(template, values) { - for (var value in values) { - template = template.replace(new RegExp(`%${value}%`, 'g'), values[value]); - } - return template; - } - - return Matrix; -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/matrix.js - - - -class matrix_Matrix extends AbstractMatrix(Array) { - constructor(nRows, nColumns) { - var i; - if (arguments.length === 1 && typeof nRows === 'number') { - return new Array(nRows); - } - if (matrix_Matrix.isMatrix(nRows)) { - return nRows.clone(); - } else if (Number.isInteger(nRows) && nRows > 0) { - // Create an empty matrix - super(nRows); - if (Number.isInteger(nColumns) && nColumns > 0) { - for (i = 0; i < nRows; i++) { - this[i] = new Array(nColumns); - } - } else { - throw new TypeError('nColumns must be a positive integer'); - } - } else if (Array.isArray(nRows)) { - // Copy the values from the 2D array - const matrix = nRows; - nRows = matrix.length; - nColumns = matrix[0].length; - if (typeof nColumns !== 'number' || nColumns === 0) { - throw new TypeError( - 'Data must be a 2D array with at least one element' - ); - } - super(nRows); - for (i = 0; i < nRows; i++) { - if (matrix[i].length !== nColumns) { - throw new RangeError('Inconsistent array dimensions'); - } - this[i] = [].concat(matrix[i]); - } - } else { - throw new TypeError( - 'First argument must be a positive number or an array' - ); - } - this.rows = nRows; - this.columns = nColumns; - return this; - } - - set(rowIndex, columnIndex, value) { - this[rowIndex][columnIndex] = value; - return this; - } - - get(rowIndex, columnIndex) { - return this[rowIndex][columnIndex]; - } - - /** - * Removes a row from the given index - * @param {number} index - Row index - * @return {Matrix} this - */ - removeRow(index) { - checkRowIndex(this, index); - if (this.rows === 1) { - throw new RangeError('A matrix cannot have less than one row'); - } - this.splice(index, 1); - this.rows -= 1; - return this; - } - - /** - * Adds a row at the given index - * @param {number} [index = this.rows] - Row index - * @param {Array|Matrix} array - Array or vector - * @return {Matrix} this - */ - addRow(index, array) { - if (array === undefined) { - array = index; - index = this.rows; - } - checkRowIndex(this, index, true); - array = checkRowVector(this, array, true); - this.splice(index, 0, array); - this.rows += 1; - return this; - } - - /** - * Removes a column from the given index - * @param {number} index - Column index - * @return {Matrix} this - */ - removeColumn(index) { - checkColumnIndex(this, index); - if (this.columns === 1) { - throw new RangeError('A matrix cannot have less than one column'); - } - for (var i = 0; i < this.rows; i++) { - this[i].splice(index, 1); - } - this.columns -= 1; - return this; - } - - /** - * Adds a column at the given index - * @param {number} [index = this.columns] - Column index - * @param {Array|Matrix} array - Array or vector - * @return {Matrix} this - */ - addColumn(index, array) { - if (typeof array === 'undefined') { - array = index; - index = this.columns; - } - checkColumnIndex(this, index, true); - array = checkColumnVector(this, array); - for (var i = 0; i < this.rows; i++) { - this[i].splice(index, 0, array[i]); - } - this.columns += 1; - return this; - } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/wrap/WrapperMatrix1D.js - - - -class WrapperMatrix1D_WrapperMatrix1D extends AbstractMatrix() { - /** - * @class WrapperMatrix1D - * @param {Array} data - * @param {object} [options] - * @param {object} [options.rows = 1] - */ - constructor(data, options = {}) { - const { rows = 1 } = options; - - if (data.length % rows !== 0) { - throw new Error('the data length is not divisible by the number of rows'); - } - super(); - this.rows = rows; - this.columns = data.length / rows; - this.data = data; - } - - set(rowIndex, columnIndex, value) { - var index = this._calculateIndex(rowIndex, columnIndex); - this.data[index] = value; - return this; - } - - get(rowIndex, columnIndex) { - var index = this._calculateIndex(rowIndex, columnIndex); - return this.data[index]; - } - - _calculateIndex(row, column) { - return row * this.columns + column; - } - - static get [Symbol.species]() { - return matrix_Matrix; - } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/wrap/WrapperMatrix2D.js - - - -class WrapperMatrix2D_WrapperMatrix2D extends AbstractMatrix() { - /** - * @class WrapperMatrix2D - * @param {Array>} data - */ - constructor(data) { - super(); - this.data = data; - this.rows = data.length; - this.columns = data[0].length; - } - - set(rowIndex, columnIndex, value) { - this.data[rowIndex][columnIndex] = value; - return this; - } - - get(rowIndex, columnIndex) { - return this.data[rowIndex][columnIndex]; - } - - static get [Symbol.species]() { - return matrix_Matrix; - } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/wrap/wrap.js - - - -/** - * @param {Array>|Array} array - * @param {object} [options] - * @param {object} [options.rows = 1] - * @return {WrapperMatrix1D|WrapperMatrix2D} - */ -function wrap(array, options) { - if (Array.isArray(array)) { - if (array[0] && Array.isArray(array[0])) { - return new WrapperMatrix2D_WrapperMatrix2D(array); - } else { - return new WrapperMatrix1D_WrapperMatrix1D(array, options); - } - } else { - throw new Error('the argument is not an array'); - } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/dc/qr.js - - - - -/** - * @class QrDecomposition - * @link https://github.com/lutzroeder/Mapack/blob/master/Source/QrDecomposition.cs - * @param {Matrix} value - */ -class qr_QrDecomposition { - constructor(value) { - value = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(value); - - var qr = value.clone(); - var m = value.rows; - var n = value.columns; - var rdiag = new Array(n); - var i, j, k, s; - - for (k = 0; k < n; k++) { - var nrm = 0; - for (i = k; i < m; i++) { - nrm = hypotenuse(nrm, qr.get(i, k)); - } - if (nrm !== 0) { - if (qr.get(k, k) < 0) { - nrm = -nrm; - } - for (i = k; i < m; i++) { - qr.set(i, k, qr.get(i, k) / nrm); - } - qr.set(k, k, qr.get(k, k) + 1); - for (j = k + 1; j < n; j++) { - s = 0; - for (i = k; i < m; i++) { - s += qr.get(i, k) * qr.get(i, j); - } - s = -s / qr.get(k, k); - for (i = k; i < m; i++) { - qr.set(i, j, qr.get(i, j) + s * qr.get(i, k)); - } - } - } - rdiag[k] = -nrm; - } - - this.QR = qr; - this.Rdiag = rdiag; - } - - /** - * Solve a problem of least square (Ax=b) by using the QR decomposition. Useful when A is rectangular, but not working when A is singular. - * Example : We search to approximate x, with A matrix shape m*n, x vector size n, b vector size m (m > n). We will use : - * var qr = QrDecomposition(A); - * var x = qr.solve(b); - * @param {Matrix} value - Matrix 1D which is the vector b (in the equation Ax = b) - * @return {Matrix} - The vector x - */ - solve(value) { - value = matrix_Matrix.checkMatrix(value); - - var qr = this.QR; - var m = qr.rows; - - if (value.rows !== m) { - throw new Error('Matrix row dimensions must agree'); - } - if (!this.isFullRank()) { - throw new Error('Matrix is rank deficient'); - } - - var count = value.columns; - var X = value.clone(); - var n = qr.columns; - var i, j, k, s; - - for (k = 0; k < n; k++) { - for (j = 0; j < count; j++) { - s = 0; - for (i = k; i < m; i++) { - s += qr[i][k] * X[i][j]; - } - s = -s / qr[k][k]; - for (i = k; i < m; i++) { - X[i][j] += s * qr[i][k]; - } - } - } - for (k = n - 1; k >= 0; k--) { - for (j = 0; j < count; j++) { - X[k][j] /= this.Rdiag[k]; - } - for (i = 0; i < k; i++) { - for (j = 0; j < count; j++) { - X[i][j] -= X[k][j] * qr[i][k]; - } - } - } - - return X.subMatrix(0, n - 1, 0, count - 1); - } - - /** - * - * @return {boolean} - */ - isFullRank() { - var columns = this.QR.columns; - for (var i = 0; i < columns; i++) { - if (this.Rdiag[i] === 0) { - return false; - } - } - return true; - } - - /** - * - * @return {Matrix} - */ - get upperTriangularMatrix() { - var qr = this.QR; - var n = qr.columns; - var X = new matrix_Matrix(n, n); - var i, j; - for (i = 0; i < n; i++) { - for (j = 0; j < n; j++) { - if (i < j) { - X[i][j] = qr[i][j]; - } else if (i === j) { - X[i][j] = this.Rdiag[i]; - } else { - X[i][j] = 0; - } - } - } - return X; - } - - /** - * - * @return {Matrix} - */ - get orthogonalMatrix() { - var qr = this.QR; - var rows = qr.rows; - var columns = qr.columns; - var X = new matrix_Matrix(rows, columns); - var i, j, k, s; - - for (k = columns - 1; k >= 0; k--) { - for (i = 0; i < rows; i++) { - X[i][k] = 0; - } - X[k][k] = 1; - for (j = k; j < columns; j++) { - if (qr[k][k] !== 0) { - s = 0; - for (i = k; i < rows; i++) { - s += qr[i][k] * X[i][j]; - } - - s = -s / qr[k][k]; - - for (i = k; i < rows; i++) { - X[i][j] += s * qr[i][k]; - } - } - } - } - return X; - } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/decompositions.js - - - - - - -/** - * Computes the inverse of a Matrix - * @param {Matrix} matrix - * @param {boolean} [useSVD=false] - * @return {Matrix} - */ -function inverse(matrix, useSVD = false) { - matrix = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(matrix); - if (useSVD) { - return new svd_SingularValueDecomposition(matrix).inverse(); - } else { - return solve(matrix, matrix_Matrix.eye(matrix.rows)); - } -} - -/** - * - * @param {Matrix} leftHandSide - * @param {Matrix} rightHandSide - * @param {boolean} [useSVD = false] - * @return {Matrix} - */ -function solve(leftHandSide, rightHandSide, useSVD = false) { - leftHandSide = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(leftHandSide); - rightHandSide = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(rightHandSide); - if (useSVD) { - return new svd_SingularValueDecomposition(leftHandSide).solve(rightHandSide); - } else { - return leftHandSide.isSquare() - ? new lu_LuDecomposition(leftHandSide).solve(rightHandSide) - : new qr_QrDecomposition(leftHandSide).solve(rightHandSide); - } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/linearDependencies.js - - - - - -// function used by rowsDependencies -function xrange(n, exception) { - var range = []; - for (var i = 0; i < n; i++) { - if (i !== exception) { - range.push(i); - } - } - return range; -} - -// function used by rowsDependencies -function dependenciesOneRow( - error, - matrix, - index, - thresholdValue = 10e-10, - thresholdError = 10e-10 -) { - if (error > thresholdError) { - return new Array(matrix.rows + 1).fill(0); - } else { - var returnArray = matrix.addRow(index, [0]); - for (var i = 0; i < returnArray.rows; i++) { - if (Math.abs(returnArray.get(i, 0)) < thresholdValue) { - returnArray.set(i, 0, 0); - } - } - return returnArray.to1DArray(); - } -} - -/** - * Creates a matrix which represents the dependencies between rows. - * If a row is a linear combination of others rows, the result will be a row with the coefficients of this combination. - * For example : for A = [[2, 0, 0, 1], [0, 1, 6, 0], [0, 3, 0, 1], [0, 0, 1, 0], [0, 1, 2, 0]], the result will be [[0, 0, 0, 0, 0], [0, 0, 0, 4, 1], [0, 0, 0, 0, 0], [0, 0.25, 0, 0, -0.25], [0, 1, 0, -4, 0]] - * @param {Matrix} matrix - * @param {Object} [options] includes thresholdValue and thresholdError. - * @param {number} [options.thresholdValue = 10e-10] If an absolute value is inferior to this threshold, it will equals zero. - * @param {number} [options.thresholdError = 10e-10] If the error is inferior to that threshold, the linear combination found is accepted and the row is dependent from other rows. - * @return {Matrix} the matrix which represents the dependencies between rows. - */ - -function linearDependencies(matrix, options = {}) { - const { thresholdValue = 10e-10, thresholdError = 10e-10 } = options; - - var n = matrix.rows; - var results = new matrix_Matrix(n, n); - - for (var i = 0; i < n; i++) { - var b = matrix_Matrix.columnVector(matrix.getRow(i)); - var Abis = matrix.subMatrixRow(xrange(n, i)).transposeView(); - var svd = new svd_SingularValueDecomposition(Abis); - var x = svd.solve(b); - var error = lib_es6( - matrix_Matrix.sub(b, Abis.mmul(x)) - .abs() - .to1DArray() - ); - results.setRow( - i, - dependenciesOneRow(error, x, i, thresholdValue, thresholdError) - ); - } - return results; -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/dc/evd.js - - - - -/** - * @class EigenvalueDecomposition - * @link https://github.com/lutzroeder/Mapack/blob/master/Source/EigenvalueDecomposition.cs - * @param {Matrix} matrix - * @param {object} [options] - * @param {boolean} [options.assumeSymmetric=false] - */ -class evd_EigenvalueDecomposition { - constructor(matrix, options = {}) { - const { assumeSymmetric = false } = options; - - matrix = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(matrix); - if (!matrix.isSquare()) { - throw new Error('Matrix is not a square matrix'); - } - - var n = matrix.columns; - var V = getFilled2DArray(n, n, 0); - var d = new Array(n); - var e = new Array(n); - var value = matrix; - var i, j; - - var isSymmetric = false; - if (assumeSymmetric) { - isSymmetric = true; - } else { - isSymmetric = matrix.isSymmetric(); - } - - if (isSymmetric) { - for (i = 0; i < n; i++) { - for (j = 0; j < n; j++) { - V[i][j] = value.get(i, j); - } - } - tred2(n, e, d, V); - tql2(n, e, d, V); - } else { - var H = getFilled2DArray(n, n, 0); - var ort = new Array(n); - for (j = 0; j < n; j++) { - for (i = 0; i < n; i++) { - H[i][j] = value.get(i, j); - } - } - orthes(n, H, ort, V); - hqr2(n, e, d, V, H); - } - - this.n = n; - this.e = e; - this.d = d; - this.V = V; - } - - /** - * - * @return {Array} - */ - get realEigenvalues() { - return this.d; - } - - /** - * - * @return {Array} - */ - get imaginaryEigenvalues() { - return this.e; - } - - /** - * - * @return {Matrix} - */ - get eigenvectorMatrix() { - if (!matrix_Matrix.isMatrix(this.V)) { - this.V = new matrix_Matrix(this.V); - } - return this.V; - } - - /** - * - * @return {Matrix} - */ - get diagonalMatrix() { - var n = this.n; - var e = this.e; - var d = this.d; - var X = new matrix_Matrix(n, n); - var i, j; - for (i = 0; i < n; i++) { - for (j = 0; j < n; j++) { - X[i][j] = 0; - } - X[i][i] = d[i]; - if (e[i] > 0) { - X[i][i + 1] = e[i]; - } else if (e[i] < 0) { - X[i][i - 1] = e[i]; - } - } - return X; - } -} - -function tred2(n, e, d, V) { - var f, g, h, i, j, k, hh, scale; - - for (j = 0; j < n; j++) { - d[j] = V[n - 1][j]; - } - - for (i = n - 1; i > 0; i--) { - scale = 0; - h = 0; - for (k = 0; k < i; k++) { - scale = scale + Math.abs(d[k]); - } - - if (scale === 0) { - e[i] = d[i - 1]; - for (j = 0; j < i; j++) { - d[j] = V[i - 1][j]; - V[i][j] = 0; - V[j][i] = 0; - } - } else { - for (k = 0; k < i; k++) { - d[k] /= scale; - h += d[k] * d[k]; - } - - f = d[i - 1]; - g = Math.sqrt(h); - if (f > 0) { - g = -g; - } - - e[i] = scale * g; - h = h - f * g; - d[i - 1] = f - g; - for (j = 0; j < i; j++) { - e[j] = 0; - } - - for (j = 0; j < i; j++) { - f = d[j]; - V[j][i] = f; - g = e[j] + V[j][j] * f; - for (k = j + 1; k <= i - 1; k++) { - g += V[k][j] * d[k]; - e[k] += V[k][j] * f; - } - e[j] = g; - } - - f = 0; - for (j = 0; j < i; j++) { - e[j] /= h; - f += e[j] * d[j]; - } - - hh = f / (h + h); - for (j = 0; j < i; j++) { - e[j] -= hh * d[j]; - } - - for (j = 0; j < i; j++) { - f = d[j]; - g = e[j]; - for (k = j; k <= i - 1; k++) { - V[k][j] -= f * e[k] + g * d[k]; - } - d[j] = V[i - 1][j]; - V[i][j] = 0; - } - } - d[i] = h; - } - - for (i = 0; i < n - 1; i++) { - V[n - 1][i] = V[i][i]; - V[i][i] = 1; - h = d[i + 1]; - if (h !== 0) { - for (k = 0; k <= i; k++) { - d[k] = V[k][i + 1] / h; - } - - for (j = 0; j <= i; j++) { - g = 0; - for (k = 0; k <= i; k++) { - g += V[k][i + 1] * V[k][j]; - } - for (k = 0; k <= i; k++) { - V[k][j] -= g * d[k]; - } - } - } - - for (k = 0; k <= i; k++) { - V[k][i + 1] = 0; - } - } - - for (j = 0; j < n; j++) { - d[j] = V[n - 1][j]; - V[n - 1][j] = 0; - } - - V[n - 1][n - 1] = 1; - e[0] = 0; -} - -function tql2(n, e, d, V) { - var g, h, i, j, k, l, m, p, r, dl1, c, c2, c3, el1, s, s2, iter; - - for (i = 1; i < n; i++) { - e[i - 1] = e[i]; - } - - e[n - 1] = 0; - - var f = 0; - var tst1 = 0; - var eps = Number.EPSILON; - - for (l = 0; l < n; l++) { - tst1 = Math.max(tst1, Math.abs(d[l]) + Math.abs(e[l])); - m = l; - while (m < n) { - if (Math.abs(e[m]) <= eps * tst1) { - break; - } - m++; - } - - if (m > l) { - iter = 0; - do { - iter = iter + 1; - - g = d[l]; - p = (d[l + 1] - g) / (2 * e[l]); - r = hypotenuse(p, 1); - if (p < 0) { - r = -r; - } - - d[l] = e[l] / (p + r); - d[l + 1] = e[l] * (p + r); - dl1 = d[l + 1]; - h = g - d[l]; - for (i = l + 2; i < n; i++) { - d[i] -= h; - } - - f = f + h; - - p = d[m]; - c = 1; - c2 = c; - c3 = c; - el1 = e[l + 1]; - s = 0; - s2 = 0; - for (i = m - 1; i >= l; i--) { - c3 = c2; - c2 = c; - s2 = s; - g = c * e[i]; - h = c * p; - r = hypotenuse(p, e[i]); - e[i + 1] = s * r; - s = e[i] / r; - c = p / r; - p = c * d[i] - s * g; - d[i + 1] = h + s * (c * g + s * d[i]); - - for (k = 0; k < n; k++) { - h = V[k][i + 1]; - V[k][i + 1] = s * V[k][i] + c * h; - V[k][i] = c * V[k][i] - s * h; - } - } - - p = -s * s2 * c3 * el1 * e[l] / dl1; - e[l] = s * p; - d[l] = c * p; - } while (Math.abs(e[l]) > eps * tst1); - } - d[l] = d[l] + f; - e[l] = 0; - } - - for (i = 0; i < n - 1; i++) { - k = i; - p = d[i]; - for (j = i + 1; j < n; j++) { - if (d[j] < p) { - k = j; - p = d[j]; - } - } - - if (k !== i) { - d[k] = d[i]; - d[i] = p; - for (j = 0; j < n; j++) { - p = V[j][i]; - V[j][i] = V[j][k]; - V[j][k] = p; - } - } - } -} - -function orthes(n, H, ort, V) { - var low = 0; - var high = n - 1; - var f, g, h, i, j, m; - var scale; - - for (m = low + 1; m <= high - 1; m++) { - scale = 0; - for (i = m; i <= high; i++) { - scale = scale + Math.abs(H[i][m - 1]); - } - - if (scale !== 0) { - h = 0; - for (i = high; i >= m; i--) { - ort[i] = H[i][m - 1] / scale; - h += ort[i] * ort[i]; - } - - g = Math.sqrt(h); - if (ort[m] > 0) { - g = -g; - } - - h = h - ort[m] * g; - ort[m] = ort[m] - g; - - for (j = m; j < n; j++) { - f = 0; - for (i = high; i >= m; i--) { - f += ort[i] * H[i][j]; - } - - f = f / h; - for (i = m; i <= high; i++) { - H[i][j] -= f * ort[i]; - } - } - - for (i = 0; i <= high; i++) { - f = 0; - for (j = high; j >= m; j--) { - f += ort[j] * H[i][j]; - } - - f = f / h; - for (j = m; j <= high; j++) { - H[i][j] -= f * ort[j]; - } - } - - ort[m] = scale * ort[m]; - H[m][m - 1] = scale * g; - } - } - - for (i = 0; i < n; i++) { - for (j = 0; j < n; j++) { - V[i][j] = i === j ? 1 : 0; - } - } - - for (m = high - 1; m >= low + 1; m--) { - if (H[m][m - 1] !== 0) { - for (i = m + 1; i <= high; i++) { - ort[i] = H[i][m - 1]; - } - - for (j = m; j <= high; j++) { - g = 0; - for (i = m; i <= high; i++) { - g += ort[i] * V[i][j]; - } - - g = g / ort[m] / H[m][m - 1]; - for (i = m; i <= high; i++) { - V[i][j] += g * ort[i]; - } - } - } - } -} - -function hqr2(nn, e, d, V, H) { - var n = nn - 1; - var low = 0; - var high = nn - 1; - var eps = Number.EPSILON; - var exshift = 0; - var norm = 0; - var p = 0; - var q = 0; - var r = 0; - var s = 0; - var z = 0; - var iter = 0; - var i, j, k, l, m, t, w, x, y; - var ra, sa, vr, vi; - var notlast, cdivres; - - for (i = 0; i < nn; i++) { - if (i < low || i > high) { - d[i] = H[i][i]; - e[i] = 0; - } - - for (j = Math.max(i - 1, 0); j < nn; j++) { - norm = norm + Math.abs(H[i][j]); - } - } - - while (n >= low) { - l = n; - while (l > low) { - s = Math.abs(H[l - 1][l - 1]) + Math.abs(H[l][l]); - if (s === 0) { - s = norm; - } - if (Math.abs(H[l][l - 1]) < eps * s) { - break; - } - l--; - } - - if (l === n) { - H[n][n] = H[n][n] + exshift; - d[n] = H[n][n]; - e[n] = 0; - n--; - iter = 0; - } else if (l === n - 1) { - w = H[n][n - 1] * H[n - 1][n]; - p = (H[n - 1][n - 1] - H[n][n]) / 2; - q = p * p + w; - z = Math.sqrt(Math.abs(q)); - H[n][n] = H[n][n] + exshift; - H[n - 1][n - 1] = H[n - 1][n - 1] + exshift; - x = H[n][n]; - - if (q >= 0) { - z = p >= 0 ? p + z : p - z; - d[n - 1] = x + z; - d[n] = d[n - 1]; - if (z !== 0) { - d[n] = x - w / z; - } - e[n - 1] = 0; - e[n] = 0; - x = H[n][n - 1]; - s = Math.abs(x) + Math.abs(z); - p = x / s; - q = z / s; - r = Math.sqrt(p * p + q * q); - p = p / r; - q = q / r; - - for (j = n - 1; j < nn; j++) { - z = H[n - 1][j]; - H[n - 1][j] = q * z + p * H[n][j]; - H[n][j] = q * H[n][j] - p * z; - } - - for (i = 0; i <= n; i++) { - z = H[i][n - 1]; - H[i][n - 1] = q * z + p * H[i][n]; - H[i][n] = q * H[i][n] - p * z; - } - - for (i = low; i <= high; i++) { - z = V[i][n - 1]; - V[i][n - 1] = q * z + p * V[i][n]; - V[i][n] = q * V[i][n] - p * z; - } - } else { - d[n - 1] = x + p; - d[n] = x + p; - e[n - 1] = z; - e[n] = -z; - } - - n = n - 2; - iter = 0; - } else { - x = H[n][n]; - y = 0; - w = 0; - if (l < n) { - y = H[n - 1][n - 1]; - w = H[n][n - 1] * H[n - 1][n]; - } - - if (iter === 10) { - exshift += x; - for (i = low; i <= n; i++) { - H[i][i] -= x; - } - s = Math.abs(H[n][n - 1]) + Math.abs(H[n - 1][n - 2]); - x = y = 0.75 * s; - w = -0.4375 * s * s; - } - - if (iter === 30) { - s = (y - x) / 2; - s = s * s + w; - if (s > 0) { - s = Math.sqrt(s); - if (y < x) { - s = -s; - } - s = x - w / ((y - x) / 2 + s); - for (i = low; i <= n; i++) { - H[i][i] -= s; - } - exshift += s; - x = y = w = 0.964; - } - } - - iter = iter + 1; - - m = n - 2; - while (m >= l) { - z = H[m][m]; - r = x - z; - s = y - z; - p = (r * s - w) / H[m + 1][m] + H[m][m + 1]; - q = H[m + 1][m + 1] - z - r - s; - r = H[m + 2][m + 1]; - s = Math.abs(p) + Math.abs(q) + Math.abs(r); - p = p / s; - q = q / s; - r = r / s; - if (m === l) { - break; - } - if ( - Math.abs(H[m][m - 1]) * (Math.abs(q) + Math.abs(r)) < - eps * - (Math.abs(p) * - (Math.abs(H[m - 1][m - 1]) + - Math.abs(z) + - Math.abs(H[m + 1][m + 1]))) - ) { - break; - } - m--; - } - - for (i = m + 2; i <= n; i++) { - H[i][i - 2] = 0; - if (i > m + 2) { - H[i][i - 3] = 0; - } - } - - for (k = m; k <= n - 1; k++) { - notlast = k !== n - 1; - if (k !== m) { - p = H[k][k - 1]; - q = H[k + 1][k - 1]; - r = notlast ? H[k + 2][k - 1] : 0; - x = Math.abs(p) + Math.abs(q) + Math.abs(r); - if (x !== 0) { - p = p / x; - q = q / x; - r = r / x; - } - } - - if (x === 0) { - break; - } - - s = Math.sqrt(p * p + q * q + r * r); - if (p < 0) { - s = -s; - } - - if (s !== 0) { - if (k !== m) { - H[k][k - 1] = -s * x; - } else if (l !== m) { - H[k][k - 1] = -H[k][k - 1]; - } - - p = p + s; - x = p / s; - y = q / s; - z = r / s; - q = q / p; - r = r / p; - - for (j = k; j < nn; j++) { - p = H[k][j] + q * H[k + 1][j]; - if (notlast) { - p = p + r * H[k + 2][j]; - H[k + 2][j] = H[k + 2][j] - p * z; - } - - H[k][j] = H[k][j] - p * x; - H[k + 1][j] = H[k + 1][j] - p * y; - } - - for (i = 0; i <= Math.min(n, k + 3); i++) { - p = x * H[i][k] + y * H[i][k + 1]; - if (notlast) { - p = p + z * H[i][k + 2]; - H[i][k + 2] = H[i][k + 2] - p * r; - } - - H[i][k] = H[i][k] - p; - H[i][k + 1] = H[i][k + 1] - p * q; - } - - for (i = low; i <= high; i++) { - p = x * V[i][k] + y * V[i][k + 1]; - if (notlast) { - p = p + z * V[i][k + 2]; - V[i][k + 2] = V[i][k + 2] - p * r; - } - - V[i][k] = V[i][k] - p; - V[i][k + 1] = V[i][k + 1] - p * q; - } - } - } - } - } - - if (norm === 0) { - return; - } - - for (n = nn - 1; n >= 0; n--) { - p = d[n]; - q = e[n]; - - if (q === 0) { - l = n; - H[n][n] = 1; - for (i = n - 1; i >= 0; i--) { - w = H[i][i] - p; - r = 0; - for (j = l; j <= n; j++) { - r = r + H[i][j] * H[j][n]; - } - - if (e[i] < 0) { - z = w; - s = r; - } else { - l = i; - if (e[i] === 0) { - H[i][n] = w !== 0 ? -r / w : -r / (eps * norm); - } else { - x = H[i][i + 1]; - y = H[i + 1][i]; - q = (d[i] - p) * (d[i] - p) + e[i] * e[i]; - t = (x * s - z * r) / q; - H[i][n] = t; - H[i + 1][n] = - Math.abs(x) > Math.abs(z) ? (-r - w * t) / x : (-s - y * t) / z; - } - - t = Math.abs(H[i][n]); - if (eps * t * t > 1) { - for (j = i; j <= n; j++) { - H[j][n] = H[j][n] / t; - } - } - } - } - } else if (q < 0) { - l = n - 1; - - if (Math.abs(H[n][n - 1]) > Math.abs(H[n - 1][n])) { - H[n - 1][n - 1] = q / H[n][n - 1]; - H[n - 1][n] = -(H[n][n] - p) / H[n][n - 1]; - } else { - cdivres = cdiv(0, -H[n - 1][n], H[n - 1][n - 1] - p, q); - H[n - 1][n - 1] = cdivres[0]; - H[n - 1][n] = cdivres[1]; - } - - H[n][n - 1] = 0; - H[n][n] = 1; - for (i = n - 2; i >= 0; i--) { - ra = 0; - sa = 0; - for (j = l; j <= n; j++) { - ra = ra + H[i][j] * H[j][n - 1]; - sa = sa + H[i][j] * H[j][n]; - } - - w = H[i][i] - p; - - if (e[i] < 0) { - z = w; - r = ra; - s = sa; - } else { - l = i; - if (e[i] === 0) { - cdivres = cdiv(-ra, -sa, w, q); - H[i][n - 1] = cdivres[0]; - H[i][n] = cdivres[1]; - } else { - x = H[i][i + 1]; - y = H[i + 1][i]; - vr = (d[i] - p) * (d[i] - p) + e[i] * e[i] - q * q; - vi = (d[i] - p) * 2 * q; - if (vr === 0 && vi === 0) { - vr = - eps * - norm * - (Math.abs(w) + - Math.abs(q) + - Math.abs(x) + - Math.abs(y) + - Math.abs(z)); - } - cdivres = cdiv( - x * r - z * ra + q * sa, - x * s - z * sa - q * ra, - vr, - vi - ); - H[i][n - 1] = cdivres[0]; - H[i][n] = cdivres[1]; - if (Math.abs(x) > Math.abs(z) + Math.abs(q)) { - H[i + 1][n - 1] = (-ra - w * H[i][n - 1] + q * H[i][n]) / x; - H[i + 1][n] = (-sa - w * H[i][n] - q * H[i][n - 1]) / x; - } else { - cdivres = cdiv(-r - y * H[i][n - 1], -s - y * H[i][n], z, q); - H[i + 1][n - 1] = cdivres[0]; - H[i + 1][n] = cdivres[1]; - } - } - - t = Math.max(Math.abs(H[i][n - 1]), Math.abs(H[i][n])); - if (eps * t * t > 1) { - for (j = i; j <= n; j++) { - H[j][n - 1] = H[j][n - 1] / t; - H[j][n] = H[j][n] / t; - } - } - } - } - } - } - - for (i = 0; i < nn; i++) { - if (i < low || i > high) { - for (j = i; j < nn; j++) { - V[i][j] = H[i][j]; - } - } - } - - for (j = nn - 1; j >= low; j--) { - for (i = low; i <= high; i++) { - z = 0; - for (k = low; k <= Math.min(j, high); k++) { - z = z + V[i][k] * H[k][j]; - } - V[i][j] = z; - } - } -} - -function cdiv(xr, xi, yr, yi) { - var r, d; - if (Math.abs(yr) > Math.abs(yi)) { - r = yi / yr; - d = yr + r * yi; - return [(xr + r * xi) / d, (xi - r * xr) / d]; - } else { - r = yr / yi; - d = yi + r * yr; - return [(r * xr + xi) / d, (r * xi - xr) / d]; - } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/dc/cholesky.js - - -/** - * @class CholeskyDecomposition - * @link https://github.com/lutzroeder/Mapack/blob/master/Source/CholeskyDecomposition.cs - * @param {Matrix} value - */ -class cholesky_CholeskyDecomposition { - constructor(value) { - value = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(value); - if (!value.isSymmetric()) { - throw new Error('Matrix is not symmetric'); - } - - var a = value; - var dimension = a.rows; - var l = new matrix_Matrix(dimension, dimension); - var positiveDefinite = true; - var i, j, k; - - for (j = 0; j < dimension; j++) { - var Lrowj = l[j]; - var d = 0; - for (k = 0; k < j; k++) { - var Lrowk = l[k]; - var s = 0; - for (i = 0; i < k; i++) { - s += Lrowk[i] * Lrowj[i]; - } - Lrowj[k] = s = (a.get(j, k) - s) / l[k][k]; - d = d + s * s; - } - - d = a.get(j, j) - d; - - positiveDefinite &= d > 0; - l[j][j] = Math.sqrt(Math.max(d, 0)); - for (k = j + 1; k < dimension; k++) { - l[j][k] = 0; - } - } - - if (!positiveDefinite) { - throw new Error('Matrix is not positive definite'); - } - - this.L = l; - } - - /** - * - * @param {Matrix} value - * @return {Matrix} - */ - solve(value) { - value = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(value); - - var l = this.L; - var dimension = l.rows; - - if (value.rows !== dimension) { - throw new Error('Matrix dimensions do not match'); - } - - var count = value.columns; - var B = value.clone(); - var i, j, k; - - for (k = 0; k < dimension; k++) { - for (j = 0; j < count; j++) { - for (i = 0; i < k; i++) { - B[k][j] -= B[i][j] * l[k][i]; - } - B[k][j] /= l[k][k]; - } - } - - for (k = dimension - 1; k >= 0; k--) { - for (j = 0; j < count; j++) { - for (i = k + 1; i < dimension; i++) { - B[k][j] -= B[i][j] * l[i][k]; - } - B[k][j] /= l[k][k]; - } - } - - return B; - } - - /** - * - * @return {Matrix} - */ - get lowerTriangularMatrix() { - return this.L; - } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/index.js - - - - - - - - - - - - - - - -// CONCATENATED MODULE: ./node_modules/ml-levenberg-marquardt/src/step.js - - /** * Difference of the matrix function over the parameters * @ignore @@ -6752,7 +2061,7 @@ function gradientFunction( ans[param][point] = evaluatedData[point] - funcParam(data.x[point]); } } - return new matrix_Matrix(ans); + return new mlMatrix.Matrix(ans); } /** @@ -6768,10 +2077,10 @@ function matrixFunction(data, evaluatedData) { var ans = new Array(m); for (var point = 0; point < m; point++) { - ans[point] = [data.y[point] - evaluatedData[point]]; + ans[point] = data.y[point] - evaluatedData[point]; } - return new matrix_Matrix(ans); + return new mlMatrix.Matrix([ans]); } /** @@ -6791,12 +2100,16 @@ function step( gradientDifference, parameterizedFunction ) { - var value = damping * gradientDifference * gradientDifference; - var identity = matrix_Matrix.eye(params.length, params.length, value); + var identity = mlMatrix.Matrix.eye(params.length).mul( + damping * gradientDifference * gradientDifference + ); + var l = data.x.length; + var evaluatedData = new Array(l); const func = parameterizedFunction(params); - var evaluatedData = data.x.map((e) => func(e)); - + for (var i = 0; i < l; i++) { + evaluatedData[i] = func(data.x[i]); + } var gradientFunc = gradientFunction( data, evaluatedData, @@ -6804,28 +2117,22 @@ function step( gradientDifference, parameterizedFunction ); - var matrixFunc = matrixFunction(data, evaluatedData); - var inverseMatrix = inverse( - identity.add(gradientFunc.mmul(gradientFunc.transpose())) + var matrixFunc = matrixFunction(data, evaluatedData).transposeView(); + var inverseMatrix = mlMatrix.inverse( + identity.add(gradientFunc.mmul(gradientFunc.transposeView())) ); - - params = new matrix_Matrix([params]); + params = new mlMatrix.Matrix([params]); params = params.sub( inverseMatrix .mmul(gradientFunc) .mmul(matrixFunc) .mul(gradientDifference) - .transpose() + .transposeView() ); return params.to1DArray(); } -// CONCATENATED MODULE: ./node_modules/ml-levenberg-marquardt/src/index.js -/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "default", function() { return levenbergMarquardt; }); - - - /** * Curve fitting algorithm * @param {{x:Array, y:Array}} data - Array of points to fit in the format [x1, x2, ... ], [y1, y2, ... ] @@ -6833,8 +2140,6 @@ function step( * @param {object} [options] - Options object * @param {number} [options.damping] - Levenberg-Marquardt parameter * @param {number} [options.gradientDifference = 10e-2] - Adjustment for decrease the damping parameter - * @param {Array} [options.minValues] - Minimum allowed values for parameters - * @param {Array} [options.maxValues] - Maximum allowed values for parameters * @param {Array} [options.initialValues] - Array of initial parameter values * @param {number} [options.maxIterations = 100] - Maximum of allowed iterations * @param {number} [options.errorTolerance = 10e-3] - Minimum uncertainty allowed for each point @@ -6850,8 +2155,6 @@ function levenbergMarquardt( gradientDifference = 10e-2, damping = 0, errorTolerance = 10e-3, - minValues, - maxValues, initialValues } = options; @@ -6868,19 +2171,15 @@ function levenbergMarquardt( throw new Error( 'The data parameter elements must be an array with more than 2 points' ); - } else if (data.x.length !== data.y.length) { - throw new Error('The data parameter elements must have the same size'); + } else { + let dataLen = data.x.length; + if (dataLen !== data.y.length) { + throw new Error('The data parameter elements must have the same size'); + } } var parameters = initialValues || new Array(parameterizedFunction.length).fill(1); - let parLen = parameters.length; - maxValues = maxValues || new Array(parLen).fill(Number.MAX_SAFE_INTEGER); - minValues = minValues || new Array(parLen).fill(Number.MIN_SAFE_INTEGER); - - if (maxValues.length !== minValues.length) { - throw new Error('minValues and maxValues must be the same size'); - } if (!Array.isArray(parameters)) { throw new Error('initialValues must be an array'); @@ -6902,16 +2201,7 @@ function levenbergMarquardt( gradientDifference, parameterizedFunction ); - - for (let k = 0; k < parLen; k++) { - parameters[k] = Math.min( - Math.max(minValues[k], parameters[k]), - maxValues[k] - ); - } - error = errorCalculation(data, parameters, parameterizedFunction); - if (isNaN(error)) break; converged = error <= errorTolerance; } @@ -6922,6 +2212,4663 @@ function levenbergMarquardt( }; } +module.exports = levenbergMarquardt; + + +/***/ }), +/* 9 */ +/***/ (function(module, __webpack_exports__, __webpack_require__) { + +"use strict"; +__webpack_require__.r(__webpack_exports__); + +// EXTERNAL MODULE: ./node_modules/is-any-array/src/index.js +var src = __webpack_require__(0); +var src_default = /*#__PURE__*/__webpack_require__.n(src); + +// CONCATENATED MODULE: ./node_modules/ml-array-max/lib-es6/index.js + + +/** + * Computes the maximum of the given values + * @param {Array} input + * @return {number} + */ + +function lib_es6_max(input) { + if (!src_default()(input)) { + throw new TypeError('input must be an array'); + } + + if (input.length === 0) { + throw new TypeError('input must not be empty'); + } + + var max = input[0]; + + for (var i = 1; i < input.length; i++) { + if (input[i] > max) max = input[i]; + } + + return max; +} + +/* harmony default export */ var lib_es6 = (lib_es6_max); + +// CONCATENATED MODULE: ./node_modules/ml-array-min/lib-es6/index.js + + +/** + * Computes the minimum of the given values + * @param {Array} input + * @return {number} + */ + +function lib_es6_min(input) { + if (!src_default()(input)) { + throw new TypeError('input must be an array'); + } + + if (input.length === 0) { + throw new TypeError('input must not be empty'); + } + + var min = input[0]; + + for (var i = 1; i < input.length; i++) { + if (input[i] < min) min = input[i]; + } + + return min; +} + +/* harmony default export */ var ml_array_min_lib_es6 = (lib_es6_min); + +// CONCATENATED MODULE: ./node_modules/ml-array-rescale/lib-es6/index.js + + + + +function rescale(input) { + var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; + + if (!src_default()(input)) { + throw new TypeError('input must be an array'); + } else if (input.length === 0) { + throw new TypeError('input must not be empty'); + } + + var output; + + if (options.output !== undefined) { + if (!src_default()(options.output)) { + throw new TypeError('output option must be an array if specified'); + } + + output = options.output; + } else { + output = new Array(input.length); + } + + var currentMin = ml_array_min_lib_es6(input); + var currentMax = lib_es6(input); + + if (currentMin === currentMax) { + throw new RangeError('minimum and maximum input values are equal. Cannot rescale a constant array'); + } + + var _options$min = options.min, + minValue = _options$min === void 0 ? options.autoMinMax ? currentMin : 0 : _options$min, + _options$max = options.max, + maxValue = _options$max === void 0 ? options.autoMinMax ? currentMax : 1 : _options$max; + + if (minValue >= maxValue) { + throw new RangeError('min option must be smaller than max option'); + } + + var factor = (maxValue - minValue) / (currentMax - currentMin); + + for (var i = 0; i < input.length; i++) { + output[i] = (input[i] - currentMin) * factor + minValue; + } + + return output; +} + +/* harmony default export */ var ml_array_rescale_lib_es6 = (rescale); + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/dc/lu.js + + +/** + * @class LuDecomposition + * @link https://github.com/lutzroeder/Mapack/blob/master/Source/LuDecomposition.cs + * @param {Matrix} matrix + */ +class lu_LuDecomposition { + constructor(matrix) { + matrix = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(matrix); + + var lu = matrix.clone(); + var rows = lu.rows; + var columns = lu.columns; + var pivotVector = new Array(rows); + var pivotSign = 1; + var i, j, k, p, s, t, v; + var LUcolj, kmax; + + for (i = 0; i < rows; i++) { + pivotVector[i] = i; + } + + LUcolj = new Array(rows); + + for (j = 0; j < columns; j++) { + for (i = 0; i < rows; i++) { + LUcolj[i] = lu.get(i, j); + } + + for (i = 0; i < rows; i++) { + kmax = Math.min(i, j); + s = 0; + for (k = 0; k < kmax; k++) { + s += lu.get(i, k) * LUcolj[k]; + } + LUcolj[i] -= s; + lu.set(i, j, LUcolj[i]); + } + + p = j; + for (i = j + 1; i < rows; i++) { + if (Math.abs(LUcolj[i]) > Math.abs(LUcolj[p])) { + p = i; + } + } + + if (p !== j) { + for (k = 0; k < columns; k++) { + t = lu.get(p, k); + lu.set(p, k, lu.get(j, k)); + lu.set(j, k, t); + } + + v = pivotVector[p]; + pivotVector[p] = pivotVector[j]; + pivotVector[j] = v; + + pivotSign = -pivotSign; + } + + if (j < rows && lu.get(j, j) !== 0) { + for (i = j + 1; i < rows; i++) { + lu.set(i, j, lu.get(i, j) / lu.get(j, j)); + } + } + } + + this.LU = lu; + this.pivotVector = pivotVector; + this.pivotSign = pivotSign; + } + + /** + * + * @return {boolean} + */ + isSingular() { + var data = this.LU; + var col = data.columns; + for (var j = 0; j < col; j++) { + if (data[j][j] === 0) { + return true; + } + } + return false; + } + + /** + * + * @param {Matrix} value + * @return {Matrix} + */ + solve(value) { + value = matrix_Matrix.checkMatrix(value); + + var lu = this.LU; + var rows = lu.rows; + + if (rows !== value.rows) { + throw new Error('Invalid matrix dimensions'); + } + if (this.isSingular()) { + throw new Error('LU matrix is singular'); + } + + var count = value.columns; + var X = value.subMatrixRow(this.pivotVector, 0, count - 1); + var columns = lu.columns; + var i, j, k; + + for (k = 0; k < columns; k++) { + for (i = k + 1; i < columns; i++) { + for (j = 0; j < count; j++) { + X[i][j] -= X[k][j] * lu[i][k]; + } + } + } + for (k = columns - 1; k >= 0; k--) { + for (j = 0; j < count; j++) { + X[k][j] /= lu[k][k]; + } + for (i = 0; i < k; i++) { + for (j = 0; j < count; j++) { + X[i][j] -= X[k][j] * lu[i][k]; + } + } + } + return X; + } + + /** + * + * @return {number} + */ + get determinant() { + var data = this.LU; + if (!data.isSquare()) { + throw new Error('Matrix must be square'); + } + var determinant = this.pivotSign; + var col = data.columns; + for (var j = 0; j < col; j++) { + determinant *= data[j][j]; + } + return determinant; + } + + /** + * + * @return {Matrix} + */ + get lowerTriangularMatrix() { + var data = this.LU; + var rows = data.rows; + var columns = data.columns; + var X = new matrix_Matrix(rows, columns); + for (var i = 0; i < rows; i++) { + for (var j = 0; j < columns; j++) { + if (i > j) { + X[i][j] = data[i][j]; + } else if (i === j) { + X[i][j] = 1; + } else { + X[i][j] = 0; + } + } + } + return X; + } + + /** + * + * @return {Matrix} + */ + get upperTriangularMatrix() { + var data = this.LU; + var rows = data.rows; + var columns = data.columns; + var X = new matrix_Matrix(rows, columns); + for (var i = 0; i < rows; i++) { + for (var j = 0; j < columns; j++) { + if (i <= j) { + X[i][j] = data[i][j]; + } else { + X[i][j] = 0; + } + } + } + return X; + } + + /** + * + * @return {Array} + */ + get pivotPermutationVector() { + return this.pivotVector.slice(); + } +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/dc/util.js +function hypotenuse(a, b) { + var r = 0; + if (Math.abs(a) > Math.abs(b)) { + r = b / a; + return Math.abs(a) * Math.sqrt(1 + r * r); + } + if (b !== 0) { + r = a / b; + return Math.abs(b) * Math.sqrt(1 + r * r); + } + return 0; +} + +function getFilled2DArray(rows, columns, value) { + var array = new Array(rows); + for (var i = 0; i < rows; i++) { + array[i] = new Array(columns); + for (var j = 0; j < columns; j++) { + array[i][j] = value; + } + } + return array; +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/dc/svd.js + + + + +/** + * @class SingularValueDecomposition + * @see https://github.com/accord-net/framework/blob/development/Sources/Accord.Math/Decompositions/SingularValueDecomposition.cs + * @param {Matrix} value + * @param {object} [options] + * @param {boolean} [options.computeLeftSingularVectors=true] + * @param {boolean} [options.computeRightSingularVectors=true] + * @param {boolean} [options.autoTranspose=false] + */ +class svd_SingularValueDecomposition { + constructor(value, options = {}) { + value = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(value); + + var m = value.rows; + var n = value.columns; + + const { + computeLeftSingularVectors = true, + computeRightSingularVectors = true, + autoTranspose = false + } = options; + + var wantu = Boolean(computeLeftSingularVectors); + var wantv = Boolean(computeRightSingularVectors); + + var swapped = false; + var a; + if (m < n) { + if (!autoTranspose) { + a = value.clone(); + // eslint-disable-next-line no-console + console.warn( + 'Computing SVD on a matrix with more columns than rows. Consider enabling autoTranspose' + ); + } else { + a = value.transpose(); + m = a.rows; + n = a.columns; + swapped = true; + var aux = wantu; + wantu = wantv; + wantv = aux; + } + } else { + a = value.clone(); + } + + var nu = Math.min(m, n); + var ni = Math.min(m + 1, n); + var s = new Array(ni); + var U = getFilled2DArray(m, nu, 0); + var V = getFilled2DArray(n, n, 0); + + var e = new Array(n); + var work = new Array(m); + + var si = new Array(ni); + for (let i = 0; i < ni; i++) si[i] = i; + + var nct = Math.min(m - 1, n); + var nrt = Math.max(0, Math.min(n - 2, m)); + var mrc = Math.max(nct, nrt); + + for (let k = 0; k < mrc; k++) { + if (k < nct) { + s[k] = 0; + for (let i = k; i < m; i++) { + s[k] = hypotenuse(s[k], a[i][k]); + } + if (s[k] !== 0) { + if (a[k][k] < 0) { + s[k] = -s[k]; + } + for (let i = k; i < m; i++) { + a[i][k] /= s[k]; + } + a[k][k] += 1; + } + s[k] = -s[k]; + } + + for (let j = k + 1; j < n; j++) { + if (k < nct && s[k] !== 0) { + let t = 0; + for (let i = k; i < m; i++) { + t += a[i][k] * a[i][j]; + } + t = -t / a[k][k]; + for (let i = k; i < m; i++) { + a[i][j] += t * a[i][k]; + } + } + e[j] = a[k][j]; + } + + if (wantu && k < nct) { + for (let i = k; i < m; i++) { + U[i][k] = a[i][k]; + } + } + + if (k < nrt) { + e[k] = 0; + for (let i = k + 1; i < n; i++) { + e[k] = hypotenuse(e[k], e[i]); + } + if (e[k] !== 0) { + if (e[k + 1] < 0) { + e[k] = 0 - e[k]; + } + for (let i = k + 1; i < n; i++) { + e[i] /= e[k]; + } + e[k + 1] += 1; + } + e[k] = -e[k]; + if (k + 1 < m && e[k] !== 0) { + for (let i = k + 1; i < m; i++) { + work[i] = 0; + } + for (let i = k + 1; i < m; i++) { + for (let j = k + 1; j < n; j++) { + work[i] += e[j] * a[i][j]; + } + } + for (let j = k + 1; j < n; j++) { + let t = -e[j] / e[k + 1]; + for (let i = k + 1; i < m; i++) { + a[i][j] += t * work[i]; + } + } + } + if (wantv) { + for (let i = k + 1; i < n; i++) { + V[i][k] = e[i]; + } + } + } + } + + let p = Math.min(n, m + 1); + if (nct < n) { + s[nct] = a[nct][nct]; + } + if (m < p) { + s[p - 1] = 0; + } + if (nrt + 1 < p) { + e[nrt] = a[nrt][p - 1]; + } + e[p - 1] = 0; + + if (wantu) { + for (let j = nct; j < nu; j++) { + for (let i = 0; i < m; i++) { + U[i][j] = 0; + } + U[j][j] = 1; + } + for (let k = nct - 1; k >= 0; k--) { + if (s[k] !== 0) { + for (let j = k + 1; j < nu; j++) { + let t = 0; + for (let i = k; i < m; i++) { + t += U[i][k] * U[i][j]; + } + t = -t / U[k][k]; + for (let i = k; i < m; i++) { + U[i][j] += t * U[i][k]; + } + } + for (let i = k; i < m; i++) { + U[i][k] = -U[i][k]; + } + U[k][k] = 1 + U[k][k]; + for (let i = 0; i < k - 1; i++) { + U[i][k] = 0; + } + } else { + for (let i = 0; i < m; i++) { + U[i][k] = 0; + } + U[k][k] = 1; + } + } + } + + if (wantv) { + for (let k = n - 1; k >= 0; k--) { + if (k < nrt && e[k] !== 0) { + for (let j = k + 1; j < n; j++) { + let t = 0; + for (let i = k + 1; i < n; i++) { + t += V[i][k] * V[i][j]; + } + t = -t / V[k + 1][k]; + for (let i = k + 1; i < n; i++) { + V[i][j] += t * V[i][k]; + } + } + } + for (let i = 0; i < n; i++) { + V[i][k] = 0; + } + V[k][k] = 1; + } + } + + var pp = p - 1; + var iter = 0; + var eps = Number.EPSILON; + while (p > 0) { + let k, kase; + for (k = p - 2; k >= -1; k--) { + if (k === -1) { + break; + } + const alpha = + Number.MIN_VALUE + eps * Math.abs(s[k] + Math.abs(s[k + 1])); + if (Math.abs(e[k]) <= alpha || Number.isNaN(e[k])) { + e[k] = 0; + break; + } + } + if (k === p - 2) { + kase = 4; + } else { + let ks; + for (ks = p - 1; ks >= k; ks--) { + if (ks === k) { + break; + } + let t = + (ks !== p ? Math.abs(e[ks]) : 0) + + (ks !== k + 1 ? Math.abs(e[ks - 1]) : 0); + if (Math.abs(s[ks]) <= eps * t) { + s[ks] = 0; + break; + } + } + if (ks === k) { + kase = 3; + } else if (ks === p - 1) { + kase = 1; + } else { + kase = 2; + k = ks; + } + } + + k++; + + switch (kase) { + case 1: { + let f = e[p - 2]; + e[p - 2] = 0; + for (let j = p - 2; j >= k; j--) { + let t = hypotenuse(s[j], f); + let cs = s[j] / t; + let sn = f / t; + s[j] = t; + if (j !== k) { + f = -sn * e[j - 1]; + e[j - 1] = cs * e[j - 1]; + } + if (wantv) { + for (let i = 0; i < n; i++) { + t = cs * V[i][j] + sn * V[i][p - 1]; + V[i][p - 1] = -sn * V[i][j] + cs * V[i][p - 1]; + V[i][j] = t; + } + } + } + break; + } + case 2: { + let f = e[k - 1]; + e[k - 1] = 0; + for (let j = k; j < p; j++) { + let t = hypotenuse(s[j], f); + let cs = s[j] / t; + let sn = f / t; + s[j] = t; + f = -sn * e[j]; + e[j] = cs * e[j]; + if (wantu) { + for (let i = 0; i < m; i++) { + t = cs * U[i][j] + sn * U[i][k - 1]; + U[i][k - 1] = -sn * U[i][j] + cs * U[i][k - 1]; + U[i][j] = t; + } + } + } + break; + } + case 3: { + const scale = Math.max( + Math.abs(s[p - 1]), + Math.abs(s[p - 2]), + Math.abs(e[p - 2]), + Math.abs(s[k]), + Math.abs(e[k]) + ); + const sp = s[p - 1] / scale; + const spm1 = s[p - 2] / scale; + const epm1 = e[p - 2] / scale; + const sk = s[k] / scale; + const ek = e[k] / scale; + const b = ((spm1 + sp) * (spm1 - sp) + epm1 * epm1) / 2; + const c = sp * epm1 * (sp * epm1); + let shift = 0; + if (b !== 0 || c !== 0) { + if (b < 0) { + shift = 0 - Math.sqrt(b * b + c); + } else { + shift = Math.sqrt(b * b + c); + } + shift = c / (b + shift); + } + let f = (sk + sp) * (sk - sp) + shift; + let g = sk * ek; + for (let j = k; j < p - 1; j++) { + let t = hypotenuse(f, g); + if (t === 0) t = Number.MIN_VALUE; + let cs = f / t; + let sn = g / t; + if (j !== k) { + e[j - 1] = t; + } + f = cs * s[j] + sn * e[j]; + e[j] = cs * e[j] - sn * s[j]; + g = sn * s[j + 1]; + s[j + 1] = cs * s[j + 1]; + if (wantv) { + for (let i = 0; i < n; i++) { + t = cs * V[i][j] + sn * V[i][j + 1]; + V[i][j + 1] = -sn * V[i][j] + cs * V[i][j + 1]; + V[i][j] = t; + } + } + t = hypotenuse(f, g); + if (t === 0) t = Number.MIN_VALUE; + cs = f / t; + sn = g / t; + s[j] = t; + f = cs * e[j] + sn * s[j + 1]; + s[j + 1] = -sn * e[j] + cs * s[j + 1]; + g = sn * e[j + 1]; + e[j + 1] = cs * e[j + 1]; + if (wantu && j < m - 1) { + for (let i = 0; i < m; i++) { + t = cs * U[i][j] + sn * U[i][j + 1]; + U[i][j + 1] = -sn * U[i][j] + cs * U[i][j + 1]; + U[i][j] = t; + } + } + } + e[p - 2] = f; + iter = iter + 1; + break; + } + case 4: { + if (s[k] <= 0) { + s[k] = s[k] < 0 ? -s[k] : 0; + if (wantv) { + for (let i = 0; i <= pp; i++) { + V[i][k] = -V[i][k]; + } + } + } + while (k < pp) { + if (s[k] >= s[k + 1]) { + break; + } + let t = s[k]; + s[k] = s[k + 1]; + s[k + 1] = t; + if (wantv && k < n - 1) { + for (let i = 0; i < n; i++) { + t = V[i][k + 1]; + V[i][k + 1] = V[i][k]; + V[i][k] = t; + } + } + if (wantu && k < m - 1) { + for (let i = 0; i < m; i++) { + t = U[i][k + 1]; + U[i][k + 1] = U[i][k]; + U[i][k] = t; + } + } + k++; + } + iter = 0; + p--; + break; + } + // no default + } + } + + if (swapped) { + var tmp = V; + V = U; + U = tmp; + } + + this.m = m; + this.n = n; + this.s = s; + this.U = U; + this.V = V; + } + + /** + * Solve a problem of least square (Ax=b) by using the SVD. Useful when A is singular. When A is not singular, it would be better to use qr.solve(value). + * Example : We search to approximate x, with A matrix shape m*n, x vector size n, b vector size m (m > n). We will use : + * var svd = SingularValueDecomposition(A); + * var x = svd.solve(b); + * @param {Matrix} value - Matrix 1D which is the vector b (in the equation Ax = b) + * @return {Matrix} - The vector x + */ + solve(value) { + var Y = value; + var e = this.threshold; + var scols = this.s.length; + var Ls = matrix_Matrix.zeros(scols, scols); + + for (let i = 0; i < scols; i++) { + if (Math.abs(this.s[i]) <= e) { + Ls[i][i] = 0; + } else { + Ls[i][i] = 1 / this.s[i]; + } + } + + var U = this.U; + var V = this.rightSingularVectors; + + var VL = V.mmul(Ls); + var vrows = V.rows; + var urows = U.length; + var VLU = matrix_Matrix.zeros(vrows, urows); + + for (let i = 0; i < vrows; i++) { + for (let j = 0; j < urows; j++) { + let sum = 0; + for (let k = 0; k < scols; k++) { + sum += VL[i][k] * U[j][k]; + } + VLU[i][j] = sum; + } + } + + return VLU.mmul(Y); + } + + /** + * + * @param {Array} value + * @return {Matrix} + */ + solveForDiagonal(value) { + return this.solve(matrix_Matrix.diag(value)); + } + + /** + * Get the inverse of the matrix. We compute the inverse of a matrix using SVD when this matrix is singular or ill-conditioned. Example : + * var svd = SingularValueDecomposition(A); + * var inverseA = svd.inverse(); + * @return {Matrix} - The approximation of the inverse of the matrix + */ + inverse() { + var V = this.V; + var e = this.threshold; + var vrows = V.length; + var vcols = V[0].length; + var X = new matrix_Matrix(vrows, this.s.length); + + for (let i = 0; i < vrows; i++) { + for (let j = 0; j < vcols; j++) { + if (Math.abs(this.s[j]) > e) { + X[i][j] = V[i][j] / this.s[j]; + } else { + X[i][j] = 0; + } + } + } + + var U = this.U; + + var urows = U.length; + var ucols = U[0].length; + var Y = new matrix_Matrix(vrows, urows); + + for (let i = 0; i < vrows; i++) { + for (let j = 0; j < urows; j++) { + let sum = 0; + for (let k = 0; k < ucols; k++) { + sum += X[i][k] * U[j][k]; + } + Y[i][j] = sum; + } + } + + return Y; + } + + /** + * + * @return {number} + */ + get condition() { + return this.s[0] / this.s[Math.min(this.m, this.n) - 1]; + } + + /** + * + * @return {number} + */ + get norm2() { + return this.s[0]; + } + + /** + * + * @return {number} + */ + get rank() { + var tol = Math.max(this.m, this.n) * this.s[0] * Number.EPSILON; + var r = 0; + var s = this.s; + for (var i = 0, ii = s.length; i < ii; i++) { + if (s[i] > tol) { + r++; + } + } + return r; + } + + /** + * + * @return {Array} + */ + get diagonal() { + return this.s; + } + + /** + * + * @return {number} + */ + get threshold() { + return Number.EPSILON / 2 * Math.max(this.m, this.n) * this.s[0]; + } + + /** + * + * @return {Matrix} + */ + get leftSingularVectors() { + if (!matrix_Matrix.isMatrix(this.U)) { + this.U = new matrix_Matrix(this.U); + } + return this.U; + } + + /** + * + * @return {Matrix} + */ + get rightSingularVectors() { + if (!matrix_Matrix.isMatrix(this.V)) { + this.V = new matrix_Matrix(this.V); + } + return this.V; + } + + /** + * + * @return {Matrix} + */ + get diagonalMatrix() { + return matrix_Matrix.diag(this.s); + } +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/util.js + + +/** + * @private + * Check that a row index is not out of bounds + * @param {Matrix} matrix + * @param {number} index + * @param {boolean} [outer] + */ +function checkRowIndex(matrix, index, outer) { + var max = outer ? matrix.rows : matrix.rows - 1; + if (index < 0 || index > max) { + throw new RangeError('Row index out of range'); + } +} + +/** + * @private + * Check that a column index is not out of bounds + * @param {Matrix} matrix + * @param {number} index + * @param {boolean} [outer] + */ +function checkColumnIndex(matrix, index, outer) { + var max = outer ? matrix.columns : matrix.columns - 1; + if (index < 0 || index > max) { + throw new RangeError('Column index out of range'); + } +} + +/** + * @private + * Check that the provided vector is an array with the right length + * @param {Matrix} matrix + * @param {Array|Matrix} vector + * @return {Array} + * @throws {RangeError} + */ +function checkRowVector(matrix, vector) { + if (vector.to1DArray) { + vector = vector.to1DArray(); + } + if (vector.length !== matrix.columns) { + throw new RangeError( + 'vector size must be the same as the number of columns' + ); + } + return vector; +} + +/** + * @private + * Check that the provided vector is an array with the right length + * @param {Matrix} matrix + * @param {Array|Matrix} vector + * @return {Array} + * @throws {RangeError} + */ +function checkColumnVector(matrix, vector) { + if (vector.to1DArray) { + vector = vector.to1DArray(); + } + if (vector.length !== matrix.rows) { + throw new RangeError('vector size must be the same as the number of rows'); + } + return vector; +} + +function checkIndices(matrix, rowIndices, columnIndices) { + return { + row: checkRowIndices(matrix, rowIndices), + column: checkColumnIndices(matrix, columnIndices) + }; +} + +function checkRowIndices(matrix, rowIndices) { + if (typeof rowIndices !== 'object') { + throw new TypeError('unexpected type for row indices'); + } + + var rowOut = rowIndices.some((r) => { + return r < 0 || r >= matrix.rows; + }); + + if (rowOut) { + throw new RangeError('row indices are out of range'); + } + + if (!Array.isArray(rowIndices)) rowIndices = Array.from(rowIndices); + + return rowIndices; +} + +function checkColumnIndices(matrix, columnIndices) { + if (typeof columnIndices !== 'object') { + throw new TypeError('unexpected type for column indices'); + } + + var columnOut = columnIndices.some((c) => { + return c < 0 || c >= matrix.columns; + }); + + if (columnOut) { + throw new RangeError('column indices are out of range'); + } + if (!Array.isArray(columnIndices)) columnIndices = Array.from(columnIndices); + + return columnIndices; +} + +function checkRange(matrix, startRow, endRow, startColumn, endColumn) { + if (arguments.length !== 5) { + throw new RangeError('expected 4 arguments'); + } + checkNumber('startRow', startRow); + checkNumber('endRow', endRow); + checkNumber('startColumn', startColumn); + checkNumber('endColumn', endColumn); + if ( + startRow > endRow || + startColumn > endColumn || + startRow < 0 || + startRow >= matrix.rows || + endRow < 0 || + endRow >= matrix.rows || + startColumn < 0 || + startColumn >= matrix.columns || + endColumn < 0 || + endColumn >= matrix.columns + ) { + throw new RangeError('Submatrix indices are out of range'); + } +} + +function getRange(from, to) { + var arr = new Array(to - from + 1); + for (var i = 0; i < arr.length; i++) { + arr[i] = from + i; + } + return arr; +} + +function sumByRow(matrix) { + var sum = matrix_Matrix.zeros(matrix.rows, 1); + for (var i = 0; i < matrix.rows; ++i) { + for (var j = 0; j < matrix.columns; ++j) { + sum.set(i, 0, sum.get(i, 0) + matrix.get(i, j)); + } + } + return sum; +} + +function sumByColumn(matrix) { + var sum = matrix_Matrix.zeros(1, matrix.columns); + for (var i = 0; i < matrix.rows; ++i) { + for (var j = 0; j < matrix.columns; ++j) { + sum.set(0, j, sum.get(0, j) + matrix.get(i, j)); + } + } + return sum; +} + +function sumAll(matrix) { + var v = 0; + for (var i = 0; i < matrix.rows; i++) { + for (var j = 0; j < matrix.columns; j++) { + v += matrix.get(i, j); + } + } + return v; +} + +function checkNumber(name, value) { + if (typeof value !== 'number') { + throw new TypeError(`${name} must be a number`); + } +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/base.js + + + +class base_BaseView extends AbstractMatrix() { + constructor(matrix, rows, columns) { + super(); + this.matrix = matrix; + this.rows = rows; + this.columns = columns; + } + + static get [Symbol.species]() { + return matrix_Matrix; + } +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/transpose.js + + +class transpose_MatrixTransposeView extends base_BaseView { + constructor(matrix) { + super(matrix, matrix.columns, matrix.rows); + } + + set(rowIndex, columnIndex, value) { + this.matrix.set(columnIndex, rowIndex, value); + return this; + } + + get(rowIndex, columnIndex) { + return this.matrix.get(columnIndex, rowIndex); + } +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/row.js + + +class row_MatrixRowView extends base_BaseView { + constructor(matrix, row) { + super(matrix, 1, matrix.columns); + this.row = row; + } + + set(rowIndex, columnIndex, value) { + this.matrix.set(this.row, columnIndex, value); + return this; + } + + get(rowIndex, columnIndex) { + return this.matrix.get(this.row, columnIndex); + } +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/sub.js + + + + +class sub_MatrixSubView extends base_BaseView { + constructor(matrix, startRow, endRow, startColumn, endColumn) { + checkRange(matrix, startRow, endRow, startColumn, endColumn); + super(matrix, endRow - startRow + 1, endColumn - startColumn + 1); + this.startRow = startRow; + this.startColumn = startColumn; + } + + set(rowIndex, columnIndex, value) { + this.matrix.set( + this.startRow + rowIndex, + this.startColumn + columnIndex, + value + ); + return this; + } + + get(rowIndex, columnIndex) { + return this.matrix.get( + this.startRow + rowIndex, + this.startColumn + columnIndex + ); + } +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/selection.js + + + + +class selection_MatrixSelectionView extends base_BaseView { + constructor(matrix, rowIndices, columnIndices) { + var indices = checkIndices(matrix, rowIndices, columnIndices); + super(matrix, indices.row.length, indices.column.length); + this.rowIndices = indices.row; + this.columnIndices = indices.column; + } + + set(rowIndex, columnIndex, value) { + this.matrix.set( + this.rowIndices[rowIndex], + this.columnIndices[columnIndex], + value + ); + return this; + } + + get(rowIndex, columnIndex) { + return this.matrix.get( + this.rowIndices[rowIndex], + this.columnIndices[columnIndex] + ); + } +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/rowSelection.js + + + + +class rowSelection_MatrixRowSelectionView extends base_BaseView { + constructor(matrix, rowIndices) { + rowIndices = checkRowIndices(matrix, rowIndices); + super(matrix, rowIndices.length, matrix.columns); + this.rowIndices = rowIndices; + } + + set(rowIndex, columnIndex, value) { + this.matrix.set(this.rowIndices[rowIndex], columnIndex, value); + return this; + } + + get(rowIndex, columnIndex) { + return this.matrix.get(this.rowIndices[rowIndex], columnIndex); + } +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/columnSelection.js + + + + +class columnSelection_MatrixColumnSelectionView extends base_BaseView { + constructor(matrix, columnIndices) { + columnIndices = checkColumnIndices(matrix, columnIndices); + super(matrix, matrix.rows, columnIndices.length); + this.columnIndices = columnIndices; + } + + set(rowIndex, columnIndex, value) { + this.matrix.set(rowIndex, this.columnIndices[columnIndex], value); + return this; + } + + get(rowIndex, columnIndex) { + return this.matrix.get(rowIndex, this.columnIndices[columnIndex]); + } +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/column.js + + +class column_MatrixColumnView extends base_BaseView { + constructor(matrix, column) { + super(matrix, matrix.rows, 1); + this.column = column; + } + + set(rowIndex, columnIndex, value) { + this.matrix.set(rowIndex, this.column, value); + return this; + } + + get(rowIndex) { + return this.matrix.get(rowIndex, this.column); + } +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/flipRow.js + + +class flipRow_MatrixFlipRowView extends base_BaseView { + constructor(matrix) { + super(matrix, matrix.rows, matrix.columns); + } + + set(rowIndex, columnIndex, value) { + this.matrix.set(this.rows - rowIndex - 1, columnIndex, value); + return this; + } + + get(rowIndex, columnIndex) { + return this.matrix.get(this.rows - rowIndex - 1, columnIndex); + } +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/flipColumn.js + + +class flipColumn_MatrixFlipColumnView extends base_BaseView { + constructor(matrix) { + super(matrix, matrix.rows, matrix.columns); + } + + set(rowIndex, columnIndex, value) { + this.matrix.set(rowIndex, this.columns - columnIndex - 1, value); + return this; + } + + get(rowIndex, columnIndex) { + return this.matrix.get(rowIndex, this.columns - columnIndex - 1); + } +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/abstractMatrix.js + + + + + + + + + + + + + + + +function AbstractMatrix(superCtor) { + if (superCtor === undefined) superCtor = Object; + + /** + * Real matrix + * @class Matrix + * @param {number|Array|Matrix} nRows - Number of rows of the new matrix, + * 2D array containing the data or Matrix instance to clone + * @param {number} [nColumns] - Number of columns of the new matrix + */ + class Matrix extends superCtor { + static get [Symbol.species]() { + return this; + } + + /** + * Constructs a Matrix with the chosen dimensions from a 1D array + * @param {number} newRows - Number of rows + * @param {number} newColumns - Number of columns + * @param {Array} newData - A 1D array containing data for the matrix + * @return {Matrix} - The new matrix + */ + static from1DArray(newRows, newColumns, newData) { + var length = newRows * newColumns; + if (length !== newData.length) { + throw new RangeError('Data length does not match given dimensions'); + } + var newMatrix = new this(newRows, newColumns); + for (var row = 0; row < newRows; row++) { + for (var column = 0; column < newColumns; column++) { + newMatrix.set(row, column, newData[row * newColumns + column]); + } + } + return newMatrix; + } + + /** + * Creates a row vector, a matrix with only one row. + * @param {Array} newData - A 1D array containing data for the vector + * @return {Matrix} - The new matrix + */ + static rowVector(newData) { + var vector = new this(1, newData.length); + for (var i = 0; i < newData.length; i++) { + vector.set(0, i, newData[i]); + } + return vector; + } + + /** + * Creates a column vector, a matrix with only one column. + * @param {Array} newData - A 1D array containing data for the vector + * @return {Matrix} - The new matrix + */ + static columnVector(newData) { + var vector = new this(newData.length, 1); + for (var i = 0; i < newData.length; i++) { + vector.set(i, 0, newData[i]); + } + return vector; + } + + /** + * Creates an empty matrix with the given dimensions. Values will be undefined. Same as using new Matrix(rows, columns). + * @param {number} rows - Number of rows + * @param {number} columns - Number of columns + * @return {Matrix} - The new matrix + */ + static empty(rows, columns) { + return new this(rows, columns); + } + + /** + * Creates a matrix with the given dimensions. Values will be set to zero. + * @param {number} rows - Number of rows + * @param {number} columns - Number of columns + * @return {Matrix} - The new matrix + */ + static zeros(rows, columns) { + return this.empty(rows, columns).fill(0); + } + + /** + * Creates a matrix with the given dimensions. Values will be set to one. + * @param {number} rows - Number of rows + * @param {number} columns - Number of columns + * @return {Matrix} - The new matrix + */ + static ones(rows, columns) { + return this.empty(rows, columns).fill(1); + } + + /** + * Creates a matrix with the given dimensions. Values will be randomly set. + * @param {number} rows - Number of rows + * @param {number} columns - Number of columns + * @param {function} [rng=Math.random] - Random number generator + * @return {Matrix} The new matrix + */ + static rand(rows, columns, rng) { + if (rng === undefined) rng = Math.random; + var matrix = this.empty(rows, columns); + for (var i = 0; i < rows; i++) { + for (var j = 0; j < columns; j++) { + matrix.set(i, j, rng()); + } + } + return matrix; + } + + /** + * Creates a matrix with the given dimensions. Values will be random integers. + * @param {number} rows - Number of rows + * @param {number} columns - Number of columns + * @param {number} [maxValue=1000] - Maximum value + * @param {function} [rng=Math.random] - Random number generator + * @return {Matrix} The new matrix + */ + static randInt(rows, columns, maxValue, rng) { + if (maxValue === undefined) maxValue = 1000; + if (rng === undefined) rng = Math.random; + var matrix = this.empty(rows, columns); + for (var i = 0; i < rows; i++) { + for (var j = 0; j < columns; j++) { + var value = Math.floor(rng() * maxValue); + matrix.set(i, j, value); + } + } + return matrix; + } + + /** + * Creates an identity matrix with the given dimension. Values of the diagonal will be 1 and others will be 0. + * @param {number} rows - Number of rows + * @param {number} [columns=rows] - Number of columns + * @param {number} [value=1] - Value to fill the diagonal with + * @return {Matrix} - The new identity matrix + */ + static eye(rows, columns, value) { + if (columns === undefined) columns = rows; + if (value === undefined) value = 1; + var min = Math.min(rows, columns); + var matrix = this.zeros(rows, columns); + for (var i = 0; i < min; i++) { + matrix.set(i, i, value); + } + return matrix; + } + + /** + * Creates a diagonal matrix based on the given array. + * @param {Array} data - Array containing the data for the diagonal + * @param {number} [rows] - Number of rows (Default: data.length) + * @param {number} [columns] - Number of columns (Default: rows) + * @return {Matrix} - The new diagonal matrix + */ + static diag(data, rows, columns) { + var l = data.length; + if (rows === undefined) rows = l; + if (columns === undefined) columns = rows; + var min = Math.min(l, rows, columns); + var matrix = this.zeros(rows, columns); + for (var i = 0; i < min; i++) { + matrix.set(i, i, data[i]); + } + return matrix; + } + + /** + * Returns a matrix whose elements are the minimum between matrix1 and matrix2 + * @param {Matrix} matrix1 + * @param {Matrix} matrix2 + * @return {Matrix} + */ + static min(matrix1, matrix2) { + matrix1 = this.checkMatrix(matrix1); + matrix2 = this.checkMatrix(matrix2); + var rows = matrix1.rows; + var columns = matrix1.columns; + var result = new this(rows, columns); + for (var i = 0; i < rows; i++) { + for (var j = 0; j < columns; j++) { + result.set(i, j, Math.min(matrix1.get(i, j), matrix2.get(i, j))); + } + } + return result; + } + + /** + * Returns a matrix whose elements are the maximum between matrix1 and matrix2 + * @param {Matrix} matrix1 + * @param {Matrix} matrix2 + * @return {Matrix} + */ + static max(matrix1, matrix2) { + matrix1 = this.checkMatrix(matrix1); + matrix2 = this.checkMatrix(matrix2); + var rows = matrix1.rows; + var columns = matrix1.columns; + var result = new this(rows, columns); + for (var i = 0; i < rows; i++) { + for (var j = 0; j < columns; j++) { + result.set(i, j, Math.max(matrix1.get(i, j), matrix2.get(i, j))); + } + } + return result; + } + + /** + * Check that the provided value is a Matrix and tries to instantiate one if not + * @param {*} value - The value to check + * @return {Matrix} + */ + static checkMatrix(value) { + return Matrix.isMatrix(value) ? value : new this(value); + } + + /** + * Returns true if the argument is a Matrix, false otherwise + * @param {*} value - The value to check + * @return {boolean} + */ + static isMatrix(value) { + return (value != null) && (value.klass === 'Matrix'); + } + + /** + * @prop {number} size - The number of elements in the matrix. + */ + get size() { + return this.rows * this.columns; + } + + /** + * Applies a callback for each element of the matrix. The function is called in the matrix (this) context. + * @param {function} callback - Function that will be called with two parameters : i (row) and j (column) + * @return {Matrix} this + */ + apply(callback) { + if (typeof callback !== 'function') { + throw new TypeError('callback must be a function'); + } + var ii = this.rows; + var jj = this.columns; + for (var i = 0; i < ii; i++) { + for (var j = 0; j < jj; j++) { + callback.call(this, i, j); + } + } + return this; + } + + /** + * Returns a new 1D array filled row by row with the matrix values + * @return {Array} + */ + to1DArray() { + var array = new Array(this.size); + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + array[i * this.columns + j] = this.get(i, j); + } + } + return array; + } + + /** + * Returns a 2D array containing a copy of the data + * @return {Array} + */ + to2DArray() { + var copy = new Array(this.rows); + for (var i = 0; i < this.rows; i++) { + copy[i] = new Array(this.columns); + for (var j = 0; j < this.columns; j++) { + copy[i][j] = this.get(i, j); + } + } + return copy; + } + + /** + * @return {boolean} true if the matrix has one row + */ + isRowVector() { + return this.rows === 1; + } + + /** + * @return {boolean} true if the matrix has one column + */ + isColumnVector() { + return this.columns === 1; + } + + /** + * @return {boolean} true if the matrix has one row or one column + */ + isVector() { + return (this.rows === 1) || (this.columns === 1); + } + + /** + * @return {boolean} true if the matrix has the same number of rows and columns + */ + isSquare() { + return this.rows === this.columns; + } + + /** + * @return {boolean} true if the matrix is square and has the same values on both sides of the diagonal + */ + isSymmetric() { + if (this.isSquare()) { + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j <= i; j++) { + if (this.get(i, j) !== this.get(j, i)) { + return false; + } + } + } + return true; + } + return false; + } + + /** + * Sets a given element of the matrix. mat.set(3,4,1) is equivalent to mat[3][4]=1 + * @abstract + * @param {number} rowIndex - Index of the row + * @param {number} columnIndex - Index of the column + * @param {number} value - The new value for the element + * @return {Matrix} this + */ + set(rowIndex, columnIndex, value) { // eslint-disable-line no-unused-vars + throw new Error('set method is unimplemented'); + } + + /** + * Returns the given element of the matrix. mat.get(3,4) is equivalent to matrix[3][4] + * @abstract + * @param {number} rowIndex - Index of the row + * @param {number} columnIndex - Index of the column + * @return {number} + */ + get(rowIndex, columnIndex) { // eslint-disable-line no-unused-vars + throw new Error('get method is unimplemented'); + } + + /** + * Creates a new matrix that is a repetition of the current matrix. New matrix has rowRep times the number of + * rows of the matrix, and colRep times the number of columns of the matrix + * @param {number} rowRep - Number of times the rows should be repeated + * @param {number} colRep - Number of times the columns should be re + * @return {Matrix} + * @example + * var matrix = new Matrix([[1,2]]); + * matrix.repeat(2); // [[1,2],[1,2]] + */ + repeat(rowRep, colRep) { + rowRep = rowRep || 1; + colRep = colRep || 1; + var matrix = new this.constructor[Symbol.species](this.rows * rowRep, this.columns * colRep); + for (var i = 0; i < rowRep; i++) { + for (var j = 0; j < colRep; j++) { + matrix.setSubMatrix(this, this.rows * i, this.columns * j); + } + } + return matrix; + } + + /** + * Fills the matrix with a given value. All elements will be set to this value. + * @param {number} value - New value + * @return {Matrix} this + */ + fill(value) { + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + this.set(i, j, value); + } + } + return this; + } + + /** + * Negates the matrix. All elements will be multiplied by (-1) + * @return {Matrix} this + */ + neg() { + return this.mulS(-1); + } + + /** + * Returns a new array from the given row index + * @param {number} index - Row index + * @return {Array} + */ + getRow(index) { + checkRowIndex(this, index); + var row = new Array(this.columns); + for (var i = 0; i < this.columns; i++) { + row[i] = this.get(index, i); + } + return row; + } + + /** + * Returns a new row vector from the given row index + * @param {number} index - Row index + * @return {Matrix} + */ + getRowVector(index) { + return this.constructor.rowVector(this.getRow(index)); + } + + /** + * Sets a row at the given index + * @param {number} index - Row index + * @param {Array|Matrix} array - Array or vector + * @return {Matrix} this + */ + setRow(index, array) { + checkRowIndex(this, index); + array = checkRowVector(this, array); + for (var i = 0; i < this.columns; i++) { + this.set(index, i, array[i]); + } + return this; + } + + /** + * Swaps two rows + * @param {number} row1 - First row index + * @param {number} row2 - Second row index + * @return {Matrix} this + */ + swapRows(row1, row2) { + checkRowIndex(this, row1); + checkRowIndex(this, row2); + for (var i = 0; i < this.columns; i++) { + var temp = this.get(row1, i); + this.set(row1, i, this.get(row2, i)); + this.set(row2, i, temp); + } + return this; + } + + /** + * Returns a new array from the given column index + * @param {number} index - Column index + * @return {Array} + */ + getColumn(index) { + checkColumnIndex(this, index); + var column = new Array(this.rows); + for (var i = 0; i < this.rows; i++) { + column[i] = this.get(i, index); + } + return column; + } + + /** + * Returns a new column vector from the given column index + * @param {number} index - Column index + * @return {Matrix} + */ + getColumnVector(index) { + return this.constructor.columnVector(this.getColumn(index)); + } + + /** + * Sets a column at the given index + * @param {number} index - Column index + * @param {Array|Matrix} array - Array or vector + * @return {Matrix} this + */ + setColumn(index, array) { + checkColumnIndex(this, index); + array = checkColumnVector(this, array); + for (var i = 0; i < this.rows; i++) { + this.set(i, index, array[i]); + } + return this; + } + + /** + * Swaps two columns + * @param {number} column1 - First column index + * @param {number} column2 - Second column index + * @return {Matrix} this + */ + swapColumns(column1, column2) { + checkColumnIndex(this, column1); + checkColumnIndex(this, column2); + for (var i = 0; i < this.rows; i++) { + var temp = this.get(i, column1); + this.set(i, column1, this.get(i, column2)); + this.set(i, column2, temp); + } + return this; + } + + /** + * Adds the values of a vector to each row + * @param {Array|Matrix} vector - Array or vector + * @return {Matrix} this + */ + addRowVector(vector) { + vector = checkRowVector(this, vector); + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) + vector[j]); + } + } + return this; + } + + /** + * Subtracts the values of a vector from each row + * @param {Array|Matrix} vector - Array or vector + * @return {Matrix} this + */ + subRowVector(vector) { + vector = checkRowVector(this, vector); + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) - vector[j]); + } + } + return this; + } + + /** + * Multiplies the values of a vector with each row + * @param {Array|Matrix} vector - Array or vector + * @return {Matrix} this + */ + mulRowVector(vector) { + vector = checkRowVector(this, vector); + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) * vector[j]); + } + } + return this; + } + + /** + * Divides the values of each row by those of a vector + * @param {Array|Matrix} vector - Array or vector + * @return {Matrix} this + */ + divRowVector(vector) { + vector = checkRowVector(this, vector); + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) / vector[j]); + } + } + return this; + } + + /** + * Adds the values of a vector to each column + * @param {Array|Matrix} vector - Array or vector + * @return {Matrix} this + */ + addColumnVector(vector) { + vector = checkColumnVector(this, vector); + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) + vector[i]); + } + } + return this; + } + + /** + * Subtracts the values of a vector from each column + * @param {Array|Matrix} vector - Array or vector + * @return {Matrix} this + */ + subColumnVector(vector) { + vector = checkColumnVector(this, vector); + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) - vector[i]); + } + } + return this; + } + + /** + * Multiplies the values of a vector with each column + * @param {Array|Matrix} vector - Array or vector + * @return {Matrix} this + */ + mulColumnVector(vector) { + vector = checkColumnVector(this, vector); + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) * vector[i]); + } + } + return this; + } + + /** + * Divides the values of each column by those of a vector + * @param {Array|Matrix} vector - Array or vector + * @return {Matrix} this + */ + divColumnVector(vector) { + vector = checkColumnVector(this, vector); + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) / vector[i]); + } + } + return this; + } + + /** + * Multiplies the values of a row with a scalar + * @param {number} index - Row index + * @param {number} value + * @return {Matrix} this + */ + mulRow(index, value) { + checkRowIndex(this, index); + for (var i = 0; i < this.columns; i++) { + this.set(index, i, this.get(index, i) * value); + } + return this; + } + + /** + * Multiplies the values of a column with a scalar + * @param {number} index - Column index + * @param {number} value + * @return {Matrix} this + */ + mulColumn(index, value) { + checkColumnIndex(this, index); + for (var i = 0; i < this.rows; i++) { + this.set(i, index, this.get(i, index) * value); + } + return this; + } + + /** + * Returns the maximum value of the matrix + * @return {number} + */ + max() { + var v = this.get(0, 0); + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + if (this.get(i, j) > v) { + v = this.get(i, j); + } + } + } + return v; + } + + /** + * Returns the index of the maximum value + * @return {Array} + */ + maxIndex() { + var v = this.get(0, 0); + var idx = [0, 0]; + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + if (this.get(i, j) > v) { + v = this.get(i, j); + idx[0] = i; + idx[1] = j; + } + } + } + return idx; + } + + /** + * Returns the minimum value of the matrix + * @return {number} + */ + min() { + var v = this.get(0, 0); + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + if (this.get(i, j) < v) { + v = this.get(i, j); + } + } + } + return v; + } + + /** + * Returns the index of the minimum value + * @return {Array} + */ + minIndex() { + var v = this.get(0, 0); + var idx = [0, 0]; + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + if (this.get(i, j) < v) { + v = this.get(i, j); + idx[0] = i; + idx[1] = j; + } + } + } + return idx; + } + + /** + * Returns the maximum value of one row + * @param {number} row - Row index + * @return {number} + */ + maxRow(row) { + checkRowIndex(this, row); + var v = this.get(row, 0); + for (var i = 1; i < this.columns; i++) { + if (this.get(row, i) > v) { + v = this.get(row, i); + } + } + return v; + } + + /** + * Returns the index of the maximum value of one row + * @param {number} row - Row index + * @return {Array} + */ + maxRowIndex(row) { + checkRowIndex(this, row); + var v = this.get(row, 0); + var idx = [row, 0]; + for (var i = 1; i < this.columns; i++) { + if (this.get(row, i) > v) { + v = this.get(row, i); + idx[1] = i; + } + } + return idx; + } + + /** + * Returns the minimum value of one row + * @param {number} row - Row index + * @return {number} + */ + minRow(row) { + checkRowIndex(this, row); + var v = this.get(row, 0); + for (var i = 1; i < this.columns; i++) { + if (this.get(row, i) < v) { + v = this.get(row, i); + } + } + return v; + } + + /** + * Returns the index of the maximum value of one row + * @param {number} row - Row index + * @return {Array} + */ + minRowIndex(row) { + checkRowIndex(this, row); + var v = this.get(row, 0); + var idx = [row, 0]; + for (var i = 1; i < this.columns; i++) { + if (this.get(row, i) < v) { + v = this.get(row, i); + idx[1] = i; + } + } + return idx; + } + + /** + * Returns the maximum value of one column + * @param {number} column - Column index + * @return {number} + */ + maxColumn(column) { + checkColumnIndex(this, column); + var v = this.get(0, column); + for (var i = 1; i < this.rows; i++) { + if (this.get(i, column) > v) { + v = this.get(i, column); + } + } + return v; + } + + /** + * Returns the index of the maximum value of one column + * @param {number} column - Column index + * @return {Array} + */ + maxColumnIndex(column) { + checkColumnIndex(this, column); + var v = this.get(0, column); + var idx = [0, column]; + for (var i = 1; i < this.rows; i++) { + if (this.get(i, column) > v) { + v = this.get(i, column); + idx[0] = i; + } + } + return idx; + } + + /** + * Returns the minimum value of one column + * @param {number} column - Column index + * @return {number} + */ + minColumn(column) { + checkColumnIndex(this, column); + var v = this.get(0, column); + for (var i = 1; i < this.rows; i++) { + if (this.get(i, column) < v) { + v = this.get(i, column); + } + } + return v; + } + + /** + * Returns the index of the minimum value of one column + * @param {number} column - Column index + * @return {Array} + */ + minColumnIndex(column) { + checkColumnIndex(this, column); + var v = this.get(0, column); + var idx = [0, column]; + for (var i = 1; i < this.rows; i++) { + if (this.get(i, column) < v) { + v = this.get(i, column); + idx[0] = i; + } + } + return idx; + } + + /** + * Returns an array containing the diagonal values of the matrix + * @return {Array} + */ + diag() { + var min = Math.min(this.rows, this.columns); + var diag = new Array(min); + for (var i = 0; i < min; i++) { + diag[i] = this.get(i, i); + } + return diag; + } + + /** + * Returns the sum by the argument given, if no argument given, + * it returns the sum of all elements of the matrix. + * @param {string} by - sum by 'row' or 'column'. + * @return {Matrix|number} + */ + sum(by) { + switch (by) { + case 'row': + return sumByRow(this); + case 'column': + return sumByColumn(this); + default: + return sumAll(this); + } + } + + /** + * Returns the mean of all elements of the matrix + * @return {number} + */ + mean() { + return this.sum() / this.size; + } + + /** + * Returns the product of all elements of the matrix + * @return {number} + */ + prod() { + var prod = 1; + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + prod *= this.get(i, j); + } + } + return prod; + } + + /** + * Returns the norm of a matrix. + * @param {string} type - "frobenius" (default) or "max" return resp. the Frobenius norm and the max norm. + * @return {number} + */ + norm(type = 'frobenius') { + var result = 0; + if (type === 'max') { + return this.max(); + } else if (type === 'frobenius') { + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + result = result + this.get(i, j) * this.get(i, j); + } + } + return Math.sqrt(result); + } else { + throw new RangeError(`unknown norm type: ${type}`); + } + } + + /** + * Computes the cumulative sum of the matrix elements (in place, row by row) + * @return {Matrix} this + */ + cumulativeSum() { + var sum = 0; + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + sum += this.get(i, j); + this.set(i, j, sum); + } + } + return this; + } + + /** + * Computes the dot (scalar) product between the matrix and another + * @param {Matrix} vector2 vector + * @return {number} + */ + dot(vector2) { + if (Matrix.isMatrix(vector2)) vector2 = vector2.to1DArray(); + var vector1 = this.to1DArray(); + if (vector1.length !== vector2.length) { + throw new RangeError('vectors do not have the same size'); + } + var dot = 0; + for (var i = 0; i < vector1.length; i++) { + dot += vector1[i] * vector2[i]; + } + return dot; + } + + /** + * Returns the matrix product between this and other + * @param {Matrix} other + * @return {Matrix} + */ + mmul(other) { + other = this.constructor.checkMatrix(other); + if (this.columns !== other.rows) { + // eslint-disable-next-line no-console + console.warn('Number of columns of left matrix are not equal to number of rows of right matrix.'); + } + + var m = this.rows; + var n = this.columns; + var p = other.columns; + + var result = new this.constructor[Symbol.species](m, p); + + var Bcolj = new Array(n); + for (var j = 0; j < p; j++) { + for (var k = 0; k < n; k++) { + Bcolj[k] = other.get(k, j); + } + + for (var i = 0; i < m; i++) { + var s = 0; + for (k = 0; k < n; k++) { + s += this.get(i, k) * Bcolj[k]; + } + + result.set(i, j, s); + } + } + return result; + } + + strassen2x2(other) { + var result = new this.constructor[Symbol.species](2, 2); + const a11 = this.get(0, 0); + const b11 = other.get(0, 0); + const a12 = this.get(0, 1); + const b12 = other.get(0, 1); + const a21 = this.get(1, 0); + const b21 = other.get(1, 0); + const a22 = this.get(1, 1); + const b22 = other.get(1, 1); + + // Compute intermediate values. + const m1 = (a11 + a22) * (b11 + b22); + const m2 = (a21 + a22) * b11; + const m3 = a11 * (b12 - b22); + const m4 = a22 * (b21 - b11); + const m5 = (a11 + a12) * b22; + const m6 = (a21 - a11) * (b11 + b12); + const m7 = (a12 - a22) * (b21 + b22); + + // Combine intermediate values into the output. + const c00 = m1 + m4 - m5 + m7; + const c01 = m3 + m5; + const c10 = m2 + m4; + const c11 = m1 - m2 + m3 + m6; + + result.set(0, 0, c00); + result.set(0, 1, c01); + result.set(1, 0, c10); + result.set(1, 1, c11); + return result; + } + + strassen3x3(other) { + var result = new this.constructor[Symbol.species](3, 3); + + const a00 = this.get(0, 0); + const a01 = this.get(0, 1); + const a02 = this.get(0, 2); + const a10 = this.get(1, 0); + const a11 = this.get(1, 1); + const a12 = this.get(1, 2); + const a20 = this.get(2, 0); + const a21 = this.get(2, 1); + const a22 = this.get(2, 2); + + const b00 = other.get(0, 0); + const b01 = other.get(0, 1); + const b02 = other.get(0, 2); + const b10 = other.get(1, 0); + const b11 = other.get(1, 1); + const b12 = other.get(1, 2); + const b20 = other.get(2, 0); + const b21 = other.get(2, 1); + const b22 = other.get(2, 2); + + const m1 = (a00 + a01 + a02 - a10 - a11 - a21 - a22) * b11; + const m2 = (a00 - a10) * (-b01 + b11); + const m3 = a11 * (-b00 + b01 + b10 - b11 - b12 - b20 + b22); + const m4 = (-a00 + a10 + a11) * (b00 - b01 + b11); + const m5 = (a10 + a11) * (-b00 + b01); + const m6 = a00 * b00; + const m7 = (-a00 + a20 + a21) * (b00 - b02 + b12); + const m8 = (-a00 + a20) * (b02 - b12); + const m9 = (a20 + a21) * (-b00 + b02); + const m10 = (a00 + a01 + a02 - a11 - a12 - a20 - a21) * b12; + const m11 = a21 * (-b00 + b02 + b10 - b11 - b12 - b20 + b21); + const m12 = (-a02 + a21 + a22) * (b11 + b20 - b21); + const m13 = (a02 - a22) * (b11 - b21); + const m14 = a02 * b20; + const m15 = (a21 + a22) * (-b20 + b21); + const m16 = (-a02 + a11 + a12) * (b12 + b20 - b22); + const m17 = (a02 - a12) * (b12 - b22); + const m18 = (a11 + a12) * (-b20 + b22); + const m19 = a01 * b10; + const m20 = a12 * b21; + const m21 = a10 * b02; + const m22 = a20 * b01; + const m23 = a22 * b22; + + const c00 = m6 + m14 + m19; + const c01 = m1 + m4 + m5 + m6 + m12 + m14 + m15; + const c02 = m6 + m7 + m9 + m10 + m14 + m16 + m18; + const c10 = m2 + m3 + m4 + m6 + m14 + m16 + m17; + const c11 = m2 + m4 + m5 + m6 + m20; + const c12 = m14 + m16 + m17 + m18 + m21; + const c20 = m6 + m7 + m8 + m11 + m12 + m13 + m14; + const c21 = m12 + m13 + m14 + m15 + m22; + const c22 = m6 + m7 + m8 + m9 + m23; + + result.set(0, 0, c00); + result.set(0, 1, c01); + result.set(0, 2, c02); + result.set(1, 0, c10); + result.set(1, 1, c11); + result.set(1, 2, c12); + result.set(2, 0, c20); + result.set(2, 1, c21); + result.set(2, 2, c22); + return result; + } + + /** + * Returns the matrix product between x and y. More efficient than mmul(other) only when we multiply squared matrix and when the size of the matrix is > 1000. + * @param {Matrix} y + * @return {Matrix} + */ + mmulStrassen(y) { + var x = this.clone(); + var r1 = x.rows; + var c1 = x.columns; + var r2 = y.rows; + var c2 = y.columns; + if (c1 !== r2) { + // eslint-disable-next-line no-console + console.warn(`Multiplying ${r1} x ${c1} and ${r2} x ${c2} matrix: dimensions do not match.`); + } + + // Put a matrix into the top left of a matrix of zeros. + // `rows` and `cols` are the dimensions of the output matrix. + function embed(mat, rows, cols) { + var r = mat.rows; + var c = mat.columns; + if ((r === rows) && (c === cols)) { + return mat; + } else { + var resultat = Matrix.zeros(rows, cols); + resultat = resultat.setSubMatrix(mat, 0, 0); + return resultat; + } + } + + + // Make sure both matrices are the same size. + // This is exclusively for simplicity: + // this algorithm can be implemented with matrices of different sizes. + + var r = Math.max(r1, r2); + var c = Math.max(c1, c2); + x = embed(x, r, c); + y = embed(y, r, c); + + // Our recursive multiplication function. + function blockMult(a, b, rows, cols) { + // For small matrices, resort to naive multiplication. + if (rows <= 512 || cols <= 512) { + return a.mmul(b); // a is equivalent to this + } + + // Apply dynamic padding. + if ((rows % 2 === 1) && (cols % 2 === 1)) { + a = embed(a, rows + 1, cols + 1); + b = embed(b, rows + 1, cols + 1); + } else if (rows % 2 === 1) { + a = embed(a, rows + 1, cols); + b = embed(b, rows + 1, cols); + } else if (cols % 2 === 1) { + a = embed(a, rows, cols + 1); + b = embed(b, rows, cols + 1); + } + + var halfRows = parseInt(a.rows / 2, 10); + var halfCols = parseInt(a.columns / 2, 10); + // Subdivide input matrices. + var a11 = a.subMatrix(0, halfRows - 1, 0, halfCols - 1); + var b11 = b.subMatrix(0, halfRows - 1, 0, halfCols - 1); + + var a12 = a.subMatrix(0, halfRows - 1, halfCols, a.columns - 1); + var b12 = b.subMatrix(0, halfRows - 1, halfCols, b.columns - 1); + + var a21 = a.subMatrix(halfRows, a.rows - 1, 0, halfCols - 1); + var b21 = b.subMatrix(halfRows, b.rows - 1, 0, halfCols - 1); + + var a22 = a.subMatrix(halfRows, a.rows - 1, halfCols, a.columns - 1); + var b22 = b.subMatrix(halfRows, b.rows - 1, halfCols, b.columns - 1); + + // Compute intermediate values. + var m1 = blockMult(Matrix.add(a11, a22), Matrix.add(b11, b22), halfRows, halfCols); + var m2 = blockMult(Matrix.add(a21, a22), b11, halfRows, halfCols); + var m3 = blockMult(a11, Matrix.sub(b12, b22), halfRows, halfCols); + var m4 = blockMult(a22, Matrix.sub(b21, b11), halfRows, halfCols); + var m5 = blockMult(Matrix.add(a11, a12), b22, halfRows, halfCols); + var m6 = blockMult(Matrix.sub(a21, a11), Matrix.add(b11, b12), halfRows, halfCols); + var m7 = blockMult(Matrix.sub(a12, a22), Matrix.add(b21, b22), halfRows, halfCols); + + // Combine intermediate values into the output. + var c11 = Matrix.add(m1, m4); + c11.sub(m5); + c11.add(m7); + var c12 = Matrix.add(m3, m5); + var c21 = Matrix.add(m2, m4); + var c22 = Matrix.sub(m1, m2); + c22.add(m3); + c22.add(m6); + + // Crop output to the desired size (undo dynamic padding). + var resultat = Matrix.zeros(2 * c11.rows, 2 * c11.columns); + resultat = resultat.setSubMatrix(c11, 0, 0); + resultat = resultat.setSubMatrix(c12, c11.rows, 0); + resultat = resultat.setSubMatrix(c21, 0, c11.columns); + resultat = resultat.setSubMatrix(c22, c11.rows, c11.columns); + return resultat.subMatrix(0, rows - 1, 0, cols - 1); + } + return blockMult(x, y, r, c); + } + + /** + * Returns a row-by-row scaled matrix + * @param {number} [min=0] - Minimum scaled value + * @param {number} [max=1] - Maximum scaled value + * @return {Matrix} - The scaled matrix + */ + scaleRows(min, max) { + min = min === undefined ? 0 : min; + max = max === undefined ? 1 : max; + if (min >= max) { + throw new RangeError('min should be strictly smaller than max'); + } + var newMatrix = this.constructor.empty(this.rows, this.columns); + for (var i = 0; i < this.rows; i++) { + var scaled = ml_array_rescale_lib_es6(this.getRow(i), { min, max }); + newMatrix.setRow(i, scaled); + } + return newMatrix; + } + + /** + * Returns a new column-by-column scaled matrix + * @param {number} [min=0] - Minimum scaled value + * @param {number} [max=1] - Maximum scaled value + * @return {Matrix} - The new scaled matrix + * @example + * var matrix = new Matrix([[1,2],[-1,0]]); + * var scaledMatrix = matrix.scaleColumns(); // [[1,1],[0,0]] + */ + scaleColumns(min, max) { + min = min === undefined ? 0 : min; + max = max === undefined ? 1 : max; + if (min >= max) { + throw new RangeError('min should be strictly smaller than max'); + } + var newMatrix = this.constructor.empty(this.rows, this.columns); + for (var i = 0; i < this.columns; i++) { + var scaled = ml_array_rescale_lib_es6(this.getColumn(i), { + min: min, + max: max + }); + newMatrix.setColumn(i, scaled); + } + return newMatrix; + } + + + /** + * Returns the Kronecker product (also known as tensor product) between this and other + * See https://en.wikipedia.org/wiki/Kronecker_product + * @param {Matrix} other + * @return {Matrix} + */ + kroneckerProduct(other) { + other = this.constructor.checkMatrix(other); + + var m = this.rows; + var n = this.columns; + var p = other.rows; + var q = other.columns; + + var result = new this.constructor[Symbol.species](m * p, n * q); + for (var i = 0; i < m; i++) { + for (var j = 0; j < n; j++) { + for (var k = 0; k < p; k++) { + for (var l = 0; l < q; l++) { + result[p * i + k][q * j + l] = this.get(i, j) * other.get(k, l); + } + } + } + } + return result; + } + + /** + * Transposes the matrix and returns a new one containing the result + * @return {Matrix} + */ + transpose() { + var result = new this.constructor[Symbol.species](this.columns, this.rows); + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + result.set(j, i, this.get(i, j)); + } + } + return result; + } + + /** + * Sorts the rows (in place) + * @param {function} compareFunction - usual Array.prototype.sort comparison function + * @return {Matrix} this + */ + sortRows(compareFunction) { + if (compareFunction === undefined) compareFunction = compareNumbers; + for (var i = 0; i < this.rows; i++) { + this.setRow(i, this.getRow(i).sort(compareFunction)); + } + return this; + } + + /** + * Sorts the columns (in place) + * @param {function} compareFunction - usual Array.prototype.sort comparison function + * @return {Matrix} this + */ + sortColumns(compareFunction) { + if (compareFunction === undefined) compareFunction = compareNumbers; + for (var i = 0; i < this.columns; i++) { + this.setColumn(i, this.getColumn(i).sort(compareFunction)); + } + return this; + } + + /** + * Returns a subset of the matrix + * @param {number} startRow - First row index + * @param {number} endRow - Last row index + * @param {number} startColumn - First column index + * @param {number} endColumn - Last column index + * @return {Matrix} + */ + subMatrix(startRow, endRow, startColumn, endColumn) { + checkRange(this, startRow, endRow, startColumn, endColumn); + var newMatrix = new this.constructor[Symbol.species](endRow - startRow + 1, endColumn - startColumn + 1); + for (var i = startRow; i <= endRow; i++) { + for (var j = startColumn; j <= endColumn; j++) { + newMatrix[i - startRow][j - startColumn] = this.get(i, j); + } + } + return newMatrix; + } + + /** + * Returns a subset of the matrix based on an array of row indices + * @param {Array} indices - Array containing the row indices + * @param {number} [startColumn = 0] - First column index + * @param {number} [endColumn = this.columns-1] - Last column index + * @return {Matrix} + */ + subMatrixRow(indices, startColumn, endColumn) { + if (startColumn === undefined) startColumn = 0; + if (endColumn === undefined) endColumn = this.columns - 1; + if ((startColumn > endColumn) || (startColumn < 0) || (startColumn >= this.columns) || (endColumn < 0) || (endColumn >= this.columns)) { + throw new RangeError('Argument out of range'); + } + + var newMatrix = new this.constructor[Symbol.species](indices.length, endColumn - startColumn + 1); + for (var i = 0; i < indices.length; i++) { + for (var j = startColumn; j <= endColumn; j++) { + if (indices[i] < 0 || indices[i] >= this.rows) { + throw new RangeError(`Row index out of range: ${indices[i]}`); + } + newMatrix.set(i, j - startColumn, this.get(indices[i], j)); + } + } + return newMatrix; + } + + /** + * Returns a subset of the matrix based on an array of column indices + * @param {Array} indices - Array containing the column indices + * @param {number} [startRow = 0] - First row index + * @param {number} [endRow = this.rows-1] - Last row index + * @return {Matrix} + */ + subMatrixColumn(indices, startRow, endRow) { + if (startRow === undefined) startRow = 0; + if (endRow === undefined) endRow = this.rows - 1; + if ((startRow > endRow) || (startRow < 0) || (startRow >= this.rows) || (endRow < 0) || (endRow >= this.rows)) { + throw new RangeError('Argument out of range'); + } + + var newMatrix = new this.constructor[Symbol.species](endRow - startRow + 1, indices.length); + for (var i = 0; i < indices.length; i++) { + for (var j = startRow; j <= endRow; j++) { + if (indices[i] < 0 || indices[i] >= this.columns) { + throw new RangeError(`Column index out of range: ${indices[i]}`); + } + newMatrix.set(j - startRow, i, this.get(j, indices[i])); + } + } + return newMatrix; + } + + /** + * Set a part of the matrix to the given sub-matrix + * @param {Matrix|Array< Array >} matrix - The source matrix from which to extract values. + * @param {number} startRow - The index of the first row to set + * @param {number} startColumn - The index of the first column to set + * @return {Matrix} + */ + setSubMatrix(matrix, startRow, startColumn) { + matrix = this.constructor.checkMatrix(matrix); + var endRow = startRow + matrix.rows - 1; + var endColumn = startColumn + matrix.columns - 1; + checkRange(this, startRow, endRow, startColumn, endColumn); + for (var i = 0; i < matrix.rows; i++) { + for (var j = 0; j < matrix.columns; j++) { + this[startRow + i][startColumn + j] = matrix.get(i, j); + } + } + return this; + } + + /** + * Return a new matrix based on a selection of rows and columns + * @param {Array} rowIndices - The row indices to select. Order matters and an index can be more than once. + * @param {Array} columnIndices - The column indices to select. Order matters and an index can be use more than once. + * @return {Matrix} The new matrix + */ + selection(rowIndices, columnIndices) { + var indices = checkIndices(this, rowIndices, columnIndices); + var newMatrix = new this.constructor[Symbol.species](rowIndices.length, columnIndices.length); + for (var i = 0; i < indices.row.length; i++) { + var rowIndex = indices.row[i]; + for (var j = 0; j < indices.column.length; j++) { + var columnIndex = indices.column[j]; + newMatrix[i][j] = this.get(rowIndex, columnIndex); + } + } + return newMatrix; + } + + /** + * Returns the trace of the matrix (sum of the diagonal elements) + * @return {number} + */ + trace() { + var min = Math.min(this.rows, this.columns); + var trace = 0; + for (var i = 0; i < min; i++) { + trace += this.get(i, i); + } + return trace; + } + + /* + Matrix views + */ + + /** + * Returns a view of the transposition of the matrix + * @return {MatrixTransposeView} + */ + transposeView() { + return new transpose_MatrixTransposeView(this); + } + + /** + * Returns a view of the row vector with the given index + * @param {number} row - row index of the vector + * @return {MatrixRowView} + */ + rowView(row) { + checkRowIndex(this, row); + return new row_MatrixRowView(this, row); + } + + /** + * Returns a view of the column vector with the given index + * @param {number} column - column index of the vector + * @return {MatrixColumnView} + */ + columnView(column) { + checkColumnIndex(this, column); + return new column_MatrixColumnView(this, column); + } + + /** + * Returns a view of the matrix flipped in the row axis + * @return {MatrixFlipRowView} + */ + flipRowView() { + return new flipRow_MatrixFlipRowView(this); + } + + /** + * Returns a view of the matrix flipped in the column axis + * @return {MatrixFlipColumnView} + */ + flipColumnView() { + return new flipColumn_MatrixFlipColumnView(this); + } + + /** + * Returns a view of a submatrix giving the index boundaries + * @param {number} startRow - first row index of the submatrix + * @param {number} endRow - last row index of the submatrix + * @param {number} startColumn - first column index of the submatrix + * @param {number} endColumn - last column index of the submatrix + * @return {MatrixSubView} + */ + subMatrixView(startRow, endRow, startColumn, endColumn) { + return new sub_MatrixSubView(this, startRow, endRow, startColumn, endColumn); + } + + /** + * Returns a view of the cross of the row indices and the column indices + * @example + * // resulting vector is [[2], [2]] + * var matrix = new Matrix([[1,2,3], [4,5,6]]).selectionView([0, 0], [1]) + * @param {Array} rowIndices + * @param {Array} columnIndices + * @return {MatrixSelectionView} + */ + selectionView(rowIndices, columnIndices) { + return new selection_MatrixSelectionView(this, rowIndices, columnIndices); + } + + /** + * Returns a view of the row indices + * @example + * // resulting vector is [[1,2,3], [1,2,3]] + * var matrix = new Matrix([[1,2,3], [4,5,6]]).rowSelectionView([0, 0]) + * @param {Array} rowIndices + * @return {MatrixRowSelectionView} + */ + rowSelectionView(rowIndices) { + return new rowSelection_MatrixRowSelectionView(this, rowIndices); + } + + /** + * Returns a view of the column indices + * @example + * // resulting vector is [[2, 2], [5, 5]] + * var matrix = new Matrix([[1,2,3], [4,5,6]]).columnSelectionView([1, 1]) + * @param {Array} columnIndices + * @return {MatrixColumnSelectionView} + */ + columnSelectionView(columnIndices) { + return new columnSelection_MatrixColumnSelectionView(this, columnIndices); + } + + + /** + * Calculates and returns the determinant of a matrix as a Number + * @example + * new Matrix([[1,2,3], [4,5,6]]).det() + * @return {number} + */ + det() { + if (this.isSquare()) { + var a, b, c, d; + if (this.columns === 2) { + // 2 x 2 matrix + a = this.get(0, 0); + b = this.get(0, 1); + c = this.get(1, 0); + d = this.get(1, 1); + + return a * d - (b * c); + } else if (this.columns === 3) { + // 3 x 3 matrix + var subMatrix0, subMatrix1, subMatrix2; + subMatrix0 = this.selectionView([1, 2], [1, 2]); + subMatrix1 = this.selectionView([1, 2], [0, 2]); + subMatrix2 = this.selectionView([1, 2], [0, 1]); + a = this.get(0, 0); + b = this.get(0, 1); + c = this.get(0, 2); + + return a * subMatrix0.det() - b * subMatrix1.det() + c * subMatrix2.det(); + } else { + // general purpose determinant using the LU decomposition + return new lu_LuDecomposition(this).determinant; + } + } else { + throw Error('Determinant can only be calculated for a square matrix.'); + } + } + + /** + * Returns inverse of a matrix if it exists or the pseudoinverse + * @param {number} threshold - threshold for taking inverse of singular values (default = 1e-15) + * @return {Matrix} the (pseudo)inverted matrix. + */ + pseudoInverse(threshold) { + if (threshold === undefined) threshold = Number.EPSILON; + var svdSolution = new svd_SingularValueDecomposition(this, { autoTranspose: true }); + + var U = svdSolution.leftSingularVectors; + var V = svdSolution.rightSingularVectors; + var s = svdSolution.diagonal; + + for (var i = 0; i < s.length; i++) { + if (Math.abs(s[i]) > threshold) { + s[i] = 1.0 / s[i]; + } else { + s[i] = 0.0; + } + } + + // convert list to diagonal + s = this.constructor[Symbol.species].diag(s); + return V.mmul(s.mmul(U.transposeView())); + } + + /** + * Creates an exact and independent copy of the matrix + * @return {Matrix} + */ + clone() { + var newMatrix = new this.constructor[Symbol.species](this.rows, this.columns); + for (var row = 0; row < this.rows; row++) { + for (var column = 0; column < this.columns; column++) { + newMatrix.set(row, column, this.get(row, column)); + } + } + return newMatrix; + } + } + + Matrix.prototype.klass = 'Matrix'; + + function compareNumbers(a, b) { + return a - b; + } + + /* + Synonyms + */ + + Matrix.random = Matrix.rand; + Matrix.diagonal = Matrix.diag; + Matrix.prototype.diagonal = Matrix.prototype.diag; + Matrix.identity = Matrix.eye; + Matrix.prototype.negate = Matrix.prototype.neg; + Matrix.prototype.tensorProduct = Matrix.prototype.kroneckerProduct; + Matrix.prototype.determinant = Matrix.prototype.det; + + /* + Add dynamically instance and static methods for mathematical operations + */ + + var inplaceOperator = ` +(function %name%(value) { + if (typeof value === 'number') return this.%name%S(value); + return this.%name%M(value); +}) +`; + + var inplaceOperatorScalar = ` +(function %name%S(value) { + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) %op% value); + } + } + return this; +}) +`; + + var inplaceOperatorMatrix = ` +(function %name%M(matrix) { + matrix = this.constructor.checkMatrix(matrix); + if (this.rows !== matrix.rows || + this.columns !== matrix.columns) { + throw new RangeError('Matrices dimensions must be equal'); + } + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) %op% matrix.get(i, j)); + } + } + return this; +}) +`; + + var staticOperator = ` +(function %name%(matrix, value) { + var newMatrix = new this[Symbol.species](matrix); + return newMatrix.%name%(value); +}) +`; + + var inplaceMethod = ` +(function %name%() { + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + this.set(i, j, %method%(this.get(i, j))); + } + } + return this; +}) +`; + + var staticMethod = ` +(function %name%(matrix) { + var newMatrix = new this[Symbol.species](matrix); + return newMatrix.%name%(); +}) +`; + + var inplaceMethodWithArgs = ` +(function %name%(%args%) { + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + this.set(i, j, %method%(this.get(i, j), %args%)); + } + } + return this; +}) +`; + + var staticMethodWithArgs = ` +(function %name%(matrix, %args%) { + var newMatrix = new this[Symbol.species](matrix); + return newMatrix.%name%(%args%); +}) +`; + + + var inplaceMethodWithOneArgScalar = ` +(function %name%S(value) { + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + this.set(i, j, %method%(this.get(i, j), value)); + } + } + return this; +}) +`; + var inplaceMethodWithOneArgMatrix = ` +(function %name%M(matrix) { + matrix = this.constructor.checkMatrix(matrix); + if (this.rows !== matrix.rows || + this.columns !== matrix.columns) { + throw new RangeError('Matrices dimensions must be equal'); + } + for (var i = 0; i < this.rows; i++) { + for (var j = 0; j < this.columns; j++) { + this.set(i, j, %method%(this.get(i, j), matrix.get(i, j))); + } + } + return this; +}) +`; + + var inplaceMethodWithOneArg = ` +(function %name%(value) { + if (typeof value === 'number') return this.%name%S(value); + return this.%name%M(value); +}) +`; + + var staticMethodWithOneArg = staticMethodWithArgs; + + var operators = [ + // Arithmetic operators + ['+', 'add'], + ['-', 'sub', 'subtract'], + ['*', 'mul', 'multiply'], + ['/', 'div', 'divide'], + ['%', 'mod', 'modulus'], + // Bitwise operators + ['&', 'and'], + ['|', 'or'], + ['^', 'xor'], + ['<<', 'leftShift'], + ['>>', 'signPropagatingRightShift'], + ['>>>', 'rightShift', 'zeroFillRightShift'] + ]; + + var i; + var eval2 = eval; // eslint-disable-line no-eval + for (var operator of operators) { + var inplaceOp = eval2(fillTemplateFunction(inplaceOperator, { name: operator[1], op: operator[0] })); + var inplaceOpS = eval2(fillTemplateFunction(inplaceOperatorScalar, { name: `${operator[1]}S`, op: operator[0] })); + var inplaceOpM = eval2(fillTemplateFunction(inplaceOperatorMatrix, { name: `${operator[1]}M`, op: operator[0] })); + var staticOp = eval2(fillTemplateFunction(staticOperator, { name: operator[1] })); + for (i = 1; i < operator.length; i++) { + Matrix.prototype[operator[i]] = inplaceOp; + Matrix.prototype[`${operator[i]}S`] = inplaceOpS; + Matrix.prototype[`${operator[i]}M`] = inplaceOpM; + Matrix[operator[i]] = staticOp; + } + } + + var methods = [['~', 'not']]; + + [ + 'abs', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atanh', 'cbrt', 'ceil', + 'clz32', 'cos', 'cosh', 'exp', 'expm1', 'floor', 'fround', 'log', 'log1p', + 'log10', 'log2', 'round', 'sign', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc' + ].forEach(function (mathMethod) { + methods.push([`Math.${mathMethod}`, mathMethod]); + }); + + for (var method of methods) { + var inplaceMeth = eval2(fillTemplateFunction(inplaceMethod, { name: method[1], method: method[0] })); + var staticMeth = eval2(fillTemplateFunction(staticMethod, { name: method[1] })); + for (i = 1; i < method.length; i++) { + Matrix.prototype[method[i]] = inplaceMeth; + Matrix[method[i]] = staticMeth; + } + } + + var methodsWithArgs = [['Math.pow', 1, 'pow']]; + + for (var methodWithArg of methodsWithArgs) { + var args = 'arg0'; + for (i = 1; i < methodWithArg[1]; i++) { + args += `, arg${i}`; + } + if (methodWithArg[1] !== 1) { + var inplaceMethWithArgs = eval2(fillTemplateFunction(inplaceMethodWithArgs, { + name: methodWithArg[2], + method: methodWithArg[0], + args: args + })); + var staticMethWithArgs = eval2(fillTemplateFunction(staticMethodWithArgs, { name: methodWithArg[2], args: args })); + for (i = 2; i < methodWithArg.length; i++) { + Matrix.prototype[methodWithArg[i]] = inplaceMethWithArgs; + Matrix[methodWithArg[i]] = staticMethWithArgs; + } + } else { + var tmplVar = { + name: methodWithArg[2], + args: args, + method: methodWithArg[0] + }; + var inplaceMethod2 = eval2(fillTemplateFunction(inplaceMethodWithOneArg, tmplVar)); + var inplaceMethodS = eval2(fillTemplateFunction(inplaceMethodWithOneArgScalar, tmplVar)); + var inplaceMethodM = eval2(fillTemplateFunction(inplaceMethodWithOneArgMatrix, tmplVar)); + var staticMethod2 = eval2(fillTemplateFunction(staticMethodWithOneArg, tmplVar)); + for (i = 2; i < methodWithArg.length; i++) { + Matrix.prototype[methodWithArg[i]] = inplaceMethod2; + Matrix.prototype[`${methodWithArg[i]}M`] = inplaceMethodM; + Matrix.prototype[`${methodWithArg[i]}S`] = inplaceMethodS; + Matrix[methodWithArg[i]] = staticMethod2; + } + } + } + + function fillTemplateFunction(template, values) { + for (var value in values) { + template = template.replace(new RegExp(`%${value}%`, 'g'), values[value]); + } + return template; + } + + return Matrix; +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/matrix.js + + + +class matrix_Matrix extends AbstractMatrix(Array) { + constructor(nRows, nColumns) { + var i; + if (arguments.length === 1 && typeof nRows === 'number') { + return new Array(nRows); + } + if (matrix_Matrix.isMatrix(nRows)) { + return nRows.clone(); + } else if (Number.isInteger(nRows) && nRows > 0) { + // Create an empty matrix + super(nRows); + if (Number.isInteger(nColumns) && nColumns > 0) { + for (i = 0; i < nRows; i++) { + this[i] = new Array(nColumns); + } + } else { + throw new TypeError('nColumns must be a positive integer'); + } + } else if (Array.isArray(nRows)) { + // Copy the values from the 2D array + const matrix = nRows; + nRows = matrix.length; + nColumns = matrix[0].length; + if (typeof nColumns !== 'number' || nColumns === 0) { + throw new TypeError( + 'Data must be a 2D array with at least one element' + ); + } + super(nRows); + for (i = 0; i < nRows; i++) { + if (matrix[i].length !== nColumns) { + throw new RangeError('Inconsistent array dimensions'); + } + this[i] = [].concat(matrix[i]); + } + } else { + throw new TypeError( + 'First argument must be a positive number or an array' + ); + } + this.rows = nRows; + this.columns = nColumns; + return this; + } + + set(rowIndex, columnIndex, value) { + this[rowIndex][columnIndex] = value; + return this; + } + + get(rowIndex, columnIndex) { + return this[rowIndex][columnIndex]; + } + + /** + * Removes a row from the given index + * @param {number} index - Row index + * @return {Matrix} this + */ + removeRow(index) { + checkRowIndex(this, index); + if (this.rows === 1) { + throw new RangeError('A matrix cannot have less than one row'); + } + this.splice(index, 1); + this.rows -= 1; + return this; + } + + /** + * Adds a row at the given index + * @param {number} [index = this.rows] - Row index + * @param {Array|Matrix} array - Array or vector + * @return {Matrix} this + */ + addRow(index, array) { + if (array === undefined) { + array = index; + index = this.rows; + } + checkRowIndex(this, index, true); + array = checkRowVector(this, array, true); + this.splice(index, 0, array); + this.rows += 1; + return this; + } + + /** + * Removes a column from the given index + * @param {number} index - Column index + * @return {Matrix} this + */ + removeColumn(index) { + checkColumnIndex(this, index); + if (this.columns === 1) { + throw new RangeError('A matrix cannot have less than one column'); + } + for (var i = 0; i < this.rows; i++) { + this[i].splice(index, 1); + } + this.columns -= 1; + return this; + } + + /** + * Adds a column at the given index + * @param {number} [index = this.columns] - Column index + * @param {Array|Matrix} array - Array or vector + * @return {Matrix} this + */ + addColumn(index, array) { + if (typeof array === 'undefined') { + array = index; + index = this.columns; + } + checkColumnIndex(this, index, true); + array = checkColumnVector(this, array); + for (var i = 0; i < this.rows; i++) { + this[i].splice(index, 0, array[i]); + } + this.columns += 1; + return this; + } +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/wrap/WrapperMatrix1D.js + + + +class WrapperMatrix1D_WrapperMatrix1D extends AbstractMatrix() { + /** + * @class WrapperMatrix1D + * @param {Array} data + * @param {object} [options] + * @param {object} [options.rows = 1] + */ + constructor(data, options = {}) { + const { rows = 1 } = options; + + if (data.length % rows !== 0) { + throw new Error('the data length is not divisible by the number of rows'); + } + super(); + this.rows = rows; + this.columns = data.length / rows; + this.data = data; + } + + set(rowIndex, columnIndex, value) { + var index = this._calculateIndex(rowIndex, columnIndex); + this.data[index] = value; + return this; + } + + get(rowIndex, columnIndex) { + var index = this._calculateIndex(rowIndex, columnIndex); + return this.data[index]; + } + + _calculateIndex(row, column) { + return row * this.columns + column; + } + + static get [Symbol.species]() { + return matrix_Matrix; + } +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/wrap/WrapperMatrix2D.js + + + +class WrapperMatrix2D_WrapperMatrix2D extends AbstractMatrix() { + /** + * @class WrapperMatrix2D + * @param {Array>} data + */ + constructor(data) { + super(); + this.data = data; + this.rows = data.length; + this.columns = data[0].length; + } + + set(rowIndex, columnIndex, value) { + this.data[rowIndex][columnIndex] = value; + return this; + } + + get(rowIndex, columnIndex) { + return this.data[rowIndex][columnIndex]; + } + + static get [Symbol.species]() { + return matrix_Matrix; + } +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/wrap/wrap.js + + + +/** + * @param {Array>|Array} array + * @param {object} [options] + * @param {object} [options.rows = 1] + * @return {WrapperMatrix1D|WrapperMatrix2D} + */ +function wrap(array, options) { + if (Array.isArray(array)) { + if (array[0] && Array.isArray(array[0])) { + return new WrapperMatrix2D_WrapperMatrix2D(array); + } else { + return new WrapperMatrix1D_WrapperMatrix1D(array, options); + } + } else { + throw new Error('the argument is not an array'); + } +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/dc/qr.js + + + + +/** + * @class QrDecomposition + * @link https://github.com/lutzroeder/Mapack/blob/master/Source/QrDecomposition.cs + * @param {Matrix} value + */ +class qr_QrDecomposition { + constructor(value) { + value = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(value); + + var qr = value.clone(); + var m = value.rows; + var n = value.columns; + var rdiag = new Array(n); + var i, j, k, s; + + for (k = 0; k < n; k++) { + var nrm = 0; + for (i = k; i < m; i++) { + nrm = hypotenuse(nrm, qr.get(i, k)); + } + if (nrm !== 0) { + if (qr.get(k, k) < 0) { + nrm = -nrm; + } + for (i = k; i < m; i++) { + qr.set(i, k, qr.get(i, k) / nrm); + } + qr.set(k, k, qr.get(k, k) + 1); + for (j = k + 1; j < n; j++) { + s = 0; + for (i = k; i < m; i++) { + s += qr.get(i, k) * qr.get(i, j); + } + s = -s / qr.get(k, k); + for (i = k; i < m; i++) { + qr.set(i, j, qr.get(i, j) + s * qr.get(i, k)); + } + } + } + rdiag[k] = -nrm; + } + + this.QR = qr; + this.Rdiag = rdiag; + } + + /** + * Solve a problem of least square (Ax=b) by using the QR decomposition. Useful when A is rectangular, but not working when A is singular. + * Example : We search to approximate x, with A matrix shape m*n, x vector size n, b vector size m (m > n). We will use : + * var qr = QrDecomposition(A); + * var x = qr.solve(b); + * @param {Matrix} value - Matrix 1D which is the vector b (in the equation Ax = b) + * @return {Matrix} - The vector x + */ + solve(value) { + value = matrix_Matrix.checkMatrix(value); + + var qr = this.QR; + var m = qr.rows; + + if (value.rows !== m) { + throw new Error('Matrix row dimensions must agree'); + } + if (!this.isFullRank()) { + throw new Error('Matrix is rank deficient'); + } + + var count = value.columns; + var X = value.clone(); + var n = qr.columns; + var i, j, k, s; + + for (k = 0; k < n; k++) { + for (j = 0; j < count; j++) { + s = 0; + for (i = k; i < m; i++) { + s += qr[i][k] * X[i][j]; + } + s = -s / qr[k][k]; + for (i = k; i < m; i++) { + X[i][j] += s * qr[i][k]; + } + } + } + for (k = n - 1; k >= 0; k--) { + for (j = 0; j < count; j++) { + X[k][j] /= this.Rdiag[k]; + } + for (i = 0; i < k; i++) { + for (j = 0; j < count; j++) { + X[i][j] -= X[k][j] * qr[i][k]; + } + } + } + + return X.subMatrix(0, n - 1, 0, count - 1); + } + + /** + * + * @return {boolean} + */ + isFullRank() { + var columns = this.QR.columns; + for (var i = 0; i < columns; i++) { + if (this.Rdiag[i] === 0) { + return false; + } + } + return true; + } + + /** + * + * @return {Matrix} + */ + get upperTriangularMatrix() { + var qr = this.QR; + var n = qr.columns; + var X = new matrix_Matrix(n, n); + var i, j; + for (i = 0; i < n; i++) { + for (j = 0; j < n; j++) { + if (i < j) { + X[i][j] = qr[i][j]; + } else if (i === j) { + X[i][j] = this.Rdiag[i]; + } else { + X[i][j] = 0; + } + } + } + return X; + } + + /** + * + * @return {Matrix} + */ + get orthogonalMatrix() { + var qr = this.QR; + var rows = qr.rows; + var columns = qr.columns; + var X = new matrix_Matrix(rows, columns); + var i, j, k, s; + + for (k = columns - 1; k >= 0; k--) { + for (i = 0; i < rows; i++) { + X[i][k] = 0; + } + X[k][k] = 1; + for (j = k; j < columns; j++) { + if (qr[k][k] !== 0) { + s = 0; + for (i = k; i < rows; i++) { + s += qr[i][k] * X[i][j]; + } + + s = -s / qr[k][k]; + + for (i = k; i < rows; i++) { + X[i][j] += s * qr[i][k]; + } + } + } + } + return X; + } +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/decompositions.js + + + + + + +/** + * Computes the inverse of a Matrix + * @param {Matrix} matrix + * @param {boolean} [useSVD=false] + * @return {Matrix} + */ +function inverse(matrix, useSVD = false) { + matrix = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(matrix); + if (useSVD) { + return new svd_SingularValueDecomposition(matrix).inverse(); + } else { + return solve(matrix, matrix_Matrix.eye(matrix.rows)); + } +} + +/** + * + * @param {Matrix} leftHandSide + * @param {Matrix} rightHandSide + * @param {boolean} [useSVD = false] + * @return {Matrix} + */ +function solve(leftHandSide, rightHandSide, useSVD = false) { + leftHandSide = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(leftHandSide); + rightHandSide = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(rightHandSide); + if (useSVD) { + return new svd_SingularValueDecomposition(leftHandSide).solve(rightHandSide); + } else { + return leftHandSide.isSquare() + ? new lu_LuDecomposition(leftHandSide).solve(rightHandSide) + : new qr_QrDecomposition(leftHandSide).solve(rightHandSide); + } +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/linearDependencies.js + + + + + +// function used by rowsDependencies +function xrange(n, exception) { + var range = []; + for (var i = 0; i < n; i++) { + if (i !== exception) { + range.push(i); + } + } + return range; +} + +// function used by rowsDependencies +function dependenciesOneRow( + error, + matrix, + index, + thresholdValue = 10e-10, + thresholdError = 10e-10 +) { + if (error > thresholdError) { + return new Array(matrix.rows + 1).fill(0); + } else { + var returnArray = matrix.addRow(index, [0]); + for (var i = 0; i < returnArray.rows; i++) { + if (Math.abs(returnArray.get(i, 0)) < thresholdValue) { + returnArray.set(i, 0, 0); + } + } + return returnArray.to1DArray(); + } +} + +/** + * Creates a matrix which represents the dependencies between rows. + * If a row is a linear combination of others rows, the result will be a row with the coefficients of this combination. + * For example : for A = [[2, 0, 0, 1], [0, 1, 6, 0], [0, 3, 0, 1], [0, 0, 1, 0], [0, 1, 2, 0]], the result will be [[0, 0, 0, 0, 0], [0, 0, 0, 4, 1], [0, 0, 0, 0, 0], [0, 0.25, 0, 0, -0.25], [0, 1, 0, -4, 0]] + * @param {Matrix} matrix + * @param {Object} [options] includes thresholdValue and thresholdError. + * @param {number} [options.thresholdValue = 10e-10] If an absolute value is inferior to this threshold, it will equals zero. + * @param {number} [options.thresholdError = 10e-10] If the error is inferior to that threshold, the linear combination found is accepted and the row is dependent from other rows. + * @return {Matrix} the matrix which represents the dependencies between rows. + */ + +function linearDependencies(matrix, options = {}) { + const { thresholdValue = 10e-10, thresholdError = 10e-10 } = options; + + var n = matrix.rows; + var results = new matrix_Matrix(n, n); + + for (var i = 0; i < n; i++) { + var b = matrix_Matrix.columnVector(matrix.getRow(i)); + var Abis = matrix.subMatrixRow(xrange(n, i)).transposeView(); + var svd = new svd_SingularValueDecomposition(Abis); + var x = svd.solve(b); + var error = lib_es6( + matrix_Matrix.sub(b, Abis.mmul(x)) + .abs() + .to1DArray() + ); + results.setRow( + i, + dependenciesOneRow(error, x, i, thresholdValue, thresholdError) + ); + } + return results; +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/dc/evd.js + + + + +/** + * @class EigenvalueDecomposition + * @link https://github.com/lutzroeder/Mapack/blob/master/Source/EigenvalueDecomposition.cs + * @param {Matrix} matrix + * @param {object} [options] + * @param {boolean} [options.assumeSymmetric=false] + */ +class evd_EigenvalueDecomposition { + constructor(matrix, options = {}) { + const { assumeSymmetric = false } = options; + + matrix = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(matrix); + if (!matrix.isSquare()) { + throw new Error('Matrix is not a square matrix'); + } + + var n = matrix.columns; + var V = getFilled2DArray(n, n, 0); + var d = new Array(n); + var e = new Array(n); + var value = matrix; + var i, j; + + var isSymmetric = false; + if (assumeSymmetric) { + isSymmetric = true; + } else { + isSymmetric = matrix.isSymmetric(); + } + + if (isSymmetric) { + for (i = 0; i < n; i++) { + for (j = 0; j < n; j++) { + V[i][j] = value.get(i, j); + } + } + tred2(n, e, d, V); + tql2(n, e, d, V); + } else { + var H = getFilled2DArray(n, n, 0); + var ort = new Array(n); + for (j = 0; j < n; j++) { + for (i = 0; i < n; i++) { + H[i][j] = value.get(i, j); + } + } + orthes(n, H, ort, V); + hqr2(n, e, d, V, H); + } + + this.n = n; + this.e = e; + this.d = d; + this.V = V; + } + + /** + * + * @return {Array} + */ + get realEigenvalues() { + return this.d; + } + + /** + * + * @return {Array} + */ + get imaginaryEigenvalues() { + return this.e; + } + + /** + * + * @return {Matrix} + */ + get eigenvectorMatrix() { + if (!matrix_Matrix.isMatrix(this.V)) { + this.V = new matrix_Matrix(this.V); + } + return this.V; + } + + /** + * + * @return {Matrix} + */ + get diagonalMatrix() { + var n = this.n; + var e = this.e; + var d = this.d; + var X = new matrix_Matrix(n, n); + var i, j; + for (i = 0; i < n; i++) { + for (j = 0; j < n; j++) { + X[i][j] = 0; + } + X[i][i] = d[i]; + if (e[i] > 0) { + X[i][i + 1] = e[i]; + } else if (e[i] < 0) { + X[i][i - 1] = e[i]; + } + } + return X; + } +} + +function tred2(n, e, d, V) { + var f, g, h, i, j, k, hh, scale; + + for (j = 0; j < n; j++) { + d[j] = V[n - 1][j]; + } + + for (i = n - 1; i > 0; i--) { + scale = 0; + h = 0; + for (k = 0; k < i; k++) { + scale = scale + Math.abs(d[k]); + } + + if (scale === 0) { + e[i] = d[i - 1]; + for (j = 0; j < i; j++) { + d[j] = V[i - 1][j]; + V[i][j] = 0; + V[j][i] = 0; + } + } else { + for (k = 0; k < i; k++) { + d[k] /= scale; + h += d[k] * d[k]; + } + + f = d[i - 1]; + g = Math.sqrt(h); + if (f > 0) { + g = -g; + } + + e[i] = scale * g; + h = h - f * g; + d[i - 1] = f - g; + for (j = 0; j < i; j++) { + e[j] = 0; + } + + for (j = 0; j < i; j++) { + f = d[j]; + V[j][i] = f; + g = e[j] + V[j][j] * f; + for (k = j + 1; k <= i - 1; k++) { + g += V[k][j] * d[k]; + e[k] += V[k][j] * f; + } + e[j] = g; + } + + f = 0; + for (j = 0; j < i; j++) { + e[j] /= h; + f += e[j] * d[j]; + } + + hh = f / (h + h); + for (j = 0; j < i; j++) { + e[j] -= hh * d[j]; + } + + for (j = 0; j < i; j++) { + f = d[j]; + g = e[j]; + for (k = j; k <= i - 1; k++) { + V[k][j] -= f * e[k] + g * d[k]; + } + d[j] = V[i - 1][j]; + V[i][j] = 0; + } + } + d[i] = h; + } + + for (i = 0; i < n - 1; i++) { + V[n - 1][i] = V[i][i]; + V[i][i] = 1; + h = d[i + 1]; + if (h !== 0) { + for (k = 0; k <= i; k++) { + d[k] = V[k][i + 1] / h; + } + + for (j = 0; j <= i; j++) { + g = 0; + for (k = 0; k <= i; k++) { + g += V[k][i + 1] * V[k][j]; + } + for (k = 0; k <= i; k++) { + V[k][j] -= g * d[k]; + } + } + } + + for (k = 0; k <= i; k++) { + V[k][i + 1] = 0; + } + } + + for (j = 0; j < n; j++) { + d[j] = V[n - 1][j]; + V[n - 1][j] = 0; + } + + V[n - 1][n - 1] = 1; + e[0] = 0; +} + +function tql2(n, e, d, V) { + var g, h, i, j, k, l, m, p, r, dl1, c, c2, c3, el1, s, s2, iter; + + for (i = 1; i < n; i++) { + e[i - 1] = e[i]; + } + + e[n - 1] = 0; + + var f = 0; + var tst1 = 0; + var eps = Number.EPSILON; + + for (l = 0; l < n; l++) { + tst1 = Math.max(tst1, Math.abs(d[l]) + Math.abs(e[l])); + m = l; + while (m < n) { + if (Math.abs(e[m]) <= eps * tst1) { + break; + } + m++; + } + + if (m > l) { + iter = 0; + do { + iter = iter + 1; + + g = d[l]; + p = (d[l + 1] - g) / (2 * e[l]); + r = hypotenuse(p, 1); + if (p < 0) { + r = -r; + } + + d[l] = e[l] / (p + r); + d[l + 1] = e[l] * (p + r); + dl1 = d[l + 1]; + h = g - d[l]; + for (i = l + 2; i < n; i++) { + d[i] -= h; + } + + f = f + h; + + p = d[m]; + c = 1; + c2 = c; + c3 = c; + el1 = e[l + 1]; + s = 0; + s2 = 0; + for (i = m - 1; i >= l; i--) { + c3 = c2; + c2 = c; + s2 = s; + g = c * e[i]; + h = c * p; + r = hypotenuse(p, e[i]); + e[i + 1] = s * r; + s = e[i] / r; + c = p / r; + p = c * d[i] - s * g; + d[i + 1] = h + s * (c * g + s * d[i]); + + for (k = 0; k < n; k++) { + h = V[k][i + 1]; + V[k][i + 1] = s * V[k][i] + c * h; + V[k][i] = c * V[k][i] - s * h; + } + } + + p = -s * s2 * c3 * el1 * e[l] / dl1; + e[l] = s * p; + d[l] = c * p; + } while (Math.abs(e[l]) > eps * tst1); + } + d[l] = d[l] + f; + e[l] = 0; + } + + for (i = 0; i < n - 1; i++) { + k = i; + p = d[i]; + for (j = i + 1; j < n; j++) { + if (d[j] < p) { + k = j; + p = d[j]; + } + } + + if (k !== i) { + d[k] = d[i]; + d[i] = p; + for (j = 0; j < n; j++) { + p = V[j][i]; + V[j][i] = V[j][k]; + V[j][k] = p; + } + } + } +} + +function orthes(n, H, ort, V) { + var low = 0; + var high = n - 1; + var f, g, h, i, j, m; + var scale; + + for (m = low + 1; m <= high - 1; m++) { + scale = 0; + for (i = m; i <= high; i++) { + scale = scale + Math.abs(H[i][m - 1]); + } + + if (scale !== 0) { + h = 0; + for (i = high; i >= m; i--) { + ort[i] = H[i][m - 1] / scale; + h += ort[i] * ort[i]; + } + + g = Math.sqrt(h); + if (ort[m] > 0) { + g = -g; + } + + h = h - ort[m] * g; + ort[m] = ort[m] - g; + + for (j = m; j < n; j++) { + f = 0; + for (i = high; i >= m; i--) { + f += ort[i] * H[i][j]; + } + + f = f / h; + for (i = m; i <= high; i++) { + H[i][j] -= f * ort[i]; + } + } + + for (i = 0; i <= high; i++) { + f = 0; + for (j = high; j >= m; j--) { + f += ort[j] * H[i][j]; + } + + f = f / h; + for (j = m; j <= high; j++) { + H[i][j] -= f * ort[j]; + } + } + + ort[m] = scale * ort[m]; + H[m][m - 1] = scale * g; + } + } + + for (i = 0; i < n; i++) { + for (j = 0; j < n; j++) { + V[i][j] = i === j ? 1 : 0; + } + } + + for (m = high - 1; m >= low + 1; m--) { + if (H[m][m - 1] !== 0) { + for (i = m + 1; i <= high; i++) { + ort[i] = H[i][m - 1]; + } + + for (j = m; j <= high; j++) { + g = 0; + for (i = m; i <= high; i++) { + g += ort[i] * V[i][j]; + } + + g = g / ort[m] / H[m][m - 1]; + for (i = m; i <= high; i++) { + V[i][j] += g * ort[i]; + } + } + } + } +} + +function hqr2(nn, e, d, V, H) { + var n = nn - 1; + var low = 0; + var high = nn - 1; + var eps = Number.EPSILON; + var exshift = 0; + var norm = 0; + var p = 0; + var q = 0; + var r = 0; + var s = 0; + var z = 0; + var iter = 0; + var i, j, k, l, m, t, w, x, y; + var ra, sa, vr, vi; + var notlast, cdivres; + + for (i = 0; i < nn; i++) { + if (i < low || i > high) { + d[i] = H[i][i]; + e[i] = 0; + } + + for (j = Math.max(i - 1, 0); j < nn; j++) { + norm = norm + Math.abs(H[i][j]); + } + } + + while (n >= low) { + l = n; + while (l > low) { + s = Math.abs(H[l - 1][l - 1]) + Math.abs(H[l][l]); + if (s === 0) { + s = norm; + } + if (Math.abs(H[l][l - 1]) < eps * s) { + break; + } + l--; + } + + if (l === n) { + H[n][n] = H[n][n] + exshift; + d[n] = H[n][n]; + e[n] = 0; + n--; + iter = 0; + } else if (l === n - 1) { + w = H[n][n - 1] * H[n - 1][n]; + p = (H[n - 1][n - 1] - H[n][n]) / 2; + q = p * p + w; + z = Math.sqrt(Math.abs(q)); + H[n][n] = H[n][n] + exshift; + H[n - 1][n - 1] = H[n - 1][n - 1] + exshift; + x = H[n][n]; + + if (q >= 0) { + z = p >= 0 ? p + z : p - z; + d[n - 1] = x + z; + d[n] = d[n - 1]; + if (z !== 0) { + d[n] = x - w / z; + } + e[n - 1] = 0; + e[n] = 0; + x = H[n][n - 1]; + s = Math.abs(x) + Math.abs(z); + p = x / s; + q = z / s; + r = Math.sqrt(p * p + q * q); + p = p / r; + q = q / r; + + for (j = n - 1; j < nn; j++) { + z = H[n - 1][j]; + H[n - 1][j] = q * z + p * H[n][j]; + H[n][j] = q * H[n][j] - p * z; + } + + for (i = 0; i <= n; i++) { + z = H[i][n - 1]; + H[i][n - 1] = q * z + p * H[i][n]; + H[i][n] = q * H[i][n] - p * z; + } + + for (i = low; i <= high; i++) { + z = V[i][n - 1]; + V[i][n - 1] = q * z + p * V[i][n]; + V[i][n] = q * V[i][n] - p * z; + } + } else { + d[n - 1] = x + p; + d[n] = x + p; + e[n - 1] = z; + e[n] = -z; + } + + n = n - 2; + iter = 0; + } else { + x = H[n][n]; + y = 0; + w = 0; + if (l < n) { + y = H[n - 1][n - 1]; + w = H[n][n - 1] * H[n - 1][n]; + } + + if (iter === 10) { + exshift += x; + for (i = low; i <= n; i++) { + H[i][i] -= x; + } + s = Math.abs(H[n][n - 1]) + Math.abs(H[n - 1][n - 2]); + x = y = 0.75 * s; + w = -0.4375 * s * s; + } + + if (iter === 30) { + s = (y - x) / 2; + s = s * s + w; + if (s > 0) { + s = Math.sqrt(s); + if (y < x) { + s = -s; + } + s = x - w / ((y - x) / 2 + s); + for (i = low; i <= n; i++) { + H[i][i] -= s; + } + exshift += s; + x = y = w = 0.964; + } + } + + iter = iter + 1; + + m = n - 2; + while (m >= l) { + z = H[m][m]; + r = x - z; + s = y - z; + p = (r * s - w) / H[m + 1][m] + H[m][m + 1]; + q = H[m + 1][m + 1] - z - r - s; + r = H[m + 2][m + 1]; + s = Math.abs(p) + Math.abs(q) + Math.abs(r); + p = p / s; + q = q / s; + r = r / s; + if (m === l) { + break; + } + if ( + Math.abs(H[m][m - 1]) * (Math.abs(q) + Math.abs(r)) < + eps * + (Math.abs(p) * + (Math.abs(H[m - 1][m - 1]) + + Math.abs(z) + + Math.abs(H[m + 1][m + 1]))) + ) { + break; + } + m--; + } + + for (i = m + 2; i <= n; i++) { + H[i][i - 2] = 0; + if (i > m + 2) { + H[i][i - 3] = 0; + } + } + + for (k = m; k <= n - 1; k++) { + notlast = k !== n - 1; + if (k !== m) { + p = H[k][k - 1]; + q = H[k + 1][k - 1]; + r = notlast ? H[k + 2][k - 1] : 0; + x = Math.abs(p) + Math.abs(q) + Math.abs(r); + if (x !== 0) { + p = p / x; + q = q / x; + r = r / x; + } + } + + if (x === 0) { + break; + } + + s = Math.sqrt(p * p + q * q + r * r); + if (p < 0) { + s = -s; + } + + if (s !== 0) { + if (k !== m) { + H[k][k - 1] = -s * x; + } else if (l !== m) { + H[k][k - 1] = -H[k][k - 1]; + } + + p = p + s; + x = p / s; + y = q / s; + z = r / s; + q = q / p; + r = r / p; + + for (j = k; j < nn; j++) { + p = H[k][j] + q * H[k + 1][j]; + if (notlast) { + p = p + r * H[k + 2][j]; + H[k + 2][j] = H[k + 2][j] - p * z; + } + + H[k][j] = H[k][j] - p * x; + H[k + 1][j] = H[k + 1][j] - p * y; + } + + for (i = 0; i <= Math.min(n, k + 3); i++) { + p = x * H[i][k] + y * H[i][k + 1]; + if (notlast) { + p = p + z * H[i][k + 2]; + H[i][k + 2] = H[i][k + 2] - p * r; + } + + H[i][k] = H[i][k] - p; + H[i][k + 1] = H[i][k + 1] - p * q; + } + + for (i = low; i <= high; i++) { + p = x * V[i][k] + y * V[i][k + 1]; + if (notlast) { + p = p + z * V[i][k + 2]; + V[i][k + 2] = V[i][k + 2] - p * r; + } + + V[i][k] = V[i][k] - p; + V[i][k + 1] = V[i][k + 1] - p * q; + } + } + } + } + } + + if (norm === 0) { + return; + } + + for (n = nn - 1; n >= 0; n--) { + p = d[n]; + q = e[n]; + + if (q === 0) { + l = n; + H[n][n] = 1; + for (i = n - 1; i >= 0; i--) { + w = H[i][i] - p; + r = 0; + for (j = l; j <= n; j++) { + r = r + H[i][j] * H[j][n]; + } + + if (e[i] < 0) { + z = w; + s = r; + } else { + l = i; + if (e[i] === 0) { + H[i][n] = w !== 0 ? -r / w : -r / (eps * norm); + } else { + x = H[i][i + 1]; + y = H[i + 1][i]; + q = (d[i] - p) * (d[i] - p) + e[i] * e[i]; + t = (x * s - z * r) / q; + H[i][n] = t; + H[i + 1][n] = + Math.abs(x) > Math.abs(z) ? (-r - w * t) / x : (-s - y * t) / z; + } + + t = Math.abs(H[i][n]); + if (eps * t * t > 1) { + for (j = i; j <= n; j++) { + H[j][n] = H[j][n] / t; + } + } + } + } + } else if (q < 0) { + l = n - 1; + + if (Math.abs(H[n][n - 1]) > Math.abs(H[n - 1][n])) { + H[n - 1][n - 1] = q / H[n][n - 1]; + H[n - 1][n] = -(H[n][n] - p) / H[n][n - 1]; + } else { + cdivres = cdiv(0, -H[n - 1][n], H[n - 1][n - 1] - p, q); + H[n - 1][n - 1] = cdivres[0]; + H[n - 1][n] = cdivres[1]; + } + + H[n][n - 1] = 0; + H[n][n] = 1; + for (i = n - 2; i >= 0; i--) { + ra = 0; + sa = 0; + for (j = l; j <= n; j++) { + ra = ra + H[i][j] * H[j][n - 1]; + sa = sa + H[i][j] * H[j][n]; + } + + w = H[i][i] - p; + + if (e[i] < 0) { + z = w; + r = ra; + s = sa; + } else { + l = i; + if (e[i] === 0) { + cdivres = cdiv(-ra, -sa, w, q); + H[i][n - 1] = cdivres[0]; + H[i][n] = cdivres[1]; + } else { + x = H[i][i + 1]; + y = H[i + 1][i]; + vr = (d[i] - p) * (d[i] - p) + e[i] * e[i] - q * q; + vi = (d[i] - p) * 2 * q; + if (vr === 0 && vi === 0) { + vr = + eps * + norm * + (Math.abs(w) + + Math.abs(q) + + Math.abs(x) + + Math.abs(y) + + Math.abs(z)); + } + cdivres = cdiv( + x * r - z * ra + q * sa, + x * s - z * sa - q * ra, + vr, + vi + ); + H[i][n - 1] = cdivres[0]; + H[i][n] = cdivres[1]; + if (Math.abs(x) > Math.abs(z) + Math.abs(q)) { + H[i + 1][n - 1] = (-ra - w * H[i][n - 1] + q * H[i][n]) / x; + H[i + 1][n] = (-sa - w * H[i][n] - q * H[i][n - 1]) / x; + } else { + cdivres = cdiv(-r - y * H[i][n - 1], -s - y * H[i][n], z, q); + H[i + 1][n - 1] = cdivres[0]; + H[i + 1][n] = cdivres[1]; + } + } + + t = Math.max(Math.abs(H[i][n - 1]), Math.abs(H[i][n])); + if (eps * t * t > 1) { + for (j = i; j <= n; j++) { + H[j][n - 1] = H[j][n - 1] / t; + H[j][n] = H[j][n] / t; + } + } + } + } + } + } + + for (i = 0; i < nn; i++) { + if (i < low || i > high) { + for (j = i; j < nn; j++) { + V[i][j] = H[i][j]; + } + } + } + + for (j = nn - 1; j >= low; j--) { + for (i = low; i <= high; i++) { + z = 0; + for (k = low; k <= Math.min(j, high); k++) { + z = z + V[i][k] * H[k][j]; + } + V[i][j] = z; + } + } +} + +function cdiv(xr, xi, yr, yi) { + var r, d; + if (Math.abs(yr) > Math.abs(yi)) { + r = yi / yr; + d = yr + r * yi; + return [(xr + r * xi) / d, (xi - r * xr) / d]; + } else { + r = yr / yi; + d = yi + r * yr; + return [(r * xr + xi) / d, (r * xi - xr) / d]; + } +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/dc/cholesky.js + + +/** + * @class CholeskyDecomposition + * @link https://github.com/lutzroeder/Mapack/blob/master/Source/CholeskyDecomposition.cs + * @param {Matrix} value + */ +class cholesky_CholeskyDecomposition { + constructor(value) { + value = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(value); + if (!value.isSymmetric()) { + throw new Error('Matrix is not symmetric'); + } + + var a = value; + var dimension = a.rows; + var l = new matrix_Matrix(dimension, dimension); + var positiveDefinite = true; + var i, j, k; + + for (j = 0; j < dimension; j++) { + var Lrowj = l[j]; + var d = 0; + for (k = 0; k < j; k++) { + var Lrowk = l[k]; + var s = 0; + for (i = 0; i < k; i++) { + s += Lrowk[i] * Lrowj[i]; + } + Lrowj[k] = s = (a.get(j, k) - s) / l[k][k]; + d = d + s * s; + } + + d = a.get(j, j) - d; + + positiveDefinite &= d > 0; + l[j][j] = Math.sqrt(Math.max(d, 0)); + for (k = j + 1; k < dimension; k++) { + l[j][k] = 0; + } + } + + if (!positiveDefinite) { + throw new Error('Matrix is not positive definite'); + } + + this.L = l; + } + + /** + * + * @param {Matrix} value + * @return {Matrix} + */ + solve(value) { + value = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(value); + + var l = this.L; + var dimension = l.rows; + + if (value.rows !== dimension) { + throw new Error('Matrix dimensions do not match'); + } + + var count = value.columns; + var B = value.clone(); + var i, j, k; + + for (k = 0; k < dimension; k++) { + for (j = 0; j < count; j++) { + for (i = 0; i < k; i++) { + B[k][j] -= B[i][j] * l[k][i]; + } + B[k][j] /= l[k][k]; + } + } + + for (k = dimension - 1; k >= 0; k--) { + for (j = 0; j < count; j++) { + for (i = k + 1; i < dimension; i++) { + B[k][j] -= B[i][j] * l[i][k]; + } + B[k][j] /= l[k][k]; + } + } + + return B; + } + + /** + * + * @return {Matrix} + */ + get lowerTriangularMatrix() { + return this.L; + } +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/index.js +/* concated harmony reexport default */__webpack_require__.d(__webpack_exports__, "default", function() { return matrix_Matrix; }); +/* concated harmony reexport Matrix */__webpack_require__.d(__webpack_exports__, "Matrix", function() { return matrix_Matrix; }); +/* concated harmony reexport abstractMatrix */__webpack_require__.d(__webpack_exports__, "abstractMatrix", function() { return AbstractMatrix; }); +/* concated harmony reexport wrap */__webpack_require__.d(__webpack_exports__, "wrap", function() { return wrap; }); +/* concated harmony reexport WrapperMatrix2D */__webpack_require__.d(__webpack_exports__, "WrapperMatrix2D", function() { return WrapperMatrix2D_WrapperMatrix2D; }); +/* concated harmony reexport WrapperMatrix1D */__webpack_require__.d(__webpack_exports__, "WrapperMatrix1D", function() { return WrapperMatrix1D_WrapperMatrix1D; }); +/* concated harmony reexport solve */__webpack_require__.d(__webpack_exports__, "solve", function() { return solve; }); +/* concated harmony reexport inverse */__webpack_require__.d(__webpack_exports__, "inverse", function() { return inverse; }); +/* concated harmony reexport linearDependencies */__webpack_require__.d(__webpack_exports__, "linearDependencies", function() { return linearDependencies; }); +/* concated harmony reexport SingularValueDecomposition */__webpack_require__.d(__webpack_exports__, "SingularValueDecomposition", function() { return svd_SingularValueDecomposition; }); +/* concated harmony reexport SVD */__webpack_require__.d(__webpack_exports__, "SVD", function() { return svd_SingularValueDecomposition; }); +/* concated harmony reexport EigenvalueDecomposition */__webpack_require__.d(__webpack_exports__, "EigenvalueDecomposition", function() { return evd_EigenvalueDecomposition; }); +/* concated harmony reexport EVD */__webpack_require__.d(__webpack_exports__, "EVD", function() { return evd_EigenvalueDecomposition; }); +/* concated harmony reexport CholeskyDecomposition */__webpack_require__.d(__webpack_exports__, "CholeskyDecomposition", function() { return cholesky_CholeskyDecomposition; }); +/* concated harmony reexport CHO */__webpack_require__.d(__webpack_exports__, "CHO", function() { return cholesky_CholeskyDecomposition; }); +/* concated harmony reexport LuDecomposition */__webpack_require__.d(__webpack_exports__, "LuDecomposition", function() { return lu_LuDecomposition; }); +/* concated harmony reexport LU */__webpack_require__.d(__webpack_exports__, "LU", function() { return lu_LuDecomposition; }); +/* concated harmony reexport QrDecomposition */__webpack_require__.d(__webpack_exports__, "QrDecomposition", function() { return qr_QrDecomposition; }); +/* concated harmony reexport QR */__webpack_require__.d(__webpack_exports__, "QR", function() { return qr_QrDecomposition; }); + + + + + + + + + + + + + + + /***/ }) /******/ ]); \ No newline at end of file diff --git a/lib/umap-js.min.js b/lib/umap-js.min.js index 2f3a4ba..b18fff0 100644 --- a/lib/umap-js.min.js +++ b/lib/umap-js.min.js @@ -1 +1 @@ -!function(t){var r={};function e(n){if(r[n])return r[n].exports;var i=r[n]={i:n,l:!1,exports:{}};return t[n].call(i.exports,i,i.exports,e),i.l=!0,i.exports}e.m=t,e.c=r,e.d=function(t,r,n){e.o(t,r)||Object.defineProperty(t,r,{enumerable:!0,get:n})},e.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},e.t=function(t,r){if(1&r&&(t=e(t)),8&r)return t;if(4&r&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(e.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&r&&"string"!=typeof t)for(var i in t)e.d(n,i,function(r){return t[r]}.bind(null,i));return n},e.n=function(t){var r=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(r,"a",r),r},e.o=function(t,r){return Object.prototype.hasOwnProperty.call(t,r)},e.p="",e(e.s=5)}([function(t,r,e){"use strict";const n=Object.prototype.toString;t.exports=function(t){return n.call(t).endsWith("Array]")}},function(t,r,e){"use strict";var n=this&&this.__values||function(t){var r="function"==typeof Symbol&&t[Symbol.iterator],e=0;return r?r.call(t):{next:function(){return t&&e>=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}};function i(t,r){return void 0===r&&(r=Math.random),Math.floor(r()*t)}function o(t){return void 0===t&&(t=Math.random),t()}function s(t){for(var r=[],e=0;er?t[e]:r;return r},r.max2d=function(t){for(var r=0,e=0;er?t[e][n]:r;return r},r.rejectionSample=function(t,r){for(var e=h(t),n=0;n=s[0])return 0;for(var h=0;h=s[0])return 0;s[0]=e,o[0]=n,a[0]=i;for(var h=0,u=0;;){var l=2*h+1,c=l+1,f=t[0][0].length;if(l>=f)break;if(c>=f){if(!(s[l]>e))break;u=l}else if(s[l]>=s[c]){if(!(ee.length;a++)1===i[a]&&n[a]=0?(i[s]=0,Math.floor(e[s])):-1}},function(t,r,e){"use strict";var n,i=this&&this.__read||function(t,r){var e="function"==typeof Symbol&&t[Symbol.iterator];if(!e)return t;var n,i,o=e.call(t),s=[];try{for(;(void 0===r||r-- >0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},o=this&&this.__spread||function(){for(var t=[],r=0;r=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}},a=this&&this.__importStar||function(t){if(t&&t.__esModule)return t;var r={};if(null!=t)for(var e in t)Object.hasOwnProperty.call(t,e)&&(r[e]=t[e]);return r.default=t,r};Object.defineProperty(r,"__esModule",{value:!0});var h=a(e(1)),u=function(){function t(t,r,e,n){this.entries=new Map,this.nRows=0,this.nCols=0,this.rows=o(t),this.cols=o(r),this.values=o(e);for(var i=0;ir?t:r})},r.multiplyScalar=function(t,r){return t.map(function(t){return t*r})},r.eliminateZeros=function(t){for(var r=new Set,e=t.getValues(),n=t.getRows(),i=t.getCols(),o=0;or?t[e]:r;return t.map(function(t){return t/r})},n.l1=function(t){for(var r=0,e=0;e0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},i=this&&this.__spread||function(){for(var t=[],r=0;r=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}},s=this&&this.__importStar||function(t){if(t&&t.__esModule)return t;var r={};if(null!=t)for(var e in t)Object.hasOwnProperty.call(t,e)&&(r[e]=t[e]);return r.default=t,r};Object.defineProperty(r,"__esModule",{value:!0});var a=s(e(1)),h=function(){return function(t,r,e,n){this.hyperplanes=t,this.offsets=r,this.children=e,this.indices=n}}();function u(t,r,e){for(var n=r,i=0;i0?0:1}r.FlatTree=h,r.makeForest=function(t,r,e,n){var o=Math.max(10,r);return a.range(e).map(function(r,e){return function(t,r,e,n){void 0===r&&(r=30);var i=a.range(t.length);return function t(r,e,n,i,o){if(void 0===n&&(n=30),e.length>n){var s=function(t,r,e){var n=t[0].length,i=a.tauRandInt(r.length,e),o=a.tauRandInt(r.length,e);o=(o+=i===o?1:0)%r.length;for(var s=r[i],h=r[o],u=0,l=a.zeros(n),c=0;c0?(g[c]=0,f+=1):(g[c]=1,m+=1)}var d=a.zeros(f),w=a.zeros(m);for(var c in f=0,m=0,a.range(g.length))0===g[c]?(d[f]=r[c],f+=1):(w[m]=r[c],m+=1);return{indicesLeft:d,indicesRight:w,hyperplane:l,offset:u}}(r,e,o),h=s.indicesLeft,u=s.indicesRight,l=s.hyperplane,c=s.offset,f=t(r,h,n,i+1,o),m=t(r,u,n,i+1,o),g={leftChild:f,rightChild:m,isLeaf:!1,hyperplane:l,offset:c};return g}var g={indices:e,isLeaf:!0};return g}(t,i,r,e,n)}(t,o,e,n)}).map(function(t){return function(t,r){var e=function t(r){return r.isLeaf?1:1+t(r.leftChild)+t(r.rightChild)}(t),n=function t(r){return r.isLeaf?1:t(r.leftChild)+t(r.rightChild)}(t),o=a.range(e).map(function(){return a.zeros(t.hyperplane.length)}),s=a.zeros(e),u=a.range(e).map(function(){return[-1,-1]}),l=a.range(n).map(function(){return a.range(r).map(function(){return-1})});return function t(r,e,n,o,s,a,h){var u;if(r.isLeaf)return o[a][0]=-h,(u=s[h]).splice.apply(u,i([0,r.indices.length],r.indices)),{nodeNum:a,leafNum:h+=1};e[a]=r.hyperplane,n[a]=r.offset,o[a][0]=a+1;var l=a,c=t(r.leftChild,e,n,o,s,a+1,h);return a=c.nodeNum,h=c.leafNum,o[l][1]=a+1,{nodeNum:(c=t(r.rightChild,e,n,o,s,a+1,h)).nodeNum,leafNum:c.leafNum}}(t,o,s,u,l,0,0),new h(o,s,u,l)}(t,o)})},r.makeLeafArray=function(t){var r,e;if(t.length>0){var n=[];try{for(var s=o(t),a=s.next();!a.done;a=s.next()){var h=a.value;n.push.apply(n,i(h.indices))}}catch(t){r={error:t}}finally{try{a&&!a.done&&(e=s.return)&&e.call(s)}finally{if(r)throw r.error}}return n}return[[-1]]},r.searchFlatTree=function(t,r){for(var e=0;r.children[e][0]>0;)e=0===u(r.hyperplanes[e],r.offsets[e],t)?r.children[e][0]:r.children[e][1];var n=-1*r.children[e][0];return r.indices[n]}},function(t,r,e){"use strict";Object.defineProperty(r,"__esModule",{value:!0});var n=e(6);window.UMAP=n.UMAP},function(t,r,e){"use strict";var n=this&&this.__awaiter||function(t,r,e,n){return new(e||(e=Promise))(function(i,o){function s(t){try{h(n.next(t))}catch(t){o(t)}}function a(t){try{h(n.throw(t))}catch(t){o(t)}}function h(t){t.done?i(t.value):new e(function(r){r(t.value)}).then(s,a)}h((n=n.apply(t,r||[])).next())})},i=this&&this.__generator||function(t,r){var e,n,i,o,s={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function a(o){return function(a){return function(o){if(e)throw new TypeError("Generator is already executing.");for(;s;)try{if(e=1,n&&(i=2&o[0]?n.return:o[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,o[1])).done)return i;switch(n=0,i&&(o=[2&o[0],i.value]),o[0]){case 0:case 1:i=o;break;case 4:return s.label++,{value:o[1],done:!1};case 5:s.label++,n=o[1],o=[0];continue;case 7:o=s.ops.pop(),s.trys.pop();continue;default:if(!(i=(i=s.trys).length>0&&i[i.length-1])&&(6===o[0]||2===o[0])){s=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},s=this&&this.__spread||function(){for(var t=[],r=0;r0&&i.set(o,u,c)}var f=l.transpose(i);return l.maximum(i,f)},t.prototype.transform=function(t){var r=this,e=this.X;if(void 0===e||0===e.length)throw new Error("No data has been fit.");var n=Math.floor(this.nNeighbors*this.transformQueueSize),i=c.initializeSearch(this.rpForest,e,t,n,this.initFromRandom,this.initFromTree),o=this.search(e,this.searchGraph,i,t),s=u.deheapSort(o),a=s.indices,h=s.weights;a=a.map(function(t){return t.slice(0,r.nNeighbors)}),h=h.map(function(t){return t.slice(0,r.nNeighbors)});var f=Math.max(0,this.localConnectivity-1),g=this.smoothKNNDistance(h,this.nNeighbors,f),p=g.sigmas,v=g.rhos,d=this.computeMembershipStrengths(a,h,p,v),w=d.rows,y=d.cols,b=d.vals,x=[t.length,e.length],M=new l.SparseMatrix(w,y,b,x),E=l.normalize(M,"l1"),R=l.getCSR(E),z=t.length,k=S(m.reshape2d(R.indices,z,this.nNeighbors),m.reshape2d(R.values,z,this.nNeighbors),this.embedding),N=this.nEpochs?this.nEpochs/3:M.nRows<=1e4?100:30,A=M.getValues().reduce(function(t,r){return r>t?r:t},0);M=M.map(function(t){return t0});if(g.length>=e){var p=Math.floor(e),v=e-p;p>0?(s[h]=g[p-1],v>1e-5&&(s[h]+=v*(g[p]-g[p-1]))):s[h]=v*g[0]}else g.length>0&&(s[h]=m.max(g));for(var d=0;d0?Math.exp(-b/c):1}if(Math.abs(w-o)<1e-5)break;w>o?c=(u+(l=c))/2:(u=c,l===1/0?c*=2:c=(u+l)/2)}if(a[h]=c,s[h]>0){var x=m.mean(f);a[h]<.001*x&&(a[h]=.001*x)}else{var M=m.mean(t.map(m.mean));a[h]<.001*M&&(a[h]=.001*M)}}return{sigmas:a,rhos:s}},t.prototype.computeMembershipStrengths=function(t,r,e,n){for(var i=t.length,o=t[0].length,s=m.zeros(i*o),a=m.zeros(i*o),h=m.zeros(i*o),u=0;u0&&(e[n]=r/i[n])}),e},t.prototype.assignOptimizationStateParameters=function(t){Object.assign(this.optimizationState,t)},t.prototype.prepareForOptimizationLoop=function(){var t=this.repulsionStrength,r=this.learningRate,e=this.negativeSampleRate,n=this.optimizationState,i=n.epochsPerSample,o=n.headEmbedding,a=n.tailEmbedding,h=o[0].length,u=o.length===a.length,l=i.map(function(t){return t/e}),c=s(l),f=s(i);this.assignOptimizationStateParameters({epochOfNextSample:f,epochOfNextNegativeSample:c,epochsPerNegativeSample:l,moveOther:u,initialAlpha:r,alpha:r,gamma:t,dim:h})},t.prototype.initializeOptimization=function(){var t=this.embedding,r=this.embedding,e=this.optimizationState,n=e.head,i=e.tail,o=e.epochsPerSample,s=this.getNEpochs(),a=this.graph.nCols,h=b(this.spread,this.minDist),u=h.a,l=h.b;this.assignOptimizationStateParameters({headEmbedding:t,tailEmbedding:r,head:n,tail:i,epochsPerSample:o,a:u,b:l,nEpochs:s,nVertices:a})},t.prototype.optimizeLayoutStep=function(t){for(var r=this.optimizationState,e=r.head,n=r.tail,i=r.headEmbedding,o=r.tailEmbedding,s=r.epochsPerSample,a=r.epochOfNextSample,h=r.epochOfNextNegativeSample,u=r.epochsPerNegativeSample,l=r.moveOther,c=r.initialAlpha,f=r.alpha,g=r.gamma,p=r.a,v=r.b,d=r.dim,b=r.nEpochs,x=r.nVertices,M=0;Mt)){var S=e[M],E=n[M],R=i[S],z=o[E],k=y(R,z),N=0;k>0&&(N=-2*p*v*Math.pow(k,v-1),N/=p*Math.pow(k,v)+1);for(var A=0;A0)O=2*g*v,O/=(.001+I)*(p*Math.pow(I,v)+1);else if(S===P)continue;for(A=0;A0&&(_=w(O*(R[A]-j[A]),4)),R[A]+=_*f}}h[M]+=C*u[M]}return r.alpha=c*(1-t/b),r.currentEpoch+=1,i},t.prototype.optimizeLayoutAsync=function(t){var r=this;return void 0===t&&(t=function(){return!0}),new Promise(function(e,o){var s=function(){return n(r,void 0,void 0,function(){var r,n,a,h,u,l;return i(this,function(i){try{if(r=this.optimizationState,n=r.nEpochs,a=r.currentEpoch,this.embedding=this.optimizeLayoutStep(a),h=this.optimizationState.currentEpoch,u=!1===t(h),l=h===n,u||l)return[2,e(l)];s()}catch(t){o(t)}return[2]})})};s()})},t.prototype.optimizeLayout=function(t){void 0===t&&(t=function(){return!0});for(var r=!1,e=[];!r;){var n=this.optimizationState,i=n.nEpochs,o=n.currentEpoch;e=this.optimizeLayoutStep(o);var s=this.optimizationState.currentEpoch,a=!1===t(s);r=s===i||a}return e},t.prototype.getNEpochs=function(){var t=this.graph;if(this.nEpochs>0)return this.nEpochs;var r=t.nRows;return r<=2500?500:r<=5e3?400:r<=7500?300:200},t}();function v(t,r){for(var e=0,n=0;nr?r:t<-r?-r:t}function y(t,r){for(var e=0,n=0;n=r?Math.exp(-(e[i]-r)/t):n}),i={x:e,y:n},s={damping:1.5,initialValues:[.5,.5],gradientDifference:.1,maxIterations:100,errorTolerance:.01},a=g.default(i,function(t){var r=o(t,2),e=r[0],n=r[1];return function(t){return 1/(1+e*Math.pow(t,2*n))}},s).parameterValues,h=o(a,2);return{a:h[0],b:h[1]}}function x(t,r,e,n){return void 0===e&&(e=1),void 0===n&&(n=5),t.map(function(t,i,o){return-1===r[i]||-1===r[o]?t*Math.exp(-e):r[i]!==r[o]?t*Math.exp(-n):t})}function M(t){t=l.normalize(t,"max");var r=l.transpose(t),e=l.pairwiseMultiply(r,t);return t=l.add(t,l.subtract(r,e)),l.eliminateZeros(t)}function S(t,r,e){for(var n=m.zeros(t.length).map(function(t){return m.zeros(e[0].length)}),i=0;i=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}},i=this&&this.__importStar||function(t){if(t&&t.__esModule)return t;var r={};if(null!=t)for(var e in t)Object.hasOwnProperty.call(t,e)&&(r[e]=t[e]);return r.default=t,r};Object.defineProperty(r,"__esModule",{value:!0});var o=i(e(2)),s=i(e(3)),a=i(e(4)),h=i(e(1));r.makeNNDescent=function(t,r){return function(e,n,i,s,a,u,l,c){void 0===s&&(s=10),void 0===a&&(a=50),void 0===u&&(u=.001),void 0===l&&(l=.5),void 0===c&&(c=!0);for(var f=e.length,m=o.makeHeap(e.length,i),g=0;gr&&(r=t[e]);return r};var a=function(t){if(!o()(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");for(var r=t[0],e=1;e1&&void 0!==arguments[1]?arguments[1]:{};if(!o()(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");if(void 0!==e.output){if(!o()(e.output))throw new TypeError("output option must be an array if specified");r=e.output}else r=new Array(t.length);var n=a(t),i=s(t);if(n===i)throw new RangeError("minimum and maximum input values are equal. Cannot rescale a constant array");var h=e.min,u=void 0===h?e.autoMinMax?n:0:h,l=e.max,c=void 0===l?e.autoMinMax?i:1:l;if(u>=c)throw new RangeError("min option must be smaller than max option");for(var f=(c-u)/(i-n),m=0;mMath.abs(h[i])&&(i=r);if(i!==e){for(n=0;n=0;i--){for(n=0;no?t[i][o]:i===o?1:0;return n}get upperTriangularMatrix(){for(var t=this.LU,r=t.rows,e=t.columns,n=new P(r,e),i=0;iMath.abs(r)?(e=r/t,Math.abs(t)*Math.sqrt(1+e*e)):0!==r?(e=t/r,Math.abs(r)*Math.sqrt(1+e*e)):0}function c(t,r,e){for(var n=new Array(t),i=0;i=0;t--)if(0!==v[t]){for(let r=t+1;r=0;t--){if(t0;){let t,r;for(t=R-2;t>=-1&&-1!==t;t--){const r=Number.MIN_VALUE+k*Math.abs(v[t]+Math.abs(v[t+1]));if(Math.abs(y[t])<=r||Number.isNaN(y[t])){y[t]=0;break}}if(t===R-2)r=4;else{let e;for(e=R-1;e>=t&&e!==t;e--){let r=(e!==R?Math.abs(y[e]):0)+(e!==t+1?Math.abs(y[e-1]):0);if(Math.abs(v[e])<=k*r){v[e]=0;break}}e===t?r=3:e===R-1?r=1:(r=2,t=e)}switch(t++,r){case 1:{let r=y[R-2];y[R-2]=0;for(let e=R-2;e>=t;e--){let i=l(v[e],r),o=v[e]/i,s=r/i;if(v[e]=i,e!==t&&(r=-s*y[e-1],y[e-1]=o*y[e-1]),u)for(let t=0;t=v[t+1]);){let r=v[t];if(v[t]=v[t+1],v[t+1]=r,u&&tr?i[o][e]=t[o][e]/this.s[e]:i[o][e]=0;var o=this.U,s=o.length,a=o[0].length,h=new P(e,s);for(let t=0;tt&&r++;return r}get diagonal(){return this.s}get threshold(){return Number.EPSILON/2*Math.max(this.m,this.n)*this.s[0]}get leftSingularVectors(){return P.isMatrix(this.U)||(this.U=new P(this.U)),this.U}get rightSingularVectors(){return P.isMatrix(this.V)||(this.V=new P(this.V)),this.V}get diagonalMatrix(){return P.diag(this.s)}}function m(t,r,e){var n=e?t.rows:t.rows-1;if(r<0||r>n)throw new RangeError("Row index out of range")}function g(t,r,e){var n=e?t.columns:t.columns-1;if(r<0||r>n)throw new RangeError("Column index out of range")}function p(t,r){if(r.to1DArray&&(r=r.to1DArray()),r.length!==t.columns)throw new RangeError("vector size must be the same as the number of columns");return r}function v(t,r){if(r.to1DArray&&(r=r.to1DArray()),r.length!==t.rows)throw new RangeError("vector size must be the same as the number of rows");return r}function d(t,r,e){return{row:w(t,r),column:y(t,e)}}function w(t,r){if("object"!=typeof r)throw new TypeError("unexpected type for row indices");if(r.some(r=>r<0||r>=t.rows))throw new RangeError("row indices are out of range");return Array.isArray(r)||(r=Array.from(r)),r}function y(t,r){if("object"!=typeof r)throw new TypeError("unexpected type for column indices");if(r.some(r=>r<0||r>=t.columns))throw new RangeError("column indices are out of range");return Array.isArray(r)||(r=Array.from(r)),r}function b(t,r,e,n,i){if(5!==arguments.length)throw new RangeError("expected 4 arguments");if(x("startRow",r),x("endRow",e),x("startColumn",n),x("endColumn",i),r>e||n>i||r<0||r>=t.rows||e<0||e>=t.rows||n<0||n>=t.columns||i<0||i>=t.columns)throw new RangeError("Submatrix indices are out of range")}function x(t,r){if("number"!=typeof r)throw new TypeError(`${t} must be a number`)}class M extends(V()){constructor(t,r,e){super(),this.matrix=t,this.rows=r,this.columns=e}static get[Symbol.species](){return P}}class S extends M{constructor(t){super(t,t.columns,t.rows)}set(t,r,e){return this.matrix.set(r,t,e),this}get(t,r){return this.matrix.get(r,t)}}class E extends M{constructor(t,r){super(t,1,t.columns),this.row=r}set(t,r,e){return this.matrix.set(this.row,r,e),this}get(t,r){return this.matrix.get(this.row,r)}}class R extends M{constructor(t,r,e,n,i){b(t,r,e,n,i),super(t,e-r+1,i-n+1),this.startRow=r,this.startColumn=n}set(t,r,e){return this.matrix.set(this.startRow+t,this.startColumn+r,e),this}get(t,r){return this.matrix.get(this.startRow+t,this.startColumn+r)}}class z extends M{constructor(t,r,e){var n=d(t,r,e);super(t,n.row.length,n.column.length),this.rowIndices=n.row,this.columnIndices=n.column}set(t,r,e){return this.matrix.set(this.rowIndices[t],this.columnIndices[r],e),this}get(t,r){return this.matrix.get(this.rowIndices[t],this.columnIndices[r])}}class k extends M{constructor(t,r){super(t,(r=w(t,r)).length,t.columns),this.rowIndices=r}set(t,r,e){return this.matrix.set(this.rowIndices[t],r,e),this}get(t,r){return this.matrix.get(this.rowIndices[t],r)}}class N extends M{constructor(t,r){r=y(t,r),super(t,t.rows,r.length),this.columnIndices=r}set(t,r,e){return this.matrix.set(t,this.columnIndices[r],e),this}get(t,r){return this.matrix.get(t,this.columnIndices[r])}}class A extends M{constructor(t,r){super(t,t.rows,1),this.column=r}set(t,r,e){return this.matrix.set(t,this.column,e),this}get(t){return this.matrix.get(t,this.column)}}class _ extends M{constructor(t){super(t,t.rows,t.columns)}set(t,r,e){return this.matrix.set(this.rows-t-1,r,e),this}get(t,r){return this.matrix.get(this.rows-t-1,r)}}class C extends M{constructor(t){super(t,t.rows,t.columns)}set(t,r,e){return this.matrix.set(t,this.columns-r-1,e),this}get(t,r){return this.matrix.get(t,this.columns-r-1)}}function V(t){void 0===t&&(t=Object);class r extends t{static get[Symbol.species](){return this}static from1DArray(t,r,e){if(t*r!==e.length)throw new RangeError("Data length does not match given dimensions");for(var n=new this(t,r),i=0;ie?(i=!0,e=r):(n=!1,i=!0);t++}return n}isReducedEchelonForm(){let t=0,r=0,e=-1,n=!0,i=!1;for(;te?(i=!0,e=r):(n=!1,i=!0);for(let e=r+1;et&&(t=this.get(r,e));return t}maxIndex(){for(var t=this.get(0,0),r=[0,0],e=0;et&&(t=this.get(e,n),r[0]=e,r[1]=n);return r}min(){for(var t=this.get(0,0),r=0;rr&&(r=this.get(t,e));return r}maxRowIndex(t){m(this,t);for(var r=this.get(t,0),e=[t,0],n=1;nr&&(r=this.get(t,n),e[1]=n);return e}minRow(t){m(this,t);for(var r=this.get(t,0),e=1;er&&(r=this.get(e,t));return r}maxColumnIndex(t){g(this,t);for(var r=this.get(0,t),e=[0,t],n=1;nr&&(r=this.get(n,t),e[0]=n);return e}minColumn(t){g(this,t);for(var r=this.get(0,t),e=1;e=(r=void 0===r?1:r))throw new RangeError("min should be strictly smaller than max");for(var e=this.constructor.empty(this.rows,this.columns),n=0;n=(r=void 0===r?1:r))throw new RangeError("min should be strictly smaller than max");for(var e=this.constructor.empty(this.rows,this.columns),n=0;ne||r<0||r>=this.columns||e<0||e>=this.columns)throw new RangeError("Argument out of range");for(var n=new this.constructor[Symbol.species](t.length,e-r+1),i=0;i=this.rows)throw new RangeError(`Row index out of range: ${t[i]}`);n.set(i,o-r,this.get(t[i],o))}return n}subMatrixColumn(t,r,e){if(void 0===r&&(r=0),void 0===e&&(e=this.rows-1),r>e||r<0||r>=this.rows||e<0||e>=this.rows)throw new RangeError("Argument out of range");for(var n=new this.constructor[Symbol.species](e-r+1,t.length),i=0;i=this.columns)throw new RangeError(`Column index out of range: ${t[i]}`);n.set(o-r,i,this.get(o,t[i]))}return n}setSubMatrix(t,r,e){b(this,r,r+(t=this.constructor.checkMatrix(t)).rows-1,e,e+t.columns-1);for(var n=0;nt?i[o]=1/i[o]:i[o]=0;return i=this.constructor[Symbol.species].diag(i),n.mmul(i.mmul(e.transposeView()))}clone(){for(var t=new this.constructor[Symbol.species](this.rows,this.columns),r=0;r>","signPropagatingRightShift"],[">>>","rightShift","zeroFillRightShift"]]){var a=o($("\n(function %name%(value) {\n if (typeof value === 'number') return this.%name%S(value);\n return this.%name%M(value);\n})\n",{name:s[1],op:s[0]})),l=o($("\n(function %name%S(value) {\n for (var i = 0; i < this.rows; i++) {\n for (var j = 0; j < this.columns; j++) {\n this.set(i, j, this.get(i, j) %op% value);\n }\n }\n return this;\n})\n",{name:`${s[1]}S`,op:s[0]})),c=o($("\n(function %name%M(matrix) {\n matrix = this.constructor.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 (var i = 0; i < this.rows; i++) {\n for (var j = 0; j < this.columns; j++) {\n this.set(i, j, this.get(i, j) %op% matrix.get(i, j));\n }\n }\n return this;\n})\n",{name:`${s[1]}M`,op:s[0]})),w=o($("\n(function %name%(matrix, value) {\n var newMatrix = new this[Symbol.species](matrix);\n return newMatrix.%name%(value);\n})\n",{name:s[1]}));for(n=1;n0){if(super(t),!(Number.isInteger(r)&&r>0))throw new TypeError("nColumns must be a positive integer");for(e=0;e=0;o--){for(i=0;i=0;e--){for(t=0;ta(t)),u=function(t,r,e,n,i){const o=e.length,s=t.x.length;for(var a=new Array(o),h=0;h=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}};function i(t,r){return void 0===r&&(r=Math.random),Math.floor(r()*t)}function o(t){return void 0===t&&(t=Math.random),t()}function s(t){for(var r=[],e=0;er?t[e]:r;return r},r.max2d=function(t){for(var r=0,e=0;er?t[e][n]:r;return r},r.rejectionSample=function(t,r){for(var e=h(t),n=0;n=s[0])return 0;for(var h=0;h=s[0])return 0;s[0]=e,o[0]=n,a[0]=i;for(var h=0,u=0;;){var l=2*h+1,c=l+1,f=t[0][0].length;if(l>=f)break;if(c>=f){if(!(s[l]>e))break;u=l}else if(s[l]>=s[c]){if(!(ee.length;a++)1===i[a]&&n[a]=0?(i[s]=0,Math.floor(e[s])):-1}},function(t,r,e){"use strict";var n,i=this&&this.__read||function(t,r){var e="function"==typeof Symbol&&t[Symbol.iterator];if(!e)return t;var n,i,o=e.call(t),s=[];try{for(;(void 0===r||r-- >0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},o=this&&this.__spread||function(){for(var t=[],r=0;r=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}},a=this&&this.__importStar||function(t){if(t&&t.__esModule)return t;var r={};if(null!=t)for(var e in t)Object.hasOwnProperty.call(t,e)&&(r[e]=t[e]);return r.default=t,r};Object.defineProperty(r,"__esModule",{value:!0});var h=a(e(1)),u=function(){function t(t,r,e,n){this.entries=new Map,this.nRows=0,this.nCols=0,this.rows=o(t),this.cols=o(r),this.values=o(e);for(var i=0;ir?t:r})},r.multiplyScalar=function(t,r){return t.map(function(t){return t*r})},r.eliminateZeros=function(t){for(var r=new Set,e=t.getValues(),n=t.getRows(),i=t.getCols(),o=0;or?t[e]:r;return t.map(function(t){return t/r})},n.l1=function(t){for(var r=0,e=0;e0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},i=this&&this.__spread||function(){for(var t=[],r=0;r=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}},s=this&&this.__importStar||function(t){if(t&&t.__esModule)return t;var r={};if(null!=t)for(var e in t)Object.hasOwnProperty.call(t,e)&&(r[e]=t[e]);return r.default=t,r};Object.defineProperty(r,"__esModule",{value:!0});var a=s(e(1)),h=function(){return function(t,r,e,n){this.hyperplanes=t,this.offsets=r,this.children=e,this.indices=n}}();function u(t,r,e){for(var n=r,i=0;i0?0:1}r.FlatTree=h,r.makeForest=function(t,r,e,n){var o=Math.max(10,r);return a.range(e).map(function(r,e){return function(t,r,e,n){void 0===r&&(r=30);var i=a.range(t.length);return function t(r,e,n,i,o){if(void 0===n&&(n=30),e.length>n){var s=function(t,r,e){var n=t[0].length,i=a.tauRandInt(r.length,e),o=a.tauRandInt(r.length,e);o=(o+=i===o?1:0)%r.length;for(var s=r[i],h=r[o],u=0,l=a.zeros(n),c=0;c0?(g[c]=0,f+=1):(g[c]=1,m+=1)}var d=a.zeros(f),w=a.zeros(m);for(var c in f=0,m=0,a.range(g.length))0===g[c]?(d[f]=r[c],f+=1):(w[m]=r[c],m+=1);return{indicesLeft:d,indicesRight:w,hyperplane:l,offset:u}}(r,e,o),h=s.indicesLeft,u=s.indicesRight,l=s.hyperplane,c=s.offset,f=t(r,h,n,i+1,o),m=t(r,u,n,i+1,o),g={leftChild:f,rightChild:m,isLeaf:!1,hyperplane:l,offset:c};return g}var g={indices:e,isLeaf:!0};return g}(t,i,r,e,n)}(t,o,e,n)}).map(function(t){return function(t,r){var e=function t(r){return r.isLeaf?1:1+t(r.leftChild)+t(r.rightChild)}(t),n=function t(r){return r.isLeaf?1:t(r.leftChild)+t(r.rightChild)}(t),o=a.range(e).map(function(){return a.zeros(t.hyperplane.length)}),s=a.zeros(e),u=a.range(e).map(function(){return[-1,-1]}),l=a.range(n).map(function(){return a.range(r).map(function(){return-1})});return function t(r,e,n,o,s,a,h){var u;if(r.isLeaf)return o[a][0]=-h,(u=s[h]).splice.apply(u,i([0,r.indices.length],r.indices)),{nodeNum:a,leafNum:h+=1};e[a]=r.hyperplane,n[a]=r.offset,o[a][0]=a+1;var l=a,c=t(r.leftChild,e,n,o,s,a+1,h);return a=c.nodeNum,h=c.leafNum,o[l][1]=a+1,{nodeNum:(c=t(r.rightChild,e,n,o,s,a+1,h)).nodeNum,leafNum:c.leafNum}}(t,o,s,u,l,0,0),new h(o,s,u,l)}(t,o)})},r.makeLeafArray=function(t){var r,e;if(t.length>0){var n=[];try{for(var s=o(t),a=s.next();!a.done;a=s.next()){var h=a.value;n.push.apply(n,i(h.indices))}}catch(t){r={error:t}}finally{try{a&&!a.done&&(e=s.return)&&e.call(s)}finally{if(r)throw r.error}}return n}return[[-1]]},r.searchFlatTree=function(t,r){for(var e=0;r.children[e][0]>0;)e=0===u(r.hyperplanes[e],r.offsets[e],t)?r.children[e][0]:r.children[e][1];var n=-1*r.children[e][0];return r.indices[n]}},function(t,r,e){"use strict";Object.defineProperty(r,"__esModule",{value:!0});var n=e(6);window.UMAP=n.UMAP},function(t,r,e){"use strict";var n=this&&this.__awaiter||function(t,r,e,n){return new(e||(e=Promise))(function(i,o){function s(t){try{h(n.next(t))}catch(t){o(t)}}function a(t){try{h(n.throw(t))}catch(t){o(t)}}function h(t){t.done?i(t.value):new e(function(r){r(t.value)}).then(s,a)}h((n=n.apply(t,r||[])).next())})},i=this&&this.__generator||function(t,r){var e,n,i,o,s={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function a(o){return function(a){return function(o){if(e)throw new TypeError("Generator is already executing.");for(;s;)try{if(e=1,n&&(i=2&o[0]?n.return:o[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,o[1])).done)return i;switch(n=0,i&&(o=[2&o[0],i.value]),o[0]){case 0:case 1:i=o;break;case 4:return s.label++,{value:o[1],done:!1};case 5:s.label++,n=o[1],o=[0];continue;case 7:o=s.ops.pop(),s.trys.pop();continue;default:if(!(i=(i=s.trys).length>0&&i[i.length-1])&&(6===o[0]||2===o[0])){s=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},s=this&&this.__spread||function(){for(var t=[],r=0;r0&&i.set(o,u,c)}var f=l.transpose(i);return l.maximum(i,f)},t.prototype.transform=function(t){var r=this,e=this.X;if(void 0===e||0===e.length)throw new Error("No data has been fit.");var n=Math.floor(this.nNeighbors*this.transformQueueSize),i=c.initializeSearch(this.rpForest,e,t,n,this.initFromRandom,this.initFromTree),o=this.search(e,this.searchGraph,i,t),s=u.deheapSort(o),a=s.indices,h=s.weights;a=a.map(function(t){return t.slice(0,r.nNeighbors)}),h=h.map(function(t){return t.slice(0,r.nNeighbors)});var f=Math.max(0,this.localConnectivity-1),g=this.smoothKNNDistance(h,this.nNeighbors,f),p=g.sigmas,v=g.rhos,d=this.computeMembershipStrengths(a,h,p,v),w=d.rows,y=d.cols,b=d.vals,M=[t.length,e.length],x=new l.SparseMatrix(w,y,b,M),E=l.normalize(x,"l1"),R=l.getCSR(E),k=t.length,z=S(m.reshape2d(R.indices,k,this.nNeighbors),m.reshape2d(R.values,k,this.nNeighbors),this.embedding),N=this.nEpochs?this.nEpochs/3:x.nRows<=1e4?100:30,A=x.getValues().reduce(function(t,r){return r>t?r:t},0);x=x.map(function(t){return t0});if(g.length>=e){var p=Math.floor(e),v=e-p;p>0?(s[h]=g[p-1],v>1e-5&&(s[h]+=v*(g[p]-g[p-1]))):s[h]=v*g[0]}else g.length>0&&(s[h]=m.max(g));for(var d=0;d0?Math.exp(-b/c):1}if(Math.abs(w-o)<1e-5)break;w>o?c=(u+(l=c))/2:(u=c,l===1/0?c*=2:c=(u+l)/2)}if(a[h]=c,s[h]>0){var M=m.mean(f);a[h]<.001*M&&(a[h]=.001*M)}else{var x=m.mean(t.map(m.mean));a[h]<.001*x&&(a[h]=.001*x)}}return{sigmas:a,rhos:s}},t.prototype.computeMembershipStrengths=function(t,r,e,n){for(var i=t.length,o=t[0].length,s=m.zeros(i*o),a=m.zeros(i*o),h=m.zeros(i*o),u=0;u0&&(e[n]=r/i[n])}),e},t.prototype.assignOptimizationStateParameters=function(t){Object.assign(this.optimizationState,t)},t.prototype.prepareForOptimizationLoop=function(){var t=this.repulsionStrength,r=this.learningRate,e=this.negativeSampleRate,n=this.optimizationState,i=n.epochsPerSample,o=n.headEmbedding,a=n.tailEmbedding,h=o[0].length,u=o.length===a.length,l=i.map(function(t){return t/e}),c=s(l),f=s(i);this.assignOptimizationStateParameters({epochOfNextSample:f,epochOfNextNegativeSample:c,epochsPerNegativeSample:l,moveOther:u,initialAlpha:r,alpha:r,gamma:t,dim:h})},t.prototype.initializeOptimization=function(){var t=this.embedding,r=this.embedding,e=this.optimizationState,n=e.head,i=e.tail,o=e.epochsPerSample,s=this.getNEpochs(),a=this.graph.nCols,h=b(this.spread,this.minDist),u=h.a,l=h.b;this.assignOptimizationStateParameters({headEmbedding:t,tailEmbedding:r,head:n,tail:i,epochsPerSample:o,a:u,b:l,nEpochs:s,nVertices:a})},t.prototype.optimizeLayoutStep=function(t){for(var r=this.optimizationState,e=r.head,n=r.tail,i=r.headEmbedding,o=r.tailEmbedding,s=r.epochsPerSample,a=r.epochOfNextSample,h=r.epochOfNextNegativeSample,u=r.epochsPerNegativeSample,l=r.moveOther,c=r.initialAlpha,f=r.alpha,g=r.gamma,p=r.a,v=r.b,d=r.dim,b=r.nEpochs,M=r.nVertices,x=0;xt)){var S=e[x],E=n[x],R=i[S],k=o[E],z=y(R,k),N=0;z>0&&(N=-2*p*v*Math.pow(z,v-1),N/=p*Math.pow(z,v)+1);for(var A=0;A0)O=2*g*v,O/=(.001+j)*(p*Math.pow(j,v)+1);else if(S===P)continue;for(A=0;A0&&(V=w(O*(R[A]-I[A]),4)),R[A]+=V*f}}h[x]+=C*u[x]}return r.alpha=c*(1-t/b),r.currentEpoch+=1,i},t.prototype.optimizeLayoutAsync=function(t){var r=this;return void 0===t&&(t=function(){return!0}),new Promise(function(e,o){var s=function(){return n(r,void 0,void 0,function(){var r,n,a,h,u,l;return i(this,function(i){try{if(r=this.optimizationState,n=r.nEpochs,a=r.currentEpoch,this.embedding=this.optimizeLayoutStep(a),h=this.optimizationState.currentEpoch,u=!1===t(h),l=h===n,u||l)return[2,e(l)];s()}catch(t){o(t)}return[2]})})};s()})},t.prototype.optimizeLayout=function(t){void 0===t&&(t=function(){return!0});for(var r=!1,e=[];!r;){var n=this.optimizationState,i=n.nEpochs,o=n.currentEpoch;e=this.optimizeLayoutStep(o);var s=this.optimizationState.currentEpoch,a=!1===t(s);r=s===i||a}return e},t.prototype.getNEpochs=function(){var t=this.graph;if(this.nEpochs>0)return this.nEpochs;var r=t.nRows;return r<=2500?500:r<=5e3?400:r<=7500?300:200},t}();function v(t,r){for(var e=0,n=0;nr?r:t<-r?-r:t}function y(t,r){for(var e=0,n=0;n=r?Math.exp(-(e[i]-r)/t):n}),i={x:e,y:n},s={damping:1.5,initialValues:[.5,.5],gradientDifference:.1,maxIterations:100,errorTolerance:.01},a=g.default(i,function(t){var r=o(t,2),e=r[0],n=r[1];return function(t){return 1/(1+e*Math.pow(t,2*n))}},s).parameterValues,h=o(a,2);return{a:h[0],b:h[1]}}function M(t,r,e,n){return void 0===e&&(e=1),void 0===n&&(n=5),t.map(function(t,i,o){return-1===r[i]||-1===r[o]?t*Math.exp(-e):r[i]!==r[o]?t*Math.exp(-n):t})}function x(t){t=l.normalize(t,"max");var r=l.transpose(t),e=l.pairwiseMultiply(r,t);return t=l.add(t,l.subtract(r,e)),l.eliminateZeros(t)}function S(t,r,e){for(var n=m.zeros(t.length).map(function(t){return m.zeros(e[0].length)}),i=0;i=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}},i=this&&this.__importStar||function(t){if(t&&t.__esModule)return t;var r={};if(null!=t)for(var e in t)Object.hasOwnProperty.call(t,e)&&(r[e]=t[e]);return r.default=t,r};Object.defineProperty(r,"__esModule",{value:!0});var o=i(e(2)),s=i(e(3)),a=i(e(4)),h=i(e(1));r.makeNNDescent=function(t,r){return function(e,n,i,s,a,u,l,c){void 0===s&&(s=10),void 0===a&&(a=50),void 0===u&&(u=.001),void 0===l&&(l=.5),void 0===c&&(c=!0);for(var f=e.length,m=o.makeHeap(e.length,i),g=0;gr&&(r=t[e]);return r};var s=function(t){if(!i()(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");for(var r=t[0],e=1;e1&&void 0!==arguments[1]?arguments[1]:{};if(!i()(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");if(void 0!==e.output){if(!i()(e.output))throw new TypeError("output option must be an array if specified");r=e.output}else r=new Array(t.length);var n=s(t),a=o(t);if(n===a)throw new RangeError("minimum and maximum input values are equal. Cannot rescale a constant array");var h=e.min,u=void 0===h?e.autoMinMax?n:0:h,l=e.max,c=void 0===l?e.autoMinMax?a:1:l;if(u>=c)throw new RangeError("min option must be smaller than max option");for(var f=(c-u)/(a-n),m=0;mMath.abs(h[i])&&(i=r);if(i!==e){for(n=0;n=0;i--){for(n=0;no?t[i][o]:i===o?1:0;return n}get upperTriangularMatrix(){for(var t=this.LU,r=t.rows,e=t.columns,n=new _(r,e),i=0;iMath.abs(r)?(e=r/t,Math.abs(t)*Math.sqrt(1+e*e)):0!==r?(e=t/r,Math.abs(r)*Math.sqrt(1+e*e)):0}function l(t,r,e){for(var n=new Array(t),i=0;i=0;t--)if(0!==v[t]){for(let r=t+1;r=0;t--){if(t0;){let t,r;for(t=R-2;t>=-1&&-1!==t;t--){const r=Number.MIN_VALUE+z*Math.abs(v[t]+Math.abs(v[t+1]));if(Math.abs(y[t])<=r||Number.isNaN(y[t])){y[t]=0;break}}if(t===R-2)r=4;else{let e;for(e=R-1;e>=t&&e!==t;e--){let r=(e!==R?Math.abs(y[e]):0)+(e!==t+1?Math.abs(y[e-1]):0);if(Math.abs(v[e])<=z*r){v[e]=0;break}}e===t?r=3:e===R-1?r=1:(r=2,t=e)}switch(t++,r){case 1:{let r=y[R-2];y[R-2]=0;for(let e=R-2;e>=t;e--){let i=u(v[e],r),o=v[e]/i,s=r/i;if(v[e]=i,e!==t&&(r=-s*y[e-1],y[e-1]=o*y[e-1]),c)for(let t=0;t=v[t+1]);){let r=v[t];if(v[t]=v[t+1],v[t+1]=r,c&&tr?i[o][e]=t[o][e]/this.s[e]:i[o][e]=0;var o=this.U,s=o.length,a=o[0].length,h=new _(e,s);for(let t=0;tt&&r++;return r}get diagonal(){return this.s}get threshold(){return Number.EPSILON/2*Math.max(this.m,this.n)*this.s[0]}get leftSingularVectors(){return _.isMatrix(this.U)||(this.U=new _(this.U)),this.U}get rightSingularVectors(){return _.isMatrix(this.V)||(this.V=new _(this.V)),this.V}get diagonalMatrix(){return _.diag(this.s)}}function f(t,r,e){var n=e?t.rows:t.rows-1;if(r<0||r>n)throw new RangeError("Row index out of range")}function m(t,r,e){var n=e?t.columns:t.columns-1;if(r<0||r>n)throw new RangeError("Column index out of range")}function g(t,r){if(r.to1DArray&&(r=r.to1DArray()),r.length!==t.columns)throw new RangeError("vector size must be the same as the number of columns");return r}function p(t,r){if(r.to1DArray&&(r=r.to1DArray()),r.length!==t.rows)throw new RangeError("vector size must be the same as the number of rows");return r}function v(t,r,e){return{row:d(t,r),column:w(t,e)}}function d(t,r){if("object"!=typeof r)throw new TypeError("unexpected type for row indices");if(r.some(r=>r<0||r>=t.rows))throw new RangeError("row indices are out of range");return Array.isArray(r)||(r=Array.from(r)),r}function w(t,r){if("object"!=typeof r)throw new TypeError("unexpected type for column indices");if(r.some(r=>r<0||r>=t.columns))throw new RangeError("column indices are out of range");return Array.isArray(r)||(r=Array.from(r)),r}function y(t,r,e,n,i){if(5!==arguments.length)throw new RangeError("expected 4 arguments");if(b("startRow",r),b("endRow",e),b("startColumn",n),b("endColumn",i),r>e||n>i||r<0||r>=t.rows||e<0||e>=t.rows||n<0||n>=t.columns||i<0||i>=t.columns)throw new RangeError("Submatrix indices are out of range")}function b(t,r){if("number"!=typeof r)throw new TypeError(`${t} must be a number`)}class M extends(C()){constructor(t,r,e){super(),this.matrix=t,this.rows=r,this.columns=e}static get[Symbol.species](){return _}}class x extends M{constructor(t){super(t,t.columns,t.rows)}set(t,r,e){return this.matrix.set(r,t,e),this}get(t,r){return this.matrix.get(r,t)}}class S extends M{constructor(t,r){super(t,1,t.columns),this.row=r}set(t,r,e){return this.matrix.set(this.row,r,e),this}get(t,r){return this.matrix.get(this.row,r)}}class E extends M{constructor(t,r,e,n,i){y(t,r,e,n,i),super(t,e-r+1,i-n+1),this.startRow=r,this.startColumn=n}set(t,r,e){return this.matrix.set(this.startRow+t,this.startColumn+r,e),this}get(t,r){return this.matrix.get(this.startRow+t,this.startColumn+r)}}class R extends M{constructor(t,r,e){var n=v(t,r,e);super(t,n.row.length,n.column.length),this.rowIndices=n.row,this.columnIndices=n.column}set(t,r,e){return this.matrix.set(this.rowIndices[t],this.columnIndices[r],e),this}get(t,r){return this.matrix.get(this.rowIndices[t],this.columnIndices[r])}}class k extends M{constructor(t,r){super(t,(r=d(t,r)).length,t.columns),this.rowIndices=r}set(t,r,e){return this.matrix.set(this.rowIndices[t],r,e),this}get(t,r){return this.matrix.get(this.rowIndices[t],r)}}class z extends M{constructor(t,r){r=w(t,r),super(t,t.rows,r.length),this.columnIndices=r}set(t,r,e){return this.matrix.set(t,this.columnIndices[r],e),this}get(t,r){return this.matrix.get(t,this.columnIndices[r])}}class N extends M{constructor(t,r){super(t,t.rows,1),this.column=r}set(t,r,e){return this.matrix.set(t,this.column,e),this}get(t){return this.matrix.get(t,this.column)}}class A extends M{constructor(t){super(t,t.rows,t.columns)}set(t,r,e){return this.matrix.set(this.rows-t-1,r,e),this}get(t,r){return this.matrix.get(this.rows-t-1,r)}}class V extends M{constructor(t){super(t,t.rows,t.columns)}set(t,r,e){return this.matrix.set(t,this.columns-r-1,e),this}get(t,r){return this.matrix.get(t,this.columns-r-1)}}function C(t){void 0===t&&(t=Object);class r extends t{static get[Symbol.species](){return this}static from1DArray(t,r,e){if(t*r!==e.length)throw new RangeError("Data length does not match given dimensions");for(var n=new this(t,r),i=0;it&&(t=this.get(r,e));return t}maxIndex(){for(var t=this.get(0,0),r=[0,0],e=0;et&&(t=this.get(e,n),r[0]=e,r[1]=n);return r}min(){for(var t=this.get(0,0),r=0;rr&&(r=this.get(t,e));return r}maxRowIndex(t){f(this,t);for(var r=this.get(t,0),e=[t,0],n=1;nr&&(r=this.get(t,n),e[1]=n);return e}minRow(t){f(this,t);for(var r=this.get(t,0),e=1;er&&(r=this.get(e,t));return r}maxColumnIndex(t){m(this,t);for(var r=this.get(0,t),e=[0,t],n=1;nr&&(r=this.get(n,t),e[0]=n);return e}minColumn(t){m(this,t);for(var r=this.get(0,t),e=1;e=(r=void 0===r?1:r))throw new RangeError("min should be strictly smaller than max");for(var e=this.constructor.empty(this.rows,this.columns),n=0;n=(r=void 0===r?1:r))throw new RangeError("min should be strictly smaller than max");for(var e=this.constructor.empty(this.rows,this.columns),n=0;ne||r<0||r>=this.columns||e<0||e>=this.columns)throw new RangeError("Argument out of range");for(var n=new this.constructor[Symbol.species](t.length,e-r+1),i=0;i=this.rows)throw new RangeError(`Row index out of range: ${t[i]}`);n.set(i,o-r,this.get(t[i],o))}return n}subMatrixColumn(t,r,e){if(void 0===r&&(r=0),void 0===e&&(e=this.rows-1),r>e||r<0||r>=this.rows||e<0||e>=this.rows)throw new RangeError("Argument out of range");for(var n=new this.constructor[Symbol.species](e-r+1,t.length),i=0;i=this.columns)throw new RangeError(`Column index out of range: ${t[i]}`);n.set(o-r,i,this.get(o,t[i]))}return n}setSubMatrix(t,r,e){y(this,r,r+(t=this.constructor.checkMatrix(t)).rows-1,e,e+t.columns-1);for(var n=0;nt?i[o]=1/i[o]:i[o]=0;return i=this.constructor[Symbol.species].diag(i),n.mmul(i.mmul(e.transposeView()))}clone(){for(var t=new this.constructor[Symbol.species](this.rows,this.columns),r=0;r>","signPropagatingRightShift"],[">>>","rightShift","zeroFillRightShift"]]){var u=o($("\n(function %name%(value) {\n if (typeof value === 'number') return this.%name%S(value);\n return this.%name%M(value);\n})\n",{name:s[1],op:s[0]})),l=o($("\n(function %name%S(value) {\n for (var i = 0; i < this.rows; i++) {\n for (var j = 0; j < this.columns; j++) {\n this.set(i, j, this.get(i, j) %op% value);\n }\n }\n return this;\n})\n",{name:`${s[1]}S`,op:s[0]})),d=o($("\n(function %name%M(matrix) {\n matrix = this.constructor.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 (var i = 0; i < this.rows; i++) {\n for (var j = 0; j < this.columns; j++) {\n this.set(i, j, this.get(i, j) %op% matrix.get(i, j));\n }\n }\n return this;\n})\n",{name:`${s[1]}M`,op:s[0]})),w=o($("\n(function %name%(matrix, value) {\n var newMatrix = new this[Symbol.species](matrix);\n return newMatrix.%name%(value);\n})\n",{name:s[1]}));for(n=1;n0){if(super(t),!(Number.isInteger(r)&&r>0))throw new TypeError("nColumns must be a positive integer");for(e=0;e=0;o--){for(i=0;i=0;e--){for(t=0;ti)return new Array(r.rows+1).fill(0);for(var o=r.addRow(e,[0]),s=0;s0;a--){for(c=0,s=0,u=0;u0&&(o=-o),r[a]=c*o,s-=i*o,e[a-1]=i-o,h=0;hl){0;do{for(1,i=e[l],f=(e[l+1]-i)/(2*r[l]),m=u(f,1),f<0&&(m=-m),e[l]=r[l]/(f+m),e[l+1]=r[l]*(f+m),g=e[l+1],o=i-e[l],s=l+2;s=l;s--)for(d=v,v=p,b=y,i=p*r[s],o=p*f,m=u(f,r[s]),r[s+1]=y*m,y=r[s]/m,f=(p=f/m)*e[s]-y*i,e[s+1]=o+y*(p*i+y*e[s]),h=0;hS*x)}e[l]=e[l]+M,r[l]=0}for(s=0;s=u;a--)e[a]=r[a][u-1]/l,s+=e[a]*e[a];for(o=Math.sqrt(s),e[u]>0&&(o=-o),s-=e[u]*o,e[u]=e[u]-o,h=u;h=u;a--)i+=e[a]*r[a][h];for(i/=s,a=u;a<=c;a++)r[a][h]-=i*e[a]}for(a=0;a<=c;a++){for(i=0,h=c;h>=u;h--)i+=e[h]*r[a][h];for(i/=s,h=u;h<=c;h++)r[a][h]-=i*e[h]}e[u]=l*e[u],r[u][u-1]=l*o}}for(a=0;a=1;u--)if(0!==r[u][u-1]){for(a=u+1;a<=c;a++)e[a]=r[a][u-1];for(h=u;h<=c;h++){for(o=0,a=u;a<=c;a++)o+=e[a]*n[a][h];for(o=o/e[u]/r[u][u-1],a=u;a<=c;a++)n[a][h]+=o*e[a]}}}(o,f,m,s),function(t,r,e,n,i){var o,s,a,h,u,l,c,f,m,g,p,v,d,w,y,b=t-1,M=t-1,x=Number.EPSILON,S=0,E=0,R=0,k=0,z=0,N=0,A=0,V=0;for(o=0;oM)&&(e[o]=i[o][o],r[o]=0),s=Math.max(o-1,0);s=0;){for(h=b;h>0&&(0===(N=Math.abs(i[h-1][h-1])+Math.abs(i[h][h]))&&(N=E),!(Math.abs(i[h][h-1])=0){for(A=R>=0?R+A:R-A,e[b-1]=f+A,e[b]=e[b-1],0!==A&&(e[b]=f-c/A),r[b-1]=0,r[b]=0,f=i[b][b-1],N=Math.abs(f)+Math.abs(A),R=f/N,k=A/N,z=Math.sqrt(R*R+k*k),R/=z,k/=z,s=b-1;s0){for(N=Math.sqrt(N),m=h&&(A=i[u][u],R=((z=f-A)*(N=m-A)-c)/i[u+1][u]+i[u][u+1],k=i[u+1][u+1]-A-z-N,z=i[u+2][u+1],N=Math.abs(R)+Math.abs(k)+Math.abs(z),R/=N,k/=N,z/=N,u!==h)&&!(Math.abs(i[u][u-1])*(Math.abs(k)+Math.abs(z))u+2&&(i[o][o-3]=0);for(a=u;a<=b-1&&(w=a!==b-1,a!==u&&(R=i[a][a-1],k=i[a+1][a-1],z=w?i[a+2][a-1]:0,0!==(f=Math.abs(R)+Math.abs(k)+Math.abs(z))&&(R/=f,k/=f,z/=f)),0!==f);a++)if(N=Math.sqrt(R*R+k*k+z*z),R<0&&(N=-N),0!==N){for(a!==u?i[a][a-1]=-N*f:h!==u&&(i[a][a-1]=-i[a][a-1]),f=(R+=N)/N,m=k/N,A=z/N,k/=R,z/=R,s=a;s=0;b--)if(R=e[b],0===(k=r[b]))for(h=b,i[b][b]=1,o=b-1;o>=0;o--){for(c=i[o][o]-R,z=0,s=h;s<=b;s++)z+=i[o][s]*i[s][b];if(r[o]<0)A=c,N=z;else if(h=o,0===r[o]?i[o][b]=0!==c?-z/c:-z/(x*E):(f=i[o][o+1],m=i[o+1][o],k=(e[o]-R)*(e[o]-R)+r[o]*r[o],l=(f*N-A*z)/k,i[o][b]=l,i[o+1][b]=Math.abs(f)>Math.abs(A)?(-z-c*l)/f:(-N-m*l)/A),l=Math.abs(i[o][b]),x*l*l>1)for(s=o;s<=b;s++)i[s][b]=i[s][b]/l}else if(k<0)for(h=b-1,Math.abs(i[b][b-1])>Math.abs(i[b-1][b])?(i[b-1][b-1]=k/i[b][b-1],i[b-1][b]=-(i[b][b]-R)/i[b][b-1]):(y=$(0,-i[b-1][b],i[b-1][b-1]-R,k),i[b-1][b-1]=y[0],i[b-1][b]=y[1]),i[b][b-1]=0,i[b][b]=1,o=b-2;o>=0;o--){for(g=0,p=0,s=h;s<=b;s++)g+=i[o][s]*i[s][b-1],p+=i[o][s]*i[s][b];if(c=i[o][o]-R,r[o]<0)A=c,z=g,N=p;else if(h=o,0===r[o]?(y=$(-g,-p,c,k),i[o][b-1]=y[0],i[o][b]=y[1]):(f=i[o][o+1],m=i[o+1][o],v=(e[o]-R)*(e[o]-R)+r[o]*r[o]-k*k,d=2*(e[o]-R)*k,0===v&&0===d&&(v=x*E*(Math.abs(c)+Math.abs(k)+Math.abs(f)+Math.abs(m)+Math.abs(A))),y=$(f*z-A*g+k*p,f*N-A*p-k*g,v,d),i[o][b-1]=y[0],i[o][b]=y[1],Math.abs(f)>Math.abs(A)+Math.abs(k)?(i[o+1][b-1]=(-g-c*i[o][b-1]+k*i[o][b])/f,i[o+1][b]=(-p-c*i[o][b]-k*i[o][b-1])/f):(y=$(-z-m*i[o][b-1],-N-m*i[o][b],A,k),i[o+1][b-1]=y[0],i[o+1][b]=y[1])),l=Math.max(Math.abs(i[o][b-1]),Math.abs(i[o][b])),x*l*l>1)for(s=o;s<=b;s++)i[s][b-1]=i[s][b-1]/l,i[s][b]=i[s][b]/l}for(o=0;oM)for(s=o;s=0;s--)for(o=0;o<=M;o++){for(A=0,a=0;a<=Math.min(s,M);a++)A+=n[o][a]*i[a][s];n[o][s]=A}}(o,h,a,s,f)}this.n=o,this.e=h,this.d=a,this.V=s}get realEigenvalues(){return this.d}get imaginaryEigenvalues(){return this.e}get eigenvectorMatrix(){return _.isMatrix(this.V)||(this.V=new _(this.V)),this.V}get diagonalMatrix(){var t,r,e=this.n,n=this.e,i=this.d,o=new _(e,e);for(t=0;t0?o[t][t+1]=n[t]:n[t]<0&&(o[t][t-1]=n[t])}return o}}function $(t,r,e,n){var i,o;return Math.abs(e)>Math.abs(n)?[(t+(i=n/e)*r)/(o=e+i*n),(r-i*t)/o]:[((i=e/n)*t+r)/(o=n+i*e),(i*r-t)/o]}class Q{constructor(t){if(!(t=I.checkMatrix(t)).isSymmetric())throw new Error("Matrix is not symmetric");var r,e,n,i=t,o=i.rows,s=new _(o,o),a=!0;for(e=0;e0,s[e][e]=Math.sqrt(Math.max(u,0)),n=e+1;n=0;o--)for(i=0;i Date: Thu, 23 May 2019 11:30:15 -0700 Subject: [PATCH 12/46] Up to 1.2.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 42bc77b..eced4d1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "umap-js", - "version": "1.2.0", + "version": "1.2.1", "description": "JavaScript implementation of UMAP", "author": { "name": "Andy Coenen", From c25ed006e169ed885d054da339f46f4a699b6449 Mon Sep 17 00:00:00 2001 From: Andy Coenen Date: Thu, 23 May 2019 11:31:35 -0700 Subject: [PATCH 13/46] Bump to 1.2.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index eced4d1..68af921 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "umap-js", - "version": "1.2.1", + "version": "1.2.2", "description": "JavaScript implementation of UMAP", "author": { "name": "Andy Coenen", From f3794b4d9bd153e9e01d59bbfa81d1d1c1bd9020 Mon Sep 17 00:00:00 2001 From: Andy Coenen Date: Thu, 23 May 2019 11:32:06 -0700 Subject: [PATCH 14/46] Remove publish loop --- package.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/package.json b/package.json index 68af921..3eb5142 100644 --- a/package.json +++ b/package.json @@ -13,8 +13,7 @@ "scripts": { "test": "jest", "bundle": "rm -rf lib && webpack --config ./webpack/lib.config.ts && webpack --config ./webpack/lib.min.config.ts", - "build": "rm -rf dist && tsc && yarn bundle", - "publish": "yarn build && yarn publish" + "build": "rm -rf dist && tsc && yarn bundle" }, "devDependencies": { "@types/jest": "^24.0.3", From 73f181c8d7b58b051006aff7e492e259d6a32251 Mon Sep 17 00:00:00 2001 From: Andy Coenen Date: Thu, 23 May 2019 14:14:13 -0700 Subject: [PATCH 15/46] Update travis build status badge --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 051f91f..e658b67 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -[![Build Status](https://travis-ci.org/PAIR-code/umap-js.svg?branch=master)](https://travis-ci.org/PAIR-code/umap-js) +[![Build Status](https://travis-ci.com/PAIR-code/umap-js.svg?branch=master)](https://travis-ci.org/PAIR-code/umap-js) # UMAP-JS From 1cb21afe80723f0f494e7c4b891d525c2ef807c5 Mon Sep 17 00:00:00 2001 From: Andy Coenen Date: Wed, 5 Jun 2019 15:04:19 -0700 Subject: [PATCH 16/46] Add option for custom distanceFn --- README.md | 18 +++++++++--------- src/umap.ts | 6 ++++++ test/umap.test.ts | 30 +++++++++++++++++++++++++++++- 3 files changed, 44 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index e658b67..256d815 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,6 @@ Uniform Manifold Approximation and Projection (UMAP) is a dimension reduction te There are a few important differences between the python implementation and the JS port. - The optimization step is seeded with a random embedding rather than a spectral embedding. This gives comparable results for smaller datasets. The spectral embedding computation relies on efficient eigenvalue / eigenvector computations that are not easily done in JS. -- The only distance function used is euclidean distance. - There is no specialized functionality for angular distances or sparse data representations. ### Usage @@ -75,14 +74,15 @@ const transformed = umap.transform(additionalData); The UMAP constructor can accept a number of hyperparameters via a `UMAPParameters` object, with the most common described below. See [umap.ts](./src/umap.ts) for more details. -| Parameter | Description | default | -| ------------- | ----------------------------------------------------------------------------------------------------------------------------------- | ------------------------ | -| `nComponents` | The number of components (dimensions) to project the data to | 2 | -| `nEpochs` | The number of epochs to optimize embeddings via SGD | (computed automatically) | -| `nNeighbors` | The number of nearest neighbors to construct the fuzzy manifold | 15 | -| `minDist` | The effective minimum distance between embedded points, used with `spread` to control the clumped/dispersed nature of the embedding | 0.1 | -| `spread` | The effective scale of embedded points, used with `minDist` to control the clumped/dispersed nature of the embedding | 1.0 | -| `random` | A pseudo-random-number generator for controlling stochastic processes | `Math.random` | +| Parameter | Description | default | +| ------------- | ----------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------- | +| `nComponents` | The number of components (dimensions) to project the data to | 2 | +| `nEpochs` | The number of epochs to optimize embeddings via SGD | (computed automatically) | +| `nNeighbors` | The number of nearest neighbors to construct the fuzzy manifold | 15 | +| `minDist` | The effective minimum distance between embedded points, used with `spread` to control the clumped/dispersed nature of the embedding | 0.1 | +| `spread` | The effective scale of embedded points, used with `minDist` to control the clumped/dispersed nature of the embedding | 1.0 | +| `random` | A pseudo-random-number generator for controlling stochastic processes | `Math.random` | +| `distanceFn` | A custom distance function to use | [`euclidean`](https://github.com/PAIR-code/umap-js/blob/73f181c8d7b58b051006aff7e492e259d6a32251/src/umap.ts#L1076) | ```typescript const umap = new UMAP({ diff --git a/src/umap.ts b/src/umap.ts index 589e30d..3006f0b 100644 --- a/src/umap.ts +++ b/src/umap.ts @@ -78,6 +78,11 @@ const SMOOTH_K_TOLERANCE = 1e-5; const MIN_K_DIST_SCALE = 1e-3; export interface UMAPParameters { + /** + * The distance function with which to assess nearest neighbors, defaults + * to euclidean distance. + */ + distanceFn?: DistanceFn /** * The initial learning rate for the embedding optimization. */ @@ -252,6 +257,7 @@ export class UMAP { if (params[key] !== undefined) this[key] = params[key]; }; + setParam('distanceFn'); setParam('learningRate'); setParam('localConnectivity'); setParam('minDist'); diff --git a/test/umap.test.ts b/test/umap.test.ts index 1e751c3..110f0f7 100644 --- a/test/umap.test.ts +++ b/test/umap.test.ts @@ -17,7 +17,13 @@ * ============================================================================== */ -import { UMAP, findABParams, euclidean, TargetMetric } from '../src/umap'; +import { + UMAP, + findABParams, + euclidean, + TargetMetric, + Vector, +} from '../src/umap'; import * as utils from '../src/utils'; import { additionalData, @@ -174,6 +180,28 @@ describe('UMAP', () => { expect(nearestLabel).toEqual(additionalLabels[i]); } }); + + test('Allows a custom distance function to be used', () => { + let nInvocations = 0; + // Manhattan distance function, with invocation counter + const manhattanDistance = (a: Vector, b: Vector) => { + nInvocations += 1; + let distance = 0; + for (let i = 0; i < a.length; i++) { + distance += Math.abs(a[i] - b[i]); + } + return distance; + }; + + const umap = new UMAP({ + random, + nComponents: 2, + distanceFn: manhattanDistance, + }); + umap.fit(testData); + + expect(nInvocations).toBeGreaterThan(0); + }); }); function computeMeanDistances(vectors: number[][]) { From 55e5115149c4ff3bbf058ae66e5b25150d1143e1 Mon Sep 17 00:00:00 2001 From: Andy Coenen Date: Wed, 5 Jun 2019 15:19:19 -0700 Subject: [PATCH 17/46] Remove randomNormal2D that wasnt being used --- src/utils.ts | 27 --------------------------- test/utils.test.ts | 12 ------------ 2 files changed, 39 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 6b67b16..b5d39a9 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -17,33 +17,6 @@ * ============================================================================== */ -function generateGaussian(mean: number, std: number, rng = Math.random) { - const u1 = tauRand(rng); - const u2 = tauRand(rng); - - const z0 = Math.sqrt(-2.0 * Math.log(u1)) * Math.cos(Math.PI * 2 * u2); - - return z0 * std + mean; -} - -/** - * Creates a random normal distribution with given mean and stdev. - */ -export function randomNormal2d( - mean = 0, - stdev = 1, - size: number[] = [1, 1], - rng = Math.random -) { - return Array(size[0]) - .fill(0) - .map(() => { - return Array(size[1]) - .fill(0) - .map(() => generateGaussian(mean, stdev, rng)); - }); -} - /** * Simple random integer function */ diff --git a/test/utils.test.ts b/test/utils.test.ts index e50847b..206a24e 100644 --- a/test/utils.test.ts +++ b/test/utils.test.ts @@ -18,20 +18,8 @@ */ import * as utils from '../src/utils'; -import Prando from 'prando'; describe('umap utils', () => { - const prando = new Prando(42); - const random = () => prando.next(); - - test('randomNormal2d', () => { - const results = utils.randomNormal2d(0, 1, [2, 2], random); - expect(results).toEqual([ - [0.6269684491034108, -1.1184153575598885], - [-0.8717275786498884, -0.6356319013602585], - ]); - }); - test('norm function', () => { const results = utils.norm([1, 2, 3, 4]); expect(results).toEqual(Math.sqrt(30)); From a031cade56c7a19c36d9133a5286a1c1d2d2b8c0 Mon Sep 17 00:00:00 2001 From: Andy Coenen Date: Wed, 5 Jun 2019 15:38:27 -0700 Subject: [PATCH 18/46] Ensure all functions require explicitly providing a prng --- src/heap.ts | 6 ++++-- src/nn_descent.ts | 27 ++++++++++++++++----------- src/tree.ts | 32 +++++++++++++++++++++++--------- src/umap.ts | 6 ++++-- src/utils.ts | 14 ++++++++++---- test/umap.test.ts | 4 ++-- test/utils.test.ts | 6 +++++- 7 files changed, 64 insertions(+), 31 deletions(-) diff --git a/src/heap.ts b/src/heap.ts index faf3aae..0dee6f3 100644 --- a/src/heap.ts +++ b/src/heap.ts @@ -57,7 +57,9 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +import { RandomFn } from './umap'; import * as utils from './utils'; + export type Heap = number[][][]; /** @@ -92,7 +94,7 @@ export function makeHeap(nPoints: number, size: number): Heap { export function rejectionSample( nSamples: number, poolSize: number, - random: () => number + random: RandomFn ) { const result = utils.zeros(nSamples); for (let i = 0; i < nSamples; i++) { @@ -227,7 +229,7 @@ export function buildCandidates( nVertices: number, nNeighbors: number, maxCandidates: number, - random: () => number + random: RandomFn ) { const candidateNeighbors = makeHeap(nVertices, maxCandidates); for (let i = 0; i < nVertices; i++) { diff --git a/src/nn_descent.ts b/src/nn_descent.ts index af162b0..cee7310 100644 --- a/src/nn_descent.ts +++ b/src/nn_descent.ts @@ -61,12 +61,12 @@ import * as heap from './heap'; import * as matrix from './matrix'; import * as tree from './tree'; import * as utils from './utils'; -import { Vectors, DistanceFn } from './umap'; +import { RandomFn, Vectors, DistanceFn } from './umap'; /** * Create a version of nearest neighbor descent. */ -export function makeNNDescent(distanceFn: DistanceFn, random: () => number) { +export function makeNNDescent(distanceFn: DistanceFn, random: RandomFn) { return function nNDescent( data: Vectors, leafArray: Vectors, @@ -150,14 +150,16 @@ export type InitFromRandomFn = ( nNeighbors: number, data: Vectors, queryPoints: Vectors, - _heap: heap.Heap + _heap: heap.Heap, + random: RandomFn ) => void; export type InitFromTreeFn = ( _tree: tree.FlatTree, data: Vectors, queryPoints: Vectors, - _heap: heap.Heap + _heap: heap.Heap, + random: RandomFn ) => void; export function makeInitializations(distanceFn: DistanceFn) { @@ -165,10 +167,11 @@ export function makeInitializations(distanceFn: DistanceFn) { nNeighbors: number, data: Vectors, queryPoints: Vectors, - _heap: heap.Heap + _heap: heap.Heap, + random: RandomFn ) { for (let i = 0; i < queryPoints.length; i++) { - const indices = utils.rejectionSample(nNeighbors, data.length); + const indices = utils.rejectionSample(nNeighbors, data.length, random); for (let j = 0; j < indices.length; j++) { if (indices[j] < 0) { continue; @@ -183,10 +186,11 @@ export function makeInitializations(distanceFn: DistanceFn) { _tree: tree.FlatTree, data: Vectors, queryPoints: Vectors, - _heap: heap.Heap + _heap: heap.Heap, + random: RandomFn ) { for (let i = 0; i < queryPoints.length; i++) { - const indices = tree.searchFlatTree(queryPoints[i], _tree); + const indices = tree.searchFlatTree(queryPoints[i], _tree, random); for (let j = 0; j < indices.length; j++) { if (indices[j] < 0) { @@ -252,13 +256,14 @@ export function initializeSearch( queryPoints: Vectors, nNeighbors: number, initFromRandom: InitFromRandomFn, - initFromTree: InitFromTreeFn + initFromTree: InitFromTreeFn, + random: RandomFn ) { const results = heap.makeHeap(queryPoints.length, nNeighbors); - initFromRandom(nNeighbors, data, queryPoints, results); + initFromRandom(nNeighbors, data, queryPoints, results, random); if (forest) { for (let tree of forest) { - initFromTree(tree, data, queryPoints, results); + initFromTree(tree, data, queryPoints, results, random); } } return results; diff --git a/src/tree.ts b/src/tree.ts index c73c9df..834c374 100644 --- a/src/tree.ts +++ b/src/tree.ts @@ -58,7 +58,7 @@ */ import * as utils from './utils'; -import { Vector, Vectors } from './umap'; +import { RandomFn, Vector, Vectors } from './umap'; /** * Tree functionality for approximating nearest neighbors @@ -88,7 +88,7 @@ export function makeForest( data: Vectors, nNeighbors: number, nTrees: number, - random: () => number + random: RandomFn ) { const leafSize = Math.max(10, nNeighbors); @@ -108,7 +108,7 @@ function makeTree( data: Vectors, leafSize = 30, n: number, - random: () => number + random: RandomFn ): RandomProjectionTreeNode { const indices = utils.range(data.length); const tree = makeEuclideanTree(data, indices, leafSize, n, random); @@ -120,7 +120,7 @@ function makeEuclideanTree( indices: number[], leafSize = 30, q: number, - random: () => number + random: RandomFn ): RandomProjectionTreeNode { if (indices.length > leafSize) { const splitResults = euclideanRandomProjectionSplit(data, indices, random); @@ -160,7 +160,7 @@ function makeEuclideanTree( function euclideanRandomProjectionSplit( data: Vectors, indices: number[], - random: () => number + random: RandomFn ) { const dim = data[0].length; @@ -343,14 +343,19 @@ export function makeLeafArray(rpForest: FlatTree[]): number[][] { /** * Selects the side of the tree to search during flat tree search. */ -function selectSide(hyperplane: number[], offset: number, point: Vector) { +function selectSide( + hyperplane: number[], + offset: number, + point: Vector, + random: RandomFn +) { let margin = offset; for (let d = 0; d < point.length; d++) { margin += hyperplane[d] * point[d]; } if (margin === 0) { - const side = utils.tauRandInt(2); + const side = utils.tauRandInt(2, random); return side; } else if (margin > 0) { return 0; @@ -362,10 +367,19 @@ function selectSide(hyperplane: number[], offset: number, point: Vector) { /** * Searches a flattened rp-tree for a point. */ -export function searchFlatTree(point: Vector, tree: FlatTree) { +export function searchFlatTree( + point: Vector, + tree: FlatTree, + random: RandomFn +) { let node = 0; while (tree.children[node][0] > 0) { - const side = selectSide(tree.hyperplanes[node], tree.offsets[node], point); + const side = selectSide( + tree.hyperplanes[node], + tree.offsets[node], + point, + random + ); if (side === 0) { node = tree.children[node][0]; } else { diff --git a/src/umap.ts b/src/umap.ts index 3006f0b..a5974fa 100644 --- a/src/umap.ts +++ b/src/umap.ts @@ -65,6 +65,7 @@ import * as utils from './utils'; import LM from 'ml-levenberg-marquardt'; export type DistanceFn = (x: Vector, y: Vector) => number; +export type RandomFn = () => number; export type EpochCallback = (epoch: number) => boolean | void; export type Vector = number[]; export type Vectors = Vector[]; @@ -142,7 +143,7 @@ export interface UMAPParameters { * The pseudo-random number generator used by the stochastic parts of the * algorithm. */ - random?: () => number; + random?: RandomFn; /** * Interpolate between (fuzzy) union and intersection as the set operation * used to combine local fuzzy simplicial sets to obtain a global fuzzy @@ -413,7 +414,8 @@ export class UMAP { toTransform, nNeighbors, this.initFromRandom, - this.initFromTree + this.initFromTree, + this.random ); const result = this.search(rawData, this.searchGraph, init, toTransform); diff --git a/src/utils.ts b/src/utils.ts index b5d39a9..8776980 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -17,17 +17,19 @@ * ============================================================================== */ +import { RandomFn } from './umap'; + /** * Simple random integer function */ -export function tauRandInt(n: number, random = Math.random) { +export function tauRandInt(n: number, random: RandomFn) { return Math.floor(random() * n); } /** * Simple random float function */ -export function tauRand(random = Math.random) { +export function tauRand(random: RandomFn) { return random(); } /** @@ -132,12 +134,16 @@ export function max2d(input: number[][]): number { * integer is selected twice. The duplication constraint is achieved via * rejection sampling. */ -export function rejectionSample(nSamples: number, poolSize: number): number[] { +export function rejectionSample( + nSamples: number, + poolSize: number, + random: RandomFn +): number[] { const result = zeros(nSamples); for (let i = 0; i < nSamples; i++) { let rejectSample = true; while (rejectSample) { - const j = tauRandInt(poolSize); + const j = tauRandInt(poolSize, random); let broken = false; for (let k = 0; k < i; k++) { if (j === result[k]) { diff --git a/test/umap.test.ts b/test/umap.test.ts index 110f0f7..960e2b8 100644 --- a/test/umap.test.ts +++ b/test/umap.test.ts @@ -21,6 +21,7 @@ import { UMAP, findABParams, euclidean, + RandomFn, TargetMetric, Vector, } from '../src/umap'; @@ -32,12 +33,11 @@ import { testLabels, testResults2D, testResults3D, - transformResult2d, } from './test_data'; import Prando from 'prando'; describe('UMAP', () => { - let random: () => number; + let random: RandomFn; // Expected "clustering" ratios, representing inter-cluster distance vs mean // distance to other points. diff --git a/test/utils.test.ts b/test/utils.test.ts index 206a24e..7c1c77a 100644 --- a/test/utils.test.ts +++ b/test/utils.test.ts @@ -18,8 +18,12 @@ */ import * as utils from '../src/utils'; +import Prando from 'prando'; describe('umap utils', () => { + const prando = new Prando(42); + const random = () => prando.next(); + test('norm function', () => { const results = utils.norm([1, 2, 3, 4]); expect(results).toEqual(Math.sqrt(30)); @@ -81,7 +85,7 @@ describe('umap utils', () => { }); test('rejection sample', () => { - const results = utils.rejectionSample(5, 10); + const results = utils.rejectionSample(5, 10, random); const entries = new Set(); for (const r of results) { expect(entries.has(r)).toBe(false); From 0a2f7e61416ac52b90bc40b2aa7ed6f94306cbc1 Mon Sep 17 00:00:00 2001 From: Andy Coenen Date: Wed, 5 Jun 2019 15:44:32 -0700 Subject: [PATCH 19/46] Upgrade jest --- package.json | 6 +- yarn.lock | 2478 ++++++++++++++++++++++++++++---------------------- 2 files changed, 1371 insertions(+), 1113 deletions(-) diff --git a/package.json b/package.json index 3eb5142..0ca8581 100644 --- a/package.json +++ b/package.json @@ -16,12 +16,12 @@ "build": "rm -rf dist && tsc && yarn bundle" }, "devDependencies": { - "@types/jest": "^24.0.3", + "@types/jest": "^24.0.13", "@types/node": "^10.12.21", - "jest": "23", + "jest": "^24.8.0", "prando": "^5.1.0", "prettier": "^1.16.4", - "ts-jest": "^23.10.5", + "ts-jest": "^24.0.2", "ts-loader": "^5.3.3", "ts-node": "^8.0.2", "typescript": "^3.2.4", diff --git a/yarn.lock b/yarn.lock index 1e1d461..5fad749 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2,28 +2,368 @@ # yarn lockfile v1 -"@babel/code-frame@^7.0.0-beta.35": +"@babel/code-frame@^7.0.0": version "7.0.0" resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.0.0.tgz#06e2ab19bdb535385559aabb5ba59729482800f8" + integrity sha512-OfC2uemaknXr87bdLUkWog7nYuliM9Ij5HUcajsVcMCpQrcLmtxRbVFTIqmcSkSeYRBFBRxs2FiUqFJDLdiebA== dependencies: "@babel/highlight" "^7.0.0" +"@babel/core@^7.1.0": + version "7.4.5" + resolved "https://registry.yarnpkg.com/@babel/core/-/core-7.4.5.tgz#081f97e8ffca65a9b4b0fdc7e274e703f000c06a" + integrity sha512-OvjIh6aqXtlsA8ujtGKfC7LYWksYSX8yQcM8Ay3LuvVeQ63lcOKgoZWVqcpFwkd29aYU9rVx7jxhfhiEDV9MZA== + dependencies: + "@babel/code-frame" "^7.0.0" + "@babel/generator" "^7.4.4" + "@babel/helpers" "^7.4.4" + "@babel/parser" "^7.4.5" + "@babel/template" "^7.4.4" + "@babel/traverse" "^7.4.5" + "@babel/types" "^7.4.4" + convert-source-map "^1.1.0" + debug "^4.1.0" + json5 "^2.1.0" + lodash "^4.17.11" + resolve "^1.3.2" + semver "^5.4.1" + source-map "^0.5.0" + +"@babel/generator@^7.4.0", "@babel/generator@^7.4.4": + version "7.4.4" + resolved "https://registry.yarnpkg.com/@babel/generator/-/generator-7.4.4.tgz#174a215eb843fc392c7edcaabeaa873de6e8f041" + integrity sha512-53UOLK6TVNqKxf7RUh8NE851EHRxOOeVXKbK2bivdb+iziMyk03Sr4eaE9OELCbyZAAafAKPDwF2TPUES5QbxQ== + dependencies: + "@babel/types" "^7.4.4" + jsesc "^2.5.1" + lodash "^4.17.11" + source-map "^0.5.0" + trim-right "^1.0.1" + +"@babel/helper-function-name@^7.1.0": + version "7.1.0" + resolved "https://registry.yarnpkg.com/@babel/helper-function-name/-/helper-function-name-7.1.0.tgz#a0ceb01685f73355d4360c1247f582bfafc8ff53" + integrity sha512-A95XEoCpb3TO+KZzJ4S/5uW5fNe26DjBGqf1o9ucyLyCmi1dXq/B3c8iaWTfBk3VvetUxl16e8tIrd5teOCfGw== + dependencies: + "@babel/helper-get-function-arity" "^7.0.0" + "@babel/template" "^7.1.0" + "@babel/types" "^7.0.0" + +"@babel/helper-get-function-arity@^7.0.0": + version "7.0.0" + resolved "https://registry.yarnpkg.com/@babel/helper-get-function-arity/-/helper-get-function-arity-7.0.0.tgz#83572d4320e2a4657263734113c42868b64e49c3" + integrity sha512-r2DbJeg4svYvt3HOS74U4eWKsUAMRH01Z1ds1zx8KNTPtpTL5JAsdFv8BNyOpVqdFhHkkRDIg5B4AsxmkjAlmQ== + dependencies: + "@babel/types" "^7.0.0" + +"@babel/helper-plugin-utils@^7.0.0": + version "7.0.0" + resolved "https://registry.yarnpkg.com/@babel/helper-plugin-utils/-/helper-plugin-utils-7.0.0.tgz#bbb3fbee98661c569034237cc03967ba99b4f250" + integrity sha512-CYAOUCARwExnEixLdB6sDm2dIJ/YgEAKDM1MOeMeZu9Ld/bDgVo8aiWrXwcY7OBh+1Ea2uUcVRcxKk0GJvW7QA== + +"@babel/helper-split-export-declaration@^7.4.4": + version "7.4.4" + resolved "https://registry.yarnpkg.com/@babel/helper-split-export-declaration/-/helper-split-export-declaration-7.4.4.tgz#ff94894a340be78f53f06af038b205c49d993677" + integrity sha512-Ro/XkzLf3JFITkW6b+hNxzZ1n5OQ80NvIUdmHspih1XAhtN3vPTuUFT4eQnela+2MaZ5ulH+iyP513KJrxbN7Q== + dependencies: + "@babel/types" "^7.4.4" + +"@babel/helpers@^7.4.4": + version "7.4.4" + resolved "https://registry.yarnpkg.com/@babel/helpers/-/helpers-7.4.4.tgz#868b0ef59c1dd4e78744562d5ce1b59c89f2f2a5" + integrity sha512-igczbR/0SeuPR8RFfC7tGrbdTbFL3QTvH6D+Z6zNxnTe//GyqmtHmDkzrqDmyZ3eSwPqB/LhyKoU5DXsp+Vp2A== + dependencies: + "@babel/template" "^7.4.4" + "@babel/traverse" "^7.4.4" + "@babel/types" "^7.4.4" + "@babel/highlight@^7.0.0": version "7.0.0" resolved "https://registry.yarnpkg.com/@babel/highlight/-/highlight-7.0.0.tgz#f710c38c8d458e6dd9a201afb637fcb781ce99e4" + integrity sha512-UFMC4ZeFC48Tpvj7C8UgLvtkaUuovQX+5xNWrsIoMG8o2z+XFKjKaN9iVmS84dPwVN00W4wPmqvYoZF3EGAsfw== dependencies: chalk "^2.0.0" esutils "^2.0.2" js-tokens "^4.0.0" -"@types/jest@^24.0.3": - version "24.0.3" - resolved "https://registry.yarnpkg.com/@types/jest/-/jest-24.0.3.tgz#df8dc56565bd2d27fcbd099434f04f9891e16ab7" +"@babel/parser@^7.1.0", "@babel/parser@^7.4.3", "@babel/parser@^7.4.4", "@babel/parser@^7.4.5": + version "7.4.5" + resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.4.5.tgz#04af8d5d5a2b044a2a1bffacc1e5e6673544e872" + integrity sha512-9mUqkL1FF5T7f0WDFfAoDdiMVPWsdD1gZYzSnaXsxUCUqzuch/8of9G3VUSNiZmMBoRxT3neyVsqeiL/ZPcjew== + +"@babel/plugin-syntax-object-rest-spread@^7.0.0": + version "7.2.0" + resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-object-rest-spread/-/plugin-syntax-object-rest-spread-7.2.0.tgz#3b7a3e733510c57e820b9142a6579ac8b0dfad2e" + integrity sha512-t0JKGgqk2We+9may3t0xDdmneaXmyxq0xieYcKHxIsrJO64n1OiMWNUtc5gQK1PA0NpdCRrtZp4z+IUaKugrSA== + dependencies: + "@babel/helper-plugin-utils" "^7.0.0" + +"@babel/template@^7.1.0", "@babel/template@^7.4.0", "@babel/template@^7.4.4": + version "7.4.4" + resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.4.4.tgz#f4b88d1225689a08f5bc3a17483545be9e4ed237" + integrity sha512-CiGzLN9KgAvgZsnivND7rkA+AeJ9JB0ciPOD4U59GKbQP2iQl+olF1l76kJOupqidozfZ32ghwBEJDhnk9MEcw== + dependencies: + "@babel/code-frame" "^7.0.0" + "@babel/parser" "^7.4.4" + "@babel/types" "^7.4.4" + +"@babel/traverse@^7.1.0", "@babel/traverse@^7.4.3", "@babel/traverse@^7.4.4", "@babel/traverse@^7.4.5": + version "7.4.5" + resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.4.5.tgz#4e92d1728fd2f1897dafdd321efbff92156c3216" + integrity sha512-Vc+qjynwkjRmIFGxy0KYoPj4FdVDxLej89kMHFsWScq999uX+pwcX4v9mWRjW0KcAYTPAuVQl2LKP1wEVLsp+A== + dependencies: + "@babel/code-frame" "^7.0.0" + "@babel/generator" "^7.4.4" + "@babel/helper-function-name" "^7.1.0" + "@babel/helper-split-export-declaration" "^7.4.4" + "@babel/parser" "^7.4.5" + "@babel/types" "^7.4.4" + debug "^4.1.0" + globals "^11.1.0" + lodash "^4.17.11" + +"@babel/types@^7.0.0", "@babel/types@^7.3.0", "@babel/types@^7.4.0", "@babel/types@^7.4.4": + version "7.4.4" + resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.4.4.tgz#8db9e9a629bb7c29370009b4b779ed93fe57d5f0" + integrity sha512-dOllgYdnEFOebhkKCjzSVFqw/PmmB8pH6RGOWkY4GsboQNd47b1fBThBSwlHAq9alF9vc1M3+6oqR47R50L0tQ== + dependencies: + esutils "^2.0.2" + lodash "^4.17.11" + to-fast-properties "^2.0.0" + +"@cnakazawa/watch@^1.0.3": + version "1.0.3" + resolved "https://registry.yarnpkg.com/@cnakazawa/watch/-/watch-1.0.3.tgz#099139eaec7ebf07a27c1786a3ff64f39464d2ef" + integrity sha512-r5160ogAvGyHsal38Kux7YYtodEKOj89RGb28ht1jh3SJb08VwRwAKKJL0bGb04Zd/3r9FL3BFIc3bBidYffCA== + dependencies: + exec-sh "^0.3.2" + minimist "^1.2.0" + +"@jest/console@^24.7.1": + version "24.7.1" + resolved "https://registry.yarnpkg.com/@jest/console/-/console-24.7.1.tgz#32a9e42535a97aedfe037e725bd67e954b459545" + integrity sha512-iNhtIy2M8bXlAOULWVTUxmnelTLFneTNEkHCgPmgd+zNwy9zVddJ6oS5rZ9iwoscNdT5mMwUd0C51v/fSlzItg== + dependencies: + "@jest/source-map" "^24.3.0" + chalk "^2.0.1" + slash "^2.0.0" + +"@jest/core@^24.8.0": + version "24.8.0" + resolved "https://registry.yarnpkg.com/@jest/core/-/core-24.8.0.tgz#fbbdcd42a41d0d39cddbc9f520c8bab0c33eed5b" + integrity sha512-R9rhAJwCBQzaRnrRgAdVfnglUuATXdwTRsYqs6NMdVcAl5euG8LtWDe+fVkN27YfKVBW61IojVsXKaOmSnqd/A== + dependencies: + "@jest/console" "^24.7.1" + "@jest/reporters" "^24.8.0" + "@jest/test-result" "^24.8.0" + "@jest/transform" "^24.8.0" + "@jest/types" "^24.8.0" + ansi-escapes "^3.0.0" + chalk "^2.0.1" + exit "^0.1.2" + graceful-fs "^4.1.15" + jest-changed-files "^24.8.0" + jest-config "^24.8.0" + jest-haste-map "^24.8.0" + jest-message-util "^24.8.0" + jest-regex-util "^24.3.0" + jest-resolve-dependencies "^24.8.0" + jest-runner "^24.8.0" + jest-runtime "^24.8.0" + jest-snapshot "^24.8.0" + jest-util "^24.8.0" + jest-validate "^24.8.0" + jest-watcher "^24.8.0" + micromatch "^3.1.10" + p-each-series "^1.0.0" + pirates "^4.0.1" + realpath-native "^1.1.0" + rimraf "^2.5.4" + strip-ansi "^5.0.0" + +"@jest/environment@^24.8.0": + version "24.8.0" + resolved "https://registry.yarnpkg.com/@jest/environment/-/environment-24.8.0.tgz#0342261383c776bdd652168f68065ef144af0eac" + integrity sha512-vlGt2HLg7qM+vtBrSkjDxk9K0YtRBi7HfRFaDxoRtyi+DyVChzhF20duvpdAnKVBV6W5tym8jm0U9EfXbDk1tw== + dependencies: + "@jest/fake-timers" "^24.8.0" + "@jest/transform" "^24.8.0" + "@jest/types" "^24.8.0" + jest-mock "^24.8.0" + +"@jest/fake-timers@^24.8.0": + version "24.8.0" + resolved "https://registry.yarnpkg.com/@jest/fake-timers/-/fake-timers-24.8.0.tgz#2e5b80a4f78f284bcb4bd5714b8e10dd36a8d3d1" + integrity sha512-2M4d5MufVXwi6VzZhJ9f5S/wU4ud2ck0kxPof1Iz3zWx6Y+V2eJrES9jEktB6O3o/oEyk+il/uNu9PvASjWXQw== + dependencies: + "@jest/types" "^24.8.0" + jest-message-util "^24.8.0" + jest-mock "^24.8.0" + +"@jest/reporters@^24.8.0": + version "24.8.0" + resolved "https://registry.yarnpkg.com/@jest/reporters/-/reporters-24.8.0.tgz#075169cd029bddec54b8f2c0fc489fd0b9e05729" + integrity sha512-eZ9TyUYpyIIXfYCrw0UHUWUvE35vx5I92HGMgS93Pv7du+GHIzl+/vh8Qj9MCWFK/4TqyttVBPakWMOfZRIfxw== + dependencies: + "@jest/environment" "^24.8.0" + "@jest/test-result" "^24.8.0" + "@jest/transform" "^24.8.0" + "@jest/types" "^24.8.0" + chalk "^2.0.1" + exit "^0.1.2" + glob "^7.1.2" + istanbul-lib-coverage "^2.0.2" + istanbul-lib-instrument "^3.0.1" + istanbul-lib-report "^2.0.4" + istanbul-lib-source-maps "^3.0.1" + istanbul-reports "^2.1.1" + jest-haste-map "^24.8.0" + jest-resolve "^24.8.0" + jest-runtime "^24.8.0" + jest-util "^24.8.0" + jest-worker "^24.6.0" + node-notifier "^5.2.1" + slash "^2.0.0" + source-map "^0.6.0" + string-length "^2.0.0" + +"@jest/source-map@^24.3.0": + version "24.3.0" + resolved "https://registry.yarnpkg.com/@jest/source-map/-/source-map-24.3.0.tgz#563be3aa4d224caf65ff77edc95cd1ca4da67f28" + integrity sha512-zALZt1t2ou8le/crCeeiRYzvdnTzaIlpOWaet45lNSqNJUnXbppUUFR4ZUAlzgDmKee4Q5P/tKXypI1RiHwgag== + dependencies: + callsites "^3.0.0" + graceful-fs "^4.1.15" + source-map "^0.6.0" + +"@jest/test-result@^24.8.0": + version "24.8.0" + resolved "https://registry.yarnpkg.com/@jest/test-result/-/test-result-24.8.0.tgz#7675d0aaf9d2484caa65e048d9b467d160f8e9d3" + integrity sha512-+YdLlxwizlfqkFDh7Mc7ONPQAhA4YylU1s529vVM1rsf67vGZH/2GGm5uO8QzPeVyaVMobCQ7FTxl38QrKRlng== + dependencies: + "@jest/console" "^24.7.1" + "@jest/types" "^24.8.0" + "@types/istanbul-lib-coverage" "^2.0.0" + +"@jest/test-sequencer@^24.8.0": + version "24.8.0" + resolved "https://registry.yarnpkg.com/@jest/test-sequencer/-/test-sequencer-24.8.0.tgz#2f993bcf6ef5eb4e65e8233a95a3320248cf994b" + integrity sha512-OzL/2yHyPdCHXEzhoBuq37CE99nkme15eHkAzXRVqthreWZamEMA0WoetwstsQBCXABhczpK03JNbc4L01vvLg== + dependencies: + "@jest/test-result" "^24.8.0" + jest-haste-map "^24.8.0" + jest-runner "^24.8.0" + jest-runtime "^24.8.0" + +"@jest/transform@^24.8.0": + version "24.8.0" + resolved "https://registry.yarnpkg.com/@jest/transform/-/transform-24.8.0.tgz#628fb99dce4f9d254c6fd9341e3eea262e06fef5" + integrity sha512-xBMfFUP7TortCs0O+Xtez2W7Zu1PLH9bvJgtraN1CDST6LBM/eTOZ9SfwS/lvV8yOfcDpFmwf9bq5cYbXvqsvA== + dependencies: + "@babel/core" "^7.1.0" + "@jest/types" "^24.8.0" + babel-plugin-istanbul "^5.1.0" + chalk "^2.0.1" + convert-source-map "^1.4.0" + fast-json-stable-stringify "^2.0.0" + graceful-fs "^4.1.15" + jest-haste-map "^24.8.0" + jest-regex-util "^24.3.0" + jest-util "^24.8.0" + micromatch "^3.1.10" + realpath-native "^1.1.0" + slash "^2.0.0" + source-map "^0.6.1" + write-file-atomic "2.4.1" + +"@jest/types@^24.8.0": + version "24.8.0" + resolved "https://registry.yarnpkg.com/@jest/types/-/types-24.8.0.tgz#f31e25948c58f0abd8c845ae26fcea1491dea7ad" + integrity sha512-g17UxVr2YfBtaMUxn9u/4+siG1ptg9IGYAYwvpwn61nBg779RXnjE/m7CxYcIzEt0AbHZZAHSEZNhkE2WxURVg== + dependencies: + "@types/istanbul-lib-coverage" "^2.0.0" + "@types/istanbul-reports" "^1.1.1" + "@types/yargs" "^12.0.9" + +"@types/babel__core@^7.1.0": + version "7.1.2" + resolved "https://registry.yarnpkg.com/@types/babel__core/-/babel__core-7.1.2.tgz#608c74f55928033fce18b99b213c16be4b3d114f" + integrity sha512-cfCCrFmiGY/yq0NuKNxIQvZFy9kY/1immpSpTngOnyIbD4+eJOG5mxphhHDv3CHL9GltO4GcKr54kGBg3RNdbg== + dependencies: + "@babel/parser" "^7.1.0" + "@babel/types" "^7.0.0" + "@types/babel__generator" "*" + "@types/babel__template" "*" + "@types/babel__traverse" "*" + +"@types/babel__generator@*": + version "7.0.2" + resolved "https://registry.yarnpkg.com/@types/babel__generator/-/babel__generator-7.0.2.tgz#d2112a6b21fad600d7674274293c85dce0cb47fc" + integrity sha512-NHcOfab3Zw4q5sEE2COkpfXjoE7o+PmqD9DQW4koUT3roNxwziUdXGnRndMat/LJNUtePwn1TlP4do3uoe3KZQ== + dependencies: + "@babel/types" "^7.0.0" + +"@types/babel__template@*": + version "7.0.2" + resolved "https://registry.yarnpkg.com/@types/babel__template/-/babel__template-7.0.2.tgz#4ff63d6b52eddac1de7b975a5223ed32ecea9307" + integrity sha512-/K6zCpeW7Imzgab2bLkLEbz0+1JlFSrUMdw7KoIIu+IUdu51GWaBZpd3y1VXGVXzynvGa4DaIaxNZHiON3GXUg== + dependencies: + "@babel/parser" "^7.1.0" + "@babel/types" "^7.0.0" + +"@types/babel__traverse@*", "@types/babel__traverse@^7.0.6": + version "7.0.6" + resolved "https://registry.yarnpkg.com/@types/babel__traverse/-/babel__traverse-7.0.6.tgz#328dd1a8fc4cfe3c8458be9477b219ea158fd7b2" + integrity sha512-XYVgHF2sQ0YblLRMLNPB3CkFMewzFmlDsH/TneZFHUXDlABQgh88uOxuez7ZcXxayLFrqLwtDH1t+FmlFwNZxw== + dependencies: + "@babel/types" "^7.3.0" + +"@types/istanbul-lib-coverage@*", "@types/istanbul-lib-coverage@^2.0.0": + version "2.0.1" + resolved "https://registry.yarnpkg.com/@types/istanbul-lib-coverage/-/istanbul-lib-coverage-2.0.1.tgz#42995b446db9a48a11a07ec083499a860e9138ff" + integrity sha512-hRJD2ahnnpLgsj6KWMYSrmXkM3rm2Dl1qkx6IOFD5FnuNPXJIG5L0dhgKXCYTRMGzU4n0wImQ/xfmRc4POUFlg== + +"@types/istanbul-lib-report@*": + version "1.1.1" + resolved "https://registry.yarnpkg.com/@types/istanbul-lib-report/-/istanbul-lib-report-1.1.1.tgz#e5471e7fa33c61358dd38426189c037a58433b8c" + integrity sha512-3BUTyMzbZa2DtDI2BkERNC6jJw2Mr2Y0oGI7mRxYNBPxppbtEK1F66u3bKwU2g+wxwWI7PAoRpJnOY1grJqzHg== + dependencies: + "@types/istanbul-lib-coverage" "*" + +"@types/istanbul-reports@^1.1.1": + version "1.1.1" + resolved "https://registry.yarnpkg.com/@types/istanbul-reports/-/istanbul-reports-1.1.1.tgz#7a8cbf6a406f36c8add871625b278eaf0b0d255a" + integrity sha512-UpYjBi8xefVChsCoBpKShdxTllC9pwISirfoZsUa2AAdQg/Jd2KQGtSbw+ya7GPo7x/wAPlH6JBhKhAsXUEZNA== + dependencies: + "@types/istanbul-lib-coverage" "*" + "@types/istanbul-lib-report" "*" + +"@types/jest-diff@*": + version "20.0.1" + resolved "https://registry.yarnpkg.com/@types/jest-diff/-/jest-diff-20.0.1.tgz#35cc15b9c4f30a18ef21852e255fdb02f6d59b89" + integrity sha512-yALhelO3i0hqZwhjtcr6dYyaLoCHbAMshwtj6cGxTvHZAKXHsYGdff6E8EPw3xLKY0ELUTQ69Q1rQiJENnccMA== + +"@types/jest@^24.0.13": + version "24.0.13" + resolved "https://registry.yarnpkg.com/@types/jest/-/jest-24.0.13.tgz#10f50b64cb05fb02411fbba49e9042a3a11da3f9" + integrity sha512-3m6RPnO35r7Dg+uMLj1+xfZaOgIHHHut61djNjzwExXN4/Pm9has9C6I1KMYSfz7mahDhWUOVg4HW/nZdv5Pww== + dependencies: + "@types/jest-diff" "*" "@types/node@^10.12.21": version "10.12.21" resolved "https://registry.yarnpkg.com/@types/node/-/node-10.12.21.tgz#7e8a0c34cf29f4e17a36e9bd0ea72d45ba03908e" +"@types/stack-utils@^1.0.1": + version "1.0.1" + resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-1.0.1.tgz#0a851d3bd96498fa25c33ab7278ed3bd65f06c3e" + integrity sha512-l42BggppR6zLmpfU6fq9HEa2oGPEI8yrSPL3GITjfRInppYFahObbIQOQK3UGxEnyQpltZLaPe75046NOZQikw== + +"@types/yargs@^12.0.2", "@types/yargs@^12.0.9": + version "12.0.12" + resolved "https://registry.yarnpkg.com/@types/yargs/-/yargs-12.0.12.tgz#45dd1d0638e8c8f153e87d296907659296873916" + integrity sha512-SOhuU4wNBxhhTHxYaiG5NY4HBhDIDnJF60GU+2LqHAdKKer86//e4yg69aENCtQ04n0ovz+tq2YPME5t5yp4pw== + "@webassemblyjs/ast@1.8.3": version "1.8.3" resolved "https://registry.yarnpkg.com/@webassemblyjs/ast/-/ast-1.8.3.tgz#63a741bd715a6b6783f2ea5c6ab707516aa215eb" @@ -163,18 +503,21 @@ abab@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/abab/-/abab-2.0.0.tgz#aba0ab4c5eee2d4c79d3487d85450fb2376ebb0f" + integrity sha512-sY5AXXVZv4Y1VACTtR11UJCPHHudgY5i26Qj5TypE6DKlIApbwb5uqhXcJ5UUGbvZNRh7EeIoW+LrJumBsKp7w== abbrev@1: version "1.1.1" resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.1.tgz#f8f2c887ad10bf67f634f005b6987fed3179aac8" + integrity sha512-nne9/IiQ/hzIhY6pdDnbBtz7DjPTKrY00P/zvPSm5pOFkl6xuGrGnXn/VtTNNfNtAfZ9/1RtehkszU9qcTii0Q== acorn-dynamic-import@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/acorn-dynamic-import/-/acorn-dynamic-import-4.0.0.tgz#482210140582a36b83c3e342e1cfebcaa9240948" acorn-globals@^4.1.0: - version "4.3.0" - resolved "https://registry.yarnpkg.com/acorn-globals/-/acorn-globals-4.3.0.tgz#e3b6f8da3c1552a95ae627571f7dd6923bb54103" + version "4.3.2" + resolved "https://registry.yarnpkg.com/acorn-globals/-/acorn-globals-4.3.2.tgz#4e2c2313a597fd589720395f6354b41cd5ec8006" + integrity sha512-BbzvZhVtZP+Bs1J1HcwrQe8ycfO0wStkSGxuul3He3GkHOIZ6eTqOkPuw9IP1X3+IkOo4wiJmwkobzXYz4wewQ== dependencies: acorn "^6.0.1" acorn-walk "^6.0.1" @@ -182,12 +525,19 @@ acorn-globals@^4.1.0: acorn-walk@^6.0.1: version "6.1.1" resolved "https://registry.yarnpkg.com/acorn-walk/-/acorn-walk-6.1.1.tgz#d363b66f5fac5f018ff9c3a1e7b6f8e310cc3913" + integrity sha512-OtUw6JUTgxA2QoqqmrmQ7F2NYqiBPi/L2jqHyFtllhOUvXYQXf0Z1CYUinIfyT4bTCGmrA7gX9FvHA81uzCoVw== acorn@^5.5.3: version "5.7.3" resolved "https://registry.yarnpkg.com/acorn/-/acorn-5.7.3.tgz#67aa231bf8812974b85235a96771eb6bd07ea279" + integrity sha512-T/zvzYRfbVojPWahDsE5evJdHb3oJoQfFbsrKM7w5Zcs++Tr257tia3BmMP8XYVjp1S9RZXQMh7gao96BlqZOw== -acorn@^6.0.1, acorn@^6.0.5: +acorn@^6.0.1: + version "6.1.1" + resolved "https://registry.yarnpkg.com/acorn/-/acorn-6.1.1.tgz#7d25ae05bb8ad1f9b699108e1094ecd7884adc1f" + integrity sha512-jPTiwtOxaHNaAPg/dmrJ/beuzLRnXtB0kQPQ8JpotKJgTB6rX6c8mlf315941pyjBSaPg8NHXS9fhP4u17DpGA== + +acorn@^6.0.5: version "6.1.0" resolved "https://registry.yarnpkg.com/acorn/-/acorn-6.1.0.tgz#b0a3be31752c97a0f7013c5f4903b71a05db6818" @@ -199,7 +549,7 @@ ajv-keywords@^3.1.0: version "3.4.0" resolved "https://registry.yarnpkg.com/ajv-keywords/-/ajv-keywords-3.4.0.tgz#4b831e7b531415a7cc518cd404e73f6193c6349d" -ajv@^6.1.0, ajv@^6.5.5: +ajv@^6.1.0: version "6.9.1" resolved "https://registry.yarnpkg.com/ajv/-/ajv-6.9.1.tgz#a4d3683d74abc5670e75f0b16520f70a20ea8dc1" dependencies: @@ -208,41 +558,51 @@ ajv@^6.1.0, ajv@^6.5.5: json-schema-traverse "^0.4.1" uri-js "^4.2.2" +ajv@^6.5.5: + version "6.10.0" + resolved "https://registry.yarnpkg.com/ajv/-/ajv-6.10.0.tgz#90d0d54439da587cd7e843bfb7045f50bd22bdf1" + integrity sha512-nffhOpkymDECQyR0mnsUtoCE8RlX38G0rYP+wgLWFyZuUyuuojSSvi/+euOiQBIn63whYwYVIIH1TvE3tu4OEg== + dependencies: + fast-deep-equal "^2.0.1" + fast-json-stable-stringify "^2.0.0" + json-schema-traverse "^0.4.1" + uri-js "^4.2.2" + ansi-escapes@^3.0.0: version "3.2.0" resolved "https://registry.yarnpkg.com/ansi-escapes/-/ansi-escapes-3.2.0.tgz#8780b98ff9dbf5638152d1f1fe5c1d7b4442976b" + integrity sha512-cBhpre4ma+U0T1oM5fXg7Dy1Jw7zzwv7lt/GoCpr+hDQJoYnKVPLL4dCvSEFMmQurOQvSrwT7SL/DAlhBI97RQ== ansi-regex@^2.0.0: version "2.1.1" resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-2.1.1.tgz#c3b33ab5ee360d86e0e628f0468ae7ef27d654df" + integrity sha1-w7M6te42DYbg5ijwRorn7yfWVN8= ansi-regex@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-3.0.0.tgz#ed0317c322064f79466c02966bddb605ab37d998" + integrity sha1-7QMXwyIGT3lGbAKWa922Bas32Zg= -ansi-styles@^2.2.1: - version "2.2.1" - resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-2.2.1.tgz#b432dd3358b634cf75e1e4664368240533c1ddbe" +ansi-regex@^4.0.0, ansi-regex@^4.1.0: + version "4.1.0" + resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-4.1.0.tgz#8b9f8f08cf1acb843756a839ca8c7e3168c51997" + integrity sha512-1apePfXM1UOSqw0o9IiFAovVz9M5S1Dg+4TrDwfMewQ6p/rmMueb7tWZjQ1rx4Loy1ArBggoqGpfqqdI4rondg== ansi-styles@^3.2.0, ansi-styles@^3.2.1: version "3.2.1" resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-3.2.1.tgz#41fbb20243e50b12be0f04b8dedbf07520ce841d" + integrity sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA== dependencies: color-convert "^1.9.0" anymatch@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/anymatch/-/anymatch-2.0.0.tgz#bcb24b4f37934d9aa7ac17b4adaf89e7c76ef2eb" + integrity sha512-5teOsQWABXHHBFP9y3skS5P3d/WfWXpv3FUpy+LorMrNYaT9pI4oLMQX7jzQ2KklNpGpWHzdCXTDT2Y3XGlZBw== dependencies: micromatch "^3.1.4" normalize-path "^2.1.1" -append-transform@^0.4.0: - version "0.4.0" - resolved "https://registry.yarnpkg.com/append-transform/-/append-transform-0.4.0.tgz#d76ebf8ca94d276e247a36bad44a4b74ab611991" - dependencies: - default-require-extensions "^1.0.0" - aproba@^1.0.3, aproba@^1.1.1: version "1.2.0" resolved "https://registry.yarnpkg.com/aproba/-/aproba-1.2.0.tgz#6802e6264efd18c790a1b0d517f0f2627bf2c94a" @@ -250,6 +610,7 @@ aproba@^1.0.3, aproba@^1.1.1: are-we-there-yet@~1.1.2: version "1.1.5" resolved "https://registry.yarnpkg.com/are-we-there-yet/-/are-we-there-yet-1.1.5.tgz#4b35c2944f062a8bfcda66410760350fe9ddfc21" + integrity sha512-5hYdAkZlcG8tOLujVDTgCT+uPX0VnpAH28gWsLfzpXYm7wP6mp5Q/gYyR7YQ0cKVJcXJnl3j2kpBan13PtQf6w== dependencies: delegates "^1.0.0" readable-stream "^2.0.6" @@ -258,45 +619,30 @@ arg@^4.1.0: version "4.1.0" resolved "https://registry.yarnpkg.com/arg/-/arg-4.1.0.tgz#583c518199419e0037abb74062c37f8519e575f0" -argparse@^1.0.7: - version "1.0.10" - resolved "https://registry.yarnpkg.com/argparse/-/argparse-1.0.10.tgz#bcd6791ea5ae09725e17e5ad988134cd40b3d911" - dependencies: - sprintf-js "~1.0.2" - -arr-diff@^2.0.0: - version "2.0.0" - resolved "https://registry.yarnpkg.com/arr-diff/-/arr-diff-2.0.0.tgz#8f3b827f955a8bd669697e4a4256ac3ceae356cf" - dependencies: - arr-flatten "^1.0.1" - arr-diff@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/arr-diff/-/arr-diff-4.0.0.tgz#d6461074febfec71e7e15235761a329a5dc7c520" + integrity sha1-1kYQdP6/7HHn4VI1dhoyml3HxSA= -arr-flatten@^1.0.1, arr-flatten@^1.1.0: +arr-flatten@^1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/arr-flatten/-/arr-flatten-1.1.0.tgz#36048bbff4e7b47e136644316c99669ea5ae91f1" + integrity sha512-L3hKV5R/p5o81R7O02IGnwpDmkp6E982XhtbuwSe3O4qOtMMMtodicASA1Cny2U+aCXcNpml+m4dPsvsJ3jatg== arr-union@^3.1.0: version "3.1.0" resolved "https://registry.yarnpkg.com/arr-union/-/arr-union-3.1.0.tgz#e39b09aea9def866a8f206e288af63919bae39c4" + integrity sha1-45sJrqne+Gao8gbiiK9jkZuuOcQ= array-equal@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/array-equal/-/array-equal-1.0.0.tgz#8c2a5ef2472fd9ea742b04c77a75093ba2757c93" - -array-unique@^0.2.1: - version "0.2.1" - resolved "https://registry.yarnpkg.com/array-unique/-/array-unique-0.2.1.tgz#a1d97ccafcbc2625cc70fadceb36a50c58b01a53" + integrity sha1-jCpe8kcv2ep0KwTHenUJO6J1fJM= array-unique@^0.3.2: version "0.3.2" resolved "https://registry.yarnpkg.com/array-unique/-/array-unique-0.3.2.tgz#a894b75d4bc4f6cd679ef3244a9fd8f46ae2d428" - -arrify@^1.0.1: - version "1.0.1" - resolved "https://registry.yarnpkg.com/arrify/-/arrify-1.0.1.tgz#898508da2226f380df904728456849c1501a4b0d" + integrity sha1-qJS3XUvE9s1nnvMkSp/Y9Gri1Cg= asn1.js@^4.0.0: version "4.10.1" @@ -309,12 +655,14 @@ asn1.js@^4.0.0: asn1@~0.2.3: version "0.2.4" resolved "https://registry.yarnpkg.com/asn1/-/asn1-0.2.4.tgz#8d2475dfab553bb33e77b54e59e880bb8ce23136" + integrity sha512-jxwzQpLQjSmWXgwaCZE9Nz+glAG01yF1QnWgbhGwHI5A6FRIEY6IVqtHhIepHqI7/kyEyQEagBC5mBEFlIYvdg== dependencies: safer-buffer "~2.1.0" assert-plus@1.0.0, assert-plus@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/assert-plus/-/assert-plus-1.0.0.tgz#f12e0f3c5d77b0b1cdd9146942e4e96c1e4dd525" + integrity sha1-8S4PPF13sLHN2RRpQuTpbB5N1SU= assert@^1.1.1: version "1.4.1" @@ -325,10 +673,12 @@ assert@^1.1.1: assign-symbols@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/assign-symbols/-/assign-symbols-1.0.0.tgz#59667f41fadd4f20ccbc2bb96b8d4f7f78ec0367" + integrity sha1-WWZ/QfrdTyDMvCu5a41Pf3jsA2c= astral-regex@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/astral-regex/-/astral-regex-1.0.0.tgz#6c8c3fb827dd43ee3918f27b82782ab7658a6fd9" + integrity sha512-+Ryf6g3BKoRc7jfp7ad8tM4TtMiaWvbF/1/sQcZPkkS7ag3D5nMBCe2UfOTONtAkaG0tO0ij3C5Lwmf1EiyjHg== async-each@^1.0.1: version "1.0.1" @@ -337,177 +687,69 @@ async-each@^1.0.1: async-limiter@~1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/async-limiter/-/async-limiter-1.0.0.tgz#78faed8c3d074ab81f22b4e985d79e8738f720f8" - -async@^2.1.4, async@^2.5.0: - version "2.6.2" - resolved "https://registry.yarnpkg.com/async/-/async-2.6.2.tgz#18330ea7e6e313887f5d2f2a904bac6fe4dd5381" - dependencies: - lodash "^4.17.11" + integrity sha512-jp/uFnooOiO+L211eZOoSyzpOITMXx1rBITauYykG3BRYPu8h0UcxsPNB04RR5vo4Tyz3+ay17tR6JVf9qzYWg== asynckit@^0.4.0: version "0.4.0" resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79" + integrity sha1-x57Zf380y48robyXkLzDZkdLS3k= atob@^2.1.1: version "2.1.2" resolved "https://registry.yarnpkg.com/atob/-/atob-2.1.2.tgz#6d9517eb9e030d2436666651e86bd9f6f13533c9" + integrity sha512-Wm6ukoaOGJi/73p/cl2GvLjTI5JM1k/O14isD73YML8StrH/7/lRFgmg8nICZgD3bZZvjwCGxtMOD3wWNAu8cg== aws-sign2@~0.7.0: version "0.7.0" resolved "https://registry.yarnpkg.com/aws-sign2/-/aws-sign2-0.7.0.tgz#b46e890934a9591f2d2f6f86d7e6a9f1b3fe76a8" + integrity sha1-tG6JCTSpWR8tL2+G1+ap8bP+dqg= aws4@^1.8.0: version "1.8.0" resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.8.0.tgz#f0e003d9ca9e7f59c7a508945d7b2ef9a04a542f" - -babel-code-frame@^6.26.0: - version "6.26.0" - resolved "https://registry.yarnpkg.com/babel-code-frame/-/babel-code-frame-6.26.0.tgz#63fd43f7dc1e3bb7ce35947db8fe369a3f58c74b" - dependencies: - chalk "^1.1.3" - esutils "^2.0.2" - js-tokens "^3.0.2" - -babel-core@^6.0.0, babel-core@^6.26.0: - version "6.26.3" - resolved "https://registry.yarnpkg.com/babel-core/-/babel-core-6.26.3.tgz#b2e2f09e342d0f0c88e2f02e067794125e75c207" - dependencies: - babel-code-frame "^6.26.0" - babel-generator "^6.26.0" - babel-helpers "^6.24.1" - babel-messages "^6.23.0" - babel-register "^6.26.0" - babel-runtime "^6.26.0" - babel-template "^6.26.0" - babel-traverse "^6.26.0" - babel-types "^6.26.0" - babylon "^6.18.0" - convert-source-map "^1.5.1" - debug "^2.6.9" - json5 "^0.5.1" - lodash "^4.17.4" - minimatch "^3.0.4" - path-is-absolute "^1.0.1" - private "^0.1.8" - slash "^1.0.0" - source-map "^0.5.7" - -babel-generator@^6.18.0, babel-generator@^6.26.0: - version "6.26.1" - resolved "https://registry.yarnpkg.com/babel-generator/-/babel-generator-6.26.1.tgz#1844408d3b8f0d35a404ea7ac180f087a601bd90" - dependencies: - babel-messages "^6.23.0" - babel-runtime "^6.26.0" - babel-types "^6.26.0" - detect-indent "^4.0.0" - jsesc "^1.3.0" - lodash "^4.17.4" - source-map "^0.5.7" - trim-right "^1.0.1" - -babel-helpers@^6.24.1: - version "6.24.1" - resolved "https://registry.yarnpkg.com/babel-helpers/-/babel-helpers-6.24.1.tgz#3471de9caec388e5c850e597e58a26ddf37602b2" - dependencies: - babel-runtime "^6.22.0" - babel-template "^6.24.1" - -babel-jest@^23.6.0: - version "23.6.0" - resolved "https://registry.yarnpkg.com/babel-jest/-/babel-jest-23.6.0.tgz#a644232366557a2240a0c083da6b25786185a2f1" - dependencies: - babel-plugin-istanbul "^4.1.6" - babel-preset-jest "^23.2.0" - -babel-messages@^6.23.0: - version "6.23.0" - resolved "https://registry.yarnpkg.com/babel-messages/-/babel-messages-6.23.0.tgz#f3cdf4703858035b2a2951c6ec5edf6c62f2630e" - dependencies: - babel-runtime "^6.22.0" - -babel-plugin-istanbul@^4.1.6: - version "4.1.6" - resolved "https://registry.yarnpkg.com/babel-plugin-istanbul/-/babel-plugin-istanbul-4.1.6.tgz#36c59b2192efce81c5b378321b74175add1c9a45" + integrity sha512-ReZxvNHIOv88FlT7rxcXIIC0fPt4KZqZbOlivyWtXLt8ESx84zd3kMC6iK5jVeS2qt+g7ftS7ye4fi06X5rtRQ== + +babel-jest@^24.8.0: + version "24.8.0" + resolved "https://registry.yarnpkg.com/babel-jest/-/babel-jest-24.8.0.tgz#5c15ff2b28e20b0f45df43fe6b7f2aae93dba589" + integrity sha512-+5/kaZt4I9efoXzPlZASyK/lN9qdRKmmUav9smVc0ruPQD7IsfucQ87gpOE8mn2jbDuS6M/YOW6n3v9ZoIfgnw== + dependencies: + "@jest/transform" "^24.8.0" + "@jest/types" "^24.8.0" + "@types/babel__core" "^7.1.0" + babel-plugin-istanbul "^5.1.0" + babel-preset-jest "^24.6.0" + chalk "^2.4.2" + slash "^2.0.0" + +babel-plugin-istanbul@^5.1.0: + version "5.1.4" + resolved "https://registry.yarnpkg.com/babel-plugin-istanbul/-/babel-plugin-istanbul-5.1.4.tgz#841d16b9a58eeb407a0ddce622ba02fe87a752ba" + integrity sha512-dySz4VJMH+dpndj0wjJ8JPs/7i1TdSPb1nRrn56/92pKOF9VKC1FMFJmMXjzlGGusnCAqujP6PBCiKq0sVA+YQ== dependencies: - babel-plugin-syntax-object-rest-spread "^6.13.0" - find-up "^2.1.0" - istanbul-lib-instrument "^1.10.1" - test-exclude "^4.2.1" - -babel-plugin-jest-hoist@^23.2.0: - version "23.2.0" - resolved "https://registry.yarnpkg.com/babel-plugin-jest-hoist/-/babel-plugin-jest-hoist-23.2.0.tgz#e61fae05a1ca8801aadee57a6d66b8cefaf44167" - -babel-plugin-syntax-object-rest-spread@^6.13.0: - version "6.13.0" - resolved "https://registry.yarnpkg.com/babel-plugin-syntax-object-rest-spread/-/babel-plugin-syntax-object-rest-spread-6.13.0.tgz#fd6536f2bce13836ffa3a5458c4903a597bb3bf5" + find-up "^3.0.0" + istanbul-lib-instrument "^3.3.0" + test-exclude "^5.2.3" -babel-preset-jest@^23.2.0: - version "23.2.0" - resolved "https://registry.yarnpkg.com/babel-preset-jest/-/babel-preset-jest-23.2.0.tgz#8ec7a03a138f001a1a8fb1e8113652bf1a55da46" +babel-plugin-jest-hoist@^24.6.0: + version "24.6.0" + resolved "https://registry.yarnpkg.com/babel-plugin-jest-hoist/-/babel-plugin-jest-hoist-24.6.0.tgz#f7f7f7ad150ee96d7a5e8e2c5da8319579e78019" + integrity sha512-3pKNH6hMt9SbOv0F3WVmy5CWQ4uogS3k0GY5XLyQHJ9EGpAT9XWkFd2ZiXXtkwFHdAHa5j7w7kfxSP5lAIwu7w== dependencies: - babel-plugin-jest-hoist "^23.2.0" - babel-plugin-syntax-object-rest-spread "^6.13.0" + "@types/babel__traverse" "^7.0.6" -babel-register@^6.26.0: - version "6.26.0" - resolved "https://registry.yarnpkg.com/babel-register/-/babel-register-6.26.0.tgz#6ed021173e2fcb486d7acb45c6009a856f647071" +babel-preset-jest@^24.6.0: + version "24.6.0" + resolved "https://registry.yarnpkg.com/babel-preset-jest/-/babel-preset-jest-24.6.0.tgz#66f06136eefce87797539c0d63f1769cc3915984" + integrity sha512-pdZqLEdmy1ZK5kyRUfvBb2IfTPb2BUvIJczlPspS8fWmBQslNNDBqVfh7BW5leOVJMDZKzjD8XEyABTk6gQ5yw== dependencies: - babel-core "^6.26.0" - babel-runtime "^6.26.0" - core-js "^2.5.0" - home-or-tmp "^2.0.0" - lodash "^4.17.4" - mkdirp "^0.5.1" - source-map-support "^0.4.15" - -babel-runtime@^6.22.0, babel-runtime@^6.26.0: - version "6.26.0" - resolved "https://registry.yarnpkg.com/babel-runtime/-/babel-runtime-6.26.0.tgz#965c7058668e82b55d7bfe04ff2337bc8b5647fe" - dependencies: - core-js "^2.4.0" - regenerator-runtime "^0.11.0" - -babel-template@^6.16.0, babel-template@^6.24.1, babel-template@^6.26.0: - version "6.26.0" - resolved "https://registry.yarnpkg.com/babel-template/-/babel-template-6.26.0.tgz#de03e2d16396b069f46dd9fff8521fb1a0e35e02" - dependencies: - babel-runtime "^6.26.0" - babel-traverse "^6.26.0" - babel-types "^6.26.0" - babylon "^6.18.0" - lodash "^4.17.4" - -babel-traverse@^6.0.0, babel-traverse@^6.18.0, babel-traverse@^6.26.0: - version "6.26.0" - resolved "https://registry.yarnpkg.com/babel-traverse/-/babel-traverse-6.26.0.tgz#46a9cbd7edcc62c8e5c064e2d2d8d0f4035766ee" - dependencies: - babel-code-frame "^6.26.0" - babel-messages "^6.23.0" - babel-runtime "^6.26.0" - babel-types "^6.26.0" - babylon "^6.18.0" - debug "^2.6.8" - globals "^9.18.0" - invariant "^2.2.2" - lodash "^4.17.4" - -babel-types@^6.0.0, babel-types@^6.18.0, babel-types@^6.26.0: - version "6.26.0" - resolved "https://registry.yarnpkg.com/babel-types/-/babel-types-6.26.0.tgz#a3b073f94ab49eb6fa55cd65227a334380632497" - dependencies: - babel-runtime "^6.26.0" - esutils "^2.0.2" - lodash "^4.17.4" - to-fast-properties "^1.0.3" - -babylon@^6.18.0: - version "6.18.0" - resolved "https://registry.yarnpkg.com/babylon/-/babylon-6.18.0.tgz#af2f3b88fa6f5c1e4c634d1a0f8eac4f55b395e3" + "@babel/plugin-syntax-object-rest-spread" "^7.0.0" + babel-plugin-jest-hoist "^24.6.0" balanced-match@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767" + integrity sha1-ibTRmasr7kneFk6gK4nORi1xt2c= base64-js@^1.0.2: version "1.3.0" @@ -516,6 +758,7 @@ base64-js@^1.0.2: base@^0.11.1: version "0.11.2" resolved "https://registry.yarnpkg.com/base/-/base-0.11.2.tgz#7bde5ced145b6d551a90db87f83c558b4eb48a8f" + integrity sha512-5T6P4xPgpp0YDFvSWwEZ4NoE3aM4QBQXDzmVbraCkFj8zHM+mba8SyqB5DbZWyR7mYHo6Y7BdQo3MoA4m0TeQg== dependencies: cache-base "^1.0.1" class-utils "^0.3.5" @@ -528,6 +771,7 @@ base@^0.11.1: bcrypt-pbkdf@^1.0.0: version "1.0.2" resolved "https://registry.yarnpkg.com/bcrypt-pbkdf/-/bcrypt-pbkdf-1.0.2.tgz#a4301d389b6a43f9b67ff3ca11a3f6637e360e9e" + integrity sha1-pDAdOJtqQ/m2f/PKEaP2Y342Dp4= dependencies: tweetnacl "^0.14.3" @@ -550,21 +794,15 @@ bn.js@^4.0.0, bn.js@^4.1.0, bn.js@^4.1.1, bn.js@^4.4.0: brace-expansion@^1.1.7: version "1.1.11" resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.11.tgz#3c7fcbf529d87226f3d2f52b966ff5271eb441dd" + integrity sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA== dependencies: balanced-match "^1.0.0" concat-map "0.0.1" -braces@^1.8.2: - version "1.8.5" - resolved "https://registry.yarnpkg.com/braces/-/braces-1.8.5.tgz#ba77962e12dff969d6b76711e914b737857bf6a7" - dependencies: - expand-range "^1.8.1" - preserve "^0.2.0" - repeat-element "^1.1.2" - braces@^2.3.1, braces@^2.3.2: version "2.3.2" resolved "https://registry.yarnpkg.com/braces/-/braces-2.3.2.tgz#5979fd3f14cd531565e5fa2df1abfff1dfaee729" + integrity sha512-aNdbnj9P8PjdXU4ybaWLK2IF3jc/EoDYbC7AazW6to3TRsfXxscC9UXOB5iDiEQrkyIbWp2SLQda4+QAa7nc3w== dependencies: arr-flatten "^1.1.0" array-unique "^0.3.2" @@ -584,10 +822,12 @@ brorand@^1.0.1: browser-process-hrtime@^0.1.2: version "0.1.3" resolved "https://registry.yarnpkg.com/browser-process-hrtime/-/browser-process-hrtime-0.1.3.tgz#616f00faef1df7ec1b5bf9cfe2bdc3170f26c7b4" + integrity sha512-bRFnI4NnjO6cnyLmOV/7PVoDEMJChlcfN0z4s1YMBY989/SvlfMI1lgCnkFUs53e9gQF+w7qu7XdllSTiSl8Aw== browser-resolve@^1.11.3: version "1.11.3" resolved "https://registry.yarnpkg.com/browser-resolve/-/browser-resolve-1.11.3.tgz#9b7cbb3d0f510e4cb86bdbd796124d28b5890af6" + integrity sha512-exDi1BYWB/6raKHmDTCicQfTkqwN5fioMFV4j8BsfMU4R2DK/QfZfK7kOVkmWCNANf0snkBzqGqAJBao9gZMdQ== dependencies: resolve "1.1.7" @@ -653,6 +893,7 @@ bs-logger@0.x: bser@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/bser/-/bser-2.0.0.tgz#9ac78d3ed5d915804fd87acb158bc797147a1719" + integrity sha1-mseNPtXZFYBP2HrLFYvHlxR6Fxk= dependencies: node-int64 "^0.4.0" @@ -698,6 +939,7 @@ cacache@^11.0.2: cache-base@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/cache-base/-/cache-base-1.0.1.tgz#0a7f46416831c8b662ee36fe4e7c59d76f666ab2" + integrity sha512-AKcdTnFSWATd5/GCPRxr2ChwIJ85CeyrEyjRHlKxQ56d4XJMGym0uAiKn0xbLOGOl3+yRpOTi484dVCEc5AUzQ== dependencies: collection-visit "^1.0.0" component-emitter "^1.2.1" @@ -709,39 +951,33 @@ cache-base@^1.0.1: union-value "^1.0.0" unset-value "^1.0.0" -callsites@^2.0.0: - version "2.0.0" - resolved "https://registry.yarnpkg.com/callsites/-/callsites-2.0.0.tgz#06eb84f00eea413da86affefacbffb36093b3c50" +callsites@^3.0.0: + version "3.1.0" + resolved "https://registry.yarnpkg.com/callsites/-/callsites-3.1.0.tgz#b3630abd8943432f54b3f0519238e33cd7df2f73" + integrity sha512-P8BjAsXvZS+VIDUI11hHCQEv74YT67YUi5JJFNWIqL235sBmjX4+qx9Muvls5ivyNENctx46xQLQ3aTuE7ssaQ== camelcase@^4.1.0: version "4.1.0" resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-4.1.0.tgz#d545635be1e33c542649c69173e5de6acfae34dd" + integrity sha1-1UVjW+HjPFQmScaRc+Xeas+uNN0= camelcase@^5.0.0: version "5.0.0" resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-5.0.0.tgz#03295527d58bd3cd4aa75363f35b2e8d97be2f42" -capture-exit@^1.2.0: - version "1.2.0" - resolved "https://registry.yarnpkg.com/capture-exit/-/capture-exit-1.2.0.tgz#1c5fcc489fd0ab00d4f1ac7ae1072e3173fbab6f" +capture-exit@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/capture-exit/-/capture-exit-2.0.0.tgz#fb953bfaebeb781f62898239dabb426d08a509a4" + integrity sha512-PiT/hQmTonHhl/HFGN+Lx3JJUznrVYJ3+AQsnthneZbvW7x+f08Tk7yLJTLEOUvBTbduLeeBkxEaYXUOUrRq6g== dependencies: - rsvp "^3.3.3" + rsvp "^4.8.4" caseless@~0.12.0: version "0.12.0" resolved "https://registry.yarnpkg.com/caseless/-/caseless-0.12.0.tgz#1b681c21ff84033c826543090689420d187151dc" + integrity sha1-G2gcIf+EAzyCZUMJBolCDRhxUdw= -chalk@^1.1.3: - version "1.1.3" - resolved "https://registry.yarnpkg.com/chalk/-/chalk-1.1.3.tgz#a8115c55e4a702fe4d150abd3872822a7e09fc98" - dependencies: - ansi-styles "^2.2.1" - escape-string-regexp "^1.0.2" - has-ansi "^2.0.0" - strip-ansi "^3.0.0" - supports-color "^2.0.0" - -chalk@^2.0.0, chalk@^2.0.1, chalk@^2.3.0, chalk@^2.4.1: +chalk@^2.0.0, chalk@^2.0.1, chalk@^2.3.0, chalk@^2.4.1, chalk@^2.4.2: version "2.4.2" resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.4.2.tgz#cd42541677a54333cf541a49108c1432b44c9424" dependencies: @@ -770,6 +1006,7 @@ chokidar@^2.0.2: chownr@^1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/chownr/-/chownr-1.1.1.tgz#54726b8b8fff4df053c42187e801fb4412df1494" + integrity sha512-j38EvO5+LHX84jlo6h4UzmOwi0UgW61WRyPtJz4qaadK5eY3BTS5TY/S1Stc3Uk2lIM6TPevAlULiEJwie860g== chrome-trace-event@^1.0.0: version "1.0.0" @@ -777,9 +1014,10 @@ chrome-trace-event@^1.0.0: dependencies: tslib "^1.9.0" -ci-info@^1.5.0: - version "1.6.0" - resolved "https://registry.yarnpkg.com/ci-info/-/ci-info-1.6.0.tgz#2ca20dbb9ceb32d4524a683303313f0304b1e497" +ci-info@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/ci-info/-/ci-info-2.0.0.tgz#67a9e964be31a51e15e5010d58e6f12834002f46" + integrity sha512-5tK7EtrZ0N+OLFMthtqOj4fI2Jeb88C4CAZPu25LDVUgXJ0A3Js4PMGqrn0JU1W0Mh1/Z8wZzYPxqUrXeBboCQ== cipher-base@^1.0.0, cipher-base@^1.0.1, cipher-base@^1.0.3: version "1.0.4" @@ -791,6 +1029,7 @@ cipher-base@^1.0.0, cipher-base@^1.0.1, cipher-base@^1.0.3: class-utils@^0.3.5: version "0.3.6" resolved "https://registry.yarnpkg.com/class-utils/-/class-utils-0.3.6.tgz#f93369ae8b9a7ce02fd41faad0ca83033190c463" + integrity sha512-qOhPa/Fj7s6TY8H8esGu5QNpMMQxz79h+urzrNYN6mn+9BnxlDGf5QZ+XeCDsxSjPqsSR56XOZOJmpeurnLMeg== dependencies: arr-union "^3.1.0" define-property "^0.2.5" @@ -800,6 +1039,7 @@ class-utils@^0.3.5: cliui@^4.0.0: version "4.1.0" resolved "https://registry.yarnpkg.com/cliui/-/cliui-4.1.0.tgz#348422dbe82d800b3022eef4f6ac10bf2e4d1b49" + integrity sha512-4FG+RSG9DL7uEwRUZXZn3SS34DiDPfzP0VOiEwtUWlE+AR2EIg+hSyvrIgUUfhdgR/UkAeW2QHgeP+hWrXs7jQ== dependencies: string-width "^2.1.1" strip-ansi "^4.0.0" @@ -808,14 +1048,17 @@ cliui@^4.0.0: co@^4.6.0: version "4.6.0" resolved "https://registry.yarnpkg.com/co/-/co-4.6.0.tgz#6ea6bdf3d853ae54ccb8e47bfa0bf3f9031fb184" + integrity sha1-bqa989hTrlTMuOR7+gvz+QMfsYQ= code-point-at@^1.0.0: version "1.1.0" resolved "https://registry.yarnpkg.com/code-point-at/-/code-point-at-1.1.0.tgz#0d070b4d043a5bea33a2f1a40e2edb3d9a4ccf77" + integrity sha1-DQcLTQQ6W+ozovGkDi7bPZpMz3c= collection-visit@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/collection-visit/-/collection-visit-1.0.0.tgz#4bc0373c164bc3291b4d368c829cf1a80a59dca0" + integrity sha1-S8A3PBZLwykbTTaMgpzxqApZ3KA= dependencies: map-visit "^1.0.0" object-visit "^1.0.0" @@ -823,34 +1066,45 @@ collection-visit@^1.0.0: color-convert@^1.9.0: version "1.9.3" resolved "https://registry.yarnpkg.com/color-convert/-/color-convert-1.9.3.tgz#bb71850690e1f136567de629d2d5471deda4c1e8" + integrity sha512-QfAUtd+vFdAtFQcC8CCyYt1fYWxSqAiK2cSD6zDB8N3cpsEBAvRxp9zOGg6G/SHHJYAT88/az/IuDGALsNVbGg== dependencies: color-name "1.1.3" color-name@1.1.3: version "1.1.3" resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.3.tgz#a7d0558bd89c42f795dd42328f740831ca53bc25" + integrity sha1-p9BVi9icQveV3UIyj3QIMcpTvCU= combined-stream@^1.0.6, combined-stream@~1.0.6: - version "1.0.7" - resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.7.tgz#2d1d24317afb8abe95d6d2c0b07b57813539d828" + version "1.0.8" + resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f" + integrity sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg== dependencies: delayed-stream "~1.0.0" commander@~2.17.1: version "2.17.1" resolved "https://registry.yarnpkg.com/commander/-/commander-2.17.1.tgz#bd77ab7de6de94205ceacc72f1716d29f20a77bf" + integrity sha512-wPMUt6FnH2yzG95SA6mzjQOEKUU3aLaDEmzs1ti+1E9h+CsrZghRlqEM/EJ4KscsQVG8uNN4uVreUeT8+drlgg== + +commander@~2.20.0: + version "2.20.0" + resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.0.tgz#d58bb2b5c1ee8f87b0d340027e9e94e222c5a422" + integrity sha512-7j2y+40w61zy6YC2iRNpUe/NwhNyoXrYpHMrSunaMG64nRnaf96zO/KMQR4OyN/UnE5KLyEBnKHd4aG3rskjpQ== commondir@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/commondir/-/commondir-1.0.1.tgz#ddd800da0c66127393cca5950ea968a3aaf1253b" component-emitter@^1.2.1: - version "1.2.1" - resolved "https://registry.yarnpkg.com/component-emitter/-/component-emitter-1.2.1.tgz#137918d6d78283f7df7a6b7c5a63e140e69425e6" + version "1.3.0" + resolved "https://registry.yarnpkg.com/component-emitter/-/component-emitter-1.3.0.tgz#16e4070fba8ae29b679f2215853ee181ab2eabc0" + integrity sha512-Rd3se6QB+sO1TwqZjscQrurpEPIfO0/yYnSin6Q/rD3mOutHvUrCAhJub3r90uNb+SESBuE0QYoB90YdfatsRg== concat-map@0.0.1: version "0.0.1" resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b" + integrity sha1-2Klr13/Wjfd5OnMDajug1UBdR3s= concat-stream@^1.5.0: version "1.6.2" @@ -870,14 +1124,16 @@ console-browserify@^1.1.0: console-control-strings@^1.0.0, console-control-strings@~1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/console-control-strings/-/console-control-strings-1.1.0.tgz#3d7cf4464db6446ea644bf4b39507f9851008e8e" + integrity sha1-PXz0Rk22RG6mRL9LOVB/mFEAjo4= constants-browserify@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/constants-browserify/-/constants-browserify-1.0.0.tgz#c20b96d8c617748aaf1c16021760cd27fcb8cb75" -convert-source-map@^1.4.0, convert-source-map@^1.5.1: +convert-source-map@^1.1.0, convert-source-map@^1.4.0: version "1.6.0" resolved "https://registry.yarnpkg.com/convert-source-map/-/convert-source-map-1.6.0.tgz#51b537a8c43e0f04dec1993bffcdd504e758ac20" + integrity sha512-eFu7XigvxdZ1ETfbgPBohgyQ/Z++C0eEhTor0qRwBw9unw+L0/6V8wkSuGgzdThkiS5lSpdptOQPD8Ak40a+7A== dependencies: safe-buffer "~5.1.1" @@ -895,14 +1151,12 @@ copy-concurrently@^1.0.0: copy-descriptor@^0.1.0: version "0.1.1" resolved "https://registry.yarnpkg.com/copy-descriptor/-/copy-descriptor-0.1.1.tgz#676f6eb3c39997c2ee1ac3a924fd6124748f578d" - -core-js@^2.4.0, core-js@^2.5.0: - version "2.6.4" - resolved "https://registry.yarnpkg.com/core-js/-/core-js-2.6.4.tgz#b8897c062c4d769dd30a0ac5c73976c47f92ea0d" + integrity sha1-Z29us8OZl8LuGsOpJP1hJHSPV40= core-util-is@1.0.2, core-util-is@~1.0.0: version "1.0.2" resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7" + integrity sha1-tf1UIgqivFq1eqtxQMlAdUUDwac= create-ecdh@^4.0.0: version "4.0.3" @@ -932,14 +1186,6 @@ create-hmac@^1.1.0, create-hmac@^1.1.2, create-hmac@^1.1.4: safe-buffer "^5.0.1" sha.js "^2.4.8" -cross-spawn@^5.0.1: - version "5.1.0" - resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-5.1.0.tgz#e8bd0efee58fcff6f8f94510a0a554bbfa235449" - dependencies: - lru-cache "^4.0.1" - shebang-command "^1.2.0" - which "^1.2.9" - cross-spawn@^6.0.0, cross-spawn@^6.0.5: version "6.0.5" resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-6.0.5.tgz#4a5ec7c64dfae22c3a14124dbacdee846d80cbc4" @@ -969,10 +1215,12 @@ crypto-browserify@^3.11.0: cssom@0.3.x, "cssom@>= 0.3.2 < 0.4.0": version "0.3.6" resolved "https://registry.yarnpkg.com/cssom/-/cssom-0.3.6.tgz#f85206cee04efa841f3c5982a74ba96ab20d65ad" + integrity sha512-DtUeseGk9/GBW0hl0vVPpU22iHL6YB5BUX7ml1hB+GMpo0NX5G4voX3kdWiMSEguFtcW3Vh3djqNF4aIe6ne0A== cssstyle@^1.0.0: - version "1.1.1" - resolved "https://registry.yarnpkg.com/cssstyle/-/cssstyle-1.1.1.tgz#18b038a9c44d65f7a8e428a653b9f6fe42faf5fb" + version "1.2.2" + resolved "https://registry.yarnpkg.com/cssstyle/-/cssstyle-1.2.2.tgz#427ea4d585b18624f6fdbf9de7a2a1a3ba713077" + integrity sha512-43wY3kl1CVQSvL7wUY1qXkxVGkStjpkDmVjiIKX8R97uhajy8Bybay78uOtqvh7Q5GK75dNPfW0geWjE6qQQow== dependencies: cssom "0.3.x" @@ -983,12 +1231,14 @@ cyclist@~0.2.2: dashdash@^1.12.0: version "1.14.1" resolved "https://registry.yarnpkg.com/dashdash/-/dashdash-1.14.1.tgz#853cfa0f7cbe2fed5de20326b8dd581035f6e2f0" + integrity sha1-hTz6D3y+L+1d4gMmuN1YEDX24vA= dependencies: assert-plus "^1.0.0" data-urls@^1.0.0: version "1.1.0" resolved "https://registry.yarnpkg.com/data-urls/-/data-urls-1.1.0.tgz#15ee0582baa5e22bb59c77140da8f9c76963bbfe" + integrity sha512-YTWYI9se1P55u58gL5GkQHW4P6VJBJ5iBT+B5a7i2Tjadhv52paJG0qHX4A0OR6/t52odI64KP2YvFpkDOi3eQ== dependencies: abab "^2.0.0" whatwg-mimetype "^2.2.0" @@ -998,61 +1248,71 @@ date-now@^0.1.4: version "0.1.4" resolved "https://registry.yarnpkg.com/date-now/-/date-now-0.1.4.tgz#eaf439fd4d4848ad74e5cc7dbef200672b9e345b" -debug@^2.1.2, debug@^2.2.0, debug@^2.3.3, debug@^2.6.8, debug@^2.6.9: +debug@^2.2.0, debug@^2.3.3: version "2.6.9" resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f" + integrity sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA== dependencies: ms "2.0.0" -debug@^3.1.0: +debug@^3.2.6: version "3.2.6" resolved "https://registry.yarnpkg.com/debug/-/debug-3.2.6.tgz#e83d17de16d8a7efb7717edbe5fb10135eee629b" + integrity sha512-mel+jf7nrtEl5Pn1Qx46zARXKDpBbvzezse7p7LqINmdoIk8PYP5SySaxEmYv6TZ0JyEKA1hsCId6DIhgITtWQ== + dependencies: + ms "^2.1.1" + +debug@^4.1.0, debug@^4.1.1: + version "4.1.1" + resolved "https://registry.yarnpkg.com/debug/-/debug-4.1.1.tgz#3b72260255109c6b589cee050f1d516139664791" + integrity sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw== dependencies: ms "^2.1.1" -decamelize@^1.1.1, decamelize@^1.2.0: +decamelize@^1.2.0: version "1.2.0" resolved "https://registry.yarnpkg.com/decamelize/-/decamelize-1.2.0.tgz#f6534d15148269b20352e7bee26f501f9a191290" decode-uri-component@^0.2.0: version "0.2.0" resolved "https://registry.yarnpkg.com/decode-uri-component/-/decode-uri-component-0.2.0.tgz#eb3913333458775cb84cd1a1fae062106bb87545" + integrity sha1-6zkTMzRYd1y4TNGh+uBiEGu4dUU= deep-extend@^0.6.0: version "0.6.0" resolved "https://registry.yarnpkg.com/deep-extend/-/deep-extend-0.6.0.tgz#c4fa7c95404a17a9c3e8ca7e1537312b736330ac" + integrity sha512-LOHxIOaPYdHlJRtCQfDIVZtfw/ufM8+rVj649RIHzcm/vGwQRXFt6OPqIFWsm2XEMrNIEtWR64sY1LEKD2vAOA== deep-is@~0.1.3: version "0.1.3" resolved "https://registry.yarnpkg.com/deep-is/-/deep-is-0.1.3.tgz#b369d6fb5dbc13eecf524f91b070feedc357cf34" - -default-require-extensions@^1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/default-require-extensions/-/default-require-extensions-1.0.0.tgz#f37ea15d3e13ffd9b437d33e1a75b5fb97874cb8" - dependencies: - strip-bom "^2.0.0" + integrity sha1-s2nW+128E+7PUk+RsHD+7cNXzzQ= define-properties@^1.1.2: version "1.1.3" resolved "https://registry.yarnpkg.com/define-properties/-/define-properties-1.1.3.tgz#cf88da6cbee26fe6db7094f61d870cbd84cee9f1" + integrity sha512-3MqfYKj2lLzdMSf8ZIZE/V+Zuy+BgD6f164e8K2w7dgnpKArBDerGYpM46IYYcjnkdPNMjPk9A6VFB8+3SKlXQ== dependencies: object-keys "^1.0.12" define-property@^0.2.5: version "0.2.5" resolved "https://registry.yarnpkg.com/define-property/-/define-property-0.2.5.tgz#c35b1ef918ec3c990f9a5bc57be04aacec5c8116" + integrity sha1-w1se+RjsPJkPmlvFe+BKrOxcgRY= dependencies: is-descriptor "^0.1.0" define-property@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/define-property/-/define-property-1.0.0.tgz#769ebaaf3f4a63aad3af9e8d304c9bbe79bfb0e6" + integrity sha1-dp66rz9KY6rTr56NMEybvnm/sOY= dependencies: is-descriptor "^1.0.0" define-property@^2.0.2: version "2.0.2" resolved "https://registry.yarnpkg.com/define-property/-/define-property-2.0.2.tgz#d459689e8d654ba77e02a817f8710d702cb16e9d" + integrity sha512-jwK2UV4cnPpbcG7+VRARKTZPUWowwXA8bzH5NP6ud0oeAxyYPuGZUAC7hMugpCdz4BeSZl2Dl9k66CHJ/46ZYQ== dependencies: is-descriptor "^1.0.2" isobject "^3.0.1" @@ -1060,10 +1320,12 @@ define-property@^2.0.2: delayed-stream@~1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619" + integrity sha1-3zrhmayt+31ECqrgsp4icrJOxhk= delegates@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/delegates/-/delegates-1.0.0.tgz#84c6e159b81904fdca59a0ef44cd870d31250f9a" + integrity sha1-hMbhWbgZBP3KWaDvRM2HDTElD5o= des.js@^1.0.0: version "1.0.0" @@ -1076,21 +1338,22 @@ detect-file@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/detect-file/-/detect-file-1.0.0.tgz#f0d66d03672a825cb1b73bdb3fe62310c8e552b7" -detect-indent@^4.0.0: - version "4.0.0" - resolved "https://registry.yarnpkg.com/detect-indent/-/detect-indent-4.0.0.tgz#f76d064352cdf43a1cb6ce619c4ee3a9475de208" - dependencies: - repeating "^2.0.0" - detect-libc@^1.0.2: version "1.0.3" resolved "https://registry.yarnpkg.com/detect-libc/-/detect-libc-1.0.3.tgz#fa137c4bd698edf55cd5cd02ac559f91a4c4ba9b" + integrity sha1-+hN8S9aY7fVc1c0CrFWfkaTEups= detect-newline@^2.1.0: version "2.1.0" resolved "https://registry.yarnpkg.com/detect-newline/-/detect-newline-2.1.0.tgz#f41f1c10be4b00e87b5f13da680759f2c5bfd3e2" + integrity sha1-9B8cEL5LAOh7XxPaaAdZ8sW/0+I= -diff@^3.1.0, diff@^3.2.0: +diff-sequences@^24.3.0: + version "24.3.0" + resolved "https://registry.yarnpkg.com/diff-sequences/-/diff-sequences-24.3.0.tgz#0f20e8a1df1abddaf4d9c226680952e64118b975" + integrity sha512-xLqpez+Zj9GKSnPWS0WZw1igGocZ+uua8+y+5dDNTT934N3QuY1sp2LkHzwiaYQGz60hMq0pjAshdeXm5VUOEw== + +diff@^3.1.0: version "3.5.0" resolved "https://registry.yarnpkg.com/diff/-/diff-3.5.0.tgz#800c0dd1e0a8bfbc95835c202ad220fe317e5a12" @@ -1109,6 +1372,7 @@ domain-browser@^1.1.1: domexception@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/domexception/-/domexception-1.0.1.tgz#937442644ca6a31261ef36e3ec677fe805582c90" + integrity sha512-raigMkn7CJNNo6Ihro1fzG7wr3fHuYVytzquZKX5n0yizGsTcYgzdIUwj1X9pK0VvjeihV+XiclP+DjwbsSKug== dependencies: webidl-conversions "^4.0.2" @@ -1124,6 +1388,7 @@ duplexify@^3.4.2, duplexify@^3.6.0: ecc-jsbn@~0.1.1: version "0.1.2" resolved "https://registry.yarnpkg.com/ecc-jsbn/-/ecc-jsbn-0.1.2.tgz#3a83a904e54353287874c564b7549386849a98c9" + integrity sha1-OoOpBOVDUyh4dMVkt1SThoSamMk= dependencies: jsbn "~0.1.0" safer-buffer "^2.1.0" @@ -1164,15 +1429,17 @@ errno@^0.1.3, errno@~0.1.7: dependencies: prr "~1.0.1" -error-ex@^1.2.0: +error-ex@^1.3.1: version "1.3.2" resolved "https://registry.yarnpkg.com/error-ex/-/error-ex-1.3.2.tgz#b4ac40648107fdcdcfae242f428bea8a14d4f1bf" + integrity sha512-7dFHNmqeFSEt2ZBsCriorKnn3Z2pj+fd9kmI6QoWw4//DL+icEBfc0U7qJCisqrTsKTjw4fNFy2pW9OqStD84g== dependencies: is-arrayish "^0.2.1" es-abstract@^1.5.1: version "1.13.0" resolved "https://registry.yarnpkg.com/es-abstract/-/es-abstract-1.13.0.tgz#ac86145fdd5099d8dd49558ccba2eaf9b88e24e9" + integrity sha512-vDZfg/ykNxQVwup/8E1BZhVzFfBxs9NqMzGcvIJrqg5k2/5Za2bWo40dK2J1pgLngZ7c+Shh8lwYtLGyrwPutg== dependencies: es-to-primitive "^1.2.0" function-bind "^1.1.1" @@ -1184,18 +1451,21 @@ es-abstract@^1.5.1: es-to-primitive@^1.2.0: version "1.2.0" resolved "https://registry.yarnpkg.com/es-to-primitive/-/es-to-primitive-1.2.0.tgz#edf72478033456e8dda8ef09e00ad9650707f377" + integrity sha512-qZryBOJjV//LaxLTV6UC//WewneB3LcXOL9NP++ozKVXsIIIpm/2c13UDiD9Jp2eThsecw9m3jPqDwTyobcdbg== dependencies: is-callable "^1.1.4" is-date-object "^1.0.1" is-symbol "^1.0.2" -escape-string-regexp@^1.0.2, escape-string-regexp@^1.0.5: +escape-string-regexp@^1.0.5: version "1.0.5" resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4" + integrity sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ= escodegen@^1.9.1: - version "1.11.0" - resolved "https://registry.yarnpkg.com/escodegen/-/escodegen-1.11.0.tgz#b27a9389481d5bfd5bec76f7bb1eb3f8f4556589" + version "1.11.1" + resolved "https://registry.yarnpkg.com/escodegen/-/escodegen-1.11.1.tgz#c485ff8d6b4cdb89e27f4a856e91f118401ca510" + integrity sha512-JwiqFD9KdGVVpeuRa68yU3zZnBEOcPs0nKW7wZzXky8Z7tffdYUHbe11bPCV5jYlK6DVdKLWLm0f5I/QlL0Kmw== dependencies: esprima "^3.1.3" estraverse "^4.2.0" @@ -1214,10 +1484,7 @@ eslint-scope@^4.0.0: esprima@^3.1.3: version "3.1.3" resolved "https://registry.yarnpkg.com/esprima/-/esprima-3.1.3.tgz#fdca51cee6133895e3c88d535ce49dbff62a4633" - -esprima@^4.0.0: - version "4.0.1" - resolved "https://registry.yarnpkg.com/esprima/-/esprima-4.0.1.tgz#13b04cdb3e6c5d19df91ab6987a8695619b0aa71" + integrity sha1-/cpRzuYTOJXjyI1TXOSdv/YqRjM= esrecurse@^4.1.0: version "4.2.1" @@ -1232,6 +1499,7 @@ estraverse@^4.1.0, estraverse@^4.1.1, estraverse@^4.2.0: esutils@^2.0.2: version "2.0.2" resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.2.tgz#0abf4f1caa5bcb1f7a9d8acc6dea4faaa04bac9b" + integrity sha1-Cr9PHKpbyx96nYrMbepPqqBLrJs= events@^3.0.0: version "3.0.0" @@ -1244,23 +1512,10 @@ evp_bytestokey@^1.0.0, evp_bytestokey@^1.0.3: md5.js "^1.3.4" safe-buffer "^5.1.1" -exec-sh@^0.2.0: - version "0.2.2" - resolved "https://registry.yarnpkg.com/exec-sh/-/exec-sh-0.2.2.tgz#2a5e7ffcbd7d0ba2755bdecb16e5a427dfbdec36" - dependencies: - merge "^1.2.0" - -execa@^0.7.0: - version "0.7.0" - resolved "https://registry.yarnpkg.com/execa/-/execa-0.7.0.tgz#944becd34cc41ee32a63a9faf27ad5a65fc59777" - dependencies: - cross-spawn "^5.0.1" - get-stream "^3.0.0" - is-stream "^1.1.0" - npm-run-path "^2.0.0" - p-finally "^1.0.0" - signal-exit "^3.0.0" - strip-eof "^1.0.0" +exec-sh@^0.3.2: + version "0.3.2" + resolved "https://registry.yarnpkg.com/exec-sh/-/exec-sh-0.3.2.tgz#6738de2eb7c8e671d0366aea0b0db8c6f7d7391b" + integrity sha512-9sLAvzhI5nc8TpuQUh4ahMdCrWT00wPWz7j47/emR5+2qEfoZP5zzUXvx+vdx+H6ohhnsYC31iX04QLYJK8zTg== execa@^1.0.0: version "1.0.0" @@ -1277,16 +1532,12 @@ execa@^1.0.0: exit@^0.1.2: version "0.1.2" resolved "https://registry.yarnpkg.com/exit/-/exit-0.1.2.tgz#0632638f8d877cc82107d30a0fff1a17cba1cd0c" - -expand-brackets@^0.1.4: - version "0.1.5" - resolved "https://registry.yarnpkg.com/expand-brackets/-/expand-brackets-0.1.5.tgz#df07284e342a807cd733ac5af72411e581d1177b" - dependencies: - is-posix-bracket "^0.1.0" + integrity sha1-BjJjj42HfMghB9MKD/8aF8uhzQw= expand-brackets@^2.1.4: version "2.1.4" resolved "https://registry.yarnpkg.com/expand-brackets/-/expand-brackets-2.1.4.tgz#b77735e315ce30f6b6eff0f83b04151a22449622" + integrity sha1-t3c14xXOMPa27/D4OwQVGiJEliI= dependencies: debug "^2.3.3" define-property "^0.2.5" @@ -1296,38 +1547,35 @@ expand-brackets@^2.1.4: snapdragon "^0.8.1" to-regex "^3.0.1" -expand-range@^1.8.1: - version "1.8.2" - resolved "https://registry.yarnpkg.com/expand-range/-/expand-range-1.8.2.tgz#a299effd335fe2721ebae8e257ec79644fc85337" - dependencies: - fill-range "^2.1.0" - expand-tilde@^2.0.0, expand-tilde@^2.0.2: version "2.0.2" resolved "https://registry.yarnpkg.com/expand-tilde/-/expand-tilde-2.0.2.tgz#97e801aa052df02454de46b02bf621642cdc8502" dependencies: homedir-polyfill "^1.0.1" -expect@^23.6.0: - version "23.6.0" - resolved "https://registry.yarnpkg.com/expect/-/expect-23.6.0.tgz#1e0c8d3ba9a581c87bd71fb9bc8862d443425f98" +expect@^24.8.0: + version "24.8.0" + resolved "https://registry.yarnpkg.com/expect/-/expect-24.8.0.tgz#471f8ec256b7b6129ca2524b2a62f030df38718d" + integrity sha512-/zYvP8iMDrzaaxHVa724eJBCKqSHmO0FA7EDkBiRHxg6OipmMn1fN+C8T9L9K8yr7UONkOifu6+LLH+z76CnaA== dependencies: + "@jest/types" "^24.8.0" ansi-styles "^3.2.0" - jest-diff "^23.6.0" - jest-get-type "^22.1.0" - jest-matcher-utils "^23.6.0" - jest-message-util "^23.4.0" - jest-regex-util "^23.3.0" + jest-get-type "^24.8.0" + jest-matcher-utils "^24.8.0" + jest-message-util "^24.8.0" + jest-regex-util "^24.3.0" extend-shallow@^2.0.1: version "2.0.1" resolved "https://registry.yarnpkg.com/extend-shallow/-/extend-shallow-2.0.1.tgz#51af7d614ad9a9f610ea1bafbb989d6b1c56890f" + integrity sha1-Ua99YUrZqfYQ6huvu5idaxxWiQ8= dependencies: is-extendable "^0.1.0" extend-shallow@^3.0.0, extend-shallow@^3.0.2: version "3.0.2" resolved "https://registry.yarnpkg.com/extend-shallow/-/extend-shallow-3.0.2.tgz#26a71aaf073b39fb2127172746131c2704028db8" + integrity sha1-Jqcarwc7OfshJxcnRhMcJwQCjbg= dependencies: assign-symbols "^1.0.0" is-extendable "^1.0.1" @@ -1335,16 +1583,12 @@ extend-shallow@^3.0.0, extend-shallow@^3.0.2: extend@~3.0.2: version "3.0.2" resolved "https://registry.yarnpkg.com/extend/-/extend-3.0.2.tgz#f8b1136b4071fbd8eb140aff858b1019ec2915fa" - -extglob@^0.3.1: - version "0.3.2" - resolved "https://registry.yarnpkg.com/extglob/-/extglob-0.3.2.tgz#2e18ff3d2f49ab2765cec9023f011daa8d8349a1" - dependencies: - is-extglob "^1.0.0" + integrity sha512-fjquC59cD7CyW6urNXK0FBufkZcoiGG80wTuPujX590cB5Ttln20E2UB4S/WARVqhXffZl2LNgS+gQdPIIim/g== extglob@^2.0.4: version "2.0.4" resolved "https://registry.yarnpkg.com/extglob/-/extglob-2.0.4.tgz#ad00fe4dc612a9232e8718711dc5cb5ab0285543" + integrity sha512-Nmb6QXkELsuBr24CJSkilo6UHHgbekK5UiZgfE6UHD3Eb27YC6oD+bhcT+tJ6cl8dmsgdQxnWlcry8ksBIBLpw== dependencies: array-unique "^0.3.2" define-property "^1.0.0" @@ -1358,14 +1602,17 @@ extglob@^2.0.4: extsprintf@1.3.0: version "1.3.0" resolved "https://registry.yarnpkg.com/extsprintf/-/extsprintf-1.3.0.tgz#96918440e3041a7a414f8c52e3c574eb3c3e1e05" + integrity sha1-lpGEQOMEGnpBT4xS48V06zw+HgU= extsprintf@^1.2.0: version "1.4.0" resolved "https://registry.yarnpkg.com/extsprintf/-/extsprintf-1.4.0.tgz#e2689f8f356fad62cca65a3a91c5df5f9551692f" + integrity sha1-4mifjzVvrWLMplo6kcXfX5VRaS8= fast-deep-equal@^2.0.1: version "2.0.1" resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-2.0.1.tgz#7b05218ddf9667bf7f370bf7fdb2cb15fdd0aa49" + integrity sha1-ewUhjd+WZ79/Nwv3/bLLFf3Qqkk= fast-json-stable-stringify@2.x, fast-json-stable-stringify@^2.0.0: version "2.0.0" @@ -1374,10 +1621,12 @@ fast-json-stable-stringify@2.x, fast-json-stable-stringify@^2.0.0: fast-levenshtein@~2.0.4: version "2.0.6" resolved "https://registry.yarnpkg.com/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz#3d8a5c66883a16a30ca8643e851f19baa7797917" + integrity sha1-PYpcZog6FqMMqGQ+hR8Zuqd5eRc= fb-watchman@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/fb-watchman/-/fb-watchman-2.0.0.tgz#54e9abf7dfa2f26cd9b1636c588c1afc05de5d58" + integrity sha1-VOmr99+i8mzZsWNsWIwa/AXeXVg= dependencies: bser "^2.0.0" @@ -1385,30 +1634,10 @@ figgy-pudding@^3.5.1: version "3.5.1" resolved "https://registry.yarnpkg.com/figgy-pudding/-/figgy-pudding-3.5.1.tgz#862470112901c727a0e495a80744bd5baa1d6790" -filename-regex@^2.0.0: - version "2.0.1" - resolved "https://registry.yarnpkg.com/filename-regex/-/filename-regex-2.0.1.tgz#c1c4b9bee3e09725ddb106b75c1e301fe2f18b26" - -fileset@^2.0.2: - version "2.0.3" - resolved "https://registry.yarnpkg.com/fileset/-/fileset-2.0.3.tgz#8e7548a96d3cc2327ee5e674168723a333bba2a0" - dependencies: - glob "^7.0.3" - minimatch "^3.0.3" - -fill-range@^2.1.0: - version "2.2.4" - resolved "https://registry.yarnpkg.com/fill-range/-/fill-range-2.2.4.tgz#eb1e773abb056dcd8df2bfdf6af59b8b3a936565" - dependencies: - is-number "^2.1.0" - isobject "^2.0.0" - randomatic "^3.0.0" - repeat-element "^1.1.2" - repeat-string "^1.5.2" - fill-range@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/fill-range/-/fill-range-4.0.0.tgz#d544811d428f98eb06a63dc402d2403c328c38f7" + integrity sha1-1USBHUKPmOsGpj3EAtJAPDKMOPc= dependencies: extend-shallow "^2.0.1" is-number "^3.0.0" @@ -1423,19 +1652,6 @@ find-cache-dir@^2.0.0: make-dir "^1.0.0" pkg-dir "^3.0.0" -find-up@^1.0.0: - version "1.1.2" - resolved "https://registry.yarnpkg.com/find-up/-/find-up-1.1.2.tgz#6b2e9822b1a2ce0a60ab64d610eccad53cb24d0f" - dependencies: - path-exists "^2.0.0" - pinkie-promise "^2.0.0" - -find-up@^2.1.0: - version "2.1.0" - resolved "https://registry.yarnpkg.com/find-up/-/find-up-2.1.0.tgz#45d1b7e506c717ddd482775a2b77920a3c0c57a7" - dependencies: - locate-path "^2.0.0" - find-up@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/find-up/-/find-up-3.0.0.tgz#49169f1d7993430646da61ecc5ae355c21c97b73" @@ -1458,23 +1674,20 @@ flush-write-stream@^1.0.0: inherits "^2.0.3" readable-stream "^2.3.6" -for-in@^1.0.1, for-in@^1.0.2: +for-in@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/for-in/-/for-in-1.0.2.tgz#81068d295a8142ec0ac726c6e2200c30fb6d5e80" - -for-own@^0.1.4: - version "0.1.5" - resolved "https://registry.yarnpkg.com/for-own/-/for-own-0.1.5.tgz#5265c681a4f294dabbf17c9509b6763aa84510ce" - dependencies: - for-in "^1.0.1" + integrity sha1-gQaNKVqBQuwKxybG4iAMMPttXoA= forever-agent@~0.6.1: version "0.6.1" resolved "https://registry.yarnpkg.com/forever-agent/-/forever-agent-0.6.1.tgz#fbc71f0c41adeb37f96c577ad1ed42d8fdacca91" + integrity sha1-+8cfDEGt6zf5bFd60e1C2P2sypE= form-data@~2.3.2: version "2.3.3" resolved "https://registry.yarnpkg.com/form-data/-/form-data-2.3.3.tgz#dcce52c05f644f298c6a7ab936bd724ceffbf3a6" + integrity sha512-1lLKB2Mu3aGP1Q/2eCOx0fNbRMe7XdwktwOruhfqqd0rIJWwN4Dh+E3hrPSlDCXnSR7UtZ1N38rVXm+6+MEhJQ== dependencies: asynckit "^0.4.0" combined-stream "^1.0.6" @@ -1483,6 +1696,7 @@ form-data@~2.3.2: fragment-cache@^0.2.1: version "0.2.1" resolved "https://registry.yarnpkg.com/fragment-cache/-/fragment-cache-0.2.1.tgz#4290fad27f13e89be7f33799c6bc5a0abfff0d19" + integrity sha1-QpD60n8T6Jvn8zeZxrxaCr//DRk= dependencies: map-cache "^0.2.2" @@ -1494,8 +1708,9 @@ from2@^2.1.0: readable-stream "^2.0.0" fs-minipass@^1.2.5: - version "1.2.5" - resolved "https://registry.yarnpkg.com/fs-minipass/-/fs-minipass-1.2.5.tgz#06c277218454ec288df77ada54a03b8702aacb9d" + version "1.2.6" + resolved "https://registry.yarnpkg.com/fs-minipass/-/fs-minipass-1.2.6.tgz#2c5cc30ded81282bfe8a0d7c7c1853ddeb102c07" + integrity sha512-crhvyXcMejjv3Z5d2Fa9sf5xLYVCF5O1c71QxbVnbLsmYMBEvDAftewesN/HhY03YRoA7zOMxjNGrF5svGaaeQ== dependencies: minipass "^2.2.1" @@ -1511,8 +1726,9 @@ fs-write-stream-atomic@^1.0.8: fs.realpath@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f" + integrity sha1-FQStJSMVjKpA20onh8sBQRmU6k8= -fsevents@^1.2.3, fsevents@^1.2.7: +fsevents@^1.2.7: version "1.2.7" resolved "https://registry.yarnpkg.com/fsevents/-/fsevents-1.2.7.tgz#4851b664a3783e52003b3c66eb0eee1074933aa4" dependencies: @@ -1522,10 +1738,12 @@ fsevents@^1.2.3, fsevents@^1.2.7: function-bind@^1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/function-bind/-/function-bind-1.1.1.tgz#a56899d3ea3c9bab874bb9773b7c5ede92f4895d" + integrity sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A== gauge@~2.7.3: version "2.7.4" resolved "https://registry.yarnpkg.com/gauge/-/gauge-2.7.4.tgz#2c03405c7538c39d7eb37b317022e325fb018bf7" + integrity sha1-LANAXHU4w51+s3sxcCLjJfsBi/c= dependencies: aproba "^1.0.3" console-control-strings "^1.0.0" @@ -1539,10 +1757,7 @@ gauge@~2.7.3: get-caller-file@^1.0.1: version "1.0.3" resolved "https://registry.yarnpkg.com/get-caller-file/-/get-caller-file-1.0.3.tgz#f978fa4c90d1dfe7ff2d6beda2a515e713bdcf4a" - -get-stream@^3.0.0: - version "3.0.0" - resolved "https://registry.yarnpkg.com/get-stream/-/get-stream-3.0.0.tgz#8e943d1358dc37555054ecbe2edb05aa174ede14" + integrity sha512-3t6rVToeoZfYSGd8YoLFR2DJkiQrIiUrGcjvFX2mDw3bn6k2OtwHN0TNCLbBO+w8qTvimhDkv+LSscbJY1vE6w== get-stream@^4.0.0: version "4.1.0" @@ -1553,26 +1768,15 @@ get-stream@^4.0.0: get-value@^2.0.3, get-value@^2.0.6: version "2.0.6" resolved "https://registry.yarnpkg.com/get-value/-/get-value-2.0.6.tgz#dc15ca1c672387ca76bd37ac0a395ba2042a2c28" + integrity sha1-3BXKHGcjh8p2vTesCjlbogQqLCg= getpass@^0.1.1: version "0.1.7" resolved "https://registry.yarnpkg.com/getpass/-/getpass-0.1.7.tgz#5eff8e3e684d569ae4cb2b1282604e8ba62149fa" + integrity sha1-Xv+OPmhNVprkyysSgmBOi6YhSfo= dependencies: assert-plus "^1.0.0" -glob-base@^0.3.0: - version "0.3.0" - resolved "https://registry.yarnpkg.com/glob-base/-/glob-base-0.3.0.tgz#dbb164f6221b1c0b1ccf82aea328b497df0ea3c4" - dependencies: - glob-parent "^2.0.0" - is-glob "^2.0.0" - -glob-parent@^2.0.0: - version "2.0.0" - resolved "https://registry.yarnpkg.com/glob-parent/-/glob-parent-2.0.0.tgz#81383d72db054fcccf5336daa902f182f6edbb28" - dependencies: - is-glob "^2.0.0" - glob-parent@^3.1.0: version "3.1.0" resolved "https://registry.yarnpkg.com/glob-parent/-/glob-parent-3.1.0.tgz#9e6af6299d8d3bd2bd40430832bd113df906c5ae" @@ -1580,9 +1784,10 @@ glob-parent@^3.1.0: is-glob "^3.1.0" path-dirname "^1.0.0" -glob@^7.0.3, glob@^7.1.1, glob@^7.1.2, glob@^7.1.3: - version "7.1.3" - resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.3.tgz#3960832d3f1574108342dafd3a67b332c0969df1" +glob@^7.1.1, glob@^7.1.2, glob@^7.1.3: + version "7.1.4" + resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.4.tgz#aa608a2f6c577ad357e1ae5a5c26d9a8d1969255" + integrity sha512-hkLPepehmnKk41pUGm3sYxoFs/umurYfYJCerbXEyFIWcAzvpipAgVkBqqT9RBKMGjnq6kMuyYwha6csxbiM1A== dependencies: fs.realpath "^1.0.0" inflight "^1.0.4" @@ -1609,23 +1814,27 @@ global-prefix@^1.0.1: is-windows "^1.0.1" which "^1.2.14" -globals@^9.18.0: - version "9.18.0" - resolved "https://registry.yarnpkg.com/globals/-/globals-9.18.0.tgz#aa3896b3e69b487f17e31ed2143d69a8e30c2d8a" +globals@^11.1.0: + version "11.12.0" + resolved "https://registry.yarnpkg.com/globals/-/globals-11.12.0.tgz#ab8795338868a0babd8525758018c2a7eb95c42e" + integrity sha512-WOBp/EEGUiIsJSp7wcv/y6MO+lV9UoncWqxuFfm8eBwzWNgyfBd6Gz+IeKQ9jCmyhoH99g15M3T+QaVHFjizVA== graceful-fs@^4.1.11, graceful-fs@^4.1.15, graceful-fs@^4.1.2: version "4.1.15" resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.1.15.tgz#ffb703e1066e8a0eeaa4c8b80ba9253eeefbfb00" + integrity sha512-6uHUhOPEBgQ24HM+r6b/QwWfZq+yiFcipKFrOFiBEnWdy5sdzYoi+pJeQaPI5qOLRFqWmAXUPQNsielzdLoecA== growly@^1.3.0: version "1.3.0" resolved "https://registry.yarnpkg.com/growly/-/growly-1.3.0.tgz#f10748cbe76af964b7c96c93c6bcc28af120c081" + integrity sha1-8QdIy+dq+WS3yWyTxrzCivEgwIE= -handlebars@^4.0.3: - version "4.1.0" - resolved "https://registry.yarnpkg.com/handlebars/-/handlebars-4.1.0.tgz#0d6a6f34ff1f63cecec8423aa4169827bf787c3a" +handlebars@^4.1.2: + version "4.1.2" + resolved "https://registry.yarnpkg.com/handlebars/-/handlebars-4.1.2.tgz#b6b37c1ced0306b221e094fc7aca3ec23b131b67" + integrity sha512-nvfrjqvt9xQ8Z/w0ijewdD/vvWDTOweBUm96NTr66Wfvo1mJenBLwcYmPs3TIBP5ruzYGD7Hx/DaM9RmhroGPw== dependencies: - async "^2.5.0" + neo-async "^2.6.0" optimist "^0.6.1" source-map "^0.6.1" optionalDependencies: @@ -1634,39 +1843,35 @@ handlebars@^4.0.3: har-schema@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/har-schema/-/har-schema-2.0.0.tgz#a94c2224ebcac04782a0d9035521f24735b7ec92" + integrity sha1-qUwiJOvKwEeCoNkDVSHyRzW37JI= har-validator@~5.1.0: version "5.1.3" resolved "https://registry.yarnpkg.com/har-validator/-/har-validator-5.1.3.tgz#1ef89ebd3e4996557675eed9893110dc350fa080" + integrity sha512-sNvOCzEQNr/qrvJgc3UG/kD4QtlHycrzwS+6mfTrrSq97BvaYcPZZI1ZSqGSPR73Cxn4LKTD4PttRwfU7jWq5g== dependencies: ajv "^6.5.5" har-schema "^2.0.0" -has-ansi@^2.0.0: - version "2.0.0" - resolved "https://registry.yarnpkg.com/has-ansi/-/has-ansi-2.0.0.tgz#34f5049ce1ecdf2b0649af3ef24e45ed35416d91" - dependencies: - ansi-regex "^2.0.0" - -has-flag@^1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-1.0.0.tgz#9d9e793165ce017a00f00418c43f942a7b1d11fa" - has-flag@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-3.0.0.tgz#b5d454dc2199ae225699f3467e5a07f3b955bafd" + integrity sha1-tdRU3CGZriJWmfNGfloH87lVuv0= has-symbols@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/has-symbols/-/has-symbols-1.0.0.tgz#ba1a8f1af2a0fc39650f5c850367704122063b44" + integrity sha1-uhqPGvKg/DllD1yFA2dwQSIGO0Q= has-unicode@^2.0.0: version "2.0.1" resolved "https://registry.yarnpkg.com/has-unicode/-/has-unicode-2.0.1.tgz#e0e6fe6a28cf51138855e086d1691e771de2a8b9" + integrity sha1-4Ob+aijPUROIVeCG0Wkedx3iqLk= has-value@^0.3.1: version "0.3.1" resolved "https://registry.yarnpkg.com/has-value/-/has-value-0.3.1.tgz#7b1f58bada62ca827ec0a2078025654845995e1f" + integrity sha1-ex9YutpiyoJ+wKIHgCVlSEWZXh8= dependencies: get-value "^2.0.3" has-values "^0.1.4" @@ -1675,6 +1880,7 @@ has-value@^0.3.1: has-value@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/has-value/-/has-value-1.0.0.tgz#18b281da585b1c5c51def24c930ed29a0be6b177" + integrity sha1-GLKB2lhbHFxR3vJMkw7SmgvmsXc= dependencies: get-value "^2.0.6" has-values "^1.0.0" @@ -1683,10 +1889,12 @@ has-value@^1.0.0: has-values@^0.1.4: version "0.1.4" resolved "https://registry.yarnpkg.com/has-values/-/has-values-0.1.4.tgz#6d61de95d91dfca9b9a02089ad384bff8f62b771" + integrity sha1-bWHeldkd/Km5oCCJrThL/49it3E= has-values@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/has-values/-/has-values-1.0.0.tgz#95b0b63fec2146619a6fe57fe75628d5a39efe4f" + integrity sha1-lbC2P+whRmGab+V/51Yo1aOe/k8= dependencies: is-number "^3.0.0" kind-of "^4.0.0" @@ -1694,6 +1902,7 @@ has-values@^1.0.0: has@^1.0.1, has@^1.0.3: version "1.0.3" resolved "https://registry.yarnpkg.com/has/-/has-1.0.3.tgz#722d7cbfc1f6aa8241f16dd814e011e1f41e8796" + integrity sha512-f2dvO0VU6Oej7RkWJGrehjbzMAjFp5/VKPp5tTpWIV4JHHZK1/BxbFRtf/siA2SWTe09caDmVtYYzWEIbBS4zw== dependencies: function-bind "^1.1.1" @@ -1719,13 +1928,6 @@ hmac-drbg@^1.0.0: minimalistic-assert "^1.0.0" minimalistic-crypto-utils "^1.0.1" -home-or-tmp@^2.0.0: - version "2.0.0" - resolved "https://registry.yarnpkg.com/home-or-tmp/-/home-or-tmp-2.0.0.tgz#e36c3f2d2cae7d746a857e38d18d5f32a7882db8" - dependencies: - os-homedir "^1.0.0" - os-tmpdir "^1.0.1" - homedir-polyfill@^1.0.1: version "1.0.3" resolved "https://registry.yarnpkg.com/homedir-polyfill/-/homedir-polyfill-1.0.3.tgz#743298cef4e5af3e194161fbadcc2151d3a058e8" @@ -1735,16 +1937,19 @@ homedir-polyfill@^1.0.1: hosted-git-info@^2.1.4: version "2.7.1" resolved "https://registry.yarnpkg.com/hosted-git-info/-/hosted-git-info-2.7.1.tgz#97f236977bd6e125408930ff6de3eec6281ec047" + integrity sha512-7T/BxH19zbcCTa8XkMlbK5lTo1WtgkFi3GvdWEyNuc4Vex7/9Dqbnpsf4JMydcfj9HCg4zUWFTL3Za6lapg5/w== html-encoding-sniffer@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/html-encoding-sniffer/-/html-encoding-sniffer-1.0.2.tgz#e70d84b94da53aa375e11fe3a351be6642ca46f8" + integrity sha512-71lZziiDnsuabfdYiUeWdCVyKuqwWi23L8YeIgV9jSSZHCtb6wB1BKWooH7L3tn4/FuZJMVWyNaIDr4RGmaSYw== dependencies: whatwg-encoding "^1.0.1" http-signature@~1.2.0: version "1.2.0" resolved "https://registry.yarnpkg.com/http-signature/-/http-signature-1.2.0.tgz#9aecd925114772f3d95b65a60abb8f7c18fbace1" + integrity sha1-muzZJRFHcvPZW2WmCruPfBj7rOE= dependencies: assert-plus "^1.0.0" jsprim "^1.2.2" @@ -1757,6 +1962,7 @@ https-browserify@^1.0.0: iconv-lite@0.4.24, iconv-lite@^0.4.4: version "0.4.24" resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.24.tgz#2022b4b25fbddc21d2f524974a474aafe733908b" + integrity sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA== dependencies: safer-buffer ">= 2.1.2 < 3" @@ -1771,16 +1977,10 @@ iferr@^0.1.5: ignore-walk@^3.0.1: version "3.0.1" resolved "https://registry.yarnpkg.com/ignore-walk/-/ignore-walk-3.0.1.tgz#a83e62e7d272ac0e3b551aaa82831a19b69f82f8" + integrity sha512-DTVlMx3IYPe0/JJcYP7Gxg7ttZZu3IInhuEhbchuqneY9wWe5Ojy2mXLBaQFUQmo0AW2r3qG7m1mg86js+gnlQ== dependencies: minimatch "^3.0.4" -import-local@^1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/import-local/-/import-local-1.0.0.tgz#5e4ffdc03f4fe6c009c6729beb29631c2f8227bc" - dependencies: - pkg-dir "^2.0.0" - resolve-cwd "^2.0.0" - import-local@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/import-local/-/import-local-2.0.0.tgz#55070be38a5993cf18ef6db7e961f5bee5c5a09d" @@ -1791,6 +1991,7 @@ import-local@^2.0.0: imurmurhash@^0.1.4: version "0.1.4" resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea" + integrity sha1-khi5srkoojixPcT7a21XbyMUU+o= indexof@0.0.1: version "0.0.1" @@ -1799,6 +2000,7 @@ indexof@0.0.1: inflight@^1.0.4: version "1.0.6" resolved "https://registry.yarnpkg.com/inflight/-/inflight-1.0.6.tgz#49bd6331d7d02d0c09bc910a1075ba8165b56df9" + integrity sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk= dependencies: once "^1.3.0" wrappy "1" @@ -1806,6 +2008,7 @@ inflight@^1.0.4: inherits@2, inherits@2.0.3, inherits@^2.0.1, inherits@^2.0.3, inherits@~2.0.1, inherits@~2.0.3: version "2.0.3" resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de" + integrity sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4= inherits@2.0.1: version "2.0.1" @@ -1814,38 +2017,34 @@ inherits@2.0.1: ini@^1.3.4, ini@~1.3.0: version "1.3.5" resolved "https://registry.yarnpkg.com/ini/-/ini-1.3.5.tgz#eee25f56db1c9ec6085e0c22778083f596abf927" + integrity sha512-RZY5huIKCMRWDUqZlEi72f/lmXKMvuszcMBduliQ3nnWbx9X/ZBQO7DijMEYS9EhHBb2qacRUMtC7svLwe0lcw== interpret@^1.1.0: version "1.2.0" resolved "https://registry.yarnpkg.com/interpret/-/interpret-1.2.0.tgz#d5061a6224be58e8083985f5014d844359576296" -invariant@^2.2.2, invariant@^2.2.4: +invariant@^2.2.4: version "2.2.4" resolved "https://registry.yarnpkg.com/invariant/-/invariant-2.2.4.tgz#610f3c92c9359ce1db616e538008d23ff35158e6" + integrity sha512-phJfQVBuaJM5raOpJjSfkiD6BpbCE4Ns//LaXl6wGYtUBY83nWS6Rf9tXm2e8VaK60JEjYldbPif/A2B1C2gNA== dependencies: loose-envify "^1.0.0" -invert-kv@^1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/invert-kv/-/invert-kv-1.0.0.tgz#104a8e4aaca6d3d8cd157a8ef8bfab2d7a3ffdb6" - invert-kv@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/invert-kv/-/invert-kv-2.0.0.tgz#7393f5afa59ec9ff5f67a27620d11c226e3eec02" -ip-regex@^2.1.0: - version "2.1.0" - resolved "https://registry.yarnpkg.com/ip-regex/-/ip-regex-2.1.0.tgz#fa78bf5d2e6913c911ce9f819ee5146bb6d844e9" - is-accessor-descriptor@^0.1.6: version "0.1.6" resolved "https://registry.yarnpkg.com/is-accessor-descriptor/-/is-accessor-descriptor-0.1.6.tgz#a9e12cb3ae8d876727eeef3843f8a0897b5c98d6" + integrity sha1-qeEss66Nh2cn7u84Q/igiXtcmNY= dependencies: kind-of "^3.0.2" is-accessor-descriptor@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/is-accessor-descriptor/-/is-accessor-descriptor-1.0.0.tgz#169c2f6d3df1f992618072365c9b0ea1f6878656" + integrity sha512-m5hnHTkcVsPfqx3AKlyttIPb7J+XykHvJP2B9bZDjlhLIoEq4XoK64Vg7boZlVWYK6LUY94dYPEE7Lh0ZkZKcQ== dependencies: kind-of "^6.0.0" @@ -1856,6 +2055,7 @@ is-any-array@^0.0.3: is-arrayish@^0.2.1: version "0.2.1" resolved "https://registry.yarnpkg.com/is-arrayish/-/is-arrayish-0.2.1.tgz#77c99840527aa8ecb1a8ba697b80645a7a926a9d" + integrity sha1-d8mYQFJ6qOyxqLppe4BkWnqSap0= is-binary-path@^1.0.0: version "1.0.1" @@ -1866,36 +2066,43 @@ is-binary-path@^1.0.0: is-buffer@^1.1.5: version "1.1.6" resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-1.1.6.tgz#efaa2ea9daa0d7ab2ea13a97b2b8ad51fefbe8be" + integrity sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w== is-callable@^1.1.4: version "1.1.4" resolved "https://registry.yarnpkg.com/is-callable/-/is-callable-1.1.4.tgz#1e1adf219e1eeb684d691f9d6a05ff0d30a24d75" + integrity sha512-r5p9sxJjYnArLjObpjA4xu5EKI3CuKHkJXMhT7kwbpUyIFD1n5PMAsoPvWnvtZiNz7LjkYDRZhd7FlI0eMijEA== -is-ci@^1.0.10: - version "1.2.1" - resolved "https://registry.yarnpkg.com/is-ci/-/is-ci-1.2.1.tgz#e3779c8ee17fccf428488f6e281187f2e632841c" +is-ci@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/is-ci/-/is-ci-2.0.0.tgz#6bc6334181810e04b5c22b3d589fdca55026404c" + integrity sha512-YfJT7rkpQB0updsdHLGWrvhBJfcfzNNawYDNIyQXJz0IViGf75O8EBPKSdvw2rF+LGCsX4FZ8tcr3b19LcZq4w== dependencies: - ci-info "^1.5.0" + ci-info "^2.0.0" is-data-descriptor@^0.1.4: version "0.1.4" resolved "https://registry.yarnpkg.com/is-data-descriptor/-/is-data-descriptor-0.1.4.tgz#0b5ee648388e2c860282e793f1856fec3f301b56" + integrity sha1-C17mSDiOLIYCgueT8YVv7D8wG1Y= dependencies: kind-of "^3.0.2" is-data-descriptor@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/is-data-descriptor/-/is-data-descriptor-1.0.0.tgz#d84876321d0e7add03990406abbbbd36ba9268c7" + integrity sha512-jbRXy1FmtAoCjQkVmIVYwuuqDFUbaOeDjmed1tOGPrsMhtJA4rD9tkgA0F1qJ3gRFRXcHYVkdeaP50Q5rE/jLQ== dependencies: kind-of "^6.0.0" is-date-object@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/is-date-object/-/is-date-object-1.0.1.tgz#9aa20eb6aeebbff77fbd33e74ca01b33581d3a16" + integrity sha1-mqIOtq7rv/d/vTPnTKAbM1gdOhY= is-descriptor@^0.1.0: version "0.1.6" resolved "https://registry.yarnpkg.com/is-descriptor/-/is-descriptor-0.1.6.tgz#366d8240dde487ca51823b1ab9f07a10a78251ca" + integrity sha512-avDYr0SB3DwO9zsMov0gKCESFYqCnE4hq/4z3TdUlukEy5t9C0YRq7HLrsN52NAcqXKaepeCD0n+B0arnVG3Hg== dependencies: is-accessor-descriptor "^0.1.6" is-data-descriptor "^0.1.4" @@ -1904,64 +2111,44 @@ is-descriptor@^0.1.0: is-descriptor@^1.0.0, is-descriptor@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/is-descriptor/-/is-descriptor-1.0.2.tgz#3b159746a66604b04f8c81524ba365c5f14d86ec" + integrity sha512-2eis5WqQGV7peooDyLmNEPUrps9+SXX5c9pL3xEB+4e9HnGuDa7mB7kHxHw4CbqS9k1T2hOH3miL8n8WtiYVtg== dependencies: is-accessor-descriptor "^1.0.0" is-data-descriptor "^1.0.0" kind-of "^6.0.2" -is-dotfile@^1.0.0: - version "1.0.3" - resolved "https://registry.yarnpkg.com/is-dotfile/-/is-dotfile-1.0.3.tgz#a6a2f32ffd2dfb04f5ca25ecd0f6b83cf798a1e1" - -is-equal-shallow@^0.1.3: - version "0.1.3" - resolved "https://registry.yarnpkg.com/is-equal-shallow/-/is-equal-shallow-0.1.3.tgz#2238098fc221de0bcfa5d9eac4c45d638aa1c534" - dependencies: - is-primitive "^2.0.0" - is-extendable@^0.1.0, is-extendable@^0.1.1: version "0.1.1" resolved "https://registry.yarnpkg.com/is-extendable/-/is-extendable-0.1.1.tgz#62b110e289a471418e3ec36a617d472e301dfc89" + integrity sha1-YrEQ4omkcUGOPsNqYX1HLjAd/Ik= is-extendable@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/is-extendable/-/is-extendable-1.0.1.tgz#a7470f9e426733d81bd81e1155264e3a3507cab4" + integrity sha512-arnXMxT1hhoKo9k1LZdmlNyJdDDfy2v0fXjFlmok4+i8ul/6WlbVge9bhM74OpNPQPMGUToDtz+KXa1PneJxOA== dependencies: is-plain-object "^2.0.4" -is-extglob@^1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/is-extglob/-/is-extglob-1.0.0.tgz#ac468177c4943405a092fc8f29760c6ffc6206c0" - is-extglob@^2.1.0, is-extglob@^2.1.1: version "2.1.1" resolved "https://registry.yarnpkg.com/is-extglob/-/is-extglob-2.1.1.tgz#a88c02535791f02ed37c76a1b9ea9773c833f8c2" -is-finite@^1.0.0: - version "1.0.2" - resolved "https://registry.yarnpkg.com/is-finite/-/is-finite-1.0.2.tgz#cc6677695602be550ef11e8b4aa6305342b6d0aa" - dependencies: - number-is-nan "^1.0.0" - is-fullwidth-code-point@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/is-fullwidth-code-point/-/is-fullwidth-code-point-1.0.0.tgz#ef9e31386f031a7f0d643af82fde50c457ef00cb" + integrity sha1-754xOG8DGn8NZDr4L95QxFfvAMs= dependencies: number-is-nan "^1.0.0" is-fullwidth-code-point@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/is-fullwidth-code-point/-/is-fullwidth-code-point-2.0.0.tgz#a3b30a5c4f199183167aaab93beefae3ddfb654f" + integrity sha1-o7MKXE8ZkYMWeqq5O+764937ZU8= -is-generator-fn@^1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/is-generator-fn/-/is-generator-fn-1.0.0.tgz#969d49e1bb3329f6bb7f09089be26578b2ddd46a" - -is-glob@^2.0.0, is-glob@^2.0.1: - version "2.0.1" - resolved "https://registry.yarnpkg.com/is-glob/-/is-glob-2.0.1.tgz#d096f926a3ded5600f3fdfd91198cb0888c2d863" - dependencies: - is-extglob "^1.0.0" +is-generator-fn@^2.0.0: + version "2.1.0" + resolved "https://registry.yarnpkg.com/is-generator-fn/-/is-generator-fn-2.1.0.tgz#7d140adc389aaf3011a8f2a2a4cfa6faadffb118" + integrity sha512-cTIB4yPYL/Grw0EaSzASzg6bBy9gqCofvWN8okThAYIxKJZC+udlRAmGbM0XLeniEJSs8uEgHPGuHSe1XsOLSQ== is-glob@^3.1.0: version "3.1.0" @@ -1975,465 +2162,492 @@ is-glob@^4.0.0: dependencies: is-extglob "^2.1.1" -is-number@^2.1.0: - version "2.1.0" - resolved "https://registry.yarnpkg.com/is-number/-/is-number-2.1.0.tgz#01fcbbb393463a548f2f466cce16dece49db908f" - dependencies: - kind-of "^3.0.2" - is-number@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/is-number/-/is-number-3.0.0.tgz#24fd6201a4782cf50561c810276afc7d12d71195" + integrity sha1-JP1iAaR4LPUFYcgQJ2r8fRLXEZU= dependencies: kind-of "^3.0.2" -is-number@^4.0.0: - version "4.0.0" - resolved "https://registry.yarnpkg.com/is-number/-/is-number-4.0.0.tgz#0026e37f5454d73e356dfe6564699867c6a7f0ff" - is-plain-object@^2.0.1, is-plain-object@^2.0.3, is-plain-object@^2.0.4: version "2.0.4" resolved "https://registry.yarnpkg.com/is-plain-object/-/is-plain-object-2.0.4.tgz#2c163b3fafb1b606d9d17928f05c2a1c38e07677" + integrity sha512-h5PpgXkWitc38BBMYawTYMWJHFZJVnBquFE57xFpjB8pJFiF6gZ+bU+WyI/yqXiFR5mdLsgYNaPe8uao6Uv9Og== dependencies: isobject "^3.0.1" -is-posix-bracket@^0.1.0: - version "0.1.1" - resolved "https://registry.yarnpkg.com/is-posix-bracket/-/is-posix-bracket-0.1.1.tgz#3334dc79774368e92f016e6fbc0a88f5cd6e6bc4" - -is-primitive@^2.0.0: - version "2.0.0" - resolved "https://registry.yarnpkg.com/is-primitive/-/is-primitive-2.0.0.tgz#207bab91638499c07b2adf240a41a87210034575" - is-regex@^1.0.4: version "1.0.4" resolved "https://registry.yarnpkg.com/is-regex/-/is-regex-1.0.4.tgz#5517489b547091b0930e095654ced25ee97e9491" + integrity sha1-VRdIm1RwkbCTDglWVM7SXul+lJE= dependencies: has "^1.0.1" is-stream@^1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/is-stream/-/is-stream-1.1.0.tgz#12d4a3dd4e68e0b79ceb8dbc84173ae80d91ca44" + integrity sha1-EtSj3U5o4Lec6428hBc66A2RykQ= is-symbol@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/is-symbol/-/is-symbol-1.0.2.tgz#a055f6ae57192caee329e7a860118b497a950f38" + integrity sha512-HS8bZ9ox60yCJLH9snBpIwv9pYUAkcuLhSA1oero1UB5y9aiQpRA8y2ex945AOtCZL1lJDeIk3G5LthswI46Lw== dependencies: has-symbols "^1.0.0" is-typedarray@~1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/is-typedarray/-/is-typedarray-1.0.0.tgz#e479c80858df0c1b11ddda6940f96011fcda4a9a" - -is-utf8@^0.2.0: - version "0.2.1" - resolved "https://registry.yarnpkg.com/is-utf8/-/is-utf8-0.2.1.tgz#4b0da1442104d1b336340e80797e865cf39f7d72" + integrity sha1-5HnICFjfDBsR3dppQPlgEfzaSpo= is-windows@^1.0.1, is-windows@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/is-windows/-/is-windows-1.0.2.tgz#d1850eb9791ecd18e6182ce12a30f396634bb19d" + integrity sha512-eXK1UInq2bPmjyX6e3VHIzMLobc4J94i4AWn+Hpq3OU5KkrRC96OAcR3PRJ/pGu6m8TRnBHP9dkXQVsT/COVIA== is-wsl@^1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/is-wsl/-/is-wsl-1.1.0.tgz#1f16e4aa22b04d1336b66188a66af3c600c3a66d" + integrity sha1-HxbkqiKwTRM2tmGIpmrzxgDDpm0= isarray@1.0.0, isarray@^1.0.0, isarray@~1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11" + integrity sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE= isexe@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/isexe/-/isexe-2.0.0.tgz#e8fbf374dc556ff8947a10dcb0572d633f2cfa10" + integrity sha1-6PvzdNxVb/iUehDcsFctYz8s+hA= isobject@^2.0.0: version "2.1.0" resolved "https://registry.yarnpkg.com/isobject/-/isobject-2.1.0.tgz#f065561096a3f1da2ef46272f815c840d87e0c89" + integrity sha1-8GVWEJaj8dou9GJy+BXIQNh+DIk= dependencies: isarray "1.0.0" isobject@^3.0.0, isobject@^3.0.1: version "3.0.1" resolved "https://registry.yarnpkg.com/isobject/-/isobject-3.0.1.tgz#4e431e92b11a9731636aa1f9c8d1ccbcfdab78df" + integrity sha1-TkMekrEalzFjaqH5yNHMvP2reN8= isstream@~0.1.2: version "0.1.2" resolved "https://registry.yarnpkg.com/isstream/-/isstream-0.1.2.tgz#47e63f7af55afa6f92e1500e690eb8b8529c099a" + integrity sha1-R+Y/evVa+m+S4VAOaQ64uFKcCZo= -istanbul-api@^1.3.1: - version "1.3.7" - resolved "https://registry.yarnpkg.com/istanbul-api/-/istanbul-api-1.3.7.tgz#a86c770d2b03e11e3f778cd7aedd82d2722092aa" - dependencies: - async "^2.1.4" - fileset "^2.0.2" - istanbul-lib-coverage "^1.2.1" - istanbul-lib-hook "^1.2.2" - istanbul-lib-instrument "^1.10.2" - istanbul-lib-report "^1.1.5" - istanbul-lib-source-maps "^1.2.6" - istanbul-reports "^1.5.1" - js-yaml "^3.7.0" - mkdirp "^0.5.1" - once "^1.4.0" - -istanbul-lib-coverage@^1.2.0, istanbul-lib-coverage@^1.2.1: - version "1.2.1" - resolved "https://registry.yarnpkg.com/istanbul-lib-coverage/-/istanbul-lib-coverage-1.2.1.tgz#ccf7edcd0a0bb9b8f729feeb0930470f9af664f0" - -istanbul-lib-hook@^1.2.2: - version "1.2.2" - resolved "https://registry.yarnpkg.com/istanbul-lib-hook/-/istanbul-lib-hook-1.2.2.tgz#bc6bf07f12a641fbf1c85391d0daa8f0aea6bf86" - dependencies: - append-transform "^0.4.0" - -istanbul-lib-instrument@^1.10.1, istanbul-lib-instrument@^1.10.2: - version "1.10.2" - resolved "https://registry.yarnpkg.com/istanbul-lib-instrument/-/istanbul-lib-instrument-1.10.2.tgz#1f55ed10ac3c47f2bdddd5307935126754d0a9ca" - dependencies: - babel-generator "^6.18.0" - babel-template "^6.16.0" - babel-traverse "^6.18.0" - babel-types "^6.18.0" - babylon "^6.18.0" - istanbul-lib-coverage "^1.2.1" - semver "^5.3.0" - -istanbul-lib-report@^1.1.5: - version "1.1.5" - resolved "https://registry.yarnpkg.com/istanbul-lib-report/-/istanbul-lib-report-1.1.5.tgz#f2a657fc6282f96170aaf281eb30a458f7f4170c" - dependencies: - istanbul-lib-coverage "^1.2.1" - mkdirp "^0.5.1" - path-parse "^1.0.5" - supports-color "^3.1.2" - -istanbul-lib-source-maps@^1.2.4, istanbul-lib-source-maps@^1.2.6: - version "1.2.6" - resolved "https://registry.yarnpkg.com/istanbul-lib-source-maps/-/istanbul-lib-source-maps-1.2.6.tgz#37b9ff661580f8fca11232752ee42e08c6675d8f" - dependencies: - debug "^3.1.0" - istanbul-lib-coverage "^1.2.1" - mkdirp "^0.5.1" - rimraf "^2.6.1" - source-map "^0.5.3" +istanbul-lib-coverage@^2.0.2, istanbul-lib-coverage@^2.0.5: + version "2.0.5" + resolved "https://registry.yarnpkg.com/istanbul-lib-coverage/-/istanbul-lib-coverage-2.0.5.tgz#675f0ab69503fad4b1d849f736baaca803344f49" + integrity sha512-8aXznuEPCJvGnMSRft4udDRDtb1V3pkQkMMI5LI+6HuQz5oQ4J2UFn1H82raA3qJtyOLkkwVqICBQkjnGtn5mA== + +istanbul-lib-instrument@^3.0.1, istanbul-lib-instrument@^3.3.0: + version "3.3.0" + resolved "https://registry.yarnpkg.com/istanbul-lib-instrument/-/istanbul-lib-instrument-3.3.0.tgz#a5f63d91f0bbc0c3e479ef4c5de027335ec6d630" + integrity sha512-5nnIN4vo5xQZHdXno/YDXJ0G+I3dAm4XgzfSVTPLQpj/zAV2dV6Juy0yaf10/zrJOJeHoN3fraFe+XRq2bFVZA== + dependencies: + "@babel/generator" "^7.4.0" + "@babel/parser" "^7.4.3" + "@babel/template" "^7.4.0" + "@babel/traverse" "^7.4.3" + "@babel/types" "^7.4.0" + istanbul-lib-coverage "^2.0.5" + semver "^6.0.0" + +istanbul-lib-report@^2.0.4: + version "2.0.8" + resolved "https://registry.yarnpkg.com/istanbul-lib-report/-/istanbul-lib-report-2.0.8.tgz#5a8113cd746d43c4889eba36ab10e7d50c9b4f33" + integrity sha512-fHBeG573EIihhAblwgxrSenp0Dby6tJMFR/HvlerBsrCTD5bkUuoNtn3gVh29ZCS824cGGBPn7Sg7cNk+2xUsQ== + dependencies: + istanbul-lib-coverage "^2.0.5" + make-dir "^2.1.0" + supports-color "^6.1.0" + +istanbul-lib-source-maps@^3.0.1: + version "3.0.6" + resolved "https://registry.yarnpkg.com/istanbul-lib-source-maps/-/istanbul-lib-source-maps-3.0.6.tgz#284997c48211752ec486253da97e3879defba8c8" + integrity sha512-R47KzMtDJH6X4/YW9XTx+jrLnZnscW4VpNN+1PViSYTejLVPWv7oov+Duf8YQSPyVRUvueQqz1TcsC6mooZTXw== + dependencies: + debug "^4.1.1" + istanbul-lib-coverage "^2.0.5" + make-dir "^2.1.0" + rimraf "^2.6.3" + source-map "^0.6.1" -istanbul-reports@^1.5.1: - version "1.5.1" - resolved "https://registry.yarnpkg.com/istanbul-reports/-/istanbul-reports-1.5.1.tgz#97e4dbf3b515e8c484caea15d6524eebd3ff4e1a" +istanbul-reports@^2.1.1: + version "2.2.6" + resolved "https://registry.yarnpkg.com/istanbul-reports/-/istanbul-reports-2.2.6.tgz#7b4f2660d82b29303a8fe6091f8ca4bf058da1af" + integrity sha512-SKi4rnMyLBKe0Jy2uUdx28h8oG7ph2PPuQPvIAh31d+Ci+lSiEu4C+h3oBPuJ9+mPKhOyW0M8gY4U5NM1WLeXA== dependencies: - handlebars "^4.0.3" + handlebars "^4.1.2" -jest-changed-files@^23.4.2: - version "23.4.2" - resolved "https://registry.yarnpkg.com/jest-changed-files/-/jest-changed-files-23.4.2.tgz#1eed688370cd5eebafe4ae93d34bb3b64968fe83" +jest-changed-files@^24.8.0: + version "24.8.0" + resolved "https://registry.yarnpkg.com/jest-changed-files/-/jest-changed-files-24.8.0.tgz#7e7eb21cf687587a85e50f3d249d1327e15b157b" + integrity sha512-qgANC1Yrivsq+UrLXsvJefBKVoCsKB0Hv+mBb6NMjjZ90wwxCDmU3hsCXBya30cH+LnPYjwgcU65i6yJ5Nfuug== dependencies: + "@jest/types" "^24.8.0" + execa "^1.0.0" throat "^4.0.0" -jest-cli@^23.6.0: - version "23.6.0" - resolved "https://registry.yarnpkg.com/jest-cli/-/jest-cli-23.6.0.tgz#61ab917744338f443ef2baa282ddffdd658a5da4" +jest-cli@^24.8.0: + version "24.8.0" + resolved "https://registry.yarnpkg.com/jest-cli/-/jest-cli-24.8.0.tgz#b075ac914492ed114fa338ade7362a301693e989" + integrity sha512-+p6J00jSMPQ116ZLlHJJvdf8wbjNbZdeSX9ptfHX06/MSNaXmKihQzx5vQcw0q2G6JsdVkUIdWbOWtSnaYs3yA== dependencies: - ansi-escapes "^3.0.0" + "@jest/core" "^24.8.0" + "@jest/test-result" "^24.8.0" + "@jest/types" "^24.8.0" chalk "^2.0.1" exit "^0.1.2" - glob "^7.1.2" - graceful-fs "^4.1.11" - import-local "^1.0.0" - is-ci "^1.0.10" - istanbul-api "^1.3.1" - istanbul-lib-coverage "^1.2.0" - istanbul-lib-instrument "^1.10.1" - istanbul-lib-source-maps "^1.2.4" - jest-changed-files "^23.4.2" - jest-config "^23.6.0" - jest-environment-jsdom "^23.4.0" - jest-get-type "^22.1.0" - jest-haste-map "^23.6.0" - jest-message-util "^23.4.0" - jest-regex-util "^23.3.0" - jest-resolve-dependencies "^23.6.0" - jest-runner "^23.6.0" - jest-runtime "^23.6.0" - jest-snapshot "^23.6.0" - jest-util "^23.4.0" - jest-validate "^23.6.0" - jest-watcher "^23.4.0" - jest-worker "^23.2.0" - micromatch "^2.3.11" - node-notifier "^5.2.1" - prompts "^0.1.9" - realpath-native "^1.0.0" - rimraf "^2.5.4" - slash "^1.0.0" - string-length "^2.0.0" - strip-ansi "^4.0.0" - which "^1.2.12" - yargs "^11.0.0" - -jest-config@^23.6.0: - version "23.6.0" - resolved "https://registry.yarnpkg.com/jest-config/-/jest-config-23.6.0.tgz#f82546a90ade2d8c7026fbf6ac5207fc22f8eb1d" - dependencies: - babel-core "^6.0.0" - babel-jest "^23.6.0" + import-local "^2.0.0" + is-ci "^2.0.0" + jest-config "^24.8.0" + jest-util "^24.8.0" + jest-validate "^24.8.0" + prompts "^2.0.1" + realpath-native "^1.1.0" + yargs "^12.0.2" + +jest-config@^24.8.0: + version "24.8.0" + resolved "https://registry.yarnpkg.com/jest-config/-/jest-config-24.8.0.tgz#77db3d265a6f726294687cbbccc36f8a76ee0f4f" + integrity sha512-Czl3Nn2uEzVGsOeaewGWoDPD8GStxCpAe0zOYs2x2l0fZAgPbCr3uwUkgNKV3LwE13VXythM946cd5rdGkkBZw== + dependencies: + "@babel/core" "^7.1.0" + "@jest/test-sequencer" "^24.8.0" + "@jest/types" "^24.8.0" + babel-jest "^24.8.0" chalk "^2.0.1" glob "^7.1.1" - jest-environment-jsdom "^23.4.0" - jest-environment-node "^23.4.0" - jest-get-type "^22.1.0" - jest-jasmine2 "^23.6.0" - jest-regex-util "^23.3.0" - jest-resolve "^23.6.0" - jest-util "^23.4.0" - jest-validate "^23.6.0" - micromatch "^2.3.11" - pretty-format "^23.6.0" - -jest-diff@^23.6.0: - version "23.6.0" - resolved "https://registry.yarnpkg.com/jest-diff/-/jest-diff-23.6.0.tgz#1500f3f16e850bb3d71233408089be099f610c7d" + jest-environment-jsdom "^24.8.0" + jest-environment-node "^24.8.0" + jest-get-type "^24.8.0" + jest-jasmine2 "^24.8.0" + jest-regex-util "^24.3.0" + jest-resolve "^24.8.0" + jest-util "^24.8.0" + jest-validate "^24.8.0" + micromatch "^3.1.10" + pretty-format "^24.8.0" + realpath-native "^1.1.0" + +jest-diff@^24.8.0: + version "24.8.0" + resolved "https://registry.yarnpkg.com/jest-diff/-/jest-diff-24.8.0.tgz#146435e7d1e3ffdf293d53ff97e193f1d1546172" + integrity sha512-wxetCEl49zUpJ/bvUmIFjd/o52J+yWcoc5ZyPq4/W1LUKGEhRYDIbP1KcF6t+PvqNrGAFk4/JhtxDq/Nnzs66g== dependencies: chalk "^2.0.1" - diff "^3.2.0" - jest-get-type "^22.1.0" - pretty-format "^23.6.0" + diff-sequences "^24.3.0" + jest-get-type "^24.8.0" + pretty-format "^24.8.0" -jest-docblock@^23.2.0: - version "23.2.0" - resolved "https://registry.yarnpkg.com/jest-docblock/-/jest-docblock-23.2.0.tgz#f085e1f18548d99fdd69b20207e6fd55d91383a7" +jest-docblock@^24.3.0: + version "24.3.0" + resolved "https://registry.yarnpkg.com/jest-docblock/-/jest-docblock-24.3.0.tgz#b9c32dac70f72e4464520d2ba4aec02ab14db5dd" + integrity sha512-nlANmF9Yq1dufhFlKG9rasfQlrY7wINJbo3q01tu56Jv5eBU5jirylhF2O5ZBnLxzOVBGRDz/9NAwNyBtG4Nyg== dependencies: detect-newline "^2.1.0" -jest-each@^23.6.0: - version "23.6.0" - resolved "https://registry.yarnpkg.com/jest-each/-/jest-each-23.6.0.tgz#ba0c3a82a8054387016139c733a05242d3d71575" +jest-each@^24.8.0: + version "24.8.0" + resolved "https://registry.yarnpkg.com/jest-each/-/jest-each-24.8.0.tgz#a05fd2bf94ddc0b1da66c6d13ec2457f35e52775" + integrity sha512-NrwK9gaL5+XgrgoCsd9svsoWdVkK4gnvyhcpzd6m487tXHqIdYeykgq3MKI1u4I+5Zf0tofr70at9dWJDeb+BA== dependencies: + "@jest/types" "^24.8.0" chalk "^2.0.1" - pretty-format "^23.6.0" - -jest-environment-jsdom@^23.4.0: - version "23.4.0" - resolved "https://registry.yarnpkg.com/jest-environment-jsdom/-/jest-environment-jsdom-23.4.0.tgz#056a7952b3fea513ac62a140a2c368c79d9e6023" - dependencies: - jest-mock "^23.2.0" - jest-util "^23.4.0" + jest-get-type "^24.8.0" + jest-util "^24.8.0" + pretty-format "^24.8.0" + +jest-environment-jsdom@^24.8.0: + version "24.8.0" + resolved "https://registry.yarnpkg.com/jest-environment-jsdom/-/jest-environment-jsdom-24.8.0.tgz#300f6949a146cabe1c9357ad9e9ecf9f43f38857" + integrity sha512-qbvgLmR7PpwjoFjM/sbuqHJt/NCkviuq9vus9NBn/76hhSidO+Z6Bn9tU8friecegbJL8gzZQEMZBQlFWDCwAQ== + dependencies: + "@jest/environment" "^24.8.0" + "@jest/fake-timers" "^24.8.0" + "@jest/types" "^24.8.0" + jest-mock "^24.8.0" + jest-util "^24.8.0" jsdom "^11.5.1" -jest-environment-node@^23.4.0: - version "23.4.0" - resolved "https://registry.yarnpkg.com/jest-environment-node/-/jest-environment-node-23.4.0.tgz#57e80ed0841dea303167cce8cd79521debafde10" +jest-environment-node@^24.8.0: + version "24.8.0" + resolved "https://registry.yarnpkg.com/jest-environment-node/-/jest-environment-node-24.8.0.tgz#d3f726ba8bc53087a60e7a84ca08883a4c892231" + integrity sha512-vIGUEScd1cdDgR6sqn2M08sJTRLQp6Dk/eIkCeO4PFHxZMOgy+uYLPMC4ix3PEfM5Au/x3uQ/5Tl0DpXXZsJ/Q== dependencies: - jest-mock "^23.2.0" - jest-util "^23.4.0" + "@jest/environment" "^24.8.0" + "@jest/fake-timers" "^24.8.0" + "@jest/types" "^24.8.0" + jest-mock "^24.8.0" + jest-util "^24.8.0" -jest-get-type@^22.1.0: - version "22.4.3" - resolved "https://registry.yarnpkg.com/jest-get-type/-/jest-get-type-22.4.3.tgz#e3a8504d8479342dd4420236b322869f18900ce4" +jest-get-type@^24.8.0: + version "24.8.0" + resolved "https://registry.yarnpkg.com/jest-get-type/-/jest-get-type-24.8.0.tgz#a7440de30b651f5a70ea3ed7ff073a32dfe646fc" + integrity sha512-RR4fo8jEmMD9zSz2nLbs2j0zvPpk/KCEz3a62jJWbd2ayNo0cb+KFRxPHVhE4ZmgGJEQp0fosmNz84IfqM8cMQ== -jest-haste-map@^23.6.0: - version "23.6.0" - resolved "https://registry.yarnpkg.com/jest-haste-map/-/jest-haste-map-23.6.0.tgz#2e3eb997814ca696d62afdb3f2529f5bbc935e16" +jest-haste-map@^24.8.0: + version "24.8.0" + resolved "https://registry.yarnpkg.com/jest-haste-map/-/jest-haste-map-24.8.0.tgz#51794182d877b3ddfd6e6d23920e3fe72f305800" + integrity sha512-ZBPRGHdPt1rHajWelXdqygIDpJx8u3xOoLyUBWRW28r3tagrgoepPrzAozW7kW9HrQfhvmiv1tncsxqHJO1onQ== dependencies: + "@jest/types" "^24.8.0" + anymatch "^2.0.0" fb-watchman "^2.0.0" - graceful-fs "^4.1.11" + graceful-fs "^4.1.15" invariant "^2.2.4" - jest-docblock "^23.2.0" - jest-serializer "^23.0.1" - jest-worker "^23.2.0" - micromatch "^2.3.11" - sane "^2.0.0" + jest-serializer "^24.4.0" + jest-util "^24.8.0" + jest-worker "^24.6.0" + micromatch "^3.1.10" + sane "^4.0.3" + walker "^1.0.7" + optionalDependencies: + fsevents "^1.2.7" -jest-jasmine2@^23.6.0: - version "23.6.0" - resolved "https://registry.yarnpkg.com/jest-jasmine2/-/jest-jasmine2-23.6.0.tgz#840e937f848a6c8638df24360ab869cc718592e0" +jest-jasmine2@^24.8.0: + version "24.8.0" + resolved "https://registry.yarnpkg.com/jest-jasmine2/-/jest-jasmine2-24.8.0.tgz#a9c7e14c83dd77d8b15e820549ce8987cc8cd898" + integrity sha512-cEky88npEE5LKd5jPpTdDCLvKkdyklnaRycBXL6GNmpxe41F0WN44+i7lpQKa/hcbXaQ+rc9RMaM4dsebrYong== dependencies: - babel-traverse "^6.0.0" + "@babel/traverse" "^7.1.0" + "@jest/environment" "^24.8.0" + "@jest/test-result" "^24.8.0" + "@jest/types" "^24.8.0" chalk "^2.0.1" co "^4.6.0" - expect "^23.6.0" - is-generator-fn "^1.0.0" - jest-diff "^23.6.0" - jest-each "^23.6.0" - jest-matcher-utils "^23.6.0" - jest-message-util "^23.4.0" - jest-snapshot "^23.6.0" - jest-util "^23.4.0" - pretty-format "^23.6.0" - -jest-leak-detector@^23.6.0: - version "23.6.0" - resolved "https://registry.yarnpkg.com/jest-leak-detector/-/jest-leak-detector-23.6.0.tgz#e4230fd42cf381a1a1971237ad56897de7e171de" - dependencies: - pretty-format "^23.6.0" - -jest-matcher-utils@^23.6.0: - version "23.6.0" - resolved "https://registry.yarnpkg.com/jest-matcher-utils/-/jest-matcher-utils-23.6.0.tgz#726bcea0c5294261a7417afb6da3186b4b8cac80" + expect "^24.8.0" + is-generator-fn "^2.0.0" + jest-each "^24.8.0" + jest-matcher-utils "^24.8.0" + jest-message-util "^24.8.0" + jest-runtime "^24.8.0" + jest-snapshot "^24.8.0" + jest-util "^24.8.0" + pretty-format "^24.8.0" + throat "^4.0.0" + +jest-leak-detector@^24.8.0: + version "24.8.0" + resolved "https://registry.yarnpkg.com/jest-leak-detector/-/jest-leak-detector-24.8.0.tgz#c0086384e1f650c2d8348095df769f29b48e6980" + integrity sha512-cG0yRSK8A831LN8lIHxI3AblB40uhv0z+SsQdW3GoMMVcK+sJwrIIyax5tu3eHHNJ8Fu6IMDpnLda2jhn2pD/g== dependencies: - chalk "^2.0.1" - jest-get-type "^22.1.0" - pretty-format "^23.6.0" + pretty-format "^24.8.0" -jest-message-util@^23.4.0: - version "23.4.0" - resolved "https://registry.yarnpkg.com/jest-message-util/-/jest-message-util-23.4.0.tgz#17610c50942349508d01a3d1e0bda2c079086a9f" +jest-matcher-utils@^24.8.0: + version "24.8.0" + resolved "https://registry.yarnpkg.com/jest-matcher-utils/-/jest-matcher-utils-24.8.0.tgz#2bce42204c9af12bde46f83dc839efe8be832495" + integrity sha512-lex1yASY51FvUuHgm0GOVj7DCYEouWSlIYmCW7APSqB9v8mXmKSn5+sWVF0MhuASG0bnYY106/49JU1FZNl5hw== dependencies: - "@babel/code-frame" "^7.0.0-beta.35" chalk "^2.0.1" - micromatch "^2.3.11" - slash "^1.0.0" + jest-diff "^24.8.0" + jest-get-type "^24.8.0" + pretty-format "^24.8.0" + +jest-message-util@^24.8.0: + version "24.8.0" + resolved "https://registry.yarnpkg.com/jest-message-util/-/jest-message-util-24.8.0.tgz#0d6891e72a4beacc0292b638685df42e28d6218b" + integrity sha512-p2k71rf/b6ns8btdB0uVdljWo9h0ovpnEe05ZKWceQGfXYr4KkzgKo3PBi8wdnd9OtNh46VpNIJynUn/3MKm1g== + dependencies: + "@babel/code-frame" "^7.0.0" + "@jest/test-result" "^24.8.0" + "@jest/types" "^24.8.0" + "@types/stack-utils" "^1.0.1" + chalk "^2.0.1" + micromatch "^3.1.10" + slash "^2.0.0" stack-utils "^1.0.1" -jest-mock@^23.2.0: - version "23.2.0" - resolved "https://registry.yarnpkg.com/jest-mock/-/jest-mock-23.2.0.tgz#ad1c60f29e8719d47c26e1138098b6d18b261134" +jest-mock@^24.8.0: + version "24.8.0" + resolved "https://registry.yarnpkg.com/jest-mock/-/jest-mock-24.8.0.tgz#2f9d14d37699e863f1febf4e4d5a33b7fdbbde56" + integrity sha512-6kWugwjGjJw+ZkK4mDa0Df3sDlUTsV47MSrT0nGQ0RBWJbpODDQ8MHDVtGtUYBne3IwZUhtB7elxHspU79WH3A== + dependencies: + "@jest/types" "^24.8.0" + +jest-pnp-resolver@^1.2.1: + version "1.2.1" + resolved "https://registry.yarnpkg.com/jest-pnp-resolver/-/jest-pnp-resolver-1.2.1.tgz#ecdae604c077a7fbc70defb6d517c3c1c898923a" + integrity sha512-pgFw2tm54fzgYvc/OHrnysABEObZCUNFnhjoRjaVOCN8NYc032/gVjPaHD4Aq6ApkSieWtfKAFQtmDKAmhupnQ== -jest-regex-util@^23.3.0: - version "23.3.0" - resolved "https://registry.yarnpkg.com/jest-regex-util/-/jest-regex-util-23.3.0.tgz#5f86729547c2785c4002ceaa8f849fe8ca471bc5" +jest-regex-util@^24.3.0: + version "24.3.0" + resolved "https://registry.yarnpkg.com/jest-regex-util/-/jest-regex-util-24.3.0.tgz#d5a65f60be1ae3e310d5214a0307581995227b36" + integrity sha512-tXQR1NEOyGlfylyEjg1ImtScwMq8Oh3iJbGTjN7p0J23EuVX1MA8rwU69K4sLbCmwzgCUbVkm0FkSF9TdzOhtg== -jest-resolve-dependencies@^23.6.0: - version "23.6.0" - resolved "https://registry.yarnpkg.com/jest-resolve-dependencies/-/jest-resolve-dependencies-23.6.0.tgz#b4526af24c8540d9a3fab102c15081cf509b723d" +jest-resolve-dependencies@^24.8.0: + version "24.8.0" + resolved "https://registry.yarnpkg.com/jest-resolve-dependencies/-/jest-resolve-dependencies-24.8.0.tgz#19eec3241f2045d3f990dba331d0d7526acff8e0" + integrity sha512-hyK1qfIf/krV+fSNyhyJeq3elVMhK9Eijlwy+j5jqmZ9QsxwKBiP6qukQxaHtK8k6zql/KYWwCTQ+fDGTIJauw== dependencies: - jest-regex-util "^23.3.0" - jest-snapshot "^23.6.0" + "@jest/types" "^24.8.0" + jest-regex-util "^24.3.0" + jest-snapshot "^24.8.0" -jest-resolve@^23.6.0: - version "23.6.0" - resolved "https://registry.yarnpkg.com/jest-resolve/-/jest-resolve-23.6.0.tgz#cf1d1a24ce7ee7b23d661c33ba2150f3aebfa0ae" +jest-resolve@^24.8.0: + version "24.8.0" + resolved "https://registry.yarnpkg.com/jest-resolve/-/jest-resolve-24.8.0.tgz#84b8e5408c1f6a11539793e2b5feb1b6e722439f" + integrity sha512-+hjSzi1PoRvnuOICoYd5V/KpIQmkAsfjFO71458hQ2Whi/yf1GDeBOFj8Gxw4LrApHsVJvn5fmjcPdmoUHaVKw== dependencies: + "@jest/types" "^24.8.0" browser-resolve "^1.11.3" chalk "^2.0.1" - realpath-native "^1.0.0" - -jest-runner@^23.6.0: - version "23.6.0" - resolved "https://registry.yarnpkg.com/jest-runner/-/jest-runner-23.6.0.tgz#3894bd219ffc3f3cb94dc48a4170a2e6f23a5a38" - dependencies: + jest-pnp-resolver "^1.2.1" + realpath-native "^1.1.0" + +jest-runner@^24.8.0: + version "24.8.0" + resolved "https://registry.yarnpkg.com/jest-runner/-/jest-runner-24.8.0.tgz#4f9ae07b767db27b740d7deffad0cf67ccb4c5bb" + integrity sha512-utFqC5BaA3JmznbissSs95X1ZF+d+4WuOWwpM9+Ak356YtMhHE/GXUondZdcyAAOTBEsRGAgH/0TwLzfI9h7ow== + dependencies: + "@jest/console" "^24.7.1" + "@jest/environment" "^24.8.0" + "@jest/test-result" "^24.8.0" + "@jest/types" "^24.8.0" + chalk "^2.4.2" exit "^0.1.2" - graceful-fs "^4.1.11" - jest-config "^23.6.0" - jest-docblock "^23.2.0" - jest-haste-map "^23.6.0" - jest-jasmine2 "^23.6.0" - jest-leak-detector "^23.6.0" - jest-message-util "^23.4.0" - jest-runtime "^23.6.0" - jest-util "^23.4.0" - jest-worker "^23.2.0" + graceful-fs "^4.1.15" + jest-config "^24.8.0" + jest-docblock "^24.3.0" + jest-haste-map "^24.8.0" + jest-jasmine2 "^24.8.0" + jest-leak-detector "^24.8.0" + jest-message-util "^24.8.0" + jest-resolve "^24.8.0" + jest-runtime "^24.8.0" + jest-util "^24.8.0" + jest-worker "^24.6.0" source-map-support "^0.5.6" throat "^4.0.0" -jest-runtime@^23.6.0: - version "23.6.0" - resolved "https://registry.yarnpkg.com/jest-runtime/-/jest-runtime-23.6.0.tgz#059e58c8ab445917cd0e0d84ac2ba68de8f23082" - dependencies: - babel-core "^6.0.0" - babel-plugin-istanbul "^4.1.6" +jest-runtime@^24.8.0: + version "24.8.0" + resolved "https://registry.yarnpkg.com/jest-runtime/-/jest-runtime-24.8.0.tgz#05f94d5b05c21f6dc54e427cd2e4980923350620" + integrity sha512-Mq0aIXhvO/3bX44ccT+czU1/57IgOMyy80oM0XR/nyD5zgBcesF84BPabZi39pJVA6UXw+fY2Q1N+4BiVUBWOA== + dependencies: + "@jest/console" "^24.7.1" + "@jest/environment" "^24.8.0" + "@jest/source-map" "^24.3.0" + "@jest/transform" "^24.8.0" + "@jest/types" "^24.8.0" + "@types/yargs" "^12.0.2" chalk "^2.0.1" - convert-source-map "^1.4.0" exit "^0.1.2" - fast-json-stable-stringify "^2.0.0" - graceful-fs "^4.1.11" - jest-config "^23.6.0" - jest-haste-map "^23.6.0" - jest-message-util "^23.4.0" - jest-regex-util "^23.3.0" - jest-resolve "^23.6.0" - jest-snapshot "^23.6.0" - jest-util "^23.4.0" - jest-validate "^23.6.0" - micromatch "^2.3.11" - realpath-native "^1.0.0" - slash "^1.0.0" - strip-bom "3.0.0" - write-file-atomic "^2.1.0" - yargs "^11.0.0" - -jest-serializer@^23.0.1: - version "23.0.1" - resolved "https://registry.yarnpkg.com/jest-serializer/-/jest-serializer-23.0.1.tgz#a3776aeb311e90fe83fab9e533e85102bd164165" - -jest-snapshot@^23.6.0: - version "23.6.0" - resolved "https://registry.yarnpkg.com/jest-snapshot/-/jest-snapshot-23.6.0.tgz#f9c2625d1b18acda01ec2d2b826c0ce58a5aa17a" - dependencies: - babel-types "^6.0.0" + glob "^7.1.3" + graceful-fs "^4.1.15" + jest-config "^24.8.0" + jest-haste-map "^24.8.0" + jest-message-util "^24.8.0" + jest-mock "^24.8.0" + jest-regex-util "^24.3.0" + jest-resolve "^24.8.0" + jest-snapshot "^24.8.0" + jest-util "^24.8.0" + jest-validate "^24.8.0" + realpath-native "^1.1.0" + slash "^2.0.0" + strip-bom "^3.0.0" + yargs "^12.0.2" + +jest-serializer@^24.4.0: + version "24.4.0" + resolved "https://registry.yarnpkg.com/jest-serializer/-/jest-serializer-24.4.0.tgz#f70c5918c8ea9235ccb1276d232e459080588db3" + integrity sha512-k//0DtglVstc1fv+GY/VHDIjrtNjdYvYjMlbLUed4kxrE92sIUewOi5Hj3vrpB8CXfkJntRPDRjCrCvUhBdL8Q== + +jest-snapshot@^24.8.0: + version "24.8.0" + resolved "https://registry.yarnpkg.com/jest-snapshot/-/jest-snapshot-24.8.0.tgz#3bec6a59da2ff7bc7d097a853fb67f9d415cb7c6" + integrity sha512-5ehtWoc8oU9/cAPe6fez6QofVJLBKyqkY2+TlKTOf0VllBB/mqUNdARdcjlZrs9F1Cv+/HKoCS/BknT0+tmfPg== + dependencies: + "@babel/types" "^7.0.0" + "@jest/types" "^24.8.0" chalk "^2.0.1" - jest-diff "^23.6.0" - jest-matcher-utils "^23.6.0" - jest-message-util "^23.4.0" - jest-resolve "^23.6.0" + expect "^24.8.0" + jest-diff "^24.8.0" + jest-matcher-utils "^24.8.0" + jest-message-util "^24.8.0" + jest-resolve "^24.8.0" mkdirp "^0.5.1" natural-compare "^1.4.0" - pretty-format "^23.6.0" + pretty-format "^24.8.0" semver "^5.5.0" -jest-util@^23.4.0: - version "23.4.0" - resolved "https://registry.yarnpkg.com/jest-util/-/jest-util-23.4.0.tgz#4d063cb927baf0a23831ff61bec2cbbf49793561" - dependencies: - callsites "^2.0.0" +jest-util@^24.8.0: + version "24.8.0" + resolved "https://registry.yarnpkg.com/jest-util/-/jest-util-24.8.0.tgz#41f0e945da11df44cc76d64ffb915d0716f46cd1" + integrity sha512-DYZeE+XyAnbNt0BG1OQqKy/4GVLPtzwGx5tsnDrFcax36rVE3lTA5fbvgmbVPUZf9w77AJ8otqR4VBbfFJkUZA== + dependencies: + "@jest/console" "^24.7.1" + "@jest/fake-timers" "^24.8.0" + "@jest/source-map" "^24.3.0" + "@jest/test-result" "^24.8.0" + "@jest/types" "^24.8.0" + callsites "^3.0.0" chalk "^2.0.1" - graceful-fs "^4.1.11" - is-ci "^1.0.10" - jest-message-util "^23.4.0" + graceful-fs "^4.1.15" + is-ci "^2.0.0" mkdirp "^0.5.1" - slash "^1.0.0" + slash "^2.0.0" source-map "^0.6.0" -jest-validate@^23.6.0: - version "23.6.0" - resolved "https://registry.yarnpkg.com/jest-validate/-/jest-validate-23.6.0.tgz#36761f99d1ed33fcd425b4e4c5595d62b6597474" +jest-validate@^24.8.0: + version "24.8.0" + resolved "https://registry.yarnpkg.com/jest-validate/-/jest-validate-24.8.0.tgz#624c41533e6dfe356ffadc6e2423a35c2d3b4849" + integrity sha512-+/N7VOEMW1Vzsrk3UWBDYTExTPwf68tavEPKDnJzrC6UlHtUDU/fuEdXqFoHzv9XnQ+zW6X3qMZhJ3YexfeLDA== dependencies: + "@jest/types" "^24.8.0" + camelcase "^5.0.0" chalk "^2.0.1" - jest-get-type "^22.1.0" + jest-get-type "^24.8.0" leven "^2.1.0" - pretty-format "^23.6.0" + pretty-format "^24.8.0" -jest-watcher@^23.4.0: - version "23.4.0" - resolved "https://registry.yarnpkg.com/jest-watcher/-/jest-watcher-23.4.0.tgz#d2e28ce74f8dad6c6afc922b92cabef6ed05c91c" +jest-watcher@^24.8.0: + version "24.8.0" + resolved "https://registry.yarnpkg.com/jest-watcher/-/jest-watcher-24.8.0.tgz#58d49915ceddd2de85e238f6213cef1c93715de4" + integrity sha512-SBjwHt5NedQoVu54M5GEx7cl7IGEFFznvd/HNT8ier7cCAx/Qgu9ZMlaTQkvK22G1YOpcWBLQPFSImmxdn3DAw== dependencies: + "@jest/test-result" "^24.8.0" + "@jest/types" "^24.8.0" + "@types/yargs" "^12.0.9" ansi-escapes "^3.0.0" chalk "^2.0.1" + jest-util "^24.8.0" string-length "^2.0.0" -jest-worker@^23.2.0: - version "23.2.0" - resolved "https://registry.yarnpkg.com/jest-worker/-/jest-worker-23.2.0.tgz#faf706a8da36fae60eb26957257fa7b5d8ea02b9" +jest-worker@^24.6.0: + version "24.6.0" + resolved "https://registry.yarnpkg.com/jest-worker/-/jest-worker-24.6.0.tgz#7f81ceae34b7cde0c9827a6980c35b7cdc0161b3" + integrity sha512-jDwgW5W9qGNvpI1tNnvajh0a5IE/PuGLFmHk6aR/BZFz8tSgGw17GsDPXAJ6p91IvYDjOw8GpFbvvZGAK+DPQQ== dependencies: merge-stream "^1.0.1" + supports-color "^6.1.0" -jest@23: - version "23.6.0" - resolved "https://registry.yarnpkg.com/jest/-/jest-23.6.0.tgz#ad5835e923ebf6e19e7a1d7529a432edfee7813d" +jest@^24.8.0: + version "24.8.0" + resolved "https://registry.yarnpkg.com/jest/-/jest-24.8.0.tgz#d5dff1984d0d1002196e9b7f12f75af1b2809081" + integrity sha512-o0HM90RKFRNWmAWvlyV8i5jGZ97pFwkeVoGvPW1EtLTgJc2+jcuqcbbqcSZLE/3f2S5pt0y2ZBETuhpWNl1Reg== dependencies: - import-local "^1.0.0" - jest-cli "^23.6.0" + import-local "^2.0.0" + jest-cli "^24.8.0" "js-tokens@^3.0.0 || ^4.0.0", js-tokens@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499" - -js-tokens@^3.0.2: - version "3.0.2" - resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.2.tgz#9866df395102130e38f7f996bceb65443209c25b" - -js-yaml@^3.7.0: - version "3.12.1" - resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.12.1.tgz#295c8632a18a23e054cf5c9d3cecafe678167600" - dependencies: - argparse "^1.0.7" - esprima "^4.0.0" + integrity sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ== jsbn@~0.1.0: version "0.1.1" resolved "https://registry.yarnpkg.com/jsbn/-/jsbn-0.1.1.tgz#a5e654c2e5a2deb5f201d96cefbca80c0ef2f513" + integrity sha1-peZUwuWi3rXyAdls77yoDA7y9RM= jsdom@^11.5.1: version "11.12.0" resolved "https://registry.yarnpkg.com/jsdom/-/jsdom-11.12.0.tgz#1a80d40ddd378a1de59656e9e6dc5a3ba8657bc8" + integrity sha512-y8Px43oyiBM13Zc1z780FrfNLJCXTL40EWlty/LXUtcjykRBNgLlCjWXpfSPBl2iv+N7koQN+dvqszHZgT/Fjw== dependencies: abab "^2.0.0" acorn "^5.5.3" @@ -2462,36 +2676,36 @@ jsdom@^11.5.1: ws "^5.2.0" xml-name-validator "^3.0.0" -jsesc@^1.3.0: - version "1.3.0" - resolved "https://registry.yarnpkg.com/jsesc/-/jsesc-1.3.0.tgz#46c3fec8c1892b12b0833db9bc7622176dbab34b" +jsesc@^2.5.1: + version "2.5.2" + resolved "https://registry.yarnpkg.com/jsesc/-/jsesc-2.5.2.tgz#80564d2e483dacf6e8ef209650a67df3f0c283a4" + integrity sha512-OYu7XEzjkCQ3C5Ps3QIZsQfNpqoJyZZA99wd9aWd05NCtC5pWOkShK2mkL6HXQR6/Cy2lbNdPlZBpuQHXE63gA== -json-parse-better-errors@^1.0.2: +json-parse-better-errors@^1.0.1, json-parse-better-errors@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/json-parse-better-errors/-/json-parse-better-errors-1.0.2.tgz#bb867cfb3450e69107c131d1c514bab3dc8bcaa9" json-schema-traverse@^0.4.1: version "0.4.1" resolved "https://registry.yarnpkg.com/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz#69f6a87d9513ab8bb8fe63bdb0979c448e684660" + integrity sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg== json-schema@0.2.3: version "0.2.3" resolved "https://registry.yarnpkg.com/json-schema/-/json-schema-0.2.3.tgz#b480c892e59a2f05954ce727bd3f2a4e882f9e13" + integrity sha1-tIDIkuWaLwWVTOcnvT8qTogvnhM= json-stringify-safe@~5.0.1: version "5.0.1" resolved "https://registry.yarnpkg.com/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz#1296a2d58fd45f19a0f6ce01d65701e2c735b6eb" + integrity sha1-Epai1Y/UXxmg9s4B1lcB4sc1tus= -json5@2.x: +json5@2.x, json5@^2.1.0: version "2.1.0" resolved "https://registry.yarnpkg.com/json5/-/json5-2.1.0.tgz#e7a0c62c48285c628d20a10b85c89bb807c32850" dependencies: minimist "^1.2.0" -json5@^0.5.1: - version "0.5.1" - resolved "https://registry.yarnpkg.com/json5/-/json5-0.5.1.tgz#1eade7acc012034ad84e2396767ead9fa5495821" - json5@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/json5/-/json5-1.0.1.tgz#779fb0018604fa854eacbf6252180d83543e3dbe" @@ -2501,6 +2715,7 @@ json5@^1.0.1: jsprim@^1.2.2: version "1.4.1" resolved "https://registry.yarnpkg.com/jsprim/-/jsprim-1.4.1.tgz#313e66bc1e5cc06e438bc1b7499c2e5c56acb6a2" + integrity sha1-MT5mvB5cwG5Di8G3SZwuXFastqI= dependencies: assert-plus "1.0.0" extsprintf "1.3.0" @@ -2510,32 +2725,31 @@ jsprim@^1.2.2: kind-of@^3.0.2, kind-of@^3.0.3, kind-of@^3.2.0: version "3.2.2" resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-3.2.2.tgz#31ea21a734bab9bbb0f32466d893aea51e4a3c64" + integrity sha1-MeohpzS6ubuw8yRm2JOupR5KPGQ= dependencies: is-buffer "^1.1.5" kind-of@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-4.0.0.tgz#20813df3d712928b207378691a45066fae72dd57" + integrity sha1-IIE989cSkosgc3hpGkUGb65y3Vc= dependencies: is-buffer "^1.1.5" kind-of@^5.0.0: version "5.1.0" resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-5.1.0.tgz#729c91e2d857b7a419a1f9aa65685c4c33f5845d" + integrity sha512-NGEErnH6F2vUuXDh+OlbcKW7/wOcfdRHaZ7VWtqCztfHri/++YKmP51OdWeGPuqCOba6kk2OTe5d02VmTB80Pw== kind-of@^6.0.0, kind-of@^6.0.2: version "6.0.2" resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-6.0.2.tgz#01146b36a6218e64e58f3a8d66de5d7fc6f6d051" + integrity sha512-s5kLOcnH0XqDO+FvuaLX8DDjZ18CGFk7VygH40QoKPUQhW4e2rvM0rwUq0t8IQDOwYSeLK01U90OjzBTme2QqA== -kleur@^2.0.1: - version "2.0.2" - resolved "https://registry.yarnpkg.com/kleur/-/kleur-2.0.2.tgz#b704f4944d95e255d038f0cb05fb8a602c55a300" - -lcid@^1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/lcid/-/lcid-1.0.0.tgz#308accafa0bc483a3867b4b6f2b9506251d1b835" - dependencies: - invert-kv "^1.0.0" +kleur@^3.0.2: + version "3.0.3" + resolved "https://registry.yarnpkg.com/kleur/-/kleur-3.0.3.tgz#a79c9ecc86ee1ce3fa6206d1216c501f147fc07e" + integrity sha512-eTIzlVOSUR+JxdDFepEYcBMtZ9Qqdef+rnzWdRZuMbOywu5tO2w2N7rqjoANZ5k9vywhL6Br1VRjUIgTQx4E8w== lcid@^2.0.0: version "2.0.0" @@ -2546,27 +2760,30 @@ lcid@^2.0.0: left-pad@^1.3.0: version "1.3.0" resolved "https://registry.yarnpkg.com/left-pad/-/left-pad-1.3.0.tgz#5b8a3a7765dfe001261dde915589e782f8c94d1e" + integrity sha512-XI5MPzVNApjAyhQzphX8BkmKsKUxD4LdyK24iZeQGinBN9yTQT3bFlCBy/aVx2HrNcqQGsdot8ghrjyrvMCoEA== leven@^2.1.0: version "2.1.0" resolved "https://registry.yarnpkg.com/leven/-/leven-2.1.0.tgz#c2e7a9f772094dee9d34202ae8acce4687875580" + integrity sha1-wuep93IJTe6dNCAq6KzORoeHVYA= levn@~0.3.0: version "0.3.0" resolved "https://registry.yarnpkg.com/levn/-/levn-0.3.0.tgz#3b09924edf9f083c0490fdd4c0bc4421e04764ee" + integrity sha1-OwmSTt+fCDwEkP3UwLxEIeBHZO4= dependencies: prelude-ls "~1.1.2" type-check "~0.3.2" -load-json-file@^1.0.0: - version "1.1.0" - resolved "https://registry.yarnpkg.com/load-json-file/-/load-json-file-1.1.0.tgz#956905708d58b4bab4c2261b04f59f31c99374c0" +load-json-file@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/load-json-file/-/load-json-file-4.0.0.tgz#2f5f45ab91e33216234fd53adab668eb4ec0993b" + integrity sha1-L19Fq5HjMhYjT9U62rZo607AmTs= dependencies: graceful-fs "^4.1.2" - parse-json "^2.2.0" - pify "^2.0.0" - pinkie-promise "^2.0.0" - strip-bom "^2.0.0" + parse-json "^4.0.0" + pify "^3.0.0" + strip-bom "^3.0.0" loader-runner@^2.3.0: version "2.4.0" @@ -2580,13 +2797,6 @@ loader-utils@^1.0.2, loader-utils@^1.1.0: emojis-list "^2.0.0" json5 "^1.0.1" -locate-path@^2.0.0: - version "2.0.0" - resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-2.0.0.tgz#2b568b265eec944c6d9c0de9c3dbbbca0354cd8e" - dependencies: - p-locate "^2.0.0" - path-exists "^3.0.0" - locate-path@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-3.0.0.tgz#dbec3b3ab759758071b58fe59fc41871af21400e" @@ -2597,24 +2807,20 @@ locate-path@^3.0.0: lodash.sortby@^4.7.0: version "4.7.0" resolved "https://registry.yarnpkg.com/lodash.sortby/-/lodash.sortby-4.7.0.tgz#edd14c824e2cc9c1e0b0a1b42bb5210516a42438" + integrity sha1-7dFMgk4sycHgsKG0K7UhBRakJDg= -lodash@^4.13.1, lodash@^4.17.11, lodash@^4.17.4: +lodash@^4.17.11: version "4.17.11" resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.11.tgz#b39ea6229ef607ecd89e2c8df12536891cac9b8d" + integrity sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg== loose-envify@^1.0.0: version "1.4.0" resolved "https://registry.yarnpkg.com/loose-envify/-/loose-envify-1.4.0.tgz#71ee51fa7be4caec1a63839f7e682d8132d30caf" + integrity sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q== dependencies: js-tokens "^3.0.0 || ^4.0.0" -lru-cache@^4.0.1: - version "4.1.5" - resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-4.1.5.tgz#8bbe50ea85bed59bc9e33dcab8235ee9bcf443cd" - dependencies: - pseudomap "^1.0.2" - yallist "^2.1.2" - lru-cache@^5.1.1: version "5.1.1" resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-5.1.1.tgz#1da27e6710271947695daf6848e847f01d84b920" @@ -2627,6 +2833,14 @@ make-dir@^1.0.0: dependencies: pify "^3.0.0" +make-dir@^2.1.0: + version "2.1.0" + resolved "https://registry.yarnpkg.com/make-dir/-/make-dir-2.1.0.tgz#5f0310e18b8be898cc07009295a30ae41e91e6f5" + integrity sha512-LS9X+dc8KLxXCb8dni79fLIIUA5VyZoyjSMCwTluaXA0o27cCK0bhXkpgw+sTXVpPy/lSO57ilRixqk0vDmtRA== + dependencies: + pify "^4.0.1" + semver "^5.6.0" + make-error@1.x, make-error@^1.1.1: version "1.3.5" resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.5.tgz#efe4e81f6db28cadd605c70f29c831b58ef776c8" @@ -2634,6 +2848,7 @@ make-error@1.x, make-error@^1.1.1: makeerror@1.0.x: version "1.0.11" resolved "https://registry.yarnpkg.com/makeerror/-/makeerror-1.0.11.tgz#e01a5c9109f2af79660e4e8b9587790184f5a96c" + integrity sha1-4BpckQnyr3lmDk6LlYd5AYT1qWw= dependencies: tmpl "1.0.x" @@ -2650,17 +2865,15 @@ map-age-cleaner@^0.1.1: map-cache@^0.2.2: version "0.2.2" resolved "https://registry.yarnpkg.com/map-cache/-/map-cache-0.2.2.tgz#c32abd0bd6525d9b051645bb4f26ac5dc98a0dbf" + integrity sha1-wyq9C9ZSXZsFFkW7TyasXcmKDb8= map-visit@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/map-visit/-/map-visit-1.0.0.tgz#ecdca8f13144e660f1b5bd41f12f3479d98dfb8f" + integrity sha1-7Nyo8TFE5mDxtb1B8S80edmN+48= dependencies: object-visit "^1.0.0" -math-random@^1.0.1: - version "1.0.4" - resolved "https://registry.yarnpkg.com/math-random/-/math-random-1.0.4.tgz#5dd6943c938548267016d4e34f057583080c514c" - md5.js@^1.3.4: version "1.3.5" resolved "https://registry.yarnpkg.com/md5.js/-/md5.js-1.3.5.tgz#b5d07b8e3216e3e27cd728d72f70d1e6a342005f" @@ -2669,12 +2882,6 @@ md5.js@^1.3.4: inherits "^2.0.1" safe-buffer "^5.1.2" -mem@^1.1.0: - version "1.1.0" - resolved "https://registry.yarnpkg.com/mem/-/mem-1.1.0.tgz#5edd52b485ca1d900fe64895505399a0dfa45f76" - dependencies: - mimic-fn "^1.0.0" - mem@^4.0.0: version "4.1.0" resolved "https://registry.yarnpkg.com/mem/-/mem-4.1.0.tgz#aeb9be2d21f47e78af29e4ac5978e8afa2ca5b8a" @@ -2693,34 +2900,14 @@ memory-fs@^0.4.0, memory-fs@~0.4.1: merge-stream@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/merge-stream/-/merge-stream-1.0.1.tgz#4041202d508a342ba00174008df0c251b8c135e1" + integrity sha1-QEEgLVCKNCugAXQAjfDCUbjBNeE= dependencies: readable-stream "^2.0.1" -merge@^1.2.0: - version "1.2.1" - resolved "https://registry.yarnpkg.com/merge/-/merge-1.2.1.tgz#38bebf80c3220a8a487b6fcfb3941bb11720c145" - -micromatch@^2.3.11: - version "2.3.11" - resolved "https://registry.yarnpkg.com/micromatch/-/micromatch-2.3.11.tgz#86677c97d1720b363431d04d0d15293bd38c1565" - dependencies: - arr-diff "^2.0.0" - array-unique "^0.2.1" - braces "^1.8.2" - expand-brackets "^0.1.4" - extglob "^0.3.1" - filename-regex "^2.0.0" - is-extglob "^1.0.0" - is-glob "^2.0.1" - kind-of "^3.0.2" - normalize-path "^2.0.1" - object.omit "^2.0.0" - parse-glob "^3.0.4" - regex-cache "^0.4.2" - micromatch@^3.0.4, micromatch@^3.1.10, micromatch@^3.1.4, micromatch@^3.1.8: version "3.1.10" resolved "https://registry.yarnpkg.com/micromatch/-/micromatch-3.1.10.tgz#70859bc95c9840952f359a068a3fc49f9ecfac23" + integrity sha512-MWikgl9n9M3w+bpsY3He8L+w9eF9338xRl8IAO5viDizwSzziFEyUzo2xrrloB64ADbTf8uA8vRqqttDTOmccg== dependencies: arr-diff "^4.0.0" array-unique "^0.3.2" @@ -2743,19 +2930,22 @@ miller-rabin@^4.0.0: bn.js "^4.0.0" brorand "^1.0.1" -mime-db@~1.37.0: - version "1.37.0" - resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.37.0.tgz#0b6a0ce6fdbe9576e25f1f2d2fde8830dc0ad0d8" +mime-db@1.40.0: + version "1.40.0" + resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.40.0.tgz#a65057e998db090f732a68f6c276d387d4126c32" + integrity sha512-jYdeOMPy9vnxEqFRRo6ZvTZ8d9oPb+k18PKoYNYUe2stVEBPPwsln/qWzdbmaIvnhZ9v2P+CuecK+fpUfsV2mA== mime-types@^2.1.12, mime-types@~2.1.19: - version "2.1.21" - resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.21.tgz#28995aa1ecb770742fe6ae7e58f9181c744b3f96" + version "2.1.24" + resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.24.tgz#b6f8d0b3e951efb77dedeca194cff6d16f676f81" + integrity sha512-WaFHS3MCl5fapm3oLxU4eYDw77IQM2ACcxQ9RIxfaC3ooc6PFuBMGZZsYpvoXS5D5QTWPieo1jjLdAm3TBP3cQ== dependencies: - mime-db "~1.37.0" + mime-db "1.40.0" mimic-fn@^1.0.0: version "1.2.0" resolved "https://registry.yarnpkg.com/mimic-fn/-/mimic-fn-1.2.0.tgz#820c86a39334640e99516928bd03fca88057d022" + integrity sha512-jf84uxzwiuiIVKiOLpfYk7N46TSy8ubTonmneY9vrpHNAnp0QBt2BxWV9dO3/j+BoVAb+a5G6YDPW3M5HOdMWQ== minimalistic-assert@^1.0.0, minimalistic-assert@^1.0.1: version "1.0.1" @@ -2765,34 +2955,40 @@ minimalistic-crypto-utils@^1.0.0, minimalistic-crypto-utils@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/minimalistic-crypto-utils/-/minimalistic-crypto-utils-1.0.1.tgz#f6c00c1c0b082246e5c4d99dfb8c7c083b2b582a" -minimatch@^3.0.3, minimatch@^3.0.4: +minimatch@^3.0.4: version "3.0.4" resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083" + integrity sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA== dependencies: brace-expansion "^1.1.7" minimist@0.0.8: version "0.0.8" resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.8.tgz#857fcabfc3397d2625b8228262e86aa7a011b05d" + integrity sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0= minimist@^1.1.1, minimist@^1.2.0: version "1.2.0" resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.0.tgz#a35008b20f41383eec1fb914f4cd5df79a264284" + integrity sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ= minimist@~0.0.1: version "0.0.10" resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.10.tgz#de3f98543dbf96082be48ad1a0c7cda836301dcf" + integrity sha1-3j+YVD2/lggr5IrRoMfNqDYwHc8= -minipass@^2.2.1, minipass@^2.3.4: +minipass@^2.2.1, minipass@^2.3.5: version "2.3.5" resolved "https://registry.yarnpkg.com/minipass/-/minipass-2.3.5.tgz#cacebe492022497f656b0f0f51e2682a9ed2d848" + integrity sha512-Gi1W4k059gyRbyVUZQ4mEqLm0YIUiGYfvxhF6SIlk3ui1WVxMTGfGdQ2SInh3PDrRTVvPKgULkpJtT4RH10+VA== dependencies: safe-buffer "^5.1.2" yallist "^3.0.0" -minizlib@^1.1.1: +minizlib@^1.2.1: version "1.2.1" resolved "https://registry.yarnpkg.com/minizlib/-/minizlib-1.2.1.tgz#dd27ea6136243c7c880684e8672bb3a45fd9b614" + integrity sha512-7+4oTUOWKg7AuL3vloEWekXY2/D20cevzsrNT2kGWm+39J9hGTCBv8VI5Pm5lXZ/o3/mdR4f8rflAPhnQb8mPA== dependencies: minipass "^2.2.1" @@ -2814,6 +3010,7 @@ mississippi@^3.0.0: mixin-deep@^1.2.0: version "1.3.1" resolved "https://registry.yarnpkg.com/mixin-deep/-/mixin-deep-1.3.1.tgz#a49e7268dce1a0d9698e45326c5626df3543d0fe" + integrity sha512-8ZItLHeEgaqEvd5lYBXfm4EZSFCX29Jb9K+lAHhDKzReKBQKj3R+7NOF6tjqYi9t4oI8VUfaWITJQm86wnXGNQ== dependencies: for-in "^1.0.2" is-extendable "^1.0.1" @@ -2871,18 +3068,22 @@ move-concurrently@^1.0.1: ms@2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8" + integrity sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g= ms@^2.1.1: version "2.1.1" resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.1.tgz#30a5864eb3ebb0a66f2ebe6d727af06a09d86e0a" + integrity sha512-tgp+dl5cGk28utYktBsrFqA7HKgrhgPsg6Z/EfhWI4gl1Hwq8B/GmY/0oXZ6nF8hDVesS/FpnYaD/kOWhYQvyg== nan@^2.9.2: - version "2.12.1" - resolved "https://registry.yarnpkg.com/nan/-/nan-2.12.1.tgz#7b1aa193e9aa86057e3c7bbd0ac448e770925552" + version "2.14.0" + resolved "https://registry.yarnpkg.com/nan/-/nan-2.14.0.tgz#7818f722027b2459a86f0295d434d1fc2336c52c" + integrity sha512-INOFj37C7k3AfaNTtX8RhsTw7qRy7eLET14cROi9+5HAVbbHuIWUHEauBv5qT4Av2tWasiTY1Jw6puUNqRJXQg== nanomatch@^1.2.9: version "1.2.13" resolved "https://registry.yarnpkg.com/nanomatch/-/nanomatch-1.2.13.tgz#b87a8aa4fc0de8fe6be88895b38983ff265bd119" + integrity sha512-fpoe2T0RbHwBTBUOftAfBPaDEi06ufaUai0mE6Yn1kacc3SnTErfb/h+X94VXzI64rKFHYImXSvdwGGCmwOqCA== dependencies: arr-diff "^4.0.0" array-unique "^0.3.2" @@ -2899,12 +3100,14 @@ nanomatch@^1.2.9: natural-compare@^1.4.0: version "1.4.0" resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7" + integrity sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc= needle@^2.2.1: - version "2.2.4" - resolved "https://registry.yarnpkg.com/needle/-/needle-2.2.4.tgz#51931bff82533b1928b7d1d69e01f1b00ffd2a4e" + version "2.4.0" + resolved "https://registry.yarnpkg.com/needle/-/needle-2.4.0.tgz#6833e74975c444642590e15a750288c5f939b57c" + integrity sha512-4Hnwzr3mi5L97hMYeNl8wRW/Onhy4nUKR/lVemJ8gJedxxUyBLm9kkrDColJvoSfwi0jCNhD+xCdOtiGDQiRZg== dependencies: - debug "^2.1.2" + debug "^3.2.6" iconv-lite "^0.4.4" sax "^1.2.4" @@ -2912,6 +3115,11 @@ neo-async@^2.5.0: version "2.6.0" resolved "https://registry.yarnpkg.com/neo-async/-/neo-async-2.6.0.tgz#b9d15e4d71c6762908654b5183ed38b753340835" +neo-async@^2.6.0: + version "2.6.1" + resolved "https://registry.yarnpkg.com/neo-async/-/neo-async-2.6.1.tgz#ac27ada66167fa8849a6addd837f6b189ad2081c" + integrity sha512-iyam8fBuCUpWeKPGpaNMetEocMt364qkCsfL9JuhjXX6dRnguRVOfk2GZaDpPjcOKiiXCPINZC1GczQ7iTq3Zw== + nice-try@^1.0.4: version "1.0.5" resolved "https://registry.yarnpkg.com/nice-try/-/nice-try-1.0.5.tgz#a3378a7696ce7d223e88fc9b764bd7ef1089e366" @@ -2919,6 +3127,7 @@ nice-try@^1.0.4: node-int64@^0.4.0: version "0.4.0" resolved "https://registry.yarnpkg.com/node-int64/-/node-int64-0.4.0.tgz#87a9065cdb355d3182d8f94ce11188b825c68a3b" + integrity sha1-h6kGXNs1XTGC2PlM4RGIuCXGijs= node-libs-browser@^2.0.0: version "2.2.0" @@ -2948,9 +3157,15 @@ node-libs-browser@^2.0.0: util "^0.11.0" vm-browserify "0.0.4" +node-modules-regexp@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/node-modules-regexp/-/node-modules-regexp-1.0.0.tgz#8d9dbe28964a4ac5712e9131642107c71e90ec40" + integrity sha1-jZ2+KJZKSsVxLpExZCEHxx6Q7EA= + node-notifier@^5.2.1: version "5.4.0" resolved "https://registry.yarnpkg.com/node-notifier/-/node-notifier-5.4.0.tgz#7b455fdce9f7de0c63538297354f3db468426e6a" + integrity sha512-SUDEb+o71XR5lXSTyivXd9J7fCloE3SyP4lSgt3lU2oSANiox+SxlNRGPjDKrwU1YN3ix2KN/VGGCg0t01rttQ== dependencies: growly "^1.3.0" is-wsl "^1.1.0" @@ -2961,6 +3176,7 @@ node-notifier@^5.2.1: node-pre-gyp@^0.10.0: version "0.10.3" resolved "https://registry.yarnpkg.com/node-pre-gyp/-/node-pre-gyp-0.10.3.tgz#3070040716afdc778747b61b6887bf78880b80fc" + integrity sha512-d1xFs+C/IPS8Id0qPTZ4bUT8wWryfR/OzzAFxweG+uLN85oPzyo2Iw6bVlLQ/JOdgNonXLCoRyqDzDWq4iw72A== dependencies: detect-libc "^1.0.2" mkdirp "^0.5.1" @@ -2976,6 +3192,7 @@ node-pre-gyp@^0.10.0: nopt@^4.0.1: version "4.0.1" resolved "https://registry.yarnpkg.com/nopt/-/nopt-4.0.1.tgz#d0d4685afd5415193c8c7505602d0d17cd64474d" + integrity sha1-0NRoWv1UFRk8jHUFYC0NF81kR00= dependencies: abbrev "1" osenv "^0.1.4" @@ -2983,15 +3200,17 @@ nopt@^4.0.1: normalize-package-data@^2.3.2: version "2.5.0" resolved "https://registry.yarnpkg.com/normalize-package-data/-/normalize-package-data-2.5.0.tgz#e66db1838b200c1dfc233225d12cb36520e234a8" + integrity sha512-/5CMN3T0R4XTj4DcGaexo+roZSdSFW/0AOOTROrjxzCG1wrWXEsGbRKevjlIL+ZDE4sZlJr5ED4YW0yqmkK+eA== dependencies: hosted-git-info "^2.1.4" resolve "^1.10.0" semver "2 || 3 || 4 || 5" validate-npm-package-license "^3.0.1" -normalize-path@^2.0.1, normalize-path@^2.1.1: +normalize-path@^2.1.1: version "2.1.1" resolved "https://registry.yarnpkg.com/normalize-path/-/normalize-path-2.1.1.tgz#1ab28b556e198363a8c1a6f7e6fa20137fe6aed9" + integrity sha1-GrKLVW4Zg2Oowab35vogE3/mrtk= dependencies: remove-trailing-separator "^1.0.1" @@ -3002,10 +3221,12 @@ normalize-path@^3.0.0: npm-bundled@^1.0.1: version "1.0.6" resolved "https://registry.yarnpkg.com/npm-bundled/-/npm-bundled-1.0.6.tgz#e7ba9aadcef962bb61248f91721cd932b3fe6bdd" + integrity sha512-8/JCaftHwbd//k6y2rEWp6k1wxVfpFzB6t1p825+cUb7Ym2XQfhwIC5KwhrvzZRJu+LtDE585zVaS32+CGtf0g== npm-packlist@^1.1.6: - version "1.3.0" - resolved "https://registry.yarnpkg.com/npm-packlist/-/npm-packlist-1.3.0.tgz#7f01e8e44408341379ca98cfd756e7b29bd2626c" + version "1.4.1" + resolved "https://registry.yarnpkg.com/npm-packlist/-/npm-packlist-1.4.1.tgz#19064cdf988da80ea3cee45533879d90192bbfbc" + integrity sha512-+TcdO7HJJ8peiiYhvPxsEDhF3PJFGUGRcFsGve3vxvxdcpO2Z4Z7rkosRM0kWj6LfbK/P0gu3dzk5RU1ffvFcw== dependencies: ignore-walk "^3.0.1" npm-bundled "^1.0.1" @@ -3013,12 +3234,14 @@ npm-packlist@^1.1.6: npm-run-path@^2.0.0: version "2.0.2" resolved "https://registry.yarnpkg.com/npm-run-path/-/npm-run-path-2.0.2.tgz#35a9232dfa35d7067b4cb2ddf2357b1871536c5f" + integrity sha1-NakjLfo11wZ7TLLd8jV7GHFTbF8= dependencies: path-key "^2.0.0" npmlog@^4.0.2: version "4.1.2" resolved "https://registry.yarnpkg.com/npmlog/-/npmlog-4.1.2.tgz#08a7f2a8bf734604779a9efa4ad5cc717abb954b" + integrity sha512-2uUqazuKlTaSI/dC8AzicUck7+IrEaOnN/e0jd3Xtt1KcGpwx30v50mL7oPyr/h9bL3E4aZccVwpwP+5W9Vjkg== dependencies: are-we-there-yet "~1.1.2" console-control-strings "~1.1.0" @@ -3028,54 +3251,56 @@ npmlog@^4.0.2: number-is-nan@^1.0.0: version "1.0.1" resolved "https://registry.yarnpkg.com/number-is-nan/-/number-is-nan-1.0.1.tgz#097b602b53422a522c1afb8790318336941a011d" + integrity sha1-CXtgK1NCKlIsGvuHkDGDNpQaAR0= nwsapi@^2.0.7: - version "2.1.0" - resolved "https://registry.yarnpkg.com/nwsapi/-/nwsapi-2.1.0.tgz#781065940aed90d9bb01ca5d0ce0fcf81c32712f" + version "2.1.4" + resolved "https://registry.yarnpkg.com/nwsapi/-/nwsapi-2.1.4.tgz#e006a878db23636f8e8a67d33ca0e4edf61a842f" + integrity sha512-iGfd9Y6SFdTNldEy2L0GUhcarIutFmk+MPWIn9dmj8NMIup03G08uUF2KGbbmv/Ux4RT0VZJoP/sVbWA6d/VIw== oauth-sign@~0.9.0: version "0.9.0" resolved "https://registry.yarnpkg.com/oauth-sign/-/oauth-sign-0.9.0.tgz#47a7b016baa68b5fa0ecf3dee08a85c679ac6455" + integrity sha512-fexhUFFPTGV8ybAtSIGbV6gOkSv8UtRbDBnAyLQw4QPKkgNlsH2ByPGtMUqdWkos6YCRmAqViwgZrJc/mRDzZQ== object-assign@^4.1.0: version "4.1.1" resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863" + integrity sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM= object-copy@^0.1.0: version "0.1.0" resolved "https://registry.yarnpkg.com/object-copy/-/object-copy-0.1.0.tgz#7e7d858b781bd7c991a41ba975ed3812754e998c" + integrity sha1-fn2Fi3gb18mRpBupde04EnVOmYw= dependencies: copy-descriptor "^0.1.0" define-property "^0.2.5" kind-of "^3.0.3" object-keys@^1.0.12: - version "1.1.0" - resolved "https://registry.yarnpkg.com/object-keys/-/object-keys-1.1.0.tgz#11bd22348dd2e096a045ab06f6c85bcc340fa032" + version "1.1.1" + resolved "https://registry.yarnpkg.com/object-keys/-/object-keys-1.1.1.tgz#1c47f272df277f3b1daf061677d9c82e2322c60e" + integrity sha512-NuAESUOUMrlIXOfHKzD6bpPu3tYt3xvjNdRIQ+FeT0lNb4K8WR70CaDxhuNguS2XG+GjkyMwOzsN5ZktImfhLA== object-visit@^1.0.0: version "1.0.1" resolved "https://registry.yarnpkg.com/object-visit/-/object-visit-1.0.1.tgz#f79c4493af0c5377b59fe39d395e41042dd045bb" + integrity sha1-95xEk68MU3e1n+OdOV5BBC3QRbs= dependencies: isobject "^3.0.0" object.getownpropertydescriptors@^2.0.3: version "2.0.3" resolved "https://registry.yarnpkg.com/object.getownpropertydescriptors/-/object.getownpropertydescriptors-2.0.3.tgz#8758c846f5b407adab0f236e0986f14b051caa16" + integrity sha1-h1jIRvW0B62rDyNuCYbxSwUcqhY= dependencies: define-properties "^1.1.2" es-abstract "^1.5.1" -object.omit@^2.0.0: - version "2.0.1" - resolved "https://registry.yarnpkg.com/object.omit/-/object.omit-2.0.1.tgz#1a9c744829f39dbb858c76ca3579ae2a54ebd1fa" - dependencies: - for-own "^0.1.4" - is-extendable "^0.1.1" - object.pick@^1.3.0: version "1.3.0" resolved "https://registry.yarnpkg.com/object.pick/-/object.pick-1.3.0.tgz#87a10ac4c1694bd2e1cbf53591a66141fb5dd747" + integrity sha1-h6EKxMFpS9Lhy/U1kaZhQftd10c= dependencies: isobject "^3.0.1" @@ -3088,6 +3313,7 @@ once@^1.3.0, once@^1.3.1, once@^1.4.0: optimist@^0.6.1: version "0.6.1" resolved "https://registry.yarnpkg.com/optimist/-/optimist-0.6.1.tgz#da3ea74686fa21a19a111c326e90eb15a0196686" + integrity sha1-2j6nRob6IaGaERwybpDrFaAZZoY= dependencies: minimist "~0.0.1" wordwrap "~0.0.2" @@ -3095,6 +3321,7 @@ optimist@^0.6.1: optionator@^0.8.1: version "0.8.2" resolved "https://registry.yarnpkg.com/optionator/-/optionator-0.8.2.tgz#364c5e409d3f4d6301d6c0b4c05bba50180aeb64" + integrity sha1-NkxeQJ0/TWMB1sC0wFu6UBgK62Q= dependencies: deep-is "~0.1.3" fast-levenshtein "~2.0.4" @@ -3110,14 +3337,7 @@ os-browserify@^0.3.0: os-homedir@^1.0.0: version "1.0.2" resolved "https://registry.yarnpkg.com/os-homedir/-/os-homedir-1.0.2.tgz#ffbc4988336e0e833de0c168c7ef152121aa7fb3" - -os-locale@^2.0.0: - version "2.1.0" - resolved "https://registry.yarnpkg.com/os-locale/-/os-locale-2.1.0.tgz#42bc2900a6b5b8bd17376c8e882b65afccf24bf2" - dependencies: - execa "^0.7.0" - lcid "^1.0.0" - mem "^1.1.0" + integrity sha1-/7xJiDNuDoM94MFox+8VISGqf7M= os-locale@^3.0.0: version "3.1.0" @@ -3127,13 +3347,15 @@ os-locale@^3.0.0: lcid "^2.0.0" mem "^4.0.0" -os-tmpdir@^1.0.0, os-tmpdir@^1.0.1: +os-tmpdir@^1.0.0: version "1.0.2" resolved "https://registry.yarnpkg.com/os-tmpdir/-/os-tmpdir-1.0.2.tgz#bbe67406c79aa85c5cfec766fe5734555dfa1274" + integrity sha1-u+Z0BseaqFxc/sdm/lc0VV36EnQ= osenv@^0.1.4: version "0.1.5" resolved "https://registry.yarnpkg.com/osenv/-/osenv-0.1.5.tgz#85cdfafaeb28e8677f416e287592b5f3f49ea410" + integrity sha512-0CWcCECdMVc2Rw3U5w9ZjqX6ga6ubk1xDVKxtBQPK7wis/0F2r9T6k4ydGYhecl7YUBxBVxhL5oisPsNxAPe2g== dependencies: os-homedir "^1.0.0" os-tmpdir "^1.0.0" @@ -3142,41 +3364,38 @@ p-defer@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/p-defer/-/p-defer-1.0.0.tgz#9f6eb182f6c9aa8cd743004a7d4f96b196b0fb0c" +p-each-series@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/p-each-series/-/p-each-series-1.0.0.tgz#930f3d12dd1f50e7434457a22cd6f04ac6ad7f71" + integrity sha1-kw89Et0fUOdDRFeiLNbwSsatf3E= + dependencies: + p-reduce "^1.0.0" + p-finally@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/p-finally/-/p-finally-1.0.0.tgz#3fbcfb15b899a44123b34b6dcc18b724336a2cae" + integrity sha1-P7z7FbiZpEEjs0ttzBi3JDNqLK4= p-is-promise@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/p-is-promise/-/p-is-promise-2.0.0.tgz#7554e3d572109a87e1f3f53f6a7d85d1b194f4c5" -p-limit@^1.1.0: - version "1.3.0" - resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-1.3.0.tgz#b86bd5f0c25690911c7590fcbfc2010d54b3ccb8" - dependencies: - p-try "^1.0.0" - p-limit@^2.0.0: version "2.1.0" resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-2.1.0.tgz#1d5a0d20fb12707c758a655f6bbc4386b5930d68" dependencies: p-try "^2.0.0" -p-locate@^2.0.0: - version "2.0.0" - resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-2.0.0.tgz#20a0103b222a70c8fd39cc2e580680f3dde5ec43" - dependencies: - p-limit "^1.1.0" - p-locate@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-3.0.0.tgz#322d69a05c0264b25997d9f40cd8a891ab0064a4" dependencies: p-limit "^2.0.0" -p-try@^1.0.0: +p-reduce@^1.0.0: version "1.0.0" - resolved "https://registry.yarnpkg.com/p-try/-/p-try-1.0.0.tgz#cbc79cdbaf8fd4228e13f621f2b1a237c1b207b3" + resolved "https://registry.yarnpkg.com/p-reduce/-/p-reduce-1.0.0.tgz#18c2b0dd936a4690a529f8231f58a0fdb6a47dfa" + integrity sha1-GMKw3ZNqRpClKfgjH1ig/bakffo= p-try@^2.0.0: version "2.0.0" @@ -3205,20 +3424,13 @@ parse-asn1@^5.0.0: pbkdf2 "^3.0.3" safe-buffer "^5.1.1" -parse-glob@^3.0.4: - version "3.0.4" - resolved "https://registry.yarnpkg.com/parse-glob/-/parse-glob-3.0.4.tgz#b2c376cfb11f35513badd173ef0bb6e3a388391c" - dependencies: - glob-base "^0.3.0" - is-dotfile "^1.0.0" - is-extglob "^1.0.0" - is-glob "^2.0.0" - -parse-json@^2.2.0: - version "2.2.0" - resolved "https://registry.yarnpkg.com/parse-json/-/parse-json-2.2.0.tgz#f480f40434ef80741f8469099f8dea18f55a4dc9" +parse-json@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/parse-json/-/parse-json-4.0.0.tgz#be35f5425be1f7f6c747184f98a788cb99477ee0" + integrity sha1-vjX1Qlvh9/bHRxhPmKeIy5lHfuA= dependencies: - error-ex "^1.2.0" + error-ex "^1.3.1" + json-parse-better-errors "^1.0.1" parse-passwd@^1.0.0: version "1.0.0" @@ -3227,10 +3439,12 @@ parse-passwd@^1.0.0: parse5@4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/parse5/-/parse5-4.0.0.tgz#6d78656e3da8d78b4ec0b906f7c08ef1dfe3f608" + integrity sha512-VrZ7eOd3T1Fk4XWNXMgiGBK/z0MG48BWG2uQNU4I72fkQuKUTZpl+u9k+CxEG0twMVzSmXEEz12z5Fnw1jIQFA== pascalcase@^0.1.1: version "0.1.1" resolved "https://registry.yarnpkg.com/pascalcase/-/pascalcase-0.1.1.tgz#b363e55e8006ca6fe21784d2db22bd15d7917f14" + integrity sha1-s2PlXoAGym/iF4TS2yK9FdeRfxQ= path-browserify@0.0.0: version "0.0.0" @@ -3240,35 +3454,31 @@ path-dirname@^1.0.0: version "1.0.2" resolved "https://registry.yarnpkg.com/path-dirname/-/path-dirname-1.0.2.tgz#cc33d24d525e099a5388c0336c6e32b9160609e0" -path-exists@^2.0.0: - version "2.1.0" - resolved "https://registry.yarnpkg.com/path-exists/-/path-exists-2.1.0.tgz#0feb6c64f0fc518d9a754dd5efb62c7022761f4b" - dependencies: - pinkie-promise "^2.0.0" - path-exists@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/path-exists/-/path-exists-3.0.0.tgz#ce0ebeaa5f78cb18925ea7d810d7b59b010fd515" + integrity sha1-zg6+ql94yxiSXqfYENe1mwEP1RU= -path-is-absolute@^1.0.0, path-is-absolute@^1.0.1: +path-is-absolute@^1.0.0: version "1.0.1" resolved "https://registry.yarnpkg.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f" + integrity sha1-F0uSaHNVNP+8es5r9TpanhtcX18= path-key@^2.0.0, path-key@^2.0.1: version "2.0.1" resolved "https://registry.yarnpkg.com/path-key/-/path-key-2.0.1.tgz#411cadb574c5a140d3a4b1910d40d80cc9f40b40" -path-parse@^1.0.5, path-parse@^1.0.6: +path-parse@^1.0.6: version "1.0.6" resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.6.tgz#d62dbb5679405d72c4737ec58600e9ddcf06d24c" + integrity sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw== -path-type@^1.0.0: - version "1.1.0" - resolved "https://registry.yarnpkg.com/path-type/-/path-type-1.1.0.tgz#59c44f7ee491da704da415da5a4070ba4f8fe441" +path-type@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/path-type/-/path-type-3.0.0.tgz#cef31dc8e0a1a3bb0d105c0cd97cf3bf47f4e36f" + integrity sha512-T2ZUsdZFHgA3u4e5PfPbjd7HDDpxPnQb5jN0SrDsjNSuVXHJqtwTnWqG0B1jZrgmJ/7lj1EmVIByWt1gxGkWvg== dependencies: - graceful-fs "^4.1.2" - pify "^2.0.0" - pinkie-promise "^2.0.0" + pify "^3.0.0" pbkdf2@^3.0.3: version "3.0.17" @@ -3283,30 +3493,23 @@ pbkdf2@^3.0.3: performance-now@^2.1.0: version "2.1.0" resolved "https://registry.yarnpkg.com/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b" - -pify@^2.0.0: - version "2.3.0" - resolved "https://registry.yarnpkg.com/pify/-/pify-2.3.0.tgz#ed141a6ac043a849ea588498e7dca8b15330e90c" + integrity sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns= pify@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/pify/-/pify-3.0.0.tgz#e5a4acd2c101fdf3d9a4d07f0dbc4db49dd28176" -pinkie-promise@^2.0.0: - version "2.0.1" - resolved "https://registry.yarnpkg.com/pinkie-promise/-/pinkie-promise-2.0.1.tgz#2135d6dfa7a358c069ac9b178776288228450ffa" - dependencies: - pinkie "^2.0.0" - -pinkie@^2.0.0: - version "2.0.4" - resolved "https://registry.yarnpkg.com/pinkie/-/pinkie-2.0.4.tgz#72556b80cfa0d48a974e80e77248e80ed4f7f870" +pify@^4.0.1: + version "4.0.1" + resolved "https://registry.yarnpkg.com/pify/-/pify-4.0.1.tgz#4b2cd25c50d598735c50292224fd8c6df41e3231" + integrity sha512-uB80kBFb/tfd68bVleG9T5GGsGPjJrLAUpR5PZIrhBnIaRTQRjqdJSsIKkOP6OAIFbj7GOrcudc5pNjZ+geV2g== -pkg-dir@^2.0.0: - version "2.0.0" - resolved "https://registry.yarnpkg.com/pkg-dir/-/pkg-dir-2.0.0.tgz#f6d5d1109e19d63edf428e0bd57e12777615334b" +pirates@^4.0.1: + version "4.0.1" + resolved "https://registry.yarnpkg.com/pirates/-/pirates-4.0.1.tgz#643a92caf894566f91b2b986d2c66950a8e2fb87" + integrity sha512-WuNqLTbMI3tmfef2TKxlQmAiLHKtFhlsCZnPIpuv2Ow0RDVO8lfy1Opf4NUzlMXLjPl+Men7AuVdX6TA+s+uGA== dependencies: - find-up "^2.1.0" + node-modules-regexp "^1.0.0" pkg-dir@^3.0.0: version "3.0.0" @@ -3317,10 +3520,12 @@ pkg-dir@^3.0.0: pn@^1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/pn/-/pn-1.1.0.tgz#e2f4cef0e219f463c179ab37463e4e1ecdccbafb" + integrity sha512-2qHaIQr2VLRFoxe2nASzsV6ef4yOOH+Fi9FBOVH6cqeSgUnoyySPZkxzLuzd+RYOQTRpROA0ztTMqxROKSb/nA== posix-character-classes@^0.1.0: version "0.1.1" resolved "https://registry.yarnpkg.com/posix-character-classes/-/posix-character-classes-0.1.1.tgz#01eac0fe3b5af71a2a6c02feabb8c1fef7e00eab" + integrity sha1-AerA/jta9xoqbAL+q7jB/vfgDqs= prando@^5.1.0: version "5.1.0" @@ -3329,29 +3534,26 @@ prando@^5.1.0: prelude-ls@~1.1.2: version "1.1.2" resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.1.2.tgz#21932a549f5e52ffd9a827f570e04be62a97da54" - -preserve@^0.2.0: - version "0.2.0" - resolved "https://registry.yarnpkg.com/preserve/-/preserve-0.2.0.tgz#815ed1f6ebc65926f865b310c0713bcb3315ce4b" + integrity sha1-IZMqVJ9eUv/ZqCf1cOBL5iqX2lQ= prettier@^1.16.4: version "1.16.4" resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.16.4.tgz#73e37e73e018ad2db9c76742e2647e21790c9717" -pretty-format@^23.6.0: - version "23.6.0" - resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-23.6.0.tgz#5eaac8eeb6b33b987b7fe6097ea6a8a146ab5760" +pretty-format@^24.8.0: + version "24.8.0" + resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-24.8.0.tgz#8dae7044f58db7cb8be245383b565a963e3c27f2" + integrity sha512-P952T7dkrDEplsR+TuY7q3VXDae5Sr7zmQb12JU/NDQa/3CH7/QW0yvqLcGN6jL+zQFKaoJcPc+yJxMTGmosqw== dependencies: - ansi-regex "^3.0.0" + "@jest/types" "^24.8.0" + ansi-regex "^4.0.0" ansi-styles "^3.2.0" - -private@^0.1.8: - version "0.1.8" - resolved "https://registry.yarnpkg.com/private/-/private-0.1.8.tgz#2381edb3689f7a53d653190060fcf822d2f368ff" + react-is "^16.8.4" process-nextick-args@~2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/process-nextick-args/-/process-nextick-args-2.0.0.tgz#a37d732f4271b4ab1ad070d35508e8290788ffaa" + integrity sha512-MtEC1TqN0EU5nephaJ4rAtThHtC86dNN9qCuEhtshvpVBkAW5ZO7BASN9REnF9eoXGcRub+pFuKEpOHE+HbEMw== process@^0.11.10: version "0.11.10" @@ -3361,24 +3563,22 @@ promise-inflight@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/promise-inflight/-/promise-inflight-1.0.1.tgz#98472870bf228132fcbdd868129bad12c3c029e3" -prompts@^0.1.9: - version "0.1.14" - resolved "https://registry.yarnpkg.com/prompts/-/prompts-0.1.14.tgz#a8e15c612c5c9ec8f8111847df3337c9cbd443b2" +prompts@^2.0.1: + version "2.1.0" + resolved "https://registry.yarnpkg.com/prompts/-/prompts-2.1.0.tgz#bf90bc71f6065d255ea2bdc0fe6520485c1b45db" + integrity sha512-+x5TozgqYdOwWsQFZizE/Tra3fKvAoy037kOyU6cgz84n8f6zxngLOV4O32kTwt9FcLCxAqw0P/c8rOr9y+Gfg== dependencies: - kleur "^2.0.1" - sisteransi "^0.1.1" + kleur "^3.0.2" + sisteransi "^1.0.0" prr@~1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/prr/-/prr-1.0.1.tgz#d3fc114ba06995a45ec6893f484ceb1d78f5f476" -pseudomap@^1.0.2: - version "1.0.2" - resolved "https://registry.yarnpkg.com/pseudomap/-/pseudomap-1.0.2.tgz#f052a28da70e618917ef0a8ac34c1ae5a68286b3" - psl@^1.1.24, psl@^1.1.28: - version "1.1.31" - resolved "https://registry.yarnpkg.com/psl/-/psl-1.1.31.tgz#e9aa86d0101b5b105cbe93ac6b784cd547276184" + version "1.1.32" + resolved "https://registry.yarnpkg.com/psl/-/psl-1.1.32.tgz#3f132717cf2f9c169724b2b6caf373cf694198db" + integrity sha512-MHACAkHpihU/REGGPLj4sEfc/XKW2bheigvHO1dUqjaKigMp1C8+WLQYRGgeKFMsw5PMfegZcaN8IDXK/cD0+g== public-encrypt@^4.0.0: version "4.0.3" @@ -3424,10 +3624,12 @@ punycode@^1.2.4, punycode@^1.4.1: punycode@^2.1.0, punycode@^2.1.1: version "2.1.1" resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec" + integrity sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A== qs@~6.5.2: version "6.5.2" resolved "https://registry.yarnpkg.com/qs/-/qs-6.5.2.tgz#cb3ae806e8740444584ef154ce8ee98d403f3e36" + integrity sha512-N5ZAX4/LxJmF+7wN74pUD6qAh9/wnvdQcjq9TZjevvXzSUo7bfmw91saqMjzGS2xq91/odN2dW/WOl7qQHNDGA== querystring-es3@^0.2.0: version "0.2.1" @@ -3437,14 +3639,6 @@ querystring@0.2.0: version "0.2.0" resolved "https://registry.yarnpkg.com/querystring/-/querystring-0.2.0.tgz#b209849203bb25df820da756e747005878521620" -randomatic@^3.0.0: - version "3.1.1" - resolved "https://registry.yarnpkg.com/randomatic/-/randomatic-3.1.1.tgz#b776efc59375984e36c537b2f51a1f0aff0da1ed" - dependencies: - is-number "^4.0.0" - kind-of "^6.0.0" - math-random "^1.0.1" - randombytes@^2.0.0, randombytes@^2.0.1, randombytes@^2.0.5: version "2.1.0" resolved "https://registry.yarnpkg.com/randombytes/-/randombytes-2.1.0.tgz#df6f84372f0270dc65cdf6291349ab7a473d4f2a" @@ -3461,30 +3655,39 @@ randomfill@^1.0.3: rc@^1.2.7: version "1.2.8" resolved "https://registry.yarnpkg.com/rc/-/rc-1.2.8.tgz#cd924bf5200a075b83c188cd6b9e211b7fc0d3ed" + integrity sha512-y3bGgqKj3QBdxLbLkomlohkvsA8gdAiUQlSBJnBhfn+BPxg4bc62d8TcBW15wavDfgexCgccckhcZvywyQYPOw== dependencies: deep-extend "^0.6.0" ini "~1.3.0" minimist "^1.2.0" strip-json-comments "~2.0.1" -read-pkg-up@^1.0.1: - version "1.0.1" - resolved "https://registry.yarnpkg.com/read-pkg-up/-/read-pkg-up-1.0.1.tgz#9d63c13276c065918d57f002a57f40a1b643fb02" +react-is@^16.8.4: + version "16.8.6" + resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.8.6.tgz#5bbc1e2d29141c9fbdfed456343fe2bc430a6a16" + integrity sha512-aUk3bHfZ2bRSVFFbbeVS4i+lNPZr3/WM5jT2J5omUVV1zzcs1nAaf3l51ctA5FFvCRbhrH0bdAsRRQddFJZPtA== + +read-pkg-up@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/read-pkg-up/-/read-pkg-up-4.0.0.tgz#1b221c6088ba7799601c808f91161c66e58f8978" + integrity sha512-6etQSH7nJGsK0RbG/2TeDzZFa8shjQ1um+SwQQ5cwKy0dhSXdOncEhb1CPpvQG4h7FyOV6EB6YlV0yJvZQNAkA== dependencies: - find-up "^1.0.0" - read-pkg "^1.0.0" + find-up "^3.0.0" + read-pkg "^3.0.0" -read-pkg@^1.0.0: - version "1.1.0" - resolved "https://registry.yarnpkg.com/read-pkg/-/read-pkg-1.1.0.tgz#f5ffaa5ecd29cb31c0474bca7d756b6bb29e3f28" +read-pkg@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/read-pkg/-/read-pkg-3.0.0.tgz#9cbc686978fee65d16c00e2b19c237fcf6e38389" + integrity sha1-nLxoaXj+5l0WwA4rGcI3/Pbjg4k= dependencies: - load-json-file "^1.0.0" + load-json-file "^4.0.0" normalize-package-data "^2.3.2" - path-type "^1.0.0" + path-type "^3.0.0" "readable-stream@1 || 2", readable-stream@^2.0.0, readable-stream@^2.0.1, readable-stream@^2.0.2, readable-stream@^2.0.6, readable-stream@^2.1.5, readable-stream@^2.2.2, readable-stream@^2.3.3, readable-stream@^2.3.6, readable-stream@~2.3.6: version "2.3.6" resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.3.6.tgz#b11c27d88b8ff1fbe070643cf94b0c79ae1b0aaf" + integrity sha512-tQtKA9WIAhBF3+VLAseyMqZeBjW0AHJoxOtYqSUZNJxauErmLbVm2FW1y+J/YA9dUrAC39ITejlZWhVIwawkKw== dependencies: core-util-is "~1.0.0" inherits "~2.0.3" @@ -3502,25 +3705,17 @@ readdirp@^2.2.1: micromatch "^3.1.10" readable-stream "^2.0.2" -realpath-native@^1.0.0: +realpath-native@^1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/realpath-native/-/realpath-native-1.1.0.tgz#2003294fea23fb0672f2476ebe22fcf498a2d65c" + integrity sha512-wlgPA6cCIIg9gKz0fgAPjnzh4yR/LnXovwuo9hvyGvx3h8nX4+/iLZplfUWasXpqD8BdnGnP5njOFjkUwPzvjA== dependencies: util.promisify "^1.0.0" -regenerator-runtime@^0.11.0: - version "0.11.1" - resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.11.1.tgz#be05ad7f9bf7d22e056f9726cee5017fbf19e2e9" - -regex-cache@^0.4.2: - version "0.4.4" - resolved "https://registry.yarnpkg.com/regex-cache/-/regex-cache-0.4.4.tgz#75bdc58a2a1496cec48a12835bc54c8d562336dd" - dependencies: - is-equal-shallow "^0.1.3" - regex-not@^1.0.0, regex-not@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/regex-not/-/regex-not-1.0.2.tgz#1f4ece27e00b0b65e0247a6810e6a85d83a5752c" + integrity sha512-J6SDjUgDxQj5NusnOtdFxDwN/+HWykR8GELwctJ7mdqhcyy1xEc4SRFHUXvxTp661YaVKAjfRLZ9cCqS6tn32A== dependencies: extend-shallow "^3.0.2" safe-regex "^1.1.0" @@ -3528,38 +3723,38 @@ regex-not@^1.0.0, regex-not@^1.0.2: remove-trailing-separator@^1.0.1: version "1.1.0" resolved "https://registry.yarnpkg.com/remove-trailing-separator/-/remove-trailing-separator-1.1.0.tgz#c24bce2a283adad5bc3f58e0d48249b92379d8ef" + integrity sha1-wkvOKig62tW8P1jg1IJJuSN52O8= repeat-element@^1.1.2: version "1.1.3" resolved "https://registry.yarnpkg.com/repeat-element/-/repeat-element-1.1.3.tgz#782e0d825c0c5a3bb39731f84efee6b742e6b1ce" + integrity sha512-ahGq0ZnV5m5XtZLMb+vP76kcAM5nkLqk0lpqAuojSKGgQtn4eRi4ZZGm2olo2zKFH+sMsWaqOCW1dqAnOru72g== -repeat-string@^1.5.2, repeat-string@^1.6.1: +repeat-string@^1.6.1: version "1.6.1" resolved "https://registry.yarnpkg.com/repeat-string/-/repeat-string-1.6.1.tgz#8dcae470e1c88abc2d600fff4a776286da75e637" + integrity sha1-jcrkcOHIirwtYA//Sndihtp15jc= -repeating@^2.0.0: - version "2.0.1" - resolved "https://registry.yarnpkg.com/repeating/-/repeating-2.0.1.tgz#5214c53a926d3552707527fbab415dbc08d06dda" - dependencies: - is-finite "^1.0.0" - -request-promise-core@1.1.1: - version "1.1.1" - resolved "https://registry.yarnpkg.com/request-promise-core/-/request-promise-core-1.1.1.tgz#3eee00b2c5aa83239cfb04c5700da36f81cd08b6" +request-promise-core@1.1.2: + version "1.1.2" + resolved "https://registry.yarnpkg.com/request-promise-core/-/request-promise-core-1.1.2.tgz#339f6aababcafdb31c799ff158700336301d3346" + integrity sha512-UHYyq1MO8GsefGEt7EprS8UrXsm1TxEvFUX1IMTuSLU2Rh7fTIdFtl8xD7JiEYiWU2dl+NYAjCTksTehQUxPag== dependencies: - lodash "^4.13.1" + lodash "^4.17.11" request-promise-native@^1.0.5: - version "1.0.5" - resolved "https://registry.yarnpkg.com/request-promise-native/-/request-promise-native-1.0.5.tgz#5281770f68e0c9719e5163fd3fab482215f4fda5" + version "1.0.7" + resolved "https://registry.yarnpkg.com/request-promise-native/-/request-promise-native-1.0.7.tgz#a49868a624bdea5069f1251d0a836e0d89aa2c59" + integrity sha512-rIMnbBdgNViL37nZ1b3L/VfPOpSi0TqVDQPAvO6U14lMzOLrt5nilxCQqtDKhZeDiW0/hkCXGoQjhgJd/tCh6w== dependencies: - request-promise-core "1.1.1" - stealthy-require "^1.1.0" - tough-cookie ">=2.3.3" + request-promise-core "1.1.2" + stealthy-require "^1.1.1" + tough-cookie "^2.3.3" request@^2.87.0: version "2.88.0" resolved "https://registry.yarnpkg.com/request/-/request-2.88.0.tgz#9c2fca4f7d35b592efe57c7f0a55e81052124fef" + integrity sha512-NAqBSrijGLZdM0WZNsInLJpkJokL72XYjUpnB0iwsRgxh7dB6COrHnTBNwN0E+lHDAJzu7kLAkDeY08z2/A0hg== dependencies: aws-sign2 "~0.7.0" aws4 "^1.8.0" @@ -3585,14 +3780,22 @@ request@^2.87.0: require-directory@^2.1.1: version "2.1.1" resolved "https://registry.yarnpkg.com/require-directory/-/require-directory-2.1.1.tgz#8c64ad5fd30dab1c976e2344ffe7f792a6a6df42" + integrity sha1-jGStX9MNqxyXbiNE/+f3kqam30I= require-main-filename@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/require-main-filename/-/require-main-filename-1.0.1.tgz#97f717b69d48784f5f526a6c5aa8ffdda055a4d1" + integrity sha1-l/cXtp1IeE9fUmpsWqj/3aBVpNE= + +require-main-filename@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/require-main-filename/-/require-main-filename-2.0.0.tgz#d0b329ecc7cc0f61649f62215be69af54aa8989b" + integrity sha512-NKN5kMDylKuldxYLSUfrbo5Tuzh4hd+2E8NPPX02mZtn1VuREQToYe/ZdlJy+J3uCpfaiGF05e7B8W0iXbQHmg== resolve-cwd@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/resolve-cwd/-/resolve-cwd-2.0.0.tgz#00a9f7387556e27038eae232caa372a6a59b665a" + integrity sha1-AKn3OHVW4nA46uIyyqNypqWbZlo= dependencies: resolve-from "^3.0.0" @@ -3606,26 +3809,37 @@ resolve-dir@^1.0.0, resolve-dir@^1.0.1: resolve-from@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/resolve-from/-/resolve-from-3.0.0.tgz#b22c7af7d9d6881bc8b6e653335eebcb0a188748" + integrity sha1-six699nWiBvItuZTM17rywoYh0g= resolve-url@^0.2.1: version "0.2.1" resolved "https://registry.yarnpkg.com/resolve-url/-/resolve-url-0.2.1.tgz#2c637fe77c893afd2a663fe21aa9080068e2052a" + integrity sha1-LGN/53yJOv0qZj/iGqkIAGjiBSo= resolve@1.1.7: version "1.1.7" resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.1.7.tgz#203114d82ad2c5ed9e8e0411b3932875e889e97b" + integrity sha1-IDEU2CrSxe2ejgQRs5ModeiJ6Xs= -resolve@1.x, resolve@^1.10.0: +resolve@1.x: version "1.10.0" resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.10.0.tgz#3bdaaeaf45cc07f375656dfd2e54ed0810b101ba" dependencies: path-parse "^1.0.6" +resolve@^1.10.0, resolve@^1.3.2: + version "1.11.1" + resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.11.1.tgz#ea10d8110376982fef578df8fc30b9ac30a07a3e" + integrity sha512-vIpgF6wfuJOZI7KKKSP+HmiKggadPQAdsp5HiC1mvqnfp0gF1vdwgBWZIdrVft9pgqoMFQN+R7BSWZiBxx+BBw== + dependencies: + path-parse "^1.0.6" + ret@~0.1.10: version "0.1.15" resolved "https://registry.yarnpkg.com/ret/-/ret-0.1.15.tgz#b8a4825d5bdb1fc3f6f53c2bc33f81388681c7bc" + integrity sha512-TTlYpa+OL+vMMNG24xSlQGEJ3B/RzEfUlLct7b5G/ytav+wPrplCpVMFuwzXbkecJrb6IYo1iFb0S9v37754mg== -rimraf@^2.5.4, rimraf@^2.6.1, rimraf@^2.6.2: +rimraf@^2.5.4, rimraf@^2.6.1, rimraf@^2.6.2, rimraf@^2.6.3: version "2.6.3" resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.6.3.tgz#b2d104fe0d8fb27cf9e0a1cda8262dd3833c6cab" dependencies: @@ -3638,9 +3852,10 @@ ripemd160@^2.0.0, ripemd160@^2.0.1: hash-base "^3.0.0" inherits "^2.0.1" -rsvp@^3.3.3: - version "3.6.2" - resolved "https://registry.yarnpkg.com/rsvp/-/rsvp-3.6.2.tgz#2e96491599a96cde1b515d5674a8f7a91452926a" +rsvp@^4.8.4: + version "4.8.4" + resolved "https://registry.yarnpkg.com/rsvp/-/rsvp-4.8.4.tgz#b50e6b34583f3dd89329a2f23a8a2be072845911" + integrity sha512-6FomvYPfs+Jy9TfXmBpBuMWNH94SgCsZmJKcanySzgNNP6LjWxBvyLTa9KaMfDDM5oxRfrKDB0r/qeRsLwnBfA== run-queue@^1.0.0, run-queue@^1.0.3: version "1.0.3" @@ -3651,35 +3866,39 @@ run-queue@^1.0.0, run-queue@^1.0.3: safe-buffer@^5.0.1, safe-buffer@^5.1.0, safe-buffer@^5.1.1, safe-buffer@^5.1.2, safe-buffer@~5.1.0, safe-buffer@~5.1.1: version "5.1.2" resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.2.tgz#991ec69d296e0313747d59bdfd2b745c35f8828d" + integrity sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g== safe-regex@^1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/safe-regex/-/safe-regex-1.1.0.tgz#40a3669f3b077d1e943d44629e157dd48023bf2e" + integrity sha1-QKNmnzsHfR6UPURinhV91IAjvy4= dependencies: ret "~0.1.10" "safer-buffer@>= 2.1.2 < 3", safer-buffer@^2.0.2, safer-buffer@^2.1.0, safer-buffer@~2.1.0: version "2.1.2" resolved "https://registry.yarnpkg.com/safer-buffer/-/safer-buffer-2.1.2.tgz#44fa161b0187b9549dd84bb91802f9bd8385cd6a" + integrity sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg== -sane@^2.0.0: - version "2.5.2" - resolved "https://registry.yarnpkg.com/sane/-/sane-2.5.2.tgz#b4dc1861c21b427e929507a3e751e2a2cb8ab3fa" +sane@^4.0.3: + version "4.1.0" + resolved "https://registry.yarnpkg.com/sane/-/sane-4.1.0.tgz#ed881fd922733a6c461bc189dc2b6c006f3ffded" + integrity sha512-hhbzAgTIX8O7SHfp2c8/kREfEn4qO/9q8C9beyY6+tvZ87EpoZ3i1RIEvp27YBswnNbY9mWd6paKVmKbAgLfZA== dependencies: + "@cnakazawa/watch" "^1.0.3" anymatch "^2.0.0" - capture-exit "^1.2.0" - exec-sh "^0.2.0" + capture-exit "^2.0.0" + exec-sh "^0.3.2" + execa "^1.0.0" fb-watchman "^2.0.0" micromatch "^3.1.4" minimist "^1.1.1" walker "~1.0.5" - watch "~0.18.0" - optionalDependencies: - fsevents "^1.2.3" sax@^1.2.4: version "1.2.4" resolved "https://registry.yarnpkg.com/sax/-/sax-1.2.4.tgz#2816234e2378bddc4e5354fab5caa895df7100d9" + integrity sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw== schema-utils@^1.0.0: version "1.0.0" @@ -3689,10 +3908,20 @@ schema-utils@^1.0.0: ajv-errors "^1.0.0" ajv-keywords "^3.1.0" -"semver@2 || 3 || 4 || 5", semver@^5.0.1, semver@^5.3.0, semver@^5.5, semver@^5.5.0: +"semver@2 || 3 || 4 || 5", semver@^5.3.0, semver@^5.4.1, semver@^5.5.0, semver@^5.6.0: + version "5.7.0" + resolved "https://registry.yarnpkg.com/semver/-/semver-5.7.0.tgz#790a7cf6fea5459bac96110b29b60412dc8ff96b" + integrity sha512-Ya52jSX2u7QKghxeoFGpLwCtGlt7j0oY9DYb5apt9nPlJ42ID+ulTXESnt/qAQcoSERyZ5sl3LDIOw0nAn/5DA== + +semver@^5.0.1, semver@^5.5: version "5.6.0" resolved "https://registry.yarnpkg.com/semver/-/semver-5.6.0.tgz#7e74256fbaa49c75aa7c7a205cc22799cac80004" +semver@^6.0.0: + version "6.1.1" + resolved "https://registry.yarnpkg.com/semver/-/semver-6.1.1.tgz#53f53da9b30b2103cd4f15eab3a18ecbcb210c9b" + integrity sha512-rWYq2e5iYW+fFe/oPPtYJxYgjBm8sC4rmoGdUOgBB7VnwKt6HrL793l2voH1UlsyYZpJ4g0wfjnTEO1s1NP2eQ== + serialize-javascript@^1.4.0: version "1.6.1" resolved "https://registry.yarnpkg.com/serialize-javascript/-/serialize-javascript-1.6.1.tgz#4d1f697ec49429a847ca6f442a2a755126c4d879" @@ -3700,10 +3929,12 @@ serialize-javascript@^1.4.0: set-blocking@^2.0.0, set-blocking@~2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/set-blocking/-/set-blocking-2.0.0.tgz#045f9782d011ae9a6803ddd382b24392b3d890f7" + integrity sha1-BF+XgtARrppoA93TgrJDkrPYkPc= set-value@^0.4.3: version "0.4.3" resolved "https://registry.yarnpkg.com/set-value/-/set-value-0.4.3.tgz#7db08f9d3d22dc7f78e53af3c3bf4666ecdfccf1" + integrity sha1-fbCPnT0i3H945Trzw79GZuzfzPE= dependencies: extend-shallow "^2.0.1" is-extendable "^0.1.1" @@ -3713,6 +3944,7 @@ set-value@^0.4.3: set-value@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/set-value/-/set-value-2.0.0.tgz#71ae4a88f0feefbbf52d1ea604f3fb315ebb6274" + integrity sha512-hw0yxk9GT/Hr5yJEYnHNKYXkIA8mVJgd9ditYZCe16ZczcaELYYcfvaXesNACk2O8O0nTiPQcQhGUQj8JLzeeg== dependencies: extend-shallow "^2.0.1" is-extendable "^0.1.1" @@ -3733,32 +3965,39 @@ sha.js@^2.4.0, sha.js@^2.4.8: shebang-command@^1.2.0: version "1.2.0" resolved "https://registry.yarnpkg.com/shebang-command/-/shebang-command-1.2.0.tgz#44aac65b695b03398968c39f363fee5deafdf1ea" + integrity sha1-RKrGW2lbAzmJaMOfNj/uXer98eo= dependencies: shebang-regex "^1.0.0" shebang-regex@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/shebang-regex/-/shebang-regex-1.0.0.tgz#da42f49740c0b42db2ca9728571cb190c98efea3" + integrity sha1-2kL0l0DAtC2yypcoVxyxkMmO/qM= shellwords@^0.1.1: version "0.1.1" resolved "https://registry.yarnpkg.com/shellwords/-/shellwords-0.1.1.tgz#d6b9181c1a48d397324c84871efbcfc73fc0654b" + integrity sha512-vFwSUfQvqybiICwZY5+DAWIPLKsWO31Q91JSKl3UYv+K5c2QRPzn0qzec6QPu1Qc9eHYItiP3NdJqNVqetYAww== signal-exit@^3.0.0, signal-exit@^3.0.2: version "3.0.2" resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.2.tgz#b5fdc08f1287ea1178628e415e25132b73646c6d" + integrity sha1-tf3AjxKH6hF4Yo5BXiUTK3NkbG0= -sisteransi@^0.1.1: - version "0.1.1" - resolved "https://registry.yarnpkg.com/sisteransi/-/sisteransi-0.1.1.tgz#5431447d5f7d1675aac667ccd0b865a4994cb3ce" - -slash@^1.0.0: +sisteransi@^1.0.0: version "1.0.0" - resolved "https://registry.yarnpkg.com/slash/-/slash-1.0.0.tgz#c41f2f6c39fc16d1cd17ad4b5d896114ae470d55" + resolved "https://registry.yarnpkg.com/sisteransi/-/sisteransi-1.0.0.tgz#77d9622ff909080f1c19e5f4a1df0c1b0a27b88c" + integrity sha512-N+z4pHB4AmUv0SjveWRd6q1Nj5w62m5jodv+GD8lvmbY/83T/rpbJGZOnK5T149OldDj4Db07BSv9xY4K6NTPQ== + +slash@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/slash/-/slash-2.0.0.tgz#de552851a1759df3a8f206535442f5ec4ddeab44" + integrity sha512-ZYKh3Wh2z1PpEXWr0MpSBZ0V6mZHAQfYevttO11c51CaWjGTaadiKZ+wVt1PbMlDV5qhMFslpZCemhwOK7C89A== snapdragon-node@^2.0.1: version "2.1.1" resolved "https://registry.yarnpkg.com/snapdragon-node/-/snapdragon-node-2.1.1.tgz#6c175f86ff14bdb0724563e8f3c1b021a286853b" + integrity sha512-O27l4xaMYt/RSQ5TR3vpWCAB5Kb/czIcqUFOM/C4fYcLnbZUc1PkjTAMjof2pBWaSTwOUd6qUHcFGVGj7aIwnw== dependencies: define-property "^1.0.0" isobject "^3.0.0" @@ -3767,12 +4006,14 @@ snapdragon-node@^2.0.1: snapdragon-util@^3.0.1: version "3.0.1" resolved "https://registry.yarnpkg.com/snapdragon-util/-/snapdragon-util-3.0.1.tgz#f956479486f2acd79700693f6f7b805e45ab56e2" + integrity sha512-mbKkMdQKsjX4BAL4bRYTj21edOf8cN7XHdYUJEe+Zn99hVEYcMvKPct1IqNe7+AZPirn8BCDOQBHQZknqmKlZQ== dependencies: kind-of "^3.2.0" snapdragon@^0.8.1: version "0.8.2" resolved "https://registry.yarnpkg.com/snapdragon/-/snapdragon-0.8.2.tgz#64922e7c565b0e14204ba1aa7d6964278d25182d" + integrity sha512-FtyOnWN/wCHTVXOMwvSv26d+ko5vWlIDD6zoUJ7LW8vh+ZBC8QdljveRP+crNrtBwioEUWy/4dMtbBjA4ioNlg== dependencies: base "^0.11.1" debug "^2.2.0" @@ -3790,6 +4031,7 @@ source-list-map@^2.0.0: source-map-resolve@^0.5.0: version "0.5.2" resolved "https://registry.yarnpkg.com/source-map-resolve/-/source-map-resolve-0.5.2.tgz#72e2cc34095543e43b2c62b2c4c10d4a9054f259" + integrity sha512-MjqsvNwyz1s0k81Goz/9vRBe9SZdB09Bdw+/zYyO+3CuPk6fouTaxscHkgtE8jKvf01kVfl8riHzERQ/kefaSA== dependencies: atob "^2.1.1" decode-uri-component "^0.2.0" @@ -3797,13 +4039,15 @@ source-map-resolve@^0.5.0: source-map-url "^0.4.0" urix "^0.1.0" -source-map-support@^0.4.15: - version "0.4.18" - resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.4.18.tgz#0286a6de8be42641338594e97ccea75f0a2c585f" +source-map-support@^0.5.6: + version "0.5.12" + resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.12.tgz#b4f3b10d51857a5af0138d3ce8003b201613d599" + integrity sha512-4h2Pbvyy15EE02G+JOZpUCmqWJuqrs+sEkzewTm++BPi7Hvn/HwcqLAcNxYAyI0x13CpPPn+kMjl+hplXMHITQ== dependencies: - source-map "^0.5.6" + buffer-from "^1.0.0" + source-map "^0.6.0" -source-map-support@^0.5.6, source-map-support@~0.5.9: +source-map-support@~0.5.9: version "0.5.10" resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.10.tgz#2214080bc9d51832511ee2bab96e3c2f9353120c" dependencies: @@ -3813,18 +4057,22 @@ source-map-support@^0.5.6, source-map-support@~0.5.9: source-map-url@^0.4.0: version "0.4.0" resolved "https://registry.yarnpkg.com/source-map-url/-/source-map-url-0.4.0.tgz#3e935d7ddd73631b97659956d55128e87b5084a3" + integrity sha1-PpNdfd1zYxuXZZlW1VEo6HtQhKM= -source-map@^0.5.3, source-map@^0.5.6, source-map@^0.5.7: +source-map@^0.5.0, source-map@^0.5.6: version "0.5.7" resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.5.7.tgz#8a039d2d1021d22d1ea14c80d8ea468ba2ef3fcc" + integrity sha1-igOdLRAh0i0eoUyA2OpGi6LvP8w= source-map@^0.6.0, source-map@^0.6.1, source-map@~0.6.1: version "0.6.1" resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263" + integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g== spdx-correct@^3.0.0: version "3.1.0" resolved "https://registry.yarnpkg.com/spdx-correct/-/spdx-correct-3.1.0.tgz#fb83e504445268f154b074e218c87c003cd31df4" + integrity sha512-lr2EZCctC2BNR7j7WzJ2FpDznxky1sjfxvvYEyzxNyb6lZXHODmEoJeFu4JupYlkfha1KZpJyoqiJ7pgA1qq8Q== dependencies: spdx-expression-parse "^3.0.0" spdx-license-ids "^3.0.0" @@ -3832,31 +4080,32 @@ spdx-correct@^3.0.0: spdx-exceptions@^2.1.0: version "2.2.0" resolved "https://registry.yarnpkg.com/spdx-exceptions/-/spdx-exceptions-2.2.0.tgz#2ea450aee74f2a89bfb94519c07fcd6f41322977" + integrity sha512-2XQACfElKi9SlVb1CYadKDXvoajPgBVPn/gOQLrTvHdElaVhr7ZEbqJaRnJLVNeaI4cMEAgVCeBMKF6MWRDCRA== spdx-expression-parse@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/spdx-expression-parse/-/spdx-expression-parse-3.0.0.tgz#99e119b7a5da00e05491c9fa338b7904823b41d0" + integrity sha512-Yg6D3XpRD4kkOmTpdgbUiEJFKghJH03fiC1OPll5h/0sO6neh2jqRDVHOQ4o/LMea0tgCkbMgea5ip/e+MkWyg== dependencies: spdx-exceptions "^2.1.0" spdx-license-ids "^3.0.0" spdx-license-ids@^3.0.0: - version "3.0.3" - resolved "https://registry.yarnpkg.com/spdx-license-ids/-/spdx-license-ids-3.0.3.tgz#81c0ce8f21474756148bbb5f3bfc0f36bf15d76e" + version "3.0.4" + resolved "https://registry.yarnpkg.com/spdx-license-ids/-/spdx-license-ids-3.0.4.tgz#75ecd1a88de8c184ef015eafb51b5b48bfd11bb1" + integrity sha512-7j8LYJLeY/Yb6ACbQ7F76qy5jHkp0U6jgBfJsk97bwWlVUnUWsAgpyaCvo17h0/RQGnQ036tVDomiwoI4pDkQA== split-string@^3.0.1, split-string@^3.0.2: version "3.1.0" resolved "https://registry.yarnpkg.com/split-string/-/split-string-3.1.0.tgz#7cb09dda3a86585705c64b39a6466038682e8fe2" + integrity sha512-NzNVhJDYpwceVVii8/Hu6DKfD2G+NrQHlS/V/qgv763EYudVwEcMQNxd2lh+0VrUByXN/oJkl5grOhYWvQUYiw== dependencies: extend-shallow "^3.0.0" -sprintf-js@~1.0.2: - version "1.0.3" - resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c" - sshpk@^1.7.0: version "1.16.1" resolved "https://registry.yarnpkg.com/sshpk/-/sshpk-1.16.1.tgz#fb661c0bef29b39db40769ee39fa70093d6f6877" + integrity sha512-HXXqVUq7+pcKeLqqZj6mHFUMvXtOJt1uoUx09pFW6011inTMxqI8BA8PM95myrIyyKwdnzjdFjLiE6KBPVtJIg== dependencies: asn1 "~0.2.3" assert-plus "^1.0.0" @@ -3877,17 +4126,20 @@ ssri@^6.0.1: stack-utils@^1.0.1: version "1.0.2" resolved "https://registry.yarnpkg.com/stack-utils/-/stack-utils-1.0.2.tgz#33eba3897788558bebfc2db059dc158ec36cebb8" + integrity sha512-MTX+MeG5U994cazkjd/9KNAapsHnibjMLnfXodlkXw76JEea0UiNzrqidzo1emMwk7w5Qhc9jd4Bn9TBb1MFwA== static-extend@^0.1.1: version "0.1.2" resolved "https://registry.yarnpkg.com/static-extend/-/static-extend-0.1.2.tgz#60809c39cbff55337226fd5e0b520f341f1fb5c6" + integrity sha1-YICcOcv/VTNyJv1eC1IPNB8ftcY= dependencies: define-property "^0.2.5" object-copy "^0.1.0" -stealthy-require@^1.1.0: +stealthy-require@^1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/stealthy-require/-/stealthy-require-1.1.1.tgz#35b09875b4ff49f26a777e509b3090a3226bf24b" + integrity sha1-NbCYdbT/SfJqd35QmzCQoyJr8ks= stream-browserify@^2.0.1: version "2.0.2" @@ -3920,6 +4172,7 @@ stream-shift@^1.0.0: string-length@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/string-length/-/string-length-2.0.0.tgz#d40dbb686a3ace960c1cffca562bf2c45f8363ed" + integrity sha1-1A27aGo6zpYMHP/KVivyxF+DY+0= dependencies: astral-regex "^1.0.0" strip-ansi "^4.0.0" @@ -3927,6 +4180,7 @@ string-length@^2.0.0: string-width@^1.0.1: version "1.0.2" resolved "https://registry.yarnpkg.com/string-width/-/string-width-1.0.2.tgz#118bdf5b8cdc51a2a7e70d211e07e2b0b9b107d3" + integrity sha1-EYvfW4zcUaKn5w0hHgfisLmxB9M= dependencies: code-point-at "^1.0.0" is-fullwidth-code-point "^1.0.0" @@ -3935,6 +4189,7 @@ string-width@^1.0.1: "string-width@^1.0.2 || 2", string-width@^2.0.0, string-width@^2.1.1: version "2.1.1" resolved "https://registry.yarnpkg.com/string-width/-/string-width-2.1.1.tgz#ab93f27a8dc13d28cac815c462143a6d9012ae9e" + integrity sha512-nOqH59deCq9SRHlxq1Aw85Jnt4w6KvLKqWVik6oA9ZklXLNIOlqg4F2yrT1MVaTjAqvVwdfeZ7w7aCvJD7ugkw== dependencies: is-fullwidth-code-point "^2.0.0" strip-ansi "^4.0.0" @@ -3948,74 +4203,81 @@ string_decoder@^1.0.0: string_decoder@~1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-1.1.1.tgz#9cf1611ba62685d7030ae9e4ba34149c3af03fc8" + integrity sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg== dependencies: safe-buffer "~5.1.0" strip-ansi@^3.0.0, strip-ansi@^3.0.1: version "3.0.1" resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-3.0.1.tgz#6a385fb8853d952d5ff05d0e8aaf94278dc63dcf" + integrity sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8= dependencies: ansi-regex "^2.0.0" strip-ansi@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-4.0.0.tgz#a8479022eb1ac368a871389b635262c505ee368f" + integrity sha1-qEeQIusaw2iocTibY1JixQXuNo8= dependencies: ansi-regex "^3.0.0" -strip-bom@3.0.0: +strip-ansi@^5.0.0: + version "5.2.0" + resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-5.2.0.tgz#8c9a536feb6afc962bdfa5b104a5091c1ad9c0ae" + integrity sha512-DuRs1gKbBqsMKIZlrffwlug8MHkcnpjs5VPmL1PAh+mA30U0DTotfDZ0d2UUsXpPmPmMMJ6W773MaA3J+lbiWA== + dependencies: + ansi-regex "^4.1.0" + +strip-bom@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/strip-bom/-/strip-bom-3.0.0.tgz#2334c18e9c759f7bdd56fdef7e9ae3d588e68ed3" - -strip-bom@^2.0.0: - version "2.0.0" - resolved "https://registry.yarnpkg.com/strip-bom/-/strip-bom-2.0.0.tgz#6219a85616520491f35788bdbf1447a99c7e6b0e" - dependencies: - is-utf8 "^0.2.0" + integrity sha1-IzTBjpx1n3vdVv3vfprj1YjmjtM= strip-eof@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/strip-eof/-/strip-eof-1.0.0.tgz#bb43ff5598a6eb05d89b59fcd129c983313606bf" + integrity sha1-u0P/VZim6wXYm1n80SnJgzE2Br8= strip-json-comments@~2.0.1: version "2.0.1" resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-2.0.1.tgz#3c531942e908c2697c0ec344858c286c7ca0a60a" - -supports-color@^2.0.0: - version "2.0.0" - resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-2.0.0.tgz#535d045ce6b6363fa40117084629995e9df324c7" - -supports-color@^3.1.2: - version "3.2.3" - resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-3.2.3.tgz#65ac0504b3954171d8a64946b2ae3cbb8a5f54f6" - dependencies: - has-flag "^1.0.0" + integrity sha1-PFMZQukIwml8DsNEhYwobHygpgo= supports-color@^5.3.0, supports-color@^5.5.0: version "5.5.0" resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-5.5.0.tgz#e2e69a44ac8772f78a1ec0b35b689df6530efc8f" + integrity sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow== + dependencies: + has-flag "^3.0.0" + +supports-color@^6.1.0: + version "6.1.0" + resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-6.1.0.tgz#0764abc69c63d5ac842dd4867e8d025e880df8f3" + integrity sha512-qe1jfm1Mg7Nq/NSh6XE24gPXROEVsWHxC1LIx//XNlD9iw7YZQGjZNjYN7xGaEG6iKdA8EtNFW6R0gjnVXp+wQ== dependencies: has-flag "^3.0.0" symbol-tree@^3.2.2: version "3.2.2" resolved "https://registry.yarnpkg.com/symbol-tree/-/symbol-tree-3.2.2.tgz#ae27db38f660a7ae2e1c3b7d1bc290819b8519e6" + integrity sha1-rifbOPZgp64uHDt9G8KQgZuFGeY= tapable@^1.0.0, tapable@^1.1.0: version "1.1.1" resolved "https://registry.yarnpkg.com/tapable/-/tapable-1.1.1.tgz#4d297923c5a72a42360de2ab52dadfaaec00018e" tar@^4: - version "4.4.8" - resolved "https://registry.yarnpkg.com/tar/-/tar-4.4.8.tgz#b19eec3fde2a96e64666df9fdb40c5ca1bc3747d" + version "4.4.10" + resolved "https://registry.yarnpkg.com/tar/-/tar-4.4.10.tgz#946b2810b9a5e0b26140cf78bea6b0b0d689eba1" + integrity sha512-g2SVs5QIxvo6OLp0GudTqEf05maawKUxXru104iaayWA09551tFCTI8f1Asb4lPfkBr91k07iL4c11XO3/b0tA== dependencies: chownr "^1.1.1" fs-minipass "^1.2.5" - minipass "^2.3.4" - minizlib "^1.1.1" + minipass "^2.3.5" + minizlib "^1.2.1" mkdirp "^0.5.0" safe-buffer "^5.1.2" - yallist "^3.0.2" + yallist "^3.0.3" terser-webpack-plugin@^1.1.0: version "1.2.2" @@ -4038,19 +4300,20 @@ terser@^3.16.1: source-map "~0.6.1" source-map-support "~0.5.9" -test-exclude@^4.2.1: - version "4.2.3" - resolved "https://registry.yarnpkg.com/test-exclude/-/test-exclude-4.2.3.tgz#a9a5e64474e4398339245a0a769ad7c2f4a97c20" +test-exclude@^5.2.3: + version "5.2.3" + resolved "https://registry.yarnpkg.com/test-exclude/-/test-exclude-5.2.3.tgz#c3d3e1e311eb7ee405e092dac10aefd09091eac0" + integrity sha512-M+oxtseCFO3EDtAaGH7iiej3CBkzXqFMbzqYAACdzKui4eZA+pq3tZEwChvOdNfa7xxy8BfbmgJSIr43cC/+2g== dependencies: - arrify "^1.0.1" - micromatch "^2.3.11" - object-assign "^4.1.0" - read-pkg-up "^1.0.1" - require-main-filename "^1.0.1" + glob "^7.1.3" + minimatch "^3.0.4" + read-pkg-up "^4.0.0" + require-main-filename "^2.0.0" throat@^4.0.0: version "4.1.0" resolved "https://registry.yarnpkg.com/throat/-/throat-4.1.0.tgz#89037cbc92c56ab18926e6ba4cbb200e15672a6a" + integrity sha1-iQN8vJLFarGJJua6TLsgDhVnKmo= through2@^2.0.0: version "2.0.5" @@ -4068,24 +4331,28 @@ timers-browserify@^2.0.4: tmpl@1.0.x: version "1.0.4" resolved "https://registry.yarnpkg.com/tmpl/-/tmpl-1.0.4.tgz#23640dd7b42d00433911140820e5cf440e521dd1" + integrity sha1-I2QN17QtAEM5ERQIIOXPRA5SHdE= to-arraybuffer@^1.0.0: version "1.0.1" resolved "https://registry.yarnpkg.com/to-arraybuffer/-/to-arraybuffer-1.0.1.tgz#7d229b1fcc637e466ca081180836a7aabff83f43" -to-fast-properties@^1.0.3: - version "1.0.3" - resolved "https://registry.yarnpkg.com/to-fast-properties/-/to-fast-properties-1.0.3.tgz#b83571fa4d8c25b82e231b06e3a3055de4ca1a47" +to-fast-properties@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/to-fast-properties/-/to-fast-properties-2.0.0.tgz#dc5e698cbd079265bc73e0377681a4e4e83f616e" + integrity sha1-3F5pjL0HkmW8c+A3doGk5Og/YW4= to-object-path@^0.3.0: version "0.3.0" resolved "https://registry.yarnpkg.com/to-object-path/-/to-object-path-0.3.0.tgz#297588b7b0e7e0ac08e04e672f85c1f4999e17af" + integrity sha1-KXWIt7Dn4KwI4E5nL4XB9JmeF68= dependencies: kind-of "^3.0.2" to-regex-range@^2.1.0: version "2.1.1" resolved "https://registry.yarnpkg.com/to-regex-range/-/to-regex-range-2.1.1.tgz#7c80c17b9dfebe599e27367e0d4dd5590141db38" + integrity sha1-fIDBe53+vlmeJzZ+DU3VWQFB2zg= dependencies: is-number "^3.0.0" repeat-string "^1.6.1" @@ -4093,23 +4360,17 @@ to-regex-range@^2.1.0: to-regex@^3.0.1, to-regex@^3.0.2: version "3.0.2" resolved "https://registry.yarnpkg.com/to-regex/-/to-regex-3.0.2.tgz#13cfdd9b336552f30b51f33a8ae1b42a7a7599ce" + integrity sha512-FWtleNAtZ/Ki2qtqej2CXTOayOH9bHDQF+Q48VpWyDXjbYxA4Yz8iDB31zXOBUlOHHKidDbqGVrTUvQMPmBGBw== dependencies: define-property "^2.0.2" extend-shallow "^3.0.2" regex-not "^1.0.2" safe-regex "^1.1.0" -tough-cookie@>=2.3.3: - version "3.0.1" - resolved "https://registry.yarnpkg.com/tough-cookie/-/tough-cookie-3.0.1.tgz#9df4f57e739c26930a018184887f4adb7dca73b2" - dependencies: - ip-regex "^2.1.0" - psl "^1.1.28" - punycode "^2.1.1" - -tough-cookie@^2.3.4: +tough-cookie@^2.3.3, tough-cookie@^2.3.4: version "2.5.0" resolved "https://registry.yarnpkg.com/tough-cookie/-/tough-cookie-2.5.0.tgz#cd9fb2a0aa1d5a12b473bd9fb96fa3dcff65ade2" + integrity sha512-nlLsUzgm1kfLXSXfRZMc1KLAugd4hqJHDTvc2hDIwS3mZAfMEuMbc03SujMF+GEcpaX/qboeycw6iO8JwVv2+g== dependencies: psl "^1.1.28" punycode "^2.1.1" @@ -4117,6 +4378,7 @@ tough-cookie@^2.3.4: tough-cookie@~2.4.3: version "2.4.3" resolved "https://registry.yarnpkg.com/tough-cookie/-/tough-cookie-2.4.3.tgz#53f36da3f47783b0925afa06ff9f3b165280f781" + integrity sha512-Q5srk/4vDM54WJsJio3XNn6K2sCG+CQ8G5Wz6bZhRZoAe/+TxjWB/GlFAnYEbkYVlON9FMk/fE3h2RLpPXo4lQ== dependencies: psl "^1.1.24" punycode "^1.4.1" @@ -4124,16 +4386,19 @@ tough-cookie@~2.4.3: tr46@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/tr46/-/tr46-1.0.1.tgz#a8b13fd6bfd2489519674ccde55ba3693b706d09" + integrity sha1-qLE/1r/SSJUZZ0zN5VujaTtwbQk= dependencies: punycode "^2.1.0" trim-right@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/trim-right/-/trim-right-1.0.1.tgz#cb2e1203067e0c8de1f614094b9fe45704ea6003" + integrity sha1-yy4SAwZ+DI3h9hQJS5/kVwTqYAM= -ts-jest@^23.10.5: - version "23.10.5" - resolved "https://registry.yarnpkg.com/ts-jest/-/ts-jest-23.10.5.tgz#cdb550df4466a30489bf70ba867615799f388dd5" +ts-jest@^24.0.2: + version "24.0.2" + resolved "https://registry.yarnpkg.com/ts-jest/-/ts-jest-24.0.2.tgz#8dde6cece97c31c03e80e474c749753ffd27194d" + integrity sha512-h6ZCZiA1EQgjczxq+uGLXQlNgeg02WWJBbeT8j6nyIBRQdglqbvzDoHahTEIiS6Eor6x8mK6PfZ7brQ9Q6tzHw== dependencies: bs-logger "0.x" buffer-from "1.x" @@ -4176,16 +4441,19 @@ tty-browserify@0.0.0: tunnel-agent@^0.6.0: version "0.6.0" resolved "https://registry.yarnpkg.com/tunnel-agent/-/tunnel-agent-0.6.0.tgz#27a5dea06b36b04a0a9966774b290868f0fc40fd" + integrity sha1-J6XeoGs2sEoKmWZ3SykIaPD8QP0= dependencies: safe-buffer "^5.0.1" tweetnacl@^0.14.3, tweetnacl@~0.14.0: version "0.14.5" resolved "https://registry.yarnpkg.com/tweetnacl/-/tweetnacl-0.14.5.tgz#5ae68177f192d4456269d108afa93ff8743f4f64" + integrity sha1-WuaBd/GS1EViadEIr6k/+HQ/T2Q= type-check@~0.3.2: version "0.3.2" resolved "https://registry.yarnpkg.com/type-check/-/type-check-0.3.2.tgz#5884cab512cf1d355e3fb784f30804b2b520db72" + integrity sha1-WITKtRLPHTVeP7eE8wgEsrUg23I= dependencies: prelude-ls "~1.1.2" @@ -4198,15 +4466,17 @@ typescript@^3.2.4: resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.2.4.tgz#c585cb952912263d915b462726ce244ba510ef3d" uglify-js@^3.1.4: - version "3.4.9" - resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.4.9.tgz#af02f180c1207d76432e473ed24a28f4a782bae3" + version "3.6.0" + resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.6.0.tgz#704681345c53a8b2079fb6cec294b05ead242ff5" + integrity sha512-W+jrUHJr3DXKhrsS7NUVxn3zqMOFn0hL/Ei6v0anCIMoKC93TjcflTagwIHLW7SfMFfiQuktQyFVCFHGUE0+yg== dependencies: - commander "~2.17.1" + commander "~2.20.0" source-map "~0.6.1" union-value@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/union-value/-/union-value-1.0.0.tgz#5c71c34cb5bad5dcebe3ea0cd08207ba5aa1aea4" + integrity sha1-XHHDTLW61dzr4+oM0IIHulqhrqQ= dependencies: arr-union "^3.1.0" get-value "^2.0.6" @@ -4228,6 +4498,7 @@ unique-slug@^2.0.0: unset-value@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/unset-value/-/unset-value-1.0.0.tgz#8376873f7d2335179ffb1e6fc3a8ed0dfc8ab559" + integrity sha1-g3aHP30jNRef+x5vw6jtDfyKtVk= dependencies: has-value "^0.3.1" isobject "^3.0.0" @@ -4239,12 +4510,14 @@ upath@^1.1.0: uri-js@^4.2.2: version "4.2.2" resolved "https://registry.yarnpkg.com/uri-js/-/uri-js-4.2.2.tgz#94c540e1ff772956e2299507c010aea6c8838eb0" + integrity sha512-KY9Frmirql91X2Qgjry0Wd4Y+YTdrdZheS8TFwvkbLWf/G5KNJDCh6pKL5OZctEW4+0Baa5idK2ZQuELRwPznQ== dependencies: punycode "^2.1.0" urix@^0.1.0: version "0.1.0" resolved "https://registry.yarnpkg.com/urix/-/urix-0.1.0.tgz#da937f7a62e21fec1fd18d49b35c2935067a6c72" + integrity sha1-2pN/emLiH+wf0Y1Js1wpNQZ6bHI= url@^0.11.0: version "0.11.0" @@ -4256,14 +4529,17 @@ url@^0.11.0: use@^3.1.0: version "3.1.1" resolved "https://registry.yarnpkg.com/use/-/use-3.1.1.tgz#d50c8cac79a19fbc20f2911f56eb973f4e10070f" + integrity sha512-cwESVXlO3url9YWlFW/TA9cshCEhtu7IKJ/p5soJ/gGpj7vbvFrAY/eIioQ6Dw23KjZhYgiIo8HOs1nQ2vr/oQ== util-deprecate@~1.0.1: version "1.0.2" resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf" + integrity sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8= util.promisify@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/util.promisify/-/util.promisify-1.0.0.tgz#440f7165a459c9a16dc145eb8e72f35687097030" + integrity sha512-i+6qA2MPhvoKLuxnJNpXAGhg7HphQOSUq2LKMZD0m15EiskXUkMvKdF4Uui0WYeCUGea+o2cw/ZuwehtfsrNkA== dependencies: define-properties "^1.1.2" object.getownpropertydescriptors "^2.0.3" @@ -4283,6 +4559,7 @@ util@^0.11.0: uuid@^3.3.2: version "3.3.2" resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.3.2.tgz#1b4af4955eb3077c501c23872fc6513811587131" + integrity sha512-yXJmeNaw3DnnKAOKJE51sL/ZaYfWJRl1pK9dr19YFCu0ObS231AB1/LbqTKRAQ5kw8A90rA6fr4riOUpTZvQZA== v8-compile-cache@^2.0.2: version "2.0.2" @@ -4291,6 +4568,7 @@ v8-compile-cache@^2.0.2: validate-npm-package-license@^3.0.1: version "3.0.4" resolved "https://registry.yarnpkg.com/validate-npm-package-license/-/validate-npm-package-license-3.0.4.tgz#fc91f6b9c7ba15c857f4cb2c5defeec39d4f410a" + integrity sha512-DpKm2Ui/xN7/HQKCtpZxoRWBhZ9Z0kqtygG8XCgNQ8ZlDnxuQmWhj566j8fN4Cu3/JmbhsDo7fcAJq4s9h27Ew== dependencies: spdx-correct "^3.0.0" spdx-expression-parse "^3.0.0" @@ -4298,6 +4576,7 @@ validate-npm-package-license@^3.0.1: verror@1.10.0: version "1.10.0" resolved "https://registry.yarnpkg.com/verror/-/verror-1.10.0.tgz#3a105ca17053af55d6e270c1f8288682e18da400" + integrity sha1-OhBcoXBTr1XW4nDB+CiGguGNpAA= dependencies: assert-plus "^1.0.0" core-util-is "1.0.2" @@ -4312,22 +4591,17 @@ vm-browserify@0.0.4: w3c-hr-time@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/w3c-hr-time/-/w3c-hr-time-1.0.1.tgz#82ac2bff63d950ea9e3189a58a65625fedf19045" + integrity sha1-gqwr/2PZUOqeMYmlimViX+3xkEU= dependencies: browser-process-hrtime "^0.1.2" -walker@~1.0.5: +walker@^1.0.7, walker@~1.0.5: version "1.0.7" resolved "https://registry.yarnpkg.com/walker/-/walker-1.0.7.tgz#2f7f9b8fd10d677262b18a884e28d19618e028fb" + integrity sha1-L3+bj9ENZ3JisYqITijRlhjgKPs= dependencies: makeerror "1.0.x" -watch@~0.18.0: - version "0.18.0" - resolved "https://registry.yarnpkg.com/watch/-/watch-0.18.0.tgz#28095476c6df7c90c963138990c0a5423eb4b986" - dependencies: - exec-sh "^0.2.0" - minimist "^1.2.0" - watchpack@^1.5.0: version "1.6.0" resolved "https://registry.yarnpkg.com/watchpack/-/watchpack-1.6.0.tgz#4bc12c2ebe8aa277a71f1d3f14d685c7b446cd00" @@ -4339,6 +4613,7 @@ watchpack@^1.5.0: webidl-conversions@^4.0.2: version "4.0.2" resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-4.0.2.tgz#a855980b1f0b6b359ba1d5d9fb39ae941faa63ad" + integrity sha512-YQ+BmxuTgd6UXZW3+ICGfyqRyHXVlD5GtQr5+qjiNW7bF0cqrzX500HVXPBOvgXb5YnzDd+h0zqyv61KUD7+Sg== webpack-cli@^3.2.3: version "3.2.3" @@ -4395,16 +4670,19 @@ webpack@^4.29.5: whatwg-encoding@^1.0.1, whatwg-encoding@^1.0.3: version "1.0.5" resolved "https://registry.yarnpkg.com/whatwg-encoding/-/whatwg-encoding-1.0.5.tgz#5abacf777c32166a51d085d6b4f3e7d27113ddb0" + integrity sha512-b5lim54JOPN9HtzvK9HFXvBma/rnfFeqsic0hSpjtDbVxR3dJKLc+KB4V6GgiGOvl7CY/KNh8rxSo9DKQrnUEw== dependencies: iconv-lite "0.4.24" whatwg-mimetype@^2.1.0, whatwg-mimetype@^2.2.0: version "2.3.0" resolved "https://registry.yarnpkg.com/whatwg-mimetype/-/whatwg-mimetype-2.3.0.tgz#3d4b1e0312d2079879f826aff18dbeeca5960fbf" + integrity sha512-M4yMwr6mAnQz76TbJm914+gPpB/nCwvZbJU28cUD6dR004SAxDLOOSUaB1JDRqLtaOV/vi0IC5lEAGFgrjGv/g== whatwg-url@^6.4.1: version "6.5.0" resolved "https://registry.yarnpkg.com/whatwg-url/-/whatwg-url-6.5.0.tgz#f2df02bff176fd65070df74ad5ccbb5a199965a8" + integrity sha512-rhRZRqx/TLJQWUpQ6bmrt2UV4f0HCQ463yQuONJqC6fO2VoEb1pTYddbe59SkYq87aoM5A3bdhMZiUiVws+fzQ== dependencies: lodash.sortby "^4.7.0" tr46 "^1.0.1" @@ -4413,6 +4691,7 @@ whatwg-url@^6.4.1: whatwg-url@^7.0.0: version "7.0.0" resolved "https://registry.yarnpkg.com/whatwg-url/-/whatwg-url-7.0.0.tgz#fde926fa54a599f3adf82dff25a9f7be02dc6edd" + integrity sha512-37GeVSIJ3kn1JgKyjiYNmSLP1yzbpb29jdmwBSgkD9h40/hyrR/OifpVUndji3tmwGgD8qpw7iQu3RSbCrBpsQ== dependencies: lodash.sortby "^4.7.0" tr46 "^1.0.1" @@ -4421,8 +4700,9 @@ whatwg-url@^7.0.0: which-module@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/which-module/-/which-module-2.0.0.tgz#d9ef07dce77b9902b8a3a8fa4b31c3e3f7e6e87a" + integrity sha1-2e8H3Od7mQK4o6j6SzHD4/fm6Ho= -which@^1.2.12, which@^1.2.14, which@^1.2.9, which@^1.3.0: +which@^1.2.14, which@^1.2.9, which@^1.3.0: version "1.3.1" resolved "https://registry.yarnpkg.com/which/-/which-1.3.1.tgz#a45043d54f5805316da8d62f9f50918d3da70b0a" dependencies: @@ -4431,16 +4711,19 @@ which@^1.2.12, which@^1.2.14, which@^1.2.9, which@^1.3.0: wide-align@^1.1.0: version "1.1.3" resolved "https://registry.yarnpkg.com/wide-align/-/wide-align-1.1.3.tgz#ae074e6bdc0c14a431e804e624549c633b000457" + integrity sha512-QGkOQc8XL6Bt5PwnsExKBPuMKBxnGxWWW3fU55Xt4feHozMUhdUMaBCk290qpm/wG5u/RSKzwdAC4i51YigihA== dependencies: string-width "^1.0.2 || 2" wordwrap@~0.0.2: version "0.0.3" resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-0.0.3.tgz#a3d5da6cd5c0bc0008d37234bbaf1bed63059107" + integrity sha1-o9XabNXAvAAI03I0u68b7WMFkQc= wordwrap@~1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-1.0.0.tgz#27584810891456a4171c8d0226441ade90cbcaeb" + integrity sha1-J1hIEIkUVqQXHI0CJkQa3pDLyus= worker-farm@^1.5.2: version "1.6.0" @@ -4451,6 +4734,7 @@ worker-farm@^1.5.2: wrap-ansi@^2.0.0: version "2.1.0" resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-2.1.0.tgz#d8fc3d284dd05794fe84973caecdd1cf824fdd85" + integrity sha1-2Pw9KE3QV5T+hJc8rs3Rz4JP3YU= dependencies: string-width "^1.0.1" strip-ansi "^3.0.1" @@ -4458,10 +4742,12 @@ wrap-ansi@^2.0.0: wrappy@1: version "1.0.2" resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f" + integrity sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8= -write-file-atomic@^2.1.0: - version "2.4.2" - resolved "https://registry.yarnpkg.com/write-file-atomic/-/write-file-atomic-2.4.2.tgz#a7181706dfba17855d221140a9c06e15fcdd87b9" +write-file-atomic@2.4.1: + version "2.4.1" + resolved "https://registry.yarnpkg.com/write-file-atomic/-/write-file-atomic-2.4.1.tgz#d0b05463c188ae804396fd5ab2a370062af87529" + integrity sha512-TGHFeZEZMnv+gBFRfjAcxL5bPHrsGKtnb4qsFAws7/vlh+QfwAaySIw4AXP9ZskTTh5GWu3FLuJhsWVdiJPGvg== dependencies: graceful-fs "^4.1.11" imurmurhash "^0.1.4" @@ -4470,32 +4756,27 @@ write-file-atomic@^2.1.0: ws@^5.2.0: version "5.2.2" resolved "https://registry.yarnpkg.com/ws/-/ws-5.2.2.tgz#dffef14866b8e8dc9133582514d1befaf96e980f" + integrity sha512-jaHFD6PFv6UgoIVda6qZllptQsMlDEJkTQcybzzXDYM1XO9Y8em691FGMPmM46WGyLU4z9KMgQN+qrux/nhlHA== dependencies: async-limiter "~1.0.0" xml-name-validator@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/xml-name-validator/-/xml-name-validator-3.0.0.tgz#6ae73e06de4d8c6e47f9fb181f78d648ad457c6a" + integrity sha512-A5CUptxDsvxKJEU3yO6DuWBSJz/qizqzJKOMIfUJHETbBw/sFaDxgd6fxm1ewUaM0jZ444Fc5vC5ROYurg/4Pw== xtend@^4.0.0, xtend@~4.0.1: version "4.0.1" resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.1.tgz#a5c6d532be656e23db820efb943a1f04998d63af" -y18n@^3.2.1: - version "3.2.1" - resolved "https://registry.yarnpkg.com/y18n/-/y18n-3.2.1.tgz#6d15fba884c08679c0d77e88e7759e811e07fa41" - "y18n@^3.2.1 || ^4.0.0", y18n@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/y18n/-/y18n-4.0.0.tgz#95ef94f85ecc81d007c264e190a120f0a3c8566b" -yallist@^2.1.2: - version "2.1.2" - resolved "https://registry.yarnpkg.com/yallist/-/yallist-2.1.2.tgz#1c11f9218f076089a47dd512f93c6699a6a81d52" - -yallist@^3.0.0, yallist@^3.0.2: +yallist@^3.0.0, yallist@^3.0.2, yallist@^3.0.3: version "3.0.3" resolved "https://registry.yarnpkg.com/yallist/-/yallist-3.0.3.tgz#b4b049e314be545e3ce802236d6cd22cd91c3de9" + integrity sha512-S+Zk8DEWE6oKpV+vI3qWkaK+jSbIK86pCwe2IF/xwIpQ8jEuxpw9NyaGjmp9+BoJv5FV2piqCDcoCtStppiq2A== yargs-parser@10.x: version "10.1.0" @@ -4510,30 +4791,7 @@ yargs-parser@^11.1.1: camelcase "^5.0.0" decamelize "^1.2.0" -yargs-parser@^9.0.2: - version "9.0.2" - resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-9.0.2.tgz#9ccf6a43460fe4ed40a9bb68f48d43b8a68cc077" - dependencies: - camelcase "^4.1.0" - -yargs@^11.0.0: - version "11.1.0" - resolved "https://registry.yarnpkg.com/yargs/-/yargs-11.1.0.tgz#90b869934ed6e871115ea2ff58b03f4724ed2d77" - dependencies: - cliui "^4.0.0" - decamelize "^1.1.1" - find-up "^2.1.0" - get-caller-file "^1.0.1" - os-locale "^2.0.0" - require-directory "^2.1.1" - require-main-filename "^1.0.1" - set-blocking "^2.0.0" - string-width "^2.0.0" - which-module "^2.0.0" - y18n "^3.2.1" - yargs-parser "^9.0.2" - -yargs@^12.0.4: +yargs@^12.0.2, yargs@^12.0.4: version "12.0.5" resolved "https://registry.yarnpkg.com/yargs/-/yargs-12.0.5.tgz#05f5997b609647b64f66b81e3b4b10a368e7ad13" dependencies: From dd517de37cbe6598a39faa35cd5e05a68caae785 Mon Sep 17 00:00:00 2001 From: Andy Coenen Date: Wed, 5 Jun 2019 15:50:51 -0700 Subject: [PATCH 20/46] Update docs to show JS vs TS syntax --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 256d815..27ed1bc 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ yarn add umap-js #### Synchronous fitting -```typescript +```javascript import { UMAP } from 'umap-js'; const umap = new UMAP(); @@ -30,7 +30,7 @@ const embedding = umap.fit(data); #### Asynchronous fitting -```typescript +```javascript import { UMAP } from 'umap-js'; const umap = new UMAP(); @@ -39,7 +39,7 @@ const embedding = await umap.fitAsync(data, callback); #### Step-by-step fitting -```typescript +```javascript import { UMAP } from 'umap-js'; const umap = new UMAP(); @@ -52,7 +52,7 @@ const embedding = umap.getEmbedding(); #### Supervised projection using labels -```typescript +```javascript import { UMAP } from 'umap-js'; const umap = new UMAP(); @@ -62,7 +62,7 @@ const embedding = umap.fit(data); #### Transforming additional points after fitting -```typescript +```javascript import { UMAP } from 'umap-js'; const umap = new UMAP(); From 239a275340538f3b08c3160ee860d56a41bd1a2c Mon Sep 17 00:00:00 2001 From: Andy Coenen Date: Wed, 5 Jun 2019 15:57:45 -0700 Subject: [PATCH 21/46] Update travis build icon --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 256d815..0db3485 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -[![Build Status](https://travis-ci.com/PAIR-code/umap-js.svg?branch=master)](https://travis-ci.org/PAIR-code/umap-js) +[![Build Status](https://travis-ci.com/PAIR-code/umap-js.svg?branch=master)](https://travis-ci.com/PAIR-code/umap-js.svg?branch=master) # UMAP-JS From 7353250bdddbcf4f38e40b11ebc46e255d579d1b Mon Sep 17 00:00:00 2001 From: Andy Coenen Date: Wed, 5 Jun 2019 15:59:43 -0700 Subject: [PATCH 22/46] Upgrade version and build --- lib/umap-js.js | 52 +++++++++++++--------------------------------- lib/umap-js.min.js | 2 +- package.json | 2 +- 3 files changed, 17 insertions(+), 39 deletions(-) diff --git a/lib/umap-js.js b/lib/umap-js.js index 78a544e..78206c0 100644 --- a/lib/umap-js.js +++ b/lib/umap-js.js @@ -117,34 +117,11 @@ var __values = (this && this.__values) || function (o) { }; }; Object.defineProperty(exports, "__esModule", { value: true }); -function generateGaussian(mean, std, rng) { - if (rng === void 0) { rng = Math.random; } - var u1 = tauRand(rng); - var u2 = tauRand(rng); - var z0 = Math.sqrt(-2.0 * Math.log(u1)) * Math.cos(Math.PI * 2 * u2); - return z0 * std + mean; -} -function randomNormal2d(mean, stdev, size, rng) { - if (mean === void 0) { mean = 0; } - if (stdev === void 0) { stdev = 1; } - if (size === void 0) { size = [1, 1]; } - if (rng === void 0) { rng = Math.random; } - return Array(size[0]) - .fill(0) - .map(function () { - return Array(size[1]) - .fill(0) - .map(function () { return generateGaussian(mean, stdev, rng); }); - }); -} -exports.randomNormal2d = randomNormal2d; function tauRandInt(n, random) { - if (random === void 0) { random = Math.random; } return Math.floor(random() * n); } exports.tauRandInt = tauRandInt; function tauRand(random) { - if (random === void 0) { random = Math.random; } return random(); } exports.tauRand = tauRand; @@ -223,12 +200,12 @@ function max2d(input) { return max; } exports.max2d = max2d; -function rejectionSample(nSamples, poolSize) { +function rejectionSample(nSamples, poolSize, random) { var result = zeros(nSamples); for (var i = 0; i < nSamples; i++) { var rejectSample = true; while (rejectSample) { - var j = tauRandInt(poolSize); + var j = tauRandInt(poolSize, random); var broken = false; for (var k = 0; k < i; k++) { if (j === result[k]) { @@ -1015,13 +992,13 @@ function makeLeafArray(rpForest) { } } exports.makeLeafArray = makeLeafArray; -function selectSide(hyperplane, offset, point) { +function selectSide(hyperplane, offset, point, random) { var margin = offset; for (var d = 0; d < point.length; d++) { margin += hyperplane[d] * point[d]; } if (margin === 0) { - var side = utils.tauRandInt(2); + var side = utils.tauRandInt(2, random); return side; } else if (margin > 0) { @@ -1031,10 +1008,10 @@ function selectSide(hyperplane, offset, point) { return 1; } } -function searchFlatTree(point, tree) { +function searchFlatTree(point, tree, random) { var node = 0; while (tree.children[node][0] > 0) { - var side = selectSide(tree.hyperplanes[node], tree.offsets[node], point); + var side = selectSide(tree.hyperplanes[node], tree.offsets[node], point, random); if (side === 0) { node = tree.children[node][0]; } @@ -1167,6 +1144,7 @@ var UMAP = (function () { if (params[key] !== undefined) _this[key] = params[key]; }; + setParam('distanceFn'); setParam('learningRate'); setParam('localConnectivity'); setParam('minDist'); @@ -1266,7 +1244,7 @@ var UMAP = (function () { throw new Error('No data has been fit.'); } var nNeighbors = Math.floor(this.nNeighbors * this.transformQueueSize); - var init = nnDescent.initializeSearch(this.rpForest, rawData, toTransform, nNeighbors, this.initFromRandom, this.initFromTree); + var init = nnDescent.initializeSearch(this.rpForest, rawData, toTransform, nNeighbors, this.initFromRandom, this.initFromTree, this.random); var result = this.search(rawData, this.searchGraph, init, toTransform); var _a = heap.deheapSort(result), indices = _a.indices, distances = _a.weights; indices = indices.map(function (x) { return x.slice(0, _this.nNeighbors); }); @@ -1907,9 +1885,9 @@ function makeNNDescent(distanceFn, random) { } exports.makeNNDescent = makeNNDescent; function makeInitializations(distanceFn) { - function initFromRandom(nNeighbors, data, queryPoints, _heap) { + function initFromRandom(nNeighbors, data, queryPoints, _heap, random) { for (var i = 0; i < queryPoints.length; i++) { - var indices = utils.rejectionSample(nNeighbors, data.length); + var indices = utils.rejectionSample(nNeighbors, data.length, random); for (var j = 0; j < indices.length; j++) { if (indices[j] < 0) { continue; @@ -1919,9 +1897,9 @@ function makeInitializations(distanceFn) { } } } - function initFromTree(_tree, data, queryPoints, _heap) { + function initFromTree(_tree, data, queryPoints, _heap, random) { for (var i = 0; i < queryPoints.length; i++) { - var indices = tree.searchFlatTree(queryPoints[i], _tree); + var indices = tree.searchFlatTree(queryPoints[i], _tree, random); for (var j = 0; j < indices.length; j++) { if (indices[j] < 0) { return; @@ -1973,15 +1951,15 @@ function makeInitializedNNSearch(distanceFn) { }; } exports.makeInitializedNNSearch = makeInitializedNNSearch; -function initializeSearch(forest, data, queryPoints, nNeighbors, initFromRandom, initFromTree) { +function initializeSearch(forest, data, queryPoints, nNeighbors, initFromRandom, initFromTree, random) { var e_2, _a; var results = heap.makeHeap(queryPoints.length, nNeighbors); - initFromRandom(nNeighbors, data, queryPoints, results); + initFromRandom(nNeighbors, data, queryPoints, results, random); if (forest) { try { for (var forest_1 = __values(forest), forest_1_1 = forest_1.next(); !forest_1_1.done; forest_1_1 = forest_1.next()) { var tree_1 = forest_1_1.value; - initFromTree(tree_1, data, queryPoints, results); + initFromTree(tree_1, data, queryPoints, results, random); } } catch (e_2_1) { e_2 = { error: e_2_1 }; } diff --git a/lib/umap-js.min.js b/lib/umap-js.min.js index b18fff0..0770fbb 100644 --- a/lib/umap-js.min.js +++ b/lib/umap-js.min.js @@ -1 +1 @@ -!function(t){var r={};function e(n){if(r[n])return r[n].exports;var i=r[n]={i:n,l:!1,exports:{}};return t[n].call(i.exports,i,i.exports,e),i.l=!0,i.exports}e.m=t,e.c=r,e.d=function(t,r,n){e.o(t,r)||Object.defineProperty(t,r,{enumerable:!0,get:n})},e.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},e.t=function(t,r){if(1&r&&(t=e(t)),8&r)return t;if(4&r&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(e.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&r&&"string"!=typeof t)for(var i in t)e.d(n,i,function(r){return t[r]}.bind(null,i));return n},e.n=function(t){var r=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(r,"a",r),r},e.o=function(t,r){return Object.prototype.hasOwnProperty.call(t,r)},e.p="",e(e.s=5)}([function(t,r,e){"use strict";const n=Object.prototype.toString;t.exports=function(t){return n.call(t).endsWith("Array]")}},function(t,r,e){"use strict";var n=this&&this.__values||function(t){var r="function"==typeof Symbol&&t[Symbol.iterator],e=0;return r?r.call(t):{next:function(){return t&&e>=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}};function i(t,r){return void 0===r&&(r=Math.random),Math.floor(r()*t)}function o(t){return void 0===t&&(t=Math.random),t()}function s(t){for(var r=[],e=0;er?t[e]:r;return r},r.max2d=function(t){for(var r=0,e=0;er?t[e][n]:r;return r},r.rejectionSample=function(t,r){for(var e=h(t),n=0;n=s[0])return 0;for(var h=0;h=s[0])return 0;s[0]=e,o[0]=n,a[0]=i;for(var h=0,u=0;;){var l=2*h+1,c=l+1,f=t[0][0].length;if(l>=f)break;if(c>=f){if(!(s[l]>e))break;u=l}else if(s[l]>=s[c]){if(!(ee.length;a++)1===i[a]&&n[a]=0?(i[s]=0,Math.floor(e[s])):-1}},function(t,r,e){"use strict";var n,i=this&&this.__read||function(t,r){var e="function"==typeof Symbol&&t[Symbol.iterator];if(!e)return t;var n,i,o=e.call(t),s=[];try{for(;(void 0===r||r-- >0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},o=this&&this.__spread||function(){for(var t=[],r=0;r=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}},a=this&&this.__importStar||function(t){if(t&&t.__esModule)return t;var r={};if(null!=t)for(var e in t)Object.hasOwnProperty.call(t,e)&&(r[e]=t[e]);return r.default=t,r};Object.defineProperty(r,"__esModule",{value:!0});var h=a(e(1)),u=function(){function t(t,r,e,n){this.entries=new Map,this.nRows=0,this.nCols=0,this.rows=o(t),this.cols=o(r),this.values=o(e);for(var i=0;ir?t:r})},r.multiplyScalar=function(t,r){return t.map(function(t){return t*r})},r.eliminateZeros=function(t){for(var r=new Set,e=t.getValues(),n=t.getRows(),i=t.getCols(),o=0;or?t[e]:r;return t.map(function(t){return t/r})},n.l1=function(t){for(var r=0,e=0;e0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},i=this&&this.__spread||function(){for(var t=[],r=0;r=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}},s=this&&this.__importStar||function(t){if(t&&t.__esModule)return t;var r={};if(null!=t)for(var e in t)Object.hasOwnProperty.call(t,e)&&(r[e]=t[e]);return r.default=t,r};Object.defineProperty(r,"__esModule",{value:!0});var a=s(e(1)),h=function(){return function(t,r,e,n){this.hyperplanes=t,this.offsets=r,this.children=e,this.indices=n}}();function u(t,r,e){for(var n=r,i=0;i0?0:1}r.FlatTree=h,r.makeForest=function(t,r,e,n){var o=Math.max(10,r);return a.range(e).map(function(r,e){return function(t,r,e,n){void 0===r&&(r=30);var i=a.range(t.length);return function t(r,e,n,i,o){if(void 0===n&&(n=30),e.length>n){var s=function(t,r,e){var n=t[0].length,i=a.tauRandInt(r.length,e),o=a.tauRandInt(r.length,e);o=(o+=i===o?1:0)%r.length;for(var s=r[i],h=r[o],u=0,l=a.zeros(n),c=0;c0?(g[c]=0,f+=1):(g[c]=1,m+=1)}var d=a.zeros(f),w=a.zeros(m);for(var c in f=0,m=0,a.range(g.length))0===g[c]?(d[f]=r[c],f+=1):(w[m]=r[c],m+=1);return{indicesLeft:d,indicesRight:w,hyperplane:l,offset:u}}(r,e,o),h=s.indicesLeft,u=s.indicesRight,l=s.hyperplane,c=s.offset,f=t(r,h,n,i+1,o),m=t(r,u,n,i+1,o),g={leftChild:f,rightChild:m,isLeaf:!1,hyperplane:l,offset:c};return g}var g={indices:e,isLeaf:!0};return g}(t,i,r,e,n)}(t,o,e,n)}).map(function(t){return function(t,r){var e=function t(r){return r.isLeaf?1:1+t(r.leftChild)+t(r.rightChild)}(t),n=function t(r){return r.isLeaf?1:t(r.leftChild)+t(r.rightChild)}(t),o=a.range(e).map(function(){return a.zeros(t.hyperplane.length)}),s=a.zeros(e),u=a.range(e).map(function(){return[-1,-1]}),l=a.range(n).map(function(){return a.range(r).map(function(){return-1})});return function t(r,e,n,o,s,a,h){var u;if(r.isLeaf)return o[a][0]=-h,(u=s[h]).splice.apply(u,i([0,r.indices.length],r.indices)),{nodeNum:a,leafNum:h+=1};e[a]=r.hyperplane,n[a]=r.offset,o[a][0]=a+1;var l=a,c=t(r.leftChild,e,n,o,s,a+1,h);return a=c.nodeNum,h=c.leafNum,o[l][1]=a+1,{nodeNum:(c=t(r.rightChild,e,n,o,s,a+1,h)).nodeNum,leafNum:c.leafNum}}(t,o,s,u,l,0,0),new h(o,s,u,l)}(t,o)})},r.makeLeafArray=function(t){var r,e;if(t.length>0){var n=[];try{for(var s=o(t),a=s.next();!a.done;a=s.next()){var h=a.value;n.push.apply(n,i(h.indices))}}catch(t){r={error:t}}finally{try{a&&!a.done&&(e=s.return)&&e.call(s)}finally{if(r)throw r.error}}return n}return[[-1]]},r.searchFlatTree=function(t,r){for(var e=0;r.children[e][0]>0;)e=0===u(r.hyperplanes[e],r.offsets[e],t)?r.children[e][0]:r.children[e][1];var n=-1*r.children[e][0];return r.indices[n]}},function(t,r,e){"use strict";Object.defineProperty(r,"__esModule",{value:!0});var n=e(6);window.UMAP=n.UMAP},function(t,r,e){"use strict";var n=this&&this.__awaiter||function(t,r,e,n){return new(e||(e=Promise))(function(i,o){function s(t){try{h(n.next(t))}catch(t){o(t)}}function a(t){try{h(n.throw(t))}catch(t){o(t)}}function h(t){t.done?i(t.value):new e(function(r){r(t.value)}).then(s,a)}h((n=n.apply(t,r||[])).next())})},i=this&&this.__generator||function(t,r){var e,n,i,o,s={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function a(o){return function(a){return function(o){if(e)throw new TypeError("Generator is already executing.");for(;s;)try{if(e=1,n&&(i=2&o[0]?n.return:o[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,o[1])).done)return i;switch(n=0,i&&(o=[2&o[0],i.value]),o[0]){case 0:case 1:i=o;break;case 4:return s.label++,{value:o[1],done:!1};case 5:s.label++,n=o[1],o=[0];continue;case 7:o=s.ops.pop(),s.trys.pop();continue;default:if(!(i=(i=s.trys).length>0&&i[i.length-1])&&(6===o[0]||2===o[0])){s=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},s=this&&this.__spread||function(){for(var t=[],r=0;r0&&i.set(o,u,c)}var f=l.transpose(i);return l.maximum(i,f)},t.prototype.transform=function(t){var r=this,e=this.X;if(void 0===e||0===e.length)throw new Error("No data has been fit.");var n=Math.floor(this.nNeighbors*this.transformQueueSize),i=c.initializeSearch(this.rpForest,e,t,n,this.initFromRandom,this.initFromTree),o=this.search(e,this.searchGraph,i,t),s=u.deheapSort(o),a=s.indices,h=s.weights;a=a.map(function(t){return t.slice(0,r.nNeighbors)}),h=h.map(function(t){return t.slice(0,r.nNeighbors)});var f=Math.max(0,this.localConnectivity-1),g=this.smoothKNNDistance(h,this.nNeighbors,f),p=g.sigmas,v=g.rhos,d=this.computeMembershipStrengths(a,h,p,v),w=d.rows,y=d.cols,b=d.vals,M=[t.length,e.length],x=new l.SparseMatrix(w,y,b,M),E=l.normalize(x,"l1"),R=l.getCSR(E),k=t.length,z=S(m.reshape2d(R.indices,k,this.nNeighbors),m.reshape2d(R.values,k,this.nNeighbors),this.embedding),N=this.nEpochs?this.nEpochs/3:x.nRows<=1e4?100:30,A=x.getValues().reduce(function(t,r){return r>t?r:t},0);x=x.map(function(t){return t0});if(g.length>=e){var p=Math.floor(e),v=e-p;p>0?(s[h]=g[p-1],v>1e-5&&(s[h]+=v*(g[p]-g[p-1]))):s[h]=v*g[0]}else g.length>0&&(s[h]=m.max(g));for(var d=0;d0?Math.exp(-b/c):1}if(Math.abs(w-o)<1e-5)break;w>o?c=(u+(l=c))/2:(u=c,l===1/0?c*=2:c=(u+l)/2)}if(a[h]=c,s[h]>0){var M=m.mean(f);a[h]<.001*M&&(a[h]=.001*M)}else{var x=m.mean(t.map(m.mean));a[h]<.001*x&&(a[h]=.001*x)}}return{sigmas:a,rhos:s}},t.prototype.computeMembershipStrengths=function(t,r,e,n){for(var i=t.length,o=t[0].length,s=m.zeros(i*o),a=m.zeros(i*o),h=m.zeros(i*o),u=0;u0&&(e[n]=r/i[n])}),e},t.prototype.assignOptimizationStateParameters=function(t){Object.assign(this.optimizationState,t)},t.prototype.prepareForOptimizationLoop=function(){var t=this.repulsionStrength,r=this.learningRate,e=this.negativeSampleRate,n=this.optimizationState,i=n.epochsPerSample,o=n.headEmbedding,a=n.tailEmbedding,h=o[0].length,u=o.length===a.length,l=i.map(function(t){return t/e}),c=s(l),f=s(i);this.assignOptimizationStateParameters({epochOfNextSample:f,epochOfNextNegativeSample:c,epochsPerNegativeSample:l,moveOther:u,initialAlpha:r,alpha:r,gamma:t,dim:h})},t.prototype.initializeOptimization=function(){var t=this.embedding,r=this.embedding,e=this.optimizationState,n=e.head,i=e.tail,o=e.epochsPerSample,s=this.getNEpochs(),a=this.graph.nCols,h=b(this.spread,this.minDist),u=h.a,l=h.b;this.assignOptimizationStateParameters({headEmbedding:t,tailEmbedding:r,head:n,tail:i,epochsPerSample:o,a:u,b:l,nEpochs:s,nVertices:a})},t.prototype.optimizeLayoutStep=function(t){for(var r=this.optimizationState,e=r.head,n=r.tail,i=r.headEmbedding,o=r.tailEmbedding,s=r.epochsPerSample,a=r.epochOfNextSample,h=r.epochOfNextNegativeSample,u=r.epochsPerNegativeSample,l=r.moveOther,c=r.initialAlpha,f=r.alpha,g=r.gamma,p=r.a,v=r.b,d=r.dim,b=r.nEpochs,M=r.nVertices,x=0;xt)){var S=e[x],E=n[x],R=i[S],k=o[E],z=y(R,k),N=0;z>0&&(N=-2*p*v*Math.pow(z,v-1),N/=p*Math.pow(z,v)+1);for(var A=0;A0)O=2*g*v,O/=(.001+j)*(p*Math.pow(j,v)+1);else if(S===P)continue;for(A=0;A0&&(V=w(O*(R[A]-I[A]),4)),R[A]+=V*f}}h[x]+=C*u[x]}return r.alpha=c*(1-t/b),r.currentEpoch+=1,i},t.prototype.optimizeLayoutAsync=function(t){var r=this;return void 0===t&&(t=function(){return!0}),new Promise(function(e,o){var s=function(){return n(r,void 0,void 0,function(){var r,n,a,h,u,l;return i(this,function(i){try{if(r=this.optimizationState,n=r.nEpochs,a=r.currentEpoch,this.embedding=this.optimizeLayoutStep(a),h=this.optimizationState.currentEpoch,u=!1===t(h),l=h===n,u||l)return[2,e(l)];s()}catch(t){o(t)}return[2]})})};s()})},t.prototype.optimizeLayout=function(t){void 0===t&&(t=function(){return!0});for(var r=!1,e=[];!r;){var n=this.optimizationState,i=n.nEpochs,o=n.currentEpoch;e=this.optimizeLayoutStep(o);var s=this.optimizationState.currentEpoch,a=!1===t(s);r=s===i||a}return e},t.prototype.getNEpochs=function(){var t=this.graph;if(this.nEpochs>0)return this.nEpochs;var r=t.nRows;return r<=2500?500:r<=5e3?400:r<=7500?300:200},t}();function v(t,r){for(var e=0,n=0;nr?r:t<-r?-r:t}function y(t,r){for(var e=0,n=0;n=r?Math.exp(-(e[i]-r)/t):n}),i={x:e,y:n},s={damping:1.5,initialValues:[.5,.5],gradientDifference:.1,maxIterations:100,errorTolerance:.01},a=g.default(i,function(t){var r=o(t,2),e=r[0],n=r[1];return function(t){return 1/(1+e*Math.pow(t,2*n))}},s).parameterValues,h=o(a,2);return{a:h[0],b:h[1]}}function M(t,r,e,n){return void 0===e&&(e=1),void 0===n&&(n=5),t.map(function(t,i,o){return-1===r[i]||-1===r[o]?t*Math.exp(-e):r[i]!==r[o]?t*Math.exp(-n):t})}function x(t){t=l.normalize(t,"max");var r=l.transpose(t),e=l.pairwiseMultiply(r,t);return t=l.add(t,l.subtract(r,e)),l.eliminateZeros(t)}function S(t,r,e){for(var n=m.zeros(t.length).map(function(t){return m.zeros(e[0].length)}),i=0;i=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}},i=this&&this.__importStar||function(t){if(t&&t.__esModule)return t;var r={};if(null!=t)for(var e in t)Object.hasOwnProperty.call(t,e)&&(r[e]=t[e]);return r.default=t,r};Object.defineProperty(r,"__esModule",{value:!0});var o=i(e(2)),s=i(e(3)),a=i(e(4)),h=i(e(1));r.makeNNDescent=function(t,r){return function(e,n,i,s,a,u,l,c){void 0===s&&(s=10),void 0===a&&(a=50),void 0===u&&(u=.001),void 0===l&&(l=.5),void 0===c&&(c=!0);for(var f=e.length,m=o.makeHeap(e.length,i),g=0;gr&&(r=t[e]);return r};var s=function(t){if(!i()(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");for(var r=t[0],e=1;e1&&void 0!==arguments[1]?arguments[1]:{};if(!i()(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");if(void 0!==e.output){if(!i()(e.output))throw new TypeError("output option must be an array if specified");r=e.output}else r=new Array(t.length);var n=s(t),a=o(t);if(n===a)throw new RangeError("minimum and maximum input values are equal. Cannot rescale a constant array");var h=e.min,u=void 0===h?e.autoMinMax?n:0:h,l=e.max,c=void 0===l?e.autoMinMax?a:1:l;if(u>=c)throw new RangeError("min option must be smaller than max option");for(var f=(c-u)/(a-n),m=0;mMath.abs(h[i])&&(i=r);if(i!==e){for(n=0;n=0;i--){for(n=0;no?t[i][o]:i===o?1:0;return n}get upperTriangularMatrix(){for(var t=this.LU,r=t.rows,e=t.columns,n=new _(r,e),i=0;iMath.abs(r)?(e=r/t,Math.abs(t)*Math.sqrt(1+e*e)):0!==r?(e=t/r,Math.abs(r)*Math.sqrt(1+e*e)):0}function l(t,r,e){for(var n=new Array(t),i=0;i=0;t--)if(0!==v[t]){for(let r=t+1;r=0;t--){if(t0;){let t,r;for(t=R-2;t>=-1&&-1!==t;t--){const r=Number.MIN_VALUE+z*Math.abs(v[t]+Math.abs(v[t+1]));if(Math.abs(y[t])<=r||Number.isNaN(y[t])){y[t]=0;break}}if(t===R-2)r=4;else{let e;for(e=R-1;e>=t&&e!==t;e--){let r=(e!==R?Math.abs(y[e]):0)+(e!==t+1?Math.abs(y[e-1]):0);if(Math.abs(v[e])<=z*r){v[e]=0;break}}e===t?r=3:e===R-1?r=1:(r=2,t=e)}switch(t++,r){case 1:{let r=y[R-2];y[R-2]=0;for(let e=R-2;e>=t;e--){let i=u(v[e],r),o=v[e]/i,s=r/i;if(v[e]=i,e!==t&&(r=-s*y[e-1],y[e-1]=o*y[e-1]),c)for(let t=0;t=v[t+1]);){let r=v[t];if(v[t]=v[t+1],v[t+1]=r,c&&tr?i[o][e]=t[o][e]/this.s[e]:i[o][e]=0;var o=this.U,s=o.length,a=o[0].length,h=new _(e,s);for(let t=0;tt&&r++;return r}get diagonal(){return this.s}get threshold(){return Number.EPSILON/2*Math.max(this.m,this.n)*this.s[0]}get leftSingularVectors(){return _.isMatrix(this.U)||(this.U=new _(this.U)),this.U}get rightSingularVectors(){return _.isMatrix(this.V)||(this.V=new _(this.V)),this.V}get diagonalMatrix(){return _.diag(this.s)}}function f(t,r,e){var n=e?t.rows:t.rows-1;if(r<0||r>n)throw new RangeError("Row index out of range")}function m(t,r,e){var n=e?t.columns:t.columns-1;if(r<0||r>n)throw new RangeError("Column index out of range")}function g(t,r){if(r.to1DArray&&(r=r.to1DArray()),r.length!==t.columns)throw new RangeError("vector size must be the same as the number of columns");return r}function p(t,r){if(r.to1DArray&&(r=r.to1DArray()),r.length!==t.rows)throw new RangeError("vector size must be the same as the number of rows");return r}function v(t,r,e){return{row:d(t,r),column:w(t,e)}}function d(t,r){if("object"!=typeof r)throw new TypeError("unexpected type for row indices");if(r.some(r=>r<0||r>=t.rows))throw new RangeError("row indices are out of range");return Array.isArray(r)||(r=Array.from(r)),r}function w(t,r){if("object"!=typeof r)throw new TypeError("unexpected type for column indices");if(r.some(r=>r<0||r>=t.columns))throw new RangeError("column indices are out of range");return Array.isArray(r)||(r=Array.from(r)),r}function y(t,r,e,n,i){if(5!==arguments.length)throw new RangeError("expected 4 arguments");if(b("startRow",r),b("endRow",e),b("startColumn",n),b("endColumn",i),r>e||n>i||r<0||r>=t.rows||e<0||e>=t.rows||n<0||n>=t.columns||i<0||i>=t.columns)throw new RangeError("Submatrix indices are out of range")}function b(t,r){if("number"!=typeof r)throw new TypeError(`${t} must be a number`)}class M extends(C()){constructor(t,r,e){super(),this.matrix=t,this.rows=r,this.columns=e}static get[Symbol.species](){return _}}class x extends M{constructor(t){super(t,t.columns,t.rows)}set(t,r,e){return this.matrix.set(r,t,e),this}get(t,r){return this.matrix.get(r,t)}}class S extends M{constructor(t,r){super(t,1,t.columns),this.row=r}set(t,r,e){return this.matrix.set(this.row,r,e),this}get(t,r){return this.matrix.get(this.row,r)}}class E extends M{constructor(t,r,e,n,i){y(t,r,e,n,i),super(t,e-r+1,i-n+1),this.startRow=r,this.startColumn=n}set(t,r,e){return this.matrix.set(this.startRow+t,this.startColumn+r,e),this}get(t,r){return this.matrix.get(this.startRow+t,this.startColumn+r)}}class R extends M{constructor(t,r,e){var n=v(t,r,e);super(t,n.row.length,n.column.length),this.rowIndices=n.row,this.columnIndices=n.column}set(t,r,e){return this.matrix.set(this.rowIndices[t],this.columnIndices[r],e),this}get(t,r){return this.matrix.get(this.rowIndices[t],this.columnIndices[r])}}class k extends M{constructor(t,r){super(t,(r=d(t,r)).length,t.columns),this.rowIndices=r}set(t,r,e){return this.matrix.set(this.rowIndices[t],r,e),this}get(t,r){return this.matrix.get(this.rowIndices[t],r)}}class z extends M{constructor(t,r){r=w(t,r),super(t,t.rows,r.length),this.columnIndices=r}set(t,r,e){return this.matrix.set(t,this.columnIndices[r],e),this}get(t,r){return this.matrix.get(t,this.columnIndices[r])}}class N extends M{constructor(t,r){super(t,t.rows,1),this.column=r}set(t,r,e){return this.matrix.set(t,this.column,e),this}get(t){return this.matrix.get(t,this.column)}}class A extends M{constructor(t){super(t,t.rows,t.columns)}set(t,r,e){return this.matrix.set(this.rows-t-1,r,e),this}get(t,r){return this.matrix.get(this.rows-t-1,r)}}class V extends M{constructor(t){super(t,t.rows,t.columns)}set(t,r,e){return this.matrix.set(t,this.columns-r-1,e),this}get(t,r){return this.matrix.get(t,this.columns-r-1)}}function C(t){void 0===t&&(t=Object);class r extends t{static get[Symbol.species](){return this}static from1DArray(t,r,e){if(t*r!==e.length)throw new RangeError("Data length does not match given dimensions");for(var n=new this(t,r),i=0;it&&(t=this.get(r,e));return t}maxIndex(){for(var t=this.get(0,0),r=[0,0],e=0;et&&(t=this.get(e,n),r[0]=e,r[1]=n);return r}min(){for(var t=this.get(0,0),r=0;rr&&(r=this.get(t,e));return r}maxRowIndex(t){f(this,t);for(var r=this.get(t,0),e=[t,0],n=1;nr&&(r=this.get(t,n),e[1]=n);return e}minRow(t){f(this,t);for(var r=this.get(t,0),e=1;er&&(r=this.get(e,t));return r}maxColumnIndex(t){m(this,t);for(var r=this.get(0,t),e=[0,t],n=1;nr&&(r=this.get(n,t),e[0]=n);return e}minColumn(t){m(this,t);for(var r=this.get(0,t),e=1;e=(r=void 0===r?1:r))throw new RangeError("min should be strictly smaller than max");for(var e=this.constructor.empty(this.rows,this.columns),n=0;n=(r=void 0===r?1:r))throw new RangeError("min should be strictly smaller than max");for(var e=this.constructor.empty(this.rows,this.columns),n=0;ne||r<0||r>=this.columns||e<0||e>=this.columns)throw new RangeError("Argument out of range");for(var n=new this.constructor[Symbol.species](t.length,e-r+1),i=0;i=this.rows)throw new RangeError(`Row index out of range: ${t[i]}`);n.set(i,o-r,this.get(t[i],o))}return n}subMatrixColumn(t,r,e){if(void 0===r&&(r=0),void 0===e&&(e=this.rows-1),r>e||r<0||r>=this.rows||e<0||e>=this.rows)throw new RangeError("Argument out of range");for(var n=new this.constructor[Symbol.species](e-r+1,t.length),i=0;i=this.columns)throw new RangeError(`Column index out of range: ${t[i]}`);n.set(o-r,i,this.get(o,t[i]))}return n}setSubMatrix(t,r,e){y(this,r,r+(t=this.constructor.checkMatrix(t)).rows-1,e,e+t.columns-1);for(var n=0;nt?i[o]=1/i[o]:i[o]=0;return i=this.constructor[Symbol.species].diag(i),n.mmul(i.mmul(e.transposeView()))}clone(){for(var t=new this.constructor[Symbol.species](this.rows,this.columns),r=0;r>","signPropagatingRightShift"],[">>>","rightShift","zeroFillRightShift"]]){var u=o($("\n(function %name%(value) {\n if (typeof value === 'number') return this.%name%S(value);\n return this.%name%M(value);\n})\n",{name:s[1],op:s[0]})),l=o($("\n(function %name%S(value) {\n for (var i = 0; i < this.rows; i++) {\n for (var j = 0; j < this.columns; j++) {\n this.set(i, j, this.get(i, j) %op% value);\n }\n }\n return this;\n})\n",{name:`${s[1]}S`,op:s[0]})),d=o($("\n(function %name%M(matrix) {\n matrix = this.constructor.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 (var i = 0; i < this.rows; i++) {\n for (var j = 0; j < this.columns; j++) {\n this.set(i, j, this.get(i, j) %op% matrix.get(i, j));\n }\n }\n return this;\n})\n",{name:`${s[1]}M`,op:s[0]})),w=o($("\n(function %name%(matrix, value) {\n var newMatrix = new this[Symbol.species](matrix);\n return newMatrix.%name%(value);\n})\n",{name:s[1]}));for(n=1;n0){if(super(t),!(Number.isInteger(r)&&r>0))throw new TypeError("nColumns must be a positive integer");for(e=0;e=0;o--){for(i=0;i=0;e--){for(t=0;ti)return new Array(r.rows+1).fill(0);for(var o=r.addRow(e,[0]),s=0;s0;a--){for(c=0,s=0,u=0;u0&&(o=-o),r[a]=c*o,s-=i*o,e[a-1]=i-o,h=0;hl){0;do{for(1,i=e[l],f=(e[l+1]-i)/(2*r[l]),m=u(f,1),f<0&&(m=-m),e[l]=r[l]/(f+m),e[l+1]=r[l]*(f+m),g=e[l+1],o=i-e[l],s=l+2;s=l;s--)for(d=v,v=p,b=y,i=p*r[s],o=p*f,m=u(f,r[s]),r[s+1]=y*m,y=r[s]/m,f=(p=f/m)*e[s]-y*i,e[s+1]=o+y*(p*i+y*e[s]),h=0;hS*x)}e[l]=e[l]+M,r[l]=0}for(s=0;s=u;a--)e[a]=r[a][u-1]/l,s+=e[a]*e[a];for(o=Math.sqrt(s),e[u]>0&&(o=-o),s-=e[u]*o,e[u]=e[u]-o,h=u;h=u;a--)i+=e[a]*r[a][h];for(i/=s,a=u;a<=c;a++)r[a][h]-=i*e[a]}for(a=0;a<=c;a++){for(i=0,h=c;h>=u;h--)i+=e[h]*r[a][h];for(i/=s,h=u;h<=c;h++)r[a][h]-=i*e[h]}e[u]=l*e[u],r[u][u-1]=l*o}}for(a=0;a=1;u--)if(0!==r[u][u-1]){for(a=u+1;a<=c;a++)e[a]=r[a][u-1];for(h=u;h<=c;h++){for(o=0,a=u;a<=c;a++)o+=e[a]*n[a][h];for(o=o/e[u]/r[u][u-1],a=u;a<=c;a++)n[a][h]+=o*e[a]}}}(o,f,m,s),function(t,r,e,n,i){var o,s,a,h,u,l,c,f,m,g,p,v,d,w,y,b=t-1,M=t-1,x=Number.EPSILON,S=0,E=0,R=0,k=0,z=0,N=0,A=0,V=0;for(o=0;oM)&&(e[o]=i[o][o],r[o]=0),s=Math.max(o-1,0);s=0;){for(h=b;h>0&&(0===(N=Math.abs(i[h-1][h-1])+Math.abs(i[h][h]))&&(N=E),!(Math.abs(i[h][h-1])=0){for(A=R>=0?R+A:R-A,e[b-1]=f+A,e[b]=e[b-1],0!==A&&(e[b]=f-c/A),r[b-1]=0,r[b]=0,f=i[b][b-1],N=Math.abs(f)+Math.abs(A),R=f/N,k=A/N,z=Math.sqrt(R*R+k*k),R/=z,k/=z,s=b-1;s0){for(N=Math.sqrt(N),m=h&&(A=i[u][u],R=((z=f-A)*(N=m-A)-c)/i[u+1][u]+i[u][u+1],k=i[u+1][u+1]-A-z-N,z=i[u+2][u+1],N=Math.abs(R)+Math.abs(k)+Math.abs(z),R/=N,k/=N,z/=N,u!==h)&&!(Math.abs(i[u][u-1])*(Math.abs(k)+Math.abs(z))u+2&&(i[o][o-3]=0);for(a=u;a<=b-1&&(w=a!==b-1,a!==u&&(R=i[a][a-1],k=i[a+1][a-1],z=w?i[a+2][a-1]:0,0!==(f=Math.abs(R)+Math.abs(k)+Math.abs(z))&&(R/=f,k/=f,z/=f)),0!==f);a++)if(N=Math.sqrt(R*R+k*k+z*z),R<0&&(N=-N),0!==N){for(a!==u?i[a][a-1]=-N*f:h!==u&&(i[a][a-1]=-i[a][a-1]),f=(R+=N)/N,m=k/N,A=z/N,k/=R,z/=R,s=a;s=0;b--)if(R=e[b],0===(k=r[b]))for(h=b,i[b][b]=1,o=b-1;o>=0;o--){for(c=i[o][o]-R,z=0,s=h;s<=b;s++)z+=i[o][s]*i[s][b];if(r[o]<0)A=c,N=z;else if(h=o,0===r[o]?i[o][b]=0!==c?-z/c:-z/(x*E):(f=i[o][o+1],m=i[o+1][o],k=(e[o]-R)*(e[o]-R)+r[o]*r[o],l=(f*N-A*z)/k,i[o][b]=l,i[o+1][b]=Math.abs(f)>Math.abs(A)?(-z-c*l)/f:(-N-m*l)/A),l=Math.abs(i[o][b]),x*l*l>1)for(s=o;s<=b;s++)i[s][b]=i[s][b]/l}else if(k<0)for(h=b-1,Math.abs(i[b][b-1])>Math.abs(i[b-1][b])?(i[b-1][b-1]=k/i[b][b-1],i[b-1][b]=-(i[b][b]-R)/i[b][b-1]):(y=$(0,-i[b-1][b],i[b-1][b-1]-R,k),i[b-1][b-1]=y[0],i[b-1][b]=y[1]),i[b][b-1]=0,i[b][b]=1,o=b-2;o>=0;o--){for(g=0,p=0,s=h;s<=b;s++)g+=i[o][s]*i[s][b-1],p+=i[o][s]*i[s][b];if(c=i[o][o]-R,r[o]<0)A=c,z=g,N=p;else if(h=o,0===r[o]?(y=$(-g,-p,c,k),i[o][b-1]=y[0],i[o][b]=y[1]):(f=i[o][o+1],m=i[o+1][o],v=(e[o]-R)*(e[o]-R)+r[o]*r[o]-k*k,d=2*(e[o]-R)*k,0===v&&0===d&&(v=x*E*(Math.abs(c)+Math.abs(k)+Math.abs(f)+Math.abs(m)+Math.abs(A))),y=$(f*z-A*g+k*p,f*N-A*p-k*g,v,d),i[o][b-1]=y[0],i[o][b]=y[1],Math.abs(f)>Math.abs(A)+Math.abs(k)?(i[o+1][b-1]=(-g-c*i[o][b-1]+k*i[o][b])/f,i[o+1][b]=(-p-c*i[o][b]-k*i[o][b-1])/f):(y=$(-z-m*i[o][b-1],-N-m*i[o][b],A,k),i[o+1][b-1]=y[0],i[o+1][b]=y[1])),l=Math.max(Math.abs(i[o][b-1]),Math.abs(i[o][b])),x*l*l>1)for(s=o;s<=b;s++)i[s][b-1]=i[s][b-1]/l,i[s][b]=i[s][b]/l}for(o=0;oM)for(s=o;s=0;s--)for(o=0;o<=M;o++){for(A=0,a=0;a<=Math.min(s,M);a++)A+=n[o][a]*i[a][s];n[o][s]=A}}(o,h,a,s,f)}this.n=o,this.e=h,this.d=a,this.V=s}get realEigenvalues(){return this.d}get imaginaryEigenvalues(){return this.e}get eigenvectorMatrix(){return _.isMatrix(this.V)||(this.V=new _(this.V)),this.V}get diagonalMatrix(){var t,r,e=this.n,n=this.e,i=this.d,o=new _(e,e);for(t=0;t0?o[t][t+1]=n[t]:n[t]<0&&(o[t][t-1]=n[t])}return o}}function $(t,r,e,n){var i,o;return Math.abs(e)>Math.abs(n)?[(t+(i=n/e)*r)/(o=e+i*n),(r-i*t)/o]:[((i=e/n)*t+r)/(o=n+i*e),(i*r-t)/o]}class Q{constructor(t){if(!(t=I.checkMatrix(t)).isSymmetric())throw new Error("Matrix is not symmetric");var r,e,n,i=t,o=i.rows,s=new _(o,o),a=!0;for(e=0;e0,s[e][e]=Math.sqrt(Math.max(u,0)),n=e+1;n=0;o--)for(i=0;i=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}};function i(t,r){return Math.floor(r()*t)}function o(t){for(var r=[],e=0;er?t[e]:r;return r},r.max2d=function(t){for(var r=0,e=0;er?t[e][n]:r;return r},r.rejectionSample=function(t,r,e){for(var n=a(t),o=0;o=s[0])return 0;for(var h=0;h=s[0])return 0;s[0]=e,o[0]=n,a[0]=i;for(var h=0,u=0;;){var l=2*h+1,c=l+1,f=t[0][0].length;if(l>=f)break;if(c>=f){if(!(s[l]>e))break;u=l}else if(s[l]>=s[c]){if(!(ee.length;a++)1===i[a]&&n[a]=0?(i[s]=0,Math.floor(e[s])):-1}},function(t,r,e){"use strict";var n,i=this&&this.__read||function(t,r){var e="function"==typeof Symbol&&t[Symbol.iterator];if(!e)return t;var n,i,o=e.call(t),s=[];try{for(;(void 0===r||r-- >0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},o=this&&this.__spread||function(){for(var t=[],r=0;r=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}},a=this&&this.__importStar||function(t){if(t&&t.__esModule)return t;var r={};if(null!=t)for(var e in t)Object.hasOwnProperty.call(t,e)&&(r[e]=t[e]);return r.default=t,r};Object.defineProperty(r,"__esModule",{value:!0});var h=a(e(1)),u=function(){function t(t,r,e,n){this.entries=new Map,this.nRows=0,this.nCols=0,this.rows=o(t),this.cols=o(r),this.values=o(e);for(var i=0;ir?t:r})},r.multiplyScalar=function(t,r){return t.map(function(t){return t*r})},r.eliminateZeros=function(t){for(var r=new Set,e=t.getValues(),n=t.getRows(),i=t.getCols(),o=0;or?t[e]:r;return t.map(function(t){return t/r})},n.l1=function(t){for(var r=0,e=0;e0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},i=this&&this.__spread||function(){for(var t=[],r=0;r=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}},s=this&&this.__importStar||function(t){if(t&&t.__esModule)return t;var r={};if(null!=t)for(var e in t)Object.hasOwnProperty.call(t,e)&&(r[e]=t[e]);return r.default=t,r};Object.defineProperty(r,"__esModule",{value:!0});var a=s(e(1)),h=function(){return function(t,r,e,n){this.hyperplanes=t,this.offsets=r,this.children=e,this.indices=n}}();function u(t,r,e,n){for(var i=r,o=0;o0?0:1}r.FlatTree=h,r.makeForest=function(t,r,e,n){var o=Math.max(10,r);return a.range(e).map(function(r,e){return function(t,r,e,n){void 0===r&&(r=30);var i=a.range(t.length);return function t(r,e,n,i,o){if(void 0===n&&(n=30),e.length>n){var s=function(t,r,e){var n=t[0].length,i=a.tauRandInt(r.length,e),o=a.tauRandInt(r.length,e);o=(o+=i===o?1:0)%r.length;for(var s=r[i],h=r[o],u=0,l=a.zeros(n),c=0;c0?(g[c]=0,f+=1):(g[c]=1,m+=1)}var d=a.zeros(f),w=a.zeros(m);for(var c in f=0,m=0,a.range(g.length))0===g[c]?(d[f]=r[c],f+=1):(w[m]=r[c],m+=1);return{indicesLeft:d,indicesRight:w,hyperplane:l,offset:u}}(r,e,o),h=s.indicesLeft,u=s.indicesRight,l=s.hyperplane,c=s.offset,f=t(r,h,n,i+1,o),m=t(r,u,n,i+1,o),g={leftChild:f,rightChild:m,isLeaf:!1,hyperplane:l,offset:c};return g}var g={indices:e,isLeaf:!0};return g}(t,i,r,e,n)}(t,o,e,n)}).map(function(t){return function(t,r){var e=function t(r){return r.isLeaf?1:1+t(r.leftChild)+t(r.rightChild)}(t),n=function t(r){return r.isLeaf?1:t(r.leftChild)+t(r.rightChild)}(t),o=a.range(e).map(function(){return a.zeros(t.hyperplane.length)}),s=a.zeros(e),u=a.range(e).map(function(){return[-1,-1]}),l=a.range(n).map(function(){return a.range(r).map(function(){return-1})});return function t(r,e,n,o,s,a,h){var u;if(r.isLeaf)return o[a][0]=-h,(u=s[h]).splice.apply(u,i([0,r.indices.length],r.indices)),{nodeNum:a,leafNum:h+=1};e[a]=r.hyperplane,n[a]=r.offset,o[a][0]=a+1;var l=a,c=t(r.leftChild,e,n,o,s,a+1,h);return a=c.nodeNum,h=c.leafNum,o[l][1]=a+1,{nodeNum:(c=t(r.rightChild,e,n,o,s,a+1,h)).nodeNum,leafNum:c.leafNum}}(t,o,s,u,l,0,0),new h(o,s,u,l)}(t,o)})},r.makeLeafArray=function(t){var r,e;if(t.length>0){var n=[];try{for(var s=o(t),a=s.next();!a.done;a=s.next()){var h=a.value;n.push.apply(n,i(h.indices))}}catch(t){r={error:t}}finally{try{a&&!a.done&&(e=s.return)&&e.call(s)}finally{if(r)throw r.error}}return n}return[[-1]]},r.searchFlatTree=function(t,r,e){for(var n=0;r.children[n][0]>0;)n=0===u(r.hyperplanes[n],r.offsets[n],t,e)?r.children[n][0]:r.children[n][1];var i=-1*r.children[n][0];return r.indices[i]}},function(t,r,e){"use strict";Object.defineProperty(r,"__esModule",{value:!0});var n=e(6);window.UMAP=n.UMAP},function(t,r,e){"use strict";var n=this&&this.__awaiter||function(t,r,e,n){return new(e||(e=Promise))(function(i,o){function s(t){try{h(n.next(t))}catch(t){o(t)}}function a(t){try{h(n.throw(t))}catch(t){o(t)}}function h(t){t.done?i(t.value):new e(function(r){r(t.value)}).then(s,a)}h((n=n.apply(t,r||[])).next())})},i=this&&this.__generator||function(t,r){var e,n,i,o,s={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function a(o){return function(a){return function(o){if(e)throw new TypeError("Generator is already executing.");for(;s;)try{if(e=1,n&&(i=2&o[0]?n.return:o[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,o[1])).done)return i;switch(n=0,i&&(o=[2&o[0],i.value]),o[0]){case 0:case 1:i=o;break;case 4:return s.label++,{value:o[1],done:!1};case 5:s.label++,n=o[1],o=[0];continue;case 7:o=s.ops.pop(),s.trys.pop();continue;default:if(!(i=(i=s.trys).length>0&&i[i.length-1])&&(6===o[0]||2===o[0])){s=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},s=this&&this.__spread||function(){for(var t=[],r=0;r0&&i.set(o,u,c)}var f=l.transpose(i);return l.maximum(i,f)},t.prototype.transform=function(t){var r=this,e=this.X;if(void 0===e||0===e.length)throw new Error("No data has been fit.");var n=Math.floor(this.nNeighbors*this.transformQueueSize),i=c.initializeSearch(this.rpForest,e,t,n,this.initFromRandom,this.initFromTree,this.random),o=this.search(e,this.searchGraph,i,t),s=u.deheapSort(o),a=s.indices,h=s.weights;a=a.map(function(t){return t.slice(0,r.nNeighbors)}),h=h.map(function(t){return t.slice(0,r.nNeighbors)});var f=Math.max(0,this.localConnectivity-1),g=this.smoothKNNDistance(h,this.nNeighbors,f),p=g.sigmas,v=g.rhos,d=this.computeMembershipStrengths(a,h,p,v),w=d.rows,y=d.cols,b=d.vals,M=[t.length,e.length],x=new l.SparseMatrix(w,y,b,M),E=l.normalize(x,"l1"),R=l.getCSR(E),k=t.length,z=S(m.reshape2d(R.indices,k,this.nNeighbors),m.reshape2d(R.values,k,this.nNeighbors),this.embedding),N=this.nEpochs?this.nEpochs/3:x.nRows<=1e4?100:30,A=x.getValues().reduce(function(t,r){return r>t?r:t},0);x=x.map(function(t){return t0});if(g.length>=e){var p=Math.floor(e),v=e-p;p>0?(s[h]=g[p-1],v>1e-5&&(s[h]+=v*(g[p]-g[p-1]))):s[h]=v*g[0]}else g.length>0&&(s[h]=m.max(g));for(var d=0;d0?Math.exp(-b/c):1}if(Math.abs(w-o)<1e-5)break;w>o?c=(u+(l=c))/2:(u=c,l===1/0?c*=2:c=(u+l)/2)}if(a[h]=c,s[h]>0){var M=m.mean(f);a[h]<.001*M&&(a[h]=.001*M)}else{var x=m.mean(t.map(m.mean));a[h]<.001*x&&(a[h]=.001*x)}}return{sigmas:a,rhos:s}},t.prototype.computeMembershipStrengths=function(t,r,e,n){for(var i=t.length,o=t[0].length,s=m.zeros(i*o),a=m.zeros(i*o),h=m.zeros(i*o),u=0;u0&&(e[n]=r/i[n])}),e},t.prototype.assignOptimizationStateParameters=function(t){Object.assign(this.optimizationState,t)},t.prototype.prepareForOptimizationLoop=function(){var t=this.repulsionStrength,r=this.learningRate,e=this.negativeSampleRate,n=this.optimizationState,i=n.epochsPerSample,o=n.headEmbedding,a=n.tailEmbedding,h=o[0].length,u=o.length===a.length,l=i.map(function(t){return t/e}),c=s(l),f=s(i);this.assignOptimizationStateParameters({epochOfNextSample:f,epochOfNextNegativeSample:c,epochsPerNegativeSample:l,moveOther:u,initialAlpha:r,alpha:r,gamma:t,dim:h})},t.prototype.initializeOptimization=function(){var t=this.embedding,r=this.embedding,e=this.optimizationState,n=e.head,i=e.tail,o=e.epochsPerSample,s=this.getNEpochs(),a=this.graph.nCols,h=b(this.spread,this.minDist),u=h.a,l=h.b;this.assignOptimizationStateParameters({headEmbedding:t,tailEmbedding:r,head:n,tail:i,epochsPerSample:o,a:u,b:l,nEpochs:s,nVertices:a})},t.prototype.optimizeLayoutStep=function(t){for(var r=this.optimizationState,e=r.head,n=r.tail,i=r.headEmbedding,o=r.tailEmbedding,s=r.epochsPerSample,a=r.epochOfNextSample,h=r.epochOfNextNegativeSample,u=r.epochsPerNegativeSample,l=r.moveOther,c=r.initialAlpha,f=r.alpha,g=r.gamma,p=r.a,v=r.b,d=r.dim,b=r.nEpochs,M=r.nVertices,x=0;xt)){var S=e[x],E=n[x],R=i[S],k=o[E],z=y(R,k),N=0;z>0&&(N=-2*p*v*Math.pow(z,v-1),N/=p*Math.pow(z,v)+1);for(var A=0;A0)O=2*g*v,O/=(.001+j)*(p*Math.pow(j,v)+1);else if(S===P)continue;for(A=0;A0&&(V=w(O*(R[A]-I[A]),4)),R[A]+=V*f}}h[x]+=C*u[x]}return r.alpha=c*(1-t/b),r.currentEpoch+=1,i},t.prototype.optimizeLayoutAsync=function(t){var r=this;return void 0===t&&(t=function(){return!0}),new Promise(function(e,o){var s=function(){return n(r,void 0,void 0,function(){var r,n,a,h,u,l;return i(this,function(i){try{if(r=this.optimizationState,n=r.nEpochs,a=r.currentEpoch,this.embedding=this.optimizeLayoutStep(a),h=this.optimizationState.currentEpoch,u=!1===t(h),l=h===n,u||l)return[2,e(l)];s()}catch(t){o(t)}return[2]})})};s()})},t.prototype.optimizeLayout=function(t){void 0===t&&(t=function(){return!0});for(var r=!1,e=[];!r;){var n=this.optimizationState,i=n.nEpochs,o=n.currentEpoch;e=this.optimizeLayoutStep(o);var s=this.optimizationState.currentEpoch,a=!1===t(s);r=s===i||a}return e},t.prototype.getNEpochs=function(){var t=this.graph;if(this.nEpochs>0)return this.nEpochs;var r=t.nRows;return r<=2500?500:r<=5e3?400:r<=7500?300:200},t}();function v(t,r){for(var e=0,n=0;nr?r:t<-r?-r:t}function y(t,r){for(var e=0,n=0;n=r?Math.exp(-(e[i]-r)/t):n}),i={x:e,y:n},s={damping:1.5,initialValues:[.5,.5],gradientDifference:.1,maxIterations:100,errorTolerance:.01},a=g.default(i,function(t){var r=o(t,2),e=r[0],n=r[1];return function(t){return 1/(1+e*Math.pow(t,2*n))}},s).parameterValues,h=o(a,2);return{a:h[0],b:h[1]}}function M(t,r,e,n){return void 0===e&&(e=1),void 0===n&&(n=5),t.map(function(t,i,o){return-1===r[i]||-1===r[o]?t*Math.exp(-e):r[i]!==r[o]?t*Math.exp(-n):t})}function x(t){t=l.normalize(t,"max");var r=l.transpose(t),e=l.pairwiseMultiply(r,t);return t=l.add(t,l.subtract(r,e)),l.eliminateZeros(t)}function S(t,r,e){for(var n=m.zeros(t.length).map(function(t){return m.zeros(e[0].length)}),i=0;i=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}},i=this&&this.__importStar||function(t){if(t&&t.__esModule)return t;var r={};if(null!=t)for(var e in t)Object.hasOwnProperty.call(t,e)&&(r[e]=t[e]);return r.default=t,r};Object.defineProperty(r,"__esModule",{value:!0});var o=i(e(2)),s=i(e(3)),a=i(e(4)),h=i(e(1));r.makeNNDescent=function(t,r){return function(e,n,i,s,a,u,l,c){void 0===s&&(s=10),void 0===a&&(a=50),void 0===u&&(u=.001),void 0===l&&(l=.5),void 0===c&&(c=!0);for(var f=e.length,m=o.makeHeap(e.length,i),g=0;gr&&(r=t[e]);return r};var s=function(t){if(!i()(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");for(var r=t[0],e=1;e1&&void 0!==arguments[1]?arguments[1]:{};if(!i()(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");if(void 0!==e.output){if(!i()(e.output))throw new TypeError("output option must be an array if specified");r=e.output}else r=new Array(t.length);var n=s(t),a=o(t);if(n===a)throw new RangeError("minimum and maximum input values are equal. Cannot rescale a constant array");var h=e.min,u=void 0===h?e.autoMinMax?n:0:h,l=e.max,c=void 0===l?e.autoMinMax?a:1:l;if(u>=c)throw new RangeError("min option must be smaller than max option");for(var f=(c-u)/(a-n),m=0;mMath.abs(h[i])&&(i=r);if(i!==e){for(n=0;n=0;i--){for(n=0;no?t[i][o]:i===o?1:0;return n}get upperTriangularMatrix(){for(var t=this.LU,r=t.rows,e=t.columns,n=new _(r,e),i=0;iMath.abs(r)?(e=r/t,Math.abs(t)*Math.sqrt(1+e*e)):0!==r?(e=t/r,Math.abs(r)*Math.sqrt(1+e*e)):0}function l(t,r,e){for(var n=new Array(t),i=0;i=0;t--)if(0!==v[t]){for(let r=t+1;r=0;t--){if(t0;){let t,r;for(t=R-2;t>=-1&&-1!==t;t--){const r=Number.MIN_VALUE+z*Math.abs(v[t]+Math.abs(v[t+1]));if(Math.abs(y[t])<=r||Number.isNaN(y[t])){y[t]=0;break}}if(t===R-2)r=4;else{let e;for(e=R-1;e>=t&&e!==t;e--){let r=(e!==R?Math.abs(y[e]):0)+(e!==t+1?Math.abs(y[e-1]):0);if(Math.abs(v[e])<=z*r){v[e]=0;break}}e===t?r=3:e===R-1?r=1:(r=2,t=e)}switch(t++,r){case 1:{let r=y[R-2];y[R-2]=0;for(let e=R-2;e>=t;e--){let i=u(v[e],r),o=v[e]/i,s=r/i;if(v[e]=i,e!==t&&(r=-s*y[e-1],y[e-1]=o*y[e-1]),c)for(let t=0;t=v[t+1]);){let r=v[t];if(v[t]=v[t+1],v[t+1]=r,c&&tr?i[o][e]=t[o][e]/this.s[e]:i[o][e]=0;var o=this.U,s=o.length,a=o[0].length,h=new _(e,s);for(let t=0;tt&&r++;return r}get diagonal(){return this.s}get threshold(){return Number.EPSILON/2*Math.max(this.m,this.n)*this.s[0]}get leftSingularVectors(){return _.isMatrix(this.U)||(this.U=new _(this.U)),this.U}get rightSingularVectors(){return _.isMatrix(this.V)||(this.V=new _(this.V)),this.V}get diagonalMatrix(){return _.diag(this.s)}}function f(t,r,e){var n=e?t.rows:t.rows-1;if(r<0||r>n)throw new RangeError("Row index out of range")}function m(t,r,e){var n=e?t.columns:t.columns-1;if(r<0||r>n)throw new RangeError("Column index out of range")}function g(t,r){if(r.to1DArray&&(r=r.to1DArray()),r.length!==t.columns)throw new RangeError("vector size must be the same as the number of columns");return r}function p(t,r){if(r.to1DArray&&(r=r.to1DArray()),r.length!==t.rows)throw new RangeError("vector size must be the same as the number of rows");return r}function v(t,r,e){return{row:d(t,r),column:w(t,e)}}function d(t,r){if("object"!=typeof r)throw new TypeError("unexpected type for row indices");if(r.some(r=>r<0||r>=t.rows))throw new RangeError("row indices are out of range");return Array.isArray(r)||(r=Array.from(r)),r}function w(t,r){if("object"!=typeof r)throw new TypeError("unexpected type for column indices");if(r.some(r=>r<0||r>=t.columns))throw new RangeError("column indices are out of range");return Array.isArray(r)||(r=Array.from(r)),r}function y(t,r,e,n,i){if(5!==arguments.length)throw new RangeError("expected 4 arguments");if(b("startRow",r),b("endRow",e),b("startColumn",n),b("endColumn",i),r>e||n>i||r<0||r>=t.rows||e<0||e>=t.rows||n<0||n>=t.columns||i<0||i>=t.columns)throw new RangeError("Submatrix indices are out of range")}function b(t,r){if("number"!=typeof r)throw new TypeError(`${t} must be a number`)}class M extends(C()){constructor(t,r,e){super(),this.matrix=t,this.rows=r,this.columns=e}static get[Symbol.species](){return _}}class x extends M{constructor(t){super(t,t.columns,t.rows)}set(t,r,e){return this.matrix.set(r,t,e),this}get(t,r){return this.matrix.get(r,t)}}class S extends M{constructor(t,r){super(t,1,t.columns),this.row=r}set(t,r,e){return this.matrix.set(this.row,r,e),this}get(t,r){return this.matrix.get(this.row,r)}}class E extends M{constructor(t,r,e,n,i){y(t,r,e,n,i),super(t,e-r+1,i-n+1),this.startRow=r,this.startColumn=n}set(t,r,e){return this.matrix.set(this.startRow+t,this.startColumn+r,e),this}get(t,r){return this.matrix.get(this.startRow+t,this.startColumn+r)}}class R extends M{constructor(t,r,e){var n=v(t,r,e);super(t,n.row.length,n.column.length),this.rowIndices=n.row,this.columnIndices=n.column}set(t,r,e){return this.matrix.set(this.rowIndices[t],this.columnIndices[r],e),this}get(t,r){return this.matrix.get(this.rowIndices[t],this.columnIndices[r])}}class k extends M{constructor(t,r){super(t,(r=d(t,r)).length,t.columns),this.rowIndices=r}set(t,r,e){return this.matrix.set(this.rowIndices[t],r,e),this}get(t,r){return this.matrix.get(this.rowIndices[t],r)}}class z extends M{constructor(t,r){r=w(t,r),super(t,t.rows,r.length),this.columnIndices=r}set(t,r,e){return this.matrix.set(t,this.columnIndices[r],e),this}get(t,r){return this.matrix.get(t,this.columnIndices[r])}}class N extends M{constructor(t,r){super(t,t.rows,1),this.column=r}set(t,r,e){return this.matrix.set(t,this.column,e),this}get(t){return this.matrix.get(t,this.column)}}class A extends M{constructor(t){super(t,t.rows,t.columns)}set(t,r,e){return this.matrix.set(this.rows-t-1,r,e),this}get(t,r){return this.matrix.get(this.rows-t-1,r)}}class V extends M{constructor(t){super(t,t.rows,t.columns)}set(t,r,e){return this.matrix.set(t,this.columns-r-1,e),this}get(t,r){return this.matrix.get(t,this.columns-r-1)}}function C(t){void 0===t&&(t=Object);class r extends t{static get[Symbol.species](){return this}static from1DArray(t,r,e){if(t*r!==e.length)throw new RangeError("Data length does not match given dimensions");for(var n=new this(t,r),i=0;it&&(t=this.get(r,e));return t}maxIndex(){for(var t=this.get(0,0),r=[0,0],e=0;et&&(t=this.get(e,n),r[0]=e,r[1]=n);return r}min(){for(var t=this.get(0,0),r=0;rr&&(r=this.get(t,e));return r}maxRowIndex(t){f(this,t);for(var r=this.get(t,0),e=[t,0],n=1;nr&&(r=this.get(t,n),e[1]=n);return e}minRow(t){f(this,t);for(var r=this.get(t,0),e=1;er&&(r=this.get(e,t));return r}maxColumnIndex(t){m(this,t);for(var r=this.get(0,t),e=[0,t],n=1;nr&&(r=this.get(n,t),e[0]=n);return e}minColumn(t){m(this,t);for(var r=this.get(0,t),e=1;e=(r=void 0===r?1:r))throw new RangeError("min should be strictly smaller than max");for(var e=this.constructor.empty(this.rows,this.columns),n=0;n=(r=void 0===r?1:r))throw new RangeError("min should be strictly smaller than max");for(var e=this.constructor.empty(this.rows,this.columns),n=0;ne||r<0||r>=this.columns||e<0||e>=this.columns)throw new RangeError("Argument out of range");for(var n=new this.constructor[Symbol.species](t.length,e-r+1),i=0;i=this.rows)throw new RangeError(`Row index out of range: ${t[i]}`);n.set(i,o-r,this.get(t[i],o))}return n}subMatrixColumn(t,r,e){if(void 0===r&&(r=0),void 0===e&&(e=this.rows-1),r>e||r<0||r>=this.rows||e<0||e>=this.rows)throw new RangeError("Argument out of range");for(var n=new this.constructor[Symbol.species](e-r+1,t.length),i=0;i=this.columns)throw new RangeError(`Column index out of range: ${t[i]}`);n.set(o-r,i,this.get(o,t[i]))}return n}setSubMatrix(t,r,e){y(this,r,r+(t=this.constructor.checkMatrix(t)).rows-1,e,e+t.columns-1);for(var n=0;nt?i[o]=1/i[o]:i[o]=0;return i=this.constructor[Symbol.species].diag(i),n.mmul(i.mmul(e.transposeView()))}clone(){for(var t=new this.constructor[Symbol.species](this.rows,this.columns),r=0;r>","signPropagatingRightShift"],[">>>","rightShift","zeroFillRightShift"]]){var u=o($("\n(function %name%(value) {\n if (typeof value === 'number') return this.%name%S(value);\n return this.%name%M(value);\n})\n",{name:s[1],op:s[0]})),l=o($("\n(function %name%S(value) {\n for (var i = 0; i < this.rows; i++) {\n for (var j = 0; j < this.columns; j++) {\n this.set(i, j, this.get(i, j) %op% value);\n }\n }\n return this;\n})\n",{name:`${s[1]}S`,op:s[0]})),d=o($("\n(function %name%M(matrix) {\n matrix = this.constructor.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 (var i = 0; i < this.rows; i++) {\n for (var j = 0; j < this.columns; j++) {\n this.set(i, j, this.get(i, j) %op% matrix.get(i, j));\n }\n }\n return this;\n})\n",{name:`${s[1]}M`,op:s[0]})),w=o($("\n(function %name%(matrix, value) {\n var newMatrix = new this[Symbol.species](matrix);\n return newMatrix.%name%(value);\n})\n",{name:s[1]}));for(n=1;n0){if(super(t),!(Number.isInteger(r)&&r>0))throw new TypeError("nColumns must be a positive integer");for(e=0;e=0;o--){for(i=0;i=0;e--){for(t=0;ti)return new Array(r.rows+1).fill(0);for(var o=r.addRow(e,[0]),s=0;s0;a--){for(c=0,s=0,u=0;u0&&(o=-o),r[a]=c*o,s-=i*o,e[a-1]=i-o,h=0;hl){0;do{for(1,i=e[l],f=(e[l+1]-i)/(2*r[l]),m=u(f,1),f<0&&(m=-m),e[l]=r[l]/(f+m),e[l+1]=r[l]*(f+m),g=e[l+1],o=i-e[l],s=l+2;s=l;s--)for(d=v,v=p,b=y,i=p*r[s],o=p*f,m=u(f,r[s]),r[s+1]=y*m,y=r[s]/m,f=(p=f/m)*e[s]-y*i,e[s+1]=o+y*(p*i+y*e[s]),h=0;hS*x)}e[l]=e[l]+M,r[l]=0}for(s=0;s=u;a--)e[a]=r[a][u-1]/l,s+=e[a]*e[a];for(o=Math.sqrt(s),e[u]>0&&(o=-o),s-=e[u]*o,e[u]=e[u]-o,h=u;h=u;a--)i+=e[a]*r[a][h];for(i/=s,a=u;a<=c;a++)r[a][h]-=i*e[a]}for(a=0;a<=c;a++){for(i=0,h=c;h>=u;h--)i+=e[h]*r[a][h];for(i/=s,h=u;h<=c;h++)r[a][h]-=i*e[h]}e[u]=l*e[u],r[u][u-1]=l*o}}for(a=0;a=1;u--)if(0!==r[u][u-1]){for(a=u+1;a<=c;a++)e[a]=r[a][u-1];for(h=u;h<=c;h++){for(o=0,a=u;a<=c;a++)o+=e[a]*n[a][h];for(o=o/e[u]/r[u][u-1],a=u;a<=c;a++)n[a][h]+=o*e[a]}}}(o,f,m,s),function(t,r,e,n,i){var o,s,a,h,u,l,c,f,m,g,p,v,d,w,y,b=t-1,M=t-1,x=Number.EPSILON,S=0,E=0,R=0,k=0,z=0,N=0,A=0,V=0;for(o=0;oM)&&(e[o]=i[o][o],r[o]=0),s=Math.max(o-1,0);s=0;){for(h=b;h>0&&(0===(N=Math.abs(i[h-1][h-1])+Math.abs(i[h][h]))&&(N=E),!(Math.abs(i[h][h-1])=0){for(A=R>=0?R+A:R-A,e[b-1]=f+A,e[b]=e[b-1],0!==A&&(e[b]=f-c/A),r[b-1]=0,r[b]=0,f=i[b][b-1],N=Math.abs(f)+Math.abs(A),R=f/N,k=A/N,z=Math.sqrt(R*R+k*k),R/=z,k/=z,s=b-1;s0){for(N=Math.sqrt(N),m=h&&(A=i[u][u],R=((z=f-A)*(N=m-A)-c)/i[u+1][u]+i[u][u+1],k=i[u+1][u+1]-A-z-N,z=i[u+2][u+1],N=Math.abs(R)+Math.abs(k)+Math.abs(z),R/=N,k/=N,z/=N,u!==h)&&!(Math.abs(i[u][u-1])*(Math.abs(k)+Math.abs(z))u+2&&(i[o][o-3]=0);for(a=u;a<=b-1&&(w=a!==b-1,a!==u&&(R=i[a][a-1],k=i[a+1][a-1],z=w?i[a+2][a-1]:0,0!==(f=Math.abs(R)+Math.abs(k)+Math.abs(z))&&(R/=f,k/=f,z/=f)),0!==f);a++)if(N=Math.sqrt(R*R+k*k+z*z),R<0&&(N=-N),0!==N){for(a!==u?i[a][a-1]=-N*f:h!==u&&(i[a][a-1]=-i[a][a-1]),f=(R+=N)/N,m=k/N,A=z/N,k/=R,z/=R,s=a;s=0;b--)if(R=e[b],0===(k=r[b]))for(h=b,i[b][b]=1,o=b-1;o>=0;o--){for(c=i[o][o]-R,z=0,s=h;s<=b;s++)z+=i[o][s]*i[s][b];if(r[o]<0)A=c,N=z;else if(h=o,0===r[o]?i[o][b]=0!==c?-z/c:-z/(x*E):(f=i[o][o+1],m=i[o+1][o],k=(e[o]-R)*(e[o]-R)+r[o]*r[o],l=(f*N-A*z)/k,i[o][b]=l,i[o+1][b]=Math.abs(f)>Math.abs(A)?(-z-c*l)/f:(-N-m*l)/A),l=Math.abs(i[o][b]),x*l*l>1)for(s=o;s<=b;s++)i[s][b]=i[s][b]/l}else if(k<0)for(h=b-1,Math.abs(i[b][b-1])>Math.abs(i[b-1][b])?(i[b-1][b-1]=k/i[b][b-1],i[b-1][b]=-(i[b][b]-R)/i[b][b-1]):(y=$(0,-i[b-1][b],i[b-1][b-1]-R,k),i[b-1][b-1]=y[0],i[b-1][b]=y[1]),i[b][b-1]=0,i[b][b]=1,o=b-2;o>=0;o--){for(g=0,p=0,s=h;s<=b;s++)g+=i[o][s]*i[s][b-1],p+=i[o][s]*i[s][b];if(c=i[o][o]-R,r[o]<0)A=c,z=g,N=p;else if(h=o,0===r[o]?(y=$(-g,-p,c,k),i[o][b-1]=y[0],i[o][b]=y[1]):(f=i[o][o+1],m=i[o+1][o],v=(e[o]-R)*(e[o]-R)+r[o]*r[o]-k*k,d=2*(e[o]-R)*k,0===v&&0===d&&(v=x*E*(Math.abs(c)+Math.abs(k)+Math.abs(f)+Math.abs(m)+Math.abs(A))),y=$(f*z-A*g+k*p,f*N-A*p-k*g,v,d),i[o][b-1]=y[0],i[o][b]=y[1],Math.abs(f)>Math.abs(A)+Math.abs(k)?(i[o+1][b-1]=(-g-c*i[o][b-1]+k*i[o][b])/f,i[o+1][b]=(-p-c*i[o][b]-k*i[o][b-1])/f):(y=$(-z-m*i[o][b-1],-N-m*i[o][b],A,k),i[o+1][b-1]=y[0],i[o+1][b]=y[1])),l=Math.max(Math.abs(i[o][b-1]),Math.abs(i[o][b])),x*l*l>1)for(s=o;s<=b;s++)i[s][b-1]=i[s][b-1]/l,i[s][b]=i[s][b]/l}for(o=0;oM)for(s=o;s=0;s--)for(o=0;o<=M;o++){for(A=0,a=0;a<=Math.min(s,M);a++)A+=n[o][a]*i[a][s];n[o][s]=A}}(o,h,a,s,f)}this.n=o,this.e=h,this.d=a,this.V=s}get realEigenvalues(){return this.d}get imaginaryEigenvalues(){return this.e}get eigenvectorMatrix(){return _.isMatrix(this.V)||(this.V=new _(this.V)),this.V}get diagonalMatrix(){var t,r,e=this.n,n=this.e,i=this.d,o=new _(e,e);for(t=0;t0?o[t][t+1]=n[t]:n[t]<0&&(o[t][t-1]=n[t])}return o}}function $(t,r,e,n){var i,o;return Math.abs(e)>Math.abs(n)?[(t+(i=n/e)*r)/(o=e+i*n),(r-i*t)/o]:[((i=e/n)*t+r)/(o=n+i*e),(i*r-t)/o]}class Q{constructor(t){if(!(t=I.checkMatrix(t)).isSymmetric())throw new Error("Matrix is not symmetric");var r,e,n,i=t,o=i.rows,s=new _(o,o),a=!0;for(e=0;e0,s[e][e]=Math.sqrt(Math.max(u,0)),n=e+1;n=0;o--)for(i=0;i Date: Thu, 6 Jun 2019 14:26:44 -0400 Subject: [PATCH 23/46] Added timeouts to optimizeLayoutAsync in order to properly run asynchronously. --- lib/umap-js.js | 4 ++-- lib/umap-js.min.js | 2 +- src/umap.ts | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/umap-js.js b/lib/umap-js.js index 78206c0..202d9f8 100644 --- a/lib/umap-js.js +++ b/lib/umap-js.js @@ -1606,7 +1606,7 @@ var UMAP = (function () { shouldStop = epochCallback(epochCompleted) === false; isFinished = epochCompleted === nEpochs; if (!shouldStop && !isFinished) { - step(); + setTimeout(function () { return step(); }, 0); } else { return [2, resolve(isFinished)]; @@ -1618,7 +1618,7 @@ var UMAP = (function () { return [2]; }); }); }; - step(); + setTimeout(function () { return step(); }, 0); }); }; UMAP.prototype.optimizeLayout = function (epochCallback) { diff --git a/lib/umap-js.min.js b/lib/umap-js.min.js index 0770fbb..220642a 100644 --- a/lib/umap-js.min.js +++ b/lib/umap-js.min.js @@ -1 +1 @@ -!function(t){var r={};function e(n){if(r[n])return r[n].exports;var i=r[n]={i:n,l:!1,exports:{}};return t[n].call(i.exports,i,i.exports,e),i.l=!0,i.exports}e.m=t,e.c=r,e.d=function(t,r,n){e.o(t,r)||Object.defineProperty(t,r,{enumerable:!0,get:n})},e.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},e.t=function(t,r){if(1&r&&(t=e(t)),8&r)return t;if(4&r&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(e.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&r&&"string"!=typeof t)for(var i in t)e.d(n,i,function(r){return t[r]}.bind(null,i));return n},e.n=function(t){var r=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(r,"a",r),r},e.o=function(t,r){return Object.prototype.hasOwnProperty.call(t,r)},e.p="",e(e.s=5)}([function(t,r,e){"use strict";const n=Object.prototype.toString;t.exports=function(t){return n.call(t).endsWith("Array]")}},function(t,r,e){"use strict";var n=this&&this.__values||function(t){var r="function"==typeof Symbol&&t[Symbol.iterator],e=0;return r?r.call(t):{next:function(){return t&&e>=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}};function i(t,r){return Math.floor(r()*t)}function o(t){for(var r=[],e=0;er?t[e]:r;return r},r.max2d=function(t){for(var r=0,e=0;er?t[e][n]:r;return r},r.rejectionSample=function(t,r,e){for(var n=a(t),o=0;o=s[0])return 0;for(var h=0;h=s[0])return 0;s[0]=e,o[0]=n,a[0]=i;for(var h=0,u=0;;){var l=2*h+1,c=l+1,f=t[0][0].length;if(l>=f)break;if(c>=f){if(!(s[l]>e))break;u=l}else if(s[l]>=s[c]){if(!(ee.length;a++)1===i[a]&&n[a]=0?(i[s]=0,Math.floor(e[s])):-1}},function(t,r,e){"use strict";var n,i=this&&this.__read||function(t,r){var e="function"==typeof Symbol&&t[Symbol.iterator];if(!e)return t;var n,i,o=e.call(t),s=[];try{for(;(void 0===r||r-- >0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},o=this&&this.__spread||function(){for(var t=[],r=0;r=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}},a=this&&this.__importStar||function(t){if(t&&t.__esModule)return t;var r={};if(null!=t)for(var e in t)Object.hasOwnProperty.call(t,e)&&(r[e]=t[e]);return r.default=t,r};Object.defineProperty(r,"__esModule",{value:!0});var h=a(e(1)),u=function(){function t(t,r,e,n){this.entries=new Map,this.nRows=0,this.nCols=0,this.rows=o(t),this.cols=o(r),this.values=o(e);for(var i=0;ir?t:r})},r.multiplyScalar=function(t,r){return t.map(function(t){return t*r})},r.eliminateZeros=function(t){for(var r=new Set,e=t.getValues(),n=t.getRows(),i=t.getCols(),o=0;or?t[e]:r;return t.map(function(t){return t/r})},n.l1=function(t){for(var r=0,e=0;e0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},i=this&&this.__spread||function(){for(var t=[],r=0;r=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}},s=this&&this.__importStar||function(t){if(t&&t.__esModule)return t;var r={};if(null!=t)for(var e in t)Object.hasOwnProperty.call(t,e)&&(r[e]=t[e]);return r.default=t,r};Object.defineProperty(r,"__esModule",{value:!0});var a=s(e(1)),h=function(){return function(t,r,e,n){this.hyperplanes=t,this.offsets=r,this.children=e,this.indices=n}}();function u(t,r,e,n){for(var i=r,o=0;o0?0:1}r.FlatTree=h,r.makeForest=function(t,r,e,n){var o=Math.max(10,r);return a.range(e).map(function(r,e){return function(t,r,e,n){void 0===r&&(r=30);var i=a.range(t.length);return function t(r,e,n,i,o){if(void 0===n&&(n=30),e.length>n){var s=function(t,r,e){var n=t[0].length,i=a.tauRandInt(r.length,e),o=a.tauRandInt(r.length,e);o=(o+=i===o?1:0)%r.length;for(var s=r[i],h=r[o],u=0,l=a.zeros(n),c=0;c0?(g[c]=0,f+=1):(g[c]=1,m+=1)}var d=a.zeros(f),w=a.zeros(m);for(var c in f=0,m=0,a.range(g.length))0===g[c]?(d[f]=r[c],f+=1):(w[m]=r[c],m+=1);return{indicesLeft:d,indicesRight:w,hyperplane:l,offset:u}}(r,e,o),h=s.indicesLeft,u=s.indicesRight,l=s.hyperplane,c=s.offset,f=t(r,h,n,i+1,o),m=t(r,u,n,i+1,o),g={leftChild:f,rightChild:m,isLeaf:!1,hyperplane:l,offset:c};return g}var g={indices:e,isLeaf:!0};return g}(t,i,r,e,n)}(t,o,e,n)}).map(function(t){return function(t,r){var e=function t(r){return r.isLeaf?1:1+t(r.leftChild)+t(r.rightChild)}(t),n=function t(r){return r.isLeaf?1:t(r.leftChild)+t(r.rightChild)}(t),o=a.range(e).map(function(){return a.zeros(t.hyperplane.length)}),s=a.zeros(e),u=a.range(e).map(function(){return[-1,-1]}),l=a.range(n).map(function(){return a.range(r).map(function(){return-1})});return function t(r,e,n,o,s,a,h){var u;if(r.isLeaf)return o[a][0]=-h,(u=s[h]).splice.apply(u,i([0,r.indices.length],r.indices)),{nodeNum:a,leafNum:h+=1};e[a]=r.hyperplane,n[a]=r.offset,o[a][0]=a+1;var l=a,c=t(r.leftChild,e,n,o,s,a+1,h);return a=c.nodeNum,h=c.leafNum,o[l][1]=a+1,{nodeNum:(c=t(r.rightChild,e,n,o,s,a+1,h)).nodeNum,leafNum:c.leafNum}}(t,o,s,u,l,0,0),new h(o,s,u,l)}(t,o)})},r.makeLeafArray=function(t){var r,e;if(t.length>0){var n=[];try{for(var s=o(t),a=s.next();!a.done;a=s.next()){var h=a.value;n.push.apply(n,i(h.indices))}}catch(t){r={error:t}}finally{try{a&&!a.done&&(e=s.return)&&e.call(s)}finally{if(r)throw r.error}}return n}return[[-1]]},r.searchFlatTree=function(t,r,e){for(var n=0;r.children[n][0]>0;)n=0===u(r.hyperplanes[n],r.offsets[n],t,e)?r.children[n][0]:r.children[n][1];var i=-1*r.children[n][0];return r.indices[i]}},function(t,r,e){"use strict";Object.defineProperty(r,"__esModule",{value:!0});var n=e(6);window.UMAP=n.UMAP},function(t,r,e){"use strict";var n=this&&this.__awaiter||function(t,r,e,n){return new(e||(e=Promise))(function(i,o){function s(t){try{h(n.next(t))}catch(t){o(t)}}function a(t){try{h(n.throw(t))}catch(t){o(t)}}function h(t){t.done?i(t.value):new e(function(r){r(t.value)}).then(s,a)}h((n=n.apply(t,r||[])).next())})},i=this&&this.__generator||function(t,r){var e,n,i,o,s={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function a(o){return function(a){return function(o){if(e)throw new TypeError("Generator is already executing.");for(;s;)try{if(e=1,n&&(i=2&o[0]?n.return:o[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,o[1])).done)return i;switch(n=0,i&&(o=[2&o[0],i.value]),o[0]){case 0:case 1:i=o;break;case 4:return s.label++,{value:o[1],done:!1};case 5:s.label++,n=o[1],o=[0];continue;case 7:o=s.ops.pop(),s.trys.pop();continue;default:if(!(i=(i=s.trys).length>0&&i[i.length-1])&&(6===o[0]||2===o[0])){s=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},s=this&&this.__spread||function(){for(var t=[],r=0;r0&&i.set(o,u,c)}var f=l.transpose(i);return l.maximum(i,f)},t.prototype.transform=function(t){var r=this,e=this.X;if(void 0===e||0===e.length)throw new Error("No data has been fit.");var n=Math.floor(this.nNeighbors*this.transformQueueSize),i=c.initializeSearch(this.rpForest,e,t,n,this.initFromRandom,this.initFromTree,this.random),o=this.search(e,this.searchGraph,i,t),s=u.deheapSort(o),a=s.indices,h=s.weights;a=a.map(function(t){return t.slice(0,r.nNeighbors)}),h=h.map(function(t){return t.slice(0,r.nNeighbors)});var f=Math.max(0,this.localConnectivity-1),g=this.smoothKNNDistance(h,this.nNeighbors,f),p=g.sigmas,v=g.rhos,d=this.computeMembershipStrengths(a,h,p,v),w=d.rows,y=d.cols,b=d.vals,M=[t.length,e.length],x=new l.SparseMatrix(w,y,b,M),E=l.normalize(x,"l1"),R=l.getCSR(E),k=t.length,z=S(m.reshape2d(R.indices,k,this.nNeighbors),m.reshape2d(R.values,k,this.nNeighbors),this.embedding),N=this.nEpochs?this.nEpochs/3:x.nRows<=1e4?100:30,A=x.getValues().reduce(function(t,r){return r>t?r:t},0);x=x.map(function(t){return t0});if(g.length>=e){var p=Math.floor(e),v=e-p;p>0?(s[h]=g[p-1],v>1e-5&&(s[h]+=v*(g[p]-g[p-1]))):s[h]=v*g[0]}else g.length>0&&(s[h]=m.max(g));for(var d=0;d0?Math.exp(-b/c):1}if(Math.abs(w-o)<1e-5)break;w>o?c=(u+(l=c))/2:(u=c,l===1/0?c*=2:c=(u+l)/2)}if(a[h]=c,s[h]>0){var M=m.mean(f);a[h]<.001*M&&(a[h]=.001*M)}else{var x=m.mean(t.map(m.mean));a[h]<.001*x&&(a[h]=.001*x)}}return{sigmas:a,rhos:s}},t.prototype.computeMembershipStrengths=function(t,r,e,n){for(var i=t.length,o=t[0].length,s=m.zeros(i*o),a=m.zeros(i*o),h=m.zeros(i*o),u=0;u0&&(e[n]=r/i[n])}),e},t.prototype.assignOptimizationStateParameters=function(t){Object.assign(this.optimizationState,t)},t.prototype.prepareForOptimizationLoop=function(){var t=this.repulsionStrength,r=this.learningRate,e=this.negativeSampleRate,n=this.optimizationState,i=n.epochsPerSample,o=n.headEmbedding,a=n.tailEmbedding,h=o[0].length,u=o.length===a.length,l=i.map(function(t){return t/e}),c=s(l),f=s(i);this.assignOptimizationStateParameters({epochOfNextSample:f,epochOfNextNegativeSample:c,epochsPerNegativeSample:l,moveOther:u,initialAlpha:r,alpha:r,gamma:t,dim:h})},t.prototype.initializeOptimization=function(){var t=this.embedding,r=this.embedding,e=this.optimizationState,n=e.head,i=e.tail,o=e.epochsPerSample,s=this.getNEpochs(),a=this.graph.nCols,h=b(this.spread,this.minDist),u=h.a,l=h.b;this.assignOptimizationStateParameters({headEmbedding:t,tailEmbedding:r,head:n,tail:i,epochsPerSample:o,a:u,b:l,nEpochs:s,nVertices:a})},t.prototype.optimizeLayoutStep=function(t){for(var r=this.optimizationState,e=r.head,n=r.tail,i=r.headEmbedding,o=r.tailEmbedding,s=r.epochsPerSample,a=r.epochOfNextSample,h=r.epochOfNextNegativeSample,u=r.epochsPerNegativeSample,l=r.moveOther,c=r.initialAlpha,f=r.alpha,g=r.gamma,p=r.a,v=r.b,d=r.dim,b=r.nEpochs,M=r.nVertices,x=0;xt)){var S=e[x],E=n[x],R=i[S],k=o[E],z=y(R,k),N=0;z>0&&(N=-2*p*v*Math.pow(z,v-1),N/=p*Math.pow(z,v)+1);for(var A=0;A0)O=2*g*v,O/=(.001+j)*(p*Math.pow(j,v)+1);else if(S===P)continue;for(A=0;A0&&(V=w(O*(R[A]-I[A]),4)),R[A]+=V*f}}h[x]+=C*u[x]}return r.alpha=c*(1-t/b),r.currentEpoch+=1,i},t.prototype.optimizeLayoutAsync=function(t){var r=this;return void 0===t&&(t=function(){return!0}),new Promise(function(e,o){var s=function(){return n(r,void 0,void 0,function(){var r,n,a,h,u,l;return i(this,function(i){try{if(r=this.optimizationState,n=r.nEpochs,a=r.currentEpoch,this.embedding=this.optimizeLayoutStep(a),h=this.optimizationState.currentEpoch,u=!1===t(h),l=h===n,u||l)return[2,e(l)];s()}catch(t){o(t)}return[2]})})};s()})},t.prototype.optimizeLayout=function(t){void 0===t&&(t=function(){return!0});for(var r=!1,e=[];!r;){var n=this.optimizationState,i=n.nEpochs,o=n.currentEpoch;e=this.optimizeLayoutStep(o);var s=this.optimizationState.currentEpoch,a=!1===t(s);r=s===i||a}return e},t.prototype.getNEpochs=function(){var t=this.graph;if(this.nEpochs>0)return this.nEpochs;var r=t.nRows;return r<=2500?500:r<=5e3?400:r<=7500?300:200},t}();function v(t,r){for(var e=0,n=0;nr?r:t<-r?-r:t}function y(t,r){for(var e=0,n=0;n=r?Math.exp(-(e[i]-r)/t):n}),i={x:e,y:n},s={damping:1.5,initialValues:[.5,.5],gradientDifference:.1,maxIterations:100,errorTolerance:.01},a=g.default(i,function(t){var r=o(t,2),e=r[0],n=r[1];return function(t){return 1/(1+e*Math.pow(t,2*n))}},s).parameterValues,h=o(a,2);return{a:h[0],b:h[1]}}function M(t,r,e,n){return void 0===e&&(e=1),void 0===n&&(n=5),t.map(function(t,i,o){return-1===r[i]||-1===r[o]?t*Math.exp(-e):r[i]!==r[o]?t*Math.exp(-n):t})}function x(t){t=l.normalize(t,"max");var r=l.transpose(t),e=l.pairwiseMultiply(r,t);return t=l.add(t,l.subtract(r,e)),l.eliminateZeros(t)}function S(t,r,e){for(var n=m.zeros(t.length).map(function(t){return m.zeros(e[0].length)}),i=0;i=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}},i=this&&this.__importStar||function(t){if(t&&t.__esModule)return t;var r={};if(null!=t)for(var e in t)Object.hasOwnProperty.call(t,e)&&(r[e]=t[e]);return r.default=t,r};Object.defineProperty(r,"__esModule",{value:!0});var o=i(e(2)),s=i(e(3)),a=i(e(4)),h=i(e(1));r.makeNNDescent=function(t,r){return function(e,n,i,s,a,u,l,c){void 0===s&&(s=10),void 0===a&&(a=50),void 0===u&&(u=.001),void 0===l&&(l=.5),void 0===c&&(c=!0);for(var f=e.length,m=o.makeHeap(e.length,i),g=0;gr&&(r=t[e]);return r};var s=function(t){if(!i()(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");for(var r=t[0],e=1;e1&&void 0!==arguments[1]?arguments[1]:{};if(!i()(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");if(void 0!==e.output){if(!i()(e.output))throw new TypeError("output option must be an array if specified");r=e.output}else r=new Array(t.length);var n=s(t),a=o(t);if(n===a)throw new RangeError("minimum and maximum input values are equal. Cannot rescale a constant array");var h=e.min,u=void 0===h?e.autoMinMax?n:0:h,l=e.max,c=void 0===l?e.autoMinMax?a:1:l;if(u>=c)throw new RangeError("min option must be smaller than max option");for(var f=(c-u)/(a-n),m=0;mMath.abs(h[i])&&(i=r);if(i!==e){for(n=0;n=0;i--){for(n=0;no?t[i][o]:i===o?1:0;return n}get upperTriangularMatrix(){for(var t=this.LU,r=t.rows,e=t.columns,n=new _(r,e),i=0;iMath.abs(r)?(e=r/t,Math.abs(t)*Math.sqrt(1+e*e)):0!==r?(e=t/r,Math.abs(r)*Math.sqrt(1+e*e)):0}function l(t,r,e){for(var n=new Array(t),i=0;i=0;t--)if(0!==v[t]){for(let r=t+1;r=0;t--){if(t0;){let t,r;for(t=R-2;t>=-1&&-1!==t;t--){const r=Number.MIN_VALUE+z*Math.abs(v[t]+Math.abs(v[t+1]));if(Math.abs(y[t])<=r||Number.isNaN(y[t])){y[t]=0;break}}if(t===R-2)r=4;else{let e;for(e=R-1;e>=t&&e!==t;e--){let r=(e!==R?Math.abs(y[e]):0)+(e!==t+1?Math.abs(y[e-1]):0);if(Math.abs(v[e])<=z*r){v[e]=0;break}}e===t?r=3:e===R-1?r=1:(r=2,t=e)}switch(t++,r){case 1:{let r=y[R-2];y[R-2]=0;for(let e=R-2;e>=t;e--){let i=u(v[e],r),o=v[e]/i,s=r/i;if(v[e]=i,e!==t&&(r=-s*y[e-1],y[e-1]=o*y[e-1]),c)for(let t=0;t=v[t+1]);){let r=v[t];if(v[t]=v[t+1],v[t+1]=r,c&&tr?i[o][e]=t[o][e]/this.s[e]:i[o][e]=0;var o=this.U,s=o.length,a=o[0].length,h=new _(e,s);for(let t=0;tt&&r++;return r}get diagonal(){return this.s}get threshold(){return Number.EPSILON/2*Math.max(this.m,this.n)*this.s[0]}get leftSingularVectors(){return _.isMatrix(this.U)||(this.U=new _(this.U)),this.U}get rightSingularVectors(){return _.isMatrix(this.V)||(this.V=new _(this.V)),this.V}get diagonalMatrix(){return _.diag(this.s)}}function f(t,r,e){var n=e?t.rows:t.rows-1;if(r<0||r>n)throw new RangeError("Row index out of range")}function m(t,r,e){var n=e?t.columns:t.columns-1;if(r<0||r>n)throw new RangeError("Column index out of range")}function g(t,r){if(r.to1DArray&&(r=r.to1DArray()),r.length!==t.columns)throw new RangeError("vector size must be the same as the number of columns");return r}function p(t,r){if(r.to1DArray&&(r=r.to1DArray()),r.length!==t.rows)throw new RangeError("vector size must be the same as the number of rows");return r}function v(t,r,e){return{row:d(t,r),column:w(t,e)}}function d(t,r){if("object"!=typeof r)throw new TypeError("unexpected type for row indices");if(r.some(r=>r<0||r>=t.rows))throw new RangeError("row indices are out of range");return Array.isArray(r)||(r=Array.from(r)),r}function w(t,r){if("object"!=typeof r)throw new TypeError("unexpected type for column indices");if(r.some(r=>r<0||r>=t.columns))throw new RangeError("column indices are out of range");return Array.isArray(r)||(r=Array.from(r)),r}function y(t,r,e,n,i){if(5!==arguments.length)throw new RangeError("expected 4 arguments");if(b("startRow",r),b("endRow",e),b("startColumn",n),b("endColumn",i),r>e||n>i||r<0||r>=t.rows||e<0||e>=t.rows||n<0||n>=t.columns||i<0||i>=t.columns)throw new RangeError("Submatrix indices are out of range")}function b(t,r){if("number"!=typeof r)throw new TypeError(`${t} must be a number`)}class M extends(C()){constructor(t,r,e){super(),this.matrix=t,this.rows=r,this.columns=e}static get[Symbol.species](){return _}}class x extends M{constructor(t){super(t,t.columns,t.rows)}set(t,r,e){return this.matrix.set(r,t,e),this}get(t,r){return this.matrix.get(r,t)}}class S extends M{constructor(t,r){super(t,1,t.columns),this.row=r}set(t,r,e){return this.matrix.set(this.row,r,e),this}get(t,r){return this.matrix.get(this.row,r)}}class E extends M{constructor(t,r,e,n,i){y(t,r,e,n,i),super(t,e-r+1,i-n+1),this.startRow=r,this.startColumn=n}set(t,r,e){return this.matrix.set(this.startRow+t,this.startColumn+r,e),this}get(t,r){return this.matrix.get(this.startRow+t,this.startColumn+r)}}class R extends M{constructor(t,r,e){var n=v(t,r,e);super(t,n.row.length,n.column.length),this.rowIndices=n.row,this.columnIndices=n.column}set(t,r,e){return this.matrix.set(this.rowIndices[t],this.columnIndices[r],e),this}get(t,r){return this.matrix.get(this.rowIndices[t],this.columnIndices[r])}}class k extends M{constructor(t,r){super(t,(r=d(t,r)).length,t.columns),this.rowIndices=r}set(t,r,e){return this.matrix.set(this.rowIndices[t],r,e),this}get(t,r){return this.matrix.get(this.rowIndices[t],r)}}class z extends M{constructor(t,r){r=w(t,r),super(t,t.rows,r.length),this.columnIndices=r}set(t,r,e){return this.matrix.set(t,this.columnIndices[r],e),this}get(t,r){return this.matrix.get(t,this.columnIndices[r])}}class N extends M{constructor(t,r){super(t,t.rows,1),this.column=r}set(t,r,e){return this.matrix.set(t,this.column,e),this}get(t){return this.matrix.get(t,this.column)}}class A extends M{constructor(t){super(t,t.rows,t.columns)}set(t,r,e){return this.matrix.set(this.rows-t-1,r,e),this}get(t,r){return this.matrix.get(this.rows-t-1,r)}}class V extends M{constructor(t){super(t,t.rows,t.columns)}set(t,r,e){return this.matrix.set(t,this.columns-r-1,e),this}get(t,r){return this.matrix.get(t,this.columns-r-1)}}function C(t){void 0===t&&(t=Object);class r extends t{static get[Symbol.species](){return this}static from1DArray(t,r,e){if(t*r!==e.length)throw new RangeError("Data length does not match given dimensions");for(var n=new this(t,r),i=0;it&&(t=this.get(r,e));return t}maxIndex(){for(var t=this.get(0,0),r=[0,0],e=0;et&&(t=this.get(e,n),r[0]=e,r[1]=n);return r}min(){for(var t=this.get(0,0),r=0;rr&&(r=this.get(t,e));return r}maxRowIndex(t){f(this,t);for(var r=this.get(t,0),e=[t,0],n=1;nr&&(r=this.get(t,n),e[1]=n);return e}minRow(t){f(this,t);for(var r=this.get(t,0),e=1;er&&(r=this.get(e,t));return r}maxColumnIndex(t){m(this,t);for(var r=this.get(0,t),e=[0,t],n=1;nr&&(r=this.get(n,t),e[0]=n);return e}minColumn(t){m(this,t);for(var r=this.get(0,t),e=1;e=(r=void 0===r?1:r))throw new RangeError("min should be strictly smaller than max");for(var e=this.constructor.empty(this.rows,this.columns),n=0;n=(r=void 0===r?1:r))throw new RangeError("min should be strictly smaller than max");for(var e=this.constructor.empty(this.rows,this.columns),n=0;ne||r<0||r>=this.columns||e<0||e>=this.columns)throw new RangeError("Argument out of range");for(var n=new this.constructor[Symbol.species](t.length,e-r+1),i=0;i=this.rows)throw new RangeError(`Row index out of range: ${t[i]}`);n.set(i,o-r,this.get(t[i],o))}return n}subMatrixColumn(t,r,e){if(void 0===r&&(r=0),void 0===e&&(e=this.rows-1),r>e||r<0||r>=this.rows||e<0||e>=this.rows)throw new RangeError("Argument out of range");for(var n=new this.constructor[Symbol.species](e-r+1,t.length),i=0;i=this.columns)throw new RangeError(`Column index out of range: ${t[i]}`);n.set(o-r,i,this.get(o,t[i]))}return n}setSubMatrix(t,r,e){y(this,r,r+(t=this.constructor.checkMatrix(t)).rows-1,e,e+t.columns-1);for(var n=0;nt?i[o]=1/i[o]:i[o]=0;return i=this.constructor[Symbol.species].diag(i),n.mmul(i.mmul(e.transposeView()))}clone(){for(var t=new this.constructor[Symbol.species](this.rows,this.columns),r=0;r>","signPropagatingRightShift"],[">>>","rightShift","zeroFillRightShift"]]){var u=o($("\n(function %name%(value) {\n if (typeof value === 'number') return this.%name%S(value);\n return this.%name%M(value);\n})\n",{name:s[1],op:s[0]})),l=o($("\n(function %name%S(value) {\n for (var i = 0; i < this.rows; i++) {\n for (var j = 0; j < this.columns; j++) {\n this.set(i, j, this.get(i, j) %op% value);\n }\n }\n return this;\n})\n",{name:`${s[1]}S`,op:s[0]})),d=o($("\n(function %name%M(matrix) {\n matrix = this.constructor.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 (var i = 0; i < this.rows; i++) {\n for (var j = 0; j < this.columns; j++) {\n this.set(i, j, this.get(i, j) %op% matrix.get(i, j));\n }\n }\n return this;\n})\n",{name:`${s[1]}M`,op:s[0]})),w=o($("\n(function %name%(matrix, value) {\n var newMatrix = new this[Symbol.species](matrix);\n return newMatrix.%name%(value);\n})\n",{name:s[1]}));for(n=1;n0){if(super(t),!(Number.isInteger(r)&&r>0))throw new TypeError("nColumns must be a positive integer");for(e=0;e=0;o--){for(i=0;i=0;e--){for(t=0;ti)return new Array(r.rows+1).fill(0);for(var o=r.addRow(e,[0]),s=0;s0;a--){for(c=0,s=0,u=0;u0&&(o=-o),r[a]=c*o,s-=i*o,e[a-1]=i-o,h=0;hl){0;do{for(1,i=e[l],f=(e[l+1]-i)/(2*r[l]),m=u(f,1),f<0&&(m=-m),e[l]=r[l]/(f+m),e[l+1]=r[l]*(f+m),g=e[l+1],o=i-e[l],s=l+2;s=l;s--)for(d=v,v=p,b=y,i=p*r[s],o=p*f,m=u(f,r[s]),r[s+1]=y*m,y=r[s]/m,f=(p=f/m)*e[s]-y*i,e[s+1]=o+y*(p*i+y*e[s]),h=0;hS*x)}e[l]=e[l]+M,r[l]=0}for(s=0;s=u;a--)e[a]=r[a][u-1]/l,s+=e[a]*e[a];for(o=Math.sqrt(s),e[u]>0&&(o=-o),s-=e[u]*o,e[u]=e[u]-o,h=u;h=u;a--)i+=e[a]*r[a][h];for(i/=s,a=u;a<=c;a++)r[a][h]-=i*e[a]}for(a=0;a<=c;a++){for(i=0,h=c;h>=u;h--)i+=e[h]*r[a][h];for(i/=s,h=u;h<=c;h++)r[a][h]-=i*e[h]}e[u]=l*e[u],r[u][u-1]=l*o}}for(a=0;a=1;u--)if(0!==r[u][u-1]){for(a=u+1;a<=c;a++)e[a]=r[a][u-1];for(h=u;h<=c;h++){for(o=0,a=u;a<=c;a++)o+=e[a]*n[a][h];for(o=o/e[u]/r[u][u-1],a=u;a<=c;a++)n[a][h]+=o*e[a]}}}(o,f,m,s),function(t,r,e,n,i){var o,s,a,h,u,l,c,f,m,g,p,v,d,w,y,b=t-1,M=t-1,x=Number.EPSILON,S=0,E=0,R=0,k=0,z=0,N=0,A=0,V=0;for(o=0;oM)&&(e[o]=i[o][o],r[o]=0),s=Math.max(o-1,0);s=0;){for(h=b;h>0&&(0===(N=Math.abs(i[h-1][h-1])+Math.abs(i[h][h]))&&(N=E),!(Math.abs(i[h][h-1])=0){for(A=R>=0?R+A:R-A,e[b-1]=f+A,e[b]=e[b-1],0!==A&&(e[b]=f-c/A),r[b-1]=0,r[b]=0,f=i[b][b-1],N=Math.abs(f)+Math.abs(A),R=f/N,k=A/N,z=Math.sqrt(R*R+k*k),R/=z,k/=z,s=b-1;s0){for(N=Math.sqrt(N),m=h&&(A=i[u][u],R=((z=f-A)*(N=m-A)-c)/i[u+1][u]+i[u][u+1],k=i[u+1][u+1]-A-z-N,z=i[u+2][u+1],N=Math.abs(R)+Math.abs(k)+Math.abs(z),R/=N,k/=N,z/=N,u!==h)&&!(Math.abs(i[u][u-1])*(Math.abs(k)+Math.abs(z))u+2&&(i[o][o-3]=0);for(a=u;a<=b-1&&(w=a!==b-1,a!==u&&(R=i[a][a-1],k=i[a+1][a-1],z=w?i[a+2][a-1]:0,0!==(f=Math.abs(R)+Math.abs(k)+Math.abs(z))&&(R/=f,k/=f,z/=f)),0!==f);a++)if(N=Math.sqrt(R*R+k*k+z*z),R<0&&(N=-N),0!==N){for(a!==u?i[a][a-1]=-N*f:h!==u&&(i[a][a-1]=-i[a][a-1]),f=(R+=N)/N,m=k/N,A=z/N,k/=R,z/=R,s=a;s=0;b--)if(R=e[b],0===(k=r[b]))for(h=b,i[b][b]=1,o=b-1;o>=0;o--){for(c=i[o][o]-R,z=0,s=h;s<=b;s++)z+=i[o][s]*i[s][b];if(r[o]<0)A=c,N=z;else if(h=o,0===r[o]?i[o][b]=0!==c?-z/c:-z/(x*E):(f=i[o][o+1],m=i[o+1][o],k=(e[o]-R)*(e[o]-R)+r[o]*r[o],l=(f*N-A*z)/k,i[o][b]=l,i[o+1][b]=Math.abs(f)>Math.abs(A)?(-z-c*l)/f:(-N-m*l)/A),l=Math.abs(i[o][b]),x*l*l>1)for(s=o;s<=b;s++)i[s][b]=i[s][b]/l}else if(k<0)for(h=b-1,Math.abs(i[b][b-1])>Math.abs(i[b-1][b])?(i[b-1][b-1]=k/i[b][b-1],i[b-1][b]=-(i[b][b]-R)/i[b][b-1]):(y=$(0,-i[b-1][b],i[b-1][b-1]-R,k),i[b-1][b-1]=y[0],i[b-1][b]=y[1]),i[b][b-1]=0,i[b][b]=1,o=b-2;o>=0;o--){for(g=0,p=0,s=h;s<=b;s++)g+=i[o][s]*i[s][b-1],p+=i[o][s]*i[s][b];if(c=i[o][o]-R,r[o]<0)A=c,z=g,N=p;else if(h=o,0===r[o]?(y=$(-g,-p,c,k),i[o][b-1]=y[0],i[o][b]=y[1]):(f=i[o][o+1],m=i[o+1][o],v=(e[o]-R)*(e[o]-R)+r[o]*r[o]-k*k,d=2*(e[o]-R)*k,0===v&&0===d&&(v=x*E*(Math.abs(c)+Math.abs(k)+Math.abs(f)+Math.abs(m)+Math.abs(A))),y=$(f*z-A*g+k*p,f*N-A*p-k*g,v,d),i[o][b-1]=y[0],i[o][b]=y[1],Math.abs(f)>Math.abs(A)+Math.abs(k)?(i[o+1][b-1]=(-g-c*i[o][b-1]+k*i[o][b])/f,i[o+1][b]=(-p-c*i[o][b]-k*i[o][b-1])/f):(y=$(-z-m*i[o][b-1],-N-m*i[o][b],A,k),i[o+1][b-1]=y[0],i[o+1][b]=y[1])),l=Math.max(Math.abs(i[o][b-1]),Math.abs(i[o][b])),x*l*l>1)for(s=o;s<=b;s++)i[s][b-1]=i[s][b-1]/l,i[s][b]=i[s][b]/l}for(o=0;oM)for(s=o;s=0;s--)for(o=0;o<=M;o++){for(A=0,a=0;a<=Math.min(s,M);a++)A+=n[o][a]*i[a][s];n[o][s]=A}}(o,h,a,s,f)}this.n=o,this.e=h,this.d=a,this.V=s}get realEigenvalues(){return this.d}get imaginaryEigenvalues(){return this.e}get eigenvectorMatrix(){return _.isMatrix(this.V)||(this.V=new _(this.V)),this.V}get diagonalMatrix(){var t,r,e=this.n,n=this.e,i=this.d,o=new _(e,e);for(t=0;t0?o[t][t+1]=n[t]:n[t]<0&&(o[t][t-1]=n[t])}return o}}function $(t,r,e,n){var i,o;return Math.abs(e)>Math.abs(n)?[(t+(i=n/e)*r)/(o=e+i*n),(r-i*t)/o]:[((i=e/n)*t+r)/(o=n+i*e),(i*r-t)/o]}class Q{constructor(t){if(!(t=I.checkMatrix(t)).isSymmetric())throw new Error("Matrix is not symmetric");var r,e,n,i=t,o=i.rows,s=new _(o,o),a=!0;for(e=0;e0,s[e][e]=Math.sqrt(Math.max(u,0)),n=e+1;n=0;o--)for(i=0;i=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}};function i(t,r){return Math.floor(r()*t)}function o(t){for(var r=[],e=0;er?t[e]:r;return r},r.max2d=function(t){for(var r=0,e=0;er?t[e][n]:r;return r},r.rejectionSample=function(t,r,e){for(var n=a(t),o=0;o=s[0])return 0;for(var h=0;h=s[0])return 0;s[0]=e,o[0]=n,a[0]=i;for(var h=0,u=0;;){var l=2*h+1,c=l+1,f=t[0][0].length;if(l>=f)break;if(c>=f){if(!(s[l]>e))break;u=l}else if(s[l]>=s[c]){if(!(ee.length;a++)1===i[a]&&n[a]=0?(i[s]=0,Math.floor(e[s])):-1}},function(t,r,e){"use strict";var n,i=this&&this.__read||function(t,r){var e="function"==typeof Symbol&&t[Symbol.iterator];if(!e)return t;var n,i,o=e.call(t),s=[];try{for(;(void 0===r||r-- >0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},o=this&&this.__spread||function(){for(var t=[],r=0;r=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}},a=this&&this.__importStar||function(t){if(t&&t.__esModule)return t;var r={};if(null!=t)for(var e in t)Object.hasOwnProperty.call(t,e)&&(r[e]=t[e]);return r.default=t,r};Object.defineProperty(r,"__esModule",{value:!0});var h=a(e(1)),u=function(){function t(t,r,e,n){this.entries=new Map,this.nRows=0,this.nCols=0,this.rows=o(t),this.cols=o(r),this.values=o(e);for(var i=0;ir?t:r})},r.multiplyScalar=function(t,r){return t.map(function(t){return t*r})},r.eliminateZeros=function(t){for(var r=new Set,e=t.getValues(),n=t.getRows(),i=t.getCols(),o=0;or?t[e]:r;return t.map(function(t){return t/r})},n.l1=function(t){for(var r=0,e=0;e0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},i=this&&this.__spread||function(){for(var t=[],r=0;r=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}},s=this&&this.__importStar||function(t){if(t&&t.__esModule)return t;var r={};if(null!=t)for(var e in t)Object.hasOwnProperty.call(t,e)&&(r[e]=t[e]);return r.default=t,r};Object.defineProperty(r,"__esModule",{value:!0});var a=s(e(1)),h=function(){return function(t,r,e,n){this.hyperplanes=t,this.offsets=r,this.children=e,this.indices=n}}();function u(t,r,e,n){for(var i=r,o=0;o0?0:1}r.FlatTree=h,r.makeForest=function(t,r,e,n){var o=Math.max(10,r);return a.range(e).map(function(r,e){return function(t,r,e,n){void 0===r&&(r=30);var i=a.range(t.length);return function t(r,e,n,i,o){if(void 0===n&&(n=30),e.length>n){var s=function(t,r,e){var n=t[0].length,i=a.tauRandInt(r.length,e),o=a.tauRandInt(r.length,e);o=(o+=i===o?1:0)%r.length;for(var s=r[i],h=r[o],u=0,l=a.zeros(n),c=0;c0?(g[c]=0,f+=1):(g[c]=1,m+=1)}var d=a.zeros(f),w=a.zeros(m);for(var c in f=0,m=0,a.range(g.length))0===g[c]?(d[f]=r[c],f+=1):(w[m]=r[c],m+=1);return{indicesLeft:d,indicesRight:w,hyperplane:l,offset:u}}(r,e,o),h=s.indicesLeft,u=s.indicesRight,l=s.hyperplane,c=s.offset,f=t(r,h,n,i+1,o),m=t(r,u,n,i+1,o),g={leftChild:f,rightChild:m,isLeaf:!1,hyperplane:l,offset:c};return g}var g={indices:e,isLeaf:!0};return g}(t,i,r,e,n)}(t,o,e,n)}).map(function(t){return function(t,r){var e=function t(r){return r.isLeaf?1:1+t(r.leftChild)+t(r.rightChild)}(t),n=function t(r){return r.isLeaf?1:t(r.leftChild)+t(r.rightChild)}(t),o=a.range(e).map(function(){return a.zeros(t.hyperplane.length)}),s=a.zeros(e),u=a.range(e).map(function(){return[-1,-1]}),l=a.range(n).map(function(){return a.range(r).map(function(){return-1})});return function t(r,e,n,o,s,a,h){var u;if(r.isLeaf)return o[a][0]=-h,(u=s[h]).splice.apply(u,i([0,r.indices.length],r.indices)),{nodeNum:a,leafNum:h+=1};e[a]=r.hyperplane,n[a]=r.offset,o[a][0]=a+1;var l=a,c=t(r.leftChild,e,n,o,s,a+1,h);return a=c.nodeNum,h=c.leafNum,o[l][1]=a+1,{nodeNum:(c=t(r.rightChild,e,n,o,s,a+1,h)).nodeNum,leafNum:c.leafNum}}(t,o,s,u,l,0,0),new h(o,s,u,l)}(t,o)})},r.makeLeafArray=function(t){var r,e;if(t.length>0){var n=[];try{for(var s=o(t),a=s.next();!a.done;a=s.next()){var h=a.value;n.push.apply(n,i(h.indices))}}catch(t){r={error:t}}finally{try{a&&!a.done&&(e=s.return)&&e.call(s)}finally{if(r)throw r.error}}return n}return[[-1]]},r.searchFlatTree=function(t,r,e){for(var n=0;r.children[n][0]>0;)n=0===u(r.hyperplanes[n],r.offsets[n],t,e)?r.children[n][0]:r.children[n][1];var i=-1*r.children[n][0];return r.indices[i]}},function(t,r,e){"use strict";Object.defineProperty(r,"__esModule",{value:!0});var n=e(6);window.UMAP=n.UMAP},function(t,r,e){"use strict";var n=this&&this.__awaiter||function(t,r,e,n){return new(e||(e=Promise))(function(i,o){function s(t){try{h(n.next(t))}catch(t){o(t)}}function a(t){try{h(n.throw(t))}catch(t){o(t)}}function h(t){t.done?i(t.value):new e(function(r){r(t.value)}).then(s,a)}h((n=n.apply(t,r||[])).next())})},i=this&&this.__generator||function(t,r){var e,n,i,o,s={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function a(o){return function(a){return function(o){if(e)throw new TypeError("Generator is already executing.");for(;s;)try{if(e=1,n&&(i=2&o[0]?n.return:o[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,o[1])).done)return i;switch(n=0,i&&(o=[2&o[0],i.value]),o[0]){case 0:case 1:i=o;break;case 4:return s.label++,{value:o[1],done:!1};case 5:s.label++,n=o[1],o=[0];continue;case 7:o=s.ops.pop(),s.trys.pop();continue;default:if(!(i=(i=s.trys).length>0&&i[i.length-1])&&(6===o[0]||2===o[0])){s=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},s=this&&this.__spread||function(){for(var t=[],r=0;r0&&i.set(o,u,c)}var f=l.transpose(i);return l.maximum(i,f)},t.prototype.transform=function(t){var r=this,e=this.X;if(void 0===e||0===e.length)throw new Error("No data has been fit.");var n=Math.floor(this.nNeighbors*this.transformQueueSize),i=c.initializeSearch(this.rpForest,e,t,n,this.initFromRandom,this.initFromTree,this.random),o=this.search(e,this.searchGraph,i,t),s=u.deheapSort(o),a=s.indices,h=s.weights;a=a.map(function(t){return t.slice(0,r.nNeighbors)}),h=h.map(function(t){return t.slice(0,r.nNeighbors)});var f=Math.max(0,this.localConnectivity-1),g=this.smoothKNNDistance(h,this.nNeighbors,f),p=g.sigmas,v=g.rhos,d=this.computeMembershipStrengths(a,h,p,v),w=d.rows,y=d.cols,b=d.vals,M=[t.length,e.length],x=new l.SparseMatrix(w,y,b,M),E=l.normalize(x,"l1"),R=l.getCSR(E),k=t.length,z=S(m.reshape2d(R.indices,k,this.nNeighbors),m.reshape2d(R.values,k,this.nNeighbors),this.embedding),N=this.nEpochs?this.nEpochs/3:x.nRows<=1e4?100:30,A=x.getValues().reduce(function(t,r){return r>t?r:t},0);x=x.map(function(t){return t0});if(g.length>=e){var p=Math.floor(e),v=e-p;p>0?(s[h]=g[p-1],v>1e-5&&(s[h]+=v*(g[p]-g[p-1]))):s[h]=v*g[0]}else g.length>0&&(s[h]=m.max(g));for(var d=0;d0?Math.exp(-b/c):1}if(Math.abs(w-o)<1e-5)break;w>o?c=(u+(l=c))/2:(u=c,l===1/0?c*=2:c=(u+l)/2)}if(a[h]=c,s[h]>0){var M=m.mean(f);a[h]<.001*M&&(a[h]=.001*M)}else{var x=m.mean(t.map(m.mean));a[h]<.001*x&&(a[h]=.001*x)}}return{sigmas:a,rhos:s}},t.prototype.computeMembershipStrengths=function(t,r,e,n){for(var i=t.length,o=t[0].length,s=m.zeros(i*o),a=m.zeros(i*o),h=m.zeros(i*o),u=0;u0&&(e[n]=r/i[n])}),e},t.prototype.assignOptimizationStateParameters=function(t){Object.assign(this.optimizationState,t)},t.prototype.prepareForOptimizationLoop=function(){var t=this.repulsionStrength,r=this.learningRate,e=this.negativeSampleRate,n=this.optimizationState,i=n.epochsPerSample,o=n.headEmbedding,a=n.tailEmbedding,h=o[0].length,u=o.length===a.length,l=i.map(function(t){return t/e}),c=s(l),f=s(i);this.assignOptimizationStateParameters({epochOfNextSample:f,epochOfNextNegativeSample:c,epochsPerNegativeSample:l,moveOther:u,initialAlpha:r,alpha:r,gamma:t,dim:h})},t.prototype.initializeOptimization=function(){var t=this.embedding,r=this.embedding,e=this.optimizationState,n=e.head,i=e.tail,o=e.epochsPerSample,s=this.getNEpochs(),a=this.graph.nCols,h=b(this.spread,this.minDist),u=h.a,l=h.b;this.assignOptimizationStateParameters({headEmbedding:t,tailEmbedding:r,head:n,tail:i,epochsPerSample:o,a:u,b:l,nEpochs:s,nVertices:a})},t.prototype.optimizeLayoutStep=function(t){for(var r=this.optimizationState,e=r.head,n=r.tail,i=r.headEmbedding,o=r.tailEmbedding,s=r.epochsPerSample,a=r.epochOfNextSample,h=r.epochOfNextNegativeSample,u=r.epochsPerNegativeSample,l=r.moveOther,c=r.initialAlpha,f=r.alpha,g=r.gamma,p=r.a,v=r.b,d=r.dim,b=r.nEpochs,M=r.nVertices,x=0;xt)){var S=e[x],E=n[x],R=i[S],k=o[E],z=y(R,k),N=0;z>0&&(N=-2*p*v*Math.pow(z,v-1),N/=p*Math.pow(z,v)+1);for(var A=0;A0)O=2*g*v,O/=(.001+j)*(p*Math.pow(j,v)+1);else if(S===P)continue;for(A=0;A0&&(V=w(O*(R[A]-I[A]),4)),R[A]+=V*f}}h[x]+=C*u[x]}return r.alpha=c*(1-t/b),r.currentEpoch+=1,i},t.prototype.optimizeLayoutAsync=function(t){var r=this;return void 0===t&&(t=function(){return!0}),new Promise(function(e,o){var s=function(){return n(r,void 0,void 0,function(){var r,n,a,h,u,l;return i(this,function(i){try{if(r=this.optimizationState,n=r.nEpochs,a=r.currentEpoch,this.embedding=this.optimizeLayoutStep(a),h=this.optimizationState.currentEpoch,u=!1===t(h),l=h===n,u||l)return[2,e(l)];setTimeout(function(){return s()},0)}catch(t){o(t)}return[2]})})};setTimeout(function(){return s()},0)})},t.prototype.optimizeLayout=function(t){void 0===t&&(t=function(){return!0});for(var r=!1,e=[];!r;){var n=this.optimizationState,i=n.nEpochs,o=n.currentEpoch;e=this.optimizeLayoutStep(o);var s=this.optimizationState.currentEpoch,a=!1===t(s);r=s===i||a}return e},t.prototype.getNEpochs=function(){var t=this.graph;if(this.nEpochs>0)return this.nEpochs;var r=t.nRows;return r<=2500?500:r<=5e3?400:r<=7500?300:200},t}();function v(t,r){for(var e=0,n=0;nr?r:t<-r?-r:t}function y(t,r){for(var e=0,n=0;n=r?Math.exp(-(e[i]-r)/t):n}),i={x:e,y:n},s={damping:1.5,initialValues:[.5,.5],gradientDifference:.1,maxIterations:100,errorTolerance:.01},a=g.default(i,function(t){var r=o(t,2),e=r[0],n=r[1];return function(t){return 1/(1+e*Math.pow(t,2*n))}},s).parameterValues,h=o(a,2);return{a:h[0],b:h[1]}}function M(t,r,e,n){return void 0===e&&(e=1),void 0===n&&(n=5),t.map(function(t,i,o){return-1===r[i]||-1===r[o]?t*Math.exp(-e):r[i]!==r[o]?t*Math.exp(-n):t})}function x(t){t=l.normalize(t,"max");var r=l.transpose(t),e=l.pairwiseMultiply(r,t);return t=l.add(t,l.subtract(r,e)),l.eliminateZeros(t)}function S(t,r,e){for(var n=m.zeros(t.length).map(function(t){return m.zeros(e[0].length)}),i=0;i=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}},i=this&&this.__importStar||function(t){if(t&&t.__esModule)return t;var r={};if(null!=t)for(var e in t)Object.hasOwnProperty.call(t,e)&&(r[e]=t[e]);return r.default=t,r};Object.defineProperty(r,"__esModule",{value:!0});var o=i(e(2)),s=i(e(3)),a=i(e(4)),h=i(e(1));r.makeNNDescent=function(t,r){return function(e,n,i,s,a,u,l,c){void 0===s&&(s=10),void 0===a&&(a=50),void 0===u&&(u=.001),void 0===l&&(l=.5),void 0===c&&(c=!0);for(var f=e.length,m=o.makeHeap(e.length,i),g=0;gr&&(r=t[e]);return r};var s=function(t){if(!i()(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");for(var r=t[0],e=1;e1&&void 0!==arguments[1]?arguments[1]:{};if(!i()(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");if(void 0!==e.output){if(!i()(e.output))throw new TypeError("output option must be an array if specified");r=e.output}else r=new Array(t.length);var n=s(t),a=o(t);if(n===a)throw new RangeError("minimum and maximum input values are equal. Cannot rescale a constant array");var h=e.min,u=void 0===h?e.autoMinMax?n:0:h,l=e.max,c=void 0===l?e.autoMinMax?a:1:l;if(u>=c)throw new RangeError("min option must be smaller than max option");for(var f=(c-u)/(a-n),m=0;mMath.abs(h[i])&&(i=r);if(i!==e){for(n=0;n=0;i--){for(n=0;no?t[i][o]:i===o?1:0;return n}get upperTriangularMatrix(){for(var t=this.LU,r=t.rows,e=t.columns,n=new _(r,e),i=0;iMath.abs(r)?(e=r/t,Math.abs(t)*Math.sqrt(1+e*e)):0!==r?(e=t/r,Math.abs(r)*Math.sqrt(1+e*e)):0}function l(t,r,e){for(var n=new Array(t),i=0;i=0;t--)if(0!==v[t]){for(let r=t+1;r=0;t--){if(t0;){let t,r;for(t=R-2;t>=-1&&-1!==t;t--){const r=Number.MIN_VALUE+z*Math.abs(v[t]+Math.abs(v[t+1]));if(Math.abs(y[t])<=r||Number.isNaN(y[t])){y[t]=0;break}}if(t===R-2)r=4;else{let e;for(e=R-1;e>=t&&e!==t;e--){let r=(e!==R?Math.abs(y[e]):0)+(e!==t+1?Math.abs(y[e-1]):0);if(Math.abs(v[e])<=z*r){v[e]=0;break}}e===t?r=3:e===R-1?r=1:(r=2,t=e)}switch(t++,r){case 1:{let r=y[R-2];y[R-2]=0;for(let e=R-2;e>=t;e--){let i=u(v[e],r),o=v[e]/i,s=r/i;if(v[e]=i,e!==t&&(r=-s*y[e-1],y[e-1]=o*y[e-1]),c)for(let t=0;t=v[t+1]);){let r=v[t];if(v[t]=v[t+1],v[t+1]=r,c&&tr?i[o][e]=t[o][e]/this.s[e]:i[o][e]=0;var o=this.U,s=o.length,a=o[0].length,h=new _(e,s);for(let t=0;tt&&r++;return r}get diagonal(){return this.s}get threshold(){return Number.EPSILON/2*Math.max(this.m,this.n)*this.s[0]}get leftSingularVectors(){return _.isMatrix(this.U)||(this.U=new _(this.U)),this.U}get rightSingularVectors(){return _.isMatrix(this.V)||(this.V=new _(this.V)),this.V}get diagonalMatrix(){return _.diag(this.s)}}function f(t,r,e){var n=e?t.rows:t.rows-1;if(r<0||r>n)throw new RangeError("Row index out of range")}function m(t,r,e){var n=e?t.columns:t.columns-1;if(r<0||r>n)throw new RangeError("Column index out of range")}function g(t,r){if(r.to1DArray&&(r=r.to1DArray()),r.length!==t.columns)throw new RangeError("vector size must be the same as the number of columns");return r}function p(t,r){if(r.to1DArray&&(r=r.to1DArray()),r.length!==t.rows)throw new RangeError("vector size must be the same as the number of rows");return r}function v(t,r,e){return{row:d(t,r),column:w(t,e)}}function d(t,r){if("object"!=typeof r)throw new TypeError("unexpected type for row indices");if(r.some(r=>r<0||r>=t.rows))throw new RangeError("row indices are out of range");return Array.isArray(r)||(r=Array.from(r)),r}function w(t,r){if("object"!=typeof r)throw new TypeError("unexpected type for column indices");if(r.some(r=>r<0||r>=t.columns))throw new RangeError("column indices are out of range");return Array.isArray(r)||(r=Array.from(r)),r}function y(t,r,e,n,i){if(5!==arguments.length)throw new RangeError("expected 4 arguments");if(b("startRow",r),b("endRow",e),b("startColumn",n),b("endColumn",i),r>e||n>i||r<0||r>=t.rows||e<0||e>=t.rows||n<0||n>=t.columns||i<0||i>=t.columns)throw new RangeError("Submatrix indices are out of range")}function b(t,r){if("number"!=typeof r)throw new TypeError(`${t} must be a number`)}class M extends(C()){constructor(t,r,e){super(),this.matrix=t,this.rows=r,this.columns=e}static get[Symbol.species](){return _}}class x extends M{constructor(t){super(t,t.columns,t.rows)}set(t,r,e){return this.matrix.set(r,t,e),this}get(t,r){return this.matrix.get(r,t)}}class S extends M{constructor(t,r){super(t,1,t.columns),this.row=r}set(t,r,e){return this.matrix.set(this.row,r,e),this}get(t,r){return this.matrix.get(this.row,r)}}class E extends M{constructor(t,r,e,n,i){y(t,r,e,n,i),super(t,e-r+1,i-n+1),this.startRow=r,this.startColumn=n}set(t,r,e){return this.matrix.set(this.startRow+t,this.startColumn+r,e),this}get(t,r){return this.matrix.get(this.startRow+t,this.startColumn+r)}}class R extends M{constructor(t,r,e){var n=v(t,r,e);super(t,n.row.length,n.column.length),this.rowIndices=n.row,this.columnIndices=n.column}set(t,r,e){return this.matrix.set(this.rowIndices[t],this.columnIndices[r],e),this}get(t,r){return this.matrix.get(this.rowIndices[t],this.columnIndices[r])}}class k extends M{constructor(t,r){super(t,(r=d(t,r)).length,t.columns),this.rowIndices=r}set(t,r,e){return this.matrix.set(this.rowIndices[t],r,e),this}get(t,r){return this.matrix.get(this.rowIndices[t],r)}}class z extends M{constructor(t,r){r=w(t,r),super(t,t.rows,r.length),this.columnIndices=r}set(t,r,e){return this.matrix.set(t,this.columnIndices[r],e),this}get(t,r){return this.matrix.get(t,this.columnIndices[r])}}class N extends M{constructor(t,r){super(t,t.rows,1),this.column=r}set(t,r,e){return this.matrix.set(t,this.column,e),this}get(t){return this.matrix.get(t,this.column)}}class A extends M{constructor(t){super(t,t.rows,t.columns)}set(t,r,e){return this.matrix.set(this.rows-t-1,r,e),this}get(t,r){return this.matrix.get(this.rows-t-1,r)}}class V extends M{constructor(t){super(t,t.rows,t.columns)}set(t,r,e){return this.matrix.set(t,this.columns-r-1,e),this}get(t,r){return this.matrix.get(t,this.columns-r-1)}}function C(t){void 0===t&&(t=Object);class r extends t{static get[Symbol.species](){return this}static from1DArray(t,r,e){if(t*r!==e.length)throw new RangeError("Data length does not match given dimensions");for(var n=new this(t,r),i=0;it&&(t=this.get(r,e));return t}maxIndex(){for(var t=this.get(0,0),r=[0,0],e=0;et&&(t=this.get(e,n),r[0]=e,r[1]=n);return r}min(){for(var t=this.get(0,0),r=0;rr&&(r=this.get(t,e));return r}maxRowIndex(t){f(this,t);for(var r=this.get(t,0),e=[t,0],n=1;nr&&(r=this.get(t,n),e[1]=n);return e}minRow(t){f(this,t);for(var r=this.get(t,0),e=1;er&&(r=this.get(e,t));return r}maxColumnIndex(t){m(this,t);for(var r=this.get(0,t),e=[0,t],n=1;nr&&(r=this.get(n,t),e[0]=n);return e}minColumn(t){m(this,t);for(var r=this.get(0,t),e=1;e=(r=void 0===r?1:r))throw new RangeError("min should be strictly smaller than max");for(var e=this.constructor.empty(this.rows,this.columns),n=0;n=(r=void 0===r?1:r))throw new RangeError("min should be strictly smaller than max");for(var e=this.constructor.empty(this.rows,this.columns),n=0;ne||r<0||r>=this.columns||e<0||e>=this.columns)throw new RangeError("Argument out of range");for(var n=new this.constructor[Symbol.species](t.length,e-r+1),i=0;i=this.rows)throw new RangeError(`Row index out of range: ${t[i]}`);n.set(i,o-r,this.get(t[i],o))}return n}subMatrixColumn(t,r,e){if(void 0===r&&(r=0),void 0===e&&(e=this.rows-1),r>e||r<0||r>=this.rows||e<0||e>=this.rows)throw new RangeError("Argument out of range");for(var n=new this.constructor[Symbol.species](e-r+1,t.length),i=0;i=this.columns)throw new RangeError(`Column index out of range: ${t[i]}`);n.set(o-r,i,this.get(o,t[i]))}return n}setSubMatrix(t,r,e){y(this,r,r+(t=this.constructor.checkMatrix(t)).rows-1,e,e+t.columns-1);for(var n=0;nt?i[o]=1/i[o]:i[o]=0;return i=this.constructor[Symbol.species].diag(i),n.mmul(i.mmul(e.transposeView()))}clone(){for(var t=new this.constructor[Symbol.species](this.rows,this.columns),r=0;r>","signPropagatingRightShift"],[">>>","rightShift","zeroFillRightShift"]]){var u=o($("\n(function %name%(value) {\n if (typeof value === 'number') return this.%name%S(value);\n return this.%name%M(value);\n})\n",{name:s[1],op:s[0]})),l=o($("\n(function %name%S(value) {\n for (var i = 0; i < this.rows; i++) {\n for (var j = 0; j < this.columns; j++) {\n this.set(i, j, this.get(i, j) %op% value);\n }\n }\n return this;\n})\n",{name:`${s[1]}S`,op:s[0]})),d=o($("\n(function %name%M(matrix) {\n matrix = this.constructor.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 (var i = 0; i < this.rows; i++) {\n for (var j = 0; j < this.columns; j++) {\n this.set(i, j, this.get(i, j) %op% matrix.get(i, j));\n }\n }\n return this;\n})\n",{name:`${s[1]}M`,op:s[0]})),w=o($("\n(function %name%(matrix, value) {\n var newMatrix = new this[Symbol.species](matrix);\n return newMatrix.%name%(value);\n})\n",{name:s[1]}));for(n=1;n0){if(super(t),!(Number.isInteger(r)&&r>0))throw new TypeError("nColumns must be a positive integer");for(e=0;e=0;o--){for(i=0;i=0;e--){for(t=0;ti)return new Array(r.rows+1).fill(0);for(var o=r.addRow(e,[0]),s=0;s0;a--){for(c=0,s=0,u=0;u0&&(o=-o),r[a]=c*o,s-=i*o,e[a-1]=i-o,h=0;hl){0;do{for(1,i=e[l],f=(e[l+1]-i)/(2*r[l]),m=u(f,1),f<0&&(m=-m),e[l]=r[l]/(f+m),e[l+1]=r[l]*(f+m),g=e[l+1],o=i-e[l],s=l+2;s=l;s--)for(d=v,v=p,b=y,i=p*r[s],o=p*f,m=u(f,r[s]),r[s+1]=y*m,y=r[s]/m,f=(p=f/m)*e[s]-y*i,e[s+1]=o+y*(p*i+y*e[s]),h=0;hS*x)}e[l]=e[l]+M,r[l]=0}for(s=0;s=u;a--)e[a]=r[a][u-1]/l,s+=e[a]*e[a];for(o=Math.sqrt(s),e[u]>0&&(o=-o),s-=e[u]*o,e[u]=e[u]-o,h=u;h=u;a--)i+=e[a]*r[a][h];for(i/=s,a=u;a<=c;a++)r[a][h]-=i*e[a]}for(a=0;a<=c;a++){for(i=0,h=c;h>=u;h--)i+=e[h]*r[a][h];for(i/=s,h=u;h<=c;h++)r[a][h]-=i*e[h]}e[u]=l*e[u],r[u][u-1]=l*o}}for(a=0;a=1;u--)if(0!==r[u][u-1]){for(a=u+1;a<=c;a++)e[a]=r[a][u-1];for(h=u;h<=c;h++){for(o=0,a=u;a<=c;a++)o+=e[a]*n[a][h];for(o=o/e[u]/r[u][u-1],a=u;a<=c;a++)n[a][h]+=o*e[a]}}}(o,f,m,s),function(t,r,e,n,i){var o,s,a,h,u,l,c,f,m,g,p,v,d,w,y,b=t-1,M=t-1,x=Number.EPSILON,S=0,E=0,R=0,k=0,z=0,N=0,A=0,V=0;for(o=0;oM)&&(e[o]=i[o][o],r[o]=0),s=Math.max(o-1,0);s=0;){for(h=b;h>0&&(0===(N=Math.abs(i[h-1][h-1])+Math.abs(i[h][h]))&&(N=E),!(Math.abs(i[h][h-1])=0){for(A=R>=0?R+A:R-A,e[b-1]=f+A,e[b]=e[b-1],0!==A&&(e[b]=f-c/A),r[b-1]=0,r[b]=0,f=i[b][b-1],N=Math.abs(f)+Math.abs(A),R=f/N,k=A/N,z=Math.sqrt(R*R+k*k),R/=z,k/=z,s=b-1;s0){for(N=Math.sqrt(N),m=h&&(A=i[u][u],R=((z=f-A)*(N=m-A)-c)/i[u+1][u]+i[u][u+1],k=i[u+1][u+1]-A-z-N,z=i[u+2][u+1],N=Math.abs(R)+Math.abs(k)+Math.abs(z),R/=N,k/=N,z/=N,u!==h)&&!(Math.abs(i[u][u-1])*(Math.abs(k)+Math.abs(z))u+2&&(i[o][o-3]=0);for(a=u;a<=b-1&&(w=a!==b-1,a!==u&&(R=i[a][a-1],k=i[a+1][a-1],z=w?i[a+2][a-1]:0,0!==(f=Math.abs(R)+Math.abs(k)+Math.abs(z))&&(R/=f,k/=f,z/=f)),0!==f);a++)if(N=Math.sqrt(R*R+k*k+z*z),R<0&&(N=-N),0!==N){for(a!==u?i[a][a-1]=-N*f:h!==u&&(i[a][a-1]=-i[a][a-1]),f=(R+=N)/N,m=k/N,A=z/N,k/=R,z/=R,s=a;s=0;b--)if(R=e[b],0===(k=r[b]))for(h=b,i[b][b]=1,o=b-1;o>=0;o--){for(c=i[o][o]-R,z=0,s=h;s<=b;s++)z+=i[o][s]*i[s][b];if(r[o]<0)A=c,N=z;else if(h=o,0===r[o]?i[o][b]=0!==c?-z/c:-z/(x*E):(f=i[o][o+1],m=i[o+1][o],k=(e[o]-R)*(e[o]-R)+r[o]*r[o],l=(f*N-A*z)/k,i[o][b]=l,i[o+1][b]=Math.abs(f)>Math.abs(A)?(-z-c*l)/f:(-N-m*l)/A),l=Math.abs(i[o][b]),x*l*l>1)for(s=o;s<=b;s++)i[s][b]=i[s][b]/l}else if(k<0)for(h=b-1,Math.abs(i[b][b-1])>Math.abs(i[b-1][b])?(i[b-1][b-1]=k/i[b][b-1],i[b-1][b]=-(i[b][b]-R)/i[b][b-1]):(y=$(0,-i[b-1][b],i[b-1][b-1]-R,k),i[b-1][b-1]=y[0],i[b-1][b]=y[1]),i[b][b-1]=0,i[b][b]=1,o=b-2;o>=0;o--){for(g=0,p=0,s=h;s<=b;s++)g+=i[o][s]*i[s][b-1],p+=i[o][s]*i[s][b];if(c=i[o][o]-R,r[o]<0)A=c,z=g,N=p;else if(h=o,0===r[o]?(y=$(-g,-p,c,k),i[o][b-1]=y[0],i[o][b]=y[1]):(f=i[o][o+1],m=i[o+1][o],v=(e[o]-R)*(e[o]-R)+r[o]*r[o]-k*k,d=2*(e[o]-R)*k,0===v&&0===d&&(v=x*E*(Math.abs(c)+Math.abs(k)+Math.abs(f)+Math.abs(m)+Math.abs(A))),y=$(f*z-A*g+k*p,f*N-A*p-k*g,v,d),i[o][b-1]=y[0],i[o][b]=y[1],Math.abs(f)>Math.abs(A)+Math.abs(k)?(i[o+1][b-1]=(-g-c*i[o][b-1]+k*i[o][b])/f,i[o+1][b]=(-p-c*i[o][b]-k*i[o][b-1])/f):(y=$(-z-m*i[o][b-1],-N-m*i[o][b],A,k),i[o+1][b-1]=y[0],i[o+1][b]=y[1])),l=Math.max(Math.abs(i[o][b-1]),Math.abs(i[o][b])),x*l*l>1)for(s=o;s<=b;s++)i[s][b-1]=i[s][b-1]/l,i[s][b]=i[s][b]/l}for(o=0;oM)for(s=o;s=0;s--)for(o=0;o<=M;o++){for(A=0,a=0;a<=Math.min(s,M);a++)A+=n[o][a]*i[a][s];n[o][s]=A}}(o,h,a,s,f)}this.n=o,this.e=h,this.d=a,this.V=s}get realEigenvalues(){return this.d}get imaginaryEigenvalues(){return this.e}get eigenvectorMatrix(){return _.isMatrix(this.V)||(this.V=new _(this.V)),this.V}get diagonalMatrix(){var t,r,e=this.n,n=this.e,i=this.d,o=new _(e,e);for(t=0;t0?o[t][t+1]=n[t]:n[t]<0&&(o[t][t-1]=n[t])}return o}}function $(t,r,e,n){var i,o;return Math.abs(e)>Math.abs(n)?[(t+(i=n/e)*r)/(o=e+i*n),(r-i*t)/o]:[((i=e/n)*t+r)/(o=n+i*e),(i*r-t)/o]}class Q{constructor(t){if(!(t=I.checkMatrix(t)).isSymmetric())throw new Error("Matrix is not symmetric");var r,e,n,i=t,o=i.rows,s=new _(o,o),a=!0;for(e=0;e0,s[e][e]=Math.sqrt(Math.max(u,0)),n=e+1;n=0;o--)for(i=0;i step(), 0); } else { return resolve(isFinished); } @@ -1031,7 +1031,7 @@ export class UMAP { reject(err); } }; - step(); + setTimeout(() => step(), 0); }); } From 60daf2602f8d055db81f9beaf63a01d0b63337c0 Mon Sep 17 00:00:00 2001 From: Tom MacWright Date: Thu, 6 Jun 2019 12:11:07 -0700 Subject: [PATCH 24/46] Create UMD build and point unpkg and jsdelivr to it --- lib/umap-js.js | 17 ++++++++++++++--- lib/umap-js.min.js | 2 +- package.json | 2 ++ src/lib.ts | 3 +-- webpack/lib.config.ts | 1 + 5 files changed, 19 insertions(+), 6 deletions(-) diff --git a/lib/umap-js.js b/lib/umap-js.js index 78206c0..5e315fd 100644 --- a/lib/umap-js.js +++ b/lib/umap-js.js @@ -1,4 +1,14 @@ -/******/ (function(modules) { // webpackBootstrap +(function webpackUniversalModuleDefinition(root, factory) { + if(typeof exports === 'object' && typeof module === 'object') + module.exports = factory(); + else if(typeof define === 'function' && define.amd) + define([], factory); + else { + var a = factory(); + for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i]; + } +})(window, function() { +return /******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; /******/ @@ -1033,7 +1043,7 @@ exports.searchFlatTree = searchFlatTree; Object.defineProperty(exports, "__esModule", { value: true }); var umap_1 = __webpack_require__(6); -window.UMAP = umap_1.UMAP; +exports.UMAP = umap_1.UMAP; /***/ }), @@ -6849,4 +6859,5 @@ class cholesky_CholeskyDecomposition { /***/ }) -/******/ ]); \ No newline at end of file +/******/ ]); +}); \ No newline at end of file diff --git a/lib/umap-js.min.js b/lib/umap-js.min.js index 0770fbb..252b704 100644 --- a/lib/umap-js.min.js +++ b/lib/umap-js.min.js @@ -1 +1 @@ -!function(t){var r={};function e(n){if(r[n])return r[n].exports;var i=r[n]={i:n,l:!1,exports:{}};return t[n].call(i.exports,i,i.exports,e),i.l=!0,i.exports}e.m=t,e.c=r,e.d=function(t,r,n){e.o(t,r)||Object.defineProperty(t,r,{enumerable:!0,get:n})},e.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},e.t=function(t,r){if(1&r&&(t=e(t)),8&r)return t;if(4&r&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(e.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&r&&"string"!=typeof t)for(var i in t)e.d(n,i,function(r){return t[r]}.bind(null,i));return n},e.n=function(t){var r=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(r,"a",r),r},e.o=function(t,r){return Object.prototype.hasOwnProperty.call(t,r)},e.p="",e(e.s=5)}([function(t,r,e){"use strict";const n=Object.prototype.toString;t.exports=function(t){return n.call(t).endsWith("Array]")}},function(t,r,e){"use strict";var n=this&&this.__values||function(t){var r="function"==typeof Symbol&&t[Symbol.iterator],e=0;return r?r.call(t):{next:function(){return t&&e>=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}};function i(t,r){return Math.floor(r()*t)}function o(t){for(var r=[],e=0;er?t[e]:r;return r},r.max2d=function(t){for(var r=0,e=0;er?t[e][n]:r;return r},r.rejectionSample=function(t,r,e){for(var n=a(t),o=0;o=s[0])return 0;for(var h=0;h=s[0])return 0;s[0]=e,o[0]=n,a[0]=i;for(var h=0,u=0;;){var l=2*h+1,c=l+1,f=t[0][0].length;if(l>=f)break;if(c>=f){if(!(s[l]>e))break;u=l}else if(s[l]>=s[c]){if(!(ee.length;a++)1===i[a]&&n[a]=0?(i[s]=0,Math.floor(e[s])):-1}},function(t,r,e){"use strict";var n,i=this&&this.__read||function(t,r){var e="function"==typeof Symbol&&t[Symbol.iterator];if(!e)return t;var n,i,o=e.call(t),s=[];try{for(;(void 0===r||r-- >0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},o=this&&this.__spread||function(){for(var t=[],r=0;r=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}},a=this&&this.__importStar||function(t){if(t&&t.__esModule)return t;var r={};if(null!=t)for(var e in t)Object.hasOwnProperty.call(t,e)&&(r[e]=t[e]);return r.default=t,r};Object.defineProperty(r,"__esModule",{value:!0});var h=a(e(1)),u=function(){function t(t,r,e,n){this.entries=new Map,this.nRows=0,this.nCols=0,this.rows=o(t),this.cols=o(r),this.values=o(e);for(var i=0;ir?t:r})},r.multiplyScalar=function(t,r){return t.map(function(t){return t*r})},r.eliminateZeros=function(t){for(var r=new Set,e=t.getValues(),n=t.getRows(),i=t.getCols(),o=0;or?t[e]:r;return t.map(function(t){return t/r})},n.l1=function(t){for(var r=0,e=0;e0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},i=this&&this.__spread||function(){for(var t=[],r=0;r=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}},s=this&&this.__importStar||function(t){if(t&&t.__esModule)return t;var r={};if(null!=t)for(var e in t)Object.hasOwnProperty.call(t,e)&&(r[e]=t[e]);return r.default=t,r};Object.defineProperty(r,"__esModule",{value:!0});var a=s(e(1)),h=function(){return function(t,r,e,n){this.hyperplanes=t,this.offsets=r,this.children=e,this.indices=n}}();function u(t,r,e,n){for(var i=r,o=0;o0?0:1}r.FlatTree=h,r.makeForest=function(t,r,e,n){var o=Math.max(10,r);return a.range(e).map(function(r,e){return function(t,r,e,n){void 0===r&&(r=30);var i=a.range(t.length);return function t(r,e,n,i,o){if(void 0===n&&(n=30),e.length>n){var s=function(t,r,e){var n=t[0].length,i=a.tauRandInt(r.length,e),o=a.tauRandInt(r.length,e);o=(o+=i===o?1:0)%r.length;for(var s=r[i],h=r[o],u=0,l=a.zeros(n),c=0;c0?(g[c]=0,f+=1):(g[c]=1,m+=1)}var d=a.zeros(f),w=a.zeros(m);for(var c in f=0,m=0,a.range(g.length))0===g[c]?(d[f]=r[c],f+=1):(w[m]=r[c],m+=1);return{indicesLeft:d,indicesRight:w,hyperplane:l,offset:u}}(r,e,o),h=s.indicesLeft,u=s.indicesRight,l=s.hyperplane,c=s.offset,f=t(r,h,n,i+1,o),m=t(r,u,n,i+1,o),g={leftChild:f,rightChild:m,isLeaf:!1,hyperplane:l,offset:c};return g}var g={indices:e,isLeaf:!0};return g}(t,i,r,e,n)}(t,o,e,n)}).map(function(t){return function(t,r){var e=function t(r){return r.isLeaf?1:1+t(r.leftChild)+t(r.rightChild)}(t),n=function t(r){return r.isLeaf?1:t(r.leftChild)+t(r.rightChild)}(t),o=a.range(e).map(function(){return a.zeros(t.hyperplane.length)}),s=a.zeros(e),u=a.range(e).map(function(){return[-1,-1]}),l=a.range(n).map(function(){return a.range(r).map(function(){return-1})});return function t(r,e,n,o,s,a,h){var u;if(r.isLeaf)return o[a][0]=-h,(u=s[h]).splice.apply(u,i([0,r.indices.length],r.indices)),{nodeNum:a,leafNum:h+=1};e[a]=r.hyperplane,n[a]=r.offset,o[a][0]=a+1;var l=a,c=t(r.leftChild,e,n,o,s,a+1,h);return a=c.nodeNum,h=c.leafNum,o[l][1]=a+1,{nodeNum:(c=t(r.rightChild,e,n,o,s,a+1,h)).nodeNum,leafNum:c.leafNum}}(t,o,s,u,l,0,0),new h(o,s,u,l)}(t,o)})},r.makeLeafArray=function(t){var r,e;if(t.length>0){var n=[];try{for(var s=o(t),a=s.next();!a.done;a=s.next()){var h=a.value;n.push.apply(n,i(h.indices))}}catch(t){r={error:t}}finally{try{a&&!a.done&&(e=s.return)&&e.call(s)}finally{if(r)throw r.error}}return n}return[[-1]]},r.searchFlatTree=function(t,r,e){for(var n=0;r.children[n][0]>0;)n=0===u(r.hyperplanes[n],r.offsets[n],t,e)?r.children[n][0]:r.children[n][1];var i=-1*r.children[n][0];return r.indices[i]}},function(t,r,e){"use strict";Object.defineProperty(r,"__esModule",{value:!0});var n=e(6);window.UMAP=n.UMAP},function(t,r,e){"use strict";var n=this&&this.__awaiter||function(t,r,e,n){return new(e||(e=Promise))(function(i,o){function s(t){try{h(n.next(t))}catch(t){o(t)}}function a(t){try{h(n.throw(t))}catch(t){o(t)}}function h(t){t.done?i(t.value):new e(function(r){r(t.value)}).then(s,a)}h((n=n.apply(t,r||[])).next())})},i=this&&this.__generator||function(t,r){var e,n,i,o,s={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function a(o){return function(a){return function(o){if(e)throw new TypeError("Generator is already executing.");for(;s;)try{if(e=1,n&&(i=2&o[0]?n.return:o[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,o[1])).done)return i;switch(n=0,i&&(o=[2&o[0],i.value]),o[0]){case 0:case 1:i=o;break;case 4:return s.label++,{value:o[1],done:!1};case 5:s.label++,n=o[1],o=[0];continue;case 7:o=s.ops.pop(),s.trys.pop();continue;default:if(!(i=(i=s.trys).length>0&&i[i.length-1])&&(6===o[0]||2===o[0])){s=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},s=this&&this.__spread||function(){for(var t=[],r=0;r0&&i.set(o,u,c)}var f=l.transpose(i);return l.maximum(i,f)},t.prototype.transform=function(t){var r=this,e=this.X;if(void 0===e||0===e.length)throw new Error("No data has been fit.");var n=Math.floor(this.nNeighbors*this.transformQueueSize),i=c.initializeSearch(this.rpForest,e,t,n,this.initFromRandom,this.initFromTree,this.random),o=this.search(e,this.searchGraph,i,t),s=u.deheapSort(o),a=s.indices,h=s.weights;a=a.map(function(t){return t.slice(0,r.nNeighbors)}),h=h.map(function(t){return t.slice(0,r.nNeighbors)});var f=Math.max(0,this.localConnectivity-1),g=this.smoothKNNDistance(h,this.nNeighbors,f),p=g.sigmas,v=g.rhos,d=this.computeMembershipStrengths(a,h,p,v),w=d.rows,y=d.cols,b=d.vals,M=[t.length,e.length],x=new l.SparseMatrix(w,y,b,M),E=l.normalize(x,"l1"),R=l.getCSR(E),k=t.length,z=S(m.reshape2d(R.indices,k,this.nNeighbors),m.reshape2d(R.values,k,this.nNeighbors),this.embedding),N=this.nEpochs?this.nEpochs/3:x.nRows<=1e4?100:30,A=x.getValues().reduce(function(t,r){return r>t?r:t},0);x=x.map(function(t){return t0});if(g.length>=e){var p=Math.floor(e),v=e-p;p>0?(s[h]=g[p-1],v>1e-5&&(s[h]+=v*(g[p]-g[p-1]))):s[h]=v*g[0]}else g.length>0&&(s[h]=m.max(g));for(var d=0;d0?Math.exp(-b/c):1}if(Math.abs(w-o)<1e-5)break;w>o?c=(u+(l=c))/2:(u=c,l===1/0?c*=2:c=(u+l)/2)}if(a[h]=c,s[h]>0){var M=m.mean(f);a[h]<.001*M&&(a[h]=.001*M)}else{var x=m.mean(t.map(m.mean));a[h]<.001*x&&(a[h]=.001*x)}}return{sigmas:a,rhos:s}},t.prototype.computeMembershipStrengths=function(t,r,e,n){for(var i=t.length,o=t[0].length,s=m.zeros(i*o),a=m.zeros(i*o),h=m.zeros(i*o),u=0;u0&&(e[n]=r/i[n])}),e},t.prototype.assignOptimizationStateParameters=function(t){Object.assign(this.optimizationState,t)},t.prototype.prepareForOptimizationLoop=function(){var t=this.repulsionStrength,r=this.learningRate,e=this.negativeSampleRate,n=this.optimizationState,i=n.epochsPerSample,o=n.headEmbedding,a=n.tailEmbedding,h=o[0].length,u=o.length===a.length,l=i.map(function(t){return t/e}),c=s(l),f=s(i);this.assignOptimizationStateParameters({epochOfNextSample:f,epochOfNextNegativeSample:c,epochsPerNegativeSample:l,moveOther:u,initialAlpha:r,alpha:r,gamma:t,dim:h})},t.prototype.initializeOptimization=function(){var t=this.embedding,r=this.embedding,e=this.optimizationState,n=e.head,i=e.tail,o=e.epochsPerSample,s=this.getNEpochs(),a=this.graph.nCols,h=b(this.spread,this.minDist),u=h.a,l=h.b;this.assignOptimizationStateParameters({headEmbedding:t,tailEmbedding:r,head:n,tail:i,epochsPerSample:o,a:u,b:l,nEpochs:s,nVertices:a})},t.prototype.optimizeLayoutStep=function(t){for(var r=this.optimizationState,e=r.head,n=r.tail,i=r.headEmbedding,o=r.tailEmbedding,s=r.epochsPerSample,a=r.epochOfNextSample,h=r.epochOfNextNegativeSample,u=r.epochsPerNegativeSample,l=r.moveOther,c=r.initialAlpha,f=r.alpha,g=r.gamma,p=r.a,v=r.b,d=r.dim,b=r.nEpochs,M=r.nVertices,x=0;xt)){var S=e[x],E=n[x],R=i[S],k=o[E],z=y(R,k),N=0;z>0&&(N=-2*p*v*Math.pow(z,v-1),N/=p*Math.pow(z,v)+1);for(var A=0;A0)O=2*g*v,O/=(.001+j)*(p*Math.pow(j,v)+1);else if(S===P)continue;for(A=0;A0&&(V=w(O*(R[A]-I[A]),4)),R[A]+=V*f}}h[x]+=C*u[x]}return r.alpha=c*(1-t/b),r.currentEpoch+=1,i},t.prototype.optimizeLayoutAsync=function(t){var r=this;return void 0===t&&(t=function(){return!0}),new Promise(function(e,o){var s=function(){return n(r,void 0,void 0,function(){var r,n,a,h,u,l;return i(this,function(i){try{if(r=this.optimizationState,n=r.nEpochs,a=r.currentEpoch,this.embedding=this.optimizeLayoutStep(a),h=this.optimizationState.currentEpoch,u=!1===t(h),l=h===n,u||l)return[2,e(l)];s()}catch(t){o(t)}return[2]})})};s()})},t.prototype.optimizeLayout=function(t){void 0===t&&(t=function(){return!0});for(var r=!1,e=[];!r;){var n=this.optimizationState,i=n.nEpochs,o=n.currentEpoch;e=this.optimizeLayoutStep(o);var s=this.optimizationState.currentEpoch,a=!1===t(s);r=s===i||a}return e},t.prototype.getNEpochs=function(){var t=this.graph;if(this.nEpochs>0)return this.nEpochs;var r=t.nRows;return r<=2500?500:r<=5e3?400:r<=7500?300:200},t}();function v(t,r){for(var e=0,n=0;nr?r:t<-r?-r:t}function y(t,r){for(var e=0,n=0;n=r?Math.exp(-(e[i]-r)/t):n}),i={x:e,y:n},s={damping:1.5,initialValues:[.5,.5],gradientDifference:.1,maxIterations:100,errorTolerance:.01},a=g.default(i,function(t){var r=o(t,2),e=r[0],n=r[1];return function(t){return 1/(1+e*Math.pow(t,2*n))}},s).parameterValues,h=o(a,2);return{a:h[0],b:h[1]}}function M(t,r,e,n){return void 0===e&&(e=1),void 0===n&&(n=5),t.map(function(t,i,o){return-1===r[i]||-1===r[o]?t*Math.exp(-e):r[i]!==r[o]?t*Math.exp(-n):t})}function x(t){t=l.normalize(t,"max");var r=l.transpose(t),e=l.pairwiseMultiply(r,t);return t=l.add(t,l.subtract(r,e)),l.eliminateZeros(t)}function S(t,r,e){for(var n=m.zeros(t.length).map(function(t){return m.zeros(e[0].length)}),i=0;i=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}},i=this&&this.__importStar||function(t){if(t&&t.__esModule)return t;var r={};if(null!=t)for(var e in t)Object.hasOwnProperty.call(t,e)&&(r[e]=t[e]);return r.default=t,r};Object.defineProperty(r,"__esModule",{value:!0});var o=i(e(2)),s=i(e(3)),a=i(e(4)),h=i(e(1));r.makeNNDescent=function(t,r){return function(e,n,i,s,a,u,l,c){void 0===s&&(s=10),void 0===a&&(a=50),void 0===u&&(u=.001),void 0===l&&(l=.5),void 0===c&&(c=!0);for(var f=e.length,m=o.makeHeap(e.length,i),g=0;gr&&(r=t[e]);return r};var s=function(t){if(!i()(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");for(var r=t[0],e=1;e1&&void 0!==arguments[1]?arguments[1]:{};if(!i()(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");if(void 0!==e.output){if(!i()(e.output))throw new TypeError("output option must be an array if specified");r=e.output}else r=new Array(t.length);var n=s(t),a=o(t);if(n===a)throw new RangeError("minimum and maximum input values are equal. Cannot rescale a constant array");var h=e.min,u=void 0===h?e.autoMinMax?n:0:h,l=e.max,c=void 0===l?e.autoMinMax?a:1:l;if(u>=c)throw new RangeError("min option must be smaller than max option");for(var f=(c-u)/(a-n),m=0;mMath.abs(h[i])&&(i=r);if(i!==e){for(n=0;n=0;i--){for(n=0;no?t[i][o]:i===o?1:0;return n}get upperTriangularMatrix(){for(var t=this.LU,r=t.rows,e=t.columns,n=new _(r,e),i=0;iMath.abs(r)?(e=r/t,Math.abs(t)*Math.sqrt(1+e*e)):0!==r?(e=t/r,Math.abs(r)*Math.sqrt(1+e*e)):0}function l(t,r,e){for(var n=new Array(t),i=0;i=0;t--)if(0!==v[t]){for(let r=t+1;r=0;t--){if(t0;){let t,r;for(t=R-2;t>=-1&&-1!==t;t--){const r=Number.MIN_VALUE+z*Math.abs(v[t]+Math.abs(v[t+1]));if(Math.abs(y[t])<=r||Number.isNaN(y[t])){y[t]=0;break}}if(t===R-2)r=4;else{let e;for(e=R-1;e>=t&&e!==t;e--){let r=(e!==R?Math.abs(y[e]):0)+(e!==t+1?Math.abs(y[e-1]):0);if(Math.abs(v[e])<=z*r){v[e]=0;break}}e===t?r=3:e===R-1?r=1:(r=2,t=e)}switch(t++,r){case 1:{let r=y[R-2];y[R-2]=0;for(let e=R-2;e>=t;e--){let i=u(v[e],r),o=v[e]/i,s=r/i;if(v[e]=i,e!==t&&(r=-s*y[e-1],y[e-1]=o*y[e-1]),c)for(let t=0;t=v[t+1]);){let r=v[t];if(v[t]=v[t+1],v[t+1]=r,c&&tr?i[o][e]=t[o][e]/this.s[e]:i[o][e]=0;var o=this.U,s=o.length,a=o[0].length,h=new _(e,s);for(let t=0;tt&&r++;return r}get diagonal(){return this.s}get threshold(){return Number.EPSILON/2*Math.max(this.m,this.n)*this.s[0]}get leftSingularVectors(){return _.isMatrix(this.U)||(this.U=new _(this.U)),this.U}get rightSingularVectors(){return _.isMatrix(this.V)||(this.V=new _(this.V)),this.V}get diagonalMatrix(){return _.diag(this.s)}}function f(t,r,e){var n=e?t.rows:t.rows-1;if(r<0||r>n)throw new RangeError("Row index out of range")}function m(t,r,e){var n=e?t.columns:t.columns-1;if(r<0||r>n)throw new RangeError("Column index out of range")}function g(t,r){if(r.to1DArray&&(r=r.to1DArray()),r.length!==t.columns)throw new RangeError("vector size must be the same as the number of columns");return r}function p(t,r){if(r.to1DArray&&(r=r.to1DArray()),r.length!==t.rows)throw new RangeError("vector size must be the same as the number of rows");return r}function v(t,r,e){return{row:d(t,r),column:w(t,e)}}function d(t,r){if("object"!=typeof r)throw new TypeError("unexpected type for row indices");if(r.some(r=>r<0||r>=t.rows))throw new RangeError("row indices are out of range");return Array.isArray(r)||(r=Array.from(r)),r}function w(t,r){if("object"!=typeof r)throw new TypeError("unexpected type for column indices");if(r.some(r=>r<0||r>=t.columns))throw new RangeError("column indices are out of range");return Array.isArray(r)||(r=Array.from(r)),r}function y(t,r,e,n,i){if(5!==arguments.length)throw new RangeError("expected 4 arguments");if(b("startRow",r),b("endRow",e),b("startColumn",n),b("endColumn",i),r>e||n>i||r<0||r>=t.rows||e<0||e>=t.rows||n<0||n>=t.columns||i<0||i>=t.columns)throw new RangeError("Submatrix indices are out of range")}function b(t,r){if("number"!=typeof r)throw new TypeError(`${t} must be a number`)}class M extends(C()){constructor(t,r,e){super(),this.matrix=t,this.rows=r,this.columns=e}static get[Symbol.species](){return _}}class x extends M{constructor(t){super(t,t.columns,t.rows)}set(t,r,e){return this.matrix.set(r,t,e),this}get(t,r){return this.matrix.get(r,t)}}class S extends M{constructor(t,r){super(t,1,t.columns),this.row=r}set(t,r,e){return this.matrix.set(this.row,r,e),this}get(t,r){return this.matrix.get(this.row,r)}}class E extends M{constructor(t,r,e,n,i){y(t,r,e,n,i),super(t,e-r+1,i-n+1),this.startRow=r,this.startColumn=n}set(t,r,e){return this.matrix.set(this.startRow+t,this.startColumn+r,e),this}get(t,r){return this.matrix.get(this.startRow+t,this.startColumn+r)}}class R extends M{constructor(t,r,e){var n=v(t,r,e);super(t,n.row.length,n.column.length),this.rowIndices=n.row,this.columnIndices=n.column}set(t,r,e){return this.matrix.set(this.rowIndices[t],this.columnIndices[r],e),this}get(t,r){return this.matrix.get(this.rowIndices[t],this.columnIndices[r])}}class k extends M{constructor(t,r){super(t,(r=d(t,r)).length,t.columns),this.rowIndices=r}set(t,r,e){return this.matrix.set(this.rowIndices[t],r,e),this}get(t,r){return this.matrix.get(this.rowIndices[t],r)}}class z extends M{constructor(t,r){r=w(t,r),super(t,t.rows,r.length),this.columnIndices=r}set(t,r,e){return this.matrix.set(t,this.columnIndices[r],e),this}get(t,r){return this.matrix.get(t,this.columnIndices[r])}}class N extends M{constructor(t,r){super(t,t.rows,1),this.column=r}set(t,r,e){return this.matrix.set(t,this.column,e),this}get(t){return this.matrix.get(t,this.column)}}class A extends M{constructor(t){super(t,t.rows,t.columns)}set(t,r,e){return this.matrix.set(this.rows-t-1,r,e),this}get(t,r){return this.matrix.get(this.rows-t-1,r)}}class V extends M{constructor(t){super(t,t.rows,t.columns)}set(t,r,e){return this.matrix.set(t,this.columns-r-1,e),this}get(t,r){return this.matrix.get(t,this.columns-r-1)}}function C(t){void 0===t&&(t=Object);class r extends t{static get[Symbol.species](){return this}static from1DArray(t,r,e){if(t*r!==e.length)throw new RangeError("Data length does not match given dimensions");for(var n=new this(t,r),i=0;it&&(t=this.get(r,e));return t}maxIndex(){for(var t=this.get(0,0),r=[0,0],e=0;et&&(t=this.get(e,n),r[0]=e,r[1]=n);return r}min(){for(var t=this.get(0,0),r=0;rr&&(r=this.get(t,e));return r}maxRowIndex(t){f(this,t);for(var r=this.get(t,0),e=[t,0],n=1;nr&&(r=this.get(t,n),e[1]=n);return e}minRow(t){f(this,t);for(var r=this.get(t,0),e=1;er&&(r=this.get(e,t));return r}maxColumnIndex(t){m(this,t);for(var r=this.get(0,t),e=[0,t],n=1;nr&&(r=this.get(n,t),e[0]=n);return e}minColumn(t){m(this,t);for(var r=this.get(0,t),e=1;e=(r=void 0===r?1:r))throw new RangeError("min should be strictly smaller than max");for(var e=this.constructor.empty(this.rows,this.columns),n=0;n=(r=void 0===r?1:r))throw new RangeError("min should be strictly smaller than max");for(var e=this.constructor.empty(this.rows,this.columns),n=0;ne||r<0||r>=this.columns||e<0||e>=this.columns)throw new RangeError("Argument out of range");for(var n=new this.constructor[Symbol.species](t.length,e-r+1),i=0;i=this.rows)throw new RangeError(`Row index out of range: ${t[i]}`);n.set(i,o-r,this.get(t[i],o))}return n}subMatrixColumn(t,r,e){if(void 0===r&&(r=0),void 0===e&&(e=this.rows-1),r>e||r<0||r>=this.rows||e<0||e>=this.rows)throw new RangeError("Argument out of range");for(var n=new this.constructor[Symbol.species](e-r+1,t.length),i=0;i=this.columns)throw new RangeError(`Column index out of range: ${t[i]}`);n.set(o-r,i,this.get(o,t[i]))}return n}setSubMatrix(t,r,e){y(this,r,r+(t=this.constructor.checkMatrix(t)).rows-1,e,e+t.columns-1);for(var n=0;nt?i[o]=1/i[o]:i[o]=0;return i=this.constructor[Symbol.species].diag(i),n.mmul(i.mmul(e.transposeView()))}clone(){for(var t=new this.constructor[Symbol.species](this.rows,this.columns),r=0;r>","signPropagatingRightShift"],[">>>","rightShift","zeroFillRightShift"]]){var u=o($("\n(function %name%(value) {\n if (typeof value === 'number') return this.%name%S(value);\n return this.%name%M(value);\n})\n",{name:s[1],op:s[0]})),l=o($("\n(function %name%S(value) {\n for (var i = 0; i < this.rows; i++) {\n for (var j = 0; j < this.columns; j++) {\n this.set(i, j, this.get(i, j) %op% value);\n }\n }\n return this;\n})\n",{name:`${s[1]}S`,op:s[0]})),d=o($("\n(function %name%M(matrix) {\n matrix = this.constructor.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 (var i = 0; i < this.rows; i++) {\n for (var j = 0; j < this.columns; j++) {\n this.set(i, j, this.get(i, j) %op% matrix.get(i, j));\n }\n }\n return this;\n})\n",{name:`${s[1]}M`,op:s[0]})),w=o($("\n(function %name%(matrix, value) {\n var newMatrix = new this[Symbol.species](matrix);\n return newMatrix.%name%(value);\n})\n",{name:s[1]}));for(n=1;n0){if(super(t),!(Number.isInteger(r)&&r>0))throw new TypeError("nColumns must be a positive integer");for(e=0;e=0;o--){for(i=0;i=0;e--){for(t=0;ti)return new Array(r.rows+1).fill(0);for(var o=r.addRow(e,[0]),s=0;s0;a--){for(c=0,s=0,u=0;u0&&(o=-o),r[a]=c*o,s-=i*o,e[a-1]=i-o,h=0;hl){0;do{for(1,i=e[l],f=(e[l+1]-i)/(2*r[l]),m=u(f,1),f<0&&(m=-m),e[l]=r[l]/(f+m),e[l+1]=r[l]*(f+m),g=e[l+1],o=i-e[l],s=l+2;s=l;s--)for(d=v,v=p,b=y,i=p*r[s],o=p*f,m=u(f,r[s]),r[s+1]=y*m,y=r[s]/m,f=(p=f/m)*e[s]-y*i,e[s+1]=o+y*(p*i+y*e[s]),h=0;hS*x)}e[l]=e[l]+M,r[l]=0}for(s=0;s=u;a--)e[a]=r[a][u-1]/l,s+=e[a]*e[a];for(o=Math.sqrt(s),e[u]>0&&(o=-o),s-=e[u]*o,e[u]=e[u]-o,h=u;h=u;a--)i+=e[a]*r[a][h];for(i/=s,a=u;a<=c;a++)r[a][h]-=i*e[a]}for(a=0;a<=c;a++){for(i=0,h=c;h>=u;h--)i+=e[h]*r[a][h];for(i/=s,h=u;h<=c;h++)r[a][h]-=i*e[h]}e[u]=l*e[u],r[u][u-1]=l*o}}for(a=0;a=1;u--)if(0!==r[u][u-1]){for(a=u+1;a<=c;a++)e[a]=r[a][u-1];for(h=u;h<=c;h++){for(o=0,a=u;a<=c;a++)o+=e[a]*n[a][h];for(o=o/e[u]/r[u][u-1],a=u;a<=c;a++)n[a][h]+=o*e[a]}}}(o,f,m,s),function(t,r,e,n,i){var o,s,a,h,u,l,c,f,m,g,p,v,d,w,y,b=t-1,M=t-1,x=Number.EPSILON,S=0,E=0,R=0,k=0,z=0,N=0,A=0,V=0;for(o=0;oM)&&(e[o]=i[o][o],r[o]=0),s=Math.max(o-1,0);s=0;){for(h=b;h>0&&(0===(N=Math.abs(i[h-1][h-1])+Math.abs(i[h][h]))&&(N=E),!(Math.abs(i[h][h-1])=0){for(A=R>=0?R+A:R-A,e[b-1]=f+A,e[b]=e[b-1],0!==A&&(e[b]=f-c/A),r[b-1]=0,r[b]=0,f=i[b][b-1],N=Math.abs(f)+Math.abs(A),R=f/N,k=A/N,z=Math.sqrt(R*R+k*k),R/=z,k/=z,s=b-1;s0){for(N=Math.sqrt(N),m=h&&(A=i[u][u],R=((z=f-A)*(N=m-A)-c)/i[u+1][u]+i[u][u+1],k=i[u+1][u+1]-A-z-N,z=i[u+2][u+1],N=Math.abs(R)+Math.abs(k)+Math.abs(z),R/=N,k/=N,z/=N,u!==h)&&!(Math.abs(i[u][u-1])*(Math.abs(k)+Math.abs(z))u+2&&(i[o][o-3]=0);for(a=u;a<=b-1&&(w=a!==b-1,a!==u&&(R=i[a][a-1],k=i[a+1][a-1],z=w?i[a+2][a-1]:0,0!==(f=Math.abs(R)+Math.abs(k)+Math.abs(z))&&(R/=f,k/=f,z/=f)),0!==f);a++)if(N=Math.sqrt(R*R+k*k+z*z),R<0&&(N=-N),0!==N){for(a!==u?i[a][a-1]=-N*f:h!==u&&(i[a][a-1]=-i[a][a-1]),f=(R+=N)/N,m=k/N,A=z/N,k/=R,z/=R,s=a;s=0;b--)if(R=e[b],0===(k=r[b]))for(h=b,i[b][b]=1,o=b-1;o>=0;o--){for(c=i[o][o]-R,z=0,s=h;s<=b;s++)z+=i[o][s]*i[s][b];if(r[o]<0)A=c,N=z;else if(h=o,0===r[o]?i[o][b]=0!==c?-z/c:-z/(x*E):(f=i[o][o+1],m=i[o+1][o],k=(e[o]-R)*(e[o]-R)+r[o]*r[o],l=(f*N-A*z)/k,i[o][b]=l,i[o+1][b]=Math.abs(f)>Math.abs(A)?(-z-c*l)/f:(-N-m*l)/A),l=Math.abs(i[o][b]),x*l*l>1)for(s=o;s<=b;s++)i[s][b]=i[s][b]/l}else if(k<0)for(h=b-1,Math.abs(i[b][b-1])>Math.abs(i[b-1][b])?(i[b-1][b-1]=k/i[b][b-1],i[b-1][b]=-(i[b][b]-R)/i[b][b-1]):(y=$(0,-i[b-1][b],i[b-1][b-1]-R,k),i[b-1][b-1]=y[0],i[b-1][b]=y[1]),i[b][b-1]=0,i[b][b]=1,o=b-2;o>=0;o--){for(g=0,p=0,s=h;s<=b;s++)g+=i[o][s]*i[s][b-1],p+=i[o][s]*i[s][b];if(c=i[o][o]-R,r[o]<0)A=c,z=g,N=p;else if(h=o,0===r[o]?(y=$(-g,-p,c,k),i[o][b-1]=y[0],i[o][b]=y[1]):(f=i[o][o+1],m=i[o+1][o],v=(e[o]-R)*(e[o]-R)+r[o]*r[o]-k*k,d=2*(e[o]-R)*k,0===v&&0===d&&(v=x*E*(Math.abs(c)+Math.abs(k)+Math.abs(f)+Math.abs(m)+Math.abs(A))),y=$(f*z-A*g+k*p,f*N-A*p-k*g,v,d),i[o][b-1]=y[0],i[o][b]=y[1],Math.abs(f)>Math.abs(A)+Math.abs(k)?(i[o+1][b-1]=(-g-c*i[o][b-1]+k*i[o][b])/f,i[o+1][b]=(-p-c*i[o][b]-k*i[o][b-1])/f):(y=$(-z-m*i[o][b-1],-N-m*i[o][b],A,k),i[o+1][b-1]=y[0],i[o+1][b]=y[1])),l=Math.max(Math.abs(i[o][b-1]),Math.abs(i[o][b])),x*l*l>1)for(s=o;s<=b;s++)i[s][b-1]=i[s][b-1]/l,i[s][b]=i[s][b]/l}for(o=0;oM)for(s=o;s=0;s--)for(o=0;o<=M;o++){for(A=0,a=0;a<=Math.min(s,M);a++)A+=n[o][a]*i[a][s];n[o][s]=A}}(o,h,a,s,f)}this.n=o,this.e=h,this.d=a,this.V=s}get realEigenvalues(){return this.d}get imaginaryEigenvalues(){return this.e}get eigenvectorMatrix(){return _.isMatrix(this.V)||(this.V=new _(this.V)),this.V}get diagonalMatrix(){var t,r,e=this.n,n=this.e,i=this.d,o=new _(e,e);for(t=0;t0?o[t][t+1]=n[t]:n[t]<0&&(o[t][t-1]=n[t])}return o}}function $(t,r,e,n){var i,o;return Math.abs(e)>Math.abs(n)?[(t+(i=n/e)*r)/(o=e+i*n),(r-i*t)/o]:[((i=e/n)*t+r)/(o=n+i*e),(i*r-t)/o]}class Q{constructor(t){if(!(t=I.checkMatrix(t)).isSymmetric())throw new Error("Matrix is not symmetric");var r,e,n,i=t,o=i.rows,s=new _(o,o),a=!0;for(e=0;e0,s[e][e]=Math.sqrt(Math.max(u,0)),n=e+1;n=0;o--)for(i=0;i=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}};function i(t,r){return Math.floor(r()*t)}function o(t){for(var r=[],e=0;er?t[e]:r;return r},r.max2d=function(t){for(var r=0,e=0;er?t[e][n]:r;return r},r.rejectionSample=function(t,r,e){for(var n=a(t),o=0;o=s[0])return 0;for(var h=0;h=s[0])return 0;s[0]=e,o[0]=n,a[0]=i;for(var h=0,u=0;;){var l=2*h+1,c=l+1,f=t[0][0].length;if(l>=f)break;if(c>=f){if(!(s[l]>e))break;u=l}else if(s[l]>=s[c]){if(!(ee.length;a++)1===i[a]&&n[a]=0?(i[s]=0,Math.floor(e[s])):-1}},function(t,r,e){"use strict";var n,i=this&&this.__read||function(t,r){var e="function"==typeof Symbol&&t[Symbol.iterator];if(!e)return t;var n,i,o=e.call(t),s=[];try{for(;(void 0===r||r-- >0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},o=this&&this.__spread||function(){for(var t=[],r=0;r=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}},a=this&&this.__importStar||function(t){if(t&&t.__esModule)return t;var r={};if(null!=t)for(var e in t)Object.hasOwnProperty.call(t,e)&&(r[e]=t[e]);return r.default=t,r};Object.defineProperty(r,"__esModule",{value:!0});var h=a(e(1)),u=function(){function t(t,r,e,n){this.entries=new Map,this.nRows=0,this.nCols=0,this.rows=o(t),this.cols=o(r),this.values=o(e);for(var i=0;ir?t:r})},r.multiplyScalar=function(t,r){return t.map(function(t){return t*r})},r.eliminateZeros=function(t){for(var r=new Set,e=t.getValues(),n=t.getRows(),i=t.getCols(),o=0;or?t[e]:r;return t.map(function(t){return t/r})},n.l1=function(t){for(var r=0,e=0;e0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},i=this&&this.__spread||function(){for(var t=[],r=0;r=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}},s=this&&this.__importStar||function(t){if(t&&t.__esModule)return t;var r={};if(null!=t)for(var e in t)Object.hasOwnProperty.call(t,e)&&(r[e]=t[e]);return r.default=t,r};Object.defineProperty(r,"__esModule",{value:!0});var a=s(e(1)),h=function(){return function(t,r,e,n){this.hyperplanes=t,this.offsets=r,this.children=e,this.indices=n}}();function u(t,r,e,n){for(var i=r,o=0;o0?0:1}r.FlatTree=h,r.makeForest=function(t,r,e,n){var o=Math.max(10,r);return a.range(e).map(function(r,e){return function(t,r,e,n){void 0===r&&(r=30);var i=a.range(t.length);return function t(r,e,n,i,o){if(void 0===n&&(n=30),e.length>n){var s=function(t,r,e){var n=t[0].length,i=a.tauRandInt(r.length,e),o=a.tauRandInt(r.length,e);o=(o+=i===o?1:0)%r.length;for(var s=r[i],h=r[o],u=0,l=a.zeros(n),c=0;c0?(g[c]=0,f+=1):(g[c]=1,m+=1)}var d=a.zeros(f),w=a.zeros(m);for(var c in f=0,m=0,a.range(g.length))0===g[c]?(d[f]=r[c],f+=1):(w[m]=r[c],m+=1);return{indicesLeft:d,indicesRight:w,hyperplane:l,offset:u}}(r,e,o),h=s.indicesLeft,u=s.indicesRight,l=s.hyperplane,c=s.offset,f=t(r,h,n,i+1,o),m=t(r,u,n,i+1,o),g={leftChild:f,rightChild:m,isLeaf:!1,hyperplane:l,offset:c};return g}var g={indices:e,isLeaf:!0};return g}(t,i,r,e,n)}(t,o,e,n)}).map(function(t){return function(t,r){var e=function t(r){return r.isLeaf?1:1+t(r.leftChild)+t(r.rightChild)}(t),n=function t(r){return r.isLeaf?1:t(r.leftChild)+t(r.rightChild)}(t),o=a.range(e).map(function(){return a.zeros(t.hyperplane.length)}),s=a.zeros(e),u=a.range(e).map(function(){return[-1,-1]}),l=a.range(n).map(function(){return a.range(r).map(function(){return-1})});return function t(r,e,n,o,s,a,h){var u;if(r.isLeaf)return o[a][0]=-h,(u=s[h]).splice.apply(u,i([0,r.indices.length],r.indices)),{nodeNum:a,leafNum:h+=1};e[a]=r.hyperplane,n[a]=r.offset,o[a][0]=a+1;var l=a,c=t(r.leftChild,e,n,o,s,a+1,h);return a=c.nodeNum,h=c.leafNum,o[l][1]=a+1,{nodeNum:(c=t(r.rightChild,e,n,o,s,a+1,h)).nodeNum,leafNum:c.leafNum}}(t,o,s,u,l,0,0),new h(o,s,u,l)}(t,o)})},r.makeLeafArray=function(t){var r,e;if(t.length>0){var n=[];try{for(var s=o(t),a=s.next();!a.done;a=s.next()){var h=a.value;n.push.apply(n,i(h.indices))}}catch(t){r={error:t}}finally{try{a&&!a.done&&(e=s.return)&&e.call(s)}finally{if(r)throw r.error}}return n}return[[-1]]},r.searchFlatTree=function(t,r,e){for(var n=0;r.children[n][0]>0;)n=0===u(r.hyperplanes[n],r.offsets[n],t,e)?r.children[n][0]:r.children[n][1];var i=-1*r.children[n][0];return r.indices[i]}},function(t,r,e){"use strict";Object.defineProperty(r,"__esModule",{value:!0});var n=e(6);r.UMAP=n.UMAP},function(t,r,e){"use strict";var n=this&&this.__awaiter||function(t,r,e,n){return new(e||(e=Promise))(function(i,o){function s(t){try{h(n.next(t))}catch(t){o(t)}}function a(t){try{h(n.throw(t))}catch(t){o(t)}}function h(t){t.done?i(t.value):new e(function(r){r(t.value)}).then(s,a)}h((n=n.apply(t,r||[])).next())})},i=this&&this.__generator||function(t,r){var e,n,i,o,s={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function a(o){return function(a){return function(o){if(e)throw new TypeError("Generator is already executing.");for(;s;)try{if(e=1,n&&(i=2&o[0]?n.return:o[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,o[1])).done)return i;switch(n=0,i&&(o=[2&o[0],i.value]),o[0]){case 0:case 1:i=o;break;case 4:return s.label++,{value:o[1],done:!1};case 5:s.label++,n=o[1],o=[0];continue;case 7:o=s.ops.pop(),s.trys.pop();continue;default:if(!(i=(i=s.trys).length>0&&i[i.length-1])&&(6===o[0]||2===o[0])){s=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},s=this&&this.__spread||function(){for(var t=[],r=0;r0&&i.set(o,u,c)}var f=l.transpose(i);return l.maximum(i,f)},t.prototype.transform=function(t){var r=this,e=this.X;if(void 0===e||0===e.length)throw new Error("No data has been fit.");var n=Math.floor(this.nNeighbors*this.transformQueueSize),i=c.initializeSearch(this.rpForest,e,t,n,this.initFromRandom,this.initFromTree,this.random),o=this.search(e,this.searchGraph,i,t),s=u.deheapSort(o),a=s.indices,h=s.weights;a=a.map(function(t){return t.slice(0,r.nNeighbors)}),h=h.map(function(t){return t.slice(0,r.nNeighbors)});var f=Math.max(0,this.localConnectivity-1),g=this.smoothKNNDistance(h,this.nNeighbors,f),p=g.sigmas,v=g.rhos,d=this.computeMembershipStrengths(a,h,p,v),w=d.rows,y=d.cols,b=d.vals,M=[t.length,e.length],x=new l.SparseMatrix(w,y,b,M),E=l.normalize(x,"l1"),R=l.getCSR(E),k=t.length,z=S(m.reshape2d(R.indices,k,this.nNeighbors),m.reshape2d(R.values,k,this.nNeighbors),this.embedding),N=this.nEpochs?this.nEpochs/3:x.nRows<=1e4?100:30,A=x.getValues().reduce(function(t,r){return r>t?r:t},0);x=x.map(function(t){return t0});if(g.length>=e){var p=Math.floor(e),v=e-p;p>0?(s[h]=g[p-1],v>1e-5&&(s[h]+=v*(g[p]-g[p-1]))):s[h]=v*g[0]}else g.length>0&&(s[h]=m.max(g));for(var d=0;d0?Math.exp(-b/c):1}if(Math.abs(w-o)<1e-5)break;w>o?c=(u+(l=c))/2:(u=c,l===1/0?c*=2:c=(u+l)/2)}if(a[h]=c,s[h]>0){var M=m.mean(f);a[h]<.001*M&&(a[h]=.001*M)}else{var x=m.mean(t.map(m.mean));a[h]<.001*x&&(a[h]=.001*x)}}return{sigmas:a,rhos:s}},t.prototype.computeMembershipStrengths=function(t,r,e,n){for(var i=t.length,o=t[0].length,s=m.zeros(i*o),a=m.zeros(i*o),h=m.zeros(i*o),u=0;u0&&(e[n]=r/i[n])}),e},t.prototype.assignOptimizationStateParameters=function(t){Object.assign(this.optimizationState,t)},t.prototype.prepareForOptimizationLoop=function(){var t=this.repulsionStrength,r=this.learningRate,e=this.negativeSampleRate,n=this.optimizationState,i=n.epochsPerSample,o=n.headEmbedding,a=n.tailEmbedding,h=o[0].length,u=o.length===a.length,l=i.map(function(t){return t/e}),c=s(l),f=s(i);this.assignOptimizationStateParameters({epochOfNextSample:f,epochOfNextNegativeSample:c,epochsPerNegativeSample:l,moveOther:u,initialAlpha:r,alpha:r,gamma:t,dim:h})},t.prototype.initializeOptimization=function(){var t=this.embedding,r=this.embedding,e=this.optimizationState,n=e.head,i=e.tail,o=e.epochsPerSample,s=this.getNEpochs(),a=this.graph.nCols,h=b(this.spread,this.minDist),u=h.a,l=h.b;this.assignOptimizationStateParameters({headEmbedding:t,tailEmbedding:r,head:n,tail:i,epochsPerSample:o,a:u,b:l,nEpochs:s,nVertices:a})},t.prototype.optimizeLayoutStep=function(t){for(var r=this.optimizationState,e=r.head,n=r.tail,i=r.headEmbedding,o=r.tailEmbedding,s=r.epochsPerSample,a=r.epochOfNextSample,h=r.epochOfNextNegativeSample,u=r.epochsPerNegativeSample,l=r.moveOther,c=r.initialAlpha,f=r.alpha,g=r.gamma,p=r.a,v=r.b,d=r.dim,b=r.nEpochs,M=r.nVertices,x=0;xt)){var S=e[x],E=n[x],R=i[S],k=o[E],z=y(R,k),N=0;z>0&&(N=-2*p*v*Math.pow(z,v-1),N/=p*Math.pow(z,v)+1);for(var A=0;A0)O=2*g*v,O/=(.001+I)*(p*Math.pow(I,v)+1);else if(S===P)continue;for(A=0;A0&&(V=w(O*(R[A]-j[A]),4)),R[A]+=V*f}}h[x]+=C*u[x]}return r.alpha=c*(1-t/b),r.currentEpoch+=1,i},t.prototype.optimizeLayoutAsync=function(t){var r=this;return void 0===t&&(t=function(){return!0}),new Promise(function(e,o){var s=function(){return n(r,void 0,void 0,function(){var r,n,a,h,u,l;return i(this,function(i){try{if(r=this.optimizationState,n=r.nEpochs,a=r.currentEpoch,this.embedding=this.optimizeLayoutStep(a),h=this.optimizationState.currentEpoch,u=!1===t(h),l=h===n,u||l)return[2,e(l)];s()}catch(t){o(t)}return[2]})})};s()})},t.prototype.optimizeLayout=function(t){void 0===t&&(t=function(){return!0});for(var r=!1,e=[];!r;){var n=this.optimizationState,i=n.nEpochs,o=n.currentEpoch;e=this.optimizeLayoutStep(o);var s=this.optimizationState.currentEpoch,a=!1===t(s);r=s===i||a}return e},t.prototype.getNEpochs=function(){var t=this.graph;if(this.nEpochs>0)return this.nEpochs;var r=t.nRows;return r<=2500?500:r<=5e3?400:r<=7500?300:200},t}();function v(t,r){for(var e=0,n=0;nr?r:t<-r?-r:t}function y(t,r){for(var e=0,n=0;n=r?Math.exp(-(e[i]-r)/t):n}),i={x:e,y:n},s={damping:1.5,initialValues:[.5,.5],gradientDifference:.1,maxIterations:100,errorTolerance:.01},a=g.default(i,function(t){var r=o(t,2),e=r[0],n=r[1];return function(t){return 1/(1+e*Math.pow(t,2*n))}},s).parameterValues,h=o(a,2);return{a:h[0],b:h[1]}}function M(t,r,e,n){return void 0===e&&(e=1),void 0===n&&(n=5),t.map(function(t,i,o){return-1===r[i]||-1===r[o]?t*Math.exp(-e):r[i]!==r[o]?t*Math.exp(-n):t})}function x(t){t=l.normalize(t,"max");var r=l.transpose(t),e=l.pairwiseMultiply(r,t);return t=l.add(t,l.subtract(r,e)),l.eliminateZeros(t)}function S(t,r,e){for(var n=m.zeros(t.length).map(function(t){return m.zeros(e[0].length)}),i=0;i=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}},i=this&&this.__importStar||function(t){if(t&&t.__esModule)return t;var r={};if(null!=t)for(var e in t)Object.hasOwnProperty.call(t,e)&&(r[e]=t[e]);return r.default=t,r};Object.defineProperty(r,"__esModule",{value:!0});var o=i(e(2)),s=i(e(3)),a=i(e(4)),h=i(e(1));r.makeNNDescent=function(t,r){return function(e,n,i,s,a,u,l,c){void 0===s&&(s=10),void 0===a&&(a=50),void 0===u&&(u=.001),void 0===l&&(l=.5),void 0===c&&(c=!0);for(var f=e.length,m=o.makeHeap(e.length,i),g=0;gr&&(r=t[e]);return r};var s=function(t){if(!i()(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");for(var r=t[0],e=1;e1&&void 0!==arguments[1]?arguments[1]:{};if(!i()(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");if(void 0!==e.output){if(!i()(e.output))throw new TypeError("output option must be an array if specified");r=e.output}else r=new Array(t.length);var n=s(t),a=o(t);if(n===a)throw new RangeError("minimum and maximum input values are equal. Cannot rescale a constant array");var h=e.min,u=void 0===h?e.autoMinMax?n:0:h,l=e.max,c=void 0===l?e.autoMinMax?a:1:l;if(u>=c)throw new RangeError("min option must be smaller than max option");for(var f=(c-u)/(a-n),m=0;mMath.abs(h[i])&&(i=r);if(i!==e){for(n=0;n=0;i--){for(n=0;no?t[i][o]:i===o?1:0;return n}get upperTriangularMatrix(){for(var t=this.LU,r=t.rows,e=t.columns,n=new _(r,e),i=0;iMath.abs(r)?(e=r/t,Math.abs(t)*Math.sqrt(1+e*e)):0!==r?(e=t/r,Math.abs(r)*Math.sqrt(1+e*e)):0}function l(t,r,e){for(var n=new Array(t),i=0;i=0;t--)if(0!==v[t]){for(let r=t+1;r=0;t--){if(t0;){let t,r;for(t=R-2;t>=-1&&-1!==t;t--){const r=Number.MIN_VALUE+z*Math.abs(v[t]+Math.abs(v[t+1]));if(Math.abs(y[t])<=r||Number.isNaN(y[t])){y[t]=0;break}}if(t===R-2)r=4;else{let e;for(e=R-1;e>=t&&e!==t;e--){let r=(e!==R?Math.abs(y[e]):0)+(e!==t+1?Math.abs(y[e-1]):0);if(Math.abs(v[e])<=z*r){v[e]=0;break}}e===t?r=3:e===R-1?r=1:(r=2,t=e)}switch(t++,r){case 1:{let r=y[R-2];y[R-2]=0;for(let e=R-2;e>=t;e--){let i=u(v[e],r),o=v[e]/i,s=r/i;if(v[e]=i,e!==t&&(r=-s*y[e-1],y[e-1]=o*y[e-1]),c)for(let t=0;t=v[t+1]);){let r=v[t];if(v[t]=v[t+1],v[t+1]=r,c&&tr?i[o][e]=t[o][e]/this.s[e]:i[o][e]=0;var o=this.U,s=o.length,a=o[0].length,h=new _(e,s);for(let t=0;tt&&r++;return r}get diagonal(){return this.s}get threshold(){return Number.EPSILON/2*Math.max(this.m,this.n)*this.s[0]}get leftSingularVectors(){return _.isMatrix(this.U)||(this.U=new _(this.U)),this.U}get rightSingularVectors(){return _.isMatrix(this.V)||(this.V=new _(this.V)),this.V}get diagonalMatrix(){return _.diag(this.s)}}function f(t,r,e){var n=e?t.rows:t.rows-1;if(r<0||r>n)throw new RangeError("Row index out of range")}function m(t,r,e){var n=e?t.columns:t.columns-1;if(r<0||r>n)throw new RangeError("Column index out of range")}function g(t,r){if(r.to1DArray&&(r=r.to1DArray()),r.length!==t.columns)throw new RangeError("vector size must be the same as the number of columns");return r}function p(t,r){if(r.to1DArray&&(r=r.to1DArray()),r.length!==t.rows)throw new RangeError("vector size must be the same as the number of rows");return r}function v(t,r,e){return{row:d(t,r),column:w(t,e)}}function d(t,r){if("object"!=typeof r)throw new TypeError("unexpected type for row indices");if(r.some(r=>r<0||r>=t.rows))throw new RangeError("row indices are out of range");return Array.isArray(r)||(r=Array.from(r)),r}function w(t,r){if("object"!=typeof r)throw new TypeError("unexpected type for column indices");if(r.some(r=>r<0||r>=t.columns))throw new RangeError("column indices are out of range");return Array.isArray(r)||(r=Array.from(r)),r}function y(t,r,e,n,i){if(5!==arguments.length)throw new RangeError("expected 4 arguments");if(b("startRow",r),b("endRow",e),b("startColumn",n),b("endColumn",i),r>e||n>i||r<0||r>=t.rows||e<0||e>=t.rows||n<0||n>=t.columns||i<0||i>=t.columns)throw new RangeError("Submatrix indices are out of range")}function b(t,r){if("number"!=typeof r)throw new TypeError(`${t} must be a number`)}class M extends(C()){constructor(t,r,e){super(),this.matrix=t,this.rows=r,this.columns=e}static get[Symbol.species](){return _}}class x extends M{constructor(t){super(t,t.columns,t.rows)}set(t,r,e){return this.matrix.set(r,t,e),this}get(t,r){return this.matrix.get(r,t)}}class S extends M{constructor(t,r){super(t,1,t.columns),this.row=r}set(t,r,e){return this.matrix.set(this.row,r,e),this}get(t,r){return this.matrix.get(this.row,r)}}class E extends M{constructor(t,r,e,n,i){y(t,r,e,n,i),super(t,e-r+1,i-n+1),this.startRow=r,this.startColumn=n}set(t,r,e){return this.matrix.set(this.startRow+t,this.startColumn+r,e),this}get(t,r){return this.matrix.get(this.startRow+t,this.startColumn+r)}}class R extends M{constructor(t,r,e){var n=v(t,r,e);super(t,n.row.length,n.column.length),this.rowIndices=n.row,this.columnIndices=n.column}set(t,r,e){return this.matrix.set(this.rowIndices[t],this.columnIndices[r],e),this}get(t,r){return this.matrix.get(this.rowIndices[t],this.columnIndices[r])}}class k extends M{constructor(t,r){super(t,(r=d(t,r)).length,t.columns),this.rowIndices=r}set(t,r,e){return this.matrix.set(this.rowIndices[t],r,e),this}get(t,r){return this.matrix.get(this.rowIndices[t],r)}}class z extends M{constructor(t,r){r=w(t,r),super(t,t.rows,r.length),this.columnIndices=r}set(t,r,e){return this.matrix.set(t,this.columnIndices[r],e),this}get(t,r){return this.matrix.get(t,this.columnIndices[r])}}class N extends M{constructor(t,r){super(t,t.rows,1),this.column=r}set(t,r,e){return this.matrix.set(t,this.column,e),this}get(t){return this.matrix.get(t,this.column)}}class A extends M{constructor(t){super(t,t.rows,t.columns)}set(t,r,e){return this.matrix.set(this.rows-t-1,r,e),this}get(t,r){return this.matrix.get(this.rows-t-1,r)}}class V extends M{constructor(t){super(t,t.rows,t.columns)}set(t,r,e){return this.matrix.set(t,this.columns-r-1,e),this}get(t,r){return this.matrix.get(t,this.columns-r-1)}}function C(t){void 0===t&&(t=Object);class r extends t{static get[Symbol.species](){return this}static from1DArray(t,r,e){if(t*r!==e.length)throw new RangeError("Data length does not match given dimensions");for(var n=new this(t,r),i=0;it&&(t=this.get(r,e));return t}maxIndex(){for(var t=this.get(0,0),r=[0,0],e=0;et&&(t=this.get(e,n),r[0]=e,r[1]=n);return r}min(){for(var t=this.get(0,0),r=0;rr&&(r=this.get(t,e));return r}maxRowIndex(t){f(this,t);for(var r=this.get(t,0),e=[t,0],n=1;nr&&(r=this.get(t,n),e[1]=n);return e}minRow(t){f(this,t);for(var r=this.get(t,0),e=1;er&&(r=this.get(e,t));return r}maxColumnIndex(t){m(this,t);for(var r=this.get(0,t),e=[0,t],n=1;nr&&(r=this.get(n,t),e[0]=n);return e}minColumn(t){m(this,t);for(var r=this.get(0,t),e=1;e=(r=void 0===r?1:r))throw new RangeError("min should be strictly smaller than max");for(var e=this.constructor.empty(this.rows,this.columns),n=0;n=(r=void 0===r?1:r))throw new RangeError("min should be strictly smaller than max");for(var e=this.constructor.empty(this.rows,this.columns),n=0;ne||r<0||r>=this.columns||e<0||e>=this.columns)throw new RangeError("Argument out of range");for(var n=new this.constructor[Symbol.species](t.length,e-r+1),i=0;i=this.rows)throw new RangeError(`Row index out of range: ${t[i]}`);n.set(i,o-r,this.get(t[i],o))}return n}subMatrixColumn(t,r,e){if(void 0===r&&(r=0),void 0===e&&(e=this.rows-1),r>e||r<0||r>=this.rows||e<0||e>=this.rows)throw new RangeError("Argument out of range");for(var n=new this.constructor[Symbol.species](e-r+1,t.length),i=0;i=this.columns)throw new RangeError(`Column index out of range: ${t[i]}`);n.set(o-r,i,this.get(o,t[i]))}return n}setSubMatrix(t,r,e){y(this,r,r+(t=this.constructor.checkMatrix(t)).rows-1,e,e+t.columns-1);for(var n=0;nt?i[o]=1/i[o]:i[o]=0;return i=this.constructor[Symbol.species].diag(i),n.mmul(i.mmul(e.transposeView()))}clone(){for(var t=new this.constructor[Symbol.species](this.rows,this.columns),r=0;r>","signPropagatingRightShift"],[">>>","rightShift","zeroFillRightShift"]]){var u=o($("\n(function %name%(value) {\n if (typeof value === 'number') return this.%name%S(value);\n return this.%name%M(value);\n})\n",{name:s[1],op:s[0]})),l=o($("\n(function %name%S(value) {\n for (var i = 0; i < this.rows; i++) {\n for (var j = 0; j < this.columns; j++) {\n this.set(i, j, this.get(i, j) %op% value);\n }\n }\n return this;\n})\n",{name:`${s[1]}S`,op:s[0]})),d=o($("\n(function %name%M(matrix) {\n matrix = this.constructor.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 (var i = 0; i < this.rows; i++) {\n for (var j = 0; j < this.columns; j++) {\n this.set(i, j, this.get(i, j) %op% matrix.get(i, j));\n }\n }\n return this;\n})\n",{name:`${s[1]}M`,op:s[0]})),w=o($("\n(function %name%(matrix, value) {\n var newMatrix = new this[Symbol.species](matrix);\n return newMatrix.%name%(value);\n})\n",{name:s[1]}));for(n=1;n0){if(super(t),!(Number.isInteger(r)&&r>0))throw new TypeError("nColumns must be a positive integer");for(e=0;e=0;o--){for(i=0;i=0;e--){for(t=0;ti)return new Array(r.rows+1).fill(0);for(var o=r.addRow(e,[0]),s=0;s0;a--){for(c=0,s=0,u=0;u0&&(o=-o),r[a]=c*o,s-=i*o,e[a-1]=i-o,h=0;hl){0;do{for(1,i=e[l],f=(e[l+1]-i)/(2*r[l]),m=u(f,1),f<0&&(m=-m),e[l]=r[l]/(f+m),e[l+1]=r[l]*(f+m),g=e[l+1],o=i-e[l],s=l+2;s=l;s--)for(d=v,v=p,b=y,i=p*r[s],o=p*f,m=u(f,r[s]),r[s+1]=y*m,y=r[s]/m,f=(p=f/m)*e[s]-y*i,e[s+1]=o+y*(p*i+y*e[s]),h=0;hS*x)}e[l]=e[l]+M,r[l]=0}for(s=0;s=u;a--)e[a]=r[a][u-1]/l,s+=e[a]*e[a];for(o=Math.sqrt(s),e[u]>0&&(o=-o),s-=e[u]*o,e[u]=e[u]-o,h=u;h=u;a--)i+=e[a]*r[a][h];for(i/=s,a=u;a<=c;a++)r[a][h]-=i*e[a]}for(a=0;a<=c;a++){for(i=0,h=c;h>=u;h--)i+=e[h]*r[a][h];for(i/=s,h=u;h<=c;h++)r[a][h]-=i*e[h]}e[u]=l*e[u],r[u][u-1]=l*o}}for(a=0;a=1;u--)if(0!==r[u][u-1]){for(a=u+1;a<=c;a++)e[a]=r[a][u-1];for(h=u;h<=c;h++){for(o=0,a=u;a<=c;a++)o+=e[a]*n[a][h];for(o=o/e[u]/r[u][u-1],a=u;a<=c;a++)n[a][h]+=o*e[a]}}}(o,f,m,s),function(t,r,e,n,i){var o,s,a,h,u,l,c,f,m,g,p,v,d,w,y,b=t-1,M=t-1,x=Number.EPSILON,S=0,E=0,R=0,k=0,z=0,N=0,A=0,V=0;for(o=0;oM)&&(e[o]=i[o][o],r[o]=0),s=Math.max(o-1,0);s=0;){for(h=b;h>0&&(0===(N=Math.abs(i[h-1][h-1])+Math.abs(i[h][h]))&&(N=E),!(Math.abs(i[h][h-1])=0){for(A=R>=0?R+A:R-A,e[b-1]=f+A,e[b]=e[b-1],0!==A&&(e[b]=f-c/A),r[b-1]=0,r[b]=0,f=i[b][b-1],N=Math.abs(f)+Math.abs(A),R=f/N,k=A/N,z=Math.sqrt(R*R+k*k),R/=z,k/=z,s=b-1;s0){for(N=Math.sqrt(N),m=h&&(A=i[u][u],R=((z=f-A)*(N=m-A)-c)/i[u+1][u]+i[u][u+1],k=i[u+1][u+1]-A-z-N,z=i[u+2][u+1],N=Math.abs(R)+Math.abs(k)+Math.abs(z),R/=N,k/=N,z/=N,u!==h)&&!(Math.abs(i[u][u-1])*(Math.abs(k)+Math.abs(z))u+2&&(i[o][o-3]=0);for(a=u;a<=b-1&&(w=a!==b-1,a!==u&&(R=i[a][a-1],k=i[a+1][a-1],z=w?i[a+2][a-1]:0,0!==(f=Math.abs(R)+Math.abs(k)+Math.abs(z))&&(R/=f,k/=f,z/=f)),0!==f);a++)if(N=Math.sqrt(R*R+k*k+z*z),R<0&&(N=-N),0!==N){for(a!==u?i[a][a-1]=-N*f:h!==u&&(i[a][a-1]=-i[a][a-1]),f=(R+=N)/N,m=k/N,A=z/N,k/=R,z/=R,s=a;s=0;b--)if(R=e[b],0===(k=r[b]))for(h=b,i[b][b]=1,o=b-1;o>=0;o--){for(c=i[o][o]-R,z=0,s=h;s<=b;s++)z+=i[o][s]*i[s][b];if(r[o]<0)A=c,N=z;else if(h=o,0===r[o]?i[o][b]=0!==c?-z/c:-z/(x*E):(f=i[o][o+1],m=i[o+1][o],k=(e[o]-R)*(e[o]-R)+r[o]*r[o],l=(f*N-A*z)/k,i[o][b]=l,i[o+1][b]=Math.abs(f)>Math.abs(A)?(-z-c*l)/f:(-N-m*l)/A),l=Math.abs(i[o][b]),x*l*l>1)for(s=o;s<=b;s++)i[s][b]=i[s][b]/l}else if(k<0)for(h=b-1,Math.abs(i[b][b-1])>Math.abs(i[b-1][b])?(i[b-1][b-1]=k/i[b][b-1],i[b-1][b]=-(i[b][b]-R)/i[b][b-1]):(y=$(0,-i[b-1][b],i[b-1][b-1]-R,k),i[b-1][b-1]=y[0],i[b-1][b]=y[1]),i[b][b-1]=0,i[b][b]=1,o=b-2;o>=0;o--){for(g=0,p=0,s=h;s<=b;s++)g+=i[o][s]*i[s][b-1],p+=i[o][s]*i[s][b];if(c=i[o][o]-R,r[o]<0)A=c,z=g,N=p;else if(h=o,0===r[o]?(y=$(-g,-p,c,k),i[o][b-1]=y[0],i[o][b]=y[1]):(f=i[o][o+1],m=i[o+1][o],v=(e[o]-R)*(e[o]-R)+r[o]*r[o]-k*k,d=2*(e[o]-R)*k,0===v&&0===d&&(v=x*E*(Math.abs(c)+Math.abs(k)+Math.abs(f)+Math.abs(m)+Math.abs(A))),y=$(f*z-A*g+k*p,f*N-A*p-k*g,v,d),i[o][b-1]=y[0],i[o][b]=y[1],Math.abs(f)>Math.abs(A)+Math.abs(k)?(i[o+1][b-1]=(-g-c*i[o][b-1]+k*i[o][b])/f,i[o+1][b]=(-p-c*i[o][b]-k*i[o][b-1])/f):(y=$(-z-m*i[o][b-1],-N-m*i[o][b],A,k),i[o+1][b-1]=y[0],i[o+1][b]=y[1])),l=Math.max(Math.abs(i[o][b-1]),Math.abs(i[o][b])),x*l*l>1)for(s=o;s<=b;s++)i[s][b-1]=i[s][b-1]/l,i[s][b]=i[s][b]/l}for(o=0;oM)for(s=o;s=0;s--)for(o=0;o<=M;o++){for(A=0,a=0;a<=Math.min(s,M);a++)A+=n[o][a]*i[a][s];n[o][s]=A}}(o,h,a,s,f)}this.n=o,this.e=h,this.d=a,this.V=s}get realEigenvalues(){return this.d}get imaginaryEigenvalues(){return this.e}get eigenvectorMatrix(){return _.isMatrix(this.V)||(this.V=new _(this.V)),this.V}get diagonalMatrix(){var t,r,e=this.n,n=this.e,i=this.d,o=new _(e,e);for(t=0;t0?o[t][t+1]=n[t]:n[t]<0&&(o[t][t-1]=n[t])}return o}}function $(t,r,e,n){var i,o;return Math.abs(e)>Math.abs(n)?[(t+(i=n/e)*r)/(o=e+i*n),(r-i*t)/o]:[((i=e/n)*t+r)/(o=n+i*e),(i*r-t)/o]}class Q{constructor(t){if(!(t=j.checkMatrix(t)).isSymmetric())throw new Error("Matrix is not symmetric");var r,e,n,i=t,o=i.rows,s=new _(o,o),a=!0;for(e=0;e0,s[e][e]=Math.sqrt(Math.max(u,0)),n=e+1;n=0;o--)for(i=0;i Date: Fri, 7 Jun 2019 17:19:20 -0700 Subject: [PATCH 25/46] Update version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 881bee7..cc8c642 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "umap-js", - "version": "1.3.0", + "version": "1.3.1", "description": "JavaScript implementation of UMAP", "author": { "name": "Andy Coenen", From 9ade3349667144a5b4238621f585d65640615856 Mon Sep 17 00:00:00 2001 From: Dan Roberts Date: Tue, 25 Jun 2019 18:04:27 +0100 Subject: [PATCH 26/46] I think that this test is wrong (matrix.get) --- test/matrix.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/matrix.test.ts b/test/matrix.test.ts index b7fbff8..49ce015 100644 --- a/test/matrix.test.ts +++ b/test/matrix.test.ts @@ -36,7 +36,7 @@ describe('sparse matrix', () => { test('constructs a sparse matrix from rows/cols/vals ', () => { const rows = [0, 0, 1, 1]; const cols = [0, 1, 0, 1]; - const vals = [1, 2]; + const vals = [1, 2, 3, 4]; const dims = [2, 2]; const matrix = new SparseMatrix(rows, cols, vals, dims); expect(matrix.getRows()).toEqual(rows); From 618f68c6597538810480370a81342ebe7fe4eb37 Mon Sep 17 00:00:00 2001 From: Dan Roberts Date: Tue, 25 Jun 2019 18:05:51 +0100 Subject: [PATCH 27/46] Change the internal format of SparseMatrix to allow an efficient "getAll" method to be written --- src/matrix.ts | 71 ++++++++++++++++++++++----------------------- test/matrix.test.ts | 15 ++++++++++ 2 files changed, 50 insertions(+), 36 deletions(-) diff --git a/src/matrix.ts b/src/matrix.ts index da384ef..a06afa0 100644 --- a/src/matrix.ts +++ b/src/matrix.ts @@ -19,15 +19,13 @@ import * as utils from './utils'; +type Entry = { value: number; row: number; col: number }; + /** * Internal 2-dimensional sparse matrix class */ export class SparseMatrix { - private rows: number[]; - private cols: number[]; - private values: number[]; - - private entries = new Map(); + private entries = new Map(); readonly nRows: number = 0; readonly nCols: number = 0; @@ -38,19 +36,20 @@ export class SparseMatrix { values: number[], dims: number[] ) { - // TODO: Assert that rows / cols / vals are the same length. - this.rows = [...rows]; - this.cols = [...cols]; - this.values = [...values]; - - for (let i = 0; i < values.length; i++) { - const key = this.makeKey(this.rows[i], this.cols[i]); - this.entries.set(key, i); + if ((rows.length !== cols.length) || (rows.length !== values.length)) { + throw new Error("rows, cols and values arrays must all have the same length"); } // TODO: Assert that dims are legit. this.nRows = dims[0]; this.nCols = dims[1]; + for (let i = 0; i < values.length; i++) { + var row = rows[i]; + var col = cols[i]; + this.checkDims(row, col); + const key = this.makeKey(row, col); + this.entries.set(key, { value: values[i], row, col }); + } } private makeKey(row: number, col: number): string { @@ -60,7 +59,7 @@ export class SparseMatrix { private checkDims(row: number, col: number) { const withinBounds = row < this.nRows && col < this.nCols; if (!withinBounds) { - throw new Error('array index out of bounds'); + throw new Error('row and/or col specified outside of matrix dimensions'); } } @@ -68,13 +67,9 @@ export class SparseMatrix { this.checkDims(row, col); const key = this.makeKey(row, col); if (!this.entries.has(key)) { - this.rows.push(row); - this.cols.push(col); - this.values.push(value); - this.entries.set(key, this.values.length - 1); + this.entries.set(key, { value, row, col }); } else { - const index = this.entries.get(key)!; - this.values[index] = value; + this.entries.get(key)!.value = value; } } @@ -82,42 +77,47 @@ export class SparseMatrix { this.checkDims(row, col); const key = this.makeKey(row, col); if (this.entries.has(key)) { - const index = this.entries.get(key)!; - return this.values[index]; + return this.entries.get(key)!.value; } else { return defaultValue; } } + getAll(): { value: number; row: number; col: number }[] { + let rowColValues: Entry[] = []; + this.entries.forEach((value) => { + rowColValues.push(value); + }); + return rowColValues; + } + getDims(): number[] { return [this.nRows, this.nCols]; } getRows(): number[] { - return [...this.rows]; + return Array.from(this.entries, ([key, value]) => value.row); } getCols(): number[] { - return [...this.cols]; + return Array.from(this.entries, ([key, value]) => value.col); } getValues(): number[] { - return [...this.values]; + return Array.from(this.entries, ([key, value]) => value.value); } forEach(fn: (value: number, row: number, col: number) => void): void { - for (let i = 0; i < this.values.length; i++) { - fn(this.values[i], this.rows[i], this.cols[i]); - } + this.entries.forEach((value) => fn(value.value, value.row, value.col)); } map(fn: (value: number, row: number, col: number) => number): SparseMatrix { let vals: number[] = []; - for (let i = 0; i < this.values.length; i++) { - vals.push(fn(this.values[i], this.rows[i], this.cols[i])); - } + this.entries.forEach((value) => { + vals.push(fn(value.value, value.row, value.col)); + }); const dims = [this.nRows, this.nCols]; - return new SparseMatrix(this.rows, this.cols, vals, dims); + return new SparseMatrix(this.getRows(), this.getCols(), vals, dims); } toArray() { @@ -125,9 +125,9 @@ export class SparseMatrix { const output = rows.map(() => { return utils.zeros(this.nCols); }); - for (let i = 0; i < this.values.length; i++) { - output[this.rows[i]][this.cols[i]] = this.values[i]; - } + this.entries.forEach((value) => { + output[value.row][value.col] = value.value; + }); return output; } } @@ -338,7 +338,6 @@ function elementWise( * search logic depends on this data format. */ export function getCSR(x: SparseMatrix) { - type Entry = { value: number; row: number; col: number }; const entries: Entry[] = []; x.forEach((value, row, col) => { diff --git a/test/matrix.test.ts b/test/matrix.test.ts index 49ce015..82cdf45 100644 --- a/test/matrix.test.ts +++ b/test/matrix.test.ts @@ -58,6 +58,21 @@ describe('sparse matrix', () => { expect(matrix.get(0, 1)).toEqual(9); }); + test('sparse matrix has getAll method', () => { + const rows = [0, 0, 1, 1]; + const cols = [0, 1, 0, 1]; + const vals = [1, 2, 3, 4]; + const dims = [2, 2]; + const matrix = new SparseMatrix(rows, cols, vals, dims); + + expect(matrix.getAll()).toEqual([ + { row: 0, col: 0, value: 1 }, + { row: 0, col: 1, value: 2 }, + { row: 1, col: 0, value: 3 }, + { row: 1, col: 1, value: 4 } + ]); + }); + test('sparse matrix has toArray method', () => { const rows = [0, 0, 1, 1]; const cols = [0, 1, 0, 1]; From b350853137cbe1c39159201d05504a4a52fb7a01 Mon Sep 17 00:00:00 2001 From: Dan Roberts Date: Tue, 25 Jun 2019 18:06:22 +0100 Subject: [PATCH 28/46] Use SparseMatrix "getAll" method to retrieve all values, rather than enumerating across all possible row/col combinations and looking for a value there --- src/umap.ts | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/umap.ts b/src/umap.ts index 47f65b9..261f709 100644 --- a/src/umap.ts +++ b/src/umap.ts @@ -809,16 +809,15 @@ export class UMAP { const weights: number[] = []; const head: number[] = []; const tail: number[] = []; - for (let i = 0; i < graph.nRows; i++) { - for (let j = 0; j < graph.nCols; j++) { - const value = graph.get(i, j); - if (value) { - weights.push(value); - tail.push(i); - head.push(j); + var rowColValues = graph.getAll(); + for (let i = 0; i < rowColValues.length; i++) { + const entry = rowColValues[i]; + if (entry.value) { + weights.push(entry.value); + tail.push(entry.row); + head.push(entry.col); } - } - } + } const epochsPerSample = this.makeEpochsPerSample(weights, nEpochs); return { head, tail, epochsPerSample }; From 247f978e76b32cc0637cc9462bf4208e7d04a69d Mon Sep 17 00:00:00 2001 From: Dan Roberts Date: Tue, 2 Jul 2019 16:23:23 +0100 Subject: [PATCH 29/46] Ensure to use const where possible --- src/matrix.ts | 4 ++-- src/umap.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/matrix.ts b/src/matrix.ts index a06afa0..2b73361 100644 --- a/src/matrix.ts +++ b/src/matrix.ts @@ -44,8 +44,8 @@ export class SparseMatrix { this.nRows = dims[0]; this.nCols = dims[1]; for (let i = 0; i < values.length; i++) { - var row = rows[i]; - var col = cols[i]; + const row = rows[i]; + const col = cols[i]; this.checkDims(row, col); const key = this.makeKey(row, col); this.entries.set(key, { value: values[i], row, col }); diff --git a/src/umap.ts b/src/umap.ts index 261f709..b68ea24 100644 --- a/src/umap.ts +++ b/src/umap.ts @@ -809,7 +809,7 @@ export class UMAP { const weights: number[] = []; const head: number[] = []; const tail: number[] = []; - var rowColValues = graph.getAll(); + const rowColValues = graph.getAll(); for (let i = 0; i < rowColValues.length; i++) { const entry = rowColValues[i]; if (entry.value) { From d32c976655b5d7a574206f0653627c57d31fa4b5 Mon Sep 17 00:00:00 2001 From: Dan Roberts Date: Tue, 2 Jul 2019 16:23:33 +0100 Subject: [PATCH 30/46] Make indentation consistent --- src/umap.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/umap.ts b/src/umap.ts index b68ea24..5b19a69 100644 --- a/src/umap.ts +++ b/src/umap.ts @@ -811,12 +811,12 @@ export class UMAP { const tail: number[] = []; const rowColValues = graph.getAll(); for (let i = 0; i < rowColValues.length; i++) { - const entry = rowColValues[i]; - if (entry.value) { - weights.push(entry.value); - tail.push(entry.row); - head.push(entry.col); - } + const entry = rowColValues[i]; + if (entry.value) { + weights.push(entry.value); + tail.push(entry.row); + head.push(entry.col); + } } const epochsPerSample = this.makeEpochsPerSample(weights, nEpochs); From 6e54c7cba21e2853c336cb4e9bc2ad3f6843d39d Mon Sep 17 00:00:00 2001 From: Dan Roberts Date: Tue, 2 Jul 2019 16:24:04 +0100 Subject: [PATCH 31/46] Order the matrix getAll() method's results to make them easier to test --- src/matrix.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/matrix.ts b/src/matrix.ts index 2b73361..cc4e52c 100644 --- a/src/matrix.ts +++ b/src/matrix.ts @@ -83,11 +83,20 @@ export class SparseMatrix { } } - getAll(): { value: number; row: number; col: number }[] { - let rowColValues: Entry[] = []; + getAll(ordered = true): { value: number; row: number; col: number }[] { + const rowColValues: Entry[] = []; this.entries.forEach((value) => { rowColValues.push(value); }); + if (ordered) { // Ordering the result isn't required for processing but it does make it easier to write tests + rowColValues.sort((a, b) => { + if (a.row === b.row) { + return a.col - b.col; + } else { + return a.row - b.row; + } + }); + } return rowColValues; } From 83ea206b26fe4c45a0b98ca35f45a1b4708e4273 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 15 Jul 2019 15:33:34 +0000 Subject: [PATCH 32/46] Bump lodash from 4.17.11 to 4.17.14 Bumps [lodash](https://github.com/lodash/lodash) from 4.17.11 to 4.17.14. - [Release notes](https://github.com/lodash/lodash/releases) - [Commits](https://github.com/lodash/lodash/compare/4.17.11...4.17.14) Signed-off-by: dependabot[bot] --- yarn.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/yarn.lock b/yarn.lock index 5fad749..b595e12 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2810,9 +2810,9 @@ lodash.sortby@^4.7.0: integrity sha1-7dFMgk4sycHgsKG0K7UhBRakJDg= lodash@^4.17.11: - version "4.17.11" - resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.11.tgz#b39ea6229ef607ecd89e2c8df12536891cac9b8d" - integrity sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg== + version "4.17.14" + resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.14.tgz#9ce487ae66c96254fe20b599f21b6816028078ba" + integrity sha512-mmKYbW3GLuJeX+iGP+Y7Gp1AiGHGbXHCOh/jZmrawMmsE7MS4znI3RL2FsjbqOyMayHInjOeykW7PEajUk1/xw== loose-envify@^1.0.0: version "1.4.0" From f6ba61acd1dec98298c6aa56afb67236668338af Mon Sep 17 00:00:00 2001 From: Andy Coenen Date: Mon, 22 Jul 2019 15:24:45 -0700 Subject: [PATCH 33/46] Fix incorrect sort method in getCSR --- src/matrix.ts | 19 +++++++++++-------- test/matrix.test.ts | 8 ++++---- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/matrix.ts b/src/matrix.ts index cc4e52c..815760c 100644 --- a/src/matrix.ts +++ b/src/matrix.ts @@ -36,8 +36,10 @@ export class SparseMatrix { values: number[], dims: number[] ) { - if ((rows.length !== cols.length) || (rows.length !== values.length)) { - throw new Error("rows, cols and values arrays must all have the same length"); + if (rows.length !== cols.length || rows.length !== values.length) { + throw new Error( + 'rows, cols and values arrays must all have the same length' + ); } // TODO: Assert that dims are legit. @@ -85,10 +87,11 @@ export class SparseMatrix { getAll(ordered = true): { value: number; row: number; col: number }[] { const rowColValues: Entry[] = []; - this.entries.forEach((value) => { + this.entries.forEach(value => { rowColValues.push(value); }); - if (ordered) { // Ordering the result isn't required for processing but it does make it easier to write tests + if (ordered) { + // Ordering the result isn't required for processing but it does make it easier to write tests rowColValues.sort((a, b) => { if (a.row === b.row) { return a.col - b.col; @@ -117,12 +120,12 @@ export class SparseMatrix { } forEach(fn: (value: number, row: number, col: number) => void): void { - this.entries.forEach((value) => fn(value.value, value.row, value.col)); + this.entries.forEach(value => fn(value.value, value.row, value.col)); } map(fn: (value: number, row: number, col: number) => number): SparseMatrix { let vals: number[] = []; - this.entries.forEach((value) => { + this.entries.forEach(value => { vals.push(fn(value.value, value.row, value.col)); }); const dims = [this.nRows, this.nCols]; @@ -134,7 +137,7 @@ export class SparseMatrix { const output = rows.map(() => { return utils.zeros(this.nCols); }); - this.entries.forEach((value) => { + this.entries.forEach(value => { output[value.row][value.col] = value.value; }); return output; @@ -357,7 +360,7 @@ export function getCSR(x: SparseMatrix) { if (a.row === b.row) { return a.col - b.col; } else { - return a.row - b.col; + return a.row - b.row; } }); diff --git a/test/matrix.test.ts b/test/matrix.test.ts index 82cdf45..4c07e70 100644 --- a/test/matrix.test.ts +++ b/test/matrix.test.ts @@ -69,7 +69,7 @@ describe('sparse matrix', () => { { row: 0, col: 0, value: 1 }, { row: 0, col: 1, value: 2 }, { row: 1, col: 0, value: 3 }, - { row: 1, col: 1, value: 4 } + { row: 1, col: 1, value: 4 }, ]); }); @@ -223,8 +223,8 @@ describe('normalize method', () => { test('getCSR function', () => { const { indices, values, indptr } = getCSR(A); - expect(indices).toEqual([0, 1, 2, 0, 0, 1, 2, 1, 2]); - expect(values).toEqual([1, 2, 3, 7, 4, 5, 6, 8, 9]); - expect(indptr).toEqual([0, 3, 4, 7]); + expect(indices).toEqual([0, 1, 2, 0, 1, 2, 0, 1, 2]); + expect(values).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9]); + expect(indptr).toEqual([0, 3, 6]); }); }); From 6c0080f09368f410a2fd4b65aebc8644312157d0 Mon Sep 17 00:00:00 2001 From: Kevin Robinson Date: Mon, 29 Jul 2019 13:16:47 -0400 Subject: [PATCH 34/46] Update `fitAsync` example in README to describe callback Thanks for sharing this awesome work! I made the wrong assumption about how this API worked reading the doc, figuring the callback would return the value. I think it's because the method uses both a callback and async/await, so I thought it'd help clarify that the callback is for tracking progress and stopping early. --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 683a95c..b4620cf 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,9 @@ const embedding = umap.fit(data); import { UMAP } from 'umap-js'; const umap = new UMAP(); -const embedding = await umap.fitAsync(data, callback); +const embedding = await umap.fitAsync(data, epochNumber => { + // check progress and give user feedback, or return `false` to stop +}); ``` #### Step-by-step fitting From 2d96fccafca29b571ac3329bcc4591eaee5db9b4 Mon Sep 17 00:00:00 2001 From: kevinrobinson Date: Mon, 29 Jul 2019 16:09:27 -0400 Subject: [PATCH 35/46] Add note to README about data constraints --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index b4620cf..51d84b3 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,8 @@ There are a few important differences between the python implementation and the - The optimization step is seeded with a random embedding rather than a spectral embedding. This gives comparable results for smaller datasets. The spectral embedding computation relies on efficient eigenvalue / eigenvector computations that are not easily done in JS. - There is no specialized functionality for angular distances or sparse data representations. +Note that depending on how you configure UMAP, the content of your data will need to satsify a few constraints. See https://github.com/lmcinnes/umap and https://arxiv.org/abs/1802.03426 for more details. + ### Usage #### Installation From 1ead5b4c249e49b760cced59a5aecdb70be85aaa Mon Sep 17 00:00:00 2001 From: kevinrobinson Date: Mon, 29 Jul 2019 16:37:01 -0400 Subject: [PATCH 36/46] Add helpful error message for not enoughd data and tests --- README.md | 2 -- src/tree.ts | 3 +++ test/tree.test.ts | 9 +++++++++ test/umap.test.ts | 8 +++++++- 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 51d84b3..b4620cf 100644 --- a/README.md +++ b/README.md @@ -11,8 +11,6 @@ There are a few important differences between the python implementation and the - The optimization step is seeded with a random embedding rather than a spectral embedding. This gives comparable results for smaller datasets. The spectral embedding computation relies on efficient eigenvalue / eigenvector computations that are not easily done in JS. - There is no specialized functionality for angular distances or sparse data representations. -Note that depending on how you configure UMAP, the content of your data will need to satsify a few constraints. See https://github.com/lmcinnes/umap and https://arxiv.org/abs/1802.03426 for more details. - ### Usage #### Installation diff --git a/src/tree.ts b/src/tree.ts index 834c374..94f9103 100644 --- a/src/tree.ts +++ b/src/tree.ts @@ -91,6 +91,9 @@ export function makeForest( random: RandomFn ) { const leafSize = Math.max(10, nNeighbors); + if (data.length <= nNeighbors) { + throw new Error(`Not enough data points (${data.length}) to create nNeighbors: ${nNeighbors}. Add more data points or adjust the configuration.`); + } const trees = utils .range(nTrees) diff --git a/test/tree.test.ts b/test/tree.test.ts index b0e8aa1..d3588d4 100644 --- a/test/tree.test.ts +++ b/test/tree.test.ts @@ -34,6 +34,15 @@ describe('umap knn tree methods', () => { expect(forest[0]).toEqual(treeData); }); + test('makeForest throws with a helpful message if not enough data is passed' , () => { + const nNeighbors = 15; + const nTrees = 6; + const smallData = testData.slice(0, 15); + expect(() => { + tree.makeForest(smallData, nNeighbors, nTrees, random) + }).toThrow(/Not enough data points to create nNeighbors/); + }); + test('makeLeafArray method flattens indices', () => { const nNeighbors = 15; const nTrees = 6; diff --git a/test/umap.test.ts b/test/umap.test.ts index 960e2b8..7d26afa 100644 --- a/test/umap.test.ts +++ b/test/umap.test.ts @@ -32,7 +32,7 @@ import { testData, testLabels, testResults2D, - testResults3D, + testResults3D } from './test_data'; import Prando from 'prando'; @@ -202,6 +202,12 @@ describe('UMAP', () => { expect(nInvocations).toBeGreaterThan(0); }); + + test('throws helpful error if not enough data', () => { + const umap = new UMAP({ random }); + const smallData = testData.slice(0, 15); + expect(() => umap.fit(smallData)).toThrow(/Not enough data points to create nNeighbors/); + }) }); function computeMeanDistances(vectors: number[][]) { From 892e174a28dad7106a17838ed9a908de05e89255 Mon Sep 17 00:00:00 2001 From: kevinrobinson Date: Mon, 29 Jul 2019 16:44:19 -0400 Subject: [PATCH 37/46] Shorter test expectations --- test/tree.test.ts | 2 +- test/umap.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/tree.test.ts b/test/tree.test.ts index d3588d4..eba10de 100644 --- a/test/tree.test.ts +++ b/test/tree.test.ts @@ -40,7 +40,7 @@ describe('umap knn tree methods', () => { const smallData = testData.slice(0, 15); expect(() => { tree.makeForest(smallData, nNeighbors, nTrees, random) - }).toThrow(/Not enough data points to create nNeighbors/); + }).toThrow(/Not enough data points/); }); test('makeLeafArray method flattens indices', () => { diff --git a/test/umap.test.ts b/test/umap.test.ts index 7d26afa..d670028 100644 --- a/test/umap.test.ts +++ b/test/umap.test.ts @@ -206,7 +206,7 @@ describe('UMAP', () => { test('throws helpful error if not enough data', () => { const umap = new UMAP({ random }); const smallData = testData.slice(0, 15); - expect(() => umap.fit(smallData)).toThrow(/Not enough data points to create nNeighbors/); + expect(() => umap.fit(smallData)).toThrow(/Not enough data points/); }) }); From d8d55f0deaf0bd0bf3100ce43f34ca705b0fb631 Mon Sep 17 00:00:00 2001 From: kevinrobinson Date: Mon, 29 Jul 2019 16:44:46 -0400 Subject: [PATCH 38/46] Add back trailing comma --- test/umap.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/umap.test.ts b/test/umap.test.ts index d670028..7ce6a40 100644 --- a/test/umap.test.ts +++ b/test/umap.test.ts @@ -32,7 +32,7 @@ import { testData, testLabels, testResults2D, - testResults3D + testResults3D, } from './test_data'; import Prando from 'prando'; From 8a09f28fc2bdc6f09914e4af992248d08c9da870 Mon Sep 17 00:00:00 2001 From: kevinrobinson Date: Tue, 30 Jul 2019 08:51:15 -0400 Subject: [PATCH 39/46] Move guard to initializeFit --- src/tree.ts | 3 --- src/umap.ts | 4 ++++ test/tree.test.ts | 9 --------- test/umap.test.ts | 4 ++-- 4 files changed, 6 insertions(+), 14 deletions(-) diff --git a/src/tree.ts b/src/tree.ts index 94f9103..834c374 100644 --- a/src/tree.ts +++ b/src/tree.ts @@ -91,9 +91,6 @@ export function makeForest( random: RandomFn ) { const leafSize = Math.max(10, nNeighbors); - if (data.length <= nNeighbors) { - throw new Error(`Not enough data points (${data.length}) to create nNeighbors: ${nNeighbors}. Add more data points or adjust the configuration.`); - } const trees = utils .range(nTrees) diff --git a/src/umap.ts b/src/umap.ts index 5b19a69..726fe1d 100644 --- a/src/umap.ts +++ b/src/umap.ts @@ -322,6 +322,10 @@ export class UMAP { * SGD optimization. */ initializeFit(X: Vectors): number { + if (X.length <= this.nNeighbors) { + throw new Error(`Not enough data points (${X.length}) to create nNeighbors: ${this.nNeighbors}. Add more data points or adjust the configuration.`); + } + // We don't need to reinitialize if we've already initialized for this data. if (this.X === X && this.isInitialized) { return this.getNEpochs(); diff --git a/test/tree.test.ts b/test/tree.test.ts index eba10de..b0e8aa1 100644 --- a/test/tree.test.ts +++ b/test/tree.test.ts @@ -34,15 +34,6 @@ describe('umap knn tree methods', () => { expect(forest[0]).toEqual(treeData); }); - test('makeForest throws with a helpful message if not enough data is passed' , () => { - const nNeighbors = 15; - const nTrees = 6; - const smallData = testData.slice(0, 15); - expect(() => { - tree.makeForest(smallData, nNeighbors, nTrees, random) - }).toThrow(/Not enough data points/); - }); - test('makeLeafArray method flattens indices', () => { const nNeighbors = 15; const nTrees = 6; diff --git a/test/umap.test.ts b/test/umap.test.ts index 7ce6a40..812455b 100644 --- a/test/umap.test.ts +++ b/test/umap.test.ts @@ -203,10 +203,10 @@ describe('UMAP', () => { expect(nInvocations).toBeGreaterThan(0); }); - test('throws helpful error if not enough data', () => { + test('initializeFit throws helpful error if not enough data', () => { const umap = new UMAP({ random }); const smallData = testData.slice(0, 15); - expect(() => umap.fit(smallData)).toThrow(/Not enough data points/); + expect(() => umap.initializeFit(smallData)).toThrow(/Not enough data points/); }) }); From 2238e1b877af425a1ef83baa99b0a72d211affa2 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 28 Aug 2019 22:25:46 +0000 Subject: [PATCH 40/46] Bump mixin-deep from 1.3.1 to 1.3.2 Bumps [mixin-deep](https://github.com/jonschlinkert/mixin-deep) from 1.3.1 to 1.3.2. - [Release notes](https://github.com/jonschlinkert/mixin-deep/releases) - [Commits](https://github.com/jonschlinkert/mixin-deep/compare/1.3.1...1.3.2) Signed-off-by: dependabot[bot] --- yarn.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/yarn.lock b/yarn.lock index b595e12..d8accd5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3008,9 +3008,9 @@ mississippi@^3.0.0: through2 "^2.0.0" mixin-deep@^1.2.0: - version "1.3.1" - resolved "https://registry.yarnpkg.com/mixin-deep/-/mixin-deep-1.3.1.tgz#a49e7268dce1a0d9698e45326c5626df3543d0fe" - integrity sha512-8ZItLHeEgaqEvd5lYBXfm4EZSFCX29Jb9K+lAHhDKzReKBQKj3R+7NOF6tjqYi9t4oI8VUfaWITJQm86wnXGNQ== + version "1.3.2" + resolved "https://registry.yarnpkg.com/mixin-deep/-/mixin-deep-1.3.2.tgz#1120b43dc359a785dce65b55b82e257ccf479566" + integrity sha512-WRoDn//mXBiJ1H40rqa3vH0toePwSsGb45iInWlTySa+Uu4k3tYUSxa2v1KqAiLtvlrSzaExqS1gtk96A9zvEA== dependencies: for-in "^1.0.2" is-extendable "^1.0.1" From e25aa2ce0c7b0548cde142fbdc810751d91db3e8 Mon Sep 17 00:00:00 2001 From: Andy Coenen Date: Thu, 31 Oct 2019 14:36:31 -0700 Subject: [PATCH 41/46] Update license --- LICENSE | 36 ++---------------------------------- 1 file changed, 2 insertions(+), 34 deletions(-) diff --git a/LICENSE b/LICENSE index d065348..d645695 100644 --- a/LICENSE +++ b/LICENSE @@ -1,3 +1,4 @@ + Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ @@ -186,7 +187,7 @@ same "printed page" as the copyright notice for easier identification within third-party archives. - Copyright 2019 Google LLC. + Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -199,36 +200,3 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. - -LICENSES FOR SOFTWARE PACKAGES ------------------------------------------------------------------------------ -umap -BSD 3-Clause License - -Copyright (c) 2017, Leland McInnes -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are met: - -* Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. - -* Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - -* Neither the name of the copyright holder nor the names of its - contributors may be used to endorse or promote products derived from - this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE -FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, -OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. From 219cc09deec951e37877935592479b5cf01b7175 Mon Sep 17 00:00:00 2001 From: Andy Coenen Date: Wed, 22 Jan 2020 11:22:37 -0800 Subject: [PATCH 42/46] Fix bugs with tree hyperplanes and infinite loop --- src/tree.ts | 2 +- src/umap.ts | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/tree.ts b/src/tree.ts index 834c374..71391cf 100644 --- a/src/tree.ts +++ b/src/tree.ts @@ -242,7 +242,7 @@ function flattenTree(tree: RandomProjectionTreeNode, leafSize: number) { // TODO: Verify that sparse code is not relevant... const hyperplanes = utils .range(nNodes) - .map(() => utils.zeros(tree.hyperplane!.length)); + .map(() => utils.zeros(tree.hyperplane ? tree.hyperplane.length : 0)); const offsets = utils.zeros(nNodes); const children = utils.range(nNodes).map(() => [-1, -1]); diff --git a/src/umap.ts b/src/umap.ts index 726fe1d..14edea5 100644 --- a/src/umap.ts +++ b/src/umap.ts @@ -411,7 +411,8 @@ export class UMAP { throw new Error('No data has been fit.'); } - const nNeighbors = Math.floor(this.nNeighbors * this.transformQueueSize); + let nNeighbors = Math.floor(this.nNeighbors * this.transformQueueSize); + nNeighbors = Math.min(rawData.length, nNeighbors); const init = nnDescent.initializeSearch( this.rpForest, rawData, @@ -821,7 +822,7 @@ export class UMAP { tail.push(entry.row); head.push(entry.col); } - } + } const epochsPerSample = this.makeEpochsPerSample(weights, nEpochs); return { head, tail, epochsPerSample }; From c2f64d7850ce4b01eec30e47b64e67fe7c0e8863 Mon Sep 17 00:00:00 2001 From: Andy Coenen Date: Wed, 22 Jan 2020 11:33:59 -0800 Subject: [PATCH 43/46] Update version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index cc8c642..9e202bc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "umap-js", - "version": "1.3.1", + "version": "1.3.2", "description": "JavaScript implementation of UMAP", "author": { "name": "Andy Coenen", From 13d97be019f61f181e7cb80fa1eb26f8906969b0 Mon Sep 17 00:00:00 2001 From: Andy Coenen Date: Wed, 22 Jan 2020 11:34:07 -0800 Subject: [PATCH 44/46] New build --- lib/umap-js.js | 106 +++++++++++++++++++++++++++------------------ lib/umap-js.min.js | 2 +- 2 files changed, 65 insertions(+), 43 deletions(-) diff --git a/lib/umap-js.js b/lib/umap-js.js index ba3be8d..9550251 100644 --- a/lib/umap-js.js +++ b/lib/umap-js.js @@ -482,10 +482,6 @@ var __read = (this && this.__read) || function (o, n) { } return ar; }; -var __spread = (this && this.__spread) || function () { - for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); - return ar; -}; var __values = (this && this.__values) || function (o) { var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0; if (m) return m.call(o); @@ -511,15 +507,18 @@ var SparseMatrix = (function () { this.entries = new Map(); this.nRows = 0; this.nCols = 0; - this.rows = __spread(rows); - this.cols = __spread(cols); - this.values = __spread(values); - for (var i = 0; i < values.length; i++) { - var key = this.makeKey(this.rows[i], this.cols[i]); - this.entries.set(key, i); + if (rows.length !== cols.length || rows.length !== values.length) { + throw new Error('rows, cols and values arrays must all have the same length'); } this.nRows = dims[0]; this.nCols = dims[1]; + for (var i = 0; i < values.length; i++) { + var row = rows[i]; + var col = cols[i]; + this.checkDims(row, col); + var key = this.makeKey(row, col); + this.entries.set(key, { value: values[i], row: row, col: col }); + } } SparseMatrix.prototype.makeKey = function (row, col) { return row + ":" + col; @@ -527,21 +526,17 @@ var SparseMatrix = (function () { SparseMatrix.prototype.checkDims = function (row, col) { var withinBounds = row < this.nRows && col < this.nCols; if (!withinBounds) { - throw new Error('array index out of bounds'); + throw new Error('row and/or col specified outside of matrix dimensions'); } }; SparseMatrix.prototype.set = function (row, col, value) { this.checkDims(row, col); var key = this.makeKey(row, col); if (!this.entries.has(key)) { - this.rows.push(row); - this.cols.push(col); - this.values.push(value); - this.entries.set(key, this.values.length - 1); + this.entries.set(key, { value: value, row: row, col: col }); } else { - var index = this.entries.get(key); - this.values[index] = value; + this.entries.get(key).value = value; } }; SparseMatrix.prototype.get = function (row, col, defaultValue) { @@ -549,37 +544,61 @@ var SparseMatrix = (function () { this.checkDims(row, col); var key = this.makeKey(row, col); if (this.entries.has(key)) { - var index = this.entries.get(key); - return this.values[index]; + return this.entries.get(key).value; } else { return defaultValue; } }; + SparseMatrix.prototype.getAll = function (ordered) { + if (ordered === void 0) { ordered = true; } + var rowColValues = []; + this.entries.forEach(function (value) { + rowColValues.push(value); + }); + if (ordered) { + rowColValues.sort(function (a, b) { + if (a.row === b.row) { + return a.col - b.col; + } + else { + return a.row - b.row; + } + }); + } + return rowColValues; + }; SparseMatrix.prototype.getDims = function () { return [this.nRows, this.nCols]; }; SparseMatrix.prototype.getRows = function () { - return __spread(this.rows); + return Array.from(this.entries, function (_a) { + var _b = __read(_a, 2), key = _b[0], value = _b[1]; + return value.row; + }); }; SparseMatrix.prototype.getCols = function () { - return __spread(this.cols); + return Array.from(this.entries, function (_a) { + var _b = __read(_a, 2), key = _b[0], value = _b[1]; + return value.col; + }); }; SparseMatrix.prototype.getValues = function () { - return __spread(this.values); + return Array.from(this.entries, function (_a) { + var _b = __read(_a, 2), key = _b[0], value = _b[1]; + return value.value; + }); }; SparseMatrix.prototype.forEach = function (fn) { - for (var i = 0; i < this.values.length; i++) { - fn(this.values[i], this.rows[i], this.cols[i]); - } + this.entries.forEach(function (value) { return fn(value.value, value.row, value.col); }); }; SparseMatrix.prototype.map = function (fn) { var vals = []; - for (var i = 0; i < this.values.length; i++) { - vals.push(fn(this.values[i], this.rows[i], this.cols[i])); - } + this.entries.forEach(function (value) { + vals.push(fn(value.value, value.row, value.col)); + }); var dims = [this.nRows, this.nCols]; - return new SparseMatrix(this.rows, this.cols, vals, dims); + return new SparseMatrix(this.getRows(), this.getCols(), vals, dims); }; SparseMatrix.prototype.toArray = function () { var _this = this; @@ -587,9 +606,9 @@ var SparseMatrix = (function () { var output = rows.map(function () { return utils.zeros(_this.nCols); }); - for (var i = 0; i < this.values.length; i++) { - output[this.rows[i]][this.cols[i]] = this.values[i]; - } + this.entries.forEach(function (value) { + output[value.row][value.col] = value.value; + }); return output; }; return SparseMatrix; @@ -759,7 +778,7 @@ function getCSR(x) { return a.col - b.col; } else { - return a.row - b.col; + return a.row - b.row; } }); var indices = []; @@ -932,7 +951,7 @@ function flattenTree(tree, leafSize) { var nLeaves = numLeaves(tree); var hyperplanes = utils .range(nNodes) - .map(function () { return utils.zeros(tree.hyperplane.length); }); + .map(function () { return utils.zeros(tree.hyperplane ? tree.hyperplane.length : 0); }); var offsets = utils.zeros(nNodes); var children = utils.range(nNodes).map(function () { return [-1, -1]; }); var indices = utils @@ -1200,6 +1219,9 @@ var UMAP = (function () { this.knnDistances = knnDistances; }; UMAP.prototype.initializeFit = function (X) { + if (X.length <= this.nNeighbors) { + throw new Error("Not enough data points (" + X.length + ") to create nNeighbors: " + this.nNeighbors + ". Add more data points or adjust the configuration."); + } if (this.X === X && this.isInitialized) { return this.getNEpochs(); } @@ -1254,6 +1276,7 @@ var UMAP = (function () { throw new Error('No data has been fit.'); } var nNeighbors = Math.floor(this.nNeighbors * this.transformQueueSize); + nNeighbors = Math.min(rawData.length, nNeighbors); var init = nnDescent.initializeSearch(this.rpForest, rawData, toTransform, nNeighbors, this.initFromRandom, this.initFromTree, this.random); var result = this.search(rawData, this.searchGraph, init, toTransform); var _a = heap.deheapSort(result), indices = _a.indices, distances = _a.weights; @@ -1484,14 +1507,13 @@ var UMAP = (function () { var weights = []; var head = []; var tail = []; - for (var i = 0; i < graph.nRows; i++) { - for (var j = 0; j < graph.nCols; j++) { - var value = graph.get(i, j); - if (value) { - weights.push(value); - tail.push(i); - head.push(j); - } + var rowColValues = graph.getAll(); + for (var i = 0; i < rowColValues.length; i++) { + var entry = rowColValues[i]; + if (entry.value) { + weights.push(entry.value); + tail.push(entry.row); + head.push(entry.col); } } var epochsPerSample = this.makeEpochsPerSample(weights, nEpochs); diff --git a/lib/umap-js.min.js b/lib/umap-js.min.js index 8e97369..2a135f5 100644 --- a/lib/umap-js.min.js +++ b/lib/umap-js.min.js @@ -1 +1 @@ -!function(t,r){if("object"==typeof exports&&"object"==typeof module)module.exports=r();else if("function"==typeof define&&define.amd)define([],r);else{var e=r();for(var n in e)("object"==typeof exports?exports:t)[n]=e[n]}}(window,function(){return function(t){var r={};function e(n){if(r[n])return r[n].exports;var i=r[n]={i:n,l:!1,exports:{}};return t[n].call(i.exports,i,i.exports,e),i.l=!0,i.exports}return e.m=t,e.c=r,e.d=function(t,r,n){e.o(t,r)||Object.defineProperty(t,r,{enumerable:!0,get:n})},e.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},e.t=function(t,r){if(1&r&&(t=e(t)),8&r)return t;if(4&r&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(e.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&r&&"string"!=typeof t)for(var i in t)e.d(n,i,function(r){return t[r]}.bind(null,i));return n},e.n=function(t){var r=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(r,"a",r),r},e.o=function(t,r){return Object.prototype.hasOwnProperty.call(t,r)},e.p="",e(e.s=5)}([function(t,r,e){"use strict";const n=Object.prototype.toString;t.exports=function(t){return n.call(t).endsWith("Array]")}},function(t,r,e){"use strict";var n=this&&this.__values||function(t){var r="function"==typeof Symbol&&t[Symbol.iterator],e=0;return r?r.call(t):{next:function(){return t&&e>=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}};function i(t,r){return Math.floor(r()*t)}function o(t){for(var r=[],e=0;er?t[e]:r;return r},r.max2d=function(t){for(var r=0,e=0;er?t[e][n]:r;return r},r.rejectionSample=function(t,r,e){for(var n=a(t),o=0;o=s[0])return 0;for(var h=0;h=s[0])return 0;s[0]=e,o[0]=n,a[0]=i;for(var h=0,u=0;;){var l=2*h+1,c=l+1,f=t[0][0].length;if(l>=f)break;if(c>=f){if(!(s[l]>e))break;u=l}else if(s[l]>=s[c]){if(!(ee.length;a++)1===i[a]&&n[a]=0?(i[s]=0,Math.floor(e[s])):-1}},function(t,r,e){"use strict";var n,i=this&&this.__read||function(t,r){var e="function"==typeof Symbol&&t[Symbol.iterator];if(!e)return t;var n,i,o=e.call(t),s=[];try{for(;(void 0===r||r-- >0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},o=this&&this.__spread||function(){for(var t=[],r=0;r=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}},a=this&&this.__importStar||function(t){if(t&&t.__esModule)return t;var r={};if(null!=t)for(var e in t)Object.hasOwnProperty.call(t,e)&&(r[e]=t[e]);return r.default=t,r};Object.defineProperty(r,"__esModule",{value:!0});var h=a(e(1)),u=function(){function t(t,r,e,n){this.entries=new Map,this.nRows=0,this.nCols=0,this.rows=o(t),this.cols=o(r),this.values=o(e);for(var i=0;ir?t:r})},r.multiplyScalar=function(t,r){return t.map(function(t){return t*r})},r.eliminateZeros=function(t){for(var r=new Set,e=t.getValues(),n=t.getRows(),i=t.getCols(),o=0;or?t[e]:r;return t.map(function(t){return t/r})},n.l1=function(t){for(var r=0,e=0;e0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},i=this&&this.__spread||function(){for(var t=[],r=0;r=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}},s=this&&this.__importStar||function(t){if(t&&t.__esModule)return t;var r={};if(null!=t)for(var e in t)Object.hasOwnProperty.call(t,e)&&(r[e]=t[e]);return r.default=t,r};Object.defineProperty(r,"__esModule",{value:!0});var a=s(e(1)),h=function(){return function(t,r,e,n){this.hyperplanes=t,this.offsets=r,this.children=e,this.indices=n}}();function u(t,r,e,n){for(var i=r,o=0;o0?0:1}r.FlatTree=h,r.makeForest=function(t,r,e,n){var o=Math.max(10,r);return a.range(e).map(function(r,e){return function(t,r,e,n){void 0===r&&(r=30);var i=a.range(t.length);return function t(r,e,n,i,o){if(void 0===n&&(n=30),e.length>n){var s=function(t,r,e){var n=t[0].length,i=a.tauRandInt(r.length,e),o=a.tauRandInt(r.length,e);o=(o+=i===o?1:0)%r.length;for(var s=r[i],h=r[o],u=0,l=a.zeros(n),c=0;c0?(g[c]=0,f+=1):(g[c]=1,m+=1)}var d=a.zeros(f),w=a.zeros(m);for(var c in f=0,m=0,a.range(g.length))0===g[c]?(d[f]=r[c],f+=1):(w[m]=r[c],m+=1);return{indicesLeft:d,indicesRight:w,hyperplane:l,offset:u}}(r,e,o),h=s.indicesLeft,u=s.indicesRight,l=s.hyperplane,c=s.offset,f=t(r,h,n,i+1,o),m=t(r,u,n,i+1,o),g={leftChild:f,rightChild:m,isLeaf:!1,hyperplane:l,offset:c};return g}var g={indices:e,isLeaf:!0};return g}(t,i,r,e,n)}(t,o,e,n)}).map(function(t){return function(t,r){var e=function t(r){return r.isLeaf?1:1+t(r.leftChild)+t(r.rightChild)}(t),n=function t(r){return r.isLeaf?1:t(r.leftChild)+t(r.rightChild)}(t),o=a.range(e).map(function(){return a.zeros(t.hyperplane.length)}),s=a.zeros(e),u=a.range(e).map(function(){return[-1,-1]}),l=a.range(n).map(function(){return a.range(r).map(function(){return-1})});return function t(r,e,n,o,s,a,h){var u;if(r.isLeaf)return o[a][0]=-h,(u=s[h]).splice.apply(u,i([0,r.indices.length],r.indices)),{nodeNum:a,leafNum:h+=1};e[a]=r.hyperplane,n[a]=r.offset,o[a][0]=a+1;var l=a,c=t(r.leftChild,e,n,o,s,a+1,h);return a=c.nodeNum,h=c.leafNum,o[l][1]=a+1,{nodeNum:(c=t(r.rightChild,e,n,o,s,a+1,h)).nodeNum,leafNum:c.leafNum}}(t,o,s,u,l,0,0),new h(o,s,u,l)}(t,o)})},r.makeLeafArray=function(t){var r,e;if(t.length>0){var n=[];try{for(var s=o(t),a=s.next();!a.done;a=s.next()){var h=a.value;n.push.apply(n,i(h.indices))}}catch(t){r={error:t}}finally{try{a&&!a.done&&(e=s.return)&&e.call(s)}finally{if(r)throw r.error}}return n}return[[-1]]},r.searchFlatTree=function(t,r,e){for(var n=0;r.children[n][0]>0;)n=0===u(r.hyperplanes[n],r.offsets[n],t,e)?r.children[n][0]:r.children[n][1];var i=-1*r.children[n][0];return r.indices[i]}},function(t,r,e){"use strict";Object.defineProperty(r,"__esModule",{value:!0});var n=e(6);r.UMAP=n.UMAP},function(t,r,e){"use strict";var n=this&&this.__awaiter||function(t,r,e,n){return new(e||(e=Promise))(function(i,o){function s(t){try{h(n.next(t))}catch(t){o(t)}}function a(t){try{h(n.throw(t))}catch(t){o(t)}}function h(t){t.done?i(t.value):new e(function(r){r(t.value)}).then(s,a)}h((n=n.apply(t,r||[])).next())})},i=this&&this.__generator||function(t,r){var e,n,i,o,s={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function a(o){return function(a){return function(o){if(e)throw new TypeError("Generator is already executing.");for(;s;)try{if(e=1,n&&(i=2&o[0]?n.return:o[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,o[1])).done)return i;switch(n=0,i&&(o=[2&o[0],i.value]),o[0]){case 0:case 1:i=o;break;case 4:return s.label++,{value:o[1],done:!1};case 5:s.label++,n=o[1],o=[0];continue;case 7:o=s.ops.pop(),s.trys.pop();continue;default:if(!(i=(i=s.trys).length>0&&i[i.length-1])&&(6===o[0]||2===o[0])){s=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},s=this&&this.__spread||function(){for(var t=[],r=0;r0&&i.set(o,u,c)}var f=l.transpose(i);return l.maximum(i,f)},t.prototype.transform=function(t){var r=this,e=this.X;if(void 0===e||0===e.length)throw new Error("No data has been fit.");var n=Math.floor(this.nNeighbors*this.transformQueueSize),i=c.initializeSearch(this.rpForest,e,t,n,this.initFromRandom,this.initFromTree,this.random),o=this.search(e,this.searchGraph,i,t),s=u.deheapSort(o),a=s.indices,h=s.weights;a=a.map(function(t){return t.slice(0,r.nNeighbors)}),h=h.map(function(t){return t.slice(0,r.nNeighbors)});var f=Math.max(0,this.localConnectivity-1),g=this.smoothKNNDistance(h,this.nNeighbors,f),p=g.sigmas,v=g.rhos,d=this.computeMembershipStrengths(a,h,p,v),w=d.rows,y=d.cols,b=d.vals,M=[t.length,e.length],x=new l.SparseMatrix(w,y,b,M),E=l.normalize(x,"l1"),R=l.getCSR(E),k=t.length,z=S(m.reshape2d(R.indices,k,this.nNeighbors),m.reshape2d(R.values,k,this.nNeighbors),this.embedding),N=this.nEpochs?this.nEpochs/3:x.nRows<=1e4?100:30,A=x.getValues().reduce(function(t,r){return r>t?r:t},0);x=x.map(function(t){return t0});if(g.length>=e){var p=Math.floor(e),v=e-p;p>0?(s[h]=g[p-1],v>1e-5&&(s[h]+=v*(g[p]-g[p-1]))):s[h]=v*g[0]}else g.length>0&&(s[h]=m.max(g));for(var d=0;d0?Math.exp(-b/c):1}if(Math.abs(w-o)<1e-5)break;w>o?c=(u+(l=c))/2:(u=c,l===1/0?c*=2:c=(u+l)/2)}if(a[h]=c,s[h]>0){var M=m.mean(f);a[h]<.001*M&&(a[h]=.001*M)}else{var x=m.mean(t.map(m.mean));a[h]<.001*x&&(a[h]=.001*x)}}return{sigmas:a,rhos:s}},t.prototype.computeMembershipStrengths=function(t,r,e,n){for(var i=t.length,o=t[0].length,s=m.zeros(i*o),a=m.zeros(i*o),h=m.zeros(i*o),u=0;u0&&(e[n]=r/i[n])}),e},t.prototype.assignOptimizationStateParameters=function(t){Object.assign(this.optimizationState,t)},t.prototype.prepareForOptimizationLoop=function(){var t=this.repulsionStrength,r=this.learningRate,e=this.negativeSampleRate,n=this.optimizationState,i=n.epochsPerSample,o=n.headEmbedding,a=n.tailEmbedding,h=o[0].length,u=o.length===a.length,l=i.map(function(t){return t/e}),c=s(l),f=s(i);this.assignOptimizationStateParameters({epochOfNextSample:f,epochOfNextNegativeSample:c,epochsPerNegativeSample:l,moveOther:u,initialAlpha:r,alpha:r,gamma:t,dim:h})},t.prototype.initializeOptimization=function(){var t=this.embedding,r=this.embedding,e=this.optimizationState,n=e.head,i=e.tail,o=e.epochsPerSample,s=this.getNEpochs(),a=this.graph.nCols,h=b(this.spread,this.minDist),u=h.a,l=h.b;this.assignOptimizationStateParameters({headEmbedding:t,tailEmbedding:r,head:n,tail:i,epochsPerSample:o,a:u,b:l,nEpochs:s,nVertices:a})},t.prototype.optimizeLayoutStep=function(t){for(var r=this.optimizationState,e=r.head,n=r.tail,i=r.headEmbedding,o=r.tailEmbedding,s=r.epochsPerSample,a=r.epochOfNextSample,h=r.epochOfNextNegativeSample,u=r.epochsPerNegativeSample,l=r.moveOther,c=r.initialAlpha,f=r.alpha,g=r.gamma,p=r.a,v=r.b,d=r.dim,b=r.nEpochs,M=r.nVertices,x=0;xt)){var S=e[x],E=n[x],R=i[S],k=o[E],z=y(R,k),N=0;z>0&&(N=-2*p*v*Math.pow(z,v-1),N/=p*Math.pow(z,v)+1);for(var A=0;A0)O=2*g*v,O/=(.001+I)*(p*Math.pow(I,v)+1);else if(S===P)continue;for(A=0;A0&&(V=w(O*(R[A]-j[A]),4)),R[A]+=V*f}}h[x]+=C*u[x]}return r.alpha=c*(1-t/b),r.currentEpoch+=1,i},t.prototype.optimizeLayoutAsync=function(t){var r=this;return void 0===t&&(t=function(){return!0}),new Promise(function(e,o){var s=function(){return n(r,void 0,void 0,function(){var r,n,a,h,u,l;return i(this,function(i){try{if(r=this.optimizationState,n=r.nEpochs,a=r.currentEpoch,this.embedding=this.optimizeLayoutStep(a),h=this.optimizationState.currentEpoch,u=!1===t(h),l=h===n,u||l)return[2,e(l)];setTimeout(function(){return s()},0)}catch(t){o(t)}return[2]})})};setTimeout(function(){return s()},0)})},t.prototype.optimizeLayout=function(t){void 0===t&&(t=function(){return!0});for(var r=!1,e=[];!r;){var n=this.optimizationState,i=n.nEpochs,o=n.currentEpoch;e=this.optimizeLayoutStep(o);var s=this.optimizationState.currentEpoch,a=!1===t(s);r=s===i||a}return e},t.prototype.getNEpochs=function(){var t=this.graph;if(this.nEpochs>0)return this.nEpochs;var r=t.nRows;return r<=2500?500:r<=5e3?400:r<=7500?300:200},t}();function v(t,r){for(var e=0,n=0;nr?r:t<-r?-r:t}function y(t,r){for(var e=0,n=0;n=r?Math.exp(-(e[i]-r)/t):n}),i={x:e,y:n},s={damping:1.5,initialValues:[.5,.5],gradientDifference:.1,maxIterations:100,errorTolerance:.01},a=g.default(i,function(t){var r=o(t,2),e=r[0],n=r[1];return function(t){return 1/(1+e*Math.pow(t,2*n))}},s).parameterValues,h=o(a,2);return{a:h[0],b:h[1]}}function M(t,r,e,n){return void 0===e&&(e=1),void 0===n&&(n=5),t.map(function(t,i,o){return-1===r[i]||-1===r[o]?t*Math.exp(-e):r[i]!==r[o]?t*Math.exp(-n):t})}function x(t){t=l.normalize(t,"max");var r=l.transpose(t),e=l.pairwiseMultiply(r,t);return t=l.add(t,l.subtract(r,e)),l.eliminateZeros(t)}function S(t,r,e){for(var n=m.zeros(t.length).map(function(t){return m.zeros(e[0].length)}),i=0;i=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}},i=this&&this.__importStar||function(t){if(t&&t.__esModule)return t;var r={};if(null!=t)for(var e in t)Object.hasOwnProperty.call(t,e)&&(r[e]=t[e]);return r.default=t,r};Object.defineProperty(r,"__esModule",{value:!0});var o=i(e(2)),s=i(e(3)),a=i(e(4)),h=i(e(1));r.makeNNDescent=function(t,r){return function(e,n,i,s,a,u,l,c){void 0===s&&(s=10),void 0===a&&(a=50),void 0===u&&(u=.001),void 0===l&&(l=.5),void 0===c&&(c=!0);for(var f=e.length,m=o.makeHeap(e.length,i),g=0;gr&&(r=t[e]);return r};var s=function(t){if(!i()(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");for(var r=t[0],e=1;e1&&void 0!==arguments[1]?arguments[1]:{};if(!i()(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");if(void 0!==e.output){if(!i()(e.output))throw new TypeError("output option must be an array if specified");r=e.output}else r=new Array(t.length);var n=s(t),a=o(t);if(n===a)throw new RangeError("minimum and maximum input values are equal. Cannot rescale a constant array");var h=e.min,u=void 0===h?e.autoMinMax?n:0:h,l=e.max,c=void 0===l?e.autoMinMax?a:1:l;if(u>=c)throw new RangeError("min option must be smaller than max option");for(var f=(c-u)/(a-n),m=0;mMath.abs(h[i])&&(i=r);if(i!==e){for(n=0;n=0;i--){for(n=0;no?t[i][o]:i===o?1:0;return n}get upperTriangularMatrix(){for(var t=this.LU,r=t.rows,e=t.columns,n=new _(r,e),i=0;iMath.abs(r)?(e=r/t,Math.abs(t)*Math.sqrt(1+e*e)):0!==r?(e=t/r,Math.abs(r)*Math.sqrt(1+e*e)):0}function l(t,r,e){for(var n=new Array(t),i=0;i=0;t--)if(0!==v[t]){for(let r=t+1;r=0;t--){if(t0;){let t,r;for(t=R-2;t>=-1&&-1!==t;t--){const r=Number.MIN_VALUE+z*Math.abs(v[t]+Math.abs(v[t+1]));if(Math.abs(y[t])<=r||Number.isNaN(y[t])){y[t]=0;break}}if(t===R-2)r=4;else{let e;for(e=R-1;e>=t&&e!==t;e--){let r=(e!==R?Math.abs(y[e]):0)+(e!==t+1?Math.abs(y[e-1]):0);if(Math.abs(v[e])<=z*r){v[e]=0;break}}e===t?r=3:e===R-1?r=1:(r=2,t=e)}switch(t++,r){case 1:{let r=y[R-2];y[R-2]=0;for(let e=R-2;e>=t;e--){let i=u(v[e],r),o=v[e]/i,s=r/i;if(v[e]=i,e!==t&&(r=-s*y[e-1],y[e-1]=o*y[e-1]),c)for(let t=0;t=v[t+1]);){let r=v[t];if(v[t]=v[t+1],v[t+1]=r,c&&tr?i[o][e]=t[o][e]/this.s[e]:i[o][e]=0;var o=this.U,s=o.length,a=o[0].length,h=new _(e,s);for(let t=0;tt&&r++;return r}get diagonal(){return this.s}get threshold(){return Number.EPSILON/2*Math.max(this.m,this.n)*this.s[0]}get leftSingularVectors(){return _.isMatrix(this.U)||(this.U=new _(this.U)),this.U}get rightSingularVectors(){return _.isMatrix(this.V)||(this.V=new _(this.V)),this.V}get diagonalMatrix(){return _.diag(this.s)}}function f(t,r,e){var n=e?t.rows:t.rows-1;if(r<0||r>n)throw new RangeError("Row index out of range")}function m(t,r,e){var n=e?t.columns:t.columns-1;if(r<0||r>n)throw new RangeError("Column index out of range")}function g(t,r){if(r.to1DArray&&(r=r.to1DArray()),r.length!==t.columns)throw new RangeError("vector size must be the same as the number of columns");return r}function p(t,r){if(r.to1DArray&&(r=r.to1DArray()),r.length!==t.rows)throw new RangeError("vector size must be the same as the number of rows");return r}function v(t,r,e){return{row:d(t,r),column:w(t,e)}}function d(t,r){if("object"!=typeof r)throw new TypeError("unexpected type for row indices");if(r.some(r=>r<0||r>=t.rows))throw new RangeError("row indices are out of range");return Array.isArray(r)||(r=Array.from(r)),r}function w(t,r){if("object"!=typeof r)throw new TypeError("unexpected type for column indices");if(r.some(r=>r<0||r>=t.columns))throw new RangeError("column indices are out of range");return Array.isArray(r)||(r=Array.from(r)),r}function y(t,r,e,n,i){if(5!==arguments.length)throw new RangeError("expected 4 arguments");if(b("startRow",r),b("endRow",e),b("startColumn",n),b("endColumn",i),r>e||n>i||r<0||r>=t.rows||e<0||e>=t.rows||n<0||n>=t.columns||i<0||i>=t.columns)throw new RangeError("Submatrix indices are out of range")}function b(t,r){if("number"!=typeof r)throw new TypeError(`${t} must be a number`)}class M extends(C()){constructor(t,r,e){super(),this.matrix=t,this.rows=r,this.columns=e}static get[Symbol.species](){return _}}class x extends M{constructor(t){super(t,t.columns,t.rows)}set(t,r,e){return this.matrix.set(r,t,e),this}get(t,r){return this.matrix.get(r,t)}}class S extends M{constructor(t,r){super(t,1,t.columns),this.row=r}set(t,r,e){return this.matrix.set(this.row,r,e),this}get(t,r){return this.matrix.get(this.row,r)}}class E extends M{constructor(t,r,e,n,i){y(t,r,e,n,i),super(t,e-r+1,i-n+1),this.startRow=r,this.startColumn=n}set(t,r,e){return this.matrix.set(this.startRow+t,this.startColumn+r,e),this}get(t,r){return this.matrix.get(this.startRow+t,this.startColumn+r)}}class R extends M{constructor(t,r,e){var n=v(t,r,e);super(t,n.row.length,n.column.length),this.rowIndices=n.row,this.columnIndices=n.column}set(t,r,e){return this.matrix.set(this.rowIndices[t],this.columnIndices[r],e),this}get(t,r){return this.matrix.get(this.rowIndices[t],this.columnIndices[r])}}class k extends M{constructor(t,r){super(t,(r=d(t,r)).length,t.columns),this.rowIndices=r}set(t,r,e){return this.matrix.set(this.rowIndices[t],r,e),this}get(t,r){return this.matrix.get(this.rowIndices[t],r)}}class z extends M{constructor(t,r){r=w(t,r),super(t,t.rows,r.length),this.columnIndices=r}set(t,r,e){return this.matrix.set(t,this.columnIndices[r],e),this}get(t,r){return this.matrix.get(t,this.columnIndices[r])}}class N extends M{constructor(t,r){super(t,t.rows,1),this.column=r}set(t,r,e){return this.matrix.set(t,this.column,e),this}get(t){return this.matrix.get(t,this.column)}}class A extends M{constructor(t){super(t,t.rows,t.columns)}set(t,r,e){return this.matrix.set(this.rows-t-1,r,e),this}get(t,r){return this.matrix.get(this.rows-t-1,r)}}class V extends M{constructor(t){super(t,t.rows,t.columns)}set(t,r,e){return this.matrix.set(t,this.columns-r-1,e),this}get(t,r){return this.matrix.get(t,this.columns-r-1)}}function C(t){void 0===t&&(t=Object);class r extends t{static get[Symbol.species](){return this}static from1DArray(t,r,e){if(t*r!==e.length)throw new RangeError("Data length does not match given dimensions");for(var n=new this(t,r),i=0;it&&(t=this.get(r,e));return t}maxIndex(){for(var t=this.get(0,0),r=[0,0],e=0;et&&(t=this.get(e,n),r[0]=e,r[1]=n);return r}min(){for(var t=this.get(0,0),r=0;rr&&(r=this.get(t,e));return r}maxRowIndex(t){f(this,t);for(var r=this.get(t,0),e=[t,0],n=1;nr&&(r=this.get(t,n),e[1]=n);return e}minRow(t){f(this,t);for(var r=this.get(t,0),e=1;er&&(r=this.get(e,t));return r}maxColumnIndex(t){m(this,t);for(var r=this.get(0,t),e=[0,t],n=1;nr&&(r=this.get(n,t),e[0]=n);return e}minColumn(t){m(this,t);for(var r=this.get(0,t),e=1;e=(r=void 0===r?1:r))throw new RangeError("min should be strictly smaller than max");for(var e=this.constructor.empty(this.rows,this.columns),n=0;n=(r=void 0===r?1:r))throw new RangeError("min should be strictly smaller than max");for(var e=this.constructor.empty(this.rows,this.columns),n=0;ne||r<0||r>=this.columns||e<0||e>=this.columns)throw new RangeError("Argument out of range");for(var n=new this.constructor[Symbol.species](t.length,e-r+1),i=0;i=this.rows)throw new RangeError(`Row index out of range: ${t[i]}`);n.set(i,o-r,this.get(t[i],o))}return n}subMatrixColumn(t,r,e){if(void 0===r&&(r=0),void 0===e&&(e=this.rows-1),r>e||r<0||r>=this.rows||e<0||e>=this.rows)throw new RangeError("Argument out of range");for(var n=new this.constructor[Symbol.species](e-r+1,t.length),i=0;i=this.columns)throw new RangeError(`Column index out of range: ${t[i]}`);n.set(o-r,i,this.get(o,t[i]))}return n}setSubMatrix(t,r,e){y(this,r,r+(t=this.constructor.checkMatrix(t)).rows-1,e,e+t.columns-1);for(var n=0;nt?i[o]=1/i[o]:i[o]=0;return i=this.constructor[Symbol.species].diag(i),n.mmul(i.mmul(e.transposeView()))}clone(){for(var t=new this.constructor[Symbol.species](this.rows,this.columns),r=0;r>","signPropagatingRightShift"],[">>>","rightShift","zeroFillRightShift"]]){var u=o($("\n(function %name%(value) {\n if (typeof value === 'number') return this.%name%S(value);\n return this.%name%M(value);\n})\n",{name:s[1],op:s[0]})),l=o($("\n(function %name%S(value) {\n for (var i = 0; i < this.rows; i++) {\n for (var j = 0; j < this.columns; j++) {\n this.set(i, j, this.get(i, j) %op% value);\n }\n }\n return this;\n})\n",{name:`${s[1]}S`,op:s[0]})),d=o($("\n(function %name%M(matrix) {\n matrix = this.constructor.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 (var i = 0; i < this.rows; i++) {\n for (var j = 0; j < this.columns; j++) {\n this.set(i, j, this.get(i, j) %op% matrix.get(i, j));\n }\n }\n return this;\n})\n",{name:`${s[1]}M`,op:s[0]})),w=o($("\n(function %name%(matrix, value) {\n var newMatrix = new this[Symbol.species](matrix);\n return newMatrix.%name%(value);\n})\n",{name:s[1]}));for(n=1;n0){if(super(t),!(Number.isInteger(r)&&r>0))throw new TypeError("nColumns must be a positive integer");for(e=0;e=0;o--){for(i=0;i=0;e--){for(t=0;ti)return new Array(r.rows+1).fill(0);for(var o=r.addRow(e,[0]),s=0;s0;a--){for(c=0,s=0,u=0;u0&&(o=-o),r[a]=c*o,s-=i*o,e[a-1]=i-o,h=0;hl){0;do{for(1,i=e[l],f=(e[l+1]-i)/(2*r[l]),m=u(f,1),f<0&&(m=-m),e[l]=r[l]/(f+m),e[l+1]=r[l]*(f+m),g=e[l+1],o=i-e[l],s=l+2;s=l;s--)for(d=v,v=p,b=y,i=p*r[s],o=p*f,m=u(f,r[s]),r[s+1]=y*m,y=r[s]/m,f=(p=f/m)*e[s]-y*i,e[s+1]=o+y*(p*i+y*e[s]),h=0;hS*x)}e[l]=e[l]+M,r[l]=0}for(s=0;s=u;a--)e[a]=r[a][u-1]/l,s+=e[a]*e[a];for(o=Math.sqrt(s),e[u]>0&&(o=-o),s-=e[u]*o,e[u]=e[u]-o,h=u;h=u;a--)i+=e[a]*r[a][h];for(i/=s,a=u;a<=c;a++)r[a][h]-=i*e[a]}for(a=0;a<=c;a++){for(i=0,h=c;h>=u;h--)i+=e[h]*r[a][h];for(i/=s,h=u;h<=c;h++)r[a][h]-=i*e[h]}e[u]=l*e[u],r[u][u-1]=l*o}}for(a=0;a=1;u--)if(0!==r[u][u-1]){for(a=u+1;a<=c;a++)e[a]=r[a][u-1];for(h=u;h<=c;h++){for(o=0,a=u;a<=c;a++)o+=e[a]*n[a][h];for(o=o/e[u]/r[u][u-1],a=u;a<=c;a++)n[a][h]+=o*e[a]}}}(o,f,m,s),function(t,r,e,n,i){var o,s,a,h,u,l,c,f,m,g,p,v,d,w,y,b=t-1,M=t-1,x=Number.EPSILON,S=0,E=0,R=0,k=0,z=0,N=0,A=0,V=0;for(o=0;oM)&&(e[o]=i[o][o],r[o]=0),s=Math.max(o-1,0);s=0;){for(h=b;h>0&&(0===(N=Math.abs(i[h-1][h-1])+Math.abs(i[h][h]))&&(N=E),!(Math.abs(i[h][h-1])=0){for(A=R>=0?R+A:R-A,e[b-1]=f+A,e[b]=e[b-1],0!==A&&(e[b]=f-c/A),r[b-1]=0,r[b]=0,f=i[b][b-1],N=Math.abs(f)+Math.abs(A),R=f/N,k=A/N,z=Math.sqrt(R*R+k*k),R/=z,k/=z,s=b-1;s0){for(N=Math.sqrt(N),m=h&&(A=i[u][u],R=((z=f-A)*(N=m-A)-c)/i[u+1][u]+i[u][u+1],k=i[u+1][u+1]-A-z-N,z=i[u+2][u+1],N=Math.abs(R)+Math.abs(k)+Math.abs(z),R/=N,k/=N,z/=N,u!==h)&&!(Math.abs(i[u][u-1])*(Math.abs(k)+Math.abs(z))u+2&&(i[o][o-3]=0);for(a=u;a<=b-1&&(w=a!==b-1,a!==u&&(R=i[a][a-1],k=i[a+1][a-1],z=w?i[a+2][a-1]:0,0!==(f=Math.abs(R)+Math.abs(k)+Math.abs(z))&&(R/=f,k/=f,z/=f)),0!==f);a++)if(N=Math.sqrt(R*R+k*k+z*z),R<0&&(N=-N),0!==N){for(a!==u?i[a][a-1]=-N*f:h!==u&&(i[a][a-1]=-i[a][a-1]),f=(R+=N)/N,m=k/N,A=z/N,k/=R,z/=R,s=a;s=0;b--)if(R=e[b],0===(k=r[b]))for(h=b,i[b][b]=1,o=b-1;o>=0;o--){for(c=i[o][o]-R,z=0,s=h;s<=b;s++)z+=i[o][s]*i[s][b];if(r[o]<0)A=c,N=z;else if(h=o,0===r[o]?i[o][b]=0!==c?-z/c:-z/(x*E):(f=i[o][o+1],m=i[o+1][o],k=(e[o]-R)*(e[o]-R)+r[o]*r[o],l=(f*N-A*z)/k,i[o][b]=l,i[o+1][b]=Math.abs(f)>Math.abs(A)?(-z-c*l)/f:(-N-m*l)/A),l=Math.abs(i[o][b]),x*l*l>1)for(s=o;s<=b;s++)i[s][b]=i[s][b]/l}else if(k<0)for(h=b-1,Math.abs(i[b][b-1])>Math.abs(i[b-1][b])?(i[b-1][b-1]=k/i[b][b-1],i[b-1][b]=-(i[b][b]-R)/i[b][b-1]):(y=$(0,-i[b-1][b],i[b-1][b-1]-R,k),i[b-1][b-1]=y[0],i[b-1][b]=y[1]),i[b][b-1]=0,i[b][b]=1,o=b-2;o>=0;o--){for(g=0,p=0,s=h;s<=b;s++)g+=i[o][s]*i[s][b-1],p+=i[o][s]*i[s][b];if(c=i[o][o]-R,r[o]<0)A=c,z=g,N=p;else if(h=o,0===r[o]?(y=$(-g,-p,c,k),i[o][b-1]=y[0],i[o][b]=y[1]):(f=i[o][o+1],m=i[o+1][o],v=(e[o]-R)*(e[o]-R)+r[o]*r[o]-k*k,d=2*(e[o]-R)*k,0===v&&0===d&&(v=x*E*(Math.abs(c)+Math.abs(k)+Math.abs(f)+Math.abs(m)+Math.abs(A))),y=$(f*z-A*g+k*p,f*N-A*p-k*g,v,d),i[o][b-1]=y[0],i[o][b]=y[1],Math.abs(f)>Math.abs(A)+Math.abs(k)?(i[o+1][b-1]=(-g-c*i[o][b-1]+k*i[o][b])/f,i[o+1][b]=(-p-c*i[o][b]-k*i[o][b-1])/f):(y=$(-z-m*i[o][b-1],-N-m*i[o][b],A,k),i[o+1][b-1]=y[0],i[o+1][b]=y[1])),l=Math.max(Math.abs(i[o][b-1]),Math.abs(i[o][b])),x*l*l>1)for(s=o;s<=b;s++)i[s][b-1]=i[s][b-1]/l,i[s][b]=i[s][b]/l}for(o=0;oM)for(s=o;s=0;s--)for(o=0;o<=M;o++){for(A=0,a=0;a<=Math.min(s,M);a++)A+=n[o][a]*i[a][s];n[o][s]=A}}(o,h,a,s,f)}this.n=o,this.e=h,this.d=a,this.V=s}get realEigenvalues(){return this.d}get imaginaryEigenvalues(){return this.e}get eigenvectorMatrix(){return _.isMatrix(this.V)||(this.V=new _(this.V)),this.V}get diagonalMatrix(){var t,r,e=this.n,n=this.e,i=this.d,o=new _(e,e);for(t=0;t0?o[t][t+1]=n[t]:n[t]<0&&(o[t][t-1]=n[t])}return o}}function $(t,r,e,n){var i,o;return Math.abs(e)>Math.abs(n)?[(t+(i=n/e)*r)/(o=e+i*n),(r-i*t)/o]:[((i=e/n)*t+r)/(o=n+i*e),(i*r-t)/o]}class Q{constructor(t){if(!(t=j.checkMatrix(t)).isSymmetric())throw new Error("Matrix is not symmetric");var r,e,n,i=t,o=i.rows,s=new _(o,o),a=!0;for(e=0;e0,s[e][e]=Math.sqrt(Math.max(u,0)),n=e+1;n=0;o--)for(i=0;i=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}};function i(t,r){return Math.floor(r()*t)}function o(t){for(var r=[],e=0;er?t[e]:r;return r},r.max2d=function(t){for(var r=0,e=0;er?t[e][n]:r;return r},r.rejectionSample=function(t,r,e){for(var n=a(t),o=0;o=s[0])return 0;for(var h=0;h=s[0])return 0;s[0]=e,o[0]=n,a[0]=i;for(var h=0,u=0;;){var l=2*h+1,c=l+1,f=t[0][0].length;if(l>=f)break;if(c>=f){if(!(s[l]>e))break;u=l}else if(s[l]>=s[c]){if(!(ee.length;a++)1===i[a]&&n[a]=0?(i[s]=0,Math.floor(e[s])):-1}},function(t,r,e){"use strict";var n,i=this&&this.__read||function(t,r){var e="function"==typeof Symbol&&t[Symbol.iterator];if(!e)return t;var n,i,o=e.call(t),s=[];try{for(;(void 0===r||r-- >0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},o=this&&this.__values||function(t){var r="function"==typeof Symbol&&t[Symbol.iterator],e=0;return r?r.call(t):{next:function(){return t&&e>=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}},s=this&&this.__importStar||function(t){if(t&&t.__esModule)return t;var r={};if(null!=t)for(var e in t)Object.hasOwnProperty.call(t,e)&&(r[e]=t[e]);return r.default=t,r};Object.defineProperty(r,"__esModule",{value:!0});var a=s(e(1)),h=function(){function t(t,r,e,n){if(this.entries=new Map,this.nRows=0,this.nCols=0,t.length!==r.length||t.length!==e.length)throw new Error("rows, cols and values arrays must all have the same length");this.nRows=n[0],this.nCols=n[1];for(var i=0;ir?t:r})},r.multiplyScalar=function(t,r){return t.map(function(t){return t*r})},r.eliminateZeros=function(t){for(var r=new Set,e=t.getValues(),n=t.getRows(),i=t.getCols(),o=0;or?t[e]:r;return t.map(function(t){return t/r})},n.l1=function(t){for(var r=0,e=0;e0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},i=this&&this.__spread||function(){for(var t=[],r=0;r=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}},s=this&&this.__importStar||function(t){if(t&&t.__esModule)return t;var r={};if(null!=t)for(var e in t)Object.hasOwnProperty.call(t,e)&&(r[e]=t[e]);return r.default=t,r};Object.defineProperty(r,"__esModule",{value:!0});var a=s(e(1)),h=function(){return function(t,r,e,n){this.hyperplanes=t,this.offsets=r,this.children=e,this.indices=n}}();function u(t,r,e,n){for(var i=r,o=0;o0?0:1}r.FlatTree=h,r.makeForest=function(t,r,e,n){var o=Math.max(10,r);return a.range(e).map(function(r,e){return function(t,r,e,n){void 0===r&&(r=30);var i=a.range(t.length);return function t(r,e,n,i,o){if(void 0===n&&(n=30),e.length>n){var s=function(t,r,e){var n=t[0].length,i=a.tauRandInt(r.length,e),o=a.tauRandInt(r.length,e);o=(o+=i===o?1:0)%r.length;for(var s=r[i],h=r[o],u=0,l=a.zeros(n),c=0;c0?(g[c]=0,f+=1):(g[c]=1,m+=1)}var d=a.zeros(f),w=a.zeros(m);for(var c in f=0,m=0,a.range(g.length))0===g[c]?(d[f]=r[c],f+=1):(w[m]=r[c],m+=1);return{indicesLeft:d,indicesRight:w,hyperplane:l,offset:u}}(r,e,o),h=s.indicesLeft,u=s.indicesRight,l=s.hyperplane,c=s.offset,f=t(r,h,n,i+1,o),m=t(r,u,n,i+1,o),g={leftChild:f,rightChild:m,isLeaf:!1,hyperplane:l,offset:c};return g}var g={indices:e,isLeaf:!0};return g}(t,i,r,e,n)}(t,o,e,n)}).map(function(t){return function(t,r){var e=function t(r){return r.isLeaf?1:1+t(r.leftChild)+t(r.rightChild)}(t),n=function t(r){return r.isLeaf?1:t(r.leftChild)+t(r.rightChild)}(t),o=a.range(e).map(function(){return a.zeros(t.hyperplane?t.hyperplane.length:0)}),s=a.zeros(e),u=a.range(e).map(function(){return[-1,-1]}),l=a.range(n).map(function(){return a.range(r).map(function(){return-1})});return function t(r,e,n,o,s,a,h){var u;if(r.isLeaf)return o[a][0]=-h,(u=s[h]).splice.apply(u,i([0,r.indices.length],r.indices)),{nodeNum:a,leafNum:h+=1};e[a]=r.hyperplane,n[a]=r.offset,o[a][0]=a+1;var l=a,c=t(r.leftChild,e,n,o,s,a+1,h);return a=c.nodeNum,h=c.leafNum,o[l][1]=a+1,{nodeNum:(c=t(r.rightChild,e,n,o,s,a+1,h)).nodeNum,leafNum:c.leafNum}}(t,o,s,u,l,0,0),new h(o,s,u,l)}(t,o)})},r.makeLeafArray=function(t){var r,e;if(t.length>0){var n=[];try{for(var s=o(t),a=s.next();!a.done;a=s.next()){var h=a.value;n.push.apply(n,i(h.indices))}}catch(t){r={error:t}}finally{try{a&&!a.done&&(e=s.return)&&e.call(s)}finally{if(r)throw r.error}}return n}return[[-1]]},r.searchFlatTree=function(t,r,e){for(var n=0;r.children[n][0]>0;)n=0===u(r.hyperplanes[n],r.offsets[n],t,e)?r.children[n][0]:r.children[n][1];var i=-1*r.children[n][0];return r.indices[i]}},function(t,r,e){"use strict";Object.defineProperty(r,"__esModule",{value:!0});var n=e(6);r.UMAP=n.UMAP},function(t,r,e){"use strict";var n=this&&this.__awaiter||function(t,r,e,n){return new(e||(e=Promise))(function(i,o){function s(t){try{h(n.next(t))}catch(t){o(t)}}function a(t){try{h(n.throw(t))}catch(t){o(t)}}function h(t){t.done?i(t.value):new e(function(r){r(t.value)}).then(s,a)}h((n=n.apply(t,r||[])).next())})},i=this&&this.__generator||function(t,r){var e,n,i,o,s={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function a(o){return function(a){return function(o){if(e)throw new TypeError("Generator is already executing.");for(;s;)try{if(e=1,n&&(i=2&o[0]?n.return:o[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,o[1])).done)return i;switch(n=0,i&&(o=[2&o[0],i.value]),o[0]){case 0:case 1:i=o;break;case 4:return s.label++,{value:o[1],done:!1};case 5:s.label++,n=o[1],o=[0];continue;case 7:o=s.ops.pop(),s.trys.pop();continue;default:if(!(i=(i=s.trys).length>0&&i[i.length-1])&&(6===o[0]||2===o[0])){s=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},s=this&&this.__spread||function(){for(var t=[],r=0;r0&&i.set(o,u,c)}var f=l.transpose(i);return l.maximum(i,f)},t.prototype.transform=function(t){var r=this,e=this.X;if(void 0===e||0===e.length)throw new Error("No data has been fit.");var n=Math.floor(this.nNeighbors*this.transformQueueSize);n=Math.min(e.length,n);var i=c.initializeSearch(this.rpForest,e,t,n,this.initFromRandom,this.initFromTree,this.random),o=this.search(e,this.searchGraph,i,t),s=u.deheapSort(o),a=s.indices,h=s.weights;a=a.map(function(t){return t.slice(0,r.nNeighbors)}),h=h.map(function(t){return t.slice(0,r.nNeighbors)});var f=Math.max(0,this.localConnectivity-1),g=this.smoothKNNDistance(h,this.nNeighbors,f),p=g.sigmas,v=g.rhos,d=this.computeMembershipStrengths(a,h,p,v),w=d.rows,y=d.cols,b=d.vals,M=[t.length,e.length],x=new l.SparseMatrix(w,y,b,M),E=l.normalize(x,"l1"),R=l.getCSR(E),k=t.length,z=S(m.reshape2d(R.indices,k,this.nNeighbors),m.reshape2d(R.values,k,this.nNeighbors),this.embedding),N=this.nEpochs?this.nEpochs/3:x.nRows<=1e4?100:30,A=x.getValues().reduce(function(t,r){return r>t?r:t},0);x=x.map(function(t){return t0});if(g.length>=e){var p=Math.floor(e),v=e-p;p>0?(s[h]=g[p-1],v>1e-5&&(s[h]+=v*(g[p]-g[p-1]))):s[h]=v*g[0]}else g.length>0&&(s[h]=m.max(g));for(var d=0;d0?Math.exp(-b/c):1}if(Math.abs(w-o)<1e-5)break;w>o?c=(u+(l=c))/2:(u=c,l===1/0?c*=2:c=(u+l)/2)}if(a[h]=c,s[h]>0){var M=m.mean(f);a[h]<.001*M&&(a[h]=.001*M)}else{var x=m.mean(t.map(m.mean));a[h]<.001*x&&(a[h]=.001*x)}}return{sigmas:a,rhos:s}},t.prototype.computeMembershipStrengths=function(t,r,e,n){for(var i=t.length,o=t[0].length,s=m.zeros(i*o),a=m.zeros(i*o),h=m.zeros(i*o),u=0;u0&&(e[n]=r/i[n])}),e},t.prototype.assignOptimizationStateParameters=function(t){Object.assign(this.optimizationState,t)},t.prototype.prepareForOptimizationLoop=function(){var t=this.repulsionStrength,r=this.learningRate,e=this.negativeSampleRate,n=this.optimizationState,i=n.epochsPerSample,o=n.headEmbedding,a=n.tailEmbedding,h=o[0].length,u=o.length===a.length,l=i.map(function(t){return t/e}),c=s(l),f=s(i);this.assignOptimizationStateParameters({epochOfNextSample:f,epochOfNextNegativeSample:c,epochsPerNegativeSample:l,moveOther:u,initialAlpha:r,alpha:r,gamma:t,dim:h})},t.prototype.initializeOptimization=function(){var t=this.embedding,r=this.embedding,e=this.optimizationState,n=e.head,i=e.tail,o=e.epochsPerSample,s=this.getNEpochs(),a=this.graph.nCols,h=b(this.spread,this.minDist),u=h.a,l=h.b;this.assignOptimizationStateParameters({headEmbedding:t,tailEmbedding:r,head:n,tail:i,epochsPerSample:o,a:u,b:l,nEpochs:s,nVertices:a})},t.prototype.optimizeLayoutStep=function(t){for(var r=this.optimizationState,e=r.head,n=r.tail,i=r.headEmbedding,o=r.tailEmbedding,s=r.epochsPerSample,a=r.epochOfNextSample,h=r.epochOfNextNegativeSample,u=r.epochsPerNegativeSample,l=r.moveOther,c=r.initialAlpha,f=r.alpha,g=r.gamma,p=r.a,v=r.b,d=r.dim,b=r.nEpochs,M=r.nVertices,x=0;xt)){var S=e[x],E=n[x],R=i[S],k=o[E],z=y(R,k),N=0;z>0&&(N=-2*p*v*Math.pow(z,v-1),N/=p*Math.pow(z,v)+1);for(var A=0;A0)O=2*g*v,O/=(.001+I)*(p*Math.pow(I,v)+1);else if(S===P)continue;for(A=0;A0&&(V=w(O*(R[A]-j[A]),4)),R[A]+=V*f}}h[x]+=C*u[x]}return r.alpha=c*(1-t/b),r.currentEpoch+=1,i},t.prototype.optimizeLayoutAsync=function(t){var r=this;return void 0===t&&(t=function(){return!0}),new Promise(function(e,o){var s=function(){return n(r,void 0,void 0,function(){var r,n,a,h,u,l;return i(this,function(i){try{if(r=this.optimizationState,n=r.nEpochs,a=r.currentEpoch,this.embedding=this.optimizeLayoutStep(a),h=this.optimizationState.currentEpoch,u=!1===t(h),l=h===n,u||l)return[2,e(l)];setTimeout(function(){return s()},0)}catch(t){o(t)}return[2]})})};setTimeout(function(){return s()},0)})},t.prototype.optimizeLayout=function(t){void 0===t&&(t=function(){return!0});for(var r=!1,e=[];!r;){var n=this.optimizationState,i=n.nEpochs,o=n.currentEpoch;e=this.optimizeLayoutStep(o);var s=this.optimizationState.currentEpoch,a=!1===t(s);r=s===i||a}return e},t.prototype.getNEpochs=function(){var t=this.graph;if(this.nEpochs>0)return this.nEpochs;var r=t.nRows;return r<=2500?500:r<=5e3?400:r<=7500?300:200},t}();function v(t,r){for(var e=0,n=0;nr?r:t<-r?-r:t}function y(t,r){for(var e=0,n=0;n=r?Math.exp(-(e[i]-r)/t):n}),i={x:e,y:n},s={damping:1.5,initialValues:[.5,.5],gradientDifference:.1,maxIterations:100,errorTolerance:.01},a=g.default(i,function(t){var r=o(t,2),e=r[0],n=r[1];return function(t){return 1/(1+e*Math.pow(t,2*n))}},s).parameterValues,h=o(a,2);return{a:h[0],b:h[1]}}function M(t,r,e,n){return void 0===e&&(e=1),void 0===n&&(n=5),t.map(function(t,i,o){return-1===r[i]||-1===r[o]?t*Math.exp(-e):r[i]!==r[o]?t*Math.exp(-n):t})}function x(t){t=l.normalize(t,"max");var r=l.transpose(t),e=l.pairwiseMultiply(r,t);return t=l.add(t,l.subtract(r,e)),l.eliminateZeros(t)}function S(t,r,e){for(var n=m.zeros(t.length).map(function(t){return m.zeros(e[0].length)}),i=0;i=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}},i=this&&this.__importStar||function(t){if(t&&t.__esModule)return t;var r={};if(null!=t)for(var e in t)Object.hasOwnProperty.call(t,e)&&(r[e]=t[e]);return r.default=t,r};Object.defineProperty(r,"__esModule",{value:!0});var o=i(e(2)),s=i(e(3)),a=i(e(4)),h=i(e(1));r.makeNNDescent=function(t,r){return function(e,n,i,s,a,u,l,c){void 0===s&&(s=10),void 0===a&&(a=50),void 0===u&&(u=.001),void 0===l&&(l=.5),void 0===c&&(c=!0);for(var f=e.length,m=o.makeHeap(e.length,i),g=0;gr&&(r=t[e]);return r};var s=function(t){if(!i()(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");for(var r=t[0],e=1;e1&&void 0!==arguments[1]?arguments[1]:{};if(!i()(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");if(void 0!==e.output){if(!i()(e.output))throw new TypeError("output option must be an array if specified");r=e.output}else r=new Array(t.length);var n=s(t),a=o(t);if(n===a)throw new RangeError("minimum and maximum input values are equal. Cannot rescale a constant array");var h=e.min,u=void 0===h?e.autoMinMax?n:0:h,l=e.max,c=void 0===l?e.autoMinMax?a:1:l;if(u>=c)throw new RangeError("min option must be smaller than max option");for(var f=(c-u)/(a-n),m=0;mMath.abs(h[i])&&(i=r);if(i!==e){for(n=0;n=0;i--){for(n=0;no?t[i][o]:i===o?1:0;return n}get upperTriangularMatrix(){for(var t=this.LU,r=t.rows,e=t.columns,n=new _(r,e),i=0;iMath.abs(r)?(e=r/t,Math.abs(t)*Math.sqrt(1+e*e)):0!==r?(e=t/r,Math.abs(r)*Math.sqrt(1+e*e)):0}function l(t,r,e){for(var n=new Array(t),i=0;i=0;t--)if(0!==v[t]){for(let r=t+1;r=0;t--){if(t0;){let t,r;for(t=R-2;t>=-1&&-1!==t;t--){const r=Number.MIN_VALUE+z*Math.abs(v[t]+Math.abs(v[t+1]));if(Math.abs(y[t])<=r||Number.isNaN(y[t])){y[t]=0;break}}if(t===R-2)r=4;else{let e;for(e=R-1;e>=t&&e!==t;e--){let r=(e!==R?Math.abs(y[e]):0)+(e!==t+1?Math.abs(y[e-1]):0);if(Math.abs(v[e])<=z*r){v[e]=0;break}}e===t?r=3:e===R-1?r=1:(r=2,t=e)}switch(t++,r){case 1:{let r=y[R-2];y[R-2]=0;for(let e=R-2;e>=t;e--){let i=u(v[e],r),o=v[e]/i,s=r/i;if(v[e]=i,e!==t&&(r=-s*y[e-1],y[e-1]=o*y[e-1]),c)for(let t=0;t=v[t+1]);){let r=v[t];if(v[t]=v[t+1],v[t+1]=r,c&&tr?i[o][e]=t[o][e]/this.s[e]:i[o][e]=0;var o=this.U,s=o.length,a=o[0].length,h=new _(e,s);for(let t=0;tt&&r++;return r}get diagonal(){return this.s}get threshold(){return Number.EPSILON/2*Math.max(this.m,this.n)*this.s[0]}get leftSingularVectors(){return _.isMatrix(this.U)||(this.U=new _(this.U)),this.U}get rightSingularVectors(){return _.isMatrix(this.V)||(this.V=new _(this.V)),this.V}get diagonalMatrix(){return _.diag(this.s)}}function f(t,r,e){var n=e?t.rows:t.rows-1;if(r<0||r>n)throw new RangeError("Row index out of range")}function m(t,r,e){var n=e?t.columns:t.columns-1;if(r<0||r>n)throw new RangeError("Column index out of range")}function g(t,r){if(r.to1DArray&&(r=r.to1DArray()),r.length!==t.columns)throw new RangeError("vector size must be the same as the number of columns");return r}function p(t,r){if(r.to1DArray&&(r=r.to1DArray()),r.length!==t.rows)throw new RangeError("vector size must be the same as the number of rows");return r}function v(t,r,e){return{row:d(t,r),column:w(t,e)}}function d(t,r){if("object"!=typeof r)throw new TypeError("unexpected type for row indices");if(r.some(r=>r<0||r>=t.rows))throw new RangeError("row indices are out of range");return Array.isArray(r)||(r=Array.from(r)),r}function w(t,r){if("object"!=typeof r)throw new TypeError("unexpected type for column indices");if(r.some(r=>r<0||r>=t.columns))throw new RangeError("column indices are out of range");return Array.isArray(r)||(r=Array.from(r)),r}function y(t,r,e,n,i){if(5!==arguments.length)throw new RangeError("expected 4 arguments");if(b("startRow",r),b("endRow",e),b("startColumn",n),b("endColumn",i),r>e||n>i||r<0||r>=t.rows||e<0||e>=t.rows||n<0||n>=t.columns||i<0||i>=t.columns)throw new RangeError("Submatrix indices are out of range")}function b(t,r){if("number"!=typeof r)throw new TypeError(`${t} must be a number`)}class M extends(C()){constructor(t,r,e){super(),this.matrix=t,this.rows=r,this.columns=e}static get[Symbol.species](){return _}}class x extends M{constructor(t){super(t,t.columns,t.rows)}set(t,r,e){return this.matrix.set(r,t,e),this}get(t,r){return this.matrix.get(r,t)}}class S extends M{constructor(t,r){super(t,1,t.columns),this.row=r}set(t,r,e){return this.matrix.set(this.row,r,e),this}get(t,r){return this.matrix.get(this.row,r)}}class E extends M{constructor(t,r,e,n,i){y(t,r,e,n,i),super(t,e-r+1,i-n+1),this.startRow=r,this.startColumn=n}set(t,r,e){return this.matrix.set(this.startRow+t,this.startColumn+r,e),this}get(t,r){return this.matrix.get(this.startRow+t,this.startColumn+r)}}class R extends M{constructor(t,r,e){var n=v(t,r,e);super(t,n.row.length,n.column.length),this.rowIndices=n.row,this.columnIndices=n.column}set(t,r,e){return this.matrix.set(this.rowIndices[t],this.columnIndices[r],e),this}get(t,r){return this.matrix.get(this.rowIndices[t],this.columnIndices[r])}}class k extends M{constructor(t,r){super(t,(r=d(t,r)).length,t.columns),this.rowIndices=r}set(t,r,e){return this.matrix.set(this.rowIndices[t],r,e),this}get(t,r){return this.matrix.get(this.rowIndices[t],r)}}class z extends M{constructor(t,r){r=w(t,r),super(t,t.rows,r.length),this.columnIndices=r}set(t,r,e){return this.matrix.set(t,this.columnIndices[r],e),this}get(t,r){return this.matrix.get(t,this.columnIndices[r])}}class N extends M{constructor(t,r){super(t,t.rows,1),this.column=r}set(t,r,e){return this.matrix.set(t,this.column,e),this}get(t){return this.matrix.get(t,this.column)}}class A extends M{constructor(t){super(t,t.rows,t.columns)}set(t,r,e){return this.matrix.set(this.rows-t-1,r,e),this}get(t,r){return this.matrix.get(this.rows-t-1,r)}}class V extends M{constructor(t){super(t,t.rows,t.columns)}set(t,r,e){return this.matrix.set(t,this.columns-r-1,e),this}get(t,r){return this.matrix.get(t,this.columns-r-1)}}function C(t){void 0===t&&(t=Object);class r extends t{static get[Symbol.species](){return this}static from1DArray(t,r,e){if(t*r!==e.length)throw new RangeError("Data length does not match given dimensions");for(var n=new this(t,r),i=0;it&&(t=this.get(r,e));return t}maxIndex(){for(var t=this.get(0,0),r=[0,0],e=0;et&&(t=this.get(e,n),r[0]=e,r[1]=n);return r}min(){for(var t=this.get(0,0),r=0;rr&&(r=this.get(t,e));return r}maxRowIndex(t){f(this,t);for(var r=this.get(t,0),e=[t,0],n=1;nr&&(r=this.get(t,n),e[1]=n);return e}minRow(t){f(this,t);for(var r=this.get(t,0),e=1;er&&(r=this.get(e,t));return r}maxColumnIndex(t){m(this,t);for(var r=this.get(0,t),e=[0,t],n=1;nr&&(r=this.get(n,t),e[0]=n);return e}minColumn(t){m(this,t);for(var r=this.get(0,t),e=1;e=(r=void 0===r?1:r))throw new RangeError("min should be strictly smaller than max");for(var e=this.constructor.empty(this.rows,this.columns),n=0;n=(r=void 0===r?1:r))throw new RangeError("min should be strictly smaller than max");for(var e=this.constructor.empty(this.rows,this.columns),n=0;ne||r<0||r>=this.columns||e<0||e>=this.columns)throw new RangeError("Argument out of range");for(var n=new this.constructor[Symbol.species](t.length,e-r+1),i=0;i=this.rows)throw new RangeError(`Row index out of range: ${t[i]}`);n.set(i,o-r,this.get(t[i],o))}return n}subMatrixColumn(t,r,e){if(void 0===r&&(r=0),void 0===e&&(e=this.rows-1),r>e||r<0||r>=this.rows||e<0||e>=this.rows)throw new RangeError("Argument out of range");for(var n=new this.constructor[Symbol.species](e-r+1,t.length),i=0;i=this.columns)throw new RangeError(`Column index out of range: ${t[i]}`);n.set(o-r,i,this.get(o,t[i]))}return n}setSubMatrix(t,r,e){y(this,r,r+(t=this.constructor.checkMatrix(t)).rows-1,e,e+t.columns-1);for(var n=0;nt?i[o]=1/i[o]:i[o]=0;return i=this.constructor[Symbol.species].diag(i),n.mmul(i.mmul(e.transposeView()))}clone(){for(var t=new this.constructor[Symbol.species](this.rows,this.columns),r=0;r>","signPropagatingRightShift"],[">>>","rightShift","zeroFillRightShift"]]){var u=o($("\n(function %name%(value) {\n if (typeof value === 'number') return this.%name%S(value);\n return this.%name%M(value);\n})\n",{name:s[1],op:s[0]})),l=o($("\n(function %name%S(value) {\n for (var i = 0; i < this.rows; i++) {\n for (var j = 0; j < this.columns; j++) {\n this.set(i, j, this.get(i, j) %op% value);\n }\n }\n return this;\n})\n",{name:`${s[1]}S`,op:s[0]})),d=o($("\n(function %name%M(matrix) {\n matrix = this.constructor.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 (var i = 0; i < this.rows; i++) {\n for (var j = 0; j < this.columns; j++) {\n this.set(i, j, this.get(i, j) %op% matrix.get(i, j));\n }\n }\n return this;\n})\n",{name:`${s[1]}M`,op:s[0]})),w=o($("\n(function %name%(matrix, value) {\n var newMatrix = new this[Symbol.species](matrix);\n return newMatrix.%name%(value);\n})\n",{name:s[1]}));for(n=1;n0){if(super(t),!(Number.isInteger(r)&&r>0))throw new TypeError("nColumns must be a positive integer");for(e=0;e=0;o--){for(i=0;i=0;e--){for(t=0;ti)return new Array(r.rows+1).fill(0);for(var o=r.addRow(e,[0]),s=0;s0;a--){for(c=0,s=0,u=0;u0&&(o=-o),r[a]=c*o,s-=i*o,e[a-1]=i-o,h=0;hl){0;do{for(1,i=e[l],f=(e[l+1]-i)/(2*r[l]),m=u(f,1),f<0&&(m=-m),e[l]=r[l]/(f+m),e[l+1]=r[l]*(f+m),g=e[l+1],o=i-e[l],s=l+2;s=l;s--)for(d=v,v=p,b=y,i=p*r[s],o=p*f,m=u(f,r[s]),r[s+1]=y*m,y=r[s]/m,f=(p=f/m)*e[s]-y*i,e[s+1]=o+y*(p*i+y*e[s]),h=0;hS*x)}e[l]=e[l]+M,r[l]=0}for(s=0;s=u;a--)e[a]=r[a][u-1]/l,s+=e[a]*e[a];for(o=Math.sqrt(s),e[u]>0&&(o=-o),s-=e[u]*o,e[u]=e[u]-o,h=u;h=u;a--)i+=e[a]*r[a][h];for(i/=s,a=u;a<=c;a++)r[a][h]-=i*e[a]}for(a=0;a<=c;a++){for(i=0,h=c;h>=u;h--)i+=e[h]*r[a][h];for(i/=s,h=u;h<=c;h++)r[a][h]-=i*e[h]}e[u]=l*e[u],r[u][u-1]=l*o}}for(a=0;a=1;u--)if(0!==r[u][u-1]){for(a=u+1;a<=c;a++)e[a]=r[a][u-1];for(h=u;h<=c;h++){for(o=0,a=u;a<=c;a++)o+=e[a]*n[a][h];for(o=o/e[u]/r[u][u-1],a=u;a<=c;a++)n[a][h]+=o*e[a]}}}(o,f,m,s),function(t,r,e,n,i){var o,s,a,h,u,l,c,f,m,g,p,v,d,w,y,b=t-1,M=t-1,x=Number.EPSILON,S=0,E=0,R=0,k=0,z=0,N=0,A=0,V=0;for(o=0;oM)&&(e[o]=i[o][o],r[o]=0),s=Math.max(o-1,0);s=0;){for(h=b;h>0&&(0===(N=Math.abs(i[h-1][h-1])+Math.abs(i[h][h]))&&(N=E),!(Math.abs(i[h][h-1])=0){for(A=R>=0?R+A:R-A,e[b-1]=f+A,e[b]=e[b-1],0!==A&&(e[b]=f-c/A),r[b-1]=0,r[b]=0,f=i[b][b-1],N=Math.abs(f)+Math.abs(A),R=f/N,k=A/N,z=Math.sqrt(R*R+k*k),R/=z,k/=z,s=b-1;s0){for(N=Math.sqrt(N),m=h&&(A=i[u][u],R=((z=f-A)*(N=m-A)-c)/i[u+1][u]+i[u][u+1],k=i[u+1][u+1]-A-z-N,z=i[u+2][u+1],N=Math.abs(R)+Math.abs(k)+Math.abs(z),R/=N,k/=N,z/=N,u!==h)&&!(Math.abs(i[u][u-1])*(Math.abs(k)+Math.abs(z))u+2&&(i[o][o-3]=0);for(a=u;a<=b-1&&(w=a!==b-1,a!==u&&(R=i[a][a-1],k=i[a+1][a-1],z=w?i[a+2][a-1]:0,0!==(f=Math.abs(R)+Math.abs(k)+Math.abs(z))&&(R/=f,k/=f,z/=f)),0!==f);a++)if(N=Math.sqrt(R*R+k*k+z*z),R<0&&(N=-N),0!==N){for(a!==u?i[a][a-1]=-N*f:h!==u&&(i[a][a-1]=-i[a][a-1]),f=(R+=N)/N,m=k/N,A=z/N,k/=R,z/=R,s=a;s=0;b--)if(R=e[b],0===(k=r[b]))for(h=b,i[b][b]=1,o=b-1;o>=0;o--){for(c=i[o][o]-R,z=0,s=h;s<=b;s++)z+=i[o][s]*i[s][b];if(r[o]<0)A=c,N=z;else if(h=o,0===r[o]?i[o][b]=0!==c?-z/c:-z/(x*E):(f=i[o][o+1],m=i[o+1][o],k=(e[o]-R)*(e[o]-R)+r[o]*r[o],l=(f*N-A*z)/k,i[o][b]=l,i[o+1][b]=Math.abs(f)>Math.abs(A)?(-z-c*l)/f:(-N-m*l)/A),l=Math.abs(i[o][b]),x*l*l>1)for(s=o;s<=b;s++)i[s][b]=i[s][b]/l}else if(k<0)for(h=b-1,Math.abs(i[b][b-1])>Math.abs(i[b-1][b])?(i[b-1][b-1]=k/i[b][b-1],i[b-1][b]=-(i[b][b]-R)/i[b][b-1]):(y=$(0,-i[b-1][b],i[b-1][b-1]-R,k),i[b-1][b-1]=y[0],i[b-1][b]=y[1]),i[b][b-1]=0,i[b][b]=1,o=b-2;o>=0;o--){for(g=0,p=0,s=h;s<=b;s++)g+=i[o][s]*i[s][b-1],p+=i[o][s]*i[s][b];if(c=i[o][o]-R,r[o]<0)A=c,z=g,N=p;else if(h=o,0===r[o]?(y=$(-g,-p,c,k),i[o][b-1]=y[0],i[o][b]=y[1]):(f=i[o][o+1],m=i[o+1][o],v=(e[o]-R)*(e[o]-R)+r[o]*r[o]-k*k,d=2*(e[o]-R)*k,0===v&&0===d&&(v=x*E*(Math.abs(c)+Math.abs(k)+Math.abs(f)+Math.abs(m)+Math.abs(A))),y=$(f*z-A*g+k*p,f*N-A*p-k*g,v,d),i[o][b-1]=y[0],i[o][b]=y[1],Math.abs(f)>Math.abs(A)+Math.abs(k)?(i[o+1][b-1]=(-g-c*i[o][b-1]+k*i[o][b])/f,i[o+1][b]=(-p-c*i[o][b]-k*i[o][b-1])/f):(y=$(-z-m*i[o][b-1],-N-m*i[o][b],A,k),i[o+1][b-1]=y[0],i[o+1][b]=y[1])),l=Math.max(Math.abs(i[o][b-1]),Math.abs(i[o][b])),x*l*l>1)for(s=o;s<=b;s++)i[s][b-1]=i[s][b-1]/l,i[s][b]=i[s][b]/l}for(o=0;oM)for(s=o;s=0;s--)for(o=0;o<=M;o++){for(A=0,a=0;a<=Math.min(s,M);a++)A+=n[o][a]*i[a][s];n[o][s]=A}}(o,h,a,s,f)}this.n=o,this.e=h,this.d=a,this.V=s}get realEigenvalues(){return this.d}get imaginaryEigenvalues(){return this.e}get eigenvectorMatrix(){return _.isMatrix(this.V)||(this.V=new _(this.V)),this.V}get diagonalMatrix(){var t,r,e=this.n,n=this.e,i=this.d,o=new _(e,e);for(t=0;t0?o[t][t+1]=n[t]:n[t]<0&&(o[t][t-1]=n[t])}return o}}function $(t,r,e,n){var i,o;return Math.abs(e)>Math.abs(n)?[(t+(i=n/e)*r)/(o=e+i*n),(r-i*t)/o]:[((i=e/n)*t+r)/(o=n+i*e),(i*r-t)/o]}class Q{constructor(t){if(!(t=j.checkMatrix(t)).isSymmetric())throw new Error("Matrix is not symmetric");var r,e,n,i=t,o=i.rows,s=new _(o,o),a=!0;for(e=0;e0,s[e][e]=Math.sqrt(Math.max(u,0)),n=e+1;n=0;o--)for(i=0;i Date: Thu, 27 Feb 2020 12:15:36 -0500 Subject: [PATCH 45/46] Update ml-levenberg-marquardt version to 2.0.0 This resolves a runtime TypeError in production builds. --- lib/umap-js.js | 7158 +++++++++++++++++++------------------------- lib/umap-js.min.js | 2 +- package.json | 2 +- yarn.lock | 44 +- 4 files changed, 3177 insertions(+), 4029 deletions(-) diff --git a/lib/umap-js.js b/lib/umap-js.js index 9550251..f8499cd 100644 --- a/lib/umap-js.js +++ b/lib/umap-js.js @@ -2009,13 +2009,12 @@ exports.initializeSearch = initializeSearch; /***/ }), /* 8 */ -/***/ (function(module, exports, __webpack_require__) { +/***/ (function(module, __webpack_exports__, __webpack_require__) { "use strict"; +__webpack_require__.r(__webpack_exports__); - -var mlMatrix = __webpack_require__(9); - +// CONCATENATED MODULE: ./node_modules/ml-levenberg-marquardt/src/errorCalculation.js /** * Calculate current error * @ignore @@ -2039,199 +2038,6 @@ function errorCalculation( return error; } -/** - * Difference of the matrix function over the parameters - * @ignore - * @param {{x:Array, y:Array}} data - Array of points to fit in the format [x1, x2, ... ], [y1, y2, ... ] - * @param {Array} evaluatedData - Array of previous evaluated function values - * @param {Array} params - Array of previous parameter values - * @param {number} gradientDifference - Adjustment for decrease the damping parameter - * @param {function} paramFunction - The parameters and returns a function with the independent variable as a parameter - * @return {Matrix} - */ -function gradientFunction( - data, - evaluatedData, - params, - gradientDifference, - paramFunction -) { - const n = params.length; - const m = data.x.length; - - var ans = new Array(n); - - for (var param = 0; param < n; param++) { - ans[param] = new Array(m); - var auxParams = params.concat(); - auxParams[param] += gradientDifference; - var funcParam = paramFunction(auxParams); - - for (var point = 0; point < m; point++) { - ans[param][point] = evaluatedData[point] - funcParam(data.x[point]); - } - } - return new mlMatrix.Matrix(ans); -} - -/** - * Matrix function over the samples - * @ignore - * @param {{x:Array, y:Array}} data - Array of points to fit in the format [x1, x2, ... ], [y1, y2, ... ] - * @param {Array} evaluatedData - Array of previous evaluated function values - * @return {Matrix} - */ -function matrixFunction(data, evaluatedData) { - const m = data.x.length; - - var ans = new Array(m); - - for (var point = 0; point < m; point++) { - ans[point] = data.y[point] - evaluatedData[point]; - } - - return new mlMatrix.Matrix([ans]); -} - -/** - * Iteration for Levenberg-Marquardt - * @ignore - * @param {{x:Array, y:Array}} data - Array of points to fit in the format [x1, x2, ... ], [y1, y2, ... ] - * @param {Array} params - Array of previous parameter values - * @param {number} damping - Levenberg-Marquardt parameter - * @param {number} gradientDifference - Adjustment for decrease the damping parameter - * @param {function} parameterizedFunction - The parameters and returns a function with the independent variable as a parameter - * @return {Array} - */ -function step( - data, - params, - damping, - gradientDifference, - parameterizedFunction -) { - var identity = mlMatrix.Matrix.eye(params.length).mul( - damping * gradientDifference * gradientDifference - ); - - var l = data.x.length; - var evaluatedData = new Array(l); - const func = parameterizedFunction(params); - for (var i = 0; i < l; i++) { - evaluatedData[i] = func(data.x[i]); - } - var gradientFunc = gradientFunction( - data, - evaluatedData, - params, - gradientDifference, - parameterizedFunction - ); - var matrixFunc = matrixFunction(data, evaluatedData).transposeView(); - var inverseMatrix = mlMatrix.inverse( - identity.add(gradientFunc.mmul(gradientFunc.transposeView())) - ); - params = new mlMatrix.Matrix([params]); - params = params.sub( - inverseMatrix - .mmul(gradientFunc) - .mmul(matrixFunc) - .mul(gradientDifference) - .transposeView() - ); - - return params.to1DArray(); -} - -/** - * Curve fitting algorithm - * @param {{x:Array, y:Array}} data - Array of points to fit in the format [x1, x2, ... ], [y1, y2, ... ] - * @param {function} parameterizedFunction - The parameters and returns a function with the independent variable as a parameter - * @param {object} [options] - Options object - * @param {number} [options.damping] - Levenberg-Marquardt parameter - * @param {number} [options.gradientDifference = 10e-2] - Adjustment for decrease the damping parameter - * @param {Array} [options.initialValues] - Array of initial parameter values - * @param {number} [options.maxIterations = 100] - Maximum of allowed iterations - * @param {number} [options.errorTolerance = 10e-3] - Minimum uncertainty allowed for each point - * @return {{parameterValues: Array, parameterError: number, iterations: number}} - */ -function levenbergMarquardt( - data, - parameterizedFunction, - options = {} -) { - let { - maxIterations = 100, - gradientDifference = 10e-2, - damping = 0, - errorTolerance = 10e-3, - initialValues - } = options; - - if (damping <= 0) { - throw new Error('The damping option must be a positive number'); - } else if (!data.x || !data.y) { - throw new Error('The data parameter must have x and y elements'); - } else if ( - !Array.isArray(data.x) || - data.x.length < 2 || - !Array.isArray(data.y) || - data.y.length < 2 - ) { - throw new Error( - 'The data parameter elements must be an array with more than 2 points' - ); - } else { - let dataLen = data.x.length; - if (dataLen !== data.y.length) { - throw new Error('The data parameter elements must have the same size'); - } - } - - var parameters = - initialValues || new Array(parameterizedFunction.length).fill(1); - - if (!Array.isArray(parameters)) { - throw new Error('initialValues must be an array'); - } - - var error = errorCalculation(data, parameters, parameterizedFunction); - - var converged = error <= errorTolerance; - - for ( - var iteration = 0; - iteration < maxIterations && !converged; - iteration++ - ) { - parameters = step( - data, - parameters, - damping, - gradientDifference, - parameterizedFunction - ); - error = errorCalculation(data, parameters, parameterizedFunction); - converged = error <= errorTolerance; - } - - return { - parameterValues: parameters, - parameterError: error, - iterations: iteration - }; -} - -module.exports = levenbergMarquardt; - - -/***/ }), -/* 9 */ -/***/ (function(module, __webpack_exports__, __webpack_require__) { - -"use strict"; -__webpack_require__.r(__webpack_exports__); - // EXTERNAL MODULE: ./node_modules/is-any-array/src/index.js var src = __webpack_require__(0); var src_default = /*#__PURE__*/__webpack_require__.n(src); @@ -2254,13 +2060,13 @@ function lib_es6_max(input) { throw new TypeError('input must not be empty'); } - var max = input[0]; + var maxValue = input[0]; for (var i = 1; i < input.length; i++) { - if (input[i] > max) max = input[i]; + if (input[i] > maxValue) maxValue = input[i]; } - return max; + return maxValue; } /* harmony default export */ var lib_es6 = (lib_es6_max); @@ -2283,13 +2089,13 @@ function lib_es6_min(input) { throw new TypeError('input must not be empty'); } - var min = input[0]; + var minValue = input[0]; for (var i = 1; i < input.length; i++) { - if (input[i] < min) min = input[i]; + if (input[i] < minValue) minValue = input[i]; } - return min; + return minValue; } /* harmony default export */ var ml_array_min_lib_es6 = (lib_es6_min); @@ -2347,3125 +2153,2676 @@ function rescale(input) { /* harmony default export */ var ml_array_rescale_lib_es6 = (rescale); -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/dc/lu.js +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/util.js +/** + * @private + * Check that a row index is not out of bounds + * @param {Matrix} matrix + * @param {number} index + * @param {boolean} [outer] + */ +function checkRowIndex(matrix, index, outer) { + let max = outer ? matrix.rows : matrix.rows - 1; + if (index < 0 || index > max) { + throw new RangeError('Row index out of range'); + } +} +/** + * @private + * Check that a column index is not out of bounds + * @param {Matrix} matrix + * @param {number} index + * @param {boolean} [outer] + */ +function checkColumnIndex(matrix, index, outer) { + let max = outer ? matrix.columns : matrix.columns - 1; + if (index < 0 || index > max) { + throw new RangeError('Column index out of range'); + } +} /** - * @class LuDecomposition - * @link https://github.com/lutzroeder/Mapack/blob/master/Source/LuDecomposition.cs + * @private + * Check that the provided vector is an array with the right length * @param {Matrix} matrix + * @param {Array|Matrix} vector + * @return {Array} + * @throws {RangeError} */ -class lu_LuDecomposition { - constructor(matrix) { - matrix = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(matrix); +function checkRowVector(matrix, vector) { + if (vector.to1DArray) { + vector = vector.to1DArray(); + } + if (vector.length !== matrix.columns) { + throw new RangeError( + 'vector size must be the same as the number of columns', + ); + } + return vector; +} - var lu = matrix.clone(); - var rows = lu.rows; - var columns = lu.columns; - var pivotVector = new Array(rows); - var pivotSign = 1; - var i, j, k, p, s, t, v; - var LUcolj, kmax; +/** + * @private + * Check that the provided vector is an array with the right length + * @param {Matrix} matrix + * @param {Array|Matrix} vector + * @return {Array} + * @throws {RangeError} + */ +function checkColumnVector(matrix, vector) { + if (vector.to1DArray) { + vector = vector.to1DArray(); + } + if (vector.length !== matrix.rows) { + throw new RangeError('vector size must be the same as the number of rows'); + } + return vector; +} - for (i = 0; i < rows; i++) { - pivotVector[i] = i; - } +function checkIndices(matrix, rowIndices, columnIndices) { + return { + row: checkRowIndices(matrix, rowIndices), + column: checkColumnIndices(matrix, columnIndices), + }; +} - LUcolj = new Array(rows); +function checkRowIndices(matrix, rowIndices) { + if (typeof rowIndices !== 'object') { + throw new TypeError('unexpected type for row indices'); + } - for (j = 0; j < columns; j++) { - for (i = 0; i < rows; i++) { - LUcolj[i] = lu.get(i, j); - } + let rowOut = rowIndices.some((r) => { + return r < 0 || r >= matrix.rows; + }); - for (i = 0; i < rows; i++) { - kmax = Math.min(i, j); - s = 0; - for (k = 0; k < kmax; k++) { - s += lu.get(i, k) * LUcolj[k]; - } - LUcolj[i] -= s; - lu.set(i, j, LUcolj[i]); - } + if (rowOut) { + throw new RangeError('row indices are out of range'); + } - p = j; - for (i = j + 1; i < rows; i++) { - if (Math.abs(LUcolj[i]) > Math.abs(LUcolj[p])) { - p = i; - } - } + if (!Array.isArray(rowIndices)) rowIndices = Array.from(rowIndices); - if (p !== j) { - for (k = 0; k < columns; k++) { - t = lu.get(p, k); - lu.set(p, k, lu.get(j, k)); - lu.set(j, k, t); - } + return rowIndices; +} - v = pivotVector[p]; - pivotVector[p] = pivotVector[j]; - pivotVector[j] = v; +function checkColumnIndices(matrix, columnIndices) { + if (typeof columnIndices !== 'object') { + throw new TypeError('unexpected type for column indices'); + } - pivotSign = -pivotSign; - } + let columnOut = columnIndices.some((c) => { + return c < 0 || c >= matrix.columns; + }); - if (j < rows && lu.get(j, j) !== 0) { - for (i = j + 1; i < rows; i++) { - lu.set(i, j, lu.get(i, j) / lu.get(j, j)); - } - } - } + if (columnOut) { + throw new RangeError('column indices are out of range'); + } + if (!Array.isArray(columnIndices)) columnIndices = Array.from(columnIndices); - this.LU = lu; - this.pivotVector = pivotVector; - this.pivotSign = pivotSign; + return columnIndices; +} + +function checkRange(matrix, startRow, endRow, startColumn, endColumn) { + if (arguments.length !== 5) { + throw new RangeError('expected 4 arguments'); + } + checkNumber('startRow', startRow); + checkNumber('endRow', endRow); + checkNumber('startColumn', startColumn); + checkNumber('endColumn', endColumn); + if ( + startRow > endRow || + startColumn > endColumn || + startRow < 0 || + startRow >= matrix.rows || + endRow < 0 || + endRow >= matrix.rows || + startColumn < 0 || + startColumn >= matrix.columns || + endColumn < 0 || + endColumn >= matrix.columns + ) { + throw new RangeError('Submatrix indices are out of range'); } +} - /** - * - * @return {boolean} - */ - isSingular() { - var data = this.LU; - var col = data.columns; - for (var j = 0; j < col; j++) { - if (data[j][j] === 0) { - return true; - } +function newArray(length, value = 0) { + let array = []; + for (let i = 0; i < length; i++) { + array.push(value); + } + return array; +} + +function checkNumber(name, value) { + if (typeof value !== 'number') { + throw new TypeError(`${name} must be a number`); + } +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/stat.js + + +function sumByRow(matrix) { + let sum = newArray(matrix.rows); + for (let i = 0; i < matrix.rows; ++i) { + for (let j = 0; j < matrix.columns; ++j) { + sum[i] += matrix.get(i, j); } - return false; } + return sum; +} - /** - * - * @param {Matrix} value - * @return {Matrix} - */ - solve(value) { - value = matrix_Matrix.checkMatrix(value); +function sumByColumn(matrix) { + let sum = newArray(matrix.columns); + for (let i = 0; i < matrix.rows; ++i) { + for (let j = 0; j < matrix.columns; ++j) { + sum[j] += matrix.get(i, j); + } + } + return sum; +} - var lu = this.LU; - var rows = lu.rows; +function sumAll(matrix) { + let v = 0; + for (let i = 0; i < matrix.rows; i++) { + for (let j = 0; j < matrix.columns; j++) { + v += matrix.get(i, j); + } + } + return v; +} - if (rows !== value.rows) { - throw new Error('Invalid matrix dimensions'); +function productByRow(matrix) { + let sum = newArray(matrix.rows, 1); + for (let i = 0; i < matrix.rows; ++i) { + for (let j = 0; j < matrix.columns; ++j) { + sum[i] *= matrix.get(i, j); } - if (this.isSingular()) { - throw new Error('LU matrix is singular'); + } + return sum; +} + +function productByColumn(matrix) { + let sum = newArray(matrix.columns, 1); + for (let i = 0; i < matrix.rows; ++i) { + for (let j = 0; j < matrix.columns; ++j) { + sum[j] *= matrix.get(i, j); } + } + return sum; +} - var count = value.columns; - var X = value.subMatrixRow(this.pivotVector, 0, count - 1); - var columns = lu.columns; - var i, j, k; +function productAll(matrix) { + let v = 1; + for (let i = 0; i < matrix.rows; i++) { + for (let j = 0; j < matrix.columns; j++) { + v *= matrix.get(i, j); + } + } + return v; +} - for (k = 0; k < columns; k++) { - for (i = k + 1; i < columns; i++) { - for (j = 0; j < count; j++) { - X[i][j] -= X[k][j] * lu[i][k]; - } - } +function varianceByRow(matrix, unbiased, mean) { + const rows = matrix.rows; + const cols = matrix.columns; + const variance = []; + + for (let i = 0; i < rows; i++) { + let sum1 = 0; + let sum2 = 0; + let x = 0; + for (let j = 0; j < cols; j++) { + x = matrix.get(i, j) - mean[i]; + sum1 += x; + sum2 += x * x; + } + if (unbiased) { + variance.push((sum2 - (sum1 * sum1) / cols) / (cols - 1)); + } else { + variance.push((sum2 - (sum1 * sum1) / cols) / cols); } - for (k = columns - 1; k >= 0; k--) { - for (j = 0; j < count; j++) { - X[k][j] /= lu[k][k]; - } - for (i = 0; i < k; i++) { - for (j = 0; j < count; j++) { - X[i][j] -= X[k][j] * lu[i][k]; - } - } + } + return variance; +} + +function varianceByColumn(matrix, unbiased, mean) { + const rows = matrix.rows; + const cols = matrix.columns; + const variance = []; + + for (let j = 0; j < cols; j++) { + let sum1 = 0; + let sum2 = 0; + let x = 0; + for (let i = 0; i < rows; i++) { + x = matrix.get(i, j) - mean[j]; + sum1 += x; + sum2 += x * x; + } + if (unbiased) { + variance.push((sum2 - (sum1 * sum1) / rows) / (rows - 1)); + } else { + variance.push((sum2 - (sum1 * sum1) / rows) / rows); } - return X; } + return variance; +} - /** - * - * @return {number} - */ - get determinant() { - var data = this.LU; - if (!data.isSquare()) { - throw new Error('Matrix must be square'); +function varianceAll(matrix, unbiased, mean) { + const rows = matrix.rows; + const cols = matrix.columns; + const size = rows * cols; + + let sum1 = 0; + let sum2 = 0; + let x = 0; + for (let i = 0; i < rows; i++) { + for (let j = 0; j < cols; j++) { + x = matrix.get(i, j) - mean; + sum1 += x; + sum2 += x * x; } - var determinant = this.pivotSign; - var col = data.columns; - for (var j = 0; j < col; j++) { - determinant *= data[j][j]; + } + if (unbiased) { + return (sum2 - (sum1 * sum1) / size) / (size - 1); + } else { + return (sum2 - (sum1 * sum1) / size) / size; + } +} + +function centerByRow(matrix, mean) { + for (let i = 0; i < matrix.rows; i++) { + for (let j = 0; j < matrix.columns; j++) { + matrix.set(i, j, matrix.get(i, j) - mean[i]); } - return determinant; } +} - /** - * - * @return {Matrix} - */ - get lowerTriangularMatrix() { - var data = this.LU; - var rows = data.rows; - var columns = data.columns; - var X = new matrix_Matrix(rows, columns); - for (var i = 0; i < rows; i++) { - for (var j = 0; j < columns; j++) { - if (i > j) { - X[i][j] = data[i][j]; - } else if (i === j) { - X[i][j] = 1; - } else { - X[i][j] = 0; - } - } +function centerByColumn(matrix, mean) { + for (let i = 0; i < matrix.rows; i++) { + for (let j = 0; j < matrix.columns; j++) { + matrix.set(i, j, matrix.get(i, j) - mean[j]); } - return X; } +} - /** - * - * @return {Matrix} - */ - get upperTriangularMatrix() { - var data = this.LU; - var rows = data.rows; - var columns = data.columns; - var X = new matrix_Matrix(rows, columns); - for (var i = 0; i < rows; i++) { - for (var j = 0; j < columns; j++) { - if (i <= j) { - X[i][j] = data[i][j]; - } else { - X[i][j] = 0; - } - } +function centerAll(matrix, mean) { + for (let i = 0; i < matrix.rows; i++) { + for (let j = 0; j < matrix.columns; j++) { + matrix.set(i, j, matrix.get(i, j) - mean); } - return X; } +} - /** - * - * @return {Array} - */ - get pivotPermutationVector() { - return this.pivotVector.slice(); +function getScaleByRow(matrix) { + const scale = []; + for (let i = 0; i < matrix.rows; i++) { + let sum = 0; + for (let j = 0; j < matrix.columns; j++) { + sum += Math.pow(matrix.get(i, j), 2) / (matrix.columns - 1); + } + scale.push(Math.sqrt(sum)); } + return scale; } -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/dc/util.js -function hypotenuse(a, b) { - var r = 0; - if (Math.abs(a) > Math.abs(b)) { - r = b / a; - return Math.abs(a) * Math.sqrt(1 + r * r); +function scaleByRow(matrix, scale) { + for (let i = 0; i < matrix.rows; i++) { + for (let j = 0; j < matrix.columns; j++) { + matrix.set(i, j, matrix.get(i, j) / scale[i]); + } } - if (b !== 0) { - r = a / b; - return Math.abs(b) * Math.sqrt(1 + r * r); +} + +function getScaleByColumn(matrix) { + const scale = []; + for (let j = 0; j < matrix.columns; j++) { + let sum = 0; + for (let i = 0; i < matrix.rows; i++) { + sum += Math.pow(matrix.get(i, j), 2) / (matrix.rows - 1); + } + scale.push(Math.sqrt(sum)); } - return 0; + return scale; } -function getFilled2DArray(rows, columns, value) { - var array = new Array(rows); - for (var i = 0; i < rows; i++) { - array[i] = new Array(columns); - for (var j = 0; j < columns; j++) { - array[i][j] = value; +function scaleByColumn(matrix, scale) { + for (let i = 0; i < matrix.rows; i++) { + for (let j = 0; j < matrix.columns; j++) { + matrix.set(i, j, matrix.get(i, j) / scale[j]); } } - return array; } -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/dc/svd.js +function getScaleAll(matrix) { + const divider = matrix.size - 1; + let sum = 0; + for (let j = 0; j < matrix.columns; j++) { + for (let i = 0; i < matrix.rows; i++) { + sum += Math.pow(matrix.get(i, j), 2) / divider; + } + } + return Math.sqrt(sum); +} +function scaleAll(matrix, scale) { + for (let i = 0; i < matrix.rows; i++) { + for (let j = 0; j < matrix.columns; j++) { + matrix.set(i, j, matrix.get(i, j) / scale); + } + } +} +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/inspect.js +function inspectMatrix() { + const indent = ' '.repeat(2); + const indentData = ' '.repeat(4); + return `${this.constructor.name} { +${indent}[ +${indentData}${inspectData(this, indentData)} +${indent}] +${indent}rows: ${this.rows} +${indent}columns: ${this.columns} +}`; +} +const maxRows = 15; +const maxColumns = 10; +const maxNumSize = 8; -/** - * @class SingularValueDecomposition - * @see https://github.com/accord-net/framework/blob/development/Sources/Accord.Math/Decompositions/SingularValueDecomposition.cs - * @param {Matrix} value - * @param {object} [options] - * @param {boolean} [options.computeLeftSingularVectors=true] - * @param {boolean} [options.computeRightSingularVectors=true] - * @param {boolean} [options.autoTranspose=false] - */ -class svd_SingularValueDecomposition { - constructor(value, options = {}) { - value = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(value); +function inspectData(matrix, indent) { + const { rows, columns } = matrix; + const maxI = Math.min(rows, maxRows); + const maxJ = Math.min(columns, maxColumns); + const result = []; + for (let i = 0; i < maxI; i++) { + let line = []; + for (let j = 0; j < maxJ; j++) { + line.push(formatNumber(matrix.get(i, j))); + } + result.push(`${line.join(' ')}`); + } + if (maxJ !== columns) { + result[result.length - 1] += ` ... ${columns - maxColumns} more columns`; + } + if (maxI !== rows) { + result.push(`... ${rows - maxRows} more rows`); + } + return result.join(`\n${indent}`); +} - var m = value.rows; - var n = value.columns; +function formatNumber(num) { + const numStr = String(num); + if (numStr.length <= maxNumSize) { + return numStr.padEnd(maxNumSize, ' '); + } + const precise = num.toPrecision(maxNumSize - 2); + if (precise.length <= maxNumSize) { + return precise; + } + const exponential = num.toExponential(maxNumSize - 2); + const eIndex = exponential.indexOf('e'); + const e = exponential.substring(eIndex); + return exponential.substring(0, maxNumSize - e.length) + e; +} - const { - computeLeftSingularVectors = true, - computeRightSingularVectors = true, - autoTranspose = false - } = options; +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/mathOperations.js +function installMathOperations(AbstractMatrix, Matrix) { + AbstractMatrix.prototype.add = function add(value) { + if (typeof value === 'number') return this.addS(value); + return this.addM(value); + }; - var wantu = Boolean(computeLeftSingularVectors); - var wantv = Boolean(computeRightSingularVectors); + AbstractMatrix.prototype.addS = function addS(value) { + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) + value); + } + } + return this; + }; - var swapped = false; - var a; - if (m < n) { - if (!autoTranspose) { - a = value.clone(); - // eslint-disable-next-line no-console - console.warn( - 'Computing SVD on a matrix with more columns than rows. Consider enabling autoTranspose' - ); - } else { - a = value.transpose(); - m = a.rows; - n = a.columns; - swapped = true; - var aux = wantu; - wantu = wantv; - wantv = aux; + AbstractMatrix.prototype.addM = function addM(matrix) { + matrix = Matrix.checkMatrix(matrix); + if (this.rows !== matrix.rows || + this.columns !== matrix.columns) { + throw new RangeError('Matrices dimensions must be equal'); + } + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) + matrix.get(i, j)); } - } else { - a = value.clone(); } + return this; + }; - var nu = Math.min(m, n); - var ni = Math.min(m + 1, n); - var s = new Array(ni); - var U = getFilled2DArray(m, nu, 0); - var V = getFilled2DArray(n, n, 0); + AbstractMatrix.add = function add(matrix, value) { + const newMatrix = new Matrix(matrix); + return newMatrix.add(value); + }; - var e = new Array(n); - var work = new Array(m); + AbstractMatrix.prototype.sub = function sub(value) { + if (typeof value === 'number') return this.subS(value); + return this.subM(value); + }; - var si = new Array(ni); - for (let i = 0; i < ni; i++) si[i] = i; + AbstractMatrix.prototype.subS = function subS(value) { + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) - value); + } + } + return this; + }; - var nct = Math.min(m - 1, n); - var nrt = Math.max(0, Math.min(n - 2, m)); - var mrc = Math.max(nct, nrt); + AbstractMatrix.prototype.subM = function subM(matrix) { + matrix = Matrix.checkMatrix(matrix); + if (this.rows !== matrix.rows || + this.columns !== matrix.columns) { + throw new RangeError('Matrices dimensions must be equal'); + } + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) - matrix.get(i, j)); + } + } + return this; + }; - for (let k = 0; k < mrc; k++) { - if (k < nct) { - s[k] = 0; - for (let i = k; i < m; i++) { - s[k] = hypotenuse(s[k], a[i][k]); - } - if (s[k] !== 0) { - if (a[k][k] < 0) { - s[k] = -s[k]; - } - for (let i = k; i < m; i++) { - a[i][k] /= s[k]; - } - a[k][k] += 1; - } - s[k] = -s[k]; + AbstractMatrix.sub = function sub(matrix, value) { + const newMatrix = new Matrix(matrix); + return newMatrix.sub(value); + }; + AbstractMatrix.prototype.subtract = AbstractMatrix.prototype.sub; + AbstractMatrix.prototype.subtractS = AbstractMatrix.prototype.subS; + AbstractMatrix.prototype.subtractM = AbstractMatrix.prototype.subM; + AbstractMatrix.subtract = AbstractMatrix.sub; + + AbstractMatrix.prototype.mul = function mul(value) { + if (typeof value === 'number') return this.mulS(value); + return this.mulM(value); + }; + + AbstractMatrix.prototype.mulS = function mulS(value) { + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) * value); } + } + return this; + }; - for (let j = k + 1; j < n; j++) { - if (k < nct && s[k] !== 0) { - let t = 0; - for (let i = k; i < m; i++) { - t += a[i][k] * a[i][j]; - } - t = -t / a[k][k]; - for (let i = k; i < m; i++) { - a[i][j] += t * a[i][k]; - } - } - e[j] = a[k][j]; + AbstractMatrix.prototype.mulM = function mulM(matrix) { + matrix = Matrix.checkMatrix(matrix); + if (this.rows !== matrix.rows || + this.columns !== matrix.columns) { + throw new RangeError('Matrices dimensions must be equal'); + } + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) * matrix.get(i, j)); } + } + return this; + }; - if (wantu && k < nct) { - for (let i = k; i < m; i++) { - U[i][k] = a[i][k]; - } + AbstractMatrix.mul = function mul(matrix, value) { + const newMatrix = new Matrix(matrix); + return newMatrix.mul(value); + }; + AbstractMatrix.prototype.multiply = AbstractMatrix.prototype.mul; + AbstractMatrix.prototype.multiplyS = AbstractMatrix.prototype.mulS; + AbstractMatrix.prototype.multiplyM = AbstractMatrix.prototype.mulM; + AbstractMatrix.multiply = AbstractMatrix.mul; + + AbstractMatrix.prototype.div = function div(value) { + if (typeof value === 'number') return this.divS(value); + return this.divM(value); + }; + + AbstractMatrix.prototype.divS = function divS(value) { + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) / value); } + } + return this; + }; - if (k < nrt) { - e[k] = 0; - for (let i = k + 1; i < n; i++) { - e[k] = hypotenuse(e[k], e[i]); - } - if (e[k] !== 0) { - if (e[k + 1] < 0) { - e[k] = 0 - e[k]; - } - for (let i = k + 1; i < n; i++) { - e[i] /= e[k]; - } - e[k + 1] += 1; - } - e[k] = -e[k]; - if (k + 1 < m && e[k] !== 0) { - for (let i = k + 1; i < m; i++) { - work[i] = 0; - } - for (let i = k + 1; i < m; i++) { - for (let j = k + 1; j < n; j++) { - work[i] += e[j] * a[i][j]; - } - } - for (let j = k + 1; j < n; j++) { - let t = -e[j] / e[k + 1]; - for (let i = k + 1; i < m; i++) { - a[i][j] += t * work[i]; - } - } - } - if (wantv) { - for (let i = k + 1; i < n; i++) { - V[i][k] = e[i]; - } - } + AbstractMatrix.prototype.divM = function divM(matrix) { + matrix = Matrix.checkMatrix(matrix); + if (this.rows !== matrix.rows || + this.columns !== matrix.columns) { + throw new RangeError('Matrices dimensions must be equal'); + } + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) / matrix.get(i, j)); } } + return this; + }; - let p = Math.min(n, m + 1); - if (nct < n) { - s[nct] = a[nct][nct]; + AbstractMatrix.div = function div(matrix, value) { + const newMatrix = new Matrix(matrix); + return newMatrix.div(value); + }; + AbstractMatrix.prototype.divide = AbstractMatrix.prototype.div; + AbstractMatrix.prototype.divideS = AbstractMatrix.prototype.divS; + AbstractMatrix.prototype.divideM = AbstractMatrix.prototype.divM; + AbstractMatrix.divide = AbstractMatrix.div; + + AbstractMatrix.prototype.mod = function mod(value) { + if (typeof value === 'number') return this.modS(value); + return this.modM(value); + }; + + AbstractMatrix.prototype.modS = function modS(value) { + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) % value); + } } - if (m < p) { - s[p - 1] = 0; + return this; + }; + + AbstractMatrix.prototype.modM = function modM(matrix) { + matrix = Matrix.checkMatrix(matrix); + if (this.rows !== matrix.rows || + this.columns !== matrix.columns) { + throw new RangeError('Matrices dimensions must be equal'); } - if (nrt + 1 < p) { - e[nrt] = a[nrt][p - 1]; + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) % matrix.get(i, j)); + } } - e[p - 1] = 0; + return this; + }; - if (wantu) { - for (let j = nct; j < nu; j++) { - for (let i = 0; i < m; i++) { - U[i][j] = 0; - } - U[j][j] = 1; + AbstractMatrix.mod = function mod(matrix, value) { + const newMatrix = new Matrix(matrix); + return newMatrix.mod(value); + }; + AbstractMatrix.prototype.modulus = AbstractMatrix.prototype.mod; + AbstractMatrix.prototype.modulusS = AbstractMatrix.prototype.modS; + AbstractMatrix.prototype.modulusM = AbstractMatrix.prototype.modM; + AbstractMatrix.modulus = AbstractMatrix.mod; + + AbstractMatrix.prototype.and = function and(value) { + if (typeof value === 'number') return this.andS(value); + return this.andM(value); + }; + + AbstractMatrix.prototype.andS = function andS(value) { + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) & value); } - for (let k = nct - 1; k >= 0; k--) { - if (s[k] !== 0) { - for (let j = k + 1; j < nu; j++) { - let t = 0; - for (let i = k; i < m; i++) { - t += U[i][k] * U[i][j]; - } - t = -t / U[k][k]; - for (let i = k; i < m; i++) { - U[i][j] += t * U[i][k]; - } - } - for (let i = k; i < m; i++) { - U[i][k] = -U[i][k]; - } - U[k][k] = 1 + U[k][k]; - for (let i = 0; i < k - 1; i++) { - U[i][k] = 0; - } - } else { - for (let i = 0; i < m; i++) { - U[i][k] = 0; - } - U[k][k] = 1; - } + } + return this; + }; + + AbstractMatrix.prototype.andM = function andM(matrix) { + matrix = Matrix.checkMatrix(matrix); + if (this.rows !== matrix.rows || + this.columns !== matrix.columns) { + throw new RangeError('Matrices dimensions must be equal'); + } + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) & matrix.get(i, j)); } } + return this; + }; - if (wantv) { - for (let k = n - 1; k >= 0; k--) { - if (k < nrt && e[k] !== 0) { - for (let j = k + 1; j < n; j++) { - let t = 0; - for (let i = k + 1; i < n; i++) { - t += V[i][k] * V[i][j]; - } - t = -t / V[k + 1][k]; - for (let i = k + 1; i < n; i++) { - V[i][j] += t * V[i][k]; - } - } - } - for (let i = 0; i < n; i++) { - V[i][k] = 0; - } - V[k][k] = 1; + AbstractMatrix.and = function and(matrix, value) { + const newMatrix = new Matrix(matrix); + return newMatrix.and(value); + }; + + AbstractMatrix.prototype.or = function or(value) { + if (typeof value === 'number') return this.orS(value); + return this.orM(value); + }; + + AbstractMatrix.prototype.orS = function orS(value) { + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) | value); } } + return this; + }; - var pp = p - 1; - var iter = 0; - var eps = Number.EPSILON; - while (p > 0) { - let k, kase; - for (k = p - 2; k >= -1; k--) { - if (k === -1) { - break; - } - const alpha = - Number.MIN_VALUE + eps * Math.abs(s[k] + Math.abs(s[k + 1])); - if (Math.abs(e[k]) <= alpha || Number.isNaN(e[k])) { - e[k] = 0; - break; - } + AbstractMatrix.prototype.orM = function orM(matrix) { + matrix = Matrix.checkMatrix(matrix); + if (this.rows !== matrix.rows || + this.columns !== matrix.columns) { + throw new RangeError('Matrices dimensions must be equal'); + } + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) | matrix.get(i, j)); } - if (k === p - 2) { - kase = 4; - } else { - let ks; - for (ks = p - 1; ks >= k; ks--) { - if (ks === k) { - break; - } - let t = - (ks !== p ? Math.abs(e[ks]) : 0) + - (ks !== k + 1 ? Math.abs(e[ks - 1]) : 0); - if (Math.abs(s[ks]) <= eps * t) { - s[ks] = 0; - break; - } - } - if (ks === k) { - kase = 3; - } else if (ks === p - 1) { - kase = 1; - } else { - kase = 2; - k = ks; - } + } + return this; + }; + + AbstractMatrix.or = function or(matrix, value) { + const newMatrix = new Matrix(matrix); + return newMatrix.or(value); + }; + + AbstractMatrix.prototype.xor = function xor(value) { + if (typeof value === 'number') return this.xorS(value); + return this.xorM(value); + }; + + AbstractMatrix.prototype.xorS = function xorS(value) { + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) ^ value); } + } + return this; + }; - k++; + AbstractMatrix.prototype.xorM = function xorM(matrix) { + matrix = Matrix.checkMatrix(matrix); + if (this.rows !== matrix.rows || + this.columns !== matrix.columns) { + throw new RangeError('Matrices dimensions must be equal'); + } + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) ^ matrix.get(i, j)); + } + } + return this; + }; - switch (kase) { - case 1: { - let f = e[p - 2]; - e[p - 2] = 0; - for (let j = p - 2; j >= k; j--) { - let t = hypotenuse(s[j], f); - let cs = s[j] / t; - let sn = f / t; - s[j] = t; - if (j !== k) { - f = -sn * e[j - 1]; - e[j - 1] = cs * e[j - 1]; - } - if (wantv) { - for (let i = 0; i < n; i++) { - t = cs * V[i][j] + sn * V[i][p - 1]; - V[i][p - 1] = -sn * V[i][j] + cs * V[i][p - 1]; - V[i][j] = t; - } - } - } - break; - } - case 2: { - let f = e[k - 1]; - e[k - 1] = 0; - for (let j = k; j < p; j++) { - let t = hypotenuse(s[j], f); - let cs = s[j] / t; - let sn = f / t; - s[j] = t; - f = -sn * e[j]; - e[j] = cs * e[j]; - if (wantu) { - for (let i = 0; i < m; i++) { - t = cs * U[i][j] + sn * U[i][k - 1]; - U[i][k - 1] = -sn * U[i][j] + cs * U[i][k - 1]; - U[i][j] = t; - } - } - } - break; - } - case 3: { - const scale = Math.max( - Math.abs(s[p - 1]), - Math.abs(s[p - 2]), - Math.abs(e[p - 2]), - Math.abs(s[k]), - Math.abs(e[k]) - ); - const sp = s[p - 1] / scale; - const spm1 = s[p - 2] / scale; - const epm1 = e[p - 2] / scale; - const sk = s[k] / scale; - const ek = e[k] / scale; - const b = ((spm1 + sp) * (spm1 - sp) + epm1 * epm1) / 2; - const c = sp * epm1 * (sp * epm1); - let shift = 0; - if (b !== 0 || c !== 0) { - if (b < 0) { - shift = 0 - Math.sqrt(b * b + c); - } else { - shift = Math.sqrt(b * b + c); - } - shift = c / (b + shift); - } - let f = (sk + sp) * (sk - sp) + shift; - let g = sk * ek; - for (let j = k; j < p - 1; j++) { - let t = hypotenuse(f, g); - if (t === 0) t = Number.MIN_VALUE; - let cs = f / t; - let sn = g / t; - if (j !== k) { - e[j - 1] = t; - } - f = cs * s[j] + sn * e[j]; - e[j] = cs * e[j] - sn * s[j]; - g = sn * s[j + 1]; - s[j + 1] = cs * s[j + 1]; - if (wantv) { - for (let i = 0; i < n; i++) { - t = cs * V[i][j] + sn * V[i][j + 1]; - V[i][j + 1] = -sn * V[i][j] + cs * V[i][j + 1]; - V[i][j] = t; - } - } - t = hypotenuse(f, g); - if (t === 0) t = Number.MIN_VALUE; - cs = f / t; - sn = g / t; - s[j] = t; - f = cs * e[j] + sn * s[j + 1]; - s[j + 1] = -sn * e[j] + cs * s[j + 1]; - g = sn * e[j + 1]; - e[j + 1] = cs * e[j + 1]; - if (wantu && j < m - 1) { - for (let i = 0; i < m; i++) { - t = cs * U[i][j] + sn * U[i][j + 1]; - U[i][j + 1] = -sn * U[i][j] + cs * U[i][j + 1]; - U[i][j] = t; - } - } - } - e[p - 2] = f; - iter = iter + 1; - break; - } - case 4: { - if (s[k] <= 0) { - s[k] = s[k] < 0 ? -s[k] : 0; - if (wantv) { - for (let i = 0; i <= pp; i++) { - V[i][k] = -V[i][k]; - } - } - } - while (k < pp) { - if (s[k] >= s[k + 1]) { - break; - } - let t = s[k]; - s[k] = s[k + 1]; - s[k + 1] = t; - if (wantv && k < n - 1) { - for (let i = 0; i < n; i++) { - t = V[i][k + 1]; - V[i][k + 1] = V[i][k]; - V[i][k] = t; - } - } - if (wantu && k < m - 1) { - for (let i = 0; i < m; i++) { - t = U[i][k + 1]; - U[i][k + 1] = U[i][k]; - U[i][k] = t; - } - } - k++; - } - iter = 0; - p--; - break; - } - // no default + AbstractMatrix.xor = function xor(matrix, value) { + const newMatrix = new Matrix(matrix); + return newMatrix.xor(value); + }; + + AbstractMatrix.prototype.leftShift = function leftShift(value) { + if (typeof value === 'number') return this.leftShiftS(value); + return this.leftShiftM(value); + }; + + AbstractMatrix.prototype.leftShiftS = function leftShiftS(value) { + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) << value); } } + return this; + }; - if (swapped) { - var tmp = V; - V = U; - U = tmp; + AbstractMatrix.prototype.leftShiftM = function leftShiftM(matrix) { + matrix = Matrix.checkMatrix(matrix); + if (this.rows !== matrix.rows || + this.columns !== matrix.columns) { + throw new RangeError('Matrices dimensions must be equal'); } + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) << matrix.get(i, j)); + } + } + return this; + }; - this.m = m; - this.n = n; - this.s = s; - this.U = U; - this.V = V; - } + AbstractMatrix.leftShift = function leftShift(matrix, value) { + const newMatrix = new Matrix(matrix); + return newMatrix.leftShift(value); + }; - /** - * Solve a problem of least square (Ax=b) by using the SVD. Useful when A is singular. When A is not singular, it would be better to use qr.solve(value). - * Example : We search to approximate x, with A matrix shape m*n, x vector size n, b vector size m (m > n). We will use : - * var svd = SingularValueDecomposition(A); - * var x = svd.solve(b); - * @param {Matrix} value - Matrix 1D which is the vector b (in the equation Ax = b) - * @return {Matrix} - The vector x - */ - solve(value) { - var Y = value; - var e = this.threshold; - var scols = this.s.length; - var Ls = matrix_Matrix.zeros(scols, scols); + AbstractMatrix.prototype.signPropagatingRightShift = function signPropagatingRightShift(value) { + if (typeof value === 'number') return this.signPropagatingRightShiftS(value); + return this.signPropagatingRightShiftM(value); + }; + + AbstractMatrix.prototype.signPropagatingRightShiftS = function signPropagatingRightShiftS(value) { + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) >> value); + } + } + return this; + }; + + AbstractMatrix.prototype.signPropagatingRightShiftM = function signPropagatingRightShiftM(matrix) { + matrix = Matrix.checkMatrix(matrix); + if (this.rows !== matrix.rows || + this.columns !== matrix.columns) { + throw new RangeError('Matrices dimensions must be equal'); + } + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) >> matrix.get(i, j)); + } + } + return this; + }; + + AbstractMatrix.signPropagatingRightShift = function signPropagatingRightShift(matrix, value) { + const newMatrix = new Matrix(matrix); + return newMatrix.signPropagatingRightShift(value); + }; + + AbstractMatrix.prototype.rightShift = function rightShift(value) { + if (typeof value === 'number') return this.rightShiftS(value); + return this.rightShiftM(value); + }; + + AbstractMatrix.prototype.rightShiftS = function rightShiftS(value) { + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) >>> value); + } + } + return this; + }; + + AbstractMatrix.prototype.rightShiftM = function rightShiftM(matrix) { + matrix = Matrix.checkMatrix(matrix); + if (this.rows !== matrix.rows || + this.columns !== matrix.columns) { + throw new RangeError('Matrices dimensions must be equal'); + } + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) >>> matrix.get(i, j)); + } + } + return this; + }; + + AbstractMatrix.rightShift = function rightShift(matrix, value) { + const newMatrix = new Matrix(matrix); + return newMatrix.rightShift(value); + }; + AbstractMatrix.prototype.zeroFillRightShift = AbstractMatrix.prototype.rightShift; + AbstractMatrix.prototype.zeroFillRightShiftS = AbstractMatrix.prototype.rightShiftS; + AbstractMatrix.prototype.zeroFillRightShiftM = AbstractMatrix.prototype.rightShiftM; + AbstractMatrix.zeroFillRightShift = AbstractMatrix.rightShift; + + AbstractMatrix.prototype.not = function not() { + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, ~(this.get(i, j))); + } + } + return this; + }; + + AbstractMatrix.not = function not(matrix) { + const newMatrix = new Matrix(matrix); + return newMatrix.not(); + }; + + AbstractMatrix.prototype.abs = function abs() { + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, Math.abs(this.get(i, j))); + } + } + return this; + }; + + AbstractMatrix.abs = function abs(matrix) { + const newMatrix = new Matrix(matrix); + return newMatrix.abs(); + }; + + AbstractMatrix.prototype.acos = function acos() { + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, Math.acos(this.get(i, j))); + } + } + return this; + }; + + AbstractMatrix.acos = function acos(matrix) { + const newMatrix = new Matrix(matrix); + return newMatrix.acos(); + }; + + AbstractMatrix.prototype.acosh = function acosh() { + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, Math.acosh(this.get(i, j))); + } + } + return this; + }; + + AbstractMatrix.acosh = function acosh(matrix) { + const newMatrix = new Matrix(matrix); + return newMatrix.acosh(); + }; + + AbstractMatrix.prototype.asin = function asin() { + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, Math.asin(this.get(i, j))); + } + } + return this; + }; + + AbstractMatrix.asin = function asin(matrix) { + const newMatrix = new Matrix(matrix); + return newMatrix.asin(); + }; + + AbstractMatrix.prototype.asinh = function asinh() { + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, Math.asinh(this.get(i, j))); + } + } + return this; + }; + + AbstractMatrix.asinh = function asinh(matrix) { + const newMatrix = new Matrix(matrix); + return newMatrix.asinh(); + }; + + AbstractMatrix.prototype.atan = function atan() { + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, Math.atan(this.get(i, j))); + } + } + return this; + }; + + AbstractMatrix.atan = function atan(matrix) { + const newMatrix = new Matrix(matrix); + return newMatrix.atan(); + }; + + AbstractMatrix.prototype.atanh = function atanh() { + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, Math.atanh(this.get(i, j))); + } + } + return this; + }; + + AbstractMatrix.atanh = function atanh(matrix) { + const newMatrix = new Matrix(matrix); + return newMatrix.atanh(); + }; + + AbstractMatrix.prototype.cbrt = function cbrt() { + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, Math.cbrt(this.get(i, j))); + } + } + return this; + }; + + AbstractMatrix.cbrt = function cbrt(matrix) { + const newMatrix = new Matrix(matrix); + return newMatrix.cbrt(); + }; + + AbstractMatrix.prototype.ceil = function ceil() { + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, Math.ceil(this.get(i, j))); + } + } + return this; + }; + + AbstractMatrix.ceil = function ceil(matrix) { + const newMatrix = new Matrix(matrix); + return newMatrix.ceil(); + }; + + AbstractMatrix.prototype.clz32 = function clz32() { + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, Math.clz32(this.get(i, j))); + } + } + return this; + }; + + AbstractMatrix.clz32 = function clz32(matrix) { + const newMatrix = new Matrix(matrix); + return newMatrix.clz32(); + }; - for (let i = 0; i < scols; i++) { - if (Math.abs(this.s[i]) <= e) { - Ls[i][i] = 0; - } else { - Ls[i][i] = 1 / this.s[i]; + AbstractMatrix.prototype.cos = function cos() { + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, Math.cos(this.get(i, j))); } } + return this; + }; - var U = this.U; - var V = this.rightSingularVectors; - - var VL = V.mmul(Ls); - var vrows = V.rows; - var urows = U.length; - var VLU = matrix_Matrix.zeros(vrows, urows); + AbstractMatrix.cos = function cos(matrix) { + const newMatrix = new Matrix(matrix); + return newMatrix.cos(); + }; - for (let i = 0; i < vrows; i++) { - for (let j = 0; j < urows; j++) { - let sum = 0; - for (let k = 0; k < scols; k++) { - sum += VL[i][k] * U[j][k]; - } - VLU[i][j] = sum; + AbstractMatrix.prototype.cosh = function cosh() { + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, Math.cosh(this.get(i, j))); } } + return this; + }; - return VLU.mmul(Y); - } + AbstractMatrix.cosh = function cosh(matrix) { + const newMatrix = new Matrix(matrix); + return newMatrix.cosh(); + }; - /** - * - * @param {Array} value - * @return {Matrix} - */ - solveForDiagonal(value) { - return this.solve(matrix_Matrix.diag(value)); - } + AbstractMatrix.prototype.exp = function exp() { + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, Math.exp(this.get(i, j))); + } + } + return this; + }; - /** - * Get the inverse of the matrix. We compute the inverse of a matrix using SVD when this matrix is singular or ill-conditioned. Example : - * var svd = SingularValueDecomposition(A); - * var inverseA = svd.inverse(); - * @return {Matrix} - The approximation of the inverse of the matrix - */ - inverse() { - var V = this.V; - var e = this.threshold; - var vrows = V.length; - var vcols = V[0].length; - var X = new matrix_Matrix(vrows, this.s.length); + AbstractMatrix.exp = function exp(matrix) { + const newMatrix = new Matrix(matrix); + return newMatrix.exp(); + }; - for (let i = 0; i < vrows; i++) { - for (let j = 0; j < vcols; j++) { - if (Math.abs(this.s[j]) > e) { - X[i][j] = V[i][j] / this.s[j]; - } else { - X[i][j] = 0; - } + AbstractMatrix.prototype.expm1 = function expm1() { + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, Math.expm1(this.get(i, j))); } } + return this; + }; - var U = this.U; + AbstractMatrix.expm1 = function expm1(matrix) { + const newMatrix = new Matrix(matrix); + return newMatrix.expm1(); + }; - var urows = U.length; - var ucols = U[0].length; - var Y = new matrix_Matrix(vrows, urows); + AbstractMatrix.prototype.floor = function floor() { + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, Math.floor(this.get(i, j))); + } + } + return this; + }; - for (let i = 0; i < vrows; i++) { - for (let j = 0; j < urows; j++) { - let sum = 0; - for (let k = 0; k < ucols; k++) { - sum += X[i][k] * U[j][k]; - } - Y[i][j] = sum; + AbstractMatrix.floor = function floor(matrix) { + const newMatrix = new Matrix(matrix); + return newMatrix.floor(); + }; + + AbstractMatrix.prototype.fround = function fround() { + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, Math.fround(this.get(i, j))); } } + return this; + }; - return Y; - } + AbstractMatrix.fround = function fround(matrix) { + const newMatrix = new Matrix(matrix); + return newMatrix.fround(); + }; - /** - * - * @return {number} - */ - get condition() { - return this.s[0] / this.s[Math.min(this.m, this.n) - 1]; - } + AbstractMatrix.prototype.log = function log() { + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, Math.log(this.get(i, j))); + } + } + return this; + }; - /** - * - * @return {number} - */ - get norm2() { - return this.s[0]; - } + AbstractMatrix.log = function log(matrix) { + const newMatrix = new Matrix(matrix); + return newMatrix.log(); + }; - /** - * - * @return {number} - */ - get rank() { - var tol = Math.max(this.m, this.n) * this.s[0] * Number.EPSILON; - var r = 0; - var s = this.s; - for (var i = 0, ii = s.length; i < ii; i++) { - if (s[i] > tol) { - r++; + AbstractMatrix.prototype.log1p = function log1p() { + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, Math.log1p(this.get(i, j))); } } - return r; - } + return this; + }; - /** - * - * @return {Array} - */ - get diagonal() { - return this.s; - } + AbstractMatrix.log1p = function log1p(matrix) { + const newMatrix = new Matrix(matrix); + return newMatrix.log1p(); + }; - /** - * - * @return {number} - */ - get threshold() { - return Number.EPSILON / 2 * Math.max(this.m, this.n) * this.s[0]; - } + AbstractMatrix.prototype.log10 = function log10() { + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, Math.log10(this.get(i, j))); + } + } + return this; + }; - /** - * - * @return {Matrix} - */ - get leftSingularVectors() { - if (!matrix_Matrix.isMatrix(this.U)) { - this.U = new matrix_Matrix(this.U); + AbstractMatrix.log10 = function log10(matrix) { + const newMatrix = new Matrix(matrix); + return newMatrix.log10(); + }; + + AbstractMatrix.prototype.log2 = function log2() { + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, Math.log2(this.get(i, j))); + } } - return this.U; - } + return this; + }; - /** - * - * @return {Matrix} - */ - get rightSingularVectors() { - if (!matrix_Matrix.isMatrix(this.V)) { - this.V = new matrix_Matrix(this.V); + AbstractMatrix.log2 = function log2(matrix) { + const newMatrix = new Matrix(matrix); + return newMatrix.log2(); + }; + + AbstractMatrix.prototype.round = function round() { + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, Math.round(this.get(i, j))); + } } - return this.V; - } + return this; + }; - /** - * - * @return {Matrix} - */ - get diagonalMatrix() { - return matrix_Matrix.diag(this.s); - } -} + AbstractMatrix.round = function round(matrix) { + const newMatrix = new Matrix(matrix); + return newMatrix.round(); + }; -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/util.js + AbstractMatrix.prototype.sign = function sign() { + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, Math.sign(this.get(i, j))); + } + } + return this; + }; + AbstractMatrix.sign = function sign(matrix) { + const newMatrix = new Matrix(matrix); + return newMatrix.sign(); + }; -/** - * @private - * Check that a row index is not out of bounds - * @param {Matrix} matrix - * @param {number} index - * @param {boolean} [outer] - */ -function checkRowIndex(matrix, index, outer) { - var max = outer ? matrix.rows : matrix.rows - 1; - if (index < 0 || index > max) { - throw new RangeError('Row index out of range'); - } -} + AbstractMatrix.prototype.sin = function sin() { + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, Math.sin(this.get(i, j))); + } + } + return this; + }; -/** - * @private - * Check that a column index is not out of bounds - * @param {Matrix} matrix - * @param {number} index - * @param {boolean} [outer] - */ -function checkColumnIndex(matrix, index, outer) { - var max = outer ? matrix.columns : matrix.columns - 1; - if (index < 0 || index > max) { - throw new RangeError('Column index out of range'); - } -} + AbstractMatrix.sin = function sin(matrix) { + const newMatrix = new Matrix(matrix); + return newMatrix.sin(); + }; -/** - * @private - * Check that the provided vector is an array with the right length - * @param {Matrix} matrix - * @param {Array|Matrix} vector - * @return {Array} - * @throws {RangeError} - */ -function checkRowVector(matrix, vector) { - if (vector.to1DArray) { - vector = vector.to1DArray(); - } - if (vector.length !== matrix.columns) { - throw new RangeError( - 'vector size must be the same as the number of columns' - ); - } - return vector; -} + AbstractMatrix.prototype.sinh = function sinh() { + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, Math.sinh(this.get(i, j))); + } + } + return this; + }; -/** - * @private - * Check that the provided vector is an array with the right length - * @param {Matrix} matrix - * @param {Array|Matrix} vector - * @return {Array} - * @throws {RangeError} - */ -function checkColumnVector(matrix, vector) { - if (vector.to1DArray) { - vector = vector.to1DArray(); - } - if (vector.length !== matrix.rows) { - throw new RangeError('vector size must be the same as the number of rows'); - } - return vector; -} + AbstractMatrix.sinh = function sinh(matrix) { + const newMatrix = new Matrix(matrix); + return newMatrix.sinh(); + }; -function checkIndices(matrix, rowIndices, columnIndices) { - return { - row: checkRowIndices(matrix, rowIndices), - column: checkColumnIndices(matrix, columnIndices) + AbstractMatrix.prototype.sqrt = function sqrt() { + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, Math.sqrt(this.get(i, j))); + } + } + return this; }; -} -function checkRowIndices(matrix, rowIndices) { - if (typeof rowIndices !== 'object') { - throw new TypeError('unexpected type for row indices'); - } + AbstractMatrix.sqrt = function sqrt(matrix) { + const newMatrix = new Matrix(matrix); + return newMatrix.sqrt(); + }; - var rowOut = rowIndices.some((r) => { - return r < 0 || r >= matrix.rows; - }); + AbstractMatrix.prototype.tan = function tan() { + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, Math.tan(this.get(i, j))); + } + } + return this; + }; - if (rowOut) { - throw new RangeError('row indices are out of range'); - } + AbstractMatrix.tan = function tan(matrix) { + const newMatrix = new Matrix(matrix); + return newMatrix.tan(); + }; - if (!Array.isArray(rowIndices)) rowIndices = Array.from(rowIndices); + AbstractMatrix.prototype.tanh = function tanh() { + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, Math.tanh(this.get(i, j))); + } + } + return this; + }; - return rowIndices; -} + AbstractMatrix.tanh = function tanh(matrix) { + const newMatrix = new Matrix(matrix); + return newMatrix.tanh(); + }; -function checkColumnIndices(matrix, columnIndices) { - if (typeof columnIndices !== 'object') { - throw new TypeError('unexpected type for column indices'); - } + AbstractMatrix.prototype.trunc = function trunc() { + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, Math.trunc(this.get(i, j))); + } + } + return this; + }; - var columnOut = columnIndices.some((c) => { - return c < 0 || c >= matrix.columns; - }); + AbstractMatrix.trunc = function trunc(matrix) { + const newMatrix = new Matrix(matrix); + return newMatrix.trunc(); + }; - if (columnOut) { - throw new RangeError('column indices are out of range'); - } - if (!Array.isArray(columnIndices)) columnIndices = Array.from(columnIndices); + AbstractMatrix.pow = function pow(matrix, arg0) { + const newMatrix = new Matrix(matrix); + return newMatrix.pow(arg0); + }; - return columnIndices; -} + AbstractMatrix.prototype.pow = function pow(value) { + if (typeof value === 'number') return this.powS(value); + return this.powM(value); + }; -function checkRange(matrix, startRow, endRow, startColumn, endColumn) { - if (arguments.length !== 5) { - throw new RangeError('expected 4 arguments'); - } - checkNumber('startRow', startRow); - checkNumber('endRow', endRow); - checkNumber('startColumn', startColumn); - checkNumber('endColumn', endColumn); - if ( - startRow > endRow || - startColumn > endColumn || - startRow < 0 || - startRow >= matrix.rows || - endRow < 0 || - endRow >= matrix.rows || - startColumn < 0 || - startColumn >= matrix.columns || - endColumn < 0 || - endColumn >= matrix.columns - ) { - throw new RangeError('Submatrix indices are out of range'); - } + AbstractMatrix.prototype.powS = function powS(value) { + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, Math.pow(this.get(i, j), value)); + } + } + return this; + }; + + AbstractMatrix.prototype.powM = function powM(matrix) { + matrix = Matrix.checkMatrix(matrix); + if (this.rows !== matrix.rows || + this.columns !== matrix.columns) { + throw new RangeError('Matrices dimensions must be equal'); + } + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, Math.pow(this.get(i, j), matrix.get(i, j))); + } + } + return this; + }; } -function getRange(from, to) { - var arr = new Array(to - from + 1); - for (var i = 0; i < arr.length; i++) { - arr[i] = from + i; - } - return arr; -} +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/matrix.js -function sumByRow(matrix) { - var sum = matrix_Matrix.zeros(matrix.rows, 1); - for (var i = 0; i < matrix.rows; ++i) { - for (var j = 0; j < matrix.columns; ++j) { - sum.set(i, 0, sum.get(i, 0) + matrix.get(i, j)); + + + + + + +class matrix_AbstractMatrix { + static from1DArray(newRows, newColumns, newData) { + let length = newRows * newColumns; + if (length !== newData.length) { + throw new RangeError('data length does not match given dimensions'); + } + let newMatrix = new matrix_Matrix(newRows, newColumns); + for (let row = 0; row < newRows; row++) { + for (let column = 0; column < newColumns; column++) { + newMatrix.set(row, column, newData[row * newColumns + column]); + } } + return newMatrix; } - return sum; -} -function sumByColumn(matrix) { - var sum = matrix_Matrix.zeros(1, matrix.columns); - for (var i = 0; i < matrix.rows; ++i) { - for (var j = 0; j < matrix.columns; ++j) { - sum.set(0, j, sum.get(0, j) + matrix.get(i, j)); + static rowVector(newData) { + let vector = new matrix_Matrix(1, newData.length); + for (let i = 0; i < newData.length; i++) { + vector.set(0, i, newData[i]); } + return vector; } - return sum; -} -function sumAll(matrix) { - var v = 0; - for (var i = 0; i < matrix.rows; i++) { - for (var j = 0; j < matrix.columns; j++) { - v += matrix.get(i, j); + static columnVector(newData) { + let vector = new matrix_Matrix(newData.length, 1); + for (let i = 0; i < newData.length; i++) { + vector.set(i, 0, newData[i]); } + return vector; } - return v; -} -function checkNumber(name, value) { - if (typeof value !== 'number') { - throw new TypeError(`${name} must be a number`); + static zeros(rows, columns) { + return new matrix_Matrix(rows, columns); } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/base.js - - -class base_BaseView extends AbstractMatrix() { - constructor(matrix, rows, columns) { - super(); - this.matrix = matrix; - this.rows = rows; - this.columns = columns; + static ones(rows, columns) { + return new matrix_Matrix(rows, columns).fill(1); } - static get [Symbol.species]() { - return matrix_Matrix; + static rand(rows, columns, options = {}) { + if (typeof options !== 'object') { + throw new TypeError('options must be an object'); + } + const { random = Math.random } = options; + let matrix = new matrix_Matrix(rows, columns); + for (let i = 0; i < rows; i++) { + for (let j = 0; j < columns; j++) { + matrix.set(i, j, random()); + } + } + return matrix; } -} -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/transpose.js + static randInt(rows, columns, options = {}) { + if (typeof options !== 'object') { + throw new TypeError('options must be an object'); + } + const { min = 0, max = 1000, random = Math.random } = options; + if (!Number.isInteger(min)) throw new TypeError('min must be an integer'); + if (!Number.isInteger(max)) throw new TypeError('max must be an integer'); + if (min >= max) throw new RangeError('min must be smaller than max'); + let interval = max - min; + let matrix = new matrix_Matrix(rows, columns); + for (let i = 0; i < rows; i++) { + for (let j = 0; j < columns; j++) { + let value = min + Math.round(random() * interval); + matrix.set(i, j, value); + } + } + return matrix; + } + static eye(rows, columns, value) { + if (columns === undefined) columns = rows; + if (value === undefined) value = 1; + let min = Math.min(rows, columns); + let matrix = this.zeros(rows, columns); + for (let i = 0; i < min; i++) { + matrix.set(i, i, value); + } + return matrix; + } -class transpose_MatrixTransposeView extends base_BaseView { - constructor(matrix) { - super(matrix, matrix.columns, matrix.rows); + static diag(data, rows, columns) { + let l = data.length; + if (rows === undefined) rows = l; + if (columns === undefined) columns = rows; + let min = Math.min(l, rows, columns); + let matrix = this.zeros(rows, columns); + for (let i = 0; i < min; i++) { + matrix.set(i, i, data[i]); + } + return matrix; } - set(rowIndex, columnIndex, value) { - this.matrix.set(columnIndex, rowIndex, value); - return this; + static min(matrix1, matrix2) { + matrix1 = this.checkMatrix(matrix1); + matrix2 = this.checkMatrix(matrix2); + let rows = matrix1.rows; + let columns = matrix1.columns; + let result = new matrix_Matrix(rows, columns); + for (let i = 0; i < rows; i++) { + for (let j = 0; j < columns; j++) { + result.set(i, j, Math.min(matrix1.get(i, j), matrix2.get(i, j))); + } + } + return result; } - get(rowIndex, columnIndex) { - return this.matrix.get(columnIndex, rowIndex); + static max(matrix1, matrix2) { + matrix1 = this.checkMatrix(matrix1); + matrix2 = this.checkMatrix(matrix2); + let rows = matrix1.rows; + let columns = matrix1.columns; + let result = new this(rows, columns); + for (let i = 0; i < rows; i++) { + for (let j = 0; j < columns; j++) { + result.set(i, j, Math.max(matrix1.get(i, j), matrix2.get(i, j))); + } + } + return result; } -} -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/row.js + static checkMatrix(value) { + return matrix_AbstractMatrix.isMatrix(value) ? value : new matrix_Matrix(value); + } + static isMatrix(value) { + return value != null && value.klass === 'Matrix'; + } -class row_MatrixRowView extends base_BaseView { - constructor(matrix, row) { - super(matrix, 1, matrix.columns); - this.row = row; + get size() { + return this.rows * this.columns; } - set(rowIndex, columnIndex, value) { - this.matrix.set(this.row, columnIndex, value); + apply(callback) { + if (typeof callback !== 'function') { + throw new TypeError('callback must be a function'); + } + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + callback.call(this, i, j); + } + } return this; } - get(rowIndex, columnIndex) { - return this.matrix.get(this.row, columnIndex); + to1DArray() { + let array = []; + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + array.push(this.get(i, j)); + } + } + return array; } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/sub.js - - - -class sub_MatrixSubView extends base_BaseView { - constructor(matrix, startRow, endRow, startColumn, endColumn) { - checkRange(matrix, startRow, endRow, startColumn, endColumn); - super(matrix, endRow - startRow + 1, endColumn - startColumn + 1); - this.startRow = startRow; - this.startColumn = startColumn; + to2DArray() { + let copy = []; + for (let i = 0; i < this.rows; i++) { + copy.push([]); + for (let j = 0; j < this.columns; j++) { + copy[i].push(this.get(i, j)); + } + } + return copy; } - set(rowIndex, columnIndex, value) { - this.matrix.set( - this.startRow + rowIndex, - this.startColumn + columnIndex, - value - ); - return this; + toJSON() { + return this.to2DArray(); } - get(rowIndex, columnIndex) { - return this.matrix.get( - this.startRow + rowIndex, - this.startColumn + columnIndex - ); + isRowVector() { + return this.rows === 1; } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/selection.js - + isColumnVector() { + return this.columns === 1; + } + isVector() { + return this.rows === 1 || this.columns === 1; + } -class selection_MatrixSelectionView extends base_BaseView { - constructor(matrix, rowIndices, columnIndices) { - var indices = checkIndices(matrix, rowIndices, columnIndices); - super(matrix, indices.row.length, indices.column.length); - this.rowIndices = indices.row; - this.columnIndices = indices.column; + isSquare() { + return this.rows === this.columns; } - set(rowIndex, columnIndex, value) { - this.matrix.set( - this.rowIndices[rowIndex], - this.columnIndices[columnIndex], - value - ); - return this; + isSymmetric() { + if (this.isSquare()) { + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j <= i; j++) { + if (this.get(i, j) !== this.get(j, i)) { + return false; + } + } + } + return true; + } + return false; } - get(rowIndex, columnIndex) { - return this.matrix.get( - this.rowIndices[rowIndex], - this.columnIndices[columnIndex] - ); + isEchelonForm() { + let i = 0; + let j = 0; + let previousColumn = -1; + let isEchelonForm = true; + let checked = false; + while (i < this.rows && isEchelonForm) { + j = 0; + checked = false; + while (j < this.columns && checked === false) { + if (this.get(i, j) === 0) { + j++; + } else if (this.get(i, j) === 1 && j > previousColumn) { + checked = true; + previousColumn = j; + } else { + isEchelonForm = false; + checked = true; + } + } + i++; + } + return isEchelonForm; + } + + isReducedEchelonForm() { + let i = 0; + let j = 0; + let previousColumn = -1; + let isReducedEchelonForm = true; + let checked = false; + while (i < this.rows && isReducedEchelonForm) { + j = 0; + checked = false; + while (j < this.columns && checked === false) { + if (this.get(i, j) === 0) { + j++; + } else if (this.get(i, j) === 1 && j > previousColumn) { + checked = true; + previousColumn = j; + } else { + isReducedEchelonForm = false; + checked = true; + } + } + for (let k = j + 1; k < this.rows; k++) { + if (this.get(i, k) !== 0) { + isReducedEchelonForm = false; + } + } + i++; + } + return isReducedEchelonForm; } -} -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/rowSelection.js + echelonForm() { + let result = this.clone(); + let h = 0; + let k = 0; + while (h < result.rows && k < result.columns) { + let iMax = h; + for (let i = h; i < result.rows; i++) { + if (result.get(i, k) > result.get(iMax, k)) { + iMax = i; + } + } + if (result.get(iMax, k) === 0) { + k++; + } else { + result.swapRows(h, iMax); + let tmp = result.get(h, k); + for (let j = k; j < result.columns; j++) { + result.set(h, j, result.get(h, j) / tmp); + } + for (let i = h + 1; i < result.rows; i++) { + let factor = result.get(i, k) / result.get(h, k); + result.set(i, k, 0); + for (let j = k + 1; j < result.columns; j++) { + result.set(i, j, result.get(i, j) - result.get(h, j) * factor); + } + } + h++; + k++; + } + } + return result; + } + reducedEchelonForm() { + let result = this.echelonForm(); + let m = result.columns; + let n = result.rows; + let h = n - 1; + while (h >= 0) { + if (result.maxRow(h) === 0) { + h--; + } else { + let p = 0; + let pivot = false; + while (p < n && pivot === false) { + if (result.get(h, p) === 1) { + pivot = true; + } else { + p++; + } + } + for (let i = 0; i < h; i++) { + let factor = result.get(i, p); + for (let j = p; j < m; j++) { + let tmp = result.get(i, j) - factor * result.get(h, j); + result.set(i, j, tmp); + } + } + h--; + } + } + return result; + } + set() { + throw new Error('set method is unimplemented'); + } + get() { + throw new Error('get method is unimplemented'); + } -class rowSelection_MatrixRowSelectionView extends base_BaseView { - constructor(matrix, rowIndices) { - rowIndices = checkRowIndices(matrix, rowIndices); - super(matrix, rowIndices.length, matrix.columns); - this.rowIndices = rowIndices; + repeat(options = {}) { + if (typeof options !== 'object') { + throw new TypeError('options must be an object'); + } + const { rows = 1, columns = 1 } = options; + if (!Number.isInteger(rows) || rows <= 0) { + throw new TypeError('rows must be a positive integer'); + } + if (!Number.isInteger(columns) || columns <= 0) { + throw new TypeError('columns must be a positive integer'); + } + let matrix = new matrix_Matrix(this.rows * rows, this.columns * columns); + for (let i = 0; i < rows; i++) { + for (let j = 0; j < columns; j++) { + matrix.setSubMatrix(this, this.rows * i, this.columns * j); + } + } + return matrix; } - set(rowIndex, columnIndex, value) { - this.matrix.set(this.rowIndices[rowIndex], columnIndex, value); + fill(value) { + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, value); + } + } return this; } - get(rowIndex, columnIndex) { - return this.matrix.get(this.rowIndices[rowIndex], columnIndex); + neg() { + return this.mulS(-1); } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/columnSelection.js - - + getRow(index) { + checkRowIndex(this, index); + let row = []; + for (let i = 0; i < this.columns; i++) { + row.push(this.get(index, i)); + } + return row; + } -class columnSelection_MatrixColumnSelectionView extends base_BaseView { - constructor(matrix, columnIndices) { - columnIndices = checkColumnIndices(matrix, columnIndices); - super(matrix, matrix.rows, columnIndices.length); - this.columnIndices = columnIndices; + getRowVector(index) { + return matrix_Matrix.rowVector(this.getRow(index)); } - set(rowIndex, columnIndex, value) { - this.matrix.set(rowIndex, this.columnIndices[columnIndex], value); + setRow(index, array) { + checkRowIndex(this, index); + array = checkRowVector(this, array); + for (let i = 0; i < this.columns; i++) { + this.set(index, i, array[i]); + } return this; } - get(rowIndex, columnIndex) { - return this.matrix.get(rowIndex, this.columnIndices[columnIndex]); + swapRows(row1, row2) { + checkRowIndex(this, row1); + checkRowIndex(this, row2); + for (let i = 0; i < this.columns; i++) { + let temp = this.get(row1, i); + this.set(row1, i, this.get(row2, i)); + this.set(row2, i, temp); + } + return this; } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/column.js + getColumn(index) { + checkColumnIndex(this, index); + let column = []; + for (let i = 0; i < this.rows; i++) { + column.push(this.get(i, index)); + } + return column; + } -class column_MatrixColumnView extends base_BaseView { - constructor(matrix, column) { - super(matrix, matrix.rows, 1); - this.column = column; + getColumnVector(index) { + return matrix_Matrix.columnVector(this.getColumn(index)); } - set(rowIndex, columnIndex, value) { - this.matrix.set(rowIndex, this.column, value); + setColumn(index, array) { + checkColumnIndex(this, index); + array = checkColumnVector(this, array); + for (let i = 0; i < this.rows; i++) { + this.set(i, index, array[i]); + } return this; } - get(rowIndex) { - return this.matrix.get(rowIndex, this.column); + swapColumns(column1, column2) { + checkColumnIndex(this, column1); + checkColumnIndex(this, column2); + for (let i = 0; i < this.rows; i++) { + let temp = this.get(i, column1); + this.set(i, column1, this.get(i, column2)); + this.set(i, column2, temp); + } + return this; } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/flipRow.js - -class flipRow_MatrixFlipRowView extends base_BaseView { - constructor(matrix) { - super(matrix, matrix.rows, matrix.columns); + addRowVector(vector) { + vector = checkRowVector(this, vector); + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) + vector[j]); + } + } + return this; } - set(rowIndex, columnIndex, value) { - this.matrix.set(this.rows - rowIndex - 1, columnIndex, value); + subRowVector(vector) { + vector = checkRowVector(this, vector); + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) - vector[j]); + } + } return this; } - get(rowIndex, columnIndex) { - return this.matrix.get(this.rows - rowIndex - 1, columnIndex); + mulRowVector(vector) { + vector = checkRowVector(this, vector); + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) * vector[j]); + } + } + return this; } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/views/flipColumn.js - -class flipColumn_MatrixFlipColumnView extends base_BaseView { - constructor(matrix) { - super(matrix, matrix.rows, matrix.columns); + divRowVector(vector) { + vector = checkRowVector(this, vector); + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) / vector[j]); + } + } + return this; } - set(rowIndex, columnIndex, value) { - this.matrix.set(rowIndex, this.columns - columnIndex - 1, value); + addColumnVector(vector) { + vector = checkColumnVector(this, vector); + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) + vector[i]); + } + } return this; } - get(rowIndex, columnIndex) { - return this.matrix.get(rowIndex, this.columns - columnIndex - 1); + subColumnVector(vector) { + vector = checkColumnVector(this, vector); + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) - vector[i]); + } + } + return this; } -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/abstractMatrix.js - - - - - - - - - - - - + mulColumnVector(vector) { + vector = checkColumnVector(this, vector); + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) * vector[i]); + } + } + return this; + } + divColumnVector(vector) { + vector = checkColumnVector(this, vector); + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + this.set(i, j, this.get(i, j) / vector[i]); + } + } + return this; + } -function AbstractMatrix(superCtor) { - if (superCtor === undefined) superCtor = Object; + mulRow(index, value) { + checkRowIndex(this, index); + for (let i = 0; i < this.columns; i++) { + this.set(index, i, this.get(index, i) * value); + } + return this; + } - /** - * Real matrix - * @class Matrix - * @param {number|Array|Matrix} nRows - Number of rows of the new matrix, - * 2D array containing the data or Matrix instance to clone - * @param {number} [nColumns] - Number of columns of the new matrix - */ - class Matrix extends superCtor { - static get [Symbol.species]() { - return this; + mulColumn(index, value) { + checkColumnIndex(this, index); + for (let i = 0; i < this.rows; i++) { + this.set(i, index, this.get(i, index) * value); } + return this; + } - /** - * Constructs a Matrix with the chosen dimensions from a 1D array - * @param {number} newRows - Number of rows - * @param {number} newColumns - Number of columns - * @param {Array} newData - A 1D array containing data for the matrix - * @return {Matrix} - The new matrix - */ - static from1DArray(newRows, newColumns, newData) { - var length = newRows * newColumns; - if (length !== newData.length) { - throw new RangeError('Data length does not match given dimensions'); - } - var newMatrix = new this(newRows, newColumns); - for (var row = 0; row < newRows; row++) { - for (var column = 0; column < newColumns; column++) { - newMatrix.set(row, column, newData[row * newColumns + column]); + max() { + let v = this.get(0, 0); + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + if (this.get(i, j) > v) { + v = this.get(i, j); } } - return newMatrix; } + return v; + } - /** - * Creates a row vector, a matrix with only one row. - * @param {Array} newData - A 1D array containing data for the vector - * @return {Matrix} - The new matrix - */ - static rowVector(newData) { - var vector = new this(1, newData.length); - for (var i = 0; i < newData.length; i++) { - vector.set(0, i, newData[i]); + maxIndex() { + let v = this.get(0, 0); + let idx = [0, 0]; + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + if (this.get(i, j) > v) { + v = this.get(i, j); + idx[0] = i; + idx[1] = j; + } } - return vector; } + return idx; + } - /** - * Creates a column vector, a matrix with only one column. - * @param {Array} newData - A 1D array containing data for the vector - * @return {Matrix} - The new matrix - */ - static columnVector(newData) { - var vector = new this(newData.length, 1); - for (var i = 0; i < newData.length; i++) { - vector.set(i, 0, newData[i]); + min() { + let v = this.get(0, 0); + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + if (this.get(i, j) < v) { + v = this.get(i, j); + } } - return vector; } + return v; + } - /** - * Creates an empty matrix with the given dimensions. Values will be undefined. Same as using new Matrix(rows, columns). - * @param {number} rows - Number of rows - * @param {number} columns - Number of columns - * @return {Matrix} - The new matrix - */ - static empty(rows, columns) { - return new this(rows, columns); + minIndex() { + let v = this.get(0, 0); + let idx = [0, 0]; + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + if (this.get(i, j) < v) { + v = this.get(i, j); + idx[0] = i; + idx[1] = j; + } + } } + return idx; + } - /** - * Creates a matrix with the given dimensions. Values will be set to zero. - * @param {number} rows - Number of rows - * @param {number} columns - Number of columns - * @return {Matrix} - The new matrix - */ - static zeros(rows, columns) { - return this.empty(rows, columns).fill(0); + maxRow(row) { + checkRowIndex(this, row); + let v = this.get(row, 0); + for (let i = 1; i < this.columns; i++) { + if (this.get(row, i) > v) { + v = this.get(row, i); + } } + return v; + } - /** - * Creates a matrix with the given dimensions. Values will be set to one. - * @param {number} rows - Number of rows - * @param {number} columns - Number of columns - * @return {Matrix} - The new matrix - */ - static ones(rows, columns) { - return this.empty(rows, columns).fill(1); - } - - /** - * Creates a matrix with the given dimensions. Values will be randomly set. - * @param {number} rows - Number of rows - * @param {number} columns - Number of columns - * @param {function} [rng=Math.random] - Random number generator - * @return {Matrix} The new matrix - */ - static rand(rows, columns, rng) { - if (rng === undefined) rng = Math.random; - var matrix = this.empty(rows, columns); - for (var i = 0; i < rows; i++) { - for (var j = 0; j < columns; j++) { - matrix.set(i, j, rng()); - } - } - return matrix; - } - - /** - * Creates a matrix with the given dimensions. Values will be random integers. - * @param {number} rows - Number of rows - * @param {number} columns - Number of columns - * @param {number} [maxValue=1000] - Maximum value - * @param {function} [rng=Math.random] - Random number generator - * @return {Matrix} The new matrix - */ - static randInt(rows, columns, maxValue, rng) { - if (maxValue === undefined) maxValue = 1000; - if (rng === undefined) rng = Math.random; - var matrix = this.empty(rows, columns); - for (var i = 0; i < rows; i++) { - for (var j = 0; j < columns; j++) { - var value = Math.floor(rng() * maxValue); - matrix.set(i, j, value); - } - } - return matrix; - } - - /** - * Creates an identity matrix with the given dimension. Values of the diagonal will be 1 and others will be 0. - * @param {number} rows - Number of rows - * @param {number} [columns=rows] - Number of columns - * @param {number} [value=1] - Value to fill the diagonal with - * @return {Matrix} - The new identity matrix - */ - static eye(rows, columns, value) { - if (columns === undefined) columns = rows; - if (value === undefined) value = 1; - var min = Math.min(rows, columns); - var matrix = this.zeros(rows, columns); - for (var i = 0; i < min; i++) { - matrix.set(i, i, value); - } - return matrix; - } - - /** - * Creates a diagonal matrix based on the given array. - * @param {Array} data - Array containing the data for the diagonal - * @param {number} [rows] - Number of rows (Default: data.length) - * @param {number} [columns] - Number of columns (Default: rows) - * @return {Matrix} - The new diagonal matrix - */ - static diag(data, rows, columns) { - var l = data.length; - if (rows === undefined) rows = l; - if (columns === undefined) columns = rows; - var min = Math.min(l, rows, columns); - var matrix = this.zeros(rows, columns); - for (var i = 0; i < min; i++) { - matrix.set(i, i, data[i]); - } - return matrix; - } - - /** - * Returns a matrix whose elements are the minimum between matrix1 and matrix2 - * @param {Matrix} matrix1 - * @param {Matrix} matrix2 - * @return {Matrix} - */ - static min(matrix1, matrix2) { - matrix1 = this.checkMatrix(matrix1); - matrix2 = this.checkMatrix(matrix2); - var rows = matrix1.rows; - var columns = matrix1.columns; - var result = new this(rows, columns); - for (var i = 0; i < rows; i++) { - for (var j = 0; j < columns; j++) { - result.set(i, j, Math.min(matrix1.get(i, j), matrix2.get(i, j))); - } - } - return result; - } - - /** - * Returns a matrix whose elements are the maximum between matrix1 and matrix2 - * @param {Matrix} matrix1 - * @param {Matrix} matrix2 - * @return {Matrix} - */ - static max(matrix1, matrix2) { - matrix1 = this.checkMatrix(matrix1); - matrix2 = this.checkMatrix(matrix2); - var rows = matrix1.rows; - var columns = matrix1.columns; - var result = new this(rows, columns); - for (var i = 0; i < rows; i++) { - for (var j = 0; j < columns; j++) { - result.set(i, j, Math.max(matrix1.get(i, j), matrix2.get(i, j))); - } - } - return result; - } - - /** - * Check that the provided value is a Matrix and tries to instantiate one if not - * @param {*} value - The value to check - * @return {Matrix} - */ - static checkMatrix(value) { - return Matrix.isMatrix(value) ? value : new this(value); - } - - /** - * Returns true if the argument is a Matrix, false otherwise - * @param {*} value - The value to check - * @return {boolean} - */ - static isMatrix(value) { - return (value != null) && (value.klass === 'Matrix'); - } - - /** - * @prop {number} size - The number of elements in the matrix. - */ - get size() { - return this.rows * this.columns; - } - - /** - * Applies a callback for each element of the matrix. The function is called in the matrix (this) context. - * @param {function} callback - Function that will be called with two parameters : i (row) and j (column) - * @return {Matrix} this - */ - apply(callback) { - if (typeof callback !== 'function') { - throw new TypeError('callback must be a function'); - } - var ii = this.rows; - var jj = this.columns; - for (var i = 0; i < ii; i++) { - for (var j = 0; j < jj; j++) { - callback.call(this, i, j); - } - } - return this; - } - - /** - * Returns a new 1D array filled row by row with the matrix values - * @return {Array} - */ - to1DArray() { - var array = new Array(this.size); - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - array[i * this.columns + j] = this.get(i, j); - } - } - return array; - } - - /** - * Returns a 2D array containing a copy of the data - * @return {Array} - */ - to2DArray() { - var copy = new Array(this.rows); - for (var i = 0; i < this.rows; i++) { - copy[i] = new Array(this.columns); - for (var j = 0; j < this.columns; j++) { - copy[i][j] = this.get(i, j); - } + maxRowIndex(row) { + checkRowIndex(this, row); + let v = this.get(row, 0); + let idx = [row, 0]; + for (let i = 1; i < this.columns; i++) { + if (this.get(row, i) > v) { + v = this.get(row, i); + idx[1] = i; } - return copy; } + return idx; + } - /** - * @return {boolean} true if the matrix has one row - */ - isRowVector() { - return this.rows === 1; + minRow(row) { + checkRowIndex(this, row); + let v = this.get(row, 0); + for (let i = 1; i < this.columns; i++) { + if (this.get(row, i) < v) { + v = this.get(row, i); + } } + return v; + } - /** - * @return {boolean} true if the matrix has one column - */ - isColumnVector() { - return this.columns === 1; + minRowIndex(row) { + checkRowIndex(this, row); + let v = this.get(row, 0); + let idx = [row, 0]; + for (let i = 1; i < this.columns; i++) { + if (this.get(row, i) < v) { + v = this.get(row, i); + idx[1] = i; + } } + return idx; + } - /** - * @return {boolean} true if the matrix has one row or one column - */ - isVector() { - return (this.rows === 1) || (this.columns === 1); + maxColumn(column) { + checkColumnIndex(this, column); + let v = this.get(0, column); + for (let i = 1; i < this.rows; i++) { + if (this.get(i, column) > v) { + v = this.get(i, column); + } } + return v; + } - /** - * @return {boolean} true if the matrix has the same number of rows and columns - */ - isSquare() { - return this.rows === this.columns; + maxColumnIndex(column) { + checkColumnIndex(this, column); + let v = this.get(0, column); + let idx = [0, column]; + for (let i = 1; i < this.rows; i++) { + if (this.get(i, column) > v) { + v = this.get(i, column); + idx[0] = i; + } } + return idx; + } - /** - * @return {boolean} true if the matrix is square and has the same values on both sides of the diagonal - */ - isSymmetric() { - if (this.isSquare()) { - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j <= i; j++) { - if (this.get(i, j) !== this.get(j, i)) { - return false; - } - } - } - return true; + minColumn(column) { + checkColumnIndex(this, column); + let v = this.get(0, column); + for (let i = 1; i < this.rows; i++) { + if (this.get(i, column) < v) { + v = this.get(i, column); } - return false; - } - - /** - * Sets a given element of the matrix. mat.set(3,4,1) is equivalent to mat[3][4]=1 - * @abstract - * @param {number} rowIndex - Index of the row - * @param {number} columnIndex - Index of the column - * @param {number} value - The new value for the element - * @return {Matrix} this - */ - set(rowIndex, columnIndex, value) { // eslint-disable-line no-unused-vars - throw new Error('set method is unimplemented'); - } - - /** - * Returns the given element of the matrix. mat.get(3,4) is equivalent to matrix[3][4] - * @abstract - * @param {number} rowIndex - Index of the row - * @param {number} columnIndex - Index of the column - * @return {number} - */ - get(rowIndex, columnIndex) { // eslint-disable-line no-unused-vars - throw new Error('get method is unimplemented'); - } - - /** - * Creates a new matrix that is a repetition of the current matrix. New matrix has rowRep times the number of - * rows of the matrix, and colRep times the number of columns of the matrix - * @param {number} rowRep - Number of times the rows should be repeated - * @param {number} colRep - Number of times the columns should be re - * @return {Matrix} - * @example - * var matrix = new Matrix([[1,2]]); - * matrix.repeat(2); // [[1,2],[1,2]] - */ - repeat(rowRep, colRep) { - rowRep = rowRep || 1; - colRep = colRep || 1; - var matrix = new this.constructor[Symbol.species](this.rows * rowRep, this.columns * colRep); - for (var i = 0; i < rowRep; i++) { - for (var j = 0; j < colRep; j++) { - matrix.setSubMatrix(this, this.rows * i, this.columns * j); - } - } - return matrix; - } - - /** - * Fills the matrix with a given value. All elements will be set to this value. - * @param {number} value - New value - * @return {Matrix} this - */ - fill(value) { - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, value); - } - } - return this; - } - - /** - * Negates the matrix. All elements will be multiplied by (-1) - * @return {Matrix} this - */ - neg() { - return this.mulS(-1); - } - - /** - * Returns a new array from the given row index - * @param {number} index - Row index - * @return {Array} - */ - getRow(index) { - checkRowIndex(this, index); - var row = new Array(this.columns); - for (var i = 0; i < this.columns; i++) { - row[i] = this.get(index, i); - } - return row; - } - - /** - * Returns a new row vector from the given row index - * @param {number} index - Row index - * @return {Matrix} - */ - getRowVector(index) { - return this.constructor.rowVector(this.getRow(index)); - } - - /** - * Sets a row at the given index - * @param {number} index - Row index - * @param {Array|Matrix} array - Array or vector - * @return {Matrix} this - */ - setRow(index, array) { - checkRowIndex(this, index); - array = checkRowVector(this, array); - for (var i = 0; i < this.columns; i++) { - this.set(index, i, array[i]); - } - return this; - } - - /** - * Swaps two rows - * @param {number} row1 - First row index - * @param {number} row2 - Second row index - * @return {Matrix} this - */ - swapRows(row1, row2) { - checkRowIndex(this, row1); - checkRowIndex(this, row2); - for (var i = 0; i < this.columns; i++) { - var temp = this.get(row1, i); - this.set(row1, i, this.get(row2, i)); - this.set(row2, i, temp); - } - return this; - } - - /** - * Returns a new array from the given column index - * @param {number} index - Column index - * @return {Array} - */ - getColumn(index) { - checkColumnIndex(this, index); - var column = new Array(this.rows); - for (var i = 0; i < this.rows; i++) { - column[i] = this.get(i, index); - } - return column; - } - - /** - * Returns a new column vector from the given column index - * @param {number} index - Column index - * @return {Matrix} - */ - getColumnVector(index) { - return this.constructor.columnVector(this.getColumn(index)); - } - - /** - * Sets a column at the given index - * @param {number} index - Column index - * @param {Array|Matrix} array - Array or vector - * @return {Matrix} this - */ - setColumn(index, array) { - checkColumnIndex(this, index); - array = checkColumnVector(this, array); - for (var i = 0; i < this.rows; i++) { - this.set(i, index, array[i]); - } - return this; - } - - /** - * Swaps two columns - * @param {number} column1 - First column index - * @param {number} column2 - Second column index - * @return {Matrix} this - */ - swapColumns(column1, column2) { - checkColumnIndex(this, column1); - checkColumnIndex(this, column2); - for (var i = 0; i < this.rows; i++) { - var temp = this.get(i, column1); - this.set(i, column1, this.get(i, column2)); - this.set(i, column2, temp); - } - return this; - } - - /** - * Adds the values of a vector to each row - * @param {Array|Matrix} vector - Array or vector - * @return {Matrix} this - */ - addRowVector(vector) { - vector = checkRowVector(this, vector); - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, this.get(i, j) + vector[j]); - } - } - return this; - } - - /** - * Subtracts the values of a vector from each row - * @param {Array|Matrix} vector - Array or vector - * @return {Matrix} this - */ - subRowVector(vector) { - vector = checkRowVector(this, vector); - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, this.get(i, j) - vector[j]); - } - } - return this; - } - - /** - * Multiplies the values of a vector with each row - * @param {Array|Matrix} vector - Array or vector - * @return {Matrix} this - */ - mulRowVector(vector) { - vector = checkRowVector(this, vector); - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, this.get(i, j) * vector[j]); - } - } - return this; - } - - /** - * Divides the values of each row by those of a vector - * @param {Array|Matrix} vector - Array or vector - * @return {Matrix} this - */ - divRowVector(vector) { - vector = checkRowVector(this, vector); - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, this.get(i, j) / vector[j]); - } - } - return this; - } - - /** - * Adds the values of a vector to each column - * @param {Array|Matrix} vector - Array or vector - * @return {Matrix} this - */ - addColumnVector(vector) { - vector = checkColumnVector(this, vector); - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, this.get(i, j) + vector[i]); - } - } - return this; - } - - /** - * Subtracts the values of a vector from each column - * @param {Array|Matrix} vector - Array or vector - * @return {Matrix} this - */ - subColumnVector(vector) { - vector = checkColumnVector(this, vector); - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, this.get(i, j) - vector[i]); - } - } - return this; - } - - /** - * Multiplies the values of a vector with each column - * @param {Array|Matrix} vector - Array or vector - * @return {Matrix} this - */ - mulColumnVector(vector) { - vector = checkColumnVector(this, vector); - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, this.get(i, j) * vector[i]); - } - } - return this; - } - - /** - * Divides the values of each column by those of a vector - * @param {Array|Matrix} vector - Array or vector - * @return {Matrix} this - */ - divColumnVector(vector) { - vector = checkColumnVector(this, vector); - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, this.get(i, j) / vector[i]); - } - } - return this; } + return v; + } - /** - * Multiplies the values of a row with a scalar - * @param {number} index - Row index - * @param {number} value - * @return {Matrix} this - */ - mulRow(index, value) { - checkRowIndex(this, index); - for (var i = 0; i < this.columns; i++) { - this.set(index, i, this.get(index, i) * value); - } - return this; - } - - /** - * Multiplies the values of a column with a scalar - * @param {number} index - Column index - * @param {number} value - * @return {Matrix} this - */ - mulColumn(index, value) { - checkColumnIndex(this, index); - for (var i = 0; i < this.rows; i++) { - this.set(i, index, this.get(i, index) * value); - } - return this; - } - - /** - * Returns the maximum value of the matrix - * @return {number} - */ - max() { - var v = this.get(0, 0); - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - if (this.get(i, j) > v) { - v = this.get(i, j); - } - } + minColumnIndex(column) { + checkColumnIndex(this, column); + let v = this.get(0, column); + let idx = [0, column]; + for (let i = 1; i < this.rows; i++) { + if (this.get(i, column) < v) { + v = this.get(i, column); + idx[0] = i; } - return v; } + return idx; + } - /** - * Returns the index of the maximum value - * @return {Array} - */ - maxIndex() { - var v = this.get(0, 0); - var idx = [0, 0]; - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - if (this.get(i, j) > v) { - v = this.get(i, j); - idx[0] = i; - idx[1] = j; - } - } - } - return idx; + diag() { + let min = Math.min(this.rows, this.columns); + let diag = []; + for (let i = 0; i < min; i++) { + diag.push(this.get(i, i)); } + return diag; + } - /** - * Returns the minimum value of the matrix - * @return {number} - */ - min() { - var v = this.get(0, 0); - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - if (this.get(i, j) < v) { - v = this.get(i, j); - } + norm(type = 'frobenius') { + let result = 0; + if (type === 'max') { + return this.max(); + } else if (type === 'frobenius') { + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + result = result + this.get(i, j) * this.get(i, j); } } - return v; + return Math.sqrt(result); + } else { + throw new RangeError(`unknown norm type: ${type}`); } + } - /** - * Returns the index of the minimum value - * @return {Array} - */ - minIndex() { - var v = this.get(0, 0); - var idx = [0, 0]; - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - if (this.get(i, j) < v) { - v = this.get(i, j); - idx[0] = i; - idx[1] = j; - } - } - } - return idx; - } - - /** - * Returns the maximum value of one row - * @param {number} row - Row index - * @return {number} - */ - maxRow(row) { - checkRowIndex(this, row); - var v = this.get(row, 0); - for (var i = 1; i < this.columns; i++) { - if (this.get(row, i) > v) { - v = this.get(row, i); - } - } - return v; - } - - /** - * Returns the index of the maximum value of one row - * @param {number} row - Row index - * @return {Array} - */ - maxRowIndex(row) { - checkRowIndex(this, row); - var v = this.get(row, 0); - var idx = [row, 0]; - for (var i = 1; i < this.columns; i++) { - if (this.get(row, i) > v) { - v = this.get(row, i); - idx[1] = i; - } - } - return idx; - } - - /** - * Returns the minimum value of one row - * @param {number} row - Row index - * @return {number} - */ - minRow(row) { - checkRowIndex(this, row); - var v = this.get(row, 0); - for (var i = 1; i < this.columns; i++) { - if (this.get(row, i) < v) { - v = this.get(row, i); - } - } - return v; - } - - /** - * Returns the index of the maximum value of one row - * @param {number} row - Row index - * @return {Array} - */ - minRowIndex(row) { - checkRowIndex(this, row); - var v = this.get(row, 0); - var idx = [row, 0]; - for (var i = 1; i < this.columns; i++) { - if (this.get(row, i) < v) { - v = this.get(row, i); - idx[1] = i; - } - } - return idx; - } - - /** - * Returns the maximum value of one column - * @param {number} column - Column index - * @return {number} - */ - maxColumn(column) { - checkColumnIndex(this, column); - var v = this.get(0, column); - for (var i = 1; i < this.rows; i++) { - if (this.get(i, column) > v) { - v = this.get(i, column); - } - } - return v; - } - - /** - * Returns the index of the maximum value of one column - * @param {number} column - Column index - * @return {Array} - */ - maxColumnIndex(column) { - checkColumnIndex(this, column); - var v = this.get(0, column); - var idx = [0, column]; - for (var i = 1; i < this.rows; i++) { - if (this.get(i, column) > v) { - v = this.get(i, column); - idx[0] = i; - } + cumulativeSum() { + let sum = 0; + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + sum += this.get(i, j); + this.set(i, j, sum); } - return idx; } + return this; + } - /** - * Returns the minimum value of one column - * @param {number} column - Column index - * @return {number} - */ - minColumn(column) { - checkColumnIndex(this, column); - var v = this.get(0, column); - for (var i = 1; i < this.rows; i++) { - if (this.get(i, column) < v) { - v = this.get(i, column); - } - } - return v; + dot(vector2) { + if (matrix_AbstractMatrix.isMatrix(vector2)) vector2 = vector2.to1DArray(); + let vector1 = this.to1DArray(); + if (vector1.length !== vector2.length) { + throw new RangeError('vectors do not have the same size'); } + let dot = 0; + for (let i = 0; i < vector1.length; i++) { + dot += vector1[i] * vector2[i]; + } + return dot; + } - /** - * Returns the index of the minimum value of one column - * @param {number} column - Column index - * @return {Array} - */ - minColumnIndex(column) { - checkColumnIndex(this, column); - var v = this.get(0, column); - var idx = [0, column]; - for (var i = 1; i < this.rows; i++) { - if (this.get(i, column) < v) { - v = this.get(i, column); - idx[0] = i; - } + mmul(other) { + other = matrix_Matrix.checkMatrix(other); + + let m = this.rows; + let n = this.columns; + let p = other.columns; + + let result = new matrix_Matrix(m, p); + + let Bcolj = new Float64Array(n); + for (let j = 0; j < p; j++) { + for (let k = 0; k < n; k++) { + Bcolj[k] = other.get(k, j); } - return idx; - } - - /** - * Returns an array containing the diagonal values of the matrix - * @return {Array} - */ - diag() { - var min = Math.min(this.rows, this.columns); - var diag = new Array(min); - for (var i = 0; i < min; i++) { - diag[i] = this.get(i, i); - } - return diag; - } - - /** - * Returns the sum by the argument given, if no argument given, - * it returns the sum of all elements of the matrix. - * @param {string} by - sum by 'row' or 'column'. - * @return {Matrix|number} - */ - sum(by) { - switch (by) { - case 'row': - return sumByRow(this); - case 'column': - return sumByColumn(this); - default: - return sumAll(this); - } - } - - /** - * Returns the mean of all elements of the matrix - * @return {number} - */ - mean() { - return this.sum() / this.size; - } - - /** - * Returns the product of all elements of the matrix - * @return {number} - */ - prod() { - var prod = 1; - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - prod *= this.get(i, j); - } - } - return prod; - } - - /** - * Returns the norm of a matrix. - * @param {string} type - "frobenius" (default) or "max" return resp. the Frobenius norm and the max norm. - * @return {number} - */ - norm(type = 'frobenius') { - var result = 0; - if (type === 'max') { - return this.max(); - } else if (type === 'frobenius') { - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - result = result + this.get(i, j) * this.get(i, j); - } + + for (let i = 0; i < m; i++) { + let s = 0; + for (let k = 0; k < n; k++) { + s += this.get(i, k) * Bcolj[k]; } - return Math.sqrt(result); - } else { - throw new RangeError(`unknown norm type: ${type}`); + + result.set(i, j, s); } } + return result; + } - /** - * Computes the cumulative sum of the matrix elements (in place, row by row) - * @return {Matrix} this - */ - cumulativeSum() { - var sum = 0; - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - sum += this.get(i, j); - this.set(i, j, sum); - } - } - return this; + strassen2x2(other) { + other = matrix_Matrix.checkMatrix(other); + let result = new matrix_Matrix(2, 2); + const a11 = this.get(0, 0); + const b11 = other.get(0, 0); + const a12 = this.get(0, 1); + const b12 = other.get(0, 1); + const a21 = this.get(1, 0); + const b21 = other.get(1, 0); + const a22 = this.get(1, 1); + const b22 = other.get(1, 1); + + // Compute intermediate values. + const m1 = (a11 + a22) * (b11 + b22); + const m2 = (a21 + a22) * b11; + const m3 = a11 * (b12 - b22); + const m4 = a22 * (b21 - b11); + const m5 = (a11 + a12) * b22; + const m6 = (a21 - a11) * (b11 + b12); + const m7 = (a12 - a22) * (b21 + b22); + + // Combine intermediate values into the output. + const c00 = m1 + m4 - m5 + m7; + const c01 = m3 + m5; + const c10 = m2 + m4; + const c11 = m1 - m2 + m3 + m6; + + result.set(0, 0, c00); + result.set(0, 1, c01); + result.set(1, 0, c10); + result.set(1, 1, c11); + return result; + } + + strassen3x3(other) { + other = matrix_Matrix.checkMatrix(other); + let result = new matrix_Matrix(3, 3); + + const a00 = this.get(0, 0); + const a01 = this.get(0, 1); + const a02 = this.get(0, 2); + const a10 = this.get(1, 0); + const a11 = this.get(1, 1); + const a12 = this.get(1, 2); + const a20 = this.get(2, 0); + const a21 = this.get(2, 1); + const a22 = this.get(2, 2); + + const b00 = other.get(0, 0); + const b01 = other.get(0, 1); + const b02 = other.get(0, 2); + const b10 = other.get(1, 0); + const b11 = other.get(1, 1); + const b12 = other.get(1, 2); + const b20 = other.get(2, 0); + const b21 = other.get(2, 1); + const b22 = other.get(2, 2); + + const m1 = (a00 + a01 + a02 - a10 - a11 - a21 - a22) * b11; + const m2 = (a00 - a10) * (-b01 + b11); + const m3 = a11 * (-b00 + b01 + b10 - b11 - b12 - b20 + b22); + const m4 = (-a00 + a10 + a11) * (b00 - b01 + b11); + const m5 = (a10 + a11) * (-b00 + b01); + const m6 = a00 * b00; + const m7 = (-a00 + a20 + a21) * (b00 - b02 + b12); + const m8 = (-a00 + a20) * (b02 - b12); + const m9 = (a20 + a21) * (-b00 + b02); + const m10 = (a00 + a01 + a02 - a11 - a12 - a20 - a21) * b12; + const m11 = a21 * (-b00 + b02 + b10 - b11 - b12 - b20 + b21); + const m12 = (-a02 + a21 + a22) * (b11 + b20 - b21); + const m13 = (a02 - a22) * (b11 - b21); + const m14 = a02 * b20; + const m15 = (a21 + a22) * (-b20 + b21); + const m16 = (-a02 + a11 + a12) * (b12 + b20 - b22); + const m17 = (a02 - a12) * (b12 - b22); + const m18 = (a11 + a12) * (-b20 + b22); + const m19 = a01 * b10; + const m20 = a12 * b21; + const m21 = a10 * b02; + const m22 = a20 * b01; + const m23 = a22 * b22; + + const c00 = m6 + m14 + m19; + const c01 = m1 + m4 + m5 + m6 + m12 + m14 + m15; + const c02 = m6 + m7 + m9 + m10 + m14 + m16 + m18; + const c10 = m2 + m3 + m4 + m6 + m14 + m16 + m17; + const c11 = m2 + m4 + m5 + m6 + m20; + const c12 = m14 + m16 + m17 + m18 + m21; + const c20 = m6 + m7 + m8 + m11 + m12 + m13 + m14; + const c21 = m12 + m13 + m14 + m15 + m22; + const c22 = m6 + m7 + m8 + m9 + m23; + + result.set(0, 0, c00); + result.set(0, 1, c01); + result.set(0, 2, c02); + result.set(1, 0, c10); + result.set(1, 1, c11); + result.set(1, 2, c12); + result.set(2, 0, c20); + result.set(2, 1, c21); + result.set(2, 2, c22); + return result; + } + + mmulStrassen(y) { + y = matrix_Matrix.checkMatrix(y); + let x = this.clone(); + let r1 = x.rows; + let c1 = x.columns; + let r2 = y.rows; + let c2 = y.columns; + if (c1 !== r2) { + // eslint-disable-next-line no-console + console.warn( + `Multiplying ${r1} x ${c1} and ${r2} x ${c2} matrix: dimensions do not match.`, + ); } - /** - * Computes the dot (scalar) product between the matrix and another - * @param {Matrix} vector2 vector - * @return {number} - */ - dot(vector2) { - if (Matrix.isMatrix(vector2)) vector2 = vector2.to1DArray(); - var vector1 = this.to1DArray(); - if (vector1.length !== vector2.length) { - throw new RangeError('vectors do not have the same size'); - } - var dot = 0; - for (var i = 0; i < vector1.length; i++) { - dot += vector1[i] * vector2[i]; + // Put a matrix into the top left of a matrix of zeros. + // `rows` and `cols` are the dimensions of the output matrix. + function embed(mat, rows, cols) { + let r = mat.rows; + let c = mat.columns; + if (r === rows && c === cols) { + return mat; + } else { + let resultat = matrix_AbstractMatrix.zeros(rows, cols); + resultat = resultat.setSubMatrix(mat, 0, 0); + return resultat; } - return dot; } - /** - * Returns the matrix product between this and other - * @param {Matrix} other - * @return {Matrix} - */ - mmul(other) { - other = this.constructor.checkMatrix(other); - if (this.columns !== other.rows) { - // eslint-disable-next-line no-console - console.warn('Number of columns of left matrix are not equal to number of rows of right matrix.'); - } + // Make sure both matrices are the same size. + // This is exclusively for simplicity: + // this algorithm can be implemented with matrices of different sizes. - var m = this.rows; - var n = this.columns; - var p = other.columns; + let r = Math.max(r1, r2); + let c = Math.max(c1, c2); + x = embed(x, r, c); + y = embed(y, r, c); - var result = new this.constructor[Symbol.species](m, p); + // Our recursive multiplication function. + function blockMult(a, b, rows, cols) { + // For small matrices, resort to naive multiplication. + if (rows <= 512 || cols <= 512) { + return a.mmul(b); // a is equivalent to this + } - var Bcolj = new Array(n); - for (var j = 0; j < p; j++) { - for (var k = 0; k < n; k++) { - Bcolj[k] = other.get(k, j); - } + // Apply dynamic padding. + if (rows % 2 === 1 && cols % 2 === 1) { + a = embed(a, rows + 1, cols + 1); + b = embed(b, rows + 1, cols + 1); + } else if (rows % 2 === 1) { + a = embed(a, rows + 1, cols); + b = embed(b, rows + 1, cols); + } else if (cols % 2 === 1) { + a = embed(a, rows, cols + 1); + b = embed(b, rows, cols + 1); + } - for (var i = 0; i < m; i++) { - var s = 0; - for (k = 0; k < n; k++) { - s += this.get(i, k) * Bcolj[k]; - } + let halfRows = parseInt(a.rows / 2, 10); + let halfCols = parseInt(a.columns / 2, 10); + // Subdivide input matrices. + let a11 = a.subMatrix(0, halfRows - 1, 0, halfCols - 1); + let b11 = b.subMatrix(0, halfRows - 1, 0, halfCols - 1); - result.set(i, j, s); - } - } - return result; - } + let a12 = a.subMatrix(0, halfRows - 1, halfCols, a.columns - 1); + let b12 = b.subMatrix(0, halfRows - 1, halfCols, b.columns - 1); + + let a21 = a.subMatrix(halfRows, a.rows - 1, 0, halfCols - 1); + let b21 = b.subMatrix(halfRows, b.rows - 1, 0, halfCols - 1); - strassen2x2(other) { - var result = new this.constructor[Symbol.species](2, 2); - const a11 = this.get(0, 0); - const b11 = other.get(0, 0); - const a12 = this.get(0, 1); - const b12 = other.get(0, 1); - const a21 = this.get(1, 0); - const b21 = other.get(1, 0); - const a22 = this.get(1, 1); - const b22 = other.get(1, 1); + let a22 = a.subMatrix(halfRows, a.rows - 1, halfCols, a.columns - 1); + let b22 = b.subMatrix(halfRows, b.rows - 1, halfCols, b.columns - 1); // Compute intermediate values. - const m1 = (a11 + a22) * (b11 + b22); - const m2 = (a21 + a22) * b11; - const m3 = a11 * (b12 - b22); - const m4 = a22 * (b21 - b11); - const m5 = (a11 + a12) * b22; - const m6 = (a21 - a11) * (b11 + b12); - const m7 = (a12 - a22) * (b21 + b22); + let m1 = blockMult( + matrix_AbstractMatrix.add(a11, a22), + matrix_AbstractMatrix.add(b11, b22), + halfRows, + halfCols, + ); + let m2 = blockMult(matrix_AbstractMatrix.add(a21, a22), b11, halfRows, halfCols); + let m3 = blockMult(a11, matrix_AbstractMatrix.sub(b12, b22), halfRows, halfCols); + let m4 = blockMult(a22, matrix_AbstractMatrix.sub(b21, b11), halfRows, halfCols); + let m5 = blockMult(matrix_AbstractMatrix.add(a11, a12), b22, halfRows, halfCols); + let m6 = blockMult( + matrix_AbstractMatrix.sub(a21, a11), + matrix_AbstractMatrix.add(b11, b12), + halfRows, + halfCols, + ); + let m7 = blockMult( + matrix_AbstractMatrix.sub(a12, a22), + matrix_AbstractMatrix.add(b21, b22), + halfRows, + halfCols, + ); // Combine intermediate values into the output. - const c00 = m1 + m4 - m5 + m7; - const c01 = m3 + m5; - const c10 = m2 + m4; - const c11 = m1 - m2 + m3 + m6; - - result.set(0, 0, c00); - result.set(0, 1, c01); - result.set(1, 0, c10); - result.set(1, 1, c11); - return result; - } - - strassen3x3(other) { - var result = new this.constructor[Symbol.species](3, 3); - - const a00 = this.get(0, 0); - const a01 = this.get(0, 1); - const a02 = this.get(0, 2); - const a10 = this.get(1, 0); - const a11 = this.get(1, 1); - const a12 = this.get(1, 2); - const a20 = this.get(2, 0); - const a21 = this.get(2, 1); - const a22 = this.get(2, 2); - - const b00 = other.get(0, 0); - const b01 = other.get(0, 1); - const b02 = other.get(0, 2); - const b10 = other.get(1, 0); - const b11 = other.get(1, 1); - const b12 = other.get(1, 2); - const b20 = other.get(2, 0); - const b21 = other.get(2, 1); - const b22 = other.get(2, 2); - - const m1 = (a00 + a01 + a02 - a10 - a11 - a21 - a22) * b11; - const m2 = (a00 - a10) * (-b01 + b11); - const m3 = a11 * (-b00 + b01 + b10 - b11 - b12 - b20 + b22); - const m4 = (-a00 + a10 + a11) * (b00 - b01 + b11); - const m5 = (a10 + a11) * (-b00 + b01); - const m6 = a00 * b00; - const m7 = (-a00 + a20 + a21) * (b00 - b02 + b12); - const m8 = (-a00 + a20) * (b02 - b12); - const m9 = (a20 + a21) * (-b00 + b02); - const m10 = (a00 + a01 + a02 - a11 - a12 - a20 - a21) * b12; - const m11 = a21 * (-b00 + b02 + b10 - b11 - b12 - b20 + b21); - const m12 = (-a02 + a21 + a22) * (b11 + b20 - b21); - const m13 = (a02 - a22) * (b11 - b21); - const m14 = a02 * b20; - const m15 = (a21 + a22) * (-b20 + b21); - const m16 = (-a02 + a11 + a12) * (b12 + b20 - b22); - const m17 = (a02 - a12) * (b12 - b22); - const m18 = (a11 + a12) * (-b20 + b22); - const m19 = a01 * b10; - const m20 = a12 * b21; - const m21 = a10 * b02; - const m22 = a20 * b01; - const m23 = a22 * b22; - - const c00 = m6 + m14 + m19; - const c01 = m1 + m4 + m5 + m6 + m12 + m14 + m15; - const c02 = m6 + m7 + m9 + m10 + m14 + m16 + m18; - const c10 = m2 + m3 + m4 + m6 + m14 + m16 + m17; - const c11 = m2 + m4 + m5 + m6 + m20; - const c12 = m14 + m16 + m17 + m18 + m21; - const c20 = m6 + m7 + m8 + m11 + m12 + m13 + m14; - const c21 = m12 + m13 + m14 + m15 + m22; - const c22 = m6 + m7 + m8 + m9 + m23; - - result.set(0, 0, c00); - result.set(0, 1, c01); - result.set(0, 2, c02); - result.set(1, 0, c10); - result.set(1, 1, c11); - result.set(1, 2, c12); - result.set(2, 0, c20); - result.set(2, 1, c21); - result.set(2, 2, c22); - return result; - } - - /** - * Returns the matrix product between x and y. More efficient than mmul(other) only when we multiply squared matrix and when the size of the matrix is > 1000. - * @param {Matrix} y - * @return {Matrix} - */ - mmulStrassen(y) { - var x = this.clone(); - var r1 = x.rows; - var c1 = x.columns; - var r2 = y.rows; - var c2 = y.columns; - if (c1 !== r2) { - // eslint-disable-next-line no-console - console.warn(`Multiplying ${r1} x ${c1} and ${r2} x ${c2} matrix: dimensions do not match.`); + let c11 = matrix_AbstractMatrix.add(m1, m4); + c11.sub(m5); + c11.add(m7); + let c12 = matrix_AbstractMatrix.add(m3, m5); + let c21 = matrix_AbstractMatrix.add(m2, m4); + let c22 = matrix_AbstractMatrix.sub(m1, m2); + c22.add(m3); + c22.add(m6); + + // Crop output to the desired size (undo dynamic padding). + let resultat = matrix_AbstractMatrix.zeros(2 * c11.rows, 2 * c11.columns); + resultat = resultat.setSubMatrix(c11, 0, 0); + resultat = resultat.setSubMatrix(c12, c11.rows, 0); + resultat = resultat.setSubMatrix(c21, 0, c11.columns); + resultat = resultat.setSubMatrix(c22, c11.rows, c11.columns); + return resultat.subMatrix(0, rows - 1, 0, cols - 1); + } + return blockMult(x, y, r, c); + } + + scaleRows(options = {}) { + if (typeof options !== 'object') { + throw new TypeError('options must be an object'); + } + const { min = 0, max = 1 } = options; + if (!Number.isFinite(min)) throw new TypeError('min must be a number'); + if (!Number.isFinite(max)) throw new TypeError('max must be a number'); + if (min >= max) throw new RangeError('min must be smaller than max'); + let newMatrix = new matrix_Matrix(this.rows, this.columns); + for (let i = 0; i < this.rows; i++) { + const row = this.getRow(i); + ml_array_rescale_lib_es6(row, { min, max, output: row }); + newMatrix.setRow(i, row); + } + return newMatrix; + } + + scaleColumns(options = {}) { + if (typeof options !== 'object') { + throw new TypeError('options must be an object'); + } + const { min = 0, max = 1 } = options; + if (!Number.isFinite(min)) throw new TypeError('min must be a number'); + if (!Number.isFinite(max)) throw new TypeError('max must be a number'); + if (min >= max) throw new RangeError('min must be smaller than max'); + let newMatrix = new matrix_Matrix(this.rows, this.columns); + for (let i = 0; i < this.columns; i++) { + const column = this.getColumn(i); + ml_array_rescale_lib_es6(column, { + min: min, + max: max, + output: column, + }); + newMatrix.setColumn(i, column); + } + return newMatrix; + } + + flipRows() { + const middle = Math.ceil(this.columns / 2); + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < middle; j++) { + let first = this.get(i, j); + let last = this.get(i, this.columns - 1 - j); + this.set(i, j, last); + this.set(i, this.columns - 1 - j, first); } + } + return this; + } - // Put a matrix into the top left of a matrix of zeros. - // `rows` and `cols` are the dimensions of the output matrix. - function embed(mat, rows, cols) { - var r = mat.rows; - var c = mat.columns; - if ((r === rows) && (c === cols)) { - return mat; - } else { - var resultat = Matrix.zeros(rows, cols); - resultat = resultat.setSubMatrix(mat, 0, 0); - return resultat; - } - } - - - // Make sure both matrices are the same size. - // This is exclusively for simplicity: - // this algorithm can be implemented with matrices of different sizes. - - var r = Math.max(r1, r2); - var c = Math.max(c1, c2); - x = embed(x, r, c); - y = embed(y, r, c); - - // Our recursive multiplication function. - function blockMult(a, b, rows, cols) { - // For small matrices, resort to naive multiplication. - if (rows <= 512 || cols <= 512) { - return a.mmul(b); // a is equivalent to this - } - - // Apply dynamic padding. - if ((rows % 2 === 1) && (cols % 2 === 1)) { - a = embed(a, rows + 1, cols + 1); - b = embed(b, rows + 1, cols + 1); - } else if (rows % 2 === 1) { - a = embed(a, rows + 1, cols); - b = embed(b, rows + 1, cols); - } else if (cols % 2 === 1) { - a = embed(a, rows, cols + 1); - b = embed(b, rows, cols + 1); - } - - var halfRows = parseInt(a.rows / 2, 10); - var halfCols = parseInt(a.columns / 2, 10); - // Subdivide input matrices. - var a11 = a.subMatrix(0, halfRows - 1, 0, halfCols - 1); - var b11 = b.subMatrix(0, halfRows - 1, 0, halfCols - 1); - - var a12 = a.subMatrix(0, halfRows - 1, halfCols, a.columns - 1); - var b12 = b.subMatrix(0, halfRows - 1, halfCols, b.columns - 1); - - var a21 = a.subMatrix(halfRows, a.rows - 1, 0, halfCols - 1); - var b21 = b.subMatrix(halfRows, b.rows - 1, 0, halfCols - 1); - - var a22 = a.subMatrix(halfRows, a.rows - 1, halfCols, a.columns - 1); - var b22 = b.subMatrix(halfRows, b.rows - 1, halfCols, b.columns - 1); - - // Compute intermediate values. - var m1 = blockMult(Matrix.add(a11, a22), Matrix.add(b11, b22), halfRows, halfCols); - var m2 = blockMult(Matrix.add(a21, a22), b11, halfRows, halfCols); - var m3 = blockMult(a11, Matrix.sub(b12, b22), halfRows, halfCols); - var m4 = blockMult(a22, Matrix.sub(b21, b11), halfRows, halfCols); - var m5 = blockMult(Matrix.add(a11, a12), b22, halfRows, halfCols); - var m6 = blockMult(Matrix.sub(a21, a11), Matrix.add(b11, b12), halfRows, halfCols); - var m7 = blockMult(Matrix.sub(a12, a22), Matrix.add(b21, b22), halfRows, halfCols); - - // Combine intermediate values into the output. - var c11 = Matrix.add(m1, m4); - c11.sub(m5); - c11.add(m7); - var c12 = Matrix.add(m3, m5); - var c21 = Matrix.add(m2, m4); - var c22 = Matrix.sub(m1, m2); - c22.add(m3); - c22.add(m6); - - // Crop output to the desired size (undo dynamic padding). - var resultat = Matrix.zeros(2 * c11.rows, 2 * c11.columns); - resultat = resultat.setSubMatrix(c11, 0, 0); - resultat = resultat.setSubMatrix(c12, c11.rows, 0); - resultat = resultat.setSubMatrix(c21, 0, c11.columns); - resultat = resultat.setSubMatrix(c22, c11.rows, c11.columns); - return resultat.subMatrix(0, rows - 1, 0, cols - 1); - } - return blockMult(x, y, r, c); - } - - /** - * Returns a row-by-row scaled matrix - * @param {number} [min=0] - Minimum scaled value - * @param {number} [max=1] - Maximum scaled value - * @return {Matrix} - The scaled matrix - */ - scaleRows(min, max) { - min = min === undefined ? 0 : min; - max = max === undefined ? 1 : max; - if (min >= max) { - throw new RangeError('min should be strictly smaller than max'); - } - var newMatrix = this.constructor.empty(this.rows, this.columns); - for (var i = 0; i < this.rows; i++) { - var scaled = ml_array_rescale_lib_es6(this.getRow(i), { min, max }); - newMatrix.setRow(i, scaled); - } - return newMatrix; - } - - /** - * Returns a new column-by-column scaled matrix - * @param {number} [min=0] - Minimum scaled value - * @param {number} [max=1] - Maximum scaled value - * @return {Matrix} - The new scaled matrix - * @example - * var matrix = new Matrix([[1,2],[-1,0]]); - * var scaledMatrix = matrix.scaleColumns(); // [[1,1],[0,0]] - */ - scaleColumns(min, max) { - min = min === undefined ? 0 : min; - max = max === undefined ? 1 : max; - if (min >= max) { - throw new RangeError('min should be strictly smaller than max'); - } - var newMatrix = this.constructor.empty(this.rows, this.columns); - for (var i = 0; i < this.columns; i++) { - var scaled = ml_array_rescale_lib_es6(this.getColumn(i), { - min: min, - max: max - }); - newMatrix.setColumn(i, scaled); + flipColumns() { + const middle = Math.ceil(this.rows / 2); + for (let j = 0; j < this.columns; j++) { + for (let i = 0; i < middle; i++) { + let first = this.get(i, j); + let last = this.get(this.rows - 1 - i, j); + this.set(i, j, last); + this.set(this.rows - 1 - i, j, first); } - return newMatrix; } + return this; + } + kroneckerProduct(other) { + other = matrix_Matrix.checkMatrix(other); - /** - * Returns the Kronecker product (also known as tensor product) between this and other - * See https://en.wikipedia.org/wiki/Kronecker_product - * @param {Matrix} other - * @return {Matrix} - */ - kroneckerProduct(other) { - other = this.constructor.checkMatrix(other); - - var m = this.rows; - var n = this.columns; - var p = other.rows; - var q = other.columns; + let m = this.rows; + let n = this.columns; + let p = other.rows; + let q = other.columns; - var result = new this.constructor[Symbol.species](m * p, n * q); - for (var i = 0; i < m; i++) { - for (var j = 0; j < n; j++) { - for (var k = 0; k < p; k++) { - for (var l = 0; l < q; l++) { - result[p * i + k][q * j + l] = this.get(i, j) * other.get(k, l); - } + let result = new matrix_Matrix(m * p, n * q); + for (let i = 0; i < m; i++) { + for (let j = 0; j < n; j++) { + for (let k = 0; k < p; k++) { + for (let l = 0; l < q; l++) { + result.set(p * i + k, q * j + l, this.get(i, j) * other.get(k, l)); } } } - return result; } + return result; + } - /** - * Transposes the matrix and returns a new one containing the result - * @return {Matrix} - */ - transpose() { - var result = new this.constructor[Symbol.species](this.columns, this.rows); - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - result.set(j, i, this.get(i, j)); - } + transpose() { + let result = new matrix_Matrix(this.columns, this.rows); + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.columns; j++) { + result.set(j, i, this.get(i, j)); } - return result; } + return result; + } - /** - * Sorts the rows (in place) - * @param {function} compareFunction - usual Array.prototype.sort comparison function - * @return {Matrix} this - */ - sortRows(compareFunction) { - if (compareFunction === undefined) compareFunction = compareNumbers; - for (var i = 0; i < this.rows; i++) { - this.setRow(i, this.getRow(i).sort(compareFunction)); - } - return this; + sortRows(compareFunction = compareNumbers) { + for (let i = 0; i < this.rows; i++) { + this.setRow(i, this.getRow(i).sort(compareFunction)); } + return this; + } - /** - * Sorts the columns (in place) - * @param {function} compareFunction - usual Array.prototype.sort comparison function - * @return {Matrix} this - */ - sortColumns(compareFunction) { - if (compareFunction === undefined) compareFunction = compareNumbers; - for (var i = 0; i < this.columns; i++) { - this.setColumn(i, this.getColumn(i).sort(compareFunction)); - } - return this; + sortColumns(compareFunction = compareNumbers) { + for (let i = 0; i < this.columns; i++) { + this.setColumn(i, this.getColumn(i).sort(compareFunction)); } + return this; + } - /** - * Returns a subset of the matrix - * @param {number} startRow - First row index - * @param {number} endRow - Last row index - * @param {number} startColumn - First column index - * @param {number} endColumn - Last column index - * @return {Matrix} - */ - subMatrix(startRow, endRow, startColumn, endColumn) { - checkRange(this, startRow, endRow, startColumn, endColumn); - var newMatrix = new this.constructor[Symbol.species](endRow - startRow + 1, endColumn - startColumn + 1); - for (var i = startRow; i <= endRow; i++) { - for (var j = startColumn; j <= endColumn; j++) { - newMatrix[i - startRow][j - startColumn] = this.get(i, j); - } + subMatrix(startRow, endRow, startColumn, endColumn) { + checkRange(this, startRow, endRow, startColumn, endColumn); + let newMatrix = new matrix_Matrix( + endRow - startRow + 1, + endColumn - startColumn + 1, + ); + for (let i = startRow; i <= endRow; i++) { + for (let j = startColumn; j <= endColumn; j++) { + newMatrix.set(i - startRow, j - startColumn, this.get(i, j)); } - return newMatrix; } + return newMatrix; + } - /** - * Returns a subset of the matrix based on an array of row indices - * @param {Array} indices - Array containing the row indices - * @param {number} [startColumn = 0] - First column index - * @param {number} [endColumn = this.columns-1] - Last column index - * @return {Matrix} - */ - subMatrixRow(indices, startColumn, endColumn) { - if (startColumn === undefined) startColumn = 0; - if (endColumn === undefined) endColumn = this.columns - 1; - if ((startColumn > endColumn) || (startColumn < 0) || (startColumn >= this.columns) || (endColumn < 0) || (endColumn >= this.columns)) { - throw new RangeError('Argument out of range'); - } + subMatrixRow(indices, startColumn, endColumn) { + if (startColumn === undefined) startColumn = 0; + if (endColumn === undefined) endColumn = this.columns - 1; + if ( + startColumn > endColumn || + startColumn < 0 || + startColumn >= this.columns || + endColumn < 0 || + endColumn >= this.columns + ) { + throw new RangeError('Argument out of range'); + } - var newMatrix = new this.constructor[Symbol.species](indices.length, endColumn - startColumn + 1); - for (var i = 0; i < indices.length; i++) { - for (var j = startColumn; j <= endColumn; j++) { - if (indices[i] < 0 || indices[i] >= this.rows) { - throw new RangeError(`Row index out of range: ${indices[i]}`); - } - newMatrix.set(i, j - startColumn, this.get(indices[i], j)); + let newMatrix = new matrix_Matrix(indices.length, endColumn - startColumn + 1); + for (let i = 0; i < indices.length; i++) { + for (let j = startColumn; j <= endColumn; j++) { + if (indices[i] < 0 || indices[i] >= this.rows) { + throw new RangeError(`Row index out of range: ${indices[i]}`); } + newMatrix.set(i, j - startColumn, this.get(indices[i], j)); } - return newMatrix; } + return newMatrix; + } - /** - * Returns a subset of the matrix based on an array of column indices - * @param {Array} indices - Array containing the column indices - * @param {number} [startRow = 0] - First row index - * @param {number} [endRow = this.rows-1] - Last row index - * @return {Matrix} - */ - subMatrixColumn(indices, startRow, endRow) { - if (startRow === undefined) startRow = 0; - if (endRow === undefined) endRow = this.rows - 1; - if ((startRow > endRow) || (startRow < 0) || (startRow >= this.rows) || (endRow < 0) || (endRow >= this.rows)) { - throw new RangeError('Argument out of range'); - } - - var newMatrix = new this.constructor[Symbol.species](endRow - startRow + 1, indices.length); - for (var i = 0; i < indices.length; i++) { - for (var j = startRow; j <= endRow; j++) { - if (indices[i] < 0 || indices[i] >= this.columns) { - throw new RangeError(`Column index out of range: ${indices[i]}`); - } - newMatrix.set(j - startRow, i, this.get(j, indices[i])); - } - } - return newMatrix; - } + subMatrixColumn(indices, startRow, endRow) { + if (startRow === undefined) startRow = 0; + if (endRow === undefined) endRow = this.rows - 1; + if ( + startRow > endRow || + startRow < 0 || + startRow >= this.rows || + endRow < 0 || + endRow >= this.rows + ) { + throw new RangeError('Argument out of range'); + } - /** - * Set a part of the matrix to the given sub-matrix - * @param {Matrix|Array< Array >} matrix - The source matrix from which to extract values. - * @param {number} startRow - The index of the first row to set - * @param {number} startColumn - The index of the first column to set - * @return {Matrix} - */ - setSubMatrix(matrix, startRow, startColumn) { - matrix = this.constructor.checkMatrix(matrix); - var endRow = startRow + matrix.rows - 1; - var endColumn = startColumn + matrix.columns - 1; - checkRange(this, startRow, endRow, startColumn, endColumn); - for (var i = 0; i < matrix.rows; i++) { - for (var j = 0; j < matrix.columns; j++) { - this[startRow + i][startColumn + j] = matrix.get(i, j); - } - } - return this; - } - - /** - * Return a new matrix based on a selection of rows and columns - * @param {Array} rowIndices - The row indices to select. Order matters and an index can be more than once. - * @param {Array} columnIndices - The column indices to select. Order matters and an index can be use more than once. - * @return {Matrix} The new matrix - */ - selection(rowIndices, columnIndices) { - var indices = checkIndices(this, rowIndices, columnIndices); - var newMatrix = new this.constructor[Symbol.species](rowIndices.length, columnIndices.length); - for (var i = 0; i < indices.row.length; i++) { - var rowIndex = indices.row[i]; - for (var j = 0; j < indices.column.length; j++) { - var columnIndex = indices.column[j]; - newMatrix[i][j] = this.get(rowIndex, columnIndex); - } - } - return newMatrix; - } - - /** - * Returns the trace of the matrix (sum of the diagonal elements) - * @return {number} - */ - trace() { - var min = Math.min(this.rows, this.columns); - var trace = 0; - for (var i = 0; i < min; i++) { - trace += this.get(i, i); - } - return trace; - } - - /* - Matrix views - */ - - /** - * Returns a view of the transposition of the matrix - * @return {MatrixTransposeView} - */ - transposeView() { - return new transpose_MatrixTransposeView(this); - } - - /** - * Returns a view of the row vector with the given index - * @param {number} row - row index of the vector - * @return {MatrixRowView} - */ - rowView(row) { - checkRowIndex(this, row); - return new row_MatrixRowView(this, row); - } - - /** - * Returns a view of the column vector with the given index - * @param {number} column - column index of the vector - * @return {MatrixColumnView} - */ - columnView(column) { - checkColumnIndex(this, column); - return new column_MatrixColumnView(this, column); - } - - /** - * Returns a view of the matrix flipped in the row axis - * @return {MatrixFlipRowView} - */ - flipRowView() { - return new flipRow_MatrixFlipRowView(this); - } - - /** - * Returns a view of the matrix flipped in the column axis - * @return {MatrixFlipColumnView} - */ - flipColumnView() { - return new flipColumn_MatrixFlipColumnView(this); - } - - /** - * Returns a view of a submatrix giving the index boundaries - * @param {number} startRow - first row index of the submatrix - * @param {number} endRow - last row index of the submatrix - * @param {number} startColumn - first column index of the submatrix - * @param {number} endColumn - last column index of the submatrix - * @return {MatrixSubView} - */ - subMatrixView(startRow, endRow, startColumn, endColumn) { - return new sub_MatrixSubView(this, startRow, endRow, startColumn, endColumn); - } - - /** - * Returns a view of the cross of the row indices and the column indices - * @example - * // resulting vector is [[2], [2]] - * var matrix = new Matrix([[1,2,3], [4,5,6]]).selectionView([0, 0], [1]) - * @param {Array} rowIndices - * @param {Array} columnIndices - * @return {MatrixSelectionView} - */ - selectionView(rowIndices, columnIndices) { - return new selection_MatrixSelectionView(this, rowIndices, columnIndices); - } - - /** - * Returns a view of the row indices - * @example - * // resulting vector is [[1,2,3], [1,2,3]] - * var matrix = new Matrix([[1,2,3], [4,5,6]]).rowSelectionView([0, 0]) - * @param {Array} rowIndices - * @return {MatrixRowSelectionView} - */ - rowSelectionView(rowIndices) { - return new rowSelection_MatrixRowSelectionView(this, rowIndices); - } - - /** - * Returns a view of the column indices - * @example - * // resulting vector is [[2, 2], [5, 5]] - * var matrix = new Matrix([[1,2,3], [4,5,6]]).columnSelectionView([1, 1]) - * @param {Array} columnIndices - * @return {MatrixColumnSelectionView} - */ - columnSelectionView(columnIndices) { - return new columnSelection_MatrixColumnSelectionView(this, columnIndices); - } - - - /** - * Calculates and returns the determinant of a matrix as a Number - * @example - * new Matrix([[1,2,3], [4,5,6]]).det() - * @return {number} - */ - det() { - if (this.isSquare()) { - var a, b, c, d; - if (this.columns === 2) { - // 2 x 2 matrix - a = this.get(0, 0); - b = this.get(0, 1); - c = this.get(1, 0); - d = this.get(1, 1); - - return a * d - (b * c); - } else if (this.columns === 3) { - // 3 x 3 matrix - var subMatrix0, subMatrix1, subMatrix2; - subMatrix0 = this.selectionView([1, 2], [1, 2]); - subMatrix1 = this.selectionView([1, 2], [0, 2]); - subMatrix2 = this.selectionView([1, 2], [0, 1]); - a = this.get(0, 0); - b = this.get(0, 1); - c = this.get(0, 2); - - return a * subMatrix0.det() - b * subMatrix1.det() + c * subMatrix2.det(); - } else { - // general purpose determinant using the LU decomposition - return new lu_LuDecomposition(this).determinant; + let newMatrix = new matrix_Matrix(endRow - startRow + 1, indices.length); + for (let i = 0; i < indices.length; i++) { + for (let j = startRow; j <= endRow; j++) { + if (indices[i] < 0 || indices[i] >= this.columns) { + throw new RangeError(`Column index out of range: ${indices[i]}`); } - } else { - throw Error('Determinant can only be calculated for a square matrix.'); + newMatrix.set(j - startRow, i, this.get(j, indices[i])); } } + return newMatrix; + } - /** - * Returns inverse of a matrix if it exists or the pseudoinverse - * @param {number} threshold - threshold for taking inverse of singular values (default = 1e-15) - * @return {Matrix} the (pseudo)inverted matrix. - */ - pseudoInverse(threshold) { - if (threshold === undefined) threshold = Number.EPSILON; - var svdSolution = new svd_SingularValueDecomposition(this, { autoTranspose: true }); - - var U = svdSolution.leftSingularVectors; - var V = svdSolution.rightSingularVectors; - var s = svdSolution.diagonal; - - for (var i = 0; i < s.length; i++) { - if (Math.abs(s[i]) > threshold) { - s[i] = 1.0 / s[i]; - } else { - s[i] = 0.0; - } + setSubMatrix(matrix, startRow, startColumn) { + matrix = matrix_Matrix.checkMatrix(matrix); + let endRow = startRow + matrix.rows - 1; + let endColumn = startColumn + matrix.columns - 1; + checkRange(this, startRow, endRow, startColumn, endColumn); + for (let i = 0; i < matrix.rows; i++) { + for (let j = 0; j < matrix.columns; j++) { + this.set(startRow + i, startColumn + j, matrix.get(i, j)); } - - // convert list to diagonal - s = this.constructor[Symbol.species].diag(s); - return V.mmul(s.mmul(U.transposeView())); } + return this; + } - /** - * Creates an exact and independent copy of the matrix - * @return {Matrix} - */ - clone() { - var newMatrix = new this.constructor[Symbol.species](this.rows, this.columns); - for (var row = 0; row < this.rows; row++) { - for (var column = 0; column < this.columns; column++) { - newMatrix.set(row, column, this.get(row, column)); - } + selection(rowIndices, columnIndices) { + let indices = checkIndices(this, rowIndices, columnIndices); + let newMatrix = new matrix_Matrix(rowIndices.length, columnIndices.length); + for (let i = 0; i < indices.row.length; i++) { + let rowIndex = indices.row[i]; + for (let j = 0; j < indices.column.length; j++) { + let columnIndex = indices.column[j]; + newMatrix.set(i, j, this.get(rowIndex, columnIndex)); } - return newMatrix; } + return newMatrix; } - Matrix.prototype.klass = 'Matrix'; - - function compareNumbers(a, b) { - return a - b; + trace() { + let min = Math.min(this.rows, this.columns); + let trace = 0; + for (let i = 0; i < min; i++) { + trace += this.get(i, i); + } + return trace; } - /* - Synonyms - */ - - Matrix.random = Matrix.rand; - Matrix.diagonal = Matrix.diag; - Matrix.prototype.diagonal = Matrix.prototype.diag; - Matrix.identity = Matrix.eye; - Matrix.prototype.negate = Matrix.prototype.neg; - Matrix.prototype.tensorProduct = Matrix.prototype.kroneckerProduct; - Matrix.prototype.determinant = Matrix.prototype.det; - - /* - Add dynamically instance and static methods for mathematical operations - */ - - var inplaceOperator = ` -(function %name%(value) { - if (typeof value === 'number') return this.%name%S(value); - return this.%name%M(value); -}) -`; - - var inplaceOperatorScalar = ` -(function %name%S(value) { - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, this.get(i, j) %op% value); - } + clone() { + let newMatrix = new matrix_Matrix(this.rows, this.columns); + for (let row = 0; row < this.rows; row++) { + for (let column = 0; column < this.columns; column++) { + newMatrix.set(row, column, this.get(row, column)); + } } - return this; -}) -`; + return newMatrix; + } - var inplaceOperatorMatrix = ` -(function %name%M(matrix) { - matrix = this.constructor.checkMatrix(matrix); - if (this.rows !== matrix.rows || - this.columns !== matrix.columns) { - throw new RangeError('Matrices dimensions must be equal'); - } - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, this.get(i, j) %op% matrix.get(i, j)); - } + sum(by) { + switch (by) { + case 'row': + return sumByRow(this); + case 'column': + return sumByColumn(this); + case undefined: + return sumAll(this); + default: + throw new Error(`invalid option: ${by}`); } - return this; -}) -`; - - var staticOperator = ` -(function %name%(matrix, value) { - var newMatrix = new this[Symbol.species](matrix); - return newMatrix.%name%(value); -}) -`; + } - var inplaceMethod = ` -(function %name%() { - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, %method%(this.get(i, j))); - } + product(by) { + switch (by) { + case 'row': + return productByRow(this); + case 'column': + return productByColumn(this); + case undefined: + return productAll(this); + default: + throw new Error(`invalid option: ${by}`); } - return this; -}) -`; - - var staticMethod = ` -(function %name%(matrix) { - var newMatrix = new this[Symbol.species](matrix); - return newMatrix.%name%(); -}) -`; + } - var inplaceMethodWithArgs = ` -(function %name%(%args%) { - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, %method%(this.get(i, j), %args%)); + mean(by) { + const sum = this.sum(by); + switch (by) { + case 'row': { + for (let i = 0; i < this.rows; i++) { + sum[i] /= this.columns; } + return sum; + } + case 'column': { + for (let i = 0; i < this.columns; i++) { + sum[i] /= this.rows; + } + return sum; + } + case undefined: + return sum / this.size; + default: + throw new Error(`invalid option: ${by}`); } - return this; -}) -`; - - var staticMethodWithArgs = ` -(function %name%(matrix, %args%) { - var newMatrix = new this[Symbol.species](matrix); - return newMatrix.%name%(%args%); -}) -`; - + } - var inplaceMethodWithOneArgScalar = ` -(function %name%S(value) { - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, %method%(this.get(i, j), value)); - } + variance(by, options = {}) { + if (typeof by === 'object') { + options = by; + by = undefined; } - return this; -}) -`; - var inplaceMethodWithOneArgMatrix = ` -(function %name%M(matrix) { - matrix = this.constructor.checkMatrix(matrix); - if (this.rows !== matrix.rows || - this.columns !== matrix.columns) { - throw new RangeError('Matrices dimensions must be equal'); + if (typeof options !== 'object') { + throw new TypeError('options must be an object'); } - for (var i = 0; i < this.rows; i++) { - for (var j = 0; j < this.columns; j++) { - this.set(i, j, %method%(this.get(i, j), matrix.get(i, j))); + const { unbiased = true, mean = this.mean(by) } = options; + if (typeof unbiased !== 'boolean') { + throw new TypeError('unbiased must be a boolean'); + } + switch (by) { + case 'row': { + if (!Array.isArray(mean)) { + throw new TypeError('mean must be an array'); + } + return varianceByRow(this, unbiased, mean); + } + case 'column': { + if (!Array.isArray(mean)) { + throw new TypeError('mean must be an array'); + } + return varianceByColumn(this, unbiased, mean); + } + case undefined: { + if (typeof mean !== 'number') { + throw new TypeError('mean must be a number'); } + return varianceAll(this, unbiased, mean); + } + default: + throw new Error(`invalid option: ${by}`); } - return this; -}) -`; - - var inplaceMethodWithOneArg = ` -(function %name%(value) { - if (typeof value === 'number') return this.%name%S(value); - return this.%name%M(value); -}) -`; - - var staticMethodWithOneArg = staticMethodWithArgs; - - var operators = [ - // Arithmetic operators - ['+', 'add'], - ['-', 'sub', 'subtract'], - ['*', 'mul', 'multiply'], - ['/', 'div', 'divide'], - ['%', 'mod', 'modulus'], - // Bitwise operators - ['&', 'and'], - ['|', 'or'], - ['^', 'xor'], - ['<<', 'leftShift'], - ['>>', 'signPropagatingRightShift'], - ['>>>', 'rightShift', 'zeroFillRightShift'] - ]; - - var i; - var eval2 = eval; // eslint-disable-line no-eval - for (var operator of operators) { - var inplaceOp = eval2(fillTemplateFunction(inplaceOperator, { name: operator[1], op: operator[0] })); - var inplaceOpS = eval2(fillTemplateFunction(inplaceOperatorScalar, { name: `${operator[1]}S`, op: operator[0] })); - var inplaceOpM = eval2(fillTemplateFunction(inplaceOperatorMatrix, { name: `${operator[1]}M`, op: operator[0] })); - var staticOp = eval2(fillTemplateFunction(staticOperator, { name: operator[1] })); - for (i = 1; i < operator.length; i++) { - Matrix.prototype[operator[i]] = inplaceOp; - Matrix.prototype[`${operator[i]}S`] = inplaceOpS; - Matrix.prototype[`${operator[i]}M`] = inplaceOpM; - Matrix[operator[i]] = staticOp; - } - } - - var methods = [['~', 'not']]; - - [ - 'abs', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atanh', 'cbrt', 'ceil', - 'clz32', 'cos', 'cosh', 'exp', 'expm1', 'floor', 'fround', 'log', 'log1p', - 'log10', 'log2', 'round', 'sign', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc' - ].forEach(function (mathMethod) { - methods.push([`Math.${mathMethod}`, mathMethod]); - }); + } - for (var method of methods) { - var inplaceMeth = eval2(fillTemplateFunction(inplaceMethod, { name: method[1], method: method[0] })); - var staticMeth = eval2(fillTemplateFunction(staticMethod, { name: method[1] })); - for (i = 1; i < method.length; i++) { - Matrix.prototype[method[i]] = inplaceMeth; - Matrix[method[i]] = staticMeth; + standardDeviation(by, options) { + if (typeof by === 'object') { + options = by; + by = undefined; + } + const variance = this.variance(by, options); + if (by === undefined) { + return Math.sqrt(variance); + } else { + for (let i = 0; i < variance.length; i++) { + variance[i] = Math.sqrt(variance[i]); + } + return variance; } } - var methodsWithArgs = [['Math.pow', 1, 'pow']]; - - for (var methodWithArg of methodsWithArgs) { - var args = 'arg0'; - for (i = 1; i < methodWithArg[1]; i++) { - args += `, arg${i}`; + center(by, options = {}) { + if (typeof by === 'object') { + options = by; + by = undefined; + } + if (typeof options !== 'object') { + throw new TypeError('options must be an object'); } - if (methodWithArg[1] !== 1) { - var inplaceMethWithArgs = eval2(fillTemplateFunction(inplaceMethodWithArgs, { - name: methodWithArg[2], - method: methodWithArg[0], - args: args - })); - var staticMethWithArgs = eval2(fillTemplateFunction(staticMethodWithArgs, { name: methodWithArg[2], args: args })); - for (i = 2; i < methodWithArg.length; i++) { - Matrix.prototype[methodWithArg[i]] = inplaceMethWithArgs; - Matrix[methodWithArg[i]] = staticMethWithArgs; + const { center = this.mean(by) } = options; + switch (by) { + case 'row': { + if (!Array.isArray(center)) { + throw new TypeError('center must be an array'); + } + centerByRow(this, center); + return this; } - } else { - var tmplVar = { - name: methodWithArg[2], - args: args, - method: methodWithArg[0] - }; - var inplaceMethod2 = eval2(fillTemplateFunction(inplaceMethodWithOneArg, tmplVar)); - var inplaceMethodS = eval2(fillTemplateFunction(inplaceMethodWithOneArgScalar, tmplVar)); - var inplaceMethodM = eval2(fillTemplateFunction(inplaceMethodWithOneArgMatrix, tmplVar)); - var staticMethod2 = eval2(fillTemplateFunction(staticMethodWithOneArg, tmplVar)); - for (i = 2; i < methodWithArg.length; i++) { - Matrix.prototype[methodWithArg[i]] = inplaceMethod2; - Matrix.prototype[`${methodWithArg[i]}M`] = inplaceMethodM; - Matrix.prototype[`${methodWithArg[i]}S`] = inplaceMethodS; - Matrix[methodWithArg[i]] = staticMethod2; + case 'column': { + if (!Array.isArray(center)) { + throw new TypeError('center must be an array'); + } + centerByColumn(this, center); + return this; + } + case undefined: { + if (typeof center !== 'number') { + throw new TypeError('center must be a number'); + } + centerAll(this, center); + return this; } + default: + throw new Error(`invalid option: ${by}`); } } - function fillTemplateFunction(template, values) { - for (var value in values) { - template = template.replace(new RegExp(`%${value}%`, 'g'), values[value]); + scale(by, options = {}) { + if (typeof by === 'object') { + options = by; + by = undefined; + } + if (typeof options !== 'object') { + throw new TypeError('options must be an object'); + } + let scale = options.scale; + switch (by) { + case 'row': { + if (scale === undefined) { + scale = getScaleByRow(this); + } else if (!Array.isArray(scale)) { + throw new TypeError('scale must be an array'); + } + scaleByRow(this, scale); + return this; + } + case 'column': { + if (scale === undefined) { + scale = getScaleByColumn(this); + } else if (!Array.isArray(scale)) { + throw new TypeError('scale must be an array'); + } + scaleByColumn(this, scale); + return this; + } + case undefined: { + if (scale === undefined) { + scale = getScaleAll(this); + } else if (typeof scale !== 'number') { + throw new TypeError('scale must be a number'); + } + scaleAll(this, scale); + return this; + } + default: + throw new Error(`invalid option: ${by}`); } - return template; } - - return Matrix; } -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/matrix.js - +matrix_AbstractMatrix.prototype.klass = 'Matrix'; +if (typeof Symbol !== 'undefined') { + matrix_AbstractMatrix.prototype[ + Symbol.for('nodejs.util.inspect.custom') + ] = inspectMatrix; +} +function compareNumbers(a, b) { + return a - b; +} -class matrix_Matrix extends AbstractMatrix(Array) { +// Synonyms +matrix_AbstractMatrix.random = matrix_AbstractMatrix.rand; +matrix_AbstractMatrix.randomInt = matrix_AbstractMatrix.randInt; +matrix_AbstractMatrix.diagonal = matrix_AbstractMatrix.diag; +matrix_AbstractMatrix.prototype.diagonal = matrix_AbstractMatrix.prototype.diag; +matrix_AbstractMatrix.identity = matrix_AbstractMatrix.eye; +matrix_AbstractMatrix.prototype.negate = matrix_AbstractMatrix.prototype.neg; +matrix_AbstractMatrix.prototype.tensorProduct = + matrix_AbstractMatrix.prototype.kroneckerProduct; + +class matrix_Matrix extends matrix_AbstractMatrix { constructor(nRows, nColumns) { - var i; - if (arguments.length === 1 && typeof nRows === 'number') { - return new Array(nRows); - } + super(); if (matrix_Matrix.isMatrix(nRows)) { return nRows.clone(); } else if (Number.isInteger(nRows) && nRows > 0) { // Create an empty matrix - super(nRows); + this.data = []; if (Number.isInteger(nColumns) && nColumns > 0) { - for (i = 0; i < nRows; i++) { - this[i] = new Array(nColumns); + for (let i = 0; i < nRows; i++) { + this.data.push(new Float64Array(nColumns)); } } else { throw new TypeError('nColumns must be a positive integer'); } } else if (Array.isArray(nRows)) { // Copy the values from the 2D array - const matrix = nRows; - nRows = matrix.length; - nColumns = matrix[0].length; + const arrayData = nRows; + nRows = arrayData.length; + nColumns = arrayData[0].length; if (typeof nColumns !== 'number' || nColumns === 0) { throw new TypeError( - 'Data must be a 2D array with at least one element' + 'Data must be a 2D array with at least one element', ); } - super(nRows); - for (i = 0; i < nRows; i++) { - if (matrix[i].length !== nColumns) { + this.data = []; + for (let i = 0; i < nRows; i++) { + if (arrayData[i].length !== nColumns) { throw new RangeError('Inconsistent array dimensions'); } - this[i] = [].concat(matrix[i]); + this.data.push(Float64Array.from(arrayData[i])); } } else { throw new TypeError( - 'First argument must be a positive number or an array' + 'First argument must be a positive number or an array', ); } this.rows = nRows; @@ -5474,70 +4831,55 @@ class matrix_Matrix extends AbstractMatrix(Array) { } set(rowIndex, columnIndex, value) { - this[rowIndex][columnIndex] = value; + this.data[rowIndex][columnIndex] = value; return this; } get(rowIndex, columnIndex) { - return this[rowIndex][columnIndex]; + return this.data[rowIndex][columnIndex]; } - /** - * Removes a row from the given index - * @param {number} index - Row index - * @return {Matrix} this - */ removeRow(index) { checkRowIndex(this, index); if (this.rows === 1) { throw new RangeError('A matrix cannot have less than one row'); } - this.splice(index, 1); + this.data.splice(index, 1); this.rows -= 1; return this; } - /** - * Adds a row at the given index - * @param {number} [index = this.rows] - Row index - * @param {Array|Matrix} array - Array or vector - * @return {Matrix} this - */ addRow(index, array) { if (array === undefined) { array = index; index = this.rows; } checkRowIndex(this, index, true); - array = checkRowVector(this, array, true); - this.splice(index, 0, array); + array = Float64Array.from(checkRowVector(this, array, true)); + this.data.splice(index, 0, array); this.rows += 1; return this; } - /** - * Removes a column from the given index - * @param {number} index - Column index - * @return {Matrix} this - */ removeColumn(index) { checkColumnIndex(this, index); if (this.columns === 1) { throw new RangeError('A matrix cannot have less than one column'); } - for (var i = 0; i < this.rows; i++) { - this[i].splice(index, 1); + for (let i = 0; i < this.rows; i++) { + const newRow = new Float64Array(this.columns - 1); + for (let j = 0; j < index; j++) { + newRow[j] = this.data[i][j]; + } + for (let j = index + 1; j < this.columns; j++) { + newRow[j - 1] = this.data[i][j]; + } + this.data[i] = newRow; } this.columns -= 1; return this; } - /** - * Adds a column at the given index - * @param {number} [index = this.columns] - Column index - * @param {Array|Matrix} array - Array or vector - * @return {Matrix} this - */ addColumn(index, array) { if (typeof array === 'undefined') { array = index; @@ -5545,107 +4887,231 @@ class matrix_Matrix extends AbstractMatrix(Array) { } checkColumnIndex(this, index, true); array = checkColumnVector(this, array); - for (var i = 0; i < this.rows; i++) { - this[i].splice(index, 0, array[i]); + for (let i = 0; i < this.rows; i++) { + const newRow = new Float64Array(this.columns + 1); + let j = 0; + for (; j < index; j++) { + newRow[j] = this.data[i][j]; + } + newRow[j++] = array[i]; + for (; j < this.columns + 1; j++) { + newRow[j] = this.data[i][j - 1]; + } + this.data[i] = newRow; } this.columns += 1; return this; } } -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/wrap/WrapperMatrix1D.js - +installMathOperations(matrix_AbstractMatrix, matrix_Matrix); +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/wrap/WrapperMatrix2D.js -class WrapperMatrix1D_WrapperMatrix1D extends AbstractMatrix() { - /** - * @class WrapperMatrix1D - * @param {Array} data - * @param {object} [options] - * @param {object} [options.rows = 1] - */ - constructor(data, options = {}) { - const { rows = 1 } = options; - if (data.length % rows !== 0) { - throw new Error('the data length is not divisible by the number of rows'); - } +class WrapperMatrix2D_WrapperMatrix2D extends matrix_AbstractMatrix { + constructor(data) { super(); - this.rows = rows; - this.columns = data.length / rows; this.data = data; + this.rows = data.length; + this.columns = data[0].length; } set(rowIndex, columnIndex, value) { - var index = this._calculateIndex(rowIndex, columnIndex); - this.data[index] = value; + this.data[rowIndex][columnIndex] = value; return this; } get(rowIndex, columnIndex) { - var index = this._calculateIndex(rowIndex, columnIndex); - return this.data[index]; + return this.data[rowIndex][columnIndex]; } +} + +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/dc/lu.js + + + +class lu_LuDecomposition { + constructor(matrix) { + matrix = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(matrix); + + let lu = matrix.clone(); + let rows = lu.rows; + let columns = lu.columns; + let pivotVector = new Float64Array(rows); + let pivotSign = 1; + let i, j, k, p, s, t, v; + let LUcolj, kmax; + + for (i = 0; i < rows; i++) { + pivotVector[i] = i; + } + + LUcolj = new Float64Array(rows); + + for (j = 0; j < columns; j++) { + for (i = 0; i < rows; i++) { + LUcolj[i] = lu.get(i, j); + } + + for (i = 0; i < rows; i++) { + kmax = Math.min(i, j); + s = 0; + for (k = 0; k < kmax; k++) { + s += lu.get(i, k) * LUcolj[k]; + } + LUcolj[i] -= s; + lu.set(i, j, LUcolj[i]); + } - _calculateIndex(row, column) { - return row * this.columns + column; + p = j; + for (i = j + 1; i < rows; i++) { + if (Math.abs(LUcolj[i]) > Math.abs(LUcolj[p])) { + p = i; + } + } + + if (p !== j) { + for (k = 0; k < columns; k++) { + t = lu.get(p, k); + lu.set(p, k, lu.get(j, k)); + lu.set(j, k, t); + } + + v = pivotVector[p]; + pivotVector[p] = pivotVector[j]; + pivotVector[j] = v; + + pivotSign = -pivotSign; + } + + if (j < rows && lu.get(j, j) !== 0) { + for (i = j + 1; i < rows; i++) { + lu.set(i, j, lu.get(i, j) / lu.get(j, j)); + } + } + } + + this.LU = lu; + this.pivotVector = pivotVector; + this.pivotSign = pivotSign; } - static get [Symbol.species]() { - return matrix_Matrix; + isSingular() { + let data = this.LU; + let col = data.columns; + for (let j = 0; j < col; j++) { + if (data.get(j, j) === 0) { + return true; + } + } + return false; } -} -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/wrap/WrapperMatrix2D.js + solve(value) { + value = matrix_Matrix.checkMatrix(value); + + let lu = this.LU; + let rows = lu.rows; + + if (rows !== value.rows) { + throw new Error('Invalid matrix dimensions'); + } + if (this.isSingular()) { + throw new Error('LU matrix is singular'); + } + let count = value.columns; + let X = value.subMatrixRow(this.pivotVector, 0, count - 1); + let columns = lu.columns; + let i, j, k; + for (k = 0; k < columns; k++) { + for (i = k + 1; i < columns; i++) { + for (j = 0; j < count; j++) { + X.set(i, j, X.get(i, j) - X.get(k, j) * lu.get(i, k)); + } + } + } + for (k = columns - 1; k >= 0; k--) { + for (j = 0; j < count; j++) { + X.set(k, j, X.get(k, j) / lu.get(k, k)); + } + for (i = 0; i < k; i++) { + for (j = 0; j < count; j++) { + X.set(i, j, X.get(i, j) - X.get(k, j) * lu.get(i, k)); + } + } + } + return X; + } -class WrapperMatrix2D_WrapperMatrix2D extends AbstractMatrix() { - /** - * @class WrapperMatrix2D - * @param {Array>} data - */ - constructor(data) { - super(); - this.data = data; - this.rows = data.length; - this.columns = data[0].length; + get determinant() { + let data = this.LU; + if (!data.isSquare()) { + throw new Error('Matrix must be square'); + } + let determinant = this.pivotSign; + let col = data.columns; + for (let j = 0; j < col; j++) { + determinant *= data.get(j, j); + } + return determinant; } - set(rowIndex, columnIndex, value) { - this.data[rowIndex][columnIndex] = value; - return this; + get lowerTriangularMatrix() { + let data = this.LU; + let rows = data.rows; + let columns = data.columns; + let X = new matrix_Matrix(rows, columns); + for (let i = 0; i < rows; i++) { + for (let j = 0; j < columns; j++) { + if (i > j) { + X.set(i, j, data.get(i, j)); + } else if (i === j) { + X.set(i, j, 1); + } else { + X.set(i, j, 0); + } + } + } + return X; } - get(rowIndex, columnIndex) { - return this.data[rowIndex][columnIndex]; + get upperTriangularMatrix() { + let data = this.LU; + let rows = data.rows; + let columns = data.columns; + let X = new matrix_Matrix(rows, columns); + for (let i = 0; i < rows; i++) { + for (let j = 0; j < columns; j++) { + if (i <= j) { + X.set(i, j, data.get(i, j)); + } else { + X.set(i, j, 0); + } + } + } + return X; } - static get [Symbol.species]() { - return matrix_Matrix; + get pivotPermutationVector() { + return Array.from(this.pivotVector); } } -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/wrap/wrap.js - - - -/** - * @param {Array>|Array} array - * @param {object} [options] - * @param {object} [options.rows = 1] - * @return {WrapperMatrix1D|WrapperMatrix2D} - */ -function wrap(array, options) { - if (Array.isArray(array)) { - if (array[0] && Array.isArray(array[0])) { - return new WrapperMatrix2D_WrapperMatrix2D(array); - } else { - return new WrapperMatrix1D_WrapperMatrix1D(array, options); - } - } else { - throw new Error('the argument is not an array'); +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/dc/util.js +function hypotenuse(a, b) { + let r = 0; + if (Math.abs(a) > Math.abs(b)) { + r = b / a; + return Math.abs(a) * Math.sqrt(1 + r * r); + } + if (b !== 0) { + r = a / b; + return Math.abs(b) * Math.sqrt(1 + r * r); } + return 0; } // CONCATENATED MODULE: ./node_modules/ml-matrix/src/dc/qr.js @@ -5653,23 +5119,19 @@ function wrap(array, options) { -/** - * @class QrDecomposition - * @link https://github.com/lutzroeder/Mapack/blob/master/Source/QrDecomposition.cs - * @param {Matrix} value - */ + class qr_QrDecomposition { constructor(value) { value = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(value); - var qr = value.clone(); - var m = value.rows; - var n = value.columns; - var rdiag = new Array(n); - var i, j, k, s; + let qr = value.clone(); + let m = value.rows; + let n = value.columns; + let rdiag = new Float64Array(n); + let i, j, k, s; for (k = 0; k < n; k++) { - var nrm = 0; + let nrm = 0; for (i = k; i < m; i++) { nrm = hypotenuse(nrm, qr.get(i, k)); } @@ -5699,19 +5161,11 @@ class qr_QrDecomposition { this.Rdiag = rdiag; } - /** - * Solve a problem of least square (Ax=b) by using the QR decomposition. Useful when A is rectangular, but not working when A is singular. - * Example : We search to approximate x, with A matrix shape m*n, x vector size n, b vector size m (m > n). We will use : - * var qr = QrDecomposition(A); - * var x = qr.solve(b); - * @param {Matrix} value - Matrix 1D which is the vector b (in the equation Ax = b) - * @return {Matrix} - The vector x - */ solve(value) { value = matrix_Matrix.checkMatrix(value); - var qr = this.QR; - var m = qr.rows; + let qr = this.QR; + let m = qr.rows; if (value.rows !== m) { throw new Error('Matrix row dimensions must agree'); @@ -5720,30 +5174,30 @@ class qr_QrDecomposition { throw new Error('Matrix is rank deficient'); } - var count = value.columns; - var X = value.clone(); - var n = qr.columns; - var i, j, k, s; + let count = value.columns; + let X = value.clone(); + let n = qr.columns; + let i, j, k, s; for (k = 0; k < n; k++) { for (j = 0; j < count; j++) { s = 0; for (i = k; i < m; i++) { - s += qr[i][k] * X[i][j]; + s += qr.get(i, k) * X.get(i, j); } - s = -s / qr[k][k]; + s = -s / qr.get(k, k); for (i = k; i < m; i++) { - X[i][j] += s * qr[i][k]; + X.set(i, j, X.get(i, j) + s * qr.get(i, k)); } } } for (k = n - 1; k >= 0; k--) { for (j = 0; j < count; j++) { - X[k][j] /= this.Rdiag[k]; + X.set(k, j, X.get(k, j) / this.Rdiag[k]); } for (i = 0; i < k; i++) { for (j = 0; j < count; j++) { - X[i][j] -= X[k][j] * qr[i][k]; + X.set(i, j, X.get(i, j) - X.get(k, j) * qr.get(i, k)); } } } @@ -5751,13 +5205,9 @@ class qr_QrDecomposition { return X.subMatrix(0, n - 1, 0, count - 1); } - /** - * - * @return {boolean} - */ isFullRank() { - var columns = this.QR.columns; - for (var i = 0; i < columns; i++) { + let columns = this.QR.columns; + for (let i = 0; i < columns; i++) { if (this.Rdiag[i] === 0) { return false; } @@ -5765,56 +5215,48 @@ class qr_QrDecomposition { return true; } - /** - * - * @return {Matrix} - */ get upperTriangularMatrix() { - var qr = this.QR; - var n = qr.columns; - var X = new matrix_Matrix(n, n); - var i, j; + let qr = this.QR; + let n = qr.columns; + let X = new matrix_Matrix(n, n); + let i, j; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (i < j) { - X[i][j] = qr[i][j]; + X.set(i, j, qr.get(i, j)); } else if (i === j) { - X[i][j] = this.Rdiag[i]; + X.set(i, j, this.Rdiag[i]); } else { - X[i][j] = 0; + X.set(i, j, 0); } } } return X; } - /** - * - * @return {Matrix} - */ get orthogonalMatrix() { - var qr = this.QR; - var rows = qr.rows; - var columns = qr.columns; - var X = new matrix_Matrix(rows, columns); - var i, j, k, s; + let qr = this.QR; + let rows = qr.rows; + let columns = qr.columns; + let X = new matrix_Matrix(rows, columns); + let i, j, k, s; for (k = columns - 1; k >= 0; k--) { for (i = 0; i < rows; i++) { - X[i][k] = 0; + X.set(i, k, 0); } - X[k][k] = 1; + X.set(k, k, 1); for (j = k; j < columns; j++) { - if (qr[k][k] !== 0) { + if (qr.get(k, k) !== 0) { s = 0; for (i = k; i < rows; i++) { - s += qr[i][k] * X[i][j]; + s += qr.get(i, k) * X.get(i, j); } - s = -s / qr[k][k]; + s = -s / qr.get(k, k); for (i = k; i < rows; i++) { - X[i][j] += s * qr[i][k]; + X.set(i, j, X.get(i, j) + s * qr.get(i, k)); } } } @@ -5823,1061 +5265,763 @@ class qr_QrDecomposition { } } -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/decompositions.js - - - - - +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/dc/svd.js -/** - * Computes the inverse of a Matrix - * @param {Matrix} matrix - * @param {boolean} [useSVD=false] - * @return {Matrix} - */ -function inverse(matrix, useSVD = false) { - matrix = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(matrix); - if (useSVD) { - return new svd_SingularValueDecomposition(matrix).inverse(); - } else { - return solve(matrix, matrix_Matrix.eye(matrix.rows)); - } -} -/** - * - * @param {Matrix} leftHandSide - * @param {Matrix} rightHandSide - * @param {boolean} [useSVD = false] - * @return {Matrix} - */ -function solve(leftHandSide, rightHandSide, useSVD = false) { - leftHandSide = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(leftHandSide); - rightHandSide = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(rightHandSide); - if (useSVD) { - return new svd_SingularValueDecomposition(leftHandSide).solve(rightHandSide); - } else { - return leftHandSide.isSquare() - ? new lu_LuDecomposition(leftHandSide).solve(rightHandSide) - : new qr_QrDecomposition(leftHandSide).solve(rightHandSide); - } -} -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/linearDependencies.js +class svd_SingularValueDecomposition { + constructor(value, options = {}) { + value = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(value); + let m = value.rows; + let n = value.columns; + const { + computeLeftSingularVectors = true, + computeRightSingularVectors = true, + autoTranspose = false, + } = options; -// function used by rowsDependencies -function xrange(n, exception) { - var range = []; - for (var i = 0; i < n; i++) { - if (i !== exception) { - range.push(i); - } - } - return range; -} + let wantu = Boolean(computeLeftSingularVectors); + let wantv = Boolean(computeRightSingularVectors); -// function used by rowsDependencies -function dependenciesOneRow( - error, - matrix, - index, - thresholdValue = 10e-10, - thresholdError = 10e-10 -) { - if (error > thresholdError) { - return new Array(matrix.rows + 1).fill(0); - } else { - var returnArray = matrix.addRow(index, [0]); - for (var i = 0; i < returnArray.rows; i++) { - if (Math.abs(returnArray.get(i, 0)) < thresholdValue) { - returnArray.set(i, 0, 0); + let swapped = false; + let a; + if (m < n) { + if (!autoTranspose) { + a = value.clone(); + // eslint-disable-next-line no-console + console.warn( + 'Computing SVD on a matrix with more columns than rows. Consider enabling autoTranspose', + ); + } else { + a = value.transpose(); + m = a.rows; + n = a.columns; + swapped = true; + let aux = wantu; + wantu = wantv; + wantv = aux; } + } else { + a = value.clone(); } - return returnArray.to1DArray(); - } -} - -/** - * Creates a matrix which represents the dependencies between rows. - * If a row is a linear combination of others rows, the result will be a row with the coefficients of this combination. - * For example : for A = [[2, 0, 0, 1], [0, 1, 6, 0], [0, 3, 0, 1], [0, 0, 1, 0], [0, 1, 2, 0]], the result will be [[0, 0, 0, 0, 0], [0, 0, 0, 4, 1], [0, 0, 0, 0, 0], [0, 0.25, 0, 0, -0.25], [0, 1, 0, -4, 0]] - * @param {Matrix} matrix - * @param {Object} [options] includes thresholdValue and thresholdError. - * @param {number} [options.thresholdValue = 10e-10] If an absolute value is inferior to this threshold, it will equals zero. - * @param {number} [options.thresholdError = 10e-10] If the error is inferior to that threshold, the linear combination found is accepted and the row is dependent from other rows. - * @return {Matrix} the matrix which represents the dependencies between rows. - */ - -function linearDependencies(matrix, options = {}) { - const { thresholdValue = 10e-10, thresholdError = 10e-10 } = options; - - var n = matrix.rows; - var results = new matrix_Matrix(n, n); - - for (var i = 0; i < n; i++) { - var b = matrix_Matrix.columnVector(matrix.getRow(i)); - var Abis = matrix.subMatrixRow(xrange(n, i)).transposeView(); - var svd = new svd_SingularValueDecomposition(Abis); - var x = svd.solve(b); - var error = lib_es6( - matrix_Matrix.sub(b, Abis.mmul(x)) - .abs() - .to1DArray() - ); - results.setRow( - i, - dependenciesOneRow(error, x, i, thresholdValue, thresholdError) - ); - } - return results; -} - -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/dc/evd.js - - + let nu = Math.min(m, n); + let ni = Math.min(m + 1, n); + let s = new Float64Array(ni); + let U = new matrix_Matrix(m, nu); + let V = new matrix_Matrix(n, n); -/** - * @class EigenvalueDecomposition - * @link https://github.com/lutzroeder/Mapack/blob/master/Source/EigenvalueDecomposition.cs - * @param {Matrix} matrix - * @param {object} [options] - * @param {boolean} [options.assumeSymmetric=false] - */ -class evd_EigenvalueDecomposition { - constructor(matrix, options = {}) { - const { assumeSymmetric = false } = options; - - matrix = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(matrix); - if (!matrix.isSquare()) { - throw new Error('Matrix is not a square matrix'); - } + let e = new Float64Array(n); + let work = new Float64Array(m); - var n = matrix.columns; - var V = getFilled2DArray(n, n, 0); - var d = new Array(n); - var e = new Array(n); - var value = matrix; - var i, j; + let si = new Float64Array(ni); + for (let i = 0; i < ni; i++) si[i] = i; - var isSymmetric = false; - if (assumeSymmetric) { - isSymmetric = true; - } else { - isSymmetric = matrix.isSymmetric(); - } + let nct = Math.min(m - 1, n); + let nrt = Math.max(0, Math.min(n - 2, m)); + let mrc = Math.max(nct, nrt); - if (isSymmetric) { - for (i = 0; i < n; i++) { - for (j = 0; j < n; j++) { - V[i][j] = value.get(i, j); + for (let k = 0; k < mrc; k++) { + if (k < nct) { + s[k] = 0; + for (let i = k; i < m; i++) { + s[k] = hypotenuse(s[k], a.get(i, k)); } - } - tred2(n, e, d, V); - tql2(n, e, d, V); - } else { - var H = getFilled2DArray(n, n, 0); - var ort = new Array(n); - for (j = 0; j < n; j++) { - for (i = 0; i < n; i++) { - H[i][j] = value.get(i, j); + if (s[k] !== 0) { + if (a.get(k, k) < 0) { + s[k] = -s[k]; + } + for (let i = k; i < m; i++) { + a.set(i, k, a.get(i, k) / s[k]); + } + a.set(k, k, a.get(k, k) + 1); } - } - orthes(n, H, ort, V); - hqr2(n, e, d, V, H); - } - - this.n = n; - this.e = e; - this.d = d; - this.V = V; - } - - /** - * - * @return {Array} - */ - get realEigenvalues() { - return this.d; - } - - /** - * - * @return {Array} - */ - get imaginaryEigenvalues() { - return this.e; - } - - /** - * - * @return {Matrix} - */ - get eigenvectorMatrix() { - if (!matrix_Matrix.isMatrix(this.V)) { - this.V = new matrix_Matrix(this.V); - } - return this.V; - } - - /** - * - * @return {Matrix} - */ - get diagonalMatrix() { - var n = this.n; - var e = this.e; - var d = this.d; - var X = new matrix_Matrix(n, n); - var i, j; - for (i = 0; i < n; i++) { - for (j = 0; j < n; j++) { - X[i][j] = 0; - } - X[i][i] = d[i]; - if (e[i] > 0) { - X[i][i + 1] = e[i]; - } else if (e[i] < 0) { - X[i][i - 1] = e[i]; - } - } - return X; - } -} - -function tred2(n, e, d, V) { - var f, g, h, i, j, k, hh, scale; - - for (j = 0; j < n; j++) { - d[j] = V[n - 1][j]; - } - - for (i = n - 1; i > 0; i--) { - scale = 0; - h = 0; - for (k = 0; k < i; k++) { - scale = scale + Math.abs(d[k]); - } - - if (scale === 0) { - e[i] = d[i - 1]; - for (j = 0; j < i; j++) { - d[j] = V[i - 1][j]; - V[i][j] = 0; - V[j][i] = 0; - } - } else { - for (k = 0; k < i; k++) { - d[k] /= scale; - h += d[k] * d[k]; - } - - f = d[i - 1]; - g = Math.sqrt(h); - if (f > 0) { - g = -g; - } - - e[i] = scale * g; - h = h - f * g; - d[i - 1] = f - g; - for (j = 0; j < i; j++) { - e[j] = 0; + s[k] = -s[k]; } - for (j = 0; j < i; j++) { - f = d[j]; - V[j][i] = f; - g = e[j] + V[j][j] * f; - for (k = j + 1; k <= i - 1; k++) { - g += V[k][j] * d[k]; - e[k] += V[k][j] * f; + for (let j = k + 1; j < n; j++) { + if (k < nct && s[k] !== 0) { + let t = 0; + for (let i = k; i < m; i++) { + t += a.get(i, k) * a.get(i, j); + } + t = -t / a.get(k, k); + for (let i = k; i < m; i++) { + a.set(i, j, a.get(i, j) + t * a.get(i, k)); + } } - e[j] = g; - } - - f = 0; - for (j = 0; j < i; j++) { - e[j] /= h; - f += e[j] * d[j]; - } - - hh = f / (h + h); - for (j = 0; j < i; j++) { - e[j] -= hh * d[j]; + e[j] = a.get(k, j); } - for (j = 0; j < i; j++) { - f = d[j]; - g = e[j]; - for (k = j; k <= i - 1; k++) { - V[k][j] -= f * e[k] + g * d[k]; + if (wantu && k < nct) { + for (let i = k; i < m; i++) { + U.set(i, k, a.get(i, k)); } - d[j] = V[i - 1][j]; - V[i][j] = 0; - } - } - d[i] = h; - } - - for (i = 0; i < n - 1; i++) { - V[n - 1][i] = V[i][i]; - V[i][i] = 1; - h = d[i + 1]; - if (h !== 0) { - for (k = 0; k <= i; k++) { - d[k] = V[k][i + 1] / h; } - for (j = 0; j <= i; j++) { - g = 0; - for (k = 0; k <= i; k++) { - g += V[k][i + 1] * V[k][j]; + if (k < nrt) { + e[k] = 0; + for (let i = k + 1; i < n; i++) { + e[k] = hypotenuse(e[k], e[i]); + } + if (e[k] !== 0) { + if (e[k + 1] < 0) { + e[k] = 0 - e[k]; + } + for (let i = k + 1; i < n; i++) { + e[i] /= e[k]; + } + e[k + 1] += 1; + } + e[k] = -e[k]; + if (k + 1 < m && e[k] !== 0) { + for (let i = k + 1; i < m; i++) { + work[i] = 0; + } + for (let i = k + 1; i < m; i++) { + for (let j = k + 1; j < n; j++) { + work[i] += e[j] * a.get(i, j); + } + } + for (let j = k + 1; j < n; j++) { + let t = -e[j] / e[k + 1]; + for (let i = k + 1; i < m; i++) { + a.set(i, j, a.get(i, j) + t * work[i]); + } + } } - for (k = 0; k <= i; k++) { - V[k][j] -= g * d[k]; + if (wantv) { + for (let i = k + 1; i < n; i++) { + V.set(i, k, e[i]); + } } } } - for (k = 0; k <= i; k++) { - V[k][i + 1] = 0; + let p = Math.min(n, m + 1); + if (nct < n) { + s[nct] = a.get(nct, nct); } - } - - for (j = 0; j < n; j++) { - d[j] = V[n - 1][j]; - V[n - 1][j] = 0; - } - - V[n - 1][n - 1] = 1; - e[0] = 0; -} - -function tql2(n, e, d, V) { - var g, h, i, j, k, l, m, p, r, dl1, c, c2, c3, el1, s, s2, iter; - - for (i = 1; i < n; i++) { - e[i - 1] = e[i]; - } - - e[n - 1] = 0; - - var f = 0; - var tst1 = 0; - var eps = Number.EPSILON; - - for (l = 0; l < n; l++) { - tst1 = Math.max(tst1, Math.abs(d[l]) + Math.abs(e[l])); - m = l; - while (m < n) { - if (Math.abs(e[m]) <= eps * tst1) { - break; - } - m++; + if (m < p) { + s[p - 1] = 0; } + if (nrt + 1 < p) { + e[nrt] = a.get(nrt, p - 1); + } + e[p - 1] = 0; - if (m > l) { - iter = 0; - do { - iter = iter + 1; - - g = d[l]; - p = (d[l + 1] - g) / (2 * e[l]); - r = hypotenuse(p, 1); - if (p < 0) { - r = -r; - } - - d[l] = e[l] / (p + r); - d[l + 1] = e[l] * (p + r); - dl1 = d[l + 1]; - h = g - d[l]; - for (i = l + 2; i < n; i++) { - d[i] -= h; + if (wantu) { + for (let j = nct; j < nu; j++) { + for (let i = 0; i < m; i++) { + U.set(i, j, 0); } - - f = f + h; - - p = d[m]; - c = 1; - c2 = c; - c3 = c; - el1 = e[l + 1]; - s = 0; - s2 = 0; - for (i = m - 1; i >= l; i--) { - c3 = c2; - c2 = c; - s2 = s; - g = c * e[i]; - h = c * p; - r = hypotenuse(p, e[i]); - e[i + 1] = s * r; - s = e[i] / r; - c = p / r; - p = c * d[i] - s * g; - d[i + 1] = h + s * (c * g + s * d[i]); - - for (k = 0; k < n; k++) { - h = V[k][i + 1]; - V[k][i + 1] = s * V[k][i] + c * h; - V[k][i] = c * V[k][i] - s * h; + U.set(j, j, 1); + } + for (let k = nct - 1; k >= 0; k--) { + if (s[k] !== 0) { + for (let j = k + 1; j < nu; j++) { + let t = 0; + for (let i = k; i < m; i++) { + t += U.get(i, k) * U.get(i, j); + } + t = -t / U.get(k, k); + for (let i = k; i < m; i++) { + U.set(i, j, U.get(i, j) + t * U.get(i, k)); + } + } + for (let i = k; i < m; i++) { + U.set(i, k, -U.get(i, k)); + } + U.set(k, k, 1 + U.get(k, k)); + for (let i = 0; i < k - 1; i++) { + U.set(i, k, 0); + } + } else { + for (let i = 0; i < m; i++) { + U.set(i, k, 0); } + U.set(k, k, 1); } - - p = -s * s2 * c3 * el1 * e[l] / dl1; - e[l] = s * p; - d[l] = c * p; - } while (Math.abs(e[l]) > eps * tst1); - } - d[l] = d[l] + f; - e[l] = 0; - } - - for (i = 0; i < n - 1; i++) { - k = i; - p = d[i]; - for (j = i + 1; j < n; j++) { - if (d[j] < p) { - k = j; - p = d[j]; } } - if (k !== i) { - d[k] = d[i]; - d[i] = p; - for (j = 0; j < n; j++) { - p = V[j][i]; - V[j][i] = V[j][k]; - V[j][k] = p; + if (wantv) { + for (let k = n - 1; k >= 0; k--) { + if (k < nrt && e[k] !== 0) { + for (let j = k + 1; j < n; j++) { + let t = 0; + for (let i = k + 1; i < n; i++) { + t += V.get(i, k) * V.get(i, j); + } + t = -t / V.get(k + 1, k); + for (let i = k + 1; i < n; i++) { + V.set(i, j, V.get(i, j) + t * V.get(i, k)); + } + } + } + for (let i = 0; i < n; i++) { + V.set(i, k, 0); + } + V.set(k, k, 1); } } - } -} - -function orthes(n, H, ort, V) { - var low = 0; - var high = n - 1; - var f, g, h, i, j, m; - var scale; - - for (m = low + 1; m <= high - 1; m++) { - scale = 0; - for (i = m; i <= high; i++) { - scale = scale + Math.abs(H[i][m - 1]); - } - if (scale !== 0) { - h = 0; - for (i = high; i >= m; i--) { - ort[i] = H[i][m - 1] / scale; - h += ort[i] * ort[i]; + let pp = p - 1; + let iter = 0; + let eps = Number.EPSILON; + while (p > 0) { + let k, kase; + for (k = p - 2; k >= -1; k--) { + if (k === -1) { + break; + } + const alpha = + Number.MIN_VALUE + eps * Math.abs(s[k] + Math.abs(s[k + 1])); + if (Math.abs(e[k]) <= alpha || Number.isNaN(e[k])) { + e[k] = 0; + break; + } } - - g = Math.sqrt(h); - if (ort[m] > 0) { - g = -g; + if (k === p - 2) { + kase = 4; + } else { + let ks; + for (ks = p - 1; ks >= k; ks--) { + if (ks === k) { + break; + } + let t = + (ks !== p ? Math.abs(e[ks]) : 0) + + (ks !== k + 1 ? Math.abs(e[ks - 1]) : 0); + if (Math.abs(s[ks]) <= eps * t) { + s[ks] = 0; + break; + } + } + if (ks === k) { + kase = 3; + } else if (ks === p - 1) { + kase = 1; + } else { + kase = 2; + k = ks; + } } - h = h - ort[m] * g; - ort[m] = ort[m] - g; + k++; - for (j = m; j < n; j++) { - f = 0; - for (i = high; i >= m; i--) { - f += ort[i] * H[i][j]; + switch (kase) { + case 1: { + let f = e[p - 2]; + e[p - 2] = 0; + for (let j = p - 2; j >= k; j--) { + let t = hypotenuse(s[j], f); + let cs = s[j] / t; + let sn = f / t; + s[j] = t; + if (j !== k) { + f = -sn * e[j - 1]; + e[j - 1] = cs * e[j - 1]; + } + if (wantv) { + for (let i = 0; i < n; i++) { + t = cs * V.get(i, j) + sn * V.get(i, p - 1); + V.set(i, p - 1, -sn * V.get(i, j) + cs * V.get(i, p - 1)); + V.set(i, j, t); + } + } + } + break; } - - f = f / h; - for (i = m; i <= high; i++) { - H[i][j] -= f * ort[i]; + case 2: { + let f = e[k - 1]; + e[k - 1] = 0; + for (let j = k; j < p; j++) { + let t = hypotenuse(s[j], f); + let cs = s[j] / t; + let sn = f / t; + s[j] = t; + f = -sn * e[j]; + e[j] = cs * e[j]; + if (wantu) { + for (let i = 0; i < m; i++) { + t = cs * U.get(i, j) + sn * U.get(i, k - 1); + U.set(i, k - 1, -sn * U.get(i, j) + cs * U.get(i, k - 1)); + U.set(i, j, t); + } + } + } + break; } - } - - for (i = 0; i <= high; i++) { - f = 0; - for (j = high; j >= m; j--) { - f += ort[j] * H[i][j]; + case 3: { + const scale = Math.max( + Math.abs(s[p - 1]), + Math.abs(s[p - 2]), + Math.abs(e[p - 2]), + Math.abs(s[k]), + Math.abs(e[k]), + ); + const sp = s[p - 1] / scale; + const spm1 = s[p - 2] / scale; + const epm1 = e[p - 2] / scale; + const sk = s[k] / scale; + const ek = e[k] / scale; + const b = ((spm1 + sp) * (spm1 - sp) + epm1 * epm1) / 2; + const c = sp * epm1 * (sp * epm1); + let shift = 0; + if (b !== 0 || c !== 0) { + if (b < 0) { + shift = 0 - Math.sqrt(b * b + c); + } else { + shift = Math.sqrt(b * b + c); + } + shift = c / (b + shift); + } + let f = (sk + sp) * (sk - sp) + shift; + let g = sk * ek; + for (let j = k; j < p - 1; j++) { + let t = hypotenuse(f, g); + if (t === 0) t = Number.MIN_VALUE; + let cs = f / t; + let sn = g / t; + if (j !== k) { + e[j - 1] = t; + } + f = cs * s[j] + sn * e[j]; + e[j] = cs * e[j] - sn * s[j]; + g = sn * s[j + 1]; + s[j + 1] = cs * s[j + 1]; + if (wantv) { + for (let i = 0; i < n; i++) { + t = cs * V.get(i, j) + sn * V.get(i, j + 1); + V.set(i, j + 1, -sn * V.get(i, j) + cs * V.get(i, j + 1)); + V.set(i, j, t); + } + } + t = hypotenuse(f, g); + if (t === 0) t = Number.MIN_VALUE; + cs = f / t; + sn = g / t; + s[j] = t; + f = cs * e[j] + sn * s[j + 1]; + s[j + 1] = -sn * e[j] + cs * s[j + 1]; + g = sn * e[j + 1]; + e[j + 1] = cs * e[j + 1]; + if (wantu && j < m - 1) { + for (let i = 0; i < m; i++) { + t = cs * U.get(i, j) + sn * U.get(i, j + 1); + U.set(i, j + 1, -sn * U.get(i, j) + cs * U.get(i, j + 1)); + U.set(i, j, t); + } + } + } + e[p - 2] = f; + iter = iter + 1; + break; } - - f = f / h; - for (j = m; j <= high; j++) { - H[i][j] -= f * ort[j]; + case 4: { + if (s[k] <= 0) { + s[k] = s[k] < 0 ? -s[k] : 0; + if (wantv) { + for (let i = 0; i <= pp; i++) { + V.set(i, k, -V.get(i, k)); + } + } + } + while (k < pp) { + if (s[k] >= s[k + 1]) { + break; + } + let t = s[k]; + s[k] = s[k + 1]; + s[k + 1] = t; + if (wantv && k < n - 1) { + for (let i = 0; i < n; i++) { + t = V.get(i, k + 1); + V.set(i, k + 1, V.get(i, k)); + V.set(i, k, t); + } + } + if (wantu && k < m - 1) { + for (let i = 0; i < m; i++) { + t = U.get(i, k + 1); + U.set(i, k + 1, U.get(i, k)); + U.set(i, k, t); + } + } + k++; + } + iter = 0; + p--; + break; } + // no default } - - ort[m] = scale * ort[m]; - H[m][m - 1] = scale * g; } - } - for (i = 0; i < n; i++) { - for (j = 0; j < n; j++) { - V[i][j] = i === j ? 1 : 0; + if (swapped) { + let tmp = V; + V = U; + U = tmp; } - } - for (m = high - 1; m >= low + 1; m--) { - if (H[m][m - 1] !== 0) { - for (i = m + 1; i <= high; i++) { - ort[i] = H[i][m - 1]; - } + this.m = m; + this.n = n; + this.s = s; + this.U = U; + this.V = V; + } - for (j = m; j <= high; j++) { - g = 0; - for (i = m; i <= high; i++) { - g += ort[i] * V[i][j]; - } + solve(value) { + let Y = value; + let e = this.threshold; + let scols = this.s.length; + let Ls = matrix_Matrix.zeros(scols, scols); - g = g / ort[m] / H[m][m - 1]; - for (i = m; i <= high; i++) { - V[i][j] += g * ort[i]; - } + for (let i = 0; i < scols; i++) { + if (Math.abs(this.s[i]) <= e) { + Ls.set(i, i, 0); + } else { + Ls.set(i, i, 1 / this.s[i]); } } - } -} -function hqr2(nn, e, d, V, H) { - var n = nn - 1; - var low = 0; - var high = nn - 1; - var eps = Number.EPSILON; - var exshift = 0; - var norm = 0; - var p = 0; - var q = 0; - var r = 0; - var s = 0; - var z = 0; - var iter = 0; - var i, j, k, l, m, t, w, x, y; - var ra, sa, vr, vi; - var notlast, cdivres; - - for (i = 0; i < nn; i++) { - if (i < low || i > high) { - d[i] = H[i][i]; - e[i] = 0; - } - - for (j = Math.max(i - 1, 0); j < nn; j++) { - norm = norm + Math.abs(H[i][j]); - } - } - - while (n >= low) { - l = n; - while (l > low) { - s = Math.abs(H[l - 1][l - 1]) + Math.abs(H[l][l]); - if (s === 0) { - s = norm; - } - if (Math.abs(H[l][l - 1]) < eps * s) { - break; - } - l--; - } - - if (l === n) { - H[n][n] = H[n][n] + exshift; - d[n] = H[n][n]; - e[n] = 0; - n--; - iter = 0; - } else if (l === n - 1) { - w = H[n][n - 1] * H[n - 1][n]; - p = (H[n - 1][n - 1] - H[n][n]) / 2; - q = p * p + w; - z = Math.sqrt(Math.abs(q)); - H[n][n] = H[n][n] + exshift; - H[n - 1][n - 1] = H[n - 1][n - 1] + exshift; - x = H[n][n]; - - if (q >= 0) { - z = p >= 0 ? p + z : p - z; - d[n - 1] = x + z; - d[n] = d[n - 1]; - if (z !== 0) { - d[n] = x - w / z; - } - e[n - 1] = 0; - e[n] = 0; - x = H[n][n - 1]; - s = Math.abs(x) + Math.abs(z); - p = x / s; - q = z / s; - r = Math.sqrt(p * p + q * q); - p = p / r; - q = q / r; - - for (j = n - 1; j < nn; j++) { - z = H[n - 1][j]; - H[n - 1][j] = q * z + p * H[n][j]; - H[n][j] = q * H[n][j] - p * z; - } - - for (i = 0; i <= n; i++) { - z = H[i][n - 1]; - H[i][n - 1] = q * z + p * H[i][n]; - H[i][n] = q * H[i][n] - p * z; - } - - for (i = low; i <= high; i++) { - z = V[i][n - 1]; - V[i][n - 1] = q * z + p * V[i][n]; - V[i][n] = q * V[i][n] - p * z; - } - } else { - d[n - 1] = x + p; - d[n] = x + p; - e[n - 1] = z; - e[n] = -z; - } + let U = this.U; + let V = this.rightSingularVectors; - n = n - 2; - iter = 0; - } else { - x = H[n][n]; - y = 0; - w = 0; - if (l < n) { - y = H[n - 1][n - 1]; - w = H[n][n - 1] * H[n - 1][n]; - } - - if (iter === 10) { - exshift += x; - for (i = low; i <= n; i++) { - H[i][i] -= x; - } - s = Math.abs(H[n][n - 1]) + Math.abs(H[n - 1][n - 2]); - x = y = 0.75 * s; - w = -0.4375 * s * s; - } - - if (iter === 30) { - s = (y - x) / 2; - s = s * s + w; - if (s > 0) { - s = Math.sqrt(s); - if (y < x) { - s = -s; - } - s = x - w / ((y - x) / 2 + s); - for (i = low; i <= n; i++) { - H[i][i] -= s; - } - exshift += s; - x = y = w = 0.964; - } - } - - iter = iter + 1; - - m = n - 2; - while (m >= l) { - z = H[m][m]; - r = x - z; - s = y - z; - p = (r * s - w) / H[m + 1][m] + H[m][m + 1]; - q = H[m + 1][m + 1] - z - r - s; - r = H[m + 2][m + 1]; - s = Math.abs(p) + Math.abs(q) + Math.abs(r); - p = p / s; - q = q / s; - r = r / s; - if (m === l) { - break; - } - if ( - Math.abs(H[m][m - 1]) * (Math.abs(q) + Math.abs(r)) < - eps * - (Math.abs(p) * - (Math.abs(H[m - 1][m - 1]) + - Math.abs(z) + - Math.abs(H[m + 1][m + 1]))) - ) { - break; - } - m--; - } + let VL = V.mmul(Ls); + let vrows = V.rows; + let urows = U.rows; + let VLU = matrix_Matrix.zeros(vrows, urows); - for (i = m + 2; i <= n; i++) { - H[i][i - 2] = 0; - if (i > m + 2) { - H[i][i - 3] = 0; + for (let i = 0; i < vrows; i++) { + for (let j = 0; j < urows; j++) { + let sum = 0; + for (let k = 0; k < scols; k++) { + sum += VL.get(i, k) * U.get(j, k); } + VLU.set(i, j, sum); } + } - for (k = m; k <= n - 1; k++) { - notlast = k !== n - 1; - if (k !== m) { - p = H[k][k - 1]; - q = H[k + 1][k - 1]; - r = notlast ? H[k + 2][k - 1] : 0; - x = Math.abs(p) + Math.abs(q) + Math.abs(r); - if (x !== 0) { - p = p / x; - q = q / x; - r = r / x; - } - } + return VLU.mmul(Y); + } - if (x === 0) { - break; - } + solveForDiagonal(value) { + return this.solve(matrix_Matrix.diag(value)); + } + + inverse() { + let V = this.V; + let e = this.threshold; + let vrows = V.rows; + let vcols = V.columns; + let X = new matrix_Matrix(vrows, this.s.length); - s = Math.sqrt(p * p + q * q + r * r); - if (p < 0) { - s = -s; + for (let i = 0; i < vrows; i++) { + for (let j = 0; j < vcols; j++) { + if (Math.abs(this.s[j]) > e) { + X.set(i, j, V.get(i, j) / this.s[j]); } + } + } - if (s !== 0) { - if (k !== m) { - H[k][k - 1] = -s * x; - } else if (l !== m) { - H[k][k - 1] = -H[k][k - 1]; - } + let U = this.U; - p = p + s; - x = p / s; - y = q / s; - z = r / s; - q = q / p; - r = r / p; - - for (j = k; j < nn; j++) { - p = H[k][j] + q * H[k + 1][j]; - if (notlast) { - p = p + r * H[k + 2][j]; - H[k + 2][j] = H[k + 2][j] - p * z; - } + let urows = U.rows; + let ucols = U.columns; + let Y = new matrix_Matrix(vrows, urows); - H[k][j] = H[k][j] - p * x; - H[k + 1][j] = H[k + 1][j] - p * y; - } + for (let i = 0; i < vrows; i++) { + for (let j = 0; j < urows; j++) { + let sum = 0; + for (let k = 0; k < ucols; k++) { + sum += X.get(i, k) * U.get(j, k); + } + Y.set(i, j, sum); + } + } - for (i = 0; i <= Math.min(n, k + 3); i++) { - p = x * H[i][k] + y * H[i][k + 1]; - if (notlast) { - p = p + z * H[i][k + 2]; - H[i][k + 2] = H[i][k + 2] - p * r; - } + return Y; + } - H[i][k] = H[i][k] - p; - H[i][k + 1] = H[i][k + 1] - p * q; - } + get condition() { + return this.s[0] / this.s[Math.min(this.m, this.n) - 1]; + } - for (i = low; i <= high; i++) { - p = x * V[i][k] + y * V[i][k + 1]; - if (notlast) { - p = p + z * V[i][k + 2]; - V[i][k + 2] = V[i][k + 2] - p * r; - } + get norm2() { + return this.s[0]; + } - V[i][k] = V[i][k] - p; - V[i][k + 1] = V[i][k + 1] - p * q; - } - } + get rank() { + let tol = Math.max(this.m, this.n) * this.s[0] * Number.EPSILON; + let r = 0; + let s = this.s; + for (let i = 0, ii = s.length; i < ii; i++) { + if (s[i] > tol) { + r++; } } + return r; } - if (norm === 0) { - return; + get diagonal() { + return Array.from(this.s); } - for (n = nn - 1; n >= 0; n--) { - p = d[n]; - q = e[n]; + get threshold() { + return (Number.EPSILON / 2) * Math.max(this.m, this.n) * this.s[0]; + } - if (q === 0) { - l = n; - H[n][n] = 1; - for (i = n - 1; i >= 0; i--) { - w = H[i][i] - p; - r = 0; - for (j = l; j <= n; j++) { - r = r + H[i][j] * H[j][n]; - } + get leftSingularVectors() { + return this.U; + } - if (e[i] < 0) { - z = w; - s = r; - } else { - l = i; - if (e[i] === 0) { - H[i][n] = w !== 0 ? -r / w : -r / (eps * norm); - } else { - x = H[i][i + 1]; - y = H[i + 1][i]; - q = (d[i] - p) * (d[i] - p) + e[i] * e[i]; - t = (x * s - z * r) / q; - H[i][n] = t; - H[i + 1][n] = - Math.abs(x) > Math.abs(z) ? (-r - w * t) / x : (-s - y * t) / z; - } + get rightSingularVectors() { + return this.V; + } - t = Math.abs(H[i][n]); - if (eps * t * t > 1) { - for (j = i; j <= n; j++) { - H[j][n] = H[j][n] / t; - } - } - } - } - } else if (q < 0) { - l = n - 1; + get diagonalMatrix() { + return matrix_Matrix.diag(this.s); + } +} - if (Math.abs(H[n][n - 1]) > Math.abs(H[n - 1][n])) { - H[n - 1][n - 1] = q / H[n][n - 1]; - H[n - 1][n] = -(H[n][n] - p) / H[n][n - 1]; - } else { - cdivres = cdiv(0, -H[n - 1][n], H[n - 1][n - 1] - p, q); - H[n - 1][n - 1] = cdivres[0]; - H[n - 1][n] = cdivres[1]; - } +// CONCATENATED MODULE: ./node_modules/ml-matrix/src/decompositions.js - H[n][n - 1] = 0; - H[n][n] = 1; - for (i = n - 2; i >= 0; i--) { - ra = 0; - sa = 0; - for (j = l; j <= n; j++) { - ra = ra + H[i][j] * H[j][n - 1]; - sa = sa + H[i][j] * H[j][n]; - } - w = H[i][i] - p; - if (e[i] < 0) { - z = w; - r = ra; - s = sa; - } else { - l = i; - if (e[i] === 0) { - cdivres = cdiv(-ra, -sa, w, q); - H[i][n - 1] = cdivres[0]; - H[i][n] = cdivres[1]; - } else { - x = H[i][i + 1]; - y = H[i + 1][i]; - vr = (d[i] - p) * (d[i] - p) + e[i] * e[i] - q * q; - vi = (d[i] - p) * 2 * q; - if (vr === 0 && vi === 0) { - vr = - eps * - norm * - (Math.abs(w) + - Math.abs(q) + - Math.abs(x) + - Math.abs(y) + - Math.abs(z)); - } - cdivres = cdiv( - x * r - z * ra + q * sa, - x * s - z * sa - q * ra, - vr, - vi - ); - H[i][n - 1] = cdivres[0]; - H[i][n] = cdivres[1]; - if (Math.abs(x) > Math.abs(z) + Math.abs(q)) { - H[i + 1][n - 1] = (-ra - w * H[i][n - 1] + q * H[i][n]) / x; - H[i + 1][n] = (-sa - w * H[i][n] - q * H[i][n - 1]) / x; - } else { - cdivres = cdiv(-r - y * H[i][n - 1], -s - y * H[i][n], z, q); - H[i + 1][n - 1] = cdivres[0]; - H[i + 1][n] = cdivres[1]; - } - } - t = Math.max(Math.abs(H[i][n - 1]), Math.abs(H[i][n])); - if (eps * t * t > 1) { - for (j = i; j <= n; j++) { - H[j][n - 1] = H[j][n - 1] / t; - H[j][n] = H[j][n] / t; - } - } - } - } - } - } - for (i = 0; i < nn; i++) { - if (i < low || i > high) { - for (j = i; j < nn; j++) { - V[i][j] = H[i][j]; - } - } - } - for (j = nn - 1; j >= low; j--) { - for (i = low; i <= high; i++) { - z = 0; - for (k = low; k <= Math.min(j, high); k++) { - z = z + V[i][k] * H[k][j]; - } - V[i][j] = z; - } +function inverse(matrix, useSVD = false) { + matrix = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(matrix); + if (useSVD) { + return new svd_SingularValueDecomposition(matrix).inverse(); + } else { + return solve(matrix, matrix_Matrix.eye(matrix.rows)); } } -function cdiv(xr, xi, yr, yi) { - var r, d; - if (Math.abs(yr) > Math.abs(yi)) { - r = yi / yr; - d = yr + r * yi; - return [(xr + r * xi) / d, (xi - r * xr) / d]; +function solve(leftHandSide, rightHandSide, useSVD = false) { + leftHandSide = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(leftHandSide); + rightHandSide = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(rightHandSide); + if (useSVD) { + return new svd_SingularValueDecomposition(leftHandSide).solve(rightHandSide); } else { - r = yr / yi; - d = yi + r * yr; - return [(r * xr + xi) / d, (r * xi - xr) / d]; + return leftHandSide.isSquare() + ? new lu_LuDecomposition(leftHandSide).solve(rightHandSide) + : new qr_QrDecomposition(leftHandSide).solve(rightHandSide); } } -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/dc/cholesky.js +// CONCATENATED MODULE: ./node_modules/ml-levenberg-marquardt/src/step.js /** - * @class CholeskyDecomposition - * @link https://github.com/lutzroeder/Mapack/blob/master/Source/CholeskyDecomposition.cs - * @param {Matrix} value + * Difference of the matrix function over the parameters + * @ignore + * @param {{x:Array, y:Array}} data - Array of points to fit in the format [x1, x2, ... ], [y1, y2, ... ] + * @param {Array} evaluatedData - Array of previous evaluated function values + * @param {Array} params - Array of previous parameter values + * @param {number} gradientDifference - Adjustment for decrease the damping parameter + * @param {function} paramFunction - The parameters and returns a function with the independent variable as a parameter + * @return {Matrix} */ -class cholesky_CholeskyDecomposition { - constructor(value) { - value = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(value); - if (!value.isSymmetric()) { - throw new Error('Matrix is not symmetric'); - } - - var a = value; - var dimension = a.rows; - var l = new matrix_Matrix(dimension, dimension); - var positiveDefinite = true; - var i, j, k; - - for (j = 0; j < dimension; j++) { - var Lrowj = l[j]; - var d = 0; - for (k = 0; k < j; k++) { - var Lrowk = l[k]; - var s = 0; - for (i = 0; i < k; i++) { - s += Lrowk[i] * Lrowj[i]; - } - Lrowj[k] = s = (a.get(j, k) - s) / l[k][k]; - d = d + s * s; - } +function gradientFunction( + data, + evaluatedData, + params, + gradientDifference, + paramFunction +) { + const n = params.length; + const m = data.x.length; - d = a.get(j, j) - d; + var ans = new Array(n); - positiveDefinite &= d > 0; - l[j][j] = Math.sqrt(Math.max(d, 0)); - for (k = j + 1; k < dimension; k++) { - l[j][k] = 0; - } - } + for (var param = 0; param < n; param++) { + ans[param] = new Array(m); + var auxParams = params.concat(); + auxParams[param] += gradientDifference; + var funcParam = paramFunction(auxParams); - if (!positiveDefinite) { - throw new Error('Matrix is not positive definite'); + for (var point = 0; point < m; point++) { + ans[param][point] = evaluatedData[point] - funcParam(data.x[point]); } - - this.L = l; } + return new matrix_Matrix(ans); +} - /** - * - * @param {Matrix} value - * @return {Matrix} - */ - solve(value) { - value = WrapperMatrix2D_WrapperMatrix2D.checkMatrix(value); +/** + * Matrix function over the samples + * @ignore + * @param {{x:Array, y:Array}} data - Array of points to fit in the format [x1, x2, ... ], [y1, y2, ... ] + * @param {Array} evaluatedData - Array of previous evaluated function values + * @return {Matrix} + */ +function matrixFunction(data, evaluatedData) { + const m = data.x.length; - var l = this.L; - var dimension = l.rows; + var ans = new Array(m); - if (value.rows !== dimension) { - throw new Error('Matrix dimensions do not match'); - } + for (var point = 0; point < m; point++) { + ans[point] = [data.y[point] - evaluatedData[point]]; + } - var count = value.columns; - var B = value.clone(); - var i, j, k; + return new matrix_Matrix(ans); +} - for (k = 0; k < dimension; k++) { - for (j = 0; j < count; j++) { - for (i = 0; i < k; i++) { - B[k][j] -= B[i][j] * l[k][i]; - } - B[k][j] /= l[k][k]; - } - } +/** + * Iteration for Levenberg-Marquardt + * @ignore + * @param {{x:Array, y:Array}} data - Array of points to fit in the format [x1, x2, ... ], [y1, y2, ... ] + * @param {Array} params - Array of previous parameter values + * @param {number} damping - Levenberg-Marquardt parameter + * @param {number} gradientDifference - Adjustment for decrease the damping parameter + * @param {function} parameterizedFunction - The parameters and returns a function with the independent variable as a parameter + * @return {Array} + */ +function step( + data, + params, + damping, + gradientDifference, + parameterizedFunction +) { + var value = damping * gradientDifference * gradientDifference; + var identity = matrix_Matrix.eye(params.length, params.length, value); - for (k = dimension - 1; k >= 0; k--) { - for (j = 0; j < count; j++) { - for (i = k + 1; i < dimension; i++) { - B[k][j] -= B[i][j] * l[i][k]; - } - B[k][j] /= l[k][k]; - } - } + const func = parameterizedFunction(params); + var evaluatedData = data.x.map((e) => func(e)); - return B; - } + var gradientFunc = gradientFunction( + data, + evaluatedData, + params, + gradientDifference, + parameterizedFunction + ); + var matrixFunc = matrixFunction(data, evaluatedData); + var inverseMatrix = inverse( + identity.add(gradientFunc.mmul(gradientFunc.transpose())) + ); - /** - * - * @return {Matrix} - */ - get lowerTriangularMatrix() { - return this.L; - } -} + params = new matrix_Matrix([params]); + params = params.sub( + inverseMatrix + .mmul(gradientFunc) + .mmul(matrixFunc) + .mul(gradientDifference) + .transpose() + ); -// CONCATENATED MODULE: ./node_modules/ml-matrix/src/index.js -/* concated harmony reexport default */__webpack_require__.d(__webpack_exports__, "default", function() { return matrix_Matrix; }); -/* concated harmony reexport Matrix */__webpack_require__.d(__webpack_exports__, "Matrix", function() { return matrix_Matrix; }); -/* concated harmony reexport abstractMatrix */__webpack_require__.d(__webpack_exports__, "abstractMatrix", function() { return AbstractMatrix; }); -/* concated harmony reexport wrap */__webpack_require__.d(__webpack_exports__, "wrap", function() { return wrap; }); -/* concated harmony reexport WrapperMatrix2D */__webpack_require__.d(__webpack_exports__, "WrapperMatrix2D", function() { return WrapperMatrix2D_WrapperMatrix2D; }); -/* concated harmony reexport WrapperMatrix1D */__webpack_require__.d(__webpack_exports__, "WrapperMatrix1D", function() { return WrapperMatrix1D_WrapperMatrix1D; }); -/* concated harmony reexport solve */__webpack_require__.d(__webpack_exports__, "solve", function() { return solve; }); -/* concated harmony reexport inverse */__webpack_require__.d(__webpack_exports__, "inverse", function() { return inverse; }); -/* concated harmony reexport linearDependencies */__webpack_require__.d(__webpack_exports__, "linearDependencies", function() { return linearDependencies; }); -/* concated harmony reexport SingularValueDecomposition */__webpack_require__.d(__webpack_exports__, "SingularValueDecomposition", function() { return svd_SingularValueDecomposition; }); -/* concated harmony reexport SVD */__webpack_require__.d(__webpack_exports__, "SVD", function() { return svd_SingularValueDecomposition; }); -/* concated harmony reexport EigenvalueDecomposition */__webpack_require__.d(__webpack_exports__, "EigenvalueDecomposition", function() { return evd_EigenvalueDecomposition; }); -/* concated harmony reexport EVD */__webpack_require__.d(__webpack_exports__, "EVD", function() { return evd_EigenvalueDecomposition; }); -/* concated harmony reexport CholeskyDecomposition */__webpack_require__.d(__webpack_exports__, "CholeskyDecomposition", function() { return cholesky_CholeskyDecomposition; }); -/* concated harmony reexport CHO */__webpack_require__.d(__webpack_exports__, "CHO", function() { return cholesky_CholeskyDecomposition; }); -/* concated harmony reexport LuDecomposition */__webpack_require__.d(__webpack_exports__, "LuDecomposition", function() { return lu_LuDecomposition; }); -/* concated harmony reexport LU */__webpack_require__.d(__webpack_exports__, "LU", function() { return lu_LuDecomposition; }); -/* concated harmony reexport QrDecomposition */__webpack_require__.d(__webpack_exports__, "QrDecomposition", function() { return qr_QrDecomposition; }); -/* concated harmony reexport QR */__webpack_require__.d(__webpack_exports__, "QR", function() { return qr_QrDecomposition; }); + return params.to1DArray(); +} +// CONCATENATED MODULE: ./node_modules/ml-levenberg-marquardt/src/index.js +/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "default", function() { return levenbergMarquardt; }); +/** + * Curve fitting algorithm + * @param {{x:Array, y:Array}} data - Array of points to fit in the format [x1, x2, ... ], [y1, y2, ... ] + * @param {function} parameterizedFunction - The parameters and returns a function with the independent variable as a parameter + * @param {object} [options] - Options object + * @param {number} [options.damping] - Levenberg-Marquardt parameter + * @param {number} [options.gradientDifference = 10e-2] - Adjustment for decrease the damping parameter + * @param {Array} [options.minValues] - Minimum allowed values for parameters + * @param {Array} [options.maxValues] - Maximum allowed values for parameters + * @param {Array} [options.initialValues] - Array of initial parameter values + * @param {number} [options.maxIterations = 100] - Maximum of allowed iterations + * @param {number} [options.errorTolerance = 10e-3] - Minimum uncertainty allowed for each point + * @return {{parameterValues: Array, parameterError: number, iterations: number}} + */ +function levenbergMarquardt( + data, + parameterizedFunction, + options = {} +) { + let { + maxIterations = 100, + gradientDifference = 10e-2, + damping = 0, + errorTolerance = 10e-3, + minValues, + maxValues, + initialValues + } = options; + if (damping <= 0) { + throw new Error('The damping option must be a positive number'); + } else if (!data.x || !data.y) { + throw new Error('The data parameter must have x and y elements'); + } else if ( + !Array.isArray(data.x) || + data.x.length < 2 || + !Array.isArray(data.y) || + data.y.length < 2 + ) { + throw new Error( + 'The data parameter elements must be an array with more than 2 points' + ); + } else if (data.x.length !== data.y.length) { + throw new Error('The data parameter elements must have the same size'); + } + var parameters = + initialValues || new Array(parameterizedFunction.length).fill(1); + let parLen = parameters.length; + maxValues = maxValues || new Array(parLen).fill(Number.MAX_SAFE_INTEGER); + minValues = minValues || new Array(parLen).fill(Number.MIN_SAFE_INTEGER); + if (maxValues.length !== minValues.length) { + throw new Error('minValues and maxValues must be the same size'); + } + if (!Array.isArray(parameters)) { + throw new Error('initialValues must be an array'); + } + var error = errorCalculation(data, parameters, parameterizedFunction); + var converged = error <= errorTolerance; + for ( + var iteration = 0; + iteration < maxIterations && !converged; + iteration++ + ) { + parameters = step( + data, + parameters, + damping, + gradientDifference, + parameterizedFunction + ); + for (let k = 0; k < parLen; k++) { + parameters[k] = Math.min( + Math.max(minValues[k], parameters[k]), + maxValues[k] + ); + } + error = errorCalculation(data, parameters, parameterizedFunction); + if (isNaN(error)) break; + converged = error <= errorTolerance; + } + return { + parameterValues: parameters, + parameterError: error, + iterations: iteration + }; +} /***/ }) diff --git a/lib/umap-js.min.js b/lib/umap-js.min.js index 2a135f5..49d39c3 100644 --- a/lib/umap-js.min.js +++ b/lib/umap-js.min.js @@ -1 +1 @@ -!function(t,r){if("object"==typeof exports&&"object"==typeof module)module.exports=r();else if("function"==typeof define&&define.amd)define([],r);else{var e=r();for(var n in e)("object"==typeof exports?exports:t)[n]=e[n]}}(window,function(){return function(t){var r={};function e(n){if(r[n])return r[n].exports;var i=r[n]={i:n,l:!1,exports:{}};return t[n].call(i.exports,i,i.exports,e),i.l=!0,i.exports}return e.m=t,e.c=r,e.d=function(t,r,n){e.o(t,r)||Object.defineProperty(t,r,{enumerable:!0,get:n})},e.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},e.t=function(t,r){if(1&r&&(t=e(t)),8&r)return t;if(4&r&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(e.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&r&&"string"!=typeof t)for(var i in t)e.d(n,i,function(r){return t[r]}.bind(null,i));return n},e.n=function(t){var r=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(r,"a",r),r},e.o=function(t,r){return Object.prototype.hasOwnProperty.call(t,r)},e.p="",e(e.s=5)}([function(t,r,e){"use strict";const n=Object.prototype.toString;t.exports=function(t){return n.call(t).endsWith("Array]")}},function(t,r,e){"use strict";var n=this&&this.__values||function(t){var r="function"==typeof Symbol&&t[Symbol.iterator],e=0;return r?r.call(t):{next:function(){return t&&e>=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}};function i(t,r){return Math.floor(r()*t)}function o(t){for(var r=[],e=0;er?t[e]:r;return r},r.max2d=function(t){for(var r=0,e=0;er?t[e][n]:r;return r},r.rejectionSample=function(t,r,e){for(var n=a(t),o=0;o=s[0])return 0;for(var h=0;h=s[0])return 0;s[0]=e,o[0]=n,a[0]=i;for(var h=0,u=0;;){var l=2*h+1,c=l+1,f=t[0][0].length;if(l>=f)break;if(c>=f){if(!(s[l]>e))break;u=l}else if(s[l]>=s[c]){if(!(ee.length;a++)1===i[a]&&n[a]=0?(i[s]=0,Math.floor(e[s])):-1}},function(t,r,e){"use strict";var n,i=this&&this.__read||function(t,r){var e="function"==typeof Symbol&&t[Symbol.iterator];if(!e)return t;var n,i,o=e.call(t),s=[];try{for(;(void 0===r||r-- >0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},o=this&&this.__values||function(t){var r="function"==typeof Symbol&&t[Symbol.iterator],e=0;return r?r.call(t):{next:function(){return t&&e>=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}},s=this&&this.__importStar||function(t){if(t&&t.__esModule)return t;var r={};if(null!=t)for(var e in t)Object.hasOwnProperty.call(t,e)&&(r[e]=t[e]);return r.default=t,r};Object.defineProperty(r,"__esModule",{value:!0});var a=s(e(1)),h=function(){function t(t,r,e,n){if(this.entries=new Map,this.nRows=0,this.nCols=0,t.length!==r.length||t.length!==e.length)throw new Error("rows, cols and values arrays must all have the same length");this.nRows=n[0],this.nCols=n[1];for(var i=0;ir?t:r})},r.multiplyScalar=function(t,r){return t.map(function(t){return t*r})},r.eliminateZeros=function(t){for(var r=new Set,e=t.getValues(),n=t.getRows(),i=t.getCols(),o=0;or?t[e]:r;return t.map(function(t){return t/r})},n.l1=function(t){for(var r=0,e=0;e0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},i=this&&this.__spread||function(){for(var t=[],r=0;r=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}},s=this&&this.__importStar||function(t){if(t&&t.__esModule)return t;var r={};if(null!=t)for(var e in t)Object.hasOwnProperty.call(t,e)&&(r[e]=t[e]);return r.default=t,r};Object.defineProperty(r,"__esModule",{value:!0});var a=s(e(1)),h=function(){return function(t,r,e,n){this.hyperplanes=t,this.offsets=r,this.children=e,this.indices=n}}();function u(t,r,e,n){for(var i=r,o=0;o0?0:1}r.FlatTree=h,r.makeForest=function(t,r,e,n){var o=Math.max(10,r);return a.range(e).map(function(r,e){return function(t,r,e,n){void 0===r&&(r=30);var i=a.range(t.length);return function t(r,e,n,i,o){if(void 0===n&&(n=30),e.length>n){var s=function(t,r,e){var n=t[0].length,i=a.tauRandInt(r.length,e),o=a.tauRandInt(r.length,e);o=(o+=i===o?1:0)%r.length;for(var s=r[i],h=r[o],u=0,l=a.zeros(n),c=0;c0?(g[c]=0,f+=1):(g[c]=1,m+=1)}var d=a.zeros(f),w=a.zeros(m);for(var c in f=0,m=0,a.range(g.length))0===g[c]?(d[f]=r[c],f+=1):(w[m]=r[c],m+=1);return{indicesLeft:d,indicesRight:w,hyperplane:l,offset:u}}(r,e,o),h=s.indicesLeft,u=s.indicesRight,l=s.hyperplane,c=s.offset,f=t(r,h,n,i+1,o),m=t(r,u,n,i+1,o),g={leftChild:f,rightChild:m,isLeaf:!1,hyperplane:l,offset:c};return g}var g={indices:e,isLeaf:!0};return g}(t,i,r,e,n)}(t,o,e,n)}).map(function(t){return function(t,r){var e=function t(r){return r.isLeaf?1:1+t(r.leftChild)+t(r.rightChild)}(t),n=function t(r){return r.isLeaf?1:t(r.leftChild)+t(r.rightChild)}(t),o=a.range(e).map(function(){return a.zeros(t.hyperplane?t.hyperplane.length:0)}),s=a.zeros(e),u=a.range(e).map(function(){return[-1,-1]}),l=a.range(n).map(function(){return a.range(r).map(function(){return-1})});return function t(r,e,n,o,s,a,h){var u;if(r.isLeaf)return o[a][0]=-h,(u=s[h]).splice.apply(u,i([0,r.indices.length],r.indices)),{nodeNum:a,leafNum:h+=1};e[a]=r.hyperplane,n[a]=r.offset,o[a][0]=a+1;var l=a,c=t(r.leftChild,e,n,o,s,a+1,h);return a=c.nodeNum,h=c.leafNum,o[l][1]=a+1,{nodeNum:(c=t(r.rightChild,e,n,o,s,a+1,h)).nodeNum,leafNum:c.leafNum}}(t,o,s,u,l,0,0),new h(o,s,u,l)}(t,o)})},r.makeLeafArray=function(t){var r,e;if(t.length>0){var n=[];try{for(var s=o(t),a=s.next();!a.done;a=s.next()){var h=a.value;n.push.apply(n,i(h.indices))}}catch(t){r={error:t}}finally{try{a&&!a.done&&(e=s.return)&&e.call(s)}finally{if(r)throw r.error}}return n}return[[-1]]},r.searchFlatTree=function(t,r,e){for(var n=0;r.children[n][0]>0;)n=0===u(r.hyperplanes[n],r.offsets[n],t,e)?r.children[n][0]:r.children[n][1];var i=-1*r.children[n][0];return r.indices[i]}},function(t,r,e){"use strict";Object.defineProperty(r,"__esModule",{value:!0});var n=e(6);r.UMAP=n.UMAP},function(t,r,e){"use strict";var n=this&&this.__awaiter||function(t,r,e,n){return new(e||(e=Promise))(function(i,o){function s(t){try{h(n.next(t))}catch(t){o(t)}}function a(t){try{h(n.throw(t))}catch(t){o(t)}}function h(t){t.done?i(t.value):new e(function(r){r(t.value)}).then(s,a)}h((n=n.apply(t,r||[])).next())})},i=this&&this.__generator||function(t,r){var e,n,i,o,s={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function a(o){return function(a){return function(o){if(e)throw new TypeError("Generator is already executing.");for(;s;)try{if(e=1,n&&(i=2&o[0]?n.return:o[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,o[1])).done)return i;switch(n=0,i&&(o=[2&o[0],i.value]),o[0]){case 0:case 1:i=o;break;case 4:return s.label++,{value:o[1],done:!1};case 5:s.label++,n=o[1],o=[0];continue;case 7:o=s.ops.pop(),s.trys.pop();continue;default:if(!(i=(i=s.trys).length>0&&i[i.length-1])&&(6===o[0]||2===o[0])){s=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]0)&&!(n=o.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return s},s=this&&this.__spread||function(){for(var t=[],r=0;r0&&i.set(o,u,c)}var f=l.transpose(i);return l.maximum(i,f)},t.prototype.transform=function(t){var r=this,e=this.X;if(void 0===e||0===e.length)throw new Error("No data has been fit.");var n=Math.floor(this.nNeighbors*this.transformQueueSize);n=Math.min(e.length,n);var i=c.initializeSearch(this.rpForest,e,t,n,this.initFromRandom,this.initFromTree,this.random),o=this.search(e,this.searchGraph,i,t),s=u.deheapSort(o),a=s.indices,h=s.weights;a=a.map(function(t){return t.slice(0,r.nNeighbors)}),h=h.map(function(t){return t.slice(0,r.nNeighbors)});var f=Math.max(0,this.localConnectivity-1),g=this.smoothKNNDistance(h,this.nNeighbors,f),p=g.sigmas,v=g.rhos,d=this.computeMembershipStrengths(a,h,p,v),w=d.rows,y=d.cols,b=d.vals,M=[t.length,e.length],x=new l.SparseMatrix(w,y,b,M),E=l.normalize(x,"l1"),R=l.getCSR(E),k=t.length,z=S(m.reshape2d(R.indices,k,this.nNeighbors),m.reshape2d(R.values,k,this.nNeighbors),this.embedding),N=this.nEpochs?this.nEpochs/3:x.nRows<=1e4?100:30,A=x.getValues().reduce(function(t,r){return r>t?r:t},0);x=x.map(function(t){return t0});if(g.length>=e){var p=Math.floor(e),v=e-p;p>0?(s[h]=g[p-1],v>1e-5&&(s[h]+=v*(g[p]-g[p-1]))):s[h]=v*g[0]}else g.length>0&&(s[h]=m.max(g));for(var d=0;d0?Math.exp(-b/c):1}if(Math.abs(w-o)<1e-5)break;w>o?c=(u+(l=c))/2:(u=c,l===1/0?c*=2:c=(u+l)/2)}if(a[h]=c,s[h]>0){var M=m.mean(f);a[h]<.001*M&&(a[h]=.001*M)}else{var x=m.mean(t.map(m.mean));a[h]<.001*x&&(a[h]=.001*x)}}return{sigmas:a,rhos:s}},t.prototype.computeMembershipStrengths=function(t,r,e,n){for(var i=t.length,o=t[0].length,s=m.zeros(i*o),a=m.zeros(i*o),h=m.zeros(i*o),u=0;u0&&(e[n]=r/i[n])}),e},t.prototype.assignOptimizationStateParameters=function(t){Object.assign(this.optimizationState,t)},t.prototype.prepareForOptimizationLoop=function(){var t=this.repulsionStrength,r=this.learningRate,e=this.negativeSampleRate,n=this.optimizationState,i=n.epochsPerSample,o=n.headEmbedding,a=n.tailEmbedding,h=o[0].length,u=o.length===a.length,l=i.map(function(t){return t/e}),c=s(l),f=s(i);this.assignOptimizationStateParameters({epochOfNextSample:f,epochOfNextNegativeSample:c,epochsPerNegativeSample:l,moveOther:u,initialAlpha:r,alpha:r,gamma:t,dim:h})},t.prototype.initializeOptimization=function(){var t=this.embedding,r=this.embedding,e=this.optimizationState,n=e.head,i=e.tail,o=e.epochsPerSample,s=this.getNEpochs(),a=this.graph.nCols,h=b(this.spread,this.minDist),u=h.a,l=h.b;this.assignOptimizationStateParameters({headEmbedding:t,tailEmbedding:r,head:n,tail:i,epochsPerSample:o,a:u,b:l,nEpochs:s,nVertices:a})},t.prototype.optimizeLayoutStep=function(t){for(var r=this.optimizationState,e=r.head,n=r.tail,i=r.headEmbedding,o=r.tailEmbedding,s=r.epochsPerSample,a=r.epochOfNextSample,h=r.epochOfNextNegativeSample,u=r.epochsPerNegativeSample,l=r.moveOther,c=r.initialAlpha,f=r.alpha,g=r.gamma,p=r.a,v=r.b,d=r.dim,b=r.nEpochs,M=r.nVertices,x=0;xt)){var S=e[x],E=n[x],R=i[S],k=o[E],z=y(R,k),N=0;z>0&&(N=-2*p*v*Math.pow(z,v-1),N/=p*Math.pow(z,v)+1);for(var A=0;A0)O=2*g*v,O/=(.001+I)*(p*Math.pow(I,v)+1);else if(S===P)continue;for(A=0;A0&&(V=w(O*(R[A]-j[A]),4)),R[A]+=V*f}}h[x]+=C*u[x]}return r.alpha=c*(1-t/b),r.currentEpoch+=1,i},t.prototype.optimizeLayoutAsync=function(t){var r=this;return void 0===t&&(t=function(){return!0}),new Promise(function(e,o){var s=function(){return n(r,void 0,void 0,function(){var r,n,a,h,u,l;return i(this,function(i){try{if(r=this.optimizationState,n=r.nEpochs,a=r.currentEpoch,this.embedding=this.optimizeLayoutStep(a),h=this.optimizationState.currentEpoch,u=!1===t(h),l=h===n,u||l)return[2,e(l)];setTimeout(function(){return s()},0)}catch(t){o(t)}return[2]})})};setTimeout(function(){return s()},0)})},t.prototype.optimizeLayout=function(t){void 0===t&&(t=function(){return!0});for(var r=!1,e=[];!r;){var n=this.optimizationState,i=n.nEpochs,o=n.currentEpoch;e=this.optimizeLayoutStep(o);var s=this.optimizationState.currentEpoch,a=!1===t(s);r=s===i||a}return e},t.prototype.getNEpochs=function(){var t=this.graph;if(this.nEpochs>0)return this.nEpochs;var r=t.nRows;return r<=2500?500:r<=5e3?400:r<=7500?300:200},t}();function v(t,r){for(var e=0,n=0;nr?r:t<-r?-r:t}function y(t,r){for(var e=0,n=0;n=r?Math.exp(-(e[i]-r)/t):n}),i={x:e,y:n},s={damping:1.5,initialValues:[.5,.5],gradientDifference:.1,maxIterations:100,errorTolerance:.01},a=g.default(i,function(t){var r=o(t,2),e=r[0],n=r[1];return function(t){return 1/(1+e*Math.pow(t,2*n))}},s).parameterValues,h=o(a,2);return{a:h[0],b:h[1]}}function M(t,r,e,n){return void 0===e&&(e=1),void 0===n&&(n=5),t.map(function(t,i,o){return-1===r[i]||-1===r[o]?t*Math.exp(-e):r[i]!==r[o]?t*Math.exp(-n):t})}function x(t){t=l.normalize(t,"max");var r=l.transpose(t),e=l.pairwiseMultiply(r,t);return t=l.add(t,l.subtract(r,e)),l.eliminateZeros(t)}function S(t,r,e){for(var n=m.zeros(t.length).map(function(t){return m.zeros(e[0].length)}),i=0;i=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}},i=this&&this.__importStar||function(t){if(t&&t.__esModule)return t;var r={};if(null!=t)for(var e in t)Object.hasOwnProperty.call(t,e)&&(r[e]=t[e]);return r.default=t,r};Object.defineProperty(r,"__esModule",{value:!0});var o=i(e(2)),s=i(e(3)),a=i(e(4)),h=i(e(1));r.makeNNDescent=function(t,r){return function(e,n,i,s,a,u,l,c){void 0===s&&(s=10),void 0===a&&(a=50),void 0===u&&(u=.001),void 0===l&&(l=.5),void 0===c&&(c=!0);for(var f=e.length,m=o.makeHeap(e.length,i),g=0;gr&&(r=t[e]);return r};var s=function(t){if(!i()(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");for(var r=t[0],e=1;e1&&void 0!==arguments[1]?arguments[1]:{};if(!i()(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");if(void 0!==e.output){if(!i()(e.output))throw new TypeError("output option must be an array if specified");r=e.output}else r=new Array(t.length);var n=s(t),a=o(t);if(n===a)throw new RangeError("minimum and maximum input values are equal. Cannot rescale a constant array");var h=e.min,u=void 0===h?e.autoMinMax?n:0:h,l=e.max,c=void 0===l?e.autoMinMax?a:1:l;if(u>=c)throw new RangeError("min option must be smaller than max option");for(var f=(c-u)/(a-n),m=0;mMath.abs(h[i])&&(i=r);if(i!==e){for(n=0;n=0;i--){for(n=0;no?t[i][o]:i===o?1:0;return n}get upperTriangularMatrix(){for(var t=this.LU,r=t.rows,e=t.columns,n=new _(r,e),i=0;iMath.abs(r)?(e=r/t,Math.abs(t)*Math.sqrt(1+e*e)):0!==r?(e=t/r,Math.abs(r)*Math.sqrt(1+e*e)):0}function l(t,r,e){for(var n=new Array(t),i=0;i=0;t--)if(0!==v[t]){for(let r=t+1;r=0;t--){if(t0;){let t,r;for(t=R-2;t>=-1&&-1!==t;t--){const r=Number.MIN_VALUE+z*Math.abs(v[t]+Math.abs(v[t+1]));if(Math.abs(y[t])<=r||Number.isNaN(y[t])){y[t]=0;break}}if(t===R-2)r=4;else{let e;for(e=R-1;e>=t&&e!==t;e--){let r=(e!==R?Math.abs(y[e]):0)+(e!==t+1?Math.abs(y[e-1]):0);if(Math.abs(v[e])<=z*r){v[e]=0;break}}e===t?r=3:e===R-1?r=1:(r=2,t=e)}switch(t++,r){case 1:{let r=y[R-2];y[R-2]=0;for(let e=R-2;e>=t;e--){let i=u(v[e],r),o=v[e]/i,s=r/i;if(v[e]=i,e!==t&&(r=-s*y[e-1],y[e-1]=o*y[e-1]),c)for(let t=0;t=v[t+1]);){let r=v[t];if(v[t]=v[t+1],v[t+1]=r,c&&tr?i[o][e]=t[o][e]/this.s[e]:i[o][e]=0;var o=this.U,s=o.length,a=o[0].length,h=new _(e,s);for(let t=0;tt&&r++;return r}get diagonal(){return this.s}get threshold(){return Number.EPSILON/2*Math.max(this.m,this.n)*this.s[0]}get leftSingularVectors(){return _.isMatrix(this.U)||(this.U=new _(this.U)),this.U}get rightSingularVectors(){return _.isMatrix(this.V)||(this.V=new _(this.V)),this.V}get diagonalMatrix(){return _.diag(this.s)}}function f(t,r,e){var n=e?t.rows:t.rows-1;if(r<0||r>n)throw new RangeError("Row index out of range")}function m(t,r,e){var n=e?t.columns:t.columns-1;if(r<0||r>n)throw new RangeError("Column index out of range")}function g(t,r){if(r.to1DArray&&(r=r.to1DArray()),r.length!==t.columns)throw new RangeError("vector size must be the same as the number of columns");return r}function p(t,r){if(r.to1DArray&&(r=r.to1DArray()),r.length!==t.rows)throw new RangeError("vector size must be the same as the number of rows");return r}function v(t,r,e){return{row:d(t,r),column:w(t,e)}}function d(t,r){if("object"!=typeof r)throw new TypeError("unexpected type for row indices");if(r.some(r=>r<0||r>=t.rows))throw new RangeError("row indices are out of range");return Array.isArray(r)||(r=Array.from(r)),r}function w(t,r){if("object"!=typeof r)throw new TypeError("unexpected type for column indices");if(r.some(r=>r<0||r>=t.columns))throw new RangeError("column indices are out of range");return Array.isArray(r)||(r=Array.from(r)),r}function y(t,r,e,n,i){if(5!==arguments.length)throw new RangeError("expected 4 arguments");if(b("startRow",r),b("endRow",e),b("startColumn",n),b("endColumn",i),r>e||n>i||r<0||r>=t.rows||e<0||e>=t.rows||n<0||n>=t.columns||i<0||i>=t.columns)throw new RangeError("Submatrix indices are out of range")}function b(t,r){if("number"!=typeof r)throw new TypeError(`${t} must be a number`)}class M extends(C()){constructor(t,r,e){super(),this.matrix=t,this.rows=r,this.columns=e}static get[Symbol.species](){return _}}class x extends M{constructor(t){super(t,t.columns,t.rows)}set(t,r,e){return this.matrix.set(r,t,e),this}get(t,r){return this.matrix.get(r,t)}}class S extends M{constructor(t,r){super(t,1,t.columns),this.row=r}set(t,r,e){return this.matrix.set(this.row,r,e),this}get(t,r){return this.matrix.get(this.row,r)}}class E extends M{constructor(t,r,e,n,i){y(t,r,e,n,i),super(t,e-r+1,i-n+1),this.startRow=r,this.startColumn=n}set(t,r,e){return this.matrix.set(this.startRow+t,this.startColumn+r,e),this}get(t,r){return this.matrix.get(this.startRow+t,this.startColumn+r)}}class R extends M{constructor(t,r,e){var n=v(t,r,e);super(t,n.row.length,n.column.length),this.rowIndices=n.row,this.columnIndices=n.column}set(t,r,e){return this.matrix.set(this.rowIndices[t],this.columnIndices[r],e),this}get(t,r){return this.matrix.get(this.rowIndices[t],this.columnIndices[r])}}class k extends M{constructor(t,r){super(t,(r=d(t,r)).length,t.columns),this.rowIndices=r}set(t,r,e){return this.matrix.set(this.rowIndices[t],r,e),this}get(t,r){return this.matrix.get(this.rowIndices[t],r)}}class z extends M{constructor(t,r){r=w(t,r),super(t,t.rows,r.length),this.columnIndices=r}set(t,r,e){return this.matrix.set(t,this.columnIndices[r],e),this}get(t,r){return this.matrix.get(t,this.columnIndices[r])}}class N extends M{constructor(t,r){super(t,t.rows,1),this.column=r}set(t,r,e){return this.matrix.set(t,this.column,e),this}get(t){return this.matrix.get(t,this.column)}}class A extends M{constructor(t){super(t,t.rows,t.columns)}set(t,r,e){return this.matrix.set(this.rows-t-1,r,e),this}get(t,r){return this.matrix.get(this.rows-t-1,r)}}class V extends M{constructor(t){super(t,t.rows,t.columns)}set(t,r,e){return this.matrix.set(t,this.columns-r-1,e),this}get(t,r){return this.matrix.get(t,this.columns-r-1)}}function C(t){void 0===t&&(t=Object);class r extends t{static get[Symbol.species](){return this}static from1DArray(t,r,e){if(t*r!==e.length)throw new RangeError("Data length does not match given dimensions");for(var n=new this(t,r),i=0;it&&(t=this.get(r,e));return t}maxIndex(){for(var t=this.get(0,0),r=[0,0],e=0;et&&(t=this.get(e,n),r[0]=e,r[1]=n);return r}min(){for(var t=this.get(0,0),r=0;rr&&(r=this.get(t,e));return r}maxRowIndex(t){f(this,t);for(var r=this.get(t,0),e=[t,0],n=1;nr&&(r=this.get(t,n),e[1]=n);return e}minRow(t){f(this,t);for(var r=this.get(t,0),e=1;er&&(r=this.get(e,t));return r}maxColumnIndex(t){m(this,t);for(var r=this.get(0,t),e=[0,t],n=1;nr&&(r=this.get(n,t),e[0]=n);return e}minColumn(t){m(this,t);for(var r=this.get(0,t),e=1;e=(r=void 0===r?1:r))throw new RangeError("min should be strictly smaller than max");for(var e=this.constructor.empty(this.rows,this.columns),n=0;n=(r=void 0===r?1:r))throw new RangeError("min should be strictly smaller than max");for(var e=this.constructor.empty(this.rows,this.columns),n=0;ne||r<0||r>=this.columns||e<0||e>=this.columns)throw new RangeError("Argument out of range");for(var n=new this.constructor[Symbol.species](t.length,e-r+1),i=0;i=this.rows)throw new RangeError(`Row index out of range: ${t[i]}`);n.set(i,o-r,this.get(t[i],o))}return n}subMatrixColumn(t,r,e){if(void 0===r&&(r=0),void 0===e&&(e=this.rows-1),r>e||r<0||r>=this.rows||e<0||e>=this.rows)throw new RangeError("Argument out of range");for(var n=new this.constructor[Symbol.species](e-r+1,t.length),i=0;i=this.columns)throw new RangeError(`Column index out of range: ${t[i]}`);n.set(o-r,i,this.get(o,t[i]))}return n}setSubMatrix(t,r,e){y(this,r,r+(t=this.constructor.checkMatrix(t)).rows-1,e,e+t.columns-1);for(var n=0;nt?i[o]=1/i[o]:i[o]=0;return i=this.constructor[Symbol.species].diag(i),n.mmul(i.mmul(e.transposeView()))}clone(){for(var t=new this.constructor[Symbol.species](this.rows,this.columns),r=0;r>","signPropagatingRightShift"],[">>>","rightShift","zeroFillRightShift"]]){var u=o($("\n(function %name%(value) {\n if (typeof value === 'number') return this.%name%S(value);\n return this.%name%M(value);\n})\n",{name:s[1],op:s[0]})),l=o($("\n(function %name%S(value) {\n for (var i = 0; i < this.rows; i++) {\n for (var j = 0; j < this.columns; j++) {\n this.set(i, j, this.get(i, j) %op% value);\n }\n }\n return this;\n})\n",{name:`${s[1]}S`,op:s[0]})),d=o($("\n(function %name%M(matrix) {\n matrix = this.constructor.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 (var i = 0; i < this.rows; i++) {\n for (var j = 0; j < this.columns; j++) {\n this.set(i, j, this.get(i, j) %op% matrix.get(i, j));\n }\n }\n return this;\n})\n",{name:`${s[1]}M`,op:s[0]})),w=o($("\n(function %name%(matrix, value) {\n var newMatrix = new this[Symbol.species](matrix);\n return newMatrix.%name%(value);\n})\n",{name:s[1]}));for(n=1;n0){if(super(t),!(Number.isInteger(r)&&r>0))throw new TypeError("nColumns must be a positive integer");for(e=0;e=0;o--){for(i=0;i=0;e--){for(t=0;ti)return new Array(r.rows+1).fill(0);for(var o=r.addRow(e,[0]),s=0;s0;a--){for(c=0,s=0,u=0;u0&&(o=-o),r[a]=c*o,s-=i*o,e[a-1]=i-o,h=0;hl){0;do{for(1,i=e[l],f=(e[l+1]-i)/(2*r[l]),m=u(f,1),f<0&&(m=-m),e[l]=r[l]/(f+m),e[l+1]=r[l]*(f+m),g=e[l+1],o=i-e[l],s=l+2;s=l;s--)for(d=v,v=p,b=y,i=p*r[s],o=p*f,m=u(f,r[s]),r[s+1]=y*m,y=r[s]/m,f=(p=f/m)*e[s]-y*i,e[s+1]=o+y*(p*i+y*e[s]),h=0;hS*x)}e[l]=e[l]+M,r[l]=0}for(s=0;s=u;a--)e[a]=r[a][u-1]/l,s+=e[a]*e[a];for(o=Math.sqrt(s),e[u]>0&&(o=-o),s-=e[u]*o,e[u]=e[u]-o,h=u;h=u;a--)i+=e[a]*r[a][h];for(i/=s,a=u;a<=c;a++)r[a][h]-=i*e[a]}for(a=0;a<=c;a++){for(i=0,h=c;h>=u;h--)i+=e[h]*r[a][h];for(i/=s,h=u;h<=c;h++)r[a][h]-=i*e[h]}e[u]=l*e[u],r[u][u-1]=l*o}}for(a=0;a=1;u--)if(0!==r[u][u-1]){for(a=u+1;a<=c;a++)e[a]=r[a][u-1];for(h=u;h<=c;h++){for(o=0,a=u;a<=c;a++)o+=e[a]*n[a][h];for(o=o/e[u]/r[u][u-1],a=u;a<=c;a++)n[a][h]+=o*e[a]}}}(o,f,m,s),function(t,r,e,n,i){var o,s,a,h,u,l,c,f,m,g,p,v,d,w,y,b=t-1,M=t-1,x=Number.EPSILON,S=0,E=0,R=0,k=0,z=0,N=0,A=0,V=0;for(o=0;oM)&&(e[o]=i[o][o],r[o]=0),s=Math.max(o-1,0);s=0;){for(h=b;h>0&&(0===(N=Math.abs(i[h-1][h-1])+Math.abs(i[h][h]))&&(N=E),!(Math.abs(i[h][h-1])=0){for(A=R>=0?R+A:R-A,e[b-1]=f+A,e[b]=e[b-1],0!==A&&(e[b]=f-c/A),r[b-1]=0,r[b]=0,f=i[b][b-1],N=Math.abs(f)+Math.abs(A),R=f/N,k=A/N,z=Math.sqrt(R*R+k*k),R/=z,k/=z,s=b-1;s0){for(N=Math.sqrt(N),m=h&&(A=i[u][u],R=((z=f-A)*(N=m-A)-c)/i[u+1][u]+i[u][u+1],k=i[u+1][u+1]-A-z-N,z=i[u+2][u+1],N=Math.abs(R)+Math.abs(k)+Math.abs(z),R/=N,k/=N,z/=N,u!==h)&&!(Math.abs(i[u][u-1])*(Math.abs(k)+Math.abs(z))u+2&&(i[o][o-3]=0);for(a=u;a<=b-1&&(w=a!==b-1,a!==u&&(R=i[a][a-1],k=i[a+1][a-1],z=w?i[a+2][a-1]:0,0!==(f=Math.abs(R)+Math.abs(k)+Math.abs(z))&&(R/=f,k/=f,z/=f)),0!==f);a++)if(N=Math.sqrt(R*R+k*k+z*z),R<0&&(N=-N),0!==N){for(a!==u?i[a][a-1]=-N*f:h!==u&&(i[a][a-1]=-i[a][a-1]),f=(R+=N)/N,m=k/N,A=z/N,k/=R,z/=R,s=a;s=0;b--)if(R=e[b],0===(k=r[b]))for(h=b,i[b][b]=1,o=b-1;o>=0;o--){for(c=i[o][o]-R,z=0,s=h;s<=b;s++)z+=i[o][s]*i[s][b];if(r[o]<0)A=c,N=z;else if(h=o,0===r[o]?i[o][b]=0!==c?-z/c:-z/(x*E):(f=i[o][o+1],m=i[o+1][o],k=(e[o]-R)*(e[o]-R)+r[o]*r[o],l=(f*N-A*z)/k,i[o][b]=l,i[o+1][b]=Math.abs(f)>Math.abs(A)?(-z-c*l)/f:(-N-m*l)/A),l=Math.abs(i[o][b]),x*l*l>1)for(s=o;s<=b;s++)i[s][b]=i[s][b]/l}else if(k<0)for(h=b-1,Math.abs(i[b][b-1])>Math.abs(i[b-1][b])?(i[b-1][b-1]=k/i[b][b-1],i[b-1][b]=-(i[b][b]-R)/i[b][b-1]):(y=$(0,-i[b-1][b],i[b-1][b-1]-R,k),i[b-1][b-1]=y[0],i[b-1][b]=y[1]),i[b][b-1]=0,i[b][b]=1,o=b-2;o>=0;o--){for(g=0,p=0,s=h;s<=b;s++)g+=i[o][s]*i[s][b-1],p+=i[o][s]*i[s][b];if(c=i[o][o]-R,r[o]<0)A=c,z=g,N=p;else if(h=o,0===r[o]?(y=$(-g,-p,c,k),i[o][b-1]=y[0],i[o][b]=y[1]):(f=i[o][o+1],m=i[o+1][o],v=(e[o]-R)*(e[o]-R)+r[o]*r[o]-k*k,d=2*(e[o]-R)*k,0===v&&0===d&&(v=x*E*(Math.abs(c)+Math.abs(k)+Math.abs(f)+Math.abs(m)+Math.abs(A))),y=$(f*z-A*g+k*p,f*N-A*p-k*g,v,d),i[o][b-1]=y[0],i[o][b]=y[1],Math.abs(f)>Math.abs(A)+Math.abs(k)?(i[o+1][b-1]=(-g-c*i[o][b-1]+k*i[o][b])/f,i[o+1][b]=(-p-c*i[o][b]-k*i[o][b-1])/f):(y=$(-z-m*i[o][b-1],-N-m*i[o][b],A,k),i[o+1][b-1]=y[0],i[o+1][b]=y[1])),l=Math.max(Math.abs(i[o][b-1]),Math.abs(i[o][b])),x*l*l>1)for(s=o;s<=b;s++)i[s][b-1]=i[s][b-1]/l,i[s][b]=i[s][b]/l}for(o=0;oM)for(s=o;s=0;s--)for(o=0;o<=M;o++){for(A=0,a=0;a<=Math.min(s,M);a++)A+=n[o][a]*i[a][s];n[o][s]=A}}(o,h,a,s,f)}this.n=o,this.e=h,this.d=a,this.V=s}get realEigenvalues(){return this.d}get imaginaryEigenvalues(){return this.e}get eigenvectorMatrix(){return _.isMatrix(this.V)||(this.V=new _(this.V)),this.V}get diagonalMatrix(){var t,r,e=this.n,n=this.e,i=this.d,o=new _(e,e);for(t=0;t0?o[t][t+1]=n[t]:n[t]<0&&(o[t][t-1]=n[t])}return o}}function $(t,r,e,n){var i,o;return Math.abs(e)>Math.abs(n)?[(t+(i=n/e)*r)/(o=e+i*n),(r-i*t)/o]:[((i=e/n)*t+r)/(o=n+i*e),(i*r-t)/o]}class Q{constructor(t){if(!(t=j.checkMatrix(t)).isSymmetric())throw new Error("Matrix is not symmetric");var r,e,n,i=t,o=i.rows,s=new _(o,o),a=!0;for(e=0;e0,s[e][e]=Math.sqrt(Math.max(u,0)),n=e+1;n=0;o--)for(i=0;i=t.length&&(t=void 0),{value:t&&t[r++],done:!t}}}};function n(t,e){return Math.floor(e()*t)}function i(t){for(var e=[],r=0;re?t[r]:e;return e},e.max2d=function(t){for(var e=0,r=0;re?t[r][o]:e;return e},e.rejectionSample=function(t,e,r){for(var o=h(t),i=0;i=s[0])return 0;for(var a=0;a=s[0])return 0;s[0]=r,i[0]=o,h[0]=n;for(var a=0,u=0;;){var l=2*a+1,f=l+1,c=t[0][0].length;if(l>=c)break;if(f>=c){if(!(s[l]>r))break;u=l}else if(s[l]>=s[f]){if(!(rr.length;h++)1===n[h]&&o[h]=0?(n[s]=0,Math.floor(r[s])):-1}},function(t,e,r){"use strict";var o,n=this&&this.__read||function(t,e){var r="function"==typeof Symbol&&t[Symbol.iterator];if(!r)return t;var o,n,i=r.call(t),s=[];try{for(;(void 0===e||e-- >0)&&!(o=i.next()).done;)s.push(o.value)}catch(t){n={error:t}}finally{try{o&&!o.done&&(r=i.return)&&r.call(i)}finally{if(n)throw n.error}}return s},i=this&&this.__values||function(t){var e="function"==typeof Symbol&&t[Symbol.iterator],r=0;return e?e.call(t):{next:function(){return t&&r>=t.length&&(t=void 0),{value:t&&t[r++],done:!t}}}},s=this&&this.__importStar||function(t){if(t&&t.__esModule)return t;var e={};if(null!=t)for(var r in t)Object.hasOwnProperty.call(t,r)&&(e[r]=t[r]);return e.default=t,e};Object.defineProperty(e,"__esModule",{value:!0});var h=s(r(1)),a=function(){function t(t,e,r,o){if(this.entries=new Map,this.nRows=0,this.nCols=0,t.length!==e.length||t.length!==r.length)throw new Error("rows, cols and values arrays must all have the same length");this.nRows=o[0],this.nCols=o[1];for(var n=0;ne?t:e})},e.multiplyScalar=function(t,e){return t.map(function(t){return t*e})},e.eliminateZeros=function(t){for(var e=new Set,r=t.getValues(),o=t.getRows(),n=t.getCols(),i=0;ie?t[r]:e;return t.map(function(t){return t/e})},o.l1=function(t){for(var e=0,r=0;r0)&&!(o=i.next()).done;)s.push(o.value)}catch(t){n={error:t}}finally{try{o&&!o.done&&(r=i.return)&&r.call(i)}finally{if(n)throw n.error}}return s},n=this&&this.__spread||function(){for(var t=[],e=0;e=t.length&&(t=void 0),{value:t&&t[r++],done:!t}}}},s=this&&this.__importStar||function(t){if(t&&t.__esModule)return t;var e={};if(null!=t)for(var r in t)Object.hasOwnProperty.call(t,r)&&(e[r]=t[r]);return e.default=t,e};Object.defineProperty(e,"__esModule",{value:!0});var h=s(r(1)),a=function(){return function(t,e,r,o){this.hyperplanes=t,this.offsets=e,this.children=r,this.indices=o}}();function u(t,e,r,o){for(var n=e,i=0;i0?0:1}e.FlatTree=a,e.makeForest=function(t,e,r,o){var i=Math.max(10,e);return h.range(r).map(function(e,r){return function(t,e,r,o){void 0===e&&(e=30);var n=h.range(t.length);return function t(e,r,o,n,i){if(void 0===o&&(o=30),r.length>o){var s=function(t,e,r){var o=t[0].length,n=h.tauRandInt(e.length,r),i=h.tauRandInt(e.length,r);i=(i+=n===i?1:0)%e.length;for(var s=e[n],a=e[i],u=0,l=h.zeros(o),f=0;f0?(p[f]=0,c+=1):(p[f]=1,m+=1)}var d=h.zeros(c),y=h.zeros(m);for(var f in c=0,m=0,h.range(p.length))0===p[f]?(d[c]=e[f],c+=1):(y[m]=e[f],m+=1);return{indicesLeft:d,indicesRight:y,hyperplane:l,offset:u}}(e,r,i),a=s.indicesLeft,u=s.indicesRight,l=s.hyperplane,f=s.offset,c=t(e,a,o,n+1,i),m=t(e,u,o,n+1,i),p={leftChild:c,rightChild:m,isLeaf:!1,hyperplane:l,offset:f};return p}var p={indices:r,isLeaf:!0};return p}(t,n,e,r,o)}(t,i,r,o)}).map(function(t){return function(t,e){var r=function t(e){return e.isLeaf?1:1+t(e.leftChild)+t(e.rightChild)}(t),o=function t(e){return e.isLeaf?1:t(e.leftChild)+t(e.rightChild)}(t),i=h.range(r).map(function(){return h.zeros(t.hyperplane?t.hyperplane.length:0)}),s=h.zeros(r),u=h.range(r).map(function(){return[-1,-1]}),l=h.range(o).map(function(){return h.range(e).map(function(){return-1})});return function t(e,r,o,i,s,h,a){var u;if(e.isLeaf)return i[h][0]=-a,(u=s[a]).splice.apply(u,n([0,e.indices.length],e.indices)),{nodeNum:h,leafNum:a+=1};r[h]=e.hyperplane,o[h]=e.offset,i[h][0]=h+1;var l=h,f=t(e.leftChild,r,o,i,s,h+1,a);return h=f.nodeNum,a=f.leafNum,i[l][1]=h+1,{nodeNum:(f=t(e.rightChild,r,o,i,s,h+1,a)).nodeNum,leafNum:f.leafNum}}(t,i,s,u,l,0,0),new a(i,s,u,l)}(t,i)})},e.makeLeafArray=function(t){var e,r;if(t.length>0){var o=[];try{for(var s=i(t),h=s.next();!h.done;h=s.next()){var a=h.value;o.push.apply(o,n(a.indices))}}catch(t){e={error:t}}finally{try{h&&!h.done&&(r=s.return)&&r.call(s)}finally{if(e)throw e.error}}return o}return[[-1]]},e.searchFlatTree=function(t,e,r){for(var o=0;e.children[o][0]>0;)o=0===u(e.hyperplanes[o],e.offsets[o],t,r)?e.children[o][0]:e.children[o][1];var n=-1*e.children[o][0];return e.indices[n]}},function(t,e,r){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var o=r(6);e.UMAP=o.UMAP},function(t,e,r){"use strict";var o=this&&this.__awaiter||function(t,e,r,o){return new(r||(r=Promise))(function(n,i){function s(t){try{a(o.next(t))}catch(t){i(t)}}function h(t){try{a(o.throw(t))}catch(t){i(t)}}function a(t){t.done?n(t.value):new r(function(e){e(t.value)}).then(s,h)}a((o=o.apply(t,e||[])).next())})},n=this&&this.__generator||function(t,e){var r,o,n,i,s={label:0,sent:function(){if(1&n[0])throw n[1];return n[1]},trys:[],ops:[]};return i={next:h(0),throw:h(1),return:h(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function h(i){return function(h){return function(i){if(r)throw new TypeError("Generator is already executing.");for(;s;)try{if(r=1,o&&(n=2&i[0]?o.return:i[0]?o.throw||((n=o.return)&&n.call(o),0):o.next)&&!(n=n.call(o,i[1])).done)return n;switch(o=0,n&&(i=[2&i[0],n.value]),i[0]){case 0:case 1:n=i;break;case 4:return s.label++,{value:i[1],done:!1};case 5:s.label++,o=i[1],i=[0];continue;case 7:i=s.ops.pop(),s.trys.pop();continue;default:if(!(n=(n=s.trys).length>0&&n[n.length-1])&&(6===i[0]||2===i[0])){s=0;continue}if(3===i[0]&&(!n||i[1]>n[0]&&i[1]0)&&!(o=i.next()).done;)s.push(o.value)}catch(t){n={error:t}}finally{try{o&&!o.done&&(r=i.return)&&r.call(i)}finally{if(n)throw n.error}}return s},s=this&&this.__spread||function(){for(var t=[],e=0;e0&&n.set(i,u,f)}var c=l.transpose(n);return l.maximum(n,c)},t.prototype.transform=function(t){var e=this,r=this.X;if(void 0===r||0===r.length)throw new Error("No data has been fit.");var o=Math.floor(this.nNeighbors*this.transformQueueSize);o=Math.min(r.length,o);var n=f.initializeSearch(this.rpForest,r,t,o,this.initFromRandom,this.initFromTree,this.random),i=this.search(r,this.searchGraph,n,t),s=u.deheapSort(i),h=s.indices,a=s.weights;h=h.map(function(t){return t.slice(0,e.nNeighbors)}),a=a.map(function(t){return t.slice(0,e.nNeighbors)});var c=Math.max(0,this.localConnectivity-1),p=this.smoothKNNDistance(a,this.nNeighbors,c),g=p.sigmas,w=p.rhos,d=this.computeMembershipStrengths(h,a,g,w),y=d.rows,v=d.cols,b=d.vals,M=[t.length,r.length],S=new l.SparseMatrix(y,v,b,M),E=l.normalize(S,"l1"),R=l.getCSR(E),k=t.length,N=x(m.reshape2d(R.indices,k,this.nNeighbors),m.reshape2d(R.values,k,this.nNeighbors),this.embedding),z=this.nEpochs?this.nEpochs/3:S.nRows<=1e4?100:30,A=S.getValues().reduce(function(t,e){return e>t?e:t},0);S=S.map(function(t){return t0});if(p.length>=r){var g=Math.floor(r),w=r-g;g>0?(s[a]=p[g-1],w>1e-5&&(s[a]+=w*(p[g]-p[g-1]))):s[a]=w*p[0]}else p.length>0&&(s[a]=m.max(p));for(var d=0;d0?Math.exp(-b/f):1}if(Math.abs(y-i)<1e-5)break;y>i?f=(u+(l=f))/2:(u=f,l===1/0?f*=2:f=(u+l)/2)}if(h[a]=f,s[a]>0){var M=m.mean(c);h[a]<.001*M&&(h[a]=.001*M)}else{var S=m.mean(t.map(m.mean));h[a]<.001*S&&(h[a]=.001*S)}}return{sigmas:h,rhos:s}},t.prototype.computeMembershipStrengths=function(t,e,r,o){for(var n=t.length,i=t[0].length,s=m.zeros(n*i),h=m.zeros(n*i),a=m.zeros(n*i),u=0;u0&&(r[o]=e/n[o])}),r},t.prototype.assignOptimizationStateParameters=function(t){Object.assign(this.optimizationState,t)},t.prototype.prepareForOptimizationLoop=function(){var t=this.repulsionStrength,e=this.learningRate,r=this.negativeSampleRate,o=this.optimizationState,n=o.epochsPerSample,i=o.headEmbedding,h=o.tailEmbedding,a=i[0].length,u=i.length===h.length,l=n.map(function(t){return t/r}),f=s(l),c=s(n);this.assignOptimizationStateParameters({epochOfNextSample:c,epochOfNextNegativeSample:f,epochsPerNegativeSample:l,moveOther:u,initialAlpha:e,alpha:e,gamma:t,dim:a})},t.prototype.initializeOptimization=function(){var t=this.embedding,e=this.embedding,r=this.optimizationState,o=r.head,n=r.tail,i=r.epochsPerSample,s=this.getNEpochs(),h=this.graph.nCols,a=b(this.spread,this.minDist),u=a.a,l=a.b;this.assignOptimizationStateParameters({headEmbedding:t,tailEmbedding:e,head:o,tail:n,epochsPerSample:i,a:u,b:l,nEpochs:s,nVertices:h})},t.prototype.optimizeLayoutStep=function(t){for(var e=this.optimizationState,r=e.head,o=e.tail,n=e.headEmbedding,i=e.tailEmbedding,s=e.epochsPerSample,h=e.epochOfNextSample,a=e.epochOfNextNegativeSample,u=e.epochsPerNegativeSample,l=e.moveOther,f=e.initialAlpha,c=e.alpha,p=e.gamma,g=e.a,w=e.b,d=e.dim,b=e.nEpochs,M=e.nVertices,S=0;St)){var x=r[S],E=o[S],R=n[x],k=i[E],N=v(R,k),z=0;N>0&&(z=-2*g*w*Math.pow(N,w-1),z/=g*Math.pow(N,w)+1);for(var A=0;A0)I=2*p*w,I/=(.001+j)*(g*Math.pow(j,w)+1);else if(x===F)continue;for(A=0;A0&&(_=y(I*(R[A]-T[A]),4)),R[A]+=_*c}}a[S]+=P*u[S]}return e.alpha=f*(1-t/b),e.currentEpoch+=1,n},t.prototype.optimizeLayoutAsync=function(t){var e=this;return void 0===t&&(t=function(){return!0}),new Promise(function(r,i){var s=function(){return o(e,void 0,void 0,function(){var e,o,h,a,u,l;return n(this,function(n){try{if(e=this.optimizationState,o=e.nEpochs,h=e.currentEpoch,this.embedding=this.optimizeLayoutStep(h),a=this.optimizationState.currentEpoch,u=!1===t(a),l=a===o,u||l)return[2,r(l)];setTimeout(function(){return s()},0)}catch(t){i(t)}return[2]})})};setTimeout(function(){return s()},0)})},t.prototype.optimizeLayout=function(t){void 0===t&&(t=function(){return!0});for(var e=!1,r=[];!e;){var o=this.optimizationState,n=o.nEpochs,i=o.currentEpoch;r=this.optimizeLayoutStep(i);var s=this.optimizationState.currentEpoch,h=!1===t(s);e=s===n||h}return r},t.prototype.getNEpochs=function(){var t=this.graph;if(this.nEpochs>0)return this.nEpochs;var e=t.nRows;return e<=2500?500:e<=5e3?400:e<=7500?300:200},t}();function w(t,e){for(var r=0,o=0;oe?e:t<-e?-e:t}function v(t,e){for(var r=0,o=0;o=e?Math.exp(-(r[n]-e)/t):o}),n={x:r,y:o},s={damping:1.5,initialValues:[.5,.5],gradientDifference:.1,maxIterations:100,errorTolerance:.01},h=p.default(n,function(t){var e=i(t,2),r=e[0],o=e[1];return function(t){return 1/(1+r*Math.pow(t,2*o))}},s).parameterValues,a=i(h,2);return{a:a[0],b:a[1]}}function M(t,e,r,o){return void 0===r&&(r=1),void 0===o&&(o=5),t.map(function(t,n,i){return-1===e[n]||-1===e[i]?t*Math.exp(-r):e[n]!==e[i]?t*Math.exp(-o):t})}function S(t){t=l.normalize(t,"max");var e=l.transpose(t),r=l.pairwiseMultiply(e,t);return t=l.add(t,l.subtract(e,r)),l.eliminateZeros(t)}function x(t,e,r){for(var o=m.zeros(t.length).map(function(t){return m.zeros(r[0].length)}),n=0;n=t.length&&(t=void 0),{value:t&&t[r++],done:!t}}}},n=this&&this.__importStar||function(t){if(t&&t.__esModule)return t;var e={};if(null!=t)for(var r in t)Object.hasOwnProperty.call(t,r)&&(e[r]=t[r]);return e.default=t,e};Object.defineProperty(e,"__esModule",{value:!0});var i=n(r(2)),s=n(r(3)),h=n(r(4)),a=n(r(1));e.makeNNDescent=function(t,e){return function(r,o,n,s,h,u,l,f){void 0===s&&(s=10),void 0===h&&(h=50),void 0===u&&(u=.001),void 0===l&&(l=.5),void 0===f&&(f=!0);for(var c=r.length,m=i.makeHeap(r.length,n),p=0;pe&&(e=t[r]);return e};var h=function(t){if(!i()(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");for(var e=t[0],r=1;r1&&void 0!==arguments[1]?arguments[1]:{};if(!i()(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");if(void 0!==r.output){if(!i()(r.output))throw new TypeError("output option must be an array if specified");e=r.output}else e=new Array(t.length);var o=h(t),n=s(t);if(o===n)throw new RangeError("minimum and maximum input values are equal. Cannot rescale a constant array");var a=r.min,u=void 0===a?r.autoMinMax?o:0:a,l=r.max,f=void 0===l?r.autoMinMax?n:1:l;if(u>=f)throw new RangeError("min option must be smaller than max option");for(var c=(f-u)/(n-o),m=0;mo)throw new RangeError("Row index out of range")}function l(t,e,r){let o=r?t.columns:t.columns-1;if(e<0||e>o)throw new RangeError("Column index out of range")}function f(t,e){if(e.to1DArray&&(e=e.to1DArray()),e.length!==t.columns)throw new RangeError("vector size must be the same as the number of columns");return e}function c(t,e){if(e.to1DArray&&(e=e.to1DArray()),e.length!==t.rows)throw new RangeError("vector size must be the same as the number of rows");return e}function m(t,e){if("object"!=typeof e)throw new TypeError("unexpected type for row indices");if(e.some(e=>e<0||e>=t.rows))throw new RangeError("row indices are out of range");return Array.isArray(e)||(e=Array.from(e)),e}function p(t,e){if("object"!=typeof e)throw new TypeError("unexpected type for column indices");if(e.some(e=>e<0||e>=t.columns))throw new RangeError("column indices are out of range");return Array.isArray(e)||(e=Array.from(e)),e}function g(t,e,r,o,n){if(5!==arguments.length)throw new RangeError("expected 4 arguments");if(d("startRow",e),d("endRow",r),d("startColumn",o),d("endColumn",n),e>r||o>n||e<0||e>=t.rows||r<0||r>=t.rows||o<0||o>=t.columns||n<0||n>=t.columns)throw new RangeError("Submatrix indices are out of range")}function w(t,e=0){let r=[];for(let o=0;o=n)throw new RangeError("min must be smaller than max");let s=n-o,h=new E(t,e);for(let r=0;rr?(n=!0,r=e):(o=!1,n=!0);t++}return o}isReducedEchelonForm(){let t=0,e=0,r=-1,o=!0,n=!1;for(;tr?(n=!0,r=e):(o=!1,n=!0);for(let r=e+1;rt.get(o,r)&&(o=n);if(0===t.get(o,r))r++;else{t.swapRows(e,o);let n=t.get(e,r);for(let o=r;o=0;)if(0===t.maxRow(o))o--;else{let n=0,i=!1;for(;nt&&(t=this.get(e,r));return t}maxIndex(){let t=this.get(0,0),e=[0,0];for(let r=0;rt&&(t=this.get(r,o),e[0]=r,e[1]=o);return e}min(){let t=this.get(0,0);for(let e=0;ee&&(e=this.get(t,r));return e}maxRowIndex(t){u(this,t);let e=this.get(t,0),r=[t,0];for(let o=1;oe&&(e=this.get(t,o),r[1]=o);return r}minRow(t){u(this,t);let e=this.get(t,0);for(let r=1;re&&(e=this.get(r,t));return e}maxColumnIndex(t){l(this,t);let e=this.get(0,t),r=[0,t];for(let o=1;oe&&(e=this.get(o,t),r[0]=o);return r}minColumn(t){l(this,t);let e=this.get(0,t);for(let r=1;r=r)throw new RangeError("min must be smaller than max");let o=new E(this.rows,this.columns);for(let t=0;t=r)throw new RangeError("min must be smaller than max");let o=new E(this.rows,this.columns);for(let t=0;tr||e<0||e>=this.columns||r<0||r>=this.columns)throw new RangeError("Argument out of range");let o=new E(t.length,r-e+1);for(let n=0;n=this.rows)throw new RangeError(`Row index out of range: ${t[n]}`);o.set(n,i-e,this.get(t[n],i))}return o}subMatrixColumn(t,e,r){if(void 0===e&&(e=0),void 0===r&&(r=this.rows-1),e>r||e<0||e>=this.rows||r<0||r>=this.rows)throw new RangeError("Argument out of range");let o=new E(r-e+1,t.length);for(let n=0;n=this.columns)throw new RangeError(`Column index out of range: ${t[n]}`);o.set(i-e,n,this.get(i,t[n]))}return o}setSubMatrix(t,e,r){g(this,e,e+(t=E.checkMatrix(t)).rows-1,r,r+t.columns-1);for(let o=0;o0){if(this.data=[],!(Number.isInteger(e)&&e>0))throw new TypeError("nColumns must be a positive integer");for(let r=0;r>t);return this},R.prototype.signPropagatingRightShiftM=function(t){if(t=k.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e>t.get(e,r));return this},R.signPropagatingRightShift=function(t,e){return new k(t).signPropagatingRightShift(e)},R.prototype.rightShift=function(t){return"number"==typeof t?this.rightShiftS(t):this.rightShiftM(t)},R.prototype.rightShiftS=function(t){for(let e=0;e>>t);return this},R.prototype.rightShiftM=function(t){if(t=k.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e>>t.get(e,r));return this},R.rightShift=function(t,e){return new k(t).rightShift(e)},R.prototype.zeroFillRightShift=R.prototype.rightShift,R.prototype.zeroFillRightShiftS=R.prototype.rightShiftS,R.prototype.zeroFillRightShiftM=R.prototype.rightShiftM,R.zeroFillRightShift=R.rightShift,R.prototype.not=function(){for(let t=0;tMath.abs(a[n])&&(n=e);if(n!==r){for(o=0;o=0;n--){for(o=0;oe?o.set(n,e,t.get(n,e)):n===e?o.set(n,e,1):o.set(n,e,0);return o}get upperTriangularMatrix(){let t=this.LU,e=t.rows,r=t.columns,o=new E(e,r);for(let n=0;nMath.abs(e)?(r=e/t,Math.abs(t)*Math.sqrt(1+r*r)):0!==e?(r=t/e,Math.abs(e)*Math.sqrt(1+r*r)):0}class _{constructor(t){let e,r,o,n,i=(t=N.checkMatrix(t)).clone(),s=t.rows,h=t.columns,a=new Float64Array(h);for(o=0;o=0;i--){for(n=0;n=0;r--){for(t=0;t=0;t--)if(0!==m[t]){for(let e=t+1;e=0;t--){if(t0;){let t,e;for(t=S-2;t>=-1&&-1!==t;t--){const e=Number.MIN_VALUE+k*Math.abs(m[t]+Math.abs(m[t+1]));if(Math.abs(w[t])<=e||Number.isNaN(w[t])){w[t]=0;break}}if(t===S-2)e=4;else{let r;for(r=S-1;r>=t&&r!==t;r--){let e=(r!==S?Math.abs(w[r]):0)+(r!==t+1?Math.abs(w[r-1]):0);if(Math.abs(m[r])<=k*e){m[r]=0;break}}r===t?e=3:r===S-1?e=1:(e=2,t=r)}switch(t++,e){case 1:{let e=w[S-2];w[S-2]=0;for(let r=S-2;r>=t;r--){let n=A(m[r],e),i=m[r]/n,s=e/n;if(m[r]=n,r!==t&&(e=-s*w[r-1],w[r-1]=i*w[r-1]),u)for(let t=0;t=m[t+1]);){let e=m[t];if(m[t]=m[t+1],m[t+1]=e,u&&te&&n.set(i,r,t.get(i,r)/this.s[r]);let i=this.U,s=i.rows,h=i.columns,a=new E(r,s);for(let t=0;tt&&e++;return e}get diagonal(){return Array.from(this.s)}get threshold(){return Number.EPSILON/2*Math.max(this.m,this.n)*this.s[0]}get leftSingularVectors(){return this.U}get rightSingularVectors(){return this.V}get diagonalMatrix(){return E.diag(this.s)}}function C(t,e=!1){return t=N.checkMatrix(t),e?new P(t).inverse():function(t,e,r=!1){return t=N.checkMatrix(t),e=N.checkMatrix(e),r?new P(t).solve(e):t.isSquare()?new z(t).solve(e):new _(t).solve(e)}(t,E.eye(t.rows))}function F(t,e,r,o,n){var i=r*o*o,s=E.eye(e.length,e.length,i);const h=n(e);var a=t.x.map(t=>h(t)),u=function(t,e,r,o,n){const i=r.length,s=t.x.length;for(var h=new Array(i),a=0;a Date: Wed, 14 Oct 2020 10:12:36 -0700 Subject: [PATCH 46/46] Fix of iterator --- src/tree.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tree.ts b/src/tree.ts index 71391cf..99da3b0 100644 --- a/src/tree.ts +++ b/src/tree.ts @@ -217,7 +217,7 @@ function euclideanRandomProjectionSplit( // Populate the arrays with indices according to which side they fell on nLeft = 0; nRight = 0; - for (let i in utils.range(side.length)) { + for (let i = 0; i < side.length; i++) { if (side[i] === 0) { indicesLeft[nLeft] = indices[i]; nLeft += 1;