repl.it
@glennyisbae/

Determinant

Java

No description

fork
loading
Files
  • Main.java
  • jdt.ls-java-project
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import java.util.Scanner;
import java.util.*;

class Main {
  public static void main(String[] args) {
    
    Scanner MFDoom= new Scanner(System.in);
	System.out.println("How wide is the matrix?");
	int size= (MFDoom.nextInt());
	Double a = 0.0;
	ArrayList<Double> flerp = new ArrayList<Double>();
	System.out.print("What is the value of ");
	for (int i=1; i<=size; i++){
		for (int j=1; j<=size; j++){
			System.out.println("a"+i+j);
			
			flerp.add(MFDoom.nextDouble());
		}
	}
	System.out.println("");
	int p=0;
	
	for (int i=0; i<size; i++){
		System.out.print("| \t");
		
		for (int j=0; j<size; j++){
			
			System.out.print(flerp.get(p).intValue()+"\t");
			p++;
		}
		System.out.println("\t|");
	}
	System.out.println(""); 
	System.out.println("The determinant is: "+solve(flerp)+"  <3");
	//eigen(flerp);
	//multimat()
	
   
  }
  
  public static double solve(ArrayList<Double> x){
	  double determinant=0;
	  
	  if (x.size()==4){
	  	determinant+= (x.get(0)*x.get(3)-(x.get(1)*x.get(2)));
	  }
	  else if (x.size()<=3){
		  determinant+=0;
	  }
	  else if (x.size()>4){
	  	for (int i=0; i<Math.sqrt(x.size()); i++){
			  if (i%2==0)
				determinant+= (x.get(i)*solve(smaller(x,i)));

			  else
			  	determinant+= (-1*x.get(i)*solve(smaller(x,i)));
		  }
	  }
	  return determinant;
  }
  private static ArrayList<Double> smaller(ArrayList<Double> x, double i){
	  ArrayList<Double> jill = new ArrayList<Double>();
	  for (int k=0; k<x.size(); k++){
		jill.add(x.get(k));
	  }
	  for (int j=(int)i; j<x.size(); j+=(int)Math.sqrt(x.size())){  
		  jill.set(j,null);
	  }
	  for (int j=0; j<(int)Math.sqrt(x.size()); j++){
		  jill.set(j,null);
	  }
	  jill.removeIf(n -> n==null);
	  return jill;
  }

  /*private static void eigen(ArrayList<Double> p){
	  ArrayList<Double> kill = new ArrayList<Double>(p.size());
	  ArrayList<Double> chill = new ArrayList<Double>();
	  ArrayList<Double> dill = new ArrayList<Double>();
	  if (solve(p)==0)
	  System.out.println("MATRIX DOES NOT HAVE EIGENVALUES");
		else{  
		  for (double i=-50.0; i<=50.0; i+=1.0){
			  if (i==-50){
				for (int k=0; k<p.size(); k++){
					kill.add(p.get(k));
	  			}}
			  else{	
				for (int k=0; k<p.size(); k++){
					kill.set(k,(p.get(k)));
				}}
				for (int a=0; a<p.size(); a+=(int)Math.sqrt(p.size())+1){
					kill.set(a,p.get(a)-i);
				}
				if (solve(kill)==0){
				System.out.println("An Eigenvalue of The Matrix is "+ i);
				chill.add(i);
				}
		  }}
	  }
  //private static void multimat(ArrayList<Double> a, ArrayList<Double> b){
	//  double tempsum=0;
	  //for (int i=0; i<a.size();)
  }*/
  }
  



?