-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoubleVector.java
More file actions
259 lines (198 loc) · 5.67 KB
/
DoubleVector.java
File metadata and controls
259 lines (198 loc) · 5.67 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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/**
*
* @author Alex Weeks
*
*/
public class DoubleVector {
protected int order;
protected double[] comps;
/**
* Creates a new DoubleVector with specified order
* @param order
*/
public DoubleVector( int order ){
this.order = order;
}
/**
* Creates a new DoubleVector with specified components
* @param initialComps
*/
public DoubleVector( double[] initialComps ) {
this.comps = initialComps.clone();
this.order = comps.length;
}
/**
* @return Returns a string representation of the vector
*/
public String toString() {
StringBuffer result = new StringBuffer("{ ");
for ( double comp : this.comps ) {
result.append( comp + ", ");
}
result.delete( result.length() - 2 , result.length());
result.append(" }");
return result.toString();
}
/**
*
* @param index
* @return Returns the vector component at the index specified
*/
public double getComp( int index ) {
return this.comps[index];
}
/**
* Sets the vector component specified by index to the given value
* @param index
* @param value Value to set
*/
public void setComp( int index, double value ) {
this.comps[index] = value;
}
/**
* Adds DoubleVectors
* @param other DoubleVector to add. Must be of the same order.
* @return Returns a new DoubleVector who's components are the sum of this vector's components, and the other vector's components.
* @throws Throws IllegalArgumentException if the orders do not match.
*/
public DoubleVector add( DoubleVector other ) {
DoubleVector result = new DoubleVector( this.comps );
result.addCompsTo( other.comps );
return result;
}
/**
*
* @param comps Components to add
* @return Returns a new DoubleVector who's components are the sum of this vector's components, and the given components.
*/
public DoubleVector addComps( double[] comps ) {
DoubleVector result = new DoubleVector( this.comps );
result.addCompsTo( comps );
return result;
}
/**
* Adds the components of other to this vector's components. The vector's orders must match.
* @param other Vector to add
* @throws Throws IllegalArgumentException if the orders do not match.
*
*/
public void addTo( DoubleVector other ) {
this.addCompsTo( other.comps );
}
/**
* Adds the given components to this vector's components
* @param comps Components to add
* @throws IllegalArgumentException if the orders do not match.
*/
public void addCompsTo( double[] comps ) {
if( this.order != comps.length ) {
throw new IllegalArgumentException("Orders do not match");
}
for( int n = 0; n < this.order; n++ ) {
this.comps[n] = this.comps[n] + comps[n];
}
}
/**
* Subtracts vectors
* @param other Vector to subtract
* @return Returns a new DoubleVector who's components are this vector's components minus the other vector's components
*/
public DoubleVector subtract( DoubleVector other ) {
DoubleVector result = new DoubleVector( this.comps );
result.subtractCompsTo( other.comps );
return result;
}
/**
* Subtracts vectors
* @param comps Components to subtract
* @return Returns a new DoubleVector who's components are this vector's components minus the given components
*/
public DoubleVector subtractComps( double[] comps ) {
DoubleVector result = new DoubleVector( this.comps );
result.subtractCompsTo( comps );
return result;
}
/**
* Subtracts the other vectors components from this vector's components
* @param other Vector to subtract
*/
public void subtractTo( DoubleVector other ) {
this.subtractCompsTo( other.comps );
}
/**
* Subtracts components from this vector's components
* @param comps Components to subtract
*/
public void subtractCompsTo( double[] comps ) {
if( this.order != comps.length ) {
throw new IllegalArgumentException("Orders do not match");
}
for( int n = 0; n < this.order; n++ ) {
this.comps[n] = this.comps[n] - comps[n];
}
}
/**
*
* @param scalar Scalar multiple
* @return Returns a new DoubleVector of same order as this DoubleVector who's components are the scalar product of this and scalar.
*/
public DoubleVector scalarMult ( double scalar ) {
DoubleVector result = new DoubleVector( this.comps );
result.scalarMultTo(scalar);
return result;
}
/**
* Multiples each component of this vector by the scalar.
* @param scalar
*/
public void scalarMultTo ( double scalar ) {
for( int n = 0; n < this.order; n++ ) {
this.comps[n] *= scalar;
}
}
/**
*
* @return Returns the magnitude of the vector.
*/
public double magnitude() {
return Math.sqrt( this.squareSumOfComps() );
}
/**
*
* @return Returns the sum of the components squared. Useful in place of the magnitude in many equations when you wish to avoid using a square root.
*/
public double squareSumOfComps() {
double result = 0;
for ( int n = 0; n < this.comps.length; n++ ) {
result += this.comps[n] * this.comps[n];
}
return result;
}
/**
*
* @return Returns a new DoubleVector of magnitude 1 in the direction of this vector.
*/
public DoubleVector unitVector() {
DoubleVector result = new DoubleVector(this.comps);
result.scalarMultTo( 1d / result.magnitude() );
return result;
}
/**
*
* @param vec1 First vector
* @param vec2 Second vector
* @return Returns the dot product of vec1 and vec2
* @throws Throws IllegalArgumentException if the orders do not match.
*
*/
public static double dotProduct ( DoubleVector vec1, DoubleVector vec2 ) {
if( vec1.order != vec2.order ) {
throw new IllegalArgumentException("Orders do not match");
}
double result = 0;
for ( int n = 0; n < vec1.order; n++ ) {
result += vec1.comps[n] * vec2.comps[n];
}
return result;
}
}