tiny-matrix
轻量级矩阵类
 
载入中...
搜索中...
未找到
Matrix.h
浏览该文件的文档.
1
101#ifndef _Matrix_H
102#define _Matrix_H
103
104#include <stdlib.h>
105#include <string.h>
106#include <iostream>
107#include <iomanip>
108#include <initializer_list>
109
110typedef std::initializer_list<double> MatIniLst; // 初始化列表
111
120{
121 // ---- 成员变量 ----
122private:
123 int miRow = 0; // 行数
124 int miCol = 0; // 列数
125 double *mpBuf = nullptr; // 存储矩阵
126 // ---- 静态成员变量 ----
127 static int iPrecise; // 控制流输出的显示精度
128
129public:
130 // ---- 构造析构函数 ----
131
137 explicit Matrix();
138
145 Matrix(double num);
146
153 Matrix(const MatIniLst &lst);
154
161 Matrix(const std::initializer_list<MatIniLst> &lst);
162
170 explicit Matrix(const std::initializer_list<MatIniLst> &lst, int col);
171
181 explicit Matrix(double *p, int row, int col, int count);
182
187 Matrix(const Matrix &tmp);
188
193 Matrix(Matrix &&tmp) noexcept;
194
198 ~Matrix();
199
200 // ---- 静态成员函数 ----
201
209 static Matrix eye(int row);
210
219 static Matrix eye(int row, int col);
220
228 static Matrix ones(int row);
229
238 static Matrix ones(int row, int col);
239
247 static Matrix zeros(int row);
248
257 static Matrix zeros(int row, int col);
258
266 static Matrix diag(Matrix mat);
267
275 static Matrix randMatrix(int n);
276
285 static Matrix randMatrix(int row, int col);
286
293 static void setPrecise(int precise);
294
295 // ---- 运算符重载 ----
296
304 Matrix &operator=(const Matrix &tmp);
305
313 Matrix &operator=(Matrix &&tmp) noexcept;
314
323 friend Matrix operator+(const Matrix &lMat, const Matrix &rMat);
324
333 friend Matrix operator-(const Matrix &lMat, const Matrix &rMat);
334
343 friend Matrix operator*(const Matrix &lMat, const Matrix &rMat);
344
353 friend Matrix operator*(const double &lFactor, const Matrix &rMat);
354
363 friend Matrix operator*(const Matrix &lMat, const double &rFactor);
364
373 friend Matrix operator/(const Matrix &lMat, const Matrix &rMat);
374
383 friend Matrix operator/(const double &lFactor, const Matrix &rMat);
384
393 friend Matrix operator/(const Matrix &lMat, const double &rFactor);
394
403 friend Matrix operator+(const Matrix &lMat, const double &rNum);
404
413 friend Matrix operator+(const double &lNum, const Matrix &rMat);
414
423 friend Matrix operator-(const Matrix &lMat, const double &rNum);
424
433 friend Matrix operator-(const double &lNum, const Matrix &rMat);
434
442 Matrix &operator+=(const Matrix &tmp);
443
451 Matrix &operator+=(const double &num);
452
460 Matrix &operator-=(const Matrix &tmp);
461
469 Matrix &operator-=(const double &num);
470
479 friend bool operator==(const Matrix &lMat, const Matrix &rMat);
488 friend bool operator!=(const Matrix &lMat, const Matrix &rMat);
489
497 friend Matrix operator-(const Matrix &Mat);
505 friend Matrix operator+(const Matrix &Mat);
506
514
522 Matrix operator++(int);
523
531
539 Matrix operator--(int);
540
552 double *operator[](int num);
564 const double *operator[](int num) const;
565
575 friend std::ostream &operator<<(std::ostream &os, const Matrix &tmp);
576
577public:
583 int getRow() const;
584
590 int getCol() const;
591
599 Matrix add(const Matrix &tmp) const;
600
608 Matrix sub(const Matrix &tmp) const;
609
617 Matrix rMultiple(const Matrix &tmp) const;
618
626 Matrix sMultiple(const double &factor) const;
627
634 Matrix transpose() const;
635
643 bool det(double &result) const;
644
651 Matrix gauss() const;
652
659 Matrix inverse() const;
660
667 int rank() const;
668
676 bool isSizeValidity();
677
685 bool isSquareMatrix() const;
686
694 bool isEmptyMatrix() const;
695
704 void rowAdd(int des, int src, double factor);
705
715 bool rowExchange(int des, int src);
716
724 void rowMultiply(int des, double factor);
725
734 static Matrix lrMerge(Matrix &left, Matrix &right);
735
744 static Matrix udMerge(Matrix &up, Matrix &down);
745
757 Matrix getBlock(int startRowId, int startColId, int blockRow, int blockCol) const;
758
769 Matrix &setBlock(int startRowId, int startColId, const Matrix &block);
770
779 Matrix hessenberg(Matrix &Q) const;
780
788 void qrDecom(Matrix &Q, Matrix &R) const;
789
797 Matrix eigen() const;
798
799private:
807 static signed char sgn(double num);
808
816 Matrix merge(Matrix &right) const;
817
825 Matrix lrDivide(int col) const;
826
838 bool qrDecom4Hessenberg(Matrix &Q, Matrix &R) const;
839};
840
841#endif // !_Matrix
std::initializer_list< double > MatIniLst
Definition Matrix.h:110
本类用于进行矩阵的基本计算
Definition Matrix.h:120
friend Matrix operator*(const Matrix &lMat, const Matrix &rMat)
运算符重载,友元函数
Definition Matrix.cpp:357
void qrDecom(Matrix &Q, Matrix &R) const
公有函数
Definition Matrix.cpp:929
void rowMultiply(int des, double factor)
公有函数
Definition Matrix.cpp:797
friend bool operator!=(const Matrix &lMat, const Matrix &rMat)
运算符重载,友元函数
Definition Matrix.cpp:441
friend bool operator==(const Matrix &lMat, const Matrix &rMat)
运算符重载,友元函数
Definition Matrix.cpp:429
Matrix & operator=(const Matrix &tmp)
运算符重载,拷贝赋值函数
Definition Matrix.cpp:313
double * operator[](int num)
运算符重载
Definition Matrix.cpp:488
Matrix()
无参数构造函数
Definition Matrix.cpp:17
static void setPrecise(int precise)
静态函数
Definition Matrix.cpp:242
static Matrix randMatrix(int n)
静态函数
Definition Matrix.cpp:284
Matrix getBlock(int startRowId, int startColId, int blockRow, int blockCol) const
公有函数
Definition Matrix.cpp:836
Matrix & operator-=(const Matrix &tmp)
运算符重载
Definition Matrix.cpp:417
static Matrix udMerge(Matrix &up, Matrix &down)
静态函数
Definition Matrix.cpp:825
int getCol() const
公有函数
Definition Matrix.cpp:521
bool rowExchange(int des, int src)
公有函数
Definition Matrix.cpp:782
bool isSizeValidity()
公有函数
Definition Matrix.cpp:740
static Matrix zeros(int row)
静态函数
Definition Matrix.cpp:217
bool det(double &result) const
公有函数
Definition Matrix.cpp:614
static Matrix lrMerge(Matrix &left, Matrix &right)
静态函数
Definition Matrix.cpp:806
static Matrix eye(int row)
静态函数
Definition Matrix.cpp:151
friend Matrix operator/(const Matrix &lMat, const Matrix &rMat)
运算符重载,友元函数
Definition Matrix.cpp:372
Matrix transpose() const
公有函数
Definition Matrix.cpp:600
static Matrix ones(int row)
静态函数
Definition Matrix.cpp:184
Matrix sMultiple(const double &factor) const
公有函数
Definition Matrix.cpp:562
bool isEmptyMatrix() const
公有函数
Definition Matrix.cpp:763
Matrix & operator++()
运算符重载
Definition Matrix.cpp:457
friend Matrix operator-(const Matrix &lMat, const Matrix &rMat)
运算符重载,友元函数
Definition Matrix.cpp:352
static Matrix diag(Matrix mat)
静态函数
Definition Matrix.cpp:248
Matrix sub(const Matrix &tmp) const
公有函数
Definition Matrix.cpp:545
~Matrix()
析构函数
Definition Matrix.cpp:140
Matrix add(const Matrix &tmp) const
公有函数
Definition Matrix.cpp:527
Matrix & setBlock(int startRowId, int startColId, const Matrix &block)
公有函数
Definition Matrix.cpp:857
Matrix inverse() const
公有函数
Definition Matrix.cpp:727
Matrix & operator+=(const Matrix &tmp)
运算符重载
Definition Matrix.cpp:406
Matrix hessenberg(Matrix &Q) const
公有函数
Definition Matrix.cpp:878
Matrix rMultiple(const Matrix &tmp) const
公有函数
Definition Matrix.cpp:573
bool isSquareMatrix() const
公有函数
Definition Matrix.cpp:757
Matrix & operator--()
运算符重载
Definition Matrix.cpp:469
void rowAdd(int des, int src, double factor)
公有函数
Definition Matrix.cpp:773
friend std::ostream & operator<<(std::ostream &os, const Matrix &tmp)
运算符重载,友元函数
Definition Matrix.cpp:498
Matrix gauss() const
公有函数
Definition Matrix.cpp:660
int rank() const
公有函数
Definition Matrix.cpp:710
Matrix eigen() const
公有函数
Definition Matrix.cpp:976
int getRow() const
公有函数
Definition Matrix.cpp:516
friend Matrix operator+(const Matrix &lMat, const Matrix &rMat)
运算符重载,友元函数
Definition Matrix.cpp:347