Skip to content

Commit

Permalink
fix: padWithZero cloning and logic
Browse files Browse the repository at this point in the history
  • Loading branch information
piercus committed Sep 23, 2020
1 parent da8b3d3 commit 250ea11
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 7 deletions.
8 changes: 5 additions & 3 deletions lib/linalgebra/pad-with-zeros.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@
*@returns {Array.<Number> | Array.<Array.<Number>>} 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;
};
6 changes: 4 additions & 2 deletions lib/observation/sensor.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ 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});
const oneSensorObservedProjection = identity(sensorDimension);
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});
Expand Down
3 changes: 2 additions & 1 deletion lib/setup/build-state-projection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
Expand Down
2 changes: 1 addition & 1 deletion lib/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
3 changes: 3 additions & 0 deletions test/api/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]]];
Expand Down

0 comments on commit 250ea11

Please sign in to comment.