diff --git a/src/lib/analysis/compounds.ts b/src/lib/analysis/compounds.ts index 8ab3665..53a71b5 100644 --- a/src/lib/analysis/compounds.ts +++ b/src/lib/analysis/compounds.ts @@ -5,7 +5,7 @@ import type { CompoundsCollectorInterface, CompoundsSummary, } from '../types/analysis'; -import { round } from '../math'; +import { createFilledArray, createVector, round } from '../math'; export class CompoundsCollector implements CompoundsCollectorInterface { private atomCompoundsMap: Map = new Map(); @@ -180,8 +180,13 @@ export class CompoundsAnalyzer implements CompoundsAnalyzerSummary { } private getCompoundSpeed(compound: Compound): number { - const speeds = [...compound].map((atom) => atom.speed.abs); - return speeds.reduce((acc, x) => acc + x, 0) / compound.size; + if (compound.size === 0) { + return 0; + } + + const speedVectors = [...compound].map((atom) => atom.speed); + const zeroVector = createVector(createFilledArray(speedVectors[0].length, 0)); + return speedVectors.reduce((acc, vector) => acc.add(vector), zeroVector).abs / compound.size; } private convertMapToArray(map: Record): Array { diff --git a/src/lib/analysis/helpers.ts b/src/lib/analysis/helpers.ts index 8edfa6f..7ea3f1b 100644 --- a/src/lib/analysis/helpers.ts +++ b/src/lib/analysis/helpers.ts @@ -29,6 +29,22 @@ export function createTransparentWeights(): TotalSummaryWeights { mean: 1, median: 1, }, + COMPOUND_SPEED_SUMMARY: { + size: 1, + frequency: 1, + min: 1, + max: 1, + mean: 1, + median: 1, + }, + COMPOUND_SPEED_BY_TYPES_SUMMARY: { + size: 1, + frequency: 1, + min: 1, + max: 1, + mean: 1, + median: 1, + }, }; } @@ -45,6 +61,8 @@ export function getSummaryMatrixGroupIndexes(typesCount: number): number[][] { createFilledArray(typesCount, 8), [9, 10, 11, 12, 13], repeatArrayValues([14, 15, 16, 17, 18], typesCount), + [19, 20, 21, 22], + repeatArrayValues([23, 24, 25, 26], typesCount), ].flat(Infinity) as number[]; const groupIndexes: number[][] = Array.from({ length: Math.max(...groups) + 1 }, () => []); @@ -67,6 +85,8 @@ export function convertWeightsToSummaryMatrixRow(weights: TotalSummaryWeights, t createFilledArray(typesCount, weights.COMPOUNDS_PER_ATOM_BY_TYPES), Object.values(weights.COMPOUND_LENGTH_SUMMARY).slice(1), repeatArrayValues(Object.values(weights.COMPOUND_LENGTH_BY_TYPES_SUMMARY).slice(1), typesCount), + Object.values(weights.COMPOUND_SPEED_SUMMARY).slice(2), + repeatArrayValues(Object.values(weights.COMPOUND_SPEED_BY_TYPES_SUMMARY).slice(2), typesCount), ].flat(Infinity) as number[]; } @@ -77,6 +97,10 @@ export function convertSummaryToSummaryMatrixRow(summary: TotalSummary): number[ const compoundLengthByTypesSummary = summary.COMPOUNDS.itemLengthByTypesSummary.map( (item) => Object.values(item).slice(1), ); + const compoundSpeedSummary = Object.values(summary.COMPOUNDS.itemSpeedSummary).slice(2); + const compoundSpeedByTypesSummary = summary.COMPOUNDS.itemSpeedByTypesSummary.map( + (item) => Object.values(item).slice(2), + ); return [ summary.WORLD.ATOMS_MEAN_SPEED, @@ -90,6 +114,8 @@ export function convertSummaryToSummaryMatrixRow(summary: TotalSummary): number[ compoundsPerAtomByTypes, compoundLengthSummary, compoundLengthByTypesSummary, + compoundSpeedSummary, + compoundSpeedByTypesSummary, ].flat(Infinity) as number[]; } diff --git a/src/lib/types/analysis.ts b/src/lib/types/analysis.ts index 04f1626..6c3f21d 100644 --- a/src/lib/types/analysis.ts +++ b/src/lib/types/analysis.ts @@ -112,4 +112,6 @@ export type TotalSummaryWeights = { COMPOUNDS_PER_ATOM_BY_TYPES: number; COMPOUND_LENGTH_SUMMARY: CompoundsSummary; COMPOUND_LENGTH_BY_TYPES_SUMMARY: CompoundsSummary; + COMPOUND_SPEED_SUMMARY: CompoundsSummary; + COMPOUND_SPEED_BY_TYPES_SUMMARY: CompoundsSummary; }