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 11 kB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 = field.Zero.coefficients;
  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. /// <summary>
  108. /// evaluation of this polynomial at a given point
  109. /// </summary>
  110. /// <param name="a">A.</param>
  111. /// <returns>evaluation of this polynomial at a given point</returns>
  112. internal int evaluateAt(int a)
  113. {
  114. int result = 0;
  115. if (a == 0)
  116. {
  117. // Just return the x^0 coefficient
  118. return getCoefficient(0);
  119. }
  120. int size = coefficients.Length;
  121. if (a == 1)
  122. {
  123. // Just the sum of the coefficients
  124. foreach (var coefficient in coefficients)
  125. {
  126. result = GenericGF.addOrSubtract(result, coefficient);
  127. }
  128. return result;
  129. }
  130. result = coefficients[0];
  131. for (int i = 1; i < size; i++)
  132. {
  133. result = GenericGF.addOrSubtract(field.multiply(a, result), coefficients[i]);
  134. }
  135. return result;
  136. }
  137. internal GenericGFPoly addOrSubtract(GenericGFPoly other)
  138. {
  139. if (!field.Equals(other.field))
  140. {
  141. throw new ArgumentException("GenericGFPolys do not have same GenericGF field");
  142. }
  143. if (isZero)
  144. {
  145. return other;
  146. }
  147. if (other.isZero)
  148. {
  149. return this;
  150. }
  151. int[] smallerCoefficients = this.coefficients;
  152. int[] largerCoefficients = other.coefficients;
  153. if (smallerCoefficients.Length > largerCoefficients.Length)
  154. {
  155. int[] temp = smallerCoefficients;
  156. smallerCoefficients = largerCoefficients;
  157. largerCoefficients = temp;
  158. }
  159. int[] sumDiff = new int[largerCoefficients.Length];
  160. int lengthDiff = largerCoefficients.Length - smallerCoefficients.Length;
  161. // Copy high-order terms only found in higher-degree polynomial's coefficients
  162. Array.Copy(largerCoefficients, 0, sumDiff, 0, lengthDiff);
  163. for (int i = lengthDiff; i < largerCoefficients.Length; i++)
  164. {
  165. sumDiff[i] = GenericGF.addOrSubtract(smallerCoefficients[i - lengthDiff], largerCoefficients[i]);
  166. }
  167. return new GenericGFPoly(field, sumDiff);
  168. }
  169. internal GenericGFPoly multiply(GenericGFPoly other)
  170. {
  171. if (!field.Equals(other.field))
  172. {
  173. throw new ArgumentException("GenericGFPolys do not have same GenericGF field");
  174. }
  175. if (isZero || other.isZero)
  176. {
  177. return field.Zero;
  178. }
  179. int[] aCoefficients = this.coefficients;
  180. int aLength = aCoefficients.Length;
  181. int[] bCoefficients = other.coefficients;
  182. int bLength = bCoefficients.Length;
  183. int[] product = new int[aLength + bLength - 1];
  184. for (int i = 0; i < aLength; i++)
  185. {
  186. int aCoeff = aCoefficients[i];
  187. for (int j = 0; j < bLength; j++)
  188. {
  189. product[i + j] = GenericGF.addOrSubtract(product[i + j],
  190. field.multiply(aCoeff, bCoefficients[j]));
  191. }
  192. }
  193. return new GenericGFPoly(field, product);
  194. }
  195. internal GenericGFPoly multiply(int scalar)
  196. {
  197. if (scalar == 0)
  198. {
  199. return field.Zero;
  200. }
  201. if (scalar == 1)
  202. {
  203. return this;
  204. }
  205. int size = coefficients.Length;
  206. int[] product = new int[size];
  207. for (int i = 0; i < size; i++)
  208. {
  209. product[i] = field.multiply(coefficients[i], scalar);
  210. }
  211. return new GenericGFPoly(field, product);
  212. }
  213. internal GenericGFPoly multiplyByMonomial(int degree, int coefficient)
  214. {
  215. if (degree < 0)
  216. {
  217. throw new ArgumentException();
  218. }
  219. if (coefficient == 0)
  220. {
  221. return field.Zero;
  222. }
  223. int size = coefficients.Length;
  224. int[] product = new int[size + degree];
  225. for (int i = 0; i < size; i++)
  226. {
  227. product[i] = field.multiply(coefficients[i], coefficient);
  228. }
  229. return new GenericGFPoly(field, product);
  230. }
  231. internal GenericGFPoly[] divide(GenericGFPoly other)
  232. {
  233. if (!field.Equals(other.field))
  234. {
  235. throw new ArgumentException("GenericGFPolys do not have same GenericGF field");
  236. }
  237. if (other.isZero)
  238. {
  239. throw new ArgumentException("Divide by 0");
  240. }
  241. GenericGFPoly quotient = field.Zero;
  242. GenericGFPoly remainder = this;
  243. int denominatorLeadingTerm = other.getCoefficient(other.Degree);
  244. int inverseDenominatorLeadingTerm = field.inverse(denominatorLeadingTerm);
  245. while (remainder.Degree >= other.Degree && !remainder.isZero)
  246. {
  247. int degreeDifference = remainder.Degree - other.Degree;
  248. int scale = field.multiply(remainder.getCoefficient(remainder.Degree), inverseDenominatorLeadingTerm);
  249. GenericGFPoly term = other.multiplyByMonomial(degreeDifference, scale);
  250. GenericGFPoly iterationQuotient = field.buildMonomial(degreeDifference, scale);
  251. quotient = quotient.addOrSubtract(iterationQuotient);
  252. remainder = remainder.addOrSubtract(term);
  253. }
  254. return new GenericGFPoly[] { quotient, remainder };
  255. }
  256. public override String ToString()
  257. {
  258. StringBuilder result = new StringBuilder(8 * Degree);
  259. for (int degree = Degree; degree >= 0; degree--)
  260. {
  261. int coefficient = getCoefficient(degree);
  262. if (coefficient != 0)
  263. {
  264. if (coefficient < 0)
  265. {
  266. result.Append(" - ");
  267. coefficient = -coefficient;
  268. }
  269. else
  270. {
  271. if (result.Length > 0)
  272. {
  273. result.Append(" + ");
  274. }
  275. }
  276. if (degree == 0 || coefficient != 1)
  277. {
  278. int alphaPower = field.log(coefficient);
  279. if (alphaPower == 0)
  280. {
  281. result.Append('1');
  282. }
  283. else if (alphaPower == 1)
  284. {
  285. result.Append('a');
  286. }
  287. else
  288. {
  289. result.Append("a^");
  290. result.Append(alphaPower);
  291. }
  292. }
  293. if (degree != 0)
  294. {
  295. if (degree == 1)
  296. {
  297. result.Append('x');
  298. }
  299. else
  300. {
  301. result.Append("x^");
  302. result.Append(degree);
  303. }
  304. }
  305. }
  306. }
  307. return result.ToString();
  308. }
  309. }
  310. }