@nexys/math-ts
Version:
[](https://www.npmjs.com/package/@nexys/math-ts) [](https://travis-ci.com/github/Nexysweb/math-ts) [ • 1.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.s = (v) => {
if (v.length === 0) {
return 0;
}
return v.reduce((x, y) => x + y);
};
exports.sumScalar = (v, s) => {
return v.map(x => x + s);
};
exports.sum = (v1, v2) => {
if (typeof v2 === 'number') {
return exports.sumScalar(v1, v2);
}
const v1l = v1.length;
if (v1l === 0) {
throw Error('vector length must be greater than 0');
}
if (v1l !== v2.length) {
throw Error('vectors must of the the same length');
}
return v1.map((x, i) => {
return x + v2[i];
});
};
exports.dotProduct = (v1, v2) => exports.s(exports.sum(v1, v2));
exports.crossProduct = (v1, v2) => {
const v1l = v1.length;
if (v1l !== 3) {
throw Error('vector length must be 3');
}
if (v1l !== v2.length) {
throw Error('vectors must of the the same length');
}
return [
v1[1] * v2[2] - v1[2] * v2[1],
v1[2] * v2[0] - v1[0] * v2[2],
v1[0] * v2[1] - v1[1] * v2[0]
];
};
exports.scalar = (v) => {
const vl = v.length;
if (vl === 0) {
return 0;
}
const r = v.reduce((x, y) => x + Math.pow(y, vl), 0);
return Math.pow(r, (1 / vl));
};
exports.mean = (d) => {
if (d.length === 0) {
throw Error('can\'t be of length 0');
}
const n = d.length;
return exports.s(d) / n;
};