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.Thu, 19 Dec 2019 20:18:18 +0100substitution of variable in the result of monomials()https://ask.sagemath.org/question/49089/substitution-of-variable-in-the-result-of-monomials/Given a polynomial in n variables, I'd like to extract the list of its monomials, and then manipulate that list by substituting certain variables for others.
Simple example: in $Z[x,y]$ consider the polynomial $1+x+y^2$; the list of its monomials (in some ordering) is $(1,x,y^2)$. My function should be able e.g. to take that list and substitute $y$ with $x$, namely return $[1,x,x^2]$.
At the moment, my code gives error, but I do not understand how to fix it.
N.<x1,x2> = PolynomialRing(ZZ, 2)
f = 1+x1+x2
g = f.monomials()
for i in range(3):
g[i] = g[i].substitute_expression(x2==x1)
Namely, how do I make a variable in the polynomial ring also have the substitute attribute? or is there a better way to achieve this?rue82Thu, 19 Dec 2019 20:18:18 +0100https://ask.sagemath.org/question/49089/Substitution of a list of variableshttps://ask.sagemath.org/question/10606/substitution-of-a-list-of-variables/I wish substituting a list of variables, without having to substitute them 1 by 1.
Here is an example with an attempt which doesn't work :
sage: V=[var('x_%d' % i) for i in range(10)]; V
[x_0, x_1, x_2, x_3, x_4, x_5, x_6, x_7, x_8, x_9]
sage: S=[sum(V),prod(V)]; S
[x_0 + x_1 + x_2 + x_3 + x_4 + x_5 + x_6 + x_7 + x_8 + x_9,
x_0*x_1*x_2*x_3*x_4*x_5*x_6*x_7*x_8*x_9]
sage: for i in range(10):
....: S.subs(V[i]=i)
....:
File "<ipython-input-99-7670392e3358>", line 2
SyntaxError: keyword can't be an expression
How can we do this ?
Sébastien PalcouxSun, 13 Oct 2013 19:44:43 +0200https://ask.sagemath.org/question/10606/Substitute list of expressionshttps://ask.sagemath.org/question/8518/substitute-list-of-expressions/How is it possible to substitute list of expressions?
For example:
sage: y=[4.0,2.0,5.0,1.0]
sage: x=[1.0,2.0,3.0,4.0]
sage: xy=zip(x,y)
sage: var('a,b,c,d,x')
(a, b, c, d, x)
sage: model(x)=a*x^3+b*x^2+c*x+d
sage: sol=find_fit(xy,model)
sage: sol
[a == -2.0000000000000031, b == 14.500000000000021, c == -31.500000000000046, d == 23.000000000000028]
Now sol is a list of expressions. How can I substitute it into my model? The only way I found is:
sage: func=model.substitute_expression(sol[0],sol[1],sol[2],sol[3])(x)
sage: func
-2.0000000000000031*x^3 + 14.500000000000021*x^2 - 31.500000000000046*x + 23.000000000000028
However, the expression like "sol[0],...,sol[3]" seems to be not a good style...
Best regards, Aleksey.Aleksey_RWed, 30 Nov 2011 02:50:02 +0100https://ask.sagemath.org/question/8518/