您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ZXGenericGFPoly.m 8.2KB

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