-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / nodejs / test (18)src/xy/__tests__/xyMassCenter.test.ts > xyMassCenter
Check failure on line 41 in src/xy/__tests__/xyMassCenter.test.ts GitHub Actions / nodejs / test (20)src/xy/__tests__/xyMassCenter.test.ts > xyMassCenter
Check failure on line 41 in src/xy/__tests__/xyMassCenter.test.ts GitHub Actions / nodejs / test (21)src/xy/__tests__/xyMassCenter.test.ts > xyMassCenter
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |