Esempio n. 1
0
        public override FormulaItem Clone(List <SymbolValuePair> values)
        {
            FunctionSum cloned = (FunctionSum)base.Clone(values);

            foreach (double coeff in Coeffs)
            {
                cloned.Coeffs.Add(coeff);
            }

            return(cloned);
        }
Esempio n. 2
0
        public override bool TryCreate(string pattern, FormulaBuilder builder, out FormulaItem item)
        {
            item = null;

            List <string> elements = FormulaBuilderTools.Instance.GetElements(pattern, OperationType.AddSubstr);

            if (elements.Count < 3)
            {
                return(false);
            }

            item         = new FunctionSum();
            item.Pattern = pattern;

            for (int i = 0; i < elements.Count; ++i)
            {
                string element = elements[i];
                if (element == "+" || element == "-")
                {
                    continue;
                }


                double coeff = 1.0;
                if (i > 0)
                {
                    coeff = (elements[i - 1] == "+") ? 1.0 : -1.0;
                }

                FormulaItem arg = builder.Create(element);
                (item as FunctionSum).Arguments.Add(arg);
                (item as FunctionSum).Coeffs.Add(coeff);
            }

            return(true);
        }
Esempio n. 3
0
        public Element Convert(FormulaItem item)
        {
            Element element = null;

            if (item is ValueItem)
            {
                ValueItem    i = (ValueItem)item;
                ValueElement e = new ValueElement();
                e.Value = i.Value.ToString();
                element = e;
            }

            if (item is SymbolItem)
            {
                SymbolItem    i = (SymbolItem)item;
                SymbolElement e = new SymbolElement();

                if (!string.IsNullOrEmpty(i.Symbol))
                {
                    string[] splited = i.Symbol.Split('.');
                    e.Symbol = splited[0];
                    if (splited.Length == 2)
                    {
                        e.Suffix = splited[1];
                    }
                }

                element = e;
            }

            if (item is UnitItem)
            {
                UnitItem    u = (UnitItem)item;
                UnitElement e = new UnitElement();
                e.Unit  = u.Unit;
                element = e;
            }



            if (item is FunctionSum)
            {
                FunctionSum sum     = (FunctionSum)item;
                SumElement  sumElem = new SumElement();

                for (int i = 0; i < sum.Coeffs.Count; ++i)
                {
                    if (i == 0)
                    {
                        sumElem.Signs.Add(string.Empty);
                        continue;
                    }
                    double coeff = sum.Coeffs[i];
                    string sign  = (coeff > 0) ? "+" : "-";
                    sumElem.Signs.Add(sign);
                }

                element = sumElem;
            }

            if (item is FunctionMultiply)
            {
                FunctionMultiply multi = (FunctionMultiply)item;

                if (multi.Arguments.Count == 2 && multi.Flags[1] == false)
                {
                    FractionElement fraction = new FractionElement();
                    element = fraction;
                }
                else
                {
                    SumElement sumElem = new SumElement();

                    for (int i = 0; i < multi.Flags.Count; ++i)
                    {
                        if (i == 0)
                        {
                            sumElem.Signs.Add(string.Empty);
                            continue;
                        }
                        bool   flag = multi.Flags[i];
                        string sign = flag ? "x" : "/";
                        sumElem.Signs.Add(sign);
                    }

                    element = sumElem;
                }
            }

            if (item is FunctionSqrt)
            {
                element = new SqrtElement();
            }

            if (item is FunctionPow)
            {
                element = new PowElement();
            }

            if (item is FunctionMax)
            {
                element = createElement("max");
            }

            if (item is FunctionMin)
            {
                element = createElement("min");
            }

            if (item is FunctionSin)
            {
                element = createElement("sin");
            }

            if (item is FunctionCos)
            {
                element = createElement("cos");
            }

            if (item is FunctionTan)
            {
                element = createElement("tan");
            }


            if (item is BracketItem)
            {
                element = createElement("");
            }



            if (item is FunctionItem)
            {
                FunctionItem function = (FunctionItem)item;
                foreach (FormulaItem arg in function.Arguments)
                {
                    Element argElement = Convert(arg);
                    element.Arguments.Add(argElement);
                }
            }

            return(element);
        }