Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to define a relationship between 3 variables first and then later when I have filled two of the variables with a number, I would like to extract the value of the last value.

For example:

a * b == c; (* Somehow define this *)
Attributes[GetValues] = {HoldAll}
GetValues[known_] := With[known, {a, b, c}]

GetValues[{a = 2, b = 3}] (* => {2, 3, 6} *)
GetValues[{a = 2, c = 10}] (* => {2, 5, 10} *)
GetValues[{b = 10, c = 10}] (* => {1, 10, 10} *)

Only two of the variables will be defined at a time and there should always be only one possible value for the last variable, if the other two values are valid.

You could possible to this with either Solve or Reduce, but how?

share|improve this question
up vote 2 down vote accepted

For very simple examples like your example you could use something like this:

GetValues[known_] := With[known, {a, b, c} /. First[Solve[a*b == c]]]

This works due to the undocumented (?) feature that Solve will work without a second argument and if only one unkown is left it seems to always be doing "the expected thing", e.g.:


I'd be careful if I'd build something on that feature, though. With only minimal additional effort you can also construct a call to Solve explicitly solving for the remaining variable, which then uses documented behavior (I also added some protection from external definitions of a, b or c):

Attributes[GetValues] = {HoldAll};
GetValues[known_] := Block[{a, b, c},With[known,
  {a, b, c} /. Solve @@ {a*b == c, Cases[{a, b, c}, _Symbol]}

Either way: Depending on your requirements and real world relations, might want to add some checking of whether everything worked as intended...

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.