symbolic-mathmaple

How to make differentiation with respect to indexed variables in a sum work?


I'm trying to make a sum with derivatives of an arbitrary number of parameters. E.g. for a vector function F(x,y)=[exp(x^3 y) - 1, y^2 - 5xy] I'd like to calculate expressions like first derivative multiplied on something.

I tried

n := 2;
xvars := seq(x[i], i = 1 .. n);
yvars := seq(y[i], i = 1 .. n);
F := (x, y) -> [exp(x^3 *y) - 1, y^2 - 5*y*x];
seq(sum(diff(F(xvars)[i], xvars[j])*yvars[j], j = 1 .. n), i = 1 .. n);

and got 0,0. And it finds correctly diff(F(xvars)[1], xvars[1]). Seems like Maple is differentiating with respect to xvars[j] rather than xvars[1] and xvars[2]. Changing diff on D gives another but also incorrect answer.

How to do it in general?

I'd like to understand how to do it with many variables and nested sums.


Solution

  • You should use add, not sum, for this adding up of a finite collection of terms.

    The sum command gets Maple's usual rules for procedure calls, and arguments are evaluated "up front", ie. before sum actually receives them. The diff call sees just the terms with non-numeric i and zero results.

    In contrast the add command (like seq) has so-called special evaluation rules, which makes prevents evaluation until its index i attains its numeric values.

    restart;
    n := 2:
    xvars := seq(x[i], i = 1 .. n):
    yvars := seq(y[i], i = 1 .. n):
    F := (x, y) -> [exp(x^3 *y) - 1, y^2 - 5*y*x]:
    
    seq(add(diff(F(xvars)[i], xvars[j])*yvars[j],
            j = 1 .. n), i = 1 .. n):lprint(%);
    
      3*x[1]^2*x[2]*exp(x[1]^3*x[2])*y[1]
      +x[1]^3*exp(x[1]^3*x[2])*y[2],
      -5*x[2]*y[1]+(-5*x[1]+2*x[2])*y[2]
    

    Delaying evaluation by using single right-ticks (aka "uneval quotes" in Maple) is possible but can get very complication for deep nesting.

    restart;
    n := 2:
    xvars := seq(x[i], i = 1 .. n):
    yvars := seq(y[i], i = 1 .. n):
    F := (x, y) -> [exp(x^3 *y) - 1, y^2 - 5*y*x]:
    
    eval(seq(sum(''diff''(F(xvars)[i], xvars[j])*yvars[j],
                 j = 1 .. n), i = 1 .. 2)):lprint(%);
    
      3*x[1]^2*x[2]*exp(x[1]^3*x[2])*y[1]
      +x[1]^3*exp(x[1]^3*x[2])*y[2],
      -5*x[2]*y[1]+(-5*x[1]+2*x[2])*y[2]