Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions src/x/xMedian.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ interface XMedianOptions {
* @default false
*/
exact?: boolean;

/**
* First index (inclusive) of the range to consider.
* @default 0
*/
fromIndex?: number;

/**
* Last index (exclusive) of the range to consider.
* @default input.length
*/
toIndex?: number;
}

/**
Expand All @@ -24,13 +36,17 @@ export function xMedian(
throw new TypeError('input must be an array');
}

if (input.length === 0) {
const {
exact = false,
fromIndex = 0,
toIndex = input.length,
} = options || {};
const array = input.slice(fromIndex, toIndex);

if (array.length === 0) {
throw new TypeError('input must not be empty');
}

const { exact = false } = options || {};
const array = input.slice();

const middleIndex = calcMiddle(0, array.length - 1);

const median = quickSelect(array, middleIndex);
Expand Down
9 changes: 4 additions & 5 deletions src/xy/xyAlign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,6 @@ export function xyAlign(
i++;
}
} else {
const weightedX =
(data1.x[i] * data1.y[i] + data2.x[j] * data2.y[j]) /
(data1.y[i] + data2.y[j]);

switch (x) {
case 'x1':
result.x.push(data1.x[i]);
Expand All @@ -111,7 +107,10 @@ export function xyAlign(
result.x.push(data2.x[j]);
break;
case 'weighted':
result.x.push(weightedX);
result.x.push(
(data1.x[i] * data1.y[i] + data2.x[j] * data2.y[j]) /
(data1.y[i] + data2.y[j]),
);
break;
default:
throw new Error(`unknown x option value: ${String(x)}`);
Expand Down
2 changes: 1 addition & 1 deletion src/xy/xyMaxY.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function xyMaxY(data: DataXY, options: XYMaxYOptions = {}): number {
const { fromIndex, toIndex } = xGetFromToIndex(x, options);

let currentxyMaxY = y[fromIndex];
for (let i = fromIndex; i <= toIndex; i++) {
for (let i = fromIndex + 1; i <= toIndex; i++) {
if (y[i] > currentxyMaxY) currentxyMaxY = y[i];
}

Expand Down
2 changes: 1 addition & 1 deletion src/xy/xyMaxYPoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export function xyMaxYPoint(
const { fromIndex, toIndex } = xGetFromToIndex(x, options);

let current = { x: x[fromIndex], y: y[fromIndex], index: fromIndex };
for (let i = fromIndex; i <= toIndex; i++) {
for (let i = fromIndex + 1; i <= toIndex; i++) {
if (y[i] > current.y) current = { x: x[i], y: y[i], index: i };
}

Expand Down
1 change: 1 addition & 0 deletions src/xy/xyMedian.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export function xyMedian(data: DataXY): number {
if (cumSumY > sumY / 2) {
return x[i];
} else if (cumSumY === sumY / 2) {
if (i === x.length - 1) return x[i];
return 0.5 * (x[i] + x[i + 1]);
}
}
Expand Down
5 changes: 1 addition & 4 deletions src/xy/xyMedianY.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ export function xyMedianY(
for (let i = 0; i < y.length; i++) {
const fromIndex = Math.max(0, i - halfWindow);
const toIndex = Math.min(y.length, i + halfWindow + 1);
const window = ArrayBuffer.isView(y)
? y.subarray(fromIndex, toIndex)
: y.slice(fromIndex, toIndex);
result[i] = xMedian(window, { exact: false });
result[i] = xMedian(y, { exact: false, fromIndex, toIndex });
}

return { x, y: result };
Expand Down
5 changes: 1 addition & 4 deletions src/xy/xyMedianYAtXs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ export function xyMedianYAtXs(
const centerIndex = xFindClosestIndex(x, xValues[i]);
const fromIndex = Math.max(0, centerIndex - halfWindow);
const toIndex = Math.min(y.length, centerIndex + halfWindow + 1);
const window = ArrayBuffer.isView(y)
? y.subarray(fromIndex, toIndex)
: y.slice(fromIndex, toIndex);
result[i] = xMedian(window, { exact: false });
result[i] = xMedian(y, { exact: false, fromIndex, toIndex });
}

return { x: xValues, y: result };
Expand Down
2 changes: 1 addition & 1 deletion src/xy/xyMinYPoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function xyMinYPoint(
y: y[fromIndex],
index: fromIndex,
};
for (let i = fromIndex; i <= toIndex; i++) {
for (let i = fromIndex + 1; i <= toIndex; i++) {
if (y[i] < current.y) current = { x: x[i], y: y[i], index: i };
}

Expand Down
Loading