## Raise a table of pre-existing data to the power of of one of the variables

4

NOTE: I have edited this question to give a table whose first elements don't count up from 0 to 5.

I'm using a sample table here, given by Table[a/b + a^(1/2), {b, 1, 6}, {a, 1, 6}] - simply because it's small and easy. This gives me the following data:

testtable1 = {{2, 2 + Sqrt[2], 3 + Sqrt[3], 6, 5 + Sqrt[5],
6 + Sqrt[6]}, {3/2, 1 + Sqrt[2], 3/2 + Sqrt[3], 4, 5/2 + Sqrt[5],
3 + Sqrt[6]}, {4/3, 2/3 + Sqrt[2], 1 + Sqrt[3], 10/3,
5/3 + Sqrt[5], 2 + Sqrt[6]}, {5/4, 1/2 + Sqrt[2], 3/4 + Sqrt[3], 3,
5/4 + Sqrt[5], 3/2 + Sqrt[6]}, {6/5, 2/5 + Sqrt[2], 3/5 + Sqrt[3],
14/5, 1 + Sqrt[5], 6/5 + Sqrt[6]}, {7/6, 1/3 + Sqrt[2],
1/2 + Sqrt[3], 8/3, 5/6 + Sqrt[5], 1 + Sqrt[6]}}


But now I want to raise that table to the power of b - without going back to create a new table. Of course, it's easy to say Table[(a/b + a^(1/2))^b, {b, 1, 6}, {a, 1, 6}] - but that's only because this is a simple table. I'm looking for an operation that I can apply generically to any testtable1, because the calculations involved in getting to my actual testtable1 are very big and very slow, and hit the error limits of Mathematica - i.e., they become inaccurate. So, I want to crunch the data I have already generated rather than modify the original calculation.

Is this possible? Maybe I need to turn testtable1 into a dataset? Pointers on how to tackle this would be much appreciated.

Do you prefer exact computations or is it okay to use (inexact) floating point numbers? The latter would speed up your calculations tremendously. – Henrik Schumacher – 2018-08-29T09:07:58.557

3

Suppose you generate you table equivalently by

alist = Range[1, 6];
blist = Range[1, 6];
testtable1 = Table[a/b + a^(1/2), {b, blist}, {a, alist}];


Then

Table[(a/b + a^(1/2))^b, {b, blist}, {a, alist}]


can be obtained also by

testtable1^blist


and

Table[(a/b + a^(1/2))^a, {b, blist}, {a, alist}]


can be obtained by

testtable1^ConstantArray[alist, Length[blist]]


The key observation is that ^ (a.k.a. Power) has the attribute Listable.

For a preexisting table

testtable2 = RandomReal[{-1, 1}, {1000, 2000}];


the following should raise each row to the power of its row count:

poweredbyrow = testtable2^Range[1, Length[testtable2]];


The same for powering by column number:

poweredbycol =
testtable2^ConstantArray[
Range[1, Dimensions[testtable2][[2]]],
Length[testtable2]
];


Correct me if I'm wrong, but this solution essentially relies on the fact that the first item in each entry in the table counts up from 0 to 5 - in which case, I gave a poor example, because that's not necessarily the case... I'll edit the original post. – Richard Burke-Ward – 2018-08-29T08:58:24.953

Well, if you generate your table by a = RandomReal[{-1, 1}, {1000}]; b = RandomReal[{-1, 1}, {2000}]; testtable1 = Outer[Plus, b, a];, then you can use testtable1^b;. Btw.: Using the list a as powers can be done as follows: testtable1^ConstantArray[a, Length[b]]; – Henrik Schumacher – 2018-08-29T09:01:44.587

Hi Henrik (again!). Thanks for helping. This is really useful, but again it's dependent on the specific nature of the table. My situation is that I have a very big pre-existing array/table of data, defined only by its name. I want to raise each row to the power of the number of that row. Row 5, raise to the power of 5; row 2, raise to the power of 2... – Richard Burke-Ward – 2018-08-29T09:09:37.767

Thanks again, Henrik. It's going to take me a bit of time to wade through that. I'm sure it's exactly right, but I need to go all the way back to my original calculations, and figure out each step of your cunning plan :-). I'll mark as answered after that, but would you mind checking back in, say, half an hour just to see if I have any further questions? Really appreciate your help. – Richard Burke-Ward – 2018-08-29T09:18:33.013

No problem. Just leave a comment under my post or bing me with @+my name in any other comment; I will be notified. – Henrik Schumacher – 2018-08-29T09:20:06.957

Hi again. This works close to perfectly - Thank you. But I have one extra query. At an earlier stage in generating testtable1, I replaced ComplexInfinity with "-", by adding /. ComplexInfinity -> "-". Now, at those locations in the table, poweredbyrow gives me expressions like -^3. Not sure how to replace them because the powers change, and I don't know how to substitute the text string back to "-". – Richard Burke-Ward – 2018-08-29T10:04:11.387

Apply ReplaceAll with the rule Power["-",p_]->"-" to the output. This will turn all expressions of the form "-"^p back to "-". – Henrik Schumacher – 2018-08-29T10:06:40.913

OK, Henrik, klgr. Marking both your answers. Very much appreciated as always. – Richard Burke-Ward – 2018-08-29T10:10:18.967

You're welcome as always. – Henrik Schumacher – 2018-08-29T10:11:22.390

3

### row $k$ raised to the power $k$:

MapIndexed[#^#2[[1]] &, testtable1, {1}] // MatrixForm // TeXForm


$\left( \begin{array}{cccccc} 2 & 2+\sqrt{2} & 3+\sqrt{3} & 6 & 5+\sqrt{5} & 6+\sqrt{6} \\ \frac{9}{4} & \left(1+\sqrt{2}\right)^2 & \left(\frac{3}{2}+\sqrt{3}\right)^2 & 16 & \left(\frac{5}{2}+\sqrt{5}\right)^2 & \left(3+\sqrt{6}\right)^2 \\ \frac{64}{27} & \left(\frac{2}{3}+\sqrt{2}\right)^3 & \left(1+\sqrt{3}\right)^3 & \frac{1000}{27} & \left(\frac{5}{3}+\sqrt{5}\right)^3 & \left(2+\sqrt{6}\right)^3 \\ \frac{625}{256} & \left(\frac{1}{2}+\sqrt{2}\right)^4 & \left(\frac{3}{4}+\sqrt{3}\right)^4 & 81 & \left(\frac{5}{4}+\sqrt{5}\right)^4 & \left(\frac{3}{2}+\sqrt{6}\right)^4 \\ \frac{7776}{3125} & \left(\frac{2}{5}+\sqrt{2}\right)^5 & \left(\frac{3}{5}+\sqrt{3}\right)^5 & \frac{537824}{3125} & \left(1+\sqrt{5}\right)^5 & \left(\frac{6}{5}+\sqrt{6}\right)^5 \\ \frac{117649}{46656} & \left(\frac{1}{3}+\sqrt{2}\right)^6 & \left(\frac{1}{2}+\sqrt{3}\right)^6 & \frac{262144}{729} & \left(\frac{5}{6}+\sqrt{5}\right)^6 & \left(1+\sqrt{6}\right)^6 \\ \end{array} \right)$

### column $k$ raised to the power $k$:

MapIndexed[#^#2[[2]] &, testtable1, {2}] // MatrixForm // TeXForm


$\left( \begin{array}{cccccc} 2 & \left(2+\sqrt{2}\right)^2 & \left(3+\sqrt{3}\right)^3 & 1296 & \left(5+\sqrt{5}\right)^5 & \left(6+\sqrt{6}\right)^6 \\ \frac{3}{2} & \left(1+\sqrt{2}\right)^2 & \left(\frac{3}{2}+\sqrt{3}\right)^3 & 256 & \left(\frac{5}{2}+\sqrt{5}\right)^5 & \left(3+\sqrt{6}\right)^6 \\ \frac{4}{3} & \left(\frac{2}{3}+\sqrt{2}\right)^2 & \left(1+\sqrt{3}\right)^3 & \frac{10000}{81} & \left(\frac{5}{3}+\sqrt{5}\right)^5 & \left(2+\sqrt{6}\right)^6 \\ \frac{5}{4} & \left(\frac{1}{2}+\sqrt{2}\right)^2 & \left(\frac{3}{4}+\sqrt{3}\right)^3 & 81 & \left(\frac{5}{4}+\sqrt{5}\right)^5 & \left(\frac{3}{2}+\sqrt{6}\right)^6 \\ \frac{6}{5} & \left(\frac{2}{5}+\sqrt{2}\right)^2 & \left(\frac{3}{5}+\sqrt{3}\right)^3 & \frac{38416}{625} & \left(1+\sqrt{5}\right)^5 & \left(\frac{6}{5}+\sqrt{6}\right)^6 \\ \frac{7}{6} & \left(\frac{1}{3}+\sqrt{2}\right)^2 & \left(\frac{1}{2}+\sqrt{3}\right)^3 & \frac{4096}{81} & \left(\frac{5}{6}+\sqrt{5}\right)^5 & \left(1+\sqrt{6}\right)^6 \\ \end{array} \right)$

Also:

Transpose @ MapIndexed[#^#2[[1]] &, Transpose @ testtable1, {1}]
`

same result