Skip to main content
deleted 7 characters in body
Source Link
John Taylor
  • 6k
  • 2
  • 14
  • 35

Consider the following code:

DistrToy[thh_, EM_] = 
  Exp[-((EM*(1 - 0.5 Cos[thh]^4))/125)]*Cos[thh]^20; 
TableDataTemp1 = 
   Table[{thh, Eh, DistrToy[thh, Eh]}, {thh, Subdivide[0, Pi, 314]}, {Eh, 125., 
     3000., 5.}];
TableData := Flatten[TableDataTemp1, {2, 1}]
DistrToyInterpolated = 
 Interpolation[TableData, InterpolationOrder -> 1]
Timing[NIntegrate[DistrToy[thh, Eh], {thh, 0, Pi}, {Eh, 125, 3000}]]
Timing[NIntegrate[
  DistrToyInterpolated[thh, Eh], {thh, 0, Pi}, {Eh, 125, 3000}]]

Here I define a function DistrToy[thh,Eh], make a double table with rows {{thh,Eh, DistrToy[thh,Eh]}} and then make an interpolation. In the end I compare the time which is needed to integrate the initial function and the interpolation over the domain of the interpolation.

I found that the times of the integrations are different: the interpolated function is integrated 40 times longer than the initial function (0.078 s vs 3.125 s)! This makes all related computations extremely slow.

How to fix this issue?

Consider the following code:

DistrToy[thh_, EM_] = 
  Exp[-((EM*(1 - 0.5 Cos[thh]^4))/125)]*Cos[thh]^20; 
TableDataTemp1 = 
   Table[{thh, Eh, DistrToy[thh, Eh]}, {thh, Subdivide[0, Pi, 314]}, {Eh, 125., 
     3000., 5.}];
TableData := Flatten[TableDataTemp1, {2, 1}]
DistrToyInterpolated = 
 Interpolation[TableData, InterpolationOrder -> 1]
Timing[NIntegrate[DistrToy[thh, Eh], {thh, 0, Pi}, {Eh, 125, 3000}]]
Timing[NIntegrate[
  DistrToyInterpolated[thh, Eh], {thh, 0, Pi}, {Eh, 125, 3000}]]

Here I define a function DistrToy[thh,Eh], make a double table with rows {{thh,Eh, DistrToy[thh,Eh]}} and then make an interpolation. In the end I compare the time which is needed to integrate the initial function and the interpolation over the domain of the interpolation.

I found that the times of the integrations are different: the interpolated function is integrated 40 times longer than the initial function (0.078 s vs 3.125 s)! This makes all related computations extremely slow.

How to fix this issue?

Consider the following code:

DistrToy[thh_, EM_] = 
  Exp[-((EM*(1 - 0.5 Cos[thh]^4))/125)]*Cos[thh]^20; 
TableDataTemp1 = 
   Table[{thh, Eh, DistrToy[thh, Eh]}, {thh, Subdivide[0, Pi, 314]}, {Eh, 125., 
     3000., 5.}];
TableData := Flatten[TableDataTemp1, {2, 1}]
DistrToyInterpolated = 
 Interpolation[TableData, InterpolationOrder -> 1]
Timing[NIntegrate[DistrToy[thh, Eh], {thh, 0, Pi}, {Eh, 125, 3000}]]
Timing[NIntegrate[
  DistrToyInterpolated[thh, Eh], {thh, 0, Pi}, {Eh, 125, 3000}]]

Here I define a function DistrToy[thh,Eh], make a table with rows {{thh,Eh, DistrToy[thh,Eh]}} and then make an interpolation. In the end I compare the time which is needed to integrate the initial function and the interpolation over the domain of the interpolation.

I found that the times of the integrations are different: the interpolated function is integrated 40 times longer than the initial function (0.078 s vs 3.125 s)! This makes all related computations extremely slow.

How to fix this issue?

Eliminate superfluous Module, and more importantly, use Subdivide so that the range of the interpolating function included Pi.
Source Link
Carl Woll
  • 131.7k
  • 6
  • 246
  • 359

Consider the following code:

DistrToy[thh_, EM_] = 
  Exp[-((EM*(1 - 0.5 Cos[thh]^4))/125)]*Cos[thh]^20;
Module[{}, 
  TableDataTemp1 = 
   Table[{thh, Eh, DistrToy[thh, Eh]}, {thh, 0Subdivide[0, Pi, 0.01314]}, {Eh, 125., 
     3000., 5.}]];];
TableData := Flatten[TableDistributionMassSingleTemp1Flatten[TableDataTemp1, {2, 1}]
DistrToyInterpolated = 
 Interpolation[TableData, InterpolationOrder -> 1]
Timing[NIntegrate[DistrToy[thh, Eh], {thh, 0, Pi}, {Eh, 125, 3000}]]
Timing[NIntegrate[
  DistrToyInterpolated[thh, Eh], {thh, 0, Pi}, {Eh, 125, 3000}]]

Here I define a function DistrToy[thh,Eh], make a double table with rows {{thh,Eh, DistrToy[thh,Eh]}} and then make an interpolation. In the end I compare the time which is needed to integrate the initial function and the interpolation over the domain of the interpolation.

I found that the times of the integrations are different: the interpolated function is integrated 40 times longer than the initial function (0.078 s vs 3.125 s)! This makes all related computations extremely slow.

How to fix this issue?

Consider the following code:

DistrToy[thh_, EM_] = 
  Exp[-((EM*(1 - 0.5 Cos[thh]^4))/125)]*Cos[thh]^20;
Module[{}, 
  TableDataTemp1 = 
   Table[{thh, Eh, DistrToy[thh, Eh]}, {thh, 0, Pi, 0.01}, {Eh, 125., 
     3000., 5.}]];
TableData := Flatten[TableDistributionMassSingleTemp1, {2, 1}]
DistrToyInterpolated = 
 Interpolation[TableData, InterpolationOrder -> 1]
Timing[NIntegrate[DistrToy[thh, Eh], {thh, 0, Pi}, {Eh, 125, 3000}]]
Timing[NIntegrate[
  DistrToyInterpolated[thh, Eh], {thh, 0, Pi}, {Eh, 125, 3000}]]

Here I define a function DistrToy[thh,Eh], make a double table with rows {{thh,Eh, DistrToy[thh,Eh]}} and then make an interpolation. In the end I compare the time which is needed to integrate the initial function and the interpolation over the domain of the interpolation.

I found that the times of the integrations are different: the interpolated function is integrated 40 times longer than the initial function (0.078 s vs 3.125 s)! This makes all related computations extremely slow.

How to fix this issue?

Consider the following code:

DistrToy[thh_, EM_] = 
  Exp[-((EM*(1 - 0.5 Cos[thh]^4))/125)]*Cos[thh]^20; 
TableDataTemp1 = 
   Table[{thh, Eh, DistrToy[thh, Eh]}, {thh, Subdivide[0, Pi, 314]}, {Eh, 125., 
     3000., 5.}];
TableData := Flatten[TableDataTemp1, {2, 1}]
DistrToyInterpolated = 
 Interpolation[TableData, InterpolationOrder -> 1]
Timing[NIntegrate[DistrToy[thh, Eh], {thh, 0, Pi}, {Eh, 125, 3000}]]
Timing[NIntegrate[
  DistrToyInterpolated[thh, Eh], {thh, 0, Pi}, {Eh, 125, 3000}]]

Here I define a function DistrToy[thh,Eh], make a double table with rows {{thh,Eh, DistrToy[thh,Eh]}} and then make an interpolation. In the end I compare the time which is needed to integrate the initial function and the interpolation over the domain of the interpolation.

I found that the times of the integrations are different: the interpolated function is integrated 40 times longer than the initial function (0.078 s vs 3.125 s)! This makes all related computations extremely slow.

How to fix this issue?

Source Link
John Taylor
  • 6k
  • 2
  • 14
  • 35

Interpolation works slowly

Consider the following code:

DistrToy[thh_, EM_] = 
  Exp[-((EM*(1 - 0.5 Cos[thh]^4))/125)]*Cos[thh]^20;
Module[{}, 
  TableDataTemp1 = 
   Table[{thh, Eh, DistrToy[thh, Eh]}, {thh, 0, Pi, 0.01}, {Eh, 125., 
     3000., 5.}]];
TableData := Flatten[TableDistributionMassSingleTemp1, {2, 1}]
DistrToyInterpolated = 
 Interpolation[TableData, InterpolationOrder -> 1]
Timing[NIntegrate[DistrToy[thh, Eh], {thh, 0, Pi}, {Eh, 125, 3000}]]
Timing[NIntegrate[
  DistrToyInterpolated[thh, Eh], {thh, 0, Pi}, {Eh, 125, 3000}]]

Here I define a function DistrToy[thh,Eh], make a double table with rows {{thh,Eh, DistrToy[thh,Eh]}} and then make an interpolation. In the end I compare the time which is needed to integrate the initial function and the interpolation over the domain of the interpolation.

I found that the times of the integrations are different: the interpolated function is integrated 40 times longer than the initial function (0.078 s vs 3.125 s)! This makes all related computations extremely slow.

How to fix this issue?