Пример #1
0
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])
Пример #2
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
Пример #3
0
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")
Пример #4
0
    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())
Пример #5
0
 def test_20(self):
     eqn = '-2x+3=2x+3'
     self.assertEqual(solve(eqn), 0)
Пример #6
0
 def test_21(self):
     eqn = '3-2x=-1'
     self.assertEqual(solve(eqn), 2)
Пример #7
0
 def test_7(self):
     eqn = 'x=-x'
     self.assertEqual(solve(eqn), 0)
Пример #8
0
 def test_3(self):
     eqn = '3=2x'
     self.assertEqual(solve(eqn), 1.5)
Пример #9
0
        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]
Пример #10
0
	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]
Пример #11
0
 def test_1(self):
     eqn = '2x=3'
     self.assertEqual(solve(eqn), 1.5)
Пример #12
0
 def test_23(self):
     eqn = '-2x+3=2x-3'
     self.assertEqual(solve(eqn), 1.5)
Пример #13
0
 def test_11(self):
     eqn = '2x=3x'
     self.assertEqual(solve(eqn), 0)
Пример #14
0
 def test_12(self):
     eqn = '0=0'
     self.assertEqual(solve(eqn), 'NO')
Пример #15
0
 def test_10(self):
     eqn = '2=3'
     self.assertEqual(solve(eqn), 'NO')
Пример #16
0
 def test_9(self):
     eqn = '2=2'
     self.assertEqual(solve(eqn), 'NO')
Пример #17
0
 def test_8(self):
     eqn = 'x=x'
     self.assertEqual(solve(eqn), 'NO')
Пример #18
0
 def test_22(self):
     eqn = '-3+2x=4x-1'
     self.assertEqual(solve(eqn), -1)
Пример #19
0
 def test_13(self):
     eqn = '2x+1=0'
     self.assertEqual(solve(eqn), -0.5)
Пример #20
0
 def test_2(self):
     eqn = '-2x=3'
     self.assertEqual(solve(eqn), -1.5)
Пример #21
0
#!/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'
Пример #22
0
 def test_24(self):
     eqn = '-12x+18=-30+12x'
     self.assertEqual(solve(eqn), 2)
Пример #23
0
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()
Пример #24
0
 def test_4(self):
     eqn = '-3=2x'
     self.assertEqual(solve(eqn), -1.5)
Пример #25
0
 def test_5(self):
     eqn = '-2x=-3'
     self.assertEqual(solve(eqn), 1.5)
Пример #26
0
 def test_15(self):
     eqn = '-2x+1=-0'
     self.assertEqual(solve(eqn), 0.5)
Пример #27
0
 def test_6(self):
     eqn = '-3=-2x'
     self.assertEqual(solve(eqn), 1.5)
Пример #28
0
 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)
Пример #29
0
 def test_17(self):
     eqn = '-0=2x-4'
     self.assertEqual(solve(eqn), 2)
Пример #30
0
 def test_18(self):
     eqn = '2x+3=2x+3'
     self.assertEqual(solve(eqn), 'NO')
Пример #31
0
 def test_19(self):
     eqn = '-2x+2=-2x+3'
     self.assertEqual(solve(eqn), 'NO')
Пример #32
0
def test_solve():
    assert equation.solve(2, 1, 2) == 0
    assert equation.solve(0, 1, 2) == 1
Пример #33
0
 def test_27(self):
     eqn = '3x-4+4x-3=0'
     self.assertEqual(solve(eqn), 1)