Advertisement
Guest User

Untitled

a guest
Dec 8th, 2023
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.01 KB | None | 0 0
  1. const memberRegex = /[+-]?[\w\d^]+/g;
  2. const ints = new Set(Array.from({ length: 10 }, (_, i) => i + ""));
  3.  
  4. const isNum = (s) => !isNaN(s);
  5.  
  6. const peelSpaces = (str) => str.replaceAll(" ", "");
  7.  
  8. const getDegree = (member) => {
  9. const units = member.split("^");
  10. return units[1] ? +units[1] : 1;
  11. };
  12.  
  13. const getCoeff = (member) => {
  14. const match = member
  15. .split("^")[0]
  16. .split("+")
  17. .reduce((acc, m) => {
  18. m.split("").forEach((i) => {
  19. (i === "-" || ints.has(i)) && (acc += i);
  20. });
  21. return acc;
  22. }, "");
  23. return parseInt(match) ? match : member[0] !== "-" ? 1 : -1;
  24. };
  25.  
  26. const getVar = (polynom) => {
  27. ["+", "-", "^"].forEach((s) => {
  28. polynom = polynom.replace(s, "");
  29. });
  30. return (polynom = polynom.split("").filter(isNaN)[0]);
  31. };
  32.  
  33. const getMembers = (polynom) => polynom.match(memberRegex) || [polynom];
  34.  
  35. function polynomialProduct(polynom1, polynom2) {
  36. if (polynom1 === "1") return polynom2;
  37. if (polynom2 === "1") return polynom1;
  38. if (polynom1 === "0" || polynom2 === "0") return "0";
  39.  
  40. polynom1 = peelSpaces(polynom1);
  41. polynom2 = peelSpaces(polynom2);
  42.  
  43. if (isNum(polynom1) && isNum(polynom2)) {
  44. return "" + polynom1 * polynom2;
  45. }
  46.  
  47. const members = [polynom1, polynom2].reduce((acc, p) => {
  48. let numbers = p;
  49. acc[p] = [
  50. ...getMembers(p).reduce((acc, mem) => {
  51. const v = getVar(mem);
  52. if (v) {
  53. numbers = numbers.replace(mem, ",");
  54. acc = [
  55. ...acc,
  56. {
  57. name: v,
  58. pow: getDegree(mem),
  59. coeff: getCoeff(mem),
  60. },
  61. ];
  62. }
  63. return acc;
  64. }, []),
  65. ...numbers
  66. .split(",")
  67. .filter(Boolean)
  68. .map((m) => {
  69. return {
  70. name: +m > 0 ? +m : +m * -1,
  71. pow: 1,
  72. coeff: +m,
  73. };
  74. }),
  75. ];
  76. return acc;
  77. }, {});
  78.  
  79. const multiplied = members[polynom2]
  80. .flatMap((variable2) =>
  81. members[polynom1].map((variable1) => {
  82. const { coeff, name, pow } = variable2;
  83. const resultVariable = { ...variable1, coeff: variable1.coeff * coeff };
  84. if (isNum(resultVariable.name)) {
  85. resultVariable.name = name;
  86. resultVariable.pow = pow;
  87. resultVariable.isNumber = isNum(resultVariable.name);
  88. } else if (resultVariable.name === name) {
  89. resultVariable.pow += pow;
  90. }
  91. return resultVariable;
  92. })
  93. )
  94. .reduce((acc, variable) => {
  95. const { pow, name, coeff, isNumber } = variable;
  96. const nameWithPow = pow > 1 ? name + `^${pow}` : name;
  97. if (acc[nameWithPow] && acc[nameWithPow].coeff + coeff === 0) {
  98. delete acc[nameWithPow];
  99. return acc;
  100. }
  101. if (!acc[nameWithPow]) {
  102. acc[nameWithPow] = { coeff: 0, isNumber: false, pow: 0 };
  103. }
  104. acc[nameWithPow].pow = pow;
  105. acc[nameWithPow].isNumber = isNumber;
  106. acc[nameWithPow].coeff += coeff;
  107. return acc;
  108. }, {});
  109.  
  110. const sorted = Object.keys(multiplied).sort((keya, keyb) => {
  111. return multiplied[keyb].pow > multiplied[keya].pow ? 1 : -1;
  112. });
  113.  
  114. const result = sorted.reduce((acc, variable) => {
  115. let { coeff, isNumber } = multiplied[variable];
  116. if (isNumber) {
  117. coeff > 0 && (acc += "+");
  118. acc += coeff;
  119. return acc;
  120. }
  121. if (coeff === -1) coeff = "-";
  122. if (acc !== "" && coeff > 0) acc += "+";
  123. if (coeff === 1) coeff = "";
  124. acc += `${coeff}${variable}`;
  125. return acc;
  126. }, "");
  127.  
  128. return result;
  129. }
  130.  
  131. function perform() {
  132. console.log("perf");
  133.  
  134. polynomialProduct("u^2 + 2u+1", "u + 1");
  135. polynomialProduct("x^2", "3x - 1");
  136. polynomialProduct("2", "4y - 4");
  137. polynomialProduct("-4r^2 + 1", "-1");
  138. polynomialProduct("1", "p^3");
  139. polynomialProduct("1", "-1");
  140. polynomialProduct("0", "2 - x");
  141. polynomialProduct("-1", "0");
  142. polynomialProduct("v^2 - 1+3v^3", "1+v^2");
  143. polynomialProduct("2x^2 + 3x + 1", "3x - 1");
  144. polynomialProduct("-x^3 + 3x^2 - 3x + 1", " -x + 1");
  145. polynomialProduct("u^2 + 3u - 1", "2u - 1");
  146. polynomialProduct("3u^2-4u-2", "u-7");
  147. polynomialProduct("u^2 - 2u + 1", "u^2 + 2u + 1");
  148. polynomialProduct("4", "- 2");
  149. polynomialProduct("-10i^13 - i + 152i^101", "-3 -98i^34 + 7i^2 - 4i");
  150. polynomialProduct(
  151. "-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",
  152. "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"
  153. );
  154. }
  155.  
  156. module.exports = polynomialProduct;
  157.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement