矩阵重载模板

struct mat {
    ll a[4][4], n;
    mat() { memset(a, 0, sizeof(a)); }
    void matE() {
        for (int i = 0; i < n; i++)a[i][i] = 1;
    }
    inline mat operator+(const mat &T) const {
        mat res;
        res.n = n;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                res.a[i][j] = (a[i][j] + T.a[i][j] + mod) % mod;
        return res;
    }
    inline mat operator-(const mat &T) const {
        mat res;
        res.n = n;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                res.a[i][j] = (a[i][j] - T.a[i][j] + mod) % mod;
        return res;
    }
    inline mat operator*(const mat &T) const {
        mat res;
        res.n = n;
        ll r;
        for (int i = 0; i < n; ++i)
            for (int k = 0; k < n; ++k) {
                r = a[i][k];
                for (int j = 0; j < n; ++j)
                    res.a[i][j] += T.a[k][j] * r,
                            res.a[i][j] %= mod;
            }
        return res;
    }
    inline mat pow(ll x) {
        mat res, bas;
        res.n = bas.n = n;
        for (int i = 0; i < n; i++) res.a[i][i] = 1;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++) bas.a[i][j] = a[i][j];
        while (x) {
            if (x & 1) res = res * bas;
            x >>= 1;
            bas = bas * bas;
        }
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++) a[i][j] = res.a[i][j];
        return res;
    }
}f,a,res;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是矩阵模板 matrix 的完整实现代码: ```cpp #include <iostream> #include <cstring> using namespace std; template<typename T> class matrix { public: matrix(int a, int b); matrix(const matrix<T>& other); ~matrix(); matrix<T>& operator=(const matrix<T>& other); matrix<T> operator+(const matrix<T>& other) const; matrix<T> operator-(const matrix<T>& other) const; matrix<T> operator*(const matrix<T>& other) const; T& operator()(int i, int j); const T& operator()(int i, int j) const; int rows() const; int cols() const; private: int m_rows; int m_cols; T* m_data; }; template<typename T> matrix<T>::matrix(int a, int b) : m_rows(a), m_cols(b), m_data(new T[a * b]) {} template<typename T> matrix<T>::matrix(const matrix<T>& other) : m_rows(other.m_rows), m_cols(other.m_cols), m_data(new T[other.m_rows * other.m_cols]) { memcpy(m_data, other.m_data, sizeof(T) * m_rows * m_cols); } template<typename T> matrix<T>::~matrix() { delete[] m_data; } template<typename T> matrix<T>& matrix<T>::operator=(const matrix<T>& other) { if (this != &other) { delete[] m_data; m_rows = other.m_rows; m_cols = other.m_cols; m_data = new T[other.m_rows * other.m_cols]; memcpy(m_data, other.m_data, sizeof(T) * m_rows * m_cols); } return *this; } template<typename T> matrix<T> matrix<T>::operator+(const matrix<T>& other) const { if (m_rows != other.m_rows || m_cols != other.m_cols) { throw "Matrices must have the same dimensions."; } matrix<T> res(m_rows, m_cols); for (int i = 0; i < m_rows; i++) { for (int j = 0; j < m_cols; j++) { res(i, j) = (*this)(i, j) + other(i, j); } } return res; } template<typename T> matrix<T> matrix<T>::operator-(const matrix<T>& other) const { if (m_rows != other.m_rows || m_cols != other.m_cols) { throw "Matrices must have the same dimensions."; } matrix<T> res(m_rows, m_cols); for (int i = 0; i < m_rows; i++) { for (int j = 0; j < m_cols; j++) { res(i, j) = (*this)(i, j) - other(i, j); } } return res; } template<typename T> matrix<T> matrix<T>::operator*(const matrix<T>& other) const { if (m_cols != other.m_rows) { throw "Number of columns in first matrix must match number of rows in second matrix."; } matrix<T> res(m_rows, other.m_cols); for (int i = 0; i < m_rows; i++) { for (int j = 0; j < other.m_cols; j++) { T sum = 0; for (int k = 0; k < m_cols; k++) { sum += (*this)(i, k) * other(k, j); } res(i, j) = sum; } } return res; } template<typename T> T& matrix<T>::operator()(int i, int j) { if (i < 0 || i >= m_rows || j < 0 || j >= m_cols) { throw "Index out of range."; } return m_data[i * m_cols + j]; } template<typename T> const T& matrix<T>::operator()(int i, int j) const { if (i < 0 || i >= m_rows || j < 0 || j >= m_cols) { throw "Index out of range."; } return m_data[i * m_cols + j]; } template<typename T> int matrix<T>::rows() const { return m_rows; } template<typename T> int matrix<T>::cols() const { return m_cols; } ``` 该矩阵模板支持以下操作: - 构造函数 `matrix(int a, int b)`:创建一个行数为 `a`,列数为 `b` 的矩阵,所有元素的初值为默认值(例如,0)。 - 拷贝构造函数 `matrix(const matrix<T>& other)`:创建一个与 `other` 完全相同的矩阵。 - 析构函数 `~matrix()`:释放矩阵占用的内存。 - 赋值运算符 `matrix<T>& operator=(const matrix<T>& other)`:将本矩阵赋值为 `other`。 - 加法运算符 `matrix<T> operator+(const matrix<T>& other) const`:返回本矩阵与 `other` 相加的结果。要求本矩阵与 `other` 的行列数相同。 - 减法运算符 `matrix<T> operator-(const matrix<T>& other) const`:返回本矩阵与 `other` 相减的结果。要求本矩阵与 `other` 的行列数相同。 - 乘法运算符 `matrix<T> operator*(const matrix<T>& other) const`:返回本矩阵与 `other` 相乘的结果。要求本矩阵的列数等于 `other` 的行数。 - 括号运算符 `T& operator()(int i, int j)` 和 `const T& operator()(int i, int j) const`:分别用于访问矩阵中第 `i` 行、第 `j` 列的元素。若下标越界,则抛出异常。 - `int rows() const` 和 `int cols() const`:分别返回矩阵的行数和列数。 使用时,需要先定义一个矩阵模板实例,例如: ```cpp matrix<int> m1(2, 3); matrix<int> m2(3, 2); ``` 然后就可以使用矩阵模板中定义的各种运算符进行矩阵计算。例如: ```cpp matrix<int> m3 = m1 + m2; matrix<int> m4 = m1 - m2; matrix<int> m5 = m1 * m2; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值