Beispiel #1
0
        public static MathObject AddToBothSides(this MathObject obj, MathObject item)
        {
            if (obj is Equation)
                return (obj as Equation).a + item == (obj as Equation).b + item;

            throw new Exception();
        }
        static MathObject GenerateLagrangePolynomial(double[] xs, double[] ys)
        {
            var xsSet = new HashSet<double>(xs);

            if (xsSet.Count < xs.Length)
            {
                throw new ArgumentException("x values not unique");
            }

            var ysSet = new HashSet<double>(ys);

            if (ysSet.Count < ys.Length)
            {
                throw new ArgumentException("y values not unique");
            }

            var x = new Symbol("x");

            var members = new MathObject[ys.Length];

            for (int i = 0; i < ys.Length; i++)
            {
                members[i] = ys[i] * GenerateBasisPolynomial(x, xs, i);
            }

            var sum = new Sum(members);

            return sum.AlgebraicExpand();
        }
Beispiel #3
0
        static void AssertEqual(MathObject a, Double b, double tolerance = 0.00000001)
        {
            var x = (DoubleFloat)a;
            var y = new DoubleFloat(b);

            if (Math.Abs(x.val - y.val) > tolerance)
                Console.WriteLine("{0} and {1} are not equal", x.val, y.val);
        }
Beispiel #4
0
        public static MathObject AssertEqToDouble(this MathObject a, MathObject b, double tolerance = 0.000001)
        {
            if (
                Math.Abs(
                    ((a as Equation).b as DoubleFloat).val
                    -
                    ((b as Equation).b as DoubleFloat).val)
                > tolerance)
            {
                Console.WriteLine("{0} and {1} are not equal", a, b);
            }

            return a;
        }
Beispiel #5
0
 public MathObject Substitute(MathObject a, double b)
 {
     return Substitute(a, new DoubleFloat(b));
 }
Beispiel #6
0
 public static MathObject Sin(MathObject arg)
 {
     return new Sin(arg).Simplify();
 }
Beispiel #7
0
 public static MathObject Atan2(MathObject a, MathObject b)
 {
     return new Symbolism.Atan2(a, b).Simplify();
 }
Beispiel #8
0
 public Point(MathObject x_val, int y_val)
 {
     x = x_val; y = new Integer(y_val);
 }
Beispiel #9
0
 public MathObject Substitute(MathObject a, MathObject b)
 {
     return bas.Substitute(a, b) ^ exp.Substitute(a, b);
 }
Beispiel #10
0
 public Power(MathObject a, MathObject b)
 {
     bas = a; exp = b;
 }
Beispiel #11
0
        public static MathObject Term(MathObject u)
        {
            if (u is Product && ((Product)u).elts[0] is Number)
                return new Product() { elts = ((Product)u).elts.Cdr() };

            if (u is Product) return u;

            return new Product() { elts = new List<MathObject>() { u } };
        }
Beispiel #12
0
        public static MathObject Exponent(MathObject u)
        {
            if (u is Power) return ((Power)u).exp;

            return new Integer(1);
        }
Beispiel #13
0
        public static MathObject Const(MathObject u)
        {
            if (u is Product)
            {
                var res = (Product)u;

                if (res.elts[0] is Integer) return res.elts[0];

                if (res.elts[0] is Fraction) return res.elts[0];

                if (res.elts[0] is DoubleFloat) return res.elts[0];

                return new Integer(1);
            }

            return new Integer(1);
        }
Beispiel #14
0
        public static bool Compare(MathObject u, MathObject v)
        {
            if (u is DoubleFloat && v is DoubleFloat) return ((DoubleFloat)u).val < ((DoubleFloat)v).val;

            if (u is DoubleFloat && v is Integer) return ((DoubleFloat)u).val < ((Integer)v).val;

            if (u is DoubleFloat && v is Fraction) return
                ((DoubleFloat)u).val < ((double)((Fraction)v).numerator.val) / ((double)((Fraction)v).denominator.val);

            if (u is Integer && v is DoubleFloat) return ((Integer)u).val < ((DoubleFloat)v).val;

            if (u is Fraction && v is DoubleFloat) return
                ((double)((Fraction)u).numerator.val) / ((double)((Fraction)u).denominator.val) < ((DoubleFloat)v).val;

            if (u is Integer)
                return Compare(new Fraction((Integer)u, new Integer(1)), v);

            if (v is Integer)
                return Compare(u, new Fraction((Integer)v, new Integer(1)));

            if (u is Fraction && v is Fraction)
            {
                var u_ = (Fraction)u;
                var v_ = (Fraction)v;

                // a / b   <   c / d
                //
                // (a d) / (b d)   <   (c b) / (b d)

                return
                    (u_.numerator.val * v_.denominator.val)
                    <
                    (v_.numerator.val * u_.denominator.val);
            }

            if (u is Symbol && v is Symbol)
                return
                    String.Compare(
                        ((Symbol)u).name,
                        ((Symbol)v).name) < 0;

            if (u is Product && v is Product)
            {
                // var a = new List<MathObject>(((Product)u).elts.Cdr());
                var a = new List<MathObject>(((Product)u).elts);
                a.Reverse();

                // var b = new List<MathObject>(((Product)v).elts.Cdr());
                var b = new List<MathObject>(((Product)v).elts);
                b.Reverse();

                return O3(a, b);
            }

            if (u is Sum && v is Sum)
            {
                // var a = new List<MathObject>(((Sum)u).elts.Cdr());
                var a = new List<MathObject>(((Sum)u).elts);
                a.Reverse();

                // var b = new List<MathObject>(((Sum)v).elts.Cdr());
                var b = new List<MathObject>(((Sum)v).elts);
                b.Reverse();

                return O3(a, b);
            }

            if (u is Power && v is Power)
            {
                var u_ = (Power)u;
                var v_ = (Power)v;

                return (u_.bas == v_.bas) ?
                    Compare(u_.exp, v_.exp) :
                    Compare(u_.bas, v_.bas);
            }

            if (u is Function && v is Function)
            {
                var u_ = (Function)u;
                var v_ = (Function)v;

                return u_.name == v_.name ?
                    O3(u_.args, v_.args) :
                    String.Compare(u_.name, v_.name) < 0;
            }

            if ((u is Integer || u is Fraction || u is DoubleFloat)
                &&
                !(v is Integer || v is Fraction || v is DoubleFloat))
                return true;

            if (u is Product &&
                (v is Power || v is Sum || v is Function || v is Symbol))
                return Compare(u, new Product(v));

            if (u is Power && (v is Sum || v is Function || v is Symbol))
                return Compare(u, new Power(v, new Integer(1)));

            if (u is Sum && (v is Function || v is Symbol))
                return Compare(u, new Sum(v));

            if (u is Function && v is Symbol)
            {
                var u_ = (Function)u;
                var v_ = (Symbol)v;

                return u_.name == v_.name ?
                    false :
                    Compare(new Symbol(u_.name), v);
            }

            return !Compare(v, u);
        }
Beispiel #15
0
        public static MathObject Base(MathObject u)
        {
            if (u is Power) return ((Power)u).bas;

            return u;
        }
Beispiel #16
0
 //////////////////////////////////////////////////////////////////////
 // overloads for 'int'
 public Point(int x_val, int y_val)
 {
     x = new Integer(x_val); y = new Integer(y_val);
 }
Beispiel #17
0
 public Point(int x_val, MathObject y_val)
 {
     x = new Integer(x_val); y = y_val;
 }
Beispiel #18
0
        public static MathObject QuadraticEquation(MathObject a, MathObject b, MathObject c, int solution=0)
        {
            if (a == new Integer(0) || a == new DoubleFloat(0.0))
                throw new Exception("a is zero. Equation is not quadratic.");

            var discriminant = b * b - 4 * a * c;

            var half = new Integer(1) / 2;

            if (solution == 0)
                return (-b + (discriminant ^ half)) / (2 * a);

            if (solution == 1)
                return (-b - (discriminant ^ half)) / (2 * a);

            throw new Exception();
        }
Beispiel #19
0
 //////////////////////////////////////////////////////////////////////
 public static Point FromAngle(MathObject angle, MathObject mag)
 {
     return new Point(Trig.Cos(angle) * mag, Trig.Sin(angle) * mag);
 }
Beispiel #20
0
 public static MathObject Sq(MathObject a)
 {
     return (a ^ 2);
 }
Beispiel #21
0
 public static MathObject Cos(MathObject arg)
 {
     return new Cos(arg).Simplify();
 }
Beispiel #22
0
 public static MathObject Sqrt(MathObject a)
 {
     return (a ^ (new Integer(1) / 2));
 }
Beispiel #23
0
        public static MathObject MultiplyBothSidesBy(this MathObject obj, MathObject item)
        {
            //if (obj is Equation)
            //    return (obj as Equation).a * item == (obj as Equation).b * item;
            
            if (obj is Equation)
                return new Equation(
                    (obj as Equation).a * item,
                    (obj as Equation).b * item,
                    (obj as Equation).Operator);
            
            if (obj is And) return (obj as And).Map(elt => elt.MultiplyBothSidesBy(item));

            throw new Exception();
        }
Beispiel #24
0
        public Obj AtTime(MathObject t)
        {
            var dt = t - time;

            return
                new Obj()
                {
                    time = t,
                    acceleration = acceleration,
                    velocity = velocity + acceleration * dt,
                    position = position + velocity * dt + acceleration * dt * dt / 2
                };
        }
Beispiel #25
0
        public Point ProjectileInclineIntersection(MathObject theta)
        {
            if (theta != null &&
                velocity.x != null &&
                velocity.y != null &&
                acceleration.y != null &&
                acceleration.y != 0 &&
                acceleration.y != 0.0)
            {
                var d =
                    2 * (Trig.Sin(theta) - velocity.y / velocity.x * Trig.Cos(theta))
                    * ((velocity.x / Trig.Cos(theta)) ^ 2)
                    / acceleration.y;

                return
                    new Point(
                        position.x + d * Trig.Cos(theta),
                        position.y + d * Trig.Sin(theta));
            }

            throw new Exception();
        }
Beispiel #26
0
        public Point VelocityAtTime(MathObject t)
        {
            var dt = t - time;

            if (Misc.NotNull(velocity.x, velocity.y, acceleration.x, acceleration.y))
                return velocity + acceleration * dt;

            throw new Exception();
        }
Beispiel #27
0
        public static MathObject AssertEqTo(this MathObject a, MathObject b)
        {
            if (!(a == b)) Console.WriteLine((a == b).ToString());

            return a;
        }
Beispiel #28
0
 public Point(MathObject x_val, MathObject y_val)
 {
     x = x_val; y = y_val;
 }
Beispiel #29
0
        public static MathObject MultiplyBothSidesBy(this MathObject obj, MathObject item)
        {
            if (obj is Equation)
                return (obj as Equation).a * item == (obj as Equation).b * item;

            throw new Exception();
        }
Beispiel #30
0
 public MathObject Substitute(MathObject a, int b)
 {
     return Substitute(a, new Integer(b));
 }