Message Boards Message Boards

0
|
6919 Views
|
1 Reply
|
1 Total Likes
View groups...
Share
Share this post:

Global and local variables

Posted 11 years ago
 (* Capture Recapture History *)
 
 myCHcc[K_] := Table[IntegerDigits[n, 2, K], {n, 1, 2^K - 1}]
 
 (* Capture Recapture Probability *)
 
 myindprobCC[CH_]:=Module[{K,ind,fi,li,ans},
 
 
    (* Get $K$ *)
    K = Length[ CH ];
   
    (* First and Last Capture times *)
    fi = First[Position[CH, 1]][[1]];
    li =  Last[Position[CH, 1]][[1]];
   
    (* Get probabilities *)
   
    ans =
  Sum[
   Product[
    Subscript[\[Phi],j]
    *
    Subscript[p,j+1]^CH[[j+1]]
    *
    (1-Subscript[p,j+1])^(1-CH[[j+1]]),
    {j,fi,d-1}
   ]*(1-Subscript[\[Phi],d]),
   {d, li, K}
  ];
   
    ans /. Subscript[\[Phi], K]-> 0
]


K = 3
xij = myCHcc[K]
ans = Map[myindprobCC, xij]

Runs perfectly.

There variables involved are
pars =Variables[ans]
However, say by accident, I have done
Subscript[p, 3] = "Dummy"
BEFORE, I define my function "myindprobCC[CH_]", then we have a problem.
Is there a way to "automatically" put ALL "possible" variables, to the Module as local variables, like what i did for "{K,ind,fi,li,ans}".

The problem is that, with the input arguments K_ and C_, the variables vary.
And this is just a simple example.

One way I can think of is to use Table[] to creat them and declare as local variables by hand, but is there a simpler way?

Thanks
POSTED BY: Casper YC
Making a "subscripted variable" a local symbol in a Module also has difficulties.
 In[1]:= Module[{Subscript[a, 1]},                                                      
          Subscript[a, 1] = 1;                                                          
          Print[Subscript[a, 1]]                                                        
          ]                                                                             
 
 Module::lvsym: Local variable specification {a } contains a  ,
                                               1            1
      which is not a symbol or an assignment to a symbol.
 
Out[1]= Module[{a }, a  = 1; Print[a ]]
                 1    1             1

This is because Subscript[a, 1] is a function call, not a Mathematica symbol.

The attached notebook has suggestions for making subscripted objects robust enough for executable code.
(If you can postpone subscripts until you are making static output for presentation, that might save some work.) 
Attachments:
POSTED BY: Bruce Miller
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract