public static Polynomial <XTerm> AddRoots(this Polynomial <XTerm> poly1, Polynomial <XTerm> poly2) { var deg1 = poly1.Degree(); var deg2 = poly2.Degree(); Polynomial <XYTerm> X = new XYTerm(1, 0); Polynomial <XYTerm> Y = new XYTerm(0, 1); var powersOfXPlusY = CollectionUtil.Naturals.Select(e => (X + Y).RaisedTo(e)); var maxDegreeOfResult = deg1 * deg2; if (maxDegreeOfResult == 0) { return(1); } var reducedPowersOfX = PolyBasisWithHighPowersReducedUsing(poly1).Select(p => p.ToPolynomialOverVariable1Of2()).Take(maxDegreeOfResult + 1).ToArray(); var reducedPowersOfY = PolyBasisWithHighPowersReducedUsing(poly2).Select(p => p.ToPolynomialOverVariable2Of2()).Take(maxDegreeOfResult + 1).ToArray(); var reducedPowersOfXPlusY = powersOfXPlusY .Select(e => e.Coefficients .Select(r => reducedPowersOfX[(int)r.Key.XPower] * reducedPowersOfY[(int)r.Key.YPower] * r.Value) .Sum()) .Take(maxDegreeOfResult + 1) .ToArray(); var linearSystem = Matrix.FromColumns( from col in reducedPowersOfXPlusY select from row in maxDegreeOfResult.Range() let exponentForX = row / deg2 let exponentForY = row % deg2 select col.Coefficient(new XYTerm(exponentForX, exponentForY))); var solvedSystem = linearSystem.Reduced(); var degreeOfSolution = solvedSystem.Rows.Count(row => row.Any(cell => cell != 0)); var lowCoefficients = solvedSystem.Columns[degreeOfSolution].Take(degreeOfSolution); return(XToThe(degreeOfSolution) - lowCoefficients.TimesPolynomialBasis()); }
public override string ToString() { return(XYTerm.PowerFactorString("x", XPower)); }