派生修改

This commit is contained in:
iridiumR 2022-03-22 15:57:58 +08:00
parent 35a9587d3d
commit d63b1a8209

632
SoftwareDesign/Code/inc/matrix.hpp Normal file → Executable file
View file

@ -1,312 +1,322 @@
/* /*
* @Author: iR * @Author: iR
* @Date: 2022-03-11 16:44:46 * @Date: 2022-03-11 16:44:46
* @LastEditors: iR * @LastEditors: iR
* @LastEditTime: 2022-03-12 17:25:28 * @LastEditTime: 2022-03-15 17:02:42
* @FilePath: \Code\inc\matrix.hpp * @FilePath: \Code\inc\matrix.hpp
* @Description: * @Description:
* *
* Copyright (c) 2022 by iR, All Rights Reserved. * Copyright (c) 2022 by iR, All Rights Reserved.
*/ */
#ifndef _MATRIX_HPP_ #ifndef _MATRIX_HPP_
#define _MATRIX_HPP_ #define _MATRIX_HPP_
#include <iostream> #include <iostream>
#include <stdio.h> #include <stdio.h>
#include "../inc/vector.hpp" #include "../inc/vector.hpp"
template <class T> template <class T>
class Matrix class MatBase
{ { // 定义基类,其中包含了矩阵元素读取的纯虚函数
private: public:
int _height; virtual T &at(int y, int x) = 0; // 定义纯虚函数
int _width; virtual const T &at(int y, int x) const = 0; // 定义纯虚函数
Vector<T> *_mat; };
protected: template <class T>
public: class Matrix : public MatBase<T>
~Matrix() {
{ private:
delete _mat; int _height;
} int _width;
/** Vector<T> *_mat;
* @description: , 2*2, 0
* @param {int} w protected:
* @param {int} h public:
*/ ~Matrix()
Matrix(); {
/** delete _mat;
* @description: 0 }
* @param {int} w /**
* @param {int} h * @description: , 2*2, 0
*/ * @param {int} w
Matrix(int w, int h); * @param {int} h
*/
/** Matrix();
* @description: /**
* @param {int} w * @description: 0
* @param {int} h * @param {int} w
* @param {T} init_value * @param {int} h
*/ */
Matrix(int w, int h, T init_value); Matrix(int w, int h);
/** /**
* @description: 访 * @description:
* , * @param {int} w
* @param {int} w * @param {int} h
* @param {int} h * @param {T} init_value
* @param {const T} elements[] */
*/ Matrix(int w, int h, T init_value);
Matrix(int w, int h, const T elements[]);
/**
// /** * @description: 访
// * @description: 以顺序访问指针的值初始化矩阵元素 * ,
// * @note: 必须给出正确的数组,因为在函数内无法判定数组是否正确 * @param {int} w
// * @param {int} w * @param {int} h
// * @param {int} h * @param {const T} elements[]
// * @param {T*} data */
// */ Matrix(int w, int h, const T elements[]);
// Matrix(int w, int h, T *data);
// /**
/** // * @description: 以顺序访问指针的值初始化矩阵元素
* @description: // * @note: 必须给出正确的数组,因为在函数内无法判定数组是否正确
* @param {Matrix} &another // * @param {int} w
*/ // * @param {int} h
Matrix(const Matrix &another); // * @param {T*} data
// */
/** // Matrix(int w, int h, T *data);
* @description: 访
* @param {int} w /**
* @param {int} h * @description:
* @return {*} * @param {Matrix} &another
*/ */
const T get(int w, int h) const; Matrix(const Matrix &another);
/** /**
* @description: [] * @description: 访
*/ * @param {int} w
const T operator[](int i) const { return _mat->get(i); } * @param {int} h
* @return {*}
/** */
* @description: = const T get(int w, int h) const;
*/
T operator=(const Matrix &b); /**
* @description: []
/** */
* @description: += const T operator[](int i) const { return _mat->get(i); }
* @note: a += b*this即为a
* @param {*} /**
* @return None * @description: =
*/ */
void operator+=(const Matrix &b); void operator=(const Matrix &b);
/** /**
* @description: += * @description: +=
* @note: c = a + b*this即为ac * @note: a += b*this即为a
* @param {*} * @param {*}
* @return None * @return None
*/ */
Matrix operator+(const Matrix &b) const; void operator+=(const Matrix &b);
void operator+=(T &a)
/** {
* @description: 访 for (int i = 0; i < _width * _height;i++)
* @param {int} w {
* @param {int} h _mat[i] = a;
* @return {*} }
*/ }
T &at(int w, int h)
{ /**
T &ref = _mat->get(h * _width + w); * @description: +=
return ref; * @note: c = a + b*this即为ac
} * @param {*}
* @return None
/** */
* @description: 访 Matrix operator+(const Matrix &b) const;
* @param {int} w
* @param {int} h /**
* @return {*} * @description: 访
*/ * @param {int} w
const T &at(int w, int h) const * @param {int} h
{ * @return {*}
T &ref = _mat->get(h * _width + w); */
return ref; T &at(int w, int h)
} {
T &ref = _mat->get(h * _width + w);
const int getWidth() const { return _width; } return ref;
const int getHeight() const { return _height; } }
};
/**
template <class T> * @description: 访
Matrix<T>::Matrix() * @param {int} w
{ * @param {int} h
_width = 2; * @return {*}
_height = 2; */
_mat = new Vector<T>(2 * 2, 0); const T &at(int w, int h) const
} {
T &ref = _mat->get(h * _width + w);
template <class T> return ref;
Matrix<T>::Matrix(int w, int h) }
{
_width = w; const int getWidth() const { return _width; }
_height = h; const int getHeight() const { return _height; }
_mat = new Vector<T>(w * h, 0); };
}
template <class T>
template <class T> Matrix<T>::Matrix()
Matrix<T>::Matrix(int w, int h, T init_value) {
{ _width = 2;
_width = w; _height = 2;
_height = h; _mat = new Vector<T>(2 * 2, 0);
_mat = new Vector<T>(w * h, init_value); }
}
template <class T>
template <class T> Matrix<T>::Matrix(int w, int h)
Matrix<T>::Matrix(int w, int h, const T elements[]) {
{ _width = w;
int size = w * h; _height = h;
try _mat = new Vector<T>(w * h, 0);
{ }
//实际上似乎并不能在这里判断数组是否合法
// if (w * h > size) template <class T>
// throw "Error: The number of array elements is less than the matrix elements.\n"; Matrix<T>::Matrix(int w, int h, T init_value)
// if (w * h < size) {
// throw "Error: The number of array elements is greater than the matrix elements.\n"; _width = w;
_height = h;
_width = w; _mat = new Vector<T>(w * h, init_value);
_height = h; }
_mat = new Vector<T>(w * h);
for (int i = 0; i < size; i++) template <class T>
_mat->put(i, elements[i]); Matrix<T>::Matrix(int w, int h, const T elements[])
} {
catch (const char *msg) int size = w * h;
{ try
std::cout << msg << "SIZE:" << size << std::endl; {
; //实际上似乎并不能在这里判断数组是否合法
} // if (w * h > size)
} // throw "Error: The number of array elements is less than the matrix elements.\n";
// if (w * h < size)
//这个方法似乎有问题 // throw "Error: The number of array elements is greater than the matrix elements.\n";
// template <class T>
// Matrix<T>::Matrix(int w, int h, T *data) _width = w;
// { _height = h;
// int size = w * h; _mat = new Vector<T>(w * h);
// try for (int i = 0; i < size; i++)
// { _mat->put(i, elements[i]);
// //实际上似乎并不能在这里判断数组是否合法 }
// // if (w * h > size) catch (const char *msg)
// // throw "Error: The number of array elements is less than the matrix elements.\n"; {
// // if (w * h < size) std::cout << msg << "SIZE:" << size << std::endl;
// // throw "Error: The number of array elements is greater than the matrix elements.\n"; ;
// if (data == NULL) }
// throw 1; }
// _width = w;
// _height = h; //这个方法似乎有问题
// _mat = new Vector<T>; // template <class T>
// for (int i = 0; i < size; i++) // Matrix<T>::Matrix(int w, int h, T *data)
// _mat->put(i, *(data+i)); // {
// } // int size = w * h;
// try
// catch (const int a) // {
// { // //实际上似乎并不能在这里判断数组是否合法
// if (a == 1) // // if (w * h > size)
// { // // throw "Error: The number of array elements is less than the matrix elements.\n";
// std::cout << "Error: Empty array. Set elements to 0." << std::endl; // // if (w * h < size)
// for (int i = 0; i < size; i++) // // throw "Error: The number of array elements is greater than the matrix elements.\n";
// _mat->put(i, 0); // if (data == NULL)
// } // throw 1;
// } // _width = w;
// } // _height = h;
// _mat = new Vector<T>;
template <class T> // for (int i = 0; i < size; i++)
Matrix<T>::Matrix(const Matrix &another) // _mat->put(i, *(data+i));
{ // }
try
{ // catch (const int a)
_width = another.getWidth(); // {
_height = another.getHeight(); // if (a == 1)
_mat = new Vector<T>; // {
for (int i = 0; i < _width * _height; i++) // std::cout << "Error: Empty array. Set elements to 0." << std::endl;
_mat->put(i, another[i]); // for (int i = 0; i < size; i++)
} // _mat->put(i, 0);
catch (const char *msg) // }
{ // }
std::cout << msg; // }
}
} template <class T>
Matrix<T>::Matrix(const Matrix &another)
template <class T> {
T Matrix<T>::operator=(const Matrix &another) try
{ {
try _width = another.getWidth();
{ _height = another.getHeight();
_width = another.getWidth(); _mat = new Vector<T>;
_height = another.getHeight(); for (int i = 0; i < _width * _height; i++)
_mat = new Vector<T>; _mat->put(i, another[i]);
for (int i = 0; i < _width * _height; i++) }
_mat->put(i, another[i]); catch (const char *msg)
} {
catch (const char *msg) std::cout << msg;
{ }
std::cout << msg; }
}
} template <class T>
void Matrix<T>::operator=(const Matrix &another)
template <class T> {
const T Matrix<T>::get(int w, int h) const try
{ {
return _mat->get(h * _width + w); _width = another.getWidth();
} _height = another.getHeight();
_mat = new Vector<T>;
template <class T> for (int i = 0; i < _width * _height; i++)
void Matrix<T>::operator+=(const Matrix &b) _mat->put(i, another[i]);
{
try }
{ catch (const char *msg)
if (this->_height == b.getHeight() && this->_width == b.getWidth()) {
{ std::cout << msg;
for (int i = 0; i < _width * _height; i++) }
this->_mat->put(i, (*this)[i] + b[i]); }
return; template <class T>
} const T Matrix<T>::get(int w, int h) const
throw "Error: Different size."; {
} return _mat->get(h * _width + w);
catch (const char *msg) }
{
std::cout << msg; template <class T>
} void Matrix<T>::operator+=(const Matrix &b)
} {
try
/** {
* @description: += if (this->_height == b.getHeight() && this->_width == b.getWidth())
* @note: c = a + b*this即为ac {
* @param {*} for (int i = 0; i < _width * _height; i++)
* @return None this->_mat->put(i, (*this)[i] + b[i]);
*/
template <class T> return;
Matrix<T> Matrix<T>::operator+(const Matrix &b) const }
{ throw "Error: Different size.";
int h = this->_height; }
int w = this->_width; catch (const char *msg)
try {
{ std::cout << msg;
if (h == b.getHeight() && w == b.getWidth()) }
{ }
Matrix<T> temp(w, h, T(0));
temp += *this; template <class T>
temp += b; Matrix<T> Matrix<T>::operator+(const Matrix &b) const
return temp; {
} int h = this->_height;
int w = this->_width;
throw "Error: Different size."; try
} {
catch (const char *msg) if (h == b.getHeight() && w == b.getWidth())
{ {
std::cout << msg; Matrix<T> temp(w, h, T(0));
Matrix<T> temp(w, h, T(0)); temp += *this;
return temp; temp += b;
} return temp;
} }
throw "Error: Different size.";
}
catch (const char *msg)
{
std::cout << msg;
Matrix<T> temp(w, h, T(0));
return temp;
}
}
#endif #endif