# get coordinates and function value as list

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 close merge delete

Sort by » oldest newest most voted

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


or

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 # prints (0,0,0)
L = (1,2, f(1,2))
print L # prints (1,2,3)

L.append((1.1, 1.2, f(1.1, 1.2)))
print L

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.