Ask Your Question

Revision history [back]

To replace an subexpression inside a sum or a product you can do

sage: t = SR.var('t')
sage: formula = sin(2*t) + sin(4*t)
sage: formula.subs({sin(2*t): t**3 + 2})
t^3 + sin(4*t) + 2

And in your expression there are values of R and t so that the result seems to be negative

sage: test.subs(R=1,t=1.)
-4.64062057506056

But it is not so bad as it seems that all substitutions are actually negative

sage: all(test.subs(R=x, t=y) < 0 for x in srange(0.1,2,0.2) for y in srange(0,4,0.2))
True

You can easily see that the sign is actually determined by the sub expression which can further be divided by R^2

sage: sub_expr = 3*R^6 - 2*(24*cos(t)^4 - 24*cos(t)^2 + 1)*R^4 + 3*R^2
sage: sub_expr2 = (sub_expr / R^2).simplify_rational()
sage: print sub_expr2
3*R^4 - 2*(24*cos(t)^4 - 24*cos(t)^2 + 1)*R^2 + 3

To simplify you can replace cos with a new variable u

sage: u = SR.var('u')
sage: sub_expr3 = sub_expr2.subs({cos(t): u})
sage: print sub_expr3
3*R^4 - 2*(24*u^4 - 24*u^2 + 1)*R^2 + 3

But as you already mentioned this is wrong

sage: bool(sub_expr3 >= 0)
False

One thing that you need to do is to specify domain of variables

sage: assume(R, 'real')
sage: assume(u, 'real')
sage: assume(R > 0)
sage: assume(-1 <= u <= 1)
sage: assumptions()
[R > 0, t is real, R is real, u is real, u >= -1, u <= 1]

But still

sage: bool(sub_expr3 >= 0)
False

But now you sub_expr3 are polynomials of degree 2 (in R^2 and u^2) and you should be able to figure out the sign.