Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I would like the solutions of my equations to be local only, so I did a Solve inside a Module:

Module[{a, b, c}, Solve[{a == 3* c, b == 2 *a}, {a, b}]]

and the result is something like

{{a$2389 -> 3 c$2389, b$2389 -> 6 c$2389}}

why do I get dollar signs, and how can I prevent that to have a readable solution even inside a Module?

share|improve this question
From the Module documentation: "Module creates new symbols to represent each of its local variables every time it is called. Module creates a symbol with name xxx$nnn to represent a local variable with name xxx. The number nnn is the current value of $ModuleNumber." –  Oleksandr R. Jul 8 '13 at 12:34
OK so no way to get an expression more readable inside a Module? When you have lots of variables those added ModuleNumber really prevent you from understanding what the equation says. –  su1 Jul 8 '13 at 12:38
add comment

closed as off-topic by Oleksandr R., rcollyer, Artes, Ajasja, Simon Woods Jul 8 '13 at 13:07

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Oleksandr R., rcollyer, Artes, Ajasja, Simon Woods
If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

How about using Block instead:

w = Block[{a, b, c, sol}, Hold[sol] /. sol -> Solve[{a == 3*c, b == 2*a}, {a, b}]]

This returns Hold[{{a -> 3 c, b -> 6 c}}] which is maybe more what you were expecting (the enclosing Hold is necessary so that the variables are not replaced by their values, if any exist). Besides the individual documentation for Module and Block, there is a good bit of documentation about scoping constructs, including a comparison of Blocks and Modules.

Yet another alternative is (Mr. Wizard's suggestion)

w = Block[{a, b, c}, Hold @@ Solve[{a == 3*c, b == 2*a}, {a, b}]]

which uses Apply (in the form of @@). The output is the same.

The question then arises, how to use this? Taking

t = First[First[w //. Hold -> List]]

gives a list of rules like {a->3 c, b->6 c}. These do not assign any variables. With this t, the a's and b's can be accessed using replacements such as

a //. t 

which returns 3 c or b//.t which gives 6 c.

share|improve this answer
You need Block[{a, b, c, sol}, Hold[sol] /. sol -> Solve[{a == 3*c, b == 2*a}, {a, b}]], otherwise the Block is effectively pointless. –  Oleksandr R. Jul 8 '13 at 12:42
@OleksandrR -- maybe you should be writing this answer. If you do, I'll remove mine... –  bill s Jul 8 '13 at 12:46
Problem solved, assuming you don't object. I don't think there's a good argument for two people writing answers to a question that will most probably be deleted anyway. If OP comes back and says that for some reason it is absolutely vital to use Module rather than Block, perhaps I will write one then. –  Oleksandr R. Jul 8 '13 at 12:57
@OleksandrR -- I've incorporated your comment into the answer then. –  bill s Jul 8 '13 at 13:08
The problem is, I already edited it... –  Oleksandr R. Jul 8 '13 at 13:10
show 12 more comments

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