Exemplo n.º 1
0
        INumber CheckValue(Variable var, INumber left, INumber right)
        {
            // 左辺に変数を持ってくるように調整する
            INumber l = right.Clone();
            INumber r = right.Clone();

            // 左辺を右辺に持っていく
            throw new NotImplementedException();
        }
Exemplo n.º 2
0
        public INumber Build(INumber referenceNumber, int unitsToReplace, int unitsToShuffle)
        {
            var newNumber = (INumber)referenceNumber.Clone();
            var random = new Random();

            foreach (var unitToInclude in this.unitsToInclude)
            {
                newNumber.EditUnit(unitToInclude.Position, unitToInclude.Value);
            }

            this.ReplaceUnits(newNumber, unitsToReplace, random);

            var newUnitsToShuffle = referenceNumber.Units.Count() - unitsToReplace - this.unitsToInclude.Count;

            if (newUnitsToShuffle > 0)
            {
                if (unitsToShuffle < newUnitsToShuffle)
                {
                    newUnitsToShuffle = unitsToShuffle;
                }

                var positionsToShuffle = newNumber.Units
                    .Where(u => !this.unitsToInclude.Select(x => x.Position).Any(p => p == u.Position))
                    .Where(u => referenceNumber.Units.Any(x => x.Value == u.Value))
                    .Take(unitsToShuffle)
                    .Select(u => u.Position)
                    .ToList();

                this.ShuffleUnits(newNumber, positionsToShuffle, random);
            }

            return newNumber;
        }
Exemplo n.º 3
0
        public override INumber Multiple(RuntimeData runtime, INumber val)
        {
            if (val is Fraction)
            {
                var f = val.Clone() as Fraction;
                f.Numerator = f.Numerator.Multiple(runtime, this);
                return(f);
            }
            if (val is Number && (val as Number).Value == 0)
            {
                return(Number.New(0));
            }

            if (val is Number || val is Variable || val is Member)
            {
                var me = this.Clone() as MultipleFormula;
                me.AddItem(runtime, val);
                return(me);
            }
            else if (val is AdditionFormula)
            {
                var res = this.Clone() as MultipleFormula;
                res.AddItem(runtime, val);
                return(res);
            }
            else if (val is MultipleFormula)
            {
                var res = new MultipleFormula();
                var v   = val as MultipleFormula;
                for (int i = 0; i < this.items.Count; i++)
                {
                    bool flag = false;
                    for (int j = 0; j < v.items.Count; j++)
                    {
                        if (this.items[i].CanJoin(runtime, v.items[j]))
                        {
                            res.AddItem(runtime, this.items[i].Multiple(runtime, v.items[j]));
                            flag = true; break;
                        }
                    }
                    if (!flag) // ConJoinに引っかからなかった
                    {
                        res.AddItem(runtime, this.items[i]);
                    }
                }
                return(res);
            }
            else
            {
                var me = this.Clone() as MultipleFormula;
                me.AddItem(runtime, val);
                return(me);
            }
        }
Exemplo n.º 4
0
        public override INumber Execute(RuntimeData runtime, params INumber[] parameters)
        {
            INumber left = parameters[0], right = parameters[1];

            // 無効パラメータはここで弾く
            if (left is Fraction || left is AdditionFormula || left is MultipleFormula ||
                left is InfinityValue || left is Variable || left is Member)
            {
                throw new RuntimeException(string.Format("gcd関数では使用できないパラメータ '{0}'が含まれています。", left.GetType().Name), left);
            }

            if (right is Fraction || right is AdditionFormula || right is MultipleFormula ||
                right is InfinityValue || right is Variable || right is Member)
            {
                throw new RuntimeException(string.Format("gcd関数では使用できないパラメータ '{0}'が含まれています。", right.GetType().Name), right);
            }

            if (left is Number && right is Number)
            {
                // ユークリッドの互除法を使用して求める
                Number l = (left.Clone()) as Number, r = (right.Clone()) as Number;
                // 並び替える
                if (l.Value > r.Value)
                {
                    Number d = l; l = r; r = d;
                }

                // 計算する
                Number res = r.Clone() as Number;
                for (;;)
                {
                    res.Value = r.Value % l.Value;
                    if (res.Value == 0)
                    {
                        res = l;
                        break;
                    }
                    else
                    {
                        r.Value = l.Value;
                        l.Value = res.Value;
                    }
                }
                return(res);
            }
            else
            {
                throw new NotImplementedException();
            }
        }
Exemplo n.º 5
0
 public override INumber Add(RuntimeData runtime, INumber val)
 {
     if (val is AdditionFormula)
     {
         var af = val.Clone() as AdditionFormula;
         af.AddItem(runtime, this);
         return(af);
     }
     else
     {
         AdditionFormula af = new AdditionFormula();
         af.AddItem(runtime, val);
         af.AddItem(runtime, this);
         return(af);
     }
 }
Exemplo n.º 6
0
        private INumber DiffExecute(RuntimeData runtime, string t, INumber m, INumber param0, bool kaiki)
        {
            if (m is IConstParameter)
            {
                return(Number.New(0));
            }

            if (m is Variable)
            {
                Variable v = m.Clone() as Variable;
                if (v.Name == t)
                {
                    if (v.Pow.Equals(runtime, Number.New(1)))
                    {
                        return(v.Multi);
                    }
                    INumber multiple   = v.Multi.Multiple(runtime, v.Pow);
                    bool    powFormula = !(v.Pow is IConstParameter);
                    var     pow        = v.Pow;
                    v.Pow   = v.Pow.Subtract(runtime, Number.New(1));
                    v.Multi = multiple;

                    if (powFormula)
                    {
                        MultipleFormula mf = new Expression.MultipleFormula();
                        mf.AddItem(runtime, v);
                        mf.AddItem(runtime, ExecuteDiff(runtime, pow, t, param0));
                        return(mf);
                    }
                    else
                    {
                        if (runtime.Setting.DoOptimize)
                        {
                            return(v.Optimise(runtime));
                        }
                        else
                        {
                            return(v);
                        }
                    }
                }
                else
                {
                    if (kaiki)
                    {
                        throw new FunctionExecutionCancelException();
                    }

                    // y*(y')みたいな形にする
                    MultipleFormula mf = new Expression.MultipleFormula();
                    FunctionFormula ff = new Expression.FunctionFormula();
                    Formula         f  = new Formula()
                    {
                        Token = new Token("(", Analyzer.TokenType.Syntax)
                    };
                    var d = new Variable(new FuncCalc.Token(t, Analyzer.TokenType.Member));
                    mf.Items.Add(m);
                    ff.AddItem(runtime, new Member(new Token("diff", Analyzer.TokenType.Member)));
                    f.AddItem(runtime, d);
                    f.AddItem(runtime, new LineBreak(new Token(",", Analyzer.TokenType.Syntax)));
                    f.AddItem(runtime, v);
                    ff.AddItem(runtime, f);
                    mf.Items.Add(ff); // AddItemを使うと無限ループ
                    // return mf;
                    return(ff);
                }
            }

            throw new NotImplementedException();
        }
Exemplo n.º 7
0
        public INumber Build(INumber referenceNumber, int unitsToReplace, IEnumerable<INumberUnit> unitsToAdd)
        {
            var newNumber = (INumber)referenceNumber.Clone();
            var random = new Random();

            foreach (var unitToInclude in this.unitsToInclude)
            {
                newNumber.EditUnit(unitToInclude.Position, unitToInclude.Value);
            }

            var usedValues = new List<int>();

            foreach (var unitToAdd in unitsToAdd)
            {
                usedValues.Add(newNumber.GetUnitValue(unitToAdd.Position));

                newNumber.EditUnit(unitToAdd.Position, unitToAdd.Value);
            }

            var lockedPositions = unitsToAdd.Select(u => u.Position).ToList();
            var originalLockedPositions = new List<int>();

            originalLockedPositions.AddRange(lockedPositions);

            this.ReplaceUnits(newNumber, unitsToReplace, lockedPositions, usedValues, random);

            var unitsToShuffle = referenceNumber.Units.Count() - unitsToReplace - unitsToAdd.Count() - this.unitsToInclude.Count;

            if (unitsToShuffle > 0)
            {
                var positionsToShuffle = newNumber.Units
                    .Where(u => !this.unitsToInclude.Select(x => x.Position).Any(p => p == u.Position))
                    .Where(u => !originalLockedPositions.Any(p => p == u.Position))
                    .Where(u => referenceNumber.Units.Any(x => x.Value == u.Value))
                    .Take(unitsToShuffle)
                    .Select(u => u.Position)
                    .ToList();

                this.ShuffleUnits(newNumber, positionsToShuffle, originalLockedPositions, random);
            }

            return newNumber;
        }