首页 > 技术文章 > 线性代数 | 行列式的计算

TQCAI 2017-10-14 17:13 原文

Java代码:

  1 import java.util.*;
  2 import java.io.*;
  3 
  4 public class Main {
  5 
  6     public static void main(String[] args) {
  7         
  8         
  9         int[][] matrix1={
 10                 {3,-2},
 11                 {2,1}
 12         };
 13         int[][] matrix2={
 14                 {1,2,-4},
 15                 {-2,2,1},
 16                 {-3,4,-2}
 17         };        
 18         Determinant D=new Determinant(matrix2);
 19         int ans=D.get();
 20         System.out.print(ans);
 21     }
 22 
 23 }
 24 
 25 class Determinant{
 26     int N=0;//行列式阶数
 27     int [][] D;//行列式
 28     int ans=0;
 29     Determinant(){}
 30     Determinant(String path){//通过文件路径,输入文件来读取邻接矩阵
 31         String content=MyFile.readFile(path);//读取文件内容
 32         String lines[]=content.split("\n");
 33         String probe[];
 34         N=lines.length;
 35         D=new int[N][N];//刻画邻接矩阵大小
 36         int i,j;
 37         for(i=0;i<lines.length;i++){
 38             probe=lines[i].split(" ");
 39             for(j=0;j<probe.length;j++){
 40                 D[i][j]=Integer.valueOf(probe[j]).intValue();//要对integer进行拆包
 41             }
 42         }
 43         calcAns();
 44     }
 45     Determinant(int[][] matrix){
 46         N=matrix.length;
 47         D=new int[N][N];//刻画邻接矩阵大小
 48         int i,j;
 49         for(i=0;i<N;i++) for(j=0;j<N;j++) D[i][j]=matrix[i][j];
 50         calcAns();
 51     }
 52     private void calcAns(){
 53         int i,j,k;
 54         int x[][]=getFullArrangement(N);
 55         for(i=0;i<x.length;i++){//对于每个下标的排列结果
 56             int sum=1;
 57             for(j=0;j<N;j++){//对于每一行
 58                 sum*=D[j][x[i][j]];
 59             }
 60             sum*=(InverseNum(x[i])%2==1 ? -1 : 1);
 61             ans+=sum;
 62         }
 63     }
 64     public int get(){
 65         return ans;
 66     }
 67     private int InverseNum(int[] vec){
 68         int i,j;
 69         int len=vec.length;
 70         int re=0;
 71         for(i=0;i<len-1;i++){
 72             for(j=i+1;j<len;j++){
 73                 if(vec[i]>vec[j])
 74                     re++;
 75             }
 76         }
 77         return re;
 78     }
 79     private int[][] getFullArrangement(int n){
 80         int [][] re;
 81         if(n>1){
 82             int [][] pre=getFullArrangement(n-1);
 83             int row=pre.length;
 84             int col=pre[0].length;
 85             re=new int[row*n][n];
 86             int i,j,k;
 87             for(i=0;i<row;i++){
 88                 for(k=0;k<n;k++){
 89                     //拷贝父结点
 90                     for(j=0;j<n-1;j++)
 91                         re[i*n+k][j]=pre[i][j];
 92                     re[i*n+k][n-1]=n-1;
 93                     swap(re[i*n+k],n-1,k);
 94                 }
 95             }
 96         }else{
 97             re=new int[1][1];
 98             re[0][0]=0;
 99         }
100         return re;
101     }
102     private void swap(int[] arr,int a,int b){
103         int tmp=arr[a];
104         arr[a]=arr[b];
105         arr[b]=tmp;
106     }
107 }
108 
109 class MyFile{
110 
111     /**
112      * 创建文件
113      * @param fileName  文件名称
114      * @param filecontent   文件内容
115      * @return  是否创建成功,成功则返回true
116      */
117     public static boolean createFile(String fileName){
118         boolean bool = false;
119         File file = new File(fileName);
120         try {
121             //如果文件不存在,则创建新的文件
122             if(!file.exists()){
123                 file.createNewFile();
124                 bool = true;
125                 System.out.println("success create file,the file is "+fileName);
126             }
127         } catch (Exception e) {
128             e.printStackTrace();
129         }
130         return bool;
131     }
132     
133     /**
134      * 向文件中写入内容
135      * @param filepath 文件路径与名称
136      * @param newstr  写入的内容
137      * @return
138      * @throws IOException
139      */
140     public static boolean writeFile(String filepath,String newstr) throws IOException{
141         boolean bool = false;
142         String filein = newstr+"\r\n";//新写入的行,换行
143         String temp  = "";
144         
145         FileInputStream fis = null;
146         InputStreamReader isr = null;
147         BufferedReader br = null;
148         FileOutputStream fos  = null;
149         PrintWriter pw = null;
150         try {
151             File file = new File(filepath);//文件路径(包括文件名称)
152             //将文件读入输入流
153             fis = new FileInputStream(file);
154             isr = new InputStreamReader(fis);
155             br = new BufferedReader(isr);
156             StringBuffer buffer = new StringBuffer();
157             
158             //文件原有内容
159             for(int i=0;(temp =br.readLine())!=null;i++){
160                 buffer.append(temp);
161                 // 行与行之间的分隔符 相当于“\n”
162                 buffer = buffer.append(System.getProperty("line.separator"));
163             }
164             buffer.append(filein);
165             
166             fos = new FileOutputStream(file);
167             pw = new PrintWriter(fos);
168             pw.write(buffer.toString().toCharArray());
169             pw.flush();
170             bool = true;
171         } catch (Exception e) {
172             // TODO: handle exception
173             e.printStackTrace();
174         }finally {
175             //不要忘记关闭
176             if (pw != null) {
177                 pw.close();
178             }
179             if (fos != null) {
180                 fos.close();
181             }
182             if (br != null) {
183                 br.close();
184             }
185             if (isr != null) {
186                 isr.close();
187             }
188             if (fis != null) {
189                 fis.close();
190             }
191         }
192         return bool;
193     }
194  
195     /**
196      * 删除文件
197      * @param fileName 文件名称
198      * @return
199      */
200     public static boolean delFile(String fileName){
201         boolean bool = false;
202         File file  = new File(fileName);
203         try {
204             if(file.exists()){
205                 file.delete();
206                 bool = true;
207             }
208         } catch (Exception e) {
209             // TODO: handle exception
210         }
211         return bool;
212     }
213     public static String readFile(String fileName) {
214         File file = new File(fileName);
215         Reader reader = null;
216         String content=new String();
217         try {
218             // 一次读一个字符
219             reader = new InputStreamReader(new FileInputStream(file));
220             int tempchar;
221             while ((tempchar = reader.read()) != -1) {
222                 // 对于windows下,\r\n这两个字符在一起时,表示一个换行。
223                 // 但如果这两个字符分开显示时,会换两次行。
224                 // 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。
225                 if (((char) tempchar) != '\r') {
226                  //   System.out.print((char) tempchar);
227                     content+=((char) tempchar);
228                 }
229             }
230             reader.close();
231         } catch (Exception e) {
232             e.printStackTrace();
233         }
234         return content;
235     }
236 }

 

推荐阅读