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.

ZXModulusPoly.m 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Copyright 2012 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. #import "ZXIntArray.h"
  17. #import "ZXModulusGF.h"
  18. #import "ZXModulusPoly.h"
  19. @interface ZXModulusPoly ()
  20. @property (nonatomic, strong, readonly) ZXIntArray *coefficients;
  21. @property (nonatomic, weak, readonly) ZXModulusGF *field;
  22. @end
  23. @implementation ZXModulusPoly
  24. - (id)initWithField:(ZXModulusGF *)field coefficients:(ZXIntArray *)coefficients {
  25. if (self = [super init]) {
  26. if (coefficients.length == 0) {
  27. @throw [NSException exceptionWithName:@"IllegalArgumentException"
  28. reason:@"coefficients must have at least one element"
  29. userInfo:nil];
  30. }
  31. _field = field;
  32. int coefficientsLength = coefficients.length;
  33. if (coefficientsLength > 1 && coefficients.array[0] == 0) {
  34. // Leading term must be non-zero for anything except the constant polynomial "0"
  35. int firstNonZero = 1;
  36. while (firstNonZero < coefficientsLength && coefficients.array[firstNonZero] == 0) {
  37. firstNonZero++;
  38. }
  39. if (firstNonZero == coefficientsLength) {
  40. _coefficients = [[ZXIntArray alloc] initWithLength:1];
  41. } else {
  42. _coefficients = [[ZXIntArray alloc] initWithLength:coefficientsLength - firstNonZero];
  43. for (int i = 0; i < _coefficients.length; i++) {
  44. _coefficients.array[i] = coefficients.array[firstNonZero + i];
  45. }
  46. }
  47. } else {
  48. _coefficients = coefficients;
  49. }
  50. }
  51. return self;
  52. }
  53. /**
  54. * @return degree of this polynomial
  55. */
  56. - (int)degree {
  57. return self.coefficients.length - 1;
  58. }
  59. /**
  60. * @return true iff this polynomial is the monomial "0"
  61. */
  62. - (BOOL)zero {
  63. return self.coefficients.array[0] == 0;
  64. }
  65. /**
  66. * @return coefficient of x^degree term in this polynomial
  67. */
  68. - (int)coefficient:(int)degree {
  69. return self.coefficients.array[self.coefficients.length - 1 - degree];
  70. }
  71. /**
  72. * @return evaluation of this polynomial at a given point
  73. */
  74. - (int)evaluateAt:(int)a {
  75. if (a == 0) {
  76. return [self coefficient:0];
  77. }
  78. int size = self.coefficients.length;
  79. if (a == 1) {
  80. // Just the sum of the coefficients
  81. int result = 0;
  82. for (int i = 0; i < size; i++) {
  83. result = [self.field add:result b:self.coefficients.array[i]];
  84. }
  85. return result;
  86. }
  87. int result = self.coefficients.array[0];
  88. for (int i = 1; i < size; i++) {
  89. result = [self.field add:[self.field multiply:a b:result] b:self.coefficients.array[i]];
  90. }
  91. return result;
  92. }
  93. - (ZXModulusPoly *)add:(ZXModulusPoly *)other {
  94. if (![self.field isEqual:other.field]) {
  95. [NSException raise:NSInvalidArgumentException format:@"ZXModulusPolys do not have same ZXModulusGF field"];
  96. }
  97. if (self.zero) {
  98. return other;
  99. }
  100. if (other.zero) {
  101. return self;
  102. }
  103. ZXIntArray *smallerCoefficients = self.coefficients;
  104. ZXIntArray *largerCoefficients = other.coefficients;
  105. if (smallerCoefficients.length > largerCoefficients.length) {
  106. ZXIntArray *temp = smallerCoefficients;
  107. smallerCoefficients = largerCoefficients;
  108. largerCoefficients = temp;
  109. }
  110. ZXIntArray *sumDiff = [[ZXIntArray alloc] initWithLength:largerCoefficients.length];
  111. int lengthDiff = largerCoefficients.length - smallerCoefficients.length;
  112. // Copy high-order terms only found in higher-degree polynomial's coefficients
  113. memcpy(sumDiff.array, largerCoefficients.array, lengthDiff * sizeof(int32_t));
  114. for (int i = lengthDiff; i < largerCoefficients.length; i++) {
  115. sumDiff.array[i] = [self.field add:smallerCoefficients.array[i - lengthDiff] b:largerCoefficients.array[i]];
  116. }
  117. return [[ZXModulusPoly alloc] initWithField:self.field coefficients:sumDiff];
  118. }
  119. - (ZXModulusPoly *)subtract:(ZXModulusPoly *)other {
  120. if (![self.field isEqual:other.field]) {
  121. [NSException raise:NSInvalidArgumentException format:@"ZXModulusPolys do not have same ZXModulusGF field"];
  122. }
  123. if (self.zero) {
  124. return self;
  125. }
  126. return [self add:[other negative]];
  127. }
  128. - (ZXModulusPoly *)multiply:(ZXModulusPoly *)other {
  129. if (![self.field isEqual:other.field]) {
  130. [NSException raise:NSInvalidArgumentException format:@"ZXModulusPolys do not have same ZXModulusGF field"];
  131. }
  132. if (self.zero || other.zero) {
  133. return self.field.zero;
  134. }
  135. ZXIntArray *aCoefficients = self.coefficients;
  136. int aLength = aCoefficients.length;
  137. ZXIntArray *bCoefficients = other.coefficients;
  138. int bLength = bCoefficients.length;
  139. ZXIntArray *product = [[ZXIntArray alloc] initWithLength:aLength + bLength - 1];
  140. for (int i = 0; i < aLength; i++) {
  141. int aCoeff = aCoefficients.array[i];
  142. for (int j = 0; j < bLength; j++) {
  143. product.array[i + j] = [self.field add:product.array[i + j]
  144. b:[self.field multiply:aCoeff b:bCoefficients.array[j]]];
  145. }
  146. }
  147. return [[ZXModulusPoly alloc] initWithField:self.field coefficients:product];
  148. }
  149. - (ZXModulusPoly *)negative {
  150. int size = self.coefficients.length;
  151. ZXIntArray *negativeCoefficients = [[ZXIntArray alloc] initWithLength:size];
  152. for (int i = 0; i < size; i++) {
  153. negativeCoefficients.array[i] = [self.field subtract:0 b:self.coefficients.array[i]];
  154. }
  155. return [[ZXModulusPoly alloc] initWithField:self.field coefficients:negativeCoefficients];
  156. }
  157. - (ZXModulusPoly *)multiplyScalar:(int)scalar {
  158. if (scalar == 0) {
  159. return self.field.zero;
  160. }
  161. if (scalar == 1) {
  162. return self;
  163. }
  164. int size = self.coefficients.length;
  165. ZXIntArray *product = [[ZXIntArray alloc] initWithLength:size];
  166. for (int i = 0; i < size; i++) {
  167. product.array[i] = [self.field multiply:self.coefficients.array[i] b:scalar];
  168. }
  169. return [[ZXModulusPoly alloc] initWithField:self.field coefficients:product];
  170. }
  171. - (ZXModulusPoly *)multiplyByMonomial:(int)degree coefficient:(int)coefficient {
  172. if (degree < 0) {
  173. [NSException raise:NSInvalidArgumentException format:@"Degree must be greater than 0."];
  174. }
  175. if (coefficient == 0) {
  176. return self.field.zero;
  177. }
  178. int size = self.coefficients.length;
  179. ZXIntArray *product = [[ZXIntArray alloc] initWithLength:size + degree];
  180. for (int i = 0; i < size; i++) {
  181. product.array[i] = [self.field multiply:self.coefficients.array[i] b:coefficient];
  182. }
  183. return [[ZXModulusPoly alloc] initWithField:self.field coefficients:product];
  184. }
  185. - (NSArray *)divide:(ZXModulusPoly *)other {
  186. if (![self.field isEqual:other.field]) {
  187. [NSException raise:NSInvalidArgumentException format:@"ZXModulusPolys do not have same ZXModulusGF field"];
  188. }
  189. if (other.zero) {
  190. [NSException raise:NSInvalidArgumentException format:@"Divide by 0"];
  191. }
  192. ZXModulusPoly *quotient = self.field.zero;
  193. ZXModulusPoly *remainder = self;
  194. int denominatorLeadingTerm = [other coefficient:other.degree];
  195. int inverseDenominatorLeadingTerm = [self.field inverse:denominatorLeadingTerm];
  196. while ([remainder degree] >= other.degree && !remainder.zero) {
  197. int degreeDifference = remainder.degree - other.degree;
  198. int scale = [self.field multiply:[remainder coefficient:remainder.degree] b:inverseDenominatorLeadingTerm];
  199. ZXModulusPoly *term = [other multiplyByMonomial:degreeDifference coefficient:scale];
  200. ZXModulusPoly *iterationQuotient = [self.field buildMonomial:degreeDifference coefficient:scale];
  201. quotient = [quotient add:iterationQuotient];
  202. remainder = [remainder subtract:term];
  203. }
  204. return @[quotient, remainder];
  205. }
  206. - (NSString *)description {
  207. NSMutableString *result = [NSMutableString stringWithCapacity:8 * [self degree]];
  208. for (int degree = [self degree]; degree >= 0; degree--) {
  209. int coefficient = [self coefficient:degree];
  210. if (coefficient != 0) {
  211. if (coefficient < 0) {
  212. [result appendString:@" - "];
  213. coefficient = -coefficient;
  214. } else {
  215. if ([result length] > 0) {
  216. [result appendString:@" + "];
  217. }
  218. }
  219. if (degree == 0 || coefficient != 1) {
  220. [result appendFormat:@"%d", coefficient];
  221. }
  222. if (degree != 0) {
  223. if (degree == 1) {
  224. [result appendString:@"x"];
  225. } else {
  226. [result appendString:@"x^"];
  227. [result appendFormat:@"%d", degree];
  228. }
  229. }
  230. }
  231. }
  232. return result;
  233. }
  234. @end