Ticket #3704: trac-3704-diagonal_matrix.patch

File trac-3704-diagonal_matrix.patch, 4.3 KB (added by jason, 13 years ago)
  • sage/matrix/constructor.py

    # HG changeset patch
    # User Jason Grout <jason-sage@creativetrax.com>
    # Date 1216743813 18000
    # Node ID df848fd45efb42a87df2476245a2e5285f74a01f
    # Parent  e873ea0c5a9ac4b33923d08e0e4f8b2d8c2daca8
    [mq]: trac-3704-diagonal_matrix.patch
    
    diff -r e873ea0c5a9a -r df848fd45efb sage/matrix/constructor.py
    a b def random_matrix(R, nrows, ncols=None,  
    655655    A.randomize(density=density, *args, **kwds)
    656656    return A
    657657
    658 def diagonal_matrix(arg0=None, arg1=None, arg2=None, sparse=None):
     658def diagonal_matrix(*args, **kwds):
    659659    """
    660     INPUT:
    661     Supported formats
    662         1. matrix(diagonal_entries, [sparse=True]):
    663                matrix with each row constructed from the list_of_rows
    664         2. matrix(nrows, diagonal_entries, [sparse=True]):
    665                matrix with each row constructed from the list_of_rows
    666         3. matrix(ring, diagonal_entries, [sparse=True]):
    667                matrix with each row constructed from the list_of_rows
    668         4. matrix(ring, nrows, diagonal_entries, [sparse=True]):
    669                matrix with given number of rows and flat list of entries
    670     The sparse option is optional, must be explicitly named (i.e.,
    671     sparse=True), and may be either True or False.
     660    The diagonal matrix command constructs a diagonal matrix by taking
     661    a list, tuple, vector, or a bunch of diagonal elements, optionally
     662    preceded by a ring.  All other arguments are passed directly to
     663    the matrix() function.  See the documentation for matrix() for
     664    additional options (e.g., the option to create sparse matrices,
     665    etc.)
    672666
    673667    EXAMPLES:
    674     Input format 1.
     668        sage: diagonal_matrix(1,2,3)
     669        [1 0 0]
     670        [0 2 0]
     671        [0 0 3]
    675672        sage: diagonal_matrix([1,2,3])
    676673        [1 0 0]
    677674        [0 2 0]
    678675        [0 0 3]
    679 
    680     Input format 2.
    681676        sage: diagonal_matrix(GF(3), [1,2,3])
    682677        [1 0 0]
    683678        [0 2 0]
    684679        [0 0 0]
    685 
    686     Input format 3:
    687         sage: diagonal_matrix(3, [1,2])
     680        sage: diagonal_matrix(1,2, nrows=3)
    688681        [1 0 0]
    689682        [0 2 0]
    690683        [0 0 0]
    691 
    692     Input format 4:
    693         sage: diagonal_matrix(GF(3), 3, [8,2])
    694         [2 0 0]
     684        sage: diagonal_matrix(GF(3), 1,2, nrows=3)
     685        [1 0 0]
    695686        [0 2 0]
    696687        [0 0 0]
     688        sage: diagonal_matrix(GF(3), (1,2,2))
     689        [1 0 0]
     690        [0 2 0]
     691        [0 0 2]
     692        sage: v = vector(QQ, [1,2,3])
     693        sage: a = diagonal_matrix(v); a
     694        [1 0 0]
     695        [0 2 0]
     696        [0 0 3]
     697        sage: a.parent()
     698        Full MatrixSpace of 3 by 3 dense matrices over Rational Field
    697699    """
    698     ring = None
    699     if isinstance(arg0, (list, tuple)):
    700         # Format 1
    701         v = arg0
    702         nrows = len(v)
    703     elif isinstance(arg0, (int, long, rings.Integer)):
    704         nrows = arg0
    705         v = arg1
    706     elif rings.is_Ring(arg0):
    707         ring = arg0
    708         if isinstance(arg1, (list, tuple)):
    709             v = arg1
    710             nrows = len(v)
    711         else:
    712             nrows = arg1
    713             v = arg2
    714            
    715     if isinstance(v, list):
    716         w = {}
     700
     701    args = list(args)
     702
     703    if len(args) >= 1 and rings.is_Ring(args[0]):
     704        ring = args.pop(0)
     705    else:
     706        ring = None
     707
     708    # Because we specify the diagonal entries as a dictionary,
     709    # matrix() wants to default to sparse.  We want dense as a default,
     710    # though, so we have to specifically test for the sparse keyword.
     711    sparse = kwds.pop('sparse',False)
     712    kwds['sparse'] = sparse
     713   
     714    # If the first item in args is a list or tuple, use that as the diagonal
     715    # Otherwise, assume that the entire list is the diagonal.
     716    w = {}
     717    if isinstance(args[0], (list, tuple)) or is_Vector(args[0]):
     718        v = Sequence(args[0])
    717719        for i in range(len(v)):
    718720            w[(i,i)] = v[i]
     721        args[0] = w
    719722    else:
    720         w = v
     723        v = Sequence(args)
     724        for i in range(len(v)):
     725            w[(i,i)] = v[i]
     726        args = [w]
    721727
    722     if ring is None:
    723         return matrix(nrows, nrows, w, sparse=sparse)
    724     else:
    725         return matrix(ring, nrows, nrows, w, sparse=sparse)
     728    if ring is not None:
     729        args.insert(0,ring)
     730
     731    return matrix(*args, **kwds)
    726732
    727733
    728734def identity_matrix(ring, n=0, sparse=False):