C++矩阵类&操作符重载

来源:互联网 发布:ios文件上传java 接口 编辑:程序博客网 时间:2024/06/01 19:38

一年前刚刚告别了数值计算的学习,也就注定了我此生与矩阵计算越走越远,说来还有些伤感——今天帮助小学弟(妹)写c++,大致是类入门的一些东西。名义上我也是翻过Primer的人,但是要说实践经历,还真是几乎零经验。总之,之前研究过的矩阵类算是用上了。

矩阵类

之前自己用std::vector实现过,也玩过boost库里边的矩阵类(boost/numeric/bindings; boost真是一个神奇的东西),还有整个库只包含头文件的Eigen,和号称能翻译matlab的Armadillo,此外听说MKLtntOpenBlasOpenCV,甚至是itpp(一个符号计算库)都能用来算矩阵。自己实现的用起来就是舒服。部分实现如下:

class matrix: public vector< vector<double> >{        size_t m,n;    public:        matrix() {}        matrix(const size_t a, const size_t b): m(a), n(b)        {            this -> resize(m);            for (size_t i = 0; i < m ; i++ )                (*this)[i].resize(n);        }        double operator () (const size_t i, const size_t j) const        {            return (*this)[i][j];        }        matrix& operator = (const matrix& rhs)        {            m = rhs.m;            n = rhs.n;            this -> resize(m);            for (size_t i = 0; i < m ; i++ )            {                (*this)[i].resize(n);                for (size_t j = 0; j < n; j++)                     (*this)[i][j] = rhs[i][j];            }            return *this;        }};

总之,就是两重vector直接继承过来。这样最大的好处是使用方便。比方说:

matrix A(2,2);A[0][0] = 1;A[0][1] = 2;A[1][0] = 3;A[1][1] = 4;cout << A << endl;

而不是

matrix A(2,2);A(0,0) = 1;A(0,1) = 2;...

大整数&操作符重载

不管是Primer还是老,对重载一事说得很明白了,反而新版(Ed.6)的Primer对此一笔带过。不亲自实现一下总是无法领悟到奥秘。

class CHugeInt{    public:        CHugeInt(const int a)        {            char* str = new char[MAX];            itoa(a, str, 10);            n = strlen(str);            e.resize(n);            for (size_t i = 0; i < n; i++)                 e[i] = str[n-i-1] - '0';        }        CHugeInt(const char* c)        {            n = strlen(c);            e.resize(n);            for (size_t i = 0; i < n; i++)                 e[i] = c[n-i-1] - '0';        }        CHugeInt& operator = (const CHugeInt& rhs)        {            n = rhs.n;            e = rhs.e;            e.resize(n);            for (size_t i = 0; i < n; i++)                 e[i] = rhs.e[i];            return *this;        }        CHugeInt& operator += (const CHugeInt& lhs)        {            *this = *this + lhs;            return *this;        }        CHugeInt& operator ++ ()        {            *this = *this + 1;            return *this;        }        CHugeInt operator ++ (int)        {            CHugeInt tmp(*this);            ++(*this);            return tmp;        }        friend CHugeInt operator + (const CHugeInt& lhs, const CHugeInt& rhs)        {            size_t a, b;            a = max(lhs.n, rhs.n);            b = min(lhs.n, rhs.n);            CHugeInt sum = ( lhs.n > rhs.n ) ? lhs : rhs;            short tmp = 0;            for (size_t i = 0; i < a; i++)             {                if (i < b)                    sum.e[i] = lhs.e[i] + rhs.e[i] + tmp;                else                    sum.e[i] += tmp;                if (sum.e[i] > 9)                {                    sum.e[i] -= 10;                    tmp = 1;                }                else                    tmp = 0;            }            if (tmp)            {                sum.n ++;                sum.e.resize(sum.n);                sum.e[sum.n-1] = 1;            }            return sum;        }        friend std::ostream& operator << (std::ostream& os, const CHugeInt& rhs)        {            for (size_t i = 0; i < rhs.n; i++)                 os << rhs.e[rhs.n-i-1];            return os;        }    private:        // digit capacity        size_t n;        // digits        vector<short> e;};

总之是个很简单的东西。

————————-分割线————————-

交完差人家说自己是初学者,不让用STL,我说我没用啊,他(她)说vector不是STL吗?服了,这么说来我也算是半个STL的精通者了——呵呵,还差得远呢。
贴个二维数组版的矩阵。

--- q3.cpp  2015-04-10 15:35:31.000000000 +0800+++ q3_nonVector.cpp    2015-04-10 16:41:18.560025299 +0800@@ -1,23 +1,27 @@ #include <iostream>-#include <vector> using namespace std;-class Array2: public vector< vector<double> >+class Array2 {    public:        Array2() {}        Array2(const size_t a, const size_t b): m(a), n(b)        {-           this -> resize(m);-           for (size_t i = 0; i < m ; i++ )-               (*this)[i].resize(n);+           e = new double*[m];+           for (size_t i = 0; i < m; i++) +               e[i] = new double[n];+       }++       double* operator [] (const size_t i)+       {+           return e[i];        }        double operator () (const size_t i, const size_t j) const        {-           return (*this)[i][j];+           return this->e[i][j];        }        Array2& operator = (const Array2& rhs)@@ -25,18 +29,19 @@            m = rhs.m;            n = rhs.n;-           this -> resize(m);-           for (size_t i = 0; i < m ; i++ )+           e = new double*[m];+           for (size_t i = 0; i < m; i++)            {-               (*this)[i].resize(n);+               e[i] = new double[n];                for (size_t j = 0; j < n; j++) -                   (*this)[i][j] = rhs[i][j];+                   this->e[i][j] = rhs.e[i][j];            }            return *this;        }    private:        size_t m,n;+       double** e; };
0 0
原创粉丝点击