/// <summary>
        ///   <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>
        /// </summary>
        /// <param name="received">data and error-correction codewords</param>
        /// <param name="twoS">number of error-correction codewords available</param>
        /// <returns>false: decoding fails</returns>
        public bool Decode(int[] received, int twoS)
        {
            var poly = new GenericGFPoly(field, received);
            var syndromeCoefficients = new int[twoS];
            var noError = true;

            for (var i = 0; i < twoS; i++)
            {
                int eval = poly.EvaluateAt(field.Exp(i + field.GeneratorBase));
                syndromeCoefficients[syndromeCoefficients.Length - 1 - i] = eval;
                if (eval != 0)
                {
                    noError = false;
                }
            }

            if (noError)
            {
                return(true);
            }

            var syndrome = new GenericGFPoly(field, syndromeCoefficients);

            GenericGFPoly[] sigmaOmega = RunEuclideanAlgorithm(field.BuildMonomial(twoS, 1), syndrome, twoS);
            if (sigmaOmega == null)
            {
                return(false);
            }

            GenericGFPoly sigma = sigmaOmega[0];

            int[] errorLocations = FindErrorLocations(sigma);

            if (errorLocations == null)
            {
                return(false);
            }

            GenericGFPoly omega = sigmaOmega[1];

            int[] errorMagnitudes = FindErrorMagnitudes(omega, errorLocations);

            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");
                    return(false);
                }
                received[position] = GenericGF.AddOrSubtract(received[position], errorMagnitudes[i]);
            }

            return(true);
        }
        internal GenericGFPoly[] Divide(GenericGFPoly other)
        {
            if (field.Equals(other.field) == false)
            {
                throw new ArgumentException("GenericGFPolys do not have same GenericGF field");
            }

            if (other.IsZero)
            {
                throw new ArgumentException("Divide by 0");
            }

            GenericGFPoly quotient  = field.Zero;
            GenericGFPoly remainder = this;

            int denominatorLeadingTerm        = other.GetCoefficient(other.Degree);
            int inverseDenominatorLeadingTerm = field.Inverse(denominatorLeadingTerm);

            while (remainder.Degree >= other.Degree && !remainder.IsZero)
            {
                int           degreeDifference  = remainder.Degree - other.Degree;
                int           scale             = field.Multiply(remainder.GetCoefficient(remainder.Degree), inverseDenominatorLeadingTerm);
                GenericGFPoly term              = other.MultiplyByMonomial(degreeDifference, scale);
                GenericGFPoly iterationQuotient = field.BuildMonomial(degreeDifference, scale);

                quotient  = quotient.AddOrSubtract(iterationQuotient);
                remainder = remainder.AddOrSubtract(term);
            }

            return(new GenericGFPoly[] { quotient, remainder });
        }