/// <summary> /// Multiply current Polynomial to anotherone. /// </summary> /// <returns>Result polynomial after multiply</returns> internal Polynomial Multiply(Polynomial other) { if (Primitive != other.Primitive) { throw new ArgumentException("Polynomial cannot perform Multiply as they do not have same Primitive for GaloisField256"); } if (IsMonomialZero || other.IsMonomialZero) { return(new Polynomial(GField, new int[] { 0 })); } int[] aCoefficients = Coefficients; int aLength = aCoefficients.Length; int[] bCoefficient = other.Coefficients; int bLength = bCoefficient.Length; int[] rCoefficients = new int[aLength + bLength - 1]; for (int aIndex = 0; aIndex < aLength; aIndex++) { int aCoeff = aCoefficients[aIndex]; for (int bIndex = 0; bIndex < bLength; bIndex++) { rCoefficients[aIndex + bIndex] = GField.Addition(rCoefficients[aIndex + bIndex], GField.Product(aCoeff, bCoefficient[bIndex])); } } return(new Polynomial(GField, rCoefficients)); }
/// <summary> /// divide current polynomial by "other" /// </summary> /// <param name="other">The other.</param> /// <returns>Result polynomial after divide</returns> /// <remarks></remarks> internal PolyDivideStruct Divide(Polynomial other) { if (Primitive != other.Primitive) { throw new ArgumentException( "Polynomial can not perform Devide as they don't have same Primitive for GaloisField256"); } if (other.isMonomialZero) { throw new ArgumentException("Can not devide by Polynomial Zero"); } //this devide by other = a devide by b int aLength = Coefficients.Length; //We will make change to aCoefficient. It will return as remainder var aCoefficients = new int[aLength]; Array.Copy(Coefficients, 0, aCoefficients, 0, aLength); int bLength = other.Coefficients.Length; if (aLength < bLength) { return(new PolyDivideStruct(new Polynomial(GField, new[] { 0 }), this)); } else { //quotient coefficients //qLastIndex = alength - blength qlength = qLastIndex + 1 var qCoefficients = new int[(aLength - bLength) + 1]; //Denominator int otherLeadingTerm = other.GetCoefficient(other.Degree); int inverseOtherLeadingTerm = GField.inverse(otherLeadingTerm); for (int aIndex = 0; aIndex <= aLength - bLength; aIndex++) { if (aCoefficients[aIndex] != 0) { int aScalar = GField.Product(inverseOtherLeadingTerm, aCoefficients[aIndex]); Polynomial term = other.MultiplyScalar(aScalar); qCoefficients[aIndex] = aScalar; int[] bCoefficient = term.Coefficients; if (bCoefficient[0] != 0) { for (int bIndex = 0; bIndex < bLength; bIndex++) { aCoefficients[aIndex + bIndex] = GField.Subtraction(aCoefficients[aIndex + bIndex], bCoefficient[bIndex]); } } } } return(new PolyDivideStruct(new Polynomial(GField, qCoefficients), new Polynomial(GField, aCoefficients))); } }
/// <summary> /// Multiplay scalar to current polynomial /// </summary> /// <param name="scalar">The scalar.</param> /// <returns>result of polynomial after multiply scalar</returns> /// <remarks></remarks> internal Polynomial MultiplyScalar(int scalar) { if (scalar == 0) { return(new Polynomial(GField, new[] { 0 })); } else if (scalar == 1) { return(this); } int length = Coefficients.Length; var rCoefficient = new int[length]; for (int index = 0; index < length; index++) { rCoefficient[index] = GField.Product(Coefficients[index], scalar); } return(new Polynomial(GField, rCoefficient)); }