Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const memberRegex = /[+-]?[\w\d^]+/g;
- const ints = new Set(Array.from({ length: 10 }, (_, i) => i + ""));
- const isNum = (s) => !isNaN(s);
- const peelSpaces = (str) => str.replaceAll(" ", "");
- const getDegree = (member) => {
- const units = member.split("^");
- return units[1] ? +units[1] : 1;
- };
- const getCoeff = (member) => {
- const match = member
- .split("^")[0]
- .split("+")
- .reduce((acc, m) => {
- m.split("").forEach((i) => {
- (i === "-" || ints.has(i)) && (acc += i);
- });
- return acc;
- }, "");
- return parseInt(match) ? match : member[0] !== "-" ? 1 : -1;
- };
- const getVar = (polynom) => {
- ["+", "-", "^"].forEach((s) => {
- polynom = polynom.replace(s, "");
- });
- return (polynom = polynom.split("").filter(isNaN)[0]);
- };
- const getMembers = (polynom) => polynom.match(memberRegex) || [polynom];
- function polynomialProduct(polynom1, polynom2) {
- if (polynom1 === "1") return polynom2;
- if (polynom2 === "1") return polynom1;
- if (polynom1 === "0" || polynom2 === "0") return "0";
- polynom1 = peelSpaces(polynom1);
- polynom2 = peelSpaces(polynom2);
- if (isNum(polynom1) && isNum(polynom2)) {
- return "" + polynom1 * polynom2;
- }
- const members = [polynom1, polynom2].reduce((acc, p) => {
- let numbers = p;
- acc[p] = [
- ...getMembers(p).reduce((acc, mem) => {
- const v = getVar(mem);
- if (v) {
- numbers = numbers.replace(mem, ",");
- acc = [
- ...acc,
- {
- name: v,
- pow: getDegree(mem),
- coeff: getCoeff(mem),
- },
- ];
- }
- return acc;
- }, []),
- ...numbers
- .split(",")
- .filter(Boolean)
- .map((m) => {
- return {
- name: +m > 0 ? +m : +m * -1,
- pow: 1,
- coeff: +m,
- };
- }),
- ];
- return acc;
- }, {});
- const multiplied = members[polynom2]
- .flatMap((variable2) =>
- members[polynom1].map((variable1) => {
- const { coeff, name, pow } = variable2;
- const resultVariable = { ...variable1, coeff: variable1.coeff * coeff };
- if (isNum(resultVariable.name)) {
- resultVariable.name = name;
- resultVariable.pow = pow;
- resultVariable.isNumber = isNum(resultVariable.name);
- } else if (resultVariable.name === name) {
- resultVariable.pow += pow;
- }
- return resultVariable;
- })
- )
- .reduce((acc, variable) => {
- const { pow, name, coeff, isNumber } = variable;
- const nameWithPow = pow > 1 ? name + `^${pow}` : name;
- if (acc[nameWithPow] && acc[nameWithPow].coeff + coeff === 0) {
- delete acc[nameWithPow];
- return acc;
- }
- if (!acc[nameWithPow]) {
- acc[nameWithPow] = { coeff: 0, isNumber: false, pow: 0 };
- }
- acc[nameWithPow].pow = pow;
- acc[nameWithPow].isNumber = isNumber;
- acc[nameWithPow].coeff += coeff;
- return acc;
- }, {});
- const sorted = Object.keys(multiplied).sort((keya, keyb) => {
- return multiplied[keyb].pow > multiplied[keya].pow ? 1 : -1;
- });
- const result = sorted.reduce((acc, variable) => {
- let { coeff, isNumber } = multiplied[variable];
- if (isNumber) {
- coeff > 0 && (acc += "+");
- acc += coeff;
- return acc;
- }
- if (coeff === -1) coeff = "-";
- if (acc !== "" && coeff > 0) acc += "+";
- if (coeff === 1) coeff = "";
- acc += `${coeff}${variable}`;
- return acc;
- }, "");
- return result;
- }
- function perform() {
- console.log("perf");
- polynomialProduct("u^2 + 2u+1", "u + 1");
- polynomialProduct("x^2", "3x - 1");
- polynomialProduct("2", "4y - 4");
- polynomialProduct("-4r^2 + 1", "-1");
- polynomialProduct("1", "p^3");
- polynomialProduct("1", "-1");
- polynomialProduct("0", "2 - x");
- polynomialProduct("-1", "0");
- polynomialProduct("v^2 - 1+3v^3", "1+v^2");
- polynomialProduct("2x^2 + 3x + 1", "3x - 1");
- polynomialProduct("-x^3 + 3x^2 - 3x + 1", " -x + 1");
- polynomialProduct("u^2 + 3u - 1", "2u - 1");
- polynomialProduct("3u^2-4u-2", "u-7");
- polynomialProduct("u^2 - 2u + 1", "u^2 + 2u + 1");
- polynomialProduct("4", "- 2");
- polynomialProduct("-10i^13 - i + 152i^101", "-3 -98i^34 + 7i^2 - 4i");
- polynomialProduct(
- "-42+33d-27d^2-50d^5+15d^7+38d^8-19d^9-30d^10-3d^12+5d^15+d^21-d^22-19d^23+12d^24+12d^26-16d^27-35d^28-39d^29-18d^31-15d^32-21d^33-41d^34+43d^35+d^36+27d^38-30d^39+29d^43+40d^45+9d^46+d^47-23d^48-42+33d-27d^2-50d^5+15d^7+38d^8-19d^9-30d^10-3d^12+5d^15+d^21-d^22-19d^23+12d^24+12d^26-16d^27-35d^28-39d^29-18d^31-15d^32-21d^33-41d^34+43d^35+d^36+27d^38-30d^39+29d^43+40d^45+9d^46+d^47-23d^48-42+33d-27d^2-50d^5+15d^7+38d^8-19d^9-30d^10-3d^12+5d^15+d^21-d^22-19d^23+12d^24+12d^26-16d^27-35d^28-39d^29-18d^31-15d^32-21d^33-41d^34+43d^35+d^36+27d^38-30d^39+29d^43+40d^45+9d^46+d^47-23d^48-42+33d-27d^2-50d^5+15d^7+38d^8-19d^9-30d^10-3d^12+5d^15+d^21-d^22-19d^23+12d^24+12d^26-16d^27-35d^28-39d^29-18d^31-15d^32-21d^33-41d^34+43d^35+d^36+27d^38-30d^39+29d^43+40d^45+9d^46+d^47-23d^48",
- "11d+20d^2-d^3+14d^11-d^13-6d^15+d^18-10d^19+16d^20-d^21+23d^23-30d^24-42+33d-27d^2-50d^5+15d^7+38d^8-19d^9-30d^10-3d^12+5d^15+d^21-d^22-19d^23+12d^24+12d^26-16d^27-35d^28-39d^29-18d^31-15d^32-21d^33-41d^34+43d^35+d^36+27d^38-30d^39+29d^43+40d^45+9d^46+d^47-23d^48-42+33d-27d^2-50d^5+15d^7+38d^8-19d^9-30d^10-3d^12+5d^15+d^21-d^22-19d^23+12d^24+12d^26-16d^27-35d^28-39d^29-18d^31-15d^32-21d^33-41d^34+43d^35+d^36+27d^38-30d^39+29d^43+40d^45+9d^46+d^47-23d^48-42+33d-27d^2-50d^5+15d^7+38d^8-19d^9-30d^10-3d^12+5d^15+d^21-d^22-19d^23+12d^24+12d^26-16d^27-35d^28-39d^29-18d^31-15d^32-21d^33-41d^34+43d^35+d^36+27d^38-30d^39+29d^43+40d^45+9d^46+d^47-23d^48-42+33d-27d^2-50d^5+15d^7+38d^8-19d^9-30d^10-3d^12+5d^15+d^21-d^22-19d^23+12d^24+12d^26-16d^27-35d^28-39d^29-18d^31-15d^32-21d^33-41d^34+43d^35+d^36+27d^38-30d^39+29d^43+40d^45+9d^46+d^47-23d^48-2d^30-27d^32-d^37-14d^38+3d^39+8d^40+11d^46"
- );
- }
- module.exports = polynomialProduct;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement