Skip to content

Commit

Permalink
feat: add xMaxAbsoluteValue
Browse files Browse the repository at this point in the history
  • Loading branch information
lpatiny committed Jul 28, 2022
1 parent ff32508 commit d173542
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export * from './x/xIsMonotone';
export * from './x/xIsMonotoneIncreasing';
export * from './x/xMaxIndex';
export * from './x/xMaxValue';
export * from './x/xMaxAbsoluteValue';
export * from './x/xMean';
export * from './x/xMeanAbsoluteError';
export * from './x/xMeanSquaredError';
Expand Down
21 changes: 21 additions & 0 deletions src/x/__tests__/xMaxAbsoluteValue.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { xMaxAbsoluteValue } from '../xMaxAbsoluteValue';

test('xMaxAbsoluteValue', () => {
let typedArray = new Int16Array(3);
typedArray[0] = 1;
typedArray[1] = 2;
typedArray[2] = -3;

expect(xMaxAbsoluteValue([0])).toBe(0);
expect(xMaxAbsoluteValue([1])).toBe(1);
expect(xMaxAbsoluteValue([1, 2])).toBe(2);
expect(xMaxAbsoluteValue([1, 2, 1])).toBe(2);
expect(xMaxAbsoluteValue([3, 2, 1])).toBe(3);
expect(xMaxAbsoluteValue(typedArray)).toBe(3);
expect(xMaxAbsoluteValue([1, 2, 3], { fromIndex: 1 })).toBe(3);
expect(xMaxAbsoluteValue([3, 2, 1], { fromIndex: 1 })).toBe(2);
expect(xMaxAbsoluteValue([1, 2, 3], { toIndex: 1 })).toBe(2);
expect(xMaxAbsoluteValue([3, 2, 1], { toIndex: 1 })).toBe(3);
expect(xMaxAbsoluteValue([1, 2, 3], { fromIndex: 1, toIndex: 1 })).toBe(2);
expect(xMaxAbsoluteValue([3, 2, 1], { fromIndex: 1, toIndex: 1 })).toBe(2);
});
32 changes: 32 additions & 0 deletions src/x/xMaxAbsoluteValue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { NumberArray } from 'cheminfo-types';

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

/**
* Computes the maximal value of an array of values
*
* @param array - array of numbers
* @param options - options
*/
export function xMaxAbsoluteValue(
array: NumberArray,
options: XGetFromToIndexOptions = {},
): number {
xCheck(array);
const { fromIndex, toIndex } = xGetFromToIndex(array, options);
let maxValue = array[fromIndex];

for (let i = fromIndex + 1; i <= toIndex; i++) {
if (array[i] >= 0) {
if (array[i] > maxValue) {
maxValue = array[i];
}
} else {
if (-array[i] > maxValue) {
maxValue = -array[i];
}
}
}
return maxValue;
}

0 comments on commit d173542

Please sign in to comment.