/// <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) { ValueStructure 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(signal.Context)); } } if (signal.IsDrivenByPortEntity("Add", "Std")) { ReadOnlySignalSet 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(signal.Context.Builder.AddSimplified(coeffs)); } return(UndefinedSymbol.Constant(signal.Context)); }
public static Signal PolynomialLeadingCoefficient(Signal signal, Signal variable) { ValueStructure degree = PolynomialDegree(signal, variable); IntegerValue iv = degree as IntegerValue; if (iv != null) { return(PolynomialCoefficient(signal, variable, (int)iv.Value)); } if (degree is NegativeInfinitySymbol) { return(IntegerValue.ConstantZero(signal.Context)); } return(UndefinedSymbol.Constant(signal.Context)); }
/// <summary> /// Returns the coefficient factor in the monomial <see cref="signal"/> /// </summary> /// <returns> /// Constant UndefinedSymbol if <see cref="signal"/> is not a single-variable monomial. /// Otherwise the coefficient factor of the term. /// </returns> /// </returns> /// <remarks><see cref="signal"/> is assumed to be automatic simplified.</remarks> public static Signal MonomialCoefficient(Signal signal, Signal variable, out ValueStructure degree) { if (signal == null) { throw new ArgumentNullException("signal"); } if (IsConstantAdditiveIdentity(signal)) { degree = NegativeInfinitySymbol.Instance; return(signal); } if (IsAlwaysRational(signal)) { degree = IntegerValue.Zero; return(signal); } Signal coeff = IntegerValue.ConstantOne(signal.Context); if (signal.IsDrivenByPortEntity("Multiply", "Std") && signal.DrivenByPort.InputSignalCount == 2 && IsAlwaysRational(signal.DrivenByPort.InputSignals[0])) { coeff = signal.DrivenByPort.InputSignals[0]; signal = signal.DrivenByPort.InputSignals[1]; } if (signal.Equals(variable)) { degree = IntegerValue.One; return(coeff); } if (signal.IsDrivenByPortEntity("Power", "Std")) { Signal b = signal.DrivenByPort.InputSignals[0]; Signal e = signal.DrivenByPort.InputSignals[1]; if (b.Equals(variable) && Std.IsAlwaysPositiveInteger(e)) { degree = e.Value; return(coeff); } } degree = UndefinedSymbol.Instance; return(UndefinedSymbol.Constant(signal.Context)); }