1
$\begingroup$

I have code of the form

someFunction[xx_?VectorQ] := 1 + anotherFunction[xx]
myfunction[xx_?VectorQ]:= Sum[D[someFunction,xx[[a]]],{a,1,4}]

After this evaluates, it shows terms like $\partial_11$ and $\partial_20$, which doesn't make any sense. (This isn't literally my code, it's just a simpler version of what I'm doing)

Why is it doing this, and how can I make it stop doing this?


Update: To make the calculation more concrete, I'll include the actual definition, which is significantly more complicated than the example

tLL[orderG_][xx_?VectorQ] := 
 Block[{gothOrderG, lowerOrderG, upperOrderG},
  gothOrderG = gothicG[orderG][xx];
  lowerOrderG = lowerG[orderG][xx];
  upperOrderG = upperG[orderG][xx];
  Table[Collect[Normal@Series[
  Sum[Sum[
    D[gothOrderG[[a, b]], xx[[l]]]*D[gothOrderG[[l, m]], xx[[m]]]
     - 
     D[gothOrderG[[a, l]], xx[[l]]]*
      D[gothOrderG[[b, m]], xx[[m]]]
     + 
     1/2 upperOrderG[[a, b]]*lowerOrderG[[l, m]]*
      Sum[Sum[D[gothOrderG[[l, n]], xx[[r]]]*
         D[gothOrderG[[m, r]], xx[[n]]], {r, 1, 4}], {n, 1, 4}]
     - 
     Sum[upperOrderG[[a, l]]*
       Sum[lowerOrderG[[m, n]]*D[gothOrderG[[b, n]], xx[[r]]], {n,
          1, 4}]*D[gothOrderG[[m, r]], xx[[l]]], {r, 1, 4}]
     - 
     Sum[upperOrderG[[b, l]]*
       Sum[lowerOrderG[[m, n]]*D[gothOrderG[[a, n]], xx[[r]]], {n,
          1, 4}]*D[gothOrderG[[m, r]], xx[[l]]], {r, 1, 4}]
     + 
     1/8 (2 upperOrderG[[a, l]]*upperOrderG[[b, m]] - 
        upperOrderG[[a, b]]*upperOrderG[[l, m]])
      *Sum[Sum[Sum[Sum[
          (2*lowerOrderG[[n, r]]*lowerOrderG[[s, t]] - 
             lowerOrderG[[r, s]]*lowerOrderG[[n, t]])
           * D[gothOrderG[[n, t]], xx[[l]]]*
           D[gothOrderG[[r, s]], xx[[m]]]
          , {t, 1, 4}], {s, 1, 4}], {r, 1, 4}], {n, 1, 4}]
    , {l, 1, 4}], {m, 1, 4}]
  , {G, 0, orderG}], G], {a, 1, 4}, {b, 1, 4}]
  ]

Where gothicG, upperG, and lowerG are all fairly cost-intensive matrix functions. Then when I call

tLL[0][{t, x, y, z}][[1, 1]]

it evaluates as $$\frac{1}{8} ( (\partial_1(-1)^2-6(\partial_10)^2 + 2(\partial_20)^2+\text{several other terms}$$.

$\endgroup$

1 Answer 1

3
$\begingroup$
Clear[someFunction, myfunction, q, r, s, t]

someFunction[xx_?VectorQ] := 1 + anotherFunction[xx]

someFunction must be called with an argument. And, since derivatives will be with respect to the elements of that argument, the elements must be symbols.

myfunction[xx_?VectorQ] := Sum[D[someFunction[xx], xx[[a]]], {a, 1, 4}]

f1 = myfunction[{q, r, s, t}]

enter image description here

myfunction can be defined more succinctly as

Clear[myfunction]

myfunction[xx_?VectorQ] := D[someFunction[xx], {xx}] // Total

f2 = myfunction[{q, r, s, t}]

enter image description here

Verifying equivalence of definitions

f1 == f2

(* True *)
$\endgroup$
4
  • $\begingroup$ Thanks Bob, let me try to fix it better. What I really have is more like myFunction[xx_?VectorQ]:=Block[{foo}, foo=someFunction[xx]; D[foo, Sum[D[foo, xx[[a]] ], {a,1,4}], and I have foo defined earlier in a block because it is fairly cost-intensive to calculate and I actually use it several different times. When I evaluate, I type myFunction[{t,x,y,z}], and still get the weird partials. $\endgroup$
    – RBoston
    Commented May 19, 2018 at 20:20
  • $\begingroup$ I should note that if I just type foo=someFunction[{t,x,y,z}]; D[foo,x] not inside of a block or function definition, then I get the correct results. Not sure what the difference is. $\endgroup$
    – RBoston
    Commented May 19, 2018 at 20:28
  • 1
    $\begingroup$ It is a scoping issue. It is generally better to use explicit arguments in the definition to avoid potential scoping conflicts when calling the function. $\endgroup$
    – Bob Hanlon
    Commented May 19, 2018 at 22:06
  • $\begingroup$ I found the error! And it was a scope issue. I had t as a dummy index inside one of the sums, and also as a coordinate name. $\endgroup$
    – RBoston
    Commented May 20, 2018 at 1:20

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.