Ticket #3704: trac-3704.patch

File trac-3704.patch, 3.1 KB (added by rlm, 13 years ago)

Independent of the other patches found here.

  • sage/matrix/constructor.py

    # HG changeset patch
    # User Robert L. Miller <rlm@rlmiller.org>
    # Date 1232714193 28800
    # Node ID bc71f5a72bbe0c5746f1389cac6cfbb2f086260d
    # Parent  f8fb0bf3e547d65a0f90f5b27d543c8cf4ca9da0
    Make diagonal matrix accept vectors as input.
    
    diff -r f8fb0bf3e547 -r bc71f5a72bbe sage/matrix/constructor.py
    a b  
    656656    """
    657657    INPUT:
    658658    Supported formats
    659         1. matrix(diagonal_entries, [sparse=True]):
    660                matrix with each row constructed from the list_of_rows
    661         2. matrix(nrows, diagonal_entries, [sparse=True]):
    662                matrix with each row constructed from the list_of_rows
    663         3. matrix(ring, diagonal_entries, [sparse=True]):
    664                matrix with each row constructed from the list_of_rows
    665         4. matrix(ring, nrows, diagonal_entries, [sparse=True]):
    666                matrix with given number of rows and flat list of entries
     659        1. diagonal_matrix(diagonal_entries, [sparse=True]):
     660            diagonal matrix with flat list of entries
     661
     662        2. diagonal_matrix(nrows, diagonal_entries, [sparse=True]):
     663            diagonal matrix with flat list of entries and the rest zeros
     664
     665        3. diagonal_matrix(ring, diagonal_entries, [sparse=True]):
     666            diagonal matrix over specified ring with flat list of entries
     667
     668        4. diagonal_matrix(ring, nrows, diagonal_entries, [sparse=True]):
     669            diagonal matrix over specified ring with flat
     670            list of entries and the rest zeros
     671
     672        5. diagonal_matrix(vect, [sparse=True]):
     673            diagonal matrix with entries taken from a vector
     674
    667675    The sparse option is optional, must be explicitly named (i.e.,
    668676    sparse=True), and may be either True or False.
    669677
     
    674682        [0 2 0]
    675683        [0 0 3]
    676684
    677     Input format 2.
    678         sage: diagonal_matrix(GF(3), [1,2,3])
     685    Input format 2:
     686        sage: diagonal_matrix(3, [1,2])
    679687        [1 0 0]
    680688        [0 2 0]
    681689        [0 0 0]
    682690
    683     Input format 3:
    684         sage: diagonal_matrix(3, [1,2])
     691    Input format 3.
     692        sage: diagonal_matrix(GF(3), [1,2,3])
    685693        [1 0 0]
    686694        [0 2 0]
    687695        [0 0 0]
     
    691699        [2 0 0]
    692700        [0 2 0]
    693701        [0 0 0]
     702   
     703    Input format 5:
     704        sage: diagonal_matrix(vector(GF(3),[1,2,3]))
     705        [1 0 0]
     706        [0 2 0]
     707        [0 0 0]
     708
    694709    """
    695710    ring = None
    696711    if isinstance(arg0, (list, tuple)):
     
    698713        v = arg0
    699714        nrows = len(v)
    700715    elif isinstance(arg0, (int, long, rings.Integer)):
     716        # Format 2
    701717        nrows = arg0
    702718        v = arg1
    703719    elif rings.is_Ring(arg0):
    704720        ring = arg0
    705721        if isinstance(arg1, (list, tuple)):
     722            # Format 3
    706723            v = arg1
    707724            nrows = len(v)
    708725        else:
     726            # Format 4
    709727            nrows = arg1
    710728            v = arg2
    711            
     729    elif is_Vector(arg0):
     730        # Format 5
     731        v = list(arg0)
     732        nrows = len(v)
    712733    if isinstance(v, list):
    713734        w = {}
    714735        for i in range(len(v)):