#ifndef MARTRIX
#define MARTRIX
class matrix
{
public:
int width;
int height;
double* data;
matrix();
matrix(int width, int height, double value);
matrix(int width, int height, double* data);
~matrix();
matrix &operator =(const matrix &a);
friend matrix operator +(const matrix &a, const matrix &b);
friend matrix operator -(const matrix &a, const matrix &b);
friend matrix operator *(const matrix &a, const matrix &b);
inline double operator ()(int w, int h) const
{
return data[h*width + w];//h是横坐标w是列
}
inline double& operator ()(int w, int h)
{
return data[h*width + w];
}
};
matrix operator +(const matrix &a, const matrix &b);
matrix operator -(const matrix &a, const matrix &b);
matrix operator *(const matrix &a, const matrix &b);
#endif
#include "martrix.h"
#include<iostream>
using namespace std;
matrix::~matrix()
{
delete[] this->data;
}
matrix::matrix(int width, int height, double *data)
{
this->width = width;
this->height = height;
this->data = data;
}
matrix::matrix(int width, int height, double value)
{
this->width = width;
this->height = height;
this->data = new double[width*height];
for (int i = 0; i < width*height; i++)
{
this->data[i] = value;
}
}
matrix &matrix::operator=(const matrix &a)
{
this->width = a.width;
this->height = a.height;
for (int i = 0; i < width*height; i++)
{
this->data[i] = a.data[i];
}
return *this;
}
matrix operator +(const matrix &a, const matrix &b)
{
matrix c(a.width,a.height,double(0));
for (int i = 0; i < a.width*a.height; i++)
{
c.data[i] = a.data[i]+b.data[i];
}
return c;
}
matrix operator -(const matrix &a, const matrix &b)
{
matrix c = a;
for (int i = 0; i < a.width*a.height; i++)
{
c.data[i] = a.data[i] - b.data[i];
}
return c;
}
matrix operator *(const matrix &a, const matrix &b)
{
if (a.width != b.height) {
cout << "cuowu" << endl;
}
int line = a.height, col = b.width;
int i, j, k;
matrix c(line, col, double(0));
for (i = 0; i < line; i++)
{
for (k = 0; k < col; k++){
for (j = 0; j < b.height; j++)
{
c.data[i *col+ k] += a.data[i*col+ j] * b.data[j*col+ k];
}
}
}
return c;
}
#define MARTRIX
class matrix
{
public:
int width;
int height;
double* data;
matrix();
matrix(int width, int height, double value);
matrix(int width, int height, double* data);
~matrix();
matrix &operator =(const matrix &a);
friend matrix operator +(const matrix &a, const matrix &b);
friend matrix operator -(const matrix &a, const matrix &b);
friend matrix operator *(const matrix &a, const matrix &b);
inline double operator ()(int w, int h) const
{
return data[h*width + w];//h是横坐标w是列
}
inline double& operator ()(int w, int h)
{
return data[h*width + w];
}
};
matrix operator +(const matrix &a, const matrix &b);
matrix operator -(const matrix &a, const matrix &b);
matrix operator *(const matrix &a, const matrix &b);
#endif
#include "martrix.h"
#include<iostream>
using namespace std;
matrix::~matrix()
{
delete[] this->data;
}
matrix::matrix(int width, int height, double *data)
{
this->width = width;
this->height = height;
this->data = data;
}
matrix::matrix(int width, int height, double value)
{
this->width = width;
this->height = height;
this->data = new double[width*height];
for (int i = 0; i < width*height; i++)
{
this->data[i] = value;
}
}
matrix &matrix::operator=(const matrix &a)
{
this->width = a.width;
this->height = a.height;
for (int i = 0; i < width*height; i++)
{
this->data[i] = a.data[i];
}
return *this;
}
matrix operator +(const matrix &a, const matrix &b)
{
matrix c(a.width,a.height,double(0));
for (int i = 0; i < a.width*a.height; i++)
{
c.data[i] = a.data[i]+b.data[i];
}
return c;
}
matrix operator -(const matrix &a, const matrix &b)
{
matrix c = a;
for (int i = 0; i < a.width*a.height; i++)
{
c.data[i] = a.data[i] - b.data[i];
}
return c;
}
matrix operator *(const matrix &a, const matrix &b)
{
if (a.width != b.height) {
cout << "cuowu" << endl;
}
int line = a.height, col = b.width;
int i, j, k;
matrix c(line, col, double(0));
for (i = 0; i < line; i++)
{
for (k = 0; k < col; k++){
for (j = 0; j < b.height; j++)
{
c.data[i *col+ k] += a.data[i*col+ j] * b.data[j*col+ k];
}
}
}
return c;
}