-
Notifications
You must be signed in to change notification settings - Fork 0
/
Row
146 lines (142 loc) · 3.9 KB
/
Row
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package MatrixRowReducer;
public class Row{
int col;
Fraction [] fractions;
// Constructs a row given an array of fractions
public Row (Fraction [] list){
//System.out.println("New row of created using a list of fractions");
fractions=list;
col=list.length;
}
// Constructs a row given the length of the row.
//Each fraction will default to 0/1
public Row (int length)
{
//System.out.println("New row of zeros created using the int: "+length);
col=length;
fractions=new Fraction [col];
}
// Creates a row with only one entry, the 0/1 fraction.
public Row ()
{
//System.out.println("New row of zeros created using nothing");
col=1;
fractions=new Fraction [1];
fractions[0]=new Fraction();
}
// Creates a row of length columns that is full zeros except
//for the chosen location which has the given value.
//This is useful for creation of scaler multiples of the identity matrix.
public Row (int location, Fraction value, int length)
{
//System.out.println("New row of zeros and 1 value created using the location int: "+location+"the fraction:"+value.getNumerator()+"/"+value.getDenominator()+"and the length int"+length);
col=length;
fractions=new Fraction [length];
for (int i=0;i<length;i++)
{
fractions[i]=new Fraction();
}
fractions[location]=value;
}
//Adds another row to the row in question.
public Row subtractRows(Row a)
{
Row c=new Row(a.col);
if (a.col==this.col)
{
for (int i=0;i<col;i++)
{
//System.out.println("index"+i);
//System.out.println("subtracting"+a.fractions[i].getNumerator()+"/"+a.fractions[i].getDenominator()+" from "+ this.fractions[i].getNumerator()+"/"+this.fractions[i].getDenominator());
Fraction temp=a.fractions[i].negativeFraction();
c.fractions[i]=Fraction.addFraction(this.fractions[i],temp);
}
}
return c;
}
public Row addRows(Row a)
{
Row c=new Row(a.col);
if (a.col==this.col)
{
for (int i=0;i<col;i++)
{
c.fractions[i]=Fraction.addFraction(this.fractions[i], a.fractions[i]);
}
}
return c;
}
//Multiplies the row in question by a constant.
public Row multiplyRows(Fraction a)
{
Row c=new Row(this.col);
for (int i=0;i<this.col; i++)
{
c.fractions[i]=Fraction.multiplyFraction(this.fractions[i], a);
}
return c;
}
// This methods returns true if the entire row is made up of zeros, false otherwise
public boolean allZero()
{
for (int i=0;i<col; i++)
{
if (fractions[i].getNumerator()!=0)
{
return false;
}
}
return true;
}
/* This next method is critical. First it finds the first non zero location in the row
* It stores that location as i then returns it.
*/
public int leadLocation()
{
boolean nonZero=false;
int i=0;
while (nonZero)
{ nonZero=(fractions[i].getNumerator()==0);
if (!nonZero)
{
i++;
/* The highest index value is at column number-1.
*If i equals col (column number), none of the index values had none zero.
*value hence a row of all zeros.
*/
if (i>=col)
{
System.out.println("Row of zeros");
/*This stops the program from testing impossible index values.
* All zero rows will have an imaginary leading 1 at col
* (which is not an actual index location) for calculation purposes.
* Specifically when sorting rows this imaginary index value will makes sure
* all zero rows are brought to the bottom.
*/
return i;
}
}
}
return i;
}
/*
* This gets the location of the leading variable from the previous method.
* Then it multiplies the entire row by the reciprocal of the fraction at i.
* This changes the row to have a leading 1.
* IT also returns i, returning the location of the leading 1.
*/
public int reducedRow()
{
int i=leadLocation();
Row temp=this.multiplyRows(this.fractions[i].reciprocal());
this.fractions=temp.fractions;
return i;
}
public void printRow()
{
for (int i=0; i<col;i++)
{
fractions[i].printFraction();
}
}
}