On 3/7/07, Kyle Schalm <[EMAIL PROTECTED]> wrote:
> > On 3/7/07, Kyle Schalm <[EMAIL PROTECTED]> wrote:
> >> . the question is: how do i evaluate w while leaving z untouched?
> >> (i actually want to do this when R1 is a multivariable ring, but i imagine
> >> it works the same way.)
> >>
> >
> > Can't you just work with all the variables together, like:
> >
> > sage: P.<z,w>=QQ['z','w']
> > sage: P
> > _2 = Polynomial Ring in z, w over Rational Field
> > sage: f=z*w
> > sage: f(z,2)
> > _4 = 2*z
>
> i suppose i could if necessary. it's not ideal for at least two reasons i
> can think of:
>
> 1. if i want to evaluate just z (the most common case), then i have to
> write f(z,_,_,_,...).
>
> 2. separating the other variables into a base ring causes the polynomial
> to group terms by powers of z, which i also like.
I agree that it should be allowed to evaluate in the way you want.
> > If, not, you can always make a little function to evaluate (if there
> > is no "built-in" way).
> >
> > sage: R1.<w> = QQ['w']
> > sage: R2.<z> = R1['z']
> > sage: f = z*w
> > sage: def my_eval(f,a):
> > ...: coef=f.coeffs()
> > ...: res=0
> > ...: for i in range(len(coef)):
> > ...: res+=coef[i](a)*z^i
> > ...: return res
> > ...:
> > sage: f = z*w
> > sage: my_eval(f,2)
> > _5 = 2*z
> > sage: g=(w+1)+w^2*z+3*z^3
> > sage: my_eval(g,2)
> > _7 = 3*z^3 + 4*z + 3
>
> hmm, this looks useful and i think that is what i will do. much thanks.
The above code is a good idea, but there would be efficiency
issues. It would be better to do this (see the ev function below).
sage: R1.<w> = QQ['w']
sage: R2.<z> = R1['z']
sage: f = w*z + (1-w)*z^3 + 3
sage: def ev(f, a):
... return f.parent()([c(a) for c in f.list()])
sage: ev(f, 3)
(-2)*z^3 + 3*z + 3
That said, I would love to add a function that basically does the above
to SAGE, but it's unclear what the notation would even be. One idea
is this:
sage: f(w=3)
would evaluate by setting w to 3. It would do this by:
(1) check if w is an indeterminate of the parent of f; if so return f(3).
(2) if the parent of f is not a poly ring, return f itself.
(3) If not, return
f.parent()([c(w=a) for c in f.list()])
This would recursively do (1)-(2) for each coefficient of f. This would
work for very very complicated expressions in great generality and hence
would probably be quite useful. This is also exactly the sort of thing
Bobby has already implemented for his symbolic calculus package.
So -- does anybody want to volunteer to implement the above? :-)
-- William Stein
"Implement it and send me a patch"
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/sage-support
URLs: http://sage.math.washington.edu/sage/ and http://sage.scipy.org/sage/
-~----------~----~----~----~------~----~------~--~---