This repository was archived by the owner on Mar 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMathVector.java
233 lines (203 loc) · 8.27 KB
/
MathVector.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package assign01;
/**
* This class represents a simple row or column vector of numbers. In a row
* vector, the numbers are written horizontally (i.e., along the columns). In a
* column vector, the numbers are written vertically (i.e., along the rows).
*
* @author Erin Parker & Nils Streedain
* @version January 20, 2021
*/
public class MathVector {
// 2D array to hold the numbers of the vector, either along the columns or rows
private double[][] data;
// set to true for a row vector and false for a column vector
private boolean isRowVector;
// count of all elements in the vector
private int vectorSize;
/**
* Creates a new row or column vector. For a row vector, the input array is
* expected to have 1 row and a positive number of columns, and this number of
* columns represents the vector's length. For a column vector, the input array
* is expected to have 1 column and a positive number of rows, and this number
* of rows represents the vector's length.
*
* @param data - a 2D array to hold the numbers of the vector
* @throws IllegalArgumentException if the numbers of rows and columns in the
* input 2D array is not compatible with a row
* or column vector
*/
public MathVector(double[][] data) {
if (data.length == 0)
throw new IllegalArgumentException("Number of rows must be positive.");
if (data[0].length == 0)
throw new IllegalArgumentException("Number of columns must be positive.");
if (data.length == 1) {
// This is a row vector with length = number of columns.
this.isRowVector = true;
this.vectorSize = data[0].length;
} else if (data[0].length == 1) {
// This is a column vector with length = number of rows.
this.isRowVector = false;
this.vectorSize = data.length;
} else
throw new IllegalArgumentException("Either the number of rows or the number of columns must be 1.");
// Create the array and copy data over.
if (this.isRowVector)
this.data = new double[1][vectorSize];
else
this.data = new double[vectorSize][1];
for (int i = 0; i < this.data.length; i++) {
for (int j = 0; j < this.data[0].length; j++) {
this.data[i][j] = data[i][j];
}
}
}
/**
* Determines whether this vector is "equal to" another vector, where equality
* is defined as both vectors being row (or both being column), having the same
* vector length, and containing the same numbers in the same positions.
*
* @param other - another vector to compare
*/
public boolean equals(Object other) {
if (!(other instanceof MathVector))
return false;
MathVector otherVec = (MathVector) other;
// Checks that each value of otherVec.data is equal to each value of this.data
for (int i = 0; i < otherVec.data.length; i++) {
for (int j = 0; j < otherVec.data[0].length; j++) {
if (!(this.data[i][j] == otherVec.data[i][j])) {
return false;
}
}
}
return true;
}
/**
* Generates a returns a new vector that is the transposed version of this
* vector.
*/
public MathVector transpose() {
// Creates a new vector with swapped dimensions of the original vector
MathVector newVector = new MathVector(new double[this.data[0].length][this.data.length]);
// Iterates over the original vector length in both dimensions and adds each
// value to newVector with the dimensions swapped
for (int i = 0; i < this.data.length; i++) {
for (int j = 0; j < this.data[0].length; j++) {
newVector.data[j][i] = this.data[i][j];
}
}
return newVector;
}
/**
* Generates and returns a new vector representing the sum of this vector and
* another vector.
*
* @param other - another vector to be added to this vector
* @throws IllegalArgumentException if the other vector and this vector are not
* both row vectors of the same length or
* column vectors of the same length
*/
public MathVector add(MathVector other) {
// Checks to make sure vectors are the same dimensions which by definition also
// is checking whether they are both row or column vectors
if (this.data.length != other.data.length || this.data[0].length != other.data[0].length)
throw new IllegalArgumentException("Only vectors of the same dimmensions may be added together.");
// Creates a new vector with swapped dimensions of the original vector
MathVector newVector = new MathVector(new double[this.data.length][this.data[0].length]);
// Iterates over the original vector length in both dimensions and adds each
// value of the original vector and the other vector to newVector
for (int i = 0; i < this.data.length; i++) {
for (int j = 0; j < this.data[0].length; j++) {
newVector.data[i][j] = this.data[i][j] + other.data[i][j];
}
}
return newVector;
}
/**
* Computes and returns the dot product of this vector and another vector.
*
* @param other - another vector to be combined with this vector to produce the
* dot product
* @throws IllegalArgumentException if the other vector and this vector are not
* both row vectors of the same length or
* column vectors of the same length
*/
public double dotProduct(MathVector other) {
// Checks to make sure vectors are the same dimensions which by definition also
// is checking whether they are both row or column vectors
if (this.data.length != other.data.length || this.data[0].length != other.data[0].length)
throw new IllegalArgumentException("Only vectors of the same dimmensions may be added together.");
// Creates a dotProduct variable to be added to
double dotProduct = 0;
// Iterates over the original vector length in both dimensions and adds each
// value of the original vector multiplied by the other vector to dotProduct
for (int i = 0; i < this.data.length; i++) {
for (int j = 0; j < this.data[0].length; j++) {
dotProduct += this.data[i][j] * other.data[i][j];
}
}
return dotProduct;
}
/**
* Computes and returns this vector's magnitude (also known as a vector's
* length) .
*/
public double magnitude() {
// Creates a temporary value to be added to
double tempValue = 0;
// Iterates over the original vector length in both dimensions and adds each
// value of the original vector squared to tempValue
for (int i = 0; i < this.data.length; i++) {
for (int j = 0; j < this.data[0].length; j++) {
tempValue += Math.pow(this.data[i][j], 2);
}
}
// Returns the square root of tempValue, completing the magnitude equation
return Math.sqrt(tempValue);
}
/**
* Generates and returns a normalized version of this vector.
*/
public MathVector normalize() {
// Creates a new vector
MathVector newVector = new MathVector(new double[this.data.length][this.data[0].length]);
// Calculates and stores the magnitude of the original vector
double magnitude = this.magnitude();
// Iterates over the original vector length in both dimensions and adds each
// value of the original vector divided by the magnitude to newVector
for (int i = 0; i < this.data.length; i++) {
for (int j = 0; j < this.data[0].length; j++) {
newVector.data[i][j] = this.data[i][j] / magnitude;
}
}
return newVector;
}
/**
* Generates and returns a textual representation of this vector. For example,
* "1.0 2.0 3.0 4.0 5.0" for a sample row vector of length 5 and "1.0 2.0 3.0
* 4.0 5.0" for a sample column vector of length 5. In both cases, notice the
* lack of a newline or space after the last number.
*/
public String toString() {
// Creates a string to add values to so they can be printed
String returnString = "";
// Iterates over all values in a vector of any dimensions and adds each value to
// a string spread out by spaces for values on the same row and lines for values
// on different rows. The last value on a row or column is not followed by a
// respective space or line.
for (int i = 0; i < this.data.length; i++) {
if (i == this.data.length - 1) {
for (int j = 0; j < this.data[0].length; j++) {
if (j < this.data[0].length - 1)
returnString += this.data[i][j] + " ";
else
returnString += this.data[i][j];
}
} else {
returnString += this.data[i][this.data[0].length - 1] + "\n";
}
}
return returnString;
}
}