最近對線性代數應用感興趣,開始研究線性代數運算的底層程式實作。 先從矩陣乘積開始。 在此用 C++ 做矩陣與向量的乘積。 我們定義以下的矩陣 Class: template <class T, size_t, size_t j> class mtx { public: mtx(){} ~mtx(){} inline T& operator()(size_t i, size_t j) { return arr_[i][j]; } void info() { cout << endl; for(size_t i = 0; i < ROWS; i++) { for(size_t j = 0; j < COLS; j++) cout << " " << arr_[i][j]; cout << endl; } cout << endl; } private: T arr_[ROWS][COLS]; }; 接著我們就可以使用此 Class 來做矩陣與向量乘積: mtx a; mtx x; mtx b; a(0, 0) = 2; a(0, 1) = 1; a(1, 0) = 3; a(1, 1) = 5; a(2, 0) = 4; a(2, 1) = 7; x(0, 0) = 10; x(1, 0) = 12; // matrix mul: for(size_t r = 0; r < 3; r++) b(r, 0) = 0; for(size_t i = 0; i < 3; i+...
Coding for Passion, Solving Problems