# Choosing the way a function is displayed

1) I have the following function z=-(D*p-p*x - D + w0)/ (p-1). Is it possible to display it as -(p/(1-p))*x + ((w0-D*(1-p)))/(1-p)? 2) Can I select a part a this function --- the factor of x, x and the last part ?

edit retag close merge delete

Sort by » oldest newest most voted

Yes. I guess you have it as a symbolic expression, like this:

sage: var('p,x,D,w0')
sage: z = -(D*p-p*x - D + w0)/(p-1)


You can consider it as an element of the ring $\mathbb{Q}(p,D,w_0)[x]$, and then get the coefficients:

sage: A = PolynomialRing(QQ, names='p,D,w0').fraction_field()
sage: B = PolynomialRing(A, names='x')
sage: f = B(z); f
(p/(p - 1))*x + (-p*D + D - w0)/(p - 1)
sage: f.coefficients()
[(-p*D + D - w0)/(p - 1), p/(p - 1)]
sage: f.monomial_coefficient(B(x))
p/(p - 1)
sage: f.monomial_coefficient(B(1))
(-p*D + D - w0)/(p - 1)


You might want to convert these back to symbolic expressions again:

sage: SR(f.monomial_coefficient(B(x)))
p/(p - 1)
sage: SR(f.monomial_coefficient(B(1)))
-(D*p - D + w0)/(p - 1)


Or avoid symbolic expressions entirely, by defining z as a polynomial directly:

sage: A.<p,D,w0> = PolynomialRing(QQ)
sage: B.<x> = PolynomialRing(A.fraction_field())
sage: z = -(D*p-p*x - D + w0)/(p-1); z
(p/(p - 1))*x + (-p*D + D - w0)/(p - 1)
sage: z.monomial_coefficient(x)
p/(p - 1)


etc.

more

Thanks to your nice answer. I wonder if working in QQ ring, could forbid me to works after on reals ?

( 2020-04-23 12:43:35 +0200 )edit

Yes, if you need irrational coefficients you should change QQ to something else. If the coefficients are numerical real or complex you could use e.g. RDF or CDF. If the coefficients are algebraic numbers then you could use e.g. QQbar or a NumberField. You could also replace an irrational by an extra variable in A and do the substitution later.

( 2020-04-23 12:49:45 +0200 )edit