模板矩阵类

template <size_t row, size_t column, typename T = XDecimal>
class XMatrix {
public:
    XMatrix()
        : _row(row)
        , _column(column)
    {
        _vals = new T[row * column]{ 0.0 };
    }
    virtual ~XMatrix()
    {
        delete[] _vals;
    };

    bool isNull() const
    {
        return _vals == nullptr;
    };
    /** 获取行数 */
    size_t rowCount() const
    {
        return _row;
    }
    /** 获取列数 */
    size_t columnCount() const
    {
        return _column;
    }
    /** 获取元素值 */
    T value(size_t r, size_t c) const
    {
        return _vals[_column * r + c];
    }
    /** 设置元素值 */
    void setValue(size_t r, size_t c, T val)
    {
        _vals[_column * r + c] = val;
    }
    /** 矩阵转置 */
    virtual XMatrix& transpose()
    {
        auto* vals = new T[_column * _row];
        for(int r = 0; r < _row; ++r) {
            for(int c = 0; c < _column; ++c) {
                vals[c * _row + r] = _vals[r * _column + c];
            }
        }
        delete[] _vals;
        _vals   = vals;
        auto r  = _row;
        _row    = _column;
        _column = r;
        return *this;
    }
    /** 是否为方阵 */
    virtual bool isSquareMatrix() const
    {
        return _row == _column;
    }
    /** 是否为行矩阵 */
    virtual bool isRowMatrix() const
    {
        return _row == 1;
    }
    /** 是否为列矩阵 */
    virtual bool isColumnMatrix() const
    {
        return 1 == _column;
    }
    /** 是否为零矩阵 */
    virtual bool isZeroMatrix() const
    {
        for(int r = 0; r < _row; ++r) {
            for(int c = 0; c < _column; ++c) {
                if(!isZero(_vals[r * _column + c])) {
                    return false;
                }
            }
        }
        return true;
    }

    T*& operator[](size_t r)
    {
        T* mat[column];
        for(int i = 0; i < column; ++i) {
            mat[i] = &(_vals[_column * r + i]);
        }
        return *mat;
    }

    /** 重载运算符 XMatrix + otherXMatrix */
    XMatrix operator+(const XMatrix& m) const
    {
        XMatrix<row, column, T> mat;
        for(int r = 0; r < row; ++r) {
            for(int c = 0; c < column; ++c) {
                mat._vals[column * r + c] = this->_vals[column * r + c] + m._vals[column * r + c];
            }
        }
        return mat;
    }

    friend std::ostream& operator<<(std::ostream& outStream, const XMatrix& m)
    {
        if(m.isNull()) {
            outStream << "{}";
        } else {
            outStream << "{";
            for(int r = 0; r < m._row; ++r) {
                outStream << "[";
                for(int c = 0; c < m._column; ++c) {
                    char paddedNumber[25];
                    sprintf(paddedNumber, "%4.8f", m._vals[r * m._column + c]);
                    outStream << paddedNumber;
                    if(c < m._column) {
                        outStream << " ";
                    }
                }
                outStream << "] ";
            }
            outStream << "}";
        }

        return outStream;
    }

protected:
    /**
     * @brief 矩阵的数值(行主序)
     */
    T*     _vals{ nullptr };
    size_t _row{ 0 };
    size_t     _column{ 0 };
};

 

posted @ 2024-07-17 13:14  禅元天道  阅读(3)  评论(0编辑  收藏  举报