{"id":449,"date":"2009-06-06T22:20:00","date_gmt":"2009-06-06T02:20:00","guid":{"rendered":""},"modified":"2013-11-17T13:05:11","modified_gmt":"2013-11-17T05:05:11","slug":"%e7%9f%a9%e9%98%b5%e7%9a%84%e5%90%84%e7%a7%8d%e8%bf%90%e7%ae%97%e6%ba%90%e7%a0%81%ef%bc%88c%ef%bc%89%e3%80%80","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/449.html","title":{"rendered":"\u77e9\u9635\u7684\u5404\u79cd\u8fd0\u7b97\u6e90\u7801\uff08C#\uff09\u3000"},"content":{"rendered":"<p>\u77e9\u9635\u7c7b\uff1a<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing System;\r\nusing System.Collections.Generic;\r\nusing System.Linq;\r\nusing System.Text;\r\nusing System.Text.RegularExpressions;\r\nusing System.IO;\r\n\r\nnamespace Matrice\r\n{\r\n    public struct NNMatrix\r\n    {\r\n        public int row, col;\r\n        public double&#x5B;,] Matrix;\r\n\r\n        public NNMatrix(int Mrow, int Mcol)  \/\/\u6307\u5b9a\u884c\u5217\u6570\u521b\u5efa\u77e9\u9635\uff0c\u521d\u59cb\u503c\u4e3a0\u77e9\u9635\r\n        {\r\n            row = Mrow;\r\n            col = Mcol;\r\n            Matrix = new double&#x5B;row, col];\r\n            for (int i = 0; i &lt; row; i++)\r\n                for (int j = 0; j &lt; col; j++)\r\n                    Matrix&#x5B;i, j] = 0;\r\n        }\r\n\r\n        public static NNMatrix operator +(NNMatrix m1, NNMatrix m2)   \/\/\u77e9\u9635\u52a0\u6cd5\r\n        {\r\n            NNMatrix temp = new NNMatrix(m1.row, m1.col);\r\n            if (m1.row == m2.row &amp;&amp; m1.col == m2.col)\r\n            {\r\n                for (int i = 0; i &lt; m1.row; i++)\r\n                    for (int j = 0; j &lt; m2.col; j++)\r\n                         temp.Matrix&#x5B;i,j]=m1.Matrix&#x5B;i, j] + m2.Matrix&#x5B;i, j];\r\n            }\r\n            return (temp);\r\n        }\r\n\r\n        public static NNMatrix operator +(NNMatrix m1, double m2)    \/\/\u77e9\u9635\u52a0\u5e38\u91cf\r\n        {\r\n            NNMatrix temp = new NNMatrix(m1.row, m1.col);\r\n            for (int i = 0; i &lt; m1.row; i++)\r\n                for (int j = 0; j &lt; m1.col; j++)\r\n                    temp.Matrix&#x5B;i,j]=m1.Matrix&#x5B;i, j] + m2;\r\n            return (temp);\r\n        }\r\n\r\n        public static NNMatrix operator -(NNMatrix m1, NNMatrix m2)  \/\/\u77e9\u9635\u51cf\u6cd5\r\n        {\r\n            NNMatrix temp = new NNMatrix(m1.row, m1.col);\r\n            if (m1.row == m2.row &amp;&amp; m1.col == m2.col)\r\n            {\r\n                for (int i = 0; i &lt; m1.row; i++)\r\n                    for (int j = 0; j &lt; m2.col; j++)\r\n                        temp.Matrix&#x5B;i,j]=m1.Matrix&#x5B;i, j] - m2.Matrix&#x5B;i, j];\r\n            }\r\n            return (temp);\r\n        }\r\n\r\n        public static NNMatrix operator *(NNMatrix m1, NNMatrix m2) \/\/\u77e9\u9635\u4e58\u6cd5\r\n        {\r\n            int m3r = m1.row;\r\n            int m3c = m2.col;\r\n            NNMatrix m3 = new NNMatrix(m3r, m3c);\r\n\r\n            if (m1.col == m2.row)\r\n            {\r\n                double value = 0.0;\r\n                for (int i = 0; i &lt; m3r; i++)\r\n                    for (int j = 0; j &lt; m3c; j++)\r\n                    {\r\n                        for (int ii = 0; ii &lt; m1.col; ii++)\r\n                            value += m1.Matrix&#x5B;i, ii] * m2.Matrix&#x5B;ii, j];\r\n                        m3.Matrix&#x5B;i, j] = value;\r\n                        value = 0.0;\r\n                    }\r\n            }\r\n            else\r\n                throw new Exception(&quot;\u77e9\u9635\u7684\u884c\/\u5217\u6570\u4e0d\u5339\u914d\u3002&quot;);\r\n            return m3;\r\n        }\r\n\r\n        public static NNMatrix operator *(NNMatrix m1, double m2) \/\/\u77e9\u9635\u4e58\u4ee5\u5e38\u91cf\r\n        {\r\n            for (int i = 0; i &lt; m1.row; i++)\r\n                for (int j = 0; j &lt; m1.col; j++)\r\n                    m1.Matrix&#x5B;i, j] *= m2;\r\n            return (m1);\r\n        }\r\n\r\n        public static NNMatrix Transpos(NNMatrix srcm)  \/\/\u77e9\u9635\u8f6c\u79e9\r\n        {\r\n            NNMatrix tmpm = new NNMatrix(srcm.col, srcm.row);\r\n            for (int i = 0; i &lt; srcm.row; i++)\r\n                for (int j = 0; j &lt; srcm.col; j++)\r\n                {\r\n                    if (i != j)\r\n                    {\r\n                        tmpm.Matrix&#x5B;j, i] = srcm.Matrix&#x5B;i, j];\r\n                    }\r\n                    else\r\n                        tmpm.Matrix&#x5B;i, j] = srcm.Matrix&#x5B;i, j];\r\n                }\r\n            return tmpm;\r\n        }\r\n\r\n        private static void swaper(double m1, double m2) \/\/\u4ea4\u6362\r\n        {\r\n            double sw;\r\n            sw = m1; m1 = m2; m2 = sw;\r\n        }\r\n\r\n        \/* \u5b9e\u77e9\u9635\u6c42\u9006\u7684\u5168\u9009\u4e3b\u5143\u9ad8\u65af\uff0d\u7ea6\u5f53\u6cd5 *\/\r\n        public static NNMatrix Invers(NNMatrix srcm)           \/\/\u77e9\u9635\u6c42\u9006\r\n        {\r\n            NNMatrix temp = new NNMatrix(srcm.row, srcm.col);\r\n            for (int i = 0; i &lt; srcm.row; i++)\r\n                for (int j = 0; j &lt; srcm.col; j++)\r\n                    temp.Matrix&#x5B;i, j] = srcm.Matrix&#x5B;i, j];\r\n            int rhc = temp.row;\r\n            if (temp.row == temp.col)\r\n            {\r\n                int&#x5B;] iss = new int&#x5B;rhc];\r\n                int&#x5B;] jss = new int&#x5B;rhc];\r\n                double fdet = 1;\r\n                double f = 1;\r\n                \/\/\u6d88\u5143\r\n                for (int k = 0; k &lt; rhc; k++)\r\n                {\r\n                    double fmax = 0;\r\n                    for (int i = k; i &lt; rhc; i++)\r\n                    {\r\n                        for (int j = k; j &lt; rhc; j++)\r\n                        {\r\n                            f = Math.Abs(temp.Matrix&#x5B;i, j]);\r\n                            if (f &gt; fmax)\r\n                            {\r\n                                fmax = f;\r\n                                iss&#x5B;k] = i;\r\n                                jss&#x5B;k] = j;\r\n                            }\r\n                        }\r\n                    }\r\n                    if (iss&#x5B;k] != k)\r\n                    {\r\n                        f = -f;\r\n                        for (int ii = 0; ii &lt; rhc; ii++)\r\n                        {\r\n                            swaper(temp.Matrix&#x5B;k, ii], temp.Matrix&#x5B;iss&#x5B;k], ii]);\r\n                        }\r\n                    }\r\n                    if (jss&#x5B;k] != k)\r\n                    {\r\n                        f = -f;\r\n                        for (int ii = 0; ii &lt; rhc; ii++)\r\n                        {\r\n                            swaper(temp.Matrix&#x5B;k, ii], temp.Matrix&#x5B;jss&#x5B;k], ii]);\r\n                        }\r\n                    }\r\n                    fdet *= temp.Matrix&#x5B;k, k];\r\n                    temp.Matrix&#x5B;k, k] = 1.0 \/ temp.Matrix&#x5B;k, k];\r\n                    for (int j = 0; j &lt; rhc; j++)\r\n                        if (j != k)\r\n                            temp.Matrix&#x5B;k, j] *= temp.Matrix&#x5B;k, k];\r\n                    for (int i = 0; i &lt; rhc; i++)\r\n                        if (i != k)\r\n                            for (int j = 0; j &lt; rhc; j++)\r\n                                if (j != k)\r\n                                    temp.Matrix&#x5B;i, j] = temp.Matrix&#x5B;i, j] - temp.Matrix&#x5B;i, k] * temp.Matrix&#x5B;k, j];\r\n                    for (int i = 0; i &lt; rhc; i++)\r\n                        if (i != k)\r\n                            temp.Matrix&#x5B;i, k] *= -temp.Matrix&#x5B;k, k];\r\n                }\r\n                \/\/ \u8c03\u6574\u6062\u590d\u884c\u5217\u6b21\u5e8f\r\n                for (int k = rhc - 1; k &gt;= 0; k--)\r\n                {\r\n                    if (jss&#x5B;k] != k)\r\n                        for (int ii = 0; ii &lt; rhc; ii++)\r\n                            swaper(temp.Matrix&#x5B;k, ii], temp.Matrix&#x5B;jss&#x5B;k], ii]);\r\n                    if (iss&#x5B;k] != k)\r\n                        for (int ii = 0; ii &lt; rhc; ii++)\r\n                            swaper(temp.Matrix&#x5B;k, ii], temp.Matrix&#x5B;iss&#x5B;k], ii]);\r\n                }\r\n            }\r\n            return temp;\r\n        }\r\n\r\n        \/*\u6c42\u884c\u5217\u5f0f\u503c*\/\r\n        public static double ComputeDet(NNMatrix m)\r\n        {\r\n            int i, j, k, nis = 0, js = 0;\r\n            double f, det, q, d;\r\n            \/\/ \u521d\u503c\r\n            f = 1.0;\r\n            det = 1.0;\r\n            \/\/ \u6d88\u5143\r\n            for (k = 0; k &lt;= m.col - 2; k++)\r\n            {\r\n                q = 0.0;\r\n                for (i = k; i &lt;= m.col - 1; i++)\r\n                {\r\n                    for (j = k; j &lt;= m.col - 1; j++)\r\n                    {\r\n                        d = Math.Abs(m.Matrix&#x5B;j, i]);\r\n                        if (d &gt; q)\r\n                        {\r\n                            q = d;\r\n                            nis = i;\r\n                            js = j;\r\n                        }\r\n                    }\r\n                }\r\n                if (q == 0.0)\r\n                {\r\n                    det = 0.0;\r\n                    return (det);\r\n                }\r\n                if (nis != k)\r\n                {\r\n                    f = -f;\r\n                    for (j = k; j &lt;= m.col - 1; j++)\r\n                    {\r\n                        d = m.Matrix&#x5B;j, k];\r\n                        m.Matrix&#x5B;j, k] = m.Matrix&#x5B;j, nis];\r\n                        m.Matrix&#x5B;j,nis] = d;\r\n                    }\r\n                }\r\n                if (js != k)\r\n                {\r\n                    f = -f;\r\n                    for (i = k; i &lt;= m.col - 1; i++)\r\n                    {\r\n                        d = m.Matrix&#x5B;js,i];\r\n                        m.Matrix&#x5B;js,i] = m.Matrix&#x5B;k,i];\r\n                        m.Matrix&#x5B;k,i] = d;\r\n                    }\r\n                }\r\n                det = det * m.Matrix&#x5B;k, k];\r\n                for (i = k + 1; i &lt;= m.col - 1; i++)\r\n                {\r\n                    d = m.Matrix&#x5B;k, i] \/ m.Matrix&#x5B;k, k];\r\n                    for (j = k + 1; j &lt;= m.col - 1; j++)\r\n                    {\r\n                        m.Matrix&#x5B;j, i] = m.Matrix&#x5B;j, i] - d * m.Matrix&#x5B;j, k];\r\n                    }\r\n                }\r\n            }\r\n            det = f * det * m.Matrix&#x5B;m.row-1, m.col-1];\r\n            return (det);\r\n        }\r\n\r\n        \/*\u4ece\u6587\u672c\u6587\u4ef6\u4e2d\u8bfb\u53d6\u77e9\u9635*\/\r\n        public static NNMatrix FromText(string filename)\r\n        {\r\n            StreamReader reader = new StreamReader(filename);\r\n            string text = &quot;&quot;;\r\n            int rows = 0; int cols = 0;\r\n            while ((text = reader.ReadLine()) != null)\r\n            {\r\n                if (text.Trim() != &quot;&quot;)\r\n                {\r\n                    rows++;\r\n                    Regex reg = new Regex(@&quot;\\s{1,}&quot;);\r\n                    string&#x5B;] list = reg.Split(text);\r\n                    cols = list.Length;\r\n                }\r\n            }\r\n            reader.Close();\r\n            NNMatrix temp = new NNMatrix(rows, cols);\r\n            reader = new StreamReader(filename);\r\n            int n = 0;\r\n            while ((text = reader.ReadLine()) != null)\r\n            {\r\n                text = text.Trim();\r\n                if (text != &quot;&quot;)\r\n                {\r\n                    Regex reg = new Regex(@&quot;\\s{1,}&quot;);\r\n                    string&#x5B;] list = reg.Split(text);\r\n                    for (int i = 0; i &lt; list.Length; i++)\r\n                    {\r\n                        temp.Matrix&#x5B;n, i] = Convert.ToDouble(list&#x5B;i]);                         \r\n                    }\r\n                    n++;\r\n                }\r\n            }\r\n            return temp;\r\n\r\n        }\r\n\r\n        public string MatrixPrint()   \/\/\u77e9\u9635\u8f93\u51fa\r\n        {\r\n            string tmprst;\r\n            tmprst = &quot;\\n&quot;;\r\n            for (int i = 0; i &lt; row; i++)\r\n            {\r\n                for (int j = 0; j &lt; col; j++)\r\n                {\r\n                    tmprst += Matrix&#x5B;i, j].ToString() + &quot;\\t&quot;;\r\n                }\r\n                tmprst += &quot;\\n&quot;;\r\n            }\r\n            return tmprst;\r\n        }\r\n    }\r\n\r\n}\r\n<\/pre>\n<p>\u6d4b\u8bd5\uff1a<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing System;\r\nusing System.Collections.Generic;\r\nusing System.Linq;\r\nusing System.Text;\r\nusing Matrice;\r\nusing MyMatrix;\r\n\r\nnamespace ConsoleApplication1\r\n{\r\n    class Program\r\n    {\r\n        static void Main(string&#x5B;] args)\r\n        {\r\n            NNMatrix m = new NNMatrix(2, 2);\r\n            m.Matrix&#x5B;0, 0] = 1.2545544; m.Matrix&#x5B;0, 1] = 2.4545; m.Matrix&#x5B;1, 0] = 3.124; m.Matrix&#x5B;1, 1] = 4.258;\r\n\r\n            NNMatrix n = new NNMatrix(2, 2);\r\n            n.Matrix&#x5B;0, 0] = 2; n.Matrix&#x5B;0, 1] = 3; n.Matrix&#x5B;1, 0] = 4; n.Matrix&#x5B;1, 1] = 5;\r\n\r\n            NNMatrix a = new NNMatrix(2, 2);\r\n            a = m + n; \/\/\u77e9\u9635\u52a0\u6cd5\r\n            \/\/Console.WriteLine(m.MatrixPrint());\r\n            \/\/Console.WriteLine(n.MatrixPrint());\r\n            \/\/Console.WriteLine(a.MatrixPrint());\r\n\r\n            a = n - m; \/\/\u77e9\u9635\u51cf\u6cd5\r\n            \/\/Console.WriteLine(m.MatrixPrint());\r\n            \/\/Console.WriteLine(n.MatrixPrint());\r\n            \/\/Console.WriteLine(a.MatrixPrint());\r\n\r\n            a = NNMatrix.Transpos(n); \/\/\u77e9\u9635\u8f6c\u7f6e\r\n            \/\/Console.WriteLine(n.MatrixPrint());\r\n            \/\/Console.WriteLine(a.MatrixPrint());\r\n\r\n            a = m * n;  \/\/\u77e9\u9635\u4e58\u6cd5\r\n            \/\/Console.WriteLine(m.MatrixPrint());\r\n            \/\/Console.WriteLine(n.MatrixPrint());\r\n            \/\/Console.WriteLine(a.MatrixPrint());\r\n\r\n            a = NNMatrix.Invers(m);  \/\/\u77e9\u9635\u6c42\u9006\r\n            \/\/Console.WriteLine(a.MatrixPrint());\r\n            \/\/Console.WriteLine(m.MatrixPrint());\r\n\r\n            double si = m.Matrix&#x5B;1, 1];\/\/\u503c\u5e8f\u53f7\u4ece0\u5f00\u59cb\r\n            \/\/Console.WriteLine(si.ToString());\r\n\r\n            double det = NNMatrix.ComputeDet(m);\/\/\u6c42\u884c\u5217\u5f0f\r\n            \/\/Console.WriteLine(det.ToString());\r\n\r\n            NNMatrix c=NNMatrix.FromText(&quot;1.txt&quot;);\/\/\u4ece\u6587\u672c\u4e2d\u8bfb\u53d6\u77e9\u9635\r\n            \/\/Console.WriteLine(c.MatrixPrint());\r\n        }\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u77e9\u9635\u7c7b\uff1a using System; using System.Collections.Generic; us [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-449","post","type-post","status-publish","format-standard","hentry","category-code_related"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/449","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=449"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/449\/revisions"}],"predecessor-version":[{"id":4617,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/449\/revisions\/4617"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}