Example #1
0
 def t_order(self, v=2):
     """
     Compute order using Terr's Baby-step Giant-step algorithm.
     """
     if (v < 1) or not(rational.isIntegerObject(v)):
         raise TypeError("input integer v >= 1")
     e = self.set.identity()
     a = self.set.identity()
     R = [(e, 0)]
     for i in range(1, v + 1):
         a = self.ope(a)
         if a == e:
             return i
         else:
             R.append((a, i))
     j = 0
     b = a.ope2(2)
     t = 2 * v
     while True:
         for (c, k) in R:
             if b == c:
                 return (t - k)
         a = self.ope(a)
         j += 1
         R.append((a, j + v))
         b = a.ope(b)
         t += j + v
Example #2
0
 def t_order(self, v=2):
     """
     Compute order using Terr's Baby-step Giant-step algorithm.
     """
     if (v < 1) or not (rational.isIntegerObject(v)):
         raise TypeError("input integer v >= 1")
     e = self.set.identity()
     a = self.set.identity()
     R = [(e, 0)]
     for i in range(1, v + 1):
         a = self.ope(a)
         if a == e:
             return i
         else:
             R.append((a, i))
     j = 0
     b = a.ope2(2)
     t = 2 * v
     while True:
         for (c, k) in R:
             if b == c:
                 return (t - k)
         a = self.ope(a)
         j += 1
         R.append((a, j + v))
         b = a.ope(b)
         t += j + v
Example #3
0
 def getDenominator(ele):
     """
     get the denominator of ele
     """
     if rational.isIntegerObject(ele):
         return 1
     else: # rational
         return ele.denominator
Example #4
0
 def getNumerator(ele):
     """
     get the numerator of ele
     """
     if rational.isIntegerObject(ele):
         return ele
     else:  # rational
         return ele.numerator
Example #5
0
 def getNumerator_and_Denominator(ele):
     """
     get the pair of numerator and denominator of ele
     """
     if rational.isIntegerObject(ele):
         return (ele, 1)
     else:  # rational
         return (ele.numerator, ele.denominator)
Example #6
0
 def getDenominator(ele):
     """
     get the denominator of ele
     """
     if rational.isIntegerObject(ele):
         return 1
     else:  # rational
         return ele.denominator
Example #7
0
 def getNumerator(ele):
     """
     get the numerator of ele
     """
     if rational.isIntegerObject(ele):
         return ele
     else: # rational
         return ele.numerator
Example #8
0
 def getNumerator_and_Denominator(ele):
     """
     get the pair of numerator and denominator of ele
     """
     if rational.isIntegerObject(ele):
         return (ele, 1)
     else: # rational
         return (ele.numerator, ele.denominator)
Example #9
0
 def __init__(self, value, key=None, flag=False):
     """
     You can initialize with various one-to-one onto mapping.
     Example,
     Permute([2,3,4,5,1]) -> normal type
     Permute([3,4,2,1,0], 0) -> [4,5,3,2,1]-normal type(index start with 0)
     Permute(['b','c','d','e','a'], 1) -> [2,3,4,5,1]-normal type(identity is ascending order)
     Permute(['b','c','d','e','a'], -1) -> [4,3,2,1,5]-normal type(identity is descending order)
     Permute(['b','c','d','e','a'], ['b','a', 'c','d','e']) -> [1,3,4,5,2]-normal type(identity=key)
     Permute({'a':'b','b':'c','c':'d','d':'e','e':'a'}) -> [2,3,4,5,1]-normal type
     """
     if isinstance(value, dict):
         if key:
             raise TypeError("key isn't need when dict type is used")
         data = value.values()
         key = value.keys()
     elif isinstance(value, (list, tuple)):
         data = list(value)
     else:
         raise TypeError("Not normal form")
     if key == 0:
         self.data = [i + 1 for i in data]
         self.key = range(len(data))
     elif key:
         if isinstance(key, (list, tuple)):
             self.key = list(key)
             if len(value) != len(key):
                 raise TypeError("Length error")
         elif key == 1:
             p_key = list(data)
             p_key.sort()
             self.key = p_key
         elif key == -1:
             p_key = list(data)
             p_key.sort()
             p_key.reverse()
             self.key = p_key
         else:
             raise TypeError("Input sequence type")
         key = self.key
         if flag:
             self.data = data
         else:
             self.data = [key.index(x) + 1 for x in data]
     else:
         self.data = data
         self.key = range(1, len(data) + 1)
     data = self.data
     p_data = range(len(data))
     for x in data:
         if not rational.isIntegerObject(x):
             raise TypeError("This number should be integer list")
         elif x <= 0 or x > len(data):
             raise TypeError("This isn't onto")
         elif p_data[x-1] == -1:
             raise ValueError("This isn't one-to-one")
         else:
             p_data[x-1] = -1
Example #10
0
 def ope2(self, other):
     """
     Group extended operation
     """
     if rational.isIntegerObject(other):
         if not self.operation:
             return GroupElement(self.entity * other, self.operation)
         else:
             return GroupElement(self.entity ** other, self.operation)
     else:
         raise TypeError("input integer")
Example #11
0
 def ope2(self, other):
     """
     Group extended operation
     """
     if rational.isIntegerObject(other):
         if not self.operation:
             return GroupElement(self.entity * other, self.operation)
         else:
             return GroupElement(self.entity**other, self.operation)
     else:
         raise TypeError("input integer")
Example #12
0
 def __pow__(self, other):
     sol = self.__class__(self.data, self.key, flag=True)
     if not rational.isIntegerObject(other):
         raise TypeError("cannot pow operation with %s" % other)
     if other > 0:
         for i in range(other - 1):
             sol = self * sol
     else:
         inv = self.inverse()
         for i in range(abs(other) + 1):
             sol = inv * sol
     return sol
Example #13
0
 def __pow__(self, other):
     sol = ExPermute(self.dim, self.data, self.key, flag=True)  # other instance
     if not rational.isIntegerObject(other):
         raise TypeError("This can't calculate")
     if other > 0:
         for i in range(other - 1):
             sol = self * sol
     else:
         inv = self.inverse()
         for i in range(abs(other) + 1):
             sol = inv * sol
     return sol
Example #14
0
 def __pow__(self, other):
     if rational.isIntegerObject(other):
         if other == 0:
             return rational.Integer(1)
         elif other == 1:
             return +self
         elif other < 0:
             return (self**(-other)).inverse()
         elif other == 2:
             return self.__class__(self.real ** 2 - self.imag ** 2, 2 * self.real * self.imag)
         else:
             return rational.Integer(other).actMultiplicative(self)
     return exp(other * log(self))
Example #15
0
 def __pow__(self, other):
     if rational.isIntegerObject(other):
         if other == 0:
             return rational.Integer(1)
         elif other == 1:
             return +self
         elif other < 0:
             return (self**(-other)).inverse()
         elif other == 2:
             return self.__class__(self.real ** 2 - self.imag ** 2, 2 * self.real * self.imag)
         else:
             return rational.Integer(other).actMultiplicative(self)
     return exp(other * log(self))
Example #16
0
 def __init__(self, dim, value, key=None, flag=False):
     if not (rational.isIntegerObject(dim) and isinstance(value, list)):
         raise TypeError(
             "cannot convert ExPermute. `dim` should be an integer and `value` should be a list."
         )
     self.dim = dim
     data = value
     self.data = []
     if key == 0:
         self.key = range(dim)
         for x in data:
             ele = [y + 1 for y in x]
             self.data.append(tuple(ele))
     elif key:
         if isinstance(key, (list, tuple)):
             self.key = list(key)
             if dim != len(key):
                 raise TypeError(
                     "cannot convert ExPermute. The length of `key` should be equal to dim."
                 )
         else:
             raise TypeError(
                 "cannot convert ExPermute. `key` should be a list or a tuple."
             )
         key = self.key
         if flag:
             self.data = data
         else:
             for x in data:
                 ele = [key.index(x[i]) + 1 for i in range(len(x))]
                 self.data.append(tuple(ele))
     else:
         self.data = data
         self.key = range(1, dim + 1)
     data = self.data
     for x in data:
         if not isinstance(x, tuple):
             raise TypeError(
                 "cannot convert ExPermute. `flag` should be False.")
         box = range(dim)
         for y in x:
             if (y > dim) or (y <= 0):
                 raise TypeError(
                     "cannot convert ExPermute. The map should be onto.")
             elif box[y - 1] == -1:
                 raise ValueError(
                     "cannot convert ExPermute. The map should be one-to-one."
                 )
             else:
                 box[y - 1] = -1
Example #17
0
 def __init__(self, dim, value, key=None, flag=False):
     if not (rational.isIntegerObject(dim) and isinstance(value, list)):
         raise TypeError("This isn't cyclic form")
     self.dim = dim
     data = value
     self.data = []
     if key == 0:
         self.key = range(dim)
         for x in data:
             ele = [ y + 1 for y in x ]
             self.data.append(tuple(ele))
     elif key:
         if isinstance(key, (list, tuple)):
             self.key = list(key)
             if dim != len(key):
                 raise TypeError("Length error")
         else:
             raise TypeError("Input sequence type")
         key = self.key
         if flag:
             self.data = data
         else:
             for x in data:
                 ele = [key.index(x[i]) + 1 for i in range(len(x))]
                 self.data.append(tuple(ele))
     else:
         self.data = data
         self.key = range(1, dim + 1)
     data = self.data
     for x in data:
         if not isinstance(x, tuple):
             raise TypeError("This isn't cyclic form")
         box = range(dim)
         for y in x:
             if (y > dim) or (y <= 0):
                 raise TypeError("This is out of range")
             elif box[y-1] == -1:
                 raise ValueError("This isn't one-to-one")
             else:
                 box[y-1] = -1
Example #18
0
 def _rational_mul(self, other):
     """
     return other * self, assuming other is a rational element
     """
     if rational.isIntegerObject(other):
         other_numerator = rational.Integer(other)
         other_denominator = rational.Integer(1)
     else:
         other_numerator = other.numerator
         other_denominator = other.denominator
     denom_gcd = gcd.gcd(self.denominator, other_numerator)
     if denom_gcd != 1:
         new_denominator = ring.exact_division(
             self.denominator, denom_gcd) * other_denominator
         multiply_num = other_numerator.exact_division(denom_gcd)
     else:
         new_denominator = self.denominator * other_denominator
         multiply_num = other_numerator
     new_module = self.__class__(
         [self.mat_repr * multiply_num, new_denominator], self.number_field,
         self.base, self.mat_repr.ishnf)
     new_module._simplify()
     return new_module
Example #19
0
 def _rational_mul(self, other):
     """
     return other * self, assuming other is a rational element
     """
     if rational.isIntegerObject(other):
         other_numerator = rational.Integer(other)
         other_denominator = rational.Integer(1)
     else:
         other_numerator = other.numerator
         other_denominator = other.denominator
     denom_gcd = gcd.gcd(self.denominator, other_numerator)
     if denom_gcd != 1:
         new_denominator = ring.exact_division(
             self.denominator, denom_gcd) * other_denominator
         multiply_num = other_numerator.exact_division(denom_gcd)
     else:
         new_denominator = self.denominator * other_denominator
         multiply_num = other_numerator
     new_module = self.__class__(
                      [self.mat_repr * multiply_num, new_denominator],
                      self.number_field, self.base, self.mat_repr.ishnf)
     new_module._simplify()
     return new_module
Example #20
0
 def __init__(self, value, key=None, flag=False):
     """
     You can initialize with various one-to-one onto mapping.
     Example,
     Permute([2,3,4,5,1]) -> normal type
     Permute([3,4,2,1,0], 0) -> [4,5,3,2,1]-normal type(index start with 0)
     Permute(['b','c','d','e','a'], 1) -> [2,3,4,5,1]-normal type(identity is ascending order)
     Permute(['b','c','d','e','a'], -1) -> [4,3,2,1,5]-normal type(identity is descending order)
     Permute(['b','c','d','e','a'], ['b','a', 'c','d','e']) -> [1,3,4,5,2]-normal type(identity=key)
     Permute({'a':'b','b':'c','c':'d','d':'e','e':'a'}) -> [2,3,4,5,1]-normal type
     """
     if isinstance(value, dict):
         if key:
             raise TypeError(
                 "cannot convert Permute. I think `key` should be None.")
         data = value.values()
         key = value.keys()
     elif isinstance(value, (list, tuple)):
         data = list(value)
     else:
         raise TypeError(
             "cannot convert Permute. `value` should be a list or a dict.")
     if key == 0:
         self.data = [i + 1 for i in data]
         self.key = range(len(data))
     elif key:
         if isinstance(key, (list, tuple)):
             self.key = list(key)
             if len(value) != len(key):
                 raise TypeError(
                     "cannot convert Permute. The length of `key` should be equal to that of `value`."
                 )
         elif key == 1:
             p_key = list(data)
             p_key.sort()
             self.key = p_key
         elif key == -1:
             p_key = list(data)
             p_key.sort()
             p_key.reverse()
             self.key = p_key
         else:
             raise TypeError(
                 "cannot convert Permute. `key` should be a list.")
         key = self.key
         if flag:
             self.data = data
         else:
             self.data = [key.index(x) + 1 for x in data]
     else:
         self.data = data
         self.key = range(1, len(data) + 1)
     data = self.data
     p_data = range(len(data))
     for x in data:
         if not rational.isIntegerObject(x):
             raise TypeError(
                 "cannot convert Permute. `flag` should be False.")
         elif x <= 0 or x > len(data):
             raise TypeError(
                 "cannot convert Permute. The map should be onto.")
         elif p_data[x - 1] == -1:
             raise ValueError(
                 "cannot convert Permute. The map should be one-to-one.")
         else:
             p_data[x - 1] = -1