繁体   English   中英

如何在 Python 中创建复数矩阵

[英]How create complex number matrix in Python

我正在尝试使用 Python 创建复数矩阵 class。 我的第一种方法适用于 class 和 complex(self.real,self.imag):运算符重载 +-* 是可以的。

class zcomplex:
def __init__(self,rows,real,imag):
    self.rows=rows
    self.real=real
    self.imag=imag
    self.tab=[[complex(self.real,self.imag)]*self.rows for i in range(rows)]
    
def __add__(self,other):
    tmp=zcomplex(self.rows,0,0)
    for i in range(self.rows):
        for j in range(self.rows):
            tmp.tab[i][j]= self.tab[i][j] + other.tab[i][j]
    return tmp

def __sub__(self,other):
    tmp=zcomplex(self.rows,0,0)
    for i in range(self.rows):
        for j in range(self.rows):
            tmp.tab[i][j]= self.tab[i][j] - other.tab[i][j]
    return tmp

def __mul__(self,other):
    tmp=zcomplex(self.rows,0,0)
    for i in range(self.rows):
        for j in range(self.rows):
            for k in range(self.rows):
                tmp.tab[i][j]+= self.tab[i][j] * other.tab[i][j]
    return tmp

def prm(self):
    for i in range(self.rows):
        print(self.tab[i])
    print('')


a=zcomplex(3,4,6)
a.prm()

b=zcomplex(3,1,2)
b.prm()

print('c+a+b')
c=a+b
c.prm()

print('d=a-b')
d=a-b
d.prm()

print('e=a*b')
e=a*b
e.prm()

但我想通过 inheritance 与 C++ 类似。

class zcomplex:
def __init__(self,real=1,imag=0):
    self.real=real
    self.imag=imag
def display(self):
    print((self.real,self.imag))

class zmatrix(zcomplex):
    def __init__(self, real=0,imag=0,rows=3):
        self.rows=rows
        zcomplex.__init__(self,real,imag)
        self.tab=[[(real,imag)]*self.rows for i in range(self.rows)]
    def display(self):
        for i in range(self.rows):
            print(self.tab[i])

a=zmatrix()
a.display()

b=zcomplex()
b.display()

但不起作用。 在 c++ 中,我可以使用 tab[i][j].real 如果没有 numpy,我该如何解决这个问题?

感谢您的关注和帮助!

对于 tab[i][j].real 我的意思是如何重载运算符 +,- 和 * 对于第二种情况。

在 c++ 中,我可以:

zmatrix zmatrix :: operator-(const zmatrix & zarray)

{ zmatrix temp(行,列);

for(int i =0; i < rows; ++i)
{
    for(int j=0; j < columns; ++j)
    {
        temp.ptab[i][j].real=ptab[i][j].real - zarray.ptab[i][j].real;
        temp.ptab[i][j].imag=ptab[i][j].imag - zarray.ptab[i][j].imag;
    }
}
return temp;

}

/*zmatrix zmatrix :: operator*(const zmatrix& TABLICA)

{ zmatrix temp(行,列);

for(int i =0; i < rows; ++i)
{
    for(int j=0; j < columns; ++j)
    {
        temp.ptab[i][j].real=temp.ptab[i][j].imag=0;
        for(int k=0; k < rows; ++k)
        {
            temp.ptab[i][j].real = temp.ptab[i][j].real + (TABLICA.ptab[i][k].real * ptab[k][j].real) - (TABLICA.ptab[i][k].imag * ptab[k][j].imag); //-28+16i
            temp.ptab[i][j].imag = temp.ptab[i][j].imag + (TABLICA.ptab[i][k].real * ptab[k][j].imag) + (TABLICA.ptab[i][k].imag * ptab[k][j].real);
        }
    }
}
return temp;

} */

如果您希望tab[i][j].real是一个有效的表达式,那么您必须将tab创建为zcomplex对象的矩阵。 你不这样做。 但你可以。 这是您的代码,只需进行简单的更改即可:

class zcomplex:
    def __init__(self,real=1,imag=0):
        self.real=real
        self.imag=imag
    def display(self):
        print((self.real,self.imag))

class zmatrix(zcomplex):
    def __init__(self, real=0,imag=0,rows=3):
        self.rows=rows
        zcomplex.__init__(self,real,imag)
        self.tab=[[zcomplex(real,imag)]*self.rows for i in range(self.rows)]
    def display(self):
        for i in range(self.rows):
            print(self.tab[i])

a=zmatrix()

b=zcomplex()

# this now works...prints `0`
i = 2
j = 1
print(a.tab[i][j].real)

更新:

我不明白您希望通过 zmatrix 从zmatrix继承来获得zcomplex 毕竟,矩阵本身从来都不是复数。 zmatrixzcomplex对象的容器。

您为构建矩阵的每一行所做的操作将不起作用。 [(real,imag)]*self.rows将创建一个列表,其中包含对同一元组的 3 个引用,这不是您想要的,对吧? 同样, [zcomplex(real,imag)]*self.rows将创建一个列表,其中包含对同一zcomplex object 的 3 个引用。 因此,您需要以不同的方式构建每一行。

这是您的代码版本,其中对我上面提到的两件事进行了更改。 我还添加了__repr__方法,以便轻松打印每个类的实例的内容。 最后,我添加了一些代码来演示该代码确实有效:

class zcomplex:
    def __init__(self,real=1,imag=0):
        self.real=real
        self.imag=imag
    def __repr__(self):
        return f'<r:{self.real} i:{self.imag}>'
    def display(self):
        print(self)

class zmatrix:
    def __init__(self, real=0 ,imag=0, rows=3):
        self.rows=rows
        self.tab=[[zcomplex(real,imag) for _ in range(rows)] for _ in range(self.rows)]
    def __repr__(self):
        return '\n'.join(' '.join(str(c) for c in self.tab[i]) for i in range(self.rows))
    def display(self):
        print(self)

a=zmatrix()
print(a)

a.tab[0][0].real = 123
a.tab[1][1].real = 456
a.tab[2][2].real = 789

a.tab[0][2].imag = 999
a.tab[1][1].imag = 888
a.tab[2][0].imag = 777

print('\n', a, sep='')

结果:

<r:0 i:0> <r:0 i:0> <r:0 i:0>
<r:0 i:0> <r:0 i:0> <r:0 i:0>
<r:0 i:0> <r:0 i:0> <r:0 i:0>

<r:123 i:0> <r:0 i:0> <r:0 i:999>
<r:0 i:0> <r:456 i:888> <r:0 i:0>
<r:0 i:777> <r:0 i:0> <r:789 i:0>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM