public void TestInteger() { var actual = Rational.Create(3); Assert.IsType <Integer>(actual); Assert.Equal(Integer.Create(3), actual); }
public void TestRationalDowncasting() { var frac21_10 = Rational.Create(21, 10); Assert.IsType <Rational>(frac21_10); Assert.Equal(frac21_10, frac21_10.EvalNumerical()); Assert.Equal(frac21_10, frac21_10.Simplify().EvalNumerical()); var squared = Entity.Number.Pow(frac21_10, 2); Assert.IsType <Rational>(squared); Assert.Equal(Rational.Create(441, 100), squared); Assert.Equal(squared, squared.EvalNumerical()); Assert.Equal(squared, squared.Simplify().EvalNumerical()); var cubed = Entity.Number.Pow(squared, Rational.Create(3, 2)); Assert.IsType <Rational>(cubed); Assert.Equal(Rational.Create(9261, 1000), cubed); Assert.Equal(cubed, cubed.EvalNumerical()); Assert.Equal(cubed, cubed.Simplify().EvalNumerical()); var ten = cubed + Rational.Create(739, 1000); Assert.IsType <Integer>(ten); Assert.Equal(Integer.Create(10), ten); Assert.Equal(ten, ten.EvalNumerical()); Assert.Equal(ten, ten.Simplify().EvalNumerical()); }
/// <summary>Solves ax^3 + bx^2 + cx + d</summary> /// <param name="a">Coefficient of x^3</param> /// <param name="b">Coefficient of x^2</param> /// <param name="c">Coefficient of x</param> /// <param name="d">Free coefficient</param> /// <returns>Set of roots</returns> internal static IEnumerable <Entity> SolveCubic(Entity a, Entity b, Entity c, Entity d) { // en: https://en.wikipedia.org/wiki/Cubic_equation // ru: https://ru.wikipedia.org/wiki/%D0%A4%D0%BE%D1%80%D0%BC%D1%83%D0%BB%D0%B0_%D0%9A%D0%B0%D1%80%D0%B4%D0%B0%D0%BD%D0%BE if (TreeAnalyzer.IsZero(d)) { return(SolveQuadratic(a, b, c).Append(0)); } if (TreeAnalyzer.IsZero(a)) { return(SolveQuadratic(b, c, d)); } var coeff = MathS.i * MathS.Sqrt(3) / 2; var u1 = Integer.Create(1); var u2 = Rational.Create(-1, 2) + coeff; var u3 = Rational.Create(-1, 2) - coeff; var D0 = MathS.Sqr(b) - 3 * a * c; var D1 = (2 * MathS.Pow(b, 3) - 9 * a * b * c + 27 * MathS.Sqr(a) * d).InnerSimplified; var C = MathS.Pow((D1 + MathS.Sqrt(MathS.Sqr(D1) - 4 * MathS.Pow(D0, 3))) / 2, Rational.Create(1, 3)); return(new[] { u1, u2, u3 }.Select(uk => C.Evaled == 0 && D0.Evaled == 0 ? -(b + uk * C) / 3 / a : -(b + uk * C + D0 / C / uk) / 3 / a)); }
public void TestWithUndefined8() { var x = Rational.Create(-1, 2); Assert.IsType <Rational>(x); Assert.Equal(Real.NaN, x / 0); }
public void TestWithUndefined9() { var x = Rational.Create(-1, 2); var result = x / Real.PositiveInfinity; Assert.IsType <Integer>(result); Assert.Equal(0, result); }
public void TestWithUndefined6() { var a = Real.Create(4); var b = Rational.Create(0, 6); Assert.IsType <Integer>(b); Assert.Equal(Real.NaN, a / b); }
/// <summary>Solves ax^4 + bx^3 + cx^2 + dx + e</summary> /// <param name="a">Coefficient of x^4</param> /// <param name="b">Coefficient of x^3</param> /// <param name="c">Coefficient of x^2</param> /// <param name="d">Coefficient of x</param> /// <param name="e">Free coefficient</param> /// <returns>Set of roots</returns> internal static IEnumerable <Entity> SolveQuartic(Entity a, Entity b, Entity c, Entity d, Entity e) { // en: https://en.wikipedia.org/wiki/Quartic_function // ru: https://ru.wikipedia.org/wiki/%D0%9C%D0%B5%D1%82%D0%BE%D0%B4_%D0%A4%D0%B5%D1%80%D1%80%D0%B0%D1%80%D0%B8 if (TreeAnalyzer.IsZero(e)) { return(SolveCubic(a, b, c, d).Append(0)); } if (TreeAnalyzer.IsZero(a)) { return(SolveCubic(b, c, d, e)); } var alpha = (-3 * MathS.Sqr(b) / (8 * MathS.Sqr(a)) + c / a) .InnerSimplified; var beta = (MathS.Pow(b, 3) / (8 * MathS.Pow(a, 3)) - (b * c) / (2 * MathS.Sqr(a)) + d / a) .InnerSimplified; var gamma = (-3 * MathS.Pow(b, 4) / (256 * MathS.Pow(a, 4)) + MathS.Sqr(b) * c / (16 * MathS.Pow(a, 3)) - (b * d) / (4 * MathS.Sqr(a)) + e / a) .InnerSimplified; if (beta.Evaled == 0) { return(sqrtsOf1.SelectMany(_ => sqrtsOf1, (s, t) => - b / 4 * a + s * MathS.Sqrt((-alpha + t * MathS.Sqrt(MathS.Sqr(alpha) - 4 * gamma)) / 2))); } var oneThird = Rational.Create(1, 3); var P = (-MathS.Sqr(alpha) / 12 - gamma) .InnerSimplified; var Q = (-MathS.Pow(alpha, 3) / 108 + alpha * gamma / 3 - MathS.Sqr(beta) / 8) .InnerSimplified; var R = -Q / 2 + MathS.Sqrt(MathS.Sqr(Q) / 4 + MathS.Pow(P, 3) / 27); var U = MathS.Pow(R, oneThird) .InnerSimplified; var y = (Rational.Create(-5, 6) * alpha + U + (U.Evaled == 0 ? -MathS.Pow(Q, oneThird) : -P / (3 * U))) .InnerSimplified; var W = MathS.Sqrt(alpha + 2 * y) .InnerSimplified; // Now we need to permutate all four combinations return(sqrtsOf1.SelectMany(_ => sqrtsOf1, (s, t) => - b / (4 * a) + (s * W + t * MathS.Sqrt(-(3 * alpha + 2 * y + s * 2 * beta / W))) / 2)); }
private static GraceObject mNumeric(EvaluationContext ctx, MethodRequest req, CodepointObject self) { // By convention, return -1 for non-numeric codepoints. if (self.parts[7] == "") { return(GraceNumber.Create(-1)); } int val; if (int.TryParse(self.parts[7], out val)) { return(GraceNumber.Create(val)); } // At this point, it must be a fraction n/m. var bits = self.parts[7].Split('/'); var rat = Rational.Create(int.Parse(bits[0]), int.Parse(bits[1])); return(GraceNumber.Create(rat)); }
public void TestRational3() => Assert.Equal(Rational.Create(15, 4), Rational.Create(7, 4) + Rational.Create(8, 4));
public void TestRational2() => Assert.Equal(Rational.Create(7, 4), Rational.Create(3, 4) + 1);
public void TestRational1() => Assert.Equal(Rational.Create(19, 12), Rational.Create(3, 4) + Rational.Create(5, 6));
private GraceNumber(double val) : base(createSharedMethods()) { Value = Rational.Create(val); }