コード例 #1
0
ファイル: QRReedSolomon.cs プロジェクト: Legoless/QRCode
        /// <summary>
        /// Initializes a new instance of the <see cref="QrReedSolomon"/> class.
        /// </summary>
        public QrReedSolomon()
        {
            this.field = new QrGaloisField();

            // Add 0 poly, as a starting point, later we are only multiplying
            this.generatorCache = new List<QrGaloisFieldPoly> { new QrGaloisFieldPoly(this.field, new[] { 1 }) };
        }
コード例 #2
0
        /// <summary>
        /// The multiply.
        /// </summary>
        /// <param name="other">
        /// The other.
        /// </param>
        /// <returns>
        /// The <see cref="QrGaloisFieldPoly"/>.
        /// </returns>
        public QrGaloisFieldPoly Multiply(QrGaloisFieldPoly other)
        {
            if (this.IsZero() || other.IsZero())
            {
                return(this.field.Zero);
            }

            int[] galoisACoefficients = this.coefficients;
            int   galoisALength       = galoisACoefficients.Length;

            int[] galoisBCoefficients = other.coefficients;

            int galoisBLength = galoisBCoefficients.Length;

            int[] product = new int[galoisALength + galoisBLength - 1];

            for (int i = 0; i < galoisALength; i++)
            {
                int galoisACoeff = galoisACoefficients[i];

                for (int j = 0; j < galoisBLength; j++)
                {
                    product[i + j] = QrGaloisField.AddOrSubtract(product[i + j], this.field.Multiply(galoisACoeff, galoisBCoefficients[j]));
                }
            }

            return(new QrGaloisFieldPoly(this.field, product));
        }
コード例 #3
0
        /// <summary>
        /// The evaluate at.
        /// </summary>
        /// <param name="a">
        /// The a.
        /// </param>
        /// <returns>
        /// The <see cref="int"/>.
        /// </returns>
        public int EvaluateAt(int a)
        {
            // Just return the x^0 coefficient
            if (a == 0)
            {
                return(this.GetCoefficient(0));
            }

            int size = this.coefficients.Length;

            if (a == 1)
            {
                // Just the sum of the coefficients
                int txt = 0;

                for (int i = 0; i < size; i++)
                {
                    txt = QrGaloisField.AddOrSubtract(txt, this.coefficients[i]);
                }

                return(txt);
            }

            int res = this.coefficients[0];

            for (int i = 1; i < size; i++)
            {
                res = QrGaloisField.AddOrSubtract(this.field.Multiply(a, res), this.coefficients[i]);
            }

            return(res);
        }
コード例 #4
0
        /// <summary>
        /// The add or subtract.
        /// </summary>
        /// <param name="other">
        /// The other.
        /// </param>
        /// <returns>
        /// The <see cref="QrGaloisFieldPoly"/>.
        /// </returns>
        public QrGaloisFieldPoly AddOrSubtract(QrGaloisFieldPoly other)
        {
            if (this.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-power polynomial's coefficients
            Array.Copy(largerCoefficients, 0, sumDiff, 0, lengthDiff);

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

            return(new QrGaloisFieldPoly(this.field, sumDiff));
        }
コード例 #5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="QrReedSolomon"/> class.
        /// </summary>
        public QrReedSolomon()
        {
            this.field = new QrGaloisField();

            // Add 0 poly, as a starting point, later we are only multiplying
            this.generatorCache = new List <QrGaloisFieldPoly> {
                new QrGaloisFieldPoly(this.field, new[] { 1 })
            };
        }
コード例 #6
0
ファイル: QRGaloisFieldPoly.cs プロジェクト: Legoless/QRCode
        /// <summary>
        /// Initializes a new instance of the <see cref="QrGaloisFieldPoly"/> class.
        /// </summary>
        /// <param name="field">
        /// The field.
        /// </param>
        /// <param name="galoisCoefficients">
        /// The coefficients.
        /// </param>
        /// <exception cref="ArgumentException">
        /// Argument exception if empty array of coefficients is provided
        /// </exception>
        public QrGaloisFieldPoly(QrGaloisField field, int[] galoisCoefficients)
        {
            if (galoisCoefficients.Length == 0)
            {
                throw new ArgumentException();
            }

            this.field = field;

            int coefficientsLength = galoisCoefficients.Length;

            if (coefficientsLength > 1 && galoisCoefficients[0] == 0)
            {
                // Leading term must be non-zero for anything except the constant polynomial "0"
                int firstNonZero = 1;

                while (firstNonZero < coefficientsLength && galoisCoefficients[firstNonZero] == 0)
                {
                    firstNonZero++;
                }

                if (firstNonZero == coefficientsLength)
                {
                    this.coefficients = field.Zero.coefficients;
                }
                else
                {
                    this.coefficients = new int[coefficientsLength - firstNonZero];

                    Array.Copy(galoisCoefficients, firstNonZero, this.coefficients, 0, this.coefficients.Length);
                }
            }
            else
            {
                this.coefficients = galoisCoefficients;
            }
        }
コード例 #7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="QrGaloisFieldPoly"/> class.
        /// </summary>
        /// <param name="field">
        /// The field.
        /// </param>
        /// <param name="galoisCoefficients">
        /// The coefficients.
        /// </param>
        /// <exception cref="ArgumentException">
        /// Argument exception if empty array of coefficients is provided
        /// </exception>
        public QrGaloisFieldPoly(QrGaloisField field, int[] galoisCoefficients)
        {
            if (galoisCoefficients.Length == 0)
            {
                throw new ArgumentException();
            }

            this.field = field;

            int coefficientsLength = galoisCoefficients.Length;

            if (coefficientsLength > 1 && galoisCoefficients[0] == 0)
            {
                // Leading term must be non-zero for anything except the constant polynomial "0"
                int firstNonZero = 1;

                while (firstNonZero < coefficientsLength && galoisCoefficients[firstNonZero] == 0)
                {
                    firstNonZero++;
                }

                if (firstNonZero == coefficientsLength)
                {
                    this.coefficients = field.Zero.coefficients;
                }
                else
                {
                    this.coefficients = new int[coefficientsLength - firstNonZero];

                    Array.Copy(galoisCoefficients, firstNonZero, this.coefficients, 0, this.coefficients.Length);
                }
            }
            else
            {
                this.coefficients = galoisCoefficients;
            }
        }