19
$\begingroup$

How can I tell MMA to display input of the form 10^n as $10^n$ and not e.g. as 1000 for $n = 3$ or the rational $\frac{1}{1000}$ for $n = -3$.

This would help to keep tables compact when some parameters sweep across a large range of magnitudes, e.g.

TableForm[Table[x y, {x, Table[10^n, {n, -3, 3}]}, {y, Table[10^n, {n, -3, 3}]}], 
 TableHeadings -> {Table[10^n, {n, -3, 3}], Table[10^n, {n, -3, 3}]}]

enter image description here

$\endgroup$
6
  • $\begingroup$ A nice extension to this question would be to have a way to display exact integer (or rational) multiple of 10^n, such as 12000000, represented by 12 10^6 $\endgroup$
    – andre314
    Jun 21, 2017 at 8:17
  • $\begingroup$ @andre If you don't mind the trailing dot on the mantissa, you can do EngineeringForm@N[12000000] to get 12.*10^6 (or ScientificForm@N[12000000] for 1.2*10^7)... but I do mind. $\endgroup$
    – Casimir
    Jun 21, 2017 at 8:27
  • $\begingroup$ I do mind too, because I want to recognise exact values from approximative values. $\endgroup$
    – andre314
    Jun 21, 2017 at 11:36
  • $\begingroup$ Another possibility for your table would be to use logarithms. $\endgroup$
    – ivbc
    Jun 21, 2017 at 13:51
  • $\begingroup$ @ivbc You mean as in Alexei's answer? $\endgroup$
    – Casimir
    Jun 21, 2017 at 14:03

6 Answers 6

12
$\begingroup$

An alternative would be to define your own number form:

ClearAll[form]
SetAttributes[form, Listable]

form[x_] := ScientificForm[N@x, NumberFormat -> (Superscript[10, #3] &)]
form[1] := Superscript[10, 0]

10^Partition[Range[-6, 6], 7, 1] // form // 
   TableForm[#, TableHeadings -> {#[[4]], #[[4]]}] &

enter image description here

$\endgroup$
2
  • 1
    $\begingroup$ Interesting solution. Unlike the other answers where the scope is obvious, however, defining a custom number format might lead someone who did not write the code himself to expect that form works with all numbers. Instead, it ignores the mantissa (including sign) and acts like a floor function to the exponent, e.g. form[999] = 10^2 or form[-3456] = 10^3. Also, form[10^-\[Infinity]] = 10 but that could be fixed by form[10^-\[Infinity]] := 0. Still, this could lead to trouble when used carelessly. $\endgroup$
    – Casimir
    Jun 21, 2017 at 14:33
  • $\begingroup$ hey @eldo, I came up with a very similar answer before seeing yours. I am not sure if i should edit yours or post mine as a new one, or something else... I posted for now so that someone can tell me what to do. $\endgroup$
    – ivbc
    Jun 21, 2017 at 15:30
7
$\begingroup$

I hadnt seen Eldos answer before now, but I did copy the SetAttributes and ClearAll from him.

Define:

ClearAll[newForm]
SetAttributes[newForm, Listable]

newForm[x_] :=
  If[IntegerQ@x == True && Divisible[x, 10],
   ScientificForm[x // N,
    NumberFormat -> (
      If[Length@Characters@#3 != 0,
        If[ToExpression@#1 == 1,
         Row[{Superscript[10, 
            ToString[ToExpression@#3 - Length@Characters@#1 + 2]]}],
         Row[{x/10^(ToExpression@#3 - Length@Characters@#1 + 2), 
           "\[Times]", 
           Superscript[10, 
            ToString[ToExpression@#3 - Length@Characters@#1 + 2]]}]],
        #1] &)],
   ScientificForm[x]];

So the following comand gives a nice result

v = {1, 8^5, 130, 130., 15400000, 11.^7, 10^3};
newForm[v]

enter image description here

$\endgroup$
4
  • $\begingroup$ OP wanted 10^3 without an integer multiplying it. Your answer gives 1 x 10^3. $\endgroup$ Jun 28, 2017 at 0:04
  • $\begingroup$ Does it? O hadn't tested for this case, thanks for letting me know. I will find a solution latter. $\endgroup$
    – ivbc
    Jun 28, 2017 at 0:12
  • $\begingroup$ @JackLaVigne, error corrected! $\endgroup$
    – ivbc
    Jun 30, 2017 at 18:02
  • $\begingroup$ You got my vote $\endgroup$ Jun 30, 2017 at 18:49
6
$\begingroup$
TableForm[
 Table[
  Superscript[10, x + y],
  {x, -3, 3},
  {y, -3, 3}
  ], TableHeadings -> {
   Table[Superscript[10, n], {n, -3, 3}],
   Table[Superscript[10, n], {n, -3, 3}]}
 ]

table

$\endgroup$
6
$\begingroup$

Try this:

  TableForm[
 Table[10^ToString[Log[10, x y]], {x, Table[10^n, {n, -3, 3}]}, {y, 
   Table[10^n, {n, -3, 3}]}], 
 TableHeadings -> {Table[10^ToString[n], {n, -3, 3}], 
   Table[10^ToString[n], {n, -3, 3}]}]

enter image description here

Have fun!

$\endgroup$
6
$\begingroup$
a = Range[-3, 3];

HoldForm:

TableForm[Replace[Outer[Plus, a, a], x_ :> 10 ^ HoldForm @ x, {2}], 
    TableHeadings -> Replace[{a, a}, x_ :> 10 ^ HoldForm @ x, {2}] ]

enter image description here

Get back to initial form, if needed:

% // ReleaseHold
$\endgroup$
4
$\begingroup$

An example using Format:

Format[h[x_]] := 
 With[{p = Log10[x]}, 
  If[p == 0, Style[1, 20], Style[10^HoldForm[p], 20]]]
r = PowerRange[1/1000, 1000, 10];
tu = Tuples[r, 2];
TableForm[Partition[h@*Times @@@ tu, 7], 
 TableHeadings -> Map[h, {r, r}, {2}], TableAlignments -> Center]

enter image description here

$\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.