Ask Your Question

get coordinates and function value as list

asked 2012-07-04 16:32:33 +0200

Cosmos gravatar image

I am quite new in sage as well as in programming, so please if you explain this, please do it thouroughly.

Let's assume we have a simple function like F(x,y)=x+y. Is it possible to create a list, something like (x,y,F)? To make clearer a list in the form of (value_of_x_coordinate,value_of_y_coordinate,value_of_function) for instance (1,1,2) or (1,2,3). If this is possible, how can we transform the coordinates x,y by operating somehow on the list e.g adding on both x and y, a number N.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2012-07-04 17:53:15 +0200

benjaminfjones gravatar image

List comprehension is a way of constructing lists that is very readable and concise. It mimics set builder notation from mathematics:

f(x,y) = x + y
L = [ (a, b, f(a, b)) for (a,b) in [ (0,0), (1,1), (5.6, 7.0) ] ]


L = [ (a, b, f(a, b)) for a in range(10) for b in range(10) ]

try these out and see what they produce.

You can now modify your list or add to it any way you want. For example:

f(x,y) = x + y
L = [ (a, b, f(a, b)) for (a,b) in [ (0,0), (1,1), (5.6, 7.0) ] ]
print L[0] # prints (0,0,0)
L[0] = (1,2, f(1,2))
print L[0] # prints (1,2,3)

L.append((1.1, 1.2, f(1.1, 1.2)))
print L[3]
edit flag offensive delete link more


Thank you for your reply. I think now i can make a more general question on the subject.When we ask sage to plot a function doesn't it replace (x,y) with values,it gets the values of the function and then plots it? Is there a way instead of asking from the program to plot the values it just calculated, to just create a list or some other construct with all the values (so that we don't have to create it ourselves each time)? Something like x_coordinates_column,y_coordinates_column,Function_value_column. Let's use this example: We have a f(x,y)=sin(x)*cos(y) and we want the contours sin(x)*cos(y)=k where k is a real number we can change as we like. I would like to have a consrtuct as described above with x_values,y_values,k.

Cosmos gravatar imageCosmos ( 2012-07-05 03:39:03 +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

1 follower


Asked: 2012-07-04 16:32:33 +0200

Seen: 565 times

Last updated: Jul 04 '12