Random Matrix Error

78 views
Skip to first unread message

Mahathi Vempati

unread,
Jan 19, 2020, 8:29:05 AM1/19/20
to sage-devel
I am working on the following ticket: https://trac.sagemath.org/ticket/28520
I have a question on how to proceed, which I wrote as the first comment on the ticket. 

If anyone could help out with this, it would be great!

I have copied the ticket and comment for your convenience here:

Ticket Description:

The following innocent code gives a TypeError?:

e=random_matrix(ZZ,4,5,algorithm='echelon_form')

while I should have given:

e=random_matrix(ZZ,4,5,algorithm='echelon_form', num_pivots=3)

The code should just use a random value for the number of pivots when the argument isn't given.



Comment:

I would like to work on this issue. So, when either random_matrix() or matrix.random() is called with the keyword argument algorithm=echelon_form, the following piece of code gets run:

   def random_matrix(...):
      ...
      elif algorithm == 'echelon_form':
          return random_rref_matrix(parent, *args, **kwds)

This calls the following signature:

def random_rref_matrix(parent, num_pivots)

Both of these pieces of code are in the file: sage/local/lib/python3.7/site-packages/sage/matrix/special.py

Now I guess I have two options to fix it:

  1. Make changes in the random_matrix() function:

I could check whether kwds has the value num_pivots. If it doesn't, I will call random_rref_matrix with a random number in the range [0, min(nrows, ncols)]. Will also have to check if ncols is there or not.

I guess this is safe but will make the code look a little ugly.

  1. Make changes to the random_rref_matrix() function:

I will change the signature to include a *args and a **kwds. Check within the function for the existence of num_pivots and assign num_pivots appropriately if it is not there.

Will have to ensure the error messages would be the same as before? Seems a bit unsafe as a lot of other functions might call this function, but should be cleaner.

Which way should I proceed? Is there anything I'm missing?


______________

Thank you,
Mahathi

Nils Bruin

unread,
Jan 19, 2020, 12:18:39 PM1/19/20
to sage-devel
The proposal in the ticket is not very desirable. If you're going to generate a random matrix, you do that with a specific probability distribution on the matrix space. The target distribution would generally be one on an infinite space of matrices, but in those cases you usually want to define a sequence of distributions on finite sets (usually parametrized by "size of coefficients" somehow) that converges nicely to to the target distribution.

What distribution are you proposing on the number of pivots? Uniform? What is the description of the distribution you are trying to model?

I think you first want to  write a reasonable specification (mathematically) of what the routine should accomplish and then see what the implementation should be.

I suspect that this ticket should actually read "generate a better error message if user does not supply`num_pivots` with `algorithm='echelon_form'`".

Alternatively, taking num_pivots to be maximal by default seems like a reasonable choice. Or perhaps the pivots should first be generated uniform-independent randomly from a (finite?) set and then if zeros are found, build the echelon form with those pivots. It all depends on what the routine is for.

Mahathi Vempati

unread,
Feb 9, 2020, 2:14:41 PM2/9/20
to sage-devel
I understand how I overlooked the possibility of the distribution not remaining the same. Thanks!
I've tried to look for applications of when people would want to randomly generate matrices in the echelon form (to make a reasonable default) but an unable to think of/ find any. 

Why is taking maximal number of pivots a reasonable choice? If this is indeed the case, I will go ahead with maximal number of pivots. 
Otherwise, the second method you suggested seems like the more expected output for a user who doesn't specify num_pivots? Doing this + adding to the documentation should fix this?
Reply all
Reply to author
Forward
0 new messages