Skip to content

Commit

Permalink
feat: add xyMassCenter
Browse files Browse the repository at this point in the history
closes: #238
  • Loading branch information
gcartier-epfl committed Apr 10, 2024
1 parent 5356dd7 commit aede9fb
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/__tests__/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ exports[`test existence of exported functions 1`] = `
"xyIntegral",
"xyIntegration",
"xyJoinX",
"xyMassCenter",
"xyMassCenterVector",
"xyMaxClosestYPoint",
"xyMaximaY",
Expand Down
1 change: 1 addition & 0 deletions src/x/xMeanWeighted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { xGetFromToIndex, XGetFromToIndexOptions } from './xGetFromToIndex';
/**
* Computes the weighted mean value of an array of values.
*
* @deprecated please use xyMassCenter
* @param array - array of numbers
* @param weights - array of weights
* @param options - options
Expand Down
42 changes: 42 additions & 0 deletions src/xy/__tests__/xyMassCenter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { expect, test } from 'vitest';

import { xyMassCenter } from '../xyMassCenter';

test('xyMassCenter', () => {
const typedArray = Uint16Array.from([1, 2, 3, 4, 5]);

expect(xyMassCenter({ x: [0], y: [1] })).toBe(0);
expect(xyMassCenter({ x: [1], y: [1] })).toBe(1);
expect(xyMassCenter({ x: [1, 2], y: [1, 1] })).toBe(1.5);
expect(xyMassCenter({ x: [1, 2, 3], y: [1, 1, 1] })).toBe(2);
expect(xyMassCenter({ x: [3, 2, 1], y: [1, 1, 1] })).toBe(2);
expect(xyMassCenter({ x: typedArray, y: [1, 1, 1, 1, 1] })).toBe(3);
expect(xyMassCenter({ x: [1, 2, 3], y: [1, 1, 1] }, { fromIndex: 1 })).toBe(
2.5,
);
expect(xyMassCenter({ x: [3, 2, 1], y: [1, 1, 1] }, { fromIndex: 1 })).toBe(
1.5,
);
expect(xyMassCenter({ x: [1, 2, 3], y: [1, 1, 1] }, { toIndex: 1 })).toBe(
1.5,
);
expect(xyMassCenter({ x: [3, 2, 1], y: [1, 1, 1] }, { toIndex: 1 })).toBe(
2.5,
);
expect(
xyMassCenter({ x: [1, 2, 3], y: [1, 1, 1] }, { fromIndex: 1, toIndex: 1 }),
).toBe(2);
expect(
xyMassCenter({ x: [1, 2, 3], y: [1, 1, 1] }, { fromIndex: 1, toIndex: 10 }),
).toBe(2.5);
expect(
xyMassCenter({ x: [3, 2, 1], y: [1, 1, 1] }, { fromIndex: 1, toIndex: 1 }),
).toBe(2);
expect(() => xyMassCenter({ x: [], y: [] })).toThrow(
'data.x must have a length of at least',
);
expect(() => xyMassCenter({ x: [1], y: [] })).toThrow(
'the x and y arrays must have the same',
);
expect(xyMassCenter({ x: [1, 2, 3], y: [0, 0, 0] })).toBe(0);

Check failure on line 41 in src/xy/__tests__/xyMassCenter.test.ts

View workflow job for this annotation

GitHub Actions / nodejs / test (18)

src/xy/__tests__/xyMassCenter.test.ts > xyMassCenter

AssertionError: expected NaN to be +0 // Object.is equality - Expected + Received - 0 + NaN ❯ src/xy/__tests__/xyMassCenter.test.ts:41:56

Check failure on line 41 in src/xy/__tests__/xyMassCenter.test.ts

View workflow job for this annotation

GitHub Actions / nodejs / test (20)

src/xy/__tests__/xyMassCenter.test.ts > xyMassCenter

AssertionError: expected NaN to be +0 // Object.is equality - Expected + Received - 0 + NaN ❯ src/xy/__tests__/xyMassCenter.test.ts:41:56

Check failure on line 41 in src/xy/__tests__/xyMassCenter.test.ts

View workflow job for this annotation

GitHub Actions / nodejs / test (21)

src/xy/__tests__/xyMassCenter.test.ts > xyMassCenter

AssertionError: expected NaN to be +0 // Object.is equality - Expected + Received - 0 + NaN ❯ src/xy/__tests__/xyMassCenter.test.ts:41:56
});
1 change: 1 addition & 0 deletions src/xy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export * from './xyGrowingX';
export * from './xyIntegral';
export * from './xyIntegration';
export * from './xyJoinX';
export * from './xyMassCenter';
export * from './xyMassCenterVector';
export * from './xyMaxClosestYPoint';
export * from './xyMaximaY';
Expand Down
29 changes: 29 additions & 0 deletions src/xy/xyMassCenter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { DataXY } from 'cheminfo-types';

import { xGetFromToIndex, XGetFromToIndexOptions } from '../x/xGetFromToIndex';

import { xyCheck } from '.';

/**
* Computes the weighted mean value of an array of values.
*
* @param data - array of DataXY
* @param options - options
*/
export function xyMassCenter(
data: DataXY,
options: XGetFromToIndexOptions = {},
): number {
xyCheck(data, { minLength: 1 });
const { x, y } = data;
const { fromIndex, toIndex } = xGetFromToIndex(x, options);

let sumWeights = 0;
let sumValue = 0;
for (let i = fromIndex; i <= toIndex; i++) {
sumValue += x[i] * y[i];
sumWeights += y[i];
}

return sumValue / sumWeights;
}

0 comments on commit aede9fb

Please sign in to comment.