import { xGetFromToIndex } from "../x/xGetFromToIndex.js";
import { xyCheck } from "./xyCheck.js";
/**
 * Computes the weighted mean value of an array of values.
 * @param data - array of DataXY.
 * @param options - options.
 * @returns weighted mean x value (center of mass).
 */
export function xyMassCenter(data, options = {}) {
    xyCheck(data, { minLength: 1 });
    const { x, y } = data;
    const { fromIndex, toIndex } = xGetFromToIndex(x, options);
    let sumYs = 0;
    let sumProducts = 0;
    for (let i = fromIndex; i <= toIndex; i++) {
        sumProducts += x[i] * y[i];
        sumYs += y[i];
    }
    if (sumYs === 0) {
        throw new Error('Sum of Ys can not be zero.');
    }
    return sumProducts / sumYs;
}
//# sourceMappingURL=xyMassCenter.js.map