/// <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 divide. /// </summary> /// <param name="other"> /// The other. /// </param> /// <returns> /// The <see cref="QrGaloisFieldPoly" /> array. /// </returns> /// <exception cref="ArgumentException"> /// Exception if division by zero. /// </exception> public QrGaloisFieldPoly[] Divide(QrGaloisFieldPoly other) { if (other.IsZero()) { throw new ArgumentException("Divide by 0"); } QrGaloisFieldPoly quotient = this.field.Zero; QrGaloisFieldPoly remainder = this; int denominatorLeadingTerm = other.GetCoefficient(other.GetPower()); int inverseDenominatorLeadingTerm = this.field.Inverse(denominatorLeadingTerm); while ((remainder.GetPower() >= other.GetPower()) && !remainder.IsZero()) { int powerDifference = remainder.GetPower() - other.GetPower(); int scale = this.field.Multiply( remainder.GetCoefficient(remainder.GetPower()), inverseDenominatorLeadingTerm); QrGaloisFieldPoly term = other.MultiplyByMonomial(powerDifference, scale); QrGaloisFieldPoly iterationQuotient = this.field.BuildMonomial(powerDifference, scale); quotient = quotient.AddOrSubtract(iterationQuotient); remainder = remainder.AddOrSubtract(term); } return(new[] { quotient, remainder }); }
/// <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)); }
/// <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 divide. /// </summary> /// <param name="other"> /// The other. /// </param> /// <returns> /// The <see cref="QrGaloisFieldPoly" /> array. /// </returns> /// <exception cref="ArgumentException"> /// Exception if division by zero. /// </exception> public QrGaloisFieldPoly[] Divide(QrGaloisFieldPoly other) { if (other.IsZero()) { throw new ArgumentException("Divide by 0"); } QrGaloisFieldPoly quotient = this.field.Zero; QrGaloisFieldPoly remainder = this; int denominatorLeadingTerm = other.GetCoefficient(other.GetPower()); int inverseDenominatorLeadingTerm = this.field.Inverse(denominatorLeadingTerm); while ((remainder.GetPower() >= other.GetPower()) && !remainder.IsZero()) { int powerDifference = remainder.GetPower() - other.GetPower(); int scale = this.field.Multiply( remainder.GetCoefficient(remainder.GetPower()), inverseDenominatorLeadingTerm); QrGaloisFieldPoly term = other.MultiplyByMonomial(powerDifference, scale); QrGaloisFieldPoly iterationQuotient = this.field.BuildMonomial(powerDifference, scale); quotient = quotient.AddOrSubtract(iterationQuotient); remainder = remainder.AddOrSubtract(term); } return new[] { quotient, remainder }; }
/// <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); }