定数×関数。
Inheritance: Function
Ejemplo n.º 1
0
        public override Function Add(Function f)
        {
            Multiple g = f as Multiple;

            if (g != null && this.inner.Equals(g.inner))
            {
                return(new Multiple(this.factor + g.factor, this.inner));
            }

            Variable v = f as Variable;

            if (v != null && this.inner.Equals(v))
            {
                return(new Multiple(this.factor + 1, this.inner));
            }

            Constant c = f as Constant;

            if (c != null && (this.inner is Constant))
            {
                Constant c0 = this.inner as Constant;
                return((Constant)(c.Value + this.factor * c0.Value));
            }

            return(base.Add(f));
        }
Ejemplo n.º 2
0
        public override bool Equals(object obj)
        {
            Multiple f = obj as Multiple;

            if (f == null)
            {
                return(false);
            }

            return((this.factor == f.factor) && this.inner.Equals(f.inner));
        }
Ejemplo n.º 3
0
        public override Function Divide(Function f)
        {
            Multiple g = f as Multiple;

            if (g != null)
            {
                return(new Multiple(this.factor / g.factor, this.inner / g.inner));
            }

            return(new Multiple(this.factor, this.inner / f));
        }
Ejemplo n.º 4
0
        public override Function Optimize()
        {
            if (this.factor == 0)
            {
                return((Constant)0);
            }

            Function f = this.inner.Optimize();

            if (this.factor == 1)
            {
                return(f);
            }

            Constant c = f as Constant;

            if (c != null)
            {
                return((Constant)(this.factor * c.Value));
            }

            Multiple m = f as Multiple;

            if (m != null)
            {
                return(new Multiple(this.factor * m.factor, m.inner));
            }

            Product p = f as Product;

            if (p != null)
            {
                Product tmp = p.Clone() as Product;
                tmp.AddList((Constant)this.factor);
                return(tmp.Optimize());
            }

            return(new Multiple(this.factor, f));
        }
Ejemplo n.º 5
0
        /*
         * public override Function Subtract(Function f)
         * {
         *      Multiple g = f as Multiple;
         *      if(g != null && this.inner.Equals(g.inner))
         *      {
         *              return new Multiple(this.factor - g.factor, this.inner);
         *      }
         *
         *      Variable v = f as Variable;
         *      if(v != null && this.inner.Equals(v))
         *      {
         *              return new Multiple(this.factor - 1, this.inner);
         *      }
         *
         *      Constant c = f as Constant;
         *      if(c != null && (this.inner is Constant))
         *      {
         *              Constant c0 = this.inner as Constant;
         *              return (Constant)(this.factor * c0.Value - c.Value);
         *      }
         *
         *      return base.Subtract(f);
         * }
         */

        public override Function Multiply(Function f)
        {
            Multiple g = f as Multiple;

            if (g != null)
            {
                return(new Multiple(this.factor * g.factor, this.inner * g.inner));
            }

            Constant c = f as Constant;

            if (c != null)
            {
                if (this.inner is Constant)
                {
                    Constant c0 = this.inner as Constant;
                    return((Constant)(this.factor * c0.Value * c.Value));
                }
                return(new Multiple(this.factor * c.Value, this.inner));
            }

            return(new Multiple(this.factor, this.inner * f));
        }
Ejemplo n.º 6
0
        /// <remarks>
        /// 同じ種類の関数ごとに掛けていった方が最適な構造が得られるため、
        /// 関数のリストを一度ソートしてから掛けなおす。
        /// あと、Constant とか Variable とか Function の辺りの乗算を最適化。
        /// </remarks>
        public override Function Optimize()
        {
            Hashtable table = new Hashtable();

            // 種類わけ
            foreach (Function f in this.functions)
            {
                Function g = f.Optimize();

                Type t = g.GetType();
                if (table[t] == null)
                {
                    table[t] = g;
                }
                else
                {
                    table[t] = (Function)table[t] * g;
                }
            }

            // 繋ぎなおす
            Constant c = table[typeof(Constant)] as Constant;

            if (c != null && c.Equals((Constant)0))
            {
                return((Constant)0);
            }

            Function v = table[typeof(Variable)] as Function;
            Multiple m = table[typeof(Multiple)] as Multiple;

            Function h = (Constant)1;

            if (v != null)
            {
                h = Sort(v);
                table.Remove(typeof(Variable));
            }
            if (c != null)
            {
                table.Remove(typeof(Constant));
            }
            if (m != null)
            {
                if (c == null)
                {
                    c = (Constant)m.Factor;
                }
                else
                {
                    c = (Constant)(c.Value * m.Factor);
                }
                h *= m.Inner;
                table.Remove(typeof(Multiple));
            }

            ArrayList func = new ArrayList();

            if (!h.Equals((Constant)1))
            {
                func.Add(h);
            }

            foreach (Function f in table.Values)
            {
                if (f is Product)
                {
                    Product p = f as Product;
                    func.AddRange(p.functions);
                }
                else
                {
                    if (!f.Equals((Constant)1))
                    {
                        func.Add(f);
                    }
                }
            }

            // 特殊な場合
            if (c == null)
            {
                if (func.Count == 0)
                {
                    return((Constant)1);
                }

                if (func.Count == 1)
                {
                    return(func[0] as Function);
                }
            }

            // 掛けなおす。
            h = func[0] as Function;
            for (int i = 1; i < func.Count; ++i)
            {
                h *= func[i] as Function;
            }

            if (c == null || c.Equals((Constant)1))
            {
                return(h);
            }
            return(new Multiple(c.Value, h));
        }