Is there a python module to solve linear equations?

后端 未结 6 962
生来不讨喜
生来不讨喜 2020-11-27 16:32

I want to solve a linear equation with three or more variables. Is there a good library in python to do it?

6条回答
  •  天涯浪人
    2020-11-27 17:16

    Using @Jeremy's example:

    from sympy import *
    x0, x1 = symbols(['x0', 'x1'])
    sol = solve([3 * x0 + x1 - 9, x0 + 2 * x1 - 8], [x0, x1])
    print(sol)
    

    output:

    {x0: 2, x1: 3}

    Using @004 example with slightly different notation:

    from sympy import *
    x, y = symbols(['x', 'y'])
    system = [
        Eq(3*x + 4*y, 7),
        Eq(5*x + 6*y, 8)
    ]
    soln = solve(system, [x, y])
    print(soln)
    

    {x: -5, y: 11/2}

    Note: Sometimes one may see the following notation for symbols: x, y = symbols('x, y'), which seems to be less pythonic.

提交回复
热议问题