{"id":22,"date":"2012-04-09T21:04:22","date_gmt":"2012-04-09T13:04:22","guid":{"rendered":"http:\/\/fishinbox.tk\/?p=22"},"modified":"2012-04-09T21:04:22","modified_gmt":"2012-04-09T13:04:22","slug":"calculate-the-determinant-of-a-square-matrix","status":"publish","type":"post","link":"https:\/\/zhuangzr.me\/wordpress\/2012\/04\/calculate-the-determinant-of-a-square-matrix\/","title":{"rendered":"Calculate the Determinant of A Square Matrix"},"content":{"rendered":"<p>This is a code that I wrote weeks ago, mainly to calculate the determinant of a squae matrix.<\/p>\n<p>It&#8217;s written in c++. Enjoy it!<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\"> #include &lt;iostream&gt;\nusing namespace std;\ntemplate&lt;typename T&gt;\nclass Matrix{\n\tT **m;\n\tint n;\n\tvoid rot90(){\n\t\tT ** tmp=new T*&#x5B;n];\n\t\tfor(int i=0;i&lt;n;i++){\n\t\t\ttmp&#x5B;i]=new T&#x5B;n];\n\t\t\tfor(int j=0;j&lt;n;j++){\n\t\t\t\ttmp&#x5B;i]&#x5B;j]=m&#x5B;i]&#x5B;j];\n\t\t\t}\n\t\t}\n\t\tfor(int i=0;i&lt;n;i++){\n\t\t\tfor(int j=0;j&lt;n;j++){\n\t\t\t\tm&#x5B;i]&#x5B;j]=tmp&#x5B;n-1-j]&#x5B;i];\n\t\t\t}\n\t\t}\n\t}\n\tMatrix&lt;t&gt; M(int i,int j){\/\/get the matrix corresponds with specific minor Mij\n\t\tMatrix&lt;t&gt; TMP(n-1);\n\t\tfor(int a=0;a&lt;n-1;a++){\n\t\t\tfor(int b=0;b&lt;n-1;b++){\n\t\t\t\tint c=0,d=0;\n\t\t\t\tif(a&lt;i) c=a;\n\t\t\t\telse c=a+1;\n\t\t\t\tif(b&lt;j) d=b;\n\t\t\t\telse d=b+1;\n\t\t\t\tTMP.m&#x5B;a]&#x5B;b]=m&#x5B;c]&#x5B;d];\n\t\t\t}\n\t\t}\n\t\treturn TMP;\n\t}\n\tT C(int i,int j){\/\/get cofactor Cij\n\t\tif((i+j)%2==0) return M(i,j).Det();\n\t\telse return -1*M(i,j).Det();\n\t}\n\tT A(int i,int j){\n\t\treturn m&#x5B;i]&#x5B;j]*C(i,j);\n\t}\npublic :\n\n\tMatrix(int para){\n\t\tn=para;\n\t\tm=new T*&#x5B;n];\n\t\tfor(int i=0;i&lt;n;i++){\n\t\t\tm&#x5B;i]=new T&#x5B;n];\n\t\t\tfor(int j=0;j&lt;n;j++) m&#x5B;i]&#x5B;j]=0;\n\t\t}\n\t}\n\tMatrix(){\n\t\tint itmp=0;\n\t\tdo{\n\t\t\tcout&lt;&lt;&quot;Please input the square matrix's  n, and n has to be larger than 1&quot;&lt;&lt;endl;\n\t\t\tcin&gt;&gt;itmp;\n\t\t}\n\t\twhile(itmp&lt;2);\n\t\tn=itmp;\n\t\tm=new T*&#x5B;n];\n\t\tfor(int i=0;i&lt;n;i++){\n\t\t\tm&#x5B;i]=new T&#x5B;n];\n\t\t\tfor(int j=0;j&lt;n;j++) m&#x5B;i]&#x5B;j]=0;\n\t\t}\n\t}\n\tvoid Input(){\n\t\tcout&lt;&lt;&quot;Please initialize the matrix&quot;&lt;&lt;endl;\n\t\tfor(int i=0;i&lt;n;i++){\n\t\t\tfor(int j=0;j&lt;n;j++) cin&gt;&gt;m&#x5B;i]&#x5B;j];\n\t\t}\n\t\tcout&lt;&lt;&quot;Initialization done&quot;&lt;&lt;endl;\n\t}\n\tvoid Display(){\n\t\tfor(int i=0;i&lt;n;i++){\n\t\t\tfor(int j=0;j&lt;n;j++){\n\t\t\t\tcout&lt;&lt;m&#x5B;i]&#x5B;j];\n\t\t\t\tif(j!=n-1) cout&lt;&lt;&quot; &quot;;\n\t\t\t}\n\t\t\tcout&lt;&lt;endl;\n\t\t}\n\t}\n\tvoid Display(int w){\n\t\tfor(int i=0;i&lt;n;i++){\n\t\t\tfor(int j=0;j&lt;n;j++){\n\t\t\t\tcout&lt;&lt;setw(w)&lt;&lt;m&#x5B;i]&#x5B;j];\n\t\t\t}\n\t\t\tcout&lt;&lt;endl;\n\t\t}\n\t}\n\tvoid Display(int i,int j){\n\t\tcout&lt;&lt;m&#x5B;i]&#x5B;j]&lt;&lt;endl;\n\t}\n\tvoid Rotation(int times=1){\n\t\tfor(;times&gt;3;times-=4){}\n\t\tfor(;times&lt;0;times+=4){}\n\t\tif(times!=0){\n\t\t\tif(times==1) rot90();\n\t\t\telse{\n\t\t\t\trot90();\n\t\t\t\tRotation(times-1);\n\t\t\t}\n\t\t}\n\t}\n\tT Det(){\n\t\tif(n==2) return m&#x5B;0]&#x5B;0]*m&#x5B;1]&#x5B;1]-m&#x5B;0]&#x5B;1]*m&#x5B;1]&#x5B;0];\n\t\telse{\n\t\t\tT sum=0;\n\t\t\tfor(int i=0;i&lt;n;i++) sum+=A(0,i);\n\t\t\treturn sum;\n\t\t}\n\t}\n}; <\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This is a code that I wrote weeks ago, mainly to calculate the determinant of a squae matrix. It&#8217;s written in c++. Enjoy it! #include &lt;iostream&gt; using namespace std; template&lt;typename T&gt; class Matrix{ T **m; int n; void rot90(){ T ** tmp=new T*&#x5B;n]; for(int i=0;i&lt;n;i++){ tmp&#x5B;i]=new T&#x5B;n]; for(int j=0;j&lt;n;j++){ tmp&#x5B;i]&#x5B;j]=m&#x5B;i]&#x5B;j]; } } for(int i=0;i&lt;n;i++){ for(int &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/zhuangzr.me\/wordpress\/2012\/04\/calculate-the-determinant-of-a-square-matrix\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Calculate the Determinant of A Square Matrix&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[25,40,63],"class_list":["post-22","post","type-post","status-publish","format-standard","hentry","category-programming","tag-c","tag-matrix","tag-programming"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/zhuangzr.me\/wordpress\/wp-json\/wp\/v2\/posts\/22","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuangzr.me\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuangzr.me\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuangzr.me\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuangzr.me\/wordpress\/wp-json\/wp\/v2\/comments?post=22"}],"version-history":[{"count":0,"href":"https:\/\/zhuangzr.me\/wordpress\/wp-json\/wp\/v2\/posts\/22\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhuangzr.me\/wordpress\/wp-json\/wp\/v2\/media?parent=22"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuangzr.me\/wordpress\/wp-json\/wp\/v2\/categories?post=22"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuangzr.me\/wordpress\/wp-json\/wp\/v2\/tags?post=22"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}