TransWikia.com

Overbar as counter in a loop

Mathematica Asked by Minkowski on April 16, 2021

I want to create a table with four different counters that I want to call $a, bar{a}, b, bar{b}$. For that my obvious code is

$$text{Table}left[pleft[a,bar{a},b,bar{b}right],{a,0,1},left{bar{a},0,1right},{b,0,1},left{bar{b},0,1right}right] // text{Grid}$$

which returns

{{p[0,0,0,0],p[0,1,0,1]},{p[0,1,1,0],p[0,1,1,1]}}   {{p[0,0,0,0],p[0,1,0,1]},{p[0,1,1,0],p[0,1,1,1]}}
{{p[1,0,0,0],p[1,0,0,1]},{p[1,0,1,0],p[1,1,1,1]}}   {{p[1,1,0,0],p[1,1,0,1]},{p[1,0,1,0],p[1,1,1,1]}}

However a quick look shows that this is not the expected result – elements in the first row are repeated. If I write

Table[p[a, c, b, d] , {a, 0, 1}, {c, 0, 1}, {b, 0, 1}, {d, 0, 1}] // Grid

instead then I get the desired output. So my question is double:

  1. Why does it happen?
  2. How can it be fixed? That is, can I use variables such as $bar{a}$ or $a’$ or $a_1$ for loops modifying something or I am forced to used other names which do not involve these symbols?

2 Answers

You could give OverBar the HoldFirst attribute so that it doesn't depend on the value of a:

SetAttributes[OverBar,HoldFirst]

Then:

t1 = Table[
    p[a, OverBar[a], b, OverBar[b]],
    {a, 0, 1}, {OverBar[a], 0, 1}, {b, 0, 1}, {OverBar[b], 0, 1}
];
t2 = Table[p[a, c, b, d], {a, 0, 1}, {c, 0, 1}, {b, 0, 1}, {d, 0, 1}];

t1 === t2

True

Correct answer by Carl Woll on April 16, 2021

The problem is that Mathematica treats OverBar as a function, so OverBar[a] is a function of your symbol a, not an independent symbol.

I would avoid using such symbols, but if the appearance is important to you, investigate the Symbolize functionality of the Notation package.

Answered by mikado on April 16, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP