I have code that runs in a loop to generate a function that has values assigned every multiple of a user-supplied step size. I want to copy the final function "value-wise" to a different function so that I can run the loop again (overwriting the dummy function) with new initial conditions and compare the plots. The naive r1 = r code seems to pass the functions by reference: when the code has finished running, all of the functions are linked as r1 = r2 = r3 = r. How do I "clone" the output lists without maintaining this unwanted linkage?

As a side note, I am not sure what to call the object I have created; it is generated by a loop assigning values by r[i] = .... Is this a list, an array, or a function?

  • $\begingroup$ What you made is a set of indexed down-values for the variable r. You can verify this by evaluating Downvalues[r]. If you are getting r[1] = r[2] = ..., then there is something wrong with the way you doing assignment to r in your loop. Note: if you wanted r to hold an array of values, you should be using r[[i]] rather than r[i]. I can't say more than this without seeing code, which I urge you to add to your question. $\endgroup$ – m_goldberg Jan 27 '19 at 19:02
  • $\begingroup$ Hi, sorry for the confusion. My code is very long, and I think the actual structure of the loop is not too relevant. To clarify: the loop works fine, and there are different values assigned to r[1], r[2], etc. that plot nicely. I would like to then "copy" this set of values to another called "r1" so that r[1]=r1[1], r[2]=r1[2], etc. The initial conditions are then changed and the loop is run again, overwriting the values in the list "r". I want this to not change "r1". $\endgroup$ – S. Thornton Jan 27 '19 at 19:24
  • $\begingroup$ Why not use a second index? For example, r[1,1] would be the first one, r[1,2] the second, r[1,3] etc. Now run your loop again and assign to r[2,1], r[2,2], r[2, 3], etc. Continue until you have them all. $\endgroup$ – bill s Jan 27 '19 at 19:52
  • $\begingroup$ I may go back into the code and do just this. The reason I haven't done this is because the loop refers to functions that involve the letters themselves, and I would have to restructure my code. It is a fourth order Runge-Kutta program numerically integrating equations for four variables ("r", "T", "P", and "L") that all depend on each other in complicated ways. I may make these lists "two-dimensional" if there is no straightforward way to "clone" the resultant values after one round of integration. $\endgroup$ – S. Thornton Jan 27 '19 at 20:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.