Ask Your Question
1

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])
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?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

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

Comments

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

((tt*n).norm()).simplify_rational()
(tt*(n.norm())).simplify_rational()

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

Stats

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

Seen: 706 times

Last updated: Oct 04 '15