跳到主要內容

[Matrix] 矩陣與向量乘積

最近對線性代數應用感興趣,開始研究線性代數運算的底層程式實作。
先從矩陣乘積開始。
在此用 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++)
    {
        for(size_t j = 0; j < 2; j++)
            b(i, 0) = b(i, 0) + a(i, j) * x(j, 0);
    }

    cout << " a:" << endl;
    a.info();
    cout << " x:" << endl;
    x.info();
    cout << " ax = b, b:" << endl;
    b.info();

執行結果
 a:

 2 1
 3 5
 4 7

 x:

 10
 12

 ax = b, b:

 32
 90
 124

做乘法的部份,重點在這段 Code:
    for(size_t r = 0; r < 3; r++)    b(r, 0) = 0;

    for(size_t i = 0; i < 3; i++)
    {
        for(size_t j = 0; j < 2; j++) // row 水平方向掃描
            b(i, 0) = b(i, 0) + a(i, j) * x(j, 0);
    }

這段 Code 在執行 ax = b 的矩陣乘積。這種寫法視 a 矩陣跟向量 x 做內積。
若把兩個 for 迴圈對調,變成 x 內的每個元素,先對 a 矩陣的 column vector 內每個分量先相乘,我們觀看一下寫法:
    for(size_t r = 0; r < 3; r++)    b(r, 0) = 0;

    for(size_t j = 0; j < 2; j++)
    {
        for(size_t i = 0; i < 3; i++) // column 垂直方向掃描
            b(i, 0) = b(i, 0) + a(i, j) * x(j, 0);
    }

兩種寫法的執行結果相同。但執行速度根據矩陣儲存方式會有差異,因為二維陣列在記憶體內儲存方式,是同 row 的值存在同一區域。
因此我推論:
上述第二種寫法在 a 矩陣有很多個 row 時,執行速度較慢,因為內層迴圈讓 a 矩陣取值一次跳一個 row。
而第一種寫法,內層迴圈只讓 a 取值一次跳一個 column,在相鄰記憶體位置取值。而對於 x 和 b 向量而言,都是在相鄰記憶體位置讀寫。

我之後會補充矩陣向量乘積執行速度的實驗在此。


留言

這個網誌中的熱門文章

[Linux] Elementary OS 字體調校

用 gesetting 取得 elementary os 的等寬字體(這也是終端機默認字體): gsettings get org.gnome.desktop.interface monospace-font-name 會顯示目前字型跟字體大小: Roboto Mono 10 設定字體大小: gsettings set org.gnome.desktop.interface monospace-font-name 'Roboto Mono 12' 可以微調 text-scaling-factor: gsettings set org.gnome.desktop.interface text-scaling-factor <value>

[Vim] 我的 .vimrc

我用過的文字編輯器有 sublime text/atom/notepad++/gedit 等等。 後來我在 window 和 Linux 寫程式還是比較慣用 vim。 以下整理我常用的 .vimrc:  set nu "show line number. set rnu "related line number. nmap ; : "replace ';' with ':' in normal mode. set ai "auto indent set mouse=a "activate mouse set expandtab "replace tab with space. set tabstop=4 "replace tab with 4 spaces. set bg=dark "background set to dark. color desert "color scheme. syntax on "enable syntax highlight. " auto complete: inoremap ( ()<Esc>i inoremap ' ''<Esc>i inoremap " ""<Esc>i inoremap [ []<Esc>i inoremap { {}<Esc>i set cursorline " active cursorline 到此 vim 搭配 Ctrl + N 當自動補全,已經很好用了,之後有好用設定會再補上來。

[心得] 復古、老派的程式設計之路

最近看到 John Carmack 2018 年的貼文中的幾段話: I’m not a Unix geek.  I get around ok, but I am most comfortable developing in Visual Studio on Windows.  I thought a week of full immersion work in the old school Unix style would be interesting, even if it meant working at a slower pace.  It was sort of an adventure in retro computing — this was fvwm and vi.  Not vim, actual BSD vi. In the end, I didn’t really explore the system all that much, with 95% of my time in just the basic vi / make / gdb operations.  I appreciated the good man pages, as I tried to do everything within the self contained system, without resorting to internet searches.  Seeing references to 30+ year old things like Tektronix terminals was amusing. In the spirit of my retro theme, I had printed out several of Yann LeCun’s old papers and was considering doing everything completely off line, as if I was actually in a mountain cabin somewhere, but I wound up watching a lot of the Stanford CS231N lectur...