0

I have a while loop in my python code which contains a nested for loop that loops over a 2D array. The variable T needs to get updated at end of the while loop. However I'm seeing that the variable is getting updated within the for loop itself. What am I doing incorrectly here and how to modify this?

error = 1
tolerance = 1e-6
T = T_new
itr = 0
while error>tolerance:
    itr = itr+1
    for i in range(1,imax-1):
        for j in range(1,jmax-1):
            T_new[i,j]=0.25*(T[i-1,j]+T[i+1,j]+T[i,j-1]+T[i,j+1])
    error = np.max(np.abs(T_new-T))
    T = T_new
martineau
  • 119,623
  • 25
  • 170
  • 301
Vyas
  • 1
  • 2
    `T = T_new` does not make a copy of `T_New`, if that is what you are thinking. – chepner Feb 03 '22 at 16:02
  • As i am within the while loop, i would like to calculate T_new using T. And then updated T with T_new and re-compute till the condition is satisfied. – Vyas Feb 03 '22 at 16:04
  • It's the first assignment to `T` (before the loop) that causes the problem. You need `T_New` to be *independent* of `T`, so that you can calculate `T_new - T` before (correctly) replacing `T` with `T_new` in the last line of the loop. – chepner Feb 03 '22 at 16:08
  • 1
    T and T_new are names for the same object. Assignment doesn’t make copies, it makes a name for an object. Try `a=[1]; b=a; a[0]=2` then `print(b)` – Mark Tolonen Feb 03 '22 at 16:10
  • Welcome to Stack Overflow. "And then updated T with T_new" Think carefully about what you mean by "updated". Presumably, you want `T` to be a *different object* with the *same values*. In Python, `=` cannot do this, no matter what the data type is. In Python `=` makes the left-hand side refer to *the same object* that the right-hand side does. Please see the linked duplicate, and also https://nedbatchelder.com/text/names1.html as supplementary reading. – Karl Knechtel Feb 03 '22 at 16:17
  • @chepner: I have tried giving different initializations to T & T_new before starting the loop. What i`m seeing is within the for loop it is updating T with T_new even through that T=T_new statement is provided at the end of for loop – Vyas Feb 03 '22 at 16:21
  • @MarkTolonen: The example you mentioned is the same issue i am seeing. Any suggestions to change my code? – Vyas Feb 03 '22 at 16:23
  • Assuming T is a shallow list, make a copy. – Mark Tolonen Feb 03 '22 at 16:28
  • T & T_new are 2D arrays – Vyas Feb 03 '22 at 16:29
  • There should be a concrete example of `T` in your question, but since the syntax looks like a numpy array, those objects have a `.copy(()` method. – Mark Tolonen Feb 04 '22 at 07:31

1 Answers1

-1

In python, only primitives: ints, floats, strings, are copied by value. Lists, dicts, and objects by default are copied by reference.

You can from copy import deepcopy and use T = deepcopy(T_new) to get the behavior you want. Before doing so though, think about if you are writing your code in an optimal manner. For example, you use itr = itr + 1 instead of itr += 1. Also think about formatting. Use an auto formatter if you can't bother to write your code properly.

Elijah
  • 1,814
  • 21
  • 27
  • 1
    No, everything is treated the same way. Immutable objects just can’t be changed. There’s nothing special about integers floats and strings regarding how assignment is handled – Mark Tolonen Feb 03 '22 at 16:14
  • Aside from being technically incorrect, the question is a commonly asked topic that is adequately addressed by a duplicate and thus should not be answered here. As well, observations about the original code formatting etc. do not belong in answers, because they are tangential at best to *the question*. Please read [answer] if you haven't before. – Karl Knechtel Feb 03 '22 at 16:19
  • @KarlKnechtel, addequantly eh? The question was closed to the wrong question. Only the original mutability question mentions deepcopy, albeit not immediately. – Elijah Feb 03 '22 at 16:30
  • The point is to understand the underlying semantics. Also, deep copy is probably not relevant here; OP didn't mention it, but the code syntax strongly suggests a Numpy array. At any rate, the data structure is only being indexed once, so there is no reason to expect issues with a shallow copy. – Karl Knechtel Feb 03 '22 at 16:35