-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVectorSet.java
More file actions
205 lines (189 loc) · 7.08 KB
/
Copy pathVectorSet.java
File metadata and controls
205 lines (189 loc) · 7.08 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package linAlgCalc;
import java.util.Arrays;
import java.util.Scanner;
/**
* Sets of vectors that operations are performed on in order to answer linear algebra questions.
*
* @author AOsterndorff
*
*/
public class VectorSet {
private String[][] vectorSetArr;
private int numRows;
private int numColumns;
private VectorSetOperator operator = new VectorSetOperator();
private PrintStringBuilder printer = new PrintStringBuilder();
public VectorSet(Scanner input) {
setVectorCountAndLength(input);
vectorSetArr = setVectorSetArr(input);
}
/**
* Prompts the user to set the number of vectors and the length of those vectors.
*
* @param input The scanner that obtains the user's input.
*/
private void setVectorCountAndLength(Scanner input) {
System.out.println("How many vectors do you wish to enter into this set?");
while (!input.hasNextInt()) {
input.next();
System.out.println("Please enter a positive integer");
}
numColumns = Math.abs(input.nextInt());
numColumns = numColumns < 1 ? numColumns + 1 : numColumns;
System.out.println("How many elements will a vector in this set contain?");
while (!input.hasNextInt()) {
input.next();
System.out.println("Please enter a positive integer");
}
numRows = Math.abs(input.nextInt());
numRows = numRows < 1 ? numRows + 1 : numRows;
input.nextLine();
}
/**
* Prompts the user to enter each element of each vector, one vector at a time. The vectors are
* entered into an array as columns would be in a matrix, and are generally treated as such.
*
* @param input The scanner that obtains the user's input.
* @return vectorArr The two dimensional array containing the vectors.
*/
private String[][] setVectorSetArr(Scanner input) {
String[][] vectorSetArr = new String[numRows][numColumns];
for (int i = 0; i < vectorSetArr.length; ++i) {
Arrays.fill(vectorSetArr[i], "_");
}
boolean isValidEntry = true;
for (int j = 0; j < numColumns; ++j) {
for (int i = 0; i < numRows; ++i) {
System.out.println("Enter Element" + (i + 1) + " , of Vector" + (j + 1));
vectorSetArr[i][j] = input.nextLine().trim();
isValidEntry = operator.checkElementValidity(vectorSetArr[i][j]);
if (!isValidEntry) {
System.out.println("Invalid: Element must be a whole number or a fraction");
--j;
}
else {
printer.printVectorSet(vectorSetArr);
}
}
}
System.out.println("The set of vectors is now created.\n");
return vectorSetArr;
}
/**
* Returns the two dimensional array containing the vector set.
*
* @return
*/
public String[][] getVectorSetArr() {
return vectorSetArr;
}
/**
* Outputs whether or not a vector falls within the span of a set of vectors.
*
* @param input The scanner that obtains the user's input.
*/
public void getVectorWithinSpan(Scanner input) {
boolean withinSpan = operator.getIsVectorInSpan(vectorSetArr, input);
if (withinSpan) {
System.out.println("This vector is with the span of the set");
}
else {
System.out.println("This vector is not with the span of the set");
}
}
/**
* Outputs a vector transformed with respect to a given basis set of vectors.
*
* @param input The scanner that obtains the user's input.
*/
public void getVrespectS(Scanner input) {
String[] vWithRespectToS = operator.getVRespectS(vectorSetArr, input);
if ( vWithRespectToS != null) {
System.out.println("The vector with respect to the basis set is:");
printer.printVector(vWithRespectToS);
}
else {
System.out.println("Error: This option requires a set containing only basis "
+ "vectors.");
}
}
/**
* Outputs whether or not the set is linearly independent to the user.
*
*
*/
public void getLinearDependence() {
operator.getLinearDependence(vectorSetArr, true);
}
/**
* Outputs whether or not a set of vectors forms a basis for R^n.
*/
public void getIsBasisForRn() {
boolean isBasis = operator.getIsBasisForRn(vectorSetArr, true);
if (isBasis) {
System.out.println("This set is a basis for R" + numRows);
}
else {
System.out.println("This set is not a basis for R" + numRows);
}
}
/**
* Outputs the set transformed into an orthogonal basis to the user.
*/
public void getOrthogonalBasis() {
String[][] orthogonalBasis = operator.applyGramSchmidt(vectorSetArr);
System.out.println("This set was transformed into the orthogonal basis below:");
printer.printVectorSet(orthogonalBasis);
}
/**
* Outputs the orthogonal complement of the span of the set of vectors.
*
* @param vectorSetArr The set spanning the subspace the orthogonal complement is of.
*/
public void getOrthogonalComplement(String[][] vectorSetArr) {
String[][] transpose = operator.getTranspose(vectorSetArr);
String[][] orthogonalComplement = operator.getNullSpace(transpose, false);
if (orthogonalComplement != null) {
System.out.println("The orthogonal complement is:");
operator.getNullSpace(transpose, true);
}
else {
System.out.println("The orthogonal complement is {0}\n");
}
}
/**
* Outputs the transition matrix from the first basis set of vectors to the second basis set.
* Only produces a transition matrix if both sets of vectors form bases.
*
* @param input The scanner that obtains the user's input.
*/
public void getTransitionMatrix(Scanner input) {
VectorSet vectorSet2 = new VectorSet(input);
//check both sets to ensure they are the same size
if (vectorSetArr.length != vectorSet2.getVectorSetArr().length
|| vectorSetArr[0].length != vectorSet2.getVectorSetArr()[0].length) {
System.out.println("Error: Both sets must have the same dimensions and number of"
+ " vectors");
return;
}
//check both sets of vectors to ensure that they both form bases
if (!operator.getIsBasisForRn(vectorSetArr, false)
|| !operator.getIsBasisForRn(vectorSet2.getVectorSetArr(), false)) {
System.out.println("Error: Both sets of vectors must form bases");
return;
}
int changeOfBasisRows = vectorSet2.getVectorSetArr().length;
int changeOfBasisCols = vectorSet2.getVectorSetArr()[0].length;
String[][] transitionMatrix = new String[changeOfBasisRows][changeOfBasisCols];
transitionMatrix = operator.getTransitionMatrix(vectorSetArr, vectorSet2.getVectorSetArr());
System.out.println("The tranisiton matrix from the first basis to the second is:");
printer.printMatrix(transitionMatrix, changeOfBasisCols);
}
/**
* Prints the vector set to the console.
*/
public void print() {
System.out.println("Your current set of vectors:");
printer.printVectorSet(vectorSetArr);
}
}