def do_GET(self): if self.path=='/': reload(gauss) self.send_response(200,'OK') self.send_header('Content-type','text/html') self.end_headers() print >>self.wfile,FRONTPAGE elif self.path=='/jquery.min.js': self.send_response(200,'OK') self.send_header('Content-type','text/html') self.end_headers() with open('jquery.min.js') as f: print >>self.wfile,f.read() elif self.path=='/favicon.ico': self.send_response(404) else: data=unquote(self.path[1:]).replace('lf','\n').strip() # print data self.send_response(200,'OK') self.send_header('Content-type','text/html') self.end_headers() try: m,n,M=gauss.parse(data) def outf(M): if isinstance(M,str):print >>self.wfile,M else: for x in M: for y in x: print >>self.wfile,'%6s'%y, print >>self.wfile print >>self.wfile,'-'*40 gauss.solve(m,n,M,outf) except Exception,e: import traceback print >>self.wfile,traceback.format_exc()
def pointOfCollision_2(simplex): ''' Another approach to calculating the collision point, using baryocentric coordinates. ''' assert isinstance(simplex, simplices.Simplex), 'Input must be a Simplex object' assert len(simplex.get_points()) == 4, 'Terminated without full simplex' #points in the minkowski simplex simpPoints = simplex.get_all_points(0) #points in the simplex of shape 1 aPoints = simplex.get_all_points(1) #points in the simplex of shape 2 bPoints = simplex.get_all_points(2) # create a matrix matrix = [[1.0]*len(simpPoints)] vecs = [] for vector in simpPoints: value = vector.value vecs.append(value) for i in range(len(vecs[0])): outvec = [] for j in range(len(vecs)): outvec.append(vecs[j][i]) matrix.append(outvec) barCoord = gauss.solve(matrix, [1.0, 0.0, 0.0, 0.0]) if barCoord: collisionPoint = vectors.Vector() for i in range(len(aPoints)): collisionPoint += aPoints[i]*barCoord[i] assert isinstance(collisionPoint, vectors.Vector), 'CollisionPoint must be a vector' return collisionPoint else: return pointOfCollision(simplex)
def test_copy_middle_row_of_solvable(): expected = [ pytest.approx(elem) for elem in [0.2608695652173913, 0.04347826086956526, -0.1304347826086957] ] actual = solve([[4, 2, 1, 1], [7, 8, 9, 1], [7, 8, 9, 1], [9, 1, 3, 2]]) actual = [pytest.approx(elem) for elem in actual] assert expected == actual
def solve(input_system): columns = matrix.columns(input_system) f = columns[-1] subspace = columns[:-1] system = [] for i, e in enumerate(subspace): assert len(f) == len(e) equation = [] for j, s in enumerate(subspace): equation.append(vector.dot(e, s)) equation.append(vector.dot(e, f)) system.append(equation) gauss.solve(system)
def Berlekamp_Welch( aList: List[int], # inputs bList: List[int], # received codeword k: int, t: int, p: int # prime ) -> nmod_poly: """ Berlekamp-Welsch-Decoder This function throws an exception if no solution exists see https://en.wikipedia.org/wiki/Berlekamp%E2%80%93Welch_algorithm """ if len(aList) < 1: raise FuzzyError("aList is empty") if len(aList) != len(bList): raise FuzzyError("bList is empty") if k < 1 or t < 1: raise FuzzyError("k={0} and t={1} are not consistent".format(k, t)) if p < 2 or not isprime(p): raise FuzzyError("p is not prime") n = len(aList) # Create the Berlekamp-Welch system of equations # and store them as an n x n matrix 'm' and a # constant source vector 'y' of length n m_entries: List[nmod] = [] y_entries: List[nmod] = [] for i in range(n): a: int = aList[i] b: int = bList[i] apowers: List[nmod] = mod_get_powers(a, k + t, p) for j in range(k+t): m_entries.append(apowers[j]) for j in range(t): m_entries.append(-b * apowers[j]) y_entries.append(b * apowers[t]) m: nmod_mat = nmod_mat(n, n, m_entries, p) y: nmod_mat = nmod_mat(n, 1, y_entries, p) # solve the linear system of equations m * x = y for x try: x = gauss.solve(m, y).entries() except gauss.NoSolutionError: raise FuzzyError("No solution exists") # create the polynomials Q and E Qs: List[nmod] = x[:k+t] Es: List[nmod] = x[k+t:] Es.append(nmod(1, p)) Q: nmod_poly = nmod_poly(Qs, p) E: nmod_poly = nmod_poly(Es, p) Answer: nmod_poly = Q // E Remainder: nmod_poly = Q - Answer * E if len(Remainder.coeffs()) > 0: raise FuzzyError("Remainder is not zero") return Answer
#!/usr/bin/python3 import gauss; mat = [ [ 1, 0, 0, 1 ], [ 0, 1, 0, 2 ], [ 0, 0, 1, 3 ] ] gauss.matrix.output(mat) comp = gauss.triangularize(mat, 3) base = gauss.base(3, comp) if not gauss.solve(mat, base, 3): print("No solution.") else: gauss.output_results(base)
def test_floats(self): m = numpy.array([[.1, .2, .3], [0., .1, .2], [0., 0., .1]]) b = numpy.array([0.1, .04, .01]) result = solve(m, b) expected = numpy.array([.3, .2, .1]) assert_array_almost_equal(result, expected)
def test(self): m = numpy.array([[1., 2., 3.], [0., 1., 2.], [0., 0., 1.]]) b = numpy.array([10., 4., 1.]) result = solve(m, b) expected = numpy.array([3., 2., 1.]) assert_array_equal(result, expected)
def test_no2(): with pytest.raises(ValueError, match="NO"): solve([[2, -1, 3, 1], [2, -1, -1, -2], [4, -2, 6, 0], [6, 8, -7, 2]])
def test_zero_column_no(): with pytest.raises(ValueError, match="NO"): solve([[4, 2, 0, 1, 1], [4, 2, 0, 1, 2], [7, 8, 0, 9, 1], [9, 1, 0, 3, 5]])
def test_zero_column_inf(): with pytest.raises(ValueError, match="INF"): solve([[0, 4, 2, 1, 1], [0, 4, 2, 1, 1], [0, 7, 8, 9, 1], [0, 9, 1, 3, 2]])
def test_inf2(): with pytest.raises(ValueError, match="INF"): solve([[1, 0, 3, 3], [0, 3, 0, 3]])
def test_solvable2(): expected = [pytest.approx(elem) for elem in [1.0, 1.0]] actual = solve([[1, 2, 3], [0, 3, 3], [0, 0, 0], [0, 0, 0]]) actual = [pytest.approx(elem) for elem in actual] assert expected == actual
def test_no(): with pytest.raises(ValueError, match="NO"): solve([[1, 3, 2, 7], [2, 6, 4, 8], [1, 4, 3, 1]])
def test_inf(): with pytest.raises(ValueError, match="INF"): solve([[1, 3, 4, 4], [2, 1, 4, 5]])