numpy.linalg.det

这个函数用于计算矩阵的行列式,计算输入的矩阵a的行列式

它要求你输入要计算的行列式的矩阵,这个输入形状要求最后的两个维度相等,并且返回形状为N的行列式,你需要保证你输入的矩阵形状最后的两个维度

相等,否则将引发错误。

它的函数说明如下

linalg.det(a)

计算矩阵a的行列式

Parameters
    a(N, M, M) array_like
    Input array to compute determinants for.
    要计算的行列式的矩阵,这个输入形状要求最后的两个维度相等

Returns
    det(N) array_like
    返回形状为N的行列式
    

LinAlgError: Last 2 dimensions of the array must be square 
有时候会发生这个错误,这个错误表示最后两个维度应当相等

下面给出了关于一个4阶的单位矩阵以及一个形状为2x2的矩阵的例子,

使用det计算他们的行列式,返回的数据类型是浮点数类型

import numpy as np
a = np.arange(4).reshape(2,2)
e = np.eye(4)

a
array([[0, 1],
       [2, 3]])

e
array([[1., 0., 0., 0.],
       [0., 1., 0., 0.],
       [0., 0., 1., 0.],
       [0., 0., 0., 1.]])

np.linalg.det(e)
1.0

np.linalg.det(a)
-2.0

 numpy.linalg.solve(a,b)

这个函数用于解方程组,

函数的各参数说明如下。


linalg.solve(a, b)
    Solve a linear matrix equation, or system of linear scalar equations.
    求解线性方程组,或系统的线性常量等式

Computes the “exact” solution, x, of the well-determined, i.e., full rank, linear matrix equation ax = b.

Parameters
a(…, M, M) array_like
    Coefficient matrix.
    系数方程组,最后的两个维度应当相等

b{(…, M,), (…, M, K)}, array_like
    Ordinate or “dependent variable” values.
    因变量

Returns
    x{(…, M,), (…, M, K)} ndarray
    Solution to the system a x = b. Returned shape is identical to b.

Raises
    LinAlgError
    If a is singular or not square.

再上面的说明中可以看到,当输入的矩阵是奇异即不可逆的或最后两个维度不等时,将会触发LinAlgError.

下面给出一些方程式,用solve进行求解:

 

a = np.array([[1, 2], [3, 5]]) 
b = np.array([0,0])

上面两个变量表示一个齐次线性方程组
 x0     + 2 * x1 = 0 
 3 * x0 + 5 * x1 = 0:

 

np.linalg.solve(a,b)
array([0., 0.])

它的解都为0

 

a = np.array([[1, 2], [3, 5]]) 
b = np.array([1,2])

 这表示一个非齐次线性方程组,
 x0     + 2 * x1 = 1 
 3 * x0 + 5 * x1 = 2:

np.linalg.solve(a,b)
array([-1.,  1.])

 一个解为-1,一个解为1

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐