public void FractionRaisedToNegative()
    {
        var expected = ExprParser.ParseOrThrow("(a/b)^-1");
        var actual   = DivideOp.Create(new Identifier("b"), new Identifier("a"));

        Assert.AreEqual(expected.Simplify(), actual);
    }
    public void QuantityTwoMinus_a_raisedTo1DividedBy3Test()
    {
        var actual   = DivideOp.Create(PowerOp.Create(SubtractOp.Create(new Two(), new Identifier("a")), new One()), Literal.Create(3)).Simplify();
        var expected = ExprParser.ParseOrThrow("(2-a)^1/3").Simplify();

        Assert.AreEqual(expected, actual);
    }
    public void Test4()
    {
        var d       = ExprParser.ParseOrThrow("2*4/3-4");
        var dsimped = d.Simplify();

        Assert.AreEqual(dsimped, NegativeOp.Create(DivideOp.Create(4, 3)));
    }
    public void Test5()
    {
        const string input    = "a*b/c";
        var          actual   = DivideOp.Create(MultiplyOp.Create(new Identifier("a"), new Identifier("b")), new Identifier("c")).Simplify();
        var          expected = ExprParser.ParseOrThrow(input);

        Assert.AreEqual(expected, actual);
    }
    public void BinomialRaisedToFraction()
    {
        var d        = ExprParser.ParseOrThrow("x+2").Simplify();
        var expected = d.Raise(DivideOp.Create(2, 3));
        var actual   = ExprParser.ParseOrThrow("(x^2+4*x+4)^(1/3)").Simplify();

        Assert.AreEqual(expected, actual);
    }
    public void DecimalTest()
    {
        var onePthree = ExprParser.ParseOrThrow("1.3");

        Assert.AreEqual(onePthree, DivideOp.Create(13, 10));
        var actual = AddOp.Create(PowerOp.Create(A, onePthree), Two);

        Assert.AreEqual(ExprParser.ParseOrThrow("a^1.3+2"), actual);
        Assert.AreEqual(A.Raise(onePthree) + Two, actual.Simplify());
    }
Esempio n. 7
0
        public void MyTestMethod()
        {
            var a = new Identifier("a");
            var b = new Identifier("b");

            Assert.AreEqual((a * b).Invert(), DivideOp.Create(1, a * b));
            Assert.AreEqual(a * b.Invert(), DivideOp.Create(a, b));
            Assert.AreEqual(a.Invert() * b, DivideOp.Create(b, a));
            Assert.AreEqual((a / b).Invert(), DivideOp.Create(b, a));
        }
Esempio n. 8
0
        public void InvertPowerOfNegativeOneTest()
        {
            var a           = new Identifier("a");
            var negativeOne = new NegativeOne();
            var aPnOne      = a.Raise(negativeOne);
            var inv         = aPnOne.Invert();

            Assert.AreEqual(inv, DivideOp.Create(1, aPnOne));
            Assert.AreEqual(inv, a);
        }
    public void NegateDivideTest()
    {
        const string input  = "- (1/3)";
        var          actual = NegativeOp.Create(
            DivideOp.Create(new One(), Literal.Create(3))
            ).Simplify();
        var expected = ExprParser.ParseOrThrow(input);

        Assert.AreEqual(expected, actual);
    }
    public void FractionRaiseToFraction()
    {
        var twentyFive = Literal.Create(25);
        var thirtyTwo  = Literal.Create(32);
        var oneTenth   = DivideOp.Create(1, 10);
        var result     = (thirtyTwo / twentyFive).Raise(oneTenth);
        var actual     = SqrtOp.Create(2) / PowerOp.Create(5, DivideOp.Create(1, 5));

        Assert.AreEqual(result, actual);
    }
    public void Test3()
    {
        const string input    = "-a/b^1+3-a/b^1";
        var          term1    = DivideOp.Create(NegativeOp.Create(new Identifier("a")), PowerOp.Create(new Identifier("b"), new One()));
        var          term2    = Literal.Create(3);
        var          term3    = DivideOp.Create(new Identifier("a"), PowerOp.Create(new Identifier("b"), new One()));
        var          actual   = SubtractOp.Create(AddOp.Create(term1, term2), term3).Simplify();
        var          expected = ExprParser.ParseOrThrow(input);

        Assert.AreEqual(expected.Simplify(), actual);
    }
Esempio n. 12
0
 public static Expr Create(Expr left, Expr right) => (left, right) switch
 {
     (null, _) => new Zero(),
     (_, null) => new Zero(),
     (_, Zero z) => z,
     (Zero z, _) => z,
     (One _, _) => right,
     (_, One _) => left,
     (Literal l, Literal r) => Literal.Create(l.Value * r.Value),
     (FractOp d1, FractOp d2) => DivideOp.Create(d1.Left * d2.Left, d1.Right * d2.Right),
     _ => new MultiplyOp(left, right)
 };
    public void Test4()
    {
        const string input    = "1-2/3+1-3+3*2";
        var          a        = new One();
        var          b        = DivideOp.Create(new Two(), Literal.Create(3));
        var          c        = new One();
        var          d        = Literal.Create(3);
        var          e        = MultiplyOp.Create(Literal.Create(3), new Two());
        var          actual   = AddOp.Create(SubtractOp.Create(AddOp.Create(SubtractOp.Create(a, b), c), d), e).Simplify();
        var          expected = ExprParser.ParseOrThrow(input);

        Assert.AreEqual(expected.Simplify(), actual);
    }
Esempio n. 14
0
 public static Expr Create(Expr left, Expr right) => (left, right) switch
 {
     (null, null) => throw new InvalidOperationException($"0^0"),
           (_, null) => new One(),
           (null, _) => new Zero(),
           (Zero _, Zero _) => throw new InvalidOperationException($"0^0"),
                 (_, Zero _) => new One(),
                 (Zero z, _) => z,
                 (_, One _) => left,
                 (One o, _) => o,
                 (DivideOp d, NegativeOp n) => new PowerOp(DivideOp.Create(d.Right, d.Left), n.Expr),
                 (_, NegativeOp n) => DivideOp.Create(1, Create(left, n.Expr)),
                 _ => new PowerOp(left, right)
 };
Esempio n. 15
0
        public Expr ToExpr()
        {
            var(VN, VSN, VRN) = _variables.OfType <VarPowN, VarN, VarRootN>();
            var numerators = VN.Concat <Variable>(VSN).Concat(VRN).Select(f => f.ToExpr());

            var(VD, VSD, VRD) = _variables.OfType <VarPowD, VarD, VarRootD>();
            var denominators = VD.Concat <Variable>(VSD).Concat(VRD).Select(f => f.ToExpr());
            var numerator    = numerators.IsEmpty() ? new One() : numerators.Aggregate((n1, n2) => MultiplyOp.Create(n1, n2));

            if (denominators.IsEmpty())
            {
                return(numerator);
            }
            return(DivideOp.Create(numerator, denominators.Aggregate((n1, n2) => MultiplyOp.Create(n1, n2))));
        }
Esempio n. 16
0
        public void InvertDivisionOpTest()
        {
            var a       = new Identifier("a");
            var b       = new Identifier("b");
            var actual1 = new One() / a;

            Assert.AreEqual(actual1.Invert(), a);
            var actual2 = DivideOp.Create(-new One(), a);

            Assert.AreEqual(actual2.Invert(), -a);
            var actual3 = DivideOp.Create(a, b);

            Assert.AreEqual(actual3.Invert(), DivideOp.Create(b, a));
            var actual4 = DivideOp.Create(NegativeOp.Create(a), b);

            Assert.AreEqual(actual4.Invert(), DivideOp.Create(-b, a));
        }
    public void Beta1Formula()
    {
        var actual   = ExprParser.ParseOrThrow("0.85-(0.05/7)*(fᐠ_c-28)");
        var expected = Dec.Create(0.85m) - (Dec.Create(0.05m) / Literal.Create(7)) * (new Identifier("fᐠ_c") - Literal.Create(28));

        Assert.AreEqual(expected, actual);
        Assert.AreEqual("0.85-((0.05/7)×(fᐠ_c-28))", actual.ToString());
        var substiResult = actual.Substitute(new Identifier("fᐠ_c"), 32).Simplify();

        Assert.AreEqual(substiResult, DivideOp.Create(23, 28));
        if (substiResult.TryToDouble(out var val))
        {
            Assert.AreEqual(0.8214, val, 0.0001);
        }
        else
        {
            Assert.Fail($"{substiResult} is not a double");
        }
    }
 public void DivideToAny()
 {
     Assert.AreEqual(i1 / i3, DivideOp.Create(i1, i3));
 }
    public void SixTo15()
    {
        var sixTo15 = DivideOp.Create(6, 15).Simplify();

        Assert.AreEqual(sixTo15, DivideOp.Create(2, 5));
    }
 private static Expr ToFraction(this string b) => DivideOp.Create(b.ToBigInt(), BigInteger.Pow(10, b.Length));
    public void Test2()
    {
        var d = ExprParser.ParseOrThrow("2*4/3").Simplify();

        Assert.AreEqual(d, DivideOp.Create(8, 3));
    }
    public void SqrtOf4()
    {
        var sqrt4 = Literal.Create(4).Raise(DivideOp.Create(1, 2)).Simplify();

        Assert.AreEqual(sqrt4, new Two());
    }
Esempio n. 23
0
 public override Expr Sqrt() => Raise(DivideOp.Create(1, 2));
    public void NegativeFractionRootOfFive()
    {
        var d = ExprParser.ParseOrThrow("-(1/2)^5").Simplify();

        Assert.AreEqual(d, NegativeOp.Create(DivideOp.Create(1, 32)));
    }
Esempio n. 25
0
        public void InvertPowerOfOneTest()
        {
            var a = new Identifier("a^1");

            Assert.AreEqual(a.Invert(), DivideOp.Create(1, a));
        }
Esempio n. 26
0
 public static Expr Root(this Expr @base, Expr root) => PowerOp.Create(@base, DivideOp.Create(1, root));
 public void DivideByAnyNumber()
 {
     Assert.AreEqual(X / Two, DivideOp.Create(X, 2));
     Assert.AreEqual(Two / X, DivideOp.Create(2, X));
 }
    public void QuantityTwoMinus_a_raisedToOneThirdTest()
    {
        const string input    = "(2-a)^(1/3)";
        var          actual   = PowerOp.Create(SubtractOp.Create(new Two(), new Identifier("a")), DivideOp.Create(new One(), Literal.Create(3))).Simplify();
        var          expected = ExprParser.ParseOrThrow(input).Simplify();

        Assert.AreEqual(expected, actual);
    }
    public void Test1()
    {
        var d = ExprParser.ParseOrThrow("1+2*4/3-4").Simplify();

        Assert.AreEqual(d, -DivideOp.Create(1, 3));
    }