0
$\begingroup$

My problem arises when I evaluate

Q[s_, n_] = 1 - Sum[Cos[-2 π (s - 1)((k - 1)/n)]/n, {k, 1, n}]
F[s_, n_] = FullSimplify[Sum[Q[s, n], {j, 1, n}], {s, n} ∈ Integers]

λ = 3;
Table[{1 - Sum[Cos[-2 π (s - 1)*((k - 1)/λ)]/λ, {k, 1, λ}]}, {s, 1, 2^λ}][[All,1]]
Table[{Q[s, λ]}, {s, 1, 2^λ}][[All, 1]]
Table[{F[s, λ]}, {s, 1, 2^λ}][[All, 1]]
Clear[λ] 

Here is a screenshot of the above code and the outputs

Image of Code

Output 29 and 30 are from the first two code boxes, while output 32-34 are from the three different generated tables.

I got this formula from a Wikipedia article on periodic sequences. Only output 32 is showing a result that agrees with the article. Why this is happening and how can I stop it from happening.

$\endgroup$
5
  • $\begingroup$ Both F and Q expect a positive integer as their second argument, but Test is undefined. The question must be missing something. $\endgroup$
    – bbgodfrey
    Jun 12, 2017 at 4:05
  • $\begingroup$ Sorry, I was using Test instead of the lambda symbol while I was struggling with the problem. They are the same though $\endgroup$
    – QC_QAOA
    Jun 12, 2017 at 4:10
  • 1
    $\begingroup$ Q[s_, n_] = FullSimplify[1 - Sum[Cos[-2*Pi*(s - 1)* ((k - 1)/n)]/n, {k, 1, n}], Element[{s, n}, Integers]] evaluates to 1 and eliminates the indeterminate expression. $\endgroup$
    – Bob Hanlon
    Jun 12, 2017 at 4:22
  • $\begingroup$ @BobHanlon But, with s == 1 and n == 3, 1 - Sum[Cos[-2 Pi*(1 - 1) (k - 1)/3]/3, {k, 3}] yields 0. Evidently, FullSimplify is returning a generic result. $\endgroup$
    – bbgodfrey
    Jun 12, 2017 at 4:37
  • 1
    $\begingroup$ Hint for improving your code: Table[Q[s, λ], {s, 1, 2^λ}] gives the same result as Table[{Q[s, λ]}, {s, 1, 2^λ}][[All, 1]], but with less computation. $\endgroup$
    – m_goldberg
    Jun 12, 2017 at 5:18

1 Answer 1

1
$\begingroup$

Your problem illustrates the difference between Set and SetDelayed. Q employs Set and so evaluates immediately.

Q[s_, n_] = 1 - Sum[Cos[-2 Pi*(s - 1) (k - 1)/n]/n, {k, n}]
(* 1 - (Cos[(π (2 + n - 2 s))/(2 n)] Csc[(π (-1 + s))/n] - 
       Cos[(-2 π + 3 n π + 2 π s - 4 n π s)/(2 n)] Csc[(π (-1 + s))/n])/(2 n) *)

The Csc[(π (-1 + s))/n] factor gives rise to the error messages when (-1 + s))/n is an integer. But, with SetDelayed,

R[s_, n_] := 1 - Sum[Cos[-2 Pi*(s - 1) (k - 1)/n]/n, {k, n}]

is evaluated only when called.

λ = 3;
Table[{R[s, λ]}, {s, 1, 2^λ}][[All, 1]]
(* {0, 1, 1, 0, 1, 1, 0, 1} *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.