Group Abstract Group Abstract

Message Boards Message Boards

0
|
2.6K Views
|
8 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Should the indexes also be defined as local variables in a custom function?

Posted 2 years ago

See my following example:

In[627]:= getTrMat//ClearAll;
getTrMat[ns_]:=Block[{dim=Sqrt[Length[ns[[1]]]],x},
If[ Length[ns]==1,
Partition[ns//First,dim],
Partition[Sum[x[i] ns[[i]], {i, Length[ns]}],dim]
]
]

In[639]:= ns ={{3/4, 5/8, 5/8, 0, 5/8, 3/4, 5/8, -(1/8), 5/8, 5/8, 3/4, -(1/8), 0, 
  0, 0, 1}};
getTrMat[ns]

Out[640]= {{3/4, 5/8, 5/8, 0}, {5/8, 3/4, 5/8, -(1/8)}, {5/8, 5/8, 3/
  4, -(1/8)}, {0, 0, 0, 1}}

In the above example, should the index variable i also be defined as local variables or not? More specifically, should I use which of the following forms at the beginning of the Block command?

Block[{dim=Sqrt[Length[ns[[1]]]], x},
Block[{dim=Sqrt[Length[ns[[1]]]], x, i}, 

Regards, Zhao

POSTED BY: Hongyi Zhao
8 Replies
Posted 2 years ago

You are right, there is no problem with your use. My mistake, sorry.

POSTED BY: Hans Milton
Posted 2 years ago

Thank you for pointing this out. Then the following confirms my previous conclusion:

In[26]:= i=7
getTrMat//ClearAll;
getTrMat[ns_]:=Block[{dim=Sqrt[Length[ns[[1]]]],x},
If[ Length[ns]==1,
(*Partition[ns//First,dim],*)
Partition[Sum[x[i] ns[[i]], {i, Length[ns]}],dim]
]
]

ns ={{3/4, 5/8, 5/8, 0, 5/8, 3/4, 5/8, -(1/8), 5/8, 5/8, 3/4, -(1/8), 0, 
  0, 0, 1}};
getTrMat[ns]

Out[26]= 7

Out[30]= {{(3 x[1])/4, (5 x[1])/8, (5 x[1])/8, 0}, {(5 x[1])/8, (
  3 x[1])/4, (5 x[1])/8, -(x[1]/8)}, {(5 x[1])/8, (5 x[1])/8, (
  3 x[1])/4, -(x[1]/8)}, {0, 0, 0, x[1]}}
POSTED BY: Hongyi Zhao
Posted 2 years ago

Hi Milton,

I read the referred discussion above, which suggested the method ClearAll["Global`*"]. But this will erase all user defined variable and function names, but I just want to clear the current definition of the function name if it has been defined.

POSTED BY: Hongyi Zhao
Posted 2 years ago

Zhao, somewhat off topic: Your use of ClearAll in

getTrMat // ClearAll;

does not really do anything. See this recent discussion.

POSTED BY: Hans Milton
Posted 2 years ago

Also, if you must have a localized codeblock, Module is the more expected construct here rather than Block.

POSTED BY: Eric Rimbey
Posted 2 years ago

You haven't tested the branch that actually uses i, so you can't conclude that yet.

POSTED BY: Eric Rimbey
Posted 2 years ago

So i is already a local variable in itself and no additional declaration is required:

In[740]:= i=7
getTrMat//ClearAll;
getTrMat[ns_]:=Block[{dim=Sqrt[Length[ns[[1]]]],x},
If[ Length[ns]==1,
Partition[ns//First,dim],
Partition[Sum[x[i] ns[[i]], {i, Length[ns]}],dim]
]
]

ns ={{3/4, 5/8, 5/8, 0, 5/8, 3/4, 5/8, -(1/8), 5/8, 5/8, 3/4, -(1/8), 0, 
  0, 0, 1}};
getTrMat[ns]

Out[740]= 7

Out[744]= {{3/4, 5/8, 5/8, 0}, {5/8, 3/4, 5/8, -(1/8)}, {5/8, 5/8, 3/
  4, -(1/8)}, {0, 0, 0, 1}}
POSTED BY: Hongyi Zhao
Posted 2 years ago

You could just test. In the form where i is not declared locally inside the Block, just add a line before the Block like this: i=7. Now, if that breaks your function, you know that i needs to be declared locally.

POSTED BY: Eric Rimbey
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard