1
$\begingroup$

I need to insert a very large expression in a Mathematica program. So I first Put[large expression,"file"] and then use

Block[{}, Get["file"];...]

to implement the function. But the variables here is not local variables and may interact with other programs. I have tried to use

Module[{},Get["file"];...],

but it does not work. Do you know a clever way to do this?

$\endgroup$
3
  • $\begingroup$ I know some. But I do not know how to solve this problem with Packages and Contexts. Can you teach me? Thank you very much! $\endgroup$
    – wxg
    Sep 20, 2012 at 14:27
  • $\begingroup$ I have tried this example and the result is 22 a b + 56 c + 13 a d. The value a=1 is not used and this is just my question. $\endgroup$
    – wxg
    Sep 20, 2012 at 15:25
  • $\begingroup$ Welcome to Mathematica.SE! For improving your experience in this site, I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the FAQs! 3) When you see good Q&A, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. ALSO, remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign $\endgroup$ Sep 21, 2012 at 1:41

2 Answers 2

1
$\begingroup$

From the help:

Block[{a = 1, b = 2, c = 3, d = 4}, Get["ExampleData/language"]]

or

Module[{a = 1, b = 2, c = 3, d = 4}, Evaluate@Get["ExampleData/language"]]

If you want your variables to be global, simply

a = 1; b = 2; c = 3; d = 4;
<< ExampleData/language
$\endgroup$
5
  • $\begingroup$ I think the form Module[{}, Evaluate@Get["file"]] is just what I need! And dose this equal to directly copy the file in the program? I usually store some very large expressions in the file and then call it to do the numerical calculation. $\endgroup$
    – wxg
    Sep 21, 2012 at 1:46
  • $\begingroup$ I find a new problem now. When I try to do Module[{a = 1, b = 2, c = 3, d = 4,x}, x=Evaluate@Get["ExampleData/language"]], the result is still 22 a b + 56 c + 13 a d not 264. Why it can not assign the value of a large expression to a variable? And I often need this assignment in my program. Can you help me? $\endgroup$
    – wxg
    Sep 21, 2012 at 9:01
  • $\begingroup$ @wxg: I think the Block version is what you want. This is one of the rare cases where Block is better suited than Module. While the Module version will in principle work, it will use the global definitions of the local variables if they exists, due to the Evaluate. It will also stop working when you add to the body and Evaluate isn't at the right level anymore... $\endgroup$ Sep 21, 2012 at 10:50
  • $\begingroup$ Note: Module[{x}, x] vs Block[{x}, x]. The x inside Module is not symbolic x, it's a unique symbol. $\endgroup$
    – amr
    Sep 21, 2012 at 22:12
  • $\begingroup$ Thank belisarius. I find my problem can be easily solved by making the program to a small package. $\endgroup$
    – wxg
    Sep 25, 2012 at 2:24
0
$\begingroup$

You could also try With, but note that the Evaluate piece is essential for both Module and With. Compare:

With[{a = 1, b = 2, c = 3, d = 4}, Get["ExampleData/language"]]

(* 22 a b + 56 c + 13 a d *)

With:

With[{a = 1, b = 2, c = 3, d = 4}, 
 Evaluate@Get["ExampleData/language"]]

(* 264 *)

Depending on the size of your problem and the number of replacements you need to make, you could also consider ReplaceAll.

Get["ExampleData/language"] /. {a -> 1, b -> 2, c -> 3, d -> 4}

(* 264 *)
$\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.