简体   繁体   English

通过运算符重载进行矩阵乘法

[英]Matrix Multiplication through operator overloading

This code shows the address when I run this instead of the multiplication of two matrices.这段代码显示了我运行它时的地址,而不是两个矩阵的乘法。

matrix matrix:: operator *(matrix x)
{  
    matrix c(m1,n2);   
    c.m=c.n=m;         
    for(int i=0;i<m1;i++) 
    {        
        for(int j=0;j<n2;j++)        
        {            
            c.a[i][j]=0;           
            for(int k=0;k<n1;k++)
            {
                c.a[i][j]+=(a[i][k]*x.a[k][j]);       
            }   
        } 
    } 
    return c; 
}

For two matrices, you can use either a member unary operator *=, ie:对于两个矩阵,您可以使用成员一元运算符 *=,即:

matrix & operator *= (matrix  const & q)
{
    // ... your code to multiply "this" by q...

    return *this;
}

or non-member binary operator:或非成员二元运算符:

matrix operator * (matrix p, matrix const & q)
{
    return p *= q;
}

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

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