Ticket #13678: 13678-matrix-methods.patch

File 13678-matrix-methods.patch, 6.1 KB (added by robertwb, 9 years ago)
  • sage/matrix/constructor.py

    # HG changeset patch
    # User Robert Bradshaw <robertwb@math.washington.edu>
    # Date 1351720134 25200
    # Node ID 2d2d8271031d98b0351a275b9f1f28adcb941775
    # Parent  95e14084836ba7484596fbb93880154e02883bc1
    Add identity, random, etc. methods to matrix constructor object.
    
    diff --git a/sage/matrix/constructor.py b/sage/matrix/constructor.py
    a b  
    3131
    3232import sage.categories.pushout
    3333
     34def matrix_method(func):
     35    """
     36    Allows a function to be tab-completed on the global matrix
     37    constructor object.
     38
     39    sage: from sage.matrix.constructor import matrix_method
     40    sage: def foo_matrix(n): return matrix.diagonal(range(n))
     41    sage: matrix_method(foo_matrix)
     42    <function foo_matrix at ...>
     43    sage: matrix.foo(5)
     44    [0 0 0 0 0]
     45    [0 1 0 0 0]
     46    [0 0 2 0 0]
     47    [0 0 0 3 0]
     48    [0 0 0 0 4]
     49    """
     50    name = func.__name__.replace('matrix', '').strip('_')
     51    setattr(matrix, name, func)
     52    return func
     53
    3454def matrix(*args, **kwds):
    3555    """
    36     Create a matrix.
     56    Create a matrix.  This object also contains methods to create
     57    special types of matrices, see matrix.[tab] for more options.
    3758   
    3859    INPUT: The matrix command takes the entries of a matrix, optionally
    3960    preceded by a ring and the dimensions of the matrix, and returns a
     
    667688           
    668689    return matrix_space.MatrixSpace(ring, nrows, ncols, sparse=sparse)(entries)
    669690       
     691Matrix = matrix
    670692
    671693
    672694def prepare(w):
     
    799821        return 0
    800822    return max([0] + [ij[1] for ij in d.keys()]) + 1   
    801823
    802 Matrix = matrix
    803 
     824
     825@matrix_method
    804826def column_matrix(*args, **kwds):
    805827    r"""
    806828    Constructs a matrix, and then swaps rows for columns and columns for rows.
     
    886908    return matrix(*args, **kwds).transpose()
    887909
    888910
     911@matrix_method
    889912def random_matrix(ring, nrows, ncols=None, algorithm='randomize', *args, **kwds):
    890913    r"""
    891914    Return a random matrix with entries in a specified ring, and possibly with additional properties.
     
    12621285        raise ValueError('random matrix algorithm "%s" is not recognized' % algorithm)
    12631286
    12641287
     1288@matrix_method
    12651289def diagonal_matrix(arg0=None, arg1=None, arg2=None, sparse=True):
    12661290    r"""
    12671291    Return a square matrix with specified diagonal entries, and zeros elsewhere.
     
    14811505        return matrix(ring, nrows, nrows, w, sparse=sparse)
    14821506
    14831507
     1508@matrix_method
    14841509def identity_matrix(ring, n=0, sparse=False):
    14851510    r"""
    14861511    Return the `n \times n` identity matrix over the given
     
    15171542    return matrix_space.MatrixSpace(ring, n, n, sparse)(1)
    15181543
    15191544
     1545@matrix_method
    15201546def zero_matrix(ring, nrows, ncols=None, sparse=False):
    15211547    r"""
    15221548    Return the `nrows \times ncols` zero matrix over the given
     
    15521578        ring = rings.ZZ
    15531579    return matrix_space.MatrixSpace(ring, nrows, ncols, sparse)(0)
    15541580
     1581
     1582@matrix_method
    15551583def ones_matrix(ring, nrows=None, ncols=None, sparse=False):
    15561584    r"""
    15571585    Return a matrix with all entries equal to 1.
     
    16431671    one = ring(1)
    16441672    return matrix_space.MatrixSpace(ring, nrows, ncols, sparse).matrix([one]*nents)
    16451673
     1674
     1675@matrix_method
    16461676def elementary_matrix(arg0, arg1=None, **kwds):
    16471677    r"""
    16481678    Creates a square matrix that corresponds to a row operation or a column operation.
     
    22472277    # If we got this far, then everything fits
    22482278    return (row_heights, zero_widths, total_width)
    22492279
     2280
     2281@matrix_method
    22502282def block_matrix(*args, **kwds):
    22512283    r"""
    22522284    Returns a larger matrix made by concatenating submatrices
     
    26202652
    26212653    return big
    26222654
     2655
     2656@matrix_method
    26232657def block_diagonal_matrix(*sub_matrices, **kwds):
    26242658    """
    26252659    Create a block matrix whose diagonal block entries are given by
     
    26552689        entries[n*i+i] = sub_matrices[i]
    26562690    return block_matrix(n, n, entries, **kwds)
    26572691
     2692
     2693@matrix_method
    26582694def jordan_block(eigenvalue, size, sparse=False):
    26592695    r"""
    26602696    Returns the Jordan block for the given eigenvalue with given size.
     
    26972733        block[i,i+1]=1
    26982734    return block
    26992735
     2736
     2737@matrix_method
    27002738def companion_matrix(poly, format='right'):
    27012739    r"""
    27022740    Create a companion matrix from a monic polynomial.
     
    28932931        raise TypeError("unable to find common ring for coefficients from polynomial")
    28942932    return M
    28952933
     2934@matrix_method
    28962935def random_rref_matrix(parent, num_pivots):
    28972936    r"""
    28982937    Generate a matrix in reduced row-echelon form with a specified number of non-zero rows.
     
    30583097                    return_matrix[rest_entries,rest_non_pivot_column]=ring.random_element()
    30593098    return return_matrix
    30603099
     3100@matrix_method
    30613101def random_echelonizable_matrix(parent, rank, upper_bound=None):
    30623102    r"""
    30633103    Generate a matrix of a desired size and rank, over a desired ring, whose reduced
     
    32463286            matrix.add_multiple_of_row(0,randint(1,rows-1),ring.random_element())
    32473287    return matrix
    32483288
     3289@matrix_method
    32493290def random_subspaces_matrix(parent, rank=None):
    32503291    r"""
    32513292    Create a matrix of the designated size and rank whose right and
     
    34273468    # K matrix to the identity matrix.
    34283469    return J.inverse()*B
    34293470
     3471@matrix_method
    34303472def random_unimodular_matrix(parent, upper_bound=None):
    34313473    """
    34323474    Generate a random unimodular (determinant 1) matrix of a desired size over a desired ring.
     
    35263568        return random_matrix(ring, size,algorithm='echelonizable',rank=size, upper_bound=upper_bound)
    35273569
    35283570
     3571@matrix_method
    35293572def random_diagonalizable_matrix(parent,eigenvalues=None,dimensions=None):
    35303573    """
    35313574    Create a random matrix that diagonalizes nicely. 
     
    37723815            eigenvector_matrix.add_multiple_of_row(upper_row,row,randint(-4,4))
    37733816    return eigenvector_matrix*diagonal_matrix*(eigenvector_matrix.inverse())
    37743817
     3818
     3819@matrix_method
    37753820def vector_on_axis_rotation_matrix(v, i, ring=None):
    37763821    r"""
    37773822    Return a rotation matrix `M` such that `det(M)=1` sending the vector
     
    38453890        m = rot * m
    38463891    return m
    38473892
     3893
     3894@matrix_method
    38483895def ith_to_zero_rotation_matrix(v, i, ring=None):
    38493896    r"""
    38503897    Return a rotation matrix that sends the i-th coordinates of the