码迷,mamicode.com
首页 > 其他好文 > 详细

矩阵类

时间:2018-11-04 11:06:29      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:print   row   int   []   ase   pen   +=   pow   struct   

无取模

template <typename Int>
struct Matrix {
    Int m[M][M];
    int row, column;
    Matrix() {
        for (int i = 0; i < M; i += 1)
            for (int j = 0; j < M; j += 1)
                m[i][j] = 0;
    }
    Matrix(int _r) : Matrix() {
        row = _r, column = _r;
        for (int i = 0; i < row; i += 1) f[i][i] = 1;
    }
    Matrix(int _r, int _c) : Matrix(){ row = _r, column = _c; }
    Int* operator [](int row) const {
        return (Int*)m[row];
    }
    void Show() {
        puts("\nbegin matrix");
        for (int i = 0; i < row; i += 1) {
            for (int j = 0; j < column; j += 1)
                printf("%.3f ", m[i][j]);
            puts("");
        }
        puts("end matrix\n");
    }
    Matrix operator * (const Matrix& o) const {
        Matrix res(Matrix(row, o.column));
        for (int i = 0; i < row; i += 1)
            for (int j = 0; j < o.column; j += 1) {
                for (int k = 0; k < column; k += 1) 
                    res.m[i][j] += m[i][k] * o.m[k][j];
            }
        return res;
    }
    Matrix operator ^ (int pow) const {
        Matrix res(Matrix(row)), base(Matrix(row, row));
        while (pow) {
            if (pow & 1) res = res * base;
            base = base * base;
            pow >>= 1;
        }
        return res;
    }
};

矩阵类

标签:print   row   int   []   ase   pen   +=   pow   struct   

原文地址:https://www.cnblogs.com/qdscwyy/p/9903124.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!