Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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}

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

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