4
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ Do you prefer exact computations or is it okay to use (inexact) floating point numbers? The latter would speed up your calculations tremendously. $\endgroup$ – Henrik Schumacher Aug 29 '18 at 9:07
3
$\begingroup$

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]
    ];
$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Richard Burke-Ward Aug 29 '18 at 8:58
  • $\begingroup$ 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]]; $\endgroup$ – Henrik Schumacher Aug 29 '18 at 9:01
  • $\begingroup$ 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... $\endgroup$ – Richard Burke-Ward Aug 29 '18 at 9:09
  • $\begingroup$ Dear Richard, always at your service ;) I exanded my answer. Does it help you? $\endgroup$ – Henrik Schumacher Aug 29 '18 at 9:15
  • $\begingroup$ 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. $\endgroup$ – Richard Burke-Ward Aug 29 '18 at 9:18
3
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.