Ask Your Question

Revision history [back]

simplify_rational gives different results

I am doing basic vector algebra calculation.

var('x0 y0 z0 a b c d')
B=vector([x0, y0, z0])
n=vector([a,b,c])
A=vector([0,0,d/c])
ab=B-A
proj=ab.dot_product(n)/n.norm()^2*n

However simplifying proj does not give me the expected results

proj.norm().simplify_rational()

Gives me:

sqrt((a^2*x0^2 + b^2*y0^2 + c^2*z0^2 - 2*a*d*x0 + d^2 + 2*(a*b*x0 -
b*d)*y0 + 2*(a*c*x0 + b*c*y0 - c*d)*z0)/(a^2 + b^2 + c^2))

However if I use the following addtional steps

tt=ab.dot_product(n)/n.norm()^2
(tt*n.norm()).simplify_rational()

The result is satisfactory:

(a*x0 + b*y0 + c*z0 - d)/sqrt(a^2 + b^2 + c^2)

In addition, if I ommit parenthesis the results become different again:

tt*n.norm().simplify_rational()

Results in

sqrt(a^2 + b^2 + c^2)*(a*x0 + b*y0 + c*(z0 - d/c))/(a*conjugate(a) +
b*conjugate(b) + c*conjugate(c))

And (tt*n).norm().simplify_rational() Results in

sqrt((a^2*x0^2 + b^2*y0^2 + c^2*z0^2 - 2*a*d*x0 + d^2 + 2*(a*b*x0 -
b*d)*y0 + 2*(a*c*x0 + b*c*y0 - c*d)*z0)/(a^2 + b^2 + c^2))

What are the exact difference? How do I ensure getting the desired outcome?