0
$\begingroup$

Let's say I want to build a function of two variables from some expression I know only in some points. My syntax is for example like this:

A = {};
Monitor[For[e = -10, e <= 10, e += 0.1,
  For[w = -10, w <= 10, w += 0.1,
   A = Append[
     A, {w, e, 
      1/\[Pi] Im[(((1 + w)/(1 - w + w^2 + w^3) + 
            I w/(10 + 3 w^2 + 5 w^3)) (w + I/10000) + e + 
         Tanh[e])/((e + Tanh[e])^2 + (1/(
          1 + e^2 + 
           w^2)) - ((1 + w)/(1 - w + w^2 + w^3) + 
            I w/(10 + 3 w^2 + 5 w^3))^2 (w + I/10000)^2)]}]]], e]
A = Interpolation[A];

(you can copy it into Mathematica and run it)

It only creates an empty list and do a push_back when the element is computed. However, it drives me crazy that the higher the "e" is (I added the Monitor to see what the current value of e is), the slower the computation gets. Try to type e+=0.05 and w+=0.05 and you will see that the computation is so slow at the e = -6, you won't bother let it finish it. What is the reason? How to prevent this to happen? Is push_back in mathematica slow when array has some elements in it? But how can I do this differently? When I try nested Table[Table[ approach, I get redundant inner {} brackets that are not compatible with Interpolation[]. Thanks for the help!

$\endgroup$
2
$\begingroup$

Is this what your looking for? Using Table, it finishes in a few seconds. Flatten is used to keep the same structure as in your code.

A = Flatten[#,1]&@Table[{w, e, 
     1/\[Pi] Im[(((1 + w)/(1 - w + w^2 + w^3) + 
        I w/(10 + 3 w^2 + 5 w^3)) (w + I/10000) + e + Tanh[e])/((e + 
        Tanh[e])^2 + (1/(1 + e^2 + w^2)) - ((1 + w)/(1 - w + w^2 + w^3) + 
         I w/(10 + 3 w^2 + 5 w^3))^2 (w + I/10000)^2)]}, 
 {e, -10, 10, .1}, {w, -10, 10, .1}];

Append is known to be very slow, see for example:

You can interpolate with interp = Interpolation@A.

$\endgroup$
7
  • $\begingroup$ No, the structure is wrong, if I interpolate that A, the result is rubbish. It has to be of the form {{w1, e1, A[w1,e1]}, {w2, e2, A[w2,e2]}, ...} What you produced is of the form: {w1, e1, A[w1,e1], w2, e2, A[w2,e2], ...} do you see the problem now? $\endgroup$
    – user16320
    Mar 6 '17 at 23:39
  • $\begingroup$ Have you seen the edit? $\endgroup$
    – anderstood
    Mar 6 '17 at 23:40
  • $\begingroup$ Thank you, now it works! ^^ I'm bit frightened tho, because there are those mysterious mathematica symbols, @#&, but thanks anyway. $\endgroup$
    – user16320
    Mar 6 '17 at 23:45
  • $\begingroup$ This is the same as Flatten[Table[...],1] :) $\endgroup$
    – anderstood
    Mar 6 '17 at 23:55
  • $\begingroup$ I think I'd need some "big 'flatten' tutorial", as I don't quite get how to regroup lists and lists within lists to my need. For example, I want to modify this so the variable 'e' does not run from -10 to 10, but just from 0 to 10 and make function even: Table[{{w, e, A[w,e]}, {w,-e, A[w,e]}},...] $\endgroup$
    – user16320
    Mar 7 '17 at 0:00

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.