Exemplo n.º 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;
            }
        }
Exemplo n.º 2
0
        public object GetValueOrNull(string name)
        {
            if (_NamespaceType != null)
            {
                if (QsFunction.IsItFunctionSymbolicName(name))
                {
                    if (HardCodedMethods == null)
                    {
                        HardCodedMethods = GetQsNamespaceMethods();
                    }

                    if (HardCodedMethods.ContainsKey(name))
                    {
                        return(HardCodedMethods[name]);
                    }
                }
                else
                {
                    var prop = _NamespaceType.GetProperty(name, BindingFlags.IgnoreCase | BindingFlags.Static | BindingFlags.Public);
                    if (prop != null)
                    {
                        return(Root.NativeToQsConvert(prop.GetValue(null, null)));
                    }
                }
            }

            // try in values hash

            object o;

            Values.TryGetValue(name, out o);

            return(o);
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
0
        public void IsItFunctionScopeNameTest()
        {
            string name     = "F#2"; // TODO: Initialize to an appropriate value
            bool   expected = true;  // TODO: Initialize to an appropriate value
            bool   actual;

            actual = QsFunction.IsItFunctionSymbolicName(name);
            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Decorate a native C# function with QsFunction
        /// </summary>
        /// <param name="methodInfo"></param>
        /// <param name="funcAttribute"></param>
        /// <returns></returns>
        public QsFunction GetQsFunctionFromTypeMethod(MethodInfo methodInfo, QsFunctionAttribute funcAttribute)
        {
            string qsNamespace = this._NamespaceType.Name;

            string qsFuncName = funcAttribute == null ? methodInfo.Name : funcAttribute.FunctionName;

            var miParameters     = methodInfo.GetParameters();
            int qsFuncParamCount = miParameters.Length;


            QsFunction QsModFunc = new QsFunction("[" + qsNamespace + "." + qsFuncName + "]", true);

            QsModFunc.FunctionNamespace = qsNamespace;
            QsModFunc.FunctionName      = qsFuncName;

            List <QsParamInfo> prms = new List <QsParamInfo>(miParameters.Length);

            foreach (var miParam in miParameters)
            {
                QsParamInfo prm = new QsParamInfo();

                //parameter name
                prm.Name = miParam.Name;

                var pis = miParam.GetCustomAttributes(typeof(QsParamInfoAttribute), true);

                if (pis.Length > 0)
                {
                    prm.Type = ((QsParamInfoAttribute)pis[0]).ParameterType;
                }
                else
                {
                    prm.Type = QsParamType.Value;
                }

                prms.Add(prm);
            }

            QsModFunc.Parameters           = prms.ToArray();
            QsModFunc.FunctionDeclaration += "(";
            StringBuilder sb = new StringBuilder();

            foreach (var p in prms)
            {
                sb.Append(", " + p.Name);
            }
            if (sb.Length > 0)
            {
                QsModFunc.FunctionDeclaration += sb.ToString().TrimStart(',', ' ');
            }
            QsModFunc.FunctionDeclaration += ")";



            QsModFunc.InternalFunctionDelegate = FormNativeFunctionDelegate(methodInfo);
            return(QsModFunc);
        }
Exemplo n.º 6
0
        public void FormFunctionSymbolicNameTest()
        {
            string functionName = "f";          // TODO: Initialize to an appropriate value

            string[] paramNames = { "u", "v" }; // TODO: Initialize to an appropriate value
            string   expected   = "f#2_u_v";    // TODO: Initialize to an appropriate value
            string   actual;

            actual = QsFunction.FormFunctionSymbolicName(functionName, paramNames);
            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 7
0
        public void ContainsParameterTest()
        {
            QsFunction target = GetTestFunction("TestContains", "x", "Gh", "FyI", "Df");



            Assert.AreEqual <bool>(true, target.ContainsParameter("gh"));
            Assert.AreEqual <bool>(true, target.ContainsParameter("fyi"));
            Assert.AreEqual <bool>(false, target.ContainsParameter("fluid"));
            Assert.AreEqual <bool>(false, target.ContainsParameter("do"));
            Assert.AreEqual <bool>(true, target.ContainsParameter("x"));
        }
Exemplo n.º 8
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);
        }
Exemplo n.º 9
0
        public void ContainsParametersTest()
        {
            QsFunction target = GetTestFunction("TestContains", "fluid", "x", "h", "u", "s", "v");


            Assert.AreEqual <bool>(false, target.ContainsParameters("gh"));

            Assert.AreEqual <bool>(true, target.ContainsParameters("fluid", "x"));

            Assert.AreEqual <bool>(false, target.ContainsParameters("fluid", "y"));

            Assert.AreEqual <bool>(true, target.ContainsParameters("fluid", "u", "x", "s", "v"));

            Assert.AreEqual <bool>(true, target.ContainsParameters("h", "s"));

            Assert.AreEqual <bool>(true, target.ContainsParameters("u", "v"));
        }
        /// <summary>
        /// The method sum all elements in the sequence between the supplied indexes.
        /// Correspondes To: S[i++k]
        /// </summary>
        public QsValue SumElements(int fromIndex, int toIndex)
        {
            if (this.Parameters.Length > 0)
            {
                // this is a call to form symbolic element
                // like g[n](x) ..> x^n
                // and calling g[0++2]
                //  the output should be x^0 + x^1 + x^2
                //  and be parsed into function  (QsFunction)


                string porma = string.Empty;  // the parameters separated by comma ','
                foreach (var prm in this.Parameters)
                {
                    porma += prm.Name + ", ";
                }
                porma = porma.TrimEnd(',', ' ');

                string FunctionBody = JoinElementsWithOperation(fromIndex, toIndex, "+");

                string FunctionDeclaration = "_(" + porma + ") = " + FunctionBody;


                QsFunction qs = QsFunction.ParseFunction(QsEvaluator.CurrentEvaluator, FunctionDeclaration);

                return(new QsScalar(ScalarTypes.FunctionQuantity)
                {
                    FunctionQuantity = qs.ToQuantity()
                });
            }
            else
            {
                FixIndices(ref fromIndex, ref toIndex);

                QsValue Total = GetElementValue(fromIndex);

                for (int i = fromIndex + 1; i <= toIndex; i++)
                {
                    Total = Total + GetElementValue(i);
                }


                return(Total);
            }
        }
Exemplo n.º 11
0
        //
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        public QsFunction GetTestFunction(string function, params string[] paramNames)
        {
            List <QsParamInfo> lpi = new List <QsParamInfo>();

            foreach (string prm in paramNames)
            {
                lpi.Add(new QsParamInfo()
                {
                    Name = prm, Type = QsParamType.Value
                });
            }

            QsFunction target = new QsFunction(function);

            target.Parameters = lpi.ToArray();

            return(target);
        }
Exemplo n.º 12
0
        /// <summary>
        /// Take the average of the sequence.
        /// Corresponds To: S[i!!k]
        /// </summary>
        /// <param name="fromIndex"></param>
        /// <param name="toIndex"></param>
        /// <returns></returns>
        public QsValue Average(int fromIndex, int toIndex)
        {
            var n = toIndex - fromIndex + 1;

            if (this.Parameters.Length > 0)
            {
                // this is a call to form symbolic element
                // like g[n](x) ..> x^n
                // and calling g[0++2]
                //  the output should be x^0 + x^1 + x^2
                //  and be parsed into function  (QsFunction)


                string porma = string.Empty;  // the parameters separated by comma ','
                foreach (var prm in this.Parameters)
                {
                    porma += prm.Name + ", ";
                }
                porma = porma.TrimEnd(',', ' ');

                string FunctionBody = "(" + JoinElementsWithOperation(fromIndex, toIndex, "+") + ")/" + n.ToString(CultureInfo.InvariantCulture);

                string FunctionDeclaration = "_(" + porma + ") = " + FunctionBody;

                QsFunction qs = QsFunction.ParseFunction(QsEvaluator.CurrentEvaluator, FunctionDeclaration);

                return(qs);
            }
            else
            {
                FixIndices(ref fromIndex, ref toIndex);

                var tot = SumElements(fromIndex, toIndex);

                var count = new QsScalar {
                    NumericalQuantity = Qs.ToQuantity((double)n)
                };

                return(tot / count);
            }
        }
Exemplo n.º 13
0
        /// <summary>
        /// It tries to get the value of the given key.
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public object GetValue(string name)
        {
            if (_NamespaceType != null)
            {
                if (QsFunction.IsItFunctionSymbolicName(name))
                {
                    if (HardCodedMethods == null)
                    {
                        HardCodedMethods = GetQsNamespaceMethods();
                    }

                    if (HardCodedMethods.ContainsKey(name))
                    {
                        return(HardCodedMethods[name]);
                    }
                }
                else
                {
                    var prop = _NamespaceType.GetProperty(name, BindingFlags.IgnoreCase | BindingFlags.Static | BindingFlags.Public);
                    if (prop != null)
                    {
                        return(Root.NativeToQsConvert(prop.GetValue(null, null)));
                    }
                }
            }

            // try in values hash

            object o;

            Values.TryGetValue(name, out o);

            if (o == null)
            {
                throw new QsVariableNotFoundException("Variable '" + name + "' not found in '" + this.Name + "' namespace.");
            }
            return(o);
        }
Exemplo n.º 14
0
        /// <summary>
        /// Gets the members of the QsNamespace or (static class ) that hold the hard coded functions and properties.
        /// </summary>
        /// <param name="namespaceType"></param>
        /// <returns></returns>
        private Dictionary <string, object> GetQsNamespaceMethods()
        {
            // The namespace is a static class with public visibility to its static members.
            var methods = _NamespaceType.GetMethods(
                BindingFlags.IgnoreCase | BindingFlags.Static | BindingFlags.Public
                /*| BindingFlags.InvokeMethod */
                );

            Dictionary <string, object> CodedMembers =
                new Dictionary <string, object>(System.StringComparer.OrdinalIgnoreCase);

            /*
             * var FilteredMethods = from m in methods
             *                    where
             *                    m.IsSpecialName == false
             *
             *                    && m.ReturnType.IsArray == false &&
             *                    (m.ReturnType.BaseType == typeof(QsValue) || m.ReturnType == typeof(QsValue) ||
             *                    m.ReturnType == typeof(string) || m.ReturnType.IsValueType == true)
             *
             *                    select m;
             */
            var FilteredMethods = from m in methods
                                  where
                                  m.IsSpecialName == false
                                  select m;

            foreach (var member in FilteredMethods)
            {
                var method = ((MethodInfo)member);
                if (!method.IsSpecialName) //because properies getters are methods also :S
                {
                    ParameterInfo[] mps = method.GetParameters();


                    int paramCount = mps.Length;


                    var fAttributes = method.GetCustomAttributes(typeof(QsFunctionAttribute), false);

                    QsFunctionAttribute funcAttribute = null;
                    string method_name    = string.Empty;
                    string methodTrueName = string.Empty;

                    if (fAttributes.Length > 0)
                    {
                        funcAttribute = (QsFunctionAttribute)fAttributes[0];

                        // if the attribute were found treat the function as namedargument function that is not default function.
                        method_name = funcAttribute.FunctionName;

                        if (funcAttribute.DefaultScopeFunction == false)
                        {
                            // alter the name so it includes the parameters also
                            string[] ptext = (from p in mps select p.Name).ToArray();
                            methodTrueName = QsFunction.FormFunctionSymbolicName(method_name, ptext);
                        }
                        else
                        {
                            // don't use special parameter symbolic naming.
                            methodTrueName = QsFunction.FormFunctionSymbolicName(method_name, paramCount);
                        }

                        CodedMembers.Add(methodTrueName, GetQsFunctionFromTypeMethod(method, funcAttribute));
                    }
                    else
                    {
                        method_name    = method.Name;
                        methodTrueName = QsFunction.FormFunctionSymbolicName(method_name, paramCount);

                        CodedMembers.Add(methodTrueName, GetQsFunctionFromTypeMethod(method, null));
                    }
                }
            }

            return(CodedMembers);
        }
Exemplo n.º 15
0
        /// <summary>
        /// Gets the element quantity and employ cach
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public QsValue GetElementValue(int index)
        {
            QsValue val;

            if (CachingEnabled)
            {
                if (CachedValues.TryGetValue(index, out val))
                {
                    return(val);
                }
            }


            if (this.Parameters.Length > 0)
            {
                // this is a call to form symbolic element
                // like g[n](x) ..> x^n
                // and calling g[2]
                //  the output should be x^2
                //  and be parsed into function  (QsFunction)
                var e = GetElement(index);

                string se_text = e.ElementDeclaration.Replace("$" + this.SequenceIndexName, "`");
                se_text = se_text.Replace(this.SequenceIndexName, index.ToString(CultureInfo.InvariantCulture));
                se_text = se_text.Replace("`", "$" + this.SequenceIndexName);

                if (!string.IsNullOrEmpty(SequenceRangeStartName))
                {
                    se_text = se_text.Replace("$" + SequenceRangeStartName, "`");
                    se_text = se_text.Replace(SequenceRangeStartName, StartIndex.ToString(CultureInfo.InvariantCulture));
                    se_text = se_text.Replace("`", "$" + SequenceRangeStartName);
                }

                if (!string.IsNullOrEmpty(SequenceRangeEndName))
                {
                    se_text = se_text.Replace("$" + SequenceRangeEndName, "`");
                    se_text = se_text.Replace(SequenceRangeEndName, EndIndex.ToString(CultureInfo.InvariantCulture));
                    se_text = se_text.Replace("`", "$" + SequenceRangeEndName);
                }

                var FunctionBody = se_text;

                string porma = string.Empty;  // the parameters separated by comma ','
                foreach (var prm in this.Parameters)
                {
                    porma += prm.Name + ", ";
                }
                porma = porma.TrimEnd(',', ' ');

                string FunctionDeclaration = "_(" + porma + ") = " + FunctionBody;

                QsFunction qs = QsFunction.ParseFunction(QsEvaluator.CurrentEvaluator, FunctionDeclaration);

                return(new QsScalar(ScalarTypes.FunctionQuantity)
                {
                    FunctionQuantity = qs.ToQuantity()
                });
            }
            else
            {
                val = GetElement(index).Execute(index);
            }

            if (CachingEnabled)
            {
                CachedValues[index] = val;
            }

            return(val);
        }