• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            公告

            聯系我:我的126郵箱: billhsu。 Locations of visitors to this page
            <2010年8月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            2930311234

            統計

            • 隨筆 - 41
            • 文章 - 0
            • 評論 - 82
            • 引用 - 0

            常用鏈接

            留言簿(16)

            隨筆分類

            隨筆檔案

            相冊

            Game Dev

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            矩陣求逆代碼

            12-12-2009更新:加入圖形化界面

            程序下載(含使用說明):http://www.shnenglu.com/Files/billhsu/MatInv.rar

            感覺線性代數作業里一直少不了矩陣求逆,

            寫個帶輸出算逆矩陣的步驟的矩陣求逆程序,希望給即將或正在學線代的同學一點方便。

            代碼寫的不好,大家見諒。

            /* ==================================
            ?*
            ?*??Copyright?(C)?Bill?Hsu?
            ?*???
            http://hi.baidu.com/probill
            ?*??2009-12-11
            ?**********************************
            */
            #include??
            < iostream >
            #include??
            < vector >
            #include??
            < math.h >
            using ??? namespace ??std;

            typedef?vector??
            < float > ??s_line;? // ?用來表示一行
            s_line?line;

            typedef?vector??
            < s_line > ??s_matrix;? // ?用來表示一個矩陣
            s_matrix?matrix;
            s_matrix?mat;
            int ??nSize;? // ?矩陣維數
            int ??nSign;? // ?標記行列式正負
            void ??outprint(s_matrix? & ??_mat);
            void ??printstep(s_matrix? & ??_mat);
            int ??step? = ? 0 ?;
            void ??line_add(s_matrix? & ??_mat,? int ??a,? int ??b,? float ??k? = ? 1.0f ?)? // ?第b行乘k加到第a行
            {
            int ??size? = ?_mat[? 0 ?].size();

            for ?(? int ??i? = ? 0 ?;i? < ?size;? ++ ?i)
            {
            _mat[a][i]?
            += ?_mat[b][i]? * ?k;

            }?
            // ?end?for
            }



            void ??work1(s_matrix? & ??_mat)? // ?主計算函數
            {

            for ?(? int ??i? = ? 1 ?;i? < ?nSize;? ++ ?i)
            {

            if ?(fabs(_mat[i? - ? 1 ?][i? - ? 1 ?])? < ? 0.000001 )
            {
            int ??mm;
            for ?(mm? = ?i;mm? < ?nSize;? ++ ?mm)
            {
            if ?(fabs(_mat[mm? - ? 1 ?][i? - ? 1 ?])? > ? 0.000001 ?)?? break ?;
            }?
            // ?end?for
            line_add(_mat,i? - ? 1 ?,mm? - ? 1 ?);
            }?
            // ?end?if

            for ?(? int ??j? = ?i;j? < ?nSize;? ++ ?j)
            {
            line_add(_mat,j,i?
            - ? 1 ?,? - ?_mat[j][i? - ? 1 ?]? / ?_mat[i? - ? 1 ?][i? - ? 1 ?]);

            }?
            // ?end?for?j
            printstep(_mat);
            }?
            // ?end?for?i

            }


            void ??work2(s_matrix? & ??_mat)? // ?第二部計算
            {
            for ?(? int ??i? = ?nSize? - ? 2 ?;i? >= ? 0 ?;? -- ?i)
            {
            for ?(? int ??j? = ?i;j? >= ? 0 ?;? -- ?j)
            {
            line_add(_mat,j,i?
            + ? 1 ?,? - ?_mat[j][i? + ? 1 ?]? / ?_mat[i? + ? 1 ?][i? + ? 1 ?]);
            }
            printstep(_mat);
            }

            }


            void ??makeunit(s_matrix? & ??_mat)? // ?單位化
            {

            mat.clear();

            for ?(? int ??i? = ? 0 ?;i? < ?nSize;? ++ ?i)
            {
            line.clear();
            for ?(? int ??j? = ? 0 ?;j? < ?nSize? * ? 2 ?;? ++ ?j)
            {
            float ??tmp? = ?_mat[i][j]? / ?_mat[i][i];
            if ?(fabs(tmp)? < ? 0.000001 ?)?tmp? = ? 0 ?;
            line.push_back(tmp);
            }
            mat.push_back(line);
            // ?cout<<endl;
            }
            _mat?
            = ?mat;
            }

            void ??printstep(s_matrix? & ??_mat)? // ?顯示求的過程
            {
            cout?
            << ? " ?第?? " ? <<++ ?step? << ? " ?步? " ? << ?endl;
            for ?(? int ??i? = ? 0 ?;i? < ?nSize;? ++ ?i)
            {

            for ??(? int ??j? = ? 0 ?;j? < ? 2 ? * ?nSize;? ++ ?j)
            {
            if ?(fabs(_mat[i][j])? < ? 0.000001 )?_mat[i][j]? = ? 0 ?;
            cout?
            << ?_mat[i][j]? << ? " ??? " ?;
            if ?(j? == ?nSize? - ? 1 ?)cout?? << ? " ??|?? " ?;
            }
            cout?
            << ?endl;
            }
            cout?
            << ?endl;

            }

            void ??outprint(s_matrix? & ??_mat)? // ?輸出函數
            {
            for ?(? int ??i? = ? 0 ?;i? < ?nSize;? ++ ?i)
            {

            for ??(? int ??j? = ?nSize;j? < ? 2 ? * ?nSize;? ++ ?j)
            {
            cout?
            << ?_mat[i][j]? << ? " ??? " ?;
            }
            cout?
            << ?endl;
            }


            }

            int ??main()
            {
            step?
            = ? 0 ?;
            matrix.clear();
            line.clear();
            cout?
            << ? " ?*********矩陣?求逆*********? " ? << ?endl;
            cout?
            << ? " ?*********Bill??Hsu*********? " ? << ?endl;
            cout?
            << ? " ?http://hi.baidu.com/probill? " ? << ?endl? << ?endl;

            cout?
            << ? " ?請輸入矩陣維數(輸入0退出):? " ?;?
            cin?
            >> ?nSize;
            if ?(nSize? <= ? 0 ?)?? return ??? 0 ?;
            for ?(? int ??i? = ? 0 ?;i? < ?nSize;? ++ ?i)
            {
            line.clear();?
            cout?
            << ? " ?輸入第? " ? << ?i? + ? 1 ? << ? " ??行:?? " ? << ?endl;
            for ??(? int ??j? = ? 0 ?;j? < ?nSize;? ++ ?j)?
            {
            float ??tmp;
            cin?
            >> ?tmp;
            line.push_back(tmp);??
            // ?壓入一個數到某行
            }

            for ??(? int ??j? = ? 0 ?;j? < ?nSize;? ++ ?j)?
            {
            if ?(i? == ?j)?line.push_back(? 1.0f ?);
            else ??line.push_back(? 0.0f ?);
            }


            matrix.push_back(line);??
            // ?壓入一行到矩陣
            }

            cout??
            << ?endl;
            work1(matrix);
            work2(matrix);
            makeunit(matrix);
            cout?
            << ?endl? << ? " ?########################? " ? << ?endl
            << ? " ?求逆結果:? " ? << ?endl;
            outprint(matrix);
            cout?
            << ? " ?########################? " ? << ?endl;

            main();


            return ??? 0 ?;????
            }

            有圖有真相:

            輸入矩陣數據

            計算步驟

            計算結果

            執行文件下載:http://www.shnenglu.com/Files/billhsu/%E7%9F%A9%E9%98%B5%E6%B1%82%E9%80%86.rar

            .

            posted on 2009-12-11 22:23 Bill Hsu 閱讀(6765) 評論(8)  編輯 收藏 引用 所屬分類: C/C++Algorithm

            評論

            # re: 矩陣求逆代碼 2009-12-12 13:10 凡客誠品官方網

            阿斯蘭卡卡拉斯揆理度勢
              回復  更多評論    

            # re: 矩陣求逆代碼 2009-12-18 09:10 rosamond

            如果會matlab的話,直接用inv()命令
              回復  更多評論    

            # re: 矩陣求逆代碼[未登錄] 2009-12-18 11:03 Bill Hsu

            @rosamond
            嗯,對的
              回復  更多評論    

            # re: 矩陣求逆代碼 2010-04-20 08:54 ccsdu2009

            我是如何計算矩陣求逆的
            首先使用matlab獲取矩陣求逆參數表達式
            然后換成c++表達式
            雖然沒什么技巧
            但是很簡便很很實用
              回復  更多評論    

            # re: 矩陣求逆代碼 2010-04-20 12:39 Bill Hsu

            @ccsdu2009
            同意
              回復  更多評論    

            # re: 矩陣求逆代碼 2010-06-08 22:28 zhaiduo

            好東西,收藏~
              回復  更多評論    

            # re: 矩陣求逆代碼 2011-01-17 11:28 唐同學

            謝謝你!
              回復  更多評論    

            # re: 矩陣求逆代碼 2011-11-25 11:05 C語言愛好者

            正需要這個資料,很給力
              回復  更多評論    
            97久久精品午夜一区二区| 久久亚洲中文字幕精品一区| 97精品依人久久久大香线蕉97| 久久美女人爽女人爽| 久久亚洲中文字幕精品有坂深雪| 久久久久亚洲国产| 日本五月天婷久久网站| 区亚洲欧美一级久久精品亚洲精品成人网久久久久 | 性做久久久久久久| 久久精品国产精品亚洲精品 | www性久久久com| 久久91精品久久91综合| 岛国搬运www久久| 久久影视综合亚洲| 精品国产乱码久久久久久呢| 久久精品a亚洲国产v高清不卡| 少妇人妻88久久中文字幕| 久久国产精品国产自线拍免费| 国产精品亚洲综合专区片高清久久久| 国产成人久久久精品二区三区| 久久久久久A亚洲欧洲AV冫| 久久99热这里只有精品66| 久久久久久久久久久久中文字幕 | 国内精品久久久久国产盗摄| 午夜精品久久久久9999高清| 亚洲va中文字幕无码久久 | 久久久久成人精品无码中文字幕| 久久久91精品国产一区二区三区| 久久久久亚洲AV综合波多野结衣 | 东京热TOKYO综合久久精品| 国产精品热久久无码av| 久久九九兔免费精品6| 亚洲成人精品久久| 久久精品国产亚洲AV影院| 色综合久久综合网观看| 亚洲综合日韩久久成人AV| 精品国产婷婷久久久| av午夜福利一片免费看久久| 国内精品久久国产| 久久99精品久久久久久野外 | 久久亚洲私人国产精品|