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 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?

share|improve this question
    
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! –  wxg Sep 20 '12 at 14:27
    
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. –  wxg Sep 20 '12 at 15:25
    
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 –  belisarius Sep 21 '12 at 1:41
add comment

2 Answers 2

up vote 1 down vote accepted

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
share|improve this answer
    
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. –  wxg Sep 21 '12 at 1:46
    
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? –  wxg Sep 21 '12 at 9:01
    
@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... –  Albert Retey Sep 21 '12 at 10:50
    
Note: Module[{x}, x] vs Block[{x}, x]. The x inside Module is not symbolic x, it's a unique symbol. –  amr Sep 21 '12 at 22:12
    
Thank belisarius. I find my problem can be easily solved by making the program to a small package. –  wxg Sep 25 '12 at 2:24
add comment

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 *)
share|improve this answer
add comment

Your Answer

 
discard

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.