3
$\begingroup$

I have a two-part question regarding NumberForm

First question

In my attempt at answering this i stumbled upon a strange behavior by NumberForm when the final result is exported.

Lets say I have a simple Table output as shown below which uses the command NumberForm

t1 = Table[{i, NumberForm[Range[i], {2, 2}]}, {i, 1, 2}]

Now if I export this it works fine

   Export["try.dat", t1];
   FilePrint["try.dat"]
  (* 1  {1.00} *)
  (* 2  {1.00, 2.00}*)

But if i slightly tweak the Table like this

 t2 = Table[{i, NumberForm[Range[i + #], {2, 2}] & /@ Range[2]}, {i, 1, 2}]

Now on export NumberForm remains unevaluated

  Export["try.dat", t2];
  FilePrint["try.dat"]

(* 1    {NumberForm[{1, 2}, {2, 2}], NumberForm[{1, 2, 3}, {2, 2}]} *)
(* 2    {NumberForm[{1, 2, 3}, {2, 2}], NumberForm[{1, 2, 3, 4}, {2, 2}]} *)

So why is this happening?

Second question

Also while searching I found this where the OP asked and answered his own question regarding NumberForm but when I run his/her solution it's not giving the expected answer in my machine (Windows 8 with mma 9). Below is the code which the OP gave.

time = Table[i, {i, 0, 0.4, 0.01}];
time2 = Table[NumberForm[i, 6], {i, 0, 0.4, 0.01}];
op = Table[time[[i]]^2 + time[[i]], {i, 1, Length@time}];
Export["data.dat", Transpose@{time2, op}, "Table"];
FilePrint["data.dat"]; 

Can someone confirm what they get on running the above piece matches with whats given in that thread. I'm getting some numbers which are not formatted.

$\endgroup$
  • $\begingroup$ I don't think your question has anything to do with Export. if t2 evaluated correctly, the export would have been fine. And t2 does evaluate correctly on my system, which is V10 on OS X. $\endgroup$ – m_goldberg Sep 20 '14 at 16:23
  • 1
    $\begingroup$ but isn't t2 giving the expected result. i see no problem in the output of t2 $\endgroup$ – Hubble07 Sep 20 '14 at 16:29
  • $\begingroup$ Re your second question, yes, some numbers on the second column are not formatted as in the linked answer (version 9.0.1.0 Windows 8 6-bit). With a small change (Export["data.dat", Transpose@{time2, NumberForm[#, 6] & /@ op}, "Table"]; you get the same result as in the linked answer. $\endgroup$ – kglr Sep 20 '14 at 21:51
4
$\begingroup$

I confirm the issue in Version 9.0.1.0 (Windows 8 64-bit). A workaround is wrapping NumberForm with OutputForm:

t2b = Table[{i, OutputForm[NumberForm[Range[i + #], {2, 2}]] & /@ Range[2]}, {i, 1, 2}]
Export["tryb.dat", t2b];
FilePrint["tryb.dat" ]

enter image description here

$\endgroup$
  • $\begingroup$ +1 Not sure why that works. Must be in the fine detail of the evaluation process. $\endgroup$ – Chris Degnen Sep 21 '14 at 10:18
2
$\begingroup$

You have an extra pair of list brackets in t2 which you can remove like so:-

t2 = Table[{i, Apply[Sequence,
    NumberForm[Range[i + #], {2, 2}] & /@ Range[2]]}, {i, 1, 2}]

Then it works like t1.

Export["try.dat", t2];
FilePrint["try.dat"]

(* 1    {1.00, 2.00}    {1.00, 2.00, 3.00} *)
(* 2    {1.00, 2.00, 3.00}  {1.00, 2.00, 3.00, 4.00} *)
$\endgroup$
  • $\begingroup$ This is fine but whats wrong with an extra bracket from the point of view of Export. I mean why Export fails to write the result given by t2. $\endgroup$ – Hubble07 Sep 20 '14 at 16:57
  • $\begingroup$ The export format, which appears to be "Data", seems to expect only a two-dimensional grid of data. So the nested items in the sublists are not being evaluated like the top level items during the Export process. $\endgroup$ – Chris Degnen Sep 20 '14 at 17:23
  • $\begingroup$ Chris, if you copy/paste the output of t2 (OP's version) into the second argument of Export it works ok. $\endgroup$ – kglr Sep 20 '14 at 18:05
  • $\begingroup$ @kguler - Yes, copy & paste evaluates the NumberForm statements. FullForm[t2] shows NumberForm unevaluated. So Export does not evaluate like copy & paste. $\endgroup$ – Chris Degnen Sep 20 '14 at 18:48
  • $\begingroup$ @ChrisDegnen But FullForm[t1] also shows NumberForm unevaluated and yet Export works in that case. $\endgroup$ – Hubble07 Sep 21 '14 at 8:51

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.