def circle4points(p, tol=1e-1): A = np.column_stack((2. * p[:, 0], 2. * p[:, 1], [-1, -1, -1])) b = np.sum(p**2, axis=1) # Normalizing the vectors norm = np.sqrt(np.sum(A * A, axis=1)) An = np.transpose(A.T / norm) bn = b / norm # Solving the system M = GaussElimination(An, bn) M.solve() sol = M.sol # Calculating determinant of a 3x3 matrix det = An[0,0] * An[1,1] * An[2,2] + An[0,1] * An[1,2] * An[2,0] + \ An[0,2] * An[1,0] * An[2,1] - An[0,2] * An[1,1] * An[2,0] - \ An[0,1] * An[1,0] * An[2,2] - An[0,0] * An[1,2] * An[2,1] r = np.sqrt(sol[0]**2 + sol[1]**2 - sol[2]) eps = np.finfo(float).eps if np.abs(det) < eps: print "System does not have a solution (det=0)" elif np.abs(det) < tol: print "System is ill conditioned: |det| = {0}".format(np.abs(det)) else: print "System has a determined solution: det = {0}".format(det) print "Solution is x = {0:.5f}, y={1:.5f} and r={2:.5f}".format( sol[0], sol[1], r) return
def circle4points(p, tol=1e-1): A = np.column_stack((2. * p[:,0], 2. * p[:,1], [-1,-1,-1])) b = np.sum(p**2, axis=1) # Normalizing the vectors norm = np.sqrt(np.sum(A*A, axis=1)) An = np.transpose(A.T / norm) bn = b / norm # Solving the system M = GaussElimination(An,bn) M.solve() sol = M.sol # Calculating determinant of a 3x3 matrix det = An[0,0] * An[1,1] * An[2,2] + An[0,1] * An[1,2] * An[2,0] + \ An[0,2] * An[1,0] * An[2,1] - An[0,2] * An[1,1] * An[2,0] - \ An[0,1] * An[1,0] * An[2,2] - An[0,0] * An[1,2] * An[2,1] r = np.sqrt(sol[0]**2 + sol[1]**2 - sol[2]) eps = np.finfo(float).eps if np.abs(det) < eps: print "System does not have a solution (det=0)" elif np.abs(det) < tol: print "System is ill conditioned: |det| = {0}".format(np.abs(det)) else: print "System has a determined solution: det = {0}".format(det) print "Solution is x = {0:.5f}, y={1:.5f} and r={2:.5f}".format(sol[0], sol[1],r) return
class chi2_minimization(): """ Calculate coefficients for linear combination of functions that minimize the chi^2. """ def __init__(self, x, y, fs): self.x = x self.y = y self.fs = fs self.dim = len(fs) self.make_system() self.M = GaussElimination(self.A, self.b) self.M.solve() self.sol = self.M.sol def make_system(self): """ Calculate the least square standard system""" fx = np.zeros((self.dim, len(self.x))) self.b = np.zeros((self.dim)) for i in range(self.dim): fx[i] = self.fs[i](self.x) self.b[i] = np.sum(self.fs[i](self.x) * self.y) self.A = np.dot(fx, fx.T)