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.Wed, 30 Nov 2011 02:50:02 +0100Substitute 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/Substituting multiple valueshttps://ask.sagemath.org/question/8292/substituting-multiple-values/What is the best way of substituting a list (or vector or whatever) of values into an expression? For example, suppose I have
z = var('x y')
zvals = (1, 2)
w = x^2 + y^2
and want to substitute `zvals` for `z` in the expression `w`. I have tried the following commands:
w.subs(z=zvals) #doesn't work
w.subs({z:zvals}) #doesn't work
w.subs(x=1,y=2) #fine, but cumbersome if z has many elements
w.subs(z[0]=zvals[0],z[1]=zvals[1]) #doesn't work
w.subs({z[0]:zvals[0],z[1]:zvals[1]}) #fine, and could turn this into a loop
#if there are many variables, but ugly
w.subs(dict(zip(z,zvals))) #best I can come up with
As far as I can see, none of this behaviour changes if `z` and `zvals` are vectors or lists instead of tuples.
Is there a simpler way of doing this? Also, why doesn't the fourth attempt work when the fifth one does - is this down to a limitation of Python?
EDIT: I also realised that you can do `w.subs(z[0]==zvals[0])` but `w.subs(z[0]==zvals[0],z[1]==zvals[1])` won't work - why is this?SagenoobThu, 25 Aug 2011 10:09:33 +0200https://ask.sagemath.org/question/8292/Substituting function value in an expressionhttps://ask.sagemath.org/question/8201/substituting-function-value-in-an-expression/I have an expression like `uR(t) == 3*iL(0) + uC(0)/2 - 4`
how can I substitute for `iL(0)` and `uC(0)`, if I'm given, that `iL(0) = 0` and `uC(0) = 0`.
`uR`, `iL`, `uC` are a functions of var `t`:
t = var('t')
uR = function('uR', t)
iL = function('iL', t)
uC = function('uC', t)
thank you :)OndraWed, 29 Jun 2011 23:27:56 +0200https://ask.sagemath.org/question/8201/substituting expressions for numbershttps://ask.sagemath.org/question/7975/substituting-expressions-for-numbers/Say you have some formula in the form of an expression:
sage: x,y,a,b = var("x y a b")
sage: f = 10*x+2*y
sage: type(f)
<type 'sage.symbolic.expression.Expression'>
Now I want to replace the 10 by the variable a. What's the easiest
way to do this? It's the opposite of the typical substitution, so the functions
I'd usually throw at it all fail.
So far I can only up with (1) string techniques, which are bugs
waiting to happen, or (2) walking the entire expression tree and
constructing a new expression from each operator/operand triplet.
DSMMon, 28 Feb 2011 02:30:23 +0100https://ask.sagemath.org/question/7975/