def graf(f): if isinstance(f, int) or isinstance(f, float): f = Polinom([f]) if isinstance(f, Polinom): f = Fraction(f, 1) print('Ničle:', solve(f)[1]) print('Poli: ', solve_poles(f)[1]) print('Asimptota:', f.num//f.denum) print('Presečišča z asimptoto:', solve(f.num % f.denum)) print('Začetna vrednost:', f[0])
def OLS(n, x, y): A = [] m = len(x) if m != len(y): return None for i in range(n + 1): A.append([]) for j in range(n + 1): if i <= j: tmp = 0.0 for p in range(m): #for q in range(m): tmp += (float(x[p])**i) * (float(x[p])**j) A[i].append(tmp) else: A[i].append(A[j][i]) B = [] for i in range(n + 1): tmp = 0.0 for p in range(m): #for q in range(m): tmp += (float(x[p])**i) * y[p] B.append(tmp) flag, answer = solve(A, B) return answer
def form_post(): try: os.remove("static/images/g.png") except FileNotFoundError: pass if request.method == "POST": a = request.form['a'] b = request.form['b'] c = request.form['c'] result = solve([a, b, c]) if isinstance(result, set): save_png(a, b, c) unixtime = time.time() return render_template("success.html", result=result, unixtime=unixtime) else: render_template("index.html")
def handle(self): self.request.settimeout(2) chal, _, sol = challenge().partition(" => ") self.wfile.write( b"You have to send back a valid expression solving the given equation\n" ) self.wfile.write( b"ex: Given ((((5 ? 1) ? (2 ? 9)) ? ((3 ? 5) ? (0 ? 6))) ? (((3 ? 9) ? (6 ? 3)) ? ((7 ? 1) ? (9 ? 3)))) => -1\n" ) self.wfile.write( b"ex: Reply ((((5 + 1) + (2 + 9)) + ((3 + 5) + (0 + 6))) + (((3 + 9) - (6 + 3)) - ((7 + 1) + (9 * 3))))\n" ) self.wfile.write(f"{chal} => {sol}\n\n".encode()) self.data = self.rfile.readline().strip() print(f"{self.client_address[0]} wrote: {self.data.decode()}") try: result = Expression.fromstring(self.data.decode()) exp = solve(f"{chal} => {sol}") assert exp.level() == result.level() except Exception as e: traceback.print_exc() self.wfile.write(b"Invalid expression; Try harder\n") else: n1, n2 = len(chal), len(str(result)) if n1 != n2: self.wfile.write(b"Invalid expression; Input has wrong size\n") return for c1, c2 in zip(str(exp), str(result)): if c1 != c2 and c2 not in ops.values(): self.wfile.write(b"Invalid expression; Input mismatch\n") return if int(result) == int(sol): print("Success!") self.wfile.write(f"Congratulations! {flag}\n".encode()) else: print("Failure!") self.wfile.write( f"Wrong answer ({int(result)} != {int(sol)}); Try harder!\n" .encode())
def test_20(self): eqn = '-2x+3=2x+3' self.assertEqual(solve(eqn), 0)
def test_21(self): eqn = '3-2x=-1' self.assertEqual(solve(eqn), 2)
def test_7(self): eqn = 'x=-x' self.assertEqual(solve(eqn), 0)
def test_3(self): eqn = '3=2x' self.assertEqual(solve(eqn), 1.5)
A.append([]) for j in range(n): A[i].append(0.0) for i in range(n): if i - 1 >= 0: A[i][i - 1] = elpson if i + 1 < n: A[i][i + 1] = elpson + h A[i][i] = -(2 * elpson + h) B = [] for i in range(n): B.append(a * h * h) return A, B if __name__ == "__main__": A = [[20, 2, 3], [1, 8, 1], [2, -3, 15]] B = [24, 12, 30] A, B = build(0.1, 100, 0.5) #n = len(A) #for i in range(n): # for j in range(n): # print A[i][j], # print print solve_jacobi(A, B, 100)[-5:-1] print solve_GS(A, B, 100)[-5:-1] print solve_SOR(A, B, 0.8, 100)[-5:-1] print equation.solve(A, B)[1][-5:-1]
for i in range(n): A.append([]) for j in range(n): A[i].append(0.0) for i in range(n): if i - 1 >= 0: A[i][i - 1] = elpson if i + 1 < n: A[i][i + 1] = elpson + h A[i][i] = -(2 * elpson + h) B = [] for i in range(n): B.append(a * h * h) return A, B if __name__=="__main__": A = [[20, 2, 3],[1, 8, 1],[2, -3, 15]] B = [24, 12, 30] A, B = build(0.1, 100, 0.5) #n = len(A) #for i in range(n): # for j in range(n): # print A[i][j], # print print solve_jacobi(A, B, 100)[-5:-1] print solve_GS(A, B, 100)[-5:-1] print solve_SOR(A, B, 0.8, 100)[-5:-1] print equation.solve(A, B)[1][-5:-1]
def test_1(self): eqn = '2x=3' self.assertEqual(solve(eqn), 1.5)
def test_23(self): eqn = '-2x+3=2x-3' self.assertEqual(solve(eqn), 1.5)
def test_11(self): eqn = '2x=3x' self.assertEqual(solve(eqn), 0)
def test_12(self): eqn = '0=0' self.assertEqual(solve(eqn), 'NO')
def test_10(self): eqn = '2=3' self.assertEqual(solve(eqn), 'NO')
def test_9(self): eqn = '2=2' self.assertEqual(solve(eqn), 'NO')
def test_8(self): eqn = 'x=x' self.assertEqual(solve(eqn), 'NO')
def test_22(self): eqn = '-3+2x=4x-1' self.assertEqual(solve(eqn), -1)
def test_13(self): eqn = '2x+1=0' self.assertEqual(solve(eqn), -0.5)
def test_2(self): eqn = '-2x=3' self.assertEqual(solve(eqn), -1.5)
#!/usr/bin/python # -*- coding: utf-8 -*- import equation A = input(u'a: ') B = input(u'b: ') C = input(u'c: ') R = equation.solve(A,B,C) print(R) #D = B*B - 4.0*A*C #if D >= 0.0 : # x1 = (-B + math.sqrt(D)) / (2.0 * A) # x2 = (-B - math.sqrt(D)) / (2.0 * A) # # print x1, x2 # #else : # print u'No soution' # #print u'END'
def test_24(self): eqn = '-12x+18=-30+12x' self.assertEqual(solve(eqn), 2)
def agile(variants_to_consider): for parameter_set in variants_to_consider: equation.solve(parameter_set['E'], parameter_set['L'], parameter_set['dx']) equation.report_filename='' equation.iteration=0 equation.dump_result()
def test_4(self): eqn = '-3=2x' self.assertEqual(solve(eqn), -1.5)
def test_5(self): eqn = '-2x=-3' self.assertEqual(solve(eqn), 1.5)
def test_15(self): eqn = '-2x+1=-0' self.assertEqual(solve(eqn), 0.5)
def test_6(self): eqn = '-3=-2x' self.assertEqual(solve(eqn), 1.5)
def test_solve(self): self.assertEqual(eq.solve([0, 10, 2]), 1) self.assertEqual(eq.solve([0, 10, 0]), 1) self.assertEqual(eq.solve([0, 0, 10]), 0) self.assertEqual(eq.solve([1, 10000, 2]), 2) self.assertEqual(eq.solve([3, 4, 5]), 0)
def test_17(self): eqn = '-0=2x-4' self.assertEqual(solve(eqn), 2)
def test_18(self): eqn = '2x+3=2x+3' self.assertEqual(solve(eqn), 'NO')
def test_19(self): eqn = '-2x+2=-2x+3' self.assertEqual(solve(eqn), 'NO')
def test_solve(): assert equation.solve(2, 1, 2) == 0 assert equation.solve(0, 1, 2) == 1
def test_27(self): eqn = '3x-4+4x-3=0' self.assertEqual(solve(eqn), 1)