Ticket #10601: trac_10601_QuaternionAlgebra_constructor.patch

File trac_10601_QuaternionAlgebra_constructor.patch, 4.4 KB (added by spice, 11 years ago)

Replaces previous patch

  • sage/algebras/quatalg/quaternion_algebra.py

    # 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  
    3838from sage.rings.arith import (GCD, fundamental_discriminant, hilbert_symbol,
    3939                              hilbert_conductor_inverse, hilbert_conductor,
    4040                              factor, gcd, lcm)
    41 from sage.rings.integer import Integer
     41from sage.rings.all import RR, Integer
    4242from sage.rings.integer_ring import ZZ
    4343from sage.rings.rational import Rational
    4444from sage.rings.finite_rings.constructor import GF
     
    5151from sage.matrix.matrix_space import MatrixSpace
    5252from sage.matrix.constructor import diagonal_matrix, matrix
    5353from sage.structure.sequence import Sequence
    54 from sage.structure.element import is_Element
     54from sage.structure.element import is_RingElement
    5555from sage.modules.free_module import VectorSpace, FreeModule
    5656from sage.modules.free_module_element import vector
    5757
     
    9898        Quaternion Algebra (-1, -5) with base ring Number Field in sqrt2 with defining polynomial x^2 - 2
    9999        sage: QuaternionAlgebra(sqrt(-1), sqrt(-3))
    100100        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
    101112        sage: QuaternionAlgebra(0,0)
    102113        Traceback (most recent call last):
    103114        ...
     
    105116        sage: QuaternionAlgebra(GF(2)(1),1)
    106117        Traceback (most recent call last):
    107118        ...
    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
    109125
    110126    ``QuaternionAlgebra(K, a, b)`` - return quaternion algebra over the
    111127    field ``K`` with generators ``i``, ``j``, ``k`` with `i^2=a`, `j^2=b`
     
    167183        a = Rational(a); b = Rational(b)
    168184
    169185    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]
    178204
    179205    # QuaternionAlgebra(K, a, b)
    180206    else:
     
    186212
    187213    if K.characteristic() == 2:
    188214        # 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"
    190216    if a == 0 or b == 0:
    191217        raise ValueError, "a and b must be nonzero"
    192218