Ask Your Question

Revision history [back]

I somehow prefer my previous answer, since it means that you were able to avoid Symbolic Ring annoyances from the beginning. Here is a workaround in case you are not able to build a rational function from the beginning.

So, you are given a symbolic expression as follows:

sage: a,b,s = var('a b s')
sage: expr1 = (a^2*s + 2)/(s^3 + s + 3) + s

We will recursively replace the symbolic variable s by the element FF(S) of the function field FF by hand as follows:

sage: FF.<S> = FunctionField(SR)

sage: def unfold_refold(expr):
sage:     operator = expr.operator()
sage:     if operator:
sage:         operands = expr.operands()
sage:         return reduce(operator, [unfold_refold(i) for i in operands])
sage:     elif expr == var('s'):
sage:         return FF(S)
sage:     else:
sage:         return expr

sage: expr2 = unfold_refold(expr1)
sage: expr2.parent()
Rational function field in S over Symbolic Ring
sage: expr2.numerator()
S^4 + S^2 + (a^2 + 3)*S + 2
sage: expr2.denominator()
S^3 + S + 3

I somehow prefer my previous answer, since it means that you were able to avoid Symbolic Ring annoyances from the beginning. Here is a workaround in case you are not able to build a rational function from the beginning.

So, you are given a symbolic expression as follows:

sage: a,b,s = var('a b s')
sage: expr1 = (a^2*s + 2)/(s^3 + s + 3) + s

We will recursively replace the symbolic variable s by the element FF(S) of the function field FF by hand as follows:

sage: FF.<S> = FunctionField(SR)

sage: def unfold_refold(expr):
sage:     operator = expr.operator()
sage:     if operator:
sage:         operands = expr.operands()
sage:         return reduce(operator, [unfold_refold(i) for i in operands])
sage:     elif expr == var('s'):
sage:         return FF(S)
sage:     else:
sage:         return expr

sage: expr2 = unfold_refold(expr1)
unfold_refold(expr1) ; expr2 
(S^4 + S^2 + (a^2 + 3)*S + 2)/(S^3 + S + 3)
sage: expr2.parent()
Rational function field in S over Symbolic Ring
sage: expr2.numerator()
S^4 + S^2 + (a^2 + 3)*S + 2
sage: expr2.denominator()
S^3 + S + 3