• 'add' and odd 'sum'

    From Peter Luschny@21:1/5 to All on Thu Oct 26 02:57:25 2017
    b := proc(n)
    if n = 0 then return 0 fi;
    if n = 1 then return 1 fi;
    -n*Zeta(1-n) end:

    Ba := n -> add(binomial(n,k)*b(k)*x^(n-k),k=0..n):
    Bs := n -> sum(binomial(n,k)*b(k)*x^(n-k),k=0..n):

    # Can I expect Ba(n) and Bs(n) give the same values?
    # If yes then this output should be zero:

    for n from 0 to 6 do
    print(Ba(n) - Bs(n));
    #print(Ba(n));
    #print(Bs(n));
    od;

    -1
    -x+1/2
    -x^2+x
    -x^3+(3/2)*x^2
    -x^4+2*x^3
    -x^5+(5/2)*x^4
    -x^6+3*x^5

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From acer@21:1/5 to Peter Luschny on Thu Oct 26 11:27:13 2017
    On Thursday, October 26, 2017 at 5:57:26 AM UTC-4, Peter Luschny wrote:
    b := proc(n)
    if n = 0 then return 0 fi;
    if n = 1 then return 1 fi;
    -n*Zeta(1-n) end:

    Ba := n -> add(binomial(n,k)*b(k)*x^(n-k),k=0..n):
    Bs := n -> sum(binomial(n,k)*b(k)*x^(n-k),k=0..n):

    # Can I expect Ba(n) and Bs(n) give the same values?
    # If yes then this output should be zero:

    for n from 0 to 6 do
    print(Ba(n) - Bs(n));
    #print(Ba(n));
    #print(Bs(n));
    od;

    -1
    -x+1/2
    -x^2+x
    -x^3+(3/2)*x^2
    -x^4+2*x^3
    -x^5+(5/2)*x^4
    -x^6+3*x^5


    This is a common user mistake.

    The command `sum` does not have the special-evaluation-rules that `add` has.

    So the arguments that get passed to `sum` within `Bs` are actually (where `n` has some value, being a parameter of `Bs`),

    binomial(n,k)*b(k)*x^(n-k);

    -binomial(n,k)*k*Zeta(1-k)*x^(n-k)

    That is because the call to b(k) has evaluated prematurely.

    Here are two ways to do it better:

    1) Make procedure `b` return unevaluated if the input argument is not numeric.

    restart;
    b := proc(n)
    if not type(n,numeric) then
    return 'procname'(args);
    end if;
    if n = 0 then return 0 fi;
    if n = 1 then return 1 fi;
    -n*Zeta(1-n);
    end proc:

    Ba := n -> add(binomial(n,k)*b(k)*x^(n-k),k=0..n):
    Bs := n -> sum(binomial(n,k)*b(k)*x^(n-k),k=0..n):

    for n from 0 to 6 do
    print(Ba(n) - Bs(n));
    od;

    0
    0
    0
    0
    0
    0
    0

    Notice that in the above revision, this returns unevaluated (where `k` does not yet have a numeric value).

    b(k);

    b(k)


    Another way to handle it is to guard against premature evaluation of the call b(n) by using unevaluation quotes.

    restart;
    b := proc(n)
    if n = 0 then return 0 fi;
    if n = 1 then return 1 fi;
    -n*Zeta(1-n) end:

    Ba := n -> add(binomial(n,k)*b(k)*x^(n-k),k=0..n):
    Bs := n -> sum(binomial(n,k)*'b'(k)*x^(n-k),k=0..n):

    for n from 0 to 6 do
    print(Ba(n) - Bs(n));
    od;

    0
    0
    0
    0
    0
    0
    0


    Using unevalution quotes in this way can be awkward when there are nested calls to `sum`, say, in which case one may require just the right number of pairs of unevaluation (single right) quotes.

    You might choose to read the Help page ?spec_eval_rules

    https://www.maplesoft.com/support/help/Maple/view.aspx?path=spec_eval_rules

    Also, there is discussion of this difference between `add` and `sum` in the last Examples on the Help page ?sum,details

    https://www.maplesoft.com/support/help/Maple/view.aspx?path=sum%2fdetails

    The Help also states the `add` is preferable over `sum` for adding up a finite number of terms (without the concept of symbolic reformulation of the sum).

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)