Ask Your Question

python equivalent of T(x,y)=(x*y,x+y) ?

asked 2011-02-08 12:49:59 +0200

Shu gravatar image

updated 2015-01-14 09:44:04 +0200

FrédéricC gravatar image

In sage command line, I can write T(x,y) = (x*y,x+y) and can apply T.diff() to compute Jacobian.

But when I write the same in python, it says you can not assign to a function.

Does anyone know what I should write in python for T(x,y)=(x*y,x+y)?

edit retag flag offensive close merge delete


I'm not sure if this belongs on ask.sagemath... after all, this is a Sage help site. Or am I missing something?

kcrisman gravatar imagekcrisman ( 2011-02-08 13:02:47 +0200 )edit

2 Answers

Sort by » oldest newest most voted

answered 2011-02-08 13:03:21 +0200

kcrisman gravatar image

I don't know if this is what you are looking for, but the Sage preparser can tell you exactly what is done in Python behind the scenes:

sage: preparse('T(x,y) = (x*y,x+y)')
'__tmp__=var("x,y"); T = symbolic_expression((x*y,x+y)).function(x,y)'
edit flag offensive delete link more


Thanks, yes, that's what I wanted.

Shu gravatar imageShu ( 2011-02-08 13:47:23 +0200 )edit

Great! If it answers things exactly, you can click the check mark to indicate this in case someone else searches for a similar question and wants to know if the right answer was found.

kcrisman gravatar imagekcrisman ( 2011-02-08 23:22:13 +0200 )edit

answered 2011-02-08 14:00:15 +0200

updated 2011-02-08 14:00:41 +0200

This is not an answer, but I need more room for my text.

I think that what Shu wants is the following :

#! /usr/bin/sage -python
# -*- coding: utf8 -*-

from sage.all import *
T = (x+y,x*y)  # (1)
print T.diff() # (2)

But the execution of that code finishes on :

AttributeError: 'tuple' object has no attribute 'diff'

Thus the question is : how to modify the line (1) in order the line (2) to produce that answer :

[(x, y) |--> y (x, y) |--> x]
[(x, y) |--> 1 (x, y) |--> 1]
edit flag offensive delete link more


Does `T = symbolic_expression((x*y,x+y)).function(x,y)` do what you need? It is true that things in parentheses are just 'tuples' in normal Python syntax.

kcrisman gravatar imagekcrisman ( 2011-02-08 23:25:32 +0200 )edit

Yes, that makes the work for what I was thinking about. I seems however that it was not the original poster's original need. Well at least I learn something :)

Laurent Claessens gravatar imageLaurent Claessens ( 2011-02-09 02:53:42 +0200 )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: 2011-02-08 12:49:59 +0200

Seen: 1,069 times

Last updated: Feb 08 '11