Ask Your Question

Limits with dictionaries

asked 2012-09-06 13:12:00 +0100

ktkohl96 gravatar image

Suppose I have a dictionary that is the result of a solve command, for example, D={n1:2}

I also have an expression stored, for example, expr1 = 3*n1^2

In this example, I want to find the limit of expr1 as n1 -> 2.

How can I do this given some expression and some dictionary in general? I haven't figured it out with sage_eval yet.

(I know that if it were just substitution, I could use expr1.subs_expr(D) but I have cases where I believe I need to use limits instead of substitution.)


edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2012-09-06 13:28:40 +0100

DSM gravatar image

updated 2012-09-06 21:54:54 +0100

Hmm, this is kind of fun! First the setup:

sage: n1 = var("n1")
sage: sols = solve(n1*2 == 4, n1, solution_dict=True)
sage: sols
[{n1: 2}]
sage: d = sols[0]
sage: d
{n1: 2}
sage: expr1 = 3*n1^2

In general, you can turn a dictionary into extra keyword arguments by using the ** operator, for example:

sage: def f(*args, **kwargs):
....:     print args, kwargs
sage: f(2,3,4,fred=97)
(2, 3, 4) {'fred': 97}
sage: fdict = {'fred': 13, 'bob': 28} 
sage: f(**fdict)
() {'bob': 28, 'fred': 13}

So at first I thought this would work:

sage: limit(expr1, **d) 
TypeError                                 Traceback (most recent call last)

/home/mcneil/m2/<ipython console> in <module>()

TypeError: limit() keywords must be strings

but it needs variable names, not the variables themselves. Well, we can do that too, using a dict comprehension to build a temporary dictionary:

sage: d_named = {str(variable): value for variable, value in d.iteritems()}
sage: d_named
{'n1': 2}
sage: limit(expr1, **d_named)                                      
edit flag offensive delete link more


Hmm, nice answer. Is this a bug, perhaps? I also note that `sage: limit(expr1,sols[0].items[0][0]=sols[0].items[0][1])` doesn't work.

kcrisman gravatar imagekcrisman ( 2012-09-06 13:33:41 +0100 )edit

BTW, you two should meet someday at a Sage Days or something. I've think you've both worked on some of the same tickets in special functions!

kcrisman gravatar imagekcrisman ( 2012-09-06 13:34:17 +0100 )edit

@kcrisman: it might be a limitation that we don't have a way to pass a dictionary, but it's not a bug pe se. Functions can only have strings as argument names, not general objects, which makes sense in retrospect.

DSM gravatar imageDSM ( 2012-09-06 13:37:42 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2012-09-06 13:12:00 +0100

Seen: 258 times

Last updated: Sep 06 '12