简体   繁体   English

如何在 Python 中计算二次方程

[英]How to calculate a quadratic equation in Python

I want to do some simple calculation with variables.我想用变量做一些简单的计算。 But this x is not variable on often seen in programs, but x = unknown in math formula.但是这个x不是程序中经常看到的变量,而是x = unknown

I want calculate this我要计算这个

(x-1)^2 =

and get并得到

x^2 - 4x + 4

is it possible??可能吗??

I am familiar with some program language, but its first time to use math purpose.我熟悉一些程序语言,但它是第一次使用数学目的。

As per Chen Guevara's comment here is a tiny snippet of sympy -using code to illustrate.根据 Chen Guevara 的评论,这里是一小段 sympy - 使用代码来说明。

from sympy import symbols, expand

x = symbols('x')
expr = (x-1)**2
print(expr)
expr2 = expand(expr)
print(expr2)

This produces the output:这将产生 output:

(x - 1)**2
x**2 - 2*x + 1

x = 8

quadratic = ((x ** 2) - (4 * x)) + 4
print(quadratic)

returns 36. is this what youre looking for?返回 36. 这是你要找的吗?

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

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