internal GenericGFPoly multiply(GenericGFPoly other) { if (!field.Equals(other.field)) { throw new ArgumentException("GenericGFPolys do not have same GenericGF field"); } if (isZero || other.isZero) { return(new GenericGFPoly(field, new int[] { 0 }, encoding)); } int[] aCoefficients = this.coefficients; int aLength = aCoefficients.Length; int[] bCoefficients = other.coefficients; int bLength = bCoefficients.Length; int[] product = new int[aLength + bLength - 1]; for (int i = 0; i < aLength; i++) { int aCoeff = aCoefficients[i]; for (int j = 0; j < bLength; j++) { product[i + j] = GenericGF.addOrSubtract(product[i + j], field.multiply(aCoeff, bCoefficients[j])); } } return(new GenericGFPoly(field, product, encoding)); }
internal GenericGFPoly[] divide(GenericGFPoly other) { if (!field.Equals(other.field)) { throw new ArgumentException("GenericGFPolys do not have same GenericGF field"); } if (other.isZero) { throw new ArgumentException("Divide by 0", "other"); } GenericGFPoly quotient = new GenericGFPoly(field, new int[] { 0 }, encoding); GenericGFPoly remainder = this; int denominatorLeadingTerm = other.getCoefficient(other.Degree); int inverseDenominatorLeadingTerm = field.inverse(denominatorLeadingTerm); while (remainder.Degree >= other.Degree && !remainder.isZero) { int degreeDifference = remainder.Degree - other.Degree; int scale = field.multiply(remainder.getCoefficient(remainder.Degree), inverseDenominatorLeadingTerm); GenericGFPoly term = other.multiplyByMonomial(degreeDifference, scale); GenericGFPoly iterationQuotient = buildMonomial(degreeDifference, scale); quotient = quotient.addOrSubtract(iterationQuotient); remainder = remainder.addOrSubtract(term); } return(new GenericGFPoly[] { quotient, remainder }); }
internal GenericGFPoly addOrSubtract(GenericGFPoly other) { if (!field.Equals(other.field)) { throw new ArgumentException("GenericGFPolys do not have same GenericGF field"); } if (isZero) { return(other); } if (other.isZero) { return(this); } int[] smallerCoefficients = this.coefficients; int[] largerCoefficients = other.coefficients; if (smallerCoefficients.Length > largerCoefficients.Length) { int[] temp = smallerCoefficients; smallerCoefficients = largerCoefficients; largerCoefficients = temp; } int[] sumDiff = new int[largerCoefficients.Length]; int lengthDiff = largerCoefficients.Length - smallerCoefficients.Length; // Copy high-order terms only found in higher-degree polynomial's coefficients Array.Copy(largerCoefficients, 0, sumDiff, 0, lengthDiff); for (int i = lengthDiff; i < largerCoefficients.Length; i++) { sumDiff[i] = GenericGF.addOrSubtract(smallerCoefficients[i - lengthDiff], largerCoefficients[i]); } return(new GenericGFPoly(field, sumDiff, encoding)); }