Referencing Copies

A lot of times in numerical methods, I need to have a temporary variable as a time stepping tool or as an “incrementing” device without altering the original variable. A code snippet like

copy_u = u
for i in range(len(copy_u)):
    copy_u[i] = f(u)

But I gotta be more careful. There’s a deep difference between making a copy of a variable, and just creating a reference to a variable. Any change to copy_u might change u itself! Use np.copy or the copy module in Python!

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.