ml-matrix-convolution
Advanced tools
Comparing version 0.1.0 to 0.2.0
{ | ||
"name": "ml-matrix-convolution", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Matrix convolution: It offers the direct and the fourier transform convolution", | ||
@@ -5,0 +5,0 @@ "main": "./src/index.js", |
108
src/index.js
@@ -8,33 +8,19 @@ 'use strict;' | ||
function convolutionFFT(input, kernel, opt) { | ||
var options = Object.assign({}, opt); | ||
var inputData = input; | ||
var tmp = matrix2Array(input); | ||
var inputData = tmp.data; | ||
var options = Object.assign({normalize : false, divisor : 1, rows:tmp.rows, cols:tmp.cols}, opt); | ||
var nRows, nCols; | ||
if (typeof input[0] != "number") { | ||
nRows = input.length; | ||
nCols = input[0].length; | ||
inputData = new Array(nRows * nCols); | ||
for (var i = 0; i < nRows; i++) { | ||
for (var j = 0; j < nCols; j++) { | ||
inputData[i * nCols + j] = input[i][j]; | ||
} | ||
} | ||
} | ||
else { | ||
if (options.rows&&options.cols) { | ||
nRows = options.rows; | ||
nCols = options.cols; | ||
if (!nRows || !nCols) { | ||
throw new Error("Invalid number of rows or columns " + nRows + " " + nCols); | ||
} | ||
} | ||
var radix2Sized = FFTUtils.toRadix2(inputData, nRows, nCols); | ||
var conv = FFTUtils.convolute(radix2Sized.data, kernel, radix2Sized.rows, radix2Sized.cols); | ||
return FFTUtils.crop(conv, radix2Sized.rows, radix2Sized.cols, nRows, nCols); | ||
} | ||
else { | ||
throw new Error("Invalid number of rows or columns " + nRows + " " + nCols) | ||
} | ||
function convolutionDirect(input, kernel, opt) { | ||
var options = Object.assign({}, {normalize : false, divisor : 1}, opt); | ||
var divisor = options.divisor; | ||
var i,j; | ||
var kHeight = kernel.length; | ||
var kWidth = kernel[0].length; | ||
var i, j, x, y, index, sum, kVal, row, col; | ||
if (options.normalize) { | ||
@@ -46,3 +32,2 @@ divisor = 0; | ||
} | ||
if (divisor === 0) { | ||
@@ -52,23 +37,43 @@ throw new RangeError('convolution: The divisor is equal to zero'); | ||
var inputData = input; | ||
var nRows, nCols; | ||
if (typeof input[0] != "number") { | ||
nRows = input.length; | ||
nCols = input[0].length; | ||
inputData = new Array(nRows * nCols); | ||
for (var i = 0; i < nRows; i++) { | ||
for (var j = 0; j < nCols; j++) { | ||
inputData[i * nCols + j] = input[i][j]; | ||
} | ||
var radix2Sized = FFTUtils.toRadix2(inputData, nRows, nCols); | ||
var conv = FFTUtils.convolute(radix2Sized.data, kernel, radix2Sized.rows, radix2Sized.cols); | ||
conv = FFTUtils.crop(conv, radix2Sized.rows, radix2Sized.cols, nRows, nCols); | ||
if(divisor!=0){ | ||
for(i=0;i<conv.length;i++){ | ||
conv[i]/divisor; | ||
} | ||
} | ||
else { | ||
return conv; | ||
} | ||
function convolutionDirect(input, kernel, opt) { | ||
var tmp = matrix2Array(input); | ||
var inputData = tmp.data; | ||
var options = Object.assign({normalize : false, divisor : 1, rows:tmp.rows, cols:tmp.cols}, opt); | ||
var nRows, nCols; | ||
if (options.rows&&options.cols) { | ||
nRows = options.rows; | ||
nCols = options.cols; | ||
if (!nRows || !nCols) { | ||
throw new Error("Invalid number of rows or columns " + nRows + " " + nCols); | ||
} | ||
} | ||
else { | ||
throw new Error("Invalid number of rows or columns " + nRows + " " + nCols) | ||
} | ||
var divisor = options.divisor; | ||
var kHeight = kernel.length; | ||
var kWidth = kernel[0].length; | ||
var i, j, x, y, index, sum, kVal, row, col; | ||
if (options.normalize) { | ||
divisor = 0; | ||
for (i = 0; i < kHeight; i++) | ||
for (j = 0; j < kWidth; j++) | ||
divisor += kernel[i][j]; | ||
} | ||
if (divisor === 0) { | ||
throw new RangeError('convolution: The divisor is equal to zero'); | ||
} | ||
var output = new Array(nRows*nCols); | ||
@@ -124,7 +129,32 @@ | ||
function matrix2Array(input){ | ||
var inputData=input; | ||
var nRows, nCols; | ||
if(typeof input[0]!="number"){ | ||
nRows = input.length; | ||
nCols = input[0].length; | ||
inputData = new Array(nRows*nCols); | ||
for(var i=0;i<nRows;i++){ | ||
for(var j=0;j<nCols;j++){ | ||
inputData[i*nCols+j]=input[i][j]; | ||
} | ||
} | ||
} | ||
else{ | ||
var tmp = Math.sqrt(input.length); | ||
if(Number.isInteger(tmp)){ | ||
nRows=tmp; | ||
nCols=tmp; | ||
} | ||
} | ||
return {data:inputData,rows:nRows,cols:nCols}; | ||
} | ||
module.exports = { | ||
fft:convolutionFFT, | ||
direct:convolutionDirect, | ||
kernelFactory:{LoG:LoG} | ||
kernelFactory:{LoG:LoG}, | ||
matrix2Array:matrix2Array | ||
}; |
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
6381
133