def Expand(formula): try: A = expand(formula) anser = LATEX(formula) + "=" + LATEX(A) except: anser = "Error" flash("エラー:もう一度入力してください") return anser
def factorization(formula): try: A = factor(formula) anser = LATEX(formula) + " = " + LATEX(A) except: anser = "Error" flash("エラー:もう一度入力してください") return anser
def lim(formula, a): try: A = limit(formula, x, sympify(a)) anser = "\lim_{x \\to "+str(a)+"}"+LATEX(formula)+"="+LATEX(A) except: anser = "Error" flash("エラー:もう一度関数を入力してください") return anser
def Factorial(formula): try: A = math.factorial(int(formula)) anser = LATEX(formula)+"! = "+LATEX(A) except: anser = "Error" flash("エラー:もう一度入力してください") return anser
def Apart(formula): try: x = Symbol('x') anser = apart(formula) anser = LATEX(formula) + "=" + LATEX(anser) except: anser = "Error" flash("エラー:もう一度入力してください") return anser
def calculation(matrixA, matrixB, type, k, l): try: k, l = [int(k), int(l)] A, Ar, Ac = MATRIX(matrixA) B, Br, Bc = MATRIX(matrixB) if type == "A": anser = LATEX(A) elif type == "B": anser = LATEX(B) elif type == "kA+lB": anser = LATEX(k * A + l * B) type = str(k) + "A+" + str(l) + "B" elif type == "AB": anser = LATEX(A * B) elif type == "BA": anser = LATEX(B * A) elif type == "A・B(内積)": anser = LATEX(B.dot(A)) elif type == "A×B(外積)": anser = LATEX(A.cross(B)) elif type == "B×A(外積)": anser = LATEX(B.cross(A)) anser = str(type) + "=" + anser except: anser = "Error" flash("エラー:もう一度入力してください") return anser
def latex(formula): try: anser = LATEX(formula) return anser except: flash("エラー:もう一度入力してください") return "Error"
def nyquist(formula): s = symbols('s') formula = sympify(formula) formula_2 = lambdify(s, formula, "numpy") w_list_1 = np.array([-10**(i/100) for i in range(-3000, 3000, 1)]) w_list_2 = np.array([10**(i/100) for i in range(-3000, 3000, 1)]) w_list = np.hstack((w_list_1, w_list_2)) formula_3 = formula_2(1j*w_list) deg = np.linspace(0, 2*np.pi, 100) circle_x = np.cos(deg) circle_y = np.sin(deg) fig = plt.figure(figsize=(7, 4)) plt.title("$G(s)="+LATEX(formula)+"$") plt.plot(np.real(formula_3), np.imag(formula_3)) plt.plot(circle_x, circle_y, label="0dB") plt.plot(-1, 0, marker="x", color="red", label="s=-1+0j") plt.axhline(y=0, color="black") plt.axvline(x=0, color="black") plt.legend() # canvasにプロットした画像を出力 canvas = FigureCanvasAgg(fig) png_output = BytesIO() canvas.print_png(png_output) data = png_output.getvalue() # HTML側に渡すレスポンスを生成する response = make_response(data) response.headers['Content-Type'] = 'image/png' response.headers['Content-Length'] = len(data) return response
def diff_equation(formula): try: x = Symbol('x') y = Function('y') formula = sympify(formula) A = dsolve(formula) Anser = [LATEX(formula) + "=0"] if (type(A) is list): for i in range(len(A)): Anser.append(LATEX(A[i])) else: Anser.append(LATEX(A)) except: Anser = ["Error"] flash("エラー:もう一度入力してください") return Anser
def bode(formula, lower_end, upper_end): s = symbols('s') formula = sympify(formula) formula_2 = lambdify(s, formula, "numpy") title = "" width = 100 fig = plt.figure() ax1 = fig.add_subplot(2, 1, 1) ax2 = fig.add_subplot(2, 1, 2) # データ作成 w_list = np.array([10**(i/width) for i in range(int(lower_end)*width, int(upper_end)*width, 1)]) g_list = 20*np.log10(np.abs(formula_2(1j*w_list))) φ_list = np.rad2deg(np.angle(formula_2(1j*w_list))) φ_list[np.where(φ_list > 0)] = φ_list[np.where(φ_list > 0)]-360 tmpWc = np.average(w_list[np.where(abs(g_list) < 5)]) if(str(tmpWc) != "nan"): w_list_c = np.array([i/100 for i in range(int((tmpWc-0.3)*100), int((tmpWc+0.3)*100), 1)]) g_list_c = 20*np.log10(np.abs(formula_2(1j*w_list_c))) Wc = w_list_c[np.argmin(np.abs(g_list_c))] Pm = 180+np.rad2deg(np.angle(formula_2(1j*Wc))) ax1.axvline(x=Wc, color="black") ax2.axvline(x=Wc, color="black") title += "Wc="+str(round(Wc, 2))+"rad/s, Pm="+str(round(Pm, 2))+"deg, " tmpWp = np.average(w_list[np.where((-190 < φ_list) & (φ_list < -170))]) if(str(tmpWp) != "nan"): w_list_p = np.array([i/100 for i in range(int((tmpWp-0.3)*100), int((tmpWp+0.3)*100), 1)]) φ_list_p = np.rad2deg(np.angle(formula_2(1j*w_list_p))) φ_list_p[np.where(φ_list_p > 0)] = φ_list_p[np.where(φ_list_p > 0)]-360 Wp = w_list_p[np.argmin(np.abs(180+φ_list_p))] Gm = -20*np.log10(np.abs(formula_2(1j*Wp))) ax1.axvline(x=Wp, color="black") ax2.axvline(x=Wp, color="black") title += "Wπ="+str(round(Wp, 2))+"rad/s, Gm="+str(round(Gm, 2))+"dB, " ax1.plot(w_list, g_list) ax2.plot(w_list, φ_list) ax1.set_xscale("log") ax2.set_xscale("log") ax1.axhline(y=0, color="black") ax2.axhline(y=-180, color="black") ax1.set_title("$G(s)="+LATEX(formula)+"$") plt.title(title, y=-0.30) # canvasにプロットした画像を出力 canvas = FigureCanvasAgg(fig) png_output = BytesIO() canvas.print_png(png_output) data = png_output.getvalue() # HTML側に渡すレスポンスを生成する response = make_response(data) response.headers['Content-Type'] = 'image/png' response.headers['Content-Length'] = len(data) return response
def equations(formula, number): try: A = solve(formula) Anser = [] if (number == 1): for i in range(len(A)): a = A[i] for B in a.items(): anser = LATEX(B[0]) + "=" + LATEX(B[1]) Anser.append(anser) else: for B in A.items(): anser = LATEX(B[0]) + " = " + LATEX(B[1]) Anser.append(anser) except: Anser = ["Error"] flash("エラー:もう一度入力してください") return Anser
def laplace(formula, type): try: formula = simplify(formula) if (type == "lap"): Anser = laplace_transform(formula, t, s) anser = str(Anser[0]).replace("Heaviside(", "u_s(") formula = str(formula).replace("Heaviside(", "u_s(") return "\mathcal{L}[" + LATEX(formula) + "]=" + LATEX(anser) elif (type == "inv"): anser = inverse_laplace_transform(formula, s, t) anser = str(anser).replace("Heaviside(", "u_s(") formula = str(formula).replace("Heaviside(", "u_s(") return "\mathcal{L}^{-1}[" + LATEX(formula) + "]=" + LATEX(anser) else: flash("type エラー:もう一度入力してください") return "Error" except: flash("エラー:もう一度入力してください") return "Error"
def taylor(formula, dimension, center): try: x = Symbol('x') f = sympify(formula) center = float(center) A = f.subs(x, center) for number in range(1, int(dimension) + 1, 1): f = diff(f) D = f.subs(x, center) / factorial(number) A += D * (x - center)**number anser_1 = "f(x)=" + LATEX(formula) anser_2 = "f(x)≒" + LATEX(A) Anser = [anser_1, anser_2] except: Anser = ["Error", ""] flash("エラー:もう一度入力してください") return Anser
def sysio_matrix(matrix_A, matrix_B, matrix_C, matrix_D, matrix_X, formula, lower_end, upper_end, type): A, Ar, Ac = MATRIX(matrix_A) B, Br, Bc = MATRIX(matrix_B) C, Cr, Cc = MATRIX(matrix_C) D, Dr, Dc = MATRIX(matrix_D) X, Xr, Xc = MATRIX(matrix_X) formula = simplify(formula) lower_end, upper_end = [float(lower_end), float(upper_end)] inverse = (s * eye(Ar) - A).inv() matrix_T = inverse_laplace_transform(inverse, s, t) print(matrix_T) anser_1 = matrix_T * X Integrand = (matrix_T.subs(t, t - τ)) * B * formula T = integrate(Integrand, τ) anser_2 = T.subs(τ, t) - T.subs(τ, 0) anser = LATEX(anser_1 + anser_2) title = str(factor(anser)).replace("Heaviside(", "u_s(") # データ作成 T = np.linspace(lower_end, upper_end, num) Y = np.array([anser.subs(t, T[i]) for i in range(len(T))]) fig = plt.figure(figsize=(7, 4)) plt.plot(T, Y) plt.xlim(lower_end, upper_end) plt.title("$y(t)=" + LATEX(title) + "(" + str(lower_end) + "<t<" + str(upper_end) + ")$") # canvasにプロットした画像を出力 canvas = FigureCanvasAgg(fig) png_output = BytesIO() canvas.print_png(png_output) data = png_output.getvalue() # HTML側に渡すレスポンスを生成する response = make_response(data) response.headers['Content-Type'] = 'image/png' response.headers['Content-Length'] = len(data) return response
def equation(formula, type): try: x = Symbol('x') A = solve(formula, dict=True) Anser = ["方程式:" + LATEX(formula) + "=0", ""] if (type == "analytical"): for i in range(len(A)): a = A[i] for B in a.items(): anser = LATEX(B[0]) + "=" + LATEX(B[1]) Anser.append(anser) elif (type == "numerical"): for i in range(len(A)): a = A[i] for B in a.items(): anser = LATEX(B[0]) + "=" + LATEX(B[1].evalf()) Anser.append(anser) except: Anser = ["Error"] flash("エラー:もう一度入力してください") return Anser
def derivative(formula, type): try: x, y, z = symbols('x y z') anser = "f_{" + type + "}=" if (type == "x"): A = diff(formula, x) elif (type == "y"): A = diff(formula, y) elif (type == "z"): A = diff(formula, z) elif (type == "xx"): A = diff(formula, x, x) elif (type == "yy"): A = diff(formula, y, y) elif (type == "zz"): A = diff(formula, z, z) elif (type == "xy"): A = diff(formula, x, y) elif (type == "yz"): A = diff(formula, y, x) elif (type == "zx"): A = diff(formula, z, x) elif (type == "grad"): anser = "\mathrm{grad} f=" A = (diff(formula, x), diff(formula, y), diff(formula, z)) elif (type == "∆"): anser = "\Delta f=" A = (diff(formula, x, x), diff(formula, y, y), diff(formula, z, z)) anser += LATEX(factor(A)) formula = "f\left(x,y,z \\right)=" + LATEX(formula) Anser = [formula, anser] except: Anser = ["Error", ""] flash("エラー:もう一度入力してください") return Anser
def graph(formula_1, lower_end_x, upper_end_x): x, y = symbols('x y') formula_1 = sympify(formula_1) lower_end_x = float(lower_end_x) upper_end_x = float(upper_end_x) fig = plt.figure(figsize=(7, 4)) # データ作成 dx = diff(formula_1, x) dy = diff(formula_1, y) if ((dx == 0) or (dy == 0)): X = np.linspace(lower_end_x, upper_end_x, 300) if ((dx == 0) and (dy == 0)): Y = lambdify(x, formula_1, "numpy")(lower_end_x) * np.ones(300) elif (dy == 0): Y = lambdify(x, formula_1, "numpy")(X) elif (dx == 0): Y = lambdify(y, formula_1, "numpy")(X) ax = fig.add_subplot(111) ax.plot(X, Y) else: s = np.linspace(lower_end_x, upper_end_x, 50) X, Y = np.meshgrid(s, s) Z = lambdify((x, y), formula_1, "numpy")(X, Y) ax = fig.add_subplot(111, projection='3d') ax.plot_surface(X, Y, Z, cmap=cm.coolwarm) ax.set_xlabel('x') ax.set_ylabel('y') plt.title("$f(x)=" + LATEX(formula_1) + "(" + str(lower_end_x) + "<x,y<" + str(upper_end_x) + ")$") # canvasにプロットした画像を出力 canvas = FigureCanvasAgg(fig) png_output = BytesIO() canvas.print_png(png_output) data = png_output.getvalue() # HTML側に渡すレスポンスを生成する response = make_response(data) response.headers['Content-Type'] = 'image/png' response.headers['Content-Length'] = len(data) return response
def sysio(formula, formula_2, lower_end, upper_end, type): formula = simplify(formula) formula_2 = simplify(formula_2) lower_end = float(lower_end) upper_end = float(upper_end) if (type == "s"): output = apart(formula * formula_2) anser = inverse_laplace_transform(output, s, t) else: g = simplify(formula).subs(t, t - τ) u = simplify(formula_2).subs(t, τ) y = integrate(g * u, τ) anser = y.subs(τ, t) - y.subs(τ, 0) anser = str(anser).replace("Heaviside(0)", "0") anser = simplify(anser) title = str(factor(anser)).replace("Heaviside(", "u_s(") # データ作成 T = np.linspace(lower_end, upper_end, num) Y = np.array([anser.subs(t, T[i]) for i in range(len(T))]) fig = plt.figure(figsize=(7, 4)) plt.plot(T, Y) plt.xlim(lower_end, upper_end) plt.title("$y(t)=" + LATEX(title) + "(" + str(lower_end) + "<t<" + str(upper_end) + ")$") # canvasにプロットした画像を出力 canvas = FigureCanvasAgg(fig) png_output = BytesIO() canvas.print_png(png_output) data = png_output.getvalue() # HTML側に渡すレスポンスを生成する response = make_response(data) response.headers['Content-Type'] = 'image/png' response.headers['Content-Length'] = len(data) return response
def max_min(formula): try: x, y = symbols('x y') formula = sympify(formula) f_x = diff(formula, x) f_y = diff(formula, y) f_xx = diff(formula, x, x) f_yy = diff(formula, y, y) f_xy = diff(formula, x, y) if f_x == 0 or f_y == 0: if f_x == 0: var = y det = f_yy A = solve(f_y, dict=True) else: var = x det = f_xx A = solve(f_x, dict=True) Max_Anser = [] Min_Anser = [] for i in range(len(A)): a = A[i] for B in a.items(): b = det.subs(var, B[1]) c = formula.subs(var, B[1]) if b >= 0: anser = "極小値 f("+LATEX(B[1])+") = "+LATEX(c) Min_Anser.append(anser) else: anser = "極大値 f("+LATEX(B[1])+") = "+LATEX(c) Max_Anser.append(anser) Anser = ["f("+LATEX(var)+")="+LATEX(formula)]+Max_Anser+Min_Anser else: A = solve([f_x, f_y]) B = [] if type(A) == list: for i in range(len(A)): a = A[i] D = [] for C in a.items(): D.append(C[1]) B.append(D) else: D = [] for C in A.items(): D.append(C[1]) B.append(D) Anser = ["f(x,y)="+LATEX(formula)] for j in range(len(B)): a = B[j][0] b = B[j][1] D = f_xx*f_yy-(f_xy)**2 D = D.subs([(x, a), (y, b)]) f_xx = f_xx.subs([(x, a), (y, b)]) if D > 0: if f_xx > 0: anser = "極小値 f("+LATEX(a)+","+LATEX(b)+")=" + \ LATEX(formula.subs([(x, a), (y, b)])) else: anser = "極大値 f("+LATEX(a)+","+LATEX(b)+")=" + \ LATEX(formula.subs([(x, a), (y, b)])) elif D < 0: anser = "点("+LATEX(a)+","+LATEX(b)+")で極値をとらない" else: anser = "点("+LATEX(a)+","+LATEX(b)+")での極値は判別できない" Anser.append(anser) if len(Anser) == 1: Anser.append("極値をとらない") except: Anser = ["Error"] flash("エラー:もう一度入力してください") return Anser
def integral(formula, Up, Low, type): try: x, y = symbols('x y') if (type == "multiple_integral_1" or type == "multiple_integral_2"): A = integrate(formula, (x, Low[0], Up[0]), (y, Low[1], Up[1])) anser = "\int_{" + LATEX(Low[1]) + "}^{" + LATEX( Up[1]) + "} \int_{" + LATEX(Low[0]) + "}^{" + LATEX( Up[0]) + "}" + LATEX(formula) + "dxdy=" if (type == "multiple_integral_1"): anser += LATEX(A) elif (type == "multiple_integral_2"): anser += LATEX(A.evalf()) else: if (type == "indefinite_integral"): A = integrate(formula, x) anser = "\int " + LATEX(formula) + "dx=" + LATEX(A) + "+C" else: A = integrate(formula, (x, Low[0], Up[0])) anser = "\int_{" + LATEX(Low[0]) + "}^{" + LATEX( Up[0]) + "}" + LATEX(formula) + "dx=" if (type == "definite_integral_1"): anser += LATEX(A) elif (type == "definite_integral_2"): anser += LATEX(A.evalf()) except: anser = "Error" flash("エラー:もう一度入力してください") return anser
def calculation(matrixA, type): try: A, Ar, Ac = MATRIX(matrixA) if type == "A": anser = "A="+LATEX(A) elif type == "A^n": if(Ar == Ac): P, D = list(A.diagonalize()) for i in range(Ac): D[i, i] = "("+str(D[i, i])+")^n" anser = "A^n="+LATEX(P*D*P.inv()) else: flash("Error:正方行列を入力してください") anser = "正方行列を入力してください" elif type == "A^{-1}": if(Ar == Ac): anser = "A^{-1}="+LATEX(A.inv()) else: flash("Error:正方行列を入力してください") anser = "正方行列を入力してください" elif type == "A^t": anser = "A^T="+LATEX(A.transpose()) elif type == "\widetilde{A}": if(Ar == Ac): anser = "\widetilde{A}="+LATEX(A.adjugate()) else: flash("Error:正方行列を入力してください") anser = "正方行列を入力してください" elif type == "det(A)": if(Ar == Ac): anser = "det(A)="+LATEX(A.det()) else: flash("Error:正方行列を入力してください") anser = "正方行列を入力してください" elif type == "rank(A)": anser = "rank(A)="+LATEX(A.rank()) elif type == "tr(A)": if(Ar == Ac): anser = "tr(A)="+LATEX(A.trace()) else: flash("Error:正方行列を入力してください") anser = "正方行列を入力してください" elif type == "λ": if(Ar == Ac): A = A.eigenvals() anser = "" for B in A.items(): anser += ("\lambda="+LATEX(B[0])+"(n="+LATEX(B[1])+"), ") else: flash("Error:正方行列を入力してください") anser = "正方行列を入力してください" elif type == "P": if(Ar == Ac): A = list(A.diagonalize()) anser = "P="+LATEX(A[0]) else: flash("Error:正方行列を入力してください") anser = "正方行列を入力してください" elif type == "P^{-1}AP": if(Ar == Ac): A = list(A.diagonalize()) anser = "P^{-1}AP="+LATEX(A[1]) else: flash("Error:正方行列を入力してください") anser = "正方行列を入力してください" elif type == "Φ(t)": if(Ar == Ac): anser = "Φ(t)="+LATEX((s*eye(Ar)-A).inv()) else: flash("Error:正方行列を入力してください") anser = "正方行列を入力してください" except: anser = "Error" flash("エラー:もう一度入力してください") return anser
def calculation(matrixA, Ar, Ac, type): try: Ar, Ac = [int(Ar), int(Ac)] A = MATRIX(matrixA, Ar, Ac) if type == "A": anser = LATEX(A) elif type == "A^n": A = list(A.diagonalize()) P = A[0] D = A[1] for i in range(0, Ac, 1): D[i, i] = "(" + str(D[i, i]) + ")^n" anser = LATEX(P * D * P.inv()) elif type == "A^t": anser = LATEX(A.transpose()) elif type == "A^{-1}": anser = LATEX(A.inv()) elif type == "\widetilde{A}": anser = LATEX(A.adjugate()) elif type == "det(A)": anser = LATEX(A.det()) elif type == "rank(A)": anser = LATEX(A.rank()) elif type == "tr(A)": anser = LATEX(A.trace()) elif type == "λ": A = A.eigenvals() anser = "" for B in A.items(): anser += LATEX(B[0]) + "(n=" + LATEX(B[1]) + "), " elif type == "P": A = A.diagonalize() A = list(A) anser = LATEX(A[0]) elif type == "P^{-1}AP": A = A.diagonalize() A = list(A) anser = LATEX(A[1]) except: anser = "Error" flash("エラー:もう一度入力してください") return anser