/** * @return evaluation of this polynomial at a given point */ public int evaluateAt(int a) { if (a == 0) { // Just return the x^0 coefficient return(getCoefficient(0)); } int size = coefficients.Length; int result = 0; if (a == 1) { // Just the sum of the coefficients result = 0; for (int i = 0; i < size; i++) { result = GF256.addOrSubtract(result, coefficients[i]); } return(result); } result = coefficients[0]; for (int i = 1; i < size; i++) { result = GF256.addOrSubtract(field.multiply(a, result), coefficients[i]); } return(result); }
public GF256Poly multiply(GF256Poly other) { if (!field.Equals(other.field)) { throw new ArgumentException("GF256Polys do not have same GF256 field"); } if (isZero() || other.isZero()) { return(field.getZero()); } 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] = GF256.addOrSubtract(product[i + j], field.multiply(aCoeff, bCoefficients[j])); } } return(new GF256Poly(field, product)); }
internal GF256Poly Multiply(GF256Poly other) { if (!field.Equals(other.field)) { throw new ArgumentException("GF256Polys do not have same GF256 field"); } if (Zero || other.Zero) { return(field.Zero); } int[] aCoefficients = 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] = GF256.AddOrSubtract(product[i + j], field.Multiply(aCoeff, bCoefficients[j])); } } return(new GF256Poly(field, product)); }
/// <summary> evaluation of this polynomial at a given point /// </summary> internal int EvaluateAt(int a) { if (a == 0) { // Just return the x^0 coefficient return(GetCoefficient(0)); } int size = Coefficients.Length; if (a == 1) { // Just the sum of the coefficients int result = 0; for (int i = 0; i < size; i++) { result = GF256.AddOrSubtract(result, Coefficients[i]); } return(result); } int result2 = Coefficients[0]; for (int i = 1; i < size; i++) { result2 = GF256.AddOrSubtract(field.Multiply(a, result2), Coefficients[i]); } return(result2); }
//UPGRADE_NOTE: Final was removed from the declaration of 'coefficients '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'" /// <param name="field">the {@link GF256} instance representing the field to use /// to perform computations /// </param> /// <param name="coefficients">coefficients as ints representing elements of GF(256), arranged /// from most significant (highest-power term) coefficient to least significant /// </param> /// <throws> IllegalArgumentException if argument is null or empty, </throws> /// <summary> or if leading coefficient is 0 and this is not a /// constant polynomial (that is, it is not the monomial "0") /// </summary> internal GF256Poly(GF256 field, int[] coefficients) { if (coefficients == null || coefficients.Length == 0) { throw new ArgumentException(); } this.field = field; int coefficientsLength = coefficients.Length; if (coefficientsLength > 1 && coefficients[0] == 0) { // Leading term must be non-zero for anything except the constant polynomial "0" int firstNonZero = 1; while (firstNonZero < coefficientsLength && coefficients[firstNonZero] == 0) { firstNonZero++; } if (firstNonZero == coefficientsLength) { Coefficients = field.Zero.Coefficients; } else { Coefficients = new int[coefficientsLength - firstNonZero]; Array.Copy(coefficients, firstNonZero, Coefficients, 0, Coefficients.Length); } } else { Coefficients = coefficients; } }
private int[] findErrorMagnitudes(GF256Poly errorEvaluator, int[] errorLocations, bool dataMatrix) { // This is directly applying Forney's Formula int s = errorLocations.Length; int[] result = new int[s]; for (int i = 0; i < s; i++) { int xiInverse = field.inverse(errorLocations[i]); int denominator = 1; for (int j = 0; j < s; j++) { if (i != j) { denominator = field.multiply(denominator, GF256.addOrSubtract(1, field.multiply(errorLocations[j], xiInverse))); } } result[i] = field.multiply(errorEvaluator.evaluateAt(xiInverse), field.inverse(denominator)); // Thanks to sanfordsquires for this fix: if (dataMatrix) { result[i] = field.multiply(result[i], xiInverse); } } return(result); }
/// <param name="field">the {@link GF256} instance representing the field to use /// to perform computations /// </param> /// <param name="coefficients">coefficients as ints representing elements of GF(256), arranged /// from most significant (highest-power term) coefficient to least significant /// </param> /// <throws> IllegalArgumentException if argument is null or empty, </throws> /// <summary> or if leading coefficient is 0 and this is not a /// constant polynomial (that is, it is not the monomial "0") /// </summary> internal GF256Poly(GF256 field, int[] coefficients) { if (coefficients == null || coefficients.Length == 0) { throw new System.ArgumentException(); } this.field = field; int coefficientsLength = coefficients.Length; if (coefficientsLength > 1 && coefficients[0] == 0) { // Leading term must be non-zero for anything except the constant polynomial "0" int firstNonZero = 1; while (firstNonZero < coefficientsLength && coefficients[firstNonZero] == 0) { firstNonZero++; } if (firstNonZero == coefficientsLength) { this.coefficients = field.Zero.coefficients; } else { this.coefficients = new int[coefficientsLength - firstNonZero]; Array.Copy(coefficients, firstNonZero, this.coefficients, 0, this.coefficients.Length); } } else { this.coefficients = coefficients; } }
public ReedSolomonEncoder(GF256 field) { if (!GF256.QR_CODE_FIELD.Equals(field)) { throw new ArgumentException("Only QR Code is supported at this time"); } this.Field = field; this.cachedGenerators = new ArrayList(); cachedGenerators.Add(new GF256Poly(field, new int[] { 1 })); }
public ReedSolomonEncoder(GF256 field) { if (!GF256.QR_CODE_FIELD.Equals(field)) { throw new System.ArgumentException("Only QR Code is supported at this time"); } this.field = field; this.cachedGenerators = new System.Collections.Generic.List <Object>(10); cachedGenerators.Add(new GF256Poly(field, new int[] { 1 })); }
public ReedSolomonEncoder(GF256 field) { if (!GF256.QR_CODE_FIELD.Equals(field)) { throw new ArgumentException("Only QR Code is supported at this time"); } this.field = field; cachedGenerators = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10)); cachedGenerators.Add(new GF256Poly(field, new[] { 1 })); }
internal ReedSolomonEncoder(GF256 field) { if (!GF256.QR_CODE_FIELD.Equals(field)) { throw new System.ArgumentException("Only QR Code is supported at this time"); } this.field = field; this.cachedGenerators = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10)); cachedGenerators.Add(new GF256Poly(field, new int[]{1})); }
public ReedSolomonEncoder(GF256 field) { if (!GF256.QR_CODE_FIELD.Equals(field)) { throw new System.ArgumentException("Only QR Code is supported at this time"); } this.field = field; this.cachedGenerators = new System.Collections.Generic.List<Object>(10); cachedGenerators.Add(new GF256Poly(field, new int[]{1})); }
public ReedSolomonEncoder(GF256 field) { if (!GF256.QR_CODE_FIELD.Equals(field)) { throw new System.ArgumentException("Only QR Code is supported at this time"); } this.field = field; // this.cachedGenerators = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10)); // commented by .net follower (http://dotnetfollower.com) this.cachedGenerators = new System.Collections.Generic.List<Object>(10); // added by .net follower (http://dotnetfollower.com) cachedGenerators.Add(new GF256Poly(field, new int[]{1})); }
private System.Collections.Generic.List <Object> cachedGenerators; // added by .net follower (http://dotnetfollower.com) public ReedSolomonEncoder(GF256 field) { if (!GF256.QR_CODE_FIELD.Equals(field)) { throw new System.ArgumentException("Only QR Code is supported at this time"); } this.field = field; // this.cachedGenerators = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10)); // commented by .net follower (http://dotnetfollower.com) this.cachedGenerators = new System.Collections.Generic.List <Object>(10); // added by .net follower (http://dotnetfollower.com) cachedGenerators.Add(new GF256Poly(field, new int[] { 1 })); }
/** * <p>Decodes given set of received codewords, which include both data and error-correction * codewords. Really, this means it uses Reed-Solomon to detect and correct errors, in-place, * in the input.</p> * * @param received data and error-correction codewords * @param twoS number of error-correction codewords available * @throws ReedSolomonException if decoding fails for any reason */ public void decode(int[] received, int twoS) { try{ GF256Poly poly = new GF256Poly(field, received); int[] syndromeCoefficients = new int[twoS]; bool dataMatrix = field.Equals(GF256.DATA_MATRIX_FIELD); bool noError = true; for (int i = 0; i < twoS; i++) { // Thanks to sanfordsquires for this fix: int eval = poly.evaluateAt(field.exp(dataMatrix ? i + 1 : i)); syndromeCoefficients[syndromeCoefficients.Length - 1 - i] = eval; if (eval != 0) { noError = false; } } if (noError) { return; } GF256Poly syndrome = new GF256Poly(field, syndromeCoefficients); GF256Poly[] sigmaOmega = runEuclideanAlgorithm(field.buildMonomial(twoS, 1), syndrome, twoS); GF256Poly sigma = sigmaOmega[0]; GF256Poly omega = sigmaOmega[1]; int[] errorLocations = findErrorLocations(sigma); int[] errorMagnitudes = findErrorMagnitudes(omega, errorLocations, dataMatrix); for (int i = 0; i < errorLocations.Length; i++) { int position = received.Length - 1 - field.log(errorLocations[i]); if (position < 0) { throw new ReedSolomonException("Bad error location"); } received[position] = GF256.addOrSubtract(received[position], errorMagnitudes[i]); } }catch (ReedSolomonException e) { throw new ReedSolomonException(e.Message); } }
internal GF256Poly AddOrSubtract(GF256Poly other) { if (!field.Equals(other.field)) { throw new ArgumentException("GF256Polys do not have same GF256 field"); } if (Zero) { return(other); } if (other.Zero) { return(this); } int[] smallerCoefficients = 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] = GF256.AddOrSubtract(smallerCoefficients[i - lengthDiff], largerCoefficients[i]); } return(new GF256Poly(field, sumDiff)); }
public ReedSolomonDecoder(GF256 field) { this.field = field; }