ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 16 Nov 2018 12:53:39 +0100Conversion from symbolic expression to polynomial stuckhttps://ask.sagemath.org/question/44302/conversion-from-symbolic-expression-to-polynomial-stuck/I have a very long symbolic expression with six variables:
> (E_mu, E_xi3, ISP, T, V_mu, V_xi3, Z,
> m, mu, n_I, n_P, xi_1, xi_3)
I call the expression AN. I want to convert it in a polynomial of two variables (mu and xi_3). I tried the following command:
AP = AN.polynomial(None,ring=SR['mu,xi_3'])
This works for simple expressions but the command remains stuck for the given expression. My aim is to get the monomials of the polynomial. mu and xi_3 are random variables and I want to compute the expectation of AN (E[AN]). Thus, I could substitute the expectations in the expression.
AN is a numerator, so it is not a fraction. Moreover, I also tried simply_rational but it did not help to convert.
Is there anyway to convert easily ?
EDIT : [I put the symbolic expression in a framabin.](https://framabin.org/p/?2417942055fc9fc9#Oenpb+BI567QXYcVCzjO4zkLRC0byQaNdhi2XW/CUkY=)borostackFri, 16 Nov 2018 12:53:39 +0100https://ask.sagemath.org/question/44302/Print symbolic variables like a_1 in A[1] stylehttps://ask.sagemath.org/question/43838/print-symbolic-variables-like-a_1-in-a1-style/Because of Sagemath's problem with symbolic arrays, I have defined my vectors like this:
A =[0 for j in range(4)]
for j in range(4):
A[j]=var('a_'+str(j))
I have some symbolic variables stored in another variable. Lets say I have a subroutine that works with `A[i]` and in the end, `L` becomes something such as: `L=a_1+a_2*a_3`. When I print `L`, I want to have it printed in the original vector format. So when I type `L` or `print(L)` in a cell and press enter, my desired output is `A[1]+A[2]*A[3]` and not `a_1+a_2*a_3`. I want this type of output because I am transferring SageMath outputs to C where I employ indexed arrays. How can I achieve this? DanialBaghSat, 06 Oct 2018 05:30:25 +0200https://ask.sagemath.org/question/43838/Convert expression to univariate polynomial over symbolic ring properlyhttps://ask.sagemath.org/question/25556/convert-expression-to-univariate-polynomial-over-symbolic-ring-properly/If I create polynomial like this
R.<x> = SR[]
var('a')
f = a*x^3+1
I get such list of variables
sage: f.variables()
(x,)
But how to convert existing expression to univariate polynomial over symbolic ring?
When I do like this
var('a')
f = a*x^3+1
P.<x> = SR[]
I get following output
sage: P(f).variables()
()
sage: SR['x'](f).variables()
()
How to convert `f` to polynomial in a right way with `x` as variable.
petRUShkaMon, 19 Jan 2015 11:57:47 +0100https://ask.sagemath.org/question/25556/conversion from polynomial to symbolichttps://ask.sagemath.org/question/23818/conversion-from-polynomial-to-symbolic/I want to convert a huge polynomial from ZZ[] into SR but with the following code I get an expression that is not expanded:
sage: R.<x> = ZZ[]
sage: var('n')
n
sage: p=16*x^5 - 20*x^3 + 5*x
sage: p.subs(x=n)
(4*(4*n^2 - 5)*n^2 + 5)*n
sage: SR(p)
(4*(4*x^2 - 5)*x^2 + 5)*x
Is there a way to get the result in expanded form without using `expand()`? Presumably the unnecessary grouping and subsequent expansion can take some time with huge polynomials, so I would like to prevent this from the start.rwsMon, 18 Aug 2014 16:39:16 +0200https://ask.sagemath.org/question/23818/Convert symbolic expressions like sqrt(2) or exp(1) to rational numbershttps://ask.sagemath.org/question/23583/convert-symbolic-expressions-like-sqrt2-or-exp1-to-rational-numbers/I have coefficents of a rational polynomial f(x) in terms of symbolic expressions like `sqrt(2)` and `exp(1)`.
How can I convert these coefficients to rational number approximations of them, so that I can work in a structure like a polynomial ring? jjackFri, 25 Jul 2014 19:35:04 +0200https://ask.sagemath.org/question/23583/conversions from/to FunctionField(SR) and symbolic expressionhttps://ask.sagemath.org/question/10633/conversions-fromto-functionfieldsr-and-symbolic-expression/Hello,
read the following session OR if you won't please go directly to the question below
$ sage
----------------------------------------------------------------------
| Sage Version 5.6, Release Date: 2013-01-21 |
----------------------------------------------------------------------
sage: a,b,s = var('a b s')
sage: expr1 = (a^2*s + 2)/(s^3 + s + 3) + s
sage: expr1.denominator()
s^3 + s + 3
sage: type(s)
<type 'sage.symbolic.expression.Expression'>
sage: FF.<s> = FunctionField(SR)
sage: FF(expr1)
s + (a^2*s + 2)/(s^3 + s + 3)
sage: FF(expr1).denominator()
1
# s in expr1 is NOT recognized as the s in the definition of the
# function field.
sage: type(s)
<type 'sage.rings.function_field.function_field_element.FunctionFieldElement_rational'>
# BUT:
sage: x = var('x')
sage: expr2 = x + (45^2 + 2)/(x^3 + x + 3)
sage: FF2.<x> = FunctionField(RR)
# now RR instead of SR and x as the variable.
sage: FF2(expr2)
(x^4 + x^2 + 3.00000000000000*x + 2027.00000000000)/(x^3 + x + 3.00000000000000)
sage: FF2(expr2).denominator()
x^3 + x + 3.00000000000000
# x is correctly recognized in expr2 but not in expr1 !
sage: type(x)
<type 'sage.rings.function_field.function_field_element.FunctionFieldElement_rational'>
QUESTION: a FunctionField over RR with the variable x correctly recognizes
expressions where x=var('x') appears in the expression (see above), and the computation
of denominator is correct; FunctionField over SR with the variable s do not
recognizes expressions with s=var('s'); instead in this case the s is treated
like a coefficient (denominator=1 in example above in the first part).
How can i adjust this behavior, so that I obtain the same answer in both following cases:
sage: expr1
s + (a^2*s + 2)/(s^3 + s + 3)
# here s = var('s') is symbolic expression
sage: FF(expr1).denominator()
1
# I DO NOT WANT THIS ANSWER
sage: FF(s + (a^2*s+2)/(s^3 + s + 3)).denominator()
s^3 + s + 3
# but this answer (that works if the expression is constructed by hand) with
sage: type(s)
<type 'sage.rings.function_field.function_field_element.FunctionFieldElement_rational'>
Any suggestion ?
THANK YOU VERY MUCH !
alessandroSun, 20 Oct 2013 07:16:23 +0200https://ask.sagemath.org/question/10633/