コード例 #1
0
        /// <returns> evaluation of this polynomial at a given point
        /// </returns>
        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);
        }
コード例 #2
0
ファイル: GF256Poly.cs プロジェクト: CareyGit/jx
		/// <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;
			}
		}
コード例 #3
0
        /// <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;
            }
        }
コード例 #4
0
        internal GF256Poly multiply(GF256Poly other)
        {
            if (!field.Equals(other.field))
            {
                throw new System.ArgumentException("GF256Polys do not have same GF256 field");
            }
            if (Zero || other.Zero)
            {
                return(field.Zero);
            }
            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));
        }
コード例 #5
0
ファイル: ReedSolomonEncoder.cs プロジェクト: CareyGit/jx
		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));
			cachedGenerators.Add(new GF256Poly(field, new int[]{1}));
		}
コード例 #6
0
 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));
     cachedGenerators.Add(new GF256Poly(field, new int[] { 1 }));
 }
コード例 #7
0
        internal GF256Poly addOrSubtract(GF256Poly other)
        {
            if (!field.Equals(other.field))
            {
                throw new System.ArgumentException("GF256Polys do not have same GF256 field");
            }
            if (Zero)
            {
                return(other);
            }
            if (other.Zero)
            {
                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] = GF256.addOrSubtract(smallerCoefficients[i - lengthDiff], largerCoefficients[i]);
            }

            return(new GF256Poly(field, sumDiff));
        }