Exemple #1
0
        /// <summary>
        /// constructor accepts quantities and qs functions also
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="radius"></param>
        public Circle(QsScalar x, QsScalar y, QsScalar radius)
        {
            if (x.ScalarType == ScalarTypes.FunctionQuantity)
            {
                xfunc = x.FunctionQuantity.Value;
            }
            else
            {
                _x = x.NumericalQuantity;
            }

            if (y.ScalarType == ScalarTypes.FunctionQuantity)
            {
                yfunc = y.FunctionQuantity.Value;
            }
            else
            {
                _y = y.NumericalQuantity;
            }

            if (radius.ScalarType == ScalarTypes.FunctionQuantity)
            {
                radfunc = radius.FunctionQuantity.Value;
            }
            else
            {
                _radius = radius.NumericalQuantity;
            }
        }
Exemple #2
0
        /// <summary>
        /// Returns Quantity of the symbolic variable based on the unit
        /// </summary>
        /// <param name="sv"></param>
        /// <param name="unit"></param>
        /// <returns></returns>
        public static AnyQuantity <SymbolicVariable> ToQuantity(this SymbolicVariable sv, string unit = "1")
        {
            Unit sunit = Unit.Parse(unit);
            AnyQuantity <SymbolicVariable> SymbolicQuantity = sunit.GetThisUnitQuantity <SymbolicVariable>(sv);

            return(SymbolicQuantity);
        }
Exemple #3
0
 /// <summary>
 /// Wrap Complex Quantity into Scalar.
 /// </summary>
 /// <param name="qty"></param>
 /// <returns></returns>
 public static QsScalar ToScalar(this AnyQuantity <Complex> qty)
 {
     return(new QsScalar(ScalarTypes.ComplexNumberQuantity)
     {
         ComplexQuantity = qty
     });
 }
        public static bool TryParseQuantity(string quantity, out AnyQuantity <double> qty)
        {
            double val;

            Match um = UnitizedNumberRegex.Match(quantity.Trim());

            if (um.Success)
            {
                string varUnit = um.Groups["unit"].Value;
                val = double.Parse(um.Groups["num"].Value, CultureInfo.InvariantCulture);

                Unit un = Unit.Parse(varUnit);
                qty = un.GetThisUnitQuantity <double>(val);

                return(true);
            }
            else if (double.TryParse(quantity, NumberStyles.Any, CultureInfo.InvariantCulture, out val))
            {
                qty = Unit.DiscoverUnit(QuantityDimension.Dimensionless).GetThisUnitQuantity <double>(val);

                return(true);
            }
            else
            {
                qty = default(AnyQuantity <double>);
                return(false);
            }
        }
Exemple #5
0
 /// <summary>
 /// Wrap Complex Quantity into Scalar.
 /// </summary>
 /// <param name="qty"></param>
 /// <returns></returns>
 public static QsScalar ToScalar(this AnyQuantity <Rational> qty)
 {
     return(new QsScalar(ScalarTypes.RationalNumberQuantity)
     {
         RationalQuantity = qty
     });
 }
Exemple #6
0
 /// <summary>
 /// Return a scalar object from quaternion quantity.
 /// </summary>
 /// <param name="qty"></param>
 /// <returns></returns>
 public static QsScalar ToScalar(this AnyQuantity <Quaternion> qty)
 {
     return(new QsScalar(ScalarTypes.QuaternionNumberQuantity)
     {
         QuaternionQuantity = qty
     });
 }
Exemple #7
0
        /// <summary>
        /// Returns a quantity from function
        /// </summary>
        /// <param name="fn"></param>
        /// <param name="unit"></param>
        /// <returns></returns>
        public static AnyQuantity <QsFunction> ToQuantity(this QsFunction fn, string unit = "1")
        {
            Unit sunit = Unit.Parse(unit);

            AnyQuantity <QsFunction> FunctionQuantity = sunit.GetThisUnitQuantity <QsFunction>(fn);

            return(FunctionQuantity);
        }
Exemple #8
0
        public Circle(AnyQuantity <double> x, AnyQuantity <double> y, AnyQuantity <double> radius)
        {
            _x      = x;
            _y      = y;
            _radius = radius;

            var t = new System.Timers.Timer();
        }
Exemple #9
0
        /// <summary>
        /// Wrap AnyQuantity of Symbolic Variable object into qs scalar object.
        /// </summary>
        /// <param name="qty"></param>
        /// <returns></returns>
        public static QsScalar ToScalar(this AnyQuantity <SymbolicVariable> qty)
        {
            QsScalar symscalar = new QsScalar(ScalarTypes.SymbolicQuantity)
            {
                SymbolicQuantity = qty
            };

            return(symscalar);
        }
Exemple #10
0
        /// <summary>
        /// Returns a scalar object from function quantity.
        /// </summary>
        /// <param name="functionQuantity"></param>
        /// <returns></returns>
        public static QsScalar ToScalar(this AnyQuantity <QsFunction> functionQuantity)
        {
            QsScalar fnScalar = new QsScalar(ScalarTypes.FunctionQuantity)
            {
                FunctionQuantity = functionQuantity
            };

            return(fnScalar);
        }
Exemple #11
0
        public static QsVector Func(QsFunction f, DimensionlessQuantity <double> from, DimensionlessQuantity <double> to)
        {
            var      increment = (to - from) / (40).ToQuantity();
            QsVector v         = new QsVector(40);

            for (AnyQuantity <double> dt = from; dt <= to; dt += increment)
            {
                v.AddComponent(f.Invoke(dt).ToScalar());
            }

            return(v);
        }
Exemple #12
0
        /// <summary>
        /// Draw a circle on the screen.
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="radius"></param>
        public Shape BareCircle(AnyQuantity <double> x, AnyQuantity <double> y, AnyQuantity <double> radius)
        {
            Shape s;

            lock (Shapes)
            {
                s = new Circle(x, y, radius);
                Shapes.Add(s);
            }

            return(s);
        }
        /// <summary>
        /// Returns quantity based on current unit instance.
        /// </summary>
        /// <typeparam name="T">Quatntity Storage Type</typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        internal AnyQuantity <T> MakeQuantity <T>(T value)
        {
            //create the corresponding quantity
            AnyQuantity <T> qty = this.GetThisUnitQuantity <T>();

            //assign the unit to the created quantity
            qty.Unit = this;

            //assign the value to the quantity
            qty.Value = value;

            return(qty);
        }
Exemple #14
0
        public static AnyQuantity <double> GetUnitizedQuantityOf <TQuantity>(double value) where TQuantity : BaseQuantity, new()
        {
            MetricUnit unit = UnitOf <TQuantity>();


            AnyQuantity <double> aq = unit.GetThisUnitQuantity <double>();


            aq.Value = value;


            return(aq);
        }
Exemple #15
0
        private static AnyQuantity <double> MakeQuantity(MetricUnit unit, MetricPrefix siPrefix, double value)
        {
            //assign its prefix
            unit.UnitPrefix = siPrefix;

            //create the corresponding quantity
            AnyQuantity <double> qty = unit.GetThisUnitQuantity <double>();

            //assign the unit to the created quantity
            qty.Unit = unit;

            //assign the value to the quantity
            qty.Value = value;

            return(qty);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //equation of friction head is
            // h = f * l/d * v^2/2g


            //where f in laminar   is   64/reynolds
            // in turbulent from moody chart.

            AnyQuantity rn = CalcReynolds();

            AnyQuantity head = CalcHead();

            lblReynolds.Text = rn.ToString();

            lblHead.Text = head.ToString();
        }
        public static QsValue FromName(QsParameter name, QsParameter value)
        {
            string ss = name.ParameterRawText;

            if (name.QsNativeValue is QsText)
            {
                ss = ((QsText)name.QsNativeValue).Text;
            }

            var qval = AnyQuantity <double> .Parse(ss);

            qval.Unit  = Unit.DiscoverUnit(qval);
            qval.Value = double.Parse(value.ParameterRawText, CultureInfo.InvariantCulture);

            var qs = new QsScalar(ScalarTypes.NumericalQuantity)
            {
                NumericalQuantity = qval
            };

            return(qs);
        }
Exemple #18
0
        /// <summary>
        /// Invert the derived quantity by inverting every inner quantity in its internal quantities.
        /// </summary>
        /// <returns></returns>
        public override BaseQuantity Invert()
        {
            List <AnyQuantity <T> > lq = new List <AnyQuantity <T> >();

            foreach (AnyQuantity <T> qty in InternalQuantities)
            {
                lq.Add((AnyQuantity <T>)qty.Invert());
            }

            DerivedQuantity <T> dq = (DerivedQuantity <T>) this.Clone();

            dq.SetInternalQuantities(lq.ToArray());
            dq.Value = AnyQuantity <T> .DivideScalarByGeneric(1.0, dq.Value);

            if (this.Unit != null)
            {
                dq.Unit = this.Unit.Invert();
            }

            return(dq);
        }
Exemple #19
0
        /// <summary>
        /// Cache all quantities with their Dimensions.
        /// </summary>
        static QuantityDimension()
        {
            Assembly CurrentAssembly = Assembly.GetExecutingAssembly();

            Type[] types = CurrentAssembly.GetTypes();

            var QuantityTypes = from QuantityType in types
                                where QuantityType.IsSubclassOf(typeof(BaseQuantity))
                                select QuantityType;

            CurrentQuantityTypes.AddRange(QuantityTypes);

            //storing the quantity types with thier dimensions

            foreach (Type QuantityType in CurrentQuantityTypes)
            {
                //cach the quantities that is not abstract types

                if (QuantityType.IsAbstract == false && QuantityType != typeof(DerivedQuantity <>))
                {
                    //make sure not to include Dimensionless quantities due to they are F0L0T0
                    if (QuantityType.BaseType.Name != typeof(DimensionlessQuantity <>).Name)
                    {
                        //store dimension as key and Quantity Type .

                        //create AnyQuantity<Object>  Object container used just for instantiation
                        AnyQuantity <Object> Quantity = (AnyQuantity <Object>)Activator.CreateInstance(QuantityType.MakeGenericType(typeof(object)));

                        //store the Dimension and the corresponding Type;
                        CurrentQuantitiesDictionary.Add(Quantity.Dimension, QuantityType);

                        //store quantity type as key and corresponding dimension as value.
                        CurrentDimensionsDictionary.Add(QuantityType, Quantity.Dimension);

                        //store the quantity name and type with insensitive names
                        CurrentQuantitiesNamesDictionary.Add(QuantityType.Name.Substring(0, QuantityType.Name.Length - 2), QuantityType);
                    }
                }
            }
        }
Exemple #20
0
        /// <summary>
        /// Get the factorial of quantity
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public static AnyQuantity <double> QuantityFactorial(AnyQuantity <double> number)
        {
            int v = (int)number.Value;

            //if (v > 170) throw new ArgumentOutOfRangeException("Number", number, "Number is greater than 170");
            if (v < 0)
            {
                throw new ArgumentOutOfRangeException("Number", "Number is less than 0");
            }

            AnyQuantity <double> num = (AnyQuantity <double>)number.Clone();

            num.Value = System.Math.Floor(num.Value);

            double Total = num.Value == 0 ? 1 : num.Value;

            //I am calculating the value part first for fast calculation
            for (int i = v; i > 1; i--)
            {
                num.Value--;
                Total = Total * num.Value;
            }

            //   Raise the power of the unit.
            //    I think 3.5<kg>! = 11.631728<kg^3> is wrong and of course not <kg^4>  but it is <kg^3.5>
            QuantitySystem.Units.Unit TotalUnit = num.Unit.RaiseUnitPower((float)number.Value);


            // if we have fraction correct the calculation with Gamma Factorial.
            if (number.Value > v)
            {
                Total = GammaFactorial(number.Value);
            }

            AnyQuantity <double> TotalQuantity = TotalUnit.GetThisUnitQuantity <double>(Total);

            return(TotalQuantity);
        }
Exemple #21
0
        /// <summary>
        /// Gets the quantity of this unit based on the desired container.
        /// <see cref="QuantityType"/>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public AnyQuantity <T> GetThisUnitQuantity <T>()
        {
            AnyQuantity <T> Quantity = null;

            Type qt = QuantityType.MakeGenericType(typeof(T));

            object j;

            if (Qcach.TryGetValue(qt, out j))
            {
                Quantity = (AnyQuantity <T>)((AnyQuantity <T>)j).Clone();  //optimization for created quantities before
            }
            else
            {
                Quantity = (AnyQuantity <T>)Activator.CreateInstance(QuantityType.MakeGenericType(typeof(T)));
                Qcach.Add(qt, Quantity);
            }

            Quantity.Unit = this;


            return(Quantity);
        }
Exemple #22
0
        public AnyQuantity <T> GetThisUnitQuantity <T>(T value)
        {
            AnyQuantity <T> Quantity = null;

            if (QuantityType != typeof(DerivedQuantity <>) && QuantityType != null)
            {
                Type qt = QuantityType.MakeGenericType(typeof(T));

                object j;
                if (Qcach.TryGetValue(qt, out j))
                {
                    Quantity = (AnyQuantity <T>)((AnyQuantity <T>)j).Clone();  //optimization for created quantities before
                }
                else
                {
                    Quantity = (AnyQuantity <T>)Activator.CreateInstance(qt);

                    Qcach.Add(qt, Quantity);
                }
            }
            else
            {
                //create it from the unit dimension
                Quantity = new DerivedQuantity <T>(UnitDimension);
            }
            Quantity.Unit = this;

            Quantity.Value = value;

            if (this.IsOverflowed)
            {
                Quantity.Value =
                    AnyQuantity <T> .MultiplyScalarByGeneric(this.GetUnitOverflow(), value);
            }

            return(Quantity);
        }
Exemple #23
0
        /// <summary>
        /// Force to return integer value from <see cref="AnyQuantity<double>"/>
        /// used mainly for calculated indexes for sequences.
        /// </summary>
        /// <param name="val"></param>
        /// <returns></returns>
        public static int IntegerFromQuantity(AnyQuantity <double> val)
        {
            Debug.Assert(val is QuantitySystem.Quantities.DimensionlessQuantities.DimensionlessQuantity <double>);

            return((int)val.Value);
        }
Exemple #24
0
 public static AnyQuantity <double> Square(AnyQuantity <double> x)
 {
     return(x * x);
 }
Exemple #25
0
        public static QsValue Log(QsParameter val, QsParameter newBase)
        {
            if (val.IsQsValue)
            {
                if (val.ParameterValue is QsScalar)
                {
                    AnyQuantity <double> q = ((QsScalar)val.ParameterValue).NumericalQuantity;

                    if (q.Dimension.IsDimensionless)
                    {
                        double r = System.Math.Log(q.Value, ((QsScalar)newBase.ParameterValue).NumericalQuantity.Value);
                        return(r.ToQuantity().ToScalarValue());
                    }
                    else
                    {
                        throw new QsInvalidInputException("Non dimensionless number");
                    }
                }
                else if (val.ParameterValue is QsVector)
                {
                    QsVector vec = (QsVector)val.ParameterValue;

                    QsVector rv = new QsVector(vec.Count);

                    foreach (QsScalar var in vec)
                    {
                        if (var.NumericalQuantity.Dimension.IsDimensionless)
                        {
                            double r = System.Math.Log(var.NumericalQuantity.Value, ((QsScalar)newBase.ParameterValue).NumericalQuantity.Value);
                            rv.AddComponent(r.ToQuantity().ToScalar());
                        }
                        else
                        {
                            throw new QsInvalidInputException("Non dimensionless component");
                        }
                    }

                    return(rv);
                }
                else if (val.ParameterValue is QsMatrix)
                {
                    QsMatrix mat = (QsMatrix)val.ParameterValue;
                    QsMatrix rm  = new QsMatrix();

                    foreach (var vec in mat.Rows)
                    {
                        rm.AddVector((QsVector)Log(QsParameter.MakeParameter(vec, string.Empty)));
                    }
                    return(rm);
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                //not known may be ordinary string
                return(null);
            }
        }
Exemple #26
0
        /// <summary>
        /// Get factorial for the <see>QsValue</see> whether it is Scalar, Vector, Matrix, and later Tensor.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static QsValue Factorial(QsValue value)
        {
            if (value is QsBoolean)
            {
                var b = (QsBoolean)value;
                return(new QsBoolean {
                    Value = !b.Value
                });
            }

            if (value is QsScalar)
            {
                QsScalar sv = (QsScalar)value;
                if (sv.ScalarType == ScalarTypes.NumericalQuantity)
                {
                    AnyQuantity <double> number = sv.NumericalQuantity;
                    return(new QsScalar {
                        NumericalQuantity = QuantityFactorial(number)
                    });
                }
                else
                {
                    throw new QsException("Unsupported scalar object");
                }
            }
            else if (value is QsVector)
            {
                var vec = value as QsVector;

                QsVector rvec = new QsVector(vec.Count);

                foreach (var v in vec)
                {
                    rvec.AddComponent((QsScalar)Factorial(v));
                }

                return(rvec);
            }
            else if (value is QsMatrix)
            {
                var      mat   = value as QsMatrix;
                QsMatrix Total = new QsMatrix();

                for (int IY = 0; IY < mat.RowsCount; IY++)
                {
                    List <QsScalar> row = new List <QsScalar>(mat.ColumnsCount);

                    for (int IX = 0; IX < mat.ColumnsCount; IX++)
                    {
                        row.Add((QsScalar)Factorial(mat[IY, IX]));
                    }

                    Total.AddRow(row.ToArray());
                }
                return(Total);
            }
            else
            {
                throw new NotSupportedException();
            }
        }
Exemple #27
0
        /// <summary>
        /// Returns Quaternion Quantity from Complex Quantity.
        /// </summary>
        /// <param name="qty"></param>
        /// <returns></returns>
        public static AnyQuantity <Quaternion> ToQuaternion(this AnyQuantity <Complex> qty)
        {
            AnyQuantity <Quaternion> converted = qty.Unit.GetThisUnitQuantity <Quaternion>(qty.Value);

            return(converted);
        }
Exemple #28
0
        public static AnyQuantity <Rational> ToRational(this AnyQuantity <double> qty)
        {
            AnyQuantity <Rational> converted = qty.Unit.GetThisUnitQuantity <Rational>(new Rational((float)qty.Value, 1));

            return(converted);
        }
Exemple #29
0
        /// <summary>
        /// Returns Complex quantity from Double Quantity
        /// </summary>
        /// <param name="qty"></param>
        /// <returns></returns>
        public static AnyQuantity <Complex> ToComplex(this AnyQuantity <double> qty)
        {
            AnyQuantity <Complex> converted = qty.Unit.GetThisUnitQuantity <Complex>(qty.Value);

            return(converted);
        }
Exemple #30
0
 public static QsValue ToScalarValue(this AnyQuantity <double> qty)
 {
     return(new QsScalar {
         NumericalQuantity = qty
     });
 }