首页 > 其他分享 >矩阵类

矩阵类

时间:2024-07-17 11:54:33浏览次数:11  
标签:const column 矩阵 XMatrix vals row size

头文件:

    class X_MATH_EXPORT XMatrix {
    public:
        XMatrix() = delete;
        XMatrix(size_t row, size_t column, XDecimal val);
        XMatrix(size_t row, size_t column, const XDecimal vals[]);
        XMatrix(size_t row, size_t column, std::initializer_list<XDecimal> vals);
        XMatrix(const XMatrix& m);
        virtual ~XMatrix();

        /** 获取行数 */
        size_t                   getRowCount() const;
        /** 获取列数 */
        size_t                   getColumnCount() const;
        /** 获取元素值 */
        XDecimal                   getValue(size_t row, size_t column) const;
        /** 设置元素值 */
        void                   setValue(size_t row, size_t column, XDecimal val);
        /** 矩阵转置 */
        virtual XMatrix&       transpose();
        /** 是否为方阵 */
        virtual bool           isSquareMatrix() const;
        /** 是否为行矩阵 */
        virtual bool           isRowMatrix() const;
        /** 是否为列矩阵 */
        virtual bool           isColumnMatrix() const;
        /** 是否为零矩阵 */
        virtual bool           isZeroMatrix() const;
        /** 重载运算符 XMatrix == otherXMatrix */
        bool                   operator==(const XMatrix& m) const;
        /** 重载运算符 XMatrix != otherXMatrix */
        bool                   operator!=(const XMatrix& m) const;
        /** 重载运算符 XMatrix += otherXMatrix */
        XMatrix&               operator+=(const XMatrix& m);
        /** 重载运算符 XMatrix -= otherXMatrix */
        XMatrix&               operator-=(const XMatrix& m);
        /** 重载运算符 XMatrix *= XDecimal */
        XMatrix&               operator*=(XDecimal val);
        /** 重载运算符 XMatrix /= XDecimal */
        XMatrix&               operator/=(XDecimal val);

        // XDecimal*&               operator[](size_t row);

        /** 重载运算符 XMatrix + otherXMatrix */
        XMatrix                   operator+(const XMatrix& m) const;
        /** 重载运算符 XMatrix - otherXMatrix */
        XMatrix                   operator-(const XMatrix& m) const;
        /** 重载运算符 XMatrix * otherXMatrix */
        XMatrix                   operator*(const XMatrix& m) const;
        /** 重载运算符 XMatrix * XDecimal */
        XMatrix                   operator*(XDecimal val) const;
        /** 重载运算符 XMatrix / XDecimal */
        XMatrix                   operator/(XDecimal val) const;
        /** 重载运算符 -XMatrix */
        friend X_MATH_EXPORT XMatrix operator-(const XMatrix& m);
        /** 重载运算符 XDecimal * XMatrix */
        friend X_MATH_EXPORT XMatrix operator*(XDecimal factor, const XMatrix& m);
        /** 重载运算符 ostream << XMatrix */
        friend X_MATH_EXPORT std::ostream& operator<<(std::ostream& outStream, const XMatrix& m);

    protected:
        XMatrix(size_t row, size_t column);
        XMatrix& swapRow(size_t row1, size_t row2);
        XMatrix& swapColumn(size_t col1, size_t col2);
        XMatrix& addTimesRow(size_t targetRow, size_t sourceRow, XDecimal times = 1.0);
        XMatrix& addTimesColumn(size_t targetCol, size_t sourceCol, XDecimal times = 1.0);

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

 

源文件:

XMatrix::XMatrix(size_t row, size_t column, XDecimal val)
        : _row(row)
        , _column(column)
    {
        XAssert(row != 0 && column != 0);
        size_t count = row * column;
        _vals         = new XDecimal[count];
        // 手动填充数组,避免编译器默认处理不同的情况
        for(size_t i = 0; i < count; ++i) {
            _vals[i] = val;
        }
    }

    XMatrix::XMatrix(size_t row, size_t column, const XDecimal* vals)
        : _row(row)
        , _column(column)
    {
        XAssert(row != 0 && column != 0);
        size_t count = row * column;
        _vals         = new XDecimal[count];
        for(size_t i = 0; i < count; ++i) {
            _vals[i] = vals[i];
        }
    }

    XMatrix::XMatrix(size_t row, size_t column, std::initializer_list<XDecimal> vals)
        : _row(row)
        , _column(column)
    {
        size_t count = row * column;
        XAssert(vals.size() == count);
        _vals         = new XDecimal[count];
        size_t index = 0;
        for(auto it = vals.begin(); index < count, it != vals.end(); ++it) {
            _vals[index] = *it;
            ++index;
        }
    }

    XMatrix::XMatrix(const XMatrix& m)
        : _row(m._row)
        , _column(m._column)
    {
        if(m._vals == nullptr)
            return;
        size_t count = _row * _column;
        _vals         = new XDecimal[count];
        for(size_t i = 0; i < count; ++i) {
            _vals[i] = m._vals[i];
        }
    }
    XMatrix::~XMatrix()
    {
        if(_vals != nullptr) {
            delete[] _vals;
            _vals = nullptr;
        }
    }

    size_t XMatrix::getRowCount() const
    {
        return _row;
    }
    size_t XMatrix::getColumnCount() const
    {
        return _column;
    }
    XDecimal XMatrix::getValue(size_t row, size_t column) const
    {
        XAssert(row < _row && column < _column);
        return _vals[_column * row + column];
    }
    void XMatrix::setValue(size_t row, size_t column, XDecimal val)
    {
        XAssert(row < _row && column < _column);
        _vals[_column * row + column] = val;
    }

    bool XMatrix::operator==(const XMatrix& m) const
    {
        if(_row != m._row || _column != m._column)
            return false;
        size_t count = _row * _column;
        for(size_t i = 0; i < count; ++i) {
            if(!X::isEqual(_vals[i], m._vals[i]))
                return false;
        }
        return true;
    }
    bool XMatrix::operator!=(const XMatrix& m) const
    {
        return !(*this == m);
    }
    XMatrix& XMatrix::operator+=(const XMatrix& m)
    {
        XAssert(_row == m._row && _column == m._column);
        size_t count = _row * _column;
        for(size_t i = 0; i < count; ++i) {
            _vals[i] += m._vals[i];
        }
        return *this;
    }
    XMatrix& XMatrix::operator-=(const XMatrix& m)
    {
        XAssert(_row == m._row && _column == m._column);
        size_t count = _row * _column;
        for(size_t i = 0; i < count; ++i) {
            _vals[i] -= m._vals[i];
        }
        return *this;
    }
    XMatrix& XMatrix::operator*=(XDecimal val)
    {
        size_t count = _row * _column;
        for(size_t i = 0; i < count; ++i) {
            _vals[i] *= val;
        }
        return *this;
    }
    XMatrix& XMatrix::operator/=(XDecimal val)
    {
        size_t count = _row * _column;
        for(size_t i = 0; i < count; ++i) {
            _vals[i] /= val;
        }
        return *this;
    }
    XMatrix XMatrix::operator+(const XMatrix& m) const
    {
        XAssert(_row == m._row && _column == m._column);
        XMatrix mat(_row, _column);
        size_t    count = _row * _column;
        for(size_t i = 0; i < count; ++i) {
            mat._vals[i] = _vals[i] + m._vals[i];
        }
        return mat;
    }
    XMatrix XMatrix::operator-(const XMatrix& m) const
    {
        XAssert(_row == m._row && _column == m._column);
        XMatrix mat(_row, _column);
        size_t    count = _row * _column;
        for(size_t i = 0; i < count; ++i) {
            mat._vals[i] = _vals[i] - m._vals[i];
        }
        return mat;
    }

    XMatrix XMatrix::operator*(const XMatrix& m) const
    {
        XAssert(_column == m._row);
        XMatrix mat(_row, m._column);
        XDecimal tempVal = 0.0;
        for(size_t row = 0; row < mat._row; ++row) {
            for(size_t col = 0; col < mat._column; ++col) {
                tempVal = 0.0;
                for(int i = 0; i < _column; ++i) {
                    tempVal += _vals[_column * row + i] * m._vals[m._column * i + col];
                }
                mat._vals[mat._column * row + col] = tempVal;
            }
        }
        return mat;
    }

    XMatrix XMatrix::operator*(XDecimal val) const
    {
        XMatrix mat(_row, _column);
        size_t    count = _row * _column;
        for(size_t i = 0; i < count; ++i) {
            mat._vals[i] = _vals[i] * val;
        }
        return mat;
    }
    XMatrix XMatrix::operator/(XDecimal val) const
    {
        XMatrix mat(_row, _column);
        size_t    count = _row * _column;
        for(size_t i = 0; i < count; ++i) {
            mat._vals[i] = _vals[i] / val;
        }
        return mat;
    }
    XMatrix operator-(const XMatrix& m)
    {
        XMatrix mat(m._row, m._column);
        size_t    count = m._row * m._column;
        for(size_t i = 0; i < count; ++i) {
            mat._vals[i] = -m._vals[i];
        }
        return mat;
    }
    XMatrix operator*(XDecimal val, const XMatrix& m)
    {
        return m * val;
    }
    std::ostream& operator<<(std::ostream& outStream, const XMatrix& m)
    {
        char paddedNumber[25];
        outStream << "{";
        for(int row = 0; row < m._row; ++row) {
            outStream << "[";
            for(int col = 0; col < m._column; ++col) {
                sprintf(paddedNumber, "% -4.8f", m._vals[row * m._column + col]);
                outStream << paddedNumber;
                if(col < m._column) {
                    outStream << " ";
                }
            }
            outStream << "] ";
        }
        outStream << "}";

        return outStream;
    }
    XMatrix::XMatrix(size_t row, size_t column)
        : _row(row)
        , _column(column)
    {
        XAssert(row != 0 && column != 0);
        _vals = new XDecimal[row * column];
    }

    XMatrix& XMatrix::swapRow(size_t row1, size_t row2)
    {
        XAssert(row1 < _row && row2 < _row);
        // 如果自己和自己交换则直接返回
        if(row1 == row2)
            return *this;
        XDecimal tempVal = 0.0;
        for(size_t col = 0; col < _column; ++col) {
            tempVal                              = _vals[_column * row1 + col];
            _vals[_column * row1 + col]          = _vals[_column * row2 + col];
            this->_vals[_column * row2 + col] = tempVal;
        }
        return *this;
    }
    XMatrix& XMatrix::swapColumn(size_t col1, size_t col2)
    {
        XAssert(col1 < _row && col2 < _row);
        // 如果自己和自己交换则直接返回
        if(col1 == col2)
            return *this;
        XDecimal tempVal = 0.0;
        for(size_t row = 0; row < _column; ++row) {
            tempVal                              = _vals[_column * row + col1];
            _vals[_column * row + col1]          = _vals[_column * row + col2];
            this->_vals[_column * row + col2] = tempVal;
        }
        return *this;
    }

    XMatrix& XMatrix::addTimesRow(size_t targetRow, size_t sourceRow, XDecimal times)
    {
        XAssert(targetRow < _row && sourceRow < _row);
        if(!X::isZero(times)) {
            for(size_t col = 0; col < _column; ++col) {
                _vals[_column * targetRow + col] += _vals[_column * sourceRow + col] * times;
            }
        }
        return *this;
    }
    XMatrix& XMatrix::addTimesColumn(size_t targetCol, size_t sourceCol, XDecimal times)
    {
        XAssert(targetCol < _row && sourceCol < _row);
        if(!X::isZero(times)) {
            for(size_t row = 0; row < _column; ++row) {
                _vals[_column * row + targetCol] += _vals[_column * row + sourceCol] * times;
            }
        }
        return *this;
    }

    XMatrix& XMatrix::transpose()
    {
        auto* vals = new XDecimal[_column * _row];
        for(int row = 0; row < _row; ++row) {
            for(int col = 0; col < _column; ++col) {
                vals[col * _row + row] = _vals[row * _column + col];
            }
        }
        delete[] _vals;
        _vals     = vals;
        auto row = _row;
        _row     = _column;
        _column     = row;
        return *this;
    }
    bool XMatrix::isSquareMatrix() const
    {
        return _row == _column;
    }
    bool XMatrix::isRowMatrix() const
    {
        return _row == 1;
    }
    bool XMatrix::isColumnMatrix() const
    {
        return _column == 1;
    }
    bool XMatrix::isZeroMatrix() const
    {
        for(int row = 0; row < _row; ++row) {
            for(int col = 0; col < _column; ++col) {
                if(!X::isZero(_vals[row * _column + col])) {
                    return false;
                }
            }
        }
        return true;
    }

 

标签:const,column,矩阵,XMatrix,vals,row,size
From: https://www.cnblogs.com/chanyuantiandao/p/18306977

相关文章

  • 机器学习分类结果精度测定 - 混淆矩阵(Confusion Matrix)
    一、引言机器学习和数据科学中一个经常被忽视,但至关重要的概念是模型评估。你可能已经建立了一个非常先进的模型,但如果没有合适的评估机制,你就无法了解模型的效能和局限性。这就是混淆矩阵(ConfusionMatrix)派上用场的地方。1.1什么是混淆矩阵?混淆矩阵是一种特定的表格布局......
  • 矩阵优化 DP 以及全局平衡二叉树实现的动态 DP 学习笔记
    矩阵乘法求斐波那契数列的第\(n\)项,其中\(n\le10^{18}\),对数\(m\)取模。写出转移方程,\(f_i=f_{i-1}+f_{i-2}\),直接算是\(O(n)\)的,似乎没什么办法优化。定义大小分别为\(n\timesp\)和\(p\timesm\)的矩阵(分别为\(a\)和\(b\))相乘的结果(矩阵\(c\))是一个大小为\(......
  • 【Leetcode--旋转矩阵】
    解题思路:先进行矩阵上下交换,接着对矩阵进行主对角线交换,就可以从上述左图变换为右图。classSolution{  publicvoidrotate(int[][]matrix){    //上下交换    for(inti=0;i<matrix.length/2;i++){      int[]temp=matrix[......
  • hnust 1963: 邻接矩阵表示法
    hnust1963:邻接矩阵表示法题目描述输入一个图,用邻接矩阵存储,并实现一些操作。拷贝下面的代码,按要求完成其中的FirstAdjVex,NextAdjVex和CreateUDG操作,其他地方不得改动。//邻接矩阵表示图#include<iostream>#include<iomanip>#include<cstdio>usingnamespaces......
  • 云微客短视频矩阵辅助短视频营销,让你获客没压力
    抖音依靠短视频赚取了庞大的用户基数,强大的算法为企业和创作者提供了无限的商业可能。现如今,数字营销快速发展,实体行业只做线下营销显然有些捉襟见肘,因此线上营销就成为了企业品牌与消费者互动的重要媒介。云微客布局短视频矩阵就是通过一系列各具特色的短视频内容,构建一个......
  • OpenJudge | 矩阵交换行
    总时间限制:1000ms内存限制:65536kB描述编写一个函数,输入参数是55的二维数组,和n,m两个行下标。功能:判断n,m是否在数组范围内,如果不在,则返回0;如果在范围内,则将n行和m行交换,并返回1。在main函数中,生成一个55的矩阵,输入矩阵数据,并输入n,m的值。调用前面的函数。如果返回值......
  • Lingo学习(二)——线性规划基础、矩阵工厂
    一、线性规划基础(一)方法①一个线性规划中只含一个目标函数。(两个以上是多目标线性规划,Lingo无法直接解)②求目标函数的最大值或最小值分别用max=…或min=…来表示。③以!开头,以;结束的语句是注释语句;④线性规划和非线性规划的本质区别是目标函数是否线性......
  • 输入一个正整数n (1≤ n ≤6),再输入一个n 行n列的矩阵,找出该矩阵中绝对值最大的元素
    /输入一个正整数n(1≤n≤6),再输入一个n行n列的矩阵,找出该矩阵中绝对值最大的元素以及它的行下标和列下标。/#include<stdio.h>voidfun(void){intn,i,j;intmax_val=0,max_row=0,max_col=0;printf("pleaseenternumber,1<=number<=6\n");......
  • 【数组、特殊矩阵的压缩存储】
    目录一、数组1.1、一维数组1.1.1、一维数组的定义方式1.1.2、一维数组的数组名1.2、二维数组1.2.1、二维数组的定义方式1.2.2、二维数组的数组名二、对称矩阵的压缩存储三、三角矩阵的压缩存储四、三对角矩阵的压缩存储五、稀疏矩阵的压缩存储一、数组概述:数组是......
  • 短视频平台搭建矩阵系统源码(搭建技术开发分享)
    #短视频矩阵#矩阵系统#矩阵源码搭建 技术自研框架开发背景:抖音账号矩阵系统是一种基于数据分析和管理的全新平台,能够帮助用户更好地管理、扩展和营销抖音账号,那矩阵的顾名思义就是可以批量话的制作和生成,来完成自己的需求和任务。//视频批量剪辑模式    $video......