# Why doesn't Mathematica make an obvious simplification?

I am doing some calculation with summation and the Kronecker symbol. Here are my steps :

$Assumptions = k1 ∈ Reals && k2 ∈ Reals && k3 ∈ Reals && p1 ∈ Reals && p2 ∈ Reals && p3 ∈ Reals k = {k1, k2, k3}; p = {p1, p2, p3}; d[i_, j_] := KroneckerDelta[i, j] proj[i_, j_, k1_, k2_, k3_] := d[i, j] - (d[i, 1]*k1 + d[i, 2]*k2 + d[i, 3]*k3)* (d[j, 1]*k1 + d[j, 2]*k2 + d[j, 3]*k3)/ (k1^2 + k2^2 + k3^2) test1 = proj[i, j, k1, k2, k3]*proj[i, j, p1, p2, p3]; test2 = Sum[Sum[test1, {i, 1, 3}], {j, 1, 3}] test2 // Expand  To explain the steps: 1) I define$\vec{k}$and$\vec{p}$with real components. 2) I define a projector$P_{ij} \left( \vec{k} \right) = \delta_{ij} - \frac{k_i k_j}{k^2}$. 3) I compute a summation on the repeated subscript. After the last step, I have a relatively big expression, the product of k and p components. It looks like $$3-\frac{a}{a+b+c} - \frac{b}{a+b+c} - \frac{c}{a+b+c} +...-...$$ The a, b and c stands for k1, k2 and k3 (or p1, 2, 3). Now the question: why doesn't Mathematica make the simplification because, as anyone can see, the preceding expression can be simplified to$2 +...-...$Is the problem linked to the Expand operation? How can I make the simplification I want?. I thought of using /. to do it, but that doesn't work either. I hope someone will understand my question! • You can try using Simplify and related, but what you think it's a simpler expression will not necessarily match Mathematica's. – b.gates.you.know.what Feb 15 '13 at 10:23 • +1 just for formatting your code:) Simplify@test2 works nicely and returns (2 k2 k3 p2 p3 + 2 k1 p1 (k2 p2 + k3 p3) + k1^2 (2 p1^2 + p2^2 + p3^2) + k2^2 (p1^2 + 2 p2^2 + p3^2) + k3^2 (p1^2 + p2^2 + 2 p3^2))/((k1^2 + k2^2 + k3^2) (p1^2 + p2^2 + p3^2)) – Ajasja Feb 15 '13 at 10:34 ## 4 Answers You have to explicitly tell Mathematica to simplify expressions. You can do this using Simplify or FullSimplify Simplify@test2 (2 k2 k3 p2 p3 + 2 k1 p1 (k2 p2 + k3 p3) + k1^2 (2 p1^2 + p2^2 + p3^2) + k2^2 (p1^2 + 2 p2^2 + p3^2) + k3^2 (p1^2 + p2^2 + 2 p3^2))/((k1^2 + k2^2 + k3^2) (p1^2 + p2^2 + p3^2))  • Thank you for your answers. The problem when I use Simplify is that the form of the result is not "usable". I mean in the sense I want to use it. Because the calculation must lead to$1 - (\vec{k}.\vec{p})^2/(k^2*p^2)$. And with simplify, Mathematica doesn't do the factorization I want. I think I have to find an other way to do it, I may already have an idea. I thought I forget an assumption to enable Mathematica to do automatically this simplification. But it won't do what I didn't ask ! – Lalylulelo Feb 15 '13 at 10:52 I would use symbolic tensors instead of explicit tensors. First, let's reformulate your question using symbolic tensors: proj[k_] /; TensorRank[k] == 1 := With[{d = First @ TensorDimensions[k]}, Inactive[IdentityMatrix][d] - TensorProduct[k, k]/k.k ]$Assumptions = (k|p) ∈ Vectors;
test = TensorContract[TensorProduct[proj[k], proj[p]], {{1, 3}, {2, 4}}];
test //TeXForm


$\operatorname{TensorContract}\left[\left(\operatorname{IdentityMatrix}-\frac{k\otimes k}{k.k}\right)\otimes \left(\operatorname{IdentityMatrix}-\frac{p\otimes p}{p.p}\right),\left( \begin{array}{cc} 1 & 3 \\ 2 & 4 \\ \end{array} \right)\right]$

Note that TeXForm doesn't yet support Inactive, which is why it doesn't appear above. Also, I've overriden the default TeXForm formatting of \[TensorProduct] so that it looks like \otimes.

Mathematica doesn't (yet) have a symbolic identity tensor, so I use Inactive[IdentityMatrix] as a substitute. This means that TensorReduce can't really be used to simplify the output since it doesn't have support for my identity tensor substitute. I've written a package that helps augment TensorReduce to work with these identity tensors, which can be found here:

PacletInstall[
"TensorSimplify",
"Site" -> "http://raw.githubusercontent.com/carlwoll/TensorSimplify/master"
]


Once installed, you can load the package with:

<<TensorSimplify


Let's see this package in action on the above expression. First we need to use TensorReduce (TensorExpand would also work):

r1 = TensorReduce[test];
r1 //TeXForm


$-\frac{\operatorname{TensorContract}\left[k\otimes k\otimes \operatorname{IdentityMatrix},\left( \begin{array}{cc} 1 & 3 \\ 2 & 4 \\ \end{array} \right)\right]}{k.k}-\frac{\operatorname{TensorContract}\left[p\otimes p\otimes \operatorname{IdentityMatrix},\left( \begin{array}{cc} 1 & 3 \\ 2 & 4 \\ \end{array} \right)\right]}{p.p}+\operatorname{TensorContract}\left[\operatorname{IdentityMatrix}\otimes \operatorname{IdentityMatrix},\left( \begin{array}{cc} 1 & 3 \\ 2 & 4 \\ \end{array} \right)\right]+\frac{\operatorname{TensorContract}\left[k\otimes p,\left( \begin{array}{cc} 1 & 2 \\ \end{array} \right)\right]^2}{k.k p.p}$

Using TensorReduce causes the symbolic identity tensor to be contracted. The package function IdentityReduce can then be used to eliminate these contracted identity tensors:

r2 = IdentityReduce[r1];
r2 //TeXForm


$\frac{\operatorname{TensorContract}\left[k\otimes p,\left( \begin{array}{cc} 1 & 2 \\ \end{array} \right)\right]^2}{k.k p.p}-\frac{\operatorname{TensorContract}\left[k\otimes k,\left( \begin{array}{cc} 1 & 2 \\ \end{array} \right)\right]}{k.k}-\frac{\operatorname{TensorContract}\left[p\otimes p,\left( \begin{array}{cc} 1 & 2 \\ \end{array} \right)\right]}{p.p}+3$

Finally, the above expression is a mix of equivalent Dot and TensorContract objects. Using a common form will enable further simplifications. The less verbose version is obtained using the Dot representation (obtained by using the package function FromTensor):

FromTensor[r2]


1 + (k.p)^2/(k.k p.p)

I believe this is the form you're looking for. The package function TensorSimplify does all of the above steps:

TensorSimplify[test]


1 + (k.p)^2/(k.k p.p)

• Impressive! It was exactly the form I was looking for. I will have to test it. I think I figured out a solution by using replacements but yours is much more elegant. Thanks! – Lalylulelo Nov 12 '17 at 9:04

Not sure precisely what you want, but maybe it's this:

x = test2 // Expand;
x1 = PolynomialReduce[
Numerator[Together[x]], {Denominator[Together[x]]}, Variables[x]];
x2 = PolynomialReduce[x1[], {k1^2 + k2^2 + k3^2}, Variables[x]];
x3 = PolynomialReduce[x2[], {p1^2 + p2^2 + p3^2}, Variables[x]];
y = x1[[1, 1]] + x2[[1, 1]]/(p1^2 + p2^2 + p3^2) +
x3[[1, 1]]/(k1^2 + k2^2 + k3^2) +
x3[]/((p1^2 + p2^2 + p3^2) (k1^2 + k2^2 + k3^2))


Here's the output in reverse order; there's an issue with copy/paste:

$\frac{2 \text{k1} \text{k2} \text{p1} \text{p2}+2 \text{k1} \text{k3} \text{p1} \text{p3}+2 \text{k2}^2 \text{p2}^2+\text{k2}^2 \text{p3}^2+2 \text{k2} \text{k3} \text{p2} \text{p3}+\text{k3}^2 \text{p2}^2+2 \text{k3}^2 \text{p3}^2}{\left(\text{k1}^2+\text{k2}^2+\text{k3}^2\right) \left(\text{p1}^2+\text{p2}^2+\text{p3}^2\right)}+\frac{-\text{k2}^2-\text{k3}^2}{\text{k1}^2+\text{k2}^2+\text{k3}^2}+\frac{-\text{p2}^2-\text{p3}^2}{\text{p1}^2+\text{p2}^2+\text{p3}^2}+2$

If you know approximately what denominators are obtained, you coud use command Collect[expression, factor]`.