nerdamer-ts
Version:
javascript light-weight symbolic math expression evaluator
79 lines • 2.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.subtract = void 0;
const add_1 = require("./add");
const Errors_1 = require("../../../Core/Errors");
const Matrix_1 = require("../../../Types/Matrix");
const Utils_1 = require("../../../Core/Utils");
/**
* Gets called when the parser finds the - operator. Not the prefix operator. See this.add
* @param {Symbol} a
* @param {Symbol} b
* @returns {Symbol}
*/
function subtract(a, b) {
var aIsSymbol = (0, Utils_1.isSymbol)(a), bIsSymbol = (0, Utils_1.isSymbol)(b);
if (aIsSymbol && bIsSymbol) {
if (a.unit || b.unit) {
return deps.Unit.subtract(a, b);
}
return (0, add_1.add)(a, b.negate());
}
else {
if (bIsSymbol && (0, Utils_1.isVector)(a)) {
b = a.map(function (x) {
return subtract(x, b.clone());
});
}
else if (aIsSymbol && (0, Utils_1.isVector)(b)) {
b = b.map(function (x) {
return subtract(a.clone(), x);
});
}
else if ((0, Utils_1.isVector)(a) && (0, Utils_1.isVector)(b)) {
if (a.dimensions() === b.dimensions())
b = a.subtract(b);
else
(0, Errors_1.err)('Unable to subtract vectors. Dimensions do not match.');
}
else if ((0, Utils_1.isMatrix)(a) && (0, Utils_1.isVector)(b)) {
if (b.elements.length === a.rows()) {
var M = new Matrix_1.Matrix(), l = a.cols();
b.each(function (e, i) {
var row = [];
for (var j = 0; j < l; j++) {
row.push(subtract(a.elements[i - 1][j].clone(), e.clone()));
}
M.elements.push(row);
});
return M;
}
else
(0, Errors_1.err)('Dimensions must match!');
}
else if ((0, Utils_1.isVector)(a) && (0, Utils_1.isMatrix)(b)) {
var M = b.clone().negate();
return (0, add_1.add)(M, a);
}
else if ((0, Utils_1.isMatrix)(a) && (0, Utils_1.isMatrix)(b)) {
b = a.subtract(b);
}
else if ((0, Utils_1.isMatrix)(a) && bIsSymbol) {
var M = new Matrix_1.Matrix();
a.each(function (x, i, j) {
M.set(i, j, subtract(x, b.clone()));
});
b = M;
}
else if (aIsSymbol && (0, Utils_1.isMatrix)(b)) {
var M = new Matrix_1.Matrix();
b.each(function (x, i, j) {
M.set(i, j, subtract(a.clone(), x));
});
b = M;
}
return b;
}
}
exports.subtract = subtract;
//# sourceMappingURL=subtract.js.map