Skip to main content
added 32 characters in body
Source Link
george2079
  • 39.1k
  • 1
  • 44
  • 111

One idea is to integrate once to get the sample points then compute the remaining integrals as sums:

 sample = Transpose@
    SortBy[First@Last@Reap[
       NIntegrate[x (c = Cos[10 x + Cos[x]]), {x, 0, 5}, 
                  EvaluationMonitor :> Sow[{x, c}]]], #[[1]] &];
 wt = ((#[[3]] - #[[1]])/2) & /@ Partition[Join[{0}, sample[[1]], {5}], 3, 1];
 wt.(sample[[2]] #) & /@ {sample[[1]], sample[[1]]^2, sample[[1]]^3}

{0.0133333, 0.133275, 0.861541}

Note the accuracy is not terribly good, NIntegrate gives:

{0.0125266, 0.131514, 0.855716}

Somethings a bit off in my quick&dirty trapezoid integration but i think this can be made to work.

roll your own

For this example there really is little benefit to NIntegrate's adaptive sampling so we might as well just use a uniform sampling:

 np = 651;(*assumed odd for simpsons rule*)
 a=5
 b=0
 wt = ( 5(a-b)/(np - 1) )/3 Join[{1}, Flatten@ConstantArray[{4, 2}, (np - 1)/2 - 1], {4, 1}] // N;
 x = b + (Range[0, np - 1] 5(a-b)/(np - 1)) // N;
 fast = Cos[10 # + Cos[#]] & /@ x // N;
 (# fast).wt & /@ {x, x^2, x^3}

{0.0125266, 0.131514, 0.855716}

One idea is to integrate once to get the sample points then compute the remaining integrals as sums:

 sample = Transpose@
    SortBy[First@Last@Reap[
       NIntegrate[x (c = Cos[10 x + Cos[x]]), {x, 0, 5}, 
                  EvaluationMonitor :> Sow[{x, c}]]], #[[1]] &];
 wt = ((#[[3]] - #[[1]])/2) & /@ Partition[Join[{0}, sample[[1]], {5}], 3, 1];
 wt.(sample[[2]] #) & /@ {sample[[1]], sample[[1]]^2, sample[[1]]^3}

{0.0133333, 0.133275, 0.861541}

Note the accuracy is not terribly good, NIntegrate gives:

{0.0125266, 0.131514, 0.855716}

Somethings a bit off in my quick&dirty trapezoid integration but i think this can be made to work.

roll your own

For this example there really is little benefit to NIntegrate's adaptive sampling so we might as well just use a uniform sampling:

 np = 651;(*assumed odd for simpsons rule*)
 wt = ( 5/(np - 1) )/3 Join[{1}, Flatten@ConstantArray[{4, 2}, (np - 1)/2 - 1], {4, 1}] // N;
 x = (Range[0, np - 1] 5/(np - 1)) // N;
 fast = Cos[10 # + Cos[#]] & /@ x // N;
 (# fast).wt & /@ {x, x^2, x^3}

{0.0125266, 0.131514, 0.855716}

One idea is to integrate once to get the sample points then compute the remaining integrals as sums:

 sample = Transpose@
    SortBy[First@Last@Reap[
       NIntegrate[x (c = Cos[10 x + Cos[x]]), {x, 0, 5}, 
                  EvaluationMonitor :> Sow[{x, c}]]], #[[1]] &];
 wt = ((#[[3]] - #[[1]])/2) & /@ Partition[Join[{0}, sample[[1]], {5}], 3, 1];
 wt.(sample[[2]] #) & /@ {sample[[1]], sample[[1]]^2, sample[[1]]^3}

{0.0133333, 0.133275, 0.861541}

Note the accuracy is not terribly good, NIntegrate gives:

{0.0125266, 0.131514, 0.855716}

Somethings a bit off in my quick&dirty trapezoid integration but i think this can be made to work.

roll your own

For this example there really is little benefit to NIntegrate's adaptive sampling so we might as well just use a uniform sampling:

 np = 651;(*assumed odd for simpsons rule*)
 a=5
 b=0
 wt = ( (a-b)/(np - 1) )/3 Join[{1}, Flatten@ConstantArray[{4, 2}, (np - 1)/2 - 1], {4, 1}] // N;
 x = b + (Range[0, np - 1] (a-b)/(np - 1)) // N;
 fast = Cos[10 # + Cos[#]] & /@ x // N;
 (# fast).wt & /@ {x, x^2, x^3}

{0.0125266, 0.131514, 0.855716}

added 52 characters in body
Source Link
george2079
  • 39.1k
  • 1
  • 44
  • 111

One idea is to integrate once to get the sample points then compute the remaining integrals as sums:

 sample = Transpose@
    SortBy[First@Last@Reap[
       NIntegrate[x (c = Cos[10 x + Cos[x]]), {x, 0, 5}, 
                  EvaluationMonitor :> Sow[{x, c}]]], #[[1]] &];
 wt = ((#[[3]] - #[[1]])/2) & /@ Partition[Join[{0}, sample[[1]], {5}], 3, 1];
 wt.(sample[[2]] #) & /@ {sample[[1]], sample[[1]]^2, sample[[1]]^3}

{0.0133333, 0.133275, 0.861541}

Note the accuracy is not terribly good, NIntegrate gives:

{0.0125266, 0.131514, 0.855716}

Somethings a bit off in my quick&dirty trapezoid integration but i think this can be made to work.

roll your own

For this example there really is little benefit to NIntegrate's adaptive sampling so we might as well just use a uniform sampling:

 np = 650;651;(*assumed odd for simpsons rule*)
 wt = ( 5/(np - 1) )/3 Join[{ 1/2},  ConstantArray[ 1Flatten@ConstantArray[{4, 2}, (np - 2]1)/2 - 1], {14, /21}] // N;
 x = ( Range[0, np - 1] 5/(np - 1)) // N;
 fast = Cos[10 # + Cos[#]] & /@ x // N;
 (# fast) .wt & /@ {x, x^2, x^3}

{0.01252390125266, 0.131539131514, 0.855964855716}

One idea is to integrate once to get the sample points then compute the remaining integrals as sums:

 sample = Transpose@
    SortBy[First@Last@Reap[
       NIntegrate[x (c = Cos[10 x + Cos[x]]), {x, 0, 5}, 
                  EvaluationMonitor :> Sow[{x, c}]]], #[[1]] &];
 wt = ((#[[3]] - #[[1]])/2) & /@ Partition[Join[{0}, sample[[1]], {5}], 3, 1];
 wt.(sample[[2]] #) & /@ {sample[[1]], sample[[1]]^2, sample[[1]]^3}

{0.0133333, 0.133275, 0.861541}

Note the accuracy is not terribly good, NIntegrate gives:

{0.0125266, 0.131514, 0.855716}

Somethings a bit off in my quick&dirty trapezoid integration but i think this can be made to work.

roll your own

For this example there really is little benefit to NIntegrate's adaptive sampling so we might as well just use a uniform sampling:

 np = 650;
 wt = 5/(np - 1) Join[{ 1/2},  ConstantArray[ 1 , np - 2], {1 /2}] // N;
 x = ( Range[0, np - 1] 5/(np - 1)) // N;
 fast = Cos[10 # + Cos[#]] & /@ x // N;
 (# fast) .wt & /@ {x, x^2, x^3}

{0.0125239, 0.131539, 0.855964}

One idea is to integrate once to get the sample points then compute the remaining integrals as sums:

 sample = Transpose@
    SortBy[First@Last@Reap[
       NIntegrate[x (c = Cos[10 x + Cos[x]]), {x, 0, 5}, 
                  EvaluationMonitor :> Sow[{x, c}]]], #[[1]] &];
 wt = ((#[[3]] - #[[1]])/2) & /@ Partition[Join[{0}, sample[[1]], {5}], 3, 1];
 wt.(sample[[2]] #) & /@ {sample[[1]], sample[[1]]^2, sample[[1]]^3}

{0.0133333, 0.133275, 0.861541}

Note the accuracy is not terribly good, NIntegrate gives:

{0.0125266, 0.131514, 0.855716}

Somethings a bit off in my quick&dirty trapezoid integration but i think this can be made to work.

roll your own

For this example there really is little benefit to NIntegrate's adaptive sampling so we might as well just use a uniform sampling:

 np = 651;(*assumed odd for simpsons rule*)
 wt = ( 5/(np - 1) )/3 Join[{1}, Flatten@ConstantArray[{4, 2}, (np - 1)/2 - 1], {4, 1}] // N;
 x = (Range[0, np - 1] 5/(np - 1)) // N;
 fast = Cos[10 # + Cos[#]] & /@ x // N;
 (# fast).wt & /@ {x, x^2, x^3}

{0.0125266, 0.131514, 0.855716}

deleted 142 characters in body
Source Link
george2079
  • 39.1k
  • 1
  • 44
  • 111

One idea is to integrate once to get the sample points then compute the remaining integrals as sums:

 sample = SortBy[ Transpose@
     First@Last@SortBy[First@Last@Reap[
        Reap[ NIntegrate[x (c = Cos[10 x + Cos[x]]), {x, 0, 5}, 
                  EvaluationMonitor :> Sow[{x, c}]] ]  ]]], #[[1]] &];
 
 wt = ((#[[3]] - #[[1]])/2) & /@ 
     Partition[Join[{0}, (#[[1]] & /@ sample ) sample[[1]], {5}] , 3 , 1 ] ;
1];
 wt . ((Transpose[sample][[2]]) (Transpose[sample][[1]]))
 wt .sample[[2]] ((Transpose[sample][[2]]#) (Transpose[sample][[1]])^2)
& wt/@ .{sample[[1]], ((Transpose[sample][[2]])sample[[1]]^2, (Transpose[sample][[1]])^3)sample[[1]]^3}

{0.0133333, 0.133275, 0.861541}

Note the accuracy is not terribly good, NIntegrate gives:

{0.0125266, 0.131514, 0.855716}

Somethings a bit off in my quick&dirty trapezoid integration but i think this can be made to work.

roll your own

For this example there really is little benefit to NIntegrate's adaptive sampling so we might as well just use a uniform sampling:

 np = 650;
 wt = 5/(np - 1) Join[{ 1/2},  ConstantArray[ 1 , np - 2], {1 /2}] // N;
 x = ( Range[0, np - 1] 5/(np - 1)) // N;
 slowfast = Cos[10 # + Cos[#]] & /@ x // N;
 (# slow fast) .wt & /@ {x, x^2, x^3}

{0.0125239, 0.131539, 0.855964}

One idea is to integrate once to get the sample points then compute the remaining integrals as sums:

 sample = SortBy[ 
     First@Last@
        Reap[ NIntegrate[x (c = Cos[10 x + Cos[x]]), {x, 0, 5}, 
            EvaluationMonitor :> Sow[{x, c}]] ]  , #[[1]] &];
 
 wt = ((#[[3]] - #[[1]])/2) & /@ 
     Partition[Join[{0}, (#[[1]] & /@ sample ) , {5}] , 3 , 1 ] ;

 wt . ((Transpose[sample][[2]]) (Transpose[sample][[1]]))
 wt . ((Transpose[sample][[2]]) (Transpose[sample][[1]])^2)
 wt . ((Transpose[sample][[2]]) (Transpose[sample][[1]])^3)

{0.0133333, 0.133275, 0.861541}

Note the accuracy is not terribly good, NIntegrate gives:

{0.0125266, 0.131514, 0.855716}

Somethings a bit off in my quick&dirty trapezoid integration but i think this can be made to work.

roll your own

For this example there really is little benefit to NIntegrate's adaptive sampling so we might as well just use a uniform sampling:

 np = 650;
 wt = 5/(np - 1) Join[{ 1/2},  ConstantArray[ 1 , np - 2], {1 /2}] // N;
 x = ( Range[0, np - 1] 5/(np - 1)) // N;
 slow = Cos[10 # + Cos[#]] & /@ x // N;
 (# slow ) .wt & /@ {x, x^2, x^3}

{0.0125239, 0.131539, 0.855964}

One idea is to integrate once to get the sample points then compute the remaining integrals as sums:

 sample = Transpose@
    SortBy[First@Last@Reap[
       NIntegrate[x (c = Cos[10 x + Cos[x]]), {x, 0, 5}, 
                  EvaluationMonitor :> Sow[{x, c}]]], #[[1]] &];
 wt = ((#[[3]] - #[[1]])/2) & /@ Partition[Join[{0}, sample[[1]], {5}], 3, 1];
 wt.(sample[[2]] #) & /@ {sample[[1]], sample[[1]]^2, sample[[1]]^3}

{0.0133333, 0.133275, 0.861541}

Note the accuracy is not terribly good, NIntegrate gives:

{0.0125266, 0.131514, 0.855716}

Somethings a bit off in my quick&dirty trapezoid integration but i think this can be made to work.

roll your own

For this example there really is little benefit to NIntegrate's adaptive sampling so we might as well just use a uniform sampling:

 np = 650;
 wt = 5/(np - 1) Join[{ 1/2},  ConstantArray[ 1 , np - 2], {1 /2}] // N;
 x = ( Range[0, np - 1] 5/(np - 1)) // N;
 fast = Cos[10 # + Cos[#]] & /@ x // N;
 (# fast) .wt & /@ {x, x^2, x^3}

{0.0125239, 0.131539, 0.855964}

added 420 characters in body
Source Link
george2079
  • 39.1k
  • 1
  • 44
  • 111
Loading
added 100 characters in body
Source Link
george2079
  • 39.1k
  • 1
  • 44
  • 111
Loading
Source Link
george2079
  • 39.1k
  • 1
  • 44
  • 111
Loading