Пример #1
0
        /// <summary>
        /// Returns the coefficient u[exponent] of x^exponent in the polynomial <see cref="signal"/>
        /// </summary>
        /// <returns>
        /// Constant UndefinedSymbol if <see cref="signal"/> is not a single-variable polynomial.
        /// Constant <see cref="IntegerValue.Zero"/> if there's no summand with the given exponent.
        /// Otherwise the sum of all coefficient factors of the term with the given exponent.
        /// </returns>
        /// <remarks><see cref="signal"/> is assumed to be automatic simplified.</remarks>
        public static Signal PolynomialCoefficient(Signal signal, Signal variable, int exponent)
        {
            IValueStructure vs;
            Signal          c = MonomialCoefficient(signal, variable, out vs);

            if (!(vs is UndefinedSymbol))
            {
                IntegerValue iv = vs as IntegerValue;
                if (iv == null || iv.Value == exponent)
                {
                    return(c);
                }
                else
                {
                    return(IntegerValue.ConstantZero);
                }
            }
            if (signal.IsDrivenByPortEntity("Add", "Std"))
            {
                ISignalSet    inputs = signal.DrivenByPort.InputSignals;
                List <Signal> coeffs = new List <Signal>(4);
                for (int i = 0; i < inputs.Count; i++)
                {
                    c = MonomialCoefficient(inputs[i], variable, out vs);
                    IntegerValue iv = vs as IntegerValue;
                    if (iv != null && iv.Value == exponent)
                    {
                        coeffs.Add(c);
                    }
                }
                return(Std.Add(coeffs));
            }
            return(UndefinedSymbol.Constant);
        }
Пример #2
0
        public static Signal ConstructPolynomial <TCoeff>(Signal variable, params TCoeff[] coefficients)
            where TCoeff : IAlgebraicRingWithUnity <TCoeff>, IValueStructure
        {
            Signal zero = null;

            Signal[] summands = new Signal[coefficients.Length];
            for (int i = 0; i < summands.Length; i++)
            {
                TCoeff c = coefficients[i];
                if (c.IsAdditiveIdentity)
                {
                    if (zero == null)
                    {
                        zero = Std.DefineConstant(c);
                    }
                    summands[i] = zero;
                    continue;
                }

                Signal coeff = Std.DefineConstant(c);

                if (i == 0)
                {
                    summands[0] = coeff;
                }
                else if (i == 1)
                {
                    if (c.IsMultiplicativeIdentity)
                    {
                        summands[1] = variable;
                    }
                    else
                    {
                        summands[1] = StdBuilder.Multiply(coeff, variable);
                    }
                }
                else
                {
                    if (c.IsMultiplicativeIdentity)
                    {
                        summands[i] = StdBuilder.Power(variable, IntegerValue.Constant(i));
                    }
                    else
                    {
                        summands[i] = StdBuilder.Multiply(coeff, StdBuilder.Power(variable, IntegerValue.Constant(i)));
                    }
                }
            }
            return(Std.Add(summands));
        }
Пример #3
0
        public static Signal ConstructPolynomial(Signal variable, params Signal[] coefficients)
        {
            Signal[] summands = new Signal[coefficients.Length];
            for (int i = 0; i < summands.Length; i++)
            {
                Signal coeff = coefficients[i];
                if (Std.IsConstantAdditiveIdentity(coeff))
                {
                    summands[i] = coeff;
                    continue;
                }

                if (i == 0)
                {
                    summands[0] = coeff;
                }
                else if (i == 1)
                {
                    if (Std.IsConstantMultiplicativeIdentity(coeff))
                    {
                        summands[1] = variable;
                    }
                    else
                    {
                        summands[1] = StdBuilder.Multiply(coeff, variable);
                    }
                }
                else
                {
                    if (Std.IsConstantMultiplicativeIdentity(coeff))
                    {
                        summands[i] = StdBuilder.Power(variable, IntegerValue.Constant(i));
                    }
                    else
                    {
                        summands[i] = StdBuilder.Multiply(coeff, StdBuilder.Power(variable, IntegerValue.Constant(i)));
                    }
                }
            }
            return(Std.Add(summands));
        }
Пример #4
0
        /// <summary>
        /// Extracts all coefficients of the polynomial <see cref="signal"/>.
        /// </summary>
        /// <returns>A signal array [c0,c1,c2,..] where ci ist the coefficient of x^i.</returns>
        public static Signal[] PolynomialCoefficients(Signal signal, Signal variable)
        {
            IValueStructure           vs;
            SortedList <long, Signal> polynom = new SortedList <long, Signal>();

            Signal c = MonomialCoefficient(signal, variable, out vs);

            if (!(vs is UndefinedSymbol))
            {
                IntegerValue iv = vs as IntegerValue;
                if (iv != null)
                {
                    polynom.Add(iv.Value, c);
                }
                else
                {
                    return new Signal[] { IntegerValue.ConstantZero }
                };
            }
            else if (signal.IsDrivenByPortEntity("Add", "Std"))
            {
                ISignalSet inputs = signal.DrivenByPort.InputSignals;
                for (int i = 0; i < inputs.Count; i++)
                {
                    c = MonomialCoefficient(inputs[i], variable, out vs);
                    IntegerValue iv = vs as IntegerValue;
                    if (iv != null)
                    {
                        if (!polynom.ContainsKey(iv.Value))
                        {
                            polynom.Add(iv.Value, c);
                        }
                        else
                        {
                            polynom[iv.Value] = Std.Add(polynom[iv.Value], c);
                        }
                    }
                }
            }
            if (polynom.Keys.Count == 0)
            {
                return new Signal[] { }
            }
            ;
            long deg = polynom.Keys[polynom.Keys.Count - 1];

            Signal[] ret  = new Signal[deg + 1];
            Signal   zero = IntegerValue.ConstantZero;

            for (int i = 0; i < ret.Length; i++)
            {
                if (polynom.ContainsKey(i))
                {
                    ret[i] = polynom[i];
                }
                else
                {
                    ret[i] = zero;
                }
            }
            return(ret);
        }