/// <summary> /// Method for multiplying current bivariate polynomial by bivariate polynomial <paramref name="b"/> /// </summary> /// <param name="b">Factor</param> public BiVariablePolynomial Multiply(BiVariablePolynomial b) { var coeficientsArray = _coefficients.ToArray(); _coefficients.Clear(); if (b.IsZero) { return(this); } foreach (var coefficient in coeficientsArray) { foreach (var otherCoefficient in b._coefficients) { FieldElement coeficientValue; var monomial = new Tuple <int, int>(coefficient.Key.Item1 + otherCoefficient.Key.Item1, coefficient.Key.Item2 + otherCoefficient.Key.Item2); if (_coefficients.TryGetValue(monomial, out coeficientValue) == false) { coeficientValue = Field.Zero(); _coefficients[monomial] = coeficientValue; } coeficientValue.Add(coefficient.Value * otherCoefficient.Value); } } return(RemoveZeroCoefficients()); }
/// <summary> /// Method for subtracting bivariate polynomial <paramref name="other"/> multiplied by <paramref name="b"/> from current bivariate polynomial /// </summary> /// <param name="b">Subtrahend multiplier</param> /// <param name="other">Subtrahend</param> public BiVariablePolynomial Subtract(FieldElement b, BiVariablePolynomial other) { if (Field.Equals(b.Field) == false) { throw new ArgumentException(NameOfExtension.nameof(() => b)); } if (Field.Equals(other.Field) == false) { throw new ArgumentException(NameOfExtension.nameof(() => other)); } if (b.Representation == 0) { return(this); } foreach (var otherCoefficient in other._coefficients) { FieldElement coeficientValue; if (_coefficients.TryGetValue(otherCoefficient.Key, out coeficientValue) == false) { coeficientValue = Field.Zero(); _coefficients[otherCoefficient.Key] = coeficientValue; } coeficientValue.Subtract(b * otherCoefficient.Value); if (coeficientValue.Representation == 0) { _coefficients.Remove(otherCoefficient.Key); } } return(this); }
/// <summary> /// Method for subtracting bivariate polynomial <paramref name="b"/> from <paramref name="a"/> /// </summary> /// <param name="a">Minuend</param> /// <param name="b">Subtrahend</param> public static BiVariablePolynomial Subtract(BiVariablePolynomial a, BiVariablePolynomial b) { var c = new BiVariablePolynomial(a); return(c.Subtract(b)); }
/// <summary> /// Method for adding bivariate polynomial <paramref name="b"/> to <paramref name="a"/> /// </summary> /// <param name="a">First term</param> /// <param name="b">Second term</param> public static BiVariablePolynomial Add(BiVariablePolynomial a, BiVariablePolynomial b) { var c = new BiVariablePolynomial(a); return(c.Add(b)); }
/// <summary> /// Method for checking the equality of the current bivariate polynomial to the <paramref name="other"/> /// </summary> /// <param name="other">Another bivariate polynomial</param> /// <returns>Checking result</returns> private bool Equals(BiVariablePolynomial other) { return(_coefficients.OrderBy(x => x.Key).SequenceEqual(other._coefficients.OrderBy(x => x.Key)) && Field.Equals(other.Field)); }
/// <summary> /// Method for subtracting bivariate polynomial <paramref name="other"/> from current bivariate polynomial /// </summary> /// <param name="other">Subtrahend</param> public BiVariablePolynomial Subtract(BiVariablePolynomial other) { return(Subtract(Field.One(), other)); }
/// <summary> /// Method for adding bivariate polynomial <paramref name="other"/> to current bivariate polynomial /// </summary> /// <param name="other">Term</param> public BiVariablePolynomial Add(BiVariablePolynomial other) { return(Add(Field.One(), other)); }