UNPKG

node-nlp

Version:

Library for NLU (Natural Language Understanding) done in Node.js

392 lines (354 loc) 14 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Source: math/matrix.js</title> <script src="scripts/prettify/prettify.js"> </script> <script src="scripts/prettify/lang-css.js"> </script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> </head> <body> <div id="main"> <h1 class="page-title">Source: math/matrix.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>/* * Copyright (c) AXA Shared Services Spain S.A. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ const Vector = require('./vector'); /** * Class representing a Matrix and some of the possible operations. */ class Matrix { /** * Constructor of the class * @param {Number[][] | Matrix} elements Elements for initializing the matrix. * @param {boolean} useClone If true, the input array is cloned. If false, it uses * the elements input directly by reference. */ constructor(elements, useClone = true) { if (useClone) { this.setElements(elements); } else { this.elements = elements.elements || elements; } } /** * Clone the input elemnts and assign to this instance elements. * @param {Number[][] | Matrix} els Input elements. */ setElements(els) { if (!els) { return this; } const elements = els.elements || els; if (!elements || elements.length === 0) { return this; } this.elements = []; if (elements[0][0] !== undefined) { for (let i = 0; i &lt; elements.length; i += 1) { this.elements.push(elements[i].slice()); } return this; } for (let i = 0; i &lt; elements.length; i += 1) { this.elements.push([elements[i]]); } return this; } /** * Return a row of the matrix as a vector. * @param {Number} i Row number. * @returns {Vector} Row of the matrix as a vector. */ row(i) { if (i &lt; 0 || i >= this.elements.length) { return undefined; } return new Vector(this.elements[i]); } /** * Return a column of the matrix as a vector. * @param {Number} j Column number. * @returns {Vector} Column of the matrix as a vector. */ column(j) { if (j &lt; 0 || j >= this.elements[0].length) { return undefined; } const result = []; for (let i = 0; i &lt; this.elements.length; i += 1) { result.push(this.elements[i][j]); } return new Vector(result); } /** * Return the amount of rows of the matrix. * @returns {Number} Amount of rows of the matrix. */ rowCount() { return this.elements.length; } /** * Creates a new Matrix with given dimensions and filled with the cell value. * @param {Number} n Amount of rows. * @param {Number} m Amount of columns. * @param {Number} v Cell value. * @returns {Number[][] | Matrix} Matrix with given dimensions and cell value. */ static fill(n, m, v) { const elements = []; for (let i = 0; i &lt; n; i += 1) { elements.push(Vector.createArray(m, v)); } return new Matrix(elements, false); } /** * Creates a new Matrix with given dimensions and filled with 0. * @param {Number} n Amount of rows. * @param {Number} m Amount of columns. * @returns {Matrix} Matrix with given dimensions filled with 0. */ static zero(n, m) { return Matrix.fill(n, m, 0); } /** * Creates a new Matrix with given dimensions and filled with 1. * @param {Number} n Amount of rows. * @param {Number} m Amount of columns. * @returns {Matrix} Matrix with given dimensions filled with 1. */ static one(n, m) { return Matrix.fill(n, m, 1); } /** * Creates a new Matrix that is the transposed of this one * (rows become columns). * @returns {Matrix} Matrix that is transposed of this one. */ transpose() { const numRows = this.elements.length; const numColumns = this.elements[0].length; const elements = []; for (let i = 0; i &lt; numColumns; i += 1) { elements.push([]); for (let j = 0; j &lt; numRows; j += 1) { elements[i][j] = this.elements[j][i]; } } return new Matrix(elements, false); } /** * Creates a new matrix where each element is the result of executiong * the provided function with signature (element, i, j). * @param {Function} fn Function for calculating each element. * @returns {Matrix} Resulting mapped matrix. */ map(fn) { const numRows = this.elements.length; const numColumns = this.elements[0].length; const elements = []; for (let i = 0; i &lt; numRows; i += 1) { elements.push([]); for (let j = 0; j &lt; numColumns; j += 1) { elements[i][j] = fn(this.elements[i][j], i, j); } } return new Matrix(elements, false); } /** * Returns a new matrix where each cell is the natural logarithm * of the input element. * @returns {Matrix} Logarithm matrix. */ log() { return this.map(x => Math.log(x)); } /** * Return a matrix where is row is augmented with the corresponding * row of the input matrix. * @param {Number[][] | Matrix} matrix Input matrix. * @returns {Matrix} Matrix that is this one augmented with the input one. */ augment(matrix) { let elements = matrix.elements || matrix; if (!elements[0][0]) { ({ elements } = new Matrix(elements)); } const clone = new Matrix(this.elements); const numColumns = clone.elements[0].length; const numRows = clone.elements.length; const elementColumns = elements[0].length; if (numRows !== elements.length) { throw new Error( 'Cannot operate two matrix with different amount of rows.' ); } for (let i = 0; i &lt; numRows; i += 1) { for (let j = 0; j &lt; elementColumns; j += 1) { clone.elements[i][numColumns + j] = elements[i][j]; } } return clone; } /** * Indicates if another matrix has exactly same dimensions as this one. * @param {Number[][] | Number[] | Matrix | Vector} matrix Input matrix. * @returns {boolean} True if both have same number of rows and columns. */ isSameSizeAs(matrix) { let elements = matrix.elements || matrix; if (!elements[0][0]) { ({ elements } = new Matrix(elements)); } return ( this.elements.length === elements.length &amp;&amp; this.elements[0].length === elements[0].length ); } /** * Indicates if another matrix has exactly same amount of rows as this * matrix amount of columns. * @param {Number[][] | Number[] | Matrix | Vector} matrix Input matrix. * @returns {boolean} True if this one column count equals input row count. */ isSameColumnToRowSize(matrix) { let elements = matrix.elements || matrix; if (!elements[0][0]) { ({ elements } = new Matrix(elements)); } return this.elements[0].length === elements.length; } /** * Performs a substraction element by element between two matrix. * @param {Matrix} matrix Input matrix. */ subtract(matrix) { if (typeof matrix === 'number') { return this.map(x => x - matrix); } let elements = matrix.elements || matrix; if (!elements[0][0]) { ({ elements } = new Matrix(elements)); } if (!this.isSameSizeAs(elements)) { throw new Error('Cannot operate two matrix with different dimensions.'); } return this.map((x, i, j) => x - elements[i][j]); } /** * Given a matrix an an operation function, applies this operation * multiplying this matrix with the provided one. * @param {Matrix} matrix Matrix to operate with this. * @param {Function} op Multiply operation. * @returns {Matrix} Result matrix of multiplying both matrix. */ mulOp(matrix, op) { if (typeof matrix === 'number') { return this.map((x, i, j) => op(x, matrix, i, j)); } let inputElements = matrix.elements || matrix; if (!inputElements[0][0]) { inputElements = new Matrix(inputElements).elements; } if (!this.isSameColumnToRowSize(inputElements)) { throw new Error('Number of rows of A should match number of cols of B.'); } const result = []; for (let i = 0; i &lt; this.elements.length; i += 1) { const currentRow = []; for (let j = 0; j &lt; inputElements[0].length; j += 1) { let sum = 0; for (let k = this.elements[0].length - 1; k >= 0; k -= 1) { sum += op(this.elements[i][k], inputElements[k][j]); } currentRow[j] = sum; } result[i] = currentRow; } return new Matrix(result); } /** * Multiply two matrix. * @param {Matrix} matrix Input matrix. * @param {Function} activation Optional activation fucntion. * @returns {Matrix} Multiplication of the matrix. */ multiply(matrix, activation) { if (typeof matrix === 'number') { const result = new Matrix(this.elements, true); for (let i = 0, li = result.elements.length; i &lt; li; i += 1) { for (let j = 0, lj = result.elements[0].length; j &lt; lj; j += 1) { result.elements[i][j] *= matrix; } } return result; } let inputElements = matrix.elements || matrix; if (!inputElements[0][0]) { inputElements = new Matrix(inputElements).elements; } if (this.elements[0].length !== inputElements.length) { throw new Error('Number of rows of A should match number of cols of B.'); } const li = this.elements.length; const lj = inputElements[0].length; const lk = this.elements[0].length - 1; const result = []; for (let i = 0; i &lt; li; i += 1) { const currentRow = []; for (let j = 0; j &lt; lj; j += 1) { let sum = 0; for (let k = lk; k >= 0; k -= 1) { sum += this.elements[i][k] * inputElements[k][j]; } if (activation) { sum = activation(sum); } currentRow[j] = sum; } result[i] = currentRow; } return new Matrix(result); } } module.exports = Matrix; </code></pre> </article> </section> </div> <nav> <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="BinaryNeuralNetworkClassifier.html">BinaryNeuralNetworkClassifier</a></li><li><a href="Classifier.html">Classifier</a></li><li><a href="ConversationContext.html">ConversationContext</a></li><li><a href="DutchStemmer.html">DutchStemmer</a></li><li><a href="EnglishStemmer.html">EnglishStemmer</a></li><li><a href="EnumNamedEntity.html">EnumNamedEntity</a></li><li><a href="Evaluator.html">Evaluator</a></li><li><a href="HungarianStemmer.html">HungarianStemmer</a></li><li><a href="ItalianStemmer.html">ItalianStemmer</a></li><li><a href="Language.html">Language</a></li><li><a href="LogisticRegressionClassifier.html">LogisticRegressionClassifier</a></li><li><a href="Matrix.html">Matrix</a></li><li><a href="MemoryConversationContext.html">MemoryConversationContext</a></li><li><a href="NamedEntity.html">NamedEntity</a></li><li><a href="NerManager.html">NerManager</a></li><li><a href="NlgManager.html">NlgManager</a></li><li><a href="NlpClassifier.html">NlpClassifier</a></li><li><a href="NlpManager.html">NlpManager</a></li><li><a href="NorwegianStemmer.html">NorwegianStemmer</a></li><li><a href="PortugueseStemmer.html">PortugueseStemmer</a></li><li><a href="Recognizer.html">Recognizer</a></li><li><a href="RegexNamedEntity.html">RegexNamedEntity</a></li><li><a href="RomanianStemmer.html">RomanianStemmer</a></li><li><a href="RussianStemmer.html">RussianStemmer</a></li><li><a href="SentimentAnalyzer.html">SentimentAnalyzer</a></li><li><a href="SentimentManager.html">SentimentManager</a></li><li><a href="SimilarSearch.html">SimilarSearch</a></li><li><a href="SlotManager.html">SlotManager</a></li><li><a href="StemmerJa.html">StemmerJa</a></li><li><a href="SwedishStemmer.html">SwedishStemmer</a></li><li><a href="Tokenizer.html">Tokenizer</a></li><li><a href="TrimNamedEntity.html">TrimNamedEntity</a></li><li><a href="TurkishStemmer.html">TurkishStemmer</a></li><li><a href="Vector.html">Vector</a></li><li><a href="XTable.html">XTable</a></li></ul><h3>Global</h3><ul><li><a href="global.html#endsinArr">endsinArr</a></li><li><a href="global.html#prelude">prelude</a></li><li><a href="global.html#regions">regions</a></li><li><a href="global.html#stem">stem</a></li><li><a href="global.html#stopwords">stopwords</a></li></ul> </nav> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Sat Oct 13 2018 19:14:51 GMT+0200 (CEST) </footer> <script> prettyPrint(); </script> <script src="scripts/linenumber.js"> </script> </body> </html>