On Thu, May 28, 2009 at 8:38 AM, Paul Sargent <psa...@gmail.com> wrote:
> # Subs for lambda (have to use "lambda", but that's a keyword)
>
> sage: e1.subs(lambda = 3)
> ------------------------------------------------------------
> File "<ipython console>", line 1
> e1.subs(lambda = Integer(3))
> ^
> SyntaxError: invalid syntax
>
> For some reason I don't understand, whilst .solve() will take the
> symbolic variable as a parameter to solve for (e.g. "t"), .subs()
> requires the variable name (e.g. "theta"). Often these are the same,
> but not always, and in the case where I have a variable "lambda" it
> causes a syntax error.
>
> Any way to get around this? Is this the intended syntax for subs(), as
> it seems a little confused?
The subs() method can take various types of input -- you can look a
the docstring to see examples of these. Here's one way to do what you
want:
sage: l = var("lambda")
sage: t = var("theta")
sage: e1 = t == l^2
sage: e2 = e1.solve(l)
sage: (e2[0].subs({t:5}), e2[1].subs({t:5}))
(lambda == -sqrt(5), lambda == sqrt(5))
sage: sage: e1.subs({l:3})
theta == 9
In Sage 4.0, you'll be able to do
sage: sage: e1.subs(l==3)
theta == 9
--Mike
> sage: sage: e1.subs({l:3})
> theta == 9
Yes I'd found the dictionary form in the docstring, but didn't know
that it took the python variable rather than the symbol name (if you
see what I mean), and so was still having the same problem.
The docstring suffers from the fact that all the python variables have
the same name as the symbol they are representing so you can't tell
which is meant to be used in each form.
Thanks.
I agree that it could be confusing to someone not intimately familiar
with how Python's keyword arguments work. Do you want to make an
addition to the docstring talking about how to use a variable named
lambda?
--Mike
Can we declare lambda as a variable? I want to use it in eigenvalues (linear algebra).like:l = var("lambda");It'll give error. So how can I declare it as a variable.