C++ 矩阵乘法问题
我实现了一个矩阵类。它为任意大小的矩阵动态分配内存。当使用我的矩阵乘法函数时,它只在特定条件下工作。如果有两个N*M矩阵,则第一个矩阵的M必须等于第二个矩阵的N。 例如:C++ 矩阵乘法问题,c++,matrix,multiplication,C++,Matrix,Multiplication,我实现了一个矩阵类。它为任意大小的矩阵动态分配内存。当使用我的矩阵乘法函数时,它只在特定条件下工作。如果有两个N*M矩阵,则第一个矩阵的M必须等于第二个矩阵的N。 例如: //matrix constructor 'matrix M(n, m) matrix A(2, 3), matrix B(3, 4); //A and B can be multiplied because A_m == B_n A * B; //this works! //changing the size of n of
//matrix constructor 'matrix M(n, m)
matrix A(2, 3), matrix B(3, 4);
//A and B can be multiplied because A_m == B_n
A * B; //this works!
//changing the size of n of the first matrix causes this to fail if n > m
matrix C(4, 3), matrix D(3, 5);
C * D; // program becomes unresponsive for unknown reasons, only happens when C_n > C_m
我的矩阵类和乘法函数
#include <iostream>
class matrix{
private:
int rows, cols;
public:
int ** data;
int row();
int col();
matrix();
matrix(int, int);
//~matrix();
matrix operator*(matrix M);
matrix operator%(matrix M);
friend std::ostream & operator<<(std::ostream & os, matrix M);
};
#包括
类矩阵{
私人:
int行,cols;
公众:
int**数据;
int行();
int col();
矩阵();
矩阵(int,int);
//~matrix();
矩阵算子*(矩阵M);
矩阵算子%(矩阵M);
friend std::ostream&operatorrow(),M.col();
int-var=0;
对于(int i=0;i数据[i][k]*M.data[k][j];
}
结果.数据[i][j]=var;
var=0;
}
}
返回结果;
}
否则,这3个循环将从分配给代码的块中访问内存
for (int i = 0; i < result.row(); i++){
for (int j = 0; j < result.col(); j++){
for (int k = 0; k < result.row(); k++){
for(int i=0;i
应该是
for (int i = 0; i < result.row(); i++){
for (int j = 0; j < M.col(); j++){
for (int k = 0; k < M.row(); k++){
var += this->data[i][k] * M.data[k][j];
}
result.data[i][j] = var;
var = 0;
}
}
for(int i=0;idata[i][k]*M.data[k][j];
}
结果.数据[i][j]=var;
var=0;
}
}
首先,为
using std::cout;
using std::endl;
很高兴看到有人不是为了这样一个简单的工作而全力以赴
现在让我们通过算法进行散步。
矩阵C(4,3)*矩阵D(3,5)将给出矩阵结果(4,5)
与
std::矢量数据;
您可以在构造函数中对其进行如下初始化:
matrix::matrix(int r, int c):rows(r), cols(c), data(r, std::vector<int>(c))
{
}
矩阵(intr,intc):行(r),列(c),数据(r,std::vector(c))
{
}
不需要通过循环将其归零。向量构造函数会为您执行此操作。如果您不想使用零加载,可以指定另一个值
matrix::matrix(int r, int c):
rows(r),
cols(c),
data(r, std::vector<int>(c, <starting value goes here>))
{
}
矩阵(int r,int c):
第(r)行,
cols(c),
数据(r,std::vector(c,))
{
}
我希望这段代码能起作用
#include<iostream>
using namespace std;
template<typename T>
class Matrix{
int rs, cs;
T **mat;
public:
Matrix() { mat = NULL; rs = cs = 0; }
istream& operator>>( Matrix <T> &);
ofstream& operator<<(const Matrix<T> &);
Matrix<T> operator=(const Matrix<T> &);
Matrix<T> operator*(const Matrix<T> &);
void read();
void print();
};
template <typename T>
Matrix<T> Matrix<T>::operator=(const Matrix<T> &a){
if(this->mat!=NULL)
{
for(int i = 0 ; i < this->rs ; i++)
delete []mat;
}
this->rs = a.rs;
this->cs = a.cs;
this->mat = new T*[a.rs];
for(int i = 0 ; i < a.rs; i++)
this->mat[i] = new T[a.cs];
for(int i = 0 ; i < a.rs ; i++){
for(int k = 0 ; k < a.cs; k++)
this->mat[i][k] =a.mat[i][k];
}
return a;
}
template <typename T>
Matrix<T> Matrix<T>::operator*(const Matrix<T> &a){
Matrix<T> b;
b.rs = this->rs;
b.cs = a.cs;
if(this->cs == a.rs){
b.mat = new T*[b.rs];
for(int i = 0 ; i < this->rs; i++)
b.mat[i] = new T[b.cs];
for(int i = 0 ; i < this->rs; i++)
{
for(int k = 0 ; k < a.cs ; k++){
b.mat[i][k] = 0;
for(int j = 0 ; j < this->cs ; j++)
b.mat[i][k] += this->mat[i][j] * a.mat[j][k];
}
}
return b;
}
else
{
cout<<"You can not multiply these matrices"<<endl;
b.mat = new T*[b.rs];
for(int i = 0 ; i < this->rs; i++)
b.mat[i] = new T[b.cs];
for(int i = 0 ; i < b.rs ; i++)
for(int k = 0 ; k < b.cs ; k++)
b.mat[i][k] =0;
return b;
}
}
template <typename T>
ostream& operator<<(ostream &g , Matrix<T> &a);
template <typename T>
istream& operator>>(istream &g , Matrix<T> &a);
int main(){
Matrix <int > A, B, C;
Matrix < double > M, R, S;
cin >> A;
cin>>C;
B = A*C;
cout<<endl<<B<<endl;
}
template <typename T>
ostream& operator<<(ostream &g , Matrix<T> &a){
a.print();
return g;
}
template <typename T>
void Matrix<T>::print(){
for(int i = 0; i < rs ;i++){
for(int k = 0 ; k < cs ; k++){
cout<<mat[i][k]<<" ";
}
cout<<endl;
}
}
template <typename T>
istream& operator>>(istream &g , Matrix<T> &a)
{
a.read();
return g;
}
template <typename T>
void Matrix<T>::read()
{
if (mat!=NULL) {
for(int i = 0 ; i < rs ; i++)
delete []mat;
}
cout<<"Enter row size: ";
cin >> rs;
cout<<"Enter column size: ";
cin>> cs;
int i;
mat = new T*[rs];
for (i = 0; i < rs; i++)
mat[i] = new T[cs];
int j;
for (i = 0; i < rs; i++)
{
for (j = 0; j < cs; j++)
{
cout<<"Element ["<<i<<"]"<<"["<<j<<"] = ";
cin >> mat[i][j];
}
}
}
#包括
使用名称空间std;
模板
类矩阵{
int-rs,cs;
T**mat;
公众:
矩阵(){mat=NULL;rs=cs=0;}
istream和运算符>>(矩阵和);
流和运算符(i++)
删除[]项;
}
这->遥感=遥感;
这->cs=a.cs;
本->材料=新T*[a.rs];
对于(int i=0;i材料[i]=新的T[a.C];
对于(int i=0;imat[i][k]=a.mat[i][k];
}
返回a;
}
模板
矩阵::运算符*(常数矩阵&a){
矩阵b;
b、 rs=这个->rs;
b、 cs=a.cs;
如果(本->cs==a.rs){
b、 mat=新T*[b.rs];
对于(inti=0;irs;i++)
b、 mat[i]=新的T[b.cs];
对于(inti=0;irs;i++)
{
对于(int k=0;kcs;j++)
b、 mat[i][k]+=this->mat[i][j]*a.mat[j][k];
}
}
返回b;
}
其他的
{
库塔;
cin>>C;
B=A*C;
好消息是,这段代码没有问题,因为您没有实现析构函数、复制因子或复制赋值运算符。坏消息是,这不是问题的原因是它像筛子一样泄漏。想想这在将1x1矩阵与1x1矩阵相乘时会发生什么。我不认为OP正在查看输出ut的函数。@user4581301我对此也有严重的保留。我建议的示例将导致根本不进行乘法,并且如果使用此处建议的解决方案,则无论操作数如何,都会得到一个单值为零的1x1矩阵。这显然是不正确的。我现在明白了,这是行不通的,总是使底部的r现在设置为0是一种奖励,使用std::vector
方法,默认情况下使类规则符合三个。不需要,0
,但这太复杂了,几乎不值得一提。此外,尽管它不影响此代码,但应该注意,该成员列表中的初始值设定项顺序不是从左到右。行
,然后cols
,然后data
将是类decl中每个成员的顺序的初始化顺序。是的,那0几乎没有用处。我将进行编辑,以明确为什么您可能要在那里指定值。还有福吉。我确实弄错了顺序。我正在查看行和列函数。这非常好。Clearly解决了这个问题并提供了解决方案。谢谢。
for (int i = 0; i < result.row(); i++){
for (int j = 0; j < result.col(); j++){
for (int k = 0; k < this->cols; k++){
int ** data;
std::vector<std::vector<int> > data;
matrix::matrix(int r, int c):rows(r), cols(c), data(r, std::vector<int>(c))
{
}
matrix::matrix(int r, int c):
rows(r),
cols(c),
data(r, std::vector<int>(c, <starting value goes here>))
{
}
#include<iostream>
using namespace std;
template<typename T>
class Matrix{
int rs, cs;
T **mat;
public:
Matrix() { mat = NULL; rs = cs = 0; }
istream& operator>>( Matrix <T> &);
ofstream& operator<<(const Matrix<T> &);
Matrix<T> operator=(const Matrix<T> &);
Matrix<T> operator*(const Matrix<T> &);
void read();
void print();
};
template <typename T>
Matrix<T> Matrix<T>::operator=(const Matrix<T> &a){
if(this->mat!=NULL)
{
for(int i = 0 ; i < this->rs ; i++)
delete []mat;
}
this->rs = a.rs;
this->cs = a.cs;
this->mat = new T*[a.rs];
for(int i = 0 ; i < a.rs; i++)
this->mat[i] = new T[a.cs];
for(int i = 0 ; i < a.rs ; i++){
for(int k = 0 ; k < a.cs; k++)
this->mat[i][k] =a.mat[i][k];
}
return a;
}
template <typename T>
Matrix<T> Matrix<T>::operator*(const Matrix<T> &a){
Matrix<T> b;
b.rs = this->rs;
b.cs = a.cs;
if(this->cs == a.rs){
b.mat = new T*[b.rs];
for(int i = 0 ; i < this->rs; i++)
b.mat[i] = new T[b.cs];
for(int i = 0 ; i < this->rs; i++)
{
for(int k = 0 ; k < a.cs ; k++){
b.mat[i][k] = 0;
for(int j = 0 ; j < this->cs ; j++)
b.mat[i][k] += this->mat[i][j] * a.mat[j][k];
}
}
return b;
}
else
{
cout<<"You can not multiply these matrices"<<endl;
b.mat = new T*[b.rs];
for(int i = 0 ; i < this->rs; i++)
b.mat[i] = new T[b.cs];
for(int i = 0 ; i < b.rs ; i++)
for(int k = 0 ; k < b.cs ; k++)
b.mat[i][k] =0;
return b;
}
}
template <typename T>
ostream& operator<<(ostream &g , Matrix<T> &a);
template <typename T>
istream& operator>>(istream &g , Matrix<T> &a);
int main(){
Matrix <int > A, B, C;
Matrix < double > M, R, S;
cin >> A;
cin>>C;
B = A*C;
cout<<endl<<B<<endl;
}
template <typename T>
ostream& operator<<(ostream &g , Matrix<T> &a){
a.print();
return g;
}
template <typename T>
void Matrix<T>::print(){
for(int i = 0; i < rs ;i++){
for(int k = 0 ; k < cs ; k++){
cout<<mat[i][k]<<" ";
}
cout<<endl;
}
}
template <typename T>
istream& operator>>(istream &g , Matrix<T> &a)
{
a.read();
return g;
}
template <typename T>
void Matrix<T>::read()
{
if (mat!=NULL) {
for(int i = 0 ; i < rs ; i++)
delete []mat;
}
cout<<"Enter row size: ";
cin >> rs;
cout<<"Enter column size: ";
cin>> cs;
int i;
mat = new T*[rs];
for (i = 0; i < rs; i++)
mat[i] = new T[cs];
int j;
for (i = 0; i < rs; i++)
{
for (j = 0; j < cs; j++)
{
cout<<"Element ["<<i<<"]"<<"["<<j<<"] = ";
cin >> mat[i][j];
}
}
}