Tag Info

New answers tagged

0

Here's a completely different take (from my other answer, which I think justifies a separate answer) that somewhat automates the idea of using HoldForm on the variables. It works as long as you name the variables consistently. It's still not perfect, since, you need to make the List of replacements, but once that's done, you don't need to do the cumbersome ...


1

You can use Inactivate and Activate to control the evaluation of the right-hand side. expression = Inactivate[a + 1]; g[x_] := x Activate[expression /. a -> 1]; g[2] (* 4 *) expression (* a + 1 *) Inactivate prevents the execution while Activate executes without losing the inactivated expression. Hope this helps.


7

There are two possibilities you could be aiming for. First, I'll take your question literally and just inject expression into the Module: expression = a + 1; With[{expression = expression}, g[x_] := Module[ {a, b}, a = 1; b = expression; x*b]] expression = 1 (* ==> 1 *) g[xi] (* ==> (1 + a) xi *) As the result after changing ...


2

As long as you use Set (m =) rather than SetDelayed (m :=) the matrix will not be given new values unless you reevaluate the definition of m. SeedRandom[1]; Clear[m] m = RandomReal[{0, 1}, {2, 2}] {{0.817389, 0.11142}, {0.789526, 0.187803}} m {{0.817389, 0.11142}, {0.789526, 0.187803}} m {{0.817389, 0.11142}, {0.789526, 0.187803}} m ...


2

Actually, it turns out that what I was looking for was a simple use of Evaluate, i.e. the following code: var={v1,v2}; Block[Evaluate[var],Print[4]] does the trick, provided there are no values assigned to v1 and v2. If that is an important consideration, the more complicated code from the other answers can be used, but for me the above simple solution ...


4

Note: shown below is an answer to the first version of the OP's question Here is a simple example using scoping constructs, namely Block in this case. The idea is that you can indicate symbols to make local to Block, which implements dynamic scoping: take a look at the "Background and Context" section of its docs for a more complete explanation. Symbols ...


4

As @MarcoB states, you should probably use scoping constructs... If you're opposed to that idea, you can set the Notebooks default context to be Unique to Each Cell Group. I wouldn't recommend that, but it works. You can set that under Evaluation > Notebook's Default Context > Unique to Each Cell Group: Note, to escape this, you need to specify ...


1

I interpret this question as asking how define a function that works like the built-in functions Part, AppendTo, and PrependTo; i.e., a function that performs non-standard argument evaluation because it has been given one of the attributes from the Hold family of attributes. Normally, in what is referred to as standard evaluation, all the actual arguments ...



Top 50 recent answers are included