Work with equation involving different independent variables [closed]

Sometimes I will use an equation, for example, imagine the old chemistry ideal gas law, PV=nRT. Depending on the situation I may want to use different variables as the independent variable. So, for example, in the case of the ideal gas law there are 4 different possible independent variables.

What is a good way to manage this situation and set it up in Mathematica so I can easily compute any particular variable in terms of the others in a literate way?

• V /. Solve[P V == n R T, V][[1]]? Commented Jan 19, 2017 at 14:01
• @Feyre I know I can repetitively solve the same equation over and over again. I am hoping there is a more elegant solution. Commented Jan 19, 2017 at 14:26
• What kind of thing do you want? Commented Jan 19, 2017 at 14:33
• It is not quite clear what you're asking. Please elaborate by a specific example. In the meanwhile I'm voting to close this question. Commented Jan 19, 2017 at 16:18

ClearAll[gasLaw];

gasLaw[var_Symbol, opts : OptionsPattern[]] :=
Solve[P V == n R T, var, Reals,
FilterRules[{opts}, Options[Solve]]][[1]]

gasLaw[P]

(*  {P -> (n R T)/V}  *)

Flatten[gasLaw /@ Variables[Level[P V == n R T, {-1}]]]

(*  {n -> (P V)/(R T), P -> (n R T)/V, R -> (P V)/(n T), T -> (P V)/(n R),
V -> (n R T)/P}  *)

• What is opts : OptionsPattern[] and FilterRules[] doing in this? Commented Sep 21, 2020 at 14:17
• @Sterling - If you evaluate Options[Solve] you will see that Solve can accept several options. This code enables gasLaw to accept these options and pass them to Solve. Not very important for this specific equation, but is useful as a general template. Commented Sep 21, 2020 at 14:30
• Ah, great! Thank you. Commented Sep 21, 2020 at 14:47

Do you mean something like this ?

var = n;
Solve[P V == n R T, var]