# 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()
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)


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 + 2*(x1^2 + 3*x2^2 + x3^2 - 2*x1*y1 + y1^2)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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 - Gamma1*y3^2 - 2*(Gamma1*x1 +     Gamma3*x3)*y1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)*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?

edit retag close merge delete

Sort by ยป oldest newest most voted

You can get much shorter expressions for the components of u_gradient by running

u_gradient.apply_map(factor)


which factorizes the components.

more

Wouldn't that be u_gradient[:].apply_map(factor) ? u_gradient.apply_map(factor) returns a None...

( 2022-06-13 10:18:19 +0200 )edit

Thanks, it is indeed u_gradient[:].apply_map(factor). It has compressed the expression. But I still see quite some expanded terms.

( 2022-06-14 10:06:07 +0200 )edit

@Emmanuel Charpentier: u_gradient.apply_map(factor) modifies in place the components of the tensor field u_gradient, hence it returns None. To see the result, one has to type

u_gradient.apply_map(factor)

( 2022-06-16 17:50:30 +0200 )edit

@eric_g : thanks ! I didn't know that.

BTW, listing and discussing methods returning their results, methods modifying their argument in place and amphoterous methods should be somewhere in the documentation (possibly in an "advanced tutorial" future section...).

( 2022-06-20 22:12:38 +0200 )edit