/** * @return evaluation of this polynomial at a given point */ 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 result = coefficients[0]; for (int i = 1; i < size; i++) { result = GF256.addOrSubtract(field.multiply(a, result), coefficients[i]); } return result; }
/** * @return evaluation of this polynomial at a given point */ 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 result = coefficients[0]; for (int i = 1; i < size; i++) { result = GF256.addOrSubtract(field.multiply(a, result), coefficients[i]); } return result; }
GF256Poly addOrSubtract(GF256Poly other) { if (!field.equals(other.field)) { throw new IllegalArgumentException("GF256Polys do not have same GF256 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 System.arraycopy(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); }
GF256Poly addOrSubtract(GF256Poly other) { if (!field.equals(other.field)) { throw new IllegalArgumentException("GF256Polys do not have same GF256 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 System.arraycopy(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); }
GF256Poly multiply(GF256Poly other) { if (!field.equals(other.field)) { throw new IllegalArgumentException("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); }
GF256Poly multiply(GF256Poly other) { if (!field.equals(other.field)) { throw new IllegalArgumentException("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); }
private int[] findErrorMagnitudes(GF256Poly errorEvaluator, int[] errorLocations, boolean 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; }
throw new ReedSolomonException("Bad error location"); received[position] = GF256.addOrSubtract(received[position], errorMagnitudes[i]);