# HG changeset patch
# User Simon Spicer <mlungu@uw.edu>
# Date 1300821940 25200
# Node ID a35ace86107dcba923487fa8e91048a662315f00
# Parent 361a4ad7d52c69b64ae2e658ffd0820af0d87e93
Ticket 10601: Fixes the QuaternionAlgebra constructor so that it can take Python ints and floats as inputs.
diff r 361a4ad7d52c r a35ace86107d sage/algebras/quatalg/quaternion_algebra.py
a

b


38  38  from sage.rings.arith import (GCD, fundamental_discriminant, hilbert_symbol, 
39  39  hilbert_conductor_inverse, hilbert_conductor, 
40  40  factor, gcd, lcm) 
41   from sage.rings.integer import Integer 
 41  from sage.rings.all import RR, Integer 
42  42  from sage.rings.integer_ring import ZZ 
43  43  from sage.rings.rational import Rational 
44  44  from sage.rings.finite_rings.constructor import GF 
… 
… 

51  51  from sage.matrix.matrix_space import MatrixSpace 
52  52  from sage.matrix.constructor import diagonal_matrix, matrix 
53  53  from sage.structure.sequence import Sequence 
54   from sage.structure.element import is_Element 
 54  from sage.structure.element import is_RingElement 
55  55  from sage.modules.free_module import VectorSpace, FreeModule 
56  56  from sage.modules.free_module_element import vector 
57  57  
… 
… 

98  98  Quaternion Algebra (1, 5) with base ring Number Field in sqrt2 with defining polynomial x^2  2 
99  99  sage: QuaternionAlgebra(sqrt(1), sqrt(3)) 
100  100  Quaternion Algebra (I, sqrt(3)) with base ring Symbolic Ring 
 101  sage: QuaternionAlgebra(1r,1) 
 102  Quaternion Algebra (1, 1) with base ring Rational Field 
 103  
 104  Python ints, longs and floats may be passed to the ``QuaternionAlgebra(a, b)`` constructor, as may 
 105  all pairs of nonzero elements of a ring not of characteristic 2. The following tests address 
 106  the issues raised in trac ticket 10601:: 
 107  
 108  sage: QuaternionAlgebra(1r,1) 
 109  Quaternion Algebra (1, 1) with base ring Rational Field 
 110  sage: QuaternionAlgebra(1,1.0r) 
 111  Quaternion Algebra (1.00000000000000, 1.00000000000000) with base ring Real Field with 53 bits of precision 
101  112  sage: QuaternionAlgebra(0,0) 
102  113  Traceback (most recent call last): 
103  114  ... 
… 
… 

105  116  sage: QuaternionAlgebra(GF(2)(1),1) 
106  117  Traceback (most recent call last): 
107  118  ... 
108   ValueError: a and b must be elements of a field with characteristic not 2 
 119  ValueError: a and b must be elements of a ring with characteristic not 2 
 120  sage: a = PermutationGroupElement([1,2,3]) 
 121  sage: QuaternionAlgebra(a, a) 
 122  Traceback (most recent call last): 
 123  ... 
 124  ValueError: a and b must be elements of a ring with characteristic not 2 
109  125  
110  126  ``QuaternionAlgebra(K, a, b)``  return quaternion algebra over the 
111  127  field ``K`` with generators ``i``, ``j``, ``k`` with `i^2=a`, `j^2=b` 
… 
… 

167  183  a = Rational(a); b = Rational(b) 
168  184  
169  185  elif arg2 is None: 
170   if is_Element(arg0): 
171   # QuaternionAlgebra(a, b) 
172   v = Sequence([arg0, arg1]) 
173   K = v.universe().fraction_field() 
174   a = v[0] 
175   b = v[1] 
176   else: 
177   raise ValueError, "unknown input" 
 186  # If arg0 or arg1 are Python data types, coerce them 
 187  # to the relevant Sage types. This is a bit inelegant. 
 188  L = [] 
 189  for a in [arg0,arg1]: 
 190  if is_RingElement(a): 
 191  L.append(a) 
 192  elif type(a) == int or type(a) == long: 
 193  L.append(Integer(a)) 
 194  elif type(a) == float: 
 195  L.append(RR(a)) 
 196  else: 
 197  raise ValueError, "a and b must be elements of a ring with characteristic not 2" 
 198  
 199  # QuaternionAlgebra(a, b) 
 200  v = Sequence(L) 
 201  K = v.universe().fraction_field() 
 202  a = v[0] 
 203  b = v[1] 
178  204  
179  205  # QuaternionAlgebra(K, a, b) 
180  206  else: 
… 
… 

186  212  
187  213  if K.characteristic() == 2: 
188  214  # Lameness! 
189   raise ValueError, "a and b must be elements of a field with characteristic not 2" 
 215  raise ValueError, "a and b must be elements of a ring with characteristic not 2" 
190  216  if a == 0 or b == 0: 
191  217  raise ValueError, "a and b must be nonzero" 
192  218  