public void TestJenkinsTraubRandomNumber() { for (var i = 0; i < 200; i++) { var count = Generator.RandomNumber(2, 10); var coeffs = new double[count]; coeffs = coeffs.Select(_ => (double)Generator.RandomNumber(-100, 100)).ToArray(); var poly = coeffs.Aggregate((KtPolynomial) new One(), (current, ktNumericBase) => current * KtPolynomial.Create(-ktNumericBase, 1)); var roots = poly.Roots(); var fixroots = roots.Select(elem => elem.Round(7).ToComplex().Real).ToArray(); Assert.AreEqual(coeffs.Length, fixroots.Length); if (!SequenceEqual(coeffs, fixroots)) { Debug.Print("Polynomial"); Debug.Print(poly.ToString()); Debug.Print("expected"); Generator.DebugArray(coeffs); Debug.Print("actual"); Generator.DebugArray(fixroots); Assert.Fail(); } Assert.IsTrue(SequenceEqual(coeffs, fixroots)); Assert.IsTrue(ContentEqual(coeffs, fixroots)); } }
public void DifferentTypes() { var poly3 = KtPolynomial.Create(2, 1); Term term1 = poly3; var poly1 = KtPolynomial.Create(3, 5, 2, 5); var poly2 = KtPolynomial.Create(3, 12, -2); Term term2 = new DiscontinuousTerm(poly1, poly2, Limit.Create(-12, 20)); var actualResult = term1 - term2; Assert.AreEqual(actualResult.Length, 3); Term expectedResult1 = new DiscontinuousTerm(poly3, KtPolynomial.Create(1), ForwardLimit.Create(-12)); Assert.AreEqual(actualResult[0], expectedResult1); Term expectedResult2 = new DiscontinuousTerm((poly3 * poly2) - poly1, poly2, Limit.Create(-12, 20)); Assert.AreEqual(actualResult[1], expectedResult2); Term expectedResult3 = new DiscontinuousTerm(poly3, KtPolynomial.Create(1), BackwardLimit.Create(20)); Assert.AreEqual(actualResult[2], expectedResult3); actualResult = term2 - term1; //reversed Assert.AreEqual(actualResult.Length, 3); expectedResult1 = new DiscontinuousTerm(-poly3, KtPolynomial.Create(1), ForwardLimit.Create(-12)); Assert.AreEqual(actualResult[0], expectedResult1); expectedResult2 = new DiscontinuousTerm(poly1 - (poly3 * poly2), poly2, Limit.Create(-12, 20)); Assert.AreEqual(actualResult[1], expectedResult2); expectedResult3 = new DiscontinuousTerm(-poly3, KtPolynomial.Create(1), BackwardLimit.Create(20)); Assert.AreEqual(actualResult[2], expectedResult3); }
private static Expression RightSideExpression(double a, double b) { var L = a + b; var kteq = KtPolynomial.Create(0, ((a * b * b) / (3 * L)) + ((a * a * b) / (6 * L)), 0, -b / (6 * L)); return(new Expression(kteq)); }
private static Expression LeftSideExpression(double a, double b) { var L = a + b; var kteq = KtPolynomial.Create(-a.RaiseTo(3) / 6, (a.RaiseTo(3) / (6 * L)) + ((L * a) / 3), -a / 2, a / (6 * L)); return(new Expression(kteq)); }
public void TestJenkinsTraub5() { var coeff = new Complex[] { 5, -20, 5, 50, -20, -40 }; var expectedRoots = new Number[] { -1, -1, 2, 2, 2.0 }; var roots = KtPolynomial.Create(coeff.Select(elem => (Number)elem).Reverse().ToArray()).Roots(); Assert.AreEqual(expectedRoots.Length, roots.Length); Assert.IsTrue(SequenceEqual(roots.ToArray(), expectedRoots)); Assert.IsTrue(ContentEqual(roots.ToArray(), expectedRoots)); }
public void TestRoot5746() { var coeff = new Number[] { 6, 4, -7, 5 }; var expectedRoots = new Number[] { (1, 1), (1, -1), -3.0 / 5 }; var roots = KtPolynomial.Create(coeff).Roots(); Assert.AreEqual(expectedRoots.Length, roots.Length); Assert.IsTrue(ContentEqual(roots.ToArray(), expectedRoots)); Assert.IsTrue(SequenceEqual(roots.ToArray(), expectedRoots)); }
public void BothDiscontinuousSpliceable() { var poly3 = KtPolynomial.Create(2, 1); var poly4 = KtPolynomial.Create(3, 1); Term term1 = new DiscontinuousTerm(poly3, poly4, ForwardLimit.Create(9)); Term term2 = new DiscontinuousTerm(poly3, poly4, BackwardLimit.Create(9)); var actualResult = term1 + term2; Assert.AreEqual(actualResult.Length, 1); Assert.AreEqual(actualResult[0], new ContinuousTerm(poly3, poly4)); }
public void MultTest() { var poly3 = KtPolynomial.Create(2, 1); Term term1 = poly3; var poly1 = KtPolynomial.Create(3, 5, 2, 5); var poly2 = KtPolynomial.Create(3, 12, -2); Term term2 = new DiscontinuousTerm(poly1, poly2, Limit.Create(-12, 20)); var actualResult = term1 * term2; Term expectedResult2 = new DiscontinuousTerm(poly3 * poly1, poly2, Limit.Create(-12, 20)); Assert.AreEqual(actualResult, expectedResult2); }
public void TestJenkinsTraub2() { var coeff = new Complex[] { 1, -3, -2, 6 }; Number[] expectedRoots = { Round(Sqrt(2), 9), -Round(Sqrt(2), 9), 3 }; var roots = KtPolynomial.Create(coeff.Select(elem => (Number)elem).Reverse().ToArray()).Roots(); Assert.AreEqual(expectedRoots.Length, roots.Length); Assert.IsTrue(roots.Contains(expectedRoots[0])); Assert.IsTrue(roots.All(expectedRoots.Contains)); Assert.IsTrue(expectedRoots.All(roots.Contains)); Assert.IsTrue(ContentEqual(roots.ToArray(), expectedRoots)); }
public void BothContinuous() { var poly1A = KtPolynomial.Create(4, 3, -24, 1); var poly1B = KtPolynomial.Create(1.134, 3, 33, -12); var poly2A = KtPolynomial.Create(-3, 45, 2, 4, 3); var poly2B = KtPolynomial.Create(8, 0, 4, -1); var cont1 = new ContinuousTerm(poly1A, poly1B); var cont2 = new ContinuousTerm(poly2A, poly2B); var actualResult = cont1 - cont2; Assert.AreEqual(actualResult.Length, 1); Assert.AreEqual(actualResult[0], new ContinuousTerm((poly1A * poly2B) - (poly1B * poly2A), poly2B * poly1B)); }
public void BothDiscontinuousNotTouching() { var poly3 = KtPolynomial.Create(2, 1); var poly4 = KtPolynomial.Create(3, 1); Term term1 = new DiscontinuousTerm(poly3, poly4, Limit.Create(-12, 9)); var poly1 = KtPolynomial.Create(3, 5, 2, 5); var poly2 = KtPolynomial.Create(3, 12, -2); Term term2 = new DiscontinuousTerm(poly1, poly2, Limit.Create(10, 20)); var actualResult = term1 + term2; Assert.AreEqual(actualResult.Length, 2); Assert.AreEqual(actualResult[0], term1); Assert.AreEqual(actualResult[1], term2); }
public void GaussianEliminationTest() { var mat1 = new Matrix(new[, ] { { 1936.0 / 45, 692.0 / 15 }, { 692.0 / 15, 324.0 / 5 } }); var polyu = KtPolynomial.Create(1); var poly1 = KtPolynomial.Create(0, 572.0 / 45, 0, -11.0 / 90); var poly2 = KtPolynomial.Create(-32.0 / 3, 932.0 / 45, -2, 2.0 / 45); var expression1 = new Expression( ((poly1, polyu), Limit.Create(0, 4)), ((poly2, polyu), Limit.Create(4, 9)), ((poly2, polyu), Limit.Create(9, 15))); var poly3 = KtPolynomial.Create(0, 63.0 / 5, 0, -1 / 15.0); var poly4 = KtPolynomial.Create(-243 / 2.0, 531.0 / 10, -9.0 / 2, 1.0 / 10); var expression2 = new Expression( ((poly3, polyu), Limit.Create(0, 4)), ((poly3, polyu), Limit.Create(4, 9)), ((poly4, polyu), Limit.Create(9, 15))); var mat2 = new Matrix(new[, ] { { expression1 }, { expression2 } }); var actual = MatrixMethods.GaussianElimination(mat1, mat2); //GetEllapsedTime(mat1, mat2); var expPoly1a = KtPolynomial.Create(0, 2727.0 / 7420, 0, -109.0 / 14840); var expPoly1b = KtPolynomial.Create(-1944.0 / 1855, 8559.0 / 7420, -729.0 / 3710, 67.0 / 7420); var expPoly1c = KtPolynomial.Create(3159.0 / 424, -24921.0 / 14840, 351.0 / 2968, -39.0 / 14840); var expPoly2a = KtPolynomial.Create(0, -374.0 / 5565, 0, 187.0 / 44520); var expPoly2b = KtPolynomial.Create(1384.0 / 1855, -3488.0 / 5565, 519.0 / 3710, -83.0 / 11130); var expPoly2c = KtPolynomial.Create(-761.0 / 106, 22427.0 / 11130, -57.0 / 371, 19.0 / 5565); var expected1 = new Expression( ((expPoly1a, polyu), Limit.Create(0, 4)), ((expPoly1b, polyu), Limit.Create(4, 9)), ((expPoly1c, polyu), Limit.Create(9, 15))); var expected2 = new Expression( ((expPoly2a, polyu), Limit.Create(0, 4)), ((expPoly2b, polyu), Limit.Create(4, 9)), ((expPoly2c, polyu), Limit.Create(9, 15))); var expected = new Matrix(new[, ] { { expected1 }, { expected2 } }); Assert.AreEqual(expected1, actual[0, 0]); Assert.AreEqual(expected2, actual[1, 0]); Assert.AreEqual(expected, actual); }
public void MultiplicationOfExpressionAndNumber() { var mat1 = new Matrix(new[, ] { { 1.0 / 2, -2, -30, 20 }, { 15, 2.1, 4, 3 }, { 3, 2, 1, 3 }, { 4, -4, 3, 2 } }); var expression1 = new Expression(((KtPolynomial.Create(3, 2), KtPolynomial.Create(1, 2)), Limit.Create(3, 12))); var expression2 = new Expression(((KtPolynomial.Create(2), KtPolynomial.Create(1, 2)), Limit.Create(3, 12))); var mat2 = new Matrix(new[, ] { { expression1 }, { expression2 }, { expression1 }, { expression2 } }); _ = mat1 * mat2; }
public void Subtracting() { var poly1 = KtPolynomial.Create(4, 0, -2, 0, 4, 1); var poly2 = KtPolynomial.Create(3, -4.3, 1); var poly3 = KtPolynomial.Create(4, 3, 7); var denom = KtPolynomial.Create(1); var term1 = new DiscontinuousTerm(poly1, denom, Limit.Create(-20, -10)); var term2 = new DiscontinuousTerm(poly2, denom, Limit.Create(-15, -5)); Term term3 = poly3; var expression = new Expression(term1, term3, term2); var actual = expression - expression; _ = new Zero(); var expected = new Expression((Number)0); Assert.AreEqual(actual, expected); }
public void BothDiscontinuousBarelyInclusive() { var poly3 = KtPolynomial.Create(2, 1); var poly4 = KtPolynomial.Create(3, 1); Term term1 = new DiscontinuousTerm(poly3, poly4, Limit.Create(-12, 10)); var poly1 = KtPolynomial.Create(3, 5, 2, 5); var poly2 = KtPolynomial.Create(3, 12, -2); Term term2 = new DiscontinuousTerm(poly1, poly2, Limit.Create(-12, 20)); var actualResult = term1 - term2; Assert.AreEqual(actualResult.Length, 2); Term expectedResult2 = new DiscontinuousTerm((poly3 * poly2) - (poly1 * poly4), poly4 * poly2, Limit.Create(-12, 10)); Assert.AreEqual(actualResult[0], expectedResult2); Term expectedResult3 = new DiscontinuousTerm(-poly1, poly2, Limit.Create(10, 20)); Assert.AreEqual(actualResult[1], expectedResult3); }
public void Adding() { var poly1 = KtPolynomial.Create(4, 0, -2, 0, 4, 1); var poly2 = KtPolynomial.Create(3, -4.3, 1); var poly3 = KtPolynomial.Create(4, 3, 7); var denom = KtPolynomial.Create(1); var term1 = new DiscontinuousTerm(poly1, denom, Limit.Create(-20, -10)); var term2 = new DiscontinuousTerm(poly2, denom, Limit.Create(-15, -5)); Term term3 = poly3; var expression = new Expression(term1, term3, term2); var expected = new Expression( new DiscontinuousTerm(2 * poly3, denom, ForwardLimit.Create(-20)), new DiscontinuousTerm(2 * (poly3 + poly1), denom, Limit.Create(-20, -15)), new DiscontinuousTerm(2 * (poly3 + poly1 + poly2), denom, Limit.Create(-15, -10)), new DiscontinuousTerm(2 * (poly3 + poly2), denom, Limit.Create(-10, -5)), new DiscontinuousTerm(2 * poly3, denom, BackwardLimit.Create(-5))); Assert.AreEqual(expression + expression, expected); }
public void BothDiscontinuousOverlap() { var poly3 = KtPolynomial.Create(2, 1); var poly4 = KtPolynomial.Create(3, 1); Term term1 = new DiscontinuousTerm(poly3, poly4, Limit.Create(-12, 15)); var poly1 = KtPolynomial.Create(3, 5, 2, 5); var poly2 = KtPolynomial.Create(3, 12, -2); Term term2 = new DiscontinuousTerm(poly1, poly2, Limit.Create(10, 20)); var actualResult = term1 + term2; Assert.AreEqual(actualResult.Length, 3); Term term3 = new DiscontinuousTerm(poly3, poly4, Limit.Create(-12, 10)); Term term4 = new DiscontinuousTerm((poly3 * poly2) + (poly1 * poly4), poly4 * poly2, Limit.Create(10, 15)); Term term5 = new DiscontinuousTerm(poly1, poly2, Limit.Create(15, 20)); Assert.AreEqual(actualResult[0], term3); Assert.AreEqual(actualResult[1], term4); Assert.AreEqual(actualResult[2], term5); }
public void ContinuousAndNot() { var poly3 = KtPolynomial.Create(2, 1); Term term1 = poly3; var poly1 = KtPolynomial.Create(3, 5, 2, 5); var poly2 = KtPolynomial.Create(3, 12, -2); Term term2 = new DiscontinuousTerm(poly1, poly2, Limit.Create(-12, 20)); var actualResult = term1 + term2; Assert.AreEqual(actualResult.Length, 3); Term expectedResult1 = new DiscontinuousTerm(poly3, KtPolynomial.Create(1), ForwardLimit.Create(-12)); Assert.AreEqual(actualResult[0], expectedResult1); Term expectedResult2 = new DiscontinuousTerm((poly3 * poly2) + poly1, poly2, Limit.Create(-12, 20)); Assert.AreEqual(actualResult[1], expectedResult2); Term expectedResult3 = new DiscontinuousTerm(poly3, KtPolynomial.Create(1), BackwardLimit.Create(20)); Assert.AreEqual(actualResult[2], expectedResult3); Assert.IsTrue(actualResult.SequenceEqual(term2 + term1)); }