numpy 中的方程组

System of Equations in numpy

因此,我正在创建一个线性方程组计算器。我希望用户输入系统中的方程数和每个方程中的变量数,然后 return 求解变量。我的代码如下:


    
def system_solve(y = None,c = None):
    import numpy as np
    
    z = []
    lefty = []
    for x in range(y):
        d = []
        for e in range(c):
            s = int(input(f"Constant {e + 1} of equation {y}: "))
            d.append(s)
        z.append(d)
        g = int(input(f'Sum of equation {x + 1}: '))
        lefty.append(g)
    

    right = np.array(z)
    left = np.array(lefty)
    return np.linalg.solve(right, left)

print(system_solve(int(input("# of Equations:")), int(input("Number of variables:"))))

当我在 Jupyter Notebook 中 运行 这段代码时,我得到这个错误:

---------------------------------------------------------------------------
LinAlgError                               Traceback (most recent call last)
<ipython-input-25-378b544e77a6> in <module>
     20     return np.linalg.solve(right, left)
     21 
---> 22 print(system_solve(int(input("# of Equations:")), int(input("Number of variables:"))))
     23 

<ipython-input-25-378b544e77a6> in system_solve(y, c)
     18     right = np.array(z)
     19     left = np.array(lefty)
---> 20     return np.linalg.solve(right, left)
     21 
     22 print(system_solve(int(input("# of Equations:")), int(input("Number of variables:"))))

<__array_function__ internals> in solve(*args, **kwargs)

C:\ProgramData\Anaconda3\lib\site-packages\numpy\linalg\linalg.py in solve(a, b)
    397     signature = 'DD->D' if isComplexType(t) else 'dd->d'
    398     extobj = get_linalg_error_extobj(_raise_linalgerror_singular)
--> 399     r = gufunc(a, b, signature=signature, extobj=extobj)
    400 
    401     return wrap(r.astype(result_t, copy=False))

C:\ProgramData\Anaconda3\lib\site-packages\numpy\linalg\linalg.py in _raise_linalgerror_singular(err, flag)
     95 
     96 def _raise_linalgerror_singular(err, flag):
---> 97     raise LinAlgError("Singular matrix")
     98 
     99 def _raise_linalgerror_nonposdef(err, flag):

LinAlgError: Singular matrix

The input is something like this:
3
3
Constant 1 of equation 3: 4
Constant 2 of equation 3: 5
Constant 3 of equation 3: 3
Sum of equation 1 4
Constant 1 of equation 3: 5
Constant 2 of equation 3: 6
Constant 3 of equation 3: 7
Sum of equation 2 8
Constant 1 of equation 3: 9
Constant 2 of equation 3: 10
Constant 3 of equation 3: 11
Sum of equation 3 0

而“右”和“左”两个数组如下:

Right:
[[ 4  5  3]
 [ 5  6  7]
 [ 9 10 11]]
Left:
 [4 8 0]

能否建议修改代码并帮助我修复此错误?

已解决。

#Take input and solve system of equations
def system_solve(y = None,c = None):
    import numpy as np
    
    z = []
    lefty = []
    for x in range(y):
        d = []
        for e in range(c):
            s = int(input(f"Constant {e + 1} of equation {y}: "))
            d.append(s)
        z.append(d)
        g = int(input(f'Sum of equation {x + 1}: '))
        lefty.append(g)
    
    
    right = np.array(z)
    left = np.array(lefty)
    try:
    
        return np.linalg.solve(right, left)
    except np.linalg.LinAlgError as err:
        if 'Singular matrix' in str(err):
            return "Cannot Have a singular martix"
        else:
            return str(err)
print(system_solve(int(input("# of Equations:")), int(input("# of variables:"))))


错误是我给了矩阵一个奇异值,这意味着系统会有无限的解决方案。