-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRowReduce.java
More file actions
97 lines (93 loc) · 1.97 KB
/
RowReduce.java
File metadata and controls
97 lines (93 loc) · 1.97 KB
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
import java.util.*;
public class RowReduce
{
//data
Scanner sc=new Scanner(System.in);
double [][] arr;
int n;
//constructor
public RowReduce()
{
System.out.println("Please state the number of rows your array will contain:");
int n= sc.nextInt();
arr = new double[n][n];
this.setValues();
}
//method
//this method will print the matrix
public void print()
{
for(int i =0;i<arr.length;i++)
{
for(int j =0; j<arr.length;j++)
{
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
//this method will take in the values of the array
public void setValues()
{
System.out.println("State the values of your coefficients and then the value at the end.");
System.out.println("For example if your equation is 3x +4y+7z=9 then you will input 3 and then 4 and then 7 and finally 9, repeat for all your equations.");
for(int i=0;i<arr.length;i++)
{
for(int j=0;j<arr[0].length;j++)
{
arr[i][j]= sc.nextDouble();
}
}
}
//the method for making the array for which rows will be changed
public void reducedRow()
{
int pointer = 0;
while(pointer<arr.length)
{
boolean h = true;
if(arr[pointer][pointer]!=1)
{
for(int j=0;j<arr.length;j++)
{
arr[pointer][j] /= arr[pointer][pointer];
}
h=false;
}
if(arr[pointer][pointer]==1 || h)
{
int i = pointer;
if(pointer==0 || i<arr.length)
{
for(int l=pointer+1;l<arr.length;l++)
{
double x = (-1*arr[l][pointer])/arr[pointer][pointer];
for(int k=0;k<arr.length;k++)
{
arr[l][k] += arr[k][pointer] * x;
}
}
}
if(i>=0)
{
for(int l=pointer-1;l>0;l--)
{
double x = (-1*arr[l][pointer])/arr[pointer][pointer];
for(int k=0;k<arr.length;k++)
{
arr[l][k] += arr[k][pointer] * x;
}
}
}
}
pointer++;
}
this.print();
}
//main
public static void main(String ars[])
{
RowReduce rr = new RowReduce();
rr.reducedRow();
}
}