substitution of lambda variable

115 views
Skip to first unread message

Paul Sargent

unread,
May 28, 2009, 11:38:35 AM5/28/09
to sage-support
Hi,

I have some equations that use the Greek letter lambda in them, but
I'm having problems because lambda is a keyword in python, and at some
points the two collide. For example:

sage: l = var("lambda")
sage: t = var("theta")
sage: e1 = t == l^2
sage: e2 = e1.solve(l)
sage: e1
theta == lambda^2
sage: e2
[lambda == -sqrt(theta), lambda == sqrt(theta)]

# Subs for theta (have to use "theta" and not "t")

sage: (e2[0].subs(theta = 5), e2[1].subs(theta = 5))
(lambda == -sqrt(5), lambda == sqrt(5))

# 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?

Mike Hansen

unread,
May 28, 2009, 11:49:09 AM5/28/09
to sage-s...@googlegroups.com
Hello,

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

Jason Grout

unread,
May 28, 2009, 12:01:46 PM5/28/09
to sage-s...@googlegroups.com
Mike Hansen wrote:
> Hello,
>
> 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.


lambda is a reserved keyword in Python, which means you can't use it in
a python statement that you are writing. You'll notice below that Mike
never writes lambda on a line (he only writes the string "lambda").

Yeah, I know; it's frustrating for me in linear algebra to not use
lambda for eigenvalues.

Jason




>>
>> 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
>
> >
>


--
Jason Grout

Paul Sargent

unread,
May 28, 2009, 12:06:31 PM5/28/09
to sage-s...@googlegroups.com

On 28 May 2009, at 16:49, Mike Hansen wrote:

> 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.

Mike Hansen

unread,
May 28, 2009, 12:11:27 PM5/28/09
to sage-s...@googlegroups.com
On Thu, May 28, 2009 at 9:06 AM, Paul Sargent <psa...@gmail.com> wrote:
> 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.

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

Mandeep Singh

unread,
Sep 16, 2015, 4:54:40 AM9/16/15
to sage-support


On Thursday, May 28, 2009 at 9:31:46 PM UTC+5:30, Jason Grout wrote:
Mike Hansen wrote:
> Hello,
>
> 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)
<snip>
<quote>lambda is a reserved keyword in Python, which means you can't use it in
a python statement that you are writing. You'll notice below that Mike
never writes lambda on a line (he only writes the string "lambda").

Yeah, I know; it's frustrating for me in linear algebra to not use
lambda for eigenvalues. 
</quote>

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.

Nils Bruin

unread,
Sep 16, 2015, 10:03:50 AM9/16/15
to sage-support
On Wednesday, September 16, 2015 at 1:54:40 AM UTC-7, Mandeep Singh wrote:
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.

This works. I don't know if it will keep working:

sage: l=SR.symbol('lambda')
sage: l
lambda
sage: l^2+l+1
lambda^2 + lambda + 1

Reply all
Reply to author
Forward
0 new messages