Ask Your Question

Revision history [back]

How to reduce the number of terms in an expression by collecting terms / factoring

Hello there, I would like some help trying to simplify the result I get by taking the gradient of a real-valued vector field in 3D Euclidean space. The vector field $\mathbf{u}$ is defined by:

$\mathbf{u}=\displaystyle\frac{\mathbf{\Gamma}\times\mathbf{r}}{\mathbf{r}\cdot\mathbf{r}}$

where

$\mathbf{\Gamma} = \mathbf{\Gamma}(\mathbf{y})$

and

$\mathbf{r} =\mathbf{r}(\mathbf{x},\mathbf{y})= \mathbf{x} - \mathbf{y}$

The gradient I am interested in is:

$\nabla\mathbf{u}=\displaystyle\frac{\partial u_i}{\partial x_j}$

The code I wrote for computing the gradient is:

E.<x1, x2, x3> = EuclideanSpace()
grad = E.metric().connection()
y1, y2, y3, Gamma1, Gamma2, Gamma3 = var('y1 y2 y3 Gamma1 Gamma2 Gamma3')

r = E.vector_field([x1 - y1, x2 - y2, x3 - y3])
Gamma = E.vector_field([Gamma1, Gamma2, Gamma3])

u = Gamma.cross(r) / r.dot(r)
u_gradient = grad(u)

The result I get for u_gradient is quite unwieldy:

u_gradient[:]

[                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               2*(Gamma3*x2 - Gamma2*x3 - Gamma3*y2 + Gamma2*y3)*(x1 - y1)/(x1^2 + x2^2 + x3^2 - 2*x1*y1 + y1^2 - 2*x2*y2 + y2^2 - 2*x3*y3 + y3^2)^2 -(Gamma3*x1^2 - Gamma3*x2^2 + 2*Gamma2*x2*x3 + Gamma3*x3^2 - 2*Gamma3*x1*y1 + Gamma3*y1^2 -     Gamma3*y2^2 + Gamma3*y3^2 + 2*(Gamma3*x2 - Gamma2*x3)*y2 - 2*(Gamma2*x2 + Gamma3*x3 - Gamma2*y2)*y3)/(x1^4 + 2*x1^2*x2^2 + x2^4 + x3^4 - 4*x1*y1^3 + y1^4 - 4*x2*y2^3 + y2^4 - 4*x3*y3^3 + y3^4 + 2*(x1^2 + x2^2)*x3^2 + 2*(3*x1^2 + x2^2     + x3^2)*y1^2 + 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3)/(x1^4 + 2*x1^2*x2^2 + x2^4 + x3^4 -     4*x1*y1^3 + y1^4 - 4*x2*y2^3 + y2^4 - 4*x3*y3^3 + y3^4 + 2*(x1^2 + x2^2)*x3^2 + 2*(3*x1^2 + x2^2 + x3^2)*y1^2 + 2*(x1^2 + 3*x2^2 + x3^2 - 2*x1*y1 + y1^2)*y2^2 + 2*(x1^2 + x2^2 + 3*x3^2 - 2*x1*y1 + y1^2 - 2*x2*y2 + y2^2)*y3^2 - 4*(x1^3     + x1*x2^2 + x1*x3^2)*y1 - 4*(x1^2*x2 + x2^3 + x2*x3^2 - 2*x1*x2*y1 + x2*y1^2)*y2 - 4*(x3^3 - 2*x1*x3*y1 + x3*y1^2 - 2*x2*x3*y2 + x3*y2^2 + (x1^2 + x2^2)*x3)*y3)                                                                                -2*(Gamma3*x1*x2 - Gamma1*x2*x3 - Gamma3*x2*y1 - (Gamma3*x1 - Gamma1*x3 - Gamma3*y1)*y2 + (Gamma1*x2 - Gamma1*y2)*y3)/(x1^4 + 2*x1^2*x2^2 + x2^4 + x3^4 - 4*x1*y1^3 + y1^4 - 4*x2*y2^3 + y2^4 - 4*x3*y3^3 + y3^4 + 2*(x1^2 + x2^2)*x3^2 +     2*(3*x1^2 + x2^2 + x3^2)*y1^2 + 2*(x1^2 + 3*x2^2 + x3^2 - 2*x1*y1 + y1^2)*y2^2 + 2*(x1^2 + x2^2 + 3*x3^2 - 2*x1*y1 + y1^2 - 2*x2*y2 + y2^2)*y3^2 - 4*(x1^3 + x1*x2^2 + x1*x3^2)*y1 - 4*(x1^2*x2 + x2^3 + x2*x3^2 - 2*x1*x2*y1 +     x2*y1^2)*y2 - 4*(x3^3 - 2*x1*x3*y1 + x3*y1^2 - 2*x2*x3*y2 + x3*y2^2 + (x1^2 + x2^2)*x3)*y3) -(Gamma1*x1^2 + Gamma1*x2^2 + 2*Gamma3*x1*x3 - Gamma1*x3^2 + Gamma1*y1^2 - 2*Gamma1*x2*y2 + Gamma1*y2^2 -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y1^2 - 2*x2*x3*y2 + x3*y2^2 + (x1^2 + x2^2)*x3)*y3)  (Gamma1*x1^2 + 2*Gamma2*x1*x2 - Gamma1*x2^2 + Gamma1*x3^2 + Gamma1*y1^2 - Gamma1*y2^2 - 2*Gamma1*x3*y3 + Gamma1*y3^2 - 2*(Gamma1*x1 + Gamma2*x2)*y1 - 2*(Gamma2*x1 - Gamma1*x2 - Gamma2*y1)*y2)/(x1^4 + 2*x1^2*x2^2 + x2^4 + x3^4 - 4*x1*y1^3 + y1^4 - 4*x2*y2^3 + y2^4 - 4*x3*y3^3 + y3^4 + 2*(x1^2 + x2^2)*x3^2 + 2*(3*x1^2 + x2^2 + x3^2)*y1^2 + 2*(x1^2 + 3*x2^2 + x3^2 - 2*x1*y1 + y1^2)*y2^2 + 2*(x1^2 + x2^2 + 3*x3^2 - 2*x1*y1 + y1^2 - 2*x2*y2 + y2^2)*y3^2 - 4*(x1^3 + x1*x2^2 + x1*x3^2)*y1 - 4*(x1^2*x2 + x2^3 + x2*x3^2 - 2*x1*x2*y1 + x2*y1^2)*y2 - 4*(x3^3 - 2*x1*x3*y1 + x3*y1^2 - 2*x2*x3*y2 + x3*y2^2 + (x1^2 + x2^2)*x3)*y3)                                                                               -2*(Gamma2*x3*y1 - Gamma1*x3*y2 - (Gamma2*x1 - Gamma1*x2)*x3 + (Gamma2*x1 - Gamma1*x2 - Gamma2*y1 + Gamma1*y2)*y3)/(x1^4 + 2*x1^2*x2^2 + x2^4 + x3^4 - 4*x1*y1^3 + y1^4 - 4*x2*y2^3 + y2^4 - 4*x3*y3^3 + y3^4 + 2*(x1^2 + x2^2)*x3^2 + 2*(3*x1^2 + x2^2 + x3^2)*y1^2 + 2*(x1^2 + 3*x2^2 + x3^2 - 2*x1*y1 + y1^2)*y2^2 + 2*(x1^2 + x2^2 + 3*x3^2 - 2*x1*y1 + y1^2 - 2*x2*y2 + y2^2)*y3^2 - 4*(x1^3 + x1*x2^2 + x1*x3^2)*y1 - 4*(x1^2*x2 + x2^3 + x2*x3^2 - 2*x1*x2*y1 + x2*y1^2)*y2 - 4*(x3^3 - 2*x1*x3*y1 + x3*y1^2 - 2*x2*x3*y2 + x3*y2^2 + (x1^2 + x2^2)*x3)*y3)]

Focusing on the first component:

u_gradient[1, 1]

2*(Gamma3*x2 - Gamma2*x3 - Gamma3*y2 + Gamma2*y3)*(x1 - y1)/(x1^2 + x2^2 + x3^2 - 2*x1*y1 + y1^2 - 2*x2*y2 + y2^2 - 2*x3*y3 + y3^2)^2

I would rather see it expressed as:

u_gradient_simplified[1, 1]

2*(Gamma3*(x2 - y2) - Gamma2*(x3 - y3))*(x1 - y1)/((x1 - y1)^2 + (x2 - y2)^2 + (x3 - y3)^2)^2

I tried using simplify but it did not change anything, unfortunately. Are there any clever tricks I could apply to rewrite the expression into something shorter?