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:
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
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP