2
$\begingroup$

I'm struggling in the use of Sum.

I want to define something like that:

(*Define the index that is going to be different than zero and equal 1*)
x=-Polynomialdegree;
(*Define all the polynomials equal zero except index=1 the value is going to be 1*)
P[i_]:=0;
P[x]=1;
P[-Polynomialdegree];
Sum[ z^(-1+k) P[k],{k,-Polynomialdegree,-1}]

My problem so far ist that when Polynomialdegree is not a "number", like 1, 2, 3 ... and just a variable like "r" the code is returning ZERO as a result. What am I doing wrong here?

$\endgroup$

1 Answer 1

3
$\begingroup$

When you give non-numeric limits, Sum will actually never see your P[x] = 1 definition, because it will evalute the general term P[k], see that it is 0, and continue with it. You can observe this, for example, by using Echo:

Clear[P, x];
P[i_] := Echo[0, i];
P[x] := Echo[1, x];

Sum[P[k], {k, 0, x}]
(* » k   0 *)
(* 0 *)

Clear[P, x];
P[i_] := Echo[0, i];
P[2] := Echo[1, 2];

Sum[P[k], {k, 1, 3}]
(* » 1   0 *)
(* » 2   1 *)
(* » 3   0 *)
(* 1 *)

Instead of giving multiple definitions for P, give only one and put everything in it. In your particular case, you can use, for example, KroneckerDelta:

Clear[P, x];
P[i_] := KroneckerDelta[i, -Polynomialdegree]
Sum[z^(-1 + k)  P[k], {k, -Polynomialdegree, -1}]
(* z^(-1 - Ceiling[Polynomialdegree]) *)

Refine[%, Polynomialdegree ∈ Integers]
(* z^(-1 - Polynomialdegree) *)

Alternatively, use If:

P[i_] := If[i == -Polynomialdegree, 1, 0]
Sum[z^(-1 + k)  P[k], {k, -Polynomialdegree, -1}]
(* z^(-1 - Polynomialdegree) *)

As you see, Sum in the first case actually gave a slightly weird result, which is incorrect if Polynomialdegree is not an integer. It looks like Sum behaves slightly inconsistent with KroneckerDelta when you give it non-simple limits. Compare the results of

Sum[KroneckerDelta[i, p], {i, 0, p}]
Sum[KroneckerDelta[i, p], {i, 0, p + 1}]
Sum[KroneckerDelta[i, p], {i, p, 1}]
Sum[KroneckerDelta[i, p], {i, p, -1}]
Sum[i KroneckerDelta[i, p], {i, p, -1}]
$\endgroup$
2
  • $\begingroup$ Right on the spot! Thanks so much. Would you recommend any reference for me to better understand how Sum works? Moreover, I would like to understand what this "Ceiling" means and what this "Refine" does. I would be more than happy receiving any recommendation that would navigate me through this. $\endgroup$ Commented Jun 10, 2024 at 14:09
  • 1
    $\begingroup$ I don't have any particular reference; Sum is one of those complicated "magic" functions, which do all sorts things inside. There are several questions about it on SE, but usually it is focused on some particular example. Sometimes, it also does weird stuff, see my edit. $\endgroup$ Commented Jun 10, 2024 at 14:45

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.