Skip to content

Commit

Permalink
fix: xMean gives correct result if fromIndex or toIndex outside range
Browse files Browse the repository at this point in the history
feat: xMean allows to specify from and to
  • Loading branch information
lpatiny committed Apr 1, 2022
1 parent c2acea7 commit 4141d0f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/x/__tests__/xMean.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ test('xMean', () => {
expect(xMean([1, 2, 3], { toIndex: 1 })).toBe(1.5);
expect(xMean([3, 2, 1], { toIndex: 1 })).toBe(2.5);
expect(xMean([1, 2, 3], { fromIndex: 1, toIndex: 1 })).toBe(2);
expect(xMean([1, 2, 3], { fromIndex: 1, toIndex: 10 })).toBe(2);
expect(xMean([1, 2, 3], { fromIndex: 1, toIndex: 10 })).toBe(2.5);
expect(xMean([3, 2, 1], { fromIndex: 1, toIndex: 1 })).toBe(2);
});
42 changes: 22 additions & 20 deletions src/x/xGetFromToIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,27 @@ import { NumberArray } from 'cheminfo-types';

import { xFindClosestIndex } from './xFindClosestIndex';

export interface XGetFromToIndexOptions {
/**
* First point for xyIntegration
* @default 0
* */
fromIndex?: number;
/**
* Last point for xyIntegration
* @default x.length-1
* */
toIndex?: number;
/**
* First value for xyIntegration in the X scale
* */
from?: number;
/**
* Last value for xyIntegration in the X scale
* */
to?: number;
}

/**
* Returns an object with {fromIndex, toIndex} for a specific from / to
*
Expand All @@ -10,26 +31,7 @@ import { xFindClosestIndex } from './xFindClosestIndex';
*/
export function xGetFromToIndex(
x: NumberArray,
options: {
/**
* First point for xyIntegration
* @default 0
* */
fromIndex?: number;
/**
* Last point for xyIntegration
* @default x.length-1
* */
toIndex?: number;
/**
* First value for xyIntegration in the X scale
* */
from?: number;
/**
* Last value for xyIntegration in the X scale
* */
to?: number;
} = {},
options: XGetFromToIndexOptions = {},
): { fromIndex: number; toIndex: number } {
let { fromIndex, toIndex, from, to } = options;

Expand Down
17 changes: 4 additions & 13 deletions src/x/xMean.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NumberArray } from 'cheminfo-types';

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

/**
* Computes the mean value of an array of values
Expand All @@ -10,21 +11,11 @@ import { xCheck } from './xCheck';
*/
export function xMean(
array: NumberArray,
options: {
/**
* First point for xyIntegration
* @default 0
*/
fromIndex?: number;
/**
* Last point for xyIntegration
* @default x.length-1
*/
toIndex?: number;
} = {},
options: XGetFromToIndexOptions = {},
): number {
xCheck(array);
const { fromIndex = 0, toIndex = array.length - 1 } = options;
const { fromIndex, toIndex } = xGetFromToIndex(array, options);

let sumValue = array[fromIndex];

for (let i = fromIndex + 1; i <= toIndex; i++) {
Expand Down

0 comments on commit 4141d0f

Please sign in to comment.