# How to change the value of an array that is outside the For loop, from inside the For loop [closed]

H is a square matrix which depends on the vector d which I define below. I redefine d in each iteration but I need to plug it in H in each iteration aswell. It is easy if I define H inside the For loop in each iteration, but since H is a huge matrix,it takes forever.

I wonder wether I can define H outside and update it in each iteration from inside as I update d.

This is a toy model of what I am doing, with out the definition of H(d):

For[j = 1, j <= jmax, j++,

{eigenval, eigenvect} = Eigensystem[H];

Do[d[[i]] = Sum[eigenvect[[i, t]], {t, 1, tmax, 1}], {i, 1, imax,1}]]


Thanks!

-

## closed as not a real question by acl, Leonid Shifrin, Sjoerd C. de Vries, m_goldberg, rm -rf♦Apr 3 '13 at 3:28

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

It's not clear what you want: do you mean that eigenvect[] will change on each iteration? In that case, you need the Eigensystem[H] statement inside the loop, too. –  Andrew Jaffe Apr 2 '13 at 14:22
@Andrew Jaffe,sorry I meant from inside the For loop, so precisely, Eigensystem is inside the loop. I edited it. –  Mencia Apr 2 '13 at 14:24
My question/point stands: Do you want eigenvect to be different once you've changed H? If so, you need to put Eigensystem[H] inside the loop (but you don't need to define H itself inside the loop). Or do you just want there to be a new H once you've finished the loop? In that case, you can just alter H itself without calling Eigensystem. –  Andrew Jaffe Apr 2 '13 at 14:27
Yes, I want it to be different once I have changed H, since they are the eigenvectors of H, they need to change as H changes. Eigensystem is inside the For loop. –  Mencia Apr 2 '13 at 14:29
I think we need more information about what you're actually trying to do. When does H change? Inside the Do? Should eigenvect change when that happens? If so, you need to explicitly call Eigensystem each time. –  Andrew Jaffe Apr 2 '13 at 14:44

Perhaps you want something like this:

d = Array[1 &, 3];
(* A random h function*)
h[d_] := SparseArray[Table[{RandomInteger[{1, 3}], RandomInteger[{1, 3}]} ->
d[[1 + Mod[i, 3]]], {i, 9}]];

Nest[N /@ Tr /@ (Eigensystem[h[#]][[2]]) &, d, 10]


Edit

According to your comments, you could try for your specific system

d             = Array[1 &, 16]; (*Your init vector*)
iters         = 100; (*Number of iterations*)
matdelta[d_] := Table[If[i == j, -d[[i]], 0], {i, 1, 16, 1}, {j, 1, 16, 1}]
h[d_]        := ArrayFlatten[{{hns, matdelta[d]}, {Conjugate[matdelta[d]], -hns}}]

Nest[N /@ Tr /@ (Eigensystem[h[#]][[2]]) &, d, iters]

-
Thanks @belisarius, I will try to understand what you suggest and see if I can use it. –  Mencia Apr 2 '13 at 15:32
Thanks alot @belisarius! Here H, for hns put any 16x16 matrix, my definition is way too long to post –  Mencia Apr 2 '13 at 16:44
matdelta = Table[If[i == j, -d[[i]], 0], {i, 1, 16, 1}, {j, 1, 16, 1}]; H = ArrayFlatten[{{hns, matdelta}, {Conjugate[matdelta], -hns}}]; –  Mencia Apr 2 '13 at 16:45
h[d_] := ( matdelta=Tab.... ; ArrayFlatten[ ... ) Note such a delayed function definition doesnt do much performance wise vs. putting the code in the loop. –  george2079 Apr 2 '13 at 20:47
@belisarius, I will email you hns, its too long to post. –  Mencia Apr 2 '13 at 21:12