def Degree_1(self): if self.a == 0: self.result.append("No Solution.") else: self.eqn = Generate_EQN(self.a, self.b) x = strfNumber(-self.b / self.a) self.result.append(x)
def Generate_EQN(*num): result = '' for i in range(len(num) - 1, -1, -1): if i == len(num) - 1: if i != 1: if num[0] == 1: result += 'x^{' + str(i) + '}' elif num[0] == -1: result += '-x^{' + str(i) + '}' else: result += strfNumber(num[0]) + 'x^{' + str(i) + '}' else: if num[-2] == 1: result += 'x' elif num[-2] == -1: result += '-x' elif num[-2] > 0: result += strfNumber(num[-2]) + 'x' elif num[len(num) - 1 - i] < 0: result += '-' + strfNumber(abs(num[-2])) + 'x' elif i == 1 and i != len(num) - 1: if num[-2] == 1: result += ' + x' elif num[-2] == -1: result += ' - x' elif num[-2] > 0: result += ' + ' + strfNumber(num[-2]) + 'x' elif num[len(num) - 1 - i] < 0: result += ' - ' + strfNumber(abs(num[-2])) + 'x' elif i == 0: if num[-1] > 0: result += ' + ' + strfNumber(num[-1]) elif num[-1] < 0: result += ' - ' + strfNumber(abs(num[-1])) else: if num[len(num) - 1 - i] == 1: result += ' + ' + 'x^{' + str(i) + '}' elif num[len(num) - 1 - i] == -1: result += ' - ' + 'x^{' + str(i) + '}' elif num[len(num) - 1 - i] > 0: result += ' + ' + strfNumber( num[len(num) - 1 - i]) + 'x^{' + str(i) + '}' elif num[len(num) - 1 - i] < 0: result += ' - ' + strfNumber(abs( num[len(num) - 1 - i])) + 'x^{' + str(i) + '}' return result + ' = 0'
def Degree_5(self): if self.a == 0: Polynomial(self.b, self.c, self.d, self.e, self.f, 0).Degree_4() else: self.Generate_EQN(self.a, self.b, self.c, self.d, self.e, self.f) self.eqn = f'{self.a}x^5 + {self.b}x^4 + {self.c}x^3 + {self.d}x^2 + {self.e}x + {self.f} = 0' solve = Solve(self.eqn, 0) solve.Solve() self.result.append(strfNumber(solve.x)) factor = horner([self.a, self.b, self.c, self.d, self.e, self.f], solve.x) rest_x = Polynomial(factor[0], factor[1], factor[2], factor[3], factor[4], 0) rest_x.Degree_4() for x in rest_x.result: self.result.append(x)
def calc(): equation = '' result = '' show = '' if request.method == 'POST': equation = request.form.get('equation') result = ReplaceSymbol(equation, 0) if request.form.get('x') != None: x = eval(request.form.get('x')) result = ReplaceSymbol(equation, 0).replace('x', str(x)) if 'x' in result: return render_template('calc.html', equation = equation, x = '', result = (show, '')) show = ReplaceSymbol(equation, 1) result = strfNumber(eval(result)) return render_template('calc.html', equation = equation, result = (show, result))
def Degree_4(self): if self.a == 0: Polynomial(self.b, self.c, self.d, self.e, 0, 0).Degree_3() else: self.eqn = Generate_EQN(self.a, self.b, self.c, self.d, self.e) # t^4 + at^2 + bt + c = 0 a = (-3 * self.b**2 + 8 * self.a * self.c) / (8 * self.a**2) b = (2 * self.b**3 - 8 * self.a * self.b * self.c + 16 * self.a**2 * self.d) / (16 * self.a**3) c = (-3 * self.b**4 + 16 * self.a * self.b**2 * self.c - 64 * self.a**2 * self.b * self.d + 256 * self.a**3 * self.e) / (256 * self.a**4) solve_m = Polynomial(8, -4 * a, -8 * c, 4 * a * c - b**2, 0, 0) #8m^3 - 4am^2 - 8cm + 4ac - b^2 = 0 solve_m.Degree_3() m = eval(solve_m.result[0]) i = 1 while (2 * m - a) == 0 and i < len(solve_m.result): m = eval(solve_m.result[i]) i += 1 list_t1 = Polynomial(1, -(2 * m - a)**0.5, m + b / (2 * (2 * m - a)**0.5), 0, 0, 0) list_t2 = Polynomial(1, (2 * m - a)**0.5, m - b / (2 * (2 * m - a)**0.5), 0, 0, 0) list_t = [] for j in [list_t1, list_t2]: j.Degree_2() for i in j.result: list_t.append(str(i)) for i in list_t: if 'i' not in i: x = round(eval(i) - self.b / (4 * self.a), 14) self.result.append(x) else: classify = i.split(' ') classify[0] = strfNumber( round(eval(classify[0]) - self.b / (4 * self.a), 14)) x = ' '.join(classify) self.result.append(x)
def Degree_2(self): if self.a == 0: self.eqn = Generate_EQN(self.b, self.c) poly = Polynomial(self.b, self.c, 0, 0, 0, 0) poly.Degree_1() self.result = poly.result else: self.eqn = Generate_EQN(self.a, self.b, self.c) common_deno = lcd(frac(self.a), frac(self.b), frac(self.c)) a, b, c = self.a * common_deno, self.b * common_deno, self.c * common_deno if a < 0: a, b, c = -a, -b, -c delta = b**2 - 4 * a * c self.hts = '$$ {' + self.eqn + '} $$' if delta == 0: x = strfNumber(-b / (2 * a)) self.result.append(x) self.hts += r'$$ {\Leftrightarrow (x ' if ( self.a == 1) else r'$$ {\Leftrightarrow ' + strfNumber( frac(self.a)) + '(x ' self.hts += '+' if b * a > 0 else '' self.hts += strfNumber(b / (2 * a)) + ')^{2} = 0} $$' self.hts += r'$$ {\Leftrightarrow x ' self.hts += '+' if b * a > 0 else '' self.hts += strfNumber(b / (2 * a)) + ' = 0} $$' self.hts += r'$$ {\Leftrightarrow x = ' + strfNumber( -b / (2 * a)) + '} $$' else: if delta > 0: if round(int(abs(delta)**0.5) - abs(delta)**0.5, 10) == 0: x1 = strfNumber((-b + (delta)**(1 / 2)) / (2 * a)) x2 = strfNumber((-b - (delta)**(1 / 2)) / (2 * a)) self.hts += r'$$ {\Leftrightarrow (x ' if ( self.a == 1) else r'$$ {\Leftrightarrow ' + strfNumber( self.a) + '(x ' self.hts += '+' if '-' in x1 else '-' self.hts += strfNumber( abs((-b + (delta)**(1 / 2)) / (2 * a))) + ')(x ' self.hts += '+' if '-' in x2 else '-' self.hts += strfNumber( abs((-b - (delta)**(1 / 2)) / (2 * a))) + ') = 0} $$' self.hts += r'$$ {\Leftrightarrow \left[ \begin{array}{l} x_{1} = ' + x1 + r' \\ x_{2} =' + x2 + r' \end{array} \right.} $$' else: if common_deno != 1: self.hts += r'$$ {\Leftrightarrow ' + strfNumber( a / self.a) + '(' + self.eqn.split( '=')[0] + ') = 0} $$' self.hts += r'$$ {\Leftrightarrow ' + Generate_EQN( a, b, c) + '} $$' self.hts += r'$$ {\Delta = b^{2} - 4ac = ' + strfNumber( delta) + ' > 0} $$' self.hts += r'$$ {x_{1,2} = \frac{-b \pm \sqrt{\Delta}}{2a} = \frac{' + strfNumber( -b) + r'\pm \sqrt{' + strfNumber( delta) + r'}}{' + strfNumber(2 * a) + r'} = ' sgcd = int(SquaredGCD(delta)**0.5) GCD = gcd(gcd(sgcd, 2 * a), b) coefficient = '' if abs(sgcd / GCD) != 1: coefficient = strfNumber(abs(sgcd / GCD)) if GCD == 2 * a: x1 = strfNumber( -b / GCD ) + '+' + coefficient + r'\sqrt{' + strfNumber( delta / (sgcd**2)) + '}' x2 = strfNumber( -b / GCD ) + '-' + coefficient + r'\sqrt{' + strfNumber( delta / (sgcd**2)) + '}' self.hts += strfNumber( -b / GCD ) + r'\pm' + coefficient + r'\sqrt{' + strfNumber( delta / (sgcd**2)) + '}' else: x1 = r'\frac{' + strfNumber( -b / GCD ) + '+' + coefficient + r'\sqrt{' + strfNumber( delta / (sgcd**2)) + '}}{' + strfNumber( 2 * a / GCD) + '}' x2 = r'\frac{' + strfNumber( -b / GCD ) + '-' + coefficient + r'\sqrt{' + strfNumber( delta / (sgcd**2)) + '}}{' + strfNumber( 2 * a / GCD) + '}' self.hts += r'\frac{' + strfNumber( -b / GCD ) + r'\pm' + coefficient + r'\sqrt{' + strfNumber( delta / (sgcd**2)) + '}}{' + strfNumber( 2 * a / GCD) + '}' self.hts += '} $$' else: self.hts += r'$$ {\Delta = b^{2} - 4ac = ' + strfNumber( delta) + ' < 0} $$' self.hts += r'$$ {x_{1,2} = \frac{-b \pm \sqrt{\Delta}}{2a} = \frac{' + strfNumber( -b) + r'\pm \sqrt{' + strfNumber( delta) + r'}}{' + strfNumber(2 * a) + r'} = ' if round(int(abs(delta)**0.5) - abs(delta)**0.5, 10) == 0: imagine = strfNumber(((-delta)**(1 / 2)) / (2 * a)) imagine = '' if imagine == '1' else imagine else: sgcd = int(SquaredGCD(-delta)**0.5) GCD = gcd(sgcd, 2 * a) coefficient = '' if abs(sgcd / GCD) != 1: coefficient = strfNumber(abs(sgcd / GCD)) if GCD == 2 * a: imagine = coefficient + r'\sqrt{' + strfNumber( -delta / (sgcd**2)) + '}' else: imagine = r'\frac{' + coefficient + r'\sqrt{' + strfNumber( -delta / (sgcd**2)) + '}}{' + strfNumber( 2 * a / GCD) + '}' if b != 0: real = strfNumber(-b / (2 * a)) x1 = f"{real} + {imagine}i" x2 = f"{real} - {imagine}i" self.hts += real + r' \pm ' + imagine + 'i} $$ <br><div class="row justify-content-center" style="border:1px solid; margin: 0 60px; border-radius: 10px;"><div style="margin-top:15px;">Assuming </div> <div>$$ i $$</div><div style="margin-top:15px"> is the imaginary unit</div></div>' else: x1 = f"{imagine}i" x2 = f"-{imagine}i" self.hts += r' \pm ' + imagine + 'i} $$ <br><div class="row justify-content-center" style="border:1px solid; margin: 0 60px; border-radius: 10px;"><div style="margin-top:15px;">Assuming </div> <div>$$ i $$</div><div style="margin-top:15px"> is the imaginary unit</div></div>' self.result.append(x1) self.result.append(x2)
def Degree_3(self): if self.a == 0: Polynomial(self.b, self.c, self.d, 0, 0, 0).Degree_2() else: self.eqn = Generate_EQN(self.a, self.b, self.c, self.d) delta = self.b**2 - 3 * self.a * self.c if delta == 0: x = round( (-self.b + cbrt(self.b**3 - 27 * self.a**2 * self.d)) / (3 * self.a), 14) self.result.append(strfNumber(x)) if x != 0: factor = horner([self.a, self.b, self.c, self.d], x) rest_x = Polynomial(factor[0], factor[1], factor[2], 0, 0, 0) rest_x.Degree_2() for x in rest_x.result: self.result.append(x) else: k = (9 * self.a * self.b * self.c - 2 * self.b**3 - 27 * self.a**2 * self.d) / (2 * (abs(delta**3))**0.5) if delta > 0: if abs(k) < 1: x1 = round( (2 * delta**0.5 * cos(acos(k) / 3) - self.b) / (3 * self.a), 14) x2 = round((2 * delta**0.5 * cos( (acos(k) - 2 * self.pi) / 3) - self.b) / (3 * self.a), 14) x3 = round((2 * delta**0.5 * cos( (acos(k) + 2 * self.pi) / 3) - self.b) / (3 * self.a), 14) self.result.append(strfNumber(x1)) self.result.append(strfNumber(x2)) self.result.append(strfNumber(x3)) elif abs(k) == 1: x1 = round((2 * delta**0.5 - self.b) / (3 * self.a), 14) x2 = round((-delta**0.5 - self.b) / (3 * self.a), 14) self.result.append(strfNumber(x1)) self.result.append(strfNumber(x2)) else: x = round((delta**0.5 * abs(k) / (3 * self.a * k)) * (cbrt(abs(k) + (k * k - 1)**0.5) + cbrt(abs(k) - (k * k - 1)**0.5)) - self.b / (3 * self.a), 14) self.result.append(strfNumber(x)) factor = horner([self.a, self.b, self.c, self.d], x) rest_x = Polynomial(factor[0], factor[1], factor[2], 0, 0, 0) rest_x.Degree_2() for x in rest_x.result: self.result.append(x) else: x = round( (abs(delta)**0.5 / (3 * self.a)) * (cbrt(k + (k * k + 1)**0.5) - cbrt((k * k + 1)**0.5 - k)) - self.b / (3 * self.a), 14) self.result.append(strfNumber(x)) factor = horner([self.a, self.b, self.c, self.d], x) rest_x = Polynomial(factor[0], factor[1], factor[2], 0, 0, 0) rest_x.Degree_2() for x in rest_x.result: self.result.append(x)