# How to change the variables Qi's to Ri's in one or two steps

I was wondering if there is a command in Mathematica to change all the following Qi's to Ri's? For example, change Q1 to R1, Q2 to R2, etc.

 Q1 a1 + Q2 a2 + Q3 a3 + Q4 a4 + Q5 a5 + Q6 a6 + Q7 a7 + Q8 a8 +
Q9 a9 + Q10 a10 + Q11 a11 + Q12 a12 + Q13 a13 + Q14 a14 + Q15 a15 +
Q16 a16 + Q17 a17 + Q18 a18 + Q19 a19 + Q20 a20 + Q21 a21 + Q22 a22 +
Q23 a23 + Q24 a24 + Q25 a25 + Q26 a26 + Q27 a27 + Q28 a28 +
Q29 a29 + Q30 a30 + Q31 a31 + Q32 a32 + Q33 a33 + Q34 a34 + Q35 a35 +
Q36 a36 + Q37 a37 + Q38 a38 + Q39 a39 + Q40 a40 + Q41 a41 +
Q42 a42 + Q43 a43 + Q44 a44


I could go through and change the Qi's by hand, but I think there should be a more efficient way to do this.

Thank you.

• Do you need to handle the case where some of the Qi symbols already have a value assigned to them? – Mr.Wizard Jul 18 '12 at 4:54
• Thank you for this comment @Mr.Wizard . All the answers below have been helpful! Yes, the Qi's and Ri's will eventually have a value to them (for example, Q1 = 1/2, Q2 = 0, Q3 = 1, etc and the same with the Ri's but the symbols Ri may have different values from the Qi's). At the moment, I'm not sure what values they will be. – math-visitor Jul 18 '12 at 9:31
• I added a method to my answer that should be useful in that case. – Mr.Wizard Jul 19 '12 at 0:43
• in the long run it saves a lot of work to generate expressions like yours programmatically, e.g. Sum[Q[i] a[i], {i, 44}], then you can go back later and just have to change the Q in one place. – Thies Heidecke Jul 19 '12 at 9:02

## 4 Answers

I believe I would use:

expr = Q1 a1 + Q2 a2 + Q3 a3 + Q4 a4 + Q5 a5 + Q6 a6 + Q7 a7 + Q8 a8 +
Q9 a9 + Q10 a10 + Q11 a11 + Q12 a12 + Q13 a13 + Q14 a14 +
Q15 a15 + Q16 a16 + Q17 a17 + Q18 a18 + Q19 a19 + Q20 a20 +
Q21 a21 + Q22 a22 + Q23 a23 + Q24 a24 + Q25 a25 + Q26 a26 +
Q27 a27 + Q28 a28 + Q29 a29 + Q30 a30 + Q31 a31 + Q32 a32 +
Q33 a33 + Q34 a34 + Q35 a35 + Q36 a36 + Q37 a37 + Q38 a38 +
Q39 a39 + Q40 a40 + Q41 a41 + Q42 a42 + Q43 a43 + Q44 a44;

expr /. S_Symbol :> Symbol @ StringReplace[SymbolName@S, "Q" -> "R"]


Or more restrictively:

rule = "Q" ~~ x : DigitCharacter .. :> "R" <> x;

expr /. S_Symbol :> Symbol @ StringReplace[SymbolName@S, rule]


I feel that operating only on Symbols is cleaner and safer than converting the entire expression to and from a String.

It is not clear to me if you need this, but if you want to do the replacement after (while) Qi's are assigned values you can do it like this:

{Q1, Q2, Q3, Q4, Q5, Q6, Q7, Q8, Q9, Q10, Q11, Q12} = Range@12;

expr = Hold[Q1 a1 + Q2 a2 + Q3 a3 + Q4 a4 + Q5 a5 + Q6 a6 +
Q7 a7 + Q8 a8 + Q9 a9 + Q10 a10 + Q11 a11 + Q12 a12];

expr /. S_Symbol :>
RuleCondition @ Symbol @ StringReplace[SymbolName@Unevaluated@S, "Q" -> "R"]

Hold[R1 a1 + R2 a2 + R3 a3 + R4 a4 + R5 a5 + R6 a6 +
R7 a7 + R8 a8 + R9 a9 + R10 a10 + R11 a11 + R12 a12]

xQ = Q1 a1 + Q2 a2 + Q3 a3 + Q4 a4 + Q5 a5 + Q6 a6 + Q7 a7 + Q8 a8 +
Q9 a9 + Q10 a10 + Q11 a11 + Q12 a12 + Q13 a13 + Q14 a14 + Q15 a15 +
Q16 a16 + Q17 a17 + Q18 a18 + Q19 a19 + Q20 a20 + Q21 a21 +
Q22 a22 + Q23 a23 + Q24 a24 + Q25 a25 + Q26 a26 + Q27 a27 +
Q28 a28 + Q29 a29 + Q30 a30 + Q31 a31 + Q32 a32 + Q33 a33 +
Q34 a34 + Q35 a35 + Q36 a36 + Q37 a37 + Q38 a38 + Q39 a39 +
Q40 a40 + Q41 a41 + Q42 a42 + Q43 a43 + Q44 a44;

xR = ToExpression@StringReplace[ToString@xQ, "Q" -> "R"]

• Thank you belisarius! We have so many options... – math-visitor Jul 18 '12 at 0:31

You can use the Find and Replace command: +F on the Mac or Ctrl+F on a PC.

• (+1) Although I also liked the string replacement answers, I can't imagine a case where this answer would not be my first choice. – Jens Jul 18 '12 at 2:38
• @Jens perhaps I'm missing something but how do you change all the Qi symbols without also replacing all the instances of the letter Q everywhere? – Mr.Wizard Jul 18 '12 at 7:19
• No need to use "Replace all" @Mr.Wizard just "Find and Replace" For code of this size putting it on a separate .nb would isolate it if you wanted to use "Replace All" too. – R Hall Jul 18 '12 at 10:39

You can do this programmatically by converting the expression to strings, replacing Qs with Rs and then converting it back to an expression. For example:

expr := Q1 a1 + Q2 a2 + Q3 a3 + Q4 a4 + Q5 a5 + Q6 a6 + Q7 a7 +
Q8 a8 + Q9 a9 + Q10 a10 + Q11 a11 + Q12 a12 + Q13 a13 + Q14 a14 +
Q15 a15 + Q16 a16 + Q17 a17 + Q18 a18 + Q19 a19 + Q20 a20 +
Q21 a21 + Q22 a22 + Q23 a23 + Q24 a24 + Q25 a25 + Q26 a26 +
Q27 a27 + Q28 a28 + Q29 a29 + Q30 a30 + Q31 a31 + Q32 a32 +
Q33 a33 + Q34 a34 + Q35 a35 + Q36 a36 + Q37 a37 + Q38 a38 +
Q39 a39 + Q40 a40 + Q41 a41 + Q42 a42 + Q43 a43 + Q44 a44;

expr2 := ToExpression[StringReplace[ToString@expr, "Q" ~~ x : DigitCharacter .. :> "R" ~~ x]]

expr2
(* a1 R1 + a10 R10 + a11 R11 + a12 R12 + a13 R13 + a14 R14 + a15 R15 +
a16 R16 + a17 R17 + a18 R18 + a19 R19 + a2 R2 + a20 R20 + a21 R21 +
a22 R22 + a23 R23 + a24 R24 + a25 R25 + a26 R26 + a27 R27 + a28 R28 +
a29 R29 + a3 R3 + a30 R30 + a31 R31 + a32 R32 + a33 R33 + a34 R34 +
a35 R35 + a36 R36 + a37 R37 + a38 R38 + a39 R39 + a4 R4 + a40 R40 +
a41 R41 + a42 R42 + a43 R43 + a44 R44 + a5 R5 + a6 R6 + a7 R7 +
a8 R8 + a9 R9 *)

• Wow, thank you R.M! – math-visitor Jul 18 '12 at 0:30