Python中的eig(a,b)给出错误“接受1个位置参数,但给出2个位置参数". [英] eig(a,b) in Python giving error "takes 1 positional argument but 2 were given"

查看:122
本文介绍了Python中的eig(a,b)给出错误“接受1个位置参数,但给出2个位置参数".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 https://docs.scipy.org/doc/numpy-1.15.0/user/numpy-for-matlab-users.html ,即MATLAB [V,D] = eig(a,b) V,D = np.linalg.eig(a,b).

According to https://docs.scipy.org/doc/numpy-1.15.0/user/numpy-for-matlab-users.html, the equivalent numpy expression for the MATLAB [V,D]=eig(a,b) is V,D = np.linalg.eig(a,b).

但是当我尝试这样做时,我得到了错误:

But when I try this I get the error:

TypeError: eig() takes 1 positional argument but 2 were given

我很困惑,文档说 np.linalg.eig 可以接受两个参数吗?

I'm confused, the documentation says np.linalg.eig can take two arguments?

奇怪的是,当我查看linalg 文档时"rel =" nofollow noreferrer> https://docs.scipy.org/doc/numpy-1.15.1/reference/routines.linalg.html ,在矩阵特征值"标题下没有提及 linalg.eig 接受两个参数?

Curiously, when I look at the linalg documentation at https://docs.scipy.org/doc/numpy-1.15.1/reference/routines.linalg.html, under the heading 'Matrix eigenvalues' there is no mention of linalg.eig taking two arguments?

如何获取 eig 以像在MATLAB中那样接受两个参数?

How can I get eig to take two arguments like in MATLAB?

a = diag(ones(3,1));
b = diag(2*ones(3,1));
[V,D] = eig(a,b)

输出:

V =

    0.7071         0         0
         0    0.7071         0
         0         0    0.7071


D =

    0.5000         0         0
         0    0.5000         0
         0         0    0.5000

这在Python中不起作用

import numpy as np

a = np.diag(np.ones(3))
b = np.diag(2*np.ones(3))

V,D = np.linalg.eig(a,b)

错误:

TypeError: eig() takes 1 positional argument but 2 were given

推荐答案

As you saw in the docs of numpy.linalg.eig, it only accepts a single array argument and correspondingly it doesn't compute generalized eigenvalue problems.

幸运的是,我们有 scipy.linalg.eig :

Fortunately we have scipy.linalg.eig:

scipy.linalg.eig(a, b=None, left=False, right=True, overwrite_a=False, overwrite_b=False, check_finite=True, homogeneous_eigvals=False)

    Solve an ordinary or generalized eigenvalue problem of a square matrix.

这是您的示例案例:

import numpy as np 
import scipy.linalg 

a = np.diag(np.ones(3)) 
b = np.diag(2*np.ones(3)) 
eigvals,eigvects = scipy.linalg.eig(a, b) 

现在我们有

>>> eigvals
array([0.5+0.j, 0.5+0.j, 0.5+0.j])

>>> eigvects
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])

本征向量的差异可能是由于对本征值的归一化选择不同.我将检查两个非平凡矩阵,看看结果是否彼此对应(当然,比较对应的特征值-特征向量对).

The difference in the eigenvectors might be due to a different choice in normalization for the eigenvalues. I'd check with two nontrivial matrices and see if the results correspond to one another (comparing corresponding eigenvalue-eigenvector pairs, of course).

这篇关于Python中的eig(a,b)给出错误“接受1个位置参数,但给出2个位置参数".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆