Generating columns in table and header for exported data file automatically



I have a code that goes like this:


const1 = 2.;
\[Kappa] = 23;

tbl = Table[{x, N[x^(1/2)], 0 x, N[x^(1/3)], 0 x, N[x^(1/4)], 0 x, 
    N[x^(1/5)], 0 x}, {x, 2, 20, 2}];


newrow[{a_, b_, c_, d_, e_, f_, g_, h_, i_}] := {a, b, c, (
   Sqrt[\[Pi]]*b* const1 Exp[-(b^2/(2 Sqrt[a]))])/(const1 \[Kappa]^5),
    d, e, (Sqrt[\[Pi]]*d *onst1 Exp[-(d^2/(2 Sqrt[a]))])/(
   const1 \[Kappa]^5), f, g, (
   Sqrt[\[Pi]]*f *const1 Exp[-(f^2/(2 Sqrt[a]))])/(const1 \[Kappa]^5),
    h, i, (Sqrt[\[Pi]]*h *const1 Exp[-(h^2/(2 Sqrt[a]))])/(
   const1 \[Kappa]^5)};
newtbl = Map[newrow, tbl];
headings = {"x", "real1", "imag1", "col1manipulated", "real2", 
   "imag2", "col2manipulated", "real3", "imag3", "col3manipulated", 
   "real4", "imag4", "col4manipulated"};
Export["file1.dat", Join[{headings}, newtbl]];

Once the table, tbl, is created, I am taking the data from each column and use it to do some arithmetic operation.

  1. Now, as you can see, I have a term $\frac{\sqrt{\pi}*b*const1*e^{\frac{-b^2}{2\sqrt{a}}}}{const1*κ^5 }$ which repeats multiple time but with only one factor changing, namely, b or d or f or h. If I am writing individual terms all the times, the code becomes messy. Is there any way to simply it? E.g. define something like $\frac{\sqrt{\pi}*i*const1*e^{\frac{-i^2}{2\sqrt{a}}}}{const1*κ^5 }$ outside and call it inside every time?
  2. In the code which gives me heading, I have to write "real1","imag1","col1manipulated" and then "real2","imag2","col2manipulated" so on. Is there any way to simplify this? Like to make "real1","imag1","col1manipulated" till "real5","imag5","col5manipulated" by some command and then append it?

Thanks in advance.

sreeraj t

Posted 2019-05-05T13:29:40.877

Reputation: 117




fun[v_] := Sqrt[\[Pi]] v const1 Exp[-b^2/(2 Sqrt[a])]/(const1 \[Kappa]^5);
newrow[{a_, b_, c_, d_, e_, f_, g_, h_, i_}] := 
  {a, b, c, fun[b], d, e, fun[d], f, g, fun[f], h, i, fun[h]};
heading=Flatten[{"x", Table[str=ToString[i];


Posted 2019-05-05T13:29:40.877

Reputation: 9 893

you are almost there. In heading, I need to put "x" one and only time, which you have not done it. If I am putting it inside {"real"<>str..."manipulated"}, it is getting repeated 5 times. What more can be done?

Also kindly check the question, I made a mistake in "headings": "real3", "imag3", "col3manipulated" was missing and I have deleted "real5", "imag5", "col5manipulated". Question has been edited. – sreeraj t – 2019-05-06T02:52:25.133

thanks you.. this is it.. – sreeraj t – 2019-05-06T05:29:38.570