Re: problem 5

5 views
Skip to first unread message

William Stein

unread,
May 4, 2009, 9:09:03 PM5/4/09
to Barry Dewitt, 480-uw09
On Mon, May 4, 2009 at 5:37 PM, Barry Dewitt <barry...@gmail.com> wrote:
> Hi Dr. Stein,
>
>  I wrote the following code that is meant to create a random nxn symmetric
> matrix with 0's along the diagonal:
>
> @interact
> def _(n = 2):
>    m = matrix(ZZ, n, n, [0 for i in [0..n-1] for j in [0..n-1]])
>    for i in [0..n-1]:
>        for j in [i+1..n-1]:
>            m[i][j] = randint(0,1)
>    for i in [0..n-1]:
>        for j in [0..i]:
>            m[i][j] = m[j][i]
>    show(m)
>
> The first two for loops set the diagonal to zero and everything above the
> diagonal to a zero or one; the second set of for loops is meant to make the
> matrix symmetric.  I keep getting the following error: "ValueError: vector
> is immutable; please change a copy instead (use self.copy())"  All the
> documentation I've been able to find says that the matrix is mutable, so I'm
> confused why I can't change the entries of the matrix after I create it.  I
> also tried playing around with m.copy() as the error message suggests, but
> have had no success.  I was wondering if you could point me in the right
> direction, because I think that my approach makes sense (to me, anyway), and
> I want to figure out how I can make it work!


Use m[i,j] not m[i][j].

William

michael

unread,
May 4, 2009, 9:17:29 PM5/4/09
to 480-uw09
I am going to high jack this thread slight and ask a related question.
As far as problem 5 goes, the interact we make needs to allow the user
to input a matrix of arbitary size, right? Or is the user entering the
size of the matrix and then we are generating a random matrix from
that?

On May 4, 6:09 pm, William Stein <wst...@gmail.com> wrote:

William Stein

unread,
May 4, 2009, 10:28:36 PM5/4/09
to 480-...@googlegroups.com
On Mon, May 4, 2009 at 6:17 PM, michael <michael.s...@gmail.com> wrote:
>
> I am going to high jack this thread slight and ask a related question.
> As far as problem 5 goes, the interact we make needs to allow the user
> to input a matrix of arbitary size, right? Or is the user entering the
> size of the matrix and then we are generating a random matrix from
> that?

You could make an interact like this. It would be tedious to use, but
it would get you full credit.

@interact
def f(n=2,v=[0,1,1,0]):
m = matrix(n, n, v)

Reply all
Reply to author
Forward
0 new messages