I have a code in which I define something like this:

cc[1, 2] = 1;
cc[2, 6] = 12;
cc[3, 7] = 3;

After some manipulation I define a sort of temporary variable related to cc:

cctemp[1, 2] = 13;
cctemp[2, 6] = 8;
cctemp[3, 7] = 4;
cctemp[1, 9] = 87;

There are new and old indices.

Now in my code i wanna replace cc with cctemp. I can do the trivial assignment and i can use a Do for the substitution but its not the best. How could I realize the assignment ?

(Ps maybe something like Activate @ Inactive ....)

Thank you

  • 2
    (1) List is in the title, but it looks like you are assigning DownValues rather than making cc a list. Do you mean cc[[1, 2]] rather than cc[1, 2], etc.? (2) Why can't you just do cc[1, 2] = cctemp[1, 2] (or cc[[1,2]] = cctemp[[1, 2]]? – march Oct 16 at 21:06
up vote 5 down vote accepted
DownValues[cc] = DownValues[cctemp] /. cctemp -> cc;

cc @@@ {{1, 2}, {2, 6}, {3, 7}, {1, 9}}

{13, 8, 4, 87}

If cc has some assignments that are not overridden by the assignments in cctemp you can use

DownValues[cc] =  DeleteDuplicatesBy[
   Join[DownValues[cctemp], DownValues[cc]] /. cctemp -> cc, #[[1,1]]&]

You can make use of the undocumented Language`ExtendedDefinition function that underlies the Wolfram Cloud. Suppose:

cctemp[1,2] = 13;
cctemp[2,6] = 8;
cctemp[3,7] = 4;
cctemp[1,9] = 87;

Then:

Language`ExtendedDefinition[cc] = Language`ExtendedDefinition[cctemp] /. cctemp -> cc;

Finally:

DownValues[cc]

{HoldPattern[cc[1, 2]] :> 13, HoldPattern[cc[1, 9]] :> 87, HoldPattern[cc[2, 6]] :> 8, HoldPattern[cc[3, 7]] :> 4}

  • ok thank you for the answer; just two things because i want to learn: 1) where can i find the package of this function 2) why should i prefer this way over the answer of kglr ? – siderius Oct 16 at 21:17
  • 1
    @siderius There is no package, the function is built-in. Using Language`ExtendedDefinition transfers UpValues, DownValues, FormatValues, etc. If all you care about are DownValues, then using DownValues instead is better. – Carl Woll Oct 16 at 21:29

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.