public void WithoutRootsTest() { var solver = new PolynomialSolver(); var roots = solver.Solve(new List <double> { 1, 0, 4 }); CollectionAssert.IsEmpty(roots); }
public void OneRootTest() { var expectedRoot = 1; var solver = new PolynomialSolver(); var roots = solver.Solve(new List <double> { 1, -1 }); Assert.AreEqual(roots.Count, 1); Assert.AreEqual(roots.First(), expectedRoot); }
/// <summary> /// Returns information about all monomials of an expression /// </summary> /// <param name="expr"></param> /// <param name="replaceVars"></param> /// <returns></returns> internal static PolyInfo GatherAllPossiblePolynomials(Entity expr, bool replaceVars) { // TODO: refactor expr = expr.DeepCopy(); // Init var res = new PolyInfo(); var mentionedVarList = MathS.Utils.GetUniqueVariables(expr); var newList = new List <string>(); if (replaceVars) { // Replace all variables we can foreach (var varMentioned in mentionedVarList.FiniteSet()) { if (expr.FindSubtree(varMentioned) == null) { continue; } var replacement = TreeAnalyzer.GetMinimumSubtree(expr, varMentioned); res.replacementInfo[varMentioned.Name] = replacement; FindAndReplace(ref expr, replacement, new VariableEntity(PolyInfo.NewVarName(varMentioned.Name))); newList.Add(PolyInfo.NewVarName(varMentioned.Name)); } } else { foreach (var v in mentionedVarList.FiniteSet()) { newList.Add(v.Name); } } // Gather info about each var as if this var was the only argument of the polynomial P(x) foreach (var varMentioned in newList) { List <Entity> children; if (expr.entType == Entity.EntType.OPERATOR && expr.Name == "sumf" || expr.Name == "minusf") { children = TreeAnalyzer.LinearChildren(expr, "sumf", "minusf", Const.FuncIfSum); } else { children = new List <Entity> { expr } }; res.monoInfo[varMentioned] = PolynomialSolver.GatherMonomialInformation <decimal>(children, MathS.Var(varMentioned)); } return(res); } }
static void Main(string[] args) { PolynomialSolver polynomialSolver = new PolynomialSolver(); polynomialSolver.Initialization(args); Polynomial polynomial = polynomialSolver.CreatePolynomial(); Polynomial polynomialDerivation = polynomial.Derive(); polynomialSolver.EvaluatePolynomial(polynomial, polynomialDerivation); }
public void WithoutOneCoeffTest() { var expectedRoots = new List <double> { 2, -2 }; var solver = new PolynomialSolver(); var roots = solver.Solve(new List <double> { 1, 0, -4 }); CollectionAssert.AreEquivalent(roots, expectedRoots); }
public void SquareEquationTest() { var expectedRoots = new List <double> { 2, 6 }; var solver = new PolynomialSolver(); var roots = solver.Solve(new List <double> { 1, -8, 12 }); CollectionAssert.AreEquivalent(roots, expectedRoots); }
static (Entity poly, Entity remainder) ExtractPolynomialLocally(Entity expr, Variable x) { var(powInfo, rem) = PolynomialSolver.GatherMonomialInformationAllowingBad <EInteger, PrimitiveInteger>(Sumf.LinearChildren(expr), x); if ( powInfo.TryGetValue(EInteger.Zero, out var info) // if we have something line x^5 + a + a over x then we should get (x^5, a + a) so that we could continute working with the remainder ) { rem += info; powInfo.Remove(EInteger.Zero); } return(Simplificator.BuildPoly(powInfo, x) ?? 0, rem); }
protected void SetupSolver() { TestedSolver = new PolynomialSolver(ConsoleMock.Object); }
public void CreateContent() { leftMostPoint = new Vector2(); tileSize = new Vector2(); // print("New building created"); //Set tile sizes. tileSize.x = roofMiddle.GetComponent <Renderer>().bounds.size.x; tileSize.y = roofMiddle.GetComponent <Renderer>().bounds.size.y; Vector2 nextPosition = new Vector2(); nextPosition.x = nextPosition.y = 0;// Make sure vector is set to 0; if (previousBuilding != null) { Building bScript = previousBuilding.GetComponent <Building>(); prevRightMostPoint = bScript.rightMostPoint; nextPosition = bScript.rightMostPoint; //Check jump stats. float jumpVelocity = Player.GetComponent <PlayerJump>().jumpVelocity; float runSpeed = Player.GetComponent <PlayerMovement>().runSpeed; float gScale = (Player.GetComponent <Rigidbody2D>().gravityScale) * (-9.81f); float t = -jumpVelocity / gScale; //v = 0 float s = jumpVelocity * t + 0.5f * gScale * t * t; float maxHeightOfPlatform = (s - tileSize.y / 2f) * 0.8f; float minDepthOfPlatform = s * 1.5f; float y = Random.Range(-minDepthOfPlatform, maxHeightOfPlatform); //Set y height. // nextPosition.y = bScript.rightMostPoint.y; nextPosition.y += y; //Calculate t, then x distance here. // 0.5 * gScale * t*t + u*t - s = 0; float a = 0.5f * gScale; float b = jumpVelocity; float c = -y; float t_y = PolynomialSolver.solve2(a, b, c); // s = ut+ 0.5 a t**2; float d_x = runSpeed * t_y; nextPosition.x += d_x - tileSize.x; // nextPosition.x -= tileSize.x; //////////////////// leftMostPoint = nextPosition; //Equations of motion. //s = -(jumpVelocity * jumpVelocity)/ (2f * gScale); //v - u = at //s = ut+ 0.5 a t**2; //v**2 -u**2 = 2*a*s; //s = 0.5(u + v) t //Calculate y component. } //Randomly decide tileCount. tileCount = Random.Range(minimumTilesCount, maximumTilesCount + 1); //Create tiles. nextPosition.x += tileSize.x / 2f;//compatible even during start without previousBuilding. nextPosition.y -= tileSize.y / 2f; float gap = 0.0f; GameObject newTile; //Create left tile. { newTile = objectPooler.SpawnFromPool(roofCornerLeftStr, nextPosition, Quaternion.identity); newTile.layer = Mathf.RoundToInt(Mathf.Log(groundLayer.value, 2)); nextPosition.x += (roofCornerLeft.GetComponent <Renderer>().bounds.size.x + gap); // newTile.transform.parent = gameObject.transform; AllTiles.Add(newTile); RoofTiles.Add(newTile); } //Create middle tiles. for (int i = 0; i < tileCount - 2; i++) { newTile = objectPooler.SpawnFromPool(roofMiddleStr, nextPosition, Quaternion.identity); newTile.layer = Mathf.RoundToInt(Mathf.Log(groundLayer.value, 2)); //Prepare next position. nextPosition.x += (tileSize.x + gap); //Set parent to Generator. // newTile.transform.parent = gameObject.transform; //Add to list. AllTiles.Add(newTile); RoofTiles.Add(newTile); } //Create right tile. { newTile = objectPooler.SpawnFromPool(roofCornerLeftStr, nextPosition, Quaternion.identity); newTile.layer = Mathf.RoundToInt(Mathf.Log(groundLayer.value, 2)); // newTile.transform.parent = gameObject.transform; nextPosition.x += (roofCornerLeft.GetComponent <Renderer>().bounds.size.x + gap); //Flip tile now. newTile.GetComponent <SpriteRenderer>().flipX = true; //Add to list. AllTiles.Add(newTile); RoofTiles.Add(newTile); } rightMostPoint.x = tileSize.x / 2f + newTile.transform.position.x; rightMostPoint.y = tileSize.y / 2f + newTile.transform.position.y; // gameObject.transform.parent = Parent.transform; // print(gameObject.GetComponent<Renderer>().bounds.size.x); //Fill Tiles Below roof. FillBelowTiles(20); mainGenerator.probeReach(rightMostPoint); }
/// <summary> /// Sorts an expression into a polynomial. /// See more MathS.Utils.TryPolynomial /// </summary> /// <param name="expr"></param> /// <param name="variable"></param> /// <param name="dst"></param> /// <returns></returns> internal static bool TryPolynomial(Entity expr, VariableEntity variable, out Entity dst) { dst = null; var children = TreeAnalyzer.LinearChildren(expr.Expand(), "sumf", "minusf", Const.FuncIfSum); var monomialsByPower = PolynomialSolver.GatherMonomialInformation <long>(children, variable); if (monomialsByPower == null) { return(false); } var newMonomialsByPower = new Dictionary <int, Entity>(); var keys = monomialsByPower.Keys.ToList(); keys.Sort((i, i1) => (i < i1 ? 1 : (i > i1 ? -1 : 0))); var terms = new List <Entity>(); foreach (var index in keys) { var pair = new KeyValuePair <long, Entity>(index, monomialsByPower[index]); Entity px; if (pair.Key == 0) { terms.Add(pair.Value.Simplify()); continue; } if (pair.Key == 1) { px = variable; } else { px = MathS.Pow(variable, pair.Key); } if (pair.Value == 1) { terms.Add(px); continue; } else { terms.Add(pair.Value.Simplify() * px); } } if (terms.Count == 0) { return(false); } dst = terms[0]; for (int i = 1; i < terms.Count; i++) { if (terms[i].Name == "mulf" && terms[i].Children[0].entType == Entity.EntType.NUMBER && terms[i].Children[0].GetValue().IsReal() && terms[i].Children[0].GetValue().Real < 0) { dst -= ((-1) * terms[i].Children[0].GetValue()) * terms[i].Children[1]; } else { dst += terms[i]; } } dst = dst.InnerSimplify(); return(true); }
/// <summary> /// Изменить метод поиска корней полинома /// </summary> /// <param name="polynomialSolver">Метод поиска корней полинома на промежутке</param> public void SetPolynomialSolver(PolynomialSolver polynomialSolver) { this.polynomialSolver = polynomialSolver; }