编译命令:
g++ -std=c++14 -Wall -pedantic maintest.cpp -g -o maintest
代码在此
//以下为 Matrix.h
#include <iostream>
#include <cassert>
using std::size_t;
class Matrix {/
private:
std::size_t row;
std::size_t col;
double initV;
double* p;
public:
p=new double[row*col];//use linear array storing all matrix elements
for(size_t i=0;i<row;i++)
for(size_t j=0;j<col;j++)
p[i*col+j]=initValue;//assign each element respectively
}
~Matrix(){//deconstructor
delete []p;
}
Matrix(const Matrix& m0){
row=m0.rows();
col=m0.cols();
initV=m0.initVal();
p=new double[row*col];
for(size_t i=0;i<row;i++)
for(size_t j=0;j<col;j++)
p[i*col+j]=initV;
}
Matrix& operator=(const Matrix& m0){
row=m0.row;
col=m0.col;
for(size_t i=0;i<row;i++)
for(size_t j=0;j<col;j++)
p[i*col+j]=m0(i,j);
return *this;
}
double& operator()(size_t i, size_t j){
return p[i*col+j];
}
const double& operator()(size_t i, size_t j) const{
return p[i*col+j];
}
bool operator ==(const Matrix& m0) const{
if(row!=m0.row)
return false;
if(col!=m0.col)
return false;
for(size_t i=0;i<row;i++)
for(size_t j=0;j<col;j++)
if(p[i*col+j]!=m0(i,j))
return false;
return true;
}
bool operator !=(const Matrix& m0) const{
if(row!=m0.row)
return true;
if(col!=m0.col)
return true;
for(size_t i=0;i<row;i++)
for(size_t j=0;j<col;j++)
if(p[i*col+j]!=m0(i,j))
return true;
return false;
}
Matrix& operator +=(const Matrix& m0){
assert("matching size check"&&(row==m0.rows()));
assert("matching size check"&&(col==m0.cols()));
for(size_t i=0;i<row;i++)
for(size_t j=0;j<col;j++)
p[i*col+j]+=m0(i,j);
return *this;
}
Matrix operator +(const Matrix& m0) const{
assert("matching size check"&&(row==m0.rows()));
assert("matching size check"&&(col==m0.cols()));
Matrix m(this->row,this->col,0);
for(size_t i=0;i<row;i++)
for(size_t j=0;j<col;j++)
m(i,j)=p[i*col+j]+m0(i,j);
return m;
}
Matrix& operator -=(const Matrix& m0){
assert("matching size check"&&(row==m0.rows()));
assert("matching size check"&&(col==m0.cols()));
for(size_t i=0;i<row;i++)
for(size_t j=0;j<col;j++)
p[i*col+j]-=m0(i,j);
return *this;
}
Matrix operator -(const Matrix& m0) const{
assert("matching size check"&&(row==m0.rows()));
assert("matching size check"&&(col==m0.cols()));
Matrix m(row,col,0);
for(size_t i=0;i<row;i++)
for(size_t j=0;j<col;j++)
m(i,j)=p[i*col+j]-m0(i,j);
return m;
}
Matrix& operator *=(const Matrix& m0){
assert("matching size check"&&(row==m0.rows()));
assert("matching size check"&&(col==m0.cols()));
assert("matching size check"&&(col==m0.rows()));
for(size_t j=0;j<m0.cols();j++)
for(size_t i=0;i<row;i++){
double sum=0;
for(size_t k=0;k<col;k++)
sum+=p[i*col+k]*m0(k,j);
p[i*col+j]=sum;
}
return *this;
}
Matrix operator *(const Matrix& m0) const{
assert("matching size check"&&(col==m0.rows()));
Matrix m(row,m0.cols(),0);
for(size_t j=0;j<m0.col;j++)
for(size_t i=0;i<row;i++)
for(size_t k=0;k<col;k++)
m(i,j)+=p[i*col+k]*m0(k,j);
return m;
}
//get rows of rows
std::size_t rows() const{
return row;
}
std::size_t cols() const{
return col;
}
double initVal() const{
return initV;
}
friend std::ostream& operator <<(std::ostream&, const Matrix&);
friend std::istream& operator >>(std::istream&, Matrix&);
};
//overload input operator>> using std::istream
std::istream& operator >>(std::istream& in, Matrix& m0){
std::cout<<"please input elements"<<std::endl;
for(size_t i=0;i<m0.row;i++)
for(size_t j=0;j<m0.col;j++)
in>>m0(i,j);
return in;
}
//overload output operator<< using std::ostream
std::ostream& operator <<(std::ostream& out,const Matrix& m0){
for(size_t i=0;i<m0.row;i++){
for(size_t j=0;j<m0.col;j++)
out<<m0(i,j)<<"";
out<<std::endl;
}
return out;
}
//以下为 main.cpp
#include <iostream>
#include <cassert>
#include <cmath>
#include "Matrix.h"
//to test if two double variables are almost equal
bool almostEqual(double a, double b, double epsilon = 1e-13) {
return std::abs(a - b) <= epsilon;
}
// this function allows only matrices of equal dimensions
bool almostEqual(const Matrix& a, const Matrix& b, double epsilon = 1e-13){
assert(a.rows() == b.rows());//check if same size
assert(a.cols() == b.cols());//check if same size
for (size_t i = 0; i < a.rows(); ++i)
for (size_t j = 0; j < a.cols(); ++j)
if (!almostEqual(a(i, j), b(i, j), epsilon))
return false;
return true;
}
int main()
{
size_t rows=2;//rows given
size_t cols=4;//cols given
double initValue=1.0;//initial Value given
Matrix m1(rows, cols, initValue);//create source Matrix
Matrix m2(1, 1, 0);//create dest. Matrix
m2 = std::move(Matrix(m1));//the fault exists here,why?
assert("testing assignment/move assignment" && almostEqual(m1, m2, 0.0));
return 0;
}
}
g++ -std=c++14 -Wall -pedantic maintest.cpp -g -o maintest
代码在此
//以下为 Matrix.h
#include <iostream>
#include <cassert>
using std::size_t;
class Matrix {/
private:
std::size_t row;
std::size_t col;
double initV;
double* p;
public:
p=new double[row*col];//use linear array storing all matrix elements
for(size_t i=0;i<row;i++)
for(size_t j=0;j<col;j++)
p[i*col+j]=initValue;//assign each element respectively
}
~Matrix(){//deconstructor
delete []p;
}
Matrix(const Matrix& m0){
row=m0.rows();
col=m0.cols();
initV=m0.initVal();
p=new double[row*col];
for(size_t i=0;i<row;i++)
for(size_t j=0;j<col;j++)
p[i*col+j]=initV;
}
Matrix& operator=(const Matrix& m0){
row=m0.row;
col=m0.col;
for(size_t i=0;i<row;i++)
for(size_t j=0;j<col;j++)
p[i*col+j]=m0(i,j);
return *this;
}
double& operator()(size_t i, size_t j){
return p[i*col+j];
}
const double& operator()(size_t i, size_t j) const{
return p[i*col+j];
}
bool operator ==(const Matrix& m0) const{
if(row!=m0.row)
return false;
if(col!=m0.col)
return false;
for(size_t i=0;i<row;i++)
for(size_t j=0;j<col;j++)
if(p[i*col+j]!=m0(i,j))
return false;
return true;
}
bool operator !=(const Matrix& m0) const{
if(row!=m0.row)
return true;
if(col!=m0.col)
return true;
for(size_t i=0;i<row;i++)
for(size_t j=0;j<col;j++)
if(p[i*col+j]!=m0(i,j))
return true;
return false;
}
Matrix& operator +=(const Matrix& m0){
assert("matching size check"&&(row==m0.rows()));
assert("matching size check"&&(col==m0.cols()));
for(size_t i=0;i<row;i++)
for(size_t j=0;j<col;j++)
p[i*col+j]+=m0(i,j);
return *this;
}
Matrix operator +(const Matrix& m0) const{
assert("matching size check"&&(row==m0.rows()));
assert("matching size check"&&(col==m0.cols()));
Matrix m(this->row,this->col,0);
for(size_t i=0;i<row;i++)
for(size_t j=0;j<col;j++)
m(i,j)=p[i*col+j]+m0(i,j);
return m;
}
Matrix& operator -=(const Matrix& m0){
assert("matching size check"&&(row==m0.rows()));
assert("matching size check"&&(col==m0.cols()));
for(size_t i=0;i<row;i++)
for(size_t j=0;j<col;j++)
p[i*col+j]-=m0(i,j);
return *this;
}
Matrix operator -(const Matrix& m0) const{
assert("matching size check"&&(row==m0.rows()));
assert("matching size check"&&(col==m0.cols()));
Matrix m(row,col,0);
for(size_t i=0;i<row;i++)
for(size_t j=0;j<col;j++)
m(i,j)=p[i*col+j]-m0(i,j);
return m;
}
Matrix& operator *=(const Matrix& m0){
assert("matching size check"&&(row==m0.rows()));
assert("matching size check"&&(col==m0.cols()));
assert("matching size check"&&(col==m0.rows()));
for(size_t j=0;j<m0.cols();j++)
for(size_t i=0;i<row;i++){
double sum=0;
for(size_t k=0;k<col;k++)
sum+=p[i*col+k]*m0(k,j);
p[i*col+j]=sum;
}
return *this;
}
Matrix operator *(const Matrix& m0) const{
assert("matching size check"&&(col==m0.rows()));
Matrix m(row,m0.cols(),0);
for(size_t j=0;j<m0.col;j++)
for(size_t i=0;i<row;i++)
for(size_t k=0;k<col;k++)
m(i,j)+=p[i*col+k]*m0(k,j);
return m;
}
//get rows of rows
std::size_t rows() const{
return row;
}
std::size_t cols() const{
return col;
}
double initVal() const{
return initV;
}
friend std::ostream& operator <<(std::ostream&, const Matrix&);
friend std::istream& operator >>(std::istream&, Matrix&);
};
//overload input operator>> using std::istream
std::istream& operator >>(std::istream& in, Matrix& m0){
std::cout<<"please input elements"<<std::endl;
for(size_t i=0;i<m0.row;i++)
for(size_t j=0;j<m0.col;j++)
in>>m0(i,j);
return in;
}
//overload output operator<< using std::ostream
std::ostream& operator <<(std::ostream& out,const Matrix& m0){
for(size_t i=0;i<m0.row;i++){
for(size_t j=0;j<m0.col;j++)
out<<m0(i,j)<<"";
out<<std::endl;
}
return out;
}
//以下为 main.cpp
#include <iostream>
#include <cassert>
#include <cmath>
#include "Matrix.h"
//to test if two double variables are almost equal
bool almostEqual(double a, double b, double epsilon = 1e-13) {
return std::abs(a - b) <= epsilon;
}
// this function allows only matrices of equal dimensions
bool almostEqual(const Matrix& a, const Matrix& b, double epsilon = 1e-13){
assert(a.rows() == b.rows());//check if same size
assert(a.cols() == b.cols());//check if same size
for (size_t i = 0; i < a.rows(); ++i)
for (size_t j = 0; j < a.cols(); ++j)
if (!almostEqual(a(i, j), b(i, j), epsilon))
return false;
return true;
}
int main()
{
size_t rows=2;//rows given
size_t cols=4;//cols given
double initValue=1.0;//initial Value given
Matrix m1(rows, cols, initValue);//create source Matrix
Matrix m2(1, 1, 0);//create dest. Matrix
m2 = std::move(Matrix(m1));//the fault exists here,why?
assert("testing assignment/move assignment" && almostEqual(m1, m2, 0.0));
return 0;
}
}