/// <summary> /// divide current polynomial by "other" /// </summary> /// <returns>Result polynomial after divide</returns> internal PolyDivideStruct Divide(Polynomial other) { if (this.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 = this.Coefficients.Length; //We will make change to aCoefficient. It will return as remainder int[] aCoefficients = new int[aLength]; Array.Copy(this.Coefficients, 0, aCoefficients, 0, aLength); int bLength = other.Coefficients.Length; if (aLength < bLength) { return(new PolyDivideStruct(new Polynomial(this.GField, new int[] { 0 }), this)); } else { //quotient coefficients //qLastIndex = alength - blength qlength = qLastIndex + 1 int[] qCoefficients = new int[(aLength - bLength) + 1]; //Denominator int otherLeadingTerm = other.GetCoefficient(other.Degree); int inverseOtherLeadingTerm = this.GField.inverse(otherLeadingTerm); for (int aIndex = 0; aIndex <= aLength - bLength; aIndex++) { if (aCoefficients[aIndex] != 0) { int aScalar = this.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] = this.GField.Subtraction(aCoefficients[aIndex + bIndex], bCoefficient[bIndex]); } } } } return(new PolyDivideStruct(new Polynomial(this.GField, qCoefficients), new Polynomial(this.GField, aCoefficients))); } }
/// <summary> /// divide current polynomial by "other" /// </summary> /// <returns>Result polynomial after divide</returns> internal PolyDivideStruct Divide(Polynomial other) { if(this.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 = this.Coefficients.Length; //We will make change to aCoefficient. It will return as remainder int[] aCoefficients = new int[aLength]; Array.Copy(this.Coefficients, 0, aCoefficients, 0, aLength); int bLength = other.Coefficients.Length; if(aLength < bLength) return new PolyDivideStruct(new Polynomial(this.GField, new int[]{0}), this); else { //quotient coefficients //qLastIndex = alength - blength qlength = qLastIndex + 1 int[] qCoefficients = new int[( aLength - bLength ) + 1]; //Denominator int otherLeadingTerm = other.GetCoefficient(other.Degree); int inverseOtherLeadingTerm = this.GField.inverse(otherLeadingTerm); for(int aIndex = 0; aIndex <= aLength - bLength; aIndex++) { if(aCoefficients[aIndex] != 0) { int aScalar = this.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] = this.GField.Subtraction(aCoefficients[aIndex + bIndex], bCoefficient[bIndex]); } } } } return new PolyDivideStruct(new Polynomial(this.GField, qCoefficients), new Polynomial(this.GField, aCoefficients)); } }