3
$\begingroup$

I have a function that sets certain subscripts of the symbol aa as in the following example:

listA = { Subscript[aa,2], Subscript[aa,1,4,7], Subscript[aa,2,4,5,8] };
listB = {1,2,3};
Evaluate[listA]=listB

This causes Subscript[aa,2] = 1, etc. Later I wish to reuse the function, sometimes with some of the same downvalues of aa. But, first I must unset those downvalues.

I know I can clear ALL subscripted variables using Clear [ "Subscript" ]. But I need to clear only the subscripted values of the symbol aa while leaving other subscripted symbols alone. Is there a way to do something like the following?

Clear [ "Subscript[ aa ]" ] or Subscript[aa,u__]=. or
clear downvalues of aa

I suppose I could consider learning the Notation package, but all my functions involving subscripts are now working. I am just having this problem of re-using them during a single session.

$\endgroup$
  • $\begingroup$ You can format inline code and code blocks by selecting the code and clicking the {} button above the edit window. The edit window help button ? is also useful for learning how to format your questions and answers. You may also find this meta Q&A helpful $\endgroup$ – Michael E2 Oct 12 '16 at 1:42
  • 2
    $\begingroup$ This is one of the reasons that experienced users of Mathematica caution new users to only use Subscripts for display purposes, and not for calculational purposes. It is standard practice to use, e.g. aa[1], aa[1, 4, 7], etc. in place of the Subscripted versions. You can use Format to have the front end display aa[1] as a subscript while having it be aa[1] in the back end. $\endgroup$ – march Oct 12 '16 at 3:53
  • 1
    $\begingroup$ matrixbud, it seems you have been given @march's advice before on some of your other questions about Subscripts. Are you starting to see why using them as you are is discouraged? Is the effort really worth it compared to switching to aa[1] etc.? :p $\endgroup$ – Marius Ladegård Meyer Oct 12 '16 at 7:49
  • $\begingroup$ Oh, yes, I quite see! Live and learn. However I am so close to being finished with this part of my project that I hate to think of modifying all my code. On the flip side, through this forum I have learned a LOT of Mathematica (and, I've learned how much I don't know.) At this point, I hope I can reach the finish line without recoding the whole thing without subscripts. Else, I will use march's front-end/back-end advice. $\endgroup$ – matrixbud Oct 13 '16 at 2:31
5
$\begingroup$

This will remove all DownValues of Subscript that contain the symbol aa:

DownValues[Subscript] = Cases[DownValues[Subscript], dv_ /; FreeQ[dv, aa]]

It won't remove SubValues, but those can be removed in a similar way.

You can avoid the trouble with DownValues by using UpValues, as shown in Clear complains that a subscripted variable is not a symbol or a string?, or in How do I clear all variables with subscripts?

$\endgroup$
  • $\begingroup$ thanks for the quick response. I have only recently learned a little about downvalues and upvalues and so I probably don't understand your reply well enough. Also, I have not heard of subvalues and when I searched Help for SubValues, nothing showed up. But... when I entered my 3 lines of code followed by yours, the values of aa2, aa1,4,7 and aa2,4,5,8 were still set at 1,2, and 3 respectively. I substituted SubValues for DownValues with the same result. To use UpValues, I would have to experiment how to use them with my lists. $\endgroup$ – matrixbud Oct 12 '16 at 2:20
  • $\begingroup$ @matrixbud This worked for me: i.stack.imgur.com/gFKgB.png -- Upvalues and subvalues won't help in this particular case. $\endgroup$ – Michael E2 Oct 12 '16 at 2:29
  • $\begingroup$ I stand corrected. When I copied and pasted you answer, it didn't work. But when I typed it in manually, it works like a charm!! Thanks so much. $\endgroup$ – matrixbud Oct 12 '16 at 2:39
  • $\begingroup$ I added a similar 3 lines using symbol bb. And even though I only used your Cases code for aa, bb also got cleared. Your FreeQ looks like it should only select aa, so I don't understand how bb also got cleared. $\endgroup$ – matrixbud Oct 12 '16 at 2:59
  • $\begingroup$ @matrixbud I'm not sure what you're doing, but, again, this worked for me: i.stack.imgur.com/hwKse.png $\endgroup$ – Michael E2 Oct 12 '16 at 3:01

Your Answer

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

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