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

229 lines
6.1 KiB

  1. /*
  2. Ported to JavaScript by Lazar Laszlo 2011
  3. lazarsoft@gmail.com, www.lazarsoft.info
  4. */
  5. /*
  6. *
  7. * Copyright 2007 ZXing authors
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License");
  10. * you may not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS,
  17. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. */
  21. function GF256Poly(field, coefficients)
  22. {
  23. if (coefficients == null || coefficients.length == 0)
  24. {
  25. throw "System.ArgumentException";
  26. }
  27. this.field = field;
  28. var coefficientsLength = coefficients.length;
  29. if (coefficientsLength > 1 && coefficients[0] == 0)
  30. {
  31. // Leading term must be non-zero for anything except the constant polynomial "0"
  32. var firstNonZero = 1;
  33. while (firstNonZero < coefficientsLength && coefficients[firstNonZero] == 0)
  34. {
  35. firstNonZero++;
  36. }
  37. if (firstNonZero == coefficientsLength)
  38. {
  39. this.coefficients = field.Zero.coefficients;
  40. }
  41. else
  42. {
  43. this.coefficients = new Array(coefficientsLength - firstNonZero);
  44. for(var i=0;i<this.coefficients.length;i++)this.coefficients[i]=0;
  45. //Array.Copy(coefficients, firstNonZero, this.coefficients, 0, this.coefficients.length);
  46. for(var ci=0;ci<this.coefficients.length;ci++)this.coefficients[ci]=coefficients[firstNonZero+ci];
  47. }
  48. }
  49. else
  50. {
  51. this.coefficients = coefficients;
  52. }
  53. this.__defineGetter__("Zero", function()
  54. {
  55. return this.coefficients[0] == 0;
  56. });
  57. this.__defineGetter__("Degree", function()
  58. {
  59. return this.coefficients.length - 1;
  60. });
  61. this.__defineGetter__("Coefficients", function()
  62. {
  63. return this.coefficients;
  64. });
  65. this.getCoefficient=function( degree)
  66. {
  67. return this.coefficients[this.coefficients.length - 1 - degree];
  68. }
  69. this.evaluateAt=function( a)
  70. {
  71. if (a == 0)
  72. {
  73. // Just return the x^0 coefficient
  74. return this.getCoefficient(0);
  75. }
  76. var size = this.coefficients.length;
  77. if (a == 1)
  78. {
  79. // Just the sum of the coefficients
  80. var result = 0;
  81. for (var i = 0; i < size; i++)
  82. {
  83. result = GF256.addOrSubtract(result, this.coefficients[i]);
  84. }
  85. return result;
  86. }
  87. var result2 = this.coefficients[0];
  88. for (var i = 1; i < size; i++)
  89. {
  90. result2 = GF256.addOrSubtract(this.field.multiply(a, result2), this.coefficients[i]);
  91. }
  92. return result2;
  93. }
  94. this.addOrSubtract=function( other)
  95. {
  96. if (this.field != other.field)
  97. {
  98. throw "GF256Polys do not have same GF256 field";
  99. }
  100. if (this.Zero)
  101. {
  102. return other;
  103. }
  104. if (other.Zero)
  105. {
  106. return this;
  107. }
  108. var smallerCoefficients = this.coefficients;
  109. var largerCoefficients = other.coefficients;
  110. if (smallerCoefficients.length > largerCoefficients.length)
  111. {
  112. var temp = smallerCoefficients;
  113. smallerCoefficients = largerCoefficients;
  114. largerCoefficients = temp;
  115. }
  116. var sumDiff = new Array(largerCoefficients.length);
  117. var lengthDiff = largerCoefficients.length - smallerCoefficients.length;
  118. // Copy high-order terms only found in higher-degree polynomial's coefficients
  119. //Array.Copy(largerCoefficients, 0, sumDiff, 0, lengthDiff);
  120. for(var ci=0;ci<lengthDiff;ci++)sumDiff[ci]=largerCoefficients[ci];
  121. for (var i = lengthDiff; i < largerCoefficients.length; i++)
  122. {
  123. sumDiff[i] = GF256.addOrSubtract(smallerCoefficients[i - lengthDiff], largerCoefficients[i]);
  124. }
  125. return new GF256Poly(field, sumDiff);
  126. }
  127. this.multiply1=function( other)
  128. {
  129. if (this.field!=other.field)
  130. {
  131. throw "GF256Polys do not have same GF256 field";
  132. }
  133. if (this.Zero || other.Zero)
  134. {
  135. return this.field.Zero;
  136. }
  137. var aCoefficients = this.coefficients;
  138. var aLength = aCoefficients.length;
  139. var bCoefficients = other.coefficients;
  140. var bLength = bCoefficients.length;
  141. var product = new Array(aLength + bLength - 1);
  142. for (var i = 0; i < aLength; i++)
  143. {
  144. var aCoeff = aCoefficients[i];
  145. for (var j = 0; j < bLength; j++)
  146. {
  147. product[i + j] = GF256.addOrSubtract(product[i + j], this.field.multiply(aCoeff, bCoefficients[j]));
  148. }
  149. }
  150. return new GF256Poly(this.field, product);
  151. }
  152. this.multiply2=function( scalar)
  153. {
  154. if (scalar == 0)
  155. {
  156. return this.field.Zero;
  157. }
  158. if (scalar == 1)
  159. {
  160. return this;
  161. }
  162. var size = this.coefficients.length;
  163. var product = new Array(size);
  164. for (var i = 0; i < size; i++)
  165. {
  166. product[i] = this.field.multiply(this.coefficients[i], scalar);
  167. }
  168. return new GF256Poly(this.field, product);
  169. }
  170. this.multiplyByMonomial=function( degree, coefficient)
  171. {
  172. if (degree < 0)
  173. {
  174. throw "System.ArgumentException";
  175. }
  176. if (coefficient == 0)
  177. {
  178. return this.field.Zero;
  179. }
  180. var size = this.coefficients.length;
  181. var product = new Array(size + degree);
  182. for(var i=0;i<product.length;i++)product[i]=0;
  183. for (var i = 0; i < size; i++)
  184. {
  185. product[i] = this.field.multiply(this.coefficients[i], coefficient);
  186. }
  187. return new GF256Poly(this.field, product);
  188. }
  189. this.divide=function( other)
  190. {
  191. if (this.field!=other.field)
  192. {
  193. throw "GF256Polys do not have same GF256 field";
  194. }
  195. if (other.Zero)
  196. {
  197. throw "Divide by 0";
  198. }
  199. var quotient = this.field.Zero;
  200. var remainder = this;
  201. var denominatorLeadingTerm = other.getCoefficient(other.Degree);
  202. var inverseDenominatorLeadingTerm = this.field.inverse(denominatorLeadingTerm);
  203. while (remainder.Degree >= other.Degree && !remainder.Zero)
  204. {
  205. var degreeDifference = remainder.Degree - other.Degree;
  206. var scale = this.field.multiply(remainder.getCoefficient(remainder.Degree), inverseDenominatorLeadingTerm);
  207. var term = other.multiplyByMonomial(degreeDifference, scale);
  208. var iterationQuotient = this.field.buildMonomial(degreeDifference, scale);
  209. quotient = quotient.addOrSubtract(iterationQuotient);
  210. remainder = remainder.addOrSubtract(term);
  211. }
  212. return new Array(quotient, remainder);
  213. }
  214. }