Example #1
0
        public void TestForComparation()
        {
            Variable x = new Variable("x");

            var c1 = x + x;
            var c2 = x + x;
            Assert.AreEqual(c1.GetHashCode(), c2.GetHashCode());
        }
Example #2
0
        public void TestForSimplification()
        {
            Variable x = new Variable("x");

            Expression s1 = SMath.Exp(SMath.Ln(x));
            Assert.AreEqual(s1.Simplify(), x);

            Expression s2 = SMath.Ln(SMath.Exp(x));
            Assert.AreEqual(s2.Simplify(), x);

            Expression s3 = SMath.Sqrt((SMath.Exp(SMath.Ln(x)) - x) ^ 10);
            Assert.AreEqual(s3.Simplify(), Constant.Zero);

            Expression s4 = (x ^ 6) / (x ^ 4);
            Assert.AreEqual(s4.Simplify(), x ^ 2);

            Expression s5 = (x ^ 6) * (x ^ 9);
            Assert.AreEqual(s5.Simplify(), x ^ 15);

            Expression s6 = (Constant.One ^ 6) ^ 2;
            Assert.AreEqual(s6.Simplify(), Constant.One);
        }
Example #3
0
        public void TestForDerivation()
        {
            Variable x = new Variable("x");
            Variable y = new Variable("y");
            Expression e = SMath.Exp(x + y);

            Expression dx = e.Derivate(x).Simplify();
            Assert.AreEqual(dx, e);

            Expression dy = e.Derivate(y).Simplify();
            Assert.AreEqual(dy, e);

            Expression dxy = e.Derivate(x).Derivate(y).Simplify();
            Assert.AreEqual(dxy, e);

            Expression xdy = x.Derivate(y).Simplify();
            Assert.AreEqual(xdy, Constant.Zero);

            Expression ydx = y.Derivate(x).Simplify();
            Assert.AreEqual(ydx, Constant.Zero);

            Expression d1 = (x * y).Derivate(x).Simplify();
            Assert.AreEqual(d1, y);

            Expression d2 = (x * y).Derivate(y).Simplify();
            Assert.AreEqual(d2, x);

            Expression d3 = (x + y).Derivate(x).Simplify();
            Assert.AreEqual(d3, Constant.One);

            Expression d4 = (x + y).Derivate(y).Simplify();
            Assert.AreEqual(d4, Constant.One);

            Expression d5 = (1 / x).Derivate(x).Simplify();
            Assert.AreEqual(d5, -(1 / x ^ 2));
        }
Example #4
0
        static Expression LaplaceTransform(Expression expr, Variable t, Variable s)
        {

            return 0;
        }
Example #5
0
 static double NSolve(Expression f, Variable v, double v0 = 0, double eps = 1e-6)
 {
     double vn1 = v0;
     Expression phi = v - f / f.Derivate(v).Simplify();
     v.Value = vn1;
     do
     {
         vn1 = phi.Process();
         v.Value = vn1;
     } while (Math.Abs(f.Process()) > eps);
     return vn1;
 }
Example #6
0
 static double NIntegrate(Expression f, Variable v, double a, double b, double eps = 1e-2)
 {
     double res = 0;
     Expression df = f.Derivate(v).Simplify();
     v.Value = a;
     for (double pv = a, cv = a + eps, step = eps;
         cv < b + step / 2;
         v.Value = cv, step = eps / (1 + Math.Abs(df.Process())), pv = cv, cv += step)
     {
         v.Value = (pv + cv) / 2;
         res += step * f.Process();
     }
     return res;
 }
Example #7
0
 public override Expression Derivate(Variable dv)
 {
     return Constant.Zero;
 }
Example #8
0
        static Expression Solve(Expression left, Expression right, Variable v)
        {
            left = left.Simplify();
            right = right.Simplify();
            Console.WriteLine("\t{0} = {1} // {2} == {3}", left, right, left.GetType().Name, right.GetType().Name);

            if (!left.IsDependOf(v)) throw new Exception("Can't solve equation. No dependencies of variable '" + v.Name + "'.");
            if (left is Symbolic.Operators.Scalar.Add)
            {
                Expression ll = Left(left);
                Expression lr = Right(left);
                if (lr.IsDependOf(v)) return Solve(lr, right - ll, v);
                else return Solve(ll, right - lr, v);
            }
            else if (left is Symbolic.Operators.Scalar.Sub)
            {
                Expression ll = Left(left);
                Expression lr = Right(left);
                if (lr.IsDependOf(v)) return Solve(-lr, right - ll, v);
                else return Solve(ll, right + lr, v);
            }
            else if (left is Symbolic.Operators.Scalar.Div)
            {
                Expression ll = Left(left);
                Expression lr = Right(left);
                if (lr.IsDependOf(v)) return Solve(lr, ll / right, v);
                else return Solve(ll, right * lr, v);
            }
            else if (left is Symbolic.Operators.Scalar.Mul)
            {
                Expression ll = Left(left);
                Expression lr = Right(left);
                if (lr.IsDependOf(v)) return Solve(lr, right / ll, v);
                else return Solve(ll, right / lr, v);
            }
            else if (left is Symbolic.Operators.Scalar.Pow)
            {
                Expression ll = Left(left);
                Expression lr = Right(left);
                if (lr.IsDependOf(v)) return Solve(lr, SMath.Log(right, ll), v);
                else return Solve(ll, right ^ (1 / lr), v);
            }
            else if (left is Symbolic.Functions.Sqrt)
            {
                Expression ll = (left as Symbolic.Functions.Sqrt).Args[0];
                return Solve(ll, right ^ 2, v);
            }
            else if (left is Symbolic.Functions.Sin)
            {
                Expression ll = (left as Symbolic.Functions.Sin).Args[0];
                return Solve(ll, SMath.ArcSin(right), v);
            }
            else if (left is Symbolic.Functions.Cos)
            {
                Expression ll = (left as Symbolic.Functions.Cos).Args[0];
                return Solve(ll, SMath.ArcCos(right), v);
            }
            else if (left is Symbolic.Functions.Tan)
            {
                Expression ll = (left as Symbolic.Functions.Tan).Args[0];
                return Solve(ll, SMath.ArcTan(right), v);
            }
            else if (left is Symbolic.Functions.ArcSin)
            {
                Expression ll = (left as Symbolic.Functions.ArcSin).Args[0];
                return Solve(ll, SMath.Sin(right), v);
            }
            else if (left is Symbolic.Functions.ArcCos)
            {
                Expression ll = (left as Symbolic.Functions.ArcCos).Args[0];
                return Solve(ll, SMath.Cos(right), v);
            }
            else if (left is Symbolic.Functions.ArcTan)
            {
                Expression ll = (left as Symbolic.Functions.ArcTan).Args[0];
                return Solve(ll, SMath.Tan(right), v);
            }
            else if (left is Symbolic.Functions.Exp)
            {
                Expression ll = (left as Symbolic.Functions.Exp).Args[0];
                return Solve(ll, SMath.Ln(right), v);
            }
            else if (left is Symbolic.Functions.Ln)
            {
                Expression ll = (left as Symbolic.Functions.Ln).Args[0];
                return Solve(ll, SMath.Exp(right), v);
            }
            else if (left is Variable && left.IsDependOf(v)) return right;
            else throw new Exception("Can't solve equation. Unknown equation type.");
        }
Example #9
0
 public override Expression Derivate(Variable dv)
 {
     throw new NotImplementedException();
 }
Example #10
0
 public override Expression Replace(Variable v, Expression expr)
 {
     return this;
 }
Example #11
0
 static Expression Laplace(Expression expr, Variable x, Variable y, Variable z)
 {
     return expr.Derivate(x, 2).Simplify() + expr.Derivate(y, 2).Simplify() + expr.Derivate(z, 2).Simplify();
 }
Example #12
0
 static Expression TableIntegral(Expression expr, Variable v)
 {
     if (expr is Constant)
     {
         if (expr == Constant.Zero) return Constant.One;
         else return expr * v;
     }
     //else if (expr is Symbolic.Functions.Sin) ;
     else throw new NotImplementedException();
 }
Example #13
0
 public override bool IsDependOf(Variable var)
 {
     throw new NotImplementedException();
 }
Example #14
0
 public override Expression Replace(Variable v, Expression expr)
 {
     if (this == v) return expr;
     else return this;
 }
Example #15
0
 public override bool IsDependOf(Variable var)
 {
     return var.Name == this.Name;
 }
Example #16
0
 public override Expression Derivate(Variable dv)
 {
     if (dv.Name == this.Name) return Constant.One;
     else return Constant.Zero;
 }
Example #17
0
 public new Vector Replace(Variable v, Expression expr)
 {
     throw new NotImplementedException();
 }
Example #18
0
 static Expression Integrate(Expression expr, Variable v)
 {
     if (IsAddOp(expr))
     {
         var l = Left(expr);
         var r = Right(expr);
         if (expr is Symbolic.Operators.Scalar.Add) return Integrate(l, v) + Integrate(r, v);
         else return Integrate(l, v) - Integrate(r, v);
     }
     else if (expr is Symbolic.Operators.Scalar.Mul)
     {
         if (!Left(expr).IsDependOf(v)) return Left(expr) * Integrate(Right(expr), v);
         else if (!Right(expr).IsDependOf(v)) return Right(expr) * Integrate(Left(expr), v);
         else throw new NotImplementedException(); //Partial integration
     }
     else return TableIntegral(expr, v);
 }
Example #19
0
 public override bool IsDependOf(Variable var)
 {
     return false;
 }
Example #20
0
        public static void Main(string[] args)
        {
            var x = new Variable("x");
            var y = new Variable("y");
            var z = new Variable("z");
            var u = new Variable("u");
            
            var f = x ^ x;
            Console.WriteLine(f);
            Console.WriteLine(f.Derivate(x));

            Console.ReadKey();
        }
Example #21
0
 public override Expression Derivate(Variable dv)
 {
     return this;
 }