public static Variable MINtoS(Variable v)
        {
            double coefficient = 60;
            Dimension si = Si.s;
            Dimension notSi = NotSi.min;

            return Convert(v, coefficient, si, notSi);
        }
        public static Variable KMtoM(Variable v)
        {
            double coefficient = 1000;
            Dimension si = Si.m;
            Dimension notSi = NotSi.km;

            return Convert(v, coefficient, si, notSi);
        }
        public static Variable HtoS(Variable v)
        {
            double coefficient = 3600;
            Dimension si = Si.s;
            Dimension notSi = NotSi.h;

            return Convert(v, coefficient, si, notSi);
        }
        public static Variable GtoKG(Variable v)
        {
            double coefficient = Math.Pow(1000, -1);
            Dimension si = Si.kg;
            Dimension notSi = NotSi.g;

            return Convert(v, coefficient, si, notSi);
        }
 private static Dictionary<BaseSi, int> Expan(Variable v)
 {
     var expanInBaseSi = new Dictionary<BaseSi, int>();
     if(!v.IsComplexDimension)
     {
         return expanInBaseSi = v.Dimension.ExpansionInBaseSi;
     }
     else
     {
         return expanInBaseSi = DimensionConvertor.Expan(v.ComplexDimension);
     }
 }
 public static Variable Minus(Variable v1, Variable v2)
 {
     Variable newV2;
     if(v2.IsComplexDimension)
     {
        newV2 = new Variable(v2.Value * (-1), CloneDimension.Clone(v2.ComplexDimension));
     }
     else
     {
         newV2 = new Variable(v2.Value * (-1), v2.Dimension);
     }
     return ReturnVariable(v1, newV2);
 }
        public static Variable Multiplication(Variable v1, Variable v2)
        {
            var newV1 = toSiConvertor.Convert(v1);
            var newV2 = toSiConvertor.Convert(v2);
            var siDimensionV1 = Expan(newV1);
            var siDimensionV2 = Expan(newV2);

            var compositeDimension = CloneDimension.Clone(siDimensionV1);

            foreach(var item in siDimensionV2)
            {
                if (compositeDimension.ContainsKey(item.Key))
                {
                    if (compositeDimension[item.Key] + item.Value != 0)
                    {
                        compositeDimension[item.Key] += item.Value;
                    }
                    else
                    {
                        compositeDimension.Remove(item.Key);
                    }
                }
                else
                {
                    compositeDimension.Add(item.Key, item.Value);
                }
            }

            double newValue = newV1.Value * newV2.Value;

            if (compositeDimension.Count != 0)
            {
                return new Variable(newValue, DimensionConvertor.Narrow(compositeDimension));
            }
            else
            {
                return new Variable(newValue, Si.ND);
            }
        }
        public Variable Convert(Variable v)
        {
            if(!v.IsComplexDimension && !v.Dimension.IsSi)
            {
                return convertors[v.Dimension](v);
            }
            else if (v.IsComplexDimension)
            {
                var newV = new Variable
                    (v.Value,
                     CloneDimension.Clone(v.ComplexDimension));

                foreach(var item in v.ComplexDimension)
                {
                    if(!item.Key.IsSi)
                    {
                        newV = convertors[item.Key](newV);
                    }
                }

                return newV;
            }
            return v;
        }
        private static Variable Convert(Variable v, double coefficient, Dimension si, Dimension notSi)
        {
            if (!v.IsComplexDimension)
            {
                return new Variable(v.Value * coefficient, si);
            }
            else
            {
                var dimension = CloneDimension.Clone(v.ComplexDimension);
                var power = dimension[notSi];

                dimension.Remove(notSi);
                dimension.Add(si, power);

                if (power > 0)
                {
                    return new Variable(v.Value * Math.Pow(coefficient, power), dimension);
                }
                else
                {
                    return new Variable(v.Value / Math.Pow(coefficient, power * (-1)) , dimension);
                }
            }
        }
Exemple #10
0
        static void Main(string[] args)
        {
            Variable v1 = new Variable(3, new Dictionary<Dimension, int>()
            {
                {Si.m, 2},
                {Si.s, -4},
                {Si.kg, 2}
            });

            Variable v2 = new Variable(3, new Dictionary<Dimension, int>()
            {
                {Si.N, -1}
            });

            var v3 = v1 / v2;

            var m = new Variable(5, Si.kg);
            var a = new Variable(2, new Dictionary<Dimension, int>
            {
                 {Si.m, 1},
                 {Si.s, -2}
            });

            var F = m * a;

            var S = new Variable(10, Si.m);

            var A = F * S;

            var unknown = A / (m * a);

            var t = new Variable(2, Si.s);

            var P = (m * a * S) / t;

            A = P * t;
            var v0 = new Variable(10, new Dictionary<Dimension, int>
                {
                    {Si.m, 1},
                    {Si.s, -1}
                });
            var v = new Variable(30, new Dictionary<Dimension, int>
                {
                    {Si.m, 1},
                    {Si.s, -1}
                });

            F = ((v0 - v) / t) * m;

            v = new Variable(100, new Dictionary<Dimension, int>
            {
                {NotSi.km, 1},
                {NotSi.h, -1}
            });

            t = new Variable(30, Si.s);

            a = (v - v0) / t;

            m = new Variable(5000, NotSi.g);

            S = new Variable(1, NotSi.km);

            P = ((m / 2) * S * a) / t;
        }
        private static Variable Inverse(Variable v)
        {
            var compositeDimension = new Dictionary<Dimension, int>();

            if(v.IsComplexDimension)
            {
                foreach(var item in v.ComplexDimension)
                {
                    compositeDimension.Add(item.Key, item.Value * (-1));
                }
            }
            else
            {
                compositeDimension.Add(v.Dimension, -1);
            }
            return new Variable(1 / v.Value, compositeDimension);
        }
 public static Variable Plus(Variable v1, Variable v2)
 {
     return ReturnVariable(v1, v2);
 }
 public static Variable Division(Variable v1, Variable v2)
 {
     var newV2 = Inverse(v2);
     return Multiplication(v1, newV2);
 }
        private static Variable ReturnVariable(Variable v1, Variable v2)
        {
            var newV1 = toSiConvertor.Convert(v1);
            var newV2 = toSiConvertor.Convert(v2);
            var siDimensionV1 = Expan(newV1);
            var siDimensionV2 = Expan(newV2);

            var newValue = newV1.Value + newV2.Value;

            if (DimensionComparator.IsEquals(siDimensionV1, siDimensionV2))
            {
                if (newV1.IsComplexDimension)
                {
                    return new Variable(newValue, DimensionConvertor.Narrow(siDimensionV1));
                }

                return new Variable(newValue, newV1.Dimension);
            }

            return null;
        }