예제 #1
0
        /// <summary>
        /// 一次方程式を解決する
        /// </summary>
        private INumber SolutionLinearEquation(RuntimeData runtime, string varname, Dictionary <int, INumber> keisu)
        {
            if (!(keisu.Keys.Where(k => k >= 2).Count() == 0 &&              // x^3のような大きなべき乗がない
                  keisu.ContainsKey(1) &&                                    // xが含まれている
                  !(keisu[1] is Number && (keisu[1] as Number).Value == 0))) // x^2の係数が0ではない
            {
                return(null);
            }

            runtime.AddLogWay("AsLinerEquation", keisu[1], keisu[0]);

            // ax + b = 0の時
            // 解: -(b / a)
            var res1 = keisu[0].Divide(runtime, keisu[1]).Multiple(runtime, Number.New(-1));

            var vr = new Variable(varname);

            runtime.AddLogWay("_LinerEquationWay1", vr, res1);

            Results res = new Results(new Variable(varname));

            res.items.Add(res1);

            return(res);
        }
예제 #2
0
        /// <summary>
        /// 二次方程式を解決する
        /// </summary>
        private INumber SolutionQuadraticEquation(RuntimeData runtime, string varname, Dictionary <int, INumber> keisu)
        {
            if (!(keisu.Keys.Where(k => k >= 3).Count() == 0 &&                    // x^3のような大きなべき乗がない
                  keisu.Values.Where(v => !(v is IConstParameter)).Count() == 0 && // 係数が全て実数である
                  keisu.ContainsKey(2) &&                                          // x^2が含まれている
                  !(keisu[2] is Number && (keisu[2] as Number).Value == 0)))       // x^2の係数が0ではない
            {
                return(null);
            }

            runtime.AddLogWay("AsQuadraticEquation", keisu[2], keisu[1], keisu[0]);

            INumber res1 = null, res2 = null;

            try {
                runtime.AddBlock(new BlockData()
                {
                    MoreScope = false
                });
                runtime.NowBlock.Variables.Add("a", keisu[2]);
                runtime.NowBlock.Variables.Add("b", keisu[1]);
                runtime.NowBlock.Variables.Add("c", keisu[0]);

                // ( -b ± √(b^2 - 4ac)) / 2a
                res1 = (runtime.Setting.GetExpression("((0-b)+sqrt(b^2-4a*c))/(2a)") as IEval).Eval(runtime);
                res2 = (runtime.Setting.GetExpression("((0-b)-sqrt(b^2-4a*c))/(2a)") as IEval).Eval(runtime);

                var vr = new Variable(varname);
                runtime.AddLogWay("_QuadraticEquationWay1", vr, res1);
                runtime.AddLogWay("_QuadraticEquationWay2", vr, res2);
            }
            finally { runtime.PopBlock(); }

            if (res1 == null && res2 == null)
            {
                return(null);
            }

            Results ress = new Results(new Variable(varname));

            ress.items.Add(res1);
            if (!res1.Equals(runtime, res2))
            {
                ress.items.Add(res2);
            }
            return(ress);
        }
예제 #3
0
        public override INumber Execute(RuntimeData runtime, params INumber[] parameters)
        {
            var res = parameters[1].Integrate(runtime, parameters[0].Token.Text);

            var             c  = IntegralConstant.Create(runtime);
            AdditionFormula af = new AdditionFormula();

            af.AddItem(runtime, res);
            af.AddItem(runtime, c);

            runtime.AddLogWay("Equal", new FuncedINumber(this, parameters), af);
            runtime.AddLogCondition("IntegralConstant", c);
            runtime.SetVariable(runtime, new Variable(c.ToString()), c);

            return(af);
        }