Python 在numpy中共轭转置运算符“.H”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26932461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:11:24  来源:igfitidea点击:

Conjugate transpose operator ".H" in numpy

pythonarraysnumpymatrixmonkeypatching

提问by benpro

It is very convenient in numpy to use the .Tattribute to get a transposed version of an ndarray. However, there is no similar way to get the conjugate transpose. Numpy's matrix class has the .Hoperator, but not ndarray. Because I like readable code, and because I'm too lazy to always write .conj().T, I would like the .Hproperty to always be available to me. How can I add this feature? Is it possible to add it so that it is brainlessly available every time numpy is imported?

在 numpy 中使用该.T属性来获取ndarray. 但是,没有类似的方法来获得共轭转置。Numpy 的矩阵类有.H操作符,但没有 ndarray。因为我喜欢可读的代码,而且因为我懒得一直写.conj().T,所以我希望该.H属性始终可供我使用。如何添加此功能?是否可以添加它,以便每次导入 numpy 时都可以无脑地使用它?

(A similar question could by asked about the .Iinverse operator.)

(一个类似的问题可以问到.I逆运算符。)

采纳答案by benpro

In general, the difficulty in this problem is that Numpy is a C-extension, which cannot be monkey patched...or can it? The forbiddenfruitmodule allows one to do this, although it feels a little like playing with knives.

一般来说,这个问题的难点在于 Numpy 是一个 C 扩展,它不能被猴子补丁......或者可以吗?该forbiddenfruit模块允许一个做到这一点,虽然感觉有点像用刀子玩。

So here is what I've done:

所以这就是我所做的:

  1. Install the very simple forbiddenfruitpackage

  2. Determine the user customization directory:

    import site
    print site.getusersitepackages()
    
  3. In that directory, edit usercustomize.pyto include the following:

    from forbiddenfruit import curse
    from numpy import ndarray
    from numpy.linalg import inv
    curse(ndarray,'H',property(fget=lambda A: A.conj().T))
    curse(ndarray,'I',property(fget=lambda A: inv(A)))
    
  4. Test it:

    python -c python -c "import numpy as np; A = np.array([[1,1j]]);  print A; print A.H"
    

    Results in:

    [[ 1.+0.j  0.+1.j]]
    [[ 1.-0.j]
     [ 0.-1.j]]
    
  1. 安装非常简单的forbiddenfruit

  2. 确定用户自定义目录:

    import site
    print site.getusersitepackages()
    
  3. 在该目录中,编辑usercustomize.py以包含以下内容:

    from forbiddenfruit import curse
    from numpy import ndarray
    from numpy.linalg import inv
    curse(ndarray,'H',property(fget=lambda A: A.conj().T))
    curse(ndarray,'I',property(fget=lambda A: inv(A)))
    
  4. 测试一下:

    python -c python -c "import numpy as np; A = np.array([[1,1j]]);  print A; print A.H"
    

    结果是:

    [[ 1.+0.j  0.+1.j]]
    [[ 1.-0.j]
     [ 0.-1.j]]
    

回答by Saullo G. P. Castro

You can subclass the ndarrayobject like:

您可以将ndarray对象子类化,例如:

from numpy import ndarray

class myarray(ndarray):    
    @property
    def H(self):
        return self.conj().T

such that:

使得:

a = np.random.random((3, 3)).view(myarray)
a.H

will give you the desired behavior.

会给你想要的行为。