GenericGFPoly.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. /*
  3. * Copyright 2007 ZXing authors
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace Zxing\Common\Reedsolomon;
  18. /**
  19. * <p>Represents a polynomial whose coefficients are elements of a GF.
  20. * Instances of this class are immutable.</p>
  21. *
  22. * <p>Much credit is due to William Rucklidge since portions of this code are an indirect
  23. * port of his C++ Reed-Solomon implementation.</p>
  24. *
  25. * @author Sean Owen
  26. */
  27. final class GenericGFPoly
  28. {
  29. /**
  30. * @var int[]|mixed|null
  31. */
  32. private $coefficients;
  33. /**
  34. * @param the $field {@link GenericGF} instance representing the field to use
  35. * to perform computations
  36. * @param array $coefficients coefficients as ints representing elements of GF(size), arranged
  37. * from most significant (highest-power term) coefficient to least significant
  38. *
  39. * @throws InvalidArgumentException if argument is null or empty,
  40. * or if leading coefficient is 0 and this is not a
  41. * constant polynomial (that is, it is not the monomial "0")
  42. */
  43. public function __construct(private $field, $coefficients)
  44. {
  45. if (count($coefficients) == 0) {
  46. throw new \InvalidArgumentException();
  47. }
  48. $coefficientsLength = count($coefficients);
  49. if ($coefficientsLength > 1 && $coefficients[0] == 0) {
  50. // Leading term must be non-zero for anything except the constant polynomial "0"
  51. $firstNonZero = 1;
  52. while ($firstNonZero < $coefficientsLength && $coefficients[$firstNonZero] == 0) {
  53. $firstNonZero++;
  54. }
  55. if ($firstNonZero == $coefficientsLength) {
  56. $this->coefficients = [0];
  57. } else {
  58. $this->coefficients = fill_array(0, $coefficientsLength - $firstNonZero, 0);
  59. $this->coefficients = arraycopy(
  60. $coefficients,
  61. $firstNonZero,
  62. $this->coefficients,
  63. 0,
  64. is_countable($this->coefficients) ? count($this->coefficients) : 0
  65. );
  66. }
  67. } else {
  68. $this->coefficients = $coefficients;
  69. }
  70. }
  71. public function getCoefficients()
  72. {
  73. return $this->coefficients;
  74. }
  75. /**
  76. * @return evaluation of this polynomial at a given point
  77. */
  78. public function evaluateAt($a)
  79. {
  80. if ($a == 0) {
  81. // Just return the x^0 coefficient
  82. return $this->getCoefficient(0);
  83. }
  84. $size = is_countable($this->coefficients) ? count($this->coefficients) : 0;
  85. if ($a == 1) {
  86. // Just the sum of the coefficients
  87. $result = 0;
  88. foreach ($this->coefficients as $coefficient) {
  89. $result = GenericGF::addOrSubtract($result, $coefficient);
  90. }
  91. return $result;
  92. }
  93. $result = $this->coefficients[0];
  94. for ($i = 1; $i < $size; $i++) {
  95. $result = GenericGF::addOrSubtract($this->field->multiply($a, $result), $this->coefficients[$i]);
  96. }
  97. return $result;
  98. }
  99. /**
  100. * @return coefficient of x^degree term in this polynomial
  101. */
  102. public function getCoefficient($degree)
  103. {
  104. return $this->coefficients[(is_countable($this->coefficients) ? count($this->coefficients) : 0) - 1 - $degree];
  105. }
  106. public function multiply($other)
  107. {
  108. $aCoefficients = [];
  109. $bCoefficients = [];
  110. $aLength = null;
  111. $bLength = null;
  112. $product = [];
  113. if (is_int($other)) {
  114. return $this->multiply_($other);
  115. }
  116. if ($this->field !== $other->field) {
  117. throw new \InvalidArgumentException("GenericGFPolys do not have same GenericGF field");
  118. }
  119. if ($this->isZero() || $other->isZero()) {
  120. return $this->field->getZero();
  121. }
  122. $aCoefficients = $this->coefficients;
  123. $aLength = count($aCoefficients);
  124. $bCoefficients = $other->coefficients;
  125. $bLength = count($bCoefficients);
  126. $product = fill_array(0, $aLength + $bLength - 1, 0);
  127. for ($i = 0; $i < $aLength; $i++) {
  128. $aCoeff = $aCoefficients[$i];
  129. for ($j = 0; $j < $bLength; $j++) {
  130. $product[$i + $j] = GenericGF::addOrSubtract(
  131. $product[$i + $j],
  132. $this->field->multiply($aCoeff, $bCoefficients[$j])
  133. );
  134. }
  135. }
  136. return new GenericGFPoly($this->field, $product);
  137. }
  138. public function multiply_($scalar)
  139. {
  140. if ($scalar == 0) {
  141. return $this->field->getZero();
  142. }
  143. if ($scalar == 1) {
  144. return $this;
  145. }
  146. $size = is_countable($this->coefficients) ? count($this->coefficients) : 0;
  147. $product = fill_array(0, $size, 0);
  148. for ($i = 0; $i < $size; $i++) {
  149. $product[$i] = $this->field->multiply($this->coefficients[$i], $scalar);
  150. }
  151. return new GenericGFPoly($this->field, $product);
  152. }
  153. /**
  154. * @return true iff this polynomial is the monomial "0"
  155. */
  156. public function isZero()
  157. {
  158. return $this->coefficients[0] == 0;
  159. }
  160. public function multiplyByMonomial($degree, $coefficient)
  161. {
  162. if ($degree < 0) {
  163. throw new \InvalidArgumentException();
  164. }
  165. if ($coefficient == 0) {
  166. return $this->field->getZero();
  167. }
  168. $size = is_countable($this->coefficients) ? count($this->coefficients) : 0;
  169. $product = fill_array(0, $size + $degree, 0);
  170. for ($i = 0; $i < $size; $i++) {
  171. $product[$i] = $this->field->multiply($this->coefficients[$i], $coefficient);
  172. }
  173. return new GenericGFPoly($this->field, $product);
  174. }
  175. public function divide($other)
  176. {
  177. if ($this->field !== $other->field) {
  178. throw new \InvalidArgumentException("GenericGFPolys do not have same GenericGF field");
  179. }
  180. if ($other->isZero()) {
  181. throw new \InvalidArgumentException("Divide by 0");
  182. }
  183. $quotient = $this->field->getZero();
  184. $remainder = $this;
  185. $denominatorLeadingTerm = $other->getCoefficient($other->getDegree());
  186. $inverseDenominatorLeadingTerm = $this->field->inverse($denominatorLeadingTerm);
  187. while ($remainder->getDegree() >= $other->getDegree() && !$remainder->isZero()) {
  188. $degreeDifference = $remainder->getDegree() - $other->getDegree();
  189. $scale = $this->field->multiply($remainder->getCoefficient($remainder->getDegree()), $inverseDenominatorLeadingTerm);
  190. $term = $other->multiplyByMonomial($degreeDifference, $scale);
  191. $iterationQuotient = $this->field->buildMonomial($degreeDifference, $scale);
  192. $quotient = $quotient->addOrSubtract($iterationQuotient);
  193. $remainder = $remainder->addOrSubtract($term);
  194. }
  195. return [$quotient, $remainder];
  196. }
  197. /**
  198. * @return degree of this polynomial
  199. */
  200. public function getDegree()
  201. {
  202. return (is_countable($this->coefficients) ? count($this->coefficients) : 0) - 1;
  203. }
  204. public function addOrSubtract($other)
  205. {
  206. $smallerCoefficients = [];
  207. $largerCoefficients = [];
  208. $sumDiff = [];
  209. $lengthDiff = null;
  210. $countLargerCoefficients = null;
  211. if ($this->field !== $other->field) {
  212. throw new \InvalidArgumentException("GenericGFPolys do not have same GenericGF field");
  213. }
  214. if ($this->isZero()) {
  215. return $other;
  216. }
  217. if ($other->isZero()) {
  218. return $this;
  219. }
  220. $smallerCoefficients = $this->coefficients;
  221. $largerCoefficients = $other->coefficients;
  222. if (count($smallerCoefficients) > count($largerCoefficients)) {
  223. $temp = $smallerCoefficients;
  224. $smallerCoefficients = $largerCoefficients;
  225. $largerCoefficients = $temp;
  226. }
  227. $sumDiff = fill_array(0, count($largerCoefficients), 0);
  228. $lengthDiff = count($largerCoefficients) - count($smallerCoefficients);
  229. // Copy high-order terms only found in higher-degree polynomial's coefficients
  230. $sumDiff = arraycopy($largerCoefficients, 0, $sumDiff, 0, $lengthDiff);
  231. $countLargerCoefficients = count($largerCoefficients);
  232. for ($i = $lengthDiff; $i < $countLargerCoefficients; $i++) {
  233. $sumDiff[$i] = GenericGF::addOrSubtract($smallerCoefficients[$i - $lengthDiff], $largerCoefficients[$i]);
  234. }
  235. return new GenericGFPoly($this->field, $sumDiff);
  236. }
  237. //@Override
  238. public function toString()
  239. {
  240. $result = '';
  241. for ($degree = $this->getDegree(); $degree >= 0; $degree--) {
  242. $coefficient = $this->getCoefficient($degree);
  243. if ($coefficient != 0) {
  244. if ($coefficient < 0) {
  245. $result .= " - ";
  246. $coefficient = -$coefficient;
  247. } else {
  248. if (strlen((string) $result) > 0) {
  249. $result .= " + ";
  250. }
  251. }
  252. if ($degree == 0 || $coefficient != 1) {
  253. $alphaPower = $this->field->log($coefficient);
  254. if ($alphaPower == 0) {
  255. $result .= '1';
  256. } elseif ($alphaPower == 1) {
  257. $result .= 'a';
  258. } else {
  259. $result .= "a^";
  260. $result .= ($alphaPower);
  261. }
  262. }
  263. if ($degree != 0) {
  264. if ($degree == 1) {
  265. $result .= 'x';
  266. } else {
  267. $result .= "x^";
  268. $result .= $degree;
  269. }
  270. }
  271. }
  272. }
  273. return $result;
  274. }
  275. }