2
$\begingroup$

I encountered weird performance issue while using FourierCoefficient[]. I narrowed it down to calculating $n$-th coefficient for $\cos(nx)$.

fc[n_] := 
  AbsoluteTiming@
   Norm[FourierCoefficient[Cos[n t], t, n, 
     Assumptions -> Element[n, Integers]]];
fc1[n_] := 
  AbsoluteTiming@1/
    2 Norm[#[Cos[n t], t, n, 
       Assumptions -> 
        Element[n, Integers]] & /@ {FourierCosCoefficient, 
      FourierSinCoefficient}];
fc2[n_] := 
  AbsoluteTiming@1/(2 Pi) Abs@
    Integrate[Cos[n t] Exp[-I n t], {t, -Pi, Pi}, 
     Assumptions -> Element[n, Integers]];

#[2000] & /@ {fc, fc1, fc2}
{{31.760921, 1/2}, {0., 1/2}, {0., 1/2}}

Why is fc[] so slow? why would it be any different from fc1[]?

It also seems than the timing depends on $n$:

#[2] & /@ {fc, fc1, fc2}

{{0.023933, 1/2}, {0., 1/2}, {0., 1/2}}

And it does depend on number representation:

#[2000.0] & /@ {fc, fc1, fc2}

{{0.211505, 0.5}, {0., 0.5}, {0., 0.5}}

I have MMA 10.2, can anyone check this with the latest version please?

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.