跳到正文

剪贴板

剪贴板 gzgxyk0n

原文
剪贴板信息

剪贴板信息

收录
更新

MatrixMatrix

C++
namespace std {

	template<typename T>
	class Matrix {

	private:

		size_t Row = 0, Column = 0;
		T *Mat = nullptr;

		inline size_t index(const size_t& i, const size_t& j) const {
			return i * Column + j;
		}


	public:

		static T Mod;
		static inline T mod(T& a) {
#ifndef Matrix_Mod_Disabled
			if (false && Mod != T()) {
				a = (a % Mod + Mod) % Mod;
			}
#endif
			return a;
		}


		Matrix() {
			Row = 0, Column = 0;
			Mat = nullptr;
		}

		Matrix(size_t _Row, size_t _Column) {
			Matrix();
			resize(_Row, _Column);
		}

		Matrix(size_t _Size, const T& _Value) {
			Matrix();
			resize(_Size, _Size, _Value);
		}

		Matrix(size_t _Row, size_t _Column, const T& _Value) {
			Matrix();
			resize(_Row, _Column, _Value);
		}

		Matrix(const Matrix& b) {
			resize(b.Row, b.Column);
			for (size_t i = 0; i < Row; i++)
				for (size_t j = 0; j < Column; j++) {
					Mat[index(i, j)] = b[i][j];
					mod(Mat[index(i, j)]);
				}
		}

		Matrix(initializer_list< initializer_list<T> > Init) {
			size_t _Row, _Column;
			_Row = Init.size(), _Column = 0;
			for (const initializer_list<T>& List : Init) {
				_Column = max(_Column, List.size());
			}
			resize(_Row, _Column);
			for (size_t i = 0; i < _Row; i++) {
				const initializer_list<T>& List = *(Init.begin() + i);
				for (size_t j = 0; j < _Column; j++) {
					if (j < List.size()) {
						Mat[index(i, j)] = *(List.begin() + j);
					} else {
						Mat[index(i, j)] = 0;
					}
					mod(Mat[index(i, j)]);
				}
			}
		}

		~Matrix() { delete[] Mat; }


		size_t size() const;
		size_t row() const;
		size_t column() const;

		void resize(size_t _Row, size_t _Column);
		void resize(size_t _Row, size_t _Column, const T& _Value);

		T* operator [] (size_t i) const;

		Matrix& operator = (const Matrix& b);
		Matrix& operator += (const Matrix& b);
		Matrix& operator -= (const Matrix& b);
		Matrix& operator *= (const T& b);
		Matrix& operator *= (const Matrix& b);

		Matrix operator + () const;
		Matrix operator - () const;
		Matrix operator + (const Matrix& b) const;
		Matrix operator - (const Matrix& b) const;
		Matrix operator * (const T& b) const;
		Matrix operator * (const Matrix& b) const;
	};


	template<typename T>
	size_t Matrix<T>::size() const {
		return Row * Column;
	}

	template<typename T>
	size_t Matrix<T>::row() const {
		return Row;
	}

	template<typename T>
	size_t Matrix<T>::column() const {
		return Column;
	}

	template<typename T>
	void Matrix<T>::resize(size_t _Row, size_t _Column) {
		if (_Row * _Column > Row * Column) {
			delete[] Mat;
			Mat = new T [_Row * _Column];
		}
		Row = _Row, Column = _Column;
	}

	template<typename T>
	void Matrix<T>::resize(size_t _Row, size_t _Column, const T& _Value) {
		if (_Row * _Column > Row * Column) {
			delete[] Mat;
			Mat = new T [_Row * _Column];
		}
		Row = _Row, Column = _Column;
		for (size_t i = 0; i < Row; i++)
			for (size_t j = 0; j < Column; j++) {
				Mat[index(i, j)] = _Value;
				mod(Mat[index(i, j)]);
			}
	}

	template<typename T>
	Matrix<T>& Matrix<T>::operator = (const Matrix<T>& b) {
		resize(b.Row, b.Column);
		for (size_t i = 0; i < Row; i++)
			for (size_t j = 0; j < Column; j++) {
				Mat[index(i, j)] = b[i][j];
				mod(Mat[index(i, j)]);
			}
		return (*this);
	}

	template<typename T>
	Matrix<T>& Matrix<T>::operator += (const Matrix<T>& b) {
		(*this) = (*this) + b;
		return (*this);
	}

	template<typename T>
	Matrix<T>& Matrix<T>::operator -= (const Matrix<T>& b) {
		(*this) = (*this) - b;
		return (*this);
	}

	template<typename T>
	Matrix<T>& Matrix<T>::operator *= (const T& b) {
		(*this) = (*this) * b;
		return (*this);
	}

	template<typename T>
	Matrix<T>& Matrix<T>::operator *= (const Matrix<T>& b) {
		(*this) = (*this) * b;
		return (*this);
	}

	template<typename T>
	T* Matrix<T>::operator [] (size_t i) const {
		return &Mat[index(i, 0)];
	}

	template<typename T>
	Matrix<T> Matrix<T>::operator + () const {
		Matrix<T> c;
		for (size_t i = 0; i < Row; i++)
			for (size_t j = 0; j < Column; j++) {
				c[i][j] = + Mat[index(i, j)];
				mod(c[i][j]);
			}
		return c;
	}

	template<typename T>
	Matrix<T> Matrix<T>::operator - () const {
		Matrix<T> c;
		for (size_t i = 0; i < Row; i++)
			for (size_t j = 0; j < Column; j++) {
				c[i][j] = - Mat[index(i, j)];
				mod(c[i][j]);
			}
		return c;
	}

	template<typename T>
	Matrix<T> Matrix<T>::operator + (const Matrix<T>& b) const {
		if (Row != b.Row || Column != b.Column) {
			throw runtime_error("");
		}
		Matrix<T> c(Row, Column);
		for (size_t i = 0; i < Row; i++)
			for (size_t j = 0; j < Column; j++) {
				c[i][j] = Mat[index(i, j)] + b[i][j];
				mod(c[i][j]);
			}
		return c;
	}

	template<typename T>
	Matrix<T> Matrix<T>::operator - (const Matrix<T>& b) const {
		if (Row != b.Row || Column != b.Column) {
			throw runtime_error("");
		}
		Matrix<T> c(Row, Column);
		for (size_t i = 0; i < Row; i++)
			for (size_t j = 0; j < Column; j++) {
				c[i][j] = Mat[index(i, j)] - b[i][j];
				mod(c[i][j]);
			}
		return c;
	}

	template<typename T>
	Matrix<T> Matrix<T>::operator * (const T& b) const {
		Matrix<T> c(Row, Column);
		for (size_t i = 0; i < Row; i++)
			for (size_t j = 0; j < Column; j++) {
				c[i][j] = Mat[index(i, j)] * b;
				mod(c[i][j]);
			}
		return c;
	}

	template<typename T>
	Matrix<T> Matrix<T>::operator * (const Matrix<T>& b) const {
		if (Column != b.Row) {
			throw runtime_error("");
		}
		Matrix<T> c(Row, b.Column, 0);
		for (size_t i = 0; i < Row; i++)
			for (size_t k = 0; k < Column; k++) {
				T a_i_k = Mat[index(i, k)];
				for (size_t j = 0; j < b.Column; j++) {
					c[i][j] += a_i_k * b[k][j];
					mod(c[i][j]);
				}
			}
		return c;
	}


	template<typename T>
	Matrix<T> transpose(const Matrix<T>& a) {
		Matrix<T> c(a.column(), a.row());
		for (size_t i = 0; i < a.row(); i++)
			for (size_t j = 0; j < a.column(); j++) {
				c[j][i] = a[i][j];
			}
		return c;
	}

	template<typename T>
	Matrix<T> IdentityMatrix(size_t _Size, const T& _IdentityElement) {
		Matrix<T> a(_Size, T());
		for (size_t i = 0; i < _Size; i++) {
			a[i][i] = _IdentityElement;
		}
		return a;
	}

	template<typename T>
	Matrix<T> IdentityMatrix(size_t _Size) {
		Matrix<T> a(_Size, T());
		for (size_t i = 0; i < _Size; i++) {
			a[i][i] = T(1);
		}
		return a;
	}


	template<typename T> T Matrix<T>::Mod = T();

}
C++
namespace std {
#define TEMPLATE template<typename T, size_t R, size_t C>
#define MATRIX Matrix<T, R, C>
#define LOOP for (size_t i = 0; i < R; ++i) for (size_t j = 0; j < C; ++j)

	TEMPLATE class Matrix {

	private:
		T **v = nullptr;
		inline void _init() {
			if (v != nullptr) return;
			v = new T*[R];
			for (size_t i = 0; i < R; ++i)
				v[i] = new T[C];
			clear();
		}
		inline void _copy(const Matrix &b) { LOOP v[i][j] = b[i][j]; }
		inline void _copy(const T b[R][C]) { LOOP v[i][j] = b[i][j]; }

	public:
		Matrix() { _init(); }
		Matrix(const Matrix &x) { _init(), _copy(x); }
		Matrix(const T b[R][C]) { _init(), _copy(b); }
		~Matrix() {
			for (size_t i = 0; i < R; ++i)
				delete[] v[i];
			delete[] v;
		}

		inline size_t row() { return R; }
		inline size_t column() { return C; }
		inline void clear() { LOOP v[i][j] = 0; }
		inline void input(string a = "");
		inline void output(string c = "", string a = " ", string b = "\n");
		inline void init_unitMatrix() {
			for (size_t i = 0; i < min(R, C); ++i) v[i][i] = 1;
		}

		T* operator[] (int i) { return v[i]; }
		const T* operator[] (int i) const { return v[i]; }
		Matrix& operator= (const Matrix &b);
		Matrix& operator= (const T b[R][C]);
	};

	TEMPLATE inline void MATRIX::input(string a) {
		cout << a;
		LOOP cin >> v[i][j];
	}
	TEMPLATE inline void MATRIX::output(string c, string a, string b) {
		for (size_t i = 0; i < R; ++i) {
			for (size_t j = 0; j < C; ++j)
				cout << v[i][j] << a;
			if (i + 1 < R) cout << b;
		}
		cout << c;
	}

	TEMPLATE MATRIX operator+ (const MATRIX &a) { return a; }
	TEMPLATE MATRIX operator- (const MATRIX &a) {
		MATRIX c;
		LOOP c[i][j] = -a[i][j];
		return c;
	}
	TEMPLATE MATRIX operator+ (const MATRIX &a, const MATRIX &b) {
		MATRIX c;
		LOOP c[i][j] = a[i][j] + b[i][j];
		return c;
	}
	TEMPLATE MATRIX operator- (const MATRIX &a, const MATRIX &b) { return a + (-b); }
	TEMPLATE MATRIX operator* (const MATRIX &a, const T &b) {
		MATRIX c;
		LOOP c[i][j] = a[i][j] * b;
		return c;
	}
	// X: a.r, Y: a.c | b.r, Z: b.c
	template<typename T, size_t R, size_t K, size_t C>
	MATRIX operator* (const Matrix<T, R, K> &a, const Matrix<T, K, C> &b) {
		MATRIX c;
		for (size_t i = 0; i < R; ++i)
			for (size_t k = 0; k < K; ++k)
				for (size_t j = 0; j < C; ++j)
					c[i][j] += a[i][k] * b[k][j];
		return c;
	}
	template<typename T, size_t R>
	Matrix<T, R, R> operator^ (Matrix<T, R, R> a, size_t b) {
		Matrix<T, R, R> c;
		c.init_unitMatrix();
		while (b) {
			if (b & 1) c *= a;
			a *= a, b >>= 1;
		}
		return c;
	}

	TEMPLATE MATRIX& MATRIX::operator= (const MATRIX &b) { _copy(b); return *this; }
	TEMPLATE MATRIX& MATRIX::operator= (const T b[R][C]) { _copy(b); return *this; }
	TEMPLATE MATRIX& operator+= (MATRIX &a, const MATRIX &b) { a = a + b; return a; }
	TEMPLATE MATRIX& operator-= (MATRIX &a, const MATRIX &b) { a = a - b; return a; }
	TEMPLATE MATRIX& operator*= (MATRIX &a, const MATRIX &b) { a = a * b; return a; }
	TEMPLATE MATRIX& operator^= (MATRIX &a, const size_t &b) { a = a ^ b; return a; }

#undef TEMPLATE
#undef MATRIX
#undef LOOP
}