protected override GCDResult PolyGCD(Polynomial g, out Polynomial gcd) { Polynomial f = this; GCDResult A = new GCDResult(new Polynomial { 1 }, new Polynomial { 0 }); GCDResult B = new GCDResult(new Polynomial { 0 }, new Polynomial { 1 }); GCDResult Temp; Polynomial a, b; if (f.degree > g.degree) { a = new Polynomial(f); b = new Polynomial(g); } else { a = new Polynomial(g); b = new Polynomial(f); } while (!b.IsNull()) { DividionResult divres = a / b; a = b; b = divres.Item2; Temp = new GCDResult(A - new GCDResult(B.Item1 * divres.Quotient, B.Item2 * divres.Quotient)); A = B; B = Temp; } gcd = a; if (f.degree <= g.degree) { A = new GCDResult(A.Coef2, A.Coef1); } if (gcd[gcd.degree] < 0) { gcd = -gcd; return(new GCDResult(-A.Item1, -A.Item2)); } return(new GCDResult(A.Item1, A.Item2)); }
protected override GCDResult PolyGCD(IntPolynomial g, out IntPolynomial gcd) { IntPolynomial f = this; GCDResult A = new GCDResult(new IntPolynomial { 1 }, new IntPolynomial { 0 }); GCDResult B = new GCDResult(new IntPolynomial { 0 }, new IntPolynomial { 1 }); IntPolynomial a, b; if (f.degree > g.degree) { a = new IntPolynomial(f); b = new IntPolynomial(g); } else { a = new IntPolynomial(g); b = new IntPolynomial(f); } BigInteger CoeffReminder; while (!b.IsNull()) { BigInteger CurrentMultiplyCoeff = BigInteger.Pow(b[b.size - 1], (a.size - 1) - (b.size - 1) + 1); DividionResult divres = (a * CurrentMultiplyCoeff) / b; a = b; CoeffReminder = divres.Item2.CoeffGCD(); b = divres.Item2 / CoeffReminder; } CoeffReminder = a.CoeffGCD(); gcd = a / CoeffReminder; if (gcd[gcd.size - 1] < 0) { gcd *= -1; } return(new GCDResult(A.Item1, A.Item2)); }
public static FullSolution SolveEquation(Polynomial f, Polynomial g, Polynomial c) { FullSolution solution = new FullSolution(); //f=0; g=0; c=0 | c!=0 if (f.IsNull() && g.IsNull()) { if (c.IsNull()) { solution.zeroSolution = new Solution(new Polynomial(), new Polynomial()); solution.solutionStep = new Solution(null, null); solution.areCoefsIndependent = true; } else { solution.isDefined = false; } return(solution); } if (c.IsNull() && f.IsNull() != g.IsNull()) { solution.zeroSolution = new Solution(new Polynomial(), new Polynomial()); if (f.IsNull() && !g.IsNull()) { solution.solutionStep = new Solution(null, new Polynomial()); } else if (!f.IsNull() && g.IsNull()) { solution.solutionStep = new Solution(new Polynomial(), null); } return(solution); } else if (f.IsNull() != g.IsNull()) { if (f.IsNull()) { DividionResult divres = c / g; if (!divres.Reminder.IsNull()) { solution.isDefined = false; return(solution); } solution.solutionStep = new Solution(null, new Polynomial { 0 }); solution.zeroSolution = new Solution(new Polynomial { 0 }, divres.Quotient); } else { DividionResult divres = c / f; if (!divres.Reminder.IsNull()) { solution.isDefined = false; return(solution); } solution.solutionStep = new Solution(new Polynomial { 0 }, null); solution.zeroSolution = new Solution(divres.Quotient, new Polynomial { 0 }); } return(solution); } Polynomial gcd = new Polynomial(); GCDResult expCoefs = GCD(f, g, out gcd); DividionResult tempX0 = (expCoefs.Coef1 * c / gcd); DividionResult tempY0 = (expCoefs.Coef2 * c / gcd); // К этому моменту f и g не нулевые (ветвление для c) if (c.IsNull()) { solution.zeroSolution = new Solution(new Polynomial(), new Polynomial()); solution.solutionStep = new Solution((g / gcd).Quotient, -(f / gcd).Quotient); } else { if (tempX0.Reminder.IsNull() && tempY0.Reminder.IsNull()) { solution.zeroSolution = new Solution(tempX0.Quotient, tempY0.Quotient); solution.solutionStep = new Solution((g / gcd).Quotient, -(f / gcd).Quotient); } else { return(new FullSolution() { isDefined = false }); } } return(solution); }