public static Signal PolynomialDivision(Signal numerator, Signal denominator, Signal variable, out Signal remainder) { Signal[] R = PolynomialCoefficients(numerator, variable); Signal[] B = PolynomialCoefficients(denominator, variable); int degB = B.Length - 1; Signal lcB = B[degB]; Signal[] Q = new Signal[R.Length - degB]; for (int delta = R.Length - 1 - degB; delta >= 0; delta--) { int degR = degB + delta; if (Std.IsConstantAdditiveIdentity(R[degR])) { Q[delta] = R[degR]; continue; } Signal T = Std.Divide(R[degR], lcB); Q[delta] = T; R[degB + delta] = IntegerValue.ConstantZero; for (int i = delta; i < degB + delta; i++) { R[i] = Std.Subtract(R[i], Std.Multiply(B[i - delta], T)); } } remainder = ConstructPolynomial(variable, R); return(ConstructPolynomial(variable, Q)); }
/// <summary> /// Evaluates the degree of the single-variable monomial <see cref="signal"/>. /// </summary> /// <returns> /// <see cref="UndefinedSymbol"/> if <see cref="signal"/> is not a single-variable monomial. /// <see cref="NegativeInfinitySymbol"/> if <see cref="signal"/> is zero. /// Otherwise an <see cref="IntegerValue"/> representing the asked degree. /// </returns> /// <remarks><see cref="signal"/> is assumed to be automatic simplified.</remarks> public static IValueStructure MonomialDegree(Signal signal, Signal variable) { if (signal == null) { throw new ArgumentNullException("signal"); } if (Std.IsConstantAdditiveIdentity(signal)) { return(NegativeInfinitySymbol.Instance); } if (Std.IsAlwaysRational(signal)) { return(IntegerValue.Zero); } if (signal.IsDrivenByPortEntity("Multiply", "Std") && signal.DrivenByPort.InputSignalCount == 2 && Std.IsAlwaysRational(signal.DrivenByPort.InputSignals[0])) { signal = signal.DrivenByPort.InputSignals[1]; } if (signal.Equals(variable)) { return(IntegerValue.One); } if (signal.IsDrivenByPortEntity("Power", "Std")) { Signal b = signal.DrivenByPort.InputSignals[0]; Signal e = signal.DrivenByPort.InputSignals[1]; if (b.Equals(variable) && Std.IsAlwaysPositiveInteger(e)) { return(e.Value); } } return(UndefinedSymbol.Instance); }
public static IValueStructure MonomialDegree(Signal signal, Signal[] generalizedVariables) { if (Std.IsConstantAdditiveIdentity(signal)) { return(NegativeInfinitySymbol.Instance); } if (Array.Exists <Signal>(generalizedVariables, signal.Equals)) { return(IntegerValue.One); } if (!Array.Exists <Signal>(generalizedVariables, signal.DependsOn)) { return(IntegerValue.Zero); } ISignalSet factors; long deg = 0; if (signal.IsDrivenByPortEntity("Multiply", "Std")) { factors = signal.DrivenByPort.InputSignals; } else { factors = new SignalSet(signal); } for (int i = 0; i < factors.Count; i++) { if (Array.Exists <Signal>(generalizedVariables, factors[i].Equals)) { deg++; } else if (factors[i].IsDrivenByPortEntity("Power", "Std")) { Signal b = signal.DrivenByPort.InputSignals[0]; Signal e = signal.DrivenByPort.InputSignals[1]; IntegerValue v; if (Array.Exists <Signal>(generalizedVariables, b.Equals) && (v = e.Value as IntegerValue) != null && v.Value > 0) { deg += v.Value; } } } return(new IntegerValue(deg)); }
/// <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 IValueStructure degree) { if (signal == null) { throw new ArgumentNullException("signal"); } if (Std.IsConstantAdditiveIdentity(signal)) { degree = NegativeInfinitySymbol.Instance; return(signal); } if (Std.IsAlwaysRational(signal)) { degree = IntegerValue.Zero; return(signal); } Signal coeff = IntegerValue.ConstantOne; if (signal.IsDrivenByPortEntity("Multiply", "Std") && signal.DrivenByPort.InputSignalCount == 2 && Std.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); }
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)); }