3

I want a function that takes expressions of the following form: a$x and converts them to a[x].

In particular, consider the following test cases

exp = a$x + b$x$y + a$x[d] + a$x[d$y]

Should become, with some ToFunctionNotation:

ToFunctionNotation[exp] == a[x] + b[x][y] + a[x][d] + a[x][d[y]]

Note: I want to keep this as an expression for further processing. I tried using matches with SymbolName and string patterns, but couldn't figure it out.

jlperla
  • 967
  • 6
  • 18
  • Do they have values? – Kuba Jun 12 '14 at 16:43
  • Nope. In reality what I am doing is using $ for the naming of the variables, then using http://mathematica.stackexchange.com/questions/30884/displaying-index-as-subscript-on-output-e-g-ci-c-i-with-notation-or for display and LaTex output (i.e., a$x -> $a_x$ after the Notation is used. – jlperla Jun 12 '14 at 16:45
  • Shucks, a simple StringReplace[exp, "$" -> "@"] does not result in your stated precedence ;-( – Yves Klett Jun 12 '14 at 16:45
  • One possible simplification: I only need 2 levels deep, e.g. b$x$y -> b[x][y] – jlperla Jun 12 '14 at 16:51
  • 1
    Don't use these symbol names. Forms such as a$1 are used internally. If you use it yourself, it might lead to conflicts and random breakage. Generally, it's a bad idea to try to manipulate code as strings (e.g. rewrite names programmatically) in Mathematica. Expression rewriting is almost always better than string rewriting. – Szabolcs Jun 12 '14 at 17:12
  • @Szabolcs Thank you. My understanding was that Mathematica only reserves the $ character as the first character in a symbol name? Nothing would make me happier than to avoid the $, but I am limited in the symbol name separators available in mathematica. Of course, the _ I would use in C++ doesn't work, and the ` as a separator introduces namespaces. Do I have any other options? All of this is to ensure that I can still do algebra with subscripts, hats, etc. and get it to display correctly, and I can't figure out any other ways. – jlperla Jun 12 '14 at 17:30
  • @jlperla Try Module[{a}, a] – Kuba Jun 12 '14 at 17:31
  • @jlperla Actually $ is not reserved as the first character. It is common practice to use $names for constants. You will run into trouble specifically when the $ is preceded by something and followed by a number. If using a[x] instead of a$x really doesn't work in your case, you can try a$x, but I definitely wouldn't try a$1. – Szabolcs Jun 12 '14 at 17:44
  • Thanks to all. @Kuba can you explain the Module[{a},a] approach? – jlperla Jun 12 '14 at 17:59

1 Answers1

2

Keep in mind Szabolcs comment.

But if it is about string manipulation exercise:

exp2 = "a$x+b$x$y+a$x[d]+a$x[d$y]";

patt = (NumberString | LetterCharacter) ..;

StringReplace[exp2, 
              x : ((patt ~~ "$") .. ~~ patt
                  ) :> StringJoin @@ Riffle[StringSplit[x, "$" -> "["], "]", {4, -1, 3}]]
"a[x]+b[x][y]+a[x][d]+a[x][d[y]]"

Edit

exp = a$x + b$x$y + a$x[d] + a$x[d$y];
exp /. s_Symbol /; StringMatchQ[SymbolName[s], "*$*"] :> ToExpression[
          StringJoin @@ Riffle[StringSplit[SymbolName[s], "$" -> "["], "]", {4, -1, 3}]]
a[x] + a[x][d] + a[x][d[y]] + b[x][y] 
Kuba
  • 136,707
  • 13
  • 279
  • 740
  • I need this to work on, and then stay as, the expression tree, rather than a string. Is that possible? – jlperla Jun 12 '14 at 18:01
  • @jlperla at some point you will have to work with string imo. but maybe not :P I don't know. Here you can exp2 = ToString[exp] and use ToExpression at the end. Only if those symbols have no values. – Kuba Jun 12 '14 at 18:34
  • Is there any way to use a Replace with a pattern matching on the symbol, which then does the transformation only on that node in the expression tree? For example, p_Symbol?StringMatchQ[SymbolName[p], "$"] :> ??????? – jlperla Jun 12 '14 at 19:10
  • @jlperla is my edit what you are after? – Kuba Jun 12 '14 at 19:13
  • Perfect, that is it. Seems to work with arbitrary expression trees, and I can't see any issues. Thanks. – jlperla Jun 12 '14 at 20:16