Example #1
0
        public static bool SimplifySummands(ISignalSet signals)
        {
            bool changed = CollectSummands(signals);

            if (signals.Count < 2)
            {
                return(changed);
            }
            IAccumulator acc = null;

            for (int i = signals.Count - 1; i >= 0; i--)
            {
                Signal s = signals[i];
                if (Std.IsConstantComplex(s))
                {
                    if (acc == null)
                    {
                        acc = Accumulator <IntegerValue, RationalValue> .Create(IntegerValue.AdditiveIdentity);
                    }
                    signals.RemoveAt(i);
                    changed = true;
                    acc     = acc.Add(s.Value);
                }
            }
            if (acc != null && !acc.Value.Equals(IntegerValue.AdditiveIdentity))
            {
                signals.Insert(0, Std.DefineConstant(acc.Value));
            }
            return(changed);
        }
Example #2
0
        public static bool SimplifyFactors(ISignalSet signals)
        {
            bool changed = CollectFactors(signals);

            if (signals.Count < 2)
            {
                return(changed);
            }
            IAccumulator acc = null;

            for (int i = signals.Count - 1; i > 0; i--)  //don't touch first item!
            {
                Signal s = signals[i];
                if (Std.IsConstantComplex(s))
                {
                    if (acc == null)
                    {
                        acc = Accumulator <IntegerValue, RationalValue> .Create(IntegerValue.MultiplicativeIdentity);
                    }
                    signals.RemoveAt(i);
                    changed = true;

                    if (Std.IsConstantAdditiveIdentity(s))
                    {
                        signals.Clear();
                        signals.Add(UndefinedSymbol.Constant);
                        return(true);
                    }
                    acc = acc.Multiply(s.Value);
                }
            }
            if (acc != null && !acc.Value.Equals(IntegerValue.MultiplicativeIdentity))
            {
                Signal first = signals[0];
                if (Std.IsConstantComplex(first))
                {
                    acc        = acc.Divide(first.Value).Invert();
                    signals[0] = Std.DefineConstant(acc.Value);
                    changed    = true;
                }
                else
                {
                    signals.Insert(1, Std.DefineConstant(acc.Value));
                }
            }
            return(changed);
        }
Example #3
0
        public static bool SimplifySummands(ISignalSet signals)
        {
            bool changed = CollectSummands(signals);

            if (signals.Count < 2)
            {
                return(changed);
            }
            IAccumulator acc = null;

            for (int i = signals.Count - 1; i > 0; i--)  //don't touch first item!
            {
                Signal s = signals[i];
                if (Std.IsConstantComplex(s))
                {
                    if (acc == null)
                    {
                        acc = Accumulator <IntegerValue, RationalValue> .Create(IntegerValue.AdditiveIdentity);
                    }
                    signals.RemoveAt(i);
                    changed = true;
                    acc     = acc.Add(s.Value);
                }
            }
            if (acc != null && !acc.Value.Equals(IntegerValue.AdditiveIdentity))
            {
                Signal first = signals[0];
                if (Std.IsConstantComplex(first))
                {
                    acc        = acc.Subtract(first.Value).Negate();
                    signals[0] = Std.DefineConstant(acc.Value);
                    changed    = true;
                }
                else
                {
                    signals.Insert(1, Std.DefineConstant(acc.Value));
                }
            }
            return(changed);
        }
Example #4
0
        public static bool SimplifyOperands(ISignalSet signals)
        {
            if (signals.Count < 2)
            {
                return(false);
            }

            bool   changed = false;
            Signal radix   = signals[0];

            if (Std.IsConstantAdditiveIdentity(radix) && Std.IsConstant(signals[1]))
            {
                if (Std.IsAlwaysNegative(signals[1]))
                {
                    signals.Clear();
                    signals.Add(UndefinedSymbol.Constant);
                    return(true);
                }
                signals.Clear();
                signals.Add(radix);
                return(true);
            }
            if (Std.IsConstantMultiplicativeIdentity(radix))
            {
                signals.Clear();
                signals.Add(radix);
                return(true);
            }

            // Evaluate all but the first two operands -> nonnegative integers
            IntegerValue value = null;

            for (int i = signals.Count - 1; i > 1; i--)
            {
                Signal s = signals[i];
                if (!Std.IsConstantNonnegativeInteger(s))
                {
                    if (changed)
                    {
                        if (!value.IsMultiplicativeIdentity)
                        {
                            signals.Add(Std.DefineConstant(value));
                        }
                        return(true);
                    }
                    return(false);
                }
                signals.RemoveAt(i);
                value = ValueConverter <IntegerValue> .ConvertFrom(s.Value).PositiveIntegerPower((int)value.Value);

                changed = true;
            }

            // Evaluate the second operand -> integer (can also be negative)
            Signal exponent = signals[1];

            if (!Std.IsConstantInteger(exponent))
            {
                if (changed)
                {
                    if (!value.IsMultiplicativeIdentity)
                    {
                        signals.Add(Std.DefineConstant(value));
                    }
                    return(true);
                }
                return(false);
            }
            IntegerValue exponentValue = ValueConverter <IntegerValue> .ConvertFrom(exponent.Value);

            if (changed)
            {
                exponentValue = exponentValue.PositiveIntegerPower((int)value.Value);
            }

            // Special case: first operand is a power
            if (radix.IsDrivenByPortEntity(PowerArchitectures.EntityIdentifier) &&
                radix.DrivenByPort.InputSignalCount == 2 &&
                Std.IsConstantInteger(radix.DrivenByPort.InputSignals[1]))
            {
                exponentValue = exponentValue.Multiply(ValueConverter <IntegerValue> .ConvertFrom(radix.DrivenByPort.InputSignals[1].Value));
                signals[0]    = radix = radix.DrivenByPort.InputSignals[0];
                changed       = true;
            }

            // Evaluate the first operand
            if (!Std.IsConstantComplex(radix))
            {
                if (changed)
                {
                    if (!exponentValue.IsMultiplicativeIdentity)
                    {
                        signals[1] = Std.DefineConstant(exponentValue);
                    }
                    else
                    {
                        signals.RemoveAt(1);
                    }
                    return(true);
                }
                return(false);
            }
            IAccumulator acc = Accumulator <IntegerValue, RationalValue> .Create(radix.Value);

            acc = acc.IntegerPower((int)exponentValue.Value);

            signals.Clear();
            signals.Add(Std.DefineConstant(acc.Value));
            return(true);
        }