From 250ea11303fc5bf6584fe8e4c2c613217c83bc8c Mon Sep 17 00:00:00 2001 From: Pierre Colle Date: Wed, 23 Sep 2020 18:42:04 +0200 Subject: [PATCH] fix: padWithZero cloning and logic --- lib/linalgebra/pad-with-zeros.js | 8 +++++--- lib/observation/sensor.js | 6 ++++-- lib/setup/build-state-projection.js | 3 ++- lib/state.js | 2 +- test/api/basic.js | 3 +++ 5 files changed, 15 insertions(+), 7 deletions(-) diff --git a/lib/linalgebra/pad-with-zeros.js b/lib/linalgebra/pad-with-zeros.js index 6ab75b0..e90d9b4 100644 --- a/lib/linalgebra/pad-with-zeros.js +++ b/lib/linalgebra/pad-with-zeros.js @@ -6,16 +6,18 @@ *@returns {Array. | Array.>} paded array */ module.exports = function (array, {dimension}) { + const l1 = array.length; const l = array[0].length; + const result = array.map(a => a.concat()); if (dimension < l) { throw (new TypeError('Dynamic dimension does not match with observedProjection')); } - for (let i = 0; i < l; i++) { + for (let i = 0; i < l1; i++) { for (let j = 0; j < dimension - l; j++) { - array[i].push(0); + result[i].push(0); } } - return array; + return result; }; diff --git a/lib/observation/sensor.js b/lib/observation/sensor.js index 5d4eb37..3b1fed8 100644 --- a/lib/observation/sensor.js +++ b/lib/observation/sensor.js @@ -8,6 +8,8 @@ const polymorphMatrix = require('../utils/polymorph-matrix.js'); * @returns {ObservationConfig} */ +const copy = mat => mat.map(a => a.concat()); + module.exports = function (options) { const {sensorDimension = 1, sensorCovariance = 1, nSensors = 1} = options; const sensorCovarianceFormatted = polymorphMatrix(sensorCovariance, {dimension: sensorDimension}); @@ -15,8 +17,8 @@ module.exports = function (options) { let concatenatedObservedProjection = []; let concatenatedCovariance = []; for (let i = 0; i < nSensors; i++) { - concatenatedObservedProjection = concatenatedObservedProjection.concat(oneSensorObservedProjection); - concatenatedCovariance = concatenatedCovariance.concat(sensorCovarianceFormatted); + concatenatedObservedProjection = concatenatedObservedProjection.concat(copy(oneSensorObservedProjection)); + concatenatedCovariance = concatenatedCovariance.concat(copy(sensorCovarianceFormatted)); } const formattedCovariance = polymorphMatrix(concatenatedCovariance, {dimension: nSensors * sensorDimension}); diff --git a/lib/setup/build-state-projection.js b/lib/setup/build-state-projection.js index 65e8906..40249cb 100644 --- a/lib/setup/build-state-projection.js +++ b/lib/setup/build-state-projection.js @@ -16,9 +16,10 @@ module.exports = function ({observation, dynamic}) { } if (observedProjection) { + const stateProjection = padWithZeros(observedProjection, {dimension: dynamicDimension}); return { observation: Object.assign({}, observation, { - stateProjection: padWithZeros(observedProjection, {dimension: dynamicDimension}) + stateProjection }), dynamic }; diff --git a/lib/state.js b/lib/state.js index cd2c83a..53372d6 100644 --- a/lib/state.js +++ b/lib/state.js @@ -57,7 +57,7 @@ class State { const {mean, covariance} = state; // Index const meanDimension = mean.length; if (typeof (dimension) === 'number' && meanDimension !== dimension) { - throw (new Error(`${meanDimension} and ${dimension} are not the same`)); + throw (new Error(`State.mean with dimension ${meanDimension} does not match expected dimension (${dimension})`)); } checkMatrix(mean, [meanDimension, 1]); diff --git a/test/api/basic.js b/test/api/basic.js index 6b431ac..e3b9a04 100644 --- a/test/api/basic.js +++ b/test/api/basic.js @@ -163,6 +163,9 @@ test('Sensor observation', t => { }); t.is(kFilter.observation.stateProjection().length, kFilter.observation.sensorDimension * kFilter.observation.nSensors); + + t.is(kFilter.observation.stateProjection()[0].length, 4); + t.is(kFilter.observation.covariance().length, 8); const observations = [[[102], [101], [98], [105]]];