def test_is_prime_power_really_big_prime(): value = 11113 error_message = re.escape(" ".join([ "is_prime_power() cannot handle the value 11113", "which is greater or equal to maximum value 10609" ])) with pytest.raises(ValueError, match=error_message): is_prime_power(value)
def __init__(self, square_size: int, dtype=DEFAULT_NDARRAY_TYPE): num_squares = 1 if utils.is_prime_power(square_size) != 0: num_squares = square_size - 1 self.squares = np.zeros((num_squares, square_size, square_size), dtype=dtype) if num_squares == 1: self.generate_single_square(square_size) else: self.generate_from_field(square_size)
def generate_oa(self) -> np.ndarray: squares = LatinSquares(self.num_values, dtype=self.ndarray_type).squares num_parms = self.num_values + 1 if utils.is_prime_power(self.num_values) else 3 num_configs = self.num_values ** self.degree result = np.empty((num_configs, num_parms), dtype=self.ndarray_type) for config_index in range(0, num_configs): matrix_row = config_index // self.num_values matrix_col = config_index % self.num_values result[config_index][0] = matrix_row + 1 result[config_index][1] = matrix_col + 1 for parm_index in range(2, num_parms): result[config_index][parm_index] = 1 + \ squares[parm_index - 2][matrix_row][matrix_col] return result
def _generate(self, order: int): # Set up vector of Galois field elements prime_factor = utils.is_prime_power(order) if prime_factor == 1: # Order is prime # Field is integers modulo order. Set IntMod modulus IntMod.modulus = order # Field elements are RemainderPolys, with simple IntMod coefficients, 0 to order - 1 for value in range(0, order): self.elements.append(RemainderPoly(coef_list=[IntMod(value)])) elif prime_factor > 0: # Order = primeFactor ^ quoDegree, where quoDegree is an # integer > 1 # Set modulus for IntMod coefficients to be primeFactor IntMod.modulus = prime_factor # Find irreducible polynomial of degree quoDegree quo_degree = int(math.log(order, prime_factor)) quotient = _find_irreducible_poly(quo_degree) # Irreducible polynomial becomes quotient polynomial for # extending the field of integers modulo primeFactor (which has # order = primeFactor) to a field with order # primeFactor ^ quoDegree RemainderPoly.quotient = quotient # Field elements will be the set of equivalence classes of # polynomial residues with respect to the quotient polynomial. # These residues will be polynomials of (maximum) degree # quoDegree - 1. degree = quo_degree - 1 # Because the polynomial coefficients are IntMods, there are # only 'primeFactor' possibilities for each coefficient. # Therefore, there are exactly primeFactor * quoDegree # polynomials, which is exactly what we want for the number of # elements in the field. # Generate possible polynomials in enumeration order. That is, # start with all coefficients as zero. Increment the constant # coefficient. When the constant coefficient increments to # zero, increment next coefficient, and keep going until some # coefficient increments to a non-zero value. # Generate field element zero. Take copy of polynomial to avoid # anomalies. element_generator = RemainderPoly(coef_list=[]) for _ in range(0, degree + 1): element_generator.coef_list.append(copy(IntMod(0))) next_element = deepcopy(element_generator) next_element.collapse_degree() self.elements.append(next_element) # Generate rest of elements for _ in range(1, order): # Try next polynomial, by incrementing constant coefficient. increment_index = 0 element_generator[increment_index] += 1 # If current coefficient is zero after increment, we have # looped through the entire set of values mod modulus. # Increment the coefficient of next higher degree. Continue # until the increment operation produces a non-zero # coefficient. The for loop limits us so that we don't # increment the final coefficient to zero and overflow the # array. while element_generator[increment_index] == IntMod(0): increment_index += 1 element_generator[increment_index] += 1 # Take copy of polynomial, and create new element next_element = deepcopy(element_generator) next_element.collapse_degree() self.elements.append(next_element) else: # order is invalid raise ArithmeticError(f"Field order {order} is not a prime power")
def test_is_prime_power_bigger_prime(): value = 10243 assert is_prime_power(value) == 1
def test_is_prime_power_big_prime(): value = 197 assert is_prime_power(value) == 1
def test_is_prime_power_small_prime(): value = 29 assert is_prime_power(value) == 1