First time here? Check out the FAQ!

Ask Your Question
0

iteration of a matrix

asked 7 years ago

Xenia gravatar image

updated 7 years ago

Hello community,

I have the following question. I have a matrix defined as K. And its diagonal defined, for instance, as z = [1, 2, 3, 4, 5]. I need to change each entry of a diagonal one by one, and then check a condition using 'if' statement. For example, change z to [2, 2, 3, 4, 5] and then [1, 3, 3, 4, 5] and then [1, 2, 4, 4, 5] (increasing each element by 1)

Now I have a code :

for i in range(16):
      K1 = K
      K1[i,i] = K[i,i] + 1
      V1 = p1 * K1 * p1t
      V2 = p2 * K1 * p2t
      if V2 < V1:
           print i

So the first three rows are more interesting for the problem. The problem is, this code changes each entry, not returning the previous one to the initial position. So it changes the diagonal as following: [1, 2, 3, 4, 5] to [2, 2, 3, 4, 5] to [2, 3, 3, 4, 5] to [2, 3, 4, 4, 5] etc. How can I modify this code to correct the problem? Thank you.

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
2

answered 7 years ago

dan_fulea gravatar image

Use a copy of K instead, e.g.

sage: K = diagonal_matrix( QQ, [1,2,3,4,5] )
sage: K1 = copy(K)
sage: K1[0,0] += 1
sage: K1

[2 0 0 0 0]
[0 2 0 0 0]
[0 0 3 0 0]
[0 0 0 4 0]
[0 0 0 0 5]
sage: K

[1 0 0 0 0]
[0 2 0 0 0]
[0 0 3 0 0]
[0 0 0 4 0]
[0 0 0 0 5]
Preview: (hide)
link

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 7 years ago

Seen: 1,164 times

Last updated: Feb 11 '18