コード例 #1
0
ファイル: FunctionFormula.cs プロジェクト: takku205/FuncCalc
        private static IExpression GetMember(RuntimeData runtime, IExpression parent, Member searchToken)
        {
            // まずは変数を検索する
            if (parent == null)
            {
                if (runtime.ContainsKey(searchToken.Text))
                {
                    return(runtime.GetData(searchToken.Token));
                }
            }

            // 親メンバーから検索
            if (parent != null)
            {
                throw new NotImplementedException();
            }

            // フィールドメンバーから検索
            if (parent == null)
            {
                if (runtime.ContainsFunc(searchToken.Text))
                {
                    return(runtime.GetFunc(searchToken, false));
                }
            }

            // 不明なものはとりあえず変数と見立てる
            if (parent == null)
            {
                return(new Variable(searchToken.Token));
            }

            throw new SyntaxException(string.Format("'{0}'は見つかりませんでした", searchToken.Text),
                                      searchToken.Token, new KeyNotFoundException());
        }
コード例 #2
0
        public override INumber FinalEval(RuntimeData runtime)
        {
            var res = runtime.GetData(this.Token).FinalEval(runtime);

            res = res.Power(runtime, this.Pow);
            return(res);
        }
コード例 #3
0
ファイル: Variable.cs プロジェクト: takku205/FuncCalc
        public override INumber FinalEval(RuntimeData runtime)
        {
            var res = runtime.GetData(this.Token);

            // resをFinalEvalする前にresがVariableじゃないことを確認する
            if (res is Variable)
            {
                return(this);
            }
            res = res.FinalEval(runtime);

            res = res.Power(runtime, this.Pow);
            return(res);
        }
コード例 #4
0
        public override INumber Eval(RuntimeData runtime)
        {
            if (runtime.ContainsKey(this.Text))
            {
                var res = runtime.GetData(this.Token).Eval(runtime).Clone();
                res = res.Power(runtime, this.Pow);
                return(res);
            }

            // 変数として処理する
            // その際に変数を評価して渡す
            return(GetVariable(runtime).Eval(runtime));

            throw new RuntimeException(string.Format("'{0}'は見つかりませんでした。", this.Text), this.Token);
        }
コード例 #5
0
ファイル: Variable.cs プロジェクト: takku205/FuncCalc
 public override INumber Eval(RuntimeData runtime)
 {
     if (runtime.ContainsKey(this.Token.Text))
     {
         var res = runtime.GetData(this.Token);
         res = res.Multiple(runtime, this.multi);
         res = res.Power(runtime, this.Pow);
         if (res == this)
         {
             return(this);
         }
         return(res.Eval(runtime));
     }
     else
     {
         return(this);
     }
 }
コード例 #6
0
        public override INumber Integrate(RuntimeData runtime, string t)
        {
            // このメンバーの名前がtと同じだった場合
            if (this.Text == t)
            {
                if (this.Pow is IConstParameter)
                {
                    // x^a

                    // 1/(a + 1)
                    MultipleFormula mf = new Expression.MultipleFormula();
                    mf.AddItem(runtime, new Fraction(this.Pow.Add(runtime, Number.New(1)), Number.New(1)));

                    // x~(a+1)
                    var cln = this.Clone(); cln.Pow = cln.Pow.Add(runtime, Number.New(1));
                    mf.AddItem(runtime, cln);

                    return(mf);
                }
                else
                {
                    throw new NotImplementedException("べき乗が定数ではない変数の積分は未実装です。");
                }
            }
            else     // このメンバーの名前がtとは違う場合

            {
                var res = runtime.GetData(this.Token);
                if (res is Member)
                {
                    throw new NotImplementedException("話が違う");
                }

                if (res is Variable)
                {
                    if ((res as Variable).Name == this.Text)
                    {
                        // yをxで微分しようとしている場合

                        // tとres(それぞれ違う名前の変数)がそれぞれ関係ないならば成り立つ
                        runtime.AddLogCondition("UnrelationWith", new Variable(t), res);

                        // resを定数として扱って積分する
                        // 参考: (2)~(3)の部分 http://www.geisya.or.jp/~mwm48961/electro/multi_integral3.htm
                        MultipleFormula mf = new Expression.MultipleFormula();
                        mf.AddItem(runtime, res);
                        mf.AddItem(runtime, new Variable(t));
                        return(mf);
                    }
                    else
                    {
                        // yからまた別の変数が出てきちゃった場合
                        return(res.Integrate(runtime, t));
                    }
                }
                else
                {
                    return(res.Integrate(runtime, t));
                }
            }
        }