Comparing version 6.0.0-0 to 6.0.0-1
@@ -5,2 +5,3 @@ declare module 'ml-matrix' { | ||
type ScalarOrMatrix = number | Matrix; | ||
type MatrixDimension = 'row' | 'column'; | ||
@@ -153,4 +154,36 @@ class BaseView extends Matrix {} | ||
diagonal(): number[]; | ||
sum(by: 'row' | 'column'): Matrix | number; | ||
/** | ||
* Returns the sum of all elements of the matrix. | ||
*/ | ||
sum(): number; | ||
/** | ||
* Returns the sum by the given dimension. | ||
* @param by - sum by 'row' or 'column'. | ||
*/ | ||
sum(by: MatrixDimension): number[]; | ||
/** | ||
* Returns the product of all elements of the matrix. | ||
*/ | ||
product(): number; | ||
/** | ||
* Returns the product by the given dimension. | ||
* @param by - product by 'row' or 'column'. | ||
*/ | ||
product(by: MatrixDimension): number[]; | ||
/** | ||
* Returns the mean of all elements of the matrix. | ||
*/ | ||
mean(): number; | ||
/** | ||
* Returns the mean by the given dimension. | ||
* @param by - mean by 'row' or 'column'. | ||
*/ | ||
mean(by: MatrixDimension): number[]; | ||
prod(): number; | ||
@@ -215,2 +248,5 @@ norm(type: 'frobenius' | 'max'): number; | ||
clone(): Matrix; | ||
entropy(eps?: number): number; | ||
variance(unbiased?: boolean, means?: number[]): number[]; | ||
standardDeviation(unbiased?: boolean, means?: number[]): number[]; | ||
@@ -217,0 +253,0 @@ // From here we document methods dynamically generated from operators |
{ | ||
"name": "ml-matrix", | ||
"version": "6.0.0-0", | ||
"version": "6.0.0-1", | ||
"description": "Matrix manipulation and computation library", | ||
@@ -5,0 +5,0 @@ "main": "matrix.js", |
@@ -11,7 +11,12 @@ import rescale from 'ml-array-rescale'; | ||
checkRange, | ||
checkIndices, | ||
checkIndices | ||
} from './util'; | ||
import { | ||
sumByRow, | ||
sumByColumn, | ||
sumAll | ||
} from './util'; | ||
sumAll, | ||
productByRow, | ||
productByColumn, | ||
productAll | ||
} from './stat'; | ||
import MatrixTransposeView from './views/transpose'; | ||
@@ -953,8 +958,2 @@ import MatrixRowView from './views/row'; | ||
/** | ||
* 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) { | ||
@@ -966,15 +965,44 @@ switch (by) { | ||
return sumByColumn(this); | ||
case undefined: | ||
return sumAll(this); | ||
default: | ||
return sumAll(this); | ||
throw new Error(`invalid option: ${by}`); | ||
} | ||
} | ||
/** | ||
* Returns the mean of all elements of the matrix | ||
* @return {number} | ||
*/ | ||
mean() { | ||
return this.sum() / this.size; | ||
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}`); | ||
} | ||
} | ||
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}`); | ||
} | ||
} | ||
/** | ||
@@ -1753,2 +1781,57 @@ * Returns the product of all elements of the matrix | ||
} | ||
entropy(eps = 0) { | ||
var sum = 0; | ||
for (var i = 0; i < this.rows; i++) { | ||
for (var j = 0; j < this.columns; j++) { | ||
sum += this.get(i, j) * Math.log(this.get(i, j) + eps); | ||
} | ||
} | ||
return 0 - sum; | ||
} | ||
variance(unbiased = true, means = this.mean('column')) { | ||
if (typeof unbiased !== 'boolean') { | ||
throw new TypeError('unbiased must be a boolean'); | ||
} | ||
if (!Array.isArray(means)) { | ||
throw new TypeError('means must be an array'); | ||
} | ||
const rows = this.rows; | ||
const cols = this.columns; | ||
const variance = []; | ||
for (var j = 0; j < cols; j++) { | ||
var sum1 = 0; | ||
var sum2 = 0; | ||
var x = 0; | ||
for (var i = 0; i < rows; i++) { | ||
x = this.get(i, j) - means[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 variance; | ||
} | ||
standardDeviation(unbiased = true, means = this.mean('column')) { | ||
if (typeof unbiased !== 'boolean') { | ||
throw new TypeError('unbiased must be a boolean'); | ||
} | ||
if (!Array.isArray(means)) { | ||
throw new TypeError('means must be an array'); | ||
} | ||
const variance = this.variance(means, unbiased); | ||
for (var i = 0; i < variance.length; i++) { | ||
variance[i] = Math.sqrt(variance[i]); | ||
} | ||
return variance; | ||
} | ||
} | ||
@@ -1755,0 +1838,0 @@ |
@@ -1,3 +0,1 @@ | ||
import Matrix from './matrix'; | ||
/** | ||
@@ -143,32 +141,10 @@ * @private | ||
export function sumByRow(matrix) { | ||
var sum = 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)); | ||
} | ||
export function newArray(length, value = 0) { | ||
var array = []; | ||
for (var i = 0; i < length; i++) { | ||
array.push(value); | ||
} | ||
return sum; | ||
return array; | ||
} | ||
export function sumByColumn(matrix) { | ||
var sum = 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; | ||
} | ||
export 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) { | ||
@@ -175,0 +151,0 @@ if (typeof value !== 'number') { |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
290719
32
8987