Example #1
0
        public void ToRealTest()
        {
            var x = Infix.ParseOrUndefined("1/3");

            var d = x.ToReal().Round(2);

            Assert.AreEqual(0.33, d);
        }
Example #2
0
        private void calculateFuncAi(string sFunction, double x)
        {
            var e      = Infix.ParseOrUndefined(sFunction);
            var result = Evaluate.Evaluate(new Dictionary <string, FloatingPoint> {
                { "x", x }
            }, e);

            _model.funcAi = result.RealValue;
        }
        public bool isValidFunction(string sFunction)
        {
            if (string.IsNullOrEmpty(sFunction))
            {
                throw new ArgumentNullException("sFunction");
            }

            return(Infix.ParseOrUndefined(sFunction) != Expression.Undefined);
        }
Example #4
0
        public double GetValue(IPoint p)
        {
            Dictionary <string, FloatingPoint> values = new Dictionary <string, FloatingPoint>();

            for (int i = 0; i < dimentions; ++i)
            {
                values.Add("X" + (i + 1).ToString(), p.GetPointOnAxis(i));
            }
            return(Evaluate.Evaluate(values, Infix.ParseOrUndefined(function_string)).RealValue);
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Please insert a function with x as dependant variable: ");
            string     fstring = Console.ReadLine();
            Expression f       = Infix.ParseOrUndefined(fstring);

            Newton(f, 4f, (float)(10 * Math.Pow(10, -10)), 20);
            Secant(f, 1f, 4f, (float)(10 * Math.Pow(10, -10)), 20);
            Multiple_Roots(f, 4f, (float)(10 * Math.Pow(10, -10)), 20);
            Console.Read();
        }
        internal bool IsSolution(string answerInfix)
        {
            var answerExpr = Infix.ParseOrUndefined(answerInfix);

            if (answerExpr.IsUndefined)
            {
                return(false);
            }
            else
            {
                return(answerExpr.Equals(Infix.ParseOrThrow(SolutionInfix)));
            }
        }
        public static Func <int, int, double> ParseInput(string input)
        {
            var e = Expr.Symbol("edges");
            var v = Expr.Symbol("vertices");

            return((vertices, edges) =>
            {
                var symbols = new Dictionary <string, FloatingPoint>
                {
                    { "vertices", vertices },
                    { "edges", edges }
                };

                return Evaluate.Evaluate(symbols, Infix.ParseOrUndefined(input)).RealValue;
            });
        }
Example #8
0
        public void FunctionTest_PasreAndEval()
        {
            var symbols = new Dictionary <string, FloatingPoint>
            {
                { "X1", 2d },
                { "X2", 2d },
                { "X3", 2d }
            };
            string    function = "1/(X1*X2*X3)";
            IFunction func     = new MathCore.Function(function, 3);

            //проеряем, что результат вычисления функции и реализации из библиотеки совпадают
            Assert.AreEqual(func.GetValue(new Point(new List <double> {
                2d, 2d, 2d
            })), Evaluate.Evaluate(symbols, Infix.ParseOrUndefined(function)).RealValue);
        }
        public double EvaluateInX(string sFunction, double x)
        {
            if (!isValidFunction(sFunction))
            {
                throw new Exception("Error de sintaxis en la ecuación: " + sFunction);
            }

            var e      = Infix.ParseOrUndefined(sFunction);
            var result = Evaluate.Evaluate(new Dictionary <string, FloatingPoint> {
                { "x", x }
            }, e);

            if (!result.IsReal)
            {
                throw new Exception("El resultado no es un número real");
            }

            return(result.RealValue);
        }
Example #10
0
 public static Expression P(this object str)
 {
     return(Infix.ParseOrUndefined(str.ToString()));
 }
Example #11
0
 public bool isValidFunction(string sFunction)
 {
     return(Infix.ParseOrUndefined(sFunction) != Expression.Undefined);
 }
 internal static string InfixToLatex(string infix)
 {
     return(LaTeX.Format(Infix.ParseOrUndefined(infix)));
 }
Example #13
0
        /// <summary>
        /// 去读配置项重新计算结果,更新数据
        /// </summary>
        public static void ReCalculation(string projectId)
        {
            var list = DAL.CommonDAL.GetProjectItemList(projectId);

            var tongjiList = new List <Model.Tongji>();

            #region 加载配置
            Model.SysConfig config = ReadConfig();
            if (config == null || string.IsNullOrEmpty(config.HanLiang))
            {
                return;
            }
            #endregion

            #region 计算含量
            Action <Model.DrugProjectItem> countHL = (Model.DrugProjectItem s) =>
            {
                try
                {
                    var dic = new Dictionary <string, FloatingPoint>()
                    {
                        { "供试峰面积", float.Parse(s.PJSFMJ) },
                        { "供试称样量", float.Parse(s.GSCYL) },
                        { "稀释倍数", float.Parse(s.XSBS) },
                        { "对照浓度", float.Parse(s.DZLD) },
                        { "对照峰面积", float.Parse(s.DZFMJ) }
                    };
                    FloatingPoint value = Evaluate.Evaluate(dic, Infix.ParseOrUndefined(config.HanLiang));
                    s.HL = Math.Round(value.RealValue, config.HanLiangPoint, MidpointRounding.AwayFromZero).ToString();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            };
            #endregion

            #region 计算平均含量和方差

            list.ForEach(s => countHL(s));

            var yplist = list.Where(s => s.type.Trim() == Model.DrugType.饮片.ToString());
            var tjlist = list.Where(s => s.type.Trim() == Model.DrugType.汤剂.ToString());

            Action <string, double, bool> addTongji = (string key, double PJHL, bool isYp) =>
            {
                if (isYp)
                {
                    if (tongjiList.Any(s => s.GroupName == key))
                    {
                        tongjiList.Where(s => s.GroupName == key).FirstOrDefault().YPHL = PJHL;
                    }
                    else
                    {
                        tongjiList.Add(new Model.Tongji()
                        {
                            GroupName = key, YPHL = PJHL
                        });
                    }
                }
                else
                {
                    if (tongjiList.Any(s => s.GroupName == key))
                    {
                        tongjiList.Where(s => s.GroupName == key).FirstOrDefault().TJHL = PJHL;
                    }
                    else
                    {
                        tongjiList.Add(new Model.Tongji()
                        {
                            GroupName = key, TJHL = PJHL
                        });
                    }
                }
            };

            Action <List <Model.DrugProjectItem>, bool> countPJHL = (List <Model.DrugProjectItem> models, bool fc) =>
            {
                foreach (var item in models.GroupBy(s => s.CodeNum1))
                {
                    double          PJHL   = 0;
                    int             lastId = 0;
                    List <FcEntity> fclist = new List <Win.FcEntity>();
                    int             index  = 0;
                    foreach (var sitem in item)
                    {
                        PJHL  += Convert.ToDouble(sitem.HL);
                        lastId = sitem.ID;
                        if (index > 1)
                        {
                            index = 0;
                        }
                        fclist.Add(new Win.FcEntity()
                        {
                            HL = Convert.ToDouble(sitem.HL), ID = sitem.ID
                        });
                    }
                    if (lastId != 0 && item.Count() > 1)
                    {
                        PJHL = Math.Round(PJHL / item.Count(), config.PingJunHLPoint, MidpointRounding.AwayFromZero);
                        list.Where(s => s.ID == lastId).FirstOrDefault().PJHL = PJHL.ToString();
                        addTongji(item.Key, PJHL, fc);
                        if (fc)
                        {
                            if (fclist.Count >= 2)
                            {
                                double fac = 0;
                                fclist = fclist.OrderBy(s => s.ID).ToList();
                                for (int i = 1; i < fclist.Count; i++)
                                {
                                    fac = fclist[i - 1].HL - fclist[i].HL;
                                }
                                list.Where(s => s.ID == lastId).FirstOrDefault().FC = Math.Round(fac, config.FangChaPoint, MidpointRounding.AwayFromZero).ToString();
                            }
                        }
                    }
                }
            };

            countPJHL(yplist.Where(s => s.IsFuCe == "False").ToList(), true);
            countPJHL(yplist.Where(s => s.IsFuCe != "False").ToList(), true);

            countPJHL(tjlist.Where(s => s.IsFuCe == "False").ToList(), false);
            countPJHL(tjlist.Where(s => s.IsFuCe != "False").ToList(), false);

            #endregion

            #region 统计数据
            foreach (var item in tongjiList)
            {
                item.ZYLv = Math.Round((item.TJHL / item.YPHL) * 100, 2, MidpointRounding.AwayFromZero);
            }
            DAL.CommonDAL.AddProjectTongji(tongjiList, projectId);
            #endregion

            #region  计算完毕更新数据库

            foreach (var item in list)
            {
                DAL.CommonDAL.UpdateProjectItem(item);
            }
            #endregion
        }
Example #14
0
        public string GetSqrt(string espression)
        {
            string result      = String.Empty;
            string AddToResult = "";

            espression = espression.Replace('.', ',');
            double num;

            if (Double.TryParse(espression, out num))
            {
                if (num < 0 && num % 1 != 0)
                {
                    espression  = espression.Substring(1);
                    AddToResult = "*i";
                }
            }
            espression = espression.Replace(',', '.');


            var expression = Infix.ParseOrUndefined("sqrt(" + espression + ")");


            if (expression == Expression.Undefined)
            {
                throw new SqrtException("Unable to parse expression");
            }

            try
            {
                var sub_result = Evaluate.Evaluate(null, expression);

                if (sub_result.IsReal)
                {
                    if (sub_result.RealValue != 0)
                    {
                        result = "+/- " + sub_result.RealValue.ToString();
                    }
                    else
                    {
                        result = "0";
                    }
                }
                else if (sub_result.IsComplex)
                {
                    result = sub_result.ComplexValue.Real.ToString() + " + i*" + sub_result.ComplexValue.Imaginary.ToString();
                }
                else
                {
                    result = sub_result.ToString();
                }
            }
            catch (NullReferenceException ex)
            {
                result = Infix.Format(Calculus.Taylor(5, Infix.ParseOrThrow(espression), Infix.ParseOrThrow("1"), expression)) + " + ...";
            }
            int i = 0;

            while (i < result.Length)
            {
                if (result[i] == ',')
                {
                    int j = i + 1;
                    for (; j - i < _accuracy && j < result.Length && Char.IsNumber(result[j]); j++)
                    {
                    }
                    i = j + 1;
                    for (; j < result.Length && Char.IsNumber(result[j]); j++)
                    {
                    }
                    if (_accuracy == 0)
                    {
                        i = i - 2;
                    }
                    result = result.Remove(i, j - i);
                }
                else
                {
                    i++;
                }
            }
            return(result + AddToResult);
        }