/// <summary> /// The evaluate at. /// </summary> /// <param name="a"> /// The a. /// </param> /// <returns> /// The <see cref="int"/>. /// </returns> public int EvaluateAt(int a) { // Just return the x^0 coefficient if (a == 0) { return(this.GetCoefficient(0)); } int size = this.coefficients.Length; if (a == 1) { // Just the sum of the coefficients int txt = 0; for (int i = 0; i < size; i++) { txt = QrGaloisField.AddOrSubtract(txt, this.coefficients[i]); } return(txt); } int res = this.coefficients[0]; for (int i = 1; i < size; i++) { res = QrGaloisField.AddOrSubtract(this.field.Multiply(a, res), this.coefficients[i]); } return(res); }
/// <summary> /// The multiply. /// </summary> /// <param name="other"> /// The other. /// </param> /// <returns> /// The <see cref="QrGaloisFieldPoly"/>. /// </returns> public QrGaloisFieldPoly Multiply(QrGaloisFieldPoly other) { if (this.IsZero() || other.IsZero()) { return(this.field.Zero); } int[] galoisACoefficients = this.coefficients; int galoisALength = galoisACoefficients.Length; int[] galoisBCoefficients = other.coefficients; int galoisBLength = galoisBCoefficients.Length; int[] product = new int[galoisALength + galoisBLength - 1]; for (int i = 0; i < galoisALength; i++) { int galoisACoeff = galoisACoefficients[i]; for (int j = 0; j < galoisBLength; j++) { product[i + j] = QrGaloisField.AddOrSubtract(product[i + j], this.field.Multiply(galoisACoeff, galoisBCoefficients[j])); } } return(new QrGaloisFieldPoly(this.field, product)); }
/// <summary> /// The add or subtract. /// </summary> /// <param name="other"> /// The other. /// </param> /// <returns> /// The <see cref="QrGaloisFieldPoly"/>. /// </returns> public QrGaloisFieldPoly AddOrSubtract(QrGaloisFieldPoly other) { if (this.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-power polynomial's coefficients Array.Copy(largerCoefficients, 0, sumDiff, 0, lengthDiff); for (int i = lengthDiff; i < largerCoefficients.Length; i++) { sumDiff[i] = QrGaloisField.AddOrSubtract(smallerCoefficients[i - lengthDiff], largerCoefficients[i]); } return(new QrGaloisFieldPoly(this.field, sumDiff)); }