# module file: ex107.py
# Purpose:
#   To compute the coefficients of a quadratic equation based on
#   three points. 
#
# Record of revisions:
#     Date         Programmer          Description of change
#     ====         ==========          =====================
#   01/03/07       Joe the Plummer     Original code
#
from pylab import *;
from sys import argv;

def para_fit(points):
	'''
		para_fit(points)
		input:  a sequence of three tuples. e.g. ((-1, 9), (1,5), (2,12))
		output: a tuple of length 3:  (a, b, c)
	
		usage:  a,b,c = para_fit(points)
	'''

	x = array([ point[0] for point in points]);
	y = array([ point[1] for point in points]);

	#the matrix formed by x^2,  x and 1
	A = column_stack( (x**2, x, ones(3)));
	# A = [ x(1)^2, x(1), 1 ; ...
	#      x(2)^2, x(2), 1 ; ...
	#      x(3)^2, x(3), 1 ];
 
	# solve for the coefficients a
	coeff = solve(A, y);

	return coeff


def main():
	'''
	usage:
	python t5_1.py (x1,y1), (x2,y2) , (x3,y3)
	'''
    points = eval('['+ "".join(argv[1:]) + ']');
 
	
    a,b,c = para_fit(points)
	
    print "y = %4.2f * x^2 + %4.2f * x + %4.2f"%(a,b,c)
	
if __name__ == "__main__":
    main()


     



