3
$\begingroup$

I have 4 different constants in a given expression. Due to their physical interpretation, it makes sense to label them with non numerical indices, such as $r_+$. The problem is that they prevent even simple commands from working, like Solve[A (r - r_+)== B , r] (imagine the "plus sign" as an index of the second "r").

Is there any elegant way out of this? I certainly cannot name my variable $r+$ and I would like to avoid large names (since my expressions are already too large).

Thanks in advance!

$\endgroup$
5
  • 2
    $\begingroup$ Just call it rp. You could also use r₊ (U+208A) subscript plus sign, but I recommend against that. Also Subscript[r, "+"], but I don't like subscripts as they can cause problems - search for why. $\endgroup$ – flinty May 26 at 21:00
  • 1
    $\begingroup$ Could also use r["+"]. $\endgroup$ – Daniel Lichtblau May 27 at 1:46
  • $\begingroup$ Dear Lichtblau, that does NOT qualify as an index, right? I guess that will solve my issue, in spite of not being so clean as an actual subscript. $\endgroup$ – Sergio Jorás May 27 at 11:49
  • $\begingroup$ If i use both suggestions I've received, i.e, Format[r["+"]] = Subscript["r", "+"]; Solve[A (x - r["+"]) == B, r["+"]] Then I'll get exactly what I was looking for: $r_+= \frac{A x-B}{A}$. $\endgroup$ – Sergio Jorás May 27 at 11:51
  • $\begingroup$ However, it's not possible to calculate the derivative with a ('), since it will derive only the "r" in $r_+$, leaving a [["+"]]! The full command D[r-r[["+"]],r] works just fine. $\endgroup$ – Sergio Jorás May 28 at 11:22
4
$\begingroup$
Clear["Global`*"]

Use Format to format output display of variables, e.g., rm and rp

Format[rm] = Subscript["r", "-"];
Format[rp] = Subscript["r", "+"];

Solve[A (r - rp) == B, r][[1]]

enter image description here

$\endgroup$
2
  • $\begingroup$ Dear Hanlon, if I use "Format" I'll have to use rm in every input formula and it's only the output that will show $r_+$, right? $\endgroup$ – Sergio Jorás May 27 at 11:46
  • $\begingroup$ Yes, Format only affects output display. $\endgroup$ – Bob Hanlon May 27 at 13:53
1
$\begingroup$

You could modify how Solve works with subscripts:

Unprotect[Solve];
Solve /: Solve[a__] /; !FreeQ[{a}, _Subscript] := Block[{CompressedData},
    With[
        {z = Unevaluated[Solve[a]] /. s_Subscript :> CompressedData[Compress[s]]},
        z /; !MatchQ[z, _Solve]
    ]
]
Protect[Solve];

Then:

Solve[A (r - Subscript[r, "+"]) == B, r]

{{r -> (B + A Subscript[r, "+"])/A}}

Another example:

Solve[1/r == 1/Subscript[r, 1] + 1/Subscript[r, 2], r] //TeXForm

$\left\{\left\{r\to \frac{r_1 r_2}{r_1+r_2}\right\}\right\}$

$\endgroup$

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.