Simplest way to solve mathematical equations in Python

后端 未结 15 1682
自闭症患者
自闭症患者 2020-12-13 10:15

I want to solve a set of equations, linear, or sometimes quadratic. I don\'t have a specific problem, but often, I have been in this situation often.

It is simple to

15条回答
  •  天涯浪人
    2020-12-13 10:29

    For reference: Wolfram Alpha's solution:

    a-1000!=0,   b = (1000 (a-500))/(a-1000),   c = (-a^2+1000 a-500000)/(a-1000)
    

    In python, using sympy's solver module (note that it assumes all equations are set equal to zero):

    >>> import sympy
    >>> a, b, c = sympy.symbols('a, b, c')
    >>> sympy.solve([a + b + c - 1000, a**2 + b**2 - c**2], b, c)
    [(1000*(a - 500)/(a - 1000), (-a**2 + 1000*a - 500000)/(a - 1000))]
    

    And of course, a != 1000, as a-1000 is the denominator of the two equations.

提交回复
热议问题