コード例 #1
0
        public async static Task <List <Complex> > SolveEquation(string f)
        {
            Formula formula = await ExpandFormula.ExpandCharFormula(f);

            //数式に分母がないことを確認
            if (!(formula.DenMonof.IsOne() || formula.IsZero()))
            {
                //Util.Trace.WriteLine(formula.DenMonof[0].GetString());
                throw new Exception(AppResources.TooComplicatedX);
            }

            var chars = formula.GetCharDics();

            //変数が一つのみであることを確認
            if (chars.Count() != 1)
            {
                throw new Exception(AppResources.ManyOrFewVariables);
            }

            var target = chars.First().Key;

            //変数が関数やルートの中にないことを確認
            if (formula.GetTargetCharInfo(target).NumMonof.Any(x => x.NumFuncs.Count() != 0 ||
                                                               x.DenFuncs.Count() != 0 ||
                                                               x.NthRoots.Count() != 0 ||
                                                               x.Recipchara.Count() != 0))
            {
                try
                {
                    var resforms = await SolveAbout.SolveAboutX(formula, new Formula(new List <Monomial> {
                    }), 'x');

                    if (resforms.Count() != 0 && resforms.All(x => x.IsDouble()))
                    {
                        return((await resforms.Select(async x => new Complex(await x.ToDouble(), 0.0)).WhenAll()).ToList());
                    }
                    else
                    {
                        throw new Exception();
                    }
                }
                catch
                {
                    throw new Exception(AppResources.TooComplicatedX);
                }
            }

            var monos = formula.NumMonof;

            var maxDegree = monos.Max(x => x.Character.ContainsKey(target) ? x.Character[target] : 0);

            var Coefficients = Enumerable.Repeat(0.0, maxDegree + 1).ToList();

            foreach (var m in monos)
            {
                if (!m.Character.ContainsKey(target))
                {
                    Coefficients[0] = m.Number.ToDouble();
                }
                else
                {
                    Coefficients[m.Character[target]] = m.Number.ToDouble();
                }
            }

            return(SolveEquation(Coefficients));
        }
コード例 #2
0
 /// <summary>
 /// 多項式(ExpandFormula.ExpandCharFormulaしてから代入)
 /// </summary>
 /// <param name="f">式</param>
 public async static Task <Formula> CreateByStr(string f)
 {
     return(await ExpandFormula.ExpandCharFormula(f));
 }