2

I would love to be able to use the power of Mathematica's patterns to find and replace within my code. I have searched for this on google, and so far I've come up with this: http://reference.wolfram.com/workbench/index.jsp?topic=/com.wolfram.eclipse.help/html/tasks/patterns/findreplace.html

Which seems to suggest that I would need another program, Mathematica Workbench, to do so? Can anyone explain what this is to me? I guess this would involve paying some money to Wolfram

I also had a go with the code written in this thread. It seems to work part of the time, but has some undesirable features (or at least, undesirable to me!). I want to replace all instances of k[i_] with Subscript[k,i] in my code (and I'm sure I would use this for more complex things later on too if I can get it to work now). Here are the current problems that I have, when I run NBReplace[k[i_ ], Subscript[k,i]]

*) Not all cases of k[_] are covered in my code. I can't see why this would be. I don't understand the code for the function in detail, but it looks to me like it should cover all cases

*) The code currently replaces k[_] with k\_Subscript\_i. I guess maybe I need to change an 'InputForm' somewhere to something else? Or I need to get rid of the code that's changing things to be strings?

*) The code replaces the k[_] in the function call itself, how can I make an exception for this cell?

Any help would be greatly appreciated

Jojo
  • 1,278
  • 8
  • 19
  • So the question is essentially the same as mine but you are not happy with answers there? – Kuba May 17 '16 at 07:39
  • Well, yes I'd say that sounds about right. I think I've explained quite clearly why I'm not happy and what else I'd like it to do though. I actually only tried the code in the first and not the second response, but it looks to me like the code in the first response only searches a pattern and doesn't replace it, which isn't what I'm after – Jojo May 17 '16 at 08:18
  • I think your question is not very clear, actually. For example: "Not all cases of $k[_]$ are covered in my code." What does this actually mean? Which cases are missed? Without those examples there is not much that can be said. Your second point is covered by changing the InputForm inside the NotebookWrite call to StandardForm. Also, please use code blocks for your code, not $\LaTeX$ markup. – Oleksandr R. May 22 '16 at 01:49
  • And you can avoid changing the function call most easily by doing this from a separate notebook. Just add a third argument to NBReplace that specifies the notebook, rather than using SelectedNotebook[] to operate on the current one. – Oleksandr R. May 22 '16 at 01:58
  • Well, thank you for answering two of the bullet points in my question, I see that these were clear enough to be understood and answered. I appreciate your help. The other bullet point is what it says it is; not all cases of $k[_]$ are changed by the function. Here is a before and after example: – Jojo May 24 '16 at 10:15
  • Before: momcononshell4d = {k[1]\[CircleDot]k[2] -> s, k[3]\[CircleDot]k[4] -> s, k[1]\[CircleDot]k[3] -> t, k[2]\[CircleDot]k[4] -> t, k[1]\[CircleDot]k[4] -> u, k[2]\[CircleDot]k[3] -> u}; – Jojo May 24 '16 at 10:17
  • After: momcononshell4d = {k[1]\[CircleDot]k[2] -> s, k[3]\[CircleDot]Subscript[k, i] -> s, Subscript[k, i]\[CircleDot]Subscript[k, i] -> t, Subscript[k, i]\[CircleDot]Subscript[k, i] -> t, Subscript[k, i]\[CircleDot]Subscript[k, i] -> u, Subscript[k, i]\[CircleDot]Subscript[k, i] -> u}; – Jojo May 24 '16 at 10:19
  • So not all cases of k[_] here have been covered by the code. If this is still unclear please explain why and I will try to further elucidate what I mean – Jojo May 24 '16 at 10:21
  • Works for me, so not sure why it doesn't work for you: result is momcononshell4d={Subscript[k, 1]\[CircleDot]Subscript[k, 2]->s,Subscript[k, 3]\[CircleDot]Subscript[k, 4]->s,Subscript[k, 1]\[CircleDot]Subscript[k, 3]->t,Subscript[k, 2]\[CircleDot]Subscript[k, 4]->t,Subscript[k, 1]\[CircleDot]Subscript[k, 4]->u,Subscript[k, 2]\[CircleDot]Subscript[k, 3]->u}; You would be better off to add this example to the question and to change all equation markup to code blocks at the same time. – Oleksandr R. May 24 '16 at 12:43
  • OK, then now I have two problems. When I open a new file and run the code on its own in the file with the example I gave here, it does cover all cases but doesn't replace the i with the correct number. I'm very surprised this works for me and not for you. I have tried just Cases[HoldComplete[k[1]], target : k[i_] :> (target -> Subscript[k, i]), Infinity]! to try to abstract out the part of the code that isn't working, and it gives {k[1] -> Subscript[k, i]} as the output, not replacing i. Do you know why this is? – Jojo May 24 '16 at 13:02
  • When I run the code in the file I actually want to use it in, then it doesn't cover all cases. I think maybe this is because the code just searches for any given case with the NotebookFind function, instead of the specific case which is being considered at the point that the code encounters it? Thanks again for your help – Jojo May 24 '16 at 13:03
  • Seems your Mathematica session is seriously screwed up, then. Your test case above gives {k[1] -> Subscript[k, 1]} for me, as it certainly should. Maybe you have some invisible characters or other structures in your notebook. – Oleksandr R. May 24 '16 at 13:07
  • OK I restarted Mathematica and it sorted out that problem, thanks. I've also found the source of my problem with not all cases being covered. Here is the offending code: Table[\n k[i], {i,5}], where the \n is a newline in my notebook, which is necessary for the problem to arise. NBReplace doesn't replace inside this statement, and the error I get is Syntax::sntxf: "Table[ " cannot be followed by "k[i], {i, 5}]", even though what I've written is valid Mathematica code. Do you know why it doesn't like this? – Jojo May 25 '16 at 09:53

0 Answers0