/// <summary>
        /// evaluation of this polynomial at a given point
        /// </summary>
        /// <param name="a">A.</param>
        /// <returns>evaluation of this polynomial at a given point</returns>
        internal int EvaluateAt(int a)
        {
            int result = 0;

            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
                foreach (var coefficient in coefficients)
                {
                    result = GenericGf.AddOrSubtract(result, coefficient);
                }
                return(result);
            }
            result = coefficients[0];
            for (int i = 1; i < size; i++)
            {
                result = GenericGf.AddOrSubtract(field.Multiply(a, result), coefficients[i]);
            }
            return(result);
        }
        internal GenericGfPoly Multiply(GenericGfPoly other)
        {
            if (!field.Equals(other.field))
            {
                throw new ArgumentException("GenericGFPolys do not have same GenericGF field");
            }
            if (IsZero || other.IsZero)
            {
                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] = GenericGf.AddOrSubtract(product[i + j],
                                                             field.Multiply(aCoeff, bCoefficients[j]));
                }
            }
            return(new GenericGfPoly(field, product));
        }
        internal GenericGfPoly(GenericGf field, int[] coefficients)
        {
            if (coefficients.Length == 0)
            {
                throw new ArgumentException();
            }
            this.field = field;
            int coefficientsLength = coefficients.Length;

            if (coefficientsLength > 1 && coefficients[0] == 0)
            {
                int firstNonZero = 1;
                while (firstNonZero < coefficientsLength && coefficients[firstNonZero] == 0)
                {
                    firstNonZero++;
                }
                if (firstNonZero == coefficientsLength)
                {
                    this.coefficients = new[] { 0 };
                }
                else
                {
                    this.coefficients = new int[coefficientsLength - firstNonZero];
                    Array.Copy(coefficients,
                               firstNonZero,
                               this.coefficients,
                               0,
                               this.coefficients.Length);
                }
            }
            else
            {
                this.coefficients = coefficients;
            }
        }
        internal GenericGfPoly AddOrSubtract(GenericGfPoly other)
        {
            if (!field.Equals(other.field))
            {
                throw new ArgumentException("GenericGFPolys do not have same GenericGF 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
            Array.Copy(largerCoefficients, 0, sumDiff, 0, lengthDiff);

            for (int i = lengthDiff; i < largerCoefficients.Length; i++)
            {
                sumDiff[i] = GenericGf.AddOrSubtract(smallerCoefficients[i - lengthDiff], largerCoefficients[i]);
            }

            return(new GenericGfPoly(field, sumDiff));
        }
Exemplo n.º 5
0
 public ReedSolomonEncoder(GenericGf field)
 {
     this.field            = field;
     this.cachedGenerators = new List <GenericGfPoly>();
     cachedGenerators.Add(new GenericGfPoly(field, new[] { 1 }));
 }