loops for matrix

369 views
Skip to first unread message

Yi

unread,
Jun 24, 2010, 1:49:27 PM6/24/10
to Macaulay2
I want to make a upper triangular matrix. The code is as follows:

p=11; d=5;
for r from 0 to d do (for s from 0 to d do (if (r<s) then
(M_(r,s)=(random(0,(p-1)))) else (M_(r,s)=0)));
H:=method();
H ZZ:=k->(U:=toList(set{M_(k,0)}); for l from 1 to d do (U=U|
(toList(set{M_(k,l)}));); return (toList U));
X=set {H(0)}; for m from 1 to d do (X=X+(set{H(m)}););
M=matrix (toList X)

It does not always produce a upper triangular matrix. What is wrong
with the code?
For example, I got:
o8 = | 0 0 0 0 0 0 |
| 0 0 3 9 10 2 |
| 0 0 1 10 7 1 |
| 0 0 0 0 0 4 |
| 0 0 0 0 4 8 |
| 0 0 0 5 5 5 |


The second question is that I need to use this in a loop. If you try
the above code twice, you will see that you still get the same matrix
as in the first time, and the message is:
stdio:8:100:(3):[0]: error: no method for assignment to binary
operator _ applied to objects:
-- | 0 0 0 0 0 0 | (of class Matrix)
-- | 0 0 3 9 10 2 |
-- | 0 0 1 10 7 1 |
-- | 0 0 0 0 0 4 |
-- | 0 0 0 0 4 8 |
-- | 0 0 0 5 5 5 |
-- _ (0, 0) (of class Sequence)


So the question is how to clear the memory for this matrix?

Thanks a lot.

Douglas Leonard

unread,
Jun 24, 2010, 2:58:03 PM6/24/10
to maca...@googlegroups.com
Well, I think I can answer part of your question.
The first application of the for loop produces an IndexedVariableTable,
not a Matrix,
as you could have found out by asking M2.

The last line probably redefines it as a Matrix.

M2 doesn't like to reassign entires of a matrix (hence the existence of
MutableMatrix).
And since you got the error, the matrix M was not recomputed, just
reprinted.

I assume something about the set command caused the rows or elements to
be reordered;
but maybe this will be moot once you correct the other errors.

Doug


>>> Yi <zhan...@umn.edu> 06/24/10 12:50 PM >>>

Thanks a lot.

--
You received this message because you are subscribed to the Google
Groups "Macaulay2" group.
To post to this group, send email to maca...@googlegroups.com.
To unsubscribe from this group, send email to
macaulay2+...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/macaulay2?hl=en.


Yi Zhang

unread,
Jun 24, 2010, 10:24:55 PM6/24/10
to maca...@googlegroups.com
Dear Professor Leonard,

Thank you very much for your time and reply.

I have read through the related articles in the documentation, but still
have no clue how to fix both questions. By the way, I do not want to
define M_(r,s) as function like M(r,s), because I need to use the value
of M_(r,s) later. If it is defined as a function with two variables r
and s, when I refer to the value, it changes to another random number.

Thanks for your time. Have a nice day.

Sincerely,
Yi

Douglas Leonard

unread,
Jun 24, 2010, 11:01:05 PM6/24/10
to maca...@googlegroups.com
Instead of your two nested

for .... do

statements, try using two nested

for ... list

statements.

Then convert the result to a matrix, avoiding all the list and set stuff
in your method.

Thanks a lot.

--

Alexandra

unread,
Jun 25, 2010, 12:06:09 AM6/25/10
to Macaulay2
Yi, I think the method you want is the following

upperTriang= method()
upperTriang = (p,d)-> (
matrix (for r from 0 to d list (
for s from 0 to d list (if (r<s) then random(0,(p-1))
else 0))))

Successive runs are ok and give you distinct matrices.

Alexandra

Yi Zhang

unread,
Jun 25, 2010, 12:36:27 AM6/25/10
to maca...@googlegroups.com
Dear Alexandra,

Thank you very much for your time and help. Your code works beautifully.
Have a nice day.

Sincerely,
Yi

Douglas Leonard

unread,
Jun 25, 2010, 8:41:43 AM6/25/10
to maca...@googlegroups.com
This is exactly what I had suggested, but in method form.

>>> Yi Zhang <zhan...@umn.edu> 06/24/10 11:36 PM >>>
Dear Alexandra,

Have a nice day.

Sincerely,
Yi

--

Yi Zhang

unread,
Jun 25, 2010, 12:01:44 PM6/25/10
to maca...@googlegroups.com, Mr. Yi Zhang
Dear Professor Leonard and Alexandra,

Thank you very much for your time and help. This time, I have learned a
lot for the matrix and list commands. I did not know that the matrix
command can be used in this way. I always tried to make a list first or
to assign the values to each position of the matrix first. Now I
understand how useful the "for..list" command is. Thanks again and have
a nice weekend.

Sincerely,
Yi

Reply all
Reply to author
Forward
0 new messages