user2679290
user2679290

Reputation: 197

Local variable and Block usage

I wrote code that executes a function it receives from the (future) client, in a loop with some parameters. will call it func(name it).

Inside the function the client usually generate expression in the same variables(by GetUncertainty - each variable must be cleared before use). To do so , the simple idea is to use Block. Later , a code is executed that handles di and i outside the function.So, di and i must be globals(there could be more, it is flexible).

BTW, I know it is not efficient, but efficiency is not an issue.

func[v_, a_, r_] := 
(V = v; A = a; R = r; 
 Block[{V, A, R},i = V A + A 10 + R 100; di = GetUncertainty[i, {V, A, R}];] ;
 Print[di])

The problem is that the client must reset the vars by hand. That means that the function parameters can't be V_,A_,R_ , otherwise The vars in the block will be replace by the values. I didn't manage to overcome this in any other way.

Another question in a similar issue. if I have vars = {V,A,R,DR} , then Block[vars , ..code.. ] , throws error that it is not a list.whereas Block[ {V,A,R,DR},..code..] works. How to overcome this?

Thanks.

Upvotes: 0

Views: 251

Answers (2)

Mr.Wizard
Mr.Wizard

Reputation: 24336

If I understand your application what you need are Formal Symbols. These are a set of Symbols with the Attribute Protected so that they cannot accidentally be assigned a value. They may be entered with e.g. Esc$AEsc for Formal Capital A. You can then use ReplaceAll (short form /.) as george showed to substitute your desired values.

Your code would be something like this:

func[v_, a_, r_] :=
  Module[{i, di},
    i = \[FormalCapitalV] \[FormalCapitalA] + \[FormalCapitalA] 10 + \[FormalCapitalR] 100;
    di = GetUncertainty[i, {\[FormalCapitalV], \[FormalCapitalA], \[FormalCapitalR]}];
    di /. {\[FormalCapitalV] -> v, \[FormalCapitalA] -> a, \[FormalCapitalR] -> r}
  ]

That looks horrible here but in a Notebook it looks like this:

enter image description here

I included Module to show how you should properly localize utility Symbols such as i and di but this particuarly simple function could also be written without them:

enter image description here

Your second question regarding "vars = {V,A,R,DR} then Block[vars , ..code.. ]" is answered here: How to set Block local variables by code?


Dedicated StackExchange site:
enter image description here

Upvotes: 1

agentp
agentp

Reputation: 6989

its hard to unravel what you are trying to do, but the best approach may be to simply never assign values to symbols that need to be used as pure symbols in some context. Then you don't even need the Block[].

func[v_, a_, r_] := (
    i = V A + A 10 + R 100; 
    di = GetUncertainty[i, {V, A, R}];
    Print[di /. {V->v,A->a,R->r])

starting your own symbol names with Caps is frowned upon by the way as you risk conflict with built in symbols.

Note also there is a dedicated site mathematica.stackexchange.com

Upvotes: 1

Related Questions