I am interested in pattern matching with two variables and not just one, and more specifically implementing derivatives on 4-vectors. In the case of interest the 4-vectors have two indices; one is in Euclidean spacetime and the other is just such that it counts sites.
So, for the raising and lowering of the spacetime indices we have: $$ \begin{equation} \begin{aligned} x^{\mu} &= \delta^{\mu \nu} x_{\nu} \, , \\ x_{\mu} &= \delta_{\mu \nu} x^{\nu} \end{aligned} \end{equation} $$ where in the above $\delta$ denotes the Kronecker delta. Greek indices are Euclidean spacetime indices, and latin indices correspond to site-counting.
In practice, the derivatives in which I am interested obey the following rules $$ \begin{equation} \begin{aligned} \partial^{\mu}_k x^{\nu}_1 &= \delta^{\mu \nu} \delta_{k ~ 1} \, , \\ \partial^{\mu}_1 x^{\nu}_1 &= \delta^{\mu \nu} \delta_{1 ~ 1} = \delta^{\mu \nu} \, , \\ \partial^{\mu}_2 x^{\nu}_1 &= \delta^{\mu \nu} \delta_{2 ~ 1} = 0 \, , \\ \partial_{k,\mu} x^2_1 &= \partial_{k,\mu}(x^{\nu}_1 x_{1,\nu}) = 2 x_{1,\mu} \delta_{k ~ 1} \, . \end{aligned} \end{equation} $$ Site counting indices are never raised, they are always downstairs as they only serve for counting.
There is a wonderful approach here on how to implement derivatives on four-vectors, when the pattern matching is only in one variable. A sample code is given below
First we implement some rules:
(*---addtn---*)
a : pD[_Plus, ___] := Thread[Unevaluated[a], Plus, 1]
(*---mltplctn---*)
pD[a_Times, x___] :=
Plus @@ (MapAt[pD[#, x] &, a, #] & /@ Range[Length[a]])
(---prs---)
pD[a_^b_, x___] := b a^(b - 1) pD[a, x]
(---cnstnts---)
pD[a_, fvd[var_, idx_List]] := 0
(---sclr prdcts---)
pD[fvd[k1_Symbol, k2_Symbol], fvd[k_Symbol, derividx_List]] :=
Module[{i},
pD[fvd[k1, {i}], fvd[k, derividx]] fvd[k2, {i}] +
fvd[k1, {i}] pD[fvd[k2, {i}], fvd[k, derividx]] /.
fvd[a_Symbol,
j_List] (fvd[j_List, l_List] | fvd[l_List, j_List]) :> fvd[a, l]]
(---mtrc drvtv---)
pD[fvd[idx1_List, idx2_List], fvd[var_, derividx_]] := 0;
(---4-vctrs drvtvs---)
pD[fvd[notk_Symbol, idx_List], fvd[k_Symbol, derividx_List]] :=
0 /; FreeQ[notk, k]
pD[fvd[k_Symbol, idx_List], fvd[k_Symbol, derividx_List]] :=
fvd[idx, derividx]
(---dspl---)
(---4-vctrs---)
Format[fvd[k_Symbol, {i_}]] := Subscript[k, i]
(---mtrc---)
Format[fvd[{i_}, {j_}]] := Subscript[[Delta], {i, j}]
(---sclr prdct---)
Format[fvd[k1_Symbol, k2_Symbol]] := DisplayForm@(k1 k2)
And the above works fine in cases where we have only the spacetime index. Examples:
- 4-vector display $x_{\alpha}$:
for which we write
fvd[x, {α}]
- Euclidean metric display $\delta_{\alpha ~ \beta}$:
we need to run
fvd[{α}, {β}]
- $\partial_b x_a = \delta_{a ~ b}$
for which we write
pD[fvd[x, {α}], fvd[x, {β}]]
- $\partial_{\mu} x^2 = 2 x_{\mu}$
for which we run:
pD[fvd[x, x], fvd[x, {μ}]]
- Sums and differences $\partial_{\mu}(x_{\alpha} \pm x_{\beta})=\delta_{\alpha \mu} \pm \delta_{\beta \mu}$
for which we have
pD[fvd[x, {α}] + fvd[x, {β}], fvd[x, {μ}]]
pD[fvd[x, {α}] - fvd[x, {β}], fvd[x, {μ}]]
And it works fine. How can I generalize the above to include the second index?
After replacing SubscriptBox by Subscript and getting rid of the RowBox, which was suggested in the comments, the formatting is just fine; see below.
- $x_{\alpha,i}$ and $x_{\alpha,1}$
are given by:
fvd[x, {α}, {i}]
fvd[x, {α}, {1}]
Then I tried to implement the derivative rule as follows, in order to account for both indices:
pD[fvd[k_Symbol, idx1_List, idx2_List],
fvd[k_Symbol, derividx1_List, derividx2_List]] :=
fvd[idx1, derividx1] fvd[idx2, derividx2]
The above works correctly in a simple example:
- $\partial_{2,\beta} x_{1, \alpha} = \delta_{1,2} \delta_{\alpha,\beta}$
we run
pD[fvd[x, {1}, {α}], fvd[x, {2}, {β}]]
and when I perform the sum
- $\partial_{1,\beta}(x_{1,\alpha}+x_{0,\alpha})=\delta_{0,1} \delta_{\alpha, \beta} + \delta_{1,1} \delta_{\alpha, \beta}$
it works without any issues
pD[(fvd[x, {1}, {α}] + fvd[x, {0}, {α}]),
fvd[x, {1}, {β}]]
However, when I tried to compute the difference it returned an un-evaluated expression:
- $\partial_{1,\beta}(x_{1,\alpha}-x_{0,\alpha})=-\delta_{0,1} \delta_{\alpha, \beta} + \delta_{1,1} \delta_{\alpha, \beta}$
which should be computable by:
pD[(fvd[x, {1}, {α}] - fvd[x, {0}, {α}]),
fvd[x, {1}, {β}]]
causes errors.
Question: what is the proper way of implementing the derivative rules (sums, differences, products) in the case of these two parameters?
Final question: The main quantity of interest would be the following:
$$ \begin{equation} \partial_{k,\nu}(x^2_{1,0}) = 2 (\delta_{k,1}-\delta_{k,0})x_{1,0,\nu} \end{equation} $$
where $x_{1,0,\nu}=x_{1,\nu}-x_{0,\nu}$ with its inverse that would be:
$$ \begin{equation} \partial_{k,\nu}(\frac{1}{x^2_{1,0}}) = - \frac{2}{x^4_{1,0}} (\delta_{k,1}-\delta_{k,0})x_{1,0,\nu} \end{equation} $$
SubscriptBoxand it works now. I still don't get the second comment about multiplication. The $\delta$ are coming from the rules in the code for the derivative, so I don't understand what multiplication has to do with them. And there's another issue, so maybe you could have a look at the edited post. – Feb 15 '22 at 16:04pD[(k_ /; FreeQ[k, fvd[_]]) v_fvd, l__] := k pD[v, l]may be appropriate? The difference case is turned into a sum case with an added-1multiple. – eyorble Feb 18 '22 at 04:40