ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 05 Jul 2012 03:39:03 +0200get coordinates and function value as listhttps://ask.sagemath.org/question/9136/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. Wed, 04 Jul 2012 16:32:33 +0200https://ask.sagemath.org/question/9136/get-coordinates-and-function-value-as-list/Answer by benjaminfjones for <p>I am quite new in sage as well as in programming, so please if you explain this, please do it thouroughly.</p>
<p>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. </p>
https://ask.sagemath.org/question/9136/get-coordinates-and-function-value-as-list/?answer=13797#post-id-13797List 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[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]
Wed, 04 Jul 2012 17:53:15 +0200https://ask.sagemath.org/question/9136/get-coordinates-and-function-value-as-list/?answer=13797#post-id-13797Comment by Cosmos for <p>List comprehension is a way of constructing lists that is very readable and concise. It mimics set builder notation from mathematics:</p>
<pre><code>f(x,y) = x + y
L = [ (a, b, f(a, b)) for (a,b) in [ (0,0), (1,1), (5.6, 7.0) ] ]
</code></pre>
<p>or</p>
<pre><code>L = [ (a, b, f(a, b)) for a in range(10) for b in range(10) ]
</code></pre>
<p>try these out and see what they produce.</p>
<p>You can now modify your list or add to it any way you want. For example:</p>
<pre><code>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]
</code></pre>
https://ask.sagemath.org/question/9136/get-coordinates-and-function-value-as-list/?comment=19443#post-id-19443Thank 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. Thu, 05 Jul 2012 03:39:03 +0200https://ask.sagemath.org/question/9136/get-coordinates-and-function-value-as-list/?comment=19443#post-id-19443