You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

GenericGFPoly.cs 8.1 kB

10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright 2007 ZXing authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. using System;
  17. using System.Text;
  18. namespace ZXing.Common.ReedSolomon
  19. {
  20. /// <summary>
  21. /// <p>Represents a polynomial whose coefficients are elements of a GF.
  22. /// Instances of this class are immutable.</p>
  23. /// <p>Much credit is due to William Rucklidge since portions of this code are an indirect
  24. /// port of his C++ Reed-Solomon implementation.</p>
  25. /// </summary>
  26. /// <author>Sean Owen</author>
  27. internal sealed class GenericGFPoly
  28. {
  29. private readonly GenericGF field;
  30. private readonly int[] coefficients;
  31. /// <summary>
  32. /// Initializes a new instance of the <see cref="GenericGFPoly"/> class.
  33. /// </summary>
  34. /// <param name="field">the {@link GenericGF} instance representing the field to use
  35. /// to perform computations</param>
  36. /// <param name="coefficients">coefficients as ints representing elements of GF(size), arranged
  37. /// from most significant (highest-power term) coefficient to least significant</param>
  38. /// <exception cref="ArgumentException">if argument is null or empty,
  39. /// or if leading coefficient is 0 and this is not a
  40. /// constant polynomial (that is, it is not the monomial "0")</exception>
  41. internal GenericGFPoly(GenericGF field, int[] coefficients)
  42. {
  43. if (coefficients.Length == 0)
  44. {
  45. throw new ArgumentException();
  46. }
  47. this.field = field;
  48. int coefficientsLength = coefficients.Length;
  49. if (coefficientsLength > 1 && coefficients[0] == 0)
  50. {
  51. // Leading term must be non-zero for anything except the constant polynomial "0"
  52. int firstNonZero = 1;
  53. while (firstNonZero < coefficientsLength && coefficients[firstNonZero] == 0)
  54. {
  55. firstNonZero++;
  56. }
  57. if (firstNonZero == coefficientsLength)
  58. {
  59. this.coefficients = new int[]{0};
  60. }
  61. else
  62. {
  63. this.coefficients = new int[coefficientsLength - firstNonZero];
  64. Array.Copy(coefficients,
  65. firstNonZero,
  66. this.coefficients,
  67. 0,
  68. this.coefficients.Length);
  69. }
  70. }
  71. else
  72. {
  73. this.coefficients = coefficients;
  74. }
  75. }
  76. internal int[] Coefficients
  77. {
  78. get { return coefficients; }
  79. }
  80. /// <summary>
  81. /// degree of this polynomial
  82. /// </summary>
  83. internal int Degree
  84. {
  85. get
  86. {
  87. return coefficients.Length - 1;
  88. }
  89. }
  90. /// <summary>
  91. /// Gets a value indicating whether this <see cref="GenericGFPoly"/> is zero.
  92. /// </summary>
  93. /// <value>true iff this polynomial is the monomial "0"</value>
  94. internal bool isZero
  95. {
  96. get { return coefficients[0] == 0; }
  97. }
  98. /// <summary>
  99. /// coefficient of x^degree term in this polynomial
  100. /// </summary>
  101. /// <param name="degree">The degree.</param>
  102. /// <returns>coefficient of x^degree term in this polynomial</returns>
  103. internal int getCoefficient(int degree)
  104. {
  105. return coefficients[coefficients.Length - 1 - degree];
  106. }
  107. internal GenericGFPoly addOrSubtract(GenericGFPoly other)
  108. {
  109. if (!field.Equals(other.field))
  110. {
  111. throw new ArgumentException("GenericGFPolys do not have same GenericGF field");
  112. }
  113. if (isZero)
  114. {
  115. return other;
  116. }
  117. if (other.isZero)
  118. {
  119. return this;
  120. }
  121. int[] smallerCoefficients = this.coefficients;
  122. int[] largerCoefficients = other.coefficients;
  123. if (smallerCoefficients.Length > largerCoefficients.Length)
  124. {
  125. int[] temp = smallerCoefficients;
  126. smallerCoefficients = largerCoefficients;
  127. largerCoefficients = temp;
  128. }
  129. int[] sumDiff = new int[largerCoefficients.Length];
  130. int lengthDiff = largerCoefficients.Length - smallerCoefficients.Length;
  131. // Copy high-order terms only found in higher-degree polynomial's coefficients
  132. Array.Copy(largerCoefficients, 0, sumDiff, 0, lengthDiff);
  133. for (int i = lengthDiff; i < largerCoefficients.Length; i++)
  134. {
  135. sumDiff[i] = GenericGF.addOrSubtract(smallerCoefficients[i - lengthDiff], largerCoefficients[i]);
  136. }
  137. return new GenericGFPoly(field, sumDiff);
  138. }
  139. internal GenericGFPoly multiply(GenericGFPoly other)
  140. {
  141. if (!field.Equals(other.field))
  142. {
  143. throw new ArgumentException("GenericGFPolys do not have same GenericGF field");
  144. }
  145. if (isZero || other.isZero)
  146. {
  147. return field.Zero;
  148. }
  149. int[] aCoefficients = this.coefficients;
  150. int aLength = aCoefficients.Length;
  151. int[] bCoefficients = other.coefficients;
  152. int bLength = bCoefficients.Length;
  153. int[] product = new int[aLength + bLength - 1];
  154. for (int i = 0; i < aLength; i++)
  155. {
  156. int aCoeff = aCoefficients[i];
  157. for (int j = 0; j < bLength; j++)
  158. {
  159. product[i + j] = GenericGF.addOrSubtract(product[i + j],
  160. field.multiply(aCoeff, bCoefficients[j]));
  161. }
  162. }
  163. return new GenericGFPoly(field, product);
  164. }
  165. internal GenericGFPoly multiplyByMonomial(int degree, int coefficient)
  166. {
  167. if (degree < 0)
  168. {
  169. throw new ArgumentException();
  170. }
  171. if (coefficient == 0)
  172. {
  173. return field.Zero;
  174. }
  175. int size = coefficients.Length;
  176. int[] product = new int[size + degree];
  177. for (int i = 0; i < size; i++)
  178. {
  179. product[i] = field.multiply(coefficients[i], coefficient);
  180. }
  181. return new GenericGFPoly(field, product);
  182. }
  183. internal GenericGFPoly[] divide(GenericGFPoly other)
  184. {
  185. if (!field.Equals(other.field))
  186. {
  187. throw new ArgumentException("GenericGFPolys do not have same GenericGF field");
  188. }
  189. if (other.isZero)
  190. {
  191. throw new ArgumentException("Divide by 0");
  192. }
  193. GenericGFPoly quotient = field.Zero;
  194. GenericGFPoly remainder = this;
  195. int denominatorLeadingTerm = other.getCoefficient(other.Degree);
  196. int inverseDenominatorLeadingTerm = field.inverse(denominatorLeadingTerm);
  197. while (remainder.Degree >= other.Degree && !remainder.isZero)
  198. {
  199. int degreeDifference = remainder.Degree - other.Degree;
  200. int scale = field.multiply(remainder.getCoefficient(remainder.Degree), inverseDenominatorLeadingTerm);
  201. GenericGFPoly term = other.multiplyByMonomial(degreeDifference, scale);
  202. GenericGFPoly iterationQuotient = field.buildMonomial(degreeDifference, scale);
  203. quotient = quotient.addOrSubtract(iterationQuotient);
  204. remainder = remainder.addOrSubtract(term);
  205. }
  206. return new GenericGFPoly[] { quotient, remainder };
  207. }
  208. }
  209. }