简体   繁体   English

如何在 sympy gamma 中计算 x^2

[英]How to calculate x^2 in sympy gamma

I am very new to coding in Python and using the django.我对使用 Python 和使用 django 编码非常陌生。

My question: is it possible to calculate x^2 in sympy gamma?In my program x**2 is working but x^2 is not working.我的问题:是否可以在 sympy gamma 中计算 x^2?在我的程序中 x**2 工作但 x^2 不工作。

^ is not working. ^不工作。

my code is-我的代码是-

from django.shortcuts import render, redirect
from django.http import HttpResponse
from sympy import *
from sympy.parsing.sympy_parser import *
import sympy as sp

# Create your views here.

def index(request):

    if request.method == "POST":

        x = symbols('x')
        init_printing()
        transformations = (standard_transformations + (implicit_multiplication_application,))
        eq = parse_expr(request.POST['equ'], transformations=transformations)       
        sympifyy = latex(sympify(eq, evaluate=False))
        sympifyy1 = Eq(eq)
        derivative = latex(sp._eval_derivative(eq,x))
        integration = latex(sp.integrate(eq, x))
        # integration = integrate(eq, x)
        # pretty(latex(Integral(eq, x)),use_unicode=False)
        # print(pretty(Integral(sqrt(1/x), x), use_unicode=False))
        rootss = solve(eq)
        limits = limit(eq, x, 0)        
        seriess = latex(series(eq, x, 0, 10))

        data = {
            'Sympify' : sympifyy,
            'Sympify1' : sympifyy1,
            'Derivative' : derivative,
            'Integration' : integration,
            'Roots' : rootss, 
            'Limit' : limits,
            'Series' : seriess
            }

        return render(request, 'index.html', {'data':data})

    return render(request, 'index.html')

Ex-when user gives input is working **(x 2+x+2)** but user give input (x^2+x+2) then error occurs Ex-当用户提供输入时 **(x 2+x+2)** 但用户提供输入(x^2+x+2)时会发生错误

The answer seems quite simple.答案似乎很简单。 Just .replace("^", "**") your incoming equation.只需.replace("^", "**")你传入的方程。

eqn = request.POST['equ'].replace("^", "**")
eq = parse_expr(eqn, transformations=transformations)

Additionally, I think there's a mistake in your code, should it be:此外,我认为您的代码中存在错误,应该是:

derivative = latex(sp.diff(eq,x))

As described in SymPy's online documentation SymPy follows the Python standard, where ^ is used for exclusive or (Xor).如 SymPy 的在线文档中所述,SymPy 遵循 Python 标准,其中^用于异或(Xor)。

So, your statement that ^ is not working, is inaccurate.因此,您关于^不起作用的说法是不准确的。 It only does something else as what you imagined it would do.它只会做你想象中的其他事情。 Xor is a quite nasty function, as it is only defined for booleans, not for unlimited integers nor for non-integer types of numbers. Xor 是一个非常讨厌的 function,因为它只为布尔值定义,而不是无限整数或非整数类型的数字。

Therefore, in your call to parse_expr you might want to add convert_xor to the list of transformations, as in因此,在调用parse_expr时,您可能希望将convert_xor添加到转换列表中,如

transformations = standard_transformations + (implicit_multiplication_application,) + (convert_xor,)

This function explicitly transforms ^ to ** .此 function 将^显式转换为**

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM