Ask Your Question

simplify_rational gives different results

asked 2015-10-04 14:05:58 +0100

neomax gravatar image

I am doing basic vector algebra calculation.

var('x0 y0 z0 a b c d')
B=vector([x0, y0, z0])

However simplifying proj does not give me the expected results


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


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:


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?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2015-10-04 15:00:02 +0100

rws gravatar image

updated 2015-10-04 15:00:52 +0100

Your proj is ab.dot_product(n)/n.norm()^2*n so your tt*n.norm() is ab.dot_product(n)/n.norm()^2 * n.norm() which is different from proj.norm() because the norm() in tt*n.norm() only applies to n. You made the same mistake when you equaled (tt*n.norm()).simplify_rational() and tt*n.norm().simplify_rational(). Here in the latter the simplify_rational() only applies to n.norm().

Operator precedence in Sage closely follows the same in Python (but in C++ for example the dot would have behaved the same way).

edit flag offensive delete link more


Thank you. However I did further tests. These following two expressions give different results.


Mathematically and programmtically the expressions in the parenthesis should be the same. They are both scalar and "sage.symbolic.expression.Expression".

What's the real difference?

neomax gravatar imageneomax ( 2015-10-04 16:39:15 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2015-10-04 14:05:58 +0100

Seen: 444 times

Last updated: Oct 04 '15