455,766 Members | 1,393 Online Need help? Post your question and get tips & solutions from a community of 455,766 IT Pros & Developers. It's quick & easy.

# fonction in python

 P: n/a Hello, If we write = x^2 and if I give to the program the values of x, it will going to calculate the values of y, and also for x. But it is possible ? that is if I give to the program the values of X and Y, it will indicate to me the relation between the two variables, in the other hand if I look to the program x=2 y=4, x=3 y=9 ect... it is going to show me that f (t)!!! Thanks -- View this message in context: http://www.nabble.com/fonction-in-py....html#a5164997 Sent from the Python - python-list forum at Nabble.com. Jul 4 '06 #1
6 Replies

 P: n/a aliassaf wrote: Hello, If we write = x^2 and if I give to the program the values of x, it will going to calculate the values of y, and also for x. But it is possible ? that is if I give to the program the values of X and Y, it will indicate to me the relation between the two variables, in the other hand if I look to the program x=2 y=4, x=3 y=9 ect... it is going to show me that f (t)!!! That is not possible at all. There are too many possible functions mapping 2 to 4, 3 to 9 etc. You can, however, create a list of candidate functions and check your input pairs (x,y) against each of those functions to see if any of them matches. Short, unoptimized example: functions = [ lambda x: x, lambda x: x+1, lambda x: x*x, lambda x: x**3, ] input = [(1,1), (2,2)] for function in functions: for x,y in input: if function(x) != y: break else: print "function", function, "matches" Georg Jul 4 '06 #2

 P: n/a On Tue, 04 Jul 2006 03:06:37 -0700, aliassaf wrote: > Hello, If we write = x^2 and if I give to the program the values of x, it will going to calculate the values of y, and also for x. But it is possible ? that is if I give to the program the values of X and Y, it will indicate to me the relation between the two variables, in the other hand if I look to the program x=2 y=4, x=3 y=9 ect... it is going to show me that f (t)!!! You are asking for curve-fitting. There is a HUGE amount of work on curve-fitting in computer science and statistics. Generally, you start with some data points (x, y). You generally have some idea of what sort of function you expect -- is it a straight line? A curve? What sort of curve? A polynomial, an exponential, a sine curve, a cubic spline, a Bezier curve? You might like to google on "least squares curve fitting" and "linear regression". That's just two methods out of many. Some curve-fitting methods also estimate the error between the predicted curve and the data points; you could then try all of the methods and pick the one with the least error. -- Steven. Jul 4 '06 #3

 P: n/a On 2006-07-04, aliassaf

 P: n/a aliassaf wrote: Hello, If we write = x^2 and if I give to the program the values of x, it will going to calculate the values of y, and also for x. But it is possible ? that is if I give to the program the values of X and Y, it will indicate to me the relation between the two variables, in the other hand if I look to the program x=2 y=4, x=3 y=9 ect... it is going to show me that f (t)!!! You can use the GMPY module to determine square & power relationships: >>import gmpyfor n in range(20): print n, if gmpy.is_square(n): print True, else: print False, if gmpy.is_power(n): print True else: print False 0 True True 1 True True 2 False False 3 False False 4 True True 5 False False 6 False False 7 False False 8 False True 9 True True 10 False False 11 False False 12 False False 13 False False 14 False False 15 False False 16 True True 17 False False 18 False False 19 False False 9 is both a power and a square whereas 8 is a power but not a square. > Thanks -- View this message in context: http://www.nabble.com/fonction-in-py....html#a5164997 Sent from the Python - python-list forum at Nabble.com. Jul 4 '06 #5

 P: n/a Steven D'Aprano wrote: On Tue, 04 Jul 2006 03:06:37 -0700, aliassaf wrote: >>Hello,If we write = x^2 and if I give to the program the values of x, it willgoing to calculate the values of y, and also for x.But it is possible ? that is if I give to the program the values of X and Y,it will indicate to me the relation between the two variables, in the otherhand if I look to the program x=2 y=4, x=3 y=9 ect... it is going to show methat f (t)!!! You are asking for curve-fitting. There is a HUGE amount of work on curve-fitting in computer science and statistics. Generally, you start with some data points (x, y). You generally have some idea of what sort of function you expect -- is it a straight line? A curve? What sort of curve? A polynomial, an exponential, a sine curve, a cubic spline, a Bezier curve? You might like to google on "least squares curve fitting" and "linear regression". That's just two methods out of many. Some curve-fitting methods also estimate the error between the predicted curve and the data points; you could then try all of the methods and pick the one with the least error. The problem being that complex enough models will fit the data arbitrarily closely (i.e. over-fit). The OP should take into account any prior expectations over the type of function (as you indicate) and apply Occam's razor (find a relatively simple model that gives a reasonable fit to the data). Duncan Jul 4 '06 #6

 P: n/a On 4/07/2006 8:06 PM, aliassaf wrote: Hello, If we write = x^2 and if I give to the program the values of x, it will going to calculate the values of y, and also for x. But it is possible ? that is if I give to the program the values of X and Y, it will indicate to me the relation between the two variables, in the other hand if I look to the program x=2 y=4, x=3 y=9 ect... it is going to show me that f (t)!!! Please pardon me for introducing Python-related subject matter into a thread devoted to curve-fitting :-) Consider the following: |>[x ^ 2 for x in range(10)] [2, 3, 0, 1, 6, 7, 4, 5, 10, 11] Not what you wanted? Try this: |>[x ** 2 for x in range(10)] [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] Cheers, John Jul 4 '06 #7

### This discussion thread is closed

Replies have been disabled for this discussion.

### Similar topics

Browse more Python Questions on Bytes 